2 * Copyright (C) 2005 Junio C Hamano
11 #include "xdiff-interface.h"
14 #include "run-command.h"
17 #include "submodule-config.h"
18 #include "submodule.h"
21 #include "string-list.h"
22 #include "argv-array.h"
26 #ifdef NO_FAST_WORKING_DIRECTORY
27 #define FAST_WORKING_DIRECTORY 0
29 #define FAST_WORKING_DIRECTORY 1
32 static int diff_detect_rename_default
;
33 static int diff_indent_heuristic
= 1;
34 static int diff_rename_limit_default
= 400;
35 static int diff_suppress_blank_empty
;
36 static int diff_use_color_default
= -1;
37 static int diff_color_moved_default
;
38 static int diff_context_default
= 3;
39 static int diff_interhunk_context_default
;
40 static const char *diff_word_regex_cfg
;
41 static const char *external_diff_cmd_cfg
;
42 static const char *diff_order_file_cfg
;
43 int diff_auto_refresh_index
= 1;
44 static int diff_mnemonic_prefix
;
45 static int diff_no_prefix
;
46 static int diff_stat_graph_width
;
47 static int diff_dirstat_permille_default
= 30;
48 static struct diff_options default_diff_options
;
49 static long diff_algorithm
;
50 static unsigned ws_error_highlight_default
= WSEH_NEW
;
52 static char diff_colors
[][COLOR_MAXLEN
] = {
54 GIT_COLOR_NORMAL
, /* CONTEXT */
55 GIT_COLOR_BOLD
, /* METAINFO */
56 GIT_COLOR_CYAN
, /* FRAGINFO */
57 GIT_COLOR_RED
, /* OLD */
58 GIT_COLOR_GREEN
, /* NEW */
59 GIT_COLOR_YELLOW
, /* COMMIT */
60 GIT_COLOR_BG_RED
, /* WHITESPACE */
61 GIT_COLOR_NORMAL
, /* FUNCINFO */
62 GIT_COLOR_BOLD_MAGENTA
, /* OLD_MOVED */
63 GIT_COLOR_BOLD_BLUE
, /* OLD_MOVED ALTERNATIVE */
64 GIT_COLOR_FAINT
, /* OLD_MOVED_DIM */
65 GIT_COLOR_FAINT_ITALIC
, /* OLD_MOVED_ALTERNATIVE_DIM */
66 GIT_COLOR_BOLD_CYAN
, /* NEW_MOVED */
67 GIT_COLOR_BOLD_YELLOW
, /* NEW_MOVED ALTERNATIVE */
68 GIT_COLOR_FAINT
, /* NEW_MOVED_DIM */
69 GIT_COLOR_FAINT_ITALIC
, /* NEW_MOVED_ALTERNATIVE_DIM */
72 static NORETURN
void die_want_option(const char *option_name
)
74 die(_("option '%s' requires a value"), option_name
);
77 static int parse_diff_color_slot(const char *var
)
79 if (!strcasecmp(var
, "context") || !strcasecmp(var
, "plain"))
81 if (!strcasecmp(var
, "meta"))
83 if (!strcasecmp(var
, "frag"))
85 if (!strcasecmp(var
, "old"))
87 if (!strcasecmp(var
, "new"))
89 if (!strcasecmp(var
, "commit"))
91 if (!strcasecmp(var
, "whitespace"))
92 return DIFF_WHITESPACE
;
93 if (!strcasecmp(var
, "func"))
95 if (!strcasecmp(var
, "oldmoved"))
96 return DIFF_FILE_OLD_MOVED
;
97 if (!strcasecmp(var
, "oldmovedalternative"))
98 return DIFF_FILE_OLD_MOVED_ALT
;
99 if (!strcasecmp(var
, "oldmoveddimmed"))
100 return DIFF_FILE_OLD_MOVED_DIM
;
101 if (!strcasecmp(var
, "oldmovedalternativedimmed"))
102 return DIFF_FILE_OLD_MOVED_ALT_DIM
;
103 if (!strcasecmp(var
, "newmoved"))
104 return DIFF_FILE_NEW_MOVED
;
105 if (!strcasecmp(var
, "newmovedalternative"))
106 return DIFF_FILE_NEW_MOVED_ALT
;
107 if (!strcasecmp(var
, "newmoveddimmed"))
108 return DIFF_FILE_NEW_MOVED_DIM
;
109 if (!strcasecmp(var
, "newmovedalternativedimmed"))
110 return DIFF_FILE_NEW_MOVED_ALT_DIM
;
114 static int parse_dirstat_params(struct diff_options
*options
, const char *params_string
,
115 struct strbuf
*errmsg
)
117 char *params_copy
= xstrdup(params_string
);
118 struct string_list params
= STRING_LIST_INIT_NODUP
;
123 string_list_split_in_place(¶ms
, params_copy
, ',', -1);
124 for (i
= 0; i
< params
.nr
; i
++) {
125 const char *p
= params
.items
[i
].string
;
126 if (!strcmp(p
, "changes")) {
127 DIFF_OPT_CLR(options
, DIRSTAT_BY_LINE
);
128 DIFF_OPT_CLR(options
, DIRSTAT_BY_FILE
);
129 } else if (!strcmp(p
, "lines")) {
130 DIFF_OPT_SET(options
, DIRSTAT_BY_LINE
);
131 DIFF_OPT_CLR(options
, DIRSTAT_BY_FILE
);
132 } else if (!strcmp(p
, "files")) {
133 DIFF_OPT_CLR(options
, DIRSTAT_BY_LINE
);
134 DIFF_OPT_SET(options
, DIRSTAT_BY_FILE
);
135 } else if (!strcmp(p
, "noncumulative")) {
136 DIFF_OPT_CLR(options
, DIRSTAT_CUMULATIVE
);
137 } else if (!strcmp(p
, "cumulative")) {
138 DIFF_OPT_SET(options
, DIRSTAT_CUMULATIVE
);
139 } else if (isdigit(*p
)) {
141 int permille
= strtoul(p
, &end
, 10) * 10;
142 if (*end
== '.' && isdigit(*++end
)) {
143 /* only use first digit */
144 permille
+= *end
- '0';
145 /* .. and ignore any further digits */
146 while (isdigit(*++end
))
150 options
->dirstat_permille
= permille
;
152 strbuf_addf(errmsg
, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
157 strbuf_addf(errmsg
, _(" Unknown dirstat parameter '%s'\n"), p
);
162 string_list_clear(¶ms
, 0);
167 static int parse_submodule_params(struct diff_options
*options
, const char *value
)
169 if (!strcmp(value
, "log"))
170 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
171 else if (!strcmp(value
, "short"))
172 options
->submodule_format
= DIFF_SUBMODULE_SHORT
;
173 else if (!strcmp(value
, "diff"))
174 options
->submodule_format
= DIFF_SUBMODULE_INLINE_DIFF
;
180 static int git_config_rename(const char *var
, const char *value
)
183 return DIFF_DETECT_RENAME
;
184 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
185 return DIFF_DETECT_COPY
;
186 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
189 long parse_algorithm_value(const char *value
)
193 else if (!strcasecmp(value
, "myers") || !strcasecmp(value
, "default"))
195 else if (!strcasecmp(value
, "minimal"))
196 return XDF_NEED_MINIMAL
;
197 else if (!strcasecmp(value
, "patience"))
198 return XDF_PATIENCE_DIFF
;
199 else if (!strcasecmp(value
, "histogram"))
200 return XDF_HISTOGRAM_DIFF
;
204 static int parse_one_token(const char **arg
, const char *token
)
207 if (skip_prefix(*arg
, token
, &rest
) && (!*rest
|| *rest
== ',')) {
214 static int parse_ws_error_highlight(const char *arg
)
216 const char *orig_arg
= arg
;
220 if (parse_one_token(&arg
, "none"))
222 else if (parse_one_token(&arg
, "default"))
224 else if (parse_one_token(&arg
, "all"))
225 val
= WSEH_NEW
| WSEH_OLD
| WSEH_CONTEXT
;
226 else if (parse_one_token(&arg
, "new"))
228 else if (parse_one_token(&arg
, "old"))
230 else if (parse_one_token(&arg
, "context"))
233 return -1 - (int)(arg
- orig_arg
);
242 * These are to give UI layer defaults.
243 * The core-level commands such as git-diff-files should
244 * never be affected by the setting of diff.renames
245 * the user happens to have in the configuration file.
247 void init_diff_ui_defaults(void)
249 diff_detect_rename_default
= 1;
252 int git_diff_heuristic_config(const char *var
, const char *value
, void *cb
)
254 if (!strcmp(var
, "diff.indentheuristic"))
255 diff_indent_heuristic
= git_config_bool(var
, value
);
259 static int parse_color_moved(const char *arg
)
261 switch (git_parse_maybe_bool(arg
)) {
263 return COLOR_MOVED_NO
;
265 return COLOR_MOVED_DEFAULT
;
270 if (!strcmp(arg
, "no"))
271 return COLOR_MOVED_NO
;
272 else if (!strcmp(arg
, "plain"))
273 return COLOR_MOVED_PLAIN
;
274 else if (!strcmp(arg
, "zebra"))
275 return COLOR_MOVED_ZEBRA
;
276 else if (!strcmp(arg
, "default"))
277 return COLOR_MOVED_DEFAULT
;
278 else if (!strcmp(arg
, "dimmed_zebra"))
279 return COLOR_MOVED_ZEBRA_DIM
;
281 return error(_("color moved setting must be one of 'no', 'default', 'zebra', 'dimmed_zebra', 'plain'"));
284 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
286 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
287 diff_use_color_default
= git_config_colorbool(var
, value
);
290 if (!strcmp(var
, "diff.colormoved")) {
291 int cm
= parse_color_moved(value
);
294 diff_color_moved_default
= cm
;
297 if (!strcmp(var
, "diff.context")) {
298 diff_context_default
= git_config_int(var
, value
);
299 if (diff_context_default
< 0)
303 if (!strcmp(var
, "diff.interhunkcontext")) {
304 diff_interhunk_context_default
= git_config_int(var
, value
);
305 if (diff_interhunk_context_default
< 0)
309 if (!strcmp(var
, "diff.renames")) {
310 diff_detect_rename_default
= git_config_rename(var
, value
);
313 if (!strcmp(var
, "diff.autorefreshindex")) {
314 diff_auto_refresh_index
= git_config_bool(var
, value
);
317 if (!strcmp(var
, "diff.mnemonicprefix")) {
318 diff_mnemonic_prefix
= git_config_bool(var
, value
);
321 if (!strcmp(var
, "diff.noprefix")) {
322 diff_no_prefix
= git_config_bool(var
, value
);
325 if (!strcmp(var
, "diff.statgraphwidth")) {
326 diff_stat_graph_width
= git_config_int(var
, value
);
329 if (!strcmp(var
, "diff.external"))
330 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
331 if (!strcmp(var
, "diff.wordregex"))
332 return git_config_string(&diff_word_regex_cfg
, var
, value
);
333 if (!strcmp(var
, "diff.orderfile"))
334 return git_config_pathname(&diff_order_file_cfg
, var
, value
);
336 if (!strcmp(var
, "diff.ignoresubmodules"))
337 handle_ignore_submodules_arg(&default_diff_options
, value
);
339 if (!strcmp(var
, "diff.submodule")) {
340 if (parse_submodule_params(&default_diff_options
, value
))
341 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
346 if (!strcmp(var
, "diff.algorithm")) {
347 diff_algorithm
= parse_algorithm_value(value
);
348 if (diff_algorithm
< 0)
353 if (!strcmp(var
, "diff.wserrorhighlight")) {
354 int val
= parse_ws_error_highlight(value
);
357 ws_error_highlight_default
= val
;
361 if (git_color_config(var
, value
, cb
) < 0)
364 return git_diff_basic_config(var
, value
, cb
);
367 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
371 if (!strcmp(var
, "diff.renamelimit")) {
372 diff_rename_limit_default
= git_config_int(var
, value
);
376 if (userdiff_config(var
, value
) < 0)
379 if (skip_prefix(var
, "diff.color.", &name
) ||
380 skip_prefix(var
, "color.diff.", &name
)) {
381 int slot
= parse_diff_color_slot(name
);
385 return config_error_nonbool(var
);
386 return color_parse(value
, diff_colors
[slot
]);
389 /* like GNU diff's --suppress-blank-empty option */
390 if (!strcmp(var
, "diff.suppressblankempty") ||
391 /* for backwards compatibility */
392 !strcmp(var
, "diff.suppress-blank-empty")) {
393 diff_suppress_blank_empty
= git_config_bool(var
, value
);
397 if (!strcmp(var
, "diff.dirstat")) {
398 struct strbuf errmsg
= STRBUF_INIT
;
399 default_diff_options
.dirstat_permille
= diff_dirstat_permille_default
;
400 if (parse_dirstat_params(&default_diff_options
, value
, &errmsg
))
401 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
403 strbuf_release(&errmsg
);
404 diff_dirstat_permille_default
= default_diff_options
.dirstat_permille
;
408 if (git_diff_heuristic_config(var
, value
, cb
) < 0)
411 return git_default_config(var
, value
, cb
);
414 static char *quote_two(const char *one
, const char *two
)
416 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
417 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
418 struct strbuf res
= STRBUF_INIT
;
420 if (need_one
+ need_two
) {
421 strbuf_addch(&res
, '"');
422 quote_c_style(one
, &res
, NULL
, 1);
423 quote_c_style(two
, &res
, NULL
, 1);
424 strbuf_addch(&res
, '"');
426 strbuf_addstr(&res
, one
);
427 strbuf_addstr(&res
, two
);
429 return strbuf_detach(&res
, NULL
);
432 static const char *external_diff(void)
434 static const char *external_diff_cmd
= NULL
;
435 static int done_preparing
= 0;
438 return external_diff_cmd
;
439 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
440 if (!external_diff_cmd
)
441 external_diff_cmd
= external_diff_cmd_cfg
;
443 return external_diff_cmd
;
447 * Keep track of files used for diffing. Sometimes such an entry
448 * refers to a temporary file, sometimes to an existing file, and
449 * sometimes to "/dev/null".
451 static struct diff_tempfile
{
453 * filename external diff should read from, or NULL if this
454 * entry is currently not in use:
458 char hex
[GIT_MAX_HEXSZ
+ 1];
462 * If this diff_tempfile instance refers to a temporary file,
463 * this tempfile object is used to manage its lifetime.
465 struct tempfile
*tempfile
;
468 struct emit_callback
{
471 int blank_at_eof_in_preimage
;
472 int blank_at_eof_in_postimage
;
474 int lno_in_postimage
;
475 const char **label_path
;
476 struct diff_words_data
*diff_words
;
477 struct diff_options
*opt
;
478 struct strbuf
*header
;
481 static int count_lines(const char *data
, int size
)
483 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
490 completely_empty
= 0;
494 completely_empty
= 0;
497 if (completely_empty
)
500 count
++; /* no trailing newline */
504 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
506 if (!DIFF_FILE_VALID(one
)) {
507 mf
->ptr
= (char *)""; /* does not matter */
511 else if (diff_populate_filespec(one
, 0))
515 mf
->size
= one
->size
;
519 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
520 static unsigned long diff_filespec_size(struct diff_filespec
*one
)
522 if (!DIFF_FILE_VALID(one
))
524 diff_populate_filespec(one
, CHECK_SIZE_ONLY
);
528 static int count_trailing_blank(mmfile_t
*mf
, unsigned ws_rule
)
531 long size
= mf
->size
;
536 ptr
+= size
- 1; /* pointing at the very end */
538 ; /* incomplete line */
540 ptr
--; /* skip the last LF */
541 while (mf
->ptr
< ptr
) {
543 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
544 if (*prev_eol
== '\n')
546 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
, ws_rule
))
554 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
555 struct emit_callback
*ecbdata
)
558 unsigned ws_rule
= ecbdata
->ws_rule
;
559 l1
= count_trailing_blank(mf1
, ws_rule
);
560 l2
= count_trailing_blank(mf2
, ws_rule
);
562 ecbdata
->blank_at_eof_in_preimage
= 0;
563 ecbdata
->blank_at_eof_in_postimage
= 0;
566 at
= count_lines(mf1
->ptr
, mf1
->size
);
567 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
569 at
= count_lines(mf2
->ptr
, mf2
->size
);
570 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
573 static void emit_line_0(struct diff_options
*o
, const char *set
, const char *reset
,
574 int first
, const char *line
, int len
)
576 int has_trailing_newline
, has_trailing_carriage_return
;
578 FILE *file
= o
->file
;
580 fputs(diff_line_prefix(o
), file
);
583 has_trailing_newline
= (first
== '\n');
584 has_trailing_carriage_return
= (!has_trailing_newline
&&
586 nofirst
= has_trailing_newline
|| has_trailing_carriage_return
;
588 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
589 if (has_trailing_newline
)
591 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
592 if (has_trailing_carriage_return
)
597 if (len
|| !nofirst
) {
601 fwrite(line
, len
, 1, file
);
604 if (has_trailing_carriage_return
)
606 if (has_trailing_newline
)
610 static void emit_line(struct diff_options
*o
, const char *set
, const char *reset
,
611 const char *line
, int len
)
613 emit_line_0(o
, set
, reset
, line
[0], line
+1, len
-1);
617 DIFF_SYMBOL_BINARY_DIFF_HEADER
,
618 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
619 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
620 DIFF_SYMBOL_BINARY_DIFF_BODY
,
621 DIFF_SYMBOL_BINARY_DIFF_FOOTER
,
622 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
623 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
624 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
625 DIFF_SYMBOL_STATS_LINE
,
626 DIFF_SYMBOL_WORD_DIFF
,
627 DIFF_SYMBOL_STAT_SEP
,
629 DIFF_SYMBOL_SUBMODULE_ADD
,
630 DIFF_SYMBOL_SUBMODULE_DEL
,
631 DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
632 DIFF_SYMBOL_SUBMODULE_MODIFIED
,
633 DIFF_SYMBOL_SUBMODULE_HEADER
,
634 DIFF_SYMBOL_SUBMODULE_ERROR
,
635 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
,
636 DIFF_SYMBOL_REWRITE_DIFF
,
637 DIFF_SYMBOL_BINARY_FILES
,
639 DIFF_SYMBOL_FILEPAIR_PLUS
,
640 DIFF_SYMBOL_FILEPAIR_MINUS
,
641 DIFF_SYMBOL_WORDS_PORCELAIN
,
644 DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
647 DIFF_SYMBOL_NO_LF_EOF
,
648 DIFF_SYMBOL_CONTEXT_FRAGINFO
,
649 DIFF_SYMBOL_CONTEXT_MARKER
,
650 DIFF_SYMBOL_SEPARATOR
653 * Flags for content lines:
654 * 0..12 are whitespace rules
655 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
656 * 16 is marking if the line is blank at EOF
658 #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
659 #define DIFF_SYMBOL_MOVED_LINE (1<<17)
660 #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
661 #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
662 #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
665 * This struct is used when we need to buffer the output of the diff output.
667 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
668 * into the pre/post image file. This pointer could be a union with the
669 * line pointer. By storing an offset into the file instead of the literal line,
670 * we can decrease the memory footprint for the buffered output. At first we
671 * may want to only have indirection for the content lines, but we could also
672 * enhance the state for emitting prefabricated lines, e.g. the similarity
673 * score line or hunk/file headers would only need to store a number or path
674 * and then the output can be constructed later on depending on state.
676 struct emitted_diff_symbol
{
682 #define EMITTED_DIFF_SYMBOL_INIT {NULL}
684 struct emitted_diff_symbols
{
685 struct emitted_diff_symbol
*buf
;
688 #define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0}
690 static void append_emitted_diff_symbol(struct diff_options
*o
,
691 struct emitted_diff_symbol
*e
)
693 struct emitted_diff_symbol
*f
;
695 ALLOC_GROW(o
->emitted_symbols
->buf
,
696 o
->emitted_symbols
->nr
+ 1,
697 o
->emitted_symbols
->alloc
);
698 f
= &o
->emitted_symbols
->buf
[o
->emitted_symbols
->nr
++];
700 memcpy(f
, e
, sizeof(struct emitted_diff_symbol
));
701 f
->line
= e
->line
? xmemdupz(e
->line
, e
->len
) : NULL
;
705 struct hashmap_entry ent
;
706 const struct emitted_diff_symbol
*es
;
707 struct moved_entry
*next_line
;
710 static int next_byte(const char **cp
, const char **endp
,
711 const struct diff_options
*diffopt
)
719 if (DIFF_XDL_TST(diffopt
, IGNORE_WHITESPACE_CHANGE
)) {
720 while (*cp
< *endp
&& isspace(**cp
))
723 * After skipping a couple of whitespaces,
724 * we still have to account for one space.
729 if (DIFF_XDL_TST(diffopt
, IGNORE_WHITESPACE
)) {
730 while (*cp
< *endp
&& isspace(**cp
))
732 /* return the first non-ws character via the usual below */
736 retval
= (unsigned char)(**cp
);
741 static int moved_entry_cmp(const struct diff_options
*diffopt
,
742 const struct moved_entry
*a
,
743 const struct moved_entry
*b
,
746 const char *ap
= a
->es
->line
, *ae
= a
->es
->line
+ a
->es
->len
;
747 const char *bp
= b
->es
->line
, *be
= b
->es
->line
+ b
->es
->len
;
749 if (!(diffopt
->xdl_opts
& XDF_WHITESPACE_FLAGS
))
750 return a
->es
->len
!= b
->es
->len
|| memcmp(ap
, bp
, a
->es
->len
);
752 if (DIFF_XDL_TST(diffopt
, IGNORE_WHITESPACE_AT_EOL
)) {
753 while (ae
> ap
&& isspace(*ae
))
755 while (be
> bp
&& isspace(*be
))
761 ca
= next_byte(&ap
, &ae
, diffopt
);
762 cb
= next_byte(&bp
, &be
, diffopt
);
770 static unsigned get_string_hash(struct emitted_diff_symbol
*es
, struct diff_options
*o
)
772 if (o
->xdl_opts
& XDF_WHITESPACE_FLAGS
) {
773 static struct strbuf sb
= STRBUF_INIT
;
774 const char *ap
= es
->line
, *ae
= es
->line
+ es
->len
;
778 while (ae
> ap
&& isspace(*ae
))
780 while ((c
= next_byte(&ap
, &ae
, o
)) > 0)
781 strbuf_addch(&sb
, c
);
783 return memhash(sb
.buf
, sb
.len
);
785 return memhash(es
->line
, es
->len
);
789 static struct moved_entry
*prepare_entry(struct diff_options
*o
,
792 struct moved_entry
*ret
= xmalloc(sizeof(*ret
));
793 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[line_no
];
795 ret
->ent
.hash
= get_string_hash(l
, o
);
797 ret
->next_line
= NULL
;
802 static void add_lines_to_move_detection(struct diff_options
*o
,
803 struct hashmap
*add_lines
,
804 struct hashmap
*del_lines
)
806 struct moved_entry
*prev_line
= NULL
;
809 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
811 struct moved_entry
*key
;
813 switch (o
->emitted_symbols
->buf
[n
].s
) {
814 case DIFF_SYMBOL_PLUS
:
817 case DIFF_SYMBOL_MINUS
:
825 key
= prepare_entry(o
, n
);
826 if (prev_line
&& prev_line
->es
->s
== o
->emitted_symbols
->buf
[n
].s
)
827 prev_line
->next_line
= key
;
829 hashmap_add(hm
, key
);
834 static int shrink_potential_moved_blocks(struct moved_entry
**pmb
,
839 /* Shrink the set of potential block to the remaining running */
840 for (lp
= 0, rp
= pmb_nr
- 1; lp
<= rp
;) {
841 while (lp
< pmb_nr
&& pmb
[lp
])
843 /* lp points at the first NULL now */
845 while (rp
> -1 && !pmb
[rp
])
847 /* rp points at the last non-NULL */
849 if (lp
< pmb_nr
&& rp
> -1 && lp
< rp
) {
857 /* Remember the number of running sets */
862 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
864 * Otherwise, if the last block has fewer alphanumeric characters than
865 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
868 * The last block consists of the (n - block_length)'th line up to but not
869 * including the nth line.
871 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
872 * Think of a way to unify them.
874 static void adjust_last_block(struct diff_options
*o
, int n
, int block_length
)
876 int i
, alnum_count
= 0;
877 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
879 for (i
= 1; i
< block_length
+ 1; i
++) {
880 const char *c
= o
->emitted_symbols
->buf
[n
- i
].line
;
885 if (alnum_count
>= COLOR_MOVED_MIN_ALNUM_COUNT
)
889 for (i
= 1; i
< block_length
+ 1; i
++)
890 o
->emitted_symbols
->buf
[n
- i
].flags
&= ~DIFF_SYMBOL_MOVED_LINE
;
893 /* Find blocks of moved code, delegate actual coloring decision to helper */
894 static void mark_color_as_moved(struct diff_options
*o
,
895 struct hashmap
*add_lines
,
896 struct hashmap
*del_lines
)
898 struct moved_entry
**pmb
= NULL
; /* potentially moved blocks */
899 int pmb_nr
= 0, pmb_alloc
= 0;
900 int n
, flipped_block
= 1, block_length
= 0;
903 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
904 struct hashmap
*hm
= NULL
;
905 struct moved_entry
*key
;
906 struct moved_entry
*match
= NULL
;
907 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
911 case DIFF_SYMBOL_PLUS
:
913 key
= prepare_entry(o
, n
);
914 match
= hashmap_get(hm
, key
, o
);
917 case DIFF_SYMBOL_MINUS
:
919 key
= prepare_entry(o
, n
);
920 match
= hashmap_get(hm
, key
, o
);
928 adjust_last_block(o
, n
, block_length
);
934 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
936 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
939 /* Check any potential block runs, advance each or nullify */
940 for (i
= 0; i
< pmb_nr
; i
++) {
941 struct moved_entry
*p
= pmb
[i
];
942 struct moved_entry
*pnext
= (p
&& p
->next_line
) ?
944 if (pnext
&& !hm
->cmpfn(o
, pnext
, match
, NULL
)) {
945 pmb
[i
] = p
->next_line
;
951 pmb_nr
= shrink_potential_moved_blocks(pmb
, pmb_nr
);
955 * The current line is the start of a new block.
956 * Setup the set of potential blocks.
958 for (; match
; match
= hashmap_get_next(hm
, match
)) {
959 ALLOC_GROW(pmb
, pmb_nr
+ 1, pmb_alloc
);
960 pmb
[pmb_nr
++] = match
;
963 flipped_block
= (flipped_block
+ 1) % 2;
965 adjust_last_block(o
, n
, block_length
);
972 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_ALT
;
974 adjust_last_block(o
, n
, block_length
);
979 #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
980 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
981 static void dim_moved_lines(struct diff_options
*o
)
984 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
985 struct emitted_diff_symbol
*prev
= (n
!= 0) ?
986 &o
->emitted_symbols
->buf
[n
- 1] : NULL
;
987 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
988 struct emitted_diff_symbol
*next
=
989 (n
< o
->emitted_symbols
->nr
- 1) ?
990 &o
->emitted_symbols
->buf
[n
+ 1] : NULL
;
992 /* Not a plus or minus line? */
993 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
)
996 /* Not a moved line? */
997 if (!(l
->flags
& DIFF_SYMBOL_MOVED_LINE
))
1001 * If prev or next are not a plus or minus line,
1002 * pretend they don't exist
1004 if (prev
&& prev
->s
!= DIFF_SYMBOL_PLUS
&&
1005 prev
->s
!= DIFF_SYMBOL_MINUS
)
1007 if (next
&& next
->s
!= DIFF_SYMBOL_PLUS
&&
1008 next
->s
!= DIFF_SYMBOL_MINUS
)
1011 /* Inside a block? */
1013 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1014 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
)) &&
1016 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1017 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
))) {
1018 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1022 /* Check if we are at an interesting bound: */
1023 if (prev
&& (prev
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1024 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1025 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1027 if (next
&& (next
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1028 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1029 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1033 * The boundary to prev and next are not interesting,
1034 * so this line is not interesting as a whole
1036 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1040 static void emit_line_ws_markup(struct diff_options
*o
,
1041 const char *set
, const char *reset
,
1042 const char *line
, int len
, char sign
,
1043 unsigned ws_rule
, int blank_at_eof
)
1045 const char *ws
= NULL
;
1047 if (o
->ws_error_highlight
& ws_rule
) {
1048 ws
= diff_get_color_opt(o
, DIFF_WHITESPACE
);
1054 emit_line_0(o
, set
, reset
, sign
, line
, len
);
1055 else if (blank_at_eof
)
1056 /* Blank line at EOF - paint '+' as well */
1057 emit_line_0(o
, ws
, reset
, sign
, line
, len
);
1059 /* Emit just the prefix, then the rest. */
1060 emit_line_0(o
, set
, reset
, sign
, "", 0);
1061 ws_check_emit(line
, len
, ws_rule
,
1062 o
->file
, set
, reset
, ws
);
1066 static void emit_diff_symbol_from_struct(struct diff_options
*o
,
1067 struct emitted_diff_symbol
*eds
)
1069 static const char *nneof
= " No newline at end of file\n";
1070 const char *context
, *reset
, *set
, *meta
, *fraginfo
;
1071 struct strbuf sb
= STRBUF_INIT
;
1073 enum diff_symbol s
= eds
->s
;
1074 const char *line
= eds
->line
;
1076 unsigned flags
= eds
->flags
;
1079 case DIFF_SYMBOL_NO_LF_EOF
:
1080 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1081 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1082 putc('\n', o
->file
);
1083 emit_line_0(o
, context
, reset
, '\\',
1084 nneof
, strlen(nneof
));
1086 case DIFF_SYMBOL_SUBMODULE_HEADER
:
1087 case DIFF_SYMBOL_SUBMODULE_ERROR
:
1088 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
:
1089 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
:
1090 case DIFF_SYMBOL_SUMMARY
:
1091 case DIFF_SYMBOL_STATS_LINE
:
1092 case DIFF_SYMBOL_BINARY_DIFF_BODY
:
1093 case DIFF_SYMBOL_CONTEXT_FRAGINFO
:
1094 emit_line(o
, "", "", line
, len
);
1096 case DIFF_SYMBOL_CONTEXT_INCOMPLETE
:
1097 case DIFF_SYMBOL_CONTEXT_MARKER
:
1098 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1099 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1100 emit_line(o
, context
, reset
, line
, len
);
1102 case DIFF_SYMBOL_SEPARATOR
:
1103 fprintf(o
->file
, "%s%c",
1104 diff_line_prefix(o
),
1105 o
->line_termination
);
1107 case DIFF_SYMBOL_CONTEXT
:
1108 set
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1109 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1110 emit_line_ws_markup(o
, set
, reset
, line
, len
, ' ',
1111 flags
& (DIFF_SYMBOL_CONTENT_WS_MASK
), 0);
1113 case DIFF_SYMBOL_PLUS
:
1114 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1115 DIFF_SYMBOL_MOVED_LINE_ALT
|
1116 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1117 case DIFF_SYMBOL_MOVED_LINE
|
1118 DIFF_SYMBOL_MOVED_LINE_ALT
|
1119 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1120 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT_DIM
);
1122 case DIFF_SYMBOL_MOVED_LINE
|
1123 DIFF_SYMBOL_MOVED_LINE_ALT
:
1124 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT
);
1126 case DIFF_SYMBOL_MOVED_LINE
|
1127 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1128 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_DIM
);
1130 case DIFF_SYMBOL_MOVED_LINE
:
1131 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED
);
1134 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1136 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1137 emit_line_ws_markup(o
, set
, reset
, line
, len
, '+',
1138 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
,
1139 flags
& DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
);
1141 case DIFF_SYMBOL_MINUS
:
1142 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1143 DIFF_SYMBOL_MOVED_LINE_ALT
|
1144 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1145 case DIFF_SYMBOL_MOVED_LINE
|
1146 DIFF_SYMBOL_MOVED_LINE_ALT
|
1147 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1148 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT_DIM
);
1150 case DIFF_SYMBOL_MOVED_LINE
|
1151 DIFF_SYMBOL_MOVED_LINE_ALT
:
1152 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT
);
1154 case DIFF_SYMBOL_MOVED_LINE
|
1155 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1156 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_DIM
);
1158 case DIFF_SYMBOL_MOVED_LINE
:
1159 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED
);
1162 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1164 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1165 emit_line_ws_markup(o
, set
, reset
, line
, len
, '-',
1166 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
, 0);
1168 case DIFF_SYMBOL_WORDS_PORCELAIN
:
1169 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1170 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1171 emit_line(o
, context
, reset
, line
, len
);
1172 fputs("~\n", o
->file
);
1174 case DIFF_SYMBOL_WORDS
:
1175 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1176 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1178 * Skip the prefix character, if any. With
1179 * diff_suppress_blank_empty, there may be
1182 if (line
[0] != '\n') {
1186 emit_line(o
, context
, reset
, line
, len
);
1188 case DIFF_SYMBOL_FILEPAIR_PLUS
:
1189 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1190 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1191 fprintf(o
->file
, "%s%s+++ %s%s%s\n", diff_line_prefix(o
), meta
,
1193 strchr(line
, ' ') ? "\t" : "");
1195 case DIFF_SYMBOL_FILEPAIR_MINUS
:
1196 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1197 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1198 fprintf(o
->file
, "%s%s--- %s%s%s\n", diff_line_prefix(o
), meta
,
1200 strchr(line
, ' ') ? "\t" : "");
1202 case DIFF_SYMBOL_BINARY_FILES
:
1203 case DIFF_SYMBOL_HEADER
:
1204 fprintf(o
->file
, "%s", line
);
1206 case DIFF_SYMBOL_BINARY_DIFF_HEADER
:
1207 fprintf(o
->file
, "%sGIT binary patch\n", diff_line_prefix(o
));
1209 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
:
1210 fprintf(o
->file
, "%sdelta %s\n", diff_line_prefix(o
), line
);
1212 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
:
1213 fprintf(o
->file
, "%sliteral %s\n", diff_line_prefix(o
), line
);
1215 case DIFF_SYMBOL_BINARY_DIFF_FOOTER
:
1216 fputs(diff_line_prefix(o
), o
->file
);
1217 fputc('\n', o
->file
);
1219 case DIFF_SYMBOL_REWRITE_DIFF
:
1220 fraginfo
= diff_get_color(o
->use_color
, DIFF_FRAGINFO
);
1221 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1222 emit_line(o
, fraginfo
, reset
, line
, len
);
1224 case DIFF_SYMBOL_SUBMODULE_ADD
:
1225 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1226 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1227 emit_line(o
, set
, reset
, line
, len
);
1229 case DIFF_SYMBOL_SUBMODULE_DEL
:
1230 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1231 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1232 emit_line(o
, set
, reset
, line
, len
);
1234 case DIFF_SYMBOL_SUBMODULE_UNTRACKED
:
1235 fprintf(o
->file
, "%sSubmodule %s contains untracked content\n",
1236 diff_line_prefix(o
), line
);
1238 case DIFF_SYMBOL_SUBMODULE_MODIFIED
:
1239 fprintf(o
->file
, "%sSubmodule %s contains modified content\n",
1240 diff_line_prefix(o
), line
);
1242 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
:
1243 emit_line(o
, "", "", " 0 files changed\n",
1244 strlen(" 0 files changed\n"));
1246 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV
:
1247 emit_line(o
, "", "", " ...\n", strlen(" ...\n"));
1249 case DIFF_SYMBOL_WORD_DIFF
:
1250 fprintf(o
->file
, "%.*s", len
, line
);
1252 case DIFF_SYMBOL_STAT_SEP
:
1253 fputs(o
->stat_sep
, o
->file
);
1256 die("BUG: unknown diff symbol");
1258 strbuf_release(&sb
);
1261 static void emit_diff_symbol(struct diff_options
*o
, enum diff_symbol s
,
1262 const char *line
, int len
, unsigned flags
)
1264 struct emitted_diff_symbol e
= {line
, len
, flags
, s
};
1266 if (o
->emitted_symbols
)
1267 append_emitted_diff_symbol(o
, &e
);
1269 emit_diff_symbol_from_struct(o
, &e
);
1272 void diff_emit_submodule_del(struct diff_options
*o
, const char *line
)
1274 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_DEL
, line
, strlen(line
), 0);
1277 void diff_emit_submodule_add(struct diff_options
*o
, const char *line
)
1279 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ADD
, line
, strlen(line
), 0);
1282 void diff_emit_submodule_untracked(struct diff_options
*o
, const char *path
)
1284 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
1285 path
, strlen(path
), 0);
1288 void diff_emit_submodule_modified(struct diff_options
*o
, const char *path
)
1290 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_MODIFIED
,
1291 path
, strlen(path
), 0);
1294 void diff_emit_submodule_header(struct diff_options
*o
, const char *header
)
1296 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_HEADER
,
1297 header
, strlen(header
), 0);
1300 void diff_emit_submodule_error(struct diff_options
*o
, const char *err
)
1302 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ERROR
, err
, strlen(err
), 0);
1305 void diff_emit_submodule_pipethrough(struct diff_options
*o
,
1306 const char *line
, int len
)
1308 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
, line
, len
, 0);
1311 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
1313 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
1314 ecbdata
->blank_at_eof_in_preimage
&&
1315 ecbdata
->blank_at_eof_in_postimage
&&
1316 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
1317 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
1319 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
1322 static void emit_add_line(const char *reset
,
1323 struct emit_callback
*ecbdata
,
1324 const char *line
, int len
)
1326 unsigned flags
= WSEH_NEW
| ecbdata
->ws_rule
;
1327 if (new_blank_line_at_eof(ecbdata
, line
, len
))
1328 flags
|= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
;
1330 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_PLUS
, line
, len
, flags
);
1333 static void emit_del_line(const char *reset
,
1334 struct emit_callback
*ecbdata
,
1335 const char *line
, int len
)
1337 unsigned flags
= WSEH_OLD
| ecbdata
->ws_rule
;
1338 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_MINUS
, line
, len
, flags
);
1341 static void emit_context_line(const char *reset
,
1342 struct emit_callback
*ecbdata
,
1343 const char *line
, int len
)
1345 unsigned flags
= WSEH_CONTEXT
| ecbdata
->ws_rule
;
1346 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_CONTEXT
, line
, len
, flags
);
1349 static void emit_hunk_header(struct emit_callback
*ecbdata
,
1350 const char *line
, int len
)
1352 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
1353 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
1354 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
1355 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1356 static const char atat
[2] = { '@', '@' };
1357 const char *cp
, *ep
;
1358 struct strbuf msgbuf
= STRBUF_INIT
;
1363 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1364 * it always is at least 10 bytes long.
1367 memcmp(line
, atat
, 2) ||
1368 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
1369 emit_diff_symbol(ecbdata
->opt
,
1370 DIFF_SYMBOL_CONTEXT_MARKER
, line
, len
, 0);
1373 ep
+= 2; /* skip over @@ */
1375 /* The hunk header in fraginfo color */
1376 strbuf_addstr(&msgbuf
, frag
);
1377 strbuf_add(&msgbuf
, line
, ep
- line
);
1378 strbuf_addstr(&msgbuf
, reset
);
1384 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
1387 /* blank before the func header */
1388 for (cp
= ep
; ep
- line
< len
; ep
++)
1389 if (*ep
!= ' ' && *ep
!= '\t')
1392 strbuf_addstr(&msgbuf
, context
);
1393 strbuf_add(&msgbuf
, cp
, ep
- cp
);
1394 strbuf_addstr(&msgbuf
, reset
);
1397 if (ep
< line
+ len
) {
1398 strbuf_addstr(&msgbuf
, func
);
1399 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
1400 strbuf_addstr(&msgbuf
, reset
);
1403 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
1404 strbuf_complete_line(&msgbuf
);
1405 emit_diff_symbol(ecbdata
->opt
,
1406 DIFF_SYMBOL_CONTEXT_FRAGINFO
, msgbuf
.buf
, msgbuf
.len
, 0);
1407 strbuf_release(&msgbuf
);
1410 static struct diff_tempfile
*claim_diff_tempfile(void) {
1412 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
1413 if (!diff_temp
[i
].name
)
1414 return diff_temp
+ i
;
1415 die("BUG: diff is failing to clean up its tempfiles");
1418 static void remove_tempfile(void)
1421 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
1422 if (is_tempfile_active(diff_temp
[i
].tempfile
))
1423 delete_tempfile(&diff_temp
[i
].tempfile
);
1424 diff_temp
[i
].name
= NULL
;
1428 static void add_line_count(struct strbuf
*out
, int count
)
1432 strbuf_addstr(out
, "0,0");
1435 strbuf_addstr(out
, "1");
1438 strbuf_addf(out
, "1,%d", count
);
1443 static void emit_rewrite_lines(struct emit_callback
*ecb
,
1444 int prefix
, const char *data
, int size
)
1446 const char *endp
= NULL
;
1447 const char *reset
= diff_get_color(ecb
->color_diff
, DIFF_RESET
);
1452 endp
= memchr(data
, '\n', size
);
1453 len
= endp
? (endp
- data
+ 1) : size
;
1454 if (prefix
!= '+') {
1455 ecb
->lno_in_preimage
++;
1456 emit_del_line(reset
, ecb
, data
, len
);
1458 ecb
->lno_in_postimage
++;
1459 emit_add_line(reset
, ecb
, data
, len
);
1465 emit_diff_symbol(ecb
->opt
, DIFF_SYMBOL_NO_LF_EOF
, NULL
, 0, 0);
1468 static void emit_rewrite_diff(const char *name_a
,
1470 struct diff_filespec
*one
,
1471 struct diff_filespec
*two
,
1472 struct userdiff_driver
*textconv_one
,
1473 struct userdiff_driver
*textconv_two
,
1474 struct diff_options
*o
)
1477 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
1478 const char *a_prefix
, *b_prefix
;
1479 char *data_one
, *data_two
;
1480 size_t size_one
, size_two
;
1481 struct emit_callback ecbdata
;
1482 struct strbuf out
= STRBUF_INIT
;
1484 if (diff_mnemonic_prefix
&& DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
1485 a_prefix
= o
->b_prefix
;
1486 b_prefix
= o
->a_prefix
;
1488 a_prefix
= o
->a_prefix
;
1489 b_prefix
= o
->b_prefix
;
1492 name_a
+= (*name_a
== '/');
1493 name_b
+= (*name_b
== '/');
1495 strbuf_reset(&a_name
);
1496 strbuf_reset(&b_name
);
1497 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
1498 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
1500 size_one
= fill_textconv(textconv_one
, one
, &data_one
);
1501 size_two
= fill_textconv(textconv_two
, two
, &data_two
);
1503 memset(&ecbdata
, 0, sizeof(ecbdata
));
1504 ecbdata
.color_diff
= want_color(o
->use_color
);
1505 ecbdata
.ws_rule
= whitespace_rule(name_b
);
1507 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
1509 mf1
.ptr
= (char *)data_one
;
1510 mf2
.ptr
= (char *)data_two
;
1511 mf1
.size
= size_one
;
1512 mf2
.size
= size_two
;
1513 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1515 ecbdata
.lno_in_preimage
= 1;
1516 ecbdata
.lno_in_postimage
= 1;
1518 lc_a
= count_lines(data_one
, size_one
);
1519 lc_b
= count_lines(data_two
, size_two
);
1521 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
1522 a_name
.buf
, a_name
.len
, 0);
1523 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
1524 b_name
.buf
, b_name
.len
, 0);
1526 strbuf_addstr(&out
, "@@ -");
1527 if (!o
->irreversible_delete
)
1528 add_line_count(&out
, lc_a
);
1530 strbuf_addstr(&out
, "?,?");
1531 strbuf_addstr(&out
, " +");
1532 add_line_count(&out
, lc_b
);
1533 strbuf_addstr(&out
, " @@\n");
1534 emit_diff_symbol(o
, DIFF_SYMBOL_REWRITE_DIFF
, out
.buf
, out
.len
, 0);
1535 strbuf_release(&out
);
1537 if (lc_a
&& !o
->irreversible_delete
)
1538 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
1540 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
1542 free((char *)data_one
);
1544 free((char *)data_two
);
1547 struct diff_words_buffer
{
1549 unsigned long alloc
;
1550 struct diff_words_orig
{
1551 const char *begin
, *end
;
1553 int orig_nr
, orig_alloc
;
1556 static void diff_words_append(char *line
, unsigned long len
,
1557 struct diff_words_buffer
*buffer
)
1559 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
1562 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
1563 buffer
->text
.size
+= len
;
1564 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
1567 struct diff_words_style_elem
{
1570 const char *color
; /* NULL; filled in by the setup code if
1571 * color is enabled */
1574 struct diff_words_style
{
1575 enum diff_words_type type
;
1576 struct diff_words_style_elem
new, old
, ctx
;
1577 const char *newline
;
1580 static struct diff_words_style diff_words_styles
[] = {
1581 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1582 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1583 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
1586 struct diff_words_data
{
1587 struct diff_words_buffer minus
, plus
;
1588 const char *current_plus
;
1590 struct diff_options
*opt
;
1591 regex_t
*word_regex
;
1592 enum diff_words_type type
;
1593 struct diff_words_style
*style
;
1596 static int fn_out_diff_words_write_helper(struct diff_options
*o
,
1597 struct diff_words_style_elem
*st_el
,
1598 const char *newline
,
1599 size_t count
, const char *buf
)
1602 struct strbuf sb
= STRBUF_INIT
;
1605 char *p
= memchr(buf
, '\n', count
);
1607 strbuf_addstr(&sb
, diff_line_prefix(o
));
1610 const char *reset
= st_el
->color
&& *st_el
->color
?
1611 GIT_COLOR_RESET
: NULL
;
1612 if (st_el
->color
&& *st_el
->color
)
1613 strbuf_addstr(&sb
, st_el
->color
);
1614 strbuf_addstr(&sb
, st_el
->prefix
);
1615 strbuf_add(&sb
, buf
, p
? p
- buf
: count
);
1616 strbuf_addstr(&sb
, st_el
->suffix
);
1618 strbuf_addstr(&sb
, reset
);
1623 strbuf_addstr(&sb
, newline
);
1624 count
-= p
+ 1 - buf
;
1628 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1636 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1638 strbuf_release(&sb
);
1643 * '--color-words' algorithm can be described as:
1645 * 1. collect the minus/plus lines of a diff hunk, divided into
1646 * minus-lines and plus-lines;
1648 * 2. break both minus-lines and plus-lines into words and
1649 * place them into two mmfile_t with one word for each line;
1651 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1653 * And for the common parts of the both file, we output the plus side text.
1654 * diff_words->current_plus is used to trace the current position of the plus file
1655 * which printed. diff_words->last_minus is used to trace the last minus word
1658 * For '--graph' to work with '--color-words', we need to output the graph prefix
1659 * on each line of color words output. Generally, there are two conditions on
1660 * which we should output the prefix.
1662 * 1. diff_words->last_minus == 0 &&
1663 * diff_words->current_plus == diff_words->plus.text.ptr
1665 * that is: the plus text must start as a new line, and if there is no minus
1666 * word printed, a graph prefix must be printed.
1668 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1669 * *(diff_words->current_plus - 1) == '\n'
1671 * that is: a graph prefix must be printed following a '\n'
1673 static int color_words_output_graph_prefix(struct diff_words_data
*diff_words
)
1675 if ((diff_words
->last_minus
== 0 &&
1676 diff_words
->current_plus
== diff_words
->plus
.text
.ptr
) ||
1677 (diff_words
->current_plus
> diff_words
->plus
.text
.ptr
&&
1678 *(diff_words
->current_plus
- 1) == '\n')) {
1685 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
1687 struct diff_words_data
*diff_words
= priv
;
1688 struct diff_words_style
*style
= diff_words
->style
;
1689 int minus_first
, minus_len
, plus_first
, plus_len
;
1690 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
1691 struct diff_options
*opt
= diff_words
->opt
;
1692 const char *line_prefix
;
1694 if (line
[0] != '@' || parse_hunk_header(line
, len
,
1695 &minus_first
, &minus_len
, &plus_first
, &plus_len
))
1699 line_prefix
= diff_line_prefix(opt
);
1701 /* POSIX requires that first be decremented by one if len == 0... */
1703 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
1705 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
1707 minus_begin
= minus_end
=
1708 diff_words
->minus
.orig
[minus_first
].end
;
1711 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
1712 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
1714 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
1716 if (color_words_output_graph_prefix(diff_words
)) {
1717 fputs(line_prefix
, diff_words
->opt
->file
);
1719 if (diff_words
->current_plus
!= plus_begin
) {
1720 fn_out_diff_words_write_helper(diff_words
->opt
,
1721 &style
->ctx
, style
->newline
,
1722 plus_begin
- diff_words
->current_plus
,
1723 diff_words
->current_plus
);
1725 if (minus_begin
!= minus_end
) {
1726 fn_out_diff_words_write_helper(diff_words
->opt
,
1727 &style
->old
, style
->newline
,
1728 minus_end
- minus_begin
, minus_begin
);
1730 if (plus_begin
!= plus_end
) {
1731 fn_out_diff_words_write_helper(diff_words
->opt
,
1732 &style
->new, style
->newline
,
1733 plus_end
- plus_begin
, plus_begin
);
1736 diff_words
->current_plus
= plus_end
;
1737 diff_words
->last_minus
= minus_first
;
1740 /* This function starts looking at *begin, and returns 0 iff a word was found. */
1741 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
1742 int *begin
, int *end
)
1744 if (word_regex
&& *begin
< buffer
->size
) {
1745 regmatch_t match
[1];
1746 if (!regexec_buf(word_regex
, buffer
->ptr
+ *begin
,
1747 buffer
->size
- *begin
, 1, match
, 0)) {
1748 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
1749 '\n', match
[0].rm_eo
- match
[0].rm_so
);
1750 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
1751 *begin
+= match
[0].rm_so
;
1752 return *begin
>= *end
;
1757 /* find the next word */
1758 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
1760 if (*begin
>= buffer
->size
)
1763 /* find the end of the word */
1765 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
1772 * This function splits the words in buffer->text, stores the list with
1773 * newline separator into out, and saves the offsets of the original words
1776 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
1777 regex_t
*word_regex
)
1785 /* fake an empty "0th" word */
1786 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
1787 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
1788 buffer
->orig_nr
= 1;
1790 for (i
= 0; i
< buffer
->text
.size
; i
++) {
1791 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
1794 /* store original boundaries */
1795 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
1796 buffer
->orig_alloc
);
1797 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
1798 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
1801 /* store one word */
1802 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
1803 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
1804 out
->ptr
[out
->size
+ j
- i
] = '\n';
1805 out
->size
+= j
- i
+ 1;
1811 /* this executes the word diff on the accumulated buffers */
1812 static void diff_words_show(struct diff_words_data
*diff_words
)
1816 mmfile_t minus
, plus
;
1817 struct diff_words_style
*style
= diff_words
->style
;
1819 struct diff_options
*opt
= diff_words
->opt
;
1820 const char *line_prefix
;
1823 line_prefix
= diff_line_prefix(opt
);
1825 /* special case: only removal */
1826 if (!diff_words
->plus
.text
.size
) {
1827 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
1828 line_prefix
, strlen(line_prefix
), 0);
1829 fn_out_diff_words_write_helper(diff_words
->opt
,
1830 &style
->old
, style
->newline
,
1831 diff_words
->minus
.text
.size
,
1832 diff_words
->minus
.text
.ptr
);
1833 diff_words
->minus
.text
.size
= 0;
1837 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
1838 diff_words
->last_minus
= 0;
1840 memset(&xpp
, 0, sizeof(xpp
));
1841 memset(&xecfg
, 0, sizeof(xecfg
));
1842 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
1843 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
1845 /* as only the hunk header will be parsed, we need a 0-context */
1847 if (xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
1849 die("unable to generate word diff");
1852 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
1853 diff_words
->plus
.text
.size
) {
1854 if (color_words_output_graph_prefix(diff_words
))
1855 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
1856 line_prefix
, strlen(line_prefix
), 0);
1857 fn_out_diff_words_write_helper(diff_words
->opt
,
1858 &style
->ctx
, style
->newline
,
1859 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
1860 - diff_words
->current_plus
, diff_words
->current_plus
);
1862 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
1865 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
1866 static void diff_words_flush(struct emit_callback
*ecbdata
)
1868 struct diff_options
*wo
= ecbdata
->diff_words
->opt
;
1870 if (ecbdata
->diff_words
->minus
.text
.size
||
1871 ecbdata
->diff_words
->plus
.text
.size
)
1872 diff_words_show(ecbdata
->diff_words
);
1874 if (wo
->emitted_symbols
) {
1875 struct diff_options
*o
= ecbdata
->opt
;
1876 struct emitted_diff_symbols
*wol
= wo
->emitted_symbols
;
1881 * Instead of appending each, concat all words to a line?
1883 for (i
= 0; i
< wol
->nr
; i
++)
1884 append_emitted_diff_symbol(o
, &wol
->buf
[i
]);
1886 for (i
= 0; i
< wol
->nr
; i
++)
1887 free((void *)wol
->buf
[i
].line
);
1893 static void diff_filespec_load_driver(struct diff_filespec
*one
)
1895 /* Use already-loaded driver */
1899 if (S_ISREG(one
->mode
))
1900 one
->driver
= userdiff_find_by_path(one
->path
);
1902 /* Fallback to default settings */
1904 one
->driver
= userdiff_find_by_name("default");
1907 static const char *userdiff_word_regex(struct diff_filespec
*one
)
1909 diff_filespec_load_driver(one
);
1910 return one
->driver
->word_regex
;
1913 static void init_diff_words_data(struct emit_callback
*ecbdata
,
1914 struct diff_options
*orig_opts
,
1915 struct diff_filespec
*one
,
1916 struct diff_filespec
*two
)
1919 struct diff_options
*o
= xmalloc(sizeof(struct diff_options
));
1920 memcpy(o
, orig_opts
, sizeof(struct diff_options
));
1922 ecbdata
->diff_words
=
1923 xcalloc(1, sizeof(struct diff_words_data
));
1924 ecbdata
->diff_words
->type
= o
->word_diff
;
1925 ecbdata
->diff_words
->opt
= o
;
1927 if (orig_opts
->emitted_symbols
)
1928 o
->emitted_symbols
=
1929 xcalloc(1, sizeof(struct emitted_diff_symbols
));
1932 o
->word_regex
= userdiff_word_regex(one
);
1934 o
->word_regex
= userdiff_word_regex(two
);
1936 o
->word_regex
= diff_word_regex_cfg
;
1937 if (o
->word_regex
) {
1938 ecbdata
->diff_words
->word_regex
= (regex_t
*)
1939 xmalloc(sizeof(regex_t
));
1940 if (regcomp(ecbdata
->diff_words
->word_regex
,
1942 REG_EXTENDED
| REG_NEWLINE
))
1943 die ("Invalid regular expression: %s",
1946 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
1947 if (o
->word_diff
== diff_words_styles
[i
].type
) {
1948 ecbdata
->diff_words
->style
=
1949 &diff_words_styles
[i
];
1953 if (want_color(o
->use_color
)) {
1954 struct diff_words_style
*st
= ecbdata
->diff_words
->style
;
1955 st
->old
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1956 st
->new.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1957 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1961 static void free_diff_words_data(struct emit_callback
*ecbdata
)
1963 if (ecbdata
->diff_words
) {
1964 diff_words_flush(ecbdata
);
1965 free (ecbdata
->diff_words
->opt
->emitted_symbols
);
1966 free (ecbdata
->diff_words
->opt
);
1967 free (ecbdata
->diff_words
->minus
.text
.ptr
);
1968 free (ecbdata
->diff_words
->minus
.orig
);
1969 free (ecbdata
->diff_words
->plus
.text
.ptr
);
1970 free (ecbdata
->diff_words
->plus
.orig
);
1971 if (ecbdata
->diff_words
->word_regex
) {
1972 regfree(ecbdata
->diff_words
->word_regex
);
1973 free(ecbdata
->diff_words
->word_regex
);
1975 FREE_AND_NULL(ecbdata
->diff_words
);
1979 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
1981 if (want_color(diff_use_color
))
1982 return diff_colors
[ix
];
1986 const char *diff_line_prefix(struct diff_options
*opt
)
1988 struct strbuf
*msgbuf
;
1989 if (!opt
->output_prefix
)
1992 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
1996 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
1999 unsigned long allot
;
2005 (void) utf8_width(&cp
, &l
);
2007 break; /* truncated in the middle? */
2012 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
2015 ecbdata
->lno_in_preimage
= 0;
2016 ecbdata
->lno_in_postimage
= 0;
2017 p
= strchr(line
, '-');
2019 return; /* cannot happen */
2020 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
2023 return; /* cannot happen */
2024 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
2027 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
2029 struct emit_callback
*ecbdata
= priv
;
2030 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
2031 struct diff_options
*o
= ecbdata
->opt
;
2033 o
->found_changes
= 1;
2035 if (ecbdata
->header
) {
2036 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
2037 ecbdata
->header
->buf
, ecbdata
->header
->len
, 0);
2038 strbuf_reset(ecbdata
->header
);
2039 ecbdata
->header
= NULL
;
2042 if (ecbdata
->label_path
[0]) {
2043 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
2044 ecbdata
->label_path
[0],
2045 strlen(ecbdata
->label_path
[0]), 0);
2046 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
2047 ecbdata
->label_path
[1],
2048 strlen(ecbdata
->label_path
[1]), 0);
2049 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
2052 if (diff_suppress_blank_empty
2053 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
2058 if (line
[0] == '@') {
2059 if (ecbdata
->diff_words
)
2060 diff_words_flush(ecbdata
);
2061 len
= sane_truncate_line(ecbdata
, line
, len
);
2062 find_lno(line
, ecbdata
);
2063 emit_hunk_header(ecbdata
, line
, len
);
2067 if (ecbdata
->diff_words
) {
2068 enum diff_symbol s
=
2069 ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
?
2070 DIFF_SYMBOL_WORDS_PORCELAIN
: DIFF_SYMBOL_WORDS
;
2071 if (line
[0] == '-') {
2072 diff_words_append(line
, len
,
2073 &ecbdata
->diff_words
->minus
);
2075 } else if (line
[0] == '+') {
2076 diff_words_append(line
, len
,
2077 &ecbdata
->diff_words
->plus
);
2079 } else if (starts_with(line
, "\\ ")) {
2081 * Eat the "no newline at eof" marker as if we
2082 * saw a "+" or "-" line with nothing on it,
2083 * and return without diff_words_flush() to
2084 * defer processing. If this is the end of
2085 * preimage, more "+" lines may come after it.
2089 diff_words_flush(ecbdata
);
2090 emit_diff_symbol(o
, s
, line
, len
, 0);
2096 ecbdata
->lno_in_postimage
++;
2097 emit_add_line(reset
, ecbdata
, line
+ 1, len
- 1);
2100 ecbdata
->lno_in_preimage
++;
2101 emit_del_line(reset
, ecbdata
, line
+ 1, len
- 1);
2104 ecbdata
->lno_in_postimage
++;
2105 ecbdata
->lno_in_preimage
++;
2106 emit_context_line(reset
, ecbdata
, line
+ 1, len
- 1);
2109 /* incomplete line at the end */
2110 ecbdata
->lno_in_preimage
++;
2111 emit_diff_symbol(o
, DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
2117 static char *pprint_rename(const char *a
, const char *b
)
2119 const char *old
= a
;
2120 const char *new = b
;
2121 struct strbuf name
= STRBUF_INIT
;
2122 int pfx_length
, sfx_length
;
2123 int pfx_adjust_for_slash
;
2124 int len_a
= strlen(a
);
2125 int len_b
= strlen(b
);
2126 int a_midlen
, b_midlen
;
2127 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
2128 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
2130 if (qlen_a
|| qlen_b
) {
2131 quote_c_style(a
, &name
, NULL
, 0);
2132 strbuf_addstr(&name
, " => ");
2133 quote_c_style(b
, &name
, NULL
, 0);
2134 return strbuf_detach(&name
, NULL
);
2137 /* Find common prefix */
2139 while (*old
&& *new && *old
== *new) {
2141 pfx_length
= old
- a
+ 1;
2146 /* Find common suffix */
2151 * If there is a common prefix, it must end in a slash. In
2152 * that case we let this loop run 1 into the prefix to see the
2155 * If there is no common prefix, we cannot do this as it would
2156 * underrun the input strings.
2158 pfx_adjust_for_slash
= (pfx_length
? 1 : 0);
2159 while (a
+ pfx_length
- pfx_adjust_for_slash
<= old
&&
2160 b
+ pfx_length
- pfx_adjust_for_slash
<= new &&
2163 sfx_length
= len_a
- (old
- a
);
2169 * pfx{mid-a => mid-b}sfx
2170 * {pfx-a => pfx-b}sfx
2171 * pfx{sfx-a => sfx-b}
2174 a_midlen
= len_a
- pfx_length
- sfx_length
;
2175 b_midlen
= len_b
- pfx_length
- sfx_length
;
2181 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
2182 if (pfx_length
+ sfx_length
) {
2183 strbuf_add(&name
, a
, pfx_length
);
2184 strbuf_addch(&name
, '{');
2186 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
2187 strbuf_addstr(&name
, " => ");
2188 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
2189 if (pfx_length
+ sfx_length
) {
2190 strbuf_addch(&name
, '}');
2191 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
2193 return strbuf_detach(&name
, NULL
);
2199 struct diffstat_file
{
2203 unsigned is_unmerged
:1;
2204 unsigned is_binary
:1;
2205 unsigned is_renamed
:1;
2206 unsigned is_interesting
:1;
2207 uintmax_t added
, deleted
;
2211 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
2215 struct diffstat_file
*x
;
2216 x
= xcalloc(1, sizeof(*x
));
2217 ALLOC_GROW(diffstat
->files
, diffstat
->nr
+ 1, diffstat
->alloc
);
2218 diffstat
->files
[diffstat
->nr
++] = x
;
2220 x
->from_name
= xstrdup(name_a
);
2221 x
->name
= xstrdup(name_b
);
2225 x
->from_name
= NULL
;
2226 x
->name
= xstrdup(name_a
);
2231 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
2233 struct diffstat_t
*diffstat
= priv
;
2234 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
2238 else if (line
[0] == '-')
2242 const char mime_boundary_leader
[] = "------------";
2244 static int scale_linear(int it
, int width
, int max_change
)
2249 * make sure that at least one '-' or '+' is printed if
2250 * there is any change to this path. The easiest way is to
2251 * scale linearly as if the alloted width is one column shorter
2252 * than it is, and then add 1 to the result.
2254 return 1 + (it
* (width
- 1) / max_change
);
2257 static void show_graph(struct strbuf
*out
, char ch
, int cnt
,
2258 const char *set
, const char *reset
)
2262 strbuf_addstr(out
, set
);
2263 strbuf_addchars(out
, ch
, cnt
);
2264 strbuf_addstr(out
, reset
);
2267 static void fill_print_name(struct diffstat_file
*file
)
2271 if (file
->print_name
)
2274 if (!file
->is_renamed
) {
2275 struct strbuf buf
= STRBUF_INIT
;
2276 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
2277 pname
= strbuf_detach(&buf
, NULL
);
2280 strbuf_release(&buf
);
2283 pname
= pprint_rename(file
->from_name
, file
->name
);
2285 file
->print_name
= pname
;
2288 static void print_stat_summary_inserts_deletes(struct diff_options
*options
,
2289 int files
, int insertions
, int deletions
)
2291 struct strbuf sb
= STRBUF_INIT
;
2294 assert(insertions
== 0 && deletions
== 0);
2295 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
2301 (files
== 1) ? " %d file changed" : " %d files changed",
2305 * For binary diff, the caller may want to print "x files
2306 * changed" with insertions == 0 && deletions == 0.
2308 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2309 * is probably less confusing (i.e skip over "2 files changed
2310 * but nothing about added/removed lines? Is this a bug in Git?").
2312 if (insertions
|| deletions
== 0) {
2314 (insertions
== 1) ? ", %d insertion(+)" : ", %d insertions(+)",
2318 if (deletions
|| insertions
== 0) {
2320 (deletions
== 1) ? ", %d deletion(-)" : ", %d deletions(-)",
2323 strbuf_addch(&sb
, '\n');
2324 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
2326 strbuf_release(&sb
);
2329 void print_stat_summary(FILE *fp
, int files
,
2330 int insertions
, int deletions
)
2332 struct diff_options o
;
2333 memset(&o
, 0, sizeof(o
));
2336 print_stat_summary_inserts_deletes(&o
, files
, insertions
, deletions
);
2339 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
2341 int i
, len
, add
, del
, adds
= 0, dels
= 0;
2342 uintmax_t max_change
= 0, max_len
= 0;
2343 int total_files
= data
->nr
, count
;
2344 int width
, name_width
, graph_width
, number_width
= 0, bin_width
= 0;
2345 const char *reset
, *add_c
, *del_c
;
2346 int extra_shown
= 0;
2347 const char *line_prefix
= diff_line_prefix(options
);
2348 struct strbuf out
= STRBUF_INIT
;
2353 count
= options
->stat_count
? options
->stat_count
: data
->nr
;
2355 reset
= diff_get_color_opt(options
, DIFF_RESET
);
2356 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
2357 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
2360 * Find the longest filename and max number of changes
2362 for (i
= 0; (i
< count
) && (i
< data
->nr
); i
++) {
2363 struct diffstat_file
*file
= data
->files
[i
];
2364 uintmax_t change
= file
->added
+ file
->deleted
;
2366 if (!file
->is_interesting
&& (change
== 0)) {
2367 count
++; /* not shown == room for one more */
2370 fill_print_name(file
);
2371 len
= strlen(file
->print_name
);
2375 if (file
->is_unmerged
) {
2376 /* "Unmerged" is 8 characters */
2377 bin_width
= bin_width
< 8 ? 8 : bin_width
;
2380 if (file
->is_binary
) {
2381 /* "Bin XXX -> YYY bytes" */
2382 int w
= 14 + decimal_width(file
->added
)
2383 + decimal_width(file
->deleted
);
2384 bin_width
= bin_width
< w
? w
: bin_width
;
2385 /* Display change counts aligned with "Bin" */
2390 if (max_change
< change
)
2391 max_change
= change
;
2393 count
= i
; /* where we can stop scanning in data->files[] */
2396 * We have width = stat_width or term_columns() columns total.
2397 * We want a maximum of min(max_len, stat_name_width) for the name part.
2398 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
2399 * We also need 1 for " " and 4 + decimal_width(max_change)
2400 * for " | NNNN " and one the empty column at the end, altogether
2401 * 6 + decimal_width(max_change).
2403 * If there's not enough space, we will use the smaller of
2404 * stat_name_width (if set) and 5/8*width for the filename,
2405 * and the rest for constant elements + graph part, but no more
2406 * than stat_graph_width for the graph part.
2407 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2408 * for the standard terminal size).
2410 * In other words: stat_width limits the maximum width, and
2411 * stat_name_width fixes the maximum width of the filename,
2412 * and is also used to divide available columns if there
2415 * Binary files are displayed with "Bin XXX -> YYY bytes"
2416 * instead of the change count and graph. This part is treated
2417 * similarly to the graph part, except that it is not
2418 * "scaled". If total width is too small to accommodate the
2419 * guaranteed minimum width of the filename part and the
2420 * separators and this message, this message will "overflow"
2421 * making the line longer than the maximum width.
2424 if (options
->stat_width
== -1)
2425 width
= term_columns() - strlen(line_prefix
);
2427 width
= options
->stat_width
? options
->stat_width
: 80;
2428 number_width
= decimal_width(max_change
) > number_width
?
2429 decimal_width(max_change
) : number_width
;
2431 if (options
->stat_graph_width
== -1)
2432 options
->stat_graph_width
= diff_stat_graph_width
;
2435 * Guarantee 3/8*16==6 for the graph part
2436 * and 5/8*16==10 for the filename part
2438 if (width
< 16 + 6 + number_width
)
2439 width
= 16 + 6 + number_width
;
2442 * First assign sizes that are wanted, ignoring available width.
2443 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2444 * starting from "XXX" should fit in graph_width.
2446 graph_width
= max_change
+ 4 > bin_width
? max_change
: bin_width
- 4;
2447 if (options
->stat_graph_width
&&
2448 options
->stat_graph_width
< graph_width
)
2449 graph_width
= options
->stat_graph_width
;
2451 name_width
= (options
->stat_name_width
> 0 &&
2452 options
->stat_name_width
< max_len
) ?
2453 options
->stat_name_width
: max_len
;
2456 * Adjust adjustable widths not to exceed maximum width
2458 if (name_width
+ number_width
+ 6 + graph_width
> width
) {
2459 if (graph_width
> width
* 3/8 - number_width
- 6) {
2460 graph_width
= width
* 3/8 - number_width
- 6;
2461 if (graph_width
< 6)
2465 if (options
->stat_graph_width
&&
2466 graph_width
> options
->stat_graph_width
)
2467 graph_width
= options
->stat_graph_width
;
2468 if (name_width
> width
- number_width
- 6 - graph_width
)
2469 name_width
= width
- number_width
- 6 - graph_width
;
2471 graph_width
= width
- number_width
- 6 - name_width
;
2475 * From here name_width is the width of the name area,
2476 * and graph_width is the width of the graph area.
2477 * max_change is used to scale graph properly.
2479 for (i
= 0; i
< count
; i
++) {
2480 const char *prefix
= "";
2481 struct diffstat_file
*file
= data
->files
[i
];
2482 char *name
= file
->print_name
;
2483 uintmax_t added
= file
->added
;
2484 uintmax_t deleted
= file
->deleted
;
2487 if (!file
->is_interesting
&& (added
+ deleted
== 0))
2491 * "scale" the filename
2494 name_len
= strlen(name
);
2495 if (name_width
< name_len
) {
2499 name
+= name_len
- len
;
2500 slash
= strchr(name
, '/');
2505 if (file
->is_binary
) {
2506 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2507 strbuf_addf(&out
, " %*s", number_width
, "Bin");
2508 if (!added
&& !deleted
) {
2509 strbuf_addch(&out
, '\n');
2510 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2511 out
.buf
, out
.len
, 0);
2515 strbuf_addf(&out
, " %s%"PRIuMAX
"%s",
2516 del_c
, deleted
, reset
);
2517 strbuf_addstr(&out
, " -> ");
2518 strbuf_addf(&out
, "%s%"PRIuMAX
"%s",
2519 add_c
, added
, reset
);
2520 strbuf_addstr(&out
, " bytes\n");
2521 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2522 out
.buf
, out
.len
, 0);
2526 else if (file
->is_unmerged
) {
2527 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2528 strbuf_addstr(&out
, " Unmerged\n");
2529 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2530 out
.buf
, out
.len
, 0);
2536 * scale the add/delete
2541 if (graph_width
<= max_change
) {
2542 int total
= scale_linear(add
+ del
, graph_width
, max_change
);
2543 if (total
< 2 && add
&& del
)
2544 /* width >= 2 due to the sanity check */
2547 add
= scale_linear(add
, graph_width
, max_change
);
2550 del
= scale_linear(del
, graph_width
, max_change
);
2554 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2555 strbuf_addf(&out
, " %*"PRIuMAX
"%s",
2556 number_width
, added
+ deleted
,
2557 added
+ deleted
? " " : "");
2558 show_graph(&out
, '+', add
, add_c
, reset
);
2559 show_graph(&out
, '-', del
, del_c
, reset
);
2560 strbuf_addch(&out
, '\n');
2561 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2562 out
.buf
, out
.len
, 0);
2566 for (i
= 0; i
< data
->nr
; i
++) {
2567 struct diffstat_file
*file
= data
->files
[i
];
2568 uintmax_t added
= file
->added
;
2569 uintmax_t deleted
= file
->deleted
;
2571 if (file
->is_unmerged
||
2572 (!file
->is_interesting
&& (added
+ deleted
== 0))) {
2577 if (!file
->is_binary
) {
2584 emit_diff_symbol(options
,
2585 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
2590 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2591 strbuf_release(&out
);
2594 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
2596 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
2601 for (i
= 0; i
< data
->nr
; i
++) {
2602 int added
= data
->files
[i
]->added
;
2603 int deleted
= data
->files
[i
]->deleted
;
2605 if (data
->files
[i
]->is_unmerged
||
2606 (!data
->files
[i
]->is_interesting
&& (added
+ deleted
== 0))) {
2608 } else if (!data
->files
[i
]->is_binary
) { /* don't count bytes */
2613 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2616 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
2623 for (i
= 0; i
< data
->nr
; i
++) {
2624 struct diffstat_file
*file
= data
->files
[i
];
2626 fprintf(options
->file
, "%s", diff_line_prefix(options
));
2628 if (file
->is_binary
)
2629 fprintf(options
->file
, "-\t-\t");
2631 fprintf(options
->file
,
2632 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
2633 file
->added
, file
->deleted
);
2634 if (options
->line_termination
) {
2635 fill_print_name(file
);
2636 if (!file
->is_renamed
)
2637 write_name_quoted(file
->name
, options
->file
,
2638 options
->line_termination
);
2640 fputs(file
->print_name
, options
->file
);
2641 putc(options
->line_termination
, options
->file
);
2644 if (file
->is_renamed
) {
2645 putc('\0', options
->file
);
2646 write_name_quoted(file
->from_name
, options
->file
, '\0');
2648 write_name_quoted(file
->name
, options
->file
, '\0');
2653 struct dirstat_file
{
2655 unsigned long changed
;
2658 struct dirstat_dir
{
2659 struct dirstat_file
*files
;
2660 int alloc
, nr
, permille
, cumulative
;
2663 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
2664 unsigned long changed
, const char *base
, int baselen
)
2666 unsigned long this_dir
= 0;
2667 unsigned int sources
= 0;
2668 const char *line_prefix
= diff_line_prefix(opt
);
2671 struct dirstat_file
*f
= dir
->files
;
2672 int namelen
= strlen(f
->name
);
2676 if (namelen
< baselen
)
2678 if (memcmp(f
->name
, base
, baselen
))
2680 slash
= strchr(f
->name
+ baselen
, '/');
2682 int newbaselen
= slash
+ 1 - f
->name
;
2683 this = gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
2695 * We don't report dirstat's for
2697 * - or cases where everything came from a single directory
2698 * under this directory (sources == 1).
2700 if (baselen
&& sources
!= 1) {
2702 int permille
= this_dir
* 1000 / changed
;
2703 if (permille
>= dir
->permille
) {
2704 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
2705 permille
/ 10, permille
% 10, baselen
, base
);
2706 if (!dir
->cumulative
)
2714 static int dirstat_compare(const void *_a
, const void *_b
)
2716 const struct dirstat_file
*a
= _a
;
2717 const struct dirstat_file
*b
= _b
;
2718 return strcmp(a
->name
, b
->name
);
2721 static void show_dirstat(struct diff_options
*options
)
2724 unsigned long changed
;
2725 struct dirstat_dir dir
;
2726 struct diff_queue_struct
*q
= &diff_queued_diff
;
2731 dir
.permille
= options
->dirstat_permille
;
2732 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
2735 for (i
= 0; i
< q
->nr
; i
++) {
2736 struct diff_filepair
*p
= q
->queue
[i
];
2738 unsigned long copied
, added
, damage
;
2739 int content_changed
;
2741 name
= p
->two
->path
? p
->two
->path
: p
->one
->path
;
2743 if (p
->one
->oid_valid
&& p
->two
->oid_valid
)
2744 content_changed
= oidcmp(&p
->one
->oid
, &p
->two
->oid
);
2746 content_changed
= 1;
2748 if (!content_changed
) {
2750 * The SHA1 has not changed, so pre-/post-content is
2751 * identical. We can therefore skip looking at the
2752 * file contents altogether.
2758 if (DIFF_OPT_TST(options
, DIRSTAT_BY_FILE
)) {
2760 * In --dirstat-by-file mode, we don't really need to
2761 * look at the actual file contents at all.
2762 * The fact that the SHA1 changed is enough for us to
2763 * add this file to the list of results
2764 * (with each file contributing equal damage).
2770 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
2771 diff_populate_filespec(p
->one
, 0);
2772 diff_populate_filespec(p
->two
, 0);
2773 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
,
2775 diff_free_filespec_data(p
->one
);
2776 diff_free_filespec_data(p
->two
);
2777 } else if (DIFF_FILE_VALID(p
->one
)) {
2778 diff_populate_filespec(p
->one
, CHECK_SIZE_ONLY
);
2780 diff_free_filespec_data(p
->one
);
2781 } else if (DIFF_FILE_VALID(p
->two
)) {
2782 diff_populate_filespec(p
->two
, CHECK_SIZE_ONLY
);
2784 added
= p
->two
->size
;
2785 diff_free_filespec_data(p
->two
);
2790 * Original minus copied is the removed material,
2791 * added is the new material. They are both damages
2792 * made to the preimage.
2793 * If the resulting damage is zero, we know that
2794 * diffcore_count_changes() considers the two entries to
2795 * be identical, but since content_changed is true, we
2796 * know that there must have been _some_ kind of change,
2797 * so we force all entries to have damage > 0.
2799 damage
= (p
->one
->size
- copied
) + added
;
2804 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
2805 dir
.files
[dir
.nr
].name
= name
;
2806 dir
.files
[dir
.nr
].changed
= damage
;
2811 /* This can happen even with many files, if everything was renames */
2815 /* Show all directories with more than x% of the changes */
2816 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
2817 gather_dirstat(options
, &dir
, changed
, "", 0);
2820 static void show_dirstat_by_line(struct diffstat_t
*data
, struct diff_options
*options
)
2823 unsigned long changed
;
2824 struct dirstat_dir dir
;
2832 dir
.permille
= options
->dirstat_permille
;
2833 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
2836 for (i
= 0; i
< data
->nr
; i
++) {
2837 struct diffstat_file
*file
= data
->files
[i
];
2838 unsigned long damage
= file
->added
+ file
->deleted
;
2839 if (file
->is_binary
)
2841 * binary files counts bytes, not lines. Must find some
2842 * way to normalize binary bytes vs. textual lines.
2843 * The following heuristic assumes that there are 64
2845 * This is stupid and ugly, but very cheap...
2847 damage
= DIV_ROUND_UP(damage
, 64);
2848 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
2849 dir
.files
[dir
.nr
].name
= file
->name
;
2850 dir
.files
[dir
.nr
].changed
= damage
;
2855 /* This can happen even with many files, if everything was renames */
2859 /* Show all directories with more than x% of the changes */
2860 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
2861 gather_dirstat(options
, &dir
, changed
, "", 0);
2864 static void free_diffstat_info(struct diffstat_t
*diffstat
)
2867 for (i
= 0; i
< diffstat
->nr
; i
++) {
2868 struct diffstat_file
*f
= diffstat
->files
[i
];
2869 if (f
->name
!= f
->print_name
)
2870 free(f
->print_name
);
2875 free(diffstat
->files
);
2878 struct checkdiff_t
{
2879 const char *filename
;
2881 int conflict_marker_size
;
2882 struct diff_options
*o
;
2887 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
2892 if (len
< marker_size
+ 1)
2894 firstchar
= line
[0];
2895 switch (firstchar
) {
2896 case '=': case '>': case '<': case '|':
2901 for (cnt
= 1; cnt
< marker_size
; cnt
++)
2902 if (line
[cnt
] != firstchar
)
2904 /* line[1] thru line[marker_size-1] are same as firstchar */
2905 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
2910 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
2912 struct checkdiff_t
*data
= priv
;
2913 int marker_size
= data
->conflict_marker_size
;
2914 const char *ws
= diff_get_color(data
->o
->use_color
, DIFF_WHITESPACE
);
2915 const char *reset
= diff_get_color(data
->o
->use_color
, DIFF_RESET
);
2916 const char *set
= diff_get_color(data
->o
->use_color
, DIFF_FILE_NEW
);
2918 const char *line_prefix
;
2921 line_prefix
= diff_line_prefix(data
->o
);
2923 if (line
[0] == '+') {
2926 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
2928 fprintf(data
->o
->file
,
2929 "%s%s:%d: leftover conflict marker\n",
2930 line_prefix
, data
->filename
, data
->lineno
);
2932 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
2935 data
->status
|= bad
;
2936 err
= whitespace_error_string(bad
);
2937 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
2938 line_prefix
, data
->filename
, data
->lineno
, err
);
2940 emit_line(data
->o
, set
, reset
, line
, 1);
2941 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
2942 data
->o
->file
, set
, reset
, ws
);
2943 } else if (line
[0] == ' ') {
2945 } else if (line
[0] == '@') {
2946 char *plus
= strchr(line
, '+');
2948 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
2950 die("invalid diff");
2954 static unsigned char *deflate_it(char *data
,
2956 unsigned long *result_size
)
2959 unsigned char *deflated
;
2962 git_deflate_init(&stream
, zlib_compression_level
);
2963 bound
= git_deflate_bound(&stream
, size
);
2964 deflated
= xmalloc(bound
);
2965 stream
.next_out
= deflated
;
2966 stream
.avail_out
= bound
;
2968 stream
.next_in
= (unsigned char *)data
;
2969 stream
.avail_in
= size
;
2970 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
2972 git_deflate_end(&stream
);
2973 *result_size
= stream
.total_out
;
2977 static void emit_binary_diff_body(struct diff_options
*o
,
2978 mmfile_t
*one
, mmfile_t
*two
)
2984 unsigned long orig_size
;
2985 unsigned long delta_size
;
2986 unsigned long deflate_size
;
2987 unsigned long data_size
;
2989 /* We could do deflated delta, or we could do just deflated two,
2990 * whichever is smaller.
2993 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
2994 if (one
->size
&& two
->size
) {
2995 delta
= diff_delta(one
->ptr
, one
->size
,
2996 two
->ptr
, two
->size
,
2997 &delta_size
, deflate_size
);
2999 void *to_free
= delta
;
3000 orig_size
= delta_size
;
3001 delta
= deflate_it(delta
, delta_size
, &delta_size
);
3006 if (delta
&& delta_size
< deflate_size
) {
3007 char *s
= xstrfmt("%lu", orig_size
);
3008 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
3013 data_size
= delta_size
;
3015 char *s
= xstrfmt("%lu", two
->size
);
3016 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
3021 data_size
= deflate_size
;
3024 /* emit data encoded in base85 */
3028 int bytes
= (52 < data_size
) ? 52 : data_size
;
3032 line
[0] = bytes
+ 'A' - 1;
3034 line
[0] = bytes
- 26 + 'a' - 1;
3035 encode_85(line
+ 1, cp
, bytes
);
3036 cp
= (char *) cp
+ bytes
;
3042 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_BODY
,
3045 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_FOOTER
, NULL
, 0, 0);
3049 static void emit_binary_diff(struct diff_options
*o
,
3050 mmfile_t
*one
, mmfile_t
*two
)
3052 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER
, NULL
, 0, 0);
3053 emit_binary_diff_body(o
, one
, two
);
3054 emit_binary_diff_body(o
, two
, one
);
3057 int diff_filespec_is_binary(struct diff_filespec
*one
)
3059 if (one
->is_binary
== -1) {
3060 diff_filespec_load_driver(one
);
3061 if (one
->driver
->binary
!= -1)
3062 one
->is_binary
= one
->driver
->binary
;
3064 if (!one
->data
&& DIFF_FILE_VALID(one
))
3065 diff_populate_filespec(one
, CHECK_BINARY
);
3066 if (one
->is_binary
== -1 && one
->data
)
3067 one
->is_binary
= buffer_is_binary(one
->data
,
3069 if (one
->is_binary
== -1)
3073 return one
->is_binary
;
3076 static const struct userdiff_funcname
*diff_funcname_pattern(struct diff_filespec
*one
)
3078 diff_filespec_load_driver(one
);
3079 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
3082 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
3084 if (!options
->a_prefix
)
3085 options
->a_prefix
= a
;
3086 if (!options
->b_prefix
)
3087 options
->b_prefix
= b
;
3090 struct userdiff_driver
*get_textconv(struct diff_filespec
*one
)
3092 if (!DIFF_FILE_VALID(one
))
3095 diff_filespec_load_driver(one
);
3096 return userdiff_get_textconv(one
->driver
);
3099 static void builtin_diff(const char *name_a
,
3101 struct diff_filespec
*one
,
3102 struct diff_filespec
*two
,
3103 const char *xfrm_msg
,
3104 int must_show_header
,
3105 struct diff_options
*o
,
3106 int complete_rewrite
)
3110 char *a_one
, *b_two
;
3111 const char *meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
3112 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
3113 const char *a_prefix
, *b_prefix
;
3114 struct userdiff_driver
*textconv_one
= NULL
;
3115 struct userdiff_driver
*textconv_two
= NULL
;
3116 struct strbuf header
= STRBUF_INIT
;
3117 const char *line_prefix
= diff_line_prefix(o
);
3119 diff_set_mnemonic_prefix(o
, "a/", "b/");
3120 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
3121 a_prefix
= o
->b_prefix
;
3122 b_prefix
= o
->a_prefix
;
3124 a_prefix
= o
->a_prefix
;
3125 b_prefix
= o
->b_prefix
;
3128 if (o
->submodule_format
== DIFF_SUBMODULE_LOG
&&
3129 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3130 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
3131 show_submodule_summary(o
, one
->path
? one
->path
: two
->path
,
3132 &one
->oid
, &two
->oid
,
3133 two
->dirty_submodule
);
3135 } else if (o
->submodule_format
== DIFF_SUBMODULE_INLINE_DIFF
&&
3136 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3137 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
3138 show_submodule_inline_diff(o
, one
->path
? one
->path
: two
->path
,
3139 &one
->oid
, &two
->oid
,
3140 two
->dirty_submodule
);
3144 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
3145 textconv_one
= get_textconv(one
);
3146 textconv_two
= get_textconv(two
);
3149 /* Never use a non-valid filename anywhere if at all possible */
3150 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
3151 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
3153 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
3154 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
3155 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
3156 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
3157 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
3158 if (lbl
[0][0] == '/') {
3160 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3162 strbuf_addstr(&header
, xfrm_msg
);
3163 must_show_header
= 1;
3165 else if (lbl
[1][0] == '/') {
3166 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3168 strbuf_addstr(&header
, xfrm_msg
);
3169 must_show_header
= 1;
3172 if (one
->mode
!= two
->mode
) {
3173 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3174 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3175 must_show_header
= 1;
3178 strbuf_addstr(&header
, xfrm_msg
);
3181 * we do not run diff between different kind
3184 if ((one
->mode
^ two
->mode
) & S_IFMT
)
3185 goto free_ab_and_return
;
3186 if (complete_rewrite
&&
3187 (textconv_one
|| !diff_filespec_is_binary(one
)) &&
3188 (textconv_two
|| !diff_filespec_is_binary(two
))) {
3189 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3190 header
.buf
, header
.len
, 0);
3191 strbuf_reset(&header
);
3192 emit_rewrite_diff(name_a
, name_b
, one
, two
,
3193 textconv_one
, textconv_two
, o
);
3194 o
->found_changes
= 1;
3195 goto free_ab_and_return
;
3199 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
3200 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
,
3202 strbuf_reset(&header
);
3203 goto free_ab_and_return
;
3204 } else if (!DIFF_OPT_TST(o
, TEXT
) &&
3205 ( (!textconv_one
&& diff_filespec_is_binary(one
)) ||
3206 (!textconv_two
&& diff_filespec_is_binary(two
)) )) {
3207 struct strbuf sb
= STRBUF_INIT
;
3208 if (!one
->data
&& !two
->data
&&
3209 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
3210 !DIFF_OPT_TST(o
, BINARY
)) {
3211 if (!oidcmp(&one
->oid
, &two
->oid
)) {
3212 if (must_show_header
)
3213 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3214 header
.buf
, header
.len
,
3216 goto free_ab_and_return
;
3218 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3219 header
.buf
, header
.len
, 0);
3220 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3221 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3222 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3224 strbuf_release(&sb
);
3225 goto free_ab_and_return
;
3227 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
3228 die("unable to read files to diff");
3229 /* Quite common confusing case */
3230 if (mf1
.size
== mf2
.size
&&
3231 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
3232 if (must_show_header
)
3233 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3234 header
.buf
, header
.len
, 0);
3235 goto free_ab_and_return
;
3237 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
, header
.len
, 0);
3238 strbuf_reset(&header
);
3239 if (DIFF_OPT_TST(o
, BINARY
))
3240 emit_binary_diff(o
, &mf1
, &mf2
);
3242 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3243 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3244 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3246 strbuf_release(&sb
);
3248 o
->found_changes
= 1;
3250 /* Crazy xdl interfaces.. */
3251 const char *diffopts
= getenv("GIT_DIFF_OPTS");
3255 struct emit_callback ecbdata
;
3256 const struct userdiff_funcname
*pe
;
3258 if (must_show_header
) {
3259 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3260 header
.buf
, header
.len
, 0);
3261 strbuf_reset(&header
);
3264 mf1
.size
= fill_textconv(textconv_one
, one
, &mf1
.ptr
);
3265 mf2
.size
= fill_textconv(textconv_two
, two
, &mf2
.ptr
);
3267 pe
= diff_funcname_pattern(one
);
3269 pe
= diff_funcname_pattern(two
);
3271 memset(&xpp
, 0, sizeof(xpp
));
3272 memset(&xecfg
, 0, sizeof(xecfg
));
3273 memset(&ecbdata
, 0, sizeof(ecbdata
));
3274 ecbdata
.label_path
= lbl
;
3275 ecbdata
.color_diff
= want_color(o
->use_color
);
3276 ecbdata
.ws_rule
= whitespace_rule(name_b
);
3277 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
3278 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3280 ecbdata
.header
= header
.len
? &header
: NULL
;
3281 xpp
.flags
= o
->xdl_opts
;
3282 xecfg
.ctxlen
= o
->context
;
3283 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3284 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3285 if (DIFF_OPT_TST(o
, FUNCCONTEXT
))
3286 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
3288 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
3291 else if (skip_prefix(diffopts
, "--unified=", &v
))
3292 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3293 else if (skip_prefix(diffopts
, "-u", &v
))
3294 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3296 init_diff_words_data(&ecbdata
, o
, one
, two
);
3297 if (xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
3299 die("unable to generate diff for %s", one
->path
);
3301 free_diff_words_data(&ecbdata
);
3306 xdiff_clear_find_func(&xecfg
);
3310 strbuf_release(&header
);
3311 diff_free_filespec_data(one
);
3312 diff_free_filespec_data(two
);
3318 static void builtin_diffstat(const char *name_a
, const char *name_b
,
3319 struct diff_filespec
*one
,
3320 struct diff_filespec
*two
,
3321 struct diffstat_t
*diffstat
,
3322 struct diff_options
*o
,
3323 struct diff_filepair
*p
)
3326 struct diffstat_file
*data
;
3328 int complete_rewrite
= 0;
3330 if (!DIFF_PAIR_UNMERGED(p
)) {
3331 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
3332 complete_rewrite
= 1;
3335 data
= diffstat_add(diffstat
, name_a
, name_b
);
3336 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
3339 data
->is_unmerged
= 1;
3343 same_contents
= !oidcmp(&one
->oid
, &two
->oid
);
3345 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
3346 data
->is_binary
= 1;
3347 if (same_contents
) {
3351 data
->added
= diff_filespec_size(two
);
3352 data
->deleted
= diff_filespec_size(one
);
3356 else if (complete_rewrite
) {
3357 diff_populate_filespec(one
, 0);
3358 diff_populate_filespec(two
, 0);
3359 data
->deleted
= count_lines(one
->data
, one
->size
);
3360 data
->added
= count_lines(two
->data
, two
->size
);
3363 else if (!same_contents
) {
3364 /* Crazy xdl interfaces.. */
3368 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
3369 die("unable to read files to diff");
3371 memset(&xpp
, 0, sizeof(xpp
));
3372 memset(&xecfg
, 0, sizeof(xecfg
));
3373 xpp
.flags
= o
->xdl_opts
;
3374 xecfg
.ctxlen
= o
->context
;
3375 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3376 if (xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
3378 die("unable to generate diffstat for %s", one
->path
);
3381 diff_free_filespec_data(one
);
3382 diff_free_filespec_data(two
);
3385 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
3386 const char *attr_path
,
3387 struct diff_filespec
*one
,
3388 struct diff_filespec
*two
,
3389 struct diff_options
*o
)
3392 struct checkdiff_t data
;
3397 memset(&data
, 0, sizeof(data
));
3398 data
.filename
= name_b
? name_b
: name_a
;
3401 data
.ws_rule
= whitespace_rule(attr_path
);
3402 data
.conflict_marker_size
= ll_merge_marker_size(attr_path
);
3404 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
3405 die("unable to read files to diff");
3408 * All the other codepaths check both sides, but not checking
3409 * the "old" side here is deliberate. We are checking the newly
3410 * introduced changes, and as long as the "new" side is text, we
3411 * can and should check what it introduces.
3413 if (diff_filespec_is_binary(two
))
3414 goto free_and_return
;
3416 /* Crazy xdl interfaces.. */
3420 memset(&xpp
, 0, sizeof(xpp
));
3421 memset(&xecfg
, 0, sizeof(xecfg
));
3422 xecfg
.ctxlen
= 1; /* at least one context line */
3424 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
3426 die("unable to generate checkdiff for %s", one
->path
);
3428 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
3429 struct emit_callback ecbdata
;
3432 ecbdata
.ws_rule
= data
.ws_rule
;
3433 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3434 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
3439 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
3440 fprintf(o
->file
, "%s:%d: %s.\n",
3441 data
.filename
, blank_at_eof
, err
);
3442 data
.status
= 1; /* report errors */
3447 diff_free_filespec_data(one
);
3448 diff_free_filespec_data(two
);
3450 DIFF_OPT_SET(o
, CHECK_FAILED
);
3453 struct diff_filespec
*alloc_filespec(const char *path
)
3455 struct diff_filespec
*spec
;
3457 FLEXPTR_ALLOC_STR(spec
, path
, path
);
3459 spec
->is_binary
= -1;
3463 void free_filespec(struct diff_filespec
*spec
)
3465 if (!--spec
->count
) {
3466 diff_free_filespec_data(spec
);
3471 void fill_filespec(struct diff_filespec
*spec
, const struct object_id
*oid
,
3472 int oid_valid
, unsigned short mode
)
3475 spec
->mode
= canon_mode(mode
);
3476 oidcpy(&spec
->oid
, oid
);
3477 spec
->oid_valid
= oid_valid
;
3482 * Given a name and sha1 pair, if the index tells us the file in
3483 * the work tree has that object contents, return true, so that
3484 * prepare_temp_file() does not have to inflate and extract.
3486 static int reuse_worktree_file(const char *name
, const struct object_id
*oid
, int want_file
)
3488 const struct cache_entry
*ce
;
3493 * We do not read the cache ourselves here, because the
3494 * benchmark with my previous version that always reads cache
3495 * shows that it makes things worse for diff-tree comparing
3496 * two linux-2.6 kernel trees in an already checked out work
3497 * tree. This is because most diff-tree comparisons deal with
3498 * only a small number of files, while reading the cache is
3499 * expensive for a large project, and its cost outweighs the
3500 * savings we get by not inflating the object to a temporary
3501 * file. Practically, this code only helps when we are used
3502 * by diff-cache --cached, which does read the cache before
3508 /* We want to avoid the working directory if our caller
3509 * doesn't need the data in a normal file, this system
3510 * is rather slow with its stat/open/mmap/close syscalls,
3511 * and the object is contained in a pack file. The pack
3512 * is probably already open and will be faster to obtain
3513 * the data through than the working directory. Loose
3514 * objects however would tend to be slower as they need
3515 * to be individually opened and inflated.
3517 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(oid
->hash
))
3521 * Similarly, if we'd have to convert the file contents anyway, that
3522 * makes the optimization not worthwhile.
3524 if (!want_file
&& would_convert_to_git(&the_index
, name
))
3528 pos
= cache_name_pos(name
, len
);
3531 ce
= active_cache
[pos
];
3534 * This is not the sha1 we are looking for, or
3535 * unreusable because it is not a regular file.
3537 if (oidcmp(oid
, &ce
->oid
) || !S_ISREG(ce
->ce_mode
))
3541 * If ce is marked as "assume unchanged", there is no
3542 * guarantee that work tree matches what we are looking for.
3544 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
3548 * If ce matches the file in the work tree, we can reuse it.
3550 if (ce_uptodate(ce
) ||
3551 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
3557 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
3559 struct strbuf buf
= STRBUF_INIT
;
3562 /* Are we looking at the work tree? */
3563 if (s
->dirty_submodule
)
3566 strbuf_addf(&buf
, "Subproject commit %s%s\n",
3567 oid_to_hex(&s
->oid
), dirty
);
3571 strbuf_release(&buf
);
3573 s
->data
= strbuf_detach(&buf
, NULL
);
3580 * While doing rename detection and pickaxe operation, we may need to
3581 * grab the data for the blob (or file) for our own in-core comparison.
3582 * diff_filespec has data and size fields for this purpose.
3584 int diff_populate_filespec(struct diff_filespec
*s
, unsigned int flags
)
3586 int size_only
= flags
& CHECK_SIZE_ONLY
;
3589 * demote FAIL to WARN to allow inspecting the situation
3590 * instead of refusing.
3592 enum safe_crlf crlf_warn
= (safe_crlf
== SAFE_CRLF_FAIL
3596 if (!DIFF_FILE_VALID(s
))
3597 die("internal error: asking to populate invalid file.");
3598 if (S_ISDIR(s
->mode
))
3604 if (size_only
&& 0 < s
->size
)
3607 if (S_ISGITLINK(s
->mode
))
3608 return diff_populate_gitlink(s
, size_only
);
3610 if (!s
->oid_valid
||
3611 reuse_worktree_file(s
->path
, &s
->oid
, 0)) {
3612 struct strbuf buf
= STRBUF_INIT
;
3616 if (lstat(s
->path
, &st
) < 0) {
3617 if (errno
== ENOENT
) {
3621 s
->data
= (char *)"";
3626 s
->size
= xsize_t(st
.st_size
);
3629 if (S_ISLNK(st
.st_mode
)) {
3630 struct strbuf sb
= STRBUF_INIT
;
3632 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
3635 s
->data
= strbuf_detach(&sb
, NULL
);
3641 * Even if the caller would be happy with getting
3642 * only the size, we cannot return early at this
3643 * point if the path requires us to run the content
3646 if (size_only
&& !would_convert_to_git(&the_index
, s
->path
))
3650 * Note: this check uses xsize_t(st.st_size) that may
3651 * not be the true size of the blob after it goes
3652 * through convert_to_git(). This may not strictly be
3653 * correct, but the whole point of big_file_threshold
3654 * and is_binary check being that we want to avoid
3655 * opening the file and inspecting the contents, this
3658 if ((flags
& CHECK_BINARY
) &&
3659 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
3663 fd
= open(s
->path
, O_RDONLY
);
3666 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
3668 s
->should_munmap
= 1;
3671 * Convert from working tree format to canonical git format
3673 if (convert_to_git(&the_index
, s
->path
, s
->data
, s
->size
, &buf
, crlf_warn
)) {
3675 munmap(s
->data
, s
->size
);
3676 s
->should_munmap
= 0;
3677 s
->data
= strbuf_detach(&buf
, &size
);
3683 enum object_type type
;
3684 if (size_only
|| (flags
& CHECK_BINARY
)) {
3685 type
= sha1_object_info(s
->oid
.hash
, &s
->size
);
3687 die("unable to read %s",
3688 oid_to_hex(&s
->oid
));
3691 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
3696 s
->data
= read_sha1_file(s
->oid
.hash
, &type
, &s
->size
);
3698 die("unable to read %s", oid_to_hex(&s
->oid
));
3704 void diff_free_filespec_blob(struct diff_filespec
*s
)
3708 else if (s
->should_munmap
)
3709 munmap(s
->data
, s
->size
);
3711 if (s
->should_free
|| s
->should_munmap
) {
3712 s
->should_free
= s
->should_munmap
= 0;
3717 void diff_free_filespec_data(struct diff_filespec
*s
)
3719 diff_free_filespec_blob(s
);
3720 FREE_AND_NULL(s
->cnt_data
);
3723 static void prep_temp_blob(const char *path
, struct diff_tempfile
*temp
,
3726 const struct object_id
*oid
,
3729 struct strbuf buf
= STRBUF_INIT
;
3730 struct strbuf
template = STRBUF_INIT
;
3731 char *path_dup
= xstrdup(path
);
3732 const char *base
= basename(path_dup
);
3734 /* Generate "XXXXXX_basename.ext" */
3735 strbuf_addstr(&template, "XXXXXX_");
3736 strbuf_addstr(&template, base
);
3738 temp
->tempfile
= mks_tempfile_ts(template.buf
, strlen(base
) + 1);
3739 if (!temp
->tempfile
)
3740 die_errno("unable to create temp-file");
3741 if (convert_to_working_tree(path
,
3742 (const char *)blob
, (size_t)size
, &buf
)) {
3746 if (write_in_full(temp
->tempfile
->fd
, blob
, size
) < 0 ||
3747 close_tempfile_gently(temp
->tempfile
))
3748 die_errno("unable to write temp-file");
3749 temp
->name
= get_tempfile_path(temp
->tempfile
);
3750 oid_to_hex_r(temp
->hex
, oid
);
3751 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
3752 strbuf_release(&buf
);
3753 strbuf_release(&template);
3757 static struct diff_tempfile
*prepare_temp_file(const char *name
,
3758 struct diff_filespec
*one
)
3760 struct diff_tempfile
*temp
= claim_diff_tempfile();
3762 if (!DIFF_FILE_VALID(one
)) {
3764 /* A '-' entry produces this for file-2, and
3765 * a '+' entry produces this for file-1.
3767 temp
->name
= "/dev/null";
3768 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
3769 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
3773 if (!S_ISGITLINK(one
->mode
) &&
3775 reuse_worktree_file(name
, &one
->oid
, 1))) {
3777 if (lstat(name
, &st
) < 0) {
3778 if (errno
== ENOENT
)
3779 goto not_a_valid_file
;
3780 die_errno("stat(%s)", name
);
3782 if (S_ISLNK(st
.st_mode
)) {
3783 struct strbuf sb
= STRBUF_INIT
;
3784 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
3785 die_errno("readlink(%s)", name
);
3786 prep_temp_blob(name
, temp
, sb
.buf
, sb
.len
,
3788 &one
->oid
: &null_oid
),
3790 one
->mode
: S_IFLNK
));
3791 strbuf_release(&sb
);
3794 /* we can borrow from the file in the work tree */
3796 if (!one
->oid_valid
)
3797 oid_to_hex_r(temp
->hex
, &null_oid
);
3799 oid_to_hex_r(temp
->hex
, &one
->oid
);
3800 /* Even though we may sometimes borrow the
3801 * contents from the work tree, we always want
3802 * one->mode. mode is trustworthy even when
3803 * !(one->oid_valid), as long as
3804 * DIFF_FILE_VALID(one).
3806 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
3811 if (diff_populate_filespec(one
, 0))
3812 die("cannot read data blob for %s", one
->path
);
3813 prep_temp_blob(name
, temp
, one
->data
, one
->size
,
3814 &one
->oid
, one
->mode
);
3819 static void add_external_diff_name(struct argv_array
*argv
,
3821 struct diff_filespec
*df
)
3823 struct diff_tempfile
*temp
= prepare_temp_file(name
, df
);
3824 argv_array_push(argv
, temp
->name
);
3825 argv_array_push(argv
, temp
->hex
);
3826 argv_array_push(argv
, temp
->mode
);
3829 /* An external diff command takes:
3831 * diff-cmd name infile1 infile1-sha1 infile1-mode \
3832 * infile2 infile2-sha1 infile2-mode [ rename-to ]
3835 static void run_external_diff(const char *pgm
,
3838 struct diff_filespec
*one
,
3839 struct diff_filespec
*two
,
3840 const char *xfrm_msg
,
3841 int complete_rewrite
,
3842 struct diff_options
*o
)
3844 struct argv_array argv
= ARGV_ARRAY_INIT
;
3845 struct argv_array env
= ARGV_ARRAY_INIT
;
3846 struct diff_queue_struct
*q
= &diff_queued_diff
;
3848 argv_array_push(&argv
, pgm
);
3849 argv_array_push(&argv
, name
);
3852 add_external_diff_name(&argv
, name
, one
);
3854 add_external_diff_name(&argv
, name
, two
);
3856 add_external_diff_name(&argv
, other
, two
);
3857 argv_array_push(&argv
, other
);
3858 argv_array_push(&argv
, xfrm_msg
);
3862 argv_array_pushf(&env
, "GIT_DIFF_PATH_COUNTER=%d", ++o
->diff_path_counter
);
3863 argv_array_pushf(&env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
3865 if (run_command_v_opt_cd_env(argv
.argv
, RUN_USING_SHELL
, NULL
, env
.argv
))
3866 die(_("external diff died, stopping at %s"), name
);
3869 argv_array_clear(&argv
);
3870 argv_array_clear(&env
);
3873 static int similarity_index(struct diff_filepair
*p
)
3875 return p
->score
* 100 / MAX_SCORE
;
3878 static const char *diff_abbrev_oid(const struct object_id
*oid
, int abbrev
)
3880 if (startup_info
->have_repository
)
3881 return find_unique_abbrev(oid
->hash
, abbrev
);
3883 char *hex
= oid_to_hex(oid
);
3885 abbrev
= FALLBACK_DEFAULT_ABBREV
;
3886 if (abbrev
> GIT_SHA1_HEXSZ
)
3887 die("BUG: oid abbreviation out of range: %d", abbrev
);
3894 static void fill_metainfo(struct strbuf
*msg
,
3897 struct diff_filespec
*one
,
3898 struct diff_filespec
*two
,
3899 struct diff_options
*o
,
3900 struct diff_filepair
*p
,
3901 int *must_show_header
,
3904 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
3905 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
3906 const char *line_prefix
= diff_line_prefix(o
);
3908 *must_show_header
= 1;
3909 strbuf_init(msg
, PATH_MAX
* 2 + 300);
3910 switch (p
->status
) {
3911 case DIFF_STATUS_COPIED
:
3912 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
3913 line_prefix
, set
, similarity_index(p
));
3914 strbuf_addf(msg
, "%s\n%s%scopy from ",
3915 reset
, line_prefix
, set
);
3916 quote_c_style(name
, msg
, NULL
, 0);
3917 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
3918 quote_c_style(other
, msg
, NULL
, 0);
3919 strbuf_addf(msg
, "%s\n", reset
);
3921 case DIFF_STATUS_RENAMED
:
3922 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
3923 line_prefix
, set
, similarity_index(p
));
3924 strbuf_addf(msg
, "%s\n%s%srename from ",
3925 reset
, line_prefix
, set
);
3926 quote_c_style(name
, msg
, NULL
, 0);
3927 strbuf_addf(msg
, "%s\n%s%srename to ",
3928 reset
, line_prefix
, set
);
3929 quote_c_style(other
, msg
, NULL
, 0);
3930 strbuf_addf(msg
, "%s\n", reset
);
3932 case DIFF_STATUS_MODIFIED
:
3934 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
3936 set
, similarity_index(p
), reset
);
3941 *must_show_header
= 0;
3943 if (one
&& two
&& oidcmp(&one
->oid
, &two
->oid
)) {
3944 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
3946 if (DIFF_OPT_TST(o
, BINARY
)) {
3948 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
3949 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
3952 strbuf_addf(msg
, "%s%sindex %s..%s", line_prefix
, set
,
3953 diff_abbrev_oid(&one
->oid
, abbrev
),
3954 diff_abbrev_oid(&two
->oid
, abbrev
));
3955 if (one
->mode
== two
->mode
)
3956 strbuf_addf(msg
, " %06o", one
->mode
);
3957 strbuf_addf(msg
, "%s\n", reset
);
3961 static void run_diff_cmd(const char *pgm
,
3964 const char *attr_path
,
3965 struct diff_filespec
*one
,
3966 struct diff_filespec
*two
,
3968 struct diff_options
*o
,
3969 struct diff_filepair
*p
)
3971 const char *xfrm_msg
= NULL
;
3972 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
3973 int must_show_header
= 0;
3976 if (DIFF_OPT_TST(o
, ALLOW_EXTERNAL
)) {
3977 struct userdiff_driver
*drv
= userdiff_find_by_path(attr_path
);
3978 if (drv
&& drv
->external
)
3979 pgm
= drv
->external
;
3984 * don't use colors when the header is intended for an
3985 * external diff driver
3987 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
3989 want_color(o
->use_color
) && !pgm
);
3990 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
3994 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
3995 complete_rewrite
, o
);
3999 builtin_diff(name
, other
? other
: name
,
4000 one
, two
, xfrm_msg
, must_show_header
,
4001 o
, complete_rewrite
);
4003 fprintf(o
->file
, "* Unmerged path %s\n", name
);
4006 static void diff_fill_oid_info(struct diff_filespec
*one
)
4008 if (DIFF_FILE_VALID(one
)) {
4009 if (!one
->oid_valid
) {
4011 if (one
->is_stdin
) {
4015 if (lstat(one
->path
, &st
) < 0)
4016 die_errno("stat '%s'", one
->path
);
4017 if (index_path(&one
->oid
, one
->path
, &st
, 0))
4018 die("cannot hash %s", one
->path
);
4025 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
4027 /* Strip the prefix but do not molest /dev/null and absolute paths */
4028 if (*namep
&& **namep
!= '/') {
4029 *namep
+= prefix_length
;
4033 if (*otherp
&& **otherp
!= '/') {
4034 *otherp
+= prefix_length
;
4035 if (**otherp
== '/')
4040 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
4042 const char *pgm
= external_diff();
4044 struct diff_filespec
*one
= p
->one
;
4045 struct diff_filespec
*two
= p
->two
;
4048 const char *attr_path
;
4051 other
= (strcmp(name
, two
->path
) ? two
->path
: NULL
);
4053 if (o
->prefix_length
)
4054 strip_prefix(o
->prefix_length
, &name
, &other
);
4056 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
4059 if (DIFF_PAIR_UNMERGED(p
)) {
4060 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
4061 NULL
, NULL
, NULL
, o
, p
);
4065 diff_fill_oid_info(one
);
4066 diff_fill_oid_info(two
);
4069 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
4070 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
4072 * a filepair that changes between file and symlink
4073 * needs to be split into deletion and creation.
4075 struct diff_filespec
*null
= alloc_filespec(two
->path
);
4076 run_diff_cmd(NULL
, name
, other
, attr_path
,
4077 one
, null
, &msg
, o
, p
);
4079 strbuf_release(&msg
);
4081 null
= alloc_filespec(one
->path
);
4082 run_diff_cmd(NULL
, name
, other
, attr_path
,
4083 null
, two
, &msg
, o
, p
);
4087 run_diff_cmd(pgm
, name
, other
, attr_path
,
4088 one
, two
, &msg
, o
, p
);
4090 strbuf_release(&msg
);
4093 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
4094 struct diffstat_t
*diffstat
)
4099 if (DIFF_PAIR_UNMERGED(p
)) {
4101 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, p
);
4105 name
= p
->one
->path
;
4106 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4108 if (o
->prefix_length
)
4109 strip_prefix(o
->prefix_length
, &name
, &other
);
4111 diff_fill_oid_info(p
->one
);
4112 diff_fill_oid_info(p
->two
);
4114 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, p
);
4117 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
4121 const char *attr_path
;
4123 if (DIFF_PAIR_UNMERGED(p
)) {
4128 name
= p
->one
->path
;
4129 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4130 attr_path
= other
? other
: name
;
4132 if (o
->prefix_length
)
4133 strip_prefix(o
->prefix_length
, &name
, &other
);
4135 diff_fill_oid_info(p
->one
);
4136 diff_fill_oid_info(p
->two
);
4138 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
4141 void diff_setup(struct diff_options
*options
)
4143 memcpy(options
, &default_diff_options
, sizeof(*options
));
4145 options
->file
= stdout
;
4147 options
->abbrev
= DEFAULT_ABBREV
;
4148 options
->line_termination
= '\n';
4149 options
->break_opt
= -1;
4150 options
->rename_limit
= -1;
4151 options
->dirstat_permille
= diff_dirstat_permille_default
;
4152 options
->context
= diff_context_default
;
4153 options
->interhunkcontext
= diff_interhunk_context_default
;
4154 options
->ws_error_highlight
= ws_error_highlight_default
;
4155 DIFF_OPT_SET(options
, RENAME_EMPTY
);
4157 /* pathchange left =NULL by default */
4158 options
->change
= diff_change
;
4159 options
->add_remove
= diff_addremove
;
4160 options
->use_color
= diff_use_color_default
;
4161 options
->detect_rename
= diff_detect_rename_default
;
4162 options
->xdl_opts
|= diff_algorithm
;
4163 if (diff_indent_heuristic
)
4164 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4166 options
->orderfile
= diff_order_file_cfg
;
4168 if (diff_no_prefix
) {
4169 options
->a_prefix
= options
->b_prefix
= "";
4170 } else if (!diff_mnemonic_prefix
) {
4171 options
->a_prefix
= "a/";
4172 options
->b_prefix
= "b/";
4175 options
->color_moved
= diff_color_moved_default
;
4178 void diff_setup_done(struct diff_options
*options
)
4182 if (options
->set_default
)
4183 options
->set_default(options
);
4185 if (options
->output_format
& DIFF_FORMAT_NAME
)
4187 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
4189 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
4191 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
4194 die(_("--name-only, --name-status, --check and -s are mutually exclusive"));
4197 * Most of the time we can say "there are changes"
4198 * only by checking if there are changed paths, but
4199 * --ignore-whitespace* options force us to look
4203 if (DIFF_XDL_TST(options
, IGNORE_WHITESPACE
) ||
4204 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_CHANGE
) ||
4205 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_AT_EOL
))
4206 DIFF_OPT_SET(options
, DIFF_FROM_CONTENTS
);
4208 DIFF_OPT_CLR(options
, DIFF_FROM_CONTENTS
);
4210 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
4211 options
->detect_rename
= DIFF_DETECT_COPY
;
4213 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
4214 options
->prefix
= NULL
;
4215 if (options
->prefix
)
4216 options
->prefix_length
= strlen(options
->prefix
);
4218 options
->prefix_length
= 0;
4220 if (options
->output_format
& (DIFF_FORMAT_NAME
|
4221 DIFF_FORMAT_NAME_STATUS
|
4222 DIFF_FORMAT_CHECKDIFF
|
4223 DIFF_FORMAT_NO_OUTPUT
))
4224 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
4225 DIFF_FORMAT_NUMSTAT
|
4226 DIFF_FORMAT_DIFFSTAT
|
4227 DIFF_FORMAT_SHORTSTAT
|
4228 DIFF_FORMAT_DIRSTAT
|
4229 DIFF_FORMAT_SUMMARY
|
4233 * These cases always need recursive; we do not drop caller-supplied
4234 * recursive bits for other formats here.
4236 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
4237 DIFF_FORMAT_NUMSTAT
|
4238 DIFF_FORMAT_DIFFSTAT
|
4239 DIFF_FORMAT_SHORTSTAT
|
4240 DIFF_FORMAT_DIRSTAT
|
4241 DIFF_FORMAT_SUMMARY
|
4242 DIFF_FORMAT_CHECKDIFF
))
4243 DIFF_OPT_SET(options
, RECURSIVE
);
4245 * Also pickaxe would not work very well if you do not say recursive
4247 if (options
->pickaxe
)
4248 DIFF_OPT_SET(options
, RECURSIVE
);
4250 * When patches are generated, submodules diffed against the work tree
4251 * must be checked for dirtiness too so it can be shown in the output
4253 if (options
->output_format
& DIFF_FORMAT_PATCH
)
4254 DIFF_OPT_SET(options
, DIRTY_SUBMODULES
);
4256 if (options
->detect_rename
&& options
->rename_limit
< 0)
4257 options
->rename_limit
= diff_rename_limit_default
;
4258 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
4260 /* read-cache does not die even when it fails
4261 * so it is safe for us to do this here. Also
4262 * it does not smudge active_cache or active_nr
4263 * when it fails, so we do not have to worry about
4264 * cleaning it up ourselves either.
4268 if (40 < options
->abbrev
)
4269 options
->abbrev
= 40; /* full */
4272 * It does not make sense to show the first hit we happened
4273 * to have found. It does not make sense not to return with
4274 * exit code in such a case either.
4276 if (DIFF_OPT_TST(options
, QUICK
)) {
4277 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
4278 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
4281 options
->diff_path_counter
= 0;
4283 if (DIFF_OPT_TST(options
, FOLLOW_RENAMES
) && options
->pathspec
.nr
!= 1)
4284 die(_("--follow requires exactly one pathspec"));
4286 if (!options
->use_color
|| external_diff())
4287 options
->color_moved
= 0;
4290 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
4300 if (c
== arg_short
) {
4304 if (val
&& isdigit(c
)) {
4306 int n
= strtoul(arg
, &end
, 10);
4317 eq
= strchrnul(arg
, '=');
4319 if (!len
|| strncmp(arg
, arg_long
, len
))
4324 if (!isdigit(*++eq
))
4326 n
= strtoul(eq
, &end
, 10);
4334 static int diff_scoreopt_parse(const char *opt
);
4336 static inline int short_opt(char opt
, const char **argv
,
4337 const char **optarg
)
4339 const char *arg
= argv
[0];
4340 if (arg
[0] != '-' || arg
[1] != opt
)
4342 if (arg
[2] != '\0') {
4347 die("Option '%c' requires a value", opt
);
4352 int parse_long_opt(const char *opt
, const char **argv
,
4353 const char **optarg
)
4355 const char *arg
= argv
[0];
4356 if (!skip_prefix(arg
, "--", &arg
))
4358 if (!skip_prefix(arg
, opt
, &arg
))
4360 if (*arg
== '=') { /* stuck form: --option=value */
4366 /* separate form: --option value */
4368 die("Option '--%s' requires a value", opt
);
4373 static int stat_opt(struct diff_options
*options
, const char **av
)
4375 const char *arg
= av
[0];
4377 int width
= options
->stat_width
;
4378 int name_width
= options
->stat_name_width
;
4379 int graph_width
= options
->stat_graph_width
;
4380 int count
= options
->stat_count
;
4383 if (!skip_prefix(arg
, "--stat", &arg
))
4384 die("BUG: stat option does not begin with --stat: %s", arg
);
4389 if (skip_prefix(arg
, "-width", &arg
)) {
4391 width
= strtoul(arg
+ 1, &end
, 10);
4392 else if (!*arg
&& !av
[1])
4393 die_want_option("--stat-width");
4395 width
= strtoul(av
[1], &end
, 10);
4398 } else if (skip_prefix(arg
, "-name-width", &arg
)) {
4400 name_width
= strtoul(arg
+ 1, &end
, 10);
4401 else if (!*arg
&& !av
[1])
4402 die_want_option("--stat-name-width");
4404 name_width
= strtoul(av
[1], &end
, 10);
4407 } else if (skip_prefix(arg
, "-graph-width", &arg
)) {
4409 graph_width
= strtoul(arg
+ 1, &end
, 10);
4410 else if (!*arg
&& !av
[1])
4411 die_want_option("--stat-graph-width");
4413 graph_width
= strtoul(av
[1], &end
, 10);
4416 } else if (skip_prefix(arg
, "-count", &arg
)) {
4418 count
= strtoul(arg
+ 1, &end
, 10);
4419 else if (!*arg
&& !av
[1])
4420 die_want_option("--stat-count");
4422 count
= strtoul(av
[1], &end
, 10);
4428 width
= strtoul(arg
+1, &end
, 10);
4430 name_width
= strtoul(end
+1, &end
, 10);
4432 count
= strtoul(end
+1, &end
, 10);
4435 /* Important! This checks all the error cases! */
4438 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
4439 options
->stat_name_width
= name_width
;
4440 options
->stat_graph_width
= graph_width
;
4441 options
->stat_width
= width
;
4442 options
->stat_count
= count
;
4446 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
4448 struct strbuf errmsg
= STRBUF_INIT
;
4449 if (parse_dirstat_params(options
, params
, &errmsg
))
4450 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
4452 strbuf_release(&errmsg
);
4454 * The caller knows a dirstat-related option is given from the command
4455 * line; allow it to say "return this_function();"
4457 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
4461 static int parse_submodule_opt(struct diff_options
*options
, const char *value
)
4463 if (parse_submodule_params(options
, value
))
4464 die(_("Failed to parse --submodule option parameter: '%s'"),
4469 static const char diff_status_letters
[] = {
4472 DIFF_STATUS_DELETED
,
4473 DIFF_STATUS_MODIFIED
,
4474 DIFF_STATUS_RENAMED
,
4475 DIFF_STATUS_TYPE_CHANGED
,
4476 DIFF_STATUS_UNKNOWN
,
4477 DIFF_STATUS_UNMERGED
,
4478 DIFF_STATUS_FILTER_AON
,
4479 DIFF_STATUS_FILTER_BROKEN
,
4483 static unsigned int filter_bit
['Z' + 1];
4485 static void prepare_filter_bits(void)
4489 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
4490 for (i
= 0; diff_status_letters
[i
]; i
++)
4491 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
4495 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
4497 return opt
->filter
& filter_bit
[(int) status
];
4500 static int parse_diff_filter_opt(const char *optarg
, struct diff_options
*opt
)
4504 prepare_filter_bits();
4507 * If there is a negation e.g. 'd' in the input, and we haven't
4508 * initialized the filter field with another --diff-filter, start
4509 * from full set of bits, except for AON.
4512 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4513 if (optch
< 'a' || 'z' < optch
)
4515 opt
->filter
= (1 << (ARRAY_SIZE(diff_status_letters
) - 1)) - 1;
4516 opt
->filter
&= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
4521 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4525 if ('a' <= optch
&& optch
<= 'z') {
4527 optch
= toupper(optch
);
4532 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
4536 opt
->filter
&= ~bit
;
4543 static void enable_patch_output(int *fmt
) {
4544 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
4545 *fmt
|= DIFF_FORMAT_PATCH
;
4548 static int parse_ws_error_highlight_opt(struct diff_options
*opt
, const char *arg
)
4550 int val
= parse_ws_error_highlight(arg
);
4553 error("unknown value after ws-error-highlight=%.*s",
4557 opt
->ws_error_highlight
= val
;
4561 int diff_opt_parse(struct diff_options
*options
,
4562 const char **av
, int ac
, const char *prefix
)
4564 const char *arg
= av
[0];
4571 /* Output format options */
4572 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u") || !strcmp(arg
, "--patch")
4573 || opt_arg(arg
, 'U', "unified", &options
->context
))
4574 enable_patch_output(&options
->output_format
);
4575 else if (!strcmp(arg
, "--raw"))
4576 options
->output_format
|= DIFF_FORMAT_RAW
;
4577 else if (!strcmp(arg
, "--patch-with-raw")) {
4578 enable_patch_output(&options
->output_format
);
4579 options
->output_format
|= DIFF_FORMAT_RAW
;
4580 } else if (!strcmp(arg
, "--numstat"))
4581 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
4582 else if (!strcmp(arg
, "--shortstat"))
4583 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
4584 else if (!strcmp(arg
, "-X") || !strcmp(arg
, "--dirstat"))
4585 return parse_dirstat_opt(options
, "");
4586 else if (skip_prefix(arg
, "-X", &arg
))
4587 return parse_dirstat_opt(options
, arg
);
4588 else if (skip_prefix(arg
, "--dirstat=", &arg
))
4589 return parse_dirstat_opt(options
, arg
);
4590 else if (!strcmp(arg
, "--cumulative"))
4591 return parse_dirstat_opt(options
, "cumulative");
4592 else if (!strcmp(arg
, "--dirstat-by-file"))
4593 return parse_dirstat_opt(options
, "files");
4594 else if (skip_prefix(arg
, "--dirstat-by-file=", &arg
)) {
4595 parse_dirstat_opt(options
, "files");
4596 return parse_dirstat_opt(options
, arg
);
4598 else if (!strcmp(arg
, "--check"))
4599 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
4600 else if (!strcmp(arg
, "--summary"))
4601 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
4602 else if (!strcmp(arg
, "--patch-with-stat")) {
4603 enable_patch_output(&options
->output_format
);
4604 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
4605 } else if (!strcmp(arg
, "--name-only"))
4606 options
->output_format
|= DIFF_FORMAT_NAME
;
4607 else if (!strcmp(arg
, "--name-status"))
4608 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
4609 else if (!strcmp(arg
, "-s") || !strcmp(arg
, "--no-patch"))
4610 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
4611 else if (starts_with(arg
, "--stat"))
4612 /* --stat, --stat-width, --stat-name-width, or --stat-count */
4613 return stat_opt(options
, av
);
4615 /* renames options */
4616 else if (starts_with(arg
, "-B") || starts_with(arg
, "--break-rewrites=") ||
4617 !strcmp(arg
, "--break-rewrites")) {
4618 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
4619 return error("invalid argument to -B: %s", arg
+2);
4621 else if (starts_with(arg
, "-M") || starts_with(arg
, "--find-renames=") ||
4622 !strcmp(arg
, "--find-renames")) {
4623 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
4624 return error("invalid argument to -M: %s", arg
+2);
4625 options
->detect_rename
= DIFF_DETECT_RENAME
;
4627 else if (!strcmp(arg
, "-D") || !strcmp(arg
, "--irreversible-delete")) {
4628 options
->irreversible_delete
= 1;
4630 else if (starts_with(arg
, "-C") || starts_with(arg
, "--find-copies=") ||
4631 !strcmp(arg
, "--find-copies")) {
4632 if (options
->detect_rename
== DIFF_DETECT_COPY
)
4633 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
4634 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
4635 return error("invalid argument to -C: %s", arg
+2);
4636 options
->detect_rename
= DIFF_DETECT_COPY
;
4638 else if (!strcmp(arg
, "--no-renames"))
4639 options
->detect_rename
= 0;
4640 else if (!strcmp(arg
, "--rename-empty"))
4641 DIFF_OPT_SET(options
, RENAME_EMPTY
);
4642 else if (!strcmp(arg
, "--no-rename-empty"))
4643 DIFF_OPT_CLR(options
, RENAME_EMPTY
);
4644 else if (!strcmp(arg
, "--relative"))
4645 DIFF_OPT_SET(options
, RELATIVE_NAME
);
4646 else if (skip_prefix(arg
, "--relative=", &arg
)) {
4647 DIFF_OPT_SET(options
, RELATIVE_NAME
);
4648 options
->prefix
= arg
;
4652 else if (!strcmp(arg
, "--minimal"))
4653 DIFF_XDL_SET(options
, NEED_MINIMAL
);
4654 else if (!strcmp(arg
, "--no-minimal"))
4655 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
4656 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
4657 DIFF_XDL_SET(options
, IGNORE_WHITESPACE
);
4658 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
4659 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_CHANGE
);
4660 else if (!strcmp(arg
, "--ignore-space-at-eol"))
4661 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_AT_EOL
);
4662 else if (!strcmp(arg
, "--ignore-blank-lines"))
4663 DIFF_XDL_SET(options
, IGNORE_BLANK_LINES
);
4664 else if (!strcmp(arg
, "--indent-heuristic"))
4665 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4666 else if (!strcmp(arg
, "--no-indent-heuristic"))
4667 DIFF_XDL_CLR(options
, INDENT_HEURISTIC
);
4668 else if (!strcmp(arg
, "--patience"))
4669 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
4670 else if (!strcmp(arg
, "--histogram"))
4671 options
->xdl_opts
= DIFF_WITH_ALG(options
, HISTOGRAM_DIFF
);
4672 else if ((argcount
= parse_long_opt("diff-algorithm", av
, &optarg
))) {
4673 long value
= parse_algorithm_value(optarg
);
4675 return error("option diff-algorithm accepts \"myers\", "
4676 "\"minimal\", \"patience\" and \"histogram\"");
4677 /* clear out previous settings */
4678 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
4679 options
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
4680 options
->xdl_opts
|= value
;
4685 else if (!strcmp(arg
, "--binary")) {
4686 enable_patch_output(&options
->output_format
);
4687 DIFF_OPT_SET(options
, BINARY
);
4689 else if (!strcmp(arg
, "--full-index"))
4690 DIFF_OPT_SET(options
, FULL_INDEX
);
4691 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
4692 DIFF_OPT_SET(options
, TEXT
);
4693 else if (!strcmp(arg
, "-R"))
4694 DIFF_OPT_SET(options
, REVERSE_DIFF
);
4695 else if (!strcmp(arg
, "--find-copies-harder"))
4696 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
4697 else if (!strcmp(arg
, "--follow"))
4698 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
4699 else if (!strcmp(arg
, "--no-follow")) {
4700 DIFF_OPT_CLR(options
, FOLLOW_RENAMES
);
4701 DIFF_OPT_CLR(options
, DEFAULT_FOLLOW_RENAMES
);
4702 } else if (!strcmp(arg
, "--color"))
4703 options
->use_color
= 1;
4704 else if (skip_prefix(arg
, "--color=", &arg
)) {
4705 int value
= git_config_colorbool(NULL
, arg
);
4707 return error("option `color' expects \"always\", \"auto\", or \"never\"");
4708 options
->use_color
= value
;
4710 else if (!strcmp(arg
, "--no-color"))
4711 options
->use_color
= 0;
4712 else if (!strcmp(arg
, "--color-moved")) {
4713 if (diff_color_moved_default
)
4714 options
->color_moved
= diff_color_moved_default
;
4715 if (options
->color_moved
== COLOR_MOVED_NO
)
4716 options
->color_moved
= COLOR_MOVED_DEFAULT
;
4717 } else if (!strcmp(arg
, "--no-color-moved"))
4718 options
->color_moved
= COLOR_MOVED_NO
;
4719 else if (skip_prefix(arg
, "--color-moved=", &arg
)) {
4720 int cm
= parse_color_moved(arg
);
4722 die("bad --color-moved argument: %s", arg
);
4723 options
->color_moved
= cm
;
4724 } else if (!strcmp(arg
, "--color-words")) {
4725 options
->use_color
= 1;
4726 options
->word_diff
= DIFF_WORDS_COLOR
;
4728 else if (skip_prefix(arg
, "--color-words=", &arg
)) {
4729 options
->use_color
= 1;
4730 options
->word_diff
= DIFF_WORDS_COLOR
;
4731 options
->word_regex
= arg
;
4733 else if (!strcmp(arg
, "--word-diff")) {
4734 if (options
->word_diff
== DIFF_WORDS_NONE
)
4735 options
->word_diff
= DIFF_WORDS_PLAIN
;
4737 else if (skip_prefix(arg
, "--word-diff=", &arg
)) {
4738 if (!strcmp(arg
, "plain"))
4739 options
->word_diff
= DIFF_WORDS_PLAIN
;
4740 else if (!strcmp(arg
, "color")) {
4741 options
->use_color
= 1;
4742 options
->word_diff
= DIFF_WORDS_COLOR
;
4744 else if (!strcmp(arg
, "porcelain"))
4745 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
4746 else if (!strcmp(arg
, "none"))
4747 options
->word_diff
= DIFF_WORDS_NONE
;
4749 die("bad --word-diff argument: %s", arg
);
4751 else if ((argcount
= parse_long_opt("word-diff-regex", av
, &optarg
))) {
4752 if (options
->word_diff
== DIFF_WORDS_NONE
)
4753 options
->word_diff
= DIFF_WORDS_PLAIN
;
4754 options
->word_regex
= optarg
;
4757 else if (!strcmp(arg
, "--exit-code"))
4758 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
4759 else if (!strcmp(arg
, "--quiet"))
4760 DIFF_OPT_SET(options
, QUICK
);
4761 else if (!strcmp(arg
, "--ext-diff"))
4762 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
4763 else if (!strcmp(arg
, "--no-ext-diff"))
4764 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
4765 else if (!strcmp(arg
, "--textconv")) {
4766 DIFF_OPT_SET(options
, ALLOW_TEXTCONV
);
4767 DIFF_OPT_SET(options
, TEXTCONV_SET_VIA_CMDLINE
);
4768 } else if (!strcmp(arg
, "--no-textconv"))
4769 DIFF_OPT_CLR(options
, ALLOW_TEXTCONV
);
4770 else if (!strcmp(arg
, "--ignore-submodules")) {
4771 DIFF_OPT_SET(options
, OVERRIDE_SUBMODULE_CONFIG
);
4772 handle_ignore_submodules_arg(options
, "all");
4773 } else if (skip_prefix(arg
, "--ignore-submodules=", &arg
)) {
4774 DIFF_OPT_SET(options
, OVERRIDE_SUBMODULE_CONFIG
);
4775 handle_ignore_submodules_arg(options
, arg
);
4776 } else if (!strcmp(arg
, "--submodule"))
4777 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
4778 else if (skip_prefix(arg
, "--submodule=", &arg
))
4779 return parse_submodule_opt(options
, arg
);
4780 else if (skip_prefix(arg
, "--ws-error-highlight=", &arg
))
4781 return parse_ws_error_highlight_opt(options
, arg
);
4782 else if (!strcmp(arg
, "--ita-invisible-in-index"))
4783 options
->ita_invisible_in_index
= 1;
4784 else if (!strcmp(arg
, "--ita-visible-in-index"))
4785 options
->ita_invisible_in_index
= 0;
4788 else if (!strcmp(arg
, "-z"))
4789 options
->line_termination
= 0;
4790 else if ((argcount
= short_opt('l', av
, &optarg
))) {
4791 options
->rename_limit
= strtoul(optarg
, NULL
, 10);
4794 else if ((argcount
= short_opt('S', av
, &optarg
))) {
4795 options
->pickaxe
= optarg
;
4796 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
4798 } else if ((argcount
= short_opt('G', av
, &optarg
))) {
4799 options
->pickaxe
= optarg
;
4800 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
4803 else if (!strcmp(arg
, "--pickaxe-all"))
4804 options
->pickaxe_opts
|= DIFF_PICKAXE_ALL
;
4805 else if (!strcmp(arg
, "--pickaxe-regex"))
4806 options
->pickaxe_opts
|= DIFF_PICKAXE_REGEX
;
4807 else if ((argcount
= short_opt('O', av
, &optarg
))) {
4808 options
->orderfile
= prefix_filename(prefix
, optarg
);
4811 else if ((argcount
= parse_long_opt("diff-filter", av
, &optarg
))) {
4812 int offending
= parse_diff_filter_opt(optarg
, options
);
4814 die("unknown change class '%c' in --diff-filter=%s",
4818 else if (!strcmp(arg
, "--no-abbrev"))
4819 options
->abbrev
= 0;
4820 else if (!strcmp(arg
, "--abbrev"))
4821 options
->abbrev
= DEFAULT_ABBREV
;
4822 else if (skip_prefix(arg
, "--abbrev=", &arg
)) {
4823 options
->abbrev
= strtoul(arg
, NULL
, 10);
4824 if (options
->abbrev
< MINIMUM_ABBREV
)
4825 options
->abbrev
= MINIMUM_ABBREV
;
4826 else if (40 < options
->abbrev
)
4827 options
->abbrev
= 40;
4829 else if ((argcount
= parse_long_opt("src-prefix", av
, &optarg
))) {
4830 options
->a_prefix
= optarg
;
4833 else if ((argcount
= parse_long_opt("line-prefix", av
, &optarg
))) {
4834 options
->line_prefix
= optarg
;
4835 options
->line_prefix_length
= strlen(options
->line_prefix
);
4836 graph_setup_line_prefix(options
);
4839 else if ((argcount
= parse_long_opt("dst-prefix", av
, &optarg
))) {
4840 options
->b_prefix
= optarg
;
4843 else if (!strcmp(arg
, "--no-prefix"))
4844 options
->a_prefix
= options
->b_prefix
= "";
4845 else if (opt_arg(arg
, '\0', "inter-hunk-context",
4846 &options
->interhunkcontext
))
4848 else if (!strcmp(arg
, "-W"))
4849 DIFF_OPT_SET(options
, FUNCCONTEXT
);
4850 else if (!strcmp(arg
, "--function-context"))
4851 DIFF_OPT_SET(options
, FUNCCONTEXT
);
4852 else if (!strcmp(arg
, "--no-function-context"))
4853 DIFF_OPT_CLR(options
, FUNCCONTEXT
);
4854 else if ((argcount
= parse_long_opt("output", av
, &optarg
))) {
4855 char *path
= prefix_filename(prefix
, optarg
);
4856 options
->file
= xfopen(path
, "w");
4857 options
->close_file
= 1;
4858 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
4859 options
->use_color
= GIT_COLOR_NEVER
;
4867 int parse_rename_score(const char **cp_p
)
4869 unsigned long num
, scale
;
4871 const char *cp
= *cp_p
;
4878 if ( !dot
&& ch
== '.' ) {
4881 } else if ( ch
== '%' ) {
4882 scale
= dot
? scale
*100 : 100;
4883 cp
++; /* % is always at the end */
4885 } else if ( ch
>= '0' && ch
<= '9' ) {
4886 if ( scale
< 100000 ) {
4888 num
= (num
*10) + (ch
-'0');
4897 /* user says num divided by scale and we say internally that
4898 * is MAX_SCORE * num / scale.
4900 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
4903 static int diff_scoreopt_parse(const char *opt
)
4905 int opt1
, opt2
, cmd
;
4911 /* convert the long-form arguments into short-form versions */
4912 if (skip_prefix(opt
, "break-rewrites", &opt
)) {
4913 if (*opt
== 0 || *opt
++ == '=')
4915 } else if (skip_prefix(opt
, "find-copies", &opt
)) {
4916 if (*opt
== 0 || *opt
++ == '=')
4918 } else if (skip_prefix(opt
, "find-renames", &opt
)) {
4919 if (*opt
== 0 || *opt
++ == '=')
4923 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
4924 return -1; /* that is not a -M, -C, or -B option */
4926 opt1
= parse_rename_score(&opt
);
4932 else if (*opt
!= '/')
4933 return -1; /* we expect -B80/99 or -B80 */
4936 opt2
= parse_rename_score(&opt
);
4941 return opt1
| (opt2
<< 16);
4944 struct diff_queue_struct diff_queued_diff
;
4946 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
4948 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
4949 queue
->queue
[queue
->nr
++] = dp
;
4952 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
4953 struct diff_filespec
*one
,
4954 struct diff_filespec
*two
)
4956 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
4964 void diff_free_filepair(struct diff_filepair
*p
)
4966 free_filespec(p
->one
);
4967 free_filespec(p
->two
);
4971 const char *diff_aligned_abbrev(const struct object_id
*oid
, int len
)
4976 if (len
== GIT_SHA1_HEXSZ
)
4977 return oid_to_hex(oid
);
4979 abbrev
= diff_abbrev_oid(oid
, len
);
4980 abblen
= strlen(abbrev
);
4983 * In well-behaved cases, where the abbbreviated result is the
4984 * same as the requested length, append three dots after the
4985 * abbreviation (hence the whole logic is limited to the case
4986 * where abblen < 37); when the actual abbreviated result is a
4987 * bit longer than the requested length, we reduce the number
4988 * of dots so that they match the well-behaved ones. However,
4989 * if the actual abbreviation is longer than the requested
4990 * length by more than three, we give up on aligning, and add
4991 * three dots anyway, to indicate that the output is not the
4992 * full object name. Yes, this may be suboptimal, but this
4993 * appears only in "diff --raw --abbrev" output and it is not
4994 * worth the effort to change it now. Note that this would
4995 * likely to work fine when the automatic sizing of default
4996 * abbreviation length is used--we would be fed -1 in "len" in
4997 * that case, and will end up always appending three-dots, but
4998 * the automatic sizing is supposed to give abblen that ensures
4999 * uniqueness across all objects (statistically speaking).
5001 if (abblen
< GIT_SHA1_HEXSZ
- 3) {
5002 static char hex
[GIT_MAX_HEXSZ
+ 1];
5003 if (len
< abblen
&& abblen
<= len
+ 2)
5004 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
5006 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
5010 return oid_to_hex(oid
);
5013 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
5015 int line_termination
= opt
->line_termination
;
5016 int inter_name_termination
= line_termination
? '\t' : '\0';
5018 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
5019 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
5020 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
5021 diff_aligned_abbrev(&p
->one
->oid
, opt
->abbrev
));
5022 fprintf(opt
->file
, "%s ",
5023 diff_aligned_abbrev(&p
->two
->oid
, opt
->abbrev
));
5026 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
5027 inter_name_termination
);
5029 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
5032 if (p
->status
== DIFF_STATUS_COPIED
||
5033 p
->status
== DIFF_STATUS_RENAMED
) {
5034 const char *name_a
, *name_b
;
5035 name_a
= p
->one
->path
;
5036 name_b
= p
->two
->path
;
5037 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5038 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
5039 write_name_quoted(name_b
, opt
->file
, line_termination
);
5041 const char *name_a
, *name_b
;
5042 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
5044 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5045 write_name_quoted(name_a
, opt
->file
, line_termination
);
5049 int diff_unmodified_pair(struct diff_filepair
*p
)
5051 /* This function is written stricter than necessary to support
5052 * the currently implemented transformers, but the idea is to
5053 * let transformers to produce diff_filepairs any way they want,
5054 * and filter and clean them up here before producing the output.
5056 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
5058 if (DIFF_PAIR_UNMERGED(p
))
5059 return 0; /* unmerged is interesting */
5061 /* deletion, addition, mode or type change
5062 * and rename are all interesting.
5064 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
5065 DIFF_PAIR_MODE_CHANGED(p
) ||
5066 strcmp(one
->path
, two
->path
))
5069 /* both are valid and point at the same path. that is, we are
5070 * dealing with a change.
5072 if (one
->oid_valid
&& two
->oid_valid
&&
5073 !oidcmp(&one
->oid
, &two
->oid
) &&
5074 !one
->dirty_submodule
&& !two
->dirty_submodule
)
5075 return 1; /* no change */
5076 if (!one
->oid_valid
&& !two
->oid_valid
)
5077 return 1; /* both look at the same file on the filesystem. */
5081 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
5083 if (diff_unmodified_pair(p
))
5086 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5087 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5088 return; /* no tree diffs in patch format */
5093 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
5094 struct diffstat_t
*diffstat
)
5096 if (diff_unmodified_pair(p
))
5099 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5100 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5101 return; /* no useful stat for tree diffs */
5103 run_diffstat(p
, o
, diffstat
);
5106 static void diff_flush_checkdiff(struct diff_filepair
*p
,
5107 struct diff_options
*o
)
5109 if (diff_unmodified_pair(p
))
5112 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5113 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5114 return; /* nothing to check in tree diffs */
5116 run_checkdiff(p
, o
);
5119 int diff_queue_is_empty(void)
5121 struct diff_queue_struct
*q
= &diff_queued_diff
;
5123 for (i
= 0; i
< q
->nr
; i
++)
5124 if (!diff_unmodified_pair(q
->queue
[i
]))
5130 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
5132 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
5135 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
5137 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
5138 fprintf(stderr
, "queue[%d] %s size %lu\n",
5143 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
5145 diff_debug_filespec(p
->one
, i
, "one");
5146 diff_debug_filespec(p
->two
, i
, "two");
5147 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
5148 p
->score
, p
->status
? p
->status
: '?',
5149 p
->one
->rename_used
, p
->broken_pair
);
5152 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
5156 fprintf(stderr
, "%s\n", msg
);
5157 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
5158 for (i
= 0; i
< q
->nr
; i
++) {
5159 struct diff_filepair
*p
= q
->queue
[i
];
5160 diff_debug_filepair(p
, i
);
5165 static void diff_resolve_rename_copy(void)
5168 struct diff_filepair
*p
;
5169 struct diff_queue_struct
*q
= &diff_queued_diff
;
5171 diff_debug_queue("resolve-rename-copy", q
);
5173 for (i
= 0; i
< q
->nr
; i
++) {
5175 p
->status
= 0; /* undecided */
5176 if (DIFF_PAIR_UNMERGED(p
))
5177 p
->status
= DIFF_STATUS_UNMERGED
;
5178 else if (!DIFF_FILE_VALID(p
->one
))
5179 p
->status
= DIFF_STATUS_ADDED
;
5180 else if (!DIFF_FILE_VALID(p
->two
))
5181 p
->status
= DIFF_STATUS_DELETED
;
5182 else if (DIFF_PAIR_TYPE_CHANGED(p
))
5183 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
5185 /* from this point on, we are dealing with a pair
5186 * whose both sides are valid and of the same type, i.e.
5187 * either in-place edit or rename/copy edit.
5189 else if (DIFF_PAIR_RENAME(p
)) {
5191 * A rename might have re-connected a broken
5192 * pair up, causing the pathnames to be the
5193 * same again. If so, that's not a rename at
5194 * all, just a modification..
5196 * Otherwise, see if this source was used for
5197 * multiple renames, in which case we decrement
5198 * the count, and call it a copy.
5200 if (!strcmp(p
->one
->path
, p
->two
->path
))
5201 p
->status
= DIFF_STATUS_MODIFIED
;
5202 else if (--p
->one
->rename_used
> 0)
5203 p
->status
= DIFF_STATUS_COPIED
;
5205 p
->status
= DIFF_STATUS_RENAMED
;
5207 else if (oidcmp(&p
->one
->oid
, &p
->two
->oid
) ||
5208 p
->one
->mode
!= p
->two
->mode
||
5209 p
->one
->dirty_submodule
||
5210 p
->two
->dirty_submodule
||
5211 is_null_oid(&p
->one
->oid
))
5212 p
->status
= DIFF_STATUS_MODIFIED
;
5214 /* This is a "no-change" entry and should not
5215 * happen anymore, but prepare for broken callers.
5217 error("feeding unmodified %s to diffcore",
5219 p
->status
= DIFF_STATUS_UNKNOWN
;
5222 diff_debug_queue("resolve-rename-copy done", q
);
5225 static int check_pair_status(struct diff_filepair
*p
)
5227 switch (p
->status
) {
5228 case DIFF_STATUS_UNKNOWN
:
5231 die("internal error in diff-resolve-rename-copy");
5237 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
5239 int fmt
= opt
->output_format
;
5241 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
5242 diff_flush_checkdiff(p
, opt
);
5243 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
5244 diff_flush_raw(p
, opt
);
5245 else if (fmt
& DIFF_FORMAT_NAME
) {
5246 const char *name_a
, *name_b
;
5247 name_a
= p
->two
->path
;
5249 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5250 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
5251 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
5255 static void show_file_mode_name(struct diff_options
*opt
, const char *newdelete
, struct diff_filespec
*fs
)
5257 struct strbuf sb
= STRBUF_INIT
;
5259 strbuf_addf(&sb
, " %s mode %06o ", newdelete
, fs
->mode
);
5261 strbuf_addf(&sb
, " %s ", newdelete
);
5263 quote_c_style(fs
->path
, &sb
, NULL
, 0);
5264 strbuf_addch(&sb
, '\n');
5265 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
5267 strbuf_release(&sb
);
5270 static void show_mode_change(struct diff_options
*opt
, struct diff_filepair
*p
,
5273 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
5274 struct strbuf sb
= STRBUF_INIT
;
5275 strbuf_addf(&sb
, " mode change %06o => %06o",
5276 p
->one
->mode
, p
->two
->mode
);
5278 strbuf_addch(&sb
, ' ');
5279 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
5281 strbuf_addch(&sb
, '\n');
5282 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
5284 strbuf_release(&sb
);
5288 static void show_rename_copy(struct diff_options
*opt
, const char *renamecopy
,
5289 struct diff_filepair
*p
)
5291 struct strbuf sb
= STRBUF_INIT
;
5292 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
5293 strbuf_addf(&sb
, " %s %s (%d%%)\n",
5294 renamecopy
, names
, similarity_index(p
));
5296 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
5298 show_mode_change(opt
, p
, 0);
5299 strbuf_release(&sb
);
5302 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
5305 case DIFF_STATUS_DELETED
:
5306 show_file_mode_name(opt
, "delete", p
->one
);
5308 case DIFF_STATUS_ADDED
:
5309 show_file_mode_name(opt
, "create", p
->two
);
5311 case DIFF_STATUS_COPIED
:
5312 show_rename_copy(opt
, "copy", p
);
5314 case DIFF_STATUS_RENAMED
:
5315 show_rename_copy(opt
, "rename", p
);
5319 struct strbuf sb
= STRBUF_INIT
;
5320 strbuf_addstr(&sb
, " rewrite ");
5321 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
5322 strbuf_addf(&sb
, " (%d%%)\n", similarity_index(p
));
5323 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
5325 strbuf_release(&sb
);
5327 show_mode_change(opt
, p
, !p
->score
);
5337 static int remove_space(char *line
, int len
)
5343 for (i
= 0; i
< len
; i
++)
5344 if (!isspace((c
= line
[i
])))
5350 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
5352 struct patch_id_t
*data
= priv
;
5355 /* Ignore line numbers when computing the SHA1 of the patch */
5356 if (starts_with(line
, "@@ -"))
5359 new_len
= remove_space(line
, len
);
5361 git_SHA1_Update(data
->ctx
, line
, new_len
);
5362 data
->patchlen
+= new_len
;
5365 static void patch_id_add_string(git_SHA_CTX
*ctx
, const char *str
)
5367 git_SHA1_Update(ctx
, str
, strlen(str
));
5370 static void patch_id_add_mode(git_SHA_CTX
*ctx
, unsigned mode
)
5372 /* large enough for 2^32 in octal */
5374 int len
= xsnprintf(buf
, sizeof(buf
), "%06o", mode
);
5375 git_SHA1_Update(ctx
, buf
, len
);
5378 /* returns 0 upon success, and writes result into sha1 */
5379 static int diff_get_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
5381 struct diff_queue_struct
*q
= &diff_queued_diff
;
5384 struct patch_id_t data
;
5386 git_SHA1_Init(&ctx
);
5387 memset(&data
, 0, sizeof(struct patch_id_t
));
5390 for (i
= 0; i
< q
->nr
; i
++) {
5394 struct diff_filepair
*p
= q
->queue
[i
];
5397 memset(&xpp
, 0, sizeof(xpp
));
5398 memset(&xecfg
, 0, sizeof(xecfg
));
5400 return error("internal diff status error");
5401 if (p
->status
== DIFF_STATUS_UNKNOWN
)
5403 if (diff_unmodified_pair(p
))
5405 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5406 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5408 if (DIFF_PAIR_UNMERGED(p
))
5411 diff_fill_oid_info(p
->one
);
5412 diff_fill_oid_info(p
->two
);
5414 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
5415 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
5416 patch_id_add_string(&ctx
, "diff--git");
5417 patch_id_add_string(&ctx
, "a/");
5418 git_SHA1_Update(&ctx
, p
->one
->path
, len1
);
5419 patch_id_add_string(&ctx
, "b/");
5420 git_SHA1_Update(&ctx
, p
->two
->path
, len2
);
5422 if (p
->one
->mode
== 0) {
5423 patch_id_add_string(&ctx
, "newfilemode");
5424 patch_id_add_mode(&ctx
, p
->two
->mode
);
5425 patch_id_add_string(&ctx
, "---/dev/null");
5426 patch_id_add_string(&ctx
, "+++b/");
5427 git_SHA1_Update(&ctx
, p
->two
->path
, len2
);
5428 } else if (p
->two
->mode
== 0) {
5429 patch_id_add_string(&ctx
, "deletedfilemode");
5430 patch_id_add_mode(&ctx
, p
->one
->mode
);
5431 patch_id_add_string(&ctx
, "---a/");
5432 git_SHA1_Update(&ctx
, p
->one
->path
, len1
);
5433 patch_id_add_string(&ctx
, "+++/dev/null");
5435 patch_id_add_string(&ctx
, "---a/");
5436 git_SHA1_Update(&ctx
, p
->one
->path
, len1
);
5437 patch_id_add_string(&ctx
, "+++b/");
5438 git_SHA1_Update(&ctx
, p
->two
->path
, len2
);
5441 if (diff_header_only
)
5444 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
5445 fill_mmfile(&mf2
, p
->two
) < 0)
5446 return error("unable to read files to diff");
5448 if (diff_filespec_is_binary(p
->one
) ||
5449 diff_filespec_is_binary(p
->two
)) {
5450 git_SHA1_Update(&ctx
, oid_to_hex(&p
->one
->oid
),
5452 git_SHA1_Update(&ctx
, oid_to_hex(&p
->two
->oid
),
5460 if (xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
5462 return error("unable to generate patch-id diff for %s",
5466 git_SHA1_Final(oid
->hash
, &ctx
);
5470 int diff_flush_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
5472 struct diff_queue_struct
*q
= &diff_queued_diff
;
5474 int result
= diff_get_patch_id(options
, oid
, diff_header_only
);
5476 for (i
= 0; i
< q
->nr
; i
++)
5477 diff_free_filepair(q
->queue
[i
]);
5480 DIFF_QUEUE_CLEAR(q
);
5485 static int is_summary_empty(const struct diff_queue_struct
*q
)
5489 for (i
= 0; i
< q
->nr
; i
++) {
5490 const struct diff_filepair
*p
= q
->queue
[i
];
5492 switch (p
->status
) {
5493 case DIFF_STATUS_DELETED
:
5494 case DIFF_STATUS_ADDED
:
5495 case DIFF_STATUS_COPIED
:
5496 case DIFF_STATUS_RENAMED
:
5501 if (p
->one
->mode
&& p
->two
->mode
&&
5502 p
->one
->mode
!= p
->two
->mode
)
5510 static const char rename_limit_warning
[] =
5511 N_("inexact rename detection was skipped due to too many files.");
5513 static const char degrade_cc_to_c_warning
[] =
5514 N_("only found copies from modified paths due to too many files.");
5516 static const char rename_limit_advice
[] =
5517 N_("you may want to set your %s variable to at least "
5518 "%d and retry the command.");
5520 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
5523 warning(_(degrade_cc_to_c_warning
));
5525 warning(_(rename_limit_warning
));
5528 if (0 < needed
&& needed
< 32767)
5529 warning(_(rename_limit_advice
), varname
, needed
);
5532 static void diff_flush_patch_all_file_pairs(struct diff_options
*o
)
5535 static struct emitted_diff_symbols esm
= EMITTED_DIFF_SYMBOLS_INIT
;
5536 struct diff_queue_struct
*q
= &diff_queued_diff
;
5538 if (WSEH_NEW
& WS_RULE_MASK
)
5539 die("BUG: WS rules bit mask overlaps with diff symbol flags");
5542 o
->emitted_symbols
= &esm
;
5544 for (i
= 0; i
< q
->nr
; i
++) {
5545 struct diff_filepair
*p
= q
->queue
[i
];
5546 if (check_pair_status(p
))
5547 diff_flush_patch(p
, o
);
5550 if (o
->emitted_symbols
) {
5551 if (o
->color_moved
) {
5552 struct hashmap add_lines
, del_lines
;
5554 hashmap_init(&del_lines
,
5555 (hashmap_cmp_fn
)moved_entry_cmp
, o
, 0);
5556 hashmap_init(&add_lines
,
5557 (hashmap_cmp_fn
)moved_entry_cmp
, o
, 0);
5559 add_lines_to_move_detection(o
, &add_lines
, &del_lines
);
5560 mark_color_as_moved(o
, &add_lines
, &del_lines
);
5561 if (o
->color_moved
== COLOR_MOVED_ZEBRA_DIM
)
5564 hashmap_free(&add_lines
, 0);
5565 hashmap_free(&del_lines
, 0);
5568 for (i
= 0; i
< esm
.nr
; i
++)
5569 emit_diff_symbol_from_struct(o
, &esm
.buf
[i
]);
5571 for (i
= 0; i
< esm
.nr
; i
++)
5572 free((void *)esm
.buf
[i
].line
);
5577 void diff_flush(struct diff_options
*options
)
5579 struct diff_queue_struct
*q
= &diff_queued_diff
;
5580 int i
, output_format
= options
->output_format
;
5582 int dirstat_by_line
= 0;
5585 * Order: raw, stat, summary, patch
5586 * or: name/name-status/checkdiff (other bits clear)
5591 if (output_format
& (DIFF_FORMAT_RAW
|
5593 DIFF_FORMAT_NAME_STATUS
|
5594 DIFF_FORMAT_CHECKDIFF
)) {
5595 for (i
= 0; i
< q
->nr
; i
++) {
5596 struct diff_filepair
*p
= q
->queue
[i
];
5597 if (check_pair_status(p
))
5598 flush_one_pair(p
, options
);
5603 if (output_format
& DIFF_FORMAT_DIRSTAT
&& DIFF_OPT_TST(options
, DIRSTAT_BY_LINE
))
5604 dirstat_by_line
= 1;
5606 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
5608 struct diffstat_t diffstat
;
5610 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
5611 for (i
= 0; i
< q
->nr
; i
++) {
5612 struct diff_filepair
*p
= q
->queue
[i
];
5613 if (check_pair_status(p
))
5614 diff_flush_stat(p
, options
, &diffstat
);
5616 if (output_format
& DIFF_FORMAT_NUMSTAT
)
5617 show_numstat(&diffstat
, options
);
5618 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
5619 show_stats(&diffstat
, options
);
5620 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
5621 show_shortstats(&diffstat
, options
);
5622 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
5623 show_dirstat_by_line(&diffstat
, options
);
5624 free_diffstat_info(&diffstat
);
5627 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
5628 show_dirstat(options
);
5630 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
5631 for (i
= 0; i
< q
->nr
; i
++) {
5632 diff_summary(options
, q
->queue
[i
]);
5637 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
5638 DIFF_OPT_TST(options
, EXIT_WITH_STATUS
) &&
5639 DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
5641 * run diff_flush_patch for the exit status. setting
5642 * options->file to /dev/null should be safe, because we
5643 * aren't supposed to produce any output anyway.
5645 if (options
->close_file
)
5646 fclose(options
->file
);
5647 options
->file
= xfopen("/dev/null", "w");
5648 options
->close_file
= 1;
5649 options
->color_moved
= 0;
5650 for (i
= 0; i
< q
->nr
; i
++) {
5651 struct diff_filepair
*p
= q
->queue
[i
];
5652 if (check_pair_status(p
))
5653 diff_flush_patch(p
, options
);
5654 if (options
->found_changes
)
5659 if (output_format
& DIFF_FORMAT_PATCH
) {
5661 emit_diff_symbol(options
, DIFF_SYMBOL_SEPARATOR
, NULL
, 0, 0);
5662 if (options
->stat_sep
)
5663 /* attach patch instead of inline */
5664 emit_diff_symbol(options
, DIFF_SYMBOL_STAT_SEP
,
5668 diff_flush_patch_all_file_pairs(options
);
5671 if (output_format
& DIFF_FORMAT_CALLBACK
)
5672 options
->format_callback(q
, options
, options
->format_callback_data
);
5674 for (i
= 0; i
< q
->nr
; i
++)
5675 diff_free_filepair(q
->queue
[i
]);
5678 DIFF_QUEUE_CLEAR(q
);
5679 if (options
->close_file
)
5680 fclose(options
->file
);
5683 * Report the content-level differences with HAS_CHANGES;
5684 * diff_addremove/diff_change does not set the bit when
5685 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
5687 if (DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
5688 if (options
->found_changes
)
5689 DIFF_OPT_SET(options
, HAS_CHANGES
);
5691 DIFF_OPT_CLR(options
, HAS_CHANGES
);
5695 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
5697 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
5699 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
5701 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
5702 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
5703 filter_bit_tst(p
->status
, options
)));
5706 static void diffcore_apply_filter(struct diff_options
*options
)
5709 struct diff_queue_struct
*q
= &diff_queued_diff
;
5710 struct diff_queue_struct outq
;
5712 DIFF_QUEUE_CLEAR(&outq
);
5714 if (!options
->filter
)
5717 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
5719 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
5720 if (match_filter(options
, q
->queue
[i
]))
5726 /* otherwise we will clear the whole queue
5727 * by copying the empty outq at the end of this
5728 * function, but first clear the current entries
5731 for (i
= 0; i
< q
->nr
; i
++)
5732 diff_free_filepair(q
->queue
[i
]);
5735 /* Only the matching ones */
5736 for (i
= 0; i
< q
->nr
; i
++) {
5737 struct diff_filepair
*p
= q
->queue
[i
];
5738 if (match_filter(options
, p
))
5741 diff_free_filepair(p
);
5748 /* Check whether two filespecs with the same mode and size are identical */
5749 static int diff_filespec_is_identical(struct diff_filespec
*one
,
5750 struct diff_filespec
*two
)
5752 if (S_ISGITLINK(one
->mode
))
5754 if (diff_populate_filespec(one
, 0))
5756 if (diff_populate_filespec(two
, 0))
5758 return !memcmp(one
->data
, two
->data
, one
->size
);
5761 static int diff_filespec_check_stat_unmatch(struct diff_filepair
*p
)
5763 if (p
->done_skip_stat_unmatch
)
5764 return p
->skip_stat_unmatch_result
;
5766 p
->done_skip_stat_unmatch
= 1;
5767 p
->skip_stat_unmatch_result
= 0;
5769 * 1. Entries that come from stat info dirtiness
5770 * always have both sides (iow, not create/delete),
5771 * one side of the object name is unknown, with
5772 * the same mode and size. Keep the ones that
5773 * do not match these criteria. They have real
5776 * 2. At this point, the file is known to be modified,
5777 * with the same mode and size, and the object
5778 * name of one side is unknown. Need to inspect
5779 * the identical contents.
5781 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
5782 !DIFF_FILE_VALID(p
->two
) ||
5783 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
5784 (p
->one
->mode
!= p
->two
->mode
) ||
5785 diff_populate_filespec(p
->one
, CHECK_SIZE_ONLY
) ||
5786 diff_populate_filespec(p
->two
, CHECK_SIZE_ONLY
) ||
5787 (p
->one
->size
!= p
->two
->size
) ||
5788 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
5789 p
->skip_stat_unmatch_result
= 1;
5790 return p
->skip_stat_unmatch_result
;
5793 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
5796 struct diff_queue_struct
*q
= &diff_queued_diff
;
5797 struct diff_queue_struct outq
;
5798 DIFF_QUEUE_CLEAR(&outq
);
5800 for (i
= 0; i
< q
->nr
; i
++) {
5801 struct diff_filepair
*p
= q
->queue
[i
];
5803 if (diff_filespec_check_stat_unmatch(p
))
5807 * The caller can subtract 1 from skip_stat_unmatch
5808 * to determine how many paths were dirty only
5809 * due to stat info mismatch.
5811 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
5812 diffopt
->skip_stat_unmatch
++;
5813 diff_free_filepair(p
);
5820 static int diffnamecmp(const void *a_
, const void *b_
)
5822 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
5823 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
5824 const char *name_a
, *name_b
;
5826 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
5827 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
5828 return strcmp(name_a
, name_b
);
5831 void diffcore_fix_diff_index(struct diff_options
*options
)
5833 struct diff_queue_struct
*q
= &diff_queued_diff
;
5834 QSORT(q
->queue
, q
->nr
, diffnamecmp
);
5837 void diffcore_std(struct diff_options
*options
)
5839 /* NOTE please keep the following in sync with diff_tree_combined() */
5840 if (options
->skip_stat_unmatch
)
5841 diffcore_skip_stat_unmatch(options
);
5842 if (!options
->found_follow
) {
5843 /* See try_to_follow_renames() in tree-diff.c */
5844 if (options
->break_opt
!= -1)
5845 diffcore_break(options
->break_opt
);
5846 if (options
->detect_rename
)
5847 diffcore_rename(options
);
5848 if (options
->break_opt
!= -1)
5849 diffcore_merge_broken();
5851 if (options
->pickaxe
)
5852 diffcore_pickaxe(options
);
5853 if (options
->orderfile
)
5854 diffcore_order(options
->orderfile
);
5855 if (!options
->found_follow
)
5856 /* See try_to_follow_renames() in tree-diff.c */
5857 diff_resolve_rename_copy();
5858 diffcore_apply_filter(options
);
5860 if (diff_queued_diff
.nr
&& !DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
5861 DIFF_OPT_SET(options
, HAS_CHANGES
);
5863 DIFF_OPT_CLR(options
, HAS_CHANGES
);
5865 options
->found_follow
= 0;
5868 int diff_result_code(struct diff_options
*opt
, int status
)
5872 diff_warn_rename_limit("diff.renameLimit",
5873 opt
->needed_rename_limit
,
5874 opt
->degraded_cc_to_c
);
5875 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
5876 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
5878 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
5879 DIFF_OPT_TST(opt
, HAS_CHANGES
))
5881 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
5882 DIFF_OPT_TST(opt
, CHECK_FAILED
))
5887 int diff_can_quit_early(struct diff_options
*opt
)
5889 return (DIFF_OPT_TST(opt
, QUICK
) &&
5891 DIFF_OPT_TST(opt
, HAS_CHANGES
));
5895 * Shall changes to this submodule be ignored?
5897 * Submodule changes can be configured to be ignored separately for each path,
5898 * but that configuration can be overridden from the command line.
5900 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
5903 struct diff_flags orig_flags
= options
->flags
;
5904 if (!DIFF_OPT_TST(options
, OVERRIDE_SUBMODULE_CONFIG
))
5905 set_diffopt_flags_from_submodule_config(options
, path
);
5906 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
))
5908 options
->flags
= orig_flags
;
5912 void diff_addremove(struct diff_options
*options
,
5913 int addremove
, unsigned mode
,
5914 const struct object_id
*oid
,
5916 const char *concatpath
, unsigned dirty_submodule
)
5918 struct diff_filespec
*one
, *two
;
5920 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
5923 /* This may look odd, but it is a preparation for
5924 * feeding "there are unchanged files which should
5925 * not produce diffs, but when you are doing copy
5926 * detection you would need them, so here they are"
5927 * entries to the diff-core. They will be prefixed
5928 * with something like '=' or '*' (I haven't decided
5929 * which but should not make any difference).
5930 * Feeding the same new and old to diff_change()
5931 * also has the same effect.
5932 * Before the final output happens, they are pruned after
5933 * merged into rename/copy pairs as appropriate.
5935 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
5936 addremove
= (addremove
== '+' ? '-' :
5937 addremove
== '-' ? '+' : addremove
);
5939 if (options
->prefix
&&
5940 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
5943 one
= alloc_filespec(concatpath
);
5944 two
= alloc_filespec(concatpath
);
5946 if (addremove
!= '+')
5947 fill_filespec(one
, oid
, oid_valid
, mode
);
5948 if (addremove
!= '-') {
5949 fill_filespec(two
, oid
, oid_valid
, mode
);
5950 two
->dirty_submodule
= dirty_submodule
;
5953 diff_queue(&diff_queued_diff
, one
, two
);
5954 if (!DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
5955 DIFF_OPT_SET(options
, HAS_CHANGES
);
5958 void diff_change(struct diff_options
*options
,
5959 unsigned old_mode
, unsigned new_mode
,
5960 const struct object_id
*old_oid
,
5961 const struct object_id
*new_oid
,
5962 int old_oid_valid
, int new_oid_valid
,
5963 const char *concatpath
,
5964 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
5966 struct diff_filespec
*one
, *two
;
5967 struct diff_filepair
*p
;
5969 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
5970 is_submodule_ignored(concatpath
, options
))
5973 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
5974 SWAP(old_mode
, new_mode
);
5975 SWAP(old_oid
, new_oid
);
5976 SWAP(old_oid_valid
, new_oid_valid
);
5977 SWAP(old_dirty_submodule
, new_dirty_submodule
);
5980 if (options
->prefix
&&
5981 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
5984 one
= alloc_filespec(concatpath
);
5985 two
= alloc_filespec(concatpath
);
5986 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
5987 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
5988 one
->dirty_submodule
= old_dirty_submodule
;
5989 two
->dirty_submodule
= new_dirty_submodule
;
5990 p
= diff_queue(&diff_queued_diff
, one
, two
);
5992 if (DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
5995 if (DIFF_OPT_TST(options
, QUICK
) && options
->skip_stat_unmatch
&&
5996 !diff_filespec_check_stat_unmatch(p
))
5999 DIFF_OPT_SET(options
, HAS_CHANGES
);
6002 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
6004 struct diff_filepair
*pair
;
6005 struct diff_filespec
*one
, *two
;
6007 if (options
->prefix
&&
6008 strncmp(path
, options
->prefix
, options
->prefix_length
))
6011 one
= alloc_filespec(path
);
6012 two
= alloc_filespec(path
);
6013 pair
= diff_queue(&diff_queued_diff
, one
, two
);
6014 pair
->is_unmerged
= 1;
6018 static char *run_textconv(const char *pgm
, struct diff_filespec
*spec
,
6021 struct diff_tempfile
*temp
;
6022 const char *argv
[3];
6023 const char **arg
= argv
;
6024 struct child_process child
= CHILD_PROCESS_INIT
;
6025 struct strbuf buf
= STRBUF_INIT
;
6028 temp
= prepare_temp_file(spec
->path
, spec
);
6030 *arg
++ = temp
->name
;
6033 child
.use_shell
= 1;
6036 if (start_command(&child
)) {
6041 if (strbuf_read(&buf
, child
.out
, 0) < 0)
6042 err
= error("error reading from textconv command '%s'", pgm
);
6045 if (finish_command(&child
) || err
) {
6046 strbuf_release(&buf
);
6052 return strbuf_detach(&buf
, outsize
);
6055 size_t fill_textconv(struct userdiff_driver
*driver
,
6056 struct diff_filespec
*df
,
6062 if (!DIFF_FILE_VALID(df
)) {
6066 if (diff_populate_filespec(df
, 0))
6067 die("unable to read files to diff");
6072 if (!driver
->textconv
)
6073 die("BUG: fill_textconv called with non-textconv driver");
6075 if (driver
->textconv_cache
&& df
->oid_valid
) {
6076 *outbuf
= notes_cache_get(driver
->textconv_cache
,
6083 *outbuf
= run_textconv(driver
->textconv
, df
, &size
);
6085 die("unable to read files to diff");
6087 if (driver
->textconv_cache
&& df
->oid_valid
) {
6088 /* ignore errors, as we might be in a readonly repository */
6089 notes_cache_put(driver
->textconv_cache
, &df
->oid
, *outbuf
,
6092 * we could save up changes and flush them all at the end,
6093 * but we would need an extra call after all diffing is done.
6094 * Since generating a cache entry is the slow path anyway,
6095 * this extra overhead probably isn't a big deal.
6097 notes_cache_write(driver
->textconv_cache
);
6103 int textconv_object(const char *path
,
6105 const struct object_id
*oid
,
6108 unsigned long *buf_size
)
6110 struct diff_filespec
*df
;
6111 struct userdiff_driver
*textconv
;
6113 df
= alloc_filespec(path
);
6114 fill_filespec(df
, oid
, oid_valid
, mode
);
6115 textconv
= get_textconv(df
);
6121 *buf_size
= fill_textconv(textconv
, df
, buf
);
6126 void setup_diff_pager(struct diff_options
*opt
)
6129 * If the user asked for our exit code, then either they want --quiet
6130 * or --exit-code. We should definitely not bother with a pager in the
6131 * former case, as we will generate no output. Since we still properly
6132 * report our exit code even when a pager is run, we _could_ run a
6133 * pager with --exit-code. But since we have not done so historically,
6134 * and because it is easy to find people oneline advising "git diff
6135 * --exit-code" in hooks and other scripts, we do not do so.
6137 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
6138 check_pager_config("diff") != 0)