2 * Copyright (C) 2005 Junio C Hamano
5 #define USE_THE_REPOSITORY_VARIABLE
6 #define DISABLE_SIGN_COMPARE_WARNINGS
8 #include "git-compat-util.h"
13 #include "environment.h"
22 #include "xdiff-interface.h"
24 #include "run-command.h"
26 #include "object-store-ll.h"
28 #include "submodule.h"
32 #include "string-list.h"
34 #include "tmp-objdir.h"
36 #include "oid-array.h"
39 #include "parse-options.h"
41 #include "promisor-remote.h"
43 #include "object-file.h"
44 #include "object-name.h"
45 #include "read-cache-ll.h"
50 #ifdef NO_FAST_WORKING_DIRECTORY
51 #define FAST_WORKING_DIRECTORY 0
53 #define FAST_WORKING_DIRECTORY 1
56 static int diff_detect_rename_default
;
57 static int diff_indent_heuristic
= 1;
58 static int diff_rename_limit_default
= 1000;
59 static int diff_suppress_blank_empty
;
60 static int diff_use_color_default
= -1;
61 static int diff_color_moved_default
;
62 static int diff_color_moved_ws_default
;
63 static int diff_context_default
= 3;
64 static int diff_interhunk_context_default
;
65 static char *diff_word_regex_cfg
;
66 static struct external_diff external_diff_cfg
;
67 static char *diff_order_file_cfg
;
68 int diff_auto_refresh_index
= 1;
69 static int diff_mnemonic_prefix
;
70 static int diff_no_prefix
;
71 static char *diff_src_prefix
;
72 static char *diff_dst_prefix
;
73 static int diff_relative
;
74 static int diff_stat_name_width
;
75 static int diff_stat_graph_width
;
76 static int diff_dirstat_permille_default
= 30;
77 static struct diff_options default_diff_options
;
78 static long diff_algorithm
;
79 static unsigned ws_error_highlight_default
= WSEH_NEW
;
81 static char diff_colors
[][COLOR_MAXLEN
] = {
83 GIT_COLOR_NORMAL
, /* CONTEXT */
84 GIT_COLOR_BOLD
, /* METAINFO */
85 GIT_COLOR_CYAN
, /* FRAGINFO */
86 GIT_COLOR_RED
, /* OLD */
87 GIT_COLOR_GREEN
, /* NEW */
88 GIT_COLOR_YELLOW
, /* COMMIT */
89 GIT_COLOR_BG_RED
, /* WHITESPACE */
90 GIT_COLOR_NORMAL
, /* FUNCINFO */
91 GIT_COLOR_BOLD_MAGENTA
, /* OLD_MOVED */
92 GIT_COLOR_BOLD_BLUE
, /* OLD_MOVED ALTERNATIVE */
93 GIT_COLOR_FAINT
, /* OLD_MOVED_DIM */
94 GIT_COLOR_FAINT_ITALIC
, /* OLD_MOVED_ALTERNATIVE_DIM */
95 GIT_COLOR_BOLD_CYAN
, /* NEW_MOVED */
96 GIT_COLOR_BOLD_YELLOW
, /* NEW_MOVED ALTERNATIVE */
97 GIT_COLOR_FAINT
, /* NEW_MOVED_DIM */
98 GIT_COLOR_FAINT_ITALIC
, /* NEW_MOVED_ALTERNATIVE_DIM */
99 GIT_COLOR_FAINT
, /* CONTEXT_DIM */
100 GIT_COLOR_FAINT_RED
, /* OLD_DIM */
101 GIT_COLOR_FAINT_GREEN
, /* NEW_DIM */
102 GIT_COLOR_BOLD
, /* CONTEXT_BOLD */
103 GIT_COLOR_BOLD_RED
, /* OLD_BOLD */
104 GIT_COLOR_BOLD_GREEN
, /* NEW_BOLD */
107 static const char *color_diff_slots
[] = {
108 [DIFF_CONTEXT
] = "context",
109 [DIFF_METAINFO
] = "meta",
110 [DIFF_FRAGINFO
] = "frag",
111 [DIFF_FILE_OLD
] = "old",
112 [DIFF_FILE_NEW
] = "new",
113 [DIFF_COMMIT
] = "commit",
114 [DIFF_WHITESPACE
] = "whitespace",
115 [DIFF_FUNCINFO
] = "func",
116 [DIFF_FILE_OLD_MOVED
] = "oldMoved",
117 [DIFF_FILE_OLD_MOVED_ALT
] = "oldMovedAlternative",
118 [DIFF_FILE_OLD_MOVED_DIM
] = "oldMovedDimmed",
119 [DIFF_FILE_OLD_MOVED_ALT_DIM
] = "oldMovedAlternativeDimmed",
120 [DIFF_FILE_NEW_MOVED
] = "newMoved",
121 [DIFF_FILE_NEW_MOVED_ALT
] = "newMovedAlternative",
122 [DIFF_FILE_NEW_MOVED_DIM
] = "newMovedDimmed",
123 [DIFF_FILE_NEW_MOVED_ALT_DIM
] = "newMovedAlternativeDimmed",
124 [DIFF_CONTEXT_DIM
] = "contextDimmed",
125 [DIFF_FILE_OLD_DIM
] = "oldDimmed",
126 [DIFF_FILE_NEW_DIM
] = "newDimmed",
127 [DIFF_CONTEXT_BOLD
] = "contextBold",
128 [DIFF_FILE_OLD_BOLD
] = "oldBold",
129 [DIFF_FILE_NEW_BOLD
] = "newBold",
132 define_list_config_array_extra(color_diff_slots
, {"plain"});
134 static int parse_diff_color_slot(const char *var
)
136 if (!strcasecmp(var
, "plain"))
138 return LOOKUP_CONFIG(color_diff_slots
, var
);
141 static int parse_dirstat_params(struct diff_options
*options
, const char *params_string
,
142 struct strbuf
*errmsg
)
144 char *params_copy
= xstrdup(params_string
);
145 struct string_list params
= STRING_LIST_INIT_NODUP
;
150 string_list_split_in_place(¶ms
, params_copy
, ",", -1);
151 for (i
= 0; i
< params
.nr
; i
++) {
152 const char *p
= params
.items
[i
].string
;
153 if (!strcmp(p
, "changes")) {
154 options
->flags
.dirstat_by_line
= 0;
155 options
->flags
.dirstat_by_file
= 0;
156 } else if (!strcmp(p
, "lines")) {
157 options
->flags
.dirstat_by_line
= 1;
158 options
->flags
.dirstat_by_file
= 0;
159 } else if (!strcmp(p
, "files")) {
160 options
->flags
.dirstat_by_line
= 0;
161 options
->flags
.dirstat_by_file
= 1;
162 } else if (!strcmp(p
, "noncumulative")) {
163 options
->flags
.dirstat_cumulative
= 0;
164 } else if (!strcmp(p
, "cumulative")) {
165 options
->flags
.dirstat_cumulative
= 1;
166 } else if (isdigit(*p
)) {
168 int permille
= strtoul(p
, &end
, 10) * 10;
169 if (*end
== '.' && isdigit(*++end
)) {
170 /* only use first digit */
171 permille
+= *end
- '0';
172 /* .. and ignore any further digits */
173 while (isdigit(*++end
))
177 options
->dirstat_permille
= permille
;
179 strbuf_addf(errmsg
, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
184 strbuf_addf(errmsg
, _(" Unknown dirstat parameter '%s'\n"), p
);
189 string_list_clear(¶ms
, 0);
194 static int parse_submodule_params(struct diff_options
*options
, const char *value
)
196 if (!strcmp(value
, "log"))
197 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
198 else if (!strcmp(value
, "short"))
199 options
->submodule_format
= DIFF_SUBMODULE_SHORT
;
200 else if (!strcmp(value
, "diff"))
201 options
->submodule_format
= DIFF_SUBMODULE_INLINE_DIFF
;
203 * Please update $__git_diff_submodule_formats in
204 * git-completion.bash when you add new formats.
211 int git_config_rename(const char *var
, const char *value
)
214 return DIFF_DETECT_RENAME
;
215 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
216 return DIFF_DETECT_COPY
;
217 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
220 long parse_algorithm_value(const char *value
)
224 else if (!strcasecmp(value
, "myers") || !strcasecmp(value
, "default"))
226 else if (!strcasecmp(value
, "minimal"))
227 return XDF_NEED_MINIMAL
;
228 else if (!strcasecmp(value
, "patience"))
229 return XDF_PATIENCE_DIFF
;
230 else if (!strcasecmp(value
, "histogram"))
231 return XDF_HISTOGRAM_DIFF
;
233 * Please update $__git_diff_algorithms in git-completion.bash
234 * when you add new algorithms.
239 static int parse_one_token(const char **arg
, const char *token
)
242 if (skip_prefix(*arg
, token
, &rest
) && (!*rest
|| *rest
== ',')) {
249 static int parse_ws_error_highlight(const char *arg
)
251 const char *orig_arg
= arg
;
255 if (parse_one_token(&arg
, "none"))
257 else if (parse_one_token(&arg
, "default"))
259 else if (parse_one_token(&arg
, "all"))
260 val
= WSEH_NEW
| WSEH_OLD
| WSEH_CONTEXT
;
261 else if (parse_one_token(&arg
, "new"))
263 else if (parse_one_token(&arg
, "old"))
265 else if (parse_one_token(&arg
, "context"))
268 return -1 - (int)(arg
- orig_arg
);
277 * These are to give UI layer defaults.
278 * The core-level commands such as git-diff-files should
279 * never be affected by the setting of diff.renames
280 * the user happens to have in the configuration file.
282 void init_diff_ui_defaults(void)
284 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
287 int git_diff_heuristic_config(const char *var
, const char *value
,
290 if (!strcmp(var
, "diff.indentheuristic"))
291 diff_indent_heuristic
= git_config_bool(var
, value
);
295 static int parse_color_moved(const char *arg
)
297 switch (git_parse_maybe_bool(arg
)) {
299 return COLOR_MOVED_NO
;
301 return COLOR_MOVED_DEFAULT
;
306 if (!strcmp(arg
, "no"))
307 return COLOR_MOVED_NO
;
308 else if (!strcmp(arg
, "plain"))
309 return COLOR_MOVED_PLAIN
;
310 else if (!strcmp(arg
, "blocks"))
311 return COLOR_MOVED_BLOCKS
;
312 else if (!strcmp(arg
, "zebra"))
313 return COLOR_MOVED_ZEBRA
;
314 else if (!strcmp(arg
, "default"))
315 return COLOR_MOVED_DEFAULT
;
316 else if (!strcmp(arg
, "dimmed-zebra"))
317 return COLOR_MOVED_ZEBRA_DIM
;
318 else if (!strcmp(arg
, "dimmed_zebra"))
319 return COLOR_MOVED_ZEBRA_DIM
;
321 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
324 static unsigned parse_color_moved_ws(const char *arg
)
327 struct string_list l
= STRING_LIST_INIT_DUP
;
328 struct string_list_item
*i
;
330 string_list_split(&l
, arg
, ',', -1);
332 for_each_string_list_item(i
, &l
) {
333 struct strbuf sb
= STRBUF_INIT
;
334 strbuf_addstr(&sb
, i
->string
);
337 if (!strcmp(sb
.buf
, "no"))
339 else if (!strcmp(sb
.buf
, "ignore-space-change"))
340 ret
|= XDF_IGNORE_WHITESPACE_CHANGE
;
341 else if (!strcmp(sb
.buf
, "ignore-space-at-eol"))
342 ret
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
343 else if (!strcmp(sb
.buf
, "ignore-all-space"))
344 ret
|= XDF_IGNORE_WHITESPACE
;
345 else if (!strcmp(sb
.buf
, "allow-indentation-change"))
346 ret
|= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
;
348 ret
|= COLOR_MOVED_WS_ERROR
;
349 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
);
355 if ((ret
& COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
) &&
356 (ret
& XDF_WHITESPACE_FLAGS
)) {
357 error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes"));
358 ret
|= COLOR_MOVED_WS_ERROR
;
361 string_list_clear(&l
, 0);
366 int git_diff_ui_config(const char *var
, const char *value
,
367 const struct config_context
*ctx
, void *cb
)
369 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
370 diff_use_color_default
= git_config_colorbool(var
, value
);
373 if (!strcmp(var
, "diff.colormoved")) {
374 int cm
= parse_color_moved(value
);
377 diff_color_moved_default
= cm
;
380 if (!strcmp(var
, "diff.colormovedws")) {
383 return config_error_nonbool(var
);
384 cm
= parse_color_moved_ws(value
);
385 if (cm
& COLOR_MOVED_WS_ERROR
)
387 diff_color_moved_ws_default
= cm
;
390 if (!strcmp(var
, "diff.context")) {
391 diff_context_default
= git_config_int(var
, value
, ctx
->kvi
);
392 if (diff_context_default
< 0)
396 if (!strcmp(var
, "diff.interhunkcontext")) {
397 diff_interhunk_context_default
= git_config_int(var
, value
,
399 if (diff_interhunk_context_default
< 0)
403 if (!strcmp(var
, "diff.renames")) {
404 diff_detect_rename_default
= git_config_rename(var
, value
);
407 if (!strcmp(var
, "diff.autorefreshindex")) {
408 diff_auto_refresh_index
= git_config_bool(var
, value
);
411 if (!strcmp(var
, "diff.mnemonicprefix")) {
412 diff_mnemonic_prefix
= git_config_bool(var
, value
);
415 if (!strcmp(var
, "diff.noprefix")) {
416 diff_no_prefix
= git_config_bool(var
, value
);
419 if (!strcmp(var
, "diff.srcprefix")) {
420 FREE_AND_NULL(diff_src_prefix
);
421 return git_config_string(&diff_src_prefix
, var
, value
);
423 if (!strcmp(var
, "diff.dstprefix")) {
424 FREE_AND_NULL(diff_dst_prefix
);
425 return git_config_string(&diff_dst_prefix
, var
, value
);
427 if (!strcmp(var
, "diff.relative")) {
428 diff_relative
= git_config_bool(var
, value
);
431 if (!strcmp(var
, "diff.statnamewidth")) {
432 diff_stat_name_width
= git_config_int(var
, value
, ctx
->kvi
);
435 if (!strcmp(var
, "diff.statgraphwidth")) {
436 diff_stat_graph_width
= git_config_int(var
, value
, ctx
->kvi
);
439 if (!strcmp(var
, "diff.external"))
440 return git_config_string(&external_diff_cfg
.cmd
, var
, value
);
441 if (!strcmp(var
, "diff.trustexitcode")) {
442 external_diff_cfg
.trust_exit_code
= git_config_bool(var
, value
);
445 if (!strcmp(var
, "diff.wordregex"))
446 return git_config_string(&diff_word_regex_cfg
, var
, value
);
447 if (!strcmp(var
, "diff.orderfile")) {
448 FREE_AND_NULL(diff_order_file_cfg
);
449 return git_config_pathname(&diff_order_file_cfg
, var
, value
);
452 if (!strcmp(var
, "diff.ignoresubmodules")) {
454 return config_error_nonbool(var
);
455 handle_ignore_submodules_arg(&default_diff_options
, value
);
458 if (!strcmp(var
, "diff.submodule")) {
460 return config_error_nonbool(var
);
461 if (parse_submodule_params(&default_diff_options
, value
))
462 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
467 if (!strcmp(var
, "diff.algorithm")) {
469 return config_error_nonbool(var
);
470 diff_algorithm
= parse_algorithm_value(value
);
471 if (diff_algorithm
< 0)
472 return error(_("unknown value for config '%s': %s"),
477 if (git_color_config(var
, value
, cb
) < 0)
480 return git_diff_basic_config(var
, value
, ctx
, cb
);
483 int git_diff_basic_config(const char *var
, const char *value
,
484 const struct config_context
*ctx
, void *cb
)
488 if (!strcmp(var
, "diff.renamelimit")) {
489 diff_rename_limit_default
= git_config_int(var
, value
, ctx
->kvi
);
493 if (userdiff_config(var
, value
) < 0)
496 if (skip_prefix(var
, "diff.color.", &name
) ||
497 skip_prefix(var
, "color.diff.", &name
)) {
498 int slot
= parse_diff_color_slot(name
);
502 return config_error_nonbool(var
);
503 return color_parse(value
, diff_colors
[slot
]);
506 if (!strcmp(var
, "diff.wserrorhighlight")) {
509 return config_error_nonbool(var
);
510 val
= parse_ws_error_highlight(value
);
512 return error(_("unknown value for config '%s': %s"),
514 ws_error_highlight_default
= val
;
518 /* like GNU diff's --suppress-blank-empty option */
519 if (!strcmp(var
, "diff.suppressblankempty") ||
520 /* for backwards compatibility */
521 !strcmp(var
, "diff.suppress-blank-empty")) {
522 diff_suppress_blank_empty
= git_config_bool(var
, value
);
526 if (!strcmp(var
, "diff.dirstat")) {
527 struct strbuf errmsg
= STRBUF_INIT
;
529 return config_error_nonbool(var
);
530 default_diff_options
.dirstat_permille
= diff_dirstat_permille_default
;
531 if (parse_dirstat_params(&default_diff_options
, value
, &errmsg
))
532 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
534 strbuf_release(&errmsg
);
535 diff_dirstat_permille_default
= default_diff_options
.dirstat_permille
;
539 if (git_diff_heuristic_config(var
, value
, cb
) < 0)
542 return git_default_config(var
, value
, ctx
, cb
);
545 static char *quote_two(const char *one
, const char *two
)
547 int need_one
= quote_c_style(one
, NULL
, NULL
, CQUOTE_NODQ
);
548 int need_two
= quote_c_style(two
, NULL
, NULL
, CQUOTE_NODQ
);
549 struct strbuf res
= STRBUF_INIT
;
551 if (need_one
+ need_two
) {
552 strbuf_addch(&res
, '"');
553 quote_c_style(one
, &res
, NULL
, CQUOTE_NODQ
);
554 quote_c_style(two
, &res
, NULL
, CQUOTE_NODQ
);
555 strbuf_addch(&res
, '"');
557 strbuf_addstr(&res
, one
);
558 strbuf_addstr(&res
, two
);
560 return strbuf_detach(&res
, NULL
);
563 static const struct external_diff
*external_diff(void)
565 static struct external_diff external_diff_env
, *external_diff_ptr
;
566 static int done_preparing
= 0;
569 return external_diff_ptr
;
570 external_diff_env
.cmd
= xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
571 if (git_env_bool("GIT_EXTERNAL_DIFF_TRUST_EXIT_CODE", 0))
572 external_diff_env
.trust_exit_code
= 1;
573 if (external_diff_env
.cmd
)
574 external_diff_ptr
= &external_diff_env
;
575 else if (external_diff_cfg
.cmd
)
576 external_diff_ptr
= &external_diff_cfg
;
578 return external_diff_ptr
;
582 * Keep track of files used for diffing. Sometimes such an entry
583 * refers to a temporary file, sometimes to an existing file, and
584 * sometimes to "/dev/null".
586 static struct diff_tempfile
{
588 * filename external diff should read from, or NULL if this
589 * entry is currently not in use:
593 char hex
[GIT_MAX_HEXSZ
+ 1];
597 * If this diff_tempfile instance refers to a temporary file,
598 * this tempfile object is used to manage its lifetime.
600 struct tempfile
*tempfile
;
603 struct emit_callback
{
606 int blank_at_eof_in_preimage
;
607 int blank_at_eof_in_postimage
;
609 int lno_in_postimage
;
610 const char **label_path
;
611 struct diff_words_data
*diff_words
;
612 struct diff_options
*opt
;
613 struct strbuf
*header
;
616 static int count_lines(const char *data
, int size
)
618 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
625 completely_empty
= 0;
629 completely_empty
= 0;
632 if (completely_empty
)
635 count
++; /* no trailing newline */
639 static int fill_mmfile(struct repository
*r
, mmfile_t
*mf
,
640 struct diff_filespec
*one
)
642 if (!DIFF_FILE_VALID(one
)) {
643 mf
->ptr
= (char *)""; /* does not matter */
647 else if (diff_populate_filespec(r
, one
, NULL
))
651 mf
->size
= one
->size
;
655 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
656 static unsigned long diff_filespec_size(struct repository
*r
,
657 struct diff_filespec
*one
)
659 struct diff_populate_filespec_options dpf_options
= {
660 .check_size_only
= 1,
663 if (!DIFF_FILE_VALID(one
))
665 diff_populate_filespec(r
, one
, &dpf_options
);
669 static int count_trailing_blank(mmfile_t
*mf
)
672 long size
= mf
->size
;
677 ptr
+= size
- 1; /* pointing at the very end */
679 ; /* incomplete line */
681 ptr
--; /* skip the last LF */
682 while (mf
->ptr
< ptr
) {
684 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
685 if (*prev_eol
== '\n')
687 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
))
695 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
696 struct emit_callback
*ecbdata
)
699 l1
= count_trailing_blank(mf1
);
700 l2
= count_trailing_blank(mf2
);
702 ecbdata
->blank_at_eof_in_preimage
= 0;
703 ecbdata
->blank_at_eof_in_postimage
= 0;
706 at
= count_lines(mf1
->ptr
, mf1
->size
);
707 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
709 at
= count_lines(mf2
->ptr
, mf2
->size
);
710 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
713 static void emit_line_0(struct diff_options
*o
,
714 const char *set_sign
, const char *set
, unsigned reverse
, const char *reset
,
715 int first
, const char *line
, int len
)
717 int has_trailing_newline
, has_trailing_carriage_return
;
718 int needs_reset
= 0; /* at the end of the line */
719 FILE *file
= o
->file
;
721 fputs(diff_line_prefix(o
), file
);
723 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
724 if (has_trailing_newline
)
727 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
728 if (has_trailing_carriage_return
)
734 if (reverse
&& want_color(o
->use_color
)) {
735 fputs(GIT_COLOR_REVERSE
, file
);
740 fputs(set_sign
, file
);
751 if (set_sign
&& set
!= set_sign
)
756 fwrite(line
, len
, 1, file
);
757 needs_reset
= 1; /* 'line' may contain color codes. */
762 if (has_trailing_carriage_return
)
764 if (has_trailing_newline
)
768 static void emit_line(struct diff_options
*o
, const char *set
, const char *reset
,
769 const char *line
, int len
)
771 emit_line_0(o
, set
, NULL
, 0, reset
, 0, line
, len
);
775 DIFF_SYMBOL_BINARY_DIFF_HEADER
,
776 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
777 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
778 DIFF_SYMBOL_BINARY_DIFF_BODY
,
779 DIFF_SYMBOL_BINARY_DIFF_FOOTER
,
780 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
781 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
782 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
783 DIFF_SYMBOL_STATS_LINE
,
784 DIFF_SYMBOL_WORD_DIFF
,
785 DIFF_SYMBOL_STAT_SEP
,
787 DIFF_SYMBOL_SUBMODULE_ADD
,
788 DIFF_SYMBOL_SUBMODULE_DEL
,
789 DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
790 DIFF_SYMBOL_SUBMODULE_MODIFIED
,
791 DIFF_SYMBOL_SUBMODULE_HEADER
,
792 DIFF_SYMBOL_SUBMODULE_ERROR
,
793 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
,
794 DIFF_SYMBOL_REWRITE_DIFF
,
795 DIFF_SYMBOL_BINARY_FILES
,
797 DIFF_SYMBOL_FILEPAIR_PLUS
,
798 DIFF_SYMBOL_FILEPAIR_MINUS
,
799 DIFF_SYMBOL_WORDS_PORCELAIN
,
802 DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
805 DIFF_SYMBOL_NO_LF_EOF
,
806 DIFF_SYMBOL_CONTEXT_FRAGINFO
,
807 DIFF_SYMBOL_CONTEXT_MARKER
,
808 DIFF_SYMBOL_SEPARATOR
811 * Flags for content lines:
812 * 0..12 are whitespace rules
813 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
814 * 16 is marking if the line is blank at EOF
816 #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
817 #define DIFF_SYMBOL_MOVED_LINE (1<<17)
818 #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
819 #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
820 #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
823 * This struct is used when we need to buffer the output of the diff output.
825 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
826 * into the pre/post image file. This pointer could be a union with the
827 * line pointer. By storing an offset into the file instead of the literal line,
828 * we can decrease the memory footprint for the buffered output. At first we
829 * may want to only have indirection for the content lines, but we could also
830 * enhance the state for emitting prefabricated lines, e.g. the similarity
831 * score line or hunk/file headers would only need to store a number or path
832 * and then the output can be constructed later on depending on state.
834 struct emitted_diff_symbol
{
838 int indent_off
; /* Offset to first non-whitespace character */
839 int indent_width
; /* The visual width of the indentation */
843 #define EMITTED_DIFF_SYMBOL_INIT { 0 }
845 struct emitted_diff_symbols
{
846 struct emitted_diff_symbol
*buf
;
849 #define EMITTED_DIFF_SYMBOLS_INIT { 0 }
851 static void append_emitted_diff_symbol(struct diff_options
*o
,
852 struct emitted_diff_symbol
*e
)
854 struct emitted_diff_symbol
*f
;
856 ALLOC_GROW(o
->emitted_symbols
->buf
,
857 o
->emitted_symbols
->nr
+ 1,
858 o
->emitted_symbols
->alloc
);
859 f
= &o
->emitted_symbols
->buf
[o
->emitted_symbols
->nr
++];
861 memcpy(f
, e
, sizeof(struct emitted_diff_symbol
));
862 f
->line
= e
->line
? xmemdupz(e
->line
, e
->len
) : NULL
;
865 static void free_emitted_diff_symbols(struct emitted_diff_symbols
*e
)
874 const struct emitted_diff_symbol
*es
;
875 struct moved_entry
*next_line
;
876 struct moved_entry
*next_match
;
880 struct moved_entry
*match
;
881 int wsd
; /* The whitespace delta of this block */
884 #define INDENT_BLANKLINE INT_MIN
886 static void fill_es_indent_data(struct emitted_diff_symbol
*es
)
888 unsigned int off
= 0, i
;
889 int width
= 0, tab_width
= es
->flags
& WS_TAB_WIDTH_MASK
;
890 const char *s
= es
->line
;
891 const int len
= es
->len
;
893 /* skip any \v \f \r at start of indentation */
894 while (s
[off
] == '\f' || s
[off
] == '\v' ||
895 (s
[off
] == '\r' && off
< len
- 1))
898 /* calculate the visual width of indentation */
903 } else if (s
[off
] == '\t') {
904 width
+= tab_width
- (width
% tab_width
);
905 while (s
[++off
] == '\t')
912 /* check if this line is blank */
913 for (i
= off
; i
< len
; i
++)
918 es
->indent_width
= INDENT_BLANKLINE
;
919 es
->indent_off
= len
;
921 es
->indent_off
= off
;
922 es
->indent_width
= width
;
926 static int compute_ws_delta(const struct emitted_diff_symbol
*a
,
927 const struct emitted_diff_symbol
*b
)
929 int a_width
= a
->indent_width
,
930 b_width
= b
->indent_width
;
932 if (a_width
== INDENT_BLANKLINE
&& b_width
== INDENT_BLANKLINE
)
933 return INDENT_BLANKLINE
;
935 return a_width
- b_width
;
938 static int cmp_in_block_with_wsd(const struct moved_entry
*cur
,
939 const struct emitted_diff_symbol
*l
,
940 struct moved_block
*pmb
)
942 int a_width
= cur
->es
->indent_width
, b_width
= l
->indent_width
;
945 /* The text of each line must match */
946 if (cur
->es
->id
!= l
->id
)
950 * If 'l' and 'cur' are both blank then we don't need to check the
951 * indent. We only need to check cur as we know the strings match.
953 if (a_width
== INDENT_BLANKLINE
)
957 * The indent changes of the block are known and stored in pmb->wsd;
958 * however we need to check if the indent changes of the current line
959 * match those of the current block.
961 delta
= b_width
- a_width
;
964 * If the previous lines of this block were all blank then set its
967 if (pmb
->wsd
== INDENT_BLANKLINE
)
970 return delta
!= pmb
->wsd
;
973 struct interned_diff_symbol
{
974 struct hashmap_entry ent
;
975 struct emitted_diff_symbol
*es
;
978 static int interned_diff_symbol_cmp(const void *hashmap_cmp_fn_data
,
979 const struct hashmap_entry
*eptr
,
980 const struct hashmap_entry
*entry_or_key
,
981 const void *keydata UNUSED
)
983 const struct diff_options
*diffopt
= hashmap_cmp_fn_data
;
984 const struct emitted_diff_symbol
*a
, *b
;
985 unsigned flags
= diffopt
->color_moved_ws_handling
986 & XDF_WHITESPACE_FLAGS
;
988 a
= container_of(eptr
, const struct interned_diff_symbol
, ent
)->es
;
989 b
= container_of(entry_or_key
, const struct interned_diff_symbol
, ent
)->es
;
991 return !xdiff_compare_lines(a
->line
+ a
->indent_off
,
992 a
->len
- a
->indent_off
,
993 b
->line
+ b
->indent_off
,
994 b
->len
- b
->indent_off
, flags
);
997 static void prepare_entry(struct diff_options
*o
, struct emitted_diff_symbol
*l
,
998 struct interned_diff_symbol
*s
)
1000 unsigned flags
= o
->color_moved_ws_handling
& XDF_WHITESPACE_FLAGS
;
1001 unsigned int hash
= xdiff_hash_string(l
->line
+ l
->indent_off
,
1002 l
->len
- l
->indent_off
, flags
);
1004 hashmap_entry_init(&s
->ent
, hash
);
1008 struct moved_entry_list
{
1009 struct moved_entry
*add
, *del
;
1012 static struct moved_entry_list
*add_lines_to_move_detection(struct diff_options
*o
,
1013 struct mem_pool
*entry_mem_pool
)
1015 struct moved_entry
*prev_line
= NULL
;
1016 struct mem_pool interned_pool
;
1017 struct hashmap interned_map
;
1018 struct moved_entry_list
*entry_list
= NULL
;
1019 size_t entry_list_alloc
= 0;
1023 hashmap_init(&interned_map
, interned_diff_symbol_cmp
, o
, 8096);
1024 mem_pool_init(&interned_pool
, 1024 * 1024);
1026 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1027 struct interned_diff_symbol key
;
1028 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1029 struct interned_diff_symbol
*s
;
1030 struct moved_entry
*entry
;
1032 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
) {
1037 if (o
->color_moved_ws_handling
&
1038 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1039 fill_es_indent_data(l
);
1041 prepare_entry(o
, l
, &key
);
1042 s
= hashmap_get_entry(&interned_map
, &key
, ent
, &key
.ent
);
1047 ALLOC_GROW_BY(entry_list
, id
, 1, entry_list_alloc
);
1048 hashmap_add(&interned_map
,
1049 memcpy(mem_pool_alloc(&interned_pool
,
1051 &key
, sizeof(key
)));
1053 entry
= mem_pool_alloc(entry_mem_pool
, sizeof(*entry
));
1055 entry
->next_line
= NULL
;
1056 if (prev_line
&& prev_line
->es
->s
== l
->s
)
1057 prev_line
->next_line
= entry
;
1059 if (l
->s
== DIFF_SYMBOL_PLUS
) {
1060 entry
->next_match
= entry_list
[l
->id
].add
;
1061 entry_list
[l
->id
].add
= entry
;
1063 entry
->next_match
= entry_list
[l
->id
].del
;
1064 entry_list
[l
->id
].del
= entry
;
1068 hashmap_clear(&interned_map
);
1069 mem_pool_discard(&interned_pool
, 0);
1074 static void pmb_advance_or_null(struct diff_options
*o
,
1075 struct emitted_diff_symbol
*l
,
1076 struct moved_block
*pmb
,
1081 for (i
= 0, j
= 0; i
< *pmb_nr
; i
++) {
1083 struct moved_entry
*prev
= pmb
[i
].match
;
1084 struct moved_entry
*cur
= (prev
&& prev
->next_line
) ?
1085 prev
->next_line
: NULL
;
1087 if (o
->color_moved_ws_handling
&
1088 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1090 !cmp_in_block_with_wsd(cur
, l
, &pmb
[i
]);
1092 match
= cur
&& cur
->es
->id
== l
->id
;
1096 pmb
[j
++].match
= cur
;
1102 static void fill_potential_moved_blocks(struct diff_options
*o
,
1103 struct moved_entry
*match
,
1104 struct emitted_diff_symbol
*l
,
1105 struct moved_block
**pmb_p
,
1106 int *pmb_alloc_p
, int *pmb_nr_p
)
1109 struct moved_block
*pmb
= *pmb_p
;
1110 int pmb_alloc
= *pmb_alloc_p
, pmb_nr
= *pmb_nr_p
;
1113 * The current line is the start of a new block.
1114 * Setup the set of potential blocks.
1116 for (; match
; match
= match
->next_match
) {
1117 ALLOC_GROW(pmb
, pmb_nr
+ 1, pmb_alloc
);
1118 if (o
->color_moved_ws_handling
&
1119 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1120 pmb
[pmb_nr
].wsd
= compute_ws_delta(l
, match
->es
);
1122 pmb
[pmb_nr
].wsd
= 0;
1123 pmb
[pmb_nr
++].match
= match
;
1127 *pmb_alloc_p
= pmb_alloc
;
1132 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
1134 * Otherwise, if the last block has fewer alphanumeric characters than
1135 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
1138 * The last block consists of the (n - block_length)'th line up to but not
1139 * including the nth line.
1141 * Returns 0 if the last block is empty or is unset by this function, non zero
1144 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
1145 * Think of a way to unify them.
1147 #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1148 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
1149 static int adjust_last_block(struct diff_options
*o
, int n
, int block_length
)
1151 int i
, alnum_count
= 0;
1152 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
1153 return block_length
;
1154 for (i
= 1; i
< block_length
+ 1; i
++) {
1155 const char *c
= o
->emitted_symbols
->buf
[n
- i
].line
;
1160 if (alnum_count
>= COLOR_MOVED_MIN_ALNUM_COUNT
)
1164 for (i
= 1; i
< block_length
+ 1; i
++)
1165 o
->emitted_symbols
->buf
[n
- i
].flags
&= ~DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
;
1169 /* Find blocks of moved code, delegate actual coloring decision to helper */
1170 static void mark_color_as_moved(struct diff_options
*o
,
1171 struct moved_entry_list
*entry_list
)
1173 struct moved_block
*pmb
= NULL
; /* potentially moved blocks */
1174 int pmb_nr
= 0, pmb_alloc
= 0;
1175 int n
, flipped_block
= 0, block_length
= 0;
1176 enum diff_symbol moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1179 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1180 struct moved_entry
*match
= NULL
;
1181 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1184 case DIFF_SYMBOL_PLUS
:
1185 match
= entry_list
[l
->id
].del
;
1187 case DIFF_SYMBOL_MINUS
:
1188 match
= entry_list
[l
->id
].add
;
1194 if (pmb_nr
&& (!match
|| l
->s
!= moved_symbol
)) {
1195 if (!adjust_last_block(o
, n
, block_length
) &&
1198 * Rewind in case there is another match
1199 * starting at the second line of the block
1209 moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1213 if (o
->color_moved
== COLOR_MOVED_PLAIN
) {
1214 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1218 pmb_advance_or_null(o
, l
, pmb
, &pmb_nr
);
1221 int contiguous
= adjust_last_block(o
, n
, block_length
);
1223 if (!contiguous
&& block_length
> 1)
1225 * Rewind in case there is another match
1226 * starting at the second line of the block
1230 fill_potential_moved_blocks(o
, match
, l
,
1234 if (contiguous
&& pmb_nr
&& moved_symbol
== l
->s
)
1235 flipped_block
= (flipped_block
+ 1) % 2;
1240 moved_symbol
= l
->s
;
1242 moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1249 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1250 if (flipped_block
&& o
->color_moved
!= COLOR_MOVED_BLOCKS
)
1251 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_ALT
;
1254 adjust_last_block(o
, n
, block_length
);
1259 static void dim_moved_lines(struct diff_options
*o
)
1262 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1263 struct emitted_diff_symbol
*prev
= (n
!= 0) ?
1264 &o
->emitted_symbols
->buf
[n
- 1] : NULL
;
1265 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1266 struct emitted_diff_symbol
*next
=
1267 (n
< o
->emitted_symbols
->nr
- 1) ?
1268 &o
->emitted_symbols
->buf
[n
+ 1] : NULL
;
1270 /* Not a plus or minus line? */
1271 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
)
1274 /* Not a moved line? */
1275 if (!(l
->flags
& DIFF_SYMBOL_MOVED_LINE
))
1279 * If prev or next are not a plus or minus line,
1280 * pretend they don't exist
1282 if (prev
&& prev
->s
!= DIFF_SYMBOL_PLUS
&&
1283 prev
->s
!= DIFF_SYMBOL_MINUS
)
1285 if (next
&& next
->s
!= DIFF_SYMBOL_PLUS
&&
1286 next
->s
!= DIFF_SYMBOL_MINUS
)
1289 /* Inside a block? */
1291 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1292 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
)) &&
1294 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1295 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
))) {
1296 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1300 /* Check if we are at an interesting bound: */
1301 if (prev
&& (prev
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1302 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1303 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1305 if (next
&& (next
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1306 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1307 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1311 * The boundary to prev and next are not interesting,
1312 * so this line is not interesting as a whole
1314 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1318 static void emit_line_ws_markup(struct diff_options
*o
,
1319 const char *set_sign
, const char *set
,
1321 int sign_index
, const char *line
, int len
,
1322 unsigned ws_rule
, int blank_at_eof
)
1324 const char *ws
= NULL
;
1325 int sign
= o
->output_indicators
[sign_index
];
1327 if (o
->ws_error_highlight
& ws_rule
) {
1328 ws
= diff_get_color_opt(o
, DIFF_WHITESPACE
);
1333 if (!ws
&& !set_sign
)
1334 emit_line_0(o
, set
, NULL
, 0, reset
, sign
, line
, len
);
1336 emit_line_0(o
, set_sign
, set
, !!set_sign
, reset
, sign
, line
, len
);
1337 } else if (blank_at_eof
)
1338 /* Blank line at EOF - paint '+' as well */
1339 emit_line_0(o
, ws
, NULL
, 0, reset
, sign
, line
, len
);
1341 /* Emit just the prefix, then the rest. */
1342 emit_line_0(o
, set_sign
? set_sign
: set
, NULL
, !!set_sign
, reset
,
1344 ws_check_emit(line
, len
, ws_rule
,
1345 o
->file
, set
, reset
, ws
);
1349 static void emit_diff_symbol_from_struct(struct diff_options
*o
,
1350 struct emitted_diff_symbol
*eds
)
1352 static const char *nneof
= " No newline at end of file\n";
1353 const char *context
, *reset
, *set
, *set_sign
, *meta
, *fraginfo
;
1355 enum diff_symbol s
= eds
->s
;
1356 const char *line
= eds
->line
;
1358 unsigned flags
= eds
->flags
;
1361 case DIFF_SYMBOL_NO_LF_EOF
:
1362 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1363 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1364 putc('\n', o
->file
);
1365 emit_line_0(o
, context
, NULL
, 0, reset
, '\\',
1366 nneof
, strlen(nneof
));
1368 case DIFF_SYMBOL_SUBMODULE_HEADER
:
1369 case DIFF_SYMBOL_SUBMODULE_ERROR
:
1370 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
:
1371 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
:
1372 case DIFF_SYMBOL_SUMMARY
:
1373 case DIFF_SYMBOL_STATS_LINE
:
1374 case DIFF_SYMBOL_BINARY_DIFF_BODY
:
1375 case DIFF_SYMBOL_CONTEXT_FRAGINFO
:
1376 emit_line(o
, "", "", line
, len
);
1378 case DIFF_SYMBOL_CONTEXT_INCOMPLETE
:
1379 case DIFF_SYMBOL_CONTEXT_MARKER
:
1380 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1381 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1382 emit_line(o
, context
, reset
, line
, len
);
1384 case DIFF_SYMBOL_SEPARATOR
:
1385 fprintf(o
->file
, "%s%c",
1386 diff_line_prefix(o
),
1387 o
->line_termination
);
1389 case DIFF_SYMBOL_CONTEXT
:
1390 set
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1391 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1393 if (o
->flags
.dual_color_diffed_diffs
) {
1394 char c
= !len
? 0 : line
[0];
1397 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1399 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1401 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1403 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1404 OUTPUT_INDICATOR_CONTEXT
, line
, len
,
1405 flags
& (DIFF_SYMBOL_CONTENT_WS_MASK
), 0);
1407 case DIFF_SYMBOL_PLUS
:
1408 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1409 DIFF_SYMBOL_MOVED_LINE_ALT
|
1410 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1411 case DIFF_SYMBOL_MOVED_LINE
|
1412 DIFF_SYMBOL_MOVED_LINE_ALT
|
1413 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1414 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT_DIM
);
1416 case DIFF_SYMBOL_MOVED_LINE
|
1417 DIFF_SYMBOL_MOVED_LINE_ALT
:
1418 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT
);
1420 case DIFF_SYMBOL_MOVED_LINE
|
1421 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1422 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_DIM
);
1424 case DIFF_SYMBOL_MOVED_LINE
:
1425 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED
);
1428 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1430 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1431 if (!o
->flags
.dual_color_diffed_diffs
)
1434 char c
= !len
? 0 : line
[0];
1438 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_BOLD
);
1440 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1442 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_BOLD
);
1444 set
= diff_get_color_opt(o
, DIFF_CONTEXT_BOLD
);
1445 flags
&= ~DIFF_SYMBOL_CONTENT_WS_MASK
;
1447 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1448 OUTPUT_INDICATOR_NEW
, line
, len
,
1449 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
,
1450 flags
& DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
);
1452 case DIFF_SYMBOL_MINUS
:
1453 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1454 DIFF_SYMBOL_MOVED_LINE_ALT
|
1455 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1456 case DIFF_SYMBOL_MOVED_LINE
|
1457 DIFF_SYMBOL_MOVED_LINE_ALT
|
1458 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1459 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT_DIM
);
1461 case DIFF_SYMBOL_MOVED_LINE
|
1462 DIFF_SYMBOL_MOVED_LINE_ALT
:
1463 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT
);
1465 case DIFF_SYMBOL_MOVED_LINE
|
1466 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1467 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_DIM
);
1469 case DIFF_SYMBOL_MOVED_LINE
:
1470 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED
);
1473 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1475 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1476 if (!o
->flags
.dual_color_diffed_diffs
)
1479 char c
= !len
? 0 : line
[0];
1483 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_DIM
);
1485 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1487 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_DIM
);
1489 set
= diff_get_color_opt(o
, DIFF_CONTEXT_DIM
);
1491 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1492 OUTPUT_INDICATOR_OLD
, line
, len
,
1493 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
, 0);
1495 case DIFF_SYMBOL_WORDS_PORCELAIN
:
1496 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1497 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1498 emit_line(o
, context
, reset
, line
, len
);
1499 fputs("~\n", o
->file
);
1501 case DIFF_SYMBOL_WORDS
:
1502 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1503 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1505 * Skip the prefix character, if any. With
1506 * diff_suppress_blank_empty, there may be
1509 if (line
[0] != '\n') {
1513 emit_line(o
, context
, reset
, line
, len
);
1515 case DIFF_SYMBOL_FILEPAIR_PLUS
:
1516 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1517 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1518 fprintf(o
->file
, "%s%s+++ %s%s%s\n", diff_line_prefix(o
), meta
,
1520 strchr(line
, ' ') ? "\t" : "");
1522 case DIFF_SYMBOL_FILEPAIR_MINUS
:
1523 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1524 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1525 fprintf(o
->file
, "%s%s--- %s%s%s\n", diff_line_prefix(o
), meta
,
1527 strchr(line
, ' ') ? "\t" : "");
1529 case DIFF_SYMBOL_BINARY_FILES
:
1530 case DIFF_SYMBOL_HEADER
:
1531 fprintf(o
->file
, "%s", line
);
1533 case DIFF_SYMBOL_BINARY_DIFF_HEADER
:
1534 fprintf(o
->file
, "%sGIT binary patch\n", diff_line_prefix(o
));
1536 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
:
1537 fprintf(o
->file
, "%sdelta %s\n", diff_line_prefix(o
), line
);
1539 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
:
1540 fprintf(o
->file
, "%sliteral %s\n", diff_line_prefix(o
), line
);
1542 case DIFF_SYMBOL_BINARY_DIFF_FOOTER
:
1543 fputs(diff_line_prefix(o
), o
->file
);
1544 fputc('\n', o
->file
);
1546 case DIFF_SYMBOL_REWRITE_DIFF
:
1547 fraginfo
= diff_get_color(o
->use_color
, DIFF_FRAGINFO
);
1548 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1549 emit_line(o
, fraginfo
, reset
, line
, len
);
1551 case DIFF_SYMBOL_SUBMODULE_ADD
:
1552 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1553 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1554 emit_line(o
, set
, reset
, line
, len
);
1556 case DIFF_SYMBOL_SUBMODULE_DEL
:
1557 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1558 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1559 emit_line(o
, set
, reset
, line
, len
);
1561 case DIFF_SYMBOL_SUBMODULE_UNTRACKED
:
1562 fprintf(o
->file
, "%sSubmodule %s contains untracked content\n",
1563 diff_line_prefix(o
), line
);
1565 case DIFF_SYMBOL_SUBMODULE_MODIFIED
:
1566 fprintf(o
->file
, "%sSubmodule %s contains modified content\n",
1567 diff_line_prefix(o
), line
);
1569 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
:
1570 emit_line(o
, "", "", " 0 files changed\n",
1571 strlen(" 0 files changed\n"));
1573 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV
:
1574 emit_line(o
, "", "", " ...\n", strlen(" ...\n"));
1576 case DIFF_SYMBOL_WORD_DIFF
:
1577 fprintf(o
->file
, "%.*s", len
, line
);
1579 case DIFF_SYMBOL_STAT_SEP
:
1580 fputs(o
->stat_sep
, o
->file
);
1583 BUG("unknown diff symbol");
1587 static void emit_diff_symbol(struct diff_options
*o
, enum diff_symbol s
,
1588 const char *line
, int len
, unsigned flags
)
1590 struct emitted_diff_symbol e
= {
1591 .line
= line
, .len
= len
, .flags
= flags
, .s
= s
1594 if (o
->emitted_symbols
)
1595 append_emitted_diff_symbol(o
, &e
);
1597 emit_diff_symbol_from_struct(o
, &e
);
1600 void diff_emit_submodule_del(struct diff_options
*o
, const char *line
)
1602 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_DEL
, line
, strlen(line
), 0);
1605 void diff_emit_submodule_add(struct diff_options
*o
, const char *line
)
1607 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ADD
, line
, strlen(line
), 0);
1610 void diff_emit_submodule_untracked(struct diff_options
*o
, const char *path
)
1612 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
1613 path
, strlen(path
), 0);
1616 void diff_emit_submodule_modified(struct diff_options
*o
, const char *path
)
1618 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_MODIFIED
,
1619 path
, strlen(path
), 0);
1622 void diff_emit_submodule_header(struct diff_options
*o
, const char *header
)
1624 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_HEADER
,
1625 header
, strlen(header
), 0);
1628 void diff_emit_submodule_error(struct diff_options
*o
, const char *err
)
1630 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ERROR
, err
, strlen(err
), 0);
1633 void diff_emit_submodule_pipethrough(struct diff_options
*o
,
1634 const char *line
, int len
)
1636 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
, line
, len
, 0);
1639 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
1641 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
1642 ecbdata
->blank_at_eof_in_preimage
&&
1643 ecbdata
->blank_at_eof_in_postimage
&&
1644 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
1645 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
1647 return ws_blank_line(line
, len
);
1650 static void emit_add_line(struct emit_callback
*ecbdata
,
1651 const char *line
, int len
)
1653 unsigned flags
= WSEH_NEW
| ecbdata
->ws_rule
;
1654 if (new_blank_line_at_eof(ecbdata
, line
, len
))
1655 flags
|= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
;
1657 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_PLUS
, line
, len
, flags
);
1660 static void emit_del_line(struct emit_callback
*ecbdata
,
1661 const char *line
, int len
)
1663 unsigned flags
= WSEH_OLD
| ecbdata
->ws_rule
;
1664 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_MINUS
, line
, len
, flags
);
1667 static void emit_context_line(struct emit_callback
*ecbdata
,
1668 const char *line
, int len
)
1670 unsigned flags
= WSEH_CONTEXT
| ecbdata
->ws_rule
;
1671 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_CONTEXT
, line
, len
, flags
);
1674 static void emit_hunk_header(struct emit_callback
*ecbdata
,
1675 const char *line
, int len
)
1677 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
1678 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
1679 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
1680 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1681 const char *reverse
= ecbdata
->color_diff
? GIT_COLOR_REVERSE
: "";
1682 static const char atat
[2] = { '@', '@' };
1683 const char *cp
, *ep
;
1684 struct strbuf msgbuf
= STRBUF_INIT
;
1689 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1690 * it always is at least 10 bytes long.
1693 memcmp(line
, atat
, 2) ||
1694 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
1695 emit_diff_symbol(ecbdata
->opt
,
1696 DIFF_SYMBOL_CONTEXT_MARKER
, line
, len
, 0);
1699 ep
+= 2; /* skip over @@ */
1701 /* The hunk header in fraginfo color */
1702 if (ecbdata
->opt
->flags
.dual_color_diffed_diffs
)
1703 strbuf_addstr(&msgbuf
, reverse
);
1704 strbuf_addstr(&msgbuf
, frag
);
1705 if (ecbdata
->opt
->flags
.suppress_hunk_header_line_count
)
1706 strbuf_add(&msgbuf
, atat
, sizeof(atat
));
1708 strbuf_add(&msgbuf
, line
, ep
- line
);
1709 strbuf_addstr(&msgbuf
, reset
);
1715 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
1718 /* blank before the func header */
1719 for (cp
= ep
; ep
- line
< len
; ep
++)
1720 if (*ep
!= ' ' && *ep
!= '\t')
1723 strbuf_addstr(&msgbuf
, context
);
1724 strbuf_add(&msgbuf
, cp
, ep
- cp
);
1725 strbuf_addstr(&msgbuf
, reset
);
1728 if (ep
< line
+ len
) {
1729 strbuf_addstr(&msgbuf
, func
);
1730 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
1731 strbuf_addstr(&msgbuf
, reset
);
1734 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
1735 strbuf_complete_line(&msgbuf
);
1736 emit_diff_symbol(ecbdata
->opt
,
1737 DIFF_SYMBOL_CONTEXT_FRAGINFO
, msgbuf
.buf
, msgbuf
.len
, 0);
1738 strbuf_release(&msgbuf
);
1741 static struct diff_tempfile
*claim_diff_tempfile(void)
1744 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
1745 if (!diff_temp
[i
].name
)
1746 return diff_temp
+ i
;
1747 BUG("diff is failing to clean up its tempfiles");
1750 static void remove_tempfile(void)
1753 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
1754 if (is_tempfile_active(diff_temp
[i
].tempfile
))
1755 delete_tempfile(&diff_temp
[i
].tempfile
);
1756 diff_temp
[i
].name
= NULL
;
1760 static void add_line_count(struct strbuf
*out
, int count
)
1764 strbuf_addstr(out
, "0,0");
1767 strbuf_addstr(out
, "1");
1770 strbuf_addf(out
, "1,%d", count
);
1775 static void emit_rewrite_lines(struct emit_callback
*ecb
,
1776 int prefix
, const char *data
, int size
)
1778 const char *endp
= NULL
;
1783 endp
= memchr(data
, '\n', size
);
1784 len
= endp
? (endp
- data
+ 1) : size
;
1785 if (prefix
!= '+') {
1786 ecb
->lno_in_preimage
++;
1787 emit_del_line(ecb
, data
, len
);
1789 ecb
->lno_in_postimage
++;
1790 emit_add_line(ecb
, data
, len
);
1796 emit_diff_symbol(ecb
->opt
, DIFF_SYMBOL_NO_LF_EOF
, NULL
, 0, 0);
1799 static void emit_rewrite_diff(const char *name_a
,
1801 struct diff_filespec
*one
,
1802 struct diff_filespec
*two
,
1803 struct userdiff_driver
*textconv_one
,
1804 struct userdiff_driver
*textconv_two
,
1805 struct diff_options
*o
)
1808 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
1809 const char *a_prefix
, *b_prefix
;
1810 char *data_one
, *data_two
;
1811 size_t size_one
, size_two
;
1812 struct emit_callback ecbdata
;
1813 struct strbuf out
= STRBUF_INIT
;
1815 if (diff_mnemonic_prefix
&& o
->flags
.reverse_diff
) {
1816 a_prefix
= o
->b_prefix
;
1817 b_prefix
= o
->a_prefix
;
1819 a_prefix
= o
->a_prefix
;
1820 b_prefix
= o
->b_prefix
;
1823 name_a
+= (*name_a
== '/');
1824 name_b
+= (*name_b
== '/');
1826 strbuf_reset(&a_name
);
1827 strbuf_reset(&b_name
);
1828 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
1829 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
1831 size_one
= fill_textconv(o
->repo
, textconv_one
, one
, &data_one
);
1832 size_two
= fill_textconv(o
->repo
, textconv_two
, two
, &data_two
);
1834 memset(&ecbdata
, 0, sizeof(ecbdata
));
1835 ecbdata
.color_diff
= want_color(o
->use_color
);
1836 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
1838 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
1840 mf1
.ptr
= (char *)data_one
;
1841 mf2
.ptr
= (char *)data_two
;
1842 mf1
.size
= size_one
;
1843 mf2
.size
= size_two
;
1844 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1846 ecbdata
.lno_in_preimage
= 1;
1847 ecbdata
.lno_in_postimage
= 1;
1849 lc_a
= count_lines(data_one
, size_one
);
1850 lc_b
= count_lines(data_two
, size_two
);
1852 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
1853 a_name
.buf
, a_name
.len
, 0);
1854 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
1855 b_name
.buf
, b_name
.len
, 0);
1857 strbuf_addstr(&out
, "@@ -");
1858 if (!o
->irreversible_delete
)
1859 add_line_count(&out
, lc_a
);
1861 strbuf_addstr(&out
, "?,?");
1862 strbuf_addstr(&out
, " +");
1863 add_line_count(&out
, lc_b
);
1864 strbuf_addstr(&out
, " @@\n");
1865 emit_diff_symbol(o
, DIFF_SYMBOL_REWRITE_DIFF
, out
.buf
, out
.len
, 0);
1866 strbuf_release(&out
);
1868 if (lc_a
&& !o
->irreversible_delete
)
1869 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
1871 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
1873 free((char *)data_one
);
1875 free((char *)data_two
);
1878 struct diff_words_buffer
{
1880 unsigned long alloc
;
1881 struct diff_words_orig
{
1882 const char *begin
, *end
;
1884 int orig_nr
, orig_alloc
;
1887 static void diff_words_append(char *line
, unsigned long len
,
1888 struct diff_words_buffer
*buffer
)
1890 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
1893 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
1894 buffer
->text
.size
+= len
;
1895 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
1898 struct diff_words_style_elem
{
1901 const char *color
; /* NULL; filled in by the setup code if
1902 * color is enabled */
1905 struct diff_words_style
{
1906 enum diff_words_type type
;
1907 struct diff_words_style_elem new_word
, old_word
, ctx
;
1908 const char *newline
;
1911 static struct diff_words_style diff_words_styles
[] = {
1912 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1913 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1914 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
1917 struct diff_words_data
{
1918 struct diff_words_buffer minus
, plus
;
1919 const char *current_plus
;
1921 struct diff_options
*opt
;
1922 regex_t
*word_regex
;
1923 enum diff_words_type type
;
1924 struct diff_words_style
*style
;
1927 static int fn_out_diff_words_write_helper(struct diff_options
*o
,
1928 struct diff_words_style_elem
*st_el
,
1929 const char *newline
,
1930 size_t count
, const char *buf
)
1933 struct strbuf sb
= STRBUF_INIT
;
1936 char *p
= memchr(buf
, '\n', count
);
1938 strbuf_addstr(&sb
, diff_line_prefix(o
));
1941 const char *reset
= st_el
->color
&& *st_el
->color
?
1942 GIT_COLOR_RESET
: NULL
;
1943 if (st_el
->color
&& *st_el
->color
)
1944 strbuf_addstr(&sb
, st_el
->color
);
1945 strbuf_addstr(&sb
, st_el
->prefix
);
1946 strbuf_add(&sb
, buf
, p
? p
- buf
: count
);
1947 strbuf_addstr(&sb
, st_el
->suffix
);
1949 strbuf_addstr(&sb
, reset
);
1954 strbuf_addstr(&sb
, newline
);
1955 count
-= p
+ 1 - buf
;
1959 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1967 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1969 strbuf_release(&sb
);
1974 * '--color-words' algorithm can be described as:
1976 * 1. collect the minus/plus lines of a diff hunk, divided into
1977 * minus-lines and plus-lines;
1979 * 2. break both minus-lines and plus-lines into words and
1980 * place them into two mmfile_t with one word for each line;
1982 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1984 * And for the common parts of the both file, we output the plus side text.
1985 * diff_words->current_plus is used to trace the current position of the plus file
1986 * which printed. diff_words->last_minus is used to trace the last minus word
1989 * For '--graph' to work with '--color-words', we need to output the graph prefix
1990 * on each line of color words output. Generally, there are two conditions on
1991 * which we should output the prefix.
1993 * 1. diff_words->last_minus == 0 &&
1994 * diff_words->current_plus == diff_words->plus.text.ptr
1996 * that is: the plus text must start as a new line, and if there is no minus
1997 * word printed, a graph prefix must be printed.
1999 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
2000 * *(diff_words->current_plus - 1) == '\n'
2002 * that is: a graph prefix must be printed following a '\n'
2004 static int color_words_output_graph_prefix(struct diff_words_data
*diff_words
)
2006 if ((diff_words
->last_minus
== 0 &&
2007 diff_words
->current_plus
== diff_words
->plus
.text
.ptr
) ||
2008 (diff_words
->current_plus
> diff_words
->plus
.text
.ptr
&&
2009 *(diff_words
->current_plus
- 1) == '\n')) {
2016 static void fn_out_diff_words_aux(void *priv
,
2017 long minus_first
, long minus_len
,
2018 long plus_first
, long plus_len
,
2019 const char *func UNUSED
, long funclen UNUSED
)
2021 struct diff_words_data
*diff_words
= priv
;
2022 struct diff_words_style
*style
= diff_words
->style
;
2023 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
2024 struct diff_options
*opt
= diff_words
->opt
;
2025 const char *line_prefix
;
2028 line_prefix
= diff_line_prefix(opt
);
2030 /* POSIX requires that first be decremented by one if len == 0... */
2032 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
2034 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
2036 minus_begin
= minus_end
=
2037 diff_words
->minus
.orig
[minus_first
].end
;
2040 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
2041 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
2043 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
2045 if (color_words_output_graph_prefix(diff_words
)) {
2046 fputs(line_prefix
, diff_words
->opt
->file
);
2048 if (diff_words
->current_plus
!= plus_begin
) {
2049 fn_out_diff_words_write_helper(diff_words
->opt
,
2050 &style
->ctx
, style
->newline
,
2051 plus_begin
- diff_words
->current_plus
,
2052 diff_words
->current_plus
);
2054 if (minus_begin
!= minus_end
) {
2055 fn_out_diff_words_write_helper(diff_words
->opt
,
2056 &style
->old_word
, style
->newline
,
2057 minus_end
- minus_begin
, minus_begin
);
2059 if (plus_begin
!= plus_end
) {
2060 fn_out_diff_words_write_helper(diff_words
->opt
,
2061 &style
->new_word
, style
->newline
,
2062 plus_end
- plus_begin
, plus_begin
);
2065 diff_words
->current_plus
= plus_end
;
2066 diff_words
->last_minus
= minus_first
;
2069 /* This function starts looking at *begin, and returns 0 iff a word was found. */
2070 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
2071 int *begin
, int *end
)
2073 while (word_regex
&& *begin
< buffer
->size
) {
2074 regmatch_t match
[1];
2075 if (!regexec_buf(word_regex
, buffer
->ptr
+ *begin
,
2076 buffer
->size
- *begin
, 1, match
, 0)) {
2077 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
2078 '\n', match
[0].rm_eo
- match
[0].rm_so
);
2079 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
2080 *begin
+= match
[0].rm_so
;
2084 return *begin
> *end
;
2090 /* find the next word */
2091 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
2093 if (*begin
>= buffer
->size
)
2096 /* find the end of the word */
2098 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
2105 * This function splits the words in buffer->text, stores the list with
2106 * newline separator into out, and saves the offsets of the original words
2109 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
2110 regex_t
*word_regex
)
2118 /* fake an empty "0th" word */
2119 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
2120 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
2121 buffer
->orig_nr
= 1;
2123 for (i
= 0; i
< buffer
->text
.size
; i
++) {
2124 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
2127 /* store original boundaries */
2128 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
2129 buffer
->orig_alloc
);
2130 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
2131 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
2134 /* store one word */
2135 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
2136 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
2137 out
->ptr
[out
->size
+ j
- i
] = '\n';
2138 out
->size
+= j
- i
+ 1;
2144 /* this executes the word diff on the accumulated buffers */
2145 static void diff_words_show(struct diff_words_data
*diff_words
)
2149 mmfile_t minus
, plus
;
2150 struct diff_words_style
*style
= diff_words
->style
;
2152 struct diff_options
*opt
= diff_words
->opt
;
2153 const char *line_prefix
;
2156 line_prefix
= diff_line_prefix(opt
);
2158 /* special case: only removal */
2159 if (!diff_words
->plus
.text
.size
) {
2160 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2161 line_prefix
, strlen(line_prefix
), 0);
2162 fn_out_diff_words_write_helper(diff_words
->opt
,
2163 &style
->old_word
, style
->newline
,
2164 diff_words
->minus
.text
.size
,
2165 diff_words
->minus
.text
.ptr
);
2166 diff_words
->minus
.text
.size
= 0;
2170 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
2171 diff_words
->last_minus
= 0;
2173 memset(&xpp
, 0, sizeof(xpp
));
2174 memset(&xecfg
, 0, sizeof(xecfg
));
2175 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
2176 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
2178 /* as only the hunk header will be parsed, we need a 0-context */
2180 if (xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, NULL
,
2181 diff_words
, &xpp
, &xecfg
))
2182 die("unable to generate word diff");
2185 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
2186 diff_words
->plus
.text
.size
) {
2187 if (color_words_output_graph_prefix(diff_words
))
2188 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2189 line_prefix
, strlen(line_prefix
), 0);
2190 fn_out_diff_words_write_helper(diff_words
->opt
,
2191 &style
->ctx
, style
->newline
,
2192 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
2193 - diff_words
->current_plus
, diff_words
->current_plus
);
2195 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
2198 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
2199 static void diff_words_flush(struct emit_callback
*ecbdata
)
2201 struct diff_options
*wo
= ecbdata
->diff_words
->opt
;
2203 if (ecbdata
->diff_words
->minus
.text
.size
||
2204 ecbdata
->diff_words
->plus
.text
.size
)
2205 diff_words_show(ecbdata
->diff_words
);
2207 if (wo
->emitted_symbols
) {
2208 struct diff_options
*o
= ecbdata
->opt
;
2209 struct emitted_diff_symbols
*wol
= wo
->emitted_symbols
;
2214 * Instead of appending each, concat all words to a line?
2216 for (i
= 0; i
< wol
->nr
; i
++)
2217 append_emitted_diff_symbol(o
, &wol
->buf
[i
]);
2219 for (i
= 0; i
< wol
->nr
; i
++)
2220 free((void *)wol
->buf
[i
].line
);
2226 static void diff_filespec_load_driver(struct diff_filespec
*one
,
2227 struct index_state
*istate
)
2229 /* Use already-loaded driver */
2233 if (S_ISREG(one
->mode
))
2234 one
->driver
= userdiff_find_by_path(istate
, one
->path
);
2236 /* Fallback to default settings */
2238 one
->driver
= userdiff_find_by_name("default");
2241 static const char *userdiff_word_regex(struct diff_filespec
*one
,
2242 struct index_state
*istate
)
2244 diff_filespec_load_driver(one
, istate
);
2245 return one
->driver
->word_regex
;
2248 static void init_diff_words_data(struct emit_callback
*ecbdata
,
2249 struct diff_options
*orig_opts
,
2250 struct diff_filespec
*one
,
2251 struct diff_filespec
*two
)
2254 struct diff_options
*o
= xmalloc(sizeof(struct diff_options
));
2255 memcpy(o
, orig_opts
, sizeof(struct diff_options
));
2257 CALLOC_ARRAY(ecbdata
->diff_words
, 1);
2258 ecbdata
->diff_words
->type
= o
->word_diff
;
2259 ecbdata
->diff_words
->opt
= o
;
2261 if (orig_opts
->emitted_symbols
)
2262 CALLOC_ARRAY(o
->emitted_symbols
, 1);
2265 o
->word_regex
= userdiff_word_regex(one
, o
->repo
->index
);
2267 o
->word_regex
= userdiff_word_regex(two
, o
->repo
->index
);
2269 o
->word_regex
= diff_word_regex_cfg
;
2270 if (o
->word_regex
) {
2271 ecbdata
->diff_words
->word_regex
= (regex_t
*)
2272 xmalloc(sizeof(regex_t
));
2273 if (regcomp(ecbdata
->diff_words
->word_regex
,
2275 REG_EXTENDED
| REG_NEWLINE
))
2276 die("invalid regular expression: %s",
2279 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
2280 if (o
->word_diff
== diff_words_styles
[i
].type
) {
2281 ecbdata
->diff_words
->style
=
2282 &diff_words_styles
[i
];
2286 if (want_color(o
->use_color
)) {
2287 struct diff_words_style
*st
= ecbdata
->diff_words
->style
;
2288 st
->old_word
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
2289 st
->new_word
.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
2290 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_CONTEXT
);
2294 static void free_diff_words_data(struct emit_callback
*ecbdata
)
2296 if (ecbdata
->diff_words
) {
2297 diff_words_flush(ecbdata
);
2298 free_emitted_diff_symbols(ecbdata
->diff_words
->opt
->emitted_symbols
);
2299 free (ecbdata
->diff_words
->opt
);
2300 free (ecbdata
->diff_words
->minus
.text
.ptr
);
2301 free (ecbdata
->diff_words
->minus
.orig
);
2302 free (ecbdata
->diff_words
->plus
.text
.ptr
);
2303 free (ecbdata
->diff_words
->plus
.orig
);
2304 if (ecbdata
->diff_words
->word_regex
) {
2305 regfree(ecbdata
->diff_words
->word_regex
);
2306 free(ecbdata
->diff_words
->word_regex
);
2308 FREE_AND_NULL(ecbdata
->diff_words
);
2312 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
2314 if (want_color(diff_use_color
))
2315 return diff_colors
[ix
];
2319 const char *diff_line_prefix(struct diff_options
*opt
)
2321 return opt
->output_prefix
?
2322 opt
->output_prefix(opt
, opt
->output_prefix_data
) :
2326 static unsigned long sane_truncate_line(char *line
, unsigned long len
)
2329 unsigned long allot
;
2335 (void) utf8_width(&cp
, &l
);
2337 break; /* truncated in the middle? */
2342 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
2345 ecbdata
->lno_in_preimage
= 0;
2346 ecbdata
->lno_in_postimage
= 0;
2347 p
= strchr(line
, '-');
2349 return; /* cannot happen */
2350 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
2353 return; /* cannot happen */
2354 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
2357 static int fn_out_consume(void *priv
, char *line
, unsigned long len
)
2359 struct emit_callback
*ecbdata
= priv
;
2360 struct diff_options
*o
= ecbdata
->opt
;
2362 o
->found_changes
= 1;
2364 if (ecbdata
->header
) {
2365 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
2366 ecbdata
->header
->buf
, ecbdata
->header
->len
, 0);
2367 strbuf_reset(ecbdata
->header
);
2368 ecbdata
->header
= NULL
;
2371 if (ecbdata
->label_path
[0]) {
2372 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
2373 ecbdata
->label_path
[0],
2374 strlen(ecbdata
->label_path
[0]), 0);
2375 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
2376 ecbdata
->label_path
[1],
2377 strlen(ecbdata
->label_path
[1]), 0);
2378 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
2381 if (diff_suppress_blank_empty
2382 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
2387 if (line
[0] == '@') {
2388 if (ecbdata
->diff_words
)
2389 diff_words_flush(ecbdata
);
2390 len
= sane_truncate_line(line
, len
);
2391 find_lno(line
, ecbdata
);
2392 emit_hunk_header(ecbdata
, line
, len
);
2396 if (ecbdata
->diff_words
) {
2397 enum diff_symbol s
=
2398 ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
?
2399 DIFF_SYMBOL_WORDS_PORCELAIN
: DIFF_SYMBOL_WORDS
;
2400 if (line
[0] == '-') {
2401 diff_words_append(line
, len
,
2402 &ecbdata
->diff_words
->minus
);
2404 } else if (line
[0] == '+') {
2405 diff_words_append(line
, len
,
2406 &ecbdata
->diff_words
->plus
);
2408 } else if (starts_with(line
, "\\ ")) {
2410 * Eat the "no newline at eof" marker as if we
2411 * saw a "+" or "-" line with nothing on it,
2412 * and return without diff_words_flush() to
2413 * defer processing. If this is the end of
2414 * preimage, more "+" lines may come after it.
2418 diff_words_flush(ecbdata
);
2419 emit_diff_symbol(o
, s
, line
, len
, 0);
2425 ecbdata
->lno_in_postimage
++;
2426 emit_add_line(ecbdata
, line
+ 1, len
- 1);
2429 ecbdata
->lno_in_preimage
++;
2430 emit_del_line(ecbdata
, line
+ 1, len
- 1);
2433 ecbdata
->lno_in_postimage
++;
2434 ecbdata
->lno_in_preimage
++;
2435 emit_context_line(ecbdata
, line
+ 1, len
- 1);
2438 /* incomplete line at the end */
2439 ecbdata
->lno_in_preimage
++;
2440 emit_diff_symbol(o
, DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
2447 static void pprint_rename(struct strbuf
*name
, const char *a
, const char *b
)
2449 const char *old_name
= a
;
2450 const char *new_name
= b
;
2451 int pfx_length
, sfx_length
;
2452 int pfx_adjust_for_slash
;
2453 int len_a
= strlen(a
);
2454 int len_b
= strlen(b
);
2455 int a_midlen
, b_midlen
;
2456 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
2457 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
2459 if (qlen_a
|| qlen_b
) {
2460 quote_c_style(a
, name
, NULL
, 0);
2461 strbuf_addstr(name
, " => ");
2462 quote_c_style(b
, name
, NULL
, 0);
2466 /* Find common prefix */
2468 while (*old_name
&& *new_name
&& *old_name
== *new_name
) {
2469 if (*old_name
== '/')
2470 pfx_length
= old_name
- a
+ 1;
2475 /* Find common suffix */
2476 old_name
= a
+ len_a
;
2477 new_name
= b
+ len_b
;
2480 * If there is a common prefix, it must end in a slash. In
2481 * that case we let this loop run 1 into the prefix to see the
2484 * If there is no common prefix, we cannot do this as it would
2485 * underrun the input strings.
2487 pfx_adjust_for_slash
= (pfx_length
? 1 : 0);
2488 while (a
+ pfx_length
- pfx_adjust_for_slash
<= old_name
&&
2489 b
+ pfx_length
- pfx_adjust_for_slash
<= new_name
&&
2490 *old_name
== *new_name
) {
2491 if (*old_name
== '/')
2492 sfx_length
= len_a
- (old_name
- a
);
2498 * pfx{mid-a => mid-b}sfx
2499 * {pfx-a => pfx-b}sfx
2500 * pfx{sfx-a => sfx-b}
2503 a_midlen
= len_a
- pfx_length
- sfx_length
;
2504 b_midlen
= len_b
- pfx_length
- sfx_length
;
2510 strbuf_grow(name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
2511 if (pfx_length
+ sfx_length
) {
2512 strbuf_add(name
, a
, pfx_length
);
2513 strbuf_addch(name
, '{');
2515 strbuf_add(name
, a
+ pfx_length
, a_midlen
);
2516 strbuf_addstr(name
, " => ");
2517 strbuf_add(name
, b
+ pfx_length
, b_midlen
);
2518 if (pfx_length
+ sfx_length
) {
2519 strbuf_addch(name
, '}');
2520 strbuf_add(name
, a
+ len_a
- sfx_length
, sfx_length
);
2524 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
2528 struct diffstat_file
*x
;
2530 ALLOC_GROW(diffstat
->files
, diffstat
->nr
+ 1, diffstat
->alloc
);
2531 diffstat
->files
[diffstat
->nr
++] = x
;
2533 x
->from_name
= xstrdup(name_a
);
2534 x
->name
= xstrdup(name_b
);
2538 x
->from_name
= NULL
;
2539 x
->name
= xstrdup(name_a
);
2544 static int diffstat_consume(void *priv
, char *line
, unsigned long len
)
2546 struct diffstat_t
*diffstat
= priv
;
2547 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
2550 BUG("xdiff fed us an empty line");
2554 else if (line
[0] == '-')
2559 const char mime_boundary_leader
[] = "------------";
2561 static int scale_linear(int it
, int width
, int max_change
)
2566 * make sure that at least one '-' or '+' is printed if
2567 * there is any change to this path. The easiest way is to
2568 * scale linearly as if the allotted width is one column shorter
2569 * than it is, and then add 1 to the result.
2571 return 1 + (it
* (width
- 1) / max_change
);
2574 static void show_graph(struct strbuf
*out
, char ch
, int cnt
,
2575 const char *set
, const char *reset
)
2579 strbuf_addstr(out
, set
);
2580 strbuf_addchars(out
, ch
, cnt
);
2581 strbuf_addstr(out
, reset
);
2584 static void fill_print_name(struct diffstat_file
*file
)
2586 struct strbuf pname
= STRBUF_INIT
;
2588 if (file
->print_name
)
2591 if (file
->is_renamed
)
2592 pprint_rename(&pname
, file
->from_name
, file
->name
);
2594 quote_c_style(file
->name
, &pname
, NULL
, 0);
2597 strbuf_addf(&pname
, " (%s)", file
->comments
);
2599 file
->print_name
= strbuf_detach(&pname
, NULL
);
2602 static void print_stat_summary_inserts_deletes(struct diff_options
*options
,
2603 int files
, int insertions
, int deletions
)
2605 struct strbuf sb
= STRBUF_INIT
;
2608 assert(insertions
== 0 && deletions
== 0);
2609 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
2615 (files
== 1) ? " %d file changed" : " %d files changed",
2619 * For binary diff, the caller may want to print "x files
2620 * changed" with insertions == 0 && deletions == 0.
2622 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2623 * is probably less confusing (i.e skip over "2 files changed
2624 * but nothing about added/removed lines? Is this a bug in Git?").
2626 if (insertions
|| deletions
== 0) {
2628 (insertions
== 1) ? ", %d insertion(+)" : ", %d insertions(+)",
2632 if (deletions
|| insertions
== 0) {
2634 (deletions
== 1) ? ", %d deletion(-)" : ", %d deletions(-)",
2637 strbuf_addch(&sb
, '\n');
2638 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
2640 strbuf_release(&sb
);
2643 void print_stat_summary(FILE *fp
, int files
,
2644 int insertions
, int deletions
)
2646 struct diff_options o
;
2647 memset(&o
, 0, sizeof(o
));
2650 print_stat_summary_inserts_deletes(&o
, files
, insertions
, deletions
);
2653 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
2655 int i
, len
, add
, del
, adds
= 0, dels
= 0;
2656 uintmax_t max_change
= 0, max_len
= 0;
2657 int total_files
= data
->nr
, count
;
2658 int width
, name_width
, graph_width
, number_width
= 0, bin_width
= 0;
2659 const char *reset
, *add_c
, *del_c
;
2660 int extra_shown
= 0;
2661 const char *line_prefix
= diff_line_prefix(options
);
2662 struct strbuf out
= STRBUF_INIT
;
2667 count
= options
->stat_count
? options
->stat_count
: data
->nr
;
2669 reset
= diff_get_color_opt(options
, DIFF_RESET
);
2670 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
2671 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
2674 * Find the longest filename and max number of changes
2676 for (i
= 0; (i
< count
) && (i
< data
->nr
); i
++) {
2677 struct diffstat_file
*file
= data
->files
[i
];
2678 uintmax_t change
= file
->added
+ file
->deleted
;
2680 if (!file
->is_interesting
&& (change
== 0)) {
2681 count
++; /* not shown == room for one more */
2684 fill_print_name(file
);
2685 len
= utf8_strwidth(file
->print_name
);
2689 if (file
->is_unmerged
) {
2690 /* "Unmerged" is 8 characters */
2691 bin_width
= bin_width
< 8 ? 8 : bin_width
;
2694 if (file
->is_binary
) {
2695 /* "Bin XXX -> YYY bytes" */
2696 int w
= 14 + decimal_width(file
->added
)
2697 + decimal_width(file
->deleted
);
2698 bin_width
= bin_width
< w
? w
: bin_width
;
2699 /* Display change counts aligned with "Bin" */
2704 if (max_change
< change
)
2705 max_change
= change
;
2707 count
= i
; /* where we can stop scanning in data->files[] */
2710 * We have width = stat_width or term_columns() columns total.
2711 * We want a maximum of min(max_len, stat_name_width) for the name part.
2712 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
2713 * We also need 1 for " " and 4 + decimal_width(max_change)
2714 * for " | NNNN " and one the empty column at the end, altogether
2715 * 6 + decimal_width(max_change).
2717 * If there's not enough space, we will use the smaller of
2718 * stat_name_width (if set) and 5/8*width for the filename,
2719 * and the rest for constant elements + graph part, but no more
2720 * than stat_graph_width for the graph part.
2721 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2722 * for the standard terminal size).
2724 * In other words: stat_width limits the maximum width, and
2725 * stat_name_width fixes the maximum width of the filename,
2726 * and is also used to divide available columns if there
2729 * Binary files are displayed with "Bin XXX -> YYY bytes"
2730 * instead of the change count and graph. This part is treated
2731 * similarly to the graph part, except that it is not
2732 * "scaled". If total width is too small to accommodate the
2733 * guaranteed minimum width of the filename part and the
2734 * separators and this message, this message will "overflow"
2735 * making the line longer than the maximum width.
2739 * NEEDSWORK: line_prefix is often used for "log --graph" output
2740 * and contains ANSI-colored string. utf8_strnwidth() should be
2741 * used to correctly count the display width instead of strlen().
2743 if (options
->stat_width
== -1)
2744 width
= term_columns() - strlen(line_prefix
);
2746 width
= options
->stat_width
? options
->stat_width
: 80;
2747 number_width
= decimal_width(max_change
) > number_width
?
2748 decimal_width(max_change
) : number_width
;
2750 if (options
->stat_name_width
== -1)
2751 options
->stat_name_width
= diff_stat_name_width
;
2752 if (options
->stat_graph_width
== -1)
2753 options
->stat_graph_width
= diff_stat_graph_width
;
2756 * Guarantee 3/8*16 == 6 for the graph part
2757 * and 5/8*16 == 10 for the filename part
2759 if (width
< 16 + 6 + number_width
)
2760 width
= 16 + 6 + number_width
;
2763 * First assign sizes that are wanted, ignoring available width.
2764 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2765 * starting from "XXX" should fit in graph_width.
2767 graph_width
= max_change
+ 4 > bin_width
? max_change
: bin_width
- 4;
2768 if (options
->stat_graph_width
&&
2769 options
->stat_graph_width
< graph_width
)
2770 graph_width
= options
->stat_graph_width
;
2772 name_width
= (options
->stat_name_width
> 0 &&
2773 options
->stat_name_width
< max_len
) ?
2774 options
->stat_name_width
: max_len
;
2777 * Adjust adjustable widths not to exceed maximum width
2779 if (name_width
+ number_width
+ 6 + graph_width
> width
) {
2780 if (graph_width
> width
* 3/8 - number_width
- 6) {
2781 graph_width
= width
* 3/8 - number_width
- 6;
2782 if (graph_width
< 6)
2786 if (options
->stat_graph_width
&&
2787 graph_width
> options
->stat_graph_width
)
2788 graph_width
= options
->stat_graph_width
;
2789 if (name_width
> width
- number_width
- 6 - graph_width
)
2790 name_width
= width
- number_width
- 6 - graph_width
;
2792 graph_width
= width
- number_width
- 6 - name_width
;
2796 * From here name_width is the width of the name area,
2797 * and graph_width is the width of the graph area.
2798 * max_change is used to scale graph properly.
2800 for (i
= 0; i
< count
; i
++) {
2801 const char *prefix
= "";
2802 struct diffstat_file
*file
= data
->files
[i
];
2803 char *name
= file
->print_name
;
2804 uintmax_t added
= file
->added
;
2805 uintmax_t deleted
= file
->deleted
;
2806 int name_len
, padding
;
2808 if (!file
->is_interesting
&& (added
+ deleted
== 0))
2812 * "scale" the filename
2815 name_len
= utf8_strwidth(name
);
2816 if (name_width
< name_len
) {
2821 * NEEDSWORK: (name_len - len) counts the display
2822 * width, which would be shorter than the byte
2823 * length of the corresponding substring.
2824 * Advancing "name" by that number of bytes does
2825 * *NOT* skip over that many columns, so it is
2826 * very likely that chomping the pathname at the
2827 * slash we will find starting from "name" will
2828 * leave the resulting string still too long.
2830 name
+= name_len
- len
;
2831 slash
= strchr(name
, '/');
2835 padding
= len
- utf8_strwidth(name
);
2839 if (file
->is_binary
) {
2840 strbuf_addf(&out
, " %s%s%*s | %*s",
2841 prefix
, name
, padding
, "",
2842 number_width
, "Bin");
2843 if (!added
&& !deleted
) {
2844 strbuf_addch(&out
, '\n');
2845 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2846 out
.buf
, out
.len
, 0);
2850 strbuf_addf(&out
, " %s%"PRIuMAX
"%s",
2851 del_c
, deleted
, reset
);
2852 strbuf_addstr(&out
, " -> ");
2853 strbuf_addf(&out
, "%s%"PRIuMAX
"%s",
2854 add_c
, added
, reset
);
2855 strbuf_addstr(&out
, " bytes\n");
2856 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2857 out
.buf
, out
.len
, 0);
2861 else if (file
->is_unmerged
) {
2862 strbuf_addf(&out
, " %s%s%*s | %*s",
2863 prefix
, name
, padding
, "",
2864 number_width
, "Unmerged\n");
2865 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2866 out
.buf
, out
.len
, 0);
2872 * scale the add/delete
2877 if (graph_width
<= max_change
) {
2878 int total
= scale_linear(add
+ del
, graph_width
, max_change
);
2879 if (total
< 2 && add
&& del
)
2880 /* width >= 2 due to the sanity check */
2883 add
= scale_linear(add
, graph_width
, max_change
);
2886 del
= scale_linear(del
, graph_width
, max_change
);
2890 strbuf_addf(&out
, " %s%s%*s | %*"PRIuMAX
"%s",
2891 prefix
, name
, padding
, "",
2892 number_width
, added
+ deleted
,
2893 added
+ deleted
? " " : "");
2894 show_graph(&out
, '+', add
, add_c
, reset
);
2895 show_graph(&out
, '-', del
, del_c
, reset
);
2896 strbuf_addch(&out
, '\n');
2897 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2898 out
.buf
, out
.len
, 0);
2902 for (i
= 0; i
< data
->nr
; i
++) {
2903 struct diffstat_file
*file
= data
->files
[i
];
2904 uintmax_t added
= file
->added
;
2905 uintmax_t deleted
= file
->deleted
;
2907 if (file
->is_unmerged
||
2908 (!file
->is_interesting
&& (added
+ deleted
== 0))) {
2913 if (!file
->is_binary
) {
2920 emit_diff_symbol(options
,
2921 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
2926 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2927 strbuf_release(&out
);
2930 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
2932 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
2937 for (i
= 0; i
< data
->nr
; i
++) {
2938 int added
= data
->files
[i
]->added
;
2939 int deleted
= data
->files
[i
]->deleted
;
2941 if (data
->files
[i
]->is_unmerged
||
2942 (!data
->files
[i
]->is_interesting
&& (added
+ deleted
== 0))) {
2944 } else if (!data
->files
[i
]->is_binary
) { /* don't count bytes */
2949 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2952 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
2959 for (i
= 0; i
< data
->nr
; i
++) {
2960 struct diffstat_file
*file
= data
->files
[i
];
2962 fprintf(options
->file
, "%s", diff_line_prefix(options
));
2964 if (file
->is_binary
)
2965 fprintf(options
->file
, "-\t-\t");
2967 fprintf(options
->file
,
2968 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
2969 file
->added
, file
->deleted
);
2970 if (options
->line_termination
) {
2971 fill_print_name(file
);
2972 if (!file
->is_renamed
)
2973 write_name_quoted(file
->name
, options
->file
,
2974 options
->line_termination
);
2976 fputs(file
->print_name
, options
->file
);
2977 putc(options
->line_termination
, options
->file
);
2980 if (file
->is_renamed
) {
2981 putc('\0', options
->file
);
2982 write_name_quoted(file
->from_name
, options
->file
, '\0');
2984 write_name_quoted(file
->name
, options
->file
, '\0');
2989 struct dirstat_file
{
2991 unsigned long changed
;
2994 struct dirstat_dir
{
2995 struct dirstat_file
*files
;
2996 int alloc
, nr
, permille
, cumulative
;
2999 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
3000 unsigned long changed
, const char *base
, int baselen
)
3002 unsigned long sum_changes
= 0;
3003 unsigned int sources
= 0;
3004 const char *line_prefix
= diff_line_prefix(opt
);
3007 struct dirstat_file
*f
= dir
->files
;
3008 int namelen
= strlen(f
->name
);
3009 unsigned long changes
;
3012 if (namelen
< baselen
)
3014 if (memcmp(f
->name
, base
, baselen
))
3016 slash
= strchr(f
->name
+ baselen
, '/');
3018 int newbaselen
= slash
+ 1 - f
->name
;
3019 changes
= gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
3022 changes
= f
->changed
;
3027 sum_changes
+= changes
;
3031 * We don't report dirstat's for
3033 * - or cases where everything came from a single directory
3034 * under this directory (sources == 1).
3036 if (baselen
&& sources
!= 1) {
3038 int permille
= sum_changes
* 1000 / changed
;
3039 if (permille
>= dir
->permille
) {
3040 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
3041 permille
/ 10, permille
% 10, baselen
, base
);
3042 if (!dir
->cumulative
)
3050 static int dirstat_compare(const void *_a
, const void *_b
)
3052 const struct dirstat_file
*a
= _a
;
3053 const struct dirstat_file
*b
= _b
;
3054 return strcmp(a
->name
, b
->name
);
3057 static void conclude_dirstat(struct diff_options
*options
,
3058 struct dirstat_dir
*dir
,
3059 unsigned long changed
)
3061 struct dirstat_file
*to_free
= dir
->files
;
3064 /* This can happen even with many files, if everything was renames */
3067 /* Show all directories with more than x% of the changes */
3068 QSORT(dir
->files
, dir
->nr
, dirstat_compare
);
3069 gather_dirstat(options
, dir
, changed
, "", 0);
3075 static void show_dirstat(struct diff_options
*options
)
3078 unsigned long changed
;
3079 struct dirstat_dir dir
;
3080 struct diff_queue_struct
*q
= &diff_queued_diff
;
3085 dir
.permille
= options
->dirstat_permille
;
3086 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
3089 for (i
= 0; i
< q
->nr
; i
++) {
3090 struct diff_filepair
*p
= q
->queue
[i
];
3092 unsigned long copied
, added
, damage
;
3093 struct diff_populate_filespec_options dpf_options
= {
3094 .check_size_only
= 1,
3097 name
= p
->two
->path
? p
->two
->path
: p
->one
->path
;
3099 if (p
->one
->oid_valid
&& p
->two
->oid_valid
&&
3100 oideq(&p
->one
->oid
, &p
->two
->oid
)) {
3102 * The SHA1 has not changed, so pre-/post-content is
3103 * identical. We can therefore skip looking at the
3104 * file contents altogether.
3110 if (options
->flags
.dirstat_by_file
) {
3112 * In --dirstat-by-file mode, we don't really need to
3113 * look at the actual file contents at all.
3114 * The fact that the SHA1 changed is enough for us to
3115 * add this file to the list of results
3116 * (with each file contributing equal damage).
3122 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
3123 diff_populate_filespec(options
->repo
, p
->one
, NULL
);
3124 diff_populate_filespec(options
->repo
, p
->two
, NULL
);
3125 diffcore_count_changes(options
->repo
,
3126 p
->one
, p
->two
, NULL
, NULL
,
3128 diff_free_filespec_data(p
->one
);
3129 diff_free_filespec_data(p
->two
);
3130 } else if (DIFF_FILE_VALID(p
->one
)) {
3131 diff_populate_filespec(options
->repo
, p
->one
, &dpf_options
);
3133 diff_free_filespec_data(p
->one
);
3134 } else if (DIFF_FILE_VALID(p
->two
)) {
3135 diff_populate_filespec(options
->repo
, p
->two
, &dpf_options
);
3137 added
= p
->two
->size
;
3138 diff_free_filespec_data(p
->two
);
3143 * Original minus copied is the removed material,
3144 * added is the new material. They are both damages
3145 * made to the preimage.
3146 * If the resulting damage is zero, we know that
3147 * diffcore_count_changes() considers the two entries to
3148 * be identical, but since the oid changed, we
3149 * know that there must have been _some_ kind of change,
3150 * so we force all entries to have damage > 0.
3152 damage
= (p
->one
->size
- copied
) + added
;
3157 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3158 dir
.files
[dir
.nr
].name
= name
;
3159 dir
.files
[dir
.nr
].changed
= damage
;
3164 conclude_dirstat(options
, &dir
, changed
);
3167 static void show_dirstat_by_line(struct diffstat_t
*data
, struct diff_options
*options
)
3170 unsigned long changed
;
3171 struct dirstat_dir dir
;
3179 dir
.permille
= options
->dirstat_permille
;
3180 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
3183 for (i
= 0; i
< data
->nr
; i
++) {
3184 struct diffstat_file
*file
= data
->files
[i
];
3185 unsigned long damage
= file
->added
+ file
->deleted
;
3186 if (file
->is_binary
)
3188 * binary files counts bytes, not lines. Must find some
3189 * way to normalize binary bytes vs. textual lines.
3190 * The following heuristic assumes that there are 64
3192 * This is stupid and ugly, but very cheap...
3194 damage
= DIV_ROUND_UP(damage
, 64);
3195 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3196 dir
.files
[dir
.nr
].name
= file
->name
;
3197 dir
.files
[dir
.nr
].changed
= damage
;
3202 conclude_dirstat(options
, &dir
, changed
);
3205 static void free_diffstat_file(struct diffstat_file
*f
)
3207 free(f
->print_name
);
3213 void free_diffstat_info(struct diffstat_t
*diffstat
)
3216 for (i
= 0; i
< diffstat
->nr
; i
++)
3217 free_diffstat_file(diffstat
->files
[i
]);
3218 free(diffstat
->files
);
3221 struct checkdiff_t
{
3222 const char *filename
;
3224 int conflict_marker_size
;
3225 struct diff_options
*o
;
3230 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
3235 if (len
< marker_size
+ 1)
3237 firstchar
= line
[0];
3238 switch (firstchar
) {
3239 case '=': case '>': case '<': case '|':
3244 for (cnt
= 1; cnt
< marker_size
; cnt
++)
3245 if (line
[cnt
] != firstchar
)
3247 /* line[1] through line[marker_size-1] are same as firstchar */
3248 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
3253 static void checkdiff_consume_hunk(void *priv
,
3254 long ob UNUSED
, long on UNUSED
,
3255 long nb
, long nn UNUSED
,
3256 const char *func UNUSED
, long funclen UNUSED
)
3259 struct checkdiff_t
*data
= priv
;
3260 data
->lineno
= nb
- 1;
3263 static int checkdiff_consume(void *priv
, char *line
, unsigned long len
)
3265 struct checkdiff_t
*data
= priv
;
3266 int marker_size
= data
->conflict_marker_size
;
3267 const char *ws
= diff_get_color(data
->o
->use_color
, DIFF_WHITESPACE
);
3268 const char *reset
= diff_get_color(data
->o
->use_color
, DIFF_RESET
);
3269 const char *set
= diff_get_color(data
->o
->use_color
, DIFF_FILE_NEW
);
3271 const char *line_prefix
;
3274 line_prefix
= diff_line_prefix(data
->o
);
3276 if (line
[0] == '+') {
3279 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
3281 fprintf(data
->o
->file
,
3282 "%s%s:%d: leftover conflict marker\n",
3283 line_prefix
, data
->filename
, data
->lineno
);
3285 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
3288 data
->status
|= bad
;
3289 err
= whitespace_error_string(bad
);
3290 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
3291 line_prefix
, data
->filename
, data
->lineno
, err
);
3293 emit_line(data
->o
, set
, reset
, line
, 1);
3294 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
3295 data
->o
->file
, set
, reset
, ws
);
3296 } else if (line
[0] == ' ') {
3302 static unsigned char *deflate_it(char *data
,
3304 unsigned long *result_size
)
3307 unsigned char *deflated
;
3310 git_deflate_init(&stream
, zlib_compression_level
);
3311 bound
= git_deflate_bound(&stream
, size
);
3312 deflated
= xmalloc(bound
);
3313 stream
.next_out
= deflated
;
3314 stream
.avail_out
= bound
;
3316 stream
.next_in
= (unsigned char *)data
;
3317 stream
.avail_in
= size
;
3318 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
3320 git_deflate_end(&stream
);
3321 *result_size
= stream
.total_out
;
3325 static void emit_binary_diff_body(struct diff_options
*o
,
3326 mmfile_t
*one
, mmfile_t
*two
)
3332 unsigned long orig_size
;
3333 unsigned long delta_size
;
3334 unsigned long deflate_size
;
3335 unsigned long data_size
;
3337 /* We could do deflated delta, or we could do just deflated two,
3338 * whichever is smaller.
3341 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
3342 if (one
->size
&& two
->size
) {
3343 delta
= diff_delta(one
->ptr
, one
->size
,
3344 two
->ptr
, two
->size
,
3345 &delta_size
, deflate_size
);
3347 void *to_free
= delta
;
3348 orig_size
= delta_size
;
3349 delta
= deflate_it(delta
, delta_size
, &delta_size
);
3354 if (delta
&& delta_size
< deflate_size
) {
3355 char *s
= xstrfmt("%"PRIuMAX
, (uintmax_t)orig_size
);
3356 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
3361 data_size
= delta_size
;
3363 char *s
= xstrfmt("%lu", two
->size
);
3364 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
3369 data_size
= deflate_size
;
3372 /* emit data encoded in base85 */
3376 int bytes
= (52 < data_size
) ? 52 : data_size
;
3380 line
[0] = bytes
+ 'A' - 1;
3382 line
[0] = bytes
- 26 + 'a' - 1;
3383 encode_85(line
+ 1, cp
, bytes
);
3384 cp
= (char *) cp
+ bytes
;
3390 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_BODY
,
3393 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_FOOTER
, NULL
, 0, 0);
3397 static void emit_binary_diff(struct diff_options
*o
,
3398 mmfile_t
*one
, mmfile_t
*two
)
3400 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER
, NULL
, 0, 0);
3401 emit_binary_diff_body(o
, one
, two
);
3402 emit_binary_diff_body(o
, two
, one
);
3405 int diff_filespec_is_binary(struct repository
*r
,
3406 struct diff_filespec
*one
)
3408 struct diff_populate_filespec_options dpf_options
= {
3412 if (one
->is_binary
== -1) {
3413 diff_filespec_load_driver(one
, r
->index
);
3414 if (one
->driver
->binary
!= -1)
3415 one
->is_binary
= one
->driver
->binary
;
3417 if (!one
->data
&& DIFF_FILE_VALID(one
))
3418 diff_populate_filespec(r
, one
, &dpf_options
);
3419 if (one
->is_binary
== -1 && one
->data
)
3420 one
->is_binary
= buffer_is_binary(one
->data
,
3422 if (one
->is_binary
== -1)
3426 return one
->is_binary
;
3429 static const struct userdiff_funcname
*
3430 diff_funcname_pattern(struct diff_options
*o
, struct diff_filespec
*one
)
3432 diff_filespec_load_driver(one
, o
->repo
->index
);
3433 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
3436 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
3438 if (!options
->a_prefix
)
3439 options
->a_prefix
= a
;
3440 if (!options
->b_prefix
)
3441 options
->b_prefix
= b
;
3444 void diff_set_noprefix(struct diff_options
*options
)
3446 options
->a_prefix
= options
->b_prefix
= "";
3449 void diff_set_default_prefix(struct diff_options
*options
)
3451 options
->a_prefix
= diff_src_prefix
? diff_src_prefix
: "a/";
3452 options
->b_prefix
= diff_dst_prefix
? diff_dst_prefix
: "b/";
3455 struct userdiff_driver
*get_textconv(struct repository
*r
,
3456 struct diff_filespec
*one
)
3458 if (!DIFF_FILE_VALID(one
))
3461 diff_filespec_load_driver(one
, r
->index
);
3462 return userdiff_get_textconv(r
, one
->driver
);
3465 static struct string_list
*additional_headers(struct diff_options
*o
,
3468 if (!o
->additional_path_headers
)
3470 return strmap_get(o
->additional_path_headers
, path
);
3473 static void add_formatted_header(struct strbuf
*msg
,
3475 const char *line_prefix
,
3479 const char *next
, *newline
;
3481 for (next
= header
; *next
; next
= newline
) {
3482 newline
= strchrnul(next
, '\n');
3483 strbuf_addf(msg
, "%s%s%.*s%s\n", line_prefix
, meta
,
3484 (int)(newline
- next
), next
, reset
);
3490 static void add_formatted_headers(struct strbuf
*msg
,
3491 struct string_list
*more_headers
,
3492 const char *line_prefix
,
3498 for (i
= 0; i
< more_headers
->nr
; i
++)
3499 add_formatted_header(msg
, more_headers
->items
[i
].string
,
3500 line_prefix
, meta
, reset
);
3503 static int diff_filepair_is_phoney(struct diff_filespec
*one
,
3504 struct diff_filespec
*two
)
3507 * This function specifically looks for pairs injected by
3508 * create_filepairs_for_header_only_notifications(). Such
3509 * pairs are "phoney" in that they do not represent any
3510 * content or even mode difference, but were inserted because
3511 * diff_queued_diff previously had no pair associated with
3512 * that path but we needed some pair to avoid losing the
3513 * "remerge CONFLICT" header associated with the path.
3515 return !DIFF_FILE_VALID(one
) && !DIFF_FILE_VALID(two
);
3518 static int set_diff_algorithm(struct diff_options
*opts
,
3521 long value
= parse_algorithm_value(alg
);
3526 /* clear out previous settings */
3527 DIFF_XDL_CLR(opts
, NEED_MINIMAL
);
3528 opts
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
3529 opts
->xdl_opts
|= value
;
3534 static void builtin_diff(const char *name_a
,
3536 struct diff_filespec
*one
,
3537 struct diff_filespec
*two
,
3538 const char *xfrm_msg
,
3539 int must_show_header
,
3540 struct diff_options
*o
,
3541 int complete_rewrite
)
3545 char *a_one
, *b_two
;
3546 const char *meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
3547 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
3548 const char *a_prefix
, *b_prefix
;
3549 struct userdiff_driver
*textconv_one
= NULL
;
3550 struct userdiff_driver
*textconv_two
= NULL
;
3551 struct strbuf header
= STRBUF_INIT
;
3552 const char *line_prefix
= diff_line_prefix(o
);
3554 diff_set_mnemonic_prefix(o
, "a/", "b/");
3555 if (o
->flags
.reverse_diff
) {
3556 a_prefix
= o
->b_prefix
;
3557 b_prefix
= o
->a_prefix
;
3559 a_prefix
= o
->a_prefix
;
3560 b_prefix
= o
->b_prefix
;
3563 if (o
->submodule_format
== DIFF_SUBMODULE_LOG
&&
3564 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3565 (!two
->mode
|| S_ISGITLINK(two
->mode
)) &&
3566 (!diff_filepair_is_phoney(one
, two
))) {
3567 show_submodule_diff_summary(o
, one
->path
? one
->path
: two
->path
,
3568 &one
->oid
, &two
->oid
,
3569 two
->dirty_submodule
);
3570 o
->found_changes
= 1;
3572 } else if (o
->submodule_format
== DIFF_SUBMODULE_INLINE_DIFF
&&
3573 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3574 (!two
->mode
|| S_ISGITLINK(two
->mode
)) &&
3575 (!diff_filepair_is_phoney(one
, two
))) {
3576 show_submodule_inline_diff(o
, one
->path
? one
->path
: two
->path
,
3577 &one
->oid
, &two
->oid
,
3578 two
->dirty_submodule
);
3579 o
->found_changes
= 1;
3583 if (o
->flags
.allow_textconv
) {
3584 textconv_one
= get_textconv(o
->repo
, one
);
3585 textconv_two
= get_textconv(o
->repo
, two
);
3588 /* Never use a non-valid filename anywhere if at all possible */
3589 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
3590 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
3592 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
3593 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
3594 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
3595 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
3596 if (diff_filepair_is_phoney(one
, two
)) {
3598 * We should only reach this point for pairs generated from
3599 * create_filepairs_for_header_only_notifications(). For
3600 * these, we want to avoid the "/dev/null" special casing
3601 * above, because we do not want such pairs shown as either
3602 * "new file" or "deleted file" below.
3607 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
3608 if (lbl
[0][0] == '/') {
3610 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3612 strbuf_addstr(&header
, xfrm_msg
);
3613 o
->found_changes
= 1;
3614 must_show_header
= 1;
3616 else if (lbl
[1][0] == '/') {
3617 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3619 strbuf_addstr(&header
, xfrm_msg
);
3620 o
->found_changes
= 1;
3621 must_show_header
= 1;
3624 if (one
->mode
!= two
->mode
) {
3625 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3626 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3627 o
->found_changes
= 1;
3628 must_show_header
= 1;
3631 strbuf_addstr(&header
, xfrm_msg
);
3634 * we do not run diff between different kind
3637 if ((one
->mode
^ two
->mode
) & S_IFMT
)
3638 goto free_ab_and_return
;
3639 if (complete_rewrite
&&
3640 (textconv_one
|| !diff_filespec_is_binary(o
->repo
, one
)) &&
3641 (textconv_two
|| !diff_filespec_is_binary(o
->repo
, two
))) {
3642 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3643 header
.buf
, header
.len
, 0);
3644 strbuf_reset(&header
);
3645 emit_rewrite_diff(name_a
, name_b
, one
, two
,
3646 textconv_one
, textconv_two
, o
);
3647 o
->found_changes
= 1;
3648 goto free_ab_and_return
;
3652 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
3653 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
,
3655 strbuf_reset(&header
);
3656 goto free_ab_and_return
;
3657 } else if (!o
->flags
.text
&&
3658 ( (!textconv_one
&& diff_filespec_is_binary(o
->repo
, one
)) ||
3659 (!textconv_two
&& diff_filespec_is_binary(o
->repo
, two
)) )) {
3660 struct strbuf sb
= STRBUF_INIT
;
3661 if (!one
->data
&& !two
->data
&&
3662 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
3664 if (oideq(&one
->oid
, &two
->oid
)) {
3665 if (must_show_header
)
3666 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3667 header
.buf
, header
.len
,
3669 goto free_ab_and_return
;
3671 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3672 header
.buf
, header
.len
, 0);
3673 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3674 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3675 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3677 strbuf_release(&sb
);
3678 o
->found_changes
= 1;
3679 goto free_ab_and_return
;
3681 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3682 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3683 die("unable to read files to diff");
3684 /* Quite common confusing case */
3685 if (mf1
.size
== mf2
.size
&&
3686 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
3687 if (must_show_header
)
3688 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3689 header
.buf
, header
.len
, 0);
3690 goto free_ab_and_return
;
3692 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
, header
.len
, 0);
3693 strbuf_reset(&header
);
3694 if (o
->flags
.binary
)
3695 emit_binary_diff(o
, &mf1
, &mf2
);
3697 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3698 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3699 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3701 strbuf_release(&sb
);
3703 o
->found_changes
= 1;
3705 /* Crazy xdl interfaces.. */
3706 const char *diffopts
;
3710 struct emit_callback ecbdata
;
3711 const struct userdiff_funcname
*pe
;
3713 if (must_show_header
) {
3714 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3715 header
.buf
, header
.len
, 0);
3716 strbuf_reset(&header
);
3719 mf1
.size
= fill_textconv(o
->repo
, textconv_one
, one
, &mf1
.ptr
);
3720 mf2
.size
= fill_textconv(o
->repo
, textconv_two
, two
, &mf2
.ptr
);
3722 pe
= diff_funcname_pattern(o
, one
);
3724 pe
= diff_funcname_pattern(o
, two
);
3726 memset(&xpp
, 0, sizeof(xpp
));
3727 memset(&xecfg
, 0, sizeof(xecfg
));
3728 memset(&ecbdata
, 0, sizeof(ecbdata
));
3729 if (o
->flags
.suppress_diff_headers
)
3731 ecbdata
.label_path
= lbl
;
3732 ecbdata
.color_diff
= want_color(o
->use_color
);
3733 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
3734 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
3735 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3737 if (header
.len
&& !o
->flags
.suppress_diff_headers
)
3738 ecbdata
.header
= &header
;
3739 xpp
.flags
= o
->xdl_opts
;
3740 xpp
.ignore_regex
= o
->ignore_regex
;
3741 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3742 xpp
.anchors
= o
->anchors
;
3743 xpp
.anchors_nr
= o
->anchors_nr
;
3744 xecfg
.ctxlen
= o
->context
;
3745 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3746 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3747 if (o
->flags
.funccontext
)
3748 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
3750 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
3752 diffopts
= getenv("GIT_DIFF_OPTS");
3755 else if (skip_prefix(diffopts
, "--unified=", &v
))
3756 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3757 else if (skip_prefix(diffopts
, "-u", &v
))
3758 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3761 init_diff_words_data(&ecbdata
, o
, one
, two
);
3762 if (xdi_diff_outf(&mf1
, &mf2
, NULL
, fn_out_consume
,
3763 &ecbdata
, &xpp
, &xecfg
))
3764 die("unable to generate diff for %s", one
->path
);
3766 free_diff_words_data(&ecbdata
);
3771 xdiff_clear_find_func(&xecfg
);
3775 strbuf_release(&header
);
3776 diff_free_filespec_data(one
);
3777 diff_free_filespec_data(two
);
3783 static const char *get_compact_summary(const struct diff_filepair
*p
, int is_renamed
)
3786 if (p
->status
== DIFF_STATUS_ADDED
) {
3787 if (S_ISLNK(p
->two
->mode
))
3789 else if ((p
->two
->mode
& 0777) == 0755)
3793 } else if (p
->status
== DIFF_STATUS_DELETED
)
3796 if (S_ISLNK(p
->one
->mode
) && !S_ISLNK(p
->two
->mode
))
3798 else if (!S_ISLNK(p
->one
->mode
) && S_ISLNK(p
->two
->mode
))
3800 else if ((p
->one
->mode
& 0777) == 0644 &&
3801 (p
->two
->mode
& 0777) == 0755)
3803 else if ((p
->one
->mode
& 0777) == 0755 &&
3804 (p
->two
->mode
& 0777) == 0644)
3809 static void builtin_diffstat(const char *name_a
, const char *name_b
,
3810 struct diff_filespec
*one
,
3811 struct diff_filespec
*two
,
3812 struct diffstat_t
*diffstat
,
3813 struct diff_options
*o
,
3814 struct diff_filepair
*p
)
3817 struct diffstat_file
*data
;
3819 int complete_rewrite
= 0;
3821 if (!DIFF_PAIR_UNMERGED(p
)) {
3822 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
3823 complete_rewrite
= 1;
3826 data
= diffstat_add(diffstat
, name_a
, name_b
);
3827 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
3828 if (o
->flags
.stat_with_summary
)
3829 data
->comments
= get_compact_summary(p
, data
->is_renamed
);
3832 data
->is_unmerged
= 1;
3836 /* saves some reads if true, not a guarantee of diff outcome */
3837 may_differ
= !(one
->oid_valid
&& two
->oid_valid
&&
3838 oideq(&one
->oid
, &two
->oid
));
3840 if (diff_filespec_is_binary(o
->repo
, one
) ||
3841 diff_filespec_is_binary(o
->repo
, two
)) {
3842 data
->is_binary
= 1;
3847 data
->added
= diff_filespec_size(o
->repo
, two
);
3848 data
->deleted
= diff_filespec_size(o
->repo
, one
);
3852 else if (complete_rewrite
) {
3853 diff_populate_filespec(o
->repo
, one
, NULL
);
3854 diff_populate_filespec(o
->repo
, two
, NULL
);
3855 data
->deleted
= count_lines(one
->data
, one
->size
);
3856 data
->added
= count_lines(two
->data
, two
->size
);
3859 else if (may_differ
) {
3860 /* Crazy xdl interfaces.. */
3864 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3865 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3866 die("unable to read files to diff");
3868 memset(&xpp
, 0, sizeof(xpp
));
3869 memset(&xecfg
, 0, sizeof(xecfg
));
3870 xpp
.flags
= o
->xdl_opts
;
3871 xpp
.ignore_regex
= o
->ignore_regex
;
3872 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3873 xpp
.anchors
= o
->anchors
;
3874 xpp
.anchors_nr
= o
->anchors_nr
;
3875 xecfg
.ctxlen
= o
->context
;
3876 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3877 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
3878 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
3879 diffstat_consume
, diffstat
, &xpp
, &xecfg
))
3880 die("unable to generate diffstat for %s", one
->path
);
3882 if (DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
)) {
3883 struct diffstat_file
*file
=
3884 diffstat
->files
[diffstat
->nr
- 1];
3886 * Omit diffstats of modified files where nothing changed.
3887 * Even if may_differ, this might be the case due to
3888 * ignoring whitespace changes, etc.
3890 * But note that we special-case additions, deletions,
3891 * renames, and mode changes as adding an empty file,
3892 * for example is still of interest.
3894 if ((p
->status
== DIFF_STATUS_MODIFIED
)
3897 && one
->mode
== two
->mode
) {
3898 free_diffstat_file(file
);
3904 diff_free_filespec_data(one
);
3905 diff_free_filespec_data(two
);
3908 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
3909 const char *attr_path
,
3910 struct diff_filespec
*one
,
3911 struct diff_filespec
*two
,
3912 struct diff_options
*o
)
3915 struct checkdiff_t data
;
3920 memset(&data
, 0, sizeof(data
));
3921 data
.filename
= name_b
? name_b
: name_a
;
3924 data
.ws_rule
= whitespace_rule(o
->repo
->index
, attr_path
);
3925 data
.conflict_marker_size
= ll_merge_marker_size(o
->repo
->index
, attr_path
);
3927 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3928 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3929 die("unable to read files to diff");
3932 * All the other codepaths check both sides, but not checking
3933 * the "old" side here is deliberate. We are checking the newly
3934 * introduced changes, and as long as the "new" side is text, we
3935 * can and should check what it introduces.
3937 if (diff_filespec_is_binary(o
->repo
, two
))
3938 goto free_and_return
;
3940 /* Crazy xdl interfaces.. */
3944 memset(&xpp
, 0, sizeof(xpp
));
3945 memset(&xecfg
, 0, sizeof(xecfg
));
3946 xecfg
.ctxlen
= 1; /* at least one context line */
3948 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume_hunk
,
3949 checkdiff_consume
, &data
,
3951 die("unable to generate checkdiff for %s", one
->path
);
3953 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
3954 struct emit_callback ecbdata
;
3957 ecbdata
.ws_rule
= data
.ws_rule
;
3958 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3959 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
3964 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
3965 fprintf(o
->file
, "%s:%d: %s.\n",
3966 data
.filename
, blank_at_eof
, err
);
3967 data
.status
= 1; /* report errors */
3972 diff_free_filespec_data(one
);
3973 diff_free_filespec_data(two
);
3975 o
->flags
.check_failed
= 1;
3978 struct diff_filespec
*alloc_filespec(const char *path
)
3980 struct diff_filespec
*spec
;
3982 FLEXPTR_ALLOC_STR(spec
, path
, path
);
3984 spec
->is_binary
= -1;
3988 void free_filespec(struct diff_filespec
*spec
)
3990 if (!--spec
->count
) {
3991 diff_free_filespec_data(spec
);
3996 void fill_filespec(struct diff_filespec
*spec
, const struct object_id
*oid
,
3997 int oid_valid
, unsigned short mode
)
4000 spec
->mode
= canon_mode(mode
);
4001 oidcpy(&spec
->oid
, oid
);
4002 spec
->oid_valid
= oid_valid
;
4007 * Given a name and sha1 pair, if the index tells us the file in
4008 * the work tree has that object contents, return true, so that
4009 * prepare_temp_file() does not have to inflate and extract.
4011 static int reuse_worktree_file(struct index_state
*istate
,
4013 const struct object_id
*oid
,
4016 const struct cache_entry
*ce
;
4021 * We do not read the cache ourselves here, because the
4022 * benchmark with my previous version that always reads cache
4023 * shows that it makes things worse for diff-tree comparing
4024 * two linux-2.6 kernel trees in an already checked out work
4025 * tree. This is because most diff-tree comparisons deal with
4026 * only a small number of files, while reading the cache is
4027 * expensive for a large project, and its cost outweighs the
4028 * savings we get by not inflating the object to a temporary
4029 * file. Practically, this code only helps when we are used
4030 * by diff-cache --cached, which does read the cache before
4036 /* We want to avoid the working directory if our caller
4037 * doesn't need the data in a normal file, this system
4038 * is rather slow with its stat/open/mmap/close syscalls,
4039 * and the object is contained in a pack file. The pack
4040 * is probably already open and will be faster to obtain
4041 * the data through than the working directory. Loose
4042 * objects however would tend to be slower as they need
4043 * to be individually opened and inflated.
4045 if (!FAST_WORKING_DIRECTORY
&& !want_file
&&
4046 has_object_pack(istate
->repo
, oid
))
4050 * Similarly, if we'd have to convert the file contents anyway, that
4051 * makes the optimization not worthwhile.
4053 if (!want_file
&& would_convert_to_git(istate
, name
))
4057 * If this path does not match our sparse-checkout definition,
4058 * then the file will not be in the working directory.
4060 if (!path_in_sparse_checkout(name
, istate
))
4064 pos
= index_name_pos(istate
, name
, len
);
4067 ce
= istate
->cache
[pos
];
4070 * This is not the sha1 we are looking for, or
4071 * unreusable because it is not a regular file.
4073 if (!oideq(oid
, &ce
->oid
) || !S_ISREG(ce
->ce_mode
))
4077 * If ce is marked as "assume unchanged", there is no
4078 * guarantee that work tree matches what we are looking for.
4080 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
4084 * If ce matches the file in the work tree, we can reuse it.
4086 if (ce_uptodate(ce
) ||
4087 (!lstat(name
, &st
) && !ie_match_stat(istate
, ce
, &st
, 0)))
4093 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
4095 struct strbuf buf
= STRBUF_INIT
;
4096 const char *dirty
= "";
4098 /* Are we looking at the work tree? */
4099 if (s
->dirty_submodule
)
4102 strbuf_addf(&buf
, "Subproject commit %s%s\n",
4103 oid_to_hex(&s
->oid
), dirty
);
4107 strbuf_release(&buf
);
4109 s
->data
= strbuf_detach(&buf
, NULL
);
4116 * While doing rename detection and pickaxe operation, we may need to
4117 * grab the data for the blob (or file) for our own in-core comparison.
4118 * diff_filespec has data and size fields for this purpose.
4120 int diff_populate_filespec(struct repository
*r
,
4121 struct diff_filespec
*s
,
4122 const struct diff_populate_filespec_options
*options
)
4124 int size_only
= options
? options
->check_size_only
: 0;
4125 int check_binary
= options
? options
->check_binary
: 0;
4127 int conv_flags
= global_conv_flags_eol
;
4129 * demote FAIL to WARN to allow inspecting the situation
4130 * instead of refusing.
4132 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
4133 conv_flags
= CONV_EOL_RNDTRP_WARN
;
4135 if (!DIFF_FILE_VALID(s
))
4136 die("internal error: asking to populate invalid file.");
4137 if (S_ISDIR(s
->mode
))
4143 if (size_only
&& 0 < s
->size
)
4146 if (S_ISGITLINK(s
->mode
))
4147 return diff_populate_gitlink(s
, size_only
);
4149 if (!s
->oid_valid
||
4150 reuse_worktree_file(r
->index
, s
->path
, &s
->oid
, 0)) {
4151 struct strbuf buf
= STRBUF_INIT
;
4155 if (lstat(s
->path
, &st
) < 0) {
4159 s
->data
= (char *)"";
4163 s
->size
= xsize_t(st
.st_size
);
4166 if (S_ISLNK(st
.st_mode
)) {
4167 struct strbuf sb
= STRBUF_INIT
;
4169 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
4172 s
->data
= strbuf_detach(&sb
, NULL
);
4178 * Even if the caller would be happy with getting
4179 * only the size, we cannot return early at this
4180 * point if the path requires us to run the content
4183 if (size_only
&& !would_convert_to_git(r
->index
, s
->path
))
4187 * Note: this check uses xsize_t(st.st_size) that may
4188 * not be the true size of the blob after it goes
4189 * through convert_to_git(). This may not strictly be
4190 * correct, but the whole point of big_file_threshold
4191 * and is_binary check being that we want to avoid
4192 * opening the file and inspecting the contents, this
4196 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4200 fd
= open(s
->path
, O_RDONLY
);
4203 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
4205 s
->should_munmap
= 1;
4208 * Convert from working tree format to canonical git format
4210 if (convert_to_git(r
->index
, s
->path
, s
->data
, s
->size
, &buf
, conv_flags
)) {
4212 munmap(s
->data
, s
->size
);
4213 s
->should_munmap
= 0;
4214 s
->data
= strbuf_detach(&buf
, &size
);
4220 struct object_info info
= {
4224 if (!(size_only
|| check_binary
))
4226 * Set contentp, since there is no chance that merely
4227 * the size is sufficient.
4229 info
.contentp
= &s
->data
;
4231 if (options
&& options
->missing_object_cb
) {
4232 if (!oid_object_info_extended(r
, &s
->oid
, &info
,
4233 OBJECT_INFO_LOOKUP_REPLACE
|
4234 OBJECT_INFO_SKIP_FETCH_OBJECT
))
4236 options
->missing_object_cb(options
->missing_object_data
);
4238 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4239 OBJECT_INFO_LOOKUP_REPLACE
))
4240 die("unable to read %s", oid_to_hex(&s
->oid
));
4243 if (size_only
|| check_binary
) {
4246 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4251 if (!info
.contentp
) {
4252 info
.contentp
= &s
->data
;
4253 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4254 OBJECT_INFO_LOOKUP_REPLACE
))
4255 die("unable to read %s", oid_to_hex(&s
->oid
));
4262 void diff_free_filespec_blob(struct diff_filespec
*s
)
4266 else if (s
->should_munmap
)
4267 munmap(s
->data
, s
->size
);
4269 if (s
->should_free
|| s
->should_munmap
) {
4270 s
->should_free
= s
->should_munmap
= 0;
4275 void diff_free_filespec_data(struct diff_filespec
*s
)
4280 diff_free_filespec_blob(s
);
4281 FREE_AND_NULL(s
->cnt_data
);
4284 static void prep_temp_blob(struct index_state
*istate
,
4285 const char *path
, struct diff_tempfile
*temp
,
4288 const struct object_id
*oid
,
4291 struct strbuf buf
= STRBUF_INIT
;
4292 char *path_dup
= xstrdup(path
);
4293 const char *base
= basename(path_dup
);
4294 struct checkout_metadata meta
;
4296 init_checkout_metadata(&meta
, NULL
, NULL
, oid
);
4298 temp
->tempfile
= mks_tempfile_dt("git-blob-XXXXXX", base
);
4299 if (!temp
->tempfile
)
4300 die_errno("unable to create temp-file");
4301 if (convert_to_working_tree(istate
, path
,
4302 (const char *)blob
, (size_t)size
, &buf
, &meta
)) {
4306 if (write_in_full(temp
->tempfile
->fd
, blob
, size
) < 0 ||
4307 close_tempfile_gently(temp
->tempfile
))
4308 die_errno("unable to write temp-file");
4309 temp
->name
= get_tempfile_path(temp
->tempfile
);
4310 oid_to_hex_r(temp
->hex
, oid
);
4311 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
4312 strbuf_release(&buf
);
4316 static struct diff_tempfile
*prepare_temp_file(struct repository
*r
,
4317 struct diff_filespec
*one
)
4319 struct diff_tempfile
*temp
= claim_diff_tempfile();
4321 if (!DIFF_FILE_VALID(one
)) {
4323 /* A '-' entry produces this for file-2, and
4324 * a '+' entry produces this for file-1.
4326 temp
->name
= "/dev/null";
4327 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
4328 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
4332 if (!S_ISGITLINK(one
->mode
) &&
4334 reuse_worktree_file(r
->index
, one
->path
, &one
->oid
, 1))) {
4336 if (lstat(one
->path
, &st
) < 0) {
4337 if (errno
== ENOENT
)
4338 goto not_a_valid_file
;
4339 die_errno("stat(%s)", one
->path
);
4341 if (S_ISLNK(st
.st_mode
)) {
4342 struct strbuf sb
= STRBUF_INIT
;
4343 if (strbuf_readlink(&sb
, one
->path
, st
.st_size
) < 0)
4344 die_errno("readlink(%s)", one
->path
);
4345 prep_temp_blob(r
->index
, one
->path
, temp
, sb
.buf
, sb
.len
,
4347 &one
->oid
: null_oid()),
4349 one
->mode
: S_IFLNK
));
4350 strbuf_release(&sb
);
4353 /* we can borrow from the file in the work tree */
4354 temp
->name
= one
->path
;
4355 if (!one
->oid_valid
)
4356 oid_to_hex_r(temp
->hex
, null_oid());
4358 oid_to_hex_r(temp
->hex
, &one
->oid
);
4359 /* Even though we may sometimes borrow the
4360 * contents from the work tree, we always want
4361 * one->mode. mode is trustworthy even when
4362 * !(one->oid_valid), as long as
4363 * DIFF_FILE_VALID(one).
4365 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
4370 if (diff_populate_filespec(r
, one
, NULL
))
4371 die("cannot read data blob for %s", one
->path
);
4372 prep_temp_blob(r
->index
, one
->path
, temp
,
4373 one
->data
, one
->size
,
4374 &one
->oid
, one
->mode
);
4379 static void add_external_diff_name(struct repository
*r
,
4380 struct strvec
*argv
,
4381 struct diff_filespec
*df
)
4383 struct diff_tempfile
*temp
= prepare_temp_file(r
, df
);
4384 strvec_push(argv
, temp
->name
);
4385 strvec_push(argv
, temp
->hex
);
4386 strvec_push(argv
, temp
->mode
);
4389 /* An external diff command takes:
4391 * diff-cmd name infile1 infile1-sha1 infile1-mode \
4392 * infile2 infile2-sha1 infile2-mode [ rename-to ]
4395 static void run_external_diff(const struct external_diff
*pgm
,
4398 struct diff_filespec
*one
,
4399 struct diff_filespec
*two
,
4400 const char *xfrm_msg
,
4401 struct diff_options
*o
)
4403 struct child_process cmd
= CHILD_PROCESS_INIT
;
4404 struct diff_queue_struct
*q
= &diff_queued_diff
;
4405 int quiet
= !(o
->output_format
& DIFF_FORMAT_PATCH
);
4409 * Trivial equality is handled by diff_unmodified_pair() before
4410 * we get here. If we don't need to show the diff and the
4411 * external diff program lacks the ability to tell us whether
4412 * it's empty then we consider it non-empty without even asking.
4414 if (!pgm
->trust_exit_code
&& quiet
) {
4415 o
->found_changes
= 1;
4419 strvec_push(&cmd
.args
, pgm
->cmd
);
4420 strvec_push(&cmd
.args
, name
);
4423 add_external_diff_name(o
->repo
, &cmd
.args
, one
);
4424 add_external_diff_name(o
->repo
, &cmd
.args
, two
);
4426 strvec_push(&cmd
.args
, other
);
4428 strvec_push(&cmd
.args
, xfrm_msg
);
4432 strvec_pushf(&cmd
.env
, "GIT_DIFF_PATH_COUNTER=%d",
4433 ++o
->diff_path_counter
);
4434 strvec_pushf(&cmd
.env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
4436 diff_free_filespec_data(one
);
4437 diff_free_filespec_data(two
);
4439 cmd
.no_stdout
= quiet
;
4440 rc
= run_command(&cmd
);
4441 if (!pgm
->trust_exit_code
&& rc
== 0)
4442 o
->found_changes
= 1;
4443 else if (pgm
->trust_exit_code
&& rc
== 0)
4445 else if (pgm
->trust_exit_code
&& rc
== 1)
4446 o
->found_changes
= 1;
4448 die(_("external diff died, stopping at %s"), name
);
4453 static int similarity_index(struct diff_filepair
*p
)
4455 return p
->score
* 100 / MAX_SCORE
;
4458 static const char *diff_abbrev_oid(const struct object_id
*oid
, int abbrev
)
4460 if (startup_info
->have_repository
)
4461 return repo_find_unique_abbrev(the_repository
, oid
, abbrev
);
4463 char *hex
= oid_to_hex(oid
);
4465 abbrev
= FALLBACK_DEFAULT_ABBREV
;
4466 if (abbrev
> the_hash_algo
->hexsz
)
4467 BUG("oid abbreviation out of range: %d", abbrev
);
4474 static void fill_metainfo(struct strbuf
*msg
,
4477 struct diff_filespec
*one
,
4478 struct diff_filespec
*two
,
4479 struct diff_options
*o
,
4480 struct diff_filepair
*p
,
4481 int *must_show_header
,
4484 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
4485 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
4486 const char *line_prefix
= diff_line_prefix(o
);
4487 struct string_list
*more_headers
= NULL
;
4489 *must_show_header
= 1;
4490 strbuf_init(msg
, PATH_MAX
* 2 + 300);
4491 switch (p
->status
) {
4492 case DIFF_STATUS_COPIED
:
4493 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4494 line_prefix
, set
, similarity_index(p
));
4495 strbuf_addf(msg
, "%s\n%s%scopy from ",
4496 reset
, line_prefix
, set
);
4497 quote_c_style(name
, msg
, NULL
, 0);
4498 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
4499 quote_c_style(other
, msg
, NULL
, 0);
4500 strbuf_addf(msg
, "%s\n", reset
);
4502 case DIFF_STATUS_RENAMED
:
4503 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4504 line_prefix
, set
, similarity_index(p
));
4505 strbuf_addf(msg
, "%s\n%s%srename from ",
4506 reset
, line_prefix
, set
);
4507 quote_c_style(name
, msg
, NULL
, 0);
4508 strbuf_addf(msg
, "%s\n%s%srename to ",
4509 reset
, line_prefix
, set
);
4510 quote_c_style(other
, msg
, NULL
, 0);
4511 strbuf_addf(msg
, "%s\n", reset
);
4513 case DIFF_STATUS_MODIFIED
:
4515 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
4517 set
, similarity_index(p
), reset
);
4522 *must_show_header
= 0;
4524 if ((more_headers
= additional_headers(o
, name
))) {
4525 add_formatted_headers(msg
, more_headers
,
4526 line_prefix
, set
, reset
);
4527 *must_show_header
= 1;
4529 if (one
&& two
&& !oideq(&one
->oid
, &two
->oid
)) {
4530 const unsigned hexsz
= the_hash_algo
->hexsz
;
4531 int abbrev
= o
->abbrev
? o
->abbrev
: DEFAULT_ABBREV
;
4533 if (o
->flags
.full_index
)
4536 if (o
->flags
.binary
) {
4538 if ((!fill_mmfile(o
->repo
, &mf
, one
) &&
4539 diff_filespec_is_binary(o
->repo
, one
)) ||
4540 (!fill_mmfile(o
->repo
, &mf
, two
) &&
4541 diff_filespec_is_binary(o
->repo
, two
)))
4544 strbuf_addf(msg
, "%s%sindex %s..%s", line_prefix
, set
,
4545 diff_abbrev_oid(&one
->oid
, abbrev
),
4546 diff_abbrev_oid(&two
->oid
, abbrev
));
4547 if (one
->mode
== two
->mode
)
4548 strbuf_addf(msg
, " %06o", one
->mode
);
4549 strbuf_addf(msg
, "%s\n", reset
);
4553 static void run_diff_cmd(const struct external_diff
*pgm
,
4556 const char *attr_path
,
4557 struct diff_filespec
*one
,
4558 struct diff_filespec
*two
,
4560 struct diff_options
*o
,
4561 struct diff_filepair
*p
)
4563 const char *xfrm_msg
= NULL
;
4564 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
4565 int must_show_header
= 0;
4566 struct userdiff_driver
*drv
= NULL
;
4568 if (o
->flags
.allow_external
|| !o
->ignore_driver_algorithm
)
4569 drv
= userdiff_find_by_path(o
->repo
->index
, attr_path
);
4571 if (o
->flags
.allow_external
&& drv
&& drv
->external
.cmd
)
4572 pgm
= &drv
->external
;
4576 * don't use colors when the header is intended for an
4577 * external diff driver
4579 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
4581 want_color(o
->use_color
) && !pgm
);
4582 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
4586 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
, o
);
4590 if (!o
->ignore_driver_algorithm
&& drv
&& drv
->algorithm
)
4591 set_diff_algorithm(o
, drv
->algorithm
);
4593 builtin_diff(name
, other
? other
: name
,
4594 one
, two
, xfrm_msg
, must_show_header
,
4595 o
, complete_rewrite
);
4596 if (p
->status
== DIFF_STATUS_COPIED
||
4597 p
->status
== DIFF_STATUS_RENAMED
)
4598 o
->found_changes
= 1;
4600 fprintf(o
->file
, "* Unmerged path %s\n", name
);
4601 o
->found_changes
= 1;
4605 static void diff_fill_oid_info(struct diff_filespec
*one
, struct index_state
*istate
)
4607 if (DIFF_FILE_VALID(one
)) {
4608 if (!one
->oid_valid
) {
4610 if (one
->is_stdin
) {
4611 oidclr(&one
->oid
, the_repository
->hash_algo
);
4614 if (lstat(one
->path
, &st
) < 0)
4615 die_errno("stat '%s'", one
->path
);
4616 if (index_path(istate
, &one
->oid
, one
->path
, &st
, 0))
4617 die("cannot hash %s", one
->path
);
4621 oidclr(&one
->oid
, the_repository
->hash_algo
);
4624 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
4626 /* Strip the prefix but do not molest /dev/null and absolute paths */
4627 if (*namep
&& !is_absolute_path(*namep
)) {
4628 *namep
+= prefix_length
;
4632 if (*otherp
&& !is_absolute_path(*otherp
)) {
4633 *otherp
+= prefix_length
;
4634 if (**otherp
== '/')
4639 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
4641 const struct external_diff
*pgm
= external_diff();
4643 struct diff_filespec
*one
= p
->one
;
4644 struct diff_filespec
*two
= p
->two
;
4647 const char *attr_path
;
4650 other
= (strcmp(name
, two
->path
) ? two
->path
: NULL
);
4652 if (o
->prefix_length
)
4653 strip_prefix(o
->prefix_length
, &name
, &other
);
4655 if (!o
->flags
.allow_external
)
4658 if (DIFF_PAIR_UNMERGED(p
)) {
4659 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
4660 NULL
, NULL
, NULL
, o
, p
);
4664 diff_fill_oid_info(one
, o
->repo
->index
);
4665 diff_fill_oid_info(two
, o
->repo
->index
);
4668 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
4669 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
4671 * a filepair that changes between file and symlink
4672 * needs to be split into deletion and creation.
4674 struct diff_filespec
*null
= alloc_filespec(two
->path
);
4675 run_diff_cmd(NULL
, name
, other
, attr_path
,
4679 strbuf_release(&msg
);
4681 null
= alloc_filespec(one
->path
);
4682 run_diff_cmd(NULL
, name
, other
, attr_path
,
4683 null
, two
, &msg
, o
, p
);
4687 run_diff_cmd(pgm
, name
, other
, attr_path
,
4688 one
, two
, &msg
, o
, p
);
4690 strbuf_release(&msg
);
4693 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
4694 struct diffstat_t
*diffstat
)
4699 if (!o
->ignore_driver_algorithm
) {
4700 struct userdiff_driver
*drv
= userdiff_find_by_path(o
->repo
->index
,
4703 if (drv
&& drv
->algorithm
)
4704 set_diff_algorithm(o
, drv
->algorithm
);
4707 if (DIFF_PAIR_UNMERGED(p
)) {
4709 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
,
4714 name
= p
->one
->path
;
4715 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4717 if (o
->prefix_length
)
4718 strip_prefix(o
->prefix_length
, &name
, &other
);
4720 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4721 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4723 builtin_diffstat(name
, other
, p
->one
, p
->two
,
4727 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
4731 const char *attr_path
;
4733 if (DIFF_PAIR_UNMERGED(p
)) {
4738 name
= p
->one
->path
;
4739 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4740 attr_path
= other
? other
: name
;
4742 if (o
->prefix_length
)
4743 strip_prefix(o
->prefix_length
, &name
, &other
);
4745 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4746 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4748 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
4751 void repo_diff_setup(struct repository
*r
, struct diff_options
*options
)
4753 memcpy(options
, &default_diff_options
, sizeof(*options
));
4755 options
->file
= stdout
;
4758 options
->output_indicators
[OUTPUT_INDICATOR_NEW
] = '+';
4759 options
->output_indicators
[OUTPUT_INDICATOR_OLD
] = '-';
4760 options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
] = ' ';
4761 options
->abbrev
= DEFAULT_ABBREV
;
4762 options
->line_termination
= '\n';
4763 options
->break_opt
= -1;
4764 options
->rename_limit
= -1;
4765 options
->dirstat_permille
= diff_dirstat_permille_default
;
4766 options
->context
= diff_context_default
;
4767 options
->interhunkcontext
= diff_interhunk_context_default
;
4768 options
->ws_error_highlight
= ws_error_highlight_default
;
4769 options
->flags
.rename_empty
= 1;
4770 options
->flags
.relative_name
= diff_relative
;
4771 options
->objfind
= NULL
;
4773 /* pathchange left =NULL by default */
4774 options
->change
= diff_change
;
4775 options
->add_remove
= diff_addremove
;
4776 options
->use_color
= diff_use_color_default
;
4777 options
->detect_rename
= diff_detect_rename_default
;
4778 options
->xdl_opts
|= diff_algorithm
;
4779 if (diff_indent_heuristic
)
4780 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4782 options
->orderfile
= xstrdup_or_null(diff_order_file_cfg
);
4784 if (!options
->flags
.ignore_submodule_set
)
4785 options
->flags
.ignore_untracked_in_submodules
= 1;
4787 if (diff_no_prefix
) {
4788 diff_set_noprefix(options
);
4789 } else if (!diff_mnemonic_prefix
) {
4790 diff_set_default_prefix(options
);
4793 options
->color_moved
= diff_color_moved_default
;
4794 options
->color_moved_ws_handling
= diff_color_moved_ws_default
;
4797 static const char diff_status_letters
[] = {
4800 DIFF_STATUS_DELETED
,
4801 DIFF_STATUS_MODIFIED
,
4802 DIFF_STATUS_RENAMED
,
4803 DIFF_STATUS_TYPE_CHANGED
,
4804 DIFF_STATUS_UNKNOWN
,
4805 DIFF_STATUS_UNMERGED
,
4806 DIFF_STATUS_FILTER_AON
,
4807 DIFF_STATUS_FILTER_BROKEN
,
4811 static unsigned int filter_bit
['Z' + 1];
4813 static void prepare_filter_bits(void)
4817 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
4818 for (i
= 0; diff_status_letters
[i
]; i
++)
4819 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
4823 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
4825 return opt
->filter
& filter_bit
[(int) status
];
4828 unsigned diff_filter_bit(char status
)
4830 prepare_filter_bits();
4831 return filter_bit
[(int) status
];
4834 int diff_check_follow_pathspec(struct pathspec
*ps
, int die_on_error
)
4836 unsigned forbidden_magic
;
4840 die(_("--follow requires exactly one pathspec"));
4844 forbidden_magic
= ps
->items
[0].magic
& ~(PATHSPEC_FROMTOP
|
4846 if (forbidden_magic
) {
4848 struct strbuf sb
= STRBUF_INIT
;
4849 pathspec_magic_names(forbidden_magic
, &sb
);
4850 die(_("pathspec magic not supported by --follow: %s"),
4859 void diff_setup_done(struct diff_options
*options
)
4861 unsigned check_mask
= DIFF_FORMAT_NAME
|
4862 DIFF_FORMAT_NAME_STATUS
|
4863 DIFF_FORMAT_CHECKDIFF
|
4864 DIFF_FORMAT_NO_OUTPUT
;
4866 * This must be signed because we're comparing against a potentially
4869 const int hexsz
= the_hash_algo
->hexsz
;
4871 if (options
->set_default
)
4872 options
->set_default(options
);
4874 if (HAS_MULTI_BITS(options
->output_format
& check_mask
))
4875 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
4876 "--name-only", "--name-status", "--check", "-s");
4878 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
4879 die(_("options '%s', '%s', and '%s' cannot be used together"),
4880 "-G", "-S", "--find-object");
4882 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_G_REGEX_MASK
))
4883 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s'"),
4884 "-G", "--pickaxe-regex", "--pickaxe-regex", "-S");
4886 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK
))
4887 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s' and '%s'"),
4888 "--pickaxe-all", "--find-object", "--pickaxe-all", "-G", "-S");
4891 * Most of the time we can say "there are changes"
4892 * only by checking if there are changed paths, but
4893 * --ignore-whitespace* options force us to look
4897 if ((options
->xdl_opts
& XDF_WHITESPACE_FLAGS
) ||
4898 options
->ignore_regex_nr
)
4899 options
->flags
.diff_from_contents
= 1;
4901 options
->flags
.diff_from_contents
= 0;
4903 if (options
->flags
.find_copies_harder
)
4904 options
->detect_rename
= DIFF_DETECT_COPY
;
4906 if (!options
->flags
.relative_name
)
4907 options
->prefix
= NULL
;
4908 if (options
->prefix
)
4909 options
->prefix_length
= strlen(options
->prefix
);
4911 options
->prefix_length
= 0;
4914 * --name-only, --name-status, --checkdiff, and -s
4915 * turn other output format off.
4917 if (options
->output_format
& (DIFF_FORMAT_NAME
|
4918 DIFF_FORMAT_NAME_STATUS
|
4919 DIFF_FORMAT_CHECKDIFF
|
4920 DIFF_FORMAT_NO_OUTPUT
))
4921 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
4922 DIFF_FORMAT_NUMSTAT
|
4923 DIFF_FORMAT_DIFFSTAT
|
4924 DIFF_FORMAT_SHORTSTAT
|
4925 DIFF_FORMAT_DIRSTAT
|
4926 DIFF_FORMAT_SUMMARY
|
4930 * These cases always need recursive; we do not drop caller-supplied
4931 * recursive bits for other formats here.
4933 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
4934 DIFF_FORMAT_NUMSTAT
|
4935 DIFF_FORMAT_DIFFSTAT
|
4936 DIFF_FORMAT_SHORTSTAT
|
4937 DIFF_FORMAT_DIRSTAT
|
4938 DIFF_FORMAT_SUMMARY
|
4939 DIFF_FORMAT_CHECKDIFF
))
4940 options
->flags
.recursive
= 1;
4942 * Also pickaxe would not work very well if you do not say recursive
4944 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
4945 options
->flags
.recursive
= 1;
4947 * When patches are generated, submodules diffed against the work tree
4948 * must be checked for dirtiness too so it can be shown in the output
4950 if (options
->output_format
& DIFF_FORMAT_PATCH
)
4951 options
->flags
.dirty_submodules
= 1;
4953 if (options
->detect_rename
&& options
->rename_limit
< 0)
4954 options
->rename_limit
= diff_rename_limit_default
;
4955 if (hexsz
< options
->abbrev
)
4956 options
->abbrev
= hexsz
; /* full */
4959 * It does not make sense to show the first hit we happened
4960 * to have found. It does not make sense not to return with
4961 * exit code in such a case either.
4963 if (options
->flags
.quick
) {
4964 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
4965 options
->flags
.exit_with_status
= 1;
4969 * External diffs could declare non-identical contents equal
4970 * (think diff --ignore-space-change).
4972 if (options
->flags
.allow_external
&& options
->flags
.exit_with_status
)
4973 options
->flags
.diff_from_contents
= 1;
4975 options
->diff_path_counter
= 0;
4977 if (options
->flags
.follow_renames
)
4978 diff_check_follow_pathspec(&options
->pathspec
, 1);
4980 if (!options
->use_color
||
4981 (options
->flags
.allow_external
&& external_diff()))
4982 options
->color_moved
= 0;
4984 if (options
->filter_not
) {
4985 if (!options
->filter
)
4986 options
->filter
= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
4987 options
->filter
&= ~options
->filter_not
;
4991 int parse_long_opt(const char *opt
, const char **argv
,
4992 const char **optarg
)
4994 const char *arg
= argv
[0];
4995 if (!skip_prefix(arg
, "--", &arg
))
4997 if (!skip_prefix(arg
, opt
, &arg
))
4999 if (*arg
== '=') { /* stuck form: --option=value */
5005 /* separate form: --option value */
5007 die("Option '--%s' requires a value", opt
);
5012 static int diff_opt_stat(const struct option
*opt
, const char *value
, int unset
)
5014 struct diff_options
*options
= opt
->value
;
5015 int width
= options
->stat_width
;
5016 int name_width
= options
->stat_name_width
;
5017 int graph_width
= options
->stat_graph_width
;
5018 int count
= options
->stat_count
;
5021 BUG_ON_OPT_NEG(unset
);
5023 if (!strcmp(opt
->long_name
, "stat")) {
5025 width
= strtoul(value
, &end
, 10);
5027 name_width
= strtoul(end
+1, &end
, 10);
5029 count
= strtoul(end
+1, &end
, 10);
5031 return error(_("invalid --stat value: %s"), value
);
5033 } else if (!strcmp(opt
->long_name
, "stat-width")) {
5034 width
= strtoul(value
, &end
, 10);
5036 return error(_("%s expects a numerical value"),
5038 } else if (!strcmp(opt
->long_name
, "stat-name-width")) {
5039 name_width
= strtoul(value
, &end
, 10);
5041 return error(_("%s expects a numerical value"),
5043 } else if (!strcmp(opt
->long_name
, "stat-graph-width")) {
5044 graph_width
= strtoul(value
, &end
, 10);
5046 return error(_("%s expects a numerical value"),
5048 } else if (!strcmp(opt
->long_name
, "stat-count")) {
5049 count
= strtoul(value
, &end
, 10);
5051 return error(_("%s expects a numerical value"),
5054 BUG("%s should not get here", opt
->long_name
);
5056 options
->output_format
&= ~DIFF_FORMAT_NO_OUTPUT
;
5057 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
5058 options
->stat_name_width
= name_width
;
5059 options
->stat_graph_width
= graph_width
;
5060 options
->stat_width
= width
;
5061 options
->stat_count
= count
;
5065 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
5067 struct strbuf errmsg
= STRBUF_INIT
;
5068 if (parse_dirstat_params(options
, params
, &errmsg
))
5069 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
5071 strbuf_release(&errmsg
);
5073 * The caller knows a dirstat-related option is given from the command
5074 * line; allow it to say "return this_function();"
5076 options
->output_format
&= ~DIFF_FORMAT_NO_OUTPUT
;
5077 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
5081 static int diff_opt_diff_filter(const struct option
*option
,
5082 const char *optarg
, int unset
)
5084 struct diff_options
*opt
= option
->value
;
5087 BUG_ON_OPT_NEG(unset
);
5088 prepare_filter_bits();
5090 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
5094 if ('a' <= optch
&& optch
<= 'z') {
5096 optch
= toupper(optch
);
5101 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
5103 return error(_("unknown change class '%c' in --diff-filter=%s"),
5106 opt
->filter_not
|= bit
;
5113 static void enable_patch_output(int *fmt
)
5115 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
5116 *fmt
|= DIFF_FORMAT_PATCH
;
5119 static int diff_opt_ws_error_highlight(const struct option
*option
,
5120 const char *arg
, int unset
)
5122 struct diff_options
*opt
= option
->value
;
5123 int val
= parse_ws_error_highlight(arg
);
5125 BUG_ON_OPT_NEG(unset
);
5127 return error(_("unknown value after ws-error-highlight=%.*s"),
5129 opt
->ws_error_highlight
= val
;
5133 static int diff_opt_find_object(const struct option
*option
,
5134 const char *arg
, int unset
)
5136 struct diff_options
*opt
= option
->value
;
5137 struct object_id oid
;
5139 BUG_ON_OPT_NEG(unset
);
5140 if (repo_get_oid(the_repository
, arg
, &oid
))
5141 return error(_("unable to resolve '%s'"), arg
);
5144 CALLOC_ARRAY(opt
->objfind
, 1);
5146 opt
->pickaxe_opts
|= DIFF_PICKAXE_KIND_OBJFIND
;
5147 opt
->flags
.recursive
= 1;
5148 opt
->flags
.tree_in_recursive
= 1;
5149 oidset_insert(opt
->objfind
, &oid
);
5153 static int diff_opt_anchored(const struct option
*opt
,
5154 const char *arg
, int unset
)
5156 struct diff_options
*options
= opt
->value
;
5158 BUG_ON_OPT_NEG(unset
);
5159 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
5160 ALLOC_GROW(options
->anchors
, options
->anchors_nr
+ 1,
5161 options
->anchors_alloc
);
5162 options
->anchors
[options
->anchors_nr
++] = xstrdup(arg
);
5166 static int diff_opt_binary(const struct option
*opt
,
5167 const char *arg
, int unset
)
5169 struct diff_options
*options
= opt
->value
;
5171 BUG_ON_OPT_NEG(unset
);
5172 BUG_ON_OPT_ARG(arg
);
5173 enable_patch_output(&options
->output_format
);
5174 options
->flags
.binary
= 1;
5178 static int diff_opt_break_rewrites(const struct option
*opt
,
5179 const char *arg
, int unset
)
5181 int *break_opt
= opt
->value
;
5184 BUG_ON_OPT_NEG(unset
);
5187 opt1
= parse_rename_score(&arg
);
5190 else if (*arg
!= '/')
5191 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
5194 opt2
= parse_rename_score(&arg
);
5197 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
5198 *break_opt
= opt1
| (opt2
<< 16);
5202 static int diff_opt_char(const struct option
*opt
,
5203 const char *arg
, int unset
)
5205 char *value
= opt
->value
;
5207 BUG_ON_OPT_NEG(unset
);
5209 return error(_("%s expects a character, got '%s'"),
5210 opt
->long_name
, arg
);
5215 static int diff_opt_color_moved(const struct option
*opt
,
5216 const char *arg
, int unset
)
5218 struct diff_options
*options
= opt
->value
;
5221 options
->color_moved
= COLOR_MOVED_NO
;
5223 if (diff_color_moved_default
)
5224 options
->color_moved
= diff_color_moved_default
;
5225 if (options
->color_moved
== COLOR_MOVED_NO
)
5226 options
->color_moved
= COLOR_MOVED_DEFAULT
;
5228 int cm
= parse_color_moved(arg
);
5230 return error(_("bad --color-moved argument: %s"), arg
);
5231 options
->color_moved
= cm
;
5236 static int diff_opt_color_moved_ws(const struct option
*opt
,
5237 const char *arg
, int unset
)
5239 struct diff_options
*options
= opt
->value
;
5243 options
->color_moved_ws_handling
= 0;
5247 cm
= parse_color_moved_ws(arg
);
5248 if (cm
& COLOR_MOVED_WS_ERROR
)
5249 return error(_("invalid mode '%s' in --color-moved-ws"), arg
);
5250 options
->color_moved_ws_handling
= cm
;
5254 static int diff_opt_color_words(const struct option
*opt
,
5255 const char *arg
, int unset
)
5257 struct diff_options
*options
= opt
->value
;
5259 BUG_ON_OPT_NEG(unset
);
5260 options
->use_color
= 1;
5261 options
->word_diff
= DIFF_WORDS_COLOR
;
5262 options
->word_regex
= arg
;
5266 static int diff_opt_compact_summary(const struct option
*opt
,
5267 const char *arg
, int unset
)
5269 struct diff_options
*options
= opt
->value
;
5271 BUG_ON_OPT_ARG(arg
);
5273 options
->flags
.stat_with_summary
= 0;
5275 options
->flags
.stat_with_summary
= 1;
5276 options
->output_format
&= ~DIFF_FORMAT_NO_OUTPUT
;
5277 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
5282 static int diff_opt_diff_algorithm(const struct option
*opt
,
5283 const char *arg
, int unset
)
5285 struct diff_options
*options
= opt
->value
;
5287 BUG_ON_OPT_NEG(unset
);
5289 if (set_diff_algorithm(options
, arg
))
5290 return error(_("option diff-algorithm accepts \"myers\", "
5291 "\"minimal\", \"patience\" and \"histogram\""));
5293 options
->ignore_driver_algorithm
= 1;
5298 static int diff_opt_diff_algorithm_no_arg(const struct option
*opt
,
5299 const char *arg
, int unset
)
5301 struct diff_options
*options
= opt
->value
;
5303 BUG_ON_OPT_NEG(unset
);
5304 BUG_ON_OPT_ARG(arg
);
5306 if (set_diff_algorithm(options
, opt
->long_name
))
5307 BUG("available diff algorithms include \"myers\", "
5308 "\"minimal\", \"patience\" and \"histogram\"");
5310 options
->ignore_driver_algorithm
= 1;
5315 static int diff_opt_dirstat(const struct option
*opt
,
5316 const char *arg
, int unset
)
5318 struct diff_options
*options
= opt
->value
;
5320 BUG_ON_OPT_NEG(unset
);
5321 if (!strcmp(opt
->long_name
, "cumulative")) {
5323 BUG("how come --cumulative take a value?");
5325 } else if (!strcmp(opt
->long_name
, "dirstat-by-file"))
5326 parse_dirstat_opt(options
, "files");
5327 parse_dirstat_opt(options
, arg
? arg
: "");
5331 static int diff_opt_find_copies(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
->rename_score
= parse_rename_score(&arg
);
5341 return error(_("invalid argument to %s"), opt
->long_name
);
5343 if (options
->detect_rename
== DIFF_DETECT_COPY
)
5344 options
->flags
.find_copies_harder
= 1;
5346 options
->detect_rename
= DIFF_DETECT_COPY
;
5351 static int diff_opt_find_renames(const struct option
*opt
,
5352 const char *arg
, int unset
)
5354 struct diff_options
*options
= opt
->value
;
5356 BUG_ON_OPT_NEG(unset
);
5359 options
->rename_score
= parse_rename_score(&arg
);
5361 return error(_("invalid argument to %s"), opt
->long_name
);
5363 options
->detect_rename
= DIFF_DETECT_RENAME
;
5367 static int diff_opt_follow(const struct option
*opt
,
5368 const char *arg
, int unset
)
5370 struct diff_options
*options
= opt
->value
;
5372 BUG_ON_OPT_ARG(arg
);
5374 options
->flags
.follow_renames
= 0;
5375 options
->flags
.default_follow_renames
= 0;
5377 options
->flags
.follow_renames
= 1;
5382 static int diff_opt_ignore_submodules(const struct option
*opt
,
5383 const char *arg
, int unset
)
5385 struct diff_options
*options
= opt
->value
;
5387 BUG_ON_OPT_NEG(unset
);
5390 options
->flags
.override_submodule_config
= 1;
5391 handle_ignore_submodules_arg(options
, arg
);
5395 static int diff_opt_line_prefix(const struct option
*opt
,
5396 const char *optarg
, int unset
)
5398 struct diff_options
*options
= opt
->value
;
5400 BUG_ON_OPT_NEG(unset
);
5401 options
->line_prefix
= optarg
;
5402 graph_setup_line_prefix(options
);
5406 static int diff_opt_no_prefix(const struct option
*opt
,
5407 const char *optarg
, int unset
)
5409 struct diff_options
*options
= opt
->value
;
5411 BUG_ON_OPT_NEG(unset
);
5412 BUG_ON_OPT_ARG(optarg
);
5413 diff_set_noprefix(options
);
5417 static int diff_opt_default_prefix(const struct option
*opt
,
5418 const char *optarg
, int unset
)
5420 struct diff_options
*options
= opt
->value
;
5422 BUG_ON_OPT_NEG(unset
);
5423 BUG_ON_OPT_ARG(optarg
);
5424 FREE_AND_NULL(diff_src_prefix
);
5425 FREE_AND_NULL(diff_dst_prefix
);
5426 diff_set_default_prefix(options
);
5430 static enum parse_opt_result
diff_opt_output(struct parse_opt_ctx_t
*ctx
,
5431 const struct option
*opt
,
5432 const char *arg
, int unset
)
5434 struct diff_options
*options
= opt
->value
;
5437 BUG_ON_OPT_NEG(unset
);
5438 path
= prefix_filename(ctx
->prefix
, arg
);
5439 options
->file
= xfopen(path
, "w");
5440 options
->close_file
= 1;
5441 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
5442 options
->use_color
= GIT_COLOR_NEVER
;
5447 static int diff_opt_patience(const struct option
*opt
,
5448 const char *arg
, int unset
)
5450 struct diff_options
*options
= opt
->value
;
5453 BUG_ON_OPT_NEG(unset
);
5454 BUG_ON_OPT_ARG(arg
);
5456 * Both --patience and --anchored use PATIENCE_DIFF
5457 * internally, so remove any anchors previously
5460 for (i
= 0; i
< options
->anchors_nr
; i
++)
5461 free(options
->anchors
[i
]);
5462 options
->anchors_nr
= 0;
5463 options
->ignore_driver_algorithm
= 1;
5465 return set_diff_algorithm(options
, "patience");
5468 static int diff_opt_ignore_regex(const struct option
*opt
,
5469 const char *arg
, int unset
)
5471 struct diff_options
*options
= opt
->value
;
5474 BUG_ON_OPT_NEG(unset
);
5476 regex
= xmalloc(sizeof(*regex
));
5477 if (regcomp(regex
, arg
, REG_EXTENDED
| REG_NEWLINE
)) {
5479 return error(_("invalid regex given to -I: '%s'"), arg
);
5482 ALLOC_GROW(options
->ignore_regex
, options
->ignore_regex_nr
+ 1,
5483 options
->ignore_regex_alloc
);
5484 options
->ignore_regex
[options
->ignore_regex_nr
++] = regex
;
5488 static int diff_opt_pickaxe_regex(const struct option
*opt
,
5489 const char *arg
, int unset
)
5491 struct diff_options
*options
= opt
->value
;
5493 BUG_ON_OPT_NEG(unset
);
5494 options
->pickaxe
= arg
;
5495 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
5499 static int diff_opt_pickaxe_string(const struct option
*opt
,
5500 const char *arg
, int unset
)
5502 struct diff_options
*options
= opt
->value
;
5504 BUG_ON_OPT_NEG(unset
);
5505 options
->pickaxe
= arg
;
5506 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
5510 static int diff_opt_relative(const struct option
*opt
,
5511 const char *arg
, int unset
)
5513 struct diff_options
*options
= opt
->value
;
5515 options
->flags
.relative_name
= !unset
;
5517 options
->prefix
= arg
;
5521 static int diff_opt_submodule(const struct option
*opt
,
5522 const char *arg
, int unset
)
5524 struct diff_options
*options
= opt
->value
;
5526 BUG_ON_OPT_NEG(unset
);
5529 if (parse_submodule_params(options
, arg
))
5530 return error(_("failed to parse --submodule option parameter: '%s'"),
5535 static int diff_opt_textconv(const struct option
*opt
,
5536 const char *arg
, int unset
)
5538 struct diff_options
*options
= opt
->value
;
5540 BUG_ON_OPT_ARG(arg
);
5542 options
->flags
.allow_textconv
= 0;
5544 options
->flags
.allow_textconv
= 1;
5545 options
->flags
.textconv_set_via_cmdline
= 1;
5550 static int diff_opt_unified(const struct option
*opt
,
5551 const char *arg
, int unset
)
5553 struct diff_options
*options
= opt
->value
;
5556 BUG_ON_OPT_NEG(unset
);
5559 options
->context
= strtol(arg
, &s
, 10);
5561 return error(_("%s expects a numerical value"), "--unified");
5563 enable_patch_output(&options
->output_format
);
5568 static int diff_opt_word_diff(const struct option
*opt
,
5569 const char *arg
, int unset
)
5571 struct diff_options
*options
= opt
->value
;
5573 BUG_ON_OPT_NEG(unset
);
5575 if (!strcmp(arg
, "plain"))
5576 options
->word_diff
= DIFF_WORDS_PLAIN
;
5577 else if (!strcmp(arg
, "color")) {
5578 options
->use_color
= 1;
5579 options
->word_diff
= DIFF_WORDS_COLOR
;
5581 else if (!strcmp(arg
, "porcelain"))
5582 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
5583 else if (!strcmp(arg
, "none"))
5584 options
->word_diff
= DIFF_WORDS_NONE
;
5586 return error(_("bad --word-diff argument: %s"), arg
);
5588 if (options
->word_diff
== DIFF_WORDS_NONE
)
5589 options
->word_diff
= DIFF_WORDS_PLAIN
;
5594 static int diff_opt_word_diff_regex(const struct option
*opt
,
5595 const char *arg
, int unset
)
5597 struct diff_options
*options
= opt
->value
;
5599 BUG_ON_OPT_NEG(unset
);
5600 if (options
->word_diff
== DIFF_WORDS_NONE
)
5601 options
->word_diff
= DIFF_WORDS_PLAIN
;
5602 options
->word_regex
= arg
;
5606 static int diff_opt_rotate_to(const struct option
*opt
, const char *arg
, int unset
)
5608 struct diff_options
*options
= opt
->value
;
5610 BUG_ON_OPT_NEG(unset
);
5611 if (!strcmp(opt
->long_name
, "skip-to"))
5612 options
->skip_instead_of_rotate
= 1;
5614 options
->skip_instead_of_rotate
= 0;
5615 options
->rotate_to
= arg
;
5620 * Consider adding new flags to __git_diff_common_options
5621 * in contrib/completion/git-completion.bash
5623 struct option
*add_diff_options(const struct option
*opts
,
5624 struct diff_options
*options
)
5626 struct option parseopts
[] = {
5627 OPT_GROUP(N_("Diff output format options")),
5628 OPT_BITOP('p', "patch", &options
->output_format
,
5629 N_("generate patch"),
5630 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5631 OPT_SET_INT('s', "no-patch", &options
->output_format
,
5632 N_("suppress diff output"), DIFF_FORMAT_NO_OUTPUT
),
5633 OPT_BITOP('u', NULL
, &options
->output_format
,
5634 N_("generate patch"),
5635 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5636 OPT_CALLBACK_F('U', "unified", options
, N_("<n>"),
5637 N_("generate diffs with <n> lines context"),
5638 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_unified
),
5639 OPT_BOOL('W', "function-context", &options
->flags
.funccontext
,
5640 N_("generate diffs with <n> lines context")),
5641 OPT_BITOP(0, "raw", &options
->output_format
,
5642 N_("generate the diff in raw format"),
5643 DIFF_FORMAT_RAW
, DIFF_FORMAT_NO_OUTPUT
),
5644 OPT_BITOP(0, "patch-with-raw", &options
->output_format
,
5645 N_("synonym for '-p --raw'"),
5646 DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
,
5647 DIFF_FORMAT_NO_OUTPUT
),
5648 OPT_BITOP(0, "patch-with-stat", &options
->output_format
,
5649 N_("synonym for '-p --stat'"),
5650 DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
,
5651 DIFF_FORMAT_NO_OUTPUT
),
5652 OPT_BITOP(0, "numstat", &options
->output_format
,
5653 N_("machine friendly --stat"),
5654 DIFF_FORMAT_NUMSTAT
, DIFF_FORMAT_NO_OUTPUT
),
5655 OPT_BITOP(0, "shortstat", &options
->output_format
,
5656 N_("output only the last line of --stat"),
5657 DIFF_FORMAT_SHORTSTAT
, DIFF_FORMAT_NO_OUTPUT
),
5658 OPT_CALLBACK_F('X', "dirstat", options
, N_("<param1>,<param2>..."),
5659 N_("output the distribution of relative amount of changes for each sub-directory"),
5660 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5662 OPT_CALLBACK_F(0, "cumulative", options
, NULL
,
5663 N_("synonym for --dirstat=cumulative"),
5664 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5666 OPT_CALLBACK_F(0, "dirstat-by-file", options
, N_("<param1>,<param2>..."),
5667 N_("synonym for --dirstat=files,<param1>,<param2>..."),
5668 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5670 OPT_BIT_F(0, "check", &options
->output_format
,
5671 N_("warn if changes introduce conflict markers or whitespace errors"),
5672 DIFF_FORMAT_CHECKDIFF
, PARSE_OPT_NONEG
),
5673 OPT_BITOP(0, "summary", &options
->output_format
,
5674 N_("condensed summary such as creations, renames and mode changes"),
5675 DIFF_FORMAT_SUMMARY
, DIFF_FORMAT_NO_OUTPUT
),
5676 OPT_BIT_F(0, "name-only", &options
->output_format
,
5677 N_("show only names of changed files"),
5678 DIFF_FORMAT_NAME
, PARSE_OPT_NONEG
),
5679 OPT_BIT_F(0, "name-status", &options
->output_format
,
5680 N_("show only names and status of changed files"),
5681 DIFF_FORMAT_NAME_STATUS
, PARSE_OPT_NONEG
),
5682 OPT_CALLBACK_F(0, "stat", options
, N_("<width>[,<name-width>[,<count>]]"),
5683 N_("generate diffstat"),
5684 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_stat
),
5685 OPT_CALLBACK_F(0, "stat-width", options
, N_("<width>"),
5686 N_("generate diffstat with a given width"),
5687 PARSE_OPT_NONEG
, diff_opt_stat
),
5688 OPT_CALLBACK_F(0, "stat-name-width", options
, N_("<width>"),
5689 N_("generate diffstat with a given name width"),
5690 PARSE_OPT_NONEG
, diff_opt_stat
),
5691 OPT_CALLBACK_F(0, "stat-graph-width", options
, N_("<width>"),
5692 N_("generate diffstat with a given graph width"),
5693 PARSE_OPT_NONEG
, diff_opt_stat
),
5694 OPT_CALLBACK_F(0, "stat-count", options
, N_("<count>"),
5695 N_("generate diffstat with limited lines"),
5696 PARSE_OPT_NONEG
, diff_opt_stat
),
5697 OPT_CALLBACK_F(0, "compact-summary", options
, NULL
,
5698 N_("generate compact summary in diffstat"),
5699 PARSE_OPT_NOARG
, diff_opt_compact_summary
),
5700 OPT_CALLBACK_F(0, "binary", options
, NULL
,
5701 N_("output a binary diff that can be applied"),
5702 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_binary
),
5703 OPT_BOOL(0, "full-index", &options
->flags
.full_index
,
5704 N_("show full pre- and post-image object names on the \"index\" lines")),
5705 OPT_COLOR_FLAG(0, "color", &options
->use_color
,
5706 N_("show colored diff")),
5707 OPT_CALLBACK_F(0, "ws-error-highlight", options
, N_("<kind>"),
5708 N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
5709 PARSE_OPT_NONEG
, diff_opt_ws_error_highlight
),
5710 OPT_SET_INT('z', NULL
, &options
->line_termination
,
5711 N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
5713 OPT__ABBREV(&options
->abbrev
),
5714 OPT_STRING_F(0, "src-prefix", &options
->a_prefix
, N_("<prefix>"),
5715 N_("show the given source prefix instead of \"a/\""),
5717 OPT_STRING_F(0, "dst-prefix", &options
->b_prefix
, N_("<prefix>"),
5718 N_("show the given destination prefix instead of \"b/\""),
5720 OPT_CALLBACK_F(0, "line-prefix", options
, N_("<prefix>"),
5721 N_("prepend an additional prefix to every line of output"),
5722 PARSE_OPT_NONEG
, diff_opt_line_prefix
),
5723 OPT_CALLBACK_F(0, "no-prefix", options
, NULL
,
5724 N_("do not show any source or destination prefix"),
5725 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_no_prefix
),
5726 OPT_CALLBACK_F(0, "default-prefix", options
, NULL
,
5727 N_("use default prefixes a/ and b/"),
5728 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_default_prefix
),
5729 OPT_INTEGER_F(0, "inter-hunk-context", &options
->interhunkcontext
,
5730 N_("show context between diff hunks up to the specified number of lines"),
5732 OPT_CALLBACK_F(0, "output-indicator-new",
5733 &options
->output_indicators
[OUTPUT_INDICATOR_NEW
],
5735 N_("specify the character to indicate a new line instead of '+'"),
5736 PARSE_OPT_NONEG
, diff_opt_char
),
5737 OPT_CALLBACK_F(0, "output-indicator-old",
5738 &options
->output_indicators
[OUTPUT_INDICATOR_OLD
],
5740 N_("specify the character to indicate an old line instead of '-'"),
5741 PARSE_OPT_NONEG
, diff_opt_char
),
5742 OPT_CALLBACK_F(0, "output-indicator-context",
5743 &options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
],
5745 N_("specify the character to indicate a context instead of ' '"),
5746 PARSE_OPT_NONEG
, diff_opt_char
),
5748 OPT_GROUP(N_("Diff rename options")),
5749 OPT_CALLBACK_F('B', "break-rewrites", &options
->break_opt
, N_("<n>[/<m>]"),
5750 N_("break complete rewrite changes into pairs of delete and create"),
5751 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5752 diff_opt_break_rewrites
),
5753 OPT_CALLBACK_F('M', "find-renames", options
, N_("<n>"),
5754 N_("detect renames"),
5755 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5756 diff_opt_find_renames
),
5757 OPT_SET_INT_F('D', "irreversible-delete", &options
->irreversible_delete
,
5758 N_("omit the preimage for deletes"),
5759 1, PARSE_OPT_NONEG
),
5760 OPT_CALLBACK_F('C', "find-copies", options
, N_("<n>"),
5761 N_("detect copies"),
5762 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5763 diff_opt_find_copies
),
5764 OPT_BOOL(0, "find-copies-harder", &options
->flags
.find_copies_harder
,
5765 N_("use unmodified files as source to find copies")),
5766 OPT_SET_INT_F(0, "no-renames", &options
->detect_rename
,
5767 N_("disable rename detection"),
5768 0, PARSE_OPT_NONEG
),
5769 OPT_BOOL(0, "rename-empty", &options
->flags
.rename_empty
,
5770 N_("use empty blobs as rename source")),
5771 OPT_CALLBACK_F(0, "follow", options
, NULL
,
5772 N_("continue listing the history of a file beyond renames"),
5773 PARSE_OPT_NOARG
, diff_opt_follow
),
5774 OPT_INTEGER('l', NULL
, &options
->rename_limit
,
5775 N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")),
5777 OPT_GROUP(N_("Diff algorithm options")),
5778 OPT_CALLBACK_F(0, "minimal", options
, NULL
,
5779 N_("produce the smallest possible diff"),
5780 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5781 diff_opt_diff_algorithm_no_arg
),
5782 OPT_BIT_F('w', "ignore-all-space", &options
->xdl_opts
,
5783 N_("ignore whitespace when comparing lines"),
5784 XDF_IGNORE_WHITESPACE
, PARSE_OPT_NONEG
),
5785 OPT_BIT_F('b', "ignore-space-change", &options
->xdl_opts
,
5786 N_("ignore changes in amount of whitespace"),
5787 XDF_IGNORE_WHITESPACE_CHANGE
, PARSE_OPT_NONEG
),
5788 OPT_BIT_F(0, "ignore-space-at-eol", &options
->xdl_opts
,
5789 N_("ignore changes in whitespace at EOL"),
5790 XDF_IGNORE_WHITESPACE_AT_EOL
, PARSE_OPT_NONEG
),
5791 OPT_BIT_F(0, "ignore-cr-at-eol", &options
->xdl_opts
,
5792 N_("ignore carrier-return at the end of line"),
5793 XDF_IGNORE_CR_AT_EOL
, PARSE_OPT_NONEG
),
5794 OPT_BIT_F(0, "ignore-blank-lines", &options
->xdl_opts
,
5795 N_("ignore changes whose lines are all blank"),
5796 XDF_IGNORE_BLANK_LINES
, PARSE_OPT_NONEG
),
5797 OPT_CALLBACK_F('I', "ignore-matching-lines", options
, N_("<regex>"),
5798 N_("ignore changes whose all lines match <regex>"),
5799 0, diff_opt_ignore_regex
),
5800 OPT_BIT(0, "indent-heuristic", &options
->xdl_opts
,
5801 N_("heuristic to shift diff hunk boundaries for easy reading"),
5802 XDF_INDENT_HEURISTIC
),
5803 OPT_CALLBACK_F(0, "patience", options
, NULL
,
5804 N_("generate diff using the \"patience diff\" algorithm"),
5805 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5807 OPT_CALLBACK_F(0, "histogram", options
, NULL
,
5808 N_("generate diff using the \"histogram diff\" algorithm"),
5809 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5810 diff_opt_diff_algorithm_no_arg
),
5811 OPT_CALLBACK_F(0, "diff-algorithm", options
, N_("<algorithm>"),
5812 N_("choose a diff algorithm"),
5813 PARSE_OPT_NONEG
, diff_opt_diff_algorithm
),
5814 OPT_CALLBACK_F(0, "anchored", options
, N_("<text>"),
5815 N_("generate diff using the \"anchored diff\" algorithm"),
5816 PARSE_OPT_NONEG
, diff_opt_anchored
),
5817 OPT_CALLBACK_F(0, "word-diff", options
, N_("<mode>"),
5818 N_("show word diff, using <mode> to delimit changed words"),
5819 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_word_diff
),
5820 OPT_CALLBACK_F(0, "word-diff-regex", options
, N_("<regex>"),
5821 N_("use <regex> to decide what a word is"),
5822 PARSE_OPT_NONEG
, diff_opt_word_diff_regex
),
5823 OPT_CALLBACK_F(0, "color-words", options
, N_("<regex>"),
5824 N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
5825 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_color_words
),
5826 OPT_CALLBACK_F(0, "color-moved", options
, N_("<mode>"),
5827 N_("moved lines of code are colored differently"),
5828 PARSE_OPT_OPTARG
, diff_opt_color_moved
),
5829 OPT_CALLBACK_F(0, "color-moved-ws", options
, N_("<mode>"),
5830 N_("how white spaces are ignored in --color-moved"),
5831 0, diff_opt_color_moved_ws
),
5833 OPT_GROUP(N_("Other diff options")),
5834 OPT_CALLBACK_F(0, "relative", options
, N_("<prefix>"),
5835 N_("when run from subdir, exclude changes outside and show relative paths"),
5838 OPT_BOOL('a', "text", &options
->flags
.text
,
5839 N_("treat all files as text")),
5840 OPT_BOOL('R', NULL
, &options
->flags
.reverse_diff
,
5841 N_("swap two inputs, reverse the diff")),
5842 OPT_BOOL(0, "exit-code", &options
->flags
.exit_with_status
,
5843 N_("exit with 1 if there were differences, 0 otherwise")),
5844 OPT_BOOL(0, "quiet", &options
->flags
.quick
,
5845 N_("disable all output of the program")),
5846 OPT_BOOL(0, "ext-diff", &options
->flags
.allow_external
,
5847 N_("allow an external diff helper to be executed")),
5848 OPT_CALLBACK_F(0, "textconv", options
, NULL
,
5849 N_("run external text conversion filters when comparing binary files"),
5850 PARSE_OPT_NOARG
, diff_opt_textconv
),
5851 OPT_CALLBACK_F(0, "ignore-submodules", options
, N_("<when>"),
5852 N_("ignore changes to submodules in the diff generation"),
5853 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5854 diff_opt_ignore_submodules
),
5855 OPT_CALLBACK_F(0, "submodule", options
, N_("<format>"),
5856 N_("specify how differences in submodules are shown"),
5857 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5858 diff_opt_submodule
),
5859 OPT_SET_INT_F(0, "ita-invisible-in-index", &options
->ita_invisible_in_index
,
5860 N_("hide 'git add -N' entries from the index"),
5861 1, PARSE_OPT_NONEG
),
5862 OPT_SET_INT_F(0, "ita-visible-in-index", &options
->ita_invisible_in_index
,
5863 N_("treat 'git add -N' entries as real in the index"),
5864 0, PARSE_OPT_NONEG
),
5865 OPT_CALLBACK_F('S', NULL
, options
, N_("<string>"),
5866 N_("look for differences that change the number of occurrences of the specified string"),
5867 0, diff_opt_pickaxe_string
),
5868 OPT_CALLBACK_F('G', NULL
, options
, N_("<regex>"),
5869 N_("look for differences that change the number of occurrences of the specified regex"),
5870 0, diff_opt_pickaxe_regex
),
5871 OPT_BIT_F(0, "pickaxe-all", &options
->pickaxe_opts
,
5872 N_("show all changes in the changeset with -S or -G"),
5873 DIFF_PICKAXE_ALL
, PARSE_OPT_NONEG
),
5874 OPT_BIT_F(0, "pickaxe-regex", &options
->pickaxe_opts
,
5875 N_("treat <string> in -S as extended POSIX regular expression"),
5876 DIFF_PICKAXE_REGEX
, PARSE_OPT_NONEG
),
5877 OPT_FILENAME('O', NULL
, &options
->orderfile
,
5878 N_("control the order in which files appear in the output")),
5879 OPT_CALLBACK_F(0, "rotate-to", options
, N_("<path>"),
5880 N_("show the change in the specified path first"),
5881 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5882 OPT_CALLBACK_F(0, "skip-to", options
, N_("<path>"),
5883 N_("skip the output to the specified path"),
5884 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5885 OPT_CALLBACK_F(0, "find-object", options
, N_("<object-id>"),
5886 N_("look for differences that change the number of occurrences of the specified object"),
5887 PARSE_OPT_NONEG
, diff_opt_find_object
),
5888 OPT_CALLBACK_F(0, "diff-filter", options
, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
5889 N_("select files by diff type"),
5890 PARSE_OPT_NONEG
, diff_opt_diff_filter
),
5891 { OPTION_CALLBACK
, 0, "output", options
, N_("<file>"),
5892 N_("output to a specific file"),
5893 PARSE_OPT_NONEG
, NULL
, 0, diff_opt_output
},
5898 return parse_options_concat(opts
, parseopts
);
5901 int diff_opt_parse(struct diff_options
*options
,
5902 const char **av
, int ac
, const char *prefix
)
5904 struct option no_options
[] = { OPT_END() };
5905 struct option
*parseopts
= add_diff_options(no_options
, options
);
5910 ac
= parse_options(ac
, av
, prefix
, parseopts
, NULL
,
5911 PARSE_OPT_KEEP_DASHDASH
|
5912 PARSE_OPT_KEEP_UNKNOWN_OPT
|
5913 PARSE_OPT_NO_INTERNAL_HELP
|
5914 PARSE_OPT_ONE_SHOT
|
5915 PARSE_OPT_STOP_AT_NON_OPTION
);
5921 int parse_rename_score(const char **cp_p
)
5923 unsigned long num
, scale
;
5925 const char *cp
= *cp_p
;
5932 if ( !dot
&& ch
== '.' ) {
5935 } else if ( ch
== '%' ) {
5936 scale
= dot
? scale
*100 : 100;
5937 cp
++; /* % is always at the end */
5939 } else if ( ch
>= '0' && ch
<= '9' ) {
5940 if ( scale
< 100000 ) {
5942 num
= (num
*10) + (ch
-'0');
5951 /* user says num divided by scale and we say internally that
5952 * is MAX_SCORE * num / scale.
5954 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
5957 struct diff_queue_struct diff_queued_diff
;
5959 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
5961 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
5962 queue
->queue
[queue
->nr
++] = dp
;
5965 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
5966 struct diff_filespec
*one
,
5967 struct diff_filespec
*two
)
5969 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
5977 void diff_free_filepair(struct diff_filepair
*p
)
5979 free_filespec(p
->one
);
5980 free_filespec(p
->two
);
5984 void diff_queue_init(struct diff_queue_struct
*q
)
5986 struct diff_queue_struct blank
= DIFF_QUEUE_INIT
;
5987 memcpy(q
, &blank
, sizeof(*q
));
5990 void diff_queue_clear(struct diff_queue_struct
*q
)
5992 for (int i
= 0; i
< q
->nr
; i
++)
5993 diff_free_filepair(q
->queue
[i
]);
5998 const char *diff_aligned_abbrev(const struct object_id
*oid
, int len
)
6003 /* Do we want all 40 hex characters? */
6004 if (len
== the_hash_algo
->hexsz
)
6005 return oid_to_hex(oid
);
6007 /* An abbreviated value is fine, possibly followed by an ellipsis. */
6008 abbrev
= diff_abbrev_oid(oid
, len
);
6010 if (!print_sha1_ellipsis())
6013 abblen
= strlen(abbrev
);
6016 * In well-behaved cases, where the abbreviated result is the
6017 * same as the requested length, append three dots after the
6018 * abbreviation (hence the whole logic is limited to the case
6019 * where abblen < 37); when the actual abbreviated result is a
6020 * bit longer than the requested length, we reduce the number
6021 * of dots so that they match the well-behaved ones. However,
6022 * if the actual abbreviation is longer than the requested
6023 * length by more than three, we give up on aligning, and add
6024 * three dots anyway, to indicate that the output is not the
6025 * full object name. Yes, this may be suboptimal, but this
6026 * appears only in "diff --raw --abbrev" output and it is not
6027 * worth the effort to change it now. Note that this would
6028 * likely to work fine when the automatic sizing of default
6029 * abbreviation length is used--we would be fed -1 in "len" in
6030 * that case, and will end up always appending three-dots, but
6031 * the automatic sizing is supposed to give abblen that ensures
6032 * uniqueness across all objects (statistically speaking).
6034 if (abblen
< the_hash_algo
->hexsz
- 3) {
6035 static char hex
[GIT_MAX_HEXSZ
+ 1];
6036 if (len
< abblen
&& abblen
<= len
+ 2)
6037 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
6039 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
6043 return oid_to_hex(oid
);
6046 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
6048 int line_termination
= opt
->line_termination
;
6049 int inter_name_termination
= line_termination
? '\t' : '\0';
6051 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
6052 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
6053 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
6054 diff_aligned_abbrev(&p
->one
->oid
, opt
->abbrev
));
6055 fprintf(opt
->file
, "%s ",
6056 diff_aligned_abbrev(&p
->two
->oid
, opt
->abbrev
));
6059 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
6060 inter_name_termination
);
6062 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
6065 if (p
->status
== DIFF_STATUS_COPIED
||
6066 p
->status
== DIFF_STATUS_RENAMED
) {
6067 const char *name_a
, *name_b
;
6068 name_a
= p
->one
->path
;
6069 name_b
= p
->two
->path
;
6070 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
6071 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
6072 write_name_quoted(name_b
, opt
->file
, line_termination
);
6074 const char *name_a
, *name_b
;
6075 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
6077 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
6078 write_name_quoted(name_a
, opt
->file
, line_termination
);
6082 int diff_unmodified_pair(struct diff_filepair
*p
)
6084 /* This function is written stricter than necessary to support
6085 * the currently implemented transformers, but the idea is to
6086 * let transformers to produce diff_filepairs any way they want,
6087 * and filter and clean them up here before producing the output.
6089 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
6091 if (DIFF_PAIR_UNMERGED(p
))
6092 return 0; /* unmerged is interesting */
6094 /* deletion, addition, mode or type change
6095 * and rename are all interesting.
6097 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
6098 DIFF_PAIR_MODE_CHANGED(p
) ||
6099 strcmp(one
->path
, two
->path
))
6102 /* both are valid and point at the same path. that is, we are
6103 * dealing with a change.
6105 if (one
->oid_valid
&& two
->oid_valid
&&
6106 oideq(&one
->oid
, &two
->oid
) &&
6107 !one
->dirty_submodule
&& !two
->dirty_submodule
)
6108 return 1; /* no change */
6109 if (!one
->oid_valid
&& !two
->oid_valid
)
6110 return 1; /* both look at the same file on the filesystem. */
6114 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
6116 int include_conflict_headers
=
6117 (additional_headers(o
, p
->one
->path
) &&
6119 (!o
->filter
|| filter_bit_tst(DIFF_STATUS_UNMERGED
, o
)));
6122 * Check if we can return early without showing a diff. Note that
6123 * diff_filepair only stores {oid, path, mode, is_valid}
6124 * information for each path, and thus diff_unmodified_pair() only
6125 * considers those bits of info. However, we do not want pairs
6126 * created by create_filepairs_for_header_only_notifications()
6127 * (which always look like unmodified pairs) to be ignored, so
6128 * return early if both p is unmodified AND we don't want to
6129 * include_conflict_headers.
6131 if (diff_unmodified_pair(p
) && !include_conflict_headers
)
6134 /* Actually, we can also return early to avoid showing tree diffs */
6135 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6136 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6142 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
6143 struct diffstat_t
*diffstat
)
6145 if (diff_unmodified_pair(p
))
6148 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6149 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6150 return; /* no useful stat for tree diffs */
6152 run_diffstat(p
, o
, diffstat
);
6155 static void diff_flush_checkdiff(struct diff_filepair
*p
,
6156 struct diff_options
*o
)
6158 if (diff_unmodified_pair(p
))
6161 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6162 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6163 return; /* nothing to check in tree diffs */
6165 run_checkdiff(p
, o
);
6168 int diff_queue_is_empty(struct diff_options
*o
)
6170 struct diff_queue_struct
*q
= &diff_queued_diff
;
6172 int include_conflict_headers
=
6173 (o
->additional_path_headers
&&
6174 strmap_get_size(o
->additional_path_headers
) &&
6176 (!o
->filter
|| filter_bit_tst(DIFF_STATUS_UNMERGED
, o
)));
6178 if (include_conflict_headers
)
6181 for (i
= 0; i
< q
->nr
; i
++)
6182 if (!diff_unmodified_pair(q
->queue
[i
]))
6188 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
6190 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
6193 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
6195 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
6196 fprintf(stderr
, "queue[%d] %s size %lu\n",
6201 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
6203 diff_debug_filespec(p
->one
, i
, "one");
6204 diff_debug_filespec(p
->two
, i
, "two");
6205 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
6206 p
->score
, p
->status
? p
->status
: '?',
6207 p
->one
->rename_used
, p
->broken_pair
);
6210 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
6214 fprintf(stderr
, "%s\n", msg
);
6215 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
6216 for (i
= 0; i
< q
->nr
; i
++) {
6217 struct diff_filepair
*p
= q
->queue
[i
];
6218 diff_debug_filepair(p
, i
);
6223 static void diff_resolve_rename_copy(void)
6226 struct diff_filepair
*p
;
6227 struct diff_queue_struct
*q
= &diff_queued_diff
;
6229 diff_debug_queue("resolve-rename-copy", q
);
6231 for (i
= 0; i
< q
->nr
; i
++) {
6233 p
->status
= 0; /* undecided */
6234 if (DIFF_PAIR_UNMERGED(p
))
6235 p
->status
= DIFF_STATUS_UNMERGED
;
6236 else if (!DIFF_FILE_VALID(p
->one
))
6237 p
->status
= DIFF_STATUS_ADDED
;
6238 else if (!DIFF_FILE_VALID(p
->two
))
6239 p
->status
= DIFF_STATUS_DELETED
;
6240 else if (DIFF_PAIR_TYPE_CHANGED(p
))
6241 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
6243 /* from this point on, we are dealing with a pair
6244 * whose both sides are valid and of the same type, i.e.
6245 * either in-place edit or rename/copy edit.
6247 else if (DIFF_PAIR_RENAME(p
)) {
6249 * A rename might have re-connected a broken
6250 * pair up, causing the pathnames to be the
6251 * same again. If so, that's not a rename at
6252 * all, just a modification..
6254 * Otherwise, see if this source was used for
6255 * multiple renames, in which case we decrement
6256 * the count, and call it a copy.
6258 if (!strcmp(p
->one
->path
, p
->two
->path
))
6259 p
->status
= DIFF_STATUS_MODIFIED
;
6260 else if (--p
->one
->rename_used
> 0)
6261 p
->status
= DIFF_STATUS_COPIED
;
6263 p
->status
= DIFF_STATUS_RENAMED
;
6265 else if (!oideq(&p
->one
->oid
, &p
->two
->oid
) ||
6266 p
->one
->mode
!= p
->two
->mode
||
6267 p
->one
->dirty_submodule
||
6268 p
->two
->dirty_submodule
||
6269 is_null_oid(&p
->one
->oid
))
6270 p
->status
= DIFF_STATUS_MODIFIED
;
6272 /* This is a "no-change" entry and should not
6273 * happen anymore, but prepare for broken callers.
6275 error("feeding unmodified %s to diffcore",
6277 p
->status
= DIFF_STATUS_UNKNOWN
;
6280 diff_debug_queue("resolve-rename-copy done", q
);
6283 static int check_pair_status(struct diff_filepair
*p
)
6285 switch (p
->status
) {
6286 case DIFF_STATUS_UNKNOWN
:
6289 die("internal error in diff-resolve-rename-copy");
6295 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
6297 int fmt
= opt
->output_format
;
6299 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
6300 diff_flush_checkdiff(p
, opt
);
6301 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
6302 diff_flush_raw(p
, opt
);
6303 else if (fmt
& DIFF_FORMAT_NAME
) {
6304 const char *name_a
, *name_b
;
6305 name_a
= p
->two
->path
;
6307 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
6308 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
6309 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
6312 opt
->found_changes
= 1;
6315 static void show_file_mode_name(struct diff_options
*opt
, const char *newdelete
, struct diff_filespec
*fs
)
6317 struct strbuf sb
= STRBUF_INIT
;
6319 strbuf_addf(&sb
, " %s mode %06o ", newdelete
, fs
->mode
);
6321 strbuf_addf(&sb
, " %s ", newdelete
);
6323 quote_c_style(fs
->path
, &sb
, NULL
, 0);
6324 strbuf_addch(&sb
, '\n');
6325 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6327 strbuf_release(&sb
);
6330 static void show_mode_change(struct diff_options
*opt
, struct diff_filepair
*p
,
6333 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
6334 struct strbuf sb
= STRBUF_INIT
;
6335 strbuf_addf(&sb
, " mode change %06o => %06o",
6336 p
->one
->mode
, p
->two
->mode
);
6338 strbuf_addch(&sb
, ' ');
6339 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6341 strbuf_addch(&sb
, '\n');
6342 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6344 strbuf_release(&sb
);
6348 static void show_rename_copy(struct diff_options
*opt
, const char *renamecopy
,
6349 struct diff_filepair
*p
)
6351 struct strbuf sb
= STRBUF_INIT
;
6352 struct strbuf names
= STRBUF_INIT
;
6354 pprint_rename(&names
, p
->one
->path
, p
->two
->path
);
6355 strbuf_addf(&sb
, " %s %s (%d%%)\n",
6356 renamecopy
, names
.buf
, similarity_index(p
));
6357 strbuf_release(&names
);
6358 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6360 show_mode_change(opt
, p
, 0);
6361 strbuf_release(&sb
);
6364 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
6367 case DIFF_STATUS_DELETED
:
6368 show_file_mode_name(opt
, "delete", p
->one
);
6370 case DIFF_STATUS_ADDED
:
6371 show_file_mode_name(opt
, "create", p
->two
);
6373 case DIFF_STATUS_COPIED
:
6374 show_rename_copy(opt
, "copy", p
);
6376 case DIFF_STATUS_RENAMED
:
6377 show_rename_copy(opt
, "rename", p
);
6381 struct strbuf sb
= STRBUF_INIT
;
6382 strbuf_addstr(&sb
, " rewrite ");
6383 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6384 strbuf_addf(&sb
, " (%d%%)\n", similarity_index(p
));
6385 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6387 strbuf_release(&sb
);
6389 show_mode_change(opt
, p
, !p
->score
);
6399 static int remove_space(char *line
, int len
)
6405 for (i
= 0; i
< len
; i
++)
6406 if (!isspace((c
= line
[i
])))
6412 void flush_one_hunk(struct object_id
*result
, git_hash_ctx
*ctx
)
6414 unsigned char hash
[GIT_MAX_RAWSZ
];
6415 unsigned short carry
= 0;
6418 the_hash_algo
->final_fn(hash
, ctx
);
6419 the_hash_algo
->init_fn(ctx
);
6420 /* 20-byte sum, with carry */
6421 for (i
= 0; i
< the_hash_algo
->rawsz
; ++i
) {
6422 carry
+= result
->hash
[i
] + hash
[i
];
6423 result
->hash
[i
] = carry
;
6428 static int patch_id_consume(void *priv
, char *line
, unsigned long len
)
6430 struct patch_id_t
*data
= priv
;
6433 if (len
> 12 && starts_with(line
, "\\ "))
6435 new_len
= remove_space(line
, len
);
6437 the_hash_algo
->update_fn(data
->ctx
, line
, new_len
);
6438 data
->patchlen
+= new_len
;
6442 static void patch_id_add_string(git_hash_ctx
*ctx
, const char *str
)
6444 the_hash_algo
->update_fn(ctx
, str
, strlen(str
));
6447 static void patch_id_add_mode(git_hash_ctx
*ctx
, unsigned mode
)
6449 /* large enough for 2^32 in octal */
6451 int len
= xsnprintf(buf
, sizeof(buf
), "%06o", mode
);
6452 the_hash_algo
->update_fn(ctx
, buf
, len
);
6455 /* returns 0 upon success, and writes result into oid */
6456 static int diff_get_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
6458 struct diff_queue_struct
*q
= &diff_queued_diff
;
6461 struct patch_id_t data
;
6463 the_hash_algo
->init_fn(&ctx
);
6464 memset(&data
, 0, sizeof(struct patch_id_t
));
6466 oidclr(oid
, the_repository
->hash_algo
);
6468 for (i
= 0; i
< q
->nr
; i
++) {
6472 struct diff_filepair
*p
= q
->queue
[i
];
6475 memset(&xpp
, 0, sizeof(xpp
));
6476 memset(&xecfg
, 0, sizeof(xecfg
));
6478 return error("internal diff status error");
6479 if (p
->status
== DIFF_STATUS_UNKNOWN
)
6481 if (diff_unmodified_pair(p
))
6483 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6484 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6486 if (DIFF_PAIR_UNMERGED(p
))
6489 diff_fill_oid_info(p
->one
, options
->repo
->index
);
6490 diff_fill_oid_info(p
->two
, options
->repo
->index
);
6492 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
6493 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
6494 patch_id_add_string(&ctx
, "diff--git");
6495 patch_id_add_string(&ctx
, "a/");
6496 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6497 patch_id_add_string(&ctx
, "b/");
6498 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6500 if (p
->one
->mode
== 0) {
6501 patch_id_add_string(&ctx
, "newfilemode");
6502 patch_id_add_mode(&ctx
, p
->two
->mode
);
6503 } else if (p
->two
->mode
== 0) {
6504 patch_id_add_string(&ctx
, "deletedfilemode");
6505 patch_id_add_mode(&ctx
, p
->one
->mode
);
6506 } else if (p
->one
->mode
!= p
->two
->mode
) {
6507 patch_id_add_string(&ctx
, "oldmode");
6508 patch_id_add_mode(&ctx
, p
->one
->mode
);
6509 patch_id_add_string(&ctx
, "newmode");
6510 patch_id_add_mode(&ctx
, p
->two
->mode
);
6513 if (diff_header_only
) {
6514 /* don't do anything since we're only populating header info */
6515 } else if (diff_filespec_is_binary(options
->repo
, p
->one
) ||
6516 diff_filespec_is_binary(options
->repo
, p
->two
)) {
6517 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->one
->oid
),
6518 the_hash_algo
->hexsz
);
6519 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->two
->oid
),
6520 the_hash_algo
->hexsz
);
6522 if (p
->one
->mode
== 0) {
6523 patch_id_add_string(&ctx
, "---/dev/null");
6524 patch_id_add_string(&ctx
, "+++b/");
6525 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6526 } else if (p
->two
->mode
== 0) {
6527 patch_id_add_string(&ctx
, "---a/");
6528 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6529 patch_id_add_string(&ctx
, "+++/dev/null");
6531 patch_id_add_string(&ctx
, "---a/");
6532 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6533 patch_id_add_string(&ctx
, "+++b/");
6534 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6537 if (fill_mmfile(options
->repo
, &mf1
, p
->one
) < 0 ||
6538 fill_mmfile(options
->repo
, &mf2
, p
->two
) < 0)
6539 return error("unable to read files to diff");
6542 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
6543 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
6544 patch_id_consume
, &data
, &xpp
, &xecfg
))
6545 return error("unable to generate patch-id diff for %s",
6548 flush_one_hunk(oid
, &ctx
);
6554 int diff_flush_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
6556 struct diff_queue_struct
*q
= &diff_queued_diff
;
6557 int result
= diff_get_patch_id(options
, oid
, diff_header_only
);
6559 diff_queue_clear(q
);
6564 static int is_summary_empty(const struct diff_queue_struct
*q
)
6568 for (i
= 0; i
< q
->nr
; i
++) {
6569 const struct diff_filepair
*p
= q
->queue
[i
];
6571 switch (p
->status
) {
6572 case DIFF_STATUS_DELETED
:
6573 case DIFF_STATUS_ADDED
:
6574 case DIFF_STATUS_COPIED
:
6575 case DIFF_STATUS_RENAMED
:
6580 if (p
->one
->mode
&& p
->two
->mode
&&
6581 p
->one
->mode
!= p
->two
->mode
)
6589 static const char rename_limit_warning
[] =
6590 N_("exhaustive rename detection was skipped due to too many files.");
6592 static const char degrade_cc_to_c_warning
[] =
6593 N_("only found copies from modified paths due to too many files.");
6595 static const char rename_limit_advice
[] =
6596 N_("you may want to set your %s variable to at least "
6597 "%d and retry the command.");
6599 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
6603 warning(_(degrade_cc_to_c_warning
));
6605 warning(_(rename_limit_warning
));
6609 warning(_(rename_limit_advice
), varname
, needed
);
6612 static void create_filepairs_for_header_only_notifications(struct diff_options
*o
)
6614 struct strset present
;
6615 struct diff_queue_struct
*q
= &diff_queued_diff
;
6616 struct hashmap_iter iter
;
6617 struct strmap_entry
*e
;
6620 strset_init_with_options(&present
, /*pool*/ NULL
, /*strdup*/ 0);
6623 * Find out which paths exist in diff_queued_diff, preferring
6624 * one->path for any pair that has multiple paths.
6626 for (i
= 0; i
< q
->nr
; i
++) {
6627 struct diff_filepair
*p
= q
->queue
[i
];
6628 char *path
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
6630 if (strmap_contains(o
->additional_path_headers
, path
))
6631 strset_add(&present
, path
);
6635 * Loop over paths in additional_path_headers; for each NOT already
6636 * in diff_queued_diff, create a synthetic filepair and insert that
6637 * into diff_queued_diff.
6639 strmap_for_each_entry(o
->additional_path_headers
, &iter
, e
) {
6640 if (!strset_contains(&present
, e
->key
)) {
6641 struct diff_filespec
*one
, *two
;
6642 struct diff_filepair
*p
;
6644 one
= alloc_filespec(e
->key
);
6645 two
= alloc_filespec(e
->key
);
6646 fill_filespec(one
, null_oid(), 0, 0);
6647 fill_filespec(two
, null_oid(), 0, 0);
6648 p
= diff_queue(q
, one
, two
);
6649 p
->status
= DIFF_STATUS_MODIFIED
;
6653 /* Re-sort the filepairs */
6654 diffcore_fix_diff_index();
6657 strset_clear(&present
);
6660 static void diff_flush_patch_all_file_pairs(struct diff_options
*o
)
6663 static struct emitted_diff_symbols esm
= EMITTED_DIFF_SYMBOLS_INIT
;
6664 struct diff_queue_struct
*q
= &diff_queued_diff
;
6666 if (WSEH_NEW
& WS_RULE_MASK
)
6667 BUG("WS rules bit mask overlaps with diff symbol flags");
6670 o
->emitted_symbols
= &esm
;
6672 if (o
->additional_path_headers
)
6673 create_filepairs_for_header_only_notifications(o
);
6675 for (i
= 0; i
< q
->nr
; i
++) {
6676 struct diff_filepair
*p
= q
->queue
[i
];
6677 if (check_pair_status(p
))
6678 diff_flush_patch(p
, o
);
6681 if (o
->emitted_symbols
) {
6682 if (o
->color_moved
) {
6683 struct mem_pool entry_pool
;
6684 struct moved_entry_list
*entry_list
;
6686 mem_pool_init(&entry_pool
, 1024 * 1024);
6687 entry_list
= add_lines_to_move_detection(o
,
6689 mark_color_as_moved(o
, entry_list
);
6690 if (o
->color_moved
== COLOR_MOVED_ZEBRA_DIM
)
6693 mem_pool_discard(&entry_pool
, 0);
6697 for (i
= 0; i
< esm
.nr
; i
++)
6698 emit_diff_symbol_from_struct(o
, &esm
.buf
[i
]);
6700 for (i
= 0; i
< esm
.nr
; i
++)
6701 free((void *)esm
.buf
[i
].line
);
6704 o
->emitted_symbols
= NULL
;
6708 static void diff_free_file(struct diff_options
*options
)
6710 if (options
->close_file
&& options
->file
) {
6711 fclose(options
->file
);
6712 options
->file
= NULL
;
6716 static void diff_free_ignore_regex(struct diff_options
*options
)
6720 for (i
= 0; i
< options
->ignore_regex_nr
; i
++) {
6721 regfree(options
->ignore_regex
[i
]);
6722 free(options
->ignore_regex
[i
]);
6725 FREE_AND_NULL(options
->ignore_regex
);
6726 options
->ignore_regex_nr
= 0;
6729 void diff_free(struct diff_options
*options
)
6731 if (options
->no_free
)
6734 if (options
->objfind
) {
6735 oidset_clear(options
->objfind
);
6736 FREE_AND_NULL(options
->objfind
);
6739 FREE_AND_NULL(options
->orderfile
);
6740 for (size_t i
= 0; i
< options
->anchors_nr
; i
++)
6741 free(options
->anchors
[i
]);
6742 FREE_AND_NULL(options
->anchors
);
6743 options
->anchors_nr
= options
->anchors_alloc
= 0;
6745 diff_free_file(options
);
6746 diff_free_ignore_regex(options
);
6747 clear_pathspec(&options
->pathspec
);
6750 void diff_flush(struct diff_options
*options
)
6752 struct diff_queue_struct
*q
= &diff_queued_diff
;
6753 int i
, output_format
= options
->output_format
;
6755 int dirstat_by_line
= 0;
6758 * Order: raw, stat, summary, patch
6759 * or: name/name-status/checkdiff (other bits clear)
6761 if (!q
->nr
&& !options
->additional_path_headers
)
6764 if (output_format
& (DIFF_FORMAT_RAW
|
6766 DIFF_FORMAT_NAME_STATUS
|
6767 DIFF_FORMAT_CHECKDIFF
)) {
6768 for (i
= 0; i
< q
->nr
; i
++) {
6769 struct diff_filepair
*p
= q
->queue
[i
];
6770 if (check_pair_status(p
))
6771 flush_one_pair(p
, options
);
6776 if (output_format
& DIFF_FORMAT_DIRSTAT
&& options
->flags
.dirstat_by_line
)
6777 dirstat_by_line
= 1;
6779 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
6781 struct diffstat_t diffstat
;
6783 compute_diffstat(options
, &diffstat
, q
);
6784 if (output_format
& DIFF_FORMAT_NUMSTAT
)
6785 show_numstat(&diffstat
, options
);
6786 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
6787 show_stats(&diffstat
, options
);
6788 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
6789 show_shortstats(&diffstat
, options
);
6790 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
6791 show_dirstat_by_line(&diffstat
, options
);
6792 free_diffstat_info(&diffstat
);
6795 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
6796 show_dirstat(options
);
6798 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
6799 for (i
= 0; i
< q
->nr
; i
++) {
6800 diff_summary(options
, q
->queue
[i
]);
6805 if (output_format
& DIFF_FORMAT_PATCH
) {
6807 emit_diff_symbol(options
, DIFF_SYMBOL_SEPARATOR
, NULL
, 0, 0);
6808 if (options
->stat_sep
)
6809 /* attach patch instead of inline */
6810 emit_diff_symbol(options
, DIFF_SYMBOL_STAT_SEP
,
6814 diff_flush_patch_all_file_pairs(options
);
6817 if (output_format
& DIFF_FORMAT_CALLBACK
)
6818 options
->format_callback(q
, options
, options
->format_callback_data
);
6820 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
6821 options
->flags
.exit_with_status
&&
6822 options
->flags
.diff_from_contents
) {
6824 * run diff_flush_patch for the exit status. setting
6825 * options->file to /dev/null should be safe, because we
6826 * aren't supposed to produce any output anyway.
6828 diff_free_file(options
);
6829 options
->file
= xfopen("/dev/null", "w");
6830 options
->close_file
= 1;
6831 options
->color_moved
= 0;
6832 for (i
= 0; i
< q
->nr
; i
++) {
6833 struct diff_filepair
*p
= q
->queue
[i
];
6834 if (check_pair_status(p
))
6835 diff_flush_patch(p
, options
);
6836 if (options
->found_changes
)
6842 diff_queue_clear(q
);
6846 * Report the content-level differences with HAS_CHANGES;
6847 * diff_addremove/diff_change does not set the bit when
6848 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
6850 if (options
->flags
.diff_from_contents
) {
6851 if (options
->found_changes
)
6852 options
->flags
.has_changes
= 1;
6854 options
->flags
.has_changes
= 0;
6858 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
6860 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
6862 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
6864 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
6865 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
6866 filter_bit_tst(p
->status
, options
)));
6869 static void diffcore_apply_filter(struct diff_options
*options
)
6872 struct diff_queue_struct
*q
= &diff_queued_diff
;
6873 struct diff_queue_struct outq
= DIFF_QUEUE_INIT
;
6875 if (!options
->filter
)
6878 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
6880 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
6881 if (match_filter(options
, q
->queue
[i
]))
6887 /* otherwise we will clear the whole queue
6888 * by copying the empty outq at the end of this
6889 * function, but first clear the current entries
6892 for (i
= 0; i
< q
->nr
; i
++)
6893 diff_free_filepair(q
->queue
[i
]);
6896 /* Only the matching ones */
6897 for (i
= 0; i
< q
->nr
; i
++) {
6898 struct diff_filepair
*p
= q
->queue
[i
];
6899 if (match_filter(options
, p
))
6902 diff_free_filepair(p
);
6909 /* Check whether two filespecs with the same mode and size are identical */
6910 static int diff_filespec_is_identical(struct repository
*r
,
6911 struct diff_filespec
*one
,
6912 struct diff_filespec
*two
)
6914 if (S_ISGITLINK(one
->mode
))
6916 if (diff_populate_filespec(r
, one
, NULL
))
6918 if (diff_populate_filespec(r
, two
, NULL
))
6920 return !memcmp(one
->data
, two
->data
, one
->size
);
6923 static int diff_filespec_check_stat_unmatch(struct repository
*r
,
6924 struct diff_filepair
*p
)
6926 struct diff_populate_filespec_options dpf_options
= {
6927 .check_size_only
= 1,
6928 .missing_object_cb
= diff_queued_diff_prefetch
,
6929 .missing_object_data
= r
,
6932 if (p
->done_skip_stat_unmatch
)
6933 return p
->skip_stat_unmatch_result
;
6935 p
->done_skip_stat_unmatch
= 1;
6936 p
->skip_stat_unmatch_result
= 0;
6938 * 1. Entries that come from stat info dirtiness
6939 * always have both sides (iow, not create/delete),
6940 * one side of the object name is unknown, with
6941 * the same mode and size. Keep the ones that
6942 * do not match these criteria. They have real
6945 * 2. At this point, the file is known to be modified,
6946 * with the same mode and size, and the object
6947 * name of one side is unknown. Need to inspect
6948 * the identical contents.
6950 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
6951 !DIFF_FILE_VALID(p
->two
) ||
6952 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
6953 (p
->one
->mode
!= p
->two
->mode
) ||
6954 diff_populate_filespec(r
, p
->one
, &dpf_options
) ||
6955 diff_populate_filespec(r
, p
->two
, &dpf_options
) ||
6956 (p
->one
->size
!= p
->two
->size
) ||
6957 !diff_filespec_is_identical(r
, p
->one
, p
->two
)) /* (2) */
6958 p
->skip_stat_unmatch_result
= 1;
6959 return p
->skip_stat_unmatch_result
;
6962 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
6965 struct diff_queue_struct
*q
= &diff_queued_diff
;
6966 struct diff_queue_struct outq
= DIFF_QUEUE_INIT
;
6968 for (i
= 0; i
< q
->nr
; i
++) {
6969 struct diff_filepair
*p
= q
->queue
[i
];
6971 if (diff_filespec_check_stat_unmatch(diffopt
->repo
, p
))
6975 * The caller can subtract 1 from skip_stat_unmatch
6976 * to determine how many paths were dirty only
6977 * due to stat info mismatch.
6979 if (!diffopt
->flags
.no_index
)
6980 diffopt
->skip_stat_unmatch
++;
6981 diff_free_filepair(p
);
6988 static int diffnamecmp(const void *a_
, const void *b_
)
6990 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
6991 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
6992 const char *name_a
, *name_b
;
6994 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
6995 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
6996 return strcmp(name_a
, name_b
);
6999 void diffcore_fix_diff_index(void)
7001 struct diff_queue_struct
*q
= &diff_queued_diff
;
7002 QSORT(q
->queue
, q
->nr
, diffnamecmp
);
7005 void diff_add_if_missing(struct repository
*r
,
7006 struct oid_array
*to_fetch
,
7007 const struct diff_filespec
*filespec
)
7009 if (filespec
&& filespec
->oid_valid
&&
7010 !S_ISGITLINK(filespec
->mode
) &&
7011 oid_object_info_extended(r
, &filespec
->oid
, NULL
,
7012 OBJECT_INFO_FOR_PREFETCH
))
7013 oid_array_append(to_fetch
, &filespec
->oid
);
7016 void diff_queued_diff_prefetch(void *repository
)
7018 struct repository
*repo
= repository
;
7020 struct diff_queue_struct
*q
= &diff_queued_diff
;
7021 struct oid_array to_fetch
= OID_ARRAY_INIT
;
7023 for (i
= 0; i
< q
->nr
; i
++) {
7024 struct diff_filepair
*p
= q
->queue
[i
];
7025 diff_add_if_missing(repo
, &to_fetch
, p
->one
);
7026 diff_add_if_missing(repo
, &to_fetch
, p
->two
);
7030 * NEEDSWORK: Consider deduplicating the OIDs sent.
7032 promisor_remote_get_direct(repo
, to_fetch
.oid
, to_fetch
.nr
);
7034 oid_array_clear(&to_fetch
);
7037 void init_diffstat_widths(struct diff_options
*options
)
7039 options
->stat_width
= -1; /* use full terminal width */
7040 options
->stat_name_width
= -1; /* respect diff.statNameWidth config */
7041 options
->stat_graph_width
= -1; /* respect diff.statGraphWidth config */
7044 void diffcore_std(struct diff_options
*options
)
7046 int output_formats_to_prefetch
= DIFF_FORMAT_DIFFSTAT
|
7047 DIFF_FORMAT_NUMSTAT
|
7049 DIFF_FORMAT_SHORTSTAT
|
7050 DIFF_FORMAT_DIRSTAT
;
7053 * Check if the user requested a blob-data-requiring diff output and/or
7054 * break-rewrite detection (which requires blob data). If yes, prefetch
7057 * If no prefetching occurs, diffcore_rename() will prefetch if it
7058 * decides that it needs inexact rename detection.
7060 if (options
->repo
== the_repository
&& repo_has_promisor_remote(the_repository
) &&
7061 (options
->output_format
& output_formats_to_prefetch
||
7062 options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
7063 diff_queued_diff_prefetch(options
->repo
);
7065 /* NOTE please keep the following in sync with diff_tree_combined() */
7066 if (options
->skip_stat_unmatch
)
7067 diffcore_skip_stat_unmatch(options
);
7068 if (!options
->found_follow
) {
7069 /* See try_to_follow_renames() in tree-diff.c */
7070 if (options
->break_opt
!= -1)
7071 diffcore_break(options
->repo
,
7072 options
->break_opt
);
7073 if (options
->detect_rename
)
7074 diffcore_rename(options
);
7075 if (options
->break_opt
!= -1)
7076 diffcore_merge_broken();
7078 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
7079 diffcore_pickaxe(options
);
7080 if (options
->orderfile
)
7081 diffcore_order(options
->orderfile
);
7082 if (options
->rotate_to
)
7083 diffcore_rotate(options
);
7084 if (!options
->found_follow
)
7085 /* See try_to_follow_renames() in tree-diff.c */
7086 diff_resolve_rename_copy();
7087 diffcore_apply_filter(options
);
7089 if (diff_queued_diff
.nr
&& !options
->flags
.diff_from_contents
)
7090 options
->flags
.has_changes
= 1;
7092 options
->flags
.has_changes
= 0;
7094 options
->found_follow
= 0;
7097 int diff_result_code(struct rev_info
*revs
)
7099 struct diff_options
*opt
= &revs
->diffopt
;
7102 if (revs
->remerge_diff
) {
7103 tmp_objdir_destroy(revs
->remerge_objdir
);
7104 revs
->remerge_objdir
= NULL
;
7107 diff_warn_rename_limit("diff.renameLimit",
7108 opt
->needed_rename_limit
,
7109 opt
->degraded_cc_to_c
);
7111 if (opt
->flags
.exit_with_status
&&
7112 opt
->flags
.has_changes
)
7114 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
7115 opt
->flags
.check_failed
)
7120 int diff_can_quit_early(struct diff_options
*opt
)
7122 return (opt
->flags
.quick
&&
7124 opt
->flags
.has_changes
);
7128 * Shall changes to this submodule be ignored?
7130 * Submodule changes can be configured to be ignored separately for each path,
7131 * but that configuration can be overridden from the command line.
7133 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
7136 struct diff_flags orig_flags
= options
->flags
;
7137 if (!options
->flags
.override_submodule_config
)
7138 set_diffopt_flags_from_submodule_config(options
, path
);
7139 if (options
->flags
.ignore_submodules
)
7141 options
->flags
= orig_flags
;
7145 void compute_diffstat(struct diff_options
*options
,
7146 struct diffstat_t
*diffstat
,
7147 struct diff_queue_struct
*q
)
7151 memset(diffstat
, 0, sizeof(struct diffstat_t
));
7152 for (i
= 0; i
< q
->nr
; i
++) {
7153 struct diff_filepair
*p
= q
->queue
[i
];
7154 if (check_pair_status(p
))
7155 diff_flush_stat(p
, options
, diffstat
);
7157 options
->found_changes
= !!diffstat
->nr
;
7160 void diff_addremove(struct diff_options
*options
,
7161 int addremove
, unsigned mode
,
7162 const struct object_id
*oid
,
7164 const char *concatpath
, unsigned dirty_submodule
)
7166 struct diff_filespec
*one
, *two
;
7168 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
7171 /* This may look odd, but it is a preparation for
7172 * feeding "there are unchanged files which should
7173 * not produce diffs, but when you are doing copy
7174 * detection you would need them, so here they are"
7175 * entries to the diff-core. They will be prefixed
7176 * with something like '=' or '*' (I haven't decided
7177 * which but should not make any difference).
7178 * Feeding the same new and old to diff_change()
7179 * also has the same effect.
7180 * Before the final output happens, they are pruned after
7181 * merged into rename/copy pairs as appropriate.
7183 if (options
->flags
.reverse_diff
)
7184 addremove
= (addremove
== '+' ? '-' :
7185 addremove
== '-' ? '+' : addremove
);
7187 if (options
->prefix
&&
7188 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
7191 one
= alloc_filespec(concatpath
);
7192 two
= alloc_filespec(concatpath
);
7194 if (addremove
!= '+')
7195 fill_filespec(one
, oid
, oid_valid
, mode
);
7196 if (addremove
!= '-') {
7197 fill_filespec(two
, oid
, oid_valid
, mode
);
7198 two
->dirty_submodule
= dirty_submodule
;
7201 diff_queue(&diff_queued_diff
, one
, two
);
7202 if (!options
->flags
.diff_from_contents
)
7203 options
->flags
.has_changes
= 1;
7206 void diff_change(struct diff_options
*options
,
7207 unsigned old_mode
, unsigned new_mode
,
7208 const struct object_id
*old_oid
,
7209 const struct object_id
*new_oid
,
7210 int old_oid_valid
, int new_oid_valid
,
7211 const char *concatpath
,
7212 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
7214 struct diff_filespec
*one
, *two
;
7215 struct diff_filepair
*p
;
7217 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
7218 is_submodule_ignored(concatpath
, options
))
7221 if (options
->flags
.reverse_diff
) {
7222 SWAP(old_mode
, new_mode
);
7223 SWAP(old_oid
, new_oid
);
7224 SWAP(old_oid_valid
, new_oid_valid
);
7225 SWAP(old_dirty_submodule
, new_dirty_submodule
);
7228 if (options
->prefix
&&
7229 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
7232 one
= alloc_filespec(concatpath
);
7233 two
= alloc_filespec(concatpath
);
7234 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
7235 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
7236 one
->dirty_submodule
= old_dirty_submodule
;
7237 two
->dirty_submodule
= new_dirty_submodule
;
7238 p
= diff_queue(&diff_queued_diff
, one
, two
);
7240 if (options
->flags
.diff_from_contents
)
7243 if (options
->flags
.quick
&& options
->skip_stat_unmatch
&&
7244 !diff_filespec_check_stat_unmatch(options
->repo
, p
)) {
7245 diff_free_filespec_data(p
->one
);
7246 diff_free_filespec_data(p
->two
);
7250 options
->flags
.has_changes
= 1;
7253 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
7255 struct diff_filepair
*pair
;
7256 struct diff_filespec
*one
, *two
;
7258 if (options
->prefix
&&
7259 strncmp(path
, options
->prefix
, options
->prefix_length
))
7262 one
= alloc_filespec(path
);
7263 two
= alloc_filespec(path
);
7264 pair
= diff_queue(&diff_queued_diff
, one
, two
);
7265 pair
->is_unmerged
= 1;
7269 static char *run_textconv(struct repository
*r
,
7271 struct diff_filespec
*spec
,
7274 struct diff_tempfile
*temp
;
7275 struct child_process child
= CHILD_PROCESS_INIT
;
7276 struct strbuf buf
= STRBUF_INIT
;
7279 temp
= prepare_temp_file(r
, spec
);
7280 strvec_push(&child
.args
, pgm
);
7281 strvec_push(&child
.args
, temp
->name
);
7283 child
.use_shell
= 1;
7285 if (start_command(&child
)) {
7290 if (strbuf_read(&buf
, child
.out
, 0) < 0)
7291 err
= error("error reading from textconv command '%s'", pgm
);
7294 if (finish_command(&child
) || err
) {
7295 strbuf_release(&buf
);
7301 return strbuf_detach(&buf
, outsize
);
7304 size_t fill_textconv(struct repository
*r
,
7305 struct userdiff_driver
*driver
,
7306 struct diff_filespec
*df
,
7312 if (!DIFF_FILE_VALID(df
)) {
7313 *outbuf
= (char *) "";
7316 if (diff_populate_filespec(r
, df
, NULL
))
7317 die("unable to read files to diff");
7322 if (!driver
->textconv
)
7323 BUG("fill_textconv called with non-textconv driver");
7325 if (driver
->textconv_cache
&& df
->oid_valid
) {
7326 *outbuf
= notes_cache_get(driver
->textconv_cache
,
7333 *outbuf
= run_textconv(r
, driver
->textconv
, df
, &size
);
7335 die("unable to read files to diff");
7337 if (driver
->textconv_cache
&& df
->oid_valid
) {
7338 /* ignore errors, as we might be in a readonly repository */
7339 notes_cache_put(driver
->textconv_cache
, &df
->oid
, *outbuf
,
7342 * we could save up changes and flush them all at the end,
7343 * but we would need an extra call after all diffing is done.
7344 * Since generating a cache entry is the slow path anyway,
7345 * this extra overhead probably isn't a big deal.
7347 notes_cache_write(driver
->textconv_cache
);
7353 int textconv_object(struct repository
*r
,
7356 const struct object_id
*oid
,
7359 unsigned long *buf_size
)
7361 struct diff_filespec
*df
;
7362 struct userdiff_driver
*textconv
;
7364 df
= alloc_filespec(path
);
7365 fill_filespec(df
, oid
, oid_valid
, mode
);
7366 textconv
= get_textconv(r
, df
);
7372 *buf_size
= fill_textconv(r
, textconv
, df
, buf
);
7377 void setup_diff_pager(struct diff_options
*opt
)
7380 * If the user asked for our exit code, then either they want --quiet
7381 * or --exit-code. We should definitely not bother with a pager in the
7382 * former case, as we will generate no output. Since we still properly
7383 * report our exit code even when a pager is run, we _could_ run a
7384 * pager with --exit-code. But since we have not done so historically,
7385 * and because it is easy to find people oneline advising "git diff
7386 * --exit-code" in hooks and other scripts, we do not do so.
7388 if (!opt
->flags
.exit_with_status
&&
7389 check_pager_config(the_repository
, "diff") != 0)
7390 setup_pager(the_repository
);