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 options
->flags
.dirstat_by_line
= 0;
128 options
->flags
.dirstat_by_file
= 0;
129 } else if (!strcmp(p
, "lines")) {
130 options
->flags
.dirstat_by_line
= 1;
131 options
->flags
.dirstat_by_file
= 0;
132 } else if (!strcmp(p
, "files")) {
133 options
->flags
.dirstat_by_line
= 0;
134 options
->flags
.dirstat_by_file
= 1;
135 } else if (!strcmp(p
, "noncumulative")) {
136 options
->flags
.dirstat_cumulative
= 0;
137 } else if (!strcmp(p
, "cumulative")) {
138 options
->flags
.dirstat_cumulative
= 1;
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
= DIFF_DETECT_RENAME
;
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 moved_entry_cmp(const struct diff_options
*diffopt
,
711 const struct moved_entry
*a
,
712 const struct moved_entry
*b
,
715 return !xdiff_compare_lines(a
->es
->line
, a
->es
->len
,
716 b
->es
->line
, b
->es
->len
,
720 static struct moved_entry
*prepare_entry(struct diff_options
*o
,
723 struct moved_entry
*ret
= xmalloc(sizeof(*ret
));
724 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[line_no
];
726 ret
->ent
.hash
= xdiff_hash_string(l
->line
, l
->len
, o
->xdl_opts
);
728 ret
->next_line
= NULL
;
733 static void add_lines_to_move_detection(struct diff_options
*o
,
734 struct hashmap
*add_lines
,
735 struct hashmap
*del_lines
)
737 struct moved_entry
*prev_line
= NULL
;
740 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
742 struct moved_entry
*key
;
744 switch (o
->emitted_symbols
->buf
[n
].s
) {
745 case DIFF_SYMBOL_PLUS
:
748 case DIFF_SYMBOL_MINUS
:
756 key
= prepare_entry(o
, n
);
757 if (prev_line
&& prev_line
->es
->s
== o
->emitted_symbols
->buf
[n
].s
)
758 prev_line
->next_line
= key
;
760 hashmap_add(hm
, key
);
765 static int shrink_potential_moved_blocks(struct moved_entry
**pmb
,
770 /* Shrink the set of potential block to the remaining running */
771 for (lp
= 0, rp
= pmb_nr
- 1; lp
<= rp
;) {
772 while (lp
< pmb_nr
&& pmb
[lp
])
774 /* lp points at the first NULL now */
776 while (rp
> -1 && !pmb
[rp
])
778 /* rp points at the last non-NULL */
780 if (lp
< pmb_nr
&& rp
> -1 && lp
< rp
) {
788 /* Remember the number of running sets */
793 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
795 * Otherwise, if the last block has fewer alphanumeric characters than
796 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
799 * The last block consists of the (n - block_length)'th line up to but not
800 * including the nth line.
802 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
803 * Think of a way to unify them.
805 static void adjust_last_block(struct diff_options
*o
, int n
, int block_length
)
807 int i
, alnum_count
= 0;
808 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
810 for (i
= 1; i
< block_length
+ 1; i
++) {
811 const char *c
= o
->emitted_symbols
->buf
[n
- i
].line
;
816 if (alnum_count
>= COLOR_MOVED_MIN_ALNUM_COUNT
)
820 for (i
= 1; i
< block_length
+ 1; i
++)
821 o
->emitted_symbols
->buf
[n
- i
].flags
&= ~DIFF_SYMBOL_MOVED_LINE
;
824 /* Find blocks of moved code, delegate actual coloring decision to helper */
825 static void mark_color_as_moved(struct diff_options
*o
,
826 struct hashmap
*add_lines
,
827 struct hashmap
*del_lines
)
829 struct moved_entry
**pmb
= NULL
; /* potentially moved blocks */
830 int pmb_nr
= 0, pmb_alloc
= 0;
831 int n
, flipped_block
= 1, block_length
= 0;
834 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
835 struct hashmap
*hm
= NULL
;
836 struct moved_entry
*key
;
837 struct moved_entry
*match
= NULL
;
838 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
842 case DIFF_SYMBOL_PLUS
:
844 key
= prepare_entry(o
, n
);
845 match
= hashmap_get(hm
, key
, o
);
848 case DIFF_SYMBOL_MINUS
:
850 key
= prepare_entry(o
, n
);
851 match
= hashmap_get(hm
, key
, o
);
859 adjust_last_block(o
, n
, block_length
);
865 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
867 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
870 /* Check any potential block runs, advance each or nullify */
871 for (i
= 0; i
< pmb_nr
; i
++) {
872 struct moved_entry
*p
= pmb
[i
];
873 struct moved_entry
*pnext
= (p
&& p
->next_line
) ?
875 if (pnext
&& !hm
->cmpfn(o
, pnext
, match
, NULL
)) {
876 pmb
[i
] = p
->next_line
;
882 pmb_nr
= shrink_potential_moved_blocks(pmb
, pmb_nr
);
886 * The current line is the start of a new block.
887 * Setup the set of potential blocks.
889 for (; match
; match
= hashmap_get_next(hm
, match
)) {
890 ALLOC_GROW(pmb
, pmb_nr
+ 1, pmb_alloc
);
891 pmb
[pmb_nr
++] = match
;
894 flipped_block
= (flipped_block
+ 1) % 2;
896 adjust_last_block(o
, n
, block_length
);
903 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_ALT
;
905 adjust_last_block(o
, n
, block_length
);
910 #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
911 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
912 static void dim_moved_lines(struct diff_options
*o
)
915 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
916 struct emitted_diff_symbol
*prev
= (n
!= 0) ?
917 &o
->emitted_symbols
->buf
[n
- 1] : NULL
;
918 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
919 struct emitted_diff_symbol
*next
=
920 (n
< o
->emitted_symbols
->nr
- 1) ?
921 &o
->emitted_symbols
->buf
[n
+ 1] : NULL
;
923 /* Not a plus or minus line? */
924 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
)
927 /* Not a moved line? */
928 if (!(l
->flags
& DIFF_SYMBOL_MOVED_LINE
))
932 * If prev or next are not a plus or minus line,
933 * pretend they don't exist
935 if (prev
&& prev
->s
!= DIFF_SYMBOL_PLUS
&&
936 prev
->s
!= DIFF_SYMBOL_MINUS
)
938 if (next
&& next
->s
!= DIFF_SYMBOL_PLUS
&&
939 next
->s
!= DIFF_SYMBOL_MINUS
)
942 /* Inside a block? */
944 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
945 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
)) &&
947 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
948 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
))) {
949 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
953 /* Check if we are at an interesting bound: */
954 if (prev
&& (prev
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
955 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
956 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
958 if (next
&& (next
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
959 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
960 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
964 * The boundary to prev and next are not interesting,
965 * so this line is not interesting as a whole
967 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
971 static void emit_line_ws_markup(struct diff_options
*o
,
972 const char *set
, const char *reset
,
973 const char *line
, int len
, char sign
,
974 unsigned ws_rule
, int blank_at_eof
)
976 const char *ws
= NULL
;
978 if (o
->ws_error_highlight
& ws_rule
) {
979 ws
= diff_get_color_opt(o
, DIFF_WHITESPACE
);
985 emit_line_0(o
, set
, reset
, sign
, line
, len
);
986 else if (blank_at_eof
)
987 /* Blank line at EOF - paint '+' as well */
988 emit_line_0(o
, ws
, reset
, sign
, line
, len
);
990 /* Emit just the prefix, then the rest. */
991 emit_line_0(o
, set
, reset
, sign
, "", 0);
992 ws_check_emit(line
, len
, ws_rule
,
993 o
->file
, set
, reset
, ws
);
997 static void emit_diff_symbol_from_struct(struct diff_options
*o
,
998 struct emitted_diff_symbol
*eds
)
1000 static const char *nneof
= " No newline at end of file\n";
1001 const char *context
, *reset
, *set
, *meta
, *fraginfo
;
1002 struct strbuf sb
= STRBUF_INIT
;
1004 enum diff_symbol s
= eds
->s
;
1005 const char *line
= eds
->line
;
1007 unsigned flags
= eds
->flags
;
1010 case DIFF_SYMBOL_NO_LF_EOF
:
1011 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1012 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1013 putc('\n', o
->file
);
1014 emit_line_0(o
, context
, reset
, '\\',
1015 nneof
, strlen(nneof
));
1017 case DIFF_SYMBOL_SUBMODULE_HEADER
:
1018 case DIFF_SYMBOL_SUBMODULE_ERROR
:
1019 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
:
1020 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
:
1021 case DIFF_SYMBOL_SUMMARY
:
1022 case DIFF_SYMBOL_STATS_LINE
:
1023 case DIFF_SYMBOL_BINARY_DIFF_BODY
:
1024 case DIFF_SYMBOL_CONTEXT_FRAGINFO
:
1025 emit_line(o
, "", "", line
, len
);
1027 case DIFF_SYMBOL_CONTEXT_INCOMPLETE
:
1028 case DIFF_SYMBOL_CONTEXT_MARKER
:
1029 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1030 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1031 emit_line(o
, context
, reset
, line
, len
);
1033 case DIFF_SYMBOL_SEPARATOR
:
1034 fprintf(o
->file
, "%s%c",
1035 diff_line_prefix(o
),
1036 o
->line_termination
);
1038 case DIFF_SYMBOL_CONTEXT
:
1039 set
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1040 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1041 emit_line_ws_markup(o
, set
, reset
, line
, len
, ' ',
1042 flags
& (DIFF_SYMBOL_CONTENT_WS_MASK
), 0);
1044 case DIFF_SYMBOL_PLUS
:
1045 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1046 DIFF_SYMBOL_MOVED_LINE_ALT
|
1047 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1048 case DIFF_SYMBOL_MOVED_LINE
|
1049 DIFF_SYMBOL_MOVED_LINE_ALT
|
1050 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1051 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT_DIM
);
1053 case DIFF_SYMBOL_MOVED_LINE
|
1054 DIFF_SYMBOL_MOVED_LINE_ALT
:
1055 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT
);
1057 case DIFF_SYMBOL_MOVED_LINE
|
1058 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1059 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_DIM
);
1061 case DIFF_SYMBOL_MOVED_LINE
:
1062 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED
);
1065 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1067 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1068 emit_line_ws_markup(o
, set
, reset
, line
, len
, '+',
1069 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
,
1070 flags
& DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
);
1072 case DIFF_SYMBOL_MINUS
:
1073 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1074 DIFF_SYMBOL_MOVED_LINE_ALT
|
1075 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1076 case DIFF_SYMBOL_MOVED_LINE
|
1077 DIFF_SYMBOL_MOVED_LINE_ALT
|
1078 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1079 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT_DIM
);
1081 case DIFF_SYMBOL_MOVED_LINE
|
1082 DIFF_SYMBOL_MOVED_LINE_ALT
:
1083 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT
);
1085 case DIFF_SYMBOL_MOVED_LINE
|
1086 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1087 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_DIM
);
1089 case DIFF_SYMBOL_MOVED_LINE
:
1090 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED
);
1093 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1095 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1096 emit_line_ws_markup(o
, set
, reset
, line
, len
, '-',
1097 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
, 0);
1099 case DIFF_SYMBOL_WORDS_PORCELAIN
:
1100 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1101 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1102 emit_line(o
, context
, reset
, line
, len
);
1103 fputs("~\n", o
->file
);
1105 case DIFF_SYMBOL_WORDS
:
1106 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1107 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1109 * Skip the prefix character, if any. With
1110 * diff_suppress_blank_empty, there may be
1113 if (line
[0] != '\n') {
1117 emit_line(o
, context
, reset
, line
, len
);
1119 case DIFF_SYMBOL_FILEPAIR_PLUS
:
1120 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1121 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1122 fprintf(o
->file
, "%s%s+++ %s%s%s\n", diff_line_prefix(o
), meta
,
1124 strchr(line
, ' ') ? "\t" : "");
1126 case DIFF_SYMBOL_FILEPAIR_MINUS
:
1127 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1128 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1129 fprintf(o
->file
, "%s%s--- %s%s%s\n", diff_line_prefix(o
), meta
,
1131 strchr(line
, ' ') ? "\t" : "");
1133 case DIFF_SYMBOL_BINARY_FILES
:
1134 case DIFF_SYMBOL_HEADER
:
1135 fprintf(o
->file
, "%s", line
);
1137 case DIFF_SYMBOL_BINARY_DIFF_HEADER
:
1138 fprintf(o
->file
, "%sGIT binary patch\n", diff_line_prefix(o
));
1140 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
:
1141 fprintf(o
->file
, "%sdelta %s\n", diff_line_prefix(o
), line
);
1143 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
:
1144 fprintf(o
->file
, "%sliteral %s\n", diff_line_prefix(o
), line
);
1146 case DIFF_SYMBOL_BINARY_DIFF_FOOTER
:
1147 fputs(diff_line_prefix(o
), o
->file
);
1148 fputc('\n', o
->file
);
1150 case DIFF_SYMBOL_REWRITE_DIFF
:
1151 fraginfo
= diff_get_color(o
->use_color
, DIFF_FRAGINFO
);
1152 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1153 emit_line(o
, fraginfo
, reset
, line
, len
);
1155 case DIFF_SYMBOL_SUBMODULE_ADD
:
1156 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1157 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1158 emit_line(o
, set
, reset
, line
, len
);
1160 case DIFF_SYMBOL_SUBMODULE_DEL
:
1161 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1162 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1163 emit_line(o
, set
, reset
, line
, len
);
1165 case DIFF_SYMBOL_SUBMODULE_UNTRACKED
:
1166 fprintf(o
->file
, "%sSubmodule %s contains untracked content\n",
1167 diff_line_prefix(o
), line
);
1169 case DIFF_SYMBOL_SUBMODULE_MODIFIED
:
1170 fprintf(o
->file
, "%sSubmodule %s contains modified content\n",
1171 diff_line_prefix(o
), line
);
1173 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
:
1174 emit_line(o
, "", "", " 0 files changed\n",
1175 strlen(" 0 files changed\n"));
1177 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV
:
1178 emit_line(o
, "", "", " ...\n", strlen(" ...\n"));
1180 case DIFF_SYMBOL_WORD_DIFF
:
1181 fprintf(o
->file
, "%.*s", len
, line
);
1183 case DIFF_SYMBOL_STAT_SEP
:
1184 fputs(o
->stat_sep
, o
->file
);
1187 die("BUG: unknown diff symbol");
1189 strbuf_release(&sb
);
1192 static void emit_diff_symbol(struct diff_options
*o
, enum diff_symbol s
,
1193 const char *line
, int len
, unsigned flags
)
1195 struct emitted_diff_symbol e
= {line
, len
, flags
, s
};
1197 if (o
->emitted_symbols
)
1198 append_emitted_diff_symbol(o
, &e
);
1200 emit_diff_symbol_from_struct(o
, &e
);
1203 void diff_emit_submodule_del(struct diff_options
*o
, const char *line
)
1205 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_DEL
, line
, strlen(line
), 0);
1208 void diff_emit_submodule_add(struct diff_options
*o
, const char *line
)
1210 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ADD
, line
, strlen(line
), 0);
1213 void diff_emit_submodule_untracked(struct diff_options
*o
, const char *path
)
1215 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
1216 path
, strlen(path
), 0);
1219 void diff_emit_submodule_modified(struct diff_options
*o
, const char *path
)
1221 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_MODIFIED
,
1222 path
, strlen(path
), 0);
1225 void diff_emit_submodule_header(struct diff_options
*o
, const char *header
)
1227 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_HEADER
,
1228 header
, strlen(header
), 0);
1231 void diff_emit_submodule_error(struct diff_options
*o
, const char *err
)
1233 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ERROR
, err
, strlen(err
), 0);
1236 void diff_emit_submodule_pipethrough(struct diff_options
*o
,
1237 const char *line
, int len
)
1239 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
, line
, len
, 0);
1242 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
1244 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
1245 ecbdata
->blank_at_eof_in_preimage
&&
1246 ecbdata
->blank_at_eof_in_postimage
&&
1247 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
1248 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
1250 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
1253 static void emit_add_line(const char *reset
,
1254 struct emit_callback
*ecbdata
,
1255 const char *line
, int len
)
1257 unsigned flags
= WSEH_NEW
| ecbdata
->ws_rule
;
1258 if (new_blank_line_at_eof(ecbdata
, line
, len
))
1259 flags
|= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
;
1261 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_PLUS
, line
, len
, flags
);
1264 static void emit_del_line(const char *reset
,
1265 struct emit_callback
*ecbdata
,
1266 const char *line
, int len
)
1268 unsigned flags
= WSEH_OLD
| ecbdata
->ws_rule
;
1269 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_MINUS
, line
, len
, flags
);
1272 static void emit_context_line(const char *reset
,
1273 struct emit_callback
*ecbdata
,
1274 const char *line
, int len
)
1276 unsigned flags
= WSEH_CONTEXT
| ecbdata
->ws_rule
;
1277 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_CONTEXT
, line
, len
, flags
);
1280 static void emit_hunk_header(struct emit_callback
*ecbdata
,
1281 const char *line
, int len
)
1283 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
1284 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
1285 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
1286 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1287 static const char atat
[2] = { '@', '@' };
1288 const char *cp
, *ep
;
1289 struct strbuf msgbuf
= STRBUF_INIT
;
1294 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1295 * it always is at least 10 bytes long.
1298 memcmp(line
, atat
, 2) ||
1299 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
1300 emit_diff_symbol(ecbdata
->opt
,
1301 DIFF_SYMBOL_CONTEXT_MARKER
, line
, len
, 0);
1304 ep
+= 2; /* skip over @@ */
1306 /* The hunk header in fraginfo color */
1307 strbuf_addstr(&msgbuf
, frag
);
1308 strbuf_add(&msgbuf
, line
, ep
- line
);
1309 strbuf_addstr(&msgbuf
, reset
);
1315 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
1318 /* blank before the func header */
1319 for (cp
= ep
; ep
- line
< len
; ep
++)
1320 if (*ep
!= ' ' && *ep
!= '\t')
1323 strbuf_addstr(&msgbuf
, context
);
1324 strbuf_add(&msgbuf
, cp
, ep
- cp
);
1325 strbuf_addstr(&msgbuf
, reset
);
1328 if (ep
< line
+ len
) {
1329 strbuf_addstr(&msgbuf
, func
);
1330 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
1331 strbuf_addstr(&msgbuf
, reset
);
1334 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
1335 strbuf_complete_line(&msgbuf
);
1336 emit_diff_symbol(ecbdata
->opt
,
1337 DIFF_SYMBOL_CONTEXT_FRAGINFO
, msgbuf
.buf
, msgbuf
.len
, 0);
1338 strbuf_release(&msgbuf
);
1341 static struct diff_tempfile
*claim_diff_tempfile(void) {
1343 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
1344 if (!diff_temp
[i
].name
)
1345 return diff_temp
+ i
;
1346 die("BUG: diff is failing to clean up its tempfiles");
1349 static void remove_tempfile(void)
1352 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
1353 if (is_tempfile_active(diff_temp
[i
].tempfile
))
1354 delete_tempfile(&diff_temp
[i
].tempfile
);
1355 diff_temp
[i
].name
= NULL
;
1359 static void add_line_count(struct strbuf
*out
, int count
)
1363 strbuf_addstr(out
, "0,0");
1366 strbuf_addstr(out
, "1");
1369 strbuf_addf(out
, "1,%d", count
);
1374 static void emit_rewrite_lines(struct emit_callback
*ecb
,
1375 int prefix
, const char *data
, int size
)
1377 const char *endp
= NULL
;
1378 const char *reset
= diff_get_color(ecb
->color_diff
, DIFF_RESET
);
1383 endp
= memchr(data
, '\n', size
);
1384 len
= endp
? (endp
- data
+ 1) : size
;
1385 if (prefix
!= '+') {
1386 ecb
->lno_in_preimage
++;
1387 emit_del_line(reset
, ecb
, data
, len
);
1389 ecb
->lno_in_postimage
++;
1390 emit_add_line(reset
, ecb
, data
, len
);
1396 emit_diff_symbol(ecb
->opt
, DIFF_SYMBOL_NO_LF_EOF
, NULL
, 0, 0);
1399 static void emit_rewrite_diff(const char *name_a
,
1401 struct diff_filespec
*one
,
1402 struct diff_filespec
*two
,
1403 struct userdiff_driver
*textconv_one
,
1404 struct userdiff_driver
*textconv_two
,
1405 struct diff_options
*o
)
1408 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
1409 const char *a_prefix
, *b_prefix
;
1410 char *data_one
, *data_two
;
1411 size_t size_one
, size_two
;
1412 struct emit_callback ecbdata
;
1413 struct strbuf out
= STRBUF_INIT
;
1415 if (diff_mnemonic_prefix
&& o
->flags
.reverse_diff
) {
1416 a_prefix
= o
->b_prefix
;
1417 b_prefix
= o
->a_prefix
;
1419 a_prefix
= o
->a_prefix
;
1420 b_prefix
= o
->b_prefix
;
1423 name_a
+= (*name_a
== '/');
1424 name_b
+= (*name_b
== '/');
1426 strbuf_reset(&a_name
);
1427 strbuf_reset(&b_name
);
1428 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
1429 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
1431 size_one
= fill_textconv(textconv_one
, one
, &data_one
);
1432 size_two
= fill_textconv(textconv_two
, two
, &data_two
);
1434 memset(&ecbdata
, 0, sizeof(ecbdata
));
1435 ecbdata
.color_diff
= want_color(o
->use_color
);
1436 ecbdata
.ws_rule
= whitespace_rule(name_b
);
1438 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
1440 mf1
.ptr
= (char *)data_one
;
1441 mf2
.ptr
= (char *)data_two
;
1442 mf1
.size
= size_one
;
1443 mf2
.size
= size_two
;
1444 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1446 ecbdata
.lno_in_preimage
= 1;
1447 ecbdata
.lno_in_postimage
= 1;
1449 lc_a
= count_lines(data_one
, size_one
);
1450 lc_b
= count_lines(data_two
, size_two
);
1452 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
1453 a_name
.buf
, a_name
.len
, 0);
1454 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
1455 b_name
.buf
, b_name
.len
, 0);
1457 strbuf_addstr(&out
, "@@ -");
1458 if (!o
->irreversible_delete
)
1459 add_line_count(&out
, lc_a
);
1461 strbuf_addstr(&out
, "?,?");
1462 strbuf_addstr(&out
, " +");
1463 add_line_count(&out
, lc_b
);
1464 strbuf_addstr(&out
, " @@\n");
1465 emit_diff_symbol(o
, DIFF_SYMBOL_REWRITE_DIFF
, out
.buf
, out
.len
, 0);
1466 strbuf_release(&out
);
1468 if (lc_a
&& !o
->irreversible_delete
)
1469 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
1471 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
1473 free((char *)data_one
);
1475 free((char *)data_two
);
1478 struct diff_words_buffer
{
1480 unsigned long alloc
;
1481 struct diff_words_orig
{
1482 const char *begin
, *end
;
1484 int orig_nr
, orig_alloc
;
1487 static void diff_words_append(char *line
, unsigned long len
,
1488 struct diff_words_buffer
*buffer
)
1490 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
1493 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
1494 buffer
->text
.size
+= len
;
1495 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
1498 struct diff_words_style_elem
{
1501 const char *color
; /* NULL; filled in by the setup code if
1502 * color is enabled */
1505 struct diff_words_style
{
1506 enum diff_words_type type
;
1507 struct diff_words_style_elem new_word
, old_word
, ctx
;
1508 const char *newline
;
1511 static struct diff_words_style diff_words_styles
[] = {
1512 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1513 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1514 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
1517 struct diff_words_data
{
1518 struct diff_words_buffer minus
, plus
;
1519 const char *current_plus
;
1521 struct diff_options
*opt
;
1522 regex_t
*word_regex
;
1523 enum diff_words_type type
;
1524 struct diff_words_style
*style
;
1527 static int fn_out_diff_words_write_helper(struct diff_options
*o
,
1528 struct diff_words_style_elem
*st_el
,
1529 const char *newline
,
1530 size_t count
, const char *buf
)
1533 struct strbuf sb
= STRBUF_INIT
;
1536 char *p
= memchr(buf
, '\n', count
);
1538 strbuf_addstr(&sb
, diff_line_prefix(o
));
1541 const char *reset
= st_el
->color
&& *st_el
->color
?
1542 GIT_COLOR_RESET
: NULL
;
1543 if (st_el
->color
&& *st_el
->color
)
1544 strbuf_addstr(&sb
, st_el
->color
);
1545 strbuf_addstr(&sb
, st_el
->prefix
);
1546 strbuf_add(&sb
, buf
, p
? p
- buf
: count
);
1547 strbuf_addstr(&sb
, st_el
->suffix
);
1549 strbuf_addstr(&sb
, reset
);
1554 strbuf_addstr(&sb
, newline
);
1555 count
-= p
+ 1 - buf
;
1559 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1567 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1569 strbuf_release(&sb
);
1574 * '--color-words' algorithm can be described as:
1576 * 1. collect the minus/plus lines of a diff hunk, divided into
1577 * minus-lines and plus-lines;
1579 * 2. break both minus-lines and plus-lines into words and
1580 * place them into two mmfile_t with one word for each line;
1582 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1584 * And for the common parts of the both file, we output the plus side text.
1585 * diff_words->current_plus is used to trace the current position of the plus file
1586 * which printed. diff_words->last_minus is used to trace the last minus word
1589 * For '--graph' to work with '--color-words', we need to output the graph prefix
1590 * on each line of color words output. Generally, there are two conditions on
1591 * which we should output the prefix.
1593 * 1. diff_words->last_minus == 0 &&
1594 * diff_words->current_plus == diff_words->plus.text.ptr
1596 * that is: the plus text must start as a new line, and if there is no minus
1597 * word printed, a graph prefix must be printed.
1599 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1600 * *(diff_words->current_plus - 1) == '\n'
1602 * that is: a graph prefix must be printed following a '\n'
1604 static int color_words_output_graph_prefix(struct diff_words_data
*diff_words
)
1606 if ((diff_words
->last_minus
== 0 &&
1607 diff_words
->current_plus
== diff_words
->plus
.text
.ptr
) ||
1608 (diff_words
->current_plus
> diff_words
->plus
.text
.ptr
&&
1609 *(diff_words
->current_plus
- 1) == '\n')) {
1616 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
1618 struct diff_words_data
*diff_words
= priv
;
1619 struct diff_words_style
*style
= diff_words
->style
;
1620 int minus_first
, minus_len
, plus_first
, plus_len
;
1621 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
1622 struct diff_options
*opt
= diff_words
->opt
;
1623 const char *line_prefix
;
1625 if (line
[0] != '@' || parse_hunk_header(line
, len
,
1626 &minus_first
, &minus_len
, &plus_first
, &plus_len
))
1630 line_prefix
= diff_line_prefix(opt
);
1632 /* POSIX requires that first be decremented by one if len == 0... */
1634 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
1636 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
1638 minus_begin
= minus_end
=
1639 diff_words
->minus
.orig
[minus_first
].end
;
1642 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
1643 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
1645 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
1647 if (color_words_output_graph_prefix(diff_words
)) {
1648 fputs(line_prefix
, diff_words
->opt
->file
);
1650 if (diff_words
->current_plus
!= plus_begin
) {
1651 fn_out_diff_words_write_helper(diff_words
->opt
,
1652 &style
->ctx
, style
->newline
,
1653 plus_begin
- diff_words
->current_plus
,
1654 diff_words
->current_plus
);
1656 if (minus_begin
!= minus_end
) {
1657 fn_out_diff_words_write_helper(diff_words
->opt
,
1658 &style
->old_word
, style
->newline
,
1659 minus_end
- minus_begin
, minus_begin
);
1661 if (plus_begin
!= plus_end
) {
1662 fn_out_diff_words_write_helper(diff_words
->opt
,
1663 &style
->new_word
, style
->newline
,
1664 plus_end
- plus_begin
, plus_begin
);
1667 diff_words
->current_plus
= plus_end
;
1668 diff_words
->last_minus
= minus_first
;
1671 /* This function starts looking at *begin, and returns 0 iff a word was found. */
1672 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
1673 int *begin
, int *end
)
1675 if (word_regex
&& *begin
< buffer
->size
) {
1676 regmatch_t match
[1];
1677 if (!regexec_buf(word_regex
, buffer
->ptr
+ *begin
,
1678 buffer
->size
- *begin
, 1, match
, 0)) {
1679 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
1680 '\n', match
[0].rm_eo
- match
[0].rm_so
);
1681 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
1682 *begin
+= match
[0].rm_so
;
1683 return *begin
>= *end
;
1688 /* find the next word */
1689 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
1691 if (*begin
>= buffer
->size
)
1694 /* find the end of the word */
1696 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
1703 * This function splits the words in buffer->text, stores the list with
1704 * newline separator into out, and saves the offsets of the original words
1707 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
1708 regex_t
*word_regex
)
1716 /* fake an empty "0th" word */
1717 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
1718 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
1719 buffer
->orig_nr
= 1;
1721 for (i
= 0; i
< buffer
->text
.size
; i
++) {
1722 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
1725 /* store original boundaries */
1726 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
1727 buffer
->orig_alloc
);
1728 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
1729 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
1732 /* store one word */
1733 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
1734 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
1735 out
->ptr
[out
->size
+ j
- i
] = '\n';
1736 out
->size
+= j
- i
+ 1;
1742 /* this executes the word diff on the accumulated buffers */
1743 static void diff_words_show(struct diff_words_data
*diff_words
)
1747 mmfile_t minus
, plus
;
1748 struct diff_words_style
*style
= diff_words
->style
;
1750 struct diff_options
*opt
= diff_words
->opt
;
1751 const char *line_prefix
;
1754 line_prefix
= diff_line_prefix(opt
);
1756 /* special case: only removal */
1757 if (!diff_words
->plus
.text
.size
) {
1758 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
1759 line_prefix
, strlen(line_prefix
), 0);
1760 fn_out_diff_words_write_helper(diff_words
->opt
,
1761 &style
->old_word
, style
->newline
,
1762 diff_words
->minus
.text
.size
,
1763 diff_words
->minus
.text
.ptr
);
1764 diff_words
->minus
.text
.size
= 0;
1768 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
1769 diff_words
->last_minus
= 0;
1771 memset(&xpp
, 0, sizeof(xpp
));
1772 memset(&xecfg
, 0, sizeof(xecfg
));
1773 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
1774 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
1776 /* as only the hunk header will be parsed, we need a 0-context */
1778 if (xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
1780 die("unable to generate word diff");
1783 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
1784 diff_words
->plus
.text
.size
) {
1785 if (color_words_output_graph_prefix(diff_words
))
1786 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
1787 line_prefix
, strlen(line_prefix
), 0);
1788 fn_out_diff_words_write_helper(diff_words
->opt
,
1789 &style
->ctx
, style
->newline
,
1790 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
1791 - diff_words
->current_plus
, diff_words
->current_plus
);
1793 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
1796 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
1797 static void diff_words_flush(struct emit_callback
*ecbdata
)
1799 struct diff_options
*wo
= ecbdata
->diff_words
->opt
;
1801 if (ecbdata
->diff_words
->minus
.text
.size
||
1802 ecbdata
->diff_words
->plus
.text
.size
)
1803 diff_words_show(ecbdata
->diff_words
);
1805 if (wo
->emitted_symbols
) {
1806 struct diff_options
*o
= ecbdata
->opt
;
1807 struct emitted_diff_symbols
*wol
= wo
->emitted_symbols
;
1812 * Instead of appending each, concat all words to a line?
1814 for (i
= 0; i
< wol
->nr
; i
++)
1815 append_emitted_diff_symbol(o
, &wol
->buf
[i
]);
1817 for (i
= 0; i
< wol
->nr
; i
++)
1818 free((void *)wol
->buf
[i
].line
);
1824 static void diff_filespec_load_driver(struct diff_filespec
*one
)
1826 /* Use already-loaded driver */
1830 if (S_ISREG(one
->mode
))
1831 one
->driver
= userdiff_find_by_path(one
->path
);
1833 /* Fallback to default settings */
1835 one
->driver
= userdiff_find_by_name("default");
1838 static const char *userdiff_word_regex(struct diff_filespec
*one
)
1840 diff_filespec_load_driver(one
);
1841 return one
->driver
->word_regex
;
1844 static void init_diff_words_data(struct emit_callback
*ecbdata
,
1845 struct diff_options
*orig_opts
,
1846 struct diff_filespec
*one
,
1847 struct diff_filespec
*two
)
1850 struct diff_options
*o
= xmalloc(sizeof(struct diff_options
));
1851 memcpy(o
, orig_opts
, sizeof(struct diff_options
));
1853 ecbdata
->diff_words
=
1854 xcalloc(1, sizeof(struct diff_words_data
));
1855 ecbdata
->diff_words
->type
= o
->word_diff
;
1856 ecbdata
->diff_words
->opt
= o
;
1858 if (orig_opts
->emitted_symbols
)
1859 o
->emitted_symbols
=
1860 xcalloc(1, sizeof(struct emitted_diff_symbols
));
1863 o
->word_regex
= userdiff_word_regex(one
);
1865 o
->word_regex
= userdiff_word_regex(two
);
1867 o
->word_regex
= diff_word_regex_cfg
;
1868 if (o
->word_regex
) {
1869 ecbdata
->diff_words
->word_regex
= (regex_t
*)
1870 xmalloc(sizeof(regex_t
));
1871 if (regcomp(ecbdata
->diff_words
->word_regex
,
1873 REG_EXTENDED
| REG_NEWLINE
))
1874 die ("Invalid regular expression: %s",
1877 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
1878 if (o
->word_diff
== diff_words_styles
[i
].type
) {
1879 ecbdata
->diff_words
->style
=
1880 &diff_words_styles
[i
];
1884 if (want_color(o
->use_color
)) {
1885 struct diff_words_style
*st
= ecbdata
->diff_words
->style
;
1886 st
->old_word
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1887 st
->new_word
.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1888 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1892 static void free_diff_words_data(struct emit_callback
*ecbdata
)
1894 if (ecbdata
->diff_words
) {
1895 diff_words_flush(ecbdata
);
1896 free (ecbdata
->diff_words
->opt
->emitted_symbols
);
1897 free (ecbdata
->diff_words
->opt
);
1898 free (ecbdata
->diff_words
->minus
.text
.ptr
);
1899 free (ecbdata
->diff_words
->minus
.orig
);
1900 free (ecbdata
->diff_words
->plus
.text
.ptr
);
1901 free (ecbdata
->diff_words
->plus
.orig
);
1902 if (ecbdata
->diff_words
->word_regex
) {
1903 regfree(ecbdata
->diff_words
->word_regex
);
1904 free(ecbdata
->diff_words
->word_regex
);
1906 FREE_AND_NULL(ecbdata
->diff_words
);
1910 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
1912 if (want_color(diff_use_color
))
1913 return diff_colors
[ix
];
1917 const char *diff_line_prefix(struct diff_options
*opt
)
1919 struct strbuf
*msgbuf
;
1920 if (!opt
->output_prefix
)
1923 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
1927 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
1930 unsigned long allot
;
1936 (void) utf8_width(&cp
, &l
);
1938 break; /* truncated in the middle? */
1943 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
1946 ecbdata
->lno_in_preimage
= 0;
1947 ecbdata
->lno_in_postimage
= 0;
1948 p
= strchr(line
, '-');
1950 return; /* cannot happen */
1951 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
1954 return; /* cannot happen */
1955 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
1958 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
1960 struct emit_callback
*ecbdata
= priv
;
1961 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1962 struct diff_options
*o
= ecbdata
->opt
;
1964 o
->found_changes
= 1;
1966 if (ecbdata
->header
) {
1967 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
1968 ecbdata
->header
->buf
, ecbdata
->header
->len
, 0);
1969 strbuf_reset(ecbdata
->header
);
1970 ecbdata
->header
= NULL
;
1973 if (ecbdata
->label_path
[0]) {
1974 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
1975 ecbdata
->label_path
[0],
1976 strlen(ecbdata
->label_path
[0]), 0);
1977 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
1978 ecbdata
->label_path
[1],
1979 strlen(ecbdata
->label_path
[1]), 0);
1980 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
1983 if (diff_suppress_blank_empty
1984 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
1989 if (line
[0] == '@') {
1990 if (ecbdata
->diff_words
)
1991 diff_words_flush(ecbdata
);
1992 len
= sane_truncate_line(ecbdata
, line
, len
);
1993 find_lno(line
, ecbdata
);
1994 emit_hunk_header(ecbdata
, line
, len
);
1998 if (ecbdata
->diff_words
) {
1999 enum diff_symbol s
=
2000 ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
?
2001 DIFF_SYMBOL_WORDS_PORCELAIN
: DIFF_SYMBOL_WORDS
;
2002 if (line
[0] == '-') {
2003 diff_words_append(line
, len
,
2004 &ecbdata
->diff_words
->minus
);
2006 } else if (line
[0] == '+') {
2007 diff_words_append(line
, len
,
2008 &ecbdata
->diff_words
->plus
);
2010 } else if (starts_with(line
, "\\ ")) {
2012 * Eat the "no newline at eof" marker as if we
2013 * saw a "+" or "-" line with nothing on it,
2014 * and return without diff_words_flush() to
2015 * defer processing. If this is the end of
2016 * preimage, more "+" lines may come after it.
2020 diff_words_flush(ecbdata
);
2021 emit_diff_symbol(o
, s
, line
, len
, 0);
2027 ecbdata
->lno_in_postimage
++;
2028 emit_add_line(reset
, ecbdata
, line
+ 1, len
- 1);
2031 ecbdata
->lno_in_preimage
++;
2032 emit_del_line(reset
, ecbdata
, line
+ 1, len
- 1);
2035 ecbdata
->lno_in_postimage
++;
2036 ecbdata
->lno_in_preimage
++;
2037 emit_context_line(reset
, ecbdata
, line
+ 1, len
- 1);
2040 /* incomplete line at the end */
2041 ecbdata
->lno_in_preimage
++;
2042 emit_diff_symbol(o
, DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
2048 static char *pprint_rename(const char *a
, const char *b
)
2050 const char *old_name
= a
;
2051 const char *new_name
= b
;
2052 struct strbuf name
= STRBUF_INIT
;
2053 int pfx_length
, sfx_length
;
2054 int pfx_adjust_for_slash
;
2055 int len_a
= strlen(a
);
2056 int len_b
= strlen(b
);
2057 int a_midlen
, b_midlen
;
2058 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
2059 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
2061 if (qlen_a
|| qlen_b
) {
2062 quote_c_style(a
, &name
, NULL
, 0);
2063 strbuf_addstr(&name
, " => ");
2064 quote_c_style(b
, &name
, NULL
, 0);
2065 return strbuf_detach(&name
, NULL
);
2068 /* Find common prefix */
2070 while (*old_name
&& *new_name
&& *old_name
== *new_name
) {
2071 if (*old_name
== '/')
2072 pfx_length
= old_name
- a
+ 1;
2077 /* Find common suffix */
2078 old_name
= a
+ len_a
;
2079 new_name
= b
+ len_b
;
2082 * If there is a common prefix, it must end in a slash. In
2083 * that case we let this loop run 1 into the prefix to see the
2086 * If there is no common prefix, we cannot do this as it would
2087 * underrun the input strings.
2089 pfx_adjust_for_slash
= (pfx_length
? 1 : 0);
2090 while (a
+ pfx_length
- pfx_adjust_for_slash
<= old_name
&&
2091 b
+ pfx_length
- pfx_adjust_for_slash
<= new_name
&&
2092 *old_name
== *new_name
) {
2093 if (*old_name
== '/')
2094 sfx_length
= len_a
- (old_name
- a
);
2100 * pfx{mid-a => mid-b}sfx
2101 * {pfx-a => pfx-b}sfx
2102 * pfx{sfx-a => sfx-b}
2105 a_midlen
= len_a
- pfx_length
- sfx_length
;
2106 b_midlen
= len_b
- pfx_length
- sfx_length
;
2112 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
2113 if (pfx_length
+ sfx_length
) {
2114 strbuf_add(&name
, a
, pfx_length
);
2115 strbuf_addch(&name
, '{');
2117 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
2118 strbuf_addstr(&name
, " => ");
2119 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
2120 if (pfx_length
+ sfx_length
) {
2121 strbuf_addch(&name
, '}');
2122 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
2124 return strbuf_detach(&name
, NULL
);
2130 struct diffstat_file
{
2134 unsigned is_unmerged
:1;
2135 unsigned is_binary
:1;
2136 unsigned is_renamed
:1;
2137 unsigned is_interesting
:1;
2138 uintmax_t added
, deleted
;
2142 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
2146 struct diffstat_file
*x
;
2147 x
= xcalloc(1, sizeof(*x
));
2148 ALLOC_GROW(diffstat
->files
, diffstat
->nr
+ 1, diffstat
->alloc
);
2149 diffstat
->files
[diffstat
->nr
++] = x
;
2151 x
->from_name
= xstrdup(name_a
);
2152 x
->name
= xstrdup(name_b
);
2156 x
->from_name
= NULL
;
2157 x
->name
= xstrdup(name_a
);
2162 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
2164 struct diffstat_t
*diffstat
= priv
;
2165 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
2169 else if (line
[0] == '-')
2173 const char mime_boundary_leader
[] = "------------";
2175 static int scale_linear(int it
, int width
, int max_change
)
2180 * make sure that at least one '-' or '+' is printed if
2181 * there is any change to this path. The easiest way is to
2182 * scale linearly as if the alloted width is one column shorter
2183 * than it is, and then add 1 to the result.
2185 return 1 + (it
* (width
- 1) / max_change
);
2188 static void show_graph(struct strbuf
*out
, char ch
, int cnt
,
2189 const char *set
, const char *reset
)
2193 strbuf_addstr(out
, set
);
2194 strbuf_addchars(out
, ch
, cnt
);
2195 strbuf_addstr(out
, reset
);
2198 static void fill_print_name(struct diffstat_file
*file
)
2202 if (file
->print_name
)
2205 if (!file
->is_renamed
) {
2206 struct strbuf buf
= STRBUF_INIT
;
2207 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
2208 pname
= strbuf_detach(&buf
, NULL
);
2211 strbuf_release(&buf
);
2214 pname
= pprint_rename(file
->from_name
, file
->name
);
2216 file
->print_name
= pname
;
2219 static void print_stat_summary_inserts_deletes(struct diff_options
*options
,
2220 int files
, int insertions
, int deletions
)
2222 struct strbuf sb
= STRBUF_INIT
;
2225 assert(insertions
== 0 && deletions
== 0);
2226 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
2232 (files
== 1) ? " %d file changed" : " %d files changed",
2236 * For binary diff, the caller may want to print "x files
2237 * changed" with insertions == 0 && deletions == 0.
2239 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2240 * is probably less confusing (i.e skip over "2 files changed
2241 * but nothing about added/removed lines? Is this a bug in Git?").
2243 if (insertions
|| deletions
== 0) {
2245 (insertions
== 1) ? ", %d insertion(+)" : ", %d insertions(+)",
2249 if (deletions
|| insertions
== 0) {
2251 (deletions
== 1) ? ", %d deletion(-)" : ", %d deletions(-)",
2254 strbuf_addch(&sb
, '\n');
2255 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
2257 strbuf_release(&sb
);
2260 void print_stat_summary(FILE *fp
, int files
,
2261 int insertions
, int deletions
)
2263 struct diff_options o
;
2264 memset(&o
, 0, sizeof(o
));
2267 print_stat_summary_inserts_deletes(&o
, files
, insertions
, deletions
);
2270 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
2272 int i
, len
, add
, del
, adds
= 0, dels
= 0;
2273 uintmax_t max_change
= 0, max_len
= 0;
2274 int total_files
= data
->nr
, count
;
2275 int width
, name_width
, graph_width
, number_width
= 0, bin_width
= 0;
2276 const char *reset
, *add_c
, *del_c
;
2277 int extra_shown
= 0;
2278 const char *line_prefix
= diff_line_prefix(options
);
2279 struct strbuf out
= STRBUF_INIT
;
2284 count
= options
->stat_count
? options
->stat_count
: data
->nr
;
2286 reset
= diff_get_color_opt(options
, DIFF_RESET
);
2287 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
2288 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
2291 * Find the longest filename and max number of changes
2293 for (i
= 0; (i
< count
) && (i
< data
->nr
); i
++) {
2294 struct diffstat_file
*file
= data
->files
[i
];
2295 uintmax_t change
= file
->added
+ file
->deleted
;
2297 if (!file
->is_interesting
&& (change
== 0)) {
2298 count
++; /* not shown == room for one more */
2301 fill_print_name(file
);
2302 len
= strlen(file
->print_name
);
2306 if (file
->is_unmerged
) {
2307 /* "Unmerged" is 8 characters */
2308 bin_width
= bin_width
< 8 ? 8 : bin_width
;
2311 if (file
->is_binary
) {
2312 /* "Bin XXX -> YYY bytes" */
2313 int w
= 14 + decimal_width(file
->added
)
2314 + decimal_width(file
->deleted
);
2315 bin_width
= bin_width
< w
? w
: bin_width
;
2316 /* Display change counts aligned with "Bin" */
2321 if (max_change
< change
)
2322 max_change
= change
;
2324 count
= i
; /* where we can stop scanning in data->files[] */
2327 * We have width = stat_width or term_columns() columns total.
2328 * We want a maximum of min(max_len, stat_name_width) for the name part.
2329 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
2330 * We also need 1 for " " and 4 + decimal_width(max_change)
2331 * for " | NNNN " and one the empty column at the end, altogether
2332 * 6 + decimal_width(max_change).
2334 * If there's not enough space, we will use the smaller of
2335 * stat_name_width (if set) and 5/8*width for the filename,
2336 * and the rest for constant elements + graph part, but no more
2337 * than stat_graph_width for the graph part.
2338 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2339 * for the standard terminal size).
2341 * In other words: stat_width limits the maximum width, and
2342 * stat_name_width fixes the maximum width of the filename,
2343 * and is also used to divide available columns if there
2346 * Binary files are displayed with "Bin XXX -> YYY bytes"
2347 * instead of the change count and graph. This part is treated
2348 * similarly to the graph part, except that it is not
2349 * "scaled". If total width is too small to accommodate the
2350 * guaranteed minimum width of the filename part and the
2351 * separators and this message, this message will "overflow"
2352 * making the line longer than the maximum width.
2355 if (options
->stat_width
== -1)
2356 width
= term_columns() - strlen(line_prefix
);
2358 width
= options
->stat_width
? options
->stat_width
: 80;
2359 number_width
= decimal_width(max_change
) > number_width
?
2360 decimal_width(max_change
) : number_width
;
2362 if (options
->stat_graph_width
== -1)
2363 options
->stat_graph_width
= diff_stat_graph_width
;
2366 * Guarantee 3/8*16==6 for the graph part
2367 * and 5/8*16==10 for the filename part
2369 if (width
< 16 + 6 + number_width
)
2370 width
= 16 + 6 + number_width
;
2373 * First assign sizes that are wanted, ignoring available width.
2374 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2375 * starting from "XXX" should fit in graph_width.
2377 graph_width
= max_change
+ 4 > bin_width
? max_change
: bin_width
- 4;
2378 if (options
->stat_graph_width
&&
2379 options
->stat_graph_width
< graph_width
)
2380 graph_width
= options
->stat_graph_width
;
2382 name_width
= (options
->stat_name_width
> 0 &&
2383 options
->stat_name_width
< max_len
) ?
2384 options
->stat_name_width
: max_len
;
2387 * Adjust adjustable widths not to exceed maximum width
2389 if (name_width
+ number_width
+ 6 + graph_width
> width
) {
2390 if (graph_width
> width
* 3/8 - number_width
- 6) {
2391 graph_width
= width
* 3/8 - number_width
- 6;
2392 if (graph_width
< 6)
2396 if (options
->stat_graph_width
&&
2397 graph_width
> options
->stat_graph_width
)
2398 graph_width
= options
->stat_graph_width
;
2399 if (name_width
> width
- number_width
- 6 - graph_width
)
2400 name_width
= width
- number_width
- 6 - graph_width
;
2402 graph_width
= width
- number_width
- 6 - name_width
;
2406 * From here name_width is the width of the name area,
2407 * and graph_width is the width of the graph area.
2408 * max_change is used to scale graph properly.
2410 for (i
= 0; i
< count
; i
++) {
2411 const char *prefix
= "";
2412 struct diffstat_file
*file
= data
->files
[i
];
2413 char *name
= file
->print_name
;
2414 uintmax_t added
= file
->added
;
2415 uintmax_t deleted
= file
->deleted
;
2418 if (!file
->is_interesting
&& (added
+ deleted
== 0))
2422 * "scale" the filename
2425 name_len
= strlen(name
);
2426 if (name_width
< name_len
) {
2430 name
+= name_len
- len
;
2431 slash
= strchr(name
, '/');
2436 if (file
->is_binary
) {
2437 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2438 strbuf_addf(&out
, " %*s", number_width
, "Bin");
2439 if (!added
&& !deleted
) {
2440 strbuf_addch(&out
, '\n');
2441 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2442 out
.buf
, out
.len
, 0);
2446 strbuf_addf(&out
, " %s%"PRIuMAX
"%s",
2447 del_c
, deleted
, reset
);
2448 strbuf_addstr(&out
, " -> ");
2449 strbuf_addf(&out
, "%s%"PRIuMAX
"%s",
2450 add_c
, added
, reset
);
2451 strbuf_addstr(&out
, " bytes\n");
2452 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2453 out
.buf
, out
.len
, 0);
2457 else if (file
->is_unmerged
) {
2458 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2459 strbuf_addstr(&out
, " Unmerged\n");
2460 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2461 out
.buf
, out
.len
, 0);
2467 * scale the add/delete
2472 if (graph_width
<= max_change
) {
2473 int total
= scale_linear(add
+ del
, graph_width
, max_change
);
2474 if (total
< 2 && add
&& del
)
2475 /* width >= 2 due to the sanity check */
2478 add
= scale_linear(add
, graph_width
, max_change
);
2481 del
= scale_linear(del
, graph_width
, max_change
);
2485 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2486 strbuf_addf(&out
, " %*"PRIuMAX
"%s",
2487 number_width
, added
+ deleted
,
2488 added
+ deleted
? " " : "");
2489 show_graph(&out
, '+', add
, add_c
, reset
);
2490 show_graph(&out
, '-', del
, del_c
, reset
);
2491 strbuf_addch(&out
, '\n');
2492 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2493 out
.buf
, out
.len
, 0);
2497 for (i
= 0; i
< data
->nr
; i
++) {
2498 struct diffstat_file
*file
= data
->files
[i
];
2499 uintmax_t added
= file
->added
;
2500 uintmax_t deleted
= file
->deleted
;
2502 if (file
->is_unmerged
||
2503 (!file
->is_interesting
&& (added
+ deleted
== 0))) {
2508 if (!file
->is_binary
) {
2515 emit_diff_symbol(options
,
2516 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
2521 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2522 strbuf_release(&out
);
2525 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
2527 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
2532 for (i
= 0; i
< data
->nr
; i
++) {
2533 int added
= data
->files
[i
]->added
;
2534 int deleted
= data
->files
[i
]->deleted
;
2536 if (data
->files
[i
]->is_unmerged
||
2537 (!data
->files
[i
]->is_interesting
&& (added
+ deleted
== 0))) {
2539 } else if (!data
->files
[i
]->is_binary
) { /* don't count bytes */
2544 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2547 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
2554 for (i
= 0; i
< data
->nr
; i
++) {
2555 struct diffstat_file
*file
= data
->files
[i
];
2557 fprintf(options
->file
, "%s", diff_line_prefix(options
));
2559 if (file
->is_binary
)
2560 fprintf(options
->file
, "-\t-\t");
2562 fprintf(options
->file
,
2563 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
2564 file
->added
, file
->deleted
);
2565 if (options
->line_termination
) {
2566 fill_print_name(file
);
2567 if (!file
->is_renamed
)
2568 write_name_quoted(file
->name
, options
->file
,
2569 options
->line_termination
);
2571 fputs(file
->print_name
, options
->file
);
2572 putc(options
->line_termination
, options
->file
);
2575 if (file
->is_renamed
) {
2576 putc('\0', options
->file
);
2577 write_name_quoted(file
->from_name
, options
->file
, '\0');
2579 write_name_quoted(file
->name
, options
->file
, '\0');
2584 struct dirstat_file
{
2586 unsigned long changed
;
2589 struct dirstat_dir
{
2590 struct dirstat_file
*files
;
2591 int alloc
, nr
, permille
, cumulative
;
2594 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
2595 unsigned long changed
, const char *base
, int baselen
)
2597 unsigned long sum_changes
= 0;
2598 unsigned int sources
= 0;
2599 const char *line_prefix
= diff_line_prefix(opt
);
2602 struct dirstat_file
*f
= dir
->files
;
2603 int namelen
= strlen(f
->name
);
2604 unsigned long changes
;
2607 if (namelen
< baselen
)
2609 if (memcmp(f
->name
, base
, baselen
))
2611 slash
= strchr(f
->name
+ baselen
, '/');
2613 int newbaselen
= slash
+ 1 - f
->name
;
2614 changes
= gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
2617 changes
= f
->changed
;
2622 sum_changes
+= changes
;
2626 * We don't report dirstat's for
2628 * - or cases where everything came from a single directory
2629 * under this directory (sources == 1).
2631 if (baselen
&& sources
!= 1) {
2633 int permille
= sum_changes
* 1000 / changed
;
2634 if (permille
>= dir
->permille
) {
2635 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
2636 permille
/ 10, permille
% 10, baselen
, base
);
2637 if (!dir
->cumulative
)
2645 static int dirstat_compare(const void *_a
, const void *_b
)
2647 const struct dirstat_file
*a
= _a
;
2648 const struct dirstat_file
*b
= _b
;
2649 return strcmp(a
->name
, b
->name
);
2652 static void show_dirstat(struct diff_options
*options
)
2655 unsigned long changed
;
2656 struct dirstat_dir dir
;
2657 struct diff_queue_struct
*q
= &diff_queued_diff
;
2662 dir
.permille
= options
->dirstat_permille
;
2663 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
2666 for (i
= 0; i
< q
->nr
; i
++) {
2667 struct diff_filepair
*p
= q
->queue
[i
];
2669 unsigned long copied
, added
, damage
;
2670 int content_changed
;
2672 name
= p
->two
->path
? p
->two
->path
: p
->one
->path
;
2674 if (p
->one
->oid_valid
&& p
->two
->oid_valid
)
2675 content_changed
= oidcmp(&p
->one
->oid
, &p
->two
->oid
);
2677 content_changed
= 1;
2679 if (!content_changed
) {
2681 * The SHA1 has not changed, so pre-/post-content is
2682 * identical. We can therefore skip looking at the
2683 * file contents altogether.
2689 if (options
->flags
.dirstat_by_file
) {
2691 * In --dirstat-by-file mode, we don't really need to
2692 * look at the actual file contents at all.
2693 * The fact that the SHA1 changed is enough for us to
2694 * add this file to the list of results
2695 * (with each file contributing equal damage).
2701 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
2702 diff_populate_filespec(p
->one
, 0);
2703 diff_populate_filespec(p
->two
, 0);
2704 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
,
2706 diff_free_filespec_data(p
->one
);
2707 diff_free_filespec_data(p
->two
);
2708 } else if (DIFF_FILE_VALID(p
->one
)) {
2709 diff_populate_filespec(p
->one
, CHECK_SIZE_ONLY
);
2711 diff_free_filespec_data(p
->one
);
2712 } else if (DIFF_FILE_VALID(p
->two
)) {
2713 diff_populate_filespec(p
->two
, CHECK_SIZE_ONLY
);
2715 added
= p
->two
->size
;
2716 diff_free_filespec_data(p
->two
);
2721 * Original minus copied is the removed material,
2722 * added is the new material. They are both damages
2723 * made to the preimage.
2724 * If the resulting damage is zero, we know that
2725 * diffcore_count_changes() considers the two entries to
2726 * be identical, but since content_changed is true, we
2727 * know that there must have been _some_ kind of change,
2728 * so we force all entries to have damage > 0.
2730 damage
= (p
->one
->size
- copied
) + added
;
2735 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
2736 dir
.files
[dir
.nr
].name
= name
;
2737 dir
.files
[dir
.nr
].changed
= damage
;
2742 /* This can happen even with many files, if everything was renames */
2746 /* Show all directories with more than x% of the changes */
2747 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
2748 gather_dirstat(options
, &dir
, changed
, "", 0);
2751 static void show_dirstat_by_line(struct diffstat_t
*data
, struct diff_options
*options
)
2754 unsigned long changed
;
2755 struct dirstat_dir dir
;
2763 dir
.permille
= options
->dirstat_permille
;
2764 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
2767 for (i
= 0; i
< data
->nr
; i
++) {
2768 struct diffstat_file
*file
= data
->files
[i
];
2769 unsigned long damage
= file
->added
+ file
->deleted
;
2770 if (file
->is_binary
)
2772 * binary files counts bytes, not lines. Must find some
2773 * way to normalize binary bytes vs. textual lines.
2774 * The following heuristic assumes that there are 64
2776 * This is stupid and ugly, but very cheap...
2778 damage
= DIV_ROUND_UP(damage
, 64);
2779 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
2780 dir
.files
[dir
.nr
].name
= file
->name
;
2781 dir
.files
[dir
.nr
].changed
= damage
;
2786 /* This can happen even with many files, if everything was renames */
2790 /* Show all directories with more than x% of the changes */
2791 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
2792 gather_dirstat(options
, &dir
, changed
, "", 0);
2795 static void free_diffstat_info(struct diffstat_t
*diffstat
)
2798 for (i
= 0; i
< diffstat
->nr
; i
++) {
2799 struct diffstat_file
*f
= diffstat
->files
[i
];
2800 if (f
->name
!= f
->print_name
)
2801 free(f
->print_name
);
2806 free(diffstat
->files
);
2809 struct checkdiff_t
{
2810 const char *filename
;
2812 int conflict_marker_size
;
2813 struct diff_options
*o
;
2818 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
2823 if (len
< marker_size
+ 1)
2825 firstchar
= line
[0];
2826 switch (firstchar
) {
2827 case '=': case '>': case '<': case '|':
2832 for (cnt
= 1; cnt
< marker_size
; cnt
++)
2833 if (line
[cnt
] != firstchar
)
2835 /* line[1] thru line[marker_size-1] are same as firstchar */
2836 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
2841 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
2843 struct checkdiff_t
*data
= priv
;
2844 int marker_size
= data
->conflict_marker_size
;
2845 const char *ws
= diff_get_color(data
->o
->use_color
, DIFF_WHITESPACE
);
2846 const char *reset
= diff_get_color(data
->o
->use_color
, DIFF_RESET
);
2847 const char *set
= diff_get_color(data
->o
->use_color
, DIFF_FILE_NEW
);
2849 const char *line_prefix
;
2852 line_prefix
= diff_line_prefix(data
->o
);
2854 if (line
[0] == '+') {
2857 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
2859 fprintf(data
->o
->file
,
2860 "%s%s:%d: leftover conflict marker\n",
2861 line_prefix
, data
->filename
, data
->lineno
);
2863 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
2866 data
->status
|= bad
;
2867 err
= whitespace_error_string(bad
);
2868 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
2869 line_prefix
, data
->filename
, data
->lineno
, err
);
2871 emit_line(data
->o
, set
, reset
, line
, 1);
2872 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
2873 data
->o
->file
, set
, reset
, ws
);
2874 } else if (line
[0] == ' ') {
2876 } else if (line
[0] == '@') {
2877 char *plus
= strchr(line
, '+');
2879 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
2881 die("invalid diff");
2885 static unsigned char *deflate_it(char *data
,
2887 unsigned long *result_size
)
2890 unsigned char *deflated
;
2893 git_deflate_init(&stream
, zlib_compression_level
);
2894 bound
= git_deflate_bound(&stream
, size
);
2895 deflated
= xmalloc(bound
);
2896 stream
.next_out
= deflated
;
2897 stream
.avail_out
= bound
;
2899 stream
.next_in
= (unsigned char *)data
;
2900 stream
.avail_in
= size
;
2901 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
2903 git_deflate_end(&stream
);
2904 *result_size
= stream
.total_out
;
2908 static void emit_binary_diff_body(struct diff_options
*o
,
2909 mmfile_t
*one
, mmfile_t
*two
)
2915 unsigned long orig_size
;
2916 unsigned long delta_size
;
2917 unsigned long deflate_size
;
2918 unsigned long data_size
;
2920 /* We could do deflated delta, or we could do just deflated two,
2921 * whichever is smaller.
2924 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
2925 if (one
->size
&& two
->size
) {
2926 delta
= diff_delta(one
->ptr
, one
->size
,
2927 two
->ptr
, two
->size
,
2928 &delta_size
, deflate_size
);
2930 void *to_free
= delta
;
2931 orig_size
= delta_size
;
2932 delta
= deflate_it(delta
, delta_size
, &delta_size
);
2937 if (delta
&& delta_size
< deflate_size
) {
2938 char *s
= xstrfmt("%lu", orig_size
);
2939 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
2944 data_size
= delta_size
;
2946 char *s
= xstrfmt("%lu", two
->size
);
2947 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
2952 data_size
= deflate_size
;
2955 /* emit data encoded in base85 */
2959 int bytes
= (52 < data_size
) ? 52 : data_size
;
2963 line
[0] = bytes
+ 'A' - 1;
2965 line
[0] = bytes
- 26 + 'a' - 1;
2966 encode_85(line
+ 1, cp
, bytes
);
2967 cp
= (char *) cp
+ bytes
;
2973 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_BODY
,
2976 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_FOOTER
, NULL
, 0, 0);
2980 static void emit_binary_diff(struct diff_options
*o
,
2981 mmfile_t
*one
, mmfile_t
*two
)
2983 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER
, NULL
, 0, 0);
2984 emit_binary_diff_body(o
, one
, two
);
2985 emit_binary_diff_body(o
, two
, one
);
2988 int diff_filespec_is_binary(struct diff_filespec
*one
)
2990 if (one
->is_binary
== -1) {
2991 diff_filespec_load_driver(one
);
2992 if (one
->driver
->binary
!= -1)
2993 one
->is_binary
= one
->driver
->binary
;
2995 if (!one
->data
&& DIFF_FILE_VALID(one
))
2996 diff_populate_filespec(one
, CHECK_BINARY
);
2997 if (one
->is_binary
== -1 && one
->data
)
2998 one
->is_binary
= buffer_is_binary(one
->data
,
3000 if (one
->is_binary
== -1)
3004 return one
->is_binary
;
3007 static const struct userdiff_funcname
*diff_funcname_pattern(struct diff_filespec
*one
)
3009 diff_filespec_load_driver(one
);
3010 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
3013 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
3015 if (!options
->a_prefix
)
3016 options
->a_prefix
= a
;
3017 if (!options
->b_prefix
)
3018 options
->b_prefix
= b
;
3021 struct userdiff_driver
*get_textconv(struct diff_filespec
*one
)
3023 if (!DIFF_FILE_VALID(one
))
3026 diff_filespec_load_driver(one
);
3027 return userdiff_get_textconv(one
->driver
);
3030 static void builtin_diff(const char *name_a
,
3032 struct diff_filespec
*one
,
3033 struct diff_filespec
*two
,
3034 const char *xfrm_msg
,
3035 int must_show_header
,
3036 struct diff_options
*o
,
3037 int complete_rewrite
)
3041 char *a_one
, *b_two
;
3042 const char *meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
3043 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
3044 const char *a_prefix
, *b_prefix
;
3045 struct userdiff_driver
*textconv_one
= NULL
;
3046 struct userdiff_driver
*textconv_two
= NULL
;
3047 struct strbuf header
= STRBUF_INIT
;
3048 const char *line_prefix
= diff_line_prefix(o
);
3050 diff_set_mnemonic_prefix(o
, "a/", "b/");
3051 if (o
->flags
.reverse_diff
) {
3052 a_prefix
= o
->b_prefix
;
3053 b_prefix
= o
->a_prefix
;
3055 a_prefix
= o
->a_prefix
;
3056 b_prefix
= o
->b_prefix
;
3059 if (o
->submodule_format
== DIFF_SUBMODULE_LOG
&&
3060 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3061 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
3062 show_submodule_summary(o
, one
->path
? one
->path
: two
->path
,
3063 &one
->oid
, &two
->oid
,
3064 two
->dirty_submodule
);
3066 } else if (o
->submodule_format
== DIFF_SUBMODULE_INLINE_DIFF
&&
3067 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3068 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
3069 show_submodule_inline_diff(o
, one
->path
? one
->path
: two
->path
,
3070 &one
->oid
, &two
->oid
,
3071 two
->dirty_submodule
);
3075 if (o
->flags
.allow_textconv
) {
3076 textconv_one
= get_textconv(one
);
3077 textconv_two
= get_textconv(two
);
3080 /* Never use a non-valid filename anywhere if at all possible */
3081 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
3082 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
3084 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
3085 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
3086 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
3087 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
3088 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
3089 if (lbl
[0][0] == '/') {
3091 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3093 strbuf_addstr(&header
, xfrm_msg
);
3094 must_show_header
= 1;
3096 else if (lbl
[1][0] == '/') {
3097 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3099 strbuf_addstr(&header
, xfrm_msg
);
3100 must_show_header
= 1;
3103 if (one
->mode
!= two
->mode
) {
3104 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3105 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3106 must_show_header
= 1;
3109 strbuf_addstr(&header
, xfrm_msg
);
3112 * we do not run diff between different kind
3115 if ((one
->mode
^ two
->mode
) & S_IFMT
)
3116 goto free_ab_and_return
;
3117 if (complete_rewrite
&&
3118 (textconv_one
|| !diff_filespec_is_binary(one
)) &&
3119 (textconv_two
|| !diff_filespec_is_binary(two
))) {
3120 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3121 header
.buf
, header
.len
, 0);
3122 strbuf_reset(&header
);
3123 emit_rewrite_diff(name_a
, name_b
, one
, two
,
3124 textconv_one
, textconv_two
, o
);
3125 o
->found_changes
= 1;
3126 goto free_ab_and_return
;
3130 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
3131 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
,
3133 strbuf_reset(&header
);
3134 goto free_ab_and_return
;
3135 } else if (!o
->flags
.text
&&
3136 ( (!textconv_one
&& diff_filespec_is_binary(one
)) ||
3137 (!textconv_two
&& diff_filespec_is_binary(two
)) )) {
3138 struct strbuf sb
= STRBUF_INIT
;
3139 if (!one
->data
&& !two
->data
&&
3140 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
3142 if (!oidcmp(&one
->oid
, &two
->oid
)) {
3143 if (must_show_header
)
3144 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3145 header
.buf
, header
.len
,
3147 goto free_ab_and_return
;
3149 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3150 header
.buf
, header
.len
, 0);
3151 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3152 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3153 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3155 strbuf_release(&sb
);
3156 goto free_ab_and_return
;
3158 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
3159 die("unable to read files to diff");
3160 /* Quite common confusing case */
3161 if (mf1
.size
== mf2
.size
&&
3162 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
3163 if (must_show_header
)
3164 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3165 header
.buf
, header
.len
, 0);
3166 goto free_ab_and_return
;
3168 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
, header
.len
, 0);
3169 strbuf_reset(&header
);
3170 if (o
->flags
.binary
)
3171 emit_binary_diff(o
, &mf1
, &mf2
);
3173 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3174 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3175 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3177 strbuf_release(&sb
);
3179 o
->found_changes
= 1;
3181 /* Crazy xdl interfaces.. */
3182 const char *diffopts
= getenv("GIT_DIFF_OPTS");
3186 struct emit_callback ecbdata
;
3187 const struct userdiff_funcname
*pe
;
3189 if (must_show_header
) {
3190 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3191 header
.buf
, header
.len
, 0);
3192 strbuf_reset(&header
);
3195 mf1
.size
= fill_textconv(textconv_one
, one
, &mf1
.ptr
);
3196 mf2
.size
= fill_textconv(textconv_two
, two
, &mf2
.ptr
);
3198 pe
= diff_funcname_pattern(one
);
3200 pe
= diff_funcname_pattern(two
);
3202 memset(&xpp
, 0, sizeof(xpp
));
3203 memset(&xecfg
, 0, sizeof(xecfg
));
3204 memset(&ecbdata
, 0, sizeof(ecbdata
));
3205 ecbdata
.label_path
= lbl
;
3206 ecbdata
.color_diff
= want_color(o
->use_color
);
3207 ecbdata
.ws_rule
= whitespace_rule(name_b
);
3208 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
3209 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3211 ecbdata
.header
= header
.len
? &header
: NULL
;
3212 xpp
.flags
= o
->xdl_opts
;
3213 xpp
.anchors
= o
->anchors
;
3214 xpp
.anchors_nr
= o
->anchors_nr
;
3215 xecfg
.ctxlen
= o
->context
;
3216 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3217 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3218 if (o
->flags
.funccontext
)
3219 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
3221 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
3224 else if (skip_prefix(diffopts
, "--unified=", &v
))
3225 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3226 else if (skip_prefix(diffopts
, "-u", &v
))
3227 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3229 init_diff_words_data(&ecbdata
, o
, one
, two
);
3230 if (xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
3232 die("unable to generate diff for %s", one
->path
);
3234 free_diff_words_data(&ecbdata
);
3239 xdiff_clear_find_func(&xecfg
);
3243 strbuf_release(&header
);
3244 diff_free_filespec_data(one
);
3245 diff_free_filespec_data(two
);
3251 static void builtin_diffstat(const char *name_a
, const char *name_b
,
3252 struct diff_filespec
*one
,
3253 struct diff_filespec
*two
,
3254 struct diffstat_t
*diffstat
,
3255 struct diff_options
*o
,
3256 struct diff_filepair
*p
)
3259 struct diffstat_file
*data
;
3261 int complete_rewrite
= 0;
3263 if (!DIFF_PAIR_UNMERGED(p
)) {
3264 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
3265 complete_rewrite
= 1;
3268 data
= diffstat_add(diffstat
, name_a
, name_b
);
3269 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
3272 data
->is_unmerged
= 1;
3276 same_contents
= !oidcmp(&one
->oid
, &two
->oid
);
3278 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
3279 data
->is_binary
= 1;
3280 if (same_contents
) {
3284 data
->added
= diff_filespec_size(two
);
3285 data
->deleted
= diff_filespec_size(one
);
3289 else if (complete_rewrite
) {
3290 diff_populate_filespec(one
, 0);
3291 diff_populate_filespec(two
, 0);
3292 data
->deleted
= count_lines(one
->data
, one
->size
);
3293 data
->added
= count_lines(two
->data
, two
->size
);
3296 else if (!same_contents
) {
3297 /* Crazy xdl interfaces.. */
3301 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
3302 die("unable to read files to diff");
3304 memset(&xpp
, 0, sizeof(xpp
));
3305 memset(&xecfg
, 0, sizeof(xecfg
));
3306 xpp
.flags
= o
->xdl_opts
;
3307 xpp
.anchors
= o
->anchors
;
3308 xpp
.anchors_nr
= o
->anchors_nr
;
3309 xecfg
.ctxlen
= o
->context
;
3310 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3311 if (xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
3313 die("unable to generate diffstat for %s", one
->path
);
3316 diff_free_filespec_data(one
);
3317 diff_free_filespec_data(two
);
3320 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
3321 const char *attr_path
,
3322 struct diff_filespec
*one
,
3323 struct diff_filespec
*two
,
3324 struct diff_options
*o
)
3327 struct checkdiff_t data
;
3332 memset(&data
, 0, sizeof(data
));
3333 data
.filename
= name_b
? name_b
: name_a
;
3336 data
.ws_rule
= whitespace_rule(attr_path
);
3337 data
.conflict_marker_size
= ll_merge_marker_size(attr_path
);
3339 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
3340 die("unable to read files to diff");
3343 * All the other codepaths check both sides, but not checking
3344 * the "old" side here is deliberate. We are checking the newly
3345 * introduced changes, and as long as the "new" side is text, we
3346 * can and should check what it introduces.
3348 if (diff_filespec_is_binary(two
))
3349 goto free_and_return
;
3351 /* Crazy xdl interfaces.. */
3355 memset(&xpp
, 0, sizeof(xpp
));
3356 memset(&xecfg
, 0, sizeof(xecfg
));
3357 xecfg
.ctxlen
= 1; /* at least one context line */
3359 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
3361 die("unable to generate checkdiff for %s", one
->path
);
3363 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
3364 struct emit_callback ecbdata
;
3367 ecbdata
.ws_rule
= data
.ws_rule
;
3368 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3369 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
3374 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
3375 fprintf(o
->file
, "%s:%d: %s.\n",
3376 data
.filename
, blank_at_eof
, err
);
3377 data
.status
= 1; /* report errors */
3382 diff_free_filespec_data(one
);
3383 diff_free_filespec_data(two
);
3385 o
->flags
.check_failed
= 1;
3388 struct diff_filespec
*alloc_filespec(const char *path
)
3390 struct diff_filespec
*spec
;
3392 FLEXPTR_ALLOC_STR(spec
, path
, path
);
3394 spec
->is_binary
= -1;
3398 void free_filespec(struct diff_filespec
*spec
)
3400 if (!--spec
->count
) {
3401 diff_free_filespec_data(spec
);
3406 void fill_filespec(struct diff_filespec
*spec
, const struct object_id
*oid
,
3407 int oid_valid
, unsigned short mode
)
3410 spec
->mode
= canon_mode(mode
);
3411 oidcpy(&spec
->oid
, oid
);
3412 spec
->oid_valid
= oid_valid
;
3417 * Given a name and sha1 pair, if the index tells us the file in
3418 * the work tree has that object contents, return true, so that
3419 * prepare_temp_file() does not have to inflate and extract.
3421 static int reuse_worktree_file(const char *name
, const struct object_id
*oid
, int want_file
)
3423 const struct cache_entry
*ce
;
3428 * We do not read the cache ourselves here, because the
3429 * benchmark with my previous version that always reads cache
3430 * shows that it makes things worse for diff-tree comparing
3431 * two linux-2.6 kernel trees in an already checked out work
3432 * tree. This is because most diff-tree comparisons deal with
3433 * only a small number of files, while reading the cache is
3434 * expensive for a large project, and its cost outweighs the
3435 * savings we get by not inflating the object to a temporary
3436 * file. Practically, this code only helps when we are used
3437 * by diff-cache --cached, which does read the cache before
3443 /* We want to avoid the working directory if our caller
3444 * doesn't need the data in a normal file, this system
3445 * is rather slow with its stat/open/mmap/close syscalls,
3446 * and the object is contained in a pack file. The pack
3447 * is probably already open and will be faster to obtain
3448 * the data through than the working directory. Loose
3449 * objects however would tend to be slower as they need
3450 * to be individually opened and inflated.
3452 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(oid
->hash
))
3456 * Similarly, if we'd have to convert the file contents anyway, that
3457 * makes the optimization not worthwhile.
3459 if (!want_file
&& would_convert_to_git(&the_index
, name
))
3463 pos
= cache_name_pos(name
, len
);
3466 ce
= active_cache
[pos
];
3469 * This is not the sha1 we are looking for, or
3470 * unreusable because it is not a regular file.
3472 if (oidcmp(oid
, &ce
->oid
) || !S_ISREG(ce
->ce_mode
))
3476 * If ce is marked as "assume unchanged", there is no
3477 * guarantee that work tree matches what we are looking for.
3479 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
3483 * If ce matches the file in the work tree, we can reuse it.
3485 if (ce_uptodate(ce
) ||
3486 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
3492 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
3494 struct strbuf buf
= STRBUF_INIT
;
3497 /* Are we looking at the work tree? */
3498 if (s
->dirty_submodule
)
3501 strbuf_addf(&buf
, "Subproject commit %s%s\n",
3502 oid_to_hex(&s
->oid
), dirty
);
3506 strbuf_release(&buf
);
3508 s
->data
= strbuf_detach(&buf
, NULL
);
3515 * While doing rename detection and pickaxe operation, we may need to
3516 * grab the data for the blob (or file) for our own in-core comparison.
3517 * diff_filespec has data and size fields for this purpose.
3519 int diff_populate_filespec(struct diff_filespec
*s
, unsigned int flags
)
3521 int size_only
= flags
& CHECK_SIZE_ONLY
;
3523 int conv_flags
= global_conv_flags_eol
;
3525 * demote FAIL to WARN to allow inspecting the situation
3526 * instead of refusing.
3528 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
3529 conv_flags
= CONV_EOL_RNDTRP_WARN
;
3531 if (!DIFF_FILE_VALID(s
))
3532 die("internal error: asking to populate invalid file.");
3533 if (S_ISDIR(s
->mode
))
3539 if (size_only
&& 0 < s
->size
)
3542 if (S_ISGITLINK(s
->mode
))
3543 return diff_populate_gitlink(s
, size_only
);
3545 if (!s
->oid_valid
||
3546 reuse_worktree_file(s
->path
, &s
->oid
, 0)) {
3547 struct strbuf buf
= STRBUF_INIT
;
3551 if (lstat(s
->path
, &st
) < 0) {
3555 s
->data
= (char *)"";
3559 s
->size
= xsize_t(st
.st_size
);
3562 if (S_ISLNK(st
.st_mode
)) {
3563 struct strbuf sb
= STRBUF_INIT
;
3565 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
3568 s
->data
= strbuf_detach(&sb
, NULL
);
3574 * Even if the caller would be happy with getting
3575 * only the size, we cannot return early at this
3576 * point if the path requires us to run the content
3579 if (size_only
&& !would_convert_to_git(&the_index
, s
->path
))
3583 * Note: this check uses xsize_t(st.st_size) that may
3584 * not be the true size of the blob after it goes
3585 * through convert_to_git(). This may not strictly be
3586 * correct, but the whole point of big_file_threshold
3587 * and is_binary check being that we want to avoid
3588 * opening the file and inspecting the contents, this
3591 if ((flags
& CHECK_BINARY
) &&
3592 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
3596 fd
= open(s
->path
, O_RDONLY
);
3599 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
3601 s
->should_munmap
= 1;
3604 * Convert from working tree format to canonical git format
3606 if (convert_to_git(&the_index
, s
->path
, s
->data
, s
->size
, &buf
, conv_flags
)) {
3608 munmap(s
->data
, s
->size
);
3609 s
->should_munmap
= 0;
3610 s
->data
= strbuf_detach(&buf
, &size
);
3616 enum object_type type
;
3617 if (size_only
|| (flags
& CHECK_BINARY
)) {
3618 type
= sha1_object_info(s
->oid
.hash
, &s
->size
);
3620 die("unable to read %s",
3621 oid_to_hex(&s
->oid
));
3624 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
3629 s
->data
= read_sha1_file(s
->oid
.hash
, &type
, &s
->size
);
3631 die("unable to read %s", oid_to_hex(&s
->oid
));
3637 void diff_free_filespec_blob(struct diff_filespec
*s
)
3641 else if (s
->should_munmap
)
3642 munmap(s
->data
, s
->size
);
3644 if (s
->should_free
|| s
->should_munmap
) {
3645 s
->should_free
= s
->should_munmap
= 0;
3650 void diff_free_filespec_data(struct diff_filespec
*s
)
3652 diff_free_filespec_blob(s
);
3653 FREE_AND_NULL(s
->cnt_data
);
3656 static void prep_temp_blob(const char *path
, struct diff_tempfile
*temp
,
3659 const struct object_id
*oid
,
3662 struct strbuf buf
= STRBUF_INIT
;
3663 struct strbuf tempfile
= STRBUF_INIT
;
3664 char *path_dup
= xstrdup(path
);
3665 const char *base
= basename(path_dup
);
3667 /* Generate "XXXXXX_basename.ext" */
3668 strbuf_addstr(&tempfile
, "XXXXXX_");
3669 strbuf_addstr(&tempfile
, base
);
3671 temp
->tempfile
= mks_tempfile_ts(tempfile
.buf
, strlen(base
) + 1);
3672 if (!temp
->tempfile
)
3673 die_errno("unable to create temp-file");
3674 if (convert_to_working_tree(path
,
3675 (const char *)blob
, (size_t)size
, &buf
)) {
3679 if (write_in_full(temp
->tempfile
->fd
, blob
, size
) < 0 ||
3680 close_tempfile_gently(temp
->tempfile
))
3681 die_errno("unable to write temp-file");
3682 temp
->name
= get_tempfile_path(temp
->tempfile
);
3683 oid_to_hex_r(temp
->hex
, oid
);
3684 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
3685 strbuf_release(&buf
);
3686 strbuf_release(&tempfile
);
3690 static struct diff_tempfile
*prepare_temp_file(const char *name
,
3691 struct diff_filespec
*one
)
3693 struct diff_tempfile
*temp
= claim_diff_tempfile();
3695 if (!DIFF_FILE_VALID(one
)) {
3697 /* A '-' entry produces this for file-2, and
3698 * a '+' entry produces this for file-1.
3700 temp
->name
= "/dev/null";
3701 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
3702 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
3706 if (!S_ISGITLINK(one
->mode
) &&
3708 reuse_worktree_file(name
, &one
->oid
, 1))) {
3710 if (lstat(name
, &st
) < 0) {
3711 if (errno
== ENOENT
)
3712 goto not_a_valid_file
;
3713 die_errno("stat(%s)", name
);
3715 if (S_ISLNK(st
.st_mode
)) {
3716 struct strbuf sb
= STRBUF_INIT
;
3717 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
3718 die_errno("readlink(%s)", name
);
3719 prep_temp_blob(name
, temp
, sb
.buf
, sb
.len
,
3721 &one
->oid
: &null_oid
),
3723 one
->mode
: S_IFLNK
));
3724 strbuf_release(&sb
);
3727 /* we can borrow from the file in the work tree */
3729 if (!one
->oid_valid
)
3730 oid_to_hex_r(temp
->hex
, &null_oid
);
3732 oid_to_hex_r(temp
->hex
, &one
->oid
);
3733 /* Even though we may sometimes borrow the
3734 * contents from the work tree, we always want
3735 * one->mode. mode is trustworthy even when
3736 * !(one->oid_valid), as long as
3737 * DIFF_FILE_VALID(one).
3739 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
3744 if (diff_populate_filespec(one
, 0))
3745 die("cannot read data blob for %s", one
->path
);
3746 prep_temp_blob(name
, temp
, one
->data
, one
->size
,
3747 &one
->oid
, one
->mode
);
3752 static void add_external_diff_name(struct argv_array
*argv
,
3754 struct diff_filespec
*df
)
3756 struct diff_tempfile
*temp
= prepare_temp_file(name
, df
);
3757 argv_array_push(argv
, temp
->name
);
3758 argv_array_push(argv
, temp
->hex
);
3759 argv_array_push(argv
, temp
->mode
);
3762 /* An external diff command takes:
3764 * diff-cmd name infile1 infile1-sha1 infile1-mode \
3765 * infile2 infile2-sha1 infile2-mode [ rename-to ]
3768 static void run_external_diff(const char *pgm
,
3771 struct diff_filespec
*one
,
3772 struct diff_filespec
*two
,
3773 const char *xfrm_msg
,
3774 int complete_rewrite
,
3775 struct diff_options
*o
)
3777 struct argv_array argv
= ARGV_ARRAY_INIT
;
3778 struct argv_array env
= ARGV_ARRAY_INIT
;
3779 struct diff_queue_struct
*q
= &diff_queued_diff
;
3781 argv_array_push(&argv
, pgm
);
3782 argv_array_push(&argv
, name
);
3785 add_external_diff_name(&argv
, name
, one
);
3787 add_external_diff_name(&argv
, name
, two
);
3789 add_external_diff_name(&argv
, other
, two
);
3790 argv_array_push(&argv
, other
);
3791 argv_array_push(&argv
, xfrm_msg
);
3795 argv_array_pushf(&env
, "GIT_DIFF_PATH_COUNTER=%d", ++o
->diff_path_counter
);
3796 argv_array_pushf(&env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
3798 if (run_command_v_opt_cd_env(argv
.argv
, RUN_USING_SHELL
, NULL
, env
.argv
))
3799 die(_("external diff died, stopping at %s"), name
);
3802 argv_array_clear(&argv
);
3803 argv_array_clear(&env
);
3806 static int similarity_index(struct diff_filepair
*p
)
3808 return p
->score
* 100 / MAX_SCORE
;
3811 static const char *diff_abbrev_oid(const struct object_id
*oid
, int abbrev
)
3813 if (startup_info
->have_repository
)
3814 return find_unique_abbrev(oid
, abbrev
);
3816 char *hex
= oid_to_hex(oid
);
3818 abbrev
= FALLBACK_DEFAULT_ABBREV
;
3819 if (abbrev
> GIT_SHA1_HEXSZ
)
3820 die("BUG: oid abbreviation out of range: %d", abbrev
);
3827 static void fill_metainfo(struct strbuf
*msg
,
3830 struct diff_filespec
*one
,
3831 struct diff_filespec
*two
,
3832 struct diff_options
*o
,
3833 struct diff_filepair
*p
,
3834 int *must_show_header
,
3837 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
3838 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
3839 const char *line_prefix
= diff_line_prefix(o
);
3841 *must_show_header
= 1;
3842 strbuf_init(msg
, PATH_MAX
* 2 + 300);
3843 switch (p
->status
) {
3844 case DIFF_STATUS_COPIED
:
3845 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
3846 line_prefix
, set
, similarity_index(p
));
3847 strbuf_addf(msg
, "%s\n%s%scopy from ",
3848 reset
, line_prefix
, set
);
3849 quote_c_style(name
, msg
, NULL
, 0);
3850 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
3851 quote_c_style(other
, msg
, NULL
, 0);
3852 strbuf_addf(msg
, "%s\n", reset
);
3854 case DIFF_STATUS_RENAMED
:
3855 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
3856 line_prefix
, set
, similarity_index(p
));
3857 strbuf_addf(msg
, "%s\n%s%srename from ",
3858 reset
, line_prefix
, set
);
3859 quote_c_style(name
, msg
, NULL
, 0);
3860 strbuf_addf(msg
, "%s\n%s%srename to ",
3861 reset
, line_prefix
, set
);
3862 quote_c_style(other
, msg
, NULL
, 0);
3863 strbuf_addf(msg
, "%s\n", reset
);
3865 case DIFF_STATUS_MODIFIED
:
3867 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
3869 set
, similarity_index(p
), reset
);
3874 *must_show_header
= 0;
3876 if (one
&& two
&& oidcmp(&one
->oid
, &two
->oid
)) {
3877 int abbrev
= o
->flags
.full_index
? 40 : DEFAULT_ABBREV
;
3879 if (o
->flags
.binary
) {
3881 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
3882 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
3885 strbuf_addf(msg
, "%s%sindex %s..%s", line_prefix
, set
,
3886 diff_abbrev_oid(&one
->oid
, abbrev
),
3887 diff_abbrev_oid(&two
->oid
, abbrev
));
3888 if (one
->mode
== two
->mode
)
3889 strbuf_addf(msg
, " %06o", one
->mode
);
3890 strbuf_addf(msg
, "%s\n", reset
);
3894 static void run_diff_cmd(const char *pgm
,
3897 const char *attr_path
,
3898 struct diff_filespec
*one
,
3899 struct diff_filespec
*two
,
3901 struct diff_options
*o
,
3902 struct diff_filepair
*p
)
3904 const char *xfrm_msg
= NULL
;
3905 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
3906 int must_show_header
= 0;
3909 if (o
->flags
.allow_external
) {
3910 struct userdiff_driver
*drv
= userdiff_find_by_path(attr_path
);
3911 if (drv
&& drv
->external
)
3912 pgm
= drv
->external
;
3917 * don't use colors when the header is intended for an
3918 * external diff driver
3920 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
3922 want_color(o
->use_color
) && !pgm
);
3923 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
3927 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
3928 complete_rewrite
, o
);
3932 builtin_diff(name
, other
? other
: name
,
3933 one
, two
, xfrm_msg
, must_show_header
,
3934 o
, complete_rewrite
);
3936 fprintf(o
->file
, "* Unmerged path %s\n", name
);
3939 static void diff_fill_oid_info(struct diff_filespec
*one
)
3941 if (DIFF_FILE_VALID(one
)) {
3942 if (!one
->oid_valid
) {
3944 if (one
->is_stdin
) {
3948 if (lstat(one
->path
, &st
) < 0)
3949 die_errno("stat '%s'", one
->path
);
3950 if (index_path(&one
->oid
, one
->path
, &st
, 0))
3951 die("cannot hash %s", one
->path
);
3958 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
3960 /* Strip the prefix but do not molest /dev/null and absolute paths */
3961 if (*namep
&& **namep
!= '/') {
3962 *namep
+= prefix_length
;
3966 if (*otherp
&& **otherp
!= '/') {
3967 *otherp
+= prefix_length
;
3968 if (**otherp
== '/')
3973 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
3975 const char *pgm
= external_diff();
3977 struct diff_filespec
*one
= p
->one
;
3978 struct diff_filespec
*two
= p
->two
;
3981 const char *attr_path
;
3984 other
= (strcmp(name
, two
->path
) ? two
->path
: NULL
);
3986 if (o
->prefix_length
)
3987 strip_prefix(o
->prefix_length
, &name
, &other
);
3989 if (!o
->flags
.allow_external
)
3992 if (DIFF_PAIR_UNMERGED(p
)) {
3993 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
3994 NULL
, NULL
, NULL
, o
, p
);
3998 diff_fill_oid_info(one
);
3999 diff_fill_oid_info(two
);
4002 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
4003 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
4005 * a filepair that changes between file and symlink
4006 * needs to be split into deletion and creation.
4008 struct diff_filespec
*null
= alloc_filespec(two
->path
);
4009 run_diff_cmd(NULL
, name
, other
, attr_path
,
4010 one
, null
, &msg
, o
, p
);
4012 strbuf_release(&msg
);
4014 null
= alloc_filespec(one
->path
);
4015 run_diff_cmd(NULL
, name
, other
, attr_path
,
4016 null
, two
, &msg
, o
, p
);
4020 run_diff_cmd(pgm
, name
, other
, attr_path
,
4021 one
, two
, &msg
, o
, p
);
4023 strbuf_release(&msg
);
4026 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
4027 struct diffstat_t
*diffstat
)
4032 if (DIFF_PAIR_UNMERGED(p
)) {
4034 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, p
);
4038 name
= p
->one
->path
;
4039 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4041 if (o
->prefix_length
)
4042 strip_prefix(o
->prefix_length
, &name
, &other
);
4044 diff_fill_oid_info(p
->one
);
4045 diff_fill_oid_info(p
->two
);
4047 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, p
);
4050 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
4054 const char *attr_path
;
4056 if (DIFF_PAIR_UNMERGED(p
)) {
4061 name
= p
->one
->path
;
4062 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4063 attr_path
= other
? other
: name
;
4065 if (o
->prefix_length
)
4066 strip_prefix(o
->prefix_length
, &name
, &other
);
4068 diff_fill_oid_info(p
->one
);
4069 diff_fill_oid_info(p
->two
);
4071 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
4074 void diff_setup(struct diff_options
*options
)
4076 memcpy(options
, &default_diff_options
, sizeof(*options
));
4078 options
->file
= stdout
;
4080 options
->abbrev
= DEFAULT_ABBREV
;
4081 options
->line_termination
= '\n';
4082 options
->break_opt
= -1;
4083 options
->rename_limit
= -1;
4084 options
->dirstat_permille
= diff_dirstat_permille_default
;
4085 options
->context
= diff_context_default
;
4086 options
->interhunkcontext
= diff_interhunk_context_default
;
4087 options
->ws_error_highlight
= ws_error_highlight_default
;
4088 options
->flags
.rename_empty
= 1;
4089 options
->objfind
= NULL
;
4091 /* pathchange left =NULL by default */
4092 options
->change
= diff_change
;
4093 options
->add_remove
= diff_addremove
;
4094 options
->use_color
= diff_use_color_default
;
4095 options
->detect_rename
= diff_detect_rename_default
;
4096 options
->xdl_opts
|= diff_algorithm
;
4097 if (diff_indent_heuristic
)
4098 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4100 options
->orderfile
= diff_order_file_cfg
;
4102 if (diff_no_prefix
) {
4103 options
->a_prefix
= options
->b_prefix
= "";
4104 } else if (!diff_mnemonic_prefix
) {
4105 options
->a_prefix
= "a/";
4106 options
->b_prefix
= "b/";
4109 options
->color_moved
= diff_color_moved_default
;
4112 void diff_setup_done(struct diff_options
*options
)
4114 unsigned check_mask
= DIFF_FORMAT_NAME
|
4115 DIFF_FORMAT_NAME_STATUS
|
4116 DIFF_FORMAT_CHECKDIFF
|
4117 DIFF_FORMAT_NO_OUTPUT
;
4119 if (options
->set_default
)
4120 options
->set_default(options
);
4122 if (HAS_MULTI_BITS(options
->output_format
& check_mask
))
4123 die(_("--name-only, --name-status, --check and -s are mutually exclusive"));
4125 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
4126 die(_("-G, -S and --find-object are mutually exclusive"));
4129 * Most of the time we can say "there are changes"
4130 * only by checking if there are changed paths, but
4131 * --ignore-whitespace* options force us to look
4135 if ((options
->xdl_opts
& XDF_WHITESPACE_FLAGS
))
4136 options
->flags
.diff_from_contents
= 1;
4138 options
->flags
.diff_from_contents
= 0;
4140 if (options
->flags
.find_copies_harder
)
4141 options
->detect_rename
= DIFF_DETECT_COPY
;
4143 if (!options
->flags
.relative_name
)
4144 options
->prefix
= NULL
;
4145 if (options
->prefix
)
4146 options
->prefix_length
= strlen(options
->prefix
);
4148 options
->prefix_length
= 0;
4150 if (options
->output_format
& (DIFF_FORMAT_NAME
|
4151 DIFF_FORMAT_NAME_STATUS
|
4152 DIFF_FORMAT_CHECKDIFF
|
4153 DIFF_FORMAT_NO_OUTPUT
))
4154 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
4155 DIFF_FORMAT_NUMSTAT
|
4156 DIFF_FORMAT_DIFFSTAT
|
4157 DIFF_FORMAT_SHORTSTAT
|
4158 DIFF_FORMAT_DIRSTAT
|
4159 DIFF_FORMAT_SUMMARY
|
4163 * These cases always need recursive; we do not drop caller-supplied
4164 * recursive bits for other formats here.
4166 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
4167 DIFF_FORMAT_NUMSTAT
|
4168 DIFF_FORMAT_DIFFSTAT
|
4169 DIFF_FORMAT_SHORTSTAT
|
4170 DIFF_FORMAT_DIRSTAT
|
4171 DIFF_FORMAT_SUMMARY
|
4172 DIFF_FORMAT_CHECKDIFF
))
4173 options
->flags
.recursive
= 1;
4175 * Also pickaxe would not work very well if you do not say recursive
4177 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
4178 options
->flags
.recursive
= 1;
4180 * When patches are generated, submodules diffed against the work tree
4181 * must be checked for dirtiness too so it can be shown in the output
4183 if (options
->output_format
& DIFF_FORMAT_PATCH
)
4184 options
->flags
.dirty_submodules
= 1;
4186 if (options
->detect_rename
&& options
->rename_limit
< 0)
4187 options
->rename_limit
= diff_rename_limit_default
;
4188 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
4190 /* read-cache does not die even when it fails
4191 * so it is safe for us to do this here. Also
4192 * it does not smudge active_cache or active_nr
4193 * when it fails, so we do not have to worry about
4194 * cleaning it up ourselves either.
4198 if (40 < options
->abbrev
)
4199 options
->abbrev
= 40; /* full */
4202 * It does not make sense to show the first hit we happened
4203 * to have found. It does not make sense not to return with
4204 * exit code in such a case either.
4206 if (options
->flags
.quick
) {
4207 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
4208 options
->flags
.exit_with_status
= 1;
4211 options
->diff_path_counter
= 0;
4213 if (options
->flags
.follow_renames
&& options
->pathspec
.nr
!= 1)
4214 die(_("--follow requires exactly one pathspec"));
4216 if (!options
->use_color
|| external_diff())
4217 options
->color_moved
= 0;
4220 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
4230 if (c
== arg_short
) {
4234 if (val
&& isdigit(c
)) {
4236 int n
= strtoul(arg
, &end
, 10);
4247 eq
= strchrnul(arg
, '=');
4249 if (!len
|| strncmp(arg
, arg_long
, len
))
4254 if (!isdigit(*++eq
))
4256 n
= strtoul(eq
, &end
, 10);
4264 static int diff_scoreopt_parse(const char *opt
);
4266 static inline int short_opt(char opt
, const char **argv
,
4267 const char **optarg
)
4269 const char *arg
= argv
[0];
4270 if (arg
[0] != '-' || arg
[1] != opt
)
4272 if (arg
[2] != '\0') {
4277 die("Option '%c' requires a value", opt
);
4282 int parse_long_opt(const char *opt
, const char **argv
,
4283 const char **optarg
)
4285 const char *arg
= argv
[0];
4286 if (!skip_prefix(arg
, "--", &arg
))
4288 if (!skip_prefix(arg
, opt
, &arg
))
4290 if (*arg
== '=') { /* stuck form: --option=value */
4296 /* separate form: --option value */
4298 die("Option '--%s' requires a value", opt
);
4303 static int stat_opt(struct diff_options
*options
, const char **av
)
4305 const char *arg
= av
[0];
4307 int width
= options
->stat_width
;
4308 int name_width
= options
->stat_name_width
;
4309 int graph_width
= options
->stat_graph_width
;
4310 int count
= options
->stat_count
;
4313 if (!skip_prefix(arg
, "--stat", &arg
))
4314 die("BUG: stat option does not begin with --stat: %s", arg
);
4319 if (skip_prefix(arg
, "-width", &arg
)) {
4321 width
= strtoul(arg
+ 1, &end
, 10);
4322 else if (!*arg
&& !av
[1])
4323 die_want_option("--stat-width");
4325 width
= strtoul(av
[1], &end
, 10);
4328 } else if (skip_prefix(arg
, "-name-width", &arg
)) {
4330 name_width
= strtoul(arg
+ 1, &end
, 10);
4331 else if (!*arg
&& !av
[1])
4332 die_want_option("--stat-name-width");
4334 name_width
= strtoul(av
[1], &end
, 10);
4337 } else if (skip_prefix(arg
, "-graph-width", &arg
)) {
4339 graph_width
= strtoul(arg
+ 1, &end
, 10);
4340 else if (!*arg
&& !av
[1])
4341 die_want_option("--stat-graph-width");
4343 graph_width
= strtoul(av
[1], &end
, 10);
4346 } else if (skip_prefix(arg
, "-count", &arg
)) {
4348 count
= strtoul(arg
+ 1, &end
, 10);
4349 else if (!*arg
&& !av
[1])
4350 die_want_option("--stat-count");
4352 count
= strtoul(av
[1], &end
, 10);
4358 width
= strtoul(arg
+1, &end
, 10);
4360 name_width
= strtoul(end
+1, &end
, 10);
4362 count
= strtoul(end
+1, &end
, 10);
4365 /* Important! This checks all the error cases! */
4368 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
4369 options
->stat_name_width
= name_width
;
4370 options
->stat_graph_width
= graph_width
;
4371 options
->stat_width
= width
;
4372 options
->stat_count
= count
;
4376 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
4378 struct strbuf errmsg
= STRBUF_INIT
;
4379 if (parse_dirstat_params(options
, params
, &errmsg
))
4380 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
4382 strbuf_release(&errmsg
);
4384 * The caller knows a dirstat-related option is given from the command
4385 * line; allow it to say "return this_function();"
4387 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
4391 static int parse_submodule_opt(struct diff_options
*options
, const char *value
)
4393 if (parse_submodule_params(options
, value
))
4394 die(_("Failed to parse --submodule option parameter: '%s'"),
4399 static const char diff_status_letters
[] = {
4402 DIFF_STATUS_DELETED
,
4403 DIFF_STATUS_MODIFIED
,
4404 DIFF_STATUS_RENAMED
,
4405 DIFF_STATUS_TYPE_CHANGED
,
4406 DIFF_STATUS_UNKNOWN
,
4407 DIFF_STATUS_UNMERGED
,
4408 DIFF_STATUS_FILTER_AON
,
4409 DIFF_STATUS_FILTER_BROKEN
,
4413 static unsigned int filter_bit
['Z' + 1];
4415 static void prepare_filter_bits(void)
4419 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
4420 for (i
= 0; diff_status_letters
[i
]; i
++)
4421 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
4425 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
4427 return opt
->filter
& filter_bit
[(int) status
];
4430 static int parse_diff_filter_opt(const char *optarg
, struct diff_options
*opt
)
4434 prepare_filter_bits();
4437 * If there is a negation e.g. 'd' in the input, and we haven't
4438 * initialized the filter field with another --diff-filter, start
4439 * from full set of bits, except for AON.
4442 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4443 if (optch
< 'a' || 'z' < optch
)
4445 opt
->filter
= (1 << (ARRAY_SIZE(diff_status_letters
) - 1)) - 1;
4446 opt
->filter
&= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
4451 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4455 if ('a' <= optch
&& optch
<= 'z') {
4457 optch
= toupper(optch
);
4462 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
4466 opt
->filter
&= ~bit
;
4473 static void enable_patch_output(int *fmt
) {
4474 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
4475 *fmt
|= DIFF_FORMAT_PATCH
;
4478 static int parse_ws_error_highlight_opt(struct diff_options
*opt
, const char *arg
)
4480 int val
= parse_ws_error_highlight(arg
);
4483 error("unknown value after ws-error-highlight=%.*s",
4487 opt
->ws_error_highlight
= val
;
4491 static int parse_objfind_opt(struct diff_options
*opt
, const char *arg
)
4493 struct object_id oid
;
4495 if (get_oid(arg
, &oid
))
4496 return error("unable to resolve '%s'", arg
);
4499 opt
->objfind
= xcalloc(1, sizeof(*opt
->objfind
));
4501 opt
->pickaxe_opts
|= DIFF_PICKAXE_KIND_OBJFIND
;
4502 opt
->flags
.recursive
= 1;
4503 opt
->flags
.tree_in_recursive
= 1;
4504 oidset_insert(opt
->objfind
, &oid
);
4508 int diff_opt_parse(struct diff_options
*options
,
4509 const char **av
, int ac
, const char *prefix
)
4511 const char *arg
= av
[0];
4518 /* Output format options */
4519 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u") || !strcmp(arg
, "--patch")
4520 || opt_arg(arg
, 'U', "unified", &options
->context
))
4521 enable_patch_output(&options
->output_format
);
4522 else if (!strcmp(arg
, "--raw"))
4523 options
->output_format
|= DIFF_FORMAT_RAW
;
4524 else if (!strcmp(arg
, "--patch-with-raw")) {
4525 enable_patch_output(&options
->output_format
);
4526 options
->output_format
|= DIFF_FORMAT_RAW
;
4527 } else if (!strcmp(arg
, "--numstat"))
4528 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
4529 else if (!strcmp(arg
, "--shortstat"))
4530 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
4531 else if (skip_prefix(arg
, "-X", &arg
) ||
4532 skip_to_optional_arg(arg
, "--dirstat", &arg
))
4533 return parse_dirstat_opt(options
, arg
);
4534 else if (!strcmp(arg
, "--cumulative"))
4535 return parse_dirstat_opt(options
, "cumulative");
4536 else if (skip_to_optional_arg(arg
, "--dirstat-by-file", &arg
)) {
4537 parse_dirstat_opt(options
, "files");
4538 return parse_dirstat_opt(options
, arg
);
4540 else if (!strcmp(arg
, "--check"))
4541 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
4542 else if (!strcmp(arg
, "--summary"))
4543 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
4544 else if (!strcmp(arg
, "--patch-with-stat")) {
4545 enable_patch_output(&options
->output_format
);
4546 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
4547 } else if (!strcmp(arg
, "--name-only"))
4548 options
->output_format
|= DIFF_FORMAT_NAME
;
4549 else if (!strcmp(arg
, "--name-status"))
4550 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
4551 else if (!strcmp(arg
, "-s") || !strcmp(arg
, "--no-patch"))
4552 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
4553 else if (starts_with(arg
, "--stat"))
4554 /* --stat, --stat-width, --stat-name-width, or --stat-count */
4555 return stat_opt(options
, av
);
4557 /* renames options */
4558 else if (starts_with(arg
, "-B") ||
4559 skip_to_optional_arg(arg
, "--break-rewrites", NULL
)) {
4560 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
4561 return error("invalid argument to -B: %s", arg
+2);
4563 else if (starts_with(arg
, "-M") ||
4564 skip_to_optional_arg(arg
, "--find-renames", NULL
)) {
4565 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
4566 return error("invalid argument to -M: %s", arg
+2);
4567 options
->detect_rename
= DIFF_DETECT_RENAME
;
4569 else if (!strcmp(arg
, "-D") || !strcmp(arg
, "--irreversible-delete")) {
4570 options
->irreversible_delete
= 1;
4572 else if (starts_with(arg
, "-C") ||
4573 skip_to_optional_arg(arg
, "--find-copies", NULL
)) {
4574 if (options
->detect_rename
== DIFF_DETECT_COPY
)
4575 options
->flags
.find_copies_harder
= 1;
4576 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
4577 return error("invalid argument to -C: %s", arg
+2);
4578 options
->detect_rename
= DIFF_DETECT_COPY
;
4580 else if (!strcmp(arg
, "--no-renames"))
4581 options
->detect_rename
= 0;
4582 else if (!strcmp(arg
, "--rename-empty"))
4583 options
->flags
.rename_empty
= 1;
4584 else if (!strcmp(arg
, "--no-rename-empty"))
4585 options
->flags
.rename_empty
= 0;
4586 else if (skip_to_optional_arg_default(arg
, "--relative", &arg
, NULL
)) {
4587 options
->flags
.relative_name
= 1;
4589 options
->prefix
= arg
;
4593 else if (!strcmp(arg
, "--minimal"))
4594 DIFF_XDL_SET(options
, NEED_MINIMAL
);
4595 else if (!strcmp(arg
, "--no-minimal"))
4596 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
4597 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
4598 DIFF_XDL_SET(options
, IGNORE_WHITESPACE
);
4599 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
4600 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_CHANGE
);
4601 else if (!strcmp(arg
, "--ignore-space-at-eol"))
4602 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_AT_EOL
);
4603 else if (!strcmp(arg
, "--ignore-cr-at-eol"))
4604 DIFF_XDL_SET(options
, IGNORE_CR_AT_EOL
);
4605 else if (!strcmp(arg
, "--ignore-blank-lines"))
4606 DIFF_XDL_SET(options
, IGNORE_BLANK_LINES
);
4607 else if (!strcmp(arg
, "--indent-heuristic"))
4608 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4609 else if (!strcmp(arg
, "--no-indent-heuristic"))
4610 DIFF_XDL_CLR(options
, INDENT_HEURISTIC
);
4611 else if (!strcmp(arg
, "--patience")) {
4613 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
4615 * Both --patience and --anchored use PATIENCE_DIFF
4616 * internally, so remove any anchors previously
4619 for (i
= 0; i
< options
->anchors_nr
; i
++)
4620 free(options
->anchors
[i
]);
4621 options
->anchors_nr
= 0;
4622 } else if (!strcmp(arg
, "--histogram"))
4623 options
->xdl_opts
= DIFF_WITH_ALG(options
, HISTOGRAM_DIFF
);
4624 else if ((argcount
= parse_long_opt("diff-algorithm", av
, &optarg
))) {
4625 long value
= parse_algorithm_value(optarg
);
4627 return error("option diff-algorithm accepts \"myers\", "
4628 "\"minimal\", \"patience\" and \"histogram\"");
4629 /* clear out previous settings */
4630 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
4631 options
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
4632 options
->xdl_opts
|= value
;
4634 } else if (skip_prefix(arg
, "--anchored=", &arg
)) {
4635 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
4636 ALLOC_GROW(options
->anchors
, options
->anchors_nr
+ 1,
4637 options
->anchors_alloc
);
4638 options
->anchors
[options
->anchors_nr
++] = xstrdup(arg
);
4642 else if (!strcmp(arg
, "--binary")) {
4643 enable_patch_output(&options
->output_format
);
4644 options
->flags
.binary
= 1;
4646 else if (!strcmp(arg
, "--full-index"))
4647 options
->flags
.full_index
= 1;
4648 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
4649 options
->flags
.text
= 1;
4650 else if (!strcmp(arg
, "-R"))
4651 options
->flags
.reverse_diff
= 1;
4652 else if (!strcmp(arg
, "--find-copies-harder"))
4653 options
->flags
.find_copies_harder
= 1;
4654 else if (!strcmp(arg
, "--follow"))
4655 options
->flags
.follow_renames
= 1;
4656 else if (!strcmp(arg
, "--no-follow")) {
4657 options
->flags
.follow_renames
= 0;
4658 options
->flags
.default_follow_renames
= 0;
4659 } else if (skip_to_optional_arg_default(arg
, "--color", &arg
, "always")) {
4660 int value
= git_config_colorbool(NULL
, arg
);
4662 return error("option `color' expects \"always\", \"auto\", or \"never\"");
4663 options
->use_color
= value
;
4665 else if (!strcmp(arg
, "--no-color"))
4666 options
->use_color
= 0;
4667 else if (!strcmp(arg
, "--color-moved")) {
4668 if (diff_color_moved_default
)
4669 options
->color_moved
= diff_color_moved_default
;
4670 if (options
->color_moved
== COLOR_MOVED_NO
)
4671 options
->color_moved
= COLOR_MOVED_DEFAULT
;
4672 } else if (!strcmp(arg
, "--no-color-moved"))
4673 options
->color_moved
= COLOR_MOVED_NO
;
4674 else if (skip_prefix(arg
, "--color-moved=", &arg
)) {
4675 int cm
= parse_color_moved(arg
);
4677 die("bad --color-moved argument: %s", arg
);
4678 options
->color_moved
= cm
;
4679 } else if (skip_to_optional_arg_default(arg
, "--color-words", &options
->word_regex
, NULL
)) {
4680 options
->use_color
= 1;
4681 options
->word_diff
= DIFF_WORDS_COLOR
;
4683 else if (!strcmp(arg
, "--word-diff")) {
4684 if (options
->word_diff
== DIFF_WORDS_NONE
)
4685 options
->word_diff
= DIFF_WORDS_PLAIN
;
4687 else if (skip_prefix(arg
, "--word-diff=", &arg
)) {
4688 if (!strcmp(arg
, "plain"))
4689 options
->word_diff
= DIFF_WORDS_PLAIN
;
4690 else if (!strcmp(arg
, "color")) {
4691 options
->use_color
= 1;
4692 options
->word_diff
= DIFF_WORDS_COLOR
;
4694 else if (!strcmp(arg
, "porcelain"))
4695 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
4696 else if (!strcmp(arg
, "none"))
4697 options
->word_diff
= DIFF_WORDS_NONE
;
4699 die("bad --word-diff argument: %s", arg
);
4701 else if ((argcount
= parse_long_opt("word-diff-regex", av
, &optarg
))) {
4702 if (options
->word_diff
== DIFF_WORDS_NONE
)
4703 options
->word_diff
= DIFF_WORDS_PLAIN
;
4704 options
->word_regex
= optarg
;
4707 else if (!strcmp(arg
, "--exit-code"))
4708 options
->flags
.exit_with_status
= 1;
4709 else if (!strcmp(arg
, "--quiet"))
4710 options
->flags
.quick
= 1;
4711 else if (!strcmp(arg
, "--ext-diff"))
4712 options
->flags
.allow_external
= 1;
4713 else if (!strcmp(arg
, "--no-ext-diff"))
4714 options
->flags
.allow_external
= 0;
4715 else if (!strcmp(arg
, "--textconv")) {
4716 options
->flags
.allow_textconv
= 1;
4717 options
->flags
.textconv_set_via_cmdline
= 1;
4718 } else if (!strcmp(arg
, "--no-textconv"))
4719 options
->flags
.allow_textconv
= 0;
4720 else if (skip_to_optional_arg_default(arg
, "--ignore-submodules", &arg
, "all")) {
4721 options
->flags
.override_submodule_config
= 1;
4722 handle_ignore_submodules_arg(options
, arg
);
4723 } else if (skip_to_optional_arg_default(arg
, "--submodule", &arg
, "log"))
4724 return parse_submodule_opt(options
, arg
);
4725 else if (skip_prefix(arg
, "--ws-error-highlight=", &arg
))
4726 return parse_ws_error_highlight_opt(options
, arg
);
4727 else if (!strcmp(arg
, "--ita-invisible-in-index"))
4728 options
->ita_invisible_in_index
= 1;
4729 else if (!strcmp(arg
, "--ita-visible-in-index"))
4730 options
->ita_invisible_in_index
= 0;
4733 else if (!strcmp(arg
, "-z"))
4734 options
->line_termination
= 0;
4735 else if ((argcount
= short_opt('l', av
, &optarg
))) {
4736 options
->rename_limit
= strtoul(optarg
, NULL
, 10);
4739 else if ((argcount
= short_opt('S', av
, &optarg
))) {
4740 options
->pickaxe
= optarg
;
4741 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
4743 } else if ((argcount
= short_opt('G', av
, &optarg
))) {
4744 options
->pickaxe
= optarg
;
4745 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
4748 else if (!strcmp(arg
, "--pickaxe-all"))
4749 options
->pickaxe_opts
|= DIFF_PICKAXE_ALL
;
4750 else if (!strcmp(arg
, "--pickaxe-regex"))
4751 options
->pickaxe_opts
|= DIFF_PICKAXE_REGEX
;
4752 else if ((argcount
= short_opt('O', av
, &optarg
))) {
4753 options
->orderfile
= prefix_filename(prefix
, optarg
);
4755 } else if (skip_prefix(arg
, "--find-object=", &arg
))
4756 return parse_objfind_opt(options
, arg
);
4757 else if ((argcount
= parse_long_opt("diff-filter", av
, &optarg
))) {
4758 int offending
= parse_diff_filter_opt(optarg
, options
);
4760 die("unknown change class '%c' in --diff-filter=%s",
4764 else if (!strcmp(arg
, "--no-abbrev"))
4765 options
->abbrev
= 0;
4766 else if (!strcmp(arg
, "--abbrev"))
4767 options
->abbrev
= DEFAULT_ABBREV
;
4768 else if (skip_prefix(arg
, "--abbrev=", &arg
)) {
4769 options
->abbrev
= strtoul(arg
, NULL
, 10);
4770 if (options
->abbrev
< MINIMUM_ABBREV
)
4771 options
->abbrev
= MINIMUM_ABBREV
;
4772 else if (40 < options
->abbrev
)
4773 options
->abbrev
= 40;
4775 else if ((argcount
= parse_long_opt("src-prefix", av
, &optarg
))) {
4776 options
->a_prefix
= optarg
;
4779 else if ((argcount
= parse_long_opt("line-prefix", av
, &optarg
))) {
4780 options
->line_prefix
= optarg
;
4781 options
->line_prefix_length
= strlen(options
->line_prefix
);
4782 graph_setup_line_prefix(options
);
4785 else if ((argcount
= parse_long_opt("dst-prefix", av
, &optarg
))) {
4786 options
->b_prefix
= optarg
;
4789 else if (!strcmp(arg
, "--no-prefix"))
4790 options
->a_prefix
= options
->b_prefix
= "";
4791 else if (opt_arg(arg
, '\0', "inter-hunk-context",
4792 &options
->interhunkcontext
))
4794 else if (!strcmp(arg
, "-W"))
4795 options
->flags
.funccontext
= 1;
4796 else if (!strcmp(arg
, "--function-context"))
4797 options
->flags
.funccontext
= 1;
4798 else if (!strcmp(arg
, "--no-function-context"))
4799 options
->flags
.funccontext
= 0;
4800 else if ((argcount
= parse_long_opt("output", av
, &optarg
))) {
4801 char *path
= prefix_filename(prefix
, optarg
);
4802 options
->file
= xfopen(path
, "w");
4803 options
->close_file
= 1;
4804 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
4805 options
->use_color
= GIT_COLOR_NEVER
;
4813 int parse_rename_score(const char **cp_p
)
4815 unsigned long num
, scale
;
4817 const char *cp
= *cp_p
;
4824 if ( !dot
&& ch
== '.' ) {
4827 } else if ( ch
== '%' ) {
4828 scale
= dot
? scale
*100 : 100;
4829 cp
++; /* % is always at the end */
4831 } else if ( ch
>= '0' && ch
<= '9' ) {
4832 if ( scale
< 100000 ) {
4834 num
= (num
*10) + (ch
-'0');
4843 /* user says num divided by scale and we say internally that
4844 * is MAX_SCORE * num / scale.
4846 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
4849 static int diff_scoreopt_parse(const char *opt
)
4851 int opt1
, opt2
, cmd
;
4857 /* convert the long-form arguments into short-form versions */
4858 if (skip_prefix(opt
, "break-rewrites", &opt
)) {
4859 if (*opt
== 0 || *opt
++ == '=')
4861 } else if (skip_prefix(opt
, "find-copies", &opt
)) {
4862 if (*opt
== 0 || *opt
++ == '=')
4864 } else if (skip_prefix(opt
, "find-renames", &opt
)) {
4865 if (*opt
== 0 || *opt
++ == '=')
4869 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
4870 return -1; /* that is not a -M, -C, or -B option */
4872 opt1
= parse_rename_score(&opt
);
4878 else if (*opt
!= '/')
4879 return -1; /* we expect -B80/99 or -B80 */
4882 opt2
= parse_rename_score(&opt
);
4887 return opt1
| (opt2
<< 16);
4890 struct diff_queue_struct diff_queued_diff
;
4892 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
4894 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
4895 queue
->queue
[queue
->nr
++] = dp
;
4898 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
4899 struct diff_filespec
*one
,
4900 struct diff_filespec
*two
)
4902 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
4910 void diff_free_filepair(struct diff_filepair
*p
)
4912 free_filespec(p
->one
);
4913 free_filespec(p
->two
);
4917 const char *diff_aligned_abbrev(const struct object_id
*oid
, int len
)
4922 /* Do we want all 40 hex characters? */
4923 if (len
== GIT_SHA1_HEXSZ
)
4924 return oid_to_hex(oid
);
4926 /* An abbreviated value is fine, possibly followed by an ellipsis. */
4927 abbrev
= diff_abbrev_oid(oid
, len
);
4929 if (!print_sha1_ellipsis())
4932 abblen
= strlen(abbrev
);
4935 * In well-behaved cases, where the abbreviated result is the
4936 * same as the requested length, append three dots after the
4937 * abbreviation (hence the whole logic is limited to the case
4938 * where abblen < 37); when the actual abbreviated result is a
4939 * bit longer than the requested length, we reduce the number
4940 * of dots so that they match the well-behaved ones. However,
4941 * if the actual abbreviation is longer than the requested
4942 * length by more than three, we give up on aligning, and add
4943 * three dots anyway, to indicate that the output is not the
4944 * full object name. Yes, this may be suboptimal, but this
4945 * appears only in "diff --raw --abbrev" output and it is not
4946 * worth the effort to change it now. Note that this would
4947 * likely to work fine when the automatic sizing of default
4948 * abbreviation length is used--we would be fed -1 in "len" in
4949 * that case, and will end up always appending three-dots, but
4950 * the automatic sizing is supposed to give abblen that ensures
4951 * uniqueness across all objects (statistically speaking).
4953 if (abblen
< GIT_SHA1_HEXSZ
- 3) {
4954 static char hex
[GIT_MAX_HEXSZ
+ 1];
4955 if (len
< abblen
&& abblen
<= len
+ 2)
4956 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
4958 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
4962 return oid_to_hex(oid
);
4965 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
4967 int line_termination
= opt
->line_termination
;
4968 int inter_name_termination
= line_termination
? '\t' : '\0';
4970 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
4971 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
4972 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
4973 diff_aligned_abbrev(&p
->one
->oid
, opt
->abbrev
));
4974 fprintf(opt
->file
, "%s ",
4975 diff_aligned_abbrev(&p
->two
->oid
, opt
->abbrev
));
4978 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
4979 inter_name_termination
);
4981 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
4984 if (p
->status
== DIFF_STATUS_COPIED
||
4985 p
->status
== DIFF_STATUS_RENAMED
) {
4986 const char *name_a
, *name_b
;
4987 name_a
= p
->one
->path
;
4988 name_b
= p
->two
->path
;
4989 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
4990 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
4991 write_name_quoted(name_b
, opt
->file
, line_termination
);
4993 const char *name_a
, *name_b
;
4994 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
4996 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
4997 write_name_quoted(name_a
, opt
->file
, line_termination
);
5001 int diff_unmodified_pair(struct diff_filepair
*p
)
5003 /* This function is written stricter than necessary to support
5004 * the currently implemented transformers, but the idea is to
5005 * let transformers to produce diff_filepairs any way they want,
5006 * and filter and clean them up here before producing the output.
5008 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
5010 if (DIFF_PAIR_UNMERGED(p
))
5011 return 0; /* unmerged is interesting */
5013 /* deletion, addition, mode or type change
5014 * and rename are all interesting.
5016 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
5017 DIFF_PAIR_MODE_CHANGED(p
) ||
5018 strcmp(one
->path
, two
->path
))
5021 /* both are valid and point at the same path. that is, we are
5022 * dealing with a change.
5024 if (one
->oid_valid
&& two
->oid_valid
&&
5025 !oidcmp(&one
->oid
, &two
->oid
) &&
5026 !one
->dirty_submodule
&& !two
->dirty_submodule
)
5027 return 1; /* no change */
5028 if (!one
->oid_valid
&& !two
->oid_valid
)
5029 return 1; /* both look at the same file on the filesystem. */
5033 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
5035 if (diff_unmodified_pair(p
))
5038 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5039 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5040 return; /* no tree diffs in patch format */
5045 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
5046 struct diffstat_t
*diffstat
)
5048 if (diff_unmodified_pair(p
))
5051 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5052 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5053 return; /* no useful stat for tree diffs */
5055 run_diffstat(p
, o
, diffstat
);
5058 static void diff_flush_checkdiff(struct diff_filepair
*p
,
5059 struct diff_options
*o
)
5061 if (diff_unmodified_pair(p
))
5064 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5065 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5066 return; /* nothing to check in tree diffs */
5068 run_checkdiff(p
, o
);
5071 int diff_queue_is_empty(void)
5073 struct diff_queue_struct
*q
= &diff_queued_diff
;
5075 for (i
= 0; i
< q
->nr
; i
++)
5076 if (!diff_unmodified_pair(q
->queue
[i
]))
5082 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
5084 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
5087 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
5089 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
5090 fprintf(stderr
, "queue[%d] %s size %lu\n",
5095 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
5097 diff_debug_filespec(p
->one
, i
, "one");
5098 diff_debug_filespec(p
->two
, i
, "two");
5099 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
5100 p
->score
, p
->status
? p
->status
: '?',
5101 p
->one
->rename_used
, p
->broken_pair
);
5104 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
5108 fprintf(stderr
, "%s\n", msg
);
5109 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
5110 for (i
= 0; i
< q
->nr
; i
++) {
5111 struct diff_filepair
*p
= q
->queue
[i
];
5112 diff_debug_filepair(p
, i
);
5117 static void diff_resolve_rename_copy(void)
5120 struct diff_filepair
*p
;
5121 struct diff_queue_struct
*q
= &diff_queued_diff
;
5123 diff_debug_queue("resolve-rename-copy", q
);
5125 for (i
= 0; i
< q
->nr
; i
++) {
5127 p
->status
= 0; /* undecided */
5128 if (DIFF_PAIR_UNMERGED(p
))
5129 p
->status
= DIFF_STATUS_UNMERGED
;
5130 else if (!DIFF_FILE_VALID(p
->one
))
5131 p
->status
= DIFF_STATUS_ADDED
;
5132 else if (!DIFF_FILE_VALID(p
->two
))
5133 p
->status
= DIFF_STATUS_DELETED
;
5134 else if (DIFF_PAIR_TYPE_CHANGED(p
))
5135 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
5137 /* from this point on, we are dealing with a pair
5138 * whose both sides are valid and of the same type, i.e.
5139 * either in-place edit or rename/copy edit.
5141 else if (DIFF_PAIR_RENAME(p
)) {
5143 * A rename might have re-connected a broken
5144 * pair up, causing the pathnames to be the
5145 * same again. If so, that's not a rename at
5146 * all, just a modification..
5148 * Otherwise, see if this source was used for
5149 * multiple renames, in which case we decrement
5150 * the count, and call it a copy.
5152 if (!strcmp(p
->one
->path
, p
->two
->path
))
5153 p
->status
= DIFF_STATUS_MODIFIED
;
5154 else if (--p
->one
->rename_used
> 0)
5155 p
->status
= DIFF_STATUS_COPIED
;
5157 p
->status
= DIFF_STATUS_RENAMED
;
5159 else if (oidcmp(&p
->one
->oid
, &p
->two
->oid
) ||
5160 p
->one
->mode
!= p
->two
->mode
||
5161 p
->one
->dirty_submodule
||
5162 p
->two
->dirty_submodule
||
5163 is_null_oid(&p
->one
->oid
))
5164 p
->status
= DIFF_STATUS_MODIFIED
;
5166 /* This is a "no-change" entry and should not
5167 * happen anymore, but prepare for broken callers.
5169 error("feeding unmodified %s to diffcore",
5171 p
->status
= DIFF_STATUS_UNKNOWN
;
5174 diff_debug_queue("resolve-rename-copy done", q
);
5177 static int check_pair_status(struct diff_filepair
*p
)
5179 switch (p
->status
) {
5180 case DIFF_STATUS_UNKNOWN
:
5183 die("internal error in diff-resolve-rename-copy");
5189 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
5191 int fmt
= opt
->output_format
;
5193 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
5194 diff_flush_checkdiff(p
, opt
);
5195 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
5196 diff_flush_raw(p
, opt
);
5197 else if (fmt
& DIFF_FORMAT_NAME
) {
5198 const char *name_a
, *name_b
;
5199 name_a
= p
->two
->path
;
5201 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5202 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
5203 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
5207 static void show_file_mode_name(struct diff_options
*opt
, const char *newdelete
, struct diff_filespec
*fs
)
5209 struct strbuf sb
= STRBUF_INIT
;
5211 strbuf_addf(&sb
, " %s mode %06o ", newdelete
, fs
->mode
);
5213 strbuf_addf(&sb
, " %s ", newdelete
);
5215 quote_c_style(fs
->path
, &sb
, NULL
, 0);
5216 strbuf_addch(&sb
, '\n');
5217 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
5219 strbuf_release(&sb
);
5222 static void show_mode_change(struct diff_options
*opt
, struct diff_filepair
*p
,
5225 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
5226 struct strbuf sb
= STRBUF_INIT
;
5227 strbuf_addf(&sb
, " mode change %06o => %06o",
5228 p
->one
->mode
, p
->two
->mode
);
5230 strbuf_addch(&sb
, ' ');
5231 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
5233 strbuf_addch(&sb
, '\n');
5234 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
5236 strbuf_release(&sb
);
5240 static void show_rename_copy(struct diff_options
*opt
, const char *renamecopy
,
5241 struct diff_filepair
*p
)
5243 struct strbuf sb
= STRBUF_INIT
;
5244 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
5245 strbuf_addf(&sb
, " %s %s (%d%%)\n",
5246 renamecopy
, names
, similarity_index(p
));
5248 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
5250 show_mode_change(opt
, p
, 0);
5251 strbuf_release(&sb
);
5254 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
5257 case DIFF_STATUS_DELETED
:
5258 show_file_mode_name(opt
, "delete", p
->one
);
5260 case DIFF_STATUS_ADDED
:
5261 show_file_mode_name(opt
, "create", p
->two
);
5263 case DIFF_STATUS_COPIED
:
5264 show_rename_copy(opt
, "copy", p
);
5266 case DIFF_STATUS_RENAMED
:
5267 show_rename_copy(opt
, "rename", p
);
5271 struct strbuf sb
= STRBUF_INIT
;
5272 strbuf_addstr(&sb
, " rewrite ");
5273 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
5274 strbuf_addf(&sb
, " (%d%%)\n", similarity_index(p
));
5275 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
5277 strbuf_release(&sb
);
5279 show_mode_change(opt
, p
, !p
->score
);
5289 static int remove_space(char *line
, int len
)
5295 for (i
= 0; i
< len
; i
++)
5296 if (!isspace((c
= line
[i
])))
5302 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
5304 struct patch_id_t
*data
= priv
;
5307 /* Ignore line numbers when computing the SHA1 of the patch */
5308 if (starts_with(line
, "@@ -"))
5311 new_len
= remove_space(line
, len
);
5313 git_SHA1_Update(data
->ctx
, line
, new_len
);
5314 data
->patchlen
+= new_len
;
5317 static void patch_id_add_string(git_SHA_CTX
*ctx
, const char *str
)
5319 git_SHA1_Update(ctx
, str
, strlen(str
));
5322 static void patch_id_add_mode(git_SHA_CTX
*ctx
, unsigned mode
)
5324 /* large enough for 2^32 in octal */
5326 int len
= xsnprintf(buf
, sizeof(buf
), "%06o", mode
);
5327 git_SHA1_Update(ctx
, buf
, len
);
5330 /* returns 0 upon success, and writes result into sha1 */
5331 static int diff_get_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
5333 struct diff_queue_struct
*q
= &diff_queued_diff
;
5336 struct patch_id_t data
;
5338 git_SHA1_Init(&ctx
);
5339 memset(&data
, 0, sizeof(struct patch_id_t
));
5342 for (i
= 0; i
< q
->nr
; i
++) {
5346 struct diff_filepair
*p
= q
->queue
[i
];
5349 memset(&xpp
, 0, sizeof(xpp
));
5350 memset(&xecfg
, 0, sizeof(xecfg
));
5352 return error("internal diff status error");
5353 if (p
->status
== DIFF_STATUS_UNKNOWN
)
5355 if (diff_unmodified_pair(p
))
5357 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5358 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5360 if (DIFF_PAIR_UNMERGED(p
))
5363 diff_fill_oid_info(p
->one
);
5364 diff_fill_oid_info(p
->two
);
5366 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
5367 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
5368 patch_id_add_string(&ctx
, "diff--git");
5369 patch_id_add_string(&ctx
, "a/");
5370 git_SHA1_Update(&ctx
, p
->one
->path
, len1
);
5371 patch_id_add_string(&ctx
, "b/");
5372 git_SHA1_Update(&ctx
, p
->two
->path
, len2
);
5374 if (p
->one
->mode
== 0) {
5375 patch_id_add_string(&ctx
, "newfilemode");
5376 patch_id_add_mode(&ctx
, p
->two
->mode
);
5377 patch_id_add_string(&ctx
, "---/dev/null");
5378 patch_id_add_string(&ctx
, "+++b/");
5379 git_SHA1_Update(&ctx
, p
->two
->path
, len2
);
5380 } else if (p
->two
->mode
== 0) {
5381 patch_id_add_string(&ctx
, "deletedfilemode");
5382 patch_id_add_mode(&ctx
, p
->one
->mode
);
5383 patch_id_add_string(&ctx
, "---a/");
5384 git_SHA1_Update(&ctx
, p
->one
->path
, len1
);
5385 patch_id_add_string(&ctx
, "+++/dev/null");
5387 patch_id_add_string(&ctx
, "---a/");
5388 git_SHA1_Update(&ctx
, p
->one
->path
, len1
);
5389 patch_id_add_string(&ctx
, "+++b/");
5390 git_SHA1_Update(&ctx
, p
->two
->path
, len2
);
5393 if (diff_header_only
)
5396 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
5397 fill_mmfile(&mf2
, p
->two
) < 0)
5398 return error("unable to read files to diff");
5400 if (diff_filespec_is_binary(p
->one
) ||
5401 diff_filespec_is_binary(p
->two
)) {
5402 git_SHA1_Update(&ctx
, oid_to_hex(&p
->one
->oid
),
5404 git_SHA1_Update(&ctx
, oid_to_hex(&p
->two
->oid
),
5412 if (xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
5414 return error("unable to generate patch-id diff for %s",
5418 git_SHA1_Final(oid
->hash
, &ctx
);
5422 int diff_flush_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
5424 struct diff_queue_struct
*q
= &diff_queued_diff
;
5426 int result
= diff_get_patch_id(options
, oid
, diff_header_only
);
5428 for (i
= 0; i
< q
->nr
; i
++)
5429 diff_free_filepair(q
->queue
[i
]);
5432 DIFF_QUEUE_CLEAR(q
);
5437 static int is_summary_empty(const struct diff_queue_struct
*q
)
5441 for (i
= 0; i
< q
->nr
; i
++) {
5442 const struct diff_filepair
*p
= q
->queue
[i
];
5444 switch (p
->status
) {
5445 case DIFF_STATUS_DELETED
:
5446 case DIFF_STATUS_ADDED
:
5447 case DIFF_STATUS_COPIED
:
5448 case DIFF_STATUS_RENAMED
:
5453 if (p
->one
->mode
&& p
->two
->mode
&&
5454 p
->one
->mode
!= p
->two
->mode
)
5462 static const char rename_limit_warning
[] =
5463 N_("inexact rename detection was skipped due to too many files.");
5465 static const char degrade_cc_to_c_warning
[] =
5466 N_("only found copies from modified paths due to too many files.");
5468 static const char rename_limit_advice
[] =
5469 N_("you may want to set your %s variable to at least "
5470 "%d and retry the command.");
5472 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
5476 warning(_(degrade_cc_to_c_warning
));
5478 warning(_(rename_limit_warning
));
5482 warning(_(rename_limit_advice
), varname
, needed
);
5485 static void diff_flush_patch_all_file_pairs(struct diff_options
*o
)
5488 static struct emitted_diff_symbols esm
= EMITTED_DIFF_SYMBOLS_INIT
;
5489 struct diff_queue_struct
*q
= &diff_queued_diff
;
5491 if (WSEH_NEW
& WS_RULE_MASK
)
5492 die("BUG: WS rules bit mask overlaps with diff symbol flags");
5495 o
->emitted_symbols
= &esm
;
5497 for (i
= 0; i
< q
->nr
; i
++) {
5498 struct diff_filepair
*p
= q
->queue
[i
];
5499 if (check_pair_status(p
))
5500 diff_flush_patch(p
, o
);
5503 if (o
->emitted_symbols
) {
5504 if (o
->color_moved
) {
5505 struct hashmap add_lines
, del_lines
;
5507 hashmap_init(&del_lines
,
5508 (hashmap_cmp_fn
)moved_entry_cmp
, o
, 0);
5509 hashmap_init(&add_lines
,
5510 (hashmap_cmp_fn
)moved_entry_cmp
, o
, 0);
5512 add_lines_to_move_detection(o
, &add_lines
, &del_lines
);
5513 mark_color_as_moved(o
, &add_lines
, &del_lines
);
5514 if (o
->color_moved
== COLOR_MOVED_ZEBRA_DIM
)
5517 hashmap_free(&add_lines
, 0);
5518 hashmap_free(&del_lines
, 0);
5521 for (i
= 0; i
< esm
.nr
; i
++)
5522 emit_diff_symbol_from_struct(o
, &esm
.buf
[i
]);
5524 for (i
= 0; i
< esm
.nr
; i
++)
5525 free((void *)esm
.buf
[i
].line
);
5530 void diff_flush(struct diff_options
*options
)
5532 struct diff_queue_struct
*q
= &diff_queued_diff
;
5533 int i
, output_format
= options
->output_format
;
5535 int dirstat_by_line
= 0;
5538 * Order: raw, stat, summary, patch
5539 * or: name/name-status/checkdiff (other bits clear)
5544 if (output_format
& (DIFF_FORMAT_RAW
|
5546 DIFF_FORMAT_NAME_STATUS
|
5547 DIFF_FORMAT_CHECKDIFF
)) {
5548 for (i
= 0; i
< q
->nr
; i
++) {
5549 struct diff_filepair
*p
= q
->queue
[i
];
5550 if (check_pair_status(p
))
5551 flush_one_pair(p
, options
);
5556 if (output_format
& DIFF_FORMAT_DIRSTAT
&& options
->flags
.dirstat_by_line
)
5557 dirstat_by_line
= 1;
5559 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
5561 struct diffstat_t diffstat
;
5563 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
5564 for (i
= 0; i
< q
->nr
; i
++) {
5565 struct diff_filepair
*p
= q
->queue
[i
];
5566 if (check_pair_status(p
))
5567 diff_flush_stat(p
, options
, &diffstat
);
5569 if (output_format
& DIFF_FORMAT_NUMSTAT
)
5570 show_numstat(&diffstat
, options
);
5571 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
5572 show_stats(&diffstat
, options
);
5573 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
5574 show_shortstats(&diffstat
, options
);
5575 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
5576 show_dirstat_by_line(&diffstat
, options
);
5577 free_diffstat_info(&diffstat
);
5580 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
5581 show_dirstat(options
);
5583 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
5584 for (i
= 0; i
< q
->nr
; i
++) {
5585 diff_summary(options
, q
->queue
[i
]);
5590 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
5591 options
->flags
.exit_with_status
&&
5592 options
->flags
.diff_from_contents
) {
5594 * run diff_flush_patch for the exit status. setting
5595 * options->file to /dev/null should be safe, because we
5596 * aren't supposed to produce any output anyway.
5598 if (options
->close_file
)
5599 fclose(options
->file
);
5600 options
->file
= xfopen("/dev/null", "w");
5601 options
->close_file
= 1;
5602 options
->color_moved
= 0;
5603 for (i
= 0; i
< q
->nr
; i
++) {
5604 struct diff_filepair
*p
= q
->queue
[i
];
5605 if (check_pair_status(p
))
5606 diff_flush_patch(p
, options
);
5607 if (options
->found_changes
)
5612 if (output_format
& DIFF_FORMAT_PATCH
) {
5614 emit_diff_symbol(options
, DIFF_SYMBOL_SEPARATOR
, NULL
, 0, 0);
5615 if (options
->stat_sep
)
5616 /* attach patch instead of inline */
5617 emit_diff_symbol(options
, DIFF_SYMBOL_STAT_SEP
,
5621 diff_flush_patch_all_file_pairs(options
);
5624 if (output_format
& DIFF_FORMAT_CALLBACK
)
5625 options
->format_callback(q
, options
, options
->format_callback_data
);
5627 for (i
= 0; i
< q
->nr
; i
++)
5628 diff_free_filepair(q
->queue
[i
]);
5631 DIFF_QUEUE_CLEAR(q
);
5632 if (options
->close_file
)
5633 fclose(options
->file
);
5636 * Report the content-level differences with HAS_CHANGES;
5637 * diff_addremove/diff_change does not set the bit when
5638 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
5640 if (options
->flags
.diff_from_contents
) {
5641 if (options
->found_changes
)
5642 options
->flags
.has_changes
= 1;
5644 options
->flags
.has_changes
= 0;
5648 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
5650 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
5652 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
5654 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
5655 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
5656 filter_bit_tst(p
->status
, options
)));
5659 static void diffcore_apply_filter(struct diff_options
*options
)
5662 struct diff_queue_struct
*q
= &diff_queued_diff
;
5663 struct diff_queue_struct outq
;
5665 DIFF_QUEUE_CLEAR(&outq
);
5667 if (!options
->filter
)
5670 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
5672 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
5673 if (match_filter(options
, q
->queue
[i
]))
5679 /* otherwise we will clear the whole queue
5680 * by copying the empty outq at the end of this
5681 * function, but first clear the current entries
5684 for (i
= 0; i
< q
->nr
; i
++)
5685 diff_free_filepair(q
->queue
[i
]);
5688 /* Only the matching ones */
5689 for (i
= 0; i
< q
->nr
; i
++) {
5690 struct diff_filepair
*p
= q
->queue
[i
];
5691 if (match_filter(options
, p
))
5694 diff_free_filepair(p
);
5701 /* Check whether two filespecs with the same mode and size are identical */
5702 static int diff_filespec_is_identical(struct diff_filespec
*one
,
5703 struct diff_filespec
*two
)
5705 if (S_ISGITLINK(one
->mode
))
5707 if (diff_populate_filespec(one
, 0))
5709 if (diff_populate_filespec(two
, 0))
5711 return !memcmp(one
->data
, two
->data
, one
->size
);
5714 static int diff_filespec_check_stat_unmatch(struct diff_filepair
*p
)
5716 if (p
->done_skip_stat_unmatch
)
5717 return p
->skip_stat_unmatch_result
;
5719 p
->done_skip_stat_unmatch
= 1;
5720 p
->skip_stat_unmatch_result
= 0;
5722 * 1. Entries that come from stat info dirtiness
5723 * always have both sides (iow, not create/delete),
5724 * one side of the object name is unknown, with
5725 * the same mode and size. Keep the ones that
5726 * do not match these criteria. They have real
5729 * 2. At this point, the file is known to be modified,
5730 * with the same mode and size, and the object
5731 * name of one side is unknown. Need to inspect
5732 * the identical contents.
5734 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
5735 !DIFF_FILE_VALID(p
->two
) ||
5736 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
5737 (p
->one
->mode
!= p
->two
->mode
) ||
5738 diff_populate_filespec(p
->one
, CHECK_SIZE_ONLY
) ||
5739 diff_populate_filespec(p
->two
, CHECK_SIZE_ONLY
) ||
5740 (p
->one
->size
!= p
->two
->size
) ||
5741 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
5742 p
->skip_stat_unmatch_result
= 1;
5743 return p
->skip_stat_unmatch_result
;
5746 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
5749 struct diff_queue_struct
*q
= &diff_queued_diff
;
5750 struct diff_queue_struct outq
;
5751 DIFF_QUEUE_CLEAR(&outq
);
5753 for (i
= 0; i
< q
->nr
; i
++) {
5754 struct diff_filepair
*p
= q
->queue
[i
];
5756 if (diff_filespec_check_stat_unmatch(p
))
5760 * The caller can subtract 1 from skip_stat_unmatch
5761 * to determine how many paths were dirty only
5762 * due to stat info mismatch.
5764 if (!diffopt
->flags
.no_index
)
5765 diffopt
->skip_stat_unmatch
++;
5766 diff_free_filepair(p
);
5773 static int diffnamecmp(const void *a_
, const void *b_
)
5775 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
5776 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
5777 const char *name_a
, *name_b
;
5779 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
5780 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
5781 return strcmp(name_a
, name_b
);
5784 void diffcore_fix_diff_index(struct diff_options
*options
)
5786 struct diff_queue_struct
*q
= &diff_queued_diff
;
5787 QSORT(q
->queue
, q
->nr
, diffnamecmp
);
5790 void diffcore_std(struct diff_options
*options
)
5792 /* NOTE please keep the following in sync with diff_tree_combined() */
5793 if (options
->skip_stat_unmatch
)
5794 diffcore_skip_stat_unmatch(options
);
5795 if (!options
->found_follow
) {
5796 /* See try_to_follow_renames() in tree-diff.c */
5797 if (options
->break_opt
!= -1)
5798 diffcore_break(options
->break_opt
);
5799 if (options
->detect_rename
)
5800 diffcore_rename(options
);
5801 if (options
->break_opt
!= -1)
5802 diffcore_merge_broken();
5804 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
5805 diffcore_pickaxe(options
);
5806 if (options
->orderfile
)
5807 diffcore_order(options
->orderfile
);
5808 if (!options
->found_follow
)
5809 /* See try_to_follow_renames() in tree-diff.c */
5810 diff_resolve_rename_copy();
5811 diffcore_apply_filter(options
);
5813 if (diff_queued_diff
.nr
&& !options
->flags
.diff_from_contents
)
5814 options
->flags
.has_changes
= 1;
5816 options
->flags
.has_changes
= 0;
5818 options
->found_follow
= 0;
5821 int diff_result_code(struct diff_options
*opt
, int status
)
5825 diff_warn_rename_limit("diff.renameLimit",
5826 opt
->needed_rename_limit
,
5827 opt
->degraded_cc_to_c
);
5828 if (!opt
->flags
.exit_with_status
&&
5829 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
5831 if (opt
->flags
.exit_with_status
&&
5832 opt
->flags
.has_changes
)
5834 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
5835 opt
->flags
.check_failed
)
5840 int diff_can_quit_early(struct diff_options
*opt
)
5842 return (opt
->flags
.quick
&&
5844 opt
->flags
.has_changes
);
5848 * Shall changes to this submodule be ignored?
5850 * Submodule changes can be configured to be ignored separately for each path,
5851 * but that configuration can be overridden from the command line.
5853 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
5856 struct diff_flags orig_flags
= options
->flags
;
5857 if (!options
->flags
.override_submodule_config
)
5858 set_diffopt_flags_from_submodule_config(options
, path
);
5859 if (options
->flags
.ignore_submodules
)
5861 options
->flags
= orig_flags
;
5865 void diff_addremove(struct diff_options
*options
,
5866 int addremove
, unsigned mode
,
5867 const struct object_id
*oid
,
5869 const char *concatpath
, unsigned dirty_submodule
)
5871 struct diff_filespec
*one
, *two
;
5873 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
5876 /* This may look odd, but it is a preparation for
5877 * feeding "there are unchanged files which should
5878 * not produce diffs, but when you are doing copy
5879 * detection you would need them, so here they are"
5880 * entries to the diff-core. They will be prefixed
5881 * with something like '=' or '*' (I haven't decided
5882 * which but should not make any difference).
5883 * Feeding the same new and old to diff_change()
5884 * also has the same effect.
5885 * Before the final output happens, they are pruned after
5886 * merged into rename/copy pairs as appropriate.
5888 if (options
->flags
.reverse_diff
)
5889 addremove
= (addremove
== '+' ? '-' :
5890 addremove
== '-' ? '+' : addremove
);
5892 if (options
->prefix
&&
5893 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
5896 one
= alloc_filespec(concatpath
);
5897 two
= alloc_filespec(concatpath
);
5899 if (addremove
!= '+')
5900 fill_filespec(one
, oid
, oid_valid
, mode
);
5901 if (addremove
!= '-') {
5902 fill_filespec(two
, oid
, oid_valid
, mode
);
5903 two
->dirty_submodule
= dirty_submodule
;
5906 diff_queue(&diff_queued_diff
, one
, two
);
5907 if (!options
->flags
.diff_from_contents
)
5908 options
->flags
.has_changes
= 1;
5911 void diff_change(struct diff_options
*options
,
5912 unsigned old_mode
, unsigned new_mode
,
5913 const struct object_id
*old_oid
,
5914 const struct object_id
*new_oid
,
5915 int old_oid_valid
, int new_oid_valid
,
5916 const char *concatpath
,
5917 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
5919 struct diff_filespec
*one
, *two
;
5920 struct diff_filepair
*p
;
5922 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
5923 is_submodule_ignored(concatpath
, options
))
5926 if (options
->flags
.reverse_diff
) {
5927 SWAP(old_mode
, new_mode
);
5928 SWAP(old_oid
, new_oid
);
5929 SWAP(old_oid_valid
, new_oid_valid
);
5930 SWAP(old_dirty_submodule
, new_dirty_submodule
);
5933 if (options
->prefix
&&
5934 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
5937 one
= alloc_filespec(concatpath
);
5938 two
= alloc_filespec(concatpath
);
5939 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
5940 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
5941 one
->dirty_submodule
= old_dirty_submodule
;
5942 two
->dirty_submodule
= new_dirty_submodule
;
5943 p
= diff_queue(&diff_queued_diff
, one
, two
);
5945 if (options
->flags
.diff_from_contents
)
5948 if (options
->flags
.quick
&& options
->skip_stat_unmatch
&&
5949 !diff_filespec_check_stat_unmatch(p
))
5952 options
->flags
.has_changes
= 1;
5955 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
5957 struct diff_filepair
*pair
;
5958 struct diff_filespec
*one
, *two
;
5960 if (options
->prefix
&&
5961 strncmp(path
, options
->prefix
, options
->prefix_length
))
5964 one
= alloc_filespec(path
);
5965 two
= alloc_filespec(path
);
5966 pair
= diff_queue(&diff_queued_diff
, one
, two
);
5967 pair
->is_unmerged
= 1;
5971 static char *run_textconv(const char *pgm
, struct diff_filespec
*spec
,
5974 struct diff_tempfile
*temp
;
5975 const char *argv
[3];
5976 const char **arg
= argv
;
5977 struct child_process child
= CHILD_PROCESS_INIT
;
5978 struct strbuf buf
= STRBUF_INIT
;
5981 temp
= prepare_temp_file(spec
->path
, spec
);
5983 *arg
++ = temp
->name
;
5986 child
.use_shell
= 1;
5989 if (start_command(&child
)) {
5994 if (strbuf_read(&buf
, child
.out
, 0) < 0)
5995 err
= error("error reading from textconv command '%s'", pgm
);
5998 if (finish_command(&child
) || err
) {
5999 strbuf_release(&buf
);
6005 return strbuf_detach(&buf
, outsize
);
6008 size_t fill_textconv(struct userdiff_driver
*driver
,
6009 struct diff_filespec
*df
,
6015 if (!DIFF_FILE_VALID(df
)) {
6019 if (diff_populate_filespec(df
, 0))
6020 die("unable to read files to diff");
6025 if (!driver
->textconv
)
6026 die("BUG: fill_textconv called with non-textconv driver");
6028 if (driver
->textconv_cache
&& df
->oid_valid
) {
6029 *outbuf
= notes_cache_get(driver
->textconv_cache
,
6036 *outbuf
= run_textconv(driver
->textconv
, df
, &size
);
6038 die("unable to read files to diff");
6040 if (driver
->textconv_cache
&& df
->oid_valid
) {
6041 /* ignore errors, as we might be in a readonly repository */
6042 notes_cache_put(driver
->textconv_cache
, &df
->oid
, *outbuf
,
6045 * we could save up changes and flush them all at the end,
6046 * but we would need an extra call after all diffing is done.
6047 * Since generating a cache entry is the slow path anyway,
6048 * this extra overhead probably isn't a big deal.
6050 notes_cache_write(driver
->textconv_cache
);
6056 int textconv_object(const char *path
,
6058 const struct object_id
*oid
,
6061 unsigned long *buf_size
)
6063 struct diff_filespec
*df
;
6064 struct userdiff_driver
*textconv
;
6066 df
= alloc_filespec(path
);
6067 fill_filespec(df
, oid
, oid_valid
, mode
);
6068 textconv
= get_textconv(df
);
6074 *buf_size
= fill_textconv(textconv
, df
, buf
);
6079 void setup_diff_pager(struct diff_options
*opt
)
6082 * If the user asked for our exit code, then either they want --quiet
6083 * or --exit-code. We should definitely not bother with a pager in the
6084 * former case, as we will generate no output. Since we still properly
6085 * report our exit code even when a pager is run, we _could_ run a
6086 * pager with --exit-code. But since we have not done so historically,
6087 * and because it is easy to find people oneline advising "git diff
6088 * --exit-code" in hooks and other scripts, we do not do so.
6090 if (!opt
->flags
.exit_with_status
&&
6091 check_pager_config("diff") != 0)