Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Junio C Hamano |
| 3 | */ |
Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 4 | #include "git-compat-util.h" |
Elijah Newren | 0b027f6 | 2023-03-21 06:25:58 +0000 | [diff] [blame] | 5 | #include "abspath.h" |
Elijah Newren | 9b5041f | 2023-04-22 20:17:13 +0000 | [diff] [blame] | 6 | #include "base85.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 7 | #include "config.h" |
Elijah Newren | 73359a9 | 2023-04-11 03:00:40 +0000 | [diff] [blame] | 8 | #include "convert.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 9 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 10 | #include "gettext.h" |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 11 | #include "tempfile.h" |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 12 | #include "quote.h" |
| 13 | #include "diff.h" |
| 14 | #include "diffcore.h" |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 15 | #include "delta.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 16 | #include "hex.h" |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 17 | #include "xdiff-interface.h" |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 18 | #include "color.h" |
Johannes Sixt | d5535ec | 2007-10-19 21:47:56 +0200 | [diff] [blame] | 19 | #include "run-command.h" |
Junio C Hamano | 2370781 | 2008-01-02 01:50:11 -0800 | [diff] [blame] | 20 | #include "utf8.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 21 | #include "object-store-ll.h" |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 22 | #include "userdiff.h" |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 23 | #include "submodule.h" |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 24 | #include "hashmap.h" |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 25 | #include "mem-pool.h" |
Elijah Newren | 6723899 | 2023-05-16 06:34:04 +0000 | [diff] [blame] | 26 | #include "merge-ll.h" |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 27 | #include "string-list.h" |
Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 28 | #include "strvec.h" |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 29 | #include "graph.h" |
Elijah Newren | 6f2d743 | 2023-04-11 03:00:42 +0000 | [diff] [blame] | 30 | #include "oid-array.h" |
Jonathan Tan | 150e300 | 2017-08-18 15:20:36 -0700 | [diff] [blame] | 31 | #include "packfile.h" |
Elijah Newren | ca4eed7 | 2023-04-11 00:41:59 -0700 | [diff] [blame] | 32 | #include "pager.h" |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 33 | #include "parse-options.h" |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 34 | #include "help.h" |
Christian Couder | b14ed5a | 2019-06-25 15:40:31 +0200 | [diff] [blame] | 35 | #include "promisor-remote.h" |
Derrick Stolee | ad90da7 | 2021-09-08 11:23:56 +0000 | [diff] [blame] | 36 | #include "dir.h" |
Elijah Newren | 87bed17 | 2023-04-11 00:41:53 -0700 | [diff] [blame] | 37 | #include "object-file.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 38 | #include "object-name.h" |
Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 +0000 | [diff] [blame] | 39 | #include "read-cache-ll.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 40 | #include "setup.h" |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 41 | #include "strmap.h" |
Elijah Newren | 6412231 | 2023-04-22 20:17:16 +0000 | [diff] [blame] | 42 | #include "ws.h" |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 43 | |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 44 | #ifdef NO_FAST_WORKING_DIRECTORY |
| 45 | #define FAST_WORKING_DIRECTORY 0 |
| 46 | #else |
| 47 | #define FAST_WORKING_DIRECTORY 1 |
| 48 | #endif |
| 49 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 50 | static int diff_detect_rename_default; |
Stefan Beller | 33de716 | 2017-05-08 12:03:38 -0400 | [diff] [blame] | 51 | static int diff_indent_heuristic = 1; |
Elijah Newren | 94b82d5 | 2021-07-15 00:45:24 +0000 | [diff] [blame] | 52 | static int diff_rename_limit_default = 1000; |
Jim Meyering | a624eaa | 2008-08-15 13:39:26 +0200 | [diff] [blame] | 53 | static int diff_suppress_blank_empty; |
Junio C Hamano | d2aea13 | 2012-09-15 13:59:59 -0700 | [diff] [blame] | 54 | static int diff_use_color_default = -1; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 55 | static int diff_color_moved_default; |
Stefan Beller | 626c0b5 | 2018-07-18 12:31:56 -0700 | [diff] [blame] | 56 | static int diff_color_moved_ws_default; |
Jeff Muizelaar | 6468a4e | 2012-09-27 15:12:52 -0400 | [diff] [blame] | 57 | static int diff_context_default = 3; |
Vegard Nossum | c488867 | 2017-01-12 13:21:11 +0100 | [diff] [blame] | 58 | static int diff_interhunk_context_default; |
Boyd Stephen Smith Jr | 98a4d87 | 2009-01-20 21:46:57 -0600 | [diff] [blame] | 59 | static const char *diff_word_regex_cfg; |
Johannes Schindelin | cbe0210 | 2007-12-17 13:42:20 +0000 | [diff] [blame] | 60 | static const char *external_diff_cmd_cfg; |
Samuel Bronson | 6d8940b | 2013-12-18 19:08:12 -0500 | [diff] [blame] | 61 | static const char *diff_order_file_cfg; |
Junio C Hamano | aecbf91 | 2007-08-31 13:13:42 -0700 | [diff] [blame] | 62 | int diff_auto_refresh_index = 1; |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 63 | static int diff_mnemonic_prefix; |
Eli Collins | f89504d | 2010-05-02 19:03:41 -0700 | [diff] [blame] | 64 | static int diff_no_prefix; |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 65 | static int diff_relative; |
Dragan Simic | bd48adc | 2023-09-11 17:39:44 +0200 | [diff] [blame] | 66 | static int diff_stat_name_width; |
Zbigniew Jędrzejewski-Szmek | df44483 | 2012-03-01 13:26:46 +0100 | [diff] [blame] | 67 | static int diff_stat_graph_width; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 68 | static int diff_dirstat_permille_default = 30; |
Johannes Schindelin | be4f2b4 | 2010-08-05 10:49:55 +0200 | [diff] [blame] | 69 | static struct diff_options default_diff_options; |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 70 | static long diff_algorithm; |
Junio C Hamano | a17505f | 2016-10-04 15:26:27 -0700 | [diff] [blame] | 71 | static unsigned ws_error_highlight_default = WSEH_NEW; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 72 | |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 73 | static char diff_colors[][COLOR_MAXLEN] = { |
Arjen Laarhoven | dc6ebd4 | 2009-02-13 22:53:40 +0100 | [diff] [blame] | 74 | GIT_COLOR_RESET, |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 75 | GIT_COLOR_NORMAL, /* CONTEXT */ |
Arjen Laarhoven | dc6ebd4 | 2009-02-13 22:53:40 +0100 | [diff] [blame] | 76 | GIT_COLOR_BOLD, /* METAINFO */ |
| 77 | GIT_COLOR_CYAN, /* FRAGINFO */ |
| 78 | GIT_COLOR_RED, /* OLD */ |
| 79 | GIT_COLOR_GREEN, /* NEW */ |
| 80 | GIT_COLOR_YELLOW, /* COMMIT */ |
| 81 | GIT_COLOR_BG_RED, /* WHITESPACE */ |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 82 | GIT_COLOR_NORMAL, /* FUNCINFO */ |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 83 | GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */ |
| 84 | GIT_COLOR_BOLD_BLUE, /* OLD_MOVED ALTERNATIVE */ |
| 85 | GIT_COLOR_FAINT, /* OLD_MOVED_DIM */ |
| 86 | GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */ |
| 87 | GIT_COLOR_BOLD_CYAN, /* NEW_MOVED */ |
| 88 | GIT_COLOR_BOLD_YELLOW, /* NEW_MOVED ALTERNATIVE */ |
| 89 | GIT_COLOR_FAINT, /* NEW_MOVED_DIM */ |
| 90 | GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */ |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 91 | GIT_COLOR_FAINT, /* CONTEXT_DIM */ |
| 92 | GIT_COLOR_FAINT_RED, /* OLD_DIM */ |
| 93 | GIT_COLOR_FAINT_GREEN, /* NEW_DIM */ |
| 94 | GIT_COLOR_BOLD, /* CONTEXT_BOLD */ |
| 95 | GIT_COLOR_BOLD_RED, /* OLD_BOLD */ |
| 96 | GIT_COLOR_BOLD_GREEN, /* NEW_BOLD */ |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 97 | }; |
| 98 | |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 99 | static const char *color_diff_slots[] = { |
| 100 | [DIFF_CONTEXT] = "context", |
| 101 | [DIFF_METAINFO] = "meta", |
| 102 | [DIFF_FRAGINFO] = "frag", |
| 103 | [DIFF_FILE_OLD] = "old", |
| 104 | [DIFF_FILE_NEW] = "new", |
| 105 | [DIFF_COMMIT] = "commit", |
| 106 | [DIFF_WHITESPACE] = "whitespace", |
| 107 | [DIFF_FUNCINFO] = "func", |
| 108 | [DIFF_FILE_OLD_MOVED] = "oldMoved", |
| 109 | [DIFF_FILE_OLD_MOVED_ALT] = "oldMovedAlternative", |
| 110 | [DIFF_FILE_OLD_MOVED_DIM] = "oldMovedDimmed", |
| 111 | [DIFF_FILE_OLD_MOVED_ALT_DIM] = "oldMovedAlternativeDimmed", |
| 112 | [DIFF_FILE_NEW_MOVED] = "newMoved", |
| 113 | [DIFF_FILE_NEW_MOVED_ALT] = "newMovedAlternative", |
| 114 | [DIFF_FILE_NEW_MOVED_DIM] = "newMovedDimmed", |
| 115 | [DIFF_FILE_NEW_MOVED_ALT_DIM] = "newMovedAlternativeDimmed", |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 116 | [DIFF_CONTEXT_DIM] = "contextDimmed", |
| 117 | [DIFF_FILE_OLD_DIM] = "oldDimmed", |
| 118 | [DIFF_FILE_NEW_DIM] = "newDimmed", |
| 119 | [DIFF_CONTEXT_BOLD] = "contextBold", |
| 120 | [DIFF_FILE_OLD_BOLD] = "oldBold", |
| 121 | [DIFF_FILE_NEW_BOLD] = "newBold", |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 124 | define_list_config_array_extra(color_diff_slots, {"plain"}); |
| 125 | |
Jeff King | 9e1a5eb | 2014-06-18 15:41:50 -0400 | [diff] [blame] | 126 | static int parse_diff_color_slot(const char *var) |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 127 | { |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 128 | if (!strcasecmp(var, "plain")) |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 129 | return DIFF_CONTEXT; |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 130 | return LOOKUP_CONFIG(color_diff_slots, var); |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 133 | static int parse_dirstat_params(struct diff_options *options, const char *params_string, |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 134 | struct strbuf *errmsg) |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 135 | { |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 136 | char *params_copy = xstrdup(params_string); |
| 137 | struct string_list params = STRING_LIST_INIT_NODUP; |
| 138 | int ret = 0; |
| 139 | int i; |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 140 | |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 141 | if (*params_copy) |
Taylor Blau | 52acddf | 2023-04-24 18:20:10 -0400 | [diff] [blame] | 142 | string_list_split_in_place(¶ms, params_copy, ",", -1); |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 143 | for (i = 0; i < params.nr; i++) { |
| 144 | const char *p = params.items[i].string; |
| 145 | if (!strcmp(p, "changes")) { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 146 | options->flags.dirstat_by_line = 0; |
| 147 | options->flags.dirstat_by_file = 0; |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 148 | } else if (!strcmp(p, "lines")) { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 149 | options->flags.dirstat_by_line = 1; |
| 150 | options->flags.dirstat_by_file = 0; |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 151 | } else if (!strcmp(p, "files")) { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 152 | options->flags.dirstat_by_line = 0; |
| 153 | options->flags.dirstat_by_file = 1; |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 154 | } else if (!strcmp(p, "noncumulative")) { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 155 | options->flags.dirstat_cumulative = 0; |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 156 | } else if (!strcmp(p, "cumulative")) { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 157 | options->flags.dirstat_cumulative = 1; |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 158 | } else if (isdigit(*p)) { |
| 159 | char *end; |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 160 | int permille = strtoul(p, &end, 10) * 10; |
| 161 | if (*end == '.' && isdigit(*++end)) { |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 162 | /* only use first digit */ |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 163 | permille += *end - '0'; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 164 | /* .. and ignore any further digits */ |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 165 | while (isdigit(*++end)) |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 166 | ; /* nothing */ |
| 167 | } |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 168 | if (!*end) |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 169 | options->dirstat_permille = permille; |
| 170 | else { |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 171 | strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"), |
| 172 | p); |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 173 | ret++; |
| 174 | } |
| 175 | } else { |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 176 | strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p); |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 177 | ret++; |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 178 | } |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 179 | |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 180 | } |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 181 | string_list_clear(¶ms, 0); |
| 182 | free(params_copy); |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 183 | return ret; |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 186 | static int parse_submodule_params(struct diff_options *options, const char *value) |
| 187 | { |
| 188 | if (!strcmp(value, "log")) |
Jacob Keller | 61cfbc0 | 2016-08-31 16:27:21 -0700 | [diff] [blame] | 189 | options->submodule_format = DIFF_SUBMODULE_LOG; |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 190 | else if (!strcmp(value, "short")) |
Jacob Keller | 61cfbc0 | 2016-08-31 16:27:21 -0700 | [diff] [blame] | 191 | options->submodule_format = DIFF_SUBMODULE_SHORT; |
Jacob Keller | fd47ae6 | 2016-08-31 16:27:25 -0700 | [diff] [blame] | 192 | else if (!strcmp(value, "diff")) |
| 193 | options->submodule_format = DIFF_SUBMODULE_INLINE_DIFF; |
Nguyễn Thái Ngọc Duy | 5a59a23 | 2019-02-16 18:24:41 +0700 | [diff] [blame] | 194 | /* |
| 195 | * Please update $__git_diff_submodule_formats in |
| 196 | * git-completion.bash when you add new formats. |
| 197 | */ |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 198 | else |
| 199 | return -1; |
| 200 | return 0; |
| 201 | } |
| 202 | |
Ben Peart | 85b4603 | 2018-05-02 16:01:14 +0000 | [diff] [blame] | 203 | int git_config_rename(const char *var, const char *value) |
Linus Torvalds | cced5fb | 2009-04-09 11:46:15 -0700 | [diff] [blame] | 204 | { |
| 205 | if (!value) |
| 206 | return DIFF_DETECT_RENAME; |
| 207 | if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy")) |
| 208 | return DIFF_DETECT_COPY; |
| 209 | return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0; |
| 210 | } |
| 211 | |
Michal Privoznik | 07924d4 | 2013-01-16 08:51:58 +0100 | [diff] [blame] | 212 | long parse_algorithm_value(const char *value) |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 213 | { |
| 214 | if (!value) |
| 215 | return -1; |
| 216 | else if (!strcasecmp(value, "myers") || !strcasecmp(value, "default")) |
| 217 | return 0; |
| 218 | else if (!strcasecmp(value, "minimal")) |
| 219 | return XDF_NEED_MINIMAL; |
| 220 | else if (!strcasecmp(value, "patience")) |
| 221 | return XDF_PATIENCE_DIFF; |
| 222 | else if (!strcasecmp(value, "histogram")) |
| 223 | return XDF_HISTOGRAM_DIFF; |
Nguyễn Thái Ngọc Duy | 5a59a23 | 2019-02-16 18:24:41 +0700 | [diff] [blame] | 224 | /* |
| 225 | * Please update $__git_diff_algorithms in git-completion.bash |
| 226 | * when you add new algorithms. |
| 227 | */ |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 228 | return -1; |
| 229 | } |
| 230 | |
Junio C Hamano | 0b4b42e | 2016-10-04 15:09:18 -0700 | [diff] [blame] | 231 | static int parse_one_token(const char **arg, const char *token) |
| 232 | { |
| 233 | const char *rest; |
| 234 | if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) { |
| 235 | *arg = rest; |
| 236 | return 1; |
| 237 | } |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static int parse_ws_error_highlight(const char *arg) |
| 242 | { |
| 243 | const char *orig_arg = arg; |
| 244 | unsigned val = 0; |
| 245 | |
| 246 | while (*arg) { |
| 247 | if (parse_one_token(&arg, "none")) |
| 248 | val = 0; |
| 249 | else if (parse_one_token(&arg, "default")) |
| 250 | val = WSEH_NEW; |
| 251 | else if (parse_one_token(&arg, "all")) |
| 252 | val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT; |
| 253 | else if (parse_one_token(&arg, "new")) |
| 254 | val |= WSEH_NEW; |
| 255 | else if (parse_one_token(&arg, "old")) |
| 256 | val |= WSEH_OLD; |
| 257 | else if (parse_one_token(&arg, "context")) |
| 258 | val |= WSEH_CONTEXT; |
| 259 | else { |
| 260 | return -1 - (int)(arg - orig_arg); |
| 261 | } |
| 262 | if (*arg) |
| 263 | arg++; |
| 264 | } |
| 265 | return val; |
| 266 | } |
| 267 | |
Junio C Hamano | e0e324a | 2007-07-07 01:49:58 -0700 | [diff] [blame] | 268 | /* |
Junio C Hamano | 83ad63c | 2006-07-08 01:05:16 -0700 | [diff] [blame] | 269 | * These are to give UI layer defaults. |
| 270 | * The core-level commands such as git-diff-files should |
| 271 | * never be affected by the setting of diff.renames |
| 272 | * the user happens to have in the configuration file. |
| 273 | */ |
Matthieu Moy | 5404c11 | 2016-02-25 09:59:21 +0100 | [diff] [blame] | 274 | void init_diff_ui_defaults(void) |
| 275 | { |
Nguyễn Thái Ngọc Duy | 06dba2b | 2017-12-27 17:18:35 +0700 | [diff] [blame] | 276 | diff_detect_rename_default = DIFF_DETECT_RENAME; |
Matthieu Moy | 5404c11 | 2016-02-25 09:59:21 +0100 | [diff] [blame] | 277 | } |
| 278 | |
Jeff King | 783a86c | 2022-08-19 06:08:44 -0400 | [diff] [blame] | 279 | int git_diff_heuristic_config(const char *var, const char *value, |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 280 | void *cb UNUSED) |
Michael Haggerty | 5b16287 | 2016-09-05 11:44:53 +0200 | [diff] [blame] | 281 | { |
Junio C Hamano | 3cde4e0 | 2016-12-23 12:32:22 -0800 | [diff] [blame] | 282 | if (!strcmp(var, "diff.indentheuristic")) |
Michael Haggerty | 5b16287 | 2016-09-05 11:44:53 +0200 | [diff] [blame] | 283 | diff_indent_heuristic = git_config_bool(var, value); |
Michael Haggerty | 5b16287 | 2016-09-05 11:44:53 +0200 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 287 | static int parse_color_moved(const char *arg) |
| 288 | { |
| 289 | switch (git_parse_maybe_bool(arg)) { |
| 290 | case 0: |
| 291 | return COLOR_MOVED_NO; |
| 292 | case 1: |
| 293 | return COLOR_MOVED_DEFAULT; |
| 294 | default: |
| 295 | break; |
| 296 | } |
| 297 | |
| 298 | if (!strcmp(arg, "no")) |
| 299 | return COLOR_MOVED_NO; |
Stefan Beller | 176841f | 2017-06-30 13:53:08 -0700 | [diff] [blame] | 300 | else if (!strcmp(arg, "plain")) |
| 301 | return COLOR_MOVED_PLAIN; |
Stefan Beller | 51da15e | 2018-07-16 16:05:39 -0700 | [diff] [blame] | 302 | else if (!strcmp(arg, "blocks")) |
| 303 | return COLOR_MOVED_BLOCKS; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 304 | else if (!strcmp(arg, "zebra")) |
| 305 | return COLOR_MOVED_ZEBRA; |
| 306 | else if (!strcmp(arg, "default")) |
| 307 | return COLOR_MOVED_DEFAULT; |
Eric Sunshine | e3f2f5f | 2018-07-24 17:58:45 -0400 | [diff] [blame] | 308 | else if (!strcmp(arg, "dimmed-zebra")) |
| 309 | return COLOR_MOVED_ZEBRA_DIM; |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 310 | else if (!strcmp(arg, "dimmed_zebra")) |
| 311 | return COLOR_MOVED_ZEBRA_DIM; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 312 | else |
Junio C Hamano | 706b0b5 | 2018-08-15 15:08:22 -0700 | [diff] [blame] | 313 | return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'")); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Stefan Beller | d173e79 | 2018-11-13 13:33:57 -0800 | [diff] [blame] | 316 | static unsigned parse_color_moved_ws(const char *arg) |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 317 | { |
| 318 | int ret = 0; |
| 319 | struct string_list l = STRING_LIST_INIT_DUP; |
| 320 | struct string_list_item *i; |
| 321 | |
| 322 | string_list_split(&l, arg, ',', -1); |
| 323 | |
| 324 | for_each_string_list_item(i, &l) { |
| 325 | struct strbuf sb = STRBUF_INIT; |
| 326 | strbuf_addstr(&sb, i->string); |
| 327 | strbuf_trim(&sb); |
| 328 | |
Phillip Wood | b73bcba | 2018-11-23 11:16:52 +0000 | [diff] [blame] | 329 | if (!strcmp(sb.buf, "no")) |
| 330 | ret = 0; |
| 331 | else if (!strcmp(sb.buf, "ignore-space-change")) |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 332 | ret |= XDF_IGNORE_WHITESPACE_CHANGE; |
| 333 | else if (!strcmp(sb.buf, "ignore-space-at-eol")) |
| 334 | ret |= XDF_IGNORE_WHITESPACE_AT_EOL; |
| 335 | else if (!strcmp(sb.buf, "ignore-all-space")) |
| 336 | ret |= XDF_IGNORE_WHITESPACE; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 337 | else if (!strcmp(sb.buf, "allow-indentation-change")) |
| 338 | ret |= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE; |
Stefan Beller | d173e79 | 2018-11-13 13:33:57 -0800 | [diff] [blame] | 339 | else { |
| 340 | ret |= COLOR_MOVED_WS_ERROR; |
| 341 | error(_("unknown color-moved-ws mode '%s', possible values are 'ignore-space-change', 'ignore-space-at-eol', 'ignore-all-space', 'allow-indentation-change'"), sb.buf); |
| 342 | } |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 343 | |
| 344 | strbuf_release(&sb); |
| 345 | } |
| 346 | |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 347 | if ((ret & COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) && |
Stefan Beller | d173e79 | 2018-11-13 13:33:57 -0800 | [diff] [blame] | 348 | (ret & XDF_WHITESPACE_FLAGS)) { |
Junio C Hamano | 15b07cb | 2019-01-29 12:47:53 -0800 | [diff] [blame] | 349 | error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes")); |
Stefan Beller | d173e79 | 2018-11-13 13:33:57 -0800 | [diff] [blame] | 350 | ret |= COLOR_MOVED_WS_ERROR; |
| 351 | } |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 352 | |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 353 | string_list_clear(&l, 0); |
| 354 | |
| 355 | return ret; |
Jeff King | e269eb7 | 2011-08-17 22:03:48 -0700 | [diff] [blame] | 356 | } |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 357 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 358 | int git_diff_ui_config(const char *var, const char *value, |
| 359 | const struct config_context *ctx, void *cb) |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 360 | { |
| 361 | if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) { |
| 362 | diff_use_color_default = git_config_colorbool(var, value); |
| 363 | return 0; |
| 364 | } |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 365 | if (!strcmp(var, "diff.colormoved")) { |
| 366 | int cm = parse_color_moved(value); |
| 367 | if (cm < 0) |
| 368 | return -1; |
| 369 | diff_color_moved_default = cm; |
| 370 | return 0; |
| 371 | } |
Stefan Beller | 626c0b5 | 2018-07-18 12:31:56 -0700 | [diff] [blame] | 372 | if (!strcmp(var, "diff.colormovedws")) { |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 373 | unsigned cm; |
| 374 | if (!value) |
| 375 | return config_error_nonbool(var); |
| 376 | cm = parse_color_moved_ws(value); |
Stefan Beller | d173e79 | 2018-11-13 13:33:57 -0800 | [diff] [blame] | 377 | if (cm & COLOR_MOVED_WS_ERROR) |
Stefan Beller | 626c0b5 | 2018-07-18 12:31:56 -0700 | [diff] [blame] | 378 | return -1; |
| 379 | diff_color_moved_ws_default = cm; |
| 380 | return 0; |
| 381 | } |
Jeff Muizelaar | 6468a4e | 2012-09-27 15:12:52 -0400 | [diff] [blame] | 382 | if (!strcmp(var, "diff.context")) { |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 383 | diff_context_default = git_config_int(var, value, ctx->kvi); |
Jeff Muizelaar | 6468a4e | 2012-09-27 15:12:52 -0400 | [diff] [blame] | 384 | if (diff_context_default < 0) |
| 385 | return -1; |
| 386 | return 0; |
| 387 | } |
Vegard Nossum | c488867 | 2017-01-12 13:21:11 +0100 | [diff] [blame] | 388 | if (!strcmp(var, "diff.interhunkcontext")) { |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 389 | diff_interhunk_context_default = git_config_int(var, value, |
| 390 | ctx->kvi); |
Vegard Nossum | c488867 | 2017-01-12 13:21:11 +0100 | [diff] [blame] | 391 | if (diff_interhunk_context_default < 0) |
| 392 | return -1; |
| 393 | return 0; |
| 394 | } |
Eric Wong | b68ea12 | 2006-07-07 04:01:23 -0700 | [diff] [blame] | 395 | if (!strcmp(var, "diff.renames")) { |
Linus Torvalds | cced5fb | 2009-04-09 11:46:15 -0700 | [diff] [blame] | 396 | diff_detect_rename_default = git_config_rename(var, value); |
Eric Wong | b68ea12 | 2006-07-07 04:01:23 -0700 | [diff] [blame] | 397 | return 0; |
| 398 | } |
Junio C Hamano | aecbf91 | 2007-08-31 13:13:42 -0700 | [diff] [blame] | 399 | if (!strcmp(var, "diff.autorefreshindex")) { |
| 400 | diff_auto_refresh_index = git_config_bool(var, value); |
| 401 | return 0; |
| 402 | } |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 403 | if (!strcmp(var, "diff.mnemonicprefix")) { |
| 404 | diff_mnemonic_prefix = git_config_bool(var, value); |
| 405 | return 0; |
| 406 | } |
Eli Collins | f89504d | 2010-05-02 19:03:41 -0700 | [diff] [blame] | 407 | if (!strcmp(var, "diff.noprefix")) { |
| 408 | diff_no_prefix = git_config_bool(var, value); |
| 409 | return 0; |
| 410 | } |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 411 | if (!strcmp(var, "diff.relative")) { |
| 412 | diff_relative = git_config_bool(var, value); |
| 413 | return 0; |
| 414 | } |
Dragan Simic | bd48adc | 2023-09-11 17:39:44 +0200 | [diff] [blame] | 415 | if (!strcmp(var, "diff.statnamewidth")) { |
| 416 | diff_stat_name_width = git_config_int(var, value, ctx->kvi); |
| 417 | return 0; |
| 418 | } |
Zbigniew Jędrzejewski-Szmek | df44483 | 2012-03-01 13:26:46 +0100 | [diff] [blame] | 419 | if (!strcmp(var, "diff.statgraphwidth")) { |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 420 | diff_stat_graph_width = git_config_int(var, value, ctx->kvi); |
Zbigniew Jędrzejewski-Szmek | df44483 | 2012-03-01 13:26:46 +0100 | [diff] [blame] | 421 | return 0; |
| 422 | } |
Brian Hetro | daec808 | 2008-07-05 01:24:43 -0400 | [diff] [blame] | 423 | if (!strcmp(var, "diff.external")) |
| 424 | return git_config_string(&external_diff_cmd_cfg, var, value); |
Boyd Stephen Smith Jr | 98a4d87 | 2009-01-20 21:46:57 -0600 | [diff] [blame] | 425 | if (!strcmp(var, "diff.wordregex")) |
| 426 | return git_config_string(&diff_word_regex_cfg, var, value); |
Samuel Bronson | 6d8940b | 2013-12-18 19:08:12 -0500 | [diff] [blame] | 427 | if (!strcmp(var, "diff.orderfile")) |
| 428 | return git_config_pathname(&diff_order_file_cfg, var, value); |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 429 | |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 430 | if (!strcmp(var, "diff.ignoresubmodules")) { |
| 431 | if (!value) |
| 432 | return config_error_nonbool(var); |
Johannes Schindelin | be4f2b4 | 2010-08-05 10:49:55 +0200 | [diff] [blame] | 433 | handle_ignore_submodules_arg(&default_diff_options, value); |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 434 | } |
Johannes Schindelin | be4f2b4 | 2010-08-05 10:49:55 +0200 | [diff] [blame] | 435 | |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 436 | if (!strcmp(var, "diff.submodule")) { |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 437 | if (!value) |
| 438 | return config_error_nonbool(var); |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 439 | if (parse_submodule_params(&default_diff_options, value)) |
| 440 | warning(_("Unknown value for 'diff.submodule' config variable: '%s'"), |
| 441 | value); |
| 442 | return 0; |
| 443 | } |
| 444 | |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 445 | if (!strcmp(var, "diff.algorithm")) { |
Jeff King | 0824879 | 2023-12-07 02:25:23 -0500 | [diff] [blame] | 446 | if (!value) |
| 447 | return config_error_nonbool(var); |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 448 | diff_algorithm = parse_algorithm_value(value); |
| 449 | if (diff_algorithm < 0) |
Jeff King | 0824879 | 2023-12-07 02:25:23 -0500 | [diff] [blame] | 450 | return error(_("unknown value for config '%s': %s"), |
| 451 | var, value); |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 452 | return 0; |
| 453 | } |
| 454 | |
Jeff King | 33c643b | 2017-10-13 13:24:31 -0400 | [diff] [blame] | 455 | if (git_color_config(var, value, cb) < 0) |
| 456 | return -1; |
| 457 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 458 | return git_diff_basic_config(var, value, ctx, cb); |
Jeff King | 9a1805a | 2008-01-04 03:59:34 -0500 | [diff] [blame] | 459 | } |
| 460 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 461 | int git_diff_basic_config(const char *var, const char *value, |
| 462 | const struct config_context *ctx, void *cb) |
Jeff King | 9a1805a | 2008-01-04 03:59:34 -0500 | [diff] [blame] | 463 | { |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 464 | const char *name; |
| 465 | |
Linus Torvalds | 2b6ca6d | 2008-08-05 11:27:30 -0700 | [diff] [blame] | 466 | if (!strcmp(var, "diff.renamelimit")) { |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 467 | diff_rename_limit_default = git_config_int(var, value, ctx->kvi); |
Linus Torvalds | 2b6ca6d | 2008-08-05 11:27:30 -0700 | [diff] [blame] | 468 | return 0; |
| 469 | } |
| 470 | |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 471 | if (userdiff_config(var, value) < 0) |
| 472 | return -1; |
Jeff King | c7534ef | 2008-10-26 00:45:55 -0400 | [diff] [blame] | 473 | |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 474 | if (skip_prefix(var, "diff.color.", &name) || |
| 475 | skip_prefix(var, "color.diff.", &name)) { |
| 476 | int slot = parse_diff_color_slot(name); |
Jeff King | 8b8e862 | 2009-12-12 07:25:24 -0500 | [diff] [blame] | 477 | if (slot < 0) |
| 478 | return 0; |
Junio C Hamano | 64f30e9 | 2008-02-11 10:53:56 -0800 | [diff] [blame] | 479 | if (!value) |
| 480 | return config_error_nonbool(var); |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 481 | return color_parse(value, diff_colors[slot]); |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 482 | } |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 483 | |
Jeff King | da80635 | 2020-01-31 04:57:49 -0500 | [diff] [blame] | 484 | if (!strcmp(var, "diff.wserrorhighlight")) { |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 485 | int val; |
| 486 | if (!value) |
| 487 | return config_error_nonbool(var); |
| 488 | val = parse_ws_error_highlight(value); |
Jeff King | da80635 | 2020-01-31 04:57:49 -0500 | [diff] [blame] | 489 | if (val < 0) |
Jeff King | 0824879 | 2023-12-07 02:25:23 -0500 | [diff] [blame] | 490 | return error(_("unknown value for config '%s': %s"), |
| 491 | var, value); |
Jeff King | da80635 | 2020-01-31 04:57:49 -0500 | [diff] [blame] | 492 | ws_error_highlight_default = val; |
| 493 | return 0; |
| 494 | } |
| 495 | |
Jim Meyering | a624eaa | 2008-08-15 13:39:26 +0200 | [diff] [blame] | 496 | /* like GNU diff's --suppress-blank-empty option */ |
Johannes Schindelin | 950db87 | 2009-01-20 22:08:33 +0100 | [diff] [blame] | 497 | if (!strcmp(var, "diff.suppressblankempty") || |
| 498 | /* for backwards compatibility */ |
| 499 | !strcmp(var, "diff.suppress-blank-empty")) { |
Jim Meyering | a624eaa | 2008-08-15 13:39:26 +0200 | [diff] [blame] | 500 | diff_suppress_blank_empty = git_config_bool(var, value); |
| 501 | return 0; |
| 502 | } |
| 503 | |
Johan Herland | 2d17495 | 2011-04-29 11:36:19 +0200 | [diff] [blame] | 504 | if (!strcmp(var, "diff.dirstat")) { |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 505 | struct strbuf errmsg = STRBUF_INIT; |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 506 | if (!value) |
| 507 | return config_error_nonbool(var); |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 508 | default_diff_options.dirstat_permille = diff_dirstat_permille_default; |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 509 | if (parse_dirstat_params(&default_diff_options, value, &errmsg)) |
Johan Herland | 7478ac5 | 2011-04-29 11:36:23 +0200 | [diff] [blame] | 510 | warning(_("Found errors in 'diff.dirstat' config variable:\n%s"), |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 511 | errmsg.buf); |
| 512 | strbuf_release(&errmsg); |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 513 | diff_dirstat_permille_default = default_diff_options.dirstat_permille; |
Johan Herland | 2d17495 | 2011-04-29 11:36:19 +0200 | [diff] [blame] | 514 | return 0; |
| 515 | } |
| 516 | |
Marc Branchaud | cf5e772 | 2017-05-08 12:03:36 -0400 | [diff] [blame] | 517 | if (git_diff_heuristic_config(var, value, cb) < 0) |
| 518 | return -1; |
| 519 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 520 | return git_default_config(var, value, ctx, cb); |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 523 | static char *quote_two(const char *one, const char *two) |
| 524 | { |
Junio C Hamano | 7c37c97 | 2020-09-10 10:01:59 -0700 | [diff] [blame] | 525 | int need_one = quote_c_style(one, NULL, NULL, CQUOTE_NODQ); |
| 526 | int need_two = quote_c_style(two, NULL, NULL, CQUOTE_NODQ); |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 527 | struct strbuf res = STRBUF_INIT; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 528 | |
| 529 | if (need_one + need_two) { |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 530 | strbuf_addch(&res, '"'); |
Junio C Hamano | 7c37c97 | 2020-09-10 10:01:59 -0700 | [diff] [blame] | 531 | quote_c_style(one, &res, NULL, CQUOTE_NODQ); |
| 532 | quote_c_style(two, &res, NULL, CQUOTE_NODQ); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 533 | strbuf_addch(&res, '"'); |
| 534 | } else { |
| 535 | strbuf_addstr(&res, one); |
| 536 | strbuf_addstr(&res, two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 537 | } |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 538 | return strbuf_detach(&res, NULL); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | static const char *external_diff(void) |
| 542 | { |
| 543 | static const char *external_diff_cmd = NULL; |
| 544 | static int done_preparing = 0; |
| 545 | |
| 546 | if (done_preparing) |
| 547 | return external_diff_cmd; |
Kim Gybels | 6776a84 | 2019-01-11 21:26:08 +0100 | [diff] [blame] | 548 | external_diff_cmd = xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF")); |
Johannes Schindelin | cbe0210 | 2007-12-17 13:42:20 +0000 | [diff] [blame] | 549 | if (!external_diff_cmd) |
| 550 | external_diff_cmd = external_diff_cmd_cfg; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 551 | done_preparing = 1; |
| 552 | return external_diff_cmd; |
| 553 | } |
| 554 | |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 555 | /* |
| 556 | * Keep track of files used for diffing. Sometimes such an entry |
| 557 | * refers to a temporary file, sometimes to an existing file, and |
| 558 | * sometimes to "/dev/null". |
| 559 | */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 560 | static struct diff_tempfile { |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 561 | /* |
| 562 | * filename external diff should read from, or NULL if this |
| 563 | * entry is currently not in use: |
| 564 | */ |
| 565 | const char *name; |
| 566 | |
brian m. carlson | dc01505 | 2017-03-26 16:01:24 +0000 | [diff] [blame] | 567 | char hex[GIT_MAX_HEXSZ + 1]; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 568 | char mode[10]; |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 569 | |
| 570 | /* |
| 571 | * If this diff_tempfile instance refers to a temporary file, |
| 572 | * this tempfile object is used to manage its lifetime. |
| 573 | */ |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 574 | struct tempfile *tempfile; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 575 | } diff_temp[2]; |
| 576 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 577 | struct emit_callback { |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 578 | int color_diff; |
| 579 | unsigned ws_rule; |
| 580 | int blank_at_eof_in_preimage; |
| 581 | int blank_at_eof_in_postimage; |
| 582 | int lno_in_preimage; |
| 583 | int lno_in_postimage; |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 584 | const char **label_path; |
| 585 | struct diff_words_data *diff_words; |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 586 | struct diff_options *opt; |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 587 | struct strbuf *header; |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 588 | }; |
| 589 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 590 | static int count_lines(const char *data, int size) |
| 591 | { |
| 592 | int count, ch, completely_empty = 1, nl_just_seen = 0; |
| 593 | count = 0; |
| 594 | while (0 < size--) { |
| 595 | ch = *data++; |
| 596 | if (ch == '\n') { |
| 597 | count++; |
| 598 | nl_just_seen = 1; |
| 599 | completely_empty = 0; |
| 600 | } |
| 601 | else { |
| 602 | nl_just_seen = 0; |
| 603 | completely_empty = 0; |
| 604 | } |
| 605 | } |
| 606 | if (completely_empty) |
| 607 | return 0; |
| 608 | if (!nl_just_seen) |
| 609 | count++; /* no trailing newline */ |
| 610 | return count; |
| 611 | } |
| 612 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 613 | static int fill_mmfile(struct repository *r, mmfile_t *mf, |
| 614 | struct diff_filespec *one) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 615 | { |
| 616 | if (!DIFF_FILE_VALID(one)) { |
| 617 | mf->ptr = (char *)""; /* does not matter */ |
| 618 | mf->size = 0; |
| 619 | return 0; |
| 620 | } |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 621 | else if (diff_populate_filespec(r, one, NULL)) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 622 | return -1; |
Junio C Hamano | bb35fef | 2009-09-15 03:38:30 -0700 | [diff] [blame] | 623 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 624 | mf->ptr = one->data; |
| 625 | mf->size = one->size; |
| 626 | return 0; |
| 627 | } |
| 628 | |
Jeff King | abb371a | 2011-02-19 03:16:32 -0500 | [diff] [blame] | 629 | /* like fill_mmfile, but only for size, so we can avoid retrieving blob */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 630 | static unsigned long diff_filespec_size(struct repository *r, |
| 631 | struct diff_filespec *one) |
Jeff King | abb371a | 2011-02-19 03:16:32 -0500 | [diff] [blame] | 632 | { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 633 | struct diff_populate_filespec_options dpf_options = { |
| 634 | .check_size_only = 1, |
| 635 | }; |
| 636 | |
Jeff King | abb371a | 2011-02-19 03:16:32 -0500 | [diff] [blame] | 637 | if (!DIFF_FILE_VALID(one)) |
| 638 | return 0; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 639 | diff_populate_filespec(r, one, &dpf_options); |
Jeff King | abb371a | 2011-02-19 03:16:32 -0500 | [diff] [blame] | 640 | return one->size; |
| 641 | } |
| 642 | |
Jeff King | c5224f0 | 2022-12-13 06:12:58 -0500 | [diff] [blame] | 643 | static int count_trailing_blank(mmfile_t *mf) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 644 | { |
| 645 | char *ptr = mf->ptr; |
| 646 | long size = mf->size; |
| 647 | int cnt = 0; |
| 648 | |
| 649 | if (!size) |
| 650 | return cnt; |
| 651 | ptr += size - 1; /* pointing at the very end */ |
| 652 | if (*ptr != '\n') |
| 653 | ; /* incomplete line */ |
| 654 | else |
| 655 | ptr--; /* skip the last LF */ |
| 656 | while (mf->ptr < ptr) { |
| 657 | char *prev_eol; |
| 658 | for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--) |
| 659 | if (*prev_eol == '\n') |
| 660 | break; |
Jeff King | c5224f0 | 2022-12-13 06:12:58 -0500 | [diff] [blame] | 661 | if (!ws_blank_line(prev_eol + 1, ptr - prev_eol)) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 662 | break; |
| 663 | cnt++; |
| 664 | ptr = prev_eol - 1; |
| 665 | } |
| 666 | return cnt; |
| 667 | } |
| 668 | |
| 669 | static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2, |
| 670 | struct emit_callback *ecbdata) |
| 671 | { |
| 672 | int l1, l2, at; |
Jeff King | c5224f0 | 2022-12-13 06:12:58 -0500 | [diff] [blame] | 673 | l1 = count_trailing_blank(mf1); |
| 674 | l2 = count_trailing_blank(mf2); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 675 | if (l2 <= l1) { |
| 676 | ecbdata->blank_at_eof_in_preimage = 0; |
| 677 | ecbdata->blank_at_eof_in_postimage = 0; |
| 678 | return; |
| 679 | } |
| 680 | at = count_lines(mf1->ptr, mf1->size); |
| 681 | ecbdata->blank_at_eof_in_preimage = (at - l1) + 1; |
| 682 | |
| 683 | at = count_lines(mf2->ptr, mf2->size); |
| 684 | ecbdata->blank_at_eof_in_postimage = (at - l2) + 1; |
| 685 | } |
| 686 | |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 687 | static void emit_line_0(struct diff_options *o, |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 688 | const char *set_sign, const char *set, unsigned reverse, const char *reset, |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 689 | int first, const char *line, int len) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 690 | { |
| 691 | int has_trailing_newline, has_trailing_carriage_return; |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 692 | int needs_reset = 0; /* at the end of the line */ |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 693 | FILE *file = o->file; |
| 694 | |
Stefan Beller | f103a6f | 2018-08-13 18:41:21 -0700 | [diff] [blame] | 695 | fputs(diff_line_prefix(o), file); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 696 | |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 697 | has_trailing_newline = (len > 0 && line[len-1] == '\n'); |
| 698 | if (has_trailing_newline) |
| 699 | len--; |
| 700 | |
| 701 | has_trailing_carriage_return = (len > 0 && line[len-1] == '\r'); |
| 702 | if (has_trailing_carriage_return) |
| 703 | len--; |
| 704 | |
| 705 | if (!len && !first) |
| 706 | goto end_of_line; |
| 707 | |
| 708 | if (reverse && want_color(o->use_color)) { |
| 709 | fputs(GIT_COLOR_REVERSE, file); |
| 710 | needs_reset = 1; |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 711 | } |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 712 | |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 713 | if (set_sign) { |
| 714 | fputs(set_sign, file); |
| 715 | needs_reset = 1; |
Junio C Hamano | 06a4755 | 2009-11-27 22:04:10 -0800 | [diff] [blame] | 716 | } |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 717 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 718 | if (first) |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 719 | fputc(first, file); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 720 | |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 721 | if (!len) |
| 722 | goto end_of_line; |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 723 | |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 724 | if (set) { |
| 725 | if (set_sign && set != set_sign) |
| 726 | fputs(reset, file); |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 727 | fputs(set, file); |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 728 | needs_reset = 1; |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 729 | } |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 730 | fwrite(line, len, 1, file); |
| 731 | needs_reset = 1; /* 'line' may contain color codes. */ |
| 732 | |
| 733 | end_of_line: |
| 734 | if (needs_reset) |
| 735 | fputs(reset, file); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 736 | if (has_trailing_carriage_return) |
| 737 | fputc('\r', file); |
| 738 | if (has_trailing_newline) |
| 739 | fputc('\n', file); |
| 740 | } |
| 741 | |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 742 | static void emit_line(struct diff_options *o, const char *set, const char *reset, |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 743 | const char *line, int len) |
| 744 | { |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 745 | emit_line_0(o, set, NULL, 0, reset, 0, line, len); |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 748 | enum diff_symbol { |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 749 | DIFF_SYMBOL_BINARY_DIFF_HEADER, |
| 750 | DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA, |
| 751 | DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL, |
| 752 | DIFF_SYMBOL_BINARY_DIFF_BODY, |
| 753 | DIFF_SYMBOL_BINARY_DIFF_FOOTER, |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 754 | DIFF_SYMBOL_STATS_SUMMARY_NO_FILES, |
| 755 | DIFF_SYMBOL_STATS_SUMMARY_ABBREV, |
| 756 | DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES, |
| 757 | DIFF_SYMBOL_STATS_LINE, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 758 | DIFF_SYMBOL_WORD_DIFF, |
Stefan Beller | 30b7e1e | 2017-06-29 17:07:04 -0700 | [diff] [blame] | 759 | DIFF_SYMBOL_STAT_SEP, |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 760 | DIFF_SYMBOL_SUMMARY, |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 761 | DIFF_SYMBOL_SUBMODULE_ADD, |
| 762 | DIFF_SYMBOL_SUBMODULE_DEL, |
| 763 | DIFF_SYMBOL_SUBMODULE_UNTRACKED, |
| 764 | DIFF_SYMBOL_SUBMODULE_MODIFIED, |
| 765 | DIFF_SYMBOL_SUBMODULE_HEADER, |
| 766 | DIFF_SYMBOL_SUBMODULE_ERROR, |
| 767 | DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 768 | DIFF_SYMBOL_REWRITE_DIFF, |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 769 | DIFF_SYMBOL_BINARY_FILES, |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 770 | DIFF_SYMBOL_HEADER, |
Stefan Beller | 3ee8b7b | 2017-06-29 17:06:56 -0700 | [diff] [blame] | 771 | DIFF_SYMBOL_FILEPAIR_PLUS, |
| 772 | DIFF_SYMBOL_FILEPAIR_MINUS, |
Stefan Beller | ff95867 | 2017-06-29 17:06:54 -0700 | [diff] [blame] | 773 | DIFF_SYMBOL_WORDS_PORCELAIN, |
| 774 | DIFF_SYMBOL_WORDS, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 775 | DIFF_SYMBOL_CONTEXT, |
Stefan Beller | f2bb121 | 2017-06-29 17:06:55 -0700 | [diff] [blame] | 776 | DIFF_SYMBOL_CONTEXT_INCOMPLETE, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 777 | DIFF_SYMBOL_PLUS, |
| 778 | DIFF_SYMBOL_MINUS, |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 779 | DIFF_SYMBOL_NO_LF_EOF, |
Stefan Beller | 68abc6f | 2017-06-29 17:06:51 -0700 | [diff] [blame] | 780 | DIFF_SYMBOL_CONTEXT_FRAGINFO, |
Stefan Beller | c64b420 | 2017-06-29 17:06:50 -0700 | [diff] [blame] | 781 | DIFF_SYMBOL_CONTEXT_MARKER, |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 782 | DIFF_SYMBOL_SEPARATOR |
| 783 | }; |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 784 | /* |
| 785 | * Flags for content lines: |
| 786 | * 0..12 are whitespace rules |
| 787 | * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT |
| 788 | * 16 is marking if the line is blank at EOF |
| 789 | */ |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 790 | #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16) |
| 791 | #define DIFF_SYMBOL_MOVED_LINE (1<<17) |
| 792 | #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18) |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 793 | #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19) |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 794 | #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK) |
| 795 | |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 796 | /* |
| 797 | * This struct is used when we need to buffer the output of the diff output. |
| 798 | * |
| 799 | * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer |
| 800 | * into the pre/post image file. This pointer could be a union with the |
| 801 | * line pointer. By storing an offset into the file instead of the literal line, |
| 802 | * we can decrease the memory footprint for the buffered output. At first we |
| 803 | * may want to only have indirection for the content lines, but we could also |
| 804 | * enhance the state for emitting prefabricated lines, e.g. the similarity |
| 805 | * score line or hunk/file headers would only need to store a number or path |
| 806 | * and then the output can be constructed later on depending on state. |
| 807 | */ |
| 808 | struct emitted_diff_symbol { |
| 809 | const char *line; |
| 810 | int len; |
| 811 | int flags; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 812 | int indent_off; /* Offset to first non-whitespace character */ |
| 813 | int indent_width; /* The visual width of the indentation */ |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 814 | unsigned id; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 815 | enum diff_symbol s; |
| 816 | }; |
Ævar Arnfjörð Bjarmason | 9865b6e | 2021-09-27 14:54:25 +0200 | [diff] [blame] | 817 | #define EMITTED_DIFF_SYMBOL_INIT { 0 } |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 818 | |
| 819 | struct emitted_diff_symbols { |
| 820 | struct emitted_diff_symbol *buf; |
| 821 | int nr, alloc; |
| 822 | }; |
Ævar Arnfjörð Bjarmason | 9865b6e | 2021-09-27 14:54:25 +0200 | [diff] [blame] | 823 | #define EMITTED_DIFF_SYMBOLS_INIT { 0 } |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 824 | |
| 825 | static void append_emitted_diff_symbol(struct diff_options *o, |
| 826 | struct emitted_diff_symbol *e) |
| 827 | { |
| 828 | struct emitted_diff_symbol *f; |
| 829 | |
| 830 | ALLOC_GROW(o->emitted_symbols->buf, |
| 831 | o->emitted_symbols->nr + 1, |
| 832 | o->emitted_symbols->alloc); |
| 833 | f = &o->emitted_symbols->buf[o->emitted_symbols->nr++]; |
| 834 | |
| 835 | memcpy(f, e, sizeof(struct emitted_diff_symbol)); |
| 836 | f->line = e->line ? xmemdupz(e->line, e->len) : NULL; |
| 837 | } |
| 838 | |
Ævar Arnfjörð Bjarmason | 77e56d5 | 2022-03-17 15:55:35 +0100 | [diff] [blame] | 839 | static void free_emitted_diff_symbols(struct emitted_diff_symbols *e) |
| 840 | { |
| 841 | if (!e) |
| 842 | return; |
| 843 | free(e->buf); |
| 844 | free(e); |
| 845 | } |
| 846 | |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 847 | struct moved_entry { |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 848 | const struct emitted_diff_symbol *es; |
| 849 | struct moved_entry *next_line; |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 850 | struct moved_entry *next_match; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 851 | }; |
| 852 | |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 853 | struct moved_block { |
| 854 | struct moved_entry *match; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 855 | int wsd; /* The whitespace delta of this block */ |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 856 | }; |
| 857 | |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 858 | #define INDENT_BLANKLINE INT_MIN |
| 859 | |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 860 | static void fill_es_indent_data(struct emitted_diff_symbol *es) |
| 861 | { |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 862 | unsigned int off = 0, i; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 863 | int width = 0, tab_width = es->flags & WS_TAB_WIDTH_MASK; |
| 864 | const char *s = es->line; |
| 865 | const int len = es->len; |
| 866 | |
| 867 | /* skip any \v \f \r at start of indentation */ |
| 868 | while (s[off] == '\f' || s[off] == '\v' || |
| 869 | (s[off] == '\r' && off < len - 1)) |
| 870 | off++; |
| 871 | |
| 872 | /* calculate the visual width of indentation */ |
| 873 | while(1) { |
| 874 | if (s[off] == ' ') { |
| 875 | width++; |
| 876 | off++; |
| 877 | } else if (s[off] == '\t') { |
| 878 | width += tab_width - (width % tab_width); |
| 879 | while (s[++off] == '\t') |
| 880 | width += tab_width; |
| 881 | } else { |
| 882 | break; |
| 883 | } |
| 884 | } |
| 885 | |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 886 | /* check if this line is blank */ |
| 887 | for (i = off; i < len; i++) |
| 888 | if (!isspace(s[i])) |
| 889 | break; |
| 890 | |
| 891 | if (i == len) { |
| 892 | es->indent_width = INDENT_BLANKLINE; |
| 893 | es->indent_off = len; |
| 894 | } else { |
| 895 | es->indent_off = off; |
| 896 | es->indent_width = width; |
| 897 | } |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 898 | } |
| 899 | |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 900 | static int compute_ws_delta(const struct emitted_diff_symbol *a, |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 901 | const struct emitted_diff_symbol *b) |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 902 | { |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 903 | int a_width = a->indent_width, |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 904 | b_width = b->indent_width; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 905 | |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 906 | if (a_width == INDENT_BLANKLINE && b_width == INDENT_BLANKLINE) |
| 907 | return INDENT_BLANKLINE; |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 908 | |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 909 | return a_width - b_width; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Phillip Wood | 52d14e1 | 2021-12-09 10:30:02 +0000 | [diff] [blame] | 912 | static int cmp_in_block_with_wsd(const struct moved_entry *cur, |
| 913 | const struct emitted_diff_symbol *l, |
| 914 | struct moved_block *pmb) |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 915 | { |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 916 | int a_width = cur->es->indent_width, b_width = l->indent_width; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 917 | int delta; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 918 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 919 | /* The text of each line must match */ |
| 920 | if (cur->es->id != l->id) |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 921 | return 1; |
| 922 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 923 | /* |
| 924 | * If 'l' and 'cur' are both blank then we don't need to check the |
| 925 | * indent. We only need to check cur as we know the strings match. |
| 926 | * */ |
| 927 | if (a_width == INDENT_BLANKLINE) |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 928 | return 0; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 929 | |
| 930 | /* |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 931 | * The indent changes of the block are known and stored in pmb->wsd; |
| 932 | * however we need to check if the indent changes of the current line |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 933 | * match those of the current block. |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 934 | */ |
Phillip Wood | 52d14e1 | 2021-12-09 10:30:02 +0000 | [diff] [blame] | 935 | delta = b_width - a_width; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 936 | |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 937 | /* |
| 938 | * If the previous lines of this block were all blank then set its |
| 939 | * whitespace delta. |
| 940 | */ |
| 941 | if (pmb->wsd == INDENT_BLANKLINE) |
| 942 | pmb->wsd = delta; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 943 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 944 | return delta != pmb->wsd; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 947 | struct interned_diff_symbol { |
| 948 | struct hashmap_entry ent; |
| 949 | struct emitted_diff_symbol *es; |
| 950 | }; |
| 951 | |
| 952 | static int interned_diff_symbol_cmp(const void *hashmap_cmp_fn_data, |
| 953 | const struct hashmap_entry *eptr, |
| 954 | const struct hashmap_entry *entry_or_key, |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 955 | const void *keydata UNUSED) |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 956 | { |
Stefan Beller | ee1df66 | 2018-07-16 16:05:38 -0700 | [diff] [blame] | 957 | const struct diff_options *diffopt = hashmap_cmp_fn_data; |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 958 | const struct emitted_diff_symbol *a, *b; |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 959 | unsigned flags = diffopt->color_moved_ws_handling |
| 960 | & XDF_WHITESPACE_FLAGS; |
Stefan Beller | ee1df66 | 2018-07-16 16:05:38 -0700 | [diff] [blame] | 961 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 962 | a = container_of(eptr, const struct interned_diff_symbol, ent)->es; |
| 963 | b = container_of(entry_or_key, const struct interned_diff_symbol, ent)->es; |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 964 | |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 965 | return !xdiff_compare_lines(a->line + a->indent_off, |
| 966 | a->len - a->indent_off, |
| 967 | b->line + b->indent_off, |
| 968 | b->len - b->indent_off, flags); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 971 | static void prepare_entry(struct diff_options *o, struct emitted_diff_symbol *l, |
| 972 | struct interned_diff_symbol *s) |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 973 | { |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 974 | unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS; |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 975 | unsigned int hash = xdiff_hash_string(l->line + l->indent_off, |
| 976 | l->len - l->indent_off, flags); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 977 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 978 | hashmap_entry_init(&s->ent, hash); |
| 979 | s->es = l; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 982 | struct moved_entry_list { |
| 983 | struct moved_entry *add, *del; |
| 984 | }; |
| 985 | |
| 986 | static struct moved_entry_list *add_lines_to_move_detection(struct diff_options *o, |
| 987 | struct mem_pool *entry_mem_pool) |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 988 | { |
| 989 | struct moved_entry *prev_line = NULL; |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 990 | struct mem_pool interned_pool; |
| 991 | struct hashmap interned_map; |
| 992 | struct moved_entry_list *entry_list = NULL; |
| 993 | size_t entry_list_alloc = 0; |
| 994 | unsigned id = 0; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 995 | int n; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 996 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 997 | hashmap_init(&interned_map, interned_diff_symbol_cmp, o, 8096); |
| 998 | mem_pool_init(&interned_pool, 1024 * 1024); |
| 999 | |
| 1000 | for (n = 0; n < o->emitted_symbols->nr; n++) { |
| 1001 | struct interned_diff_symbol key; |
| 1002 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; |
| 1003 | struct interned_diff_symbol *s; |
| 1004 | struct moved_entry *entry; |
| 1005 | |
| 1006 | if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS) { |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1007 | prev_line = NULL; |
| 1008 | continue; |
| 1009 | } |
| 1010 | |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 1011 | if (o->color_moved_ws_handling & |
| 1012 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1013 | fill_es_indent_data(l); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1014 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1015 | prepare_entry(o, l, &key); |
| 1016 | s = hashmap_get_entry(&interned_map, &key, ent, &key.ent); |
| 1017 | if (s) { |
| 1018 | l->id = s->es->id; |
| 1019 | } else { |
| 1020 | l->id = id; |
| 1021 | ALLOC_GROW_BY(entry_list, id, 1, entry_list_alloc); |
| 1022 | hashmap_add(&interned_map, |
| 1023 | memcpy(mem_pool_alloc(&interned_pool, |
| 1024 | sizeof(key)), |
| 1025 | &key, sizeof(key))); |
| 1026 | } |
| 1027 | entry = mem_pool_alloc(entry_mem_pool, sizeof(*entry)); |
| 1028 | entry->es = l; |
| 1029 | entry->next_line = NULL; |
| 1030 | if (prev_line && prev_line->es->s == l->s) |
| 1031 | prev_line->next_line = entry; |
| 1032 | prev_line = entry; |
| 1033 | if (l->s == DIFF_SYMBOL_PLUS) { |
| 1034 | entry->next_match = entry_list[l->id].add; |
| 1035 | entry_list[l->id].add = entry; |
| 1036 | } else { |
| 1037 | entry->next_match = entry_list[l->id].del; |
| 1038 | entry_list[l->id].del = entry; |
| 1039 | } |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1040 | } |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1041 | |
| 1042 | hashmap_clear(&interned_map); |
| 1043 | mem_pool_discard(&interned_pool, 0); |
| 1044 | |
| 1045 | return entry_list; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1048 | static void pmb_advance_or_null(struct diff_options *o, |
Phillip Wood | 08fba10 | 2021-12-09 10:30:03 +0000 | [diff] [blame] | 1049 | struct emitted_diff_symbol *l, |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1050 | struct moved_block *pmb, |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1051 | int *pmb_nr) |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1052 | { |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1053 | int i, j; |
Phillip Wood | 08fba10 | 2021-12-09 10:30:03 +0000 | [diff] [blame] | 1054 | |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1055 | for (i = 0, j = 0; i < *pmb_nr; i++) { |
Phillip Wood | ff046a0 | 2021-12-09 10:30:04 +0000 | [diff] [blame] | 1056 | int match; |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1057 | struct moved_entry *prev = pmb[i].match; |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1058 | struct moved_entry *cur = (prev && prev->next_line) ? |
| 1059 | prev->next_line : NULL; |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1060 | |
Phillip Wood | ff046a0 | 2021-12-09 10:30:04 +0000 | [diff] [blame] | 1061 | if (o->color_moved_ws_handling & |
| 1062 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) |
| 1063 | match = cur && |
| 1064 | !cmp_in_block_with_wsd(cur, l, &pmb[i]); |
| 1065 | else |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1066 | match = cur && cur->es->id == l->id; |
| 1067 | |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1068 | if (match) { |
| 1069 | pmb[j] = pmb[i]; |
| 1070 | pmb[j++].match = cur; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1071 | } |
| 1072 | } |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1073 | *pmb_nr = j; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1076 | static void fill_potential_moved_blocks(struct diff_options *o, |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1077 | struct moved_entry *match, |
| 1078 | struct emitted_diff_symbol *l, |
| 1079 | struct moved_block **pmb_p, |
| 1080 | int *pmb_alloc_p, int *pmb_nr_p) |
| 1081 | |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1082 | { |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1083 | struct moved_block *pmb = *pmb_p; |
| 1084 | int pmb_alloc = *pmb_alloc_p, pmb_nr = *pmb_nr_p; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1085 | |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1086 | /* |
| 1087 | * The current line is the start of a new block. |
| 1088 | * Setup the set of potential blocks. |
| 1089 | */ |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1090 | for (; match; match = match->next_match) { |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1091 | ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc); |
| 1092 | if (o->color_moved_ws_handling & |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 1093 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) |
| 1094 | pmb[pmb_nr].wsd = compute_ws_delta(l, match->es); |
| 1095 | else |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1096 | pmb[pmb_nr].wsd = 0; |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 1097 | pmb[pmb_nr++].match = match; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1100 | *pmb_p = pmb; |
| 1101 | *pmb_alloc_p = pmb_alloc; |
| 1102 | *pmb_nr_p = pmb_nr; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1105 | /* |
| 1106 | * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing. |
| 1107 | * |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1108 | * Otherwise, if the last block has fewer alphanumeric characters than |
| 1109 | * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1110 | * that block. |
| 1111 | * |
| 1112 | * The last block consists of the (n - block_length)'th line up to but not |
| 1113 | * including the nth line. |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1114 | * |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1115 | * Returns 0 if the last block is empty or is unset by this function, non zero |
| 1116 | * otherwise. |
| 1117 | * |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1118 | * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c. |
| 1119 | * Think of a way to unify them. |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1120 | */ |
Phillip Wood | bea084b | 2021-12-09 10:29:56 +0000 | [diff] [blame] | 1121 | #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \ |
| 1122 | (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT) |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1123 | static int adjust_last_block(struct diff_options *o, int n, int block_length) |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1124 | { |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1125 | int i, alnum_count = 0; |
| 1126 | if (o->color_moved == COLOR_MOVED_PLAIN) |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1127 | return block_length; |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1128 | for (i = 1; i < block_length + 1; i++) { |
| 1129 | const char *c = o->emitted_symbols->buf[n - i].line; |
| 1130 | for (; *c; c++) { |
| 1131 | if (!isalnum(*c)) |
| 1132 | continue; |
| 1133 | alnum_count++; |
| 1134 | if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT) |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1135 | return 1; |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1136 | } |
| 1137 | } |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1138 | for (i = 1; i < block_length + 1; i++) |
Phillip Wood | bea084b | 2021-12-09 10:29:56 +0000 | [diff] [blame] | 1139 | o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK; |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1140 | return 0; |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1143 | /* Find blocks of moved code, delegate actual coloring decision to helper */ |
| 1144 | static void mark_color_as_moved(struct diff_options *o, |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1145 | struct moved_entry_list *entry_list) |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1146 | { |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1147 | struct moved_block *pmb = NULL; /* potentially moved blocks */ |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1148 | int pmb_nr = 0, pmb_alloc = 0; |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1149 | int n, flipped_block = 0, block_length = 0; |
Phillip Wood | eb89352 | 2021-12-09 10:30:00 +0000 | [diff] [blame] | 1150 | enum diff_symbol moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1151 | |
| 1152 | |
| 1153 | for (n = 0; n < o->emitted_symbols->nr; n++) { |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1154 | struct moved_entry *match = NULL; |
| 1155 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1156 | |
| 1157 | switch (l->s) { |
| 1158 | case DIFF_SYMBOL_PLUS: |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1159 | match = entry_list[l->id].del; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1160 | break; |
| 1161 | case DIFF_SYMBOL_MINUS: |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1162 | match = entry_list[l->id].add; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1163 | break; |
| 1164 | default: |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1165 | flipped_block = 0; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
Phillip Wood | eb89352 | 2021-12-09 10:30:00 +0000 | [diff] [blame] | 1168 | if (pmb_nr && (!match || l->s != moved_symbol)) { |
Phillip Wood | 0990658 | 2021-12-09 10:29:58 +0000 | [diff] [blame] | 1169 | if (!adjust_last_block(o, n, block_length) && |
| 1170 | block_length > 1) { |
| 1171 | /* |
| 1172 | * Rewind in case there is another match |
| 1173 | * starting at the second line of the block |
| 1174 | */ |
| 1175 | match = NULL; |
| 1176 | n -= block_length; |
| 1177 | } |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1178 | pmb_nr = 0; |
| 1179 | block_length = 0; |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1180 | flipped_block = 0; |
Phillip Wood | eb89352 | 2021-12-09 10:30:00 +0000 | [diff] [blame] | 1181 | } |
| 1182 | if (!match) { |
| 1183 | moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1184 | continue; |
| 1185 | } |
| 1186 | |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1187 | if (o->color_moved == COLOR_MOVED_PLAIN) { |
| 1188 | l->flags |= DIFF_SYMBOL_MOVED_LINE; |
Stefan Beller | 176841f | 2017-06-30 13:53:08 -0700 | [diff] [blame] | 1189 | continue; |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1190 | } |
Stefan Beller | 176841f | 2017-06-30 13:53:08 -0700 | [diff] [blame] | 1191 | |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1192 | pmb_advance_or_null(o, l, pmb, &pmb_nr); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1193 | |
| 1194 | if (pmb_nr == 0) { |
Phillip Wood | 0990658 | 2021-12-09 10:29:58 +0000 | [diff] [blame] | 1195 | int contiguous = adjust_last_block(o, n, block_length); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1196 | |
Phillip Wood | 0990658 | 2021-12-09 10:29:58 +0000 | [diff] [blame] | 1197 | if (!contiguous && block_length > 1) |
| 1198 | /* |
| 1199 | * Rewind in case there is another match |
| 1200 | * starting at the second line of the block |
| 1201 | */ |
| 1202 | n -= block_length; |
| 1203 | else |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1204 | fill_potential_moved_blocks(o, match, l, |
Phillip Wood | 0990658 | 2021-12-09 10:29:58 +0000 | [diff] [blame] | 1205 | &pmb, &pmb_alloc, |
| 1206 | &pmb_nr); |
| 1207 | |
Phillip Wood | eb89352 | 2021-12-09 10:30:00 +0000 | [diff] [blame] | 1208 | if (contiguous && pmb_nr && moved_symbol == l->s) |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1209 | flipped_block = (flipped_block + 1) % 2; |
| 1210 | else |
| 1211 | flipped_block = 0; |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1212 | |
Phillip Wood | eb89352 | 2021-12-09 10:30:00 +0000 | [diff] [blame] | 1213 | if (pmb_nr) |
| 1214 | moved_symbol = l->s; |
| 1215 | else |
| 1216 | moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER; |
| 1217 | |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1218 | block_length = 0; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1221 | if (pmb_nr) { |
| 1222 | block_length++; |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1223 | l->flags |= DIFF_SYMBOL_MOVED_LINE; |
| 1224 | if (flipped_block && o->color_moved != COLOR_MOVED_BLOCKS) |
| 1225 | l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT; |
| 1226 | } |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1227 | } |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1228 | adjust_last_block(o, n, block_length); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1229 | |
| 1230 | free(pmb); |
| 1231 | } |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1232 | |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1233 | static void dim_moved_lines(struct diff_options *o) |
| 1234 | { |
| 1235 | int n; |
| 1236 | for (n = 0; n < o->emitted_symbols->nr; n++) { |
| 1237 | struct emitted_diff_symbol *prev = (n != 0) ? |
| 1238 | &o->emitted_symbols->buf[n - 1] : NULL; |
| 1239 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; |
| 1240 | struct emitted_diff_symbol *next = |
| 1241 | (n < o->emitted_symbols->nr - 1) ? |
| 1242 | &o->emitted_symbols->buf[n + 1] : NULL; |
| 1243 | |
| 1244 | /* Not a plus or minus line? */ |
| 1245 | if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS) |
| 1246 | continue; |
| 1247 | |
| 1248 | /* Not a moved line? */ |
| 1249 | if (!(l->flags & DIFF_SYMBOL_MOVED_LINE)) |
| 1250 | continue; |
| 1251 | |
| 1252 | /* |
| 1253 | * If prev or next are not a plus or minus line, |
| 1254 | * pretend they don't exist |
| 1255 | */ |
| 1256 | if (prev && prev->s != DIFF_SYMBOL_PLUS && |
| 1257 | prev->s != DIFF_SYMBOL_MINUS) |
| 1258 | prev = NULL; |
| 1259 | if (next && next->s != DIFF_SYMBOL_PLUS && |
| 1260 | next->s != DIFF_SYMBOL_MINUS) |
| 1261 | next = NULL; |
| 1262 | |
| 1263 | /* Inside a block? */ |
| 1264 | if ((prev && |
| 1265 | (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) == |
| 1266 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) && |
| 1267 | (next && |
| 1268 | (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) == |
| 1269 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) { |
| 1270 | l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING; |
| 1271 | continue; |
| 1272 | } |
| 1273 | |
| 1274 | /* Check if we are at an interesting bound: */ |
| 1275 | if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) && |
| 1276 | (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) != |
| 1277 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT)) |
| 1278 | continue; |
| 1279 | if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) && |
| 1280 | (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) != |
| 1281 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT)) |
| 1282 | continue; |
| 1283 | |
| 1284 | /* |
| 1285 | * The boundary to prev and next are not interesting, |
| 1286 | * so this line is not interesting as a whole |
| 1287 | */ |
| 1288 | l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING; |
| 1289 | } |
| 1290 | } |
| 1291 | |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1292 | static void emit_line_ws_markup(struct diff_options *o, |
Stefan Beller | 9d1e16b | 2018-08-13 18:41:18 -0700 | [diff] [blame] | 1293 | const char *set_sign, const char *set, |
| 1294 | const char *reset, |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1295 | int sign_index, const char *line, int len, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1296 | unsigned ws_rule, int blank_at_eof) |
| 1297 | { |
| 1298 | const char *ws = NULL; |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1299 | int sign = o->output_indicators[sign_index]; |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1300 | |
| 1301 | if (o->ws_error_highlight & ws_rule) { |
| 1302 | ws = diff_get_color_opt(o, DIFF_WHITESPACE); |
| 1303 | if (!*ws) |
| 1304 | ws = NULL; |
| 1305 | } |
| 1306 | |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1307 | if (!ws && !set_sign) |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 1308 | emit_line_0(o, set, NULL, 0, reset, sign, line, len); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1309 | else if (!ws) { |
Stefan Beller | 29ef759 | 2018-08-13 18:41:20 -0700 | [diff] [blame] | 1310 | emit_line_0(o, set_sign, set, !!set_sign, reset, sign, line, len); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1311 | } else if (blank_at_eof) |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1312 | /* Blank line at EOF - paint '+' as well */ |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 1313 | emit_line_0(o, ws, NULL, 0, reset, sign, line, len); |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1314 | else { |
| 1315 | /* Emit just the prefix, then the rest. */ |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 1316 | emit_line_0(o, set_sign ? set_sign : set, NULL, !!set_sign, reset, |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1317 | sign, "", 0); |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1318 | ws_check_emit(line, len, ws_rule, |
| 1319 | o->file, set, reset, ws); |
| 1320 | } |
| 1321 | } |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1322 | |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1323 | static void emit_diff_symbol_from_struct(struct diff_options *o, |
| 1324 | struct emitted_diff_symbol *eds) |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1325 | { |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 1326 | static const char *nneof = " No newline at end of file\n"; |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1327 | const char *context, *reset, *set, *set_sign, *meta, *fraginfo; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1328 | |
| 1329 | enum diff_symbol s = eds->s; |
| 1330 | const char *line = eds->line; |
| 1331 | int len = eds->len; |
| 1332 | unsigned flags = eds->flags; |
| 1333 | |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1334 | switch (s) { |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 1335 | case DIFF_SYMBOL_NO_LF_EOF: |
| 1336 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1337 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1338 | putc('\n', o->file); |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 1339 | emit_line_0(o, context, NULL, 0, reset, '\\', |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 1340 | nneof, strlen(nneof)); |
| 1341 | break; |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 1342 | case DIFF_SYMBOL_SUBMODULE_HEADER: |
| 1343 | case DIFF_SYMBOL_SUBMODULE_ERROR: |
| 1344 | case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH: |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 1345 | case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 1346 | case DIFF_SYMBOL_SUMMARY: |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 1347 | case DIFF_SYMBOL_STATS_LINE: |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 1348 | case DIFF_SYMBOL_BINARY_DIFF_BODY: |
Stefan Beller | 68abc6f | 2017-06-29 17:06:51 -0700 | [diff] [blame] | 1349 | case DIFF_SYMBOL_CONTEXT_FRAGINFO: |
| 1350 | emit_line(o, "", "", line, len); |
| 1351 | break; |
Stefan Beller | f2bb121 | 2017-06-29 17:06:55 -0700 | [diff] [blame] | 1352 | case DIFF_SYMBOL_CONTEXT_INCOMPLETE: |
Stefan Beller | c64b420 | 2017-06-29 17:06:50 -0700 | [diff] [blame] | 1353 | case DIFF_SYMBOL_CONTEXT_MARKER: |
| 1354 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1355 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1356 | emit_line(o, context, reset, line, len); |
| 1357 | break; |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1358 | case DIFF_SYMBOL_SEPARATOR: |
| 1359 | fprintf(o->file, "%s%c", |
| 1360 | diff_line_prefix(o), |
| 1361 | o->line_termination); |
| 1362 | break; |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1363 | case DIFF_SYMBOL_CONTEXT: |
| 1364 | set = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1365 | reset = diff_get_color_opt(o, DIFF_RESET); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1366 | set_sign = NULL; |
| 1367 | if (o->flags.dual_color_diffed_diffs) { |
| 1368 | char c = !len ? 0 : line[0]; |
| 1369 | |
| 1370 | if (c == '+') |
| 1371 | set = diff_get_color_opt(o, DIFF_FILE_NEW); |
| 1372 | else if (c == '@') |
| 1373 | set = diff_get_color_opt(o, DIFF_FRAGINFO); |
| 1374 | else if (c == '-') |
| 1375 | set = diff_get_color_opt(o, DIFF_FILE_OLD); |
| 1376 | } |
Stefan Beller | 7648b79 | 2018-08-17 13:43:52 -0700 | [diff] [blame] | 1377 | emit_line_ws_markup(o, set_sign, set, reset, |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1378 | OUTPUT_INDICATOR_CONTEXT, line, len, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1379 | flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0); |
| 1380 | break; |
| 1381 | case DIFF_SYMBOL_PLUS: |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1382 | switch (flags & (DIFF_SYMBOL_MOVED_LINE | |
| 1383 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1384 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) { |
| 1385 | case DIFF_SYMBOL_MOVED_LINE | |
| 1386 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1387 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1388 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM); |
| 1389 | break; |
| 1390 | case DIFF_SYMBOL_MOVED_LINE | |
| 1391 | DIFF_SYMBOL_MOVED_LINE_ALT: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1392 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1393 | break; |
| 1394 | case DIFF_SYMBOL_MOVED_LINE | |
| 1395 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1396 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM); |
| 1397 | break; |
| 1398 | case DIFF_SYMBOL_MOVED_LINE: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1399 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1400 | break; |
| 1401 | default: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1402 | set = diff_get_color_opt(o, DIFF_FILE_NEW); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1403 | } |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1404 | reset = diff_get_color_opt(o, DIFF_RESET); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1405 | if (!o->flags.dual_color_diffed_diffs) |
| 1406 | set_sign = NULL; |
| 1407 | else { |
| 1408 | char c = !len ? 0 : line[0]; |
| 1409 | |
| 1410 | set_sign = set; |
| 1411 | if (c == '-') |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 1412 | set = diff_get_color_opt(o, DIFF_FILE_OLD_BOLD); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1413 | else if (c == '@') |
| 1414 | set = diff_get_color_opt(o, DIFF_FRAGINFO); |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 1415 | else if (c == '+') |
| 1416 | set = diff_get_color_opt(o, DIFF_FILE_NEW_BOLD); |
| 1417 | else |
| 1418 | set = diff_get_color_opt(o, DIFF_CONTEXT_BOLD); |
Johannes Schindelin | 0b91faa | 2018-08-13 04:33:24 -0700 | [diff] [blame] | 1419 | flags &= ~DIFF_SYMBOL_CONTENT_WS_MASK; |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1420 | } |
Stefan Beller | 7648b79 | 2018-08-17 13:43:52 -0700 | [diff] [blame] | 1421 | emit_line_ws_markup(o, set_sign, set, reset, |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1422 | OUTPUT_INDICATOR_NEW, line, len, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1423 | flags & DIFF_SYMBOL_CONTENT_WS_MASK, |
| 1424 | flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF); |
| 1425 | break; |
| 1426 | case DIFF_SYMBOL_MINUS: |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1427 | switch (flags & (DIFF_SYMBOL_MOVED_LINE | |
| 1428 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1429 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) { |
| 1430 | case DIFF_SYMBOL_MOVED_LINE | |
| 1431 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1432 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1433 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM); |
| 1434 | break; |
| 1435 | case DIFF_SYMBOL_MOVED_LINE | |
| 1436 | DIFF_SYMBOL_MOVED_LINE_ALT: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1437 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1438 | break; |
| 1439 | case DIFF_SYMBOL_MOVED_LINE | |
| 1440 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1441 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM); |
| 1442 | break; |
| 1443 | case DIFF_SYMBOL_MOVED_LINE: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1444 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1445 | break; |
| 1446 | default: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1447 | set = diff_get_color_opt(o, DIFF_FILE_OLD); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1448 | } |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1449 | reset = diff_get_color_opt(o, DIFF_RESET); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1450 | if (!o->flags.dual_color_diffed_diffs) |
| 1451 | set_sign = NULL; |
| 1452 | else { |
| 1453 | char c = !len ? 0 : line[0]; |
| 1454 | |
| 1455 | set_sign = set; |
| 1456 | if (c == '+') |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 1457 | set = diff_get_color_opt(o, DIFF_FILE_NEW_DIM); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1458 | else if (c == '@') |
| 1459 | set = diff_get_color_opt(o, DIFF_FRAGINFO); |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 1460 | else if (c == '-') |
| 1461 | set = diff_get_color_opt(o, DIFF_FILE_OLD_DIM); |
| 1462 | else |
| 1463 | set = diff_get_color_opt(o, DIFF_CONTEXT_DIM); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1464 | } |
Stefan Beller | 7648b79 | 2018-08-17 13:43:52 -0700 | [diff] [blame] | 1465 | emit_line_ws_markup(o, set_sign, set, reset, |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1466 | OUTPUT_INDICATOR_OLD, line, len, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1467 | flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0); |
| 1468 | break; |
Stefan Beller | ff95867 | 2017-06-29 17:06:54 -0700 | [diff] [blame] | 1469 | case DIFF_SYMBOL_WORDS_PORCELAIN: |
| 1470 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1471 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1472 | emit_line(o, context, reset, line, len); |
| 1473 | fputs("~\n", o->file); |
| 1474 | break; |
| 1475 | case DIFF_SYMBOL_WORDS: |
| 1476 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1477 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1478 | /* |
| 1479 | * Skip the prefix character, if any. With |
| 1480 | * diff_suppress_blank_empty, there may be |
| 1481 | * none. |
| 1482 | */ |
| 1483 | if (line[0] != '\n') { |
| 1484 | line++; |
| 1485 | len--; |
| 1486 | } |
| 1487 | emit_line(o, context, reset, line, len); |
| 1488 | break; |
Stefan Beller | 3ee8b7b | 2017-06-29 17:06:56 -0700 | [diff] [blame] | 1489 | case DIFF_SYMBOL_FILEPAIR_PLUS: |
| 1490 | meta = diff_get_color_opt(o, DIFF_METAINFO); |
| 1491 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1492 | fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta, |
| 1493 | line, reset, |
| 1494 | strchr(line, ' ') ? "\t" : ""); |
| 1495 | break; |
| 1496 | case DIFF_SYMBOL_FILEPAIR_MINUS: |
| 1497 | meta = diff_get_color_opt(o, DIFF_METAINFO); |
| 1498 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1499 | fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta, |
| 1500 | line, reset, |
| 1501 | strchr(line, ' ') ? "\t" : ""); |
| 1502 | break; |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 1503 | case DIFF_SYMBOL_BINARY_FILES: |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 1504 | case DIFF_SYMBOL_HEADER: |
| 1505 | fprintf(o->file, "%s", line); |
| 1506 | break; |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 1507 | case DIFF_SYMBOL_BINARY_DIFF_HEADER: |
| 1508 | fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o)); |
| 1509 | break; |
| 1510 | case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA: |
| 1511 | fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line); |
| 1512 | break; |
| 1513 | case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL: |
| 1514 | fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line); |
| 1515 | break; |
| 1516 | case DIFF_SYMBOL_BINARY_DIFF_FOOTER: |
| 1517 | fputs(diff_line_prefix(o), o->file); |
| 1518 | fputc('\n', o->file); |
| 1519 | break; |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1520 | case DIFF_SYMBOL_REWRITE_DIFF: |
| 1521 | fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO); |
| 1522 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1523 | emit_line(o, fraginfo, reset, line, len); |
| 1524 | break; |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 1525 | case DIFF_SYMBOL_SUBMODULE_ADD: |
| 1526 | set = diff_get_color_opt(o, DIFF_FILE_NEW); |
| 1527 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1528 | emit_line(o, set, reset, line, len); |
| 1529 | break; |
| 1530 | case DIFF_SYMBOL_SUBMODULE_DEL: |
| 1531 | set = diff_get_color_opt(o, DIFF_FILE_OLD); |
| 1532 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1533 | emit_line(o, set, reset, line, len); |
| 1534 | break; |
| 1535 | case DIFF_SYMBOL_SUBMODULE_UNTRACKED: |
| 1536 | fprintf(o->file, "%sSubmodule %s contains untracked content\n", |
| 1537 | diff_line_prefix(o), line); |
| 1538 | break; |
| 1539 | case DIFF_SYMBOL_SUBMODULE_MODIFIED: |
| 1540 | fprintf(o->file, "%sSubmodule %s contains modified content\n", |
| 1541 | diff_line_prefix(o), line); |
| 1542 | break; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 1543 | case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES: |
| 1544 | emit_line(o, "", "", " 0 files changed\n", |
| 1545 | strlen(" 0 files changed\n")); |
| 1546 | break; |
| 1547 | case DIFF_SYMBOL_STATS_SUMMARY_ABBREV: |
| 1548 | emit_line(o, "", "", " ...\n", strlen(" ...\n")); |
| 1549 | break; |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1550 | case DIFF_SYMBOL_WORD_DIFF: |
| 1551 | fprintf(o->file, "%.*s", len, line); |
| 1552 | break; |
Stefan Beller | 30b7e1e | 2017-06-29 17:07:04 -0700 | [diff] [blame] | 1553 | case DIFF_SYMBOL_STAT_SEP: |
| 1554 | fputs(o->stat_sep, o->file); |
| 1555 | break; |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1556 | default: |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 1557 | BUG("unknown diff symbol"); |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1558 | } |
| 1559 | } |
| 1560 | |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1561 | static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, |
| 1562 | const char *line, int len, unsigned flags) |
| 1563 | { |
Phillip Wood | b4a5c5c | 2021-12-09 10:30:08 +0000 | [diff] [blame] | 1564 | struct emitted_diff_symbol e = { |
| 1565 | .line = line, .len = len, .flags = flags, .s = s |
| 1566 | }; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1567 | |
| 1568 | if (o->emitted_symbols) |
| 1569 | append_emitted_diff_symbol(o, &e); |
| 1570 | else |
| 1571 | emit_diff_symbol_from_struct(o, &e); |
| 1572 | } |
| 1573 | |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 1574 | void diff_emit_submodule_del(struct diff_options *o, const char *line) |
| 1575 | { |
| 1576 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0); |
| 1577 | } |
| 1578 | |
| 1579 | void diff_emit_submodule_add(struct diff_options *o, const char *line) |
| 1580 | { |
| 1581 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0); |
| 1582 | } |
| 1583 | |
| 1584 | void diff_emit_submodule_untracked(struct diff_options *o, const char *path) |
| 1585 | { |
| 1586 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED, |
| 1587 | path, strlen(path), 0); |
| 1588 | } |
| 1589 | |
| 1590 | void diff_emit_submodule_modified(struct diff_options *o, const char *path) |
| 1591 | { |
| 1592 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED, |
| 1593 | path, strlen(path), 0); |
| 1594 | } |
| 1595 | |
| 1596 | void diff_emit_submodule_header(struct diff_options *o, const char *header) |
| 1597 | { |
| 1598 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER, |
| 1599 | header, strlen(header), 0); |
| 1600 | } |
| 1601 | |
| 1602 | void diff_emit_submodule_error(struct diff_options *o, const char *err) |
| 1603 | { |
| 1604 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0); |
| 1605 | } |
| 1606 | |
| 1607 | void diff_emit_submodule_pipethrough(struct diff_options *o, |
| 1608 | const char *line, int len) |
| 1609 | { |
| 1610 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0); |
| 1611 | } |
| 1612 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1613 | static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len) |
| 1614 | { |
| 1615 | if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) && |
| 1616 | ecbdata->blank_at_eof_in_preimage && |
| 1617 | ecbdata->blank_at_eof_in_postimage && |
| 1618 | ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage && |
| 1619 | ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage)) |
| 1620 | return 0; |
Jeff King | c5224f0 | 2022-12-13 06:12:58 -0500 | [diff] [blame] | 1621 | return ws_blank_line(line, len); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1622 | } |
| 1623 | |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1624 | static void emit_add_line(struct emit_callback *ecbdata, |
Junio C Hamano | 018cff7 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1625 | const char *line, int len) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1626 | { |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1627 | unsigned flags = WSEH_NEW | ecbdata->ws_rule; |
| 1628 | if (new_blank_line_at_eof(ecbdata, line, len)) |
| 1629 | flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF; |
| 1630 | |
| 1631 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1632 | } |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1633 | |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1634 | static void emit_del_line(struct emit_callback *ecbdata, |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 1635 | const char *line, int len) |
| 1636 | { |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1637 | unsigned flags = WSEH_OLD | ecbdata->ws_rule; |
| 1638 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 1639 | } |
| 1640 | |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1641 | static void emit_context_line(struct emit_callback *ecbdata, |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 1642 | const char *line, int len) |
| 1643 | { |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1644 | unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule; |
| 1645 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1646 | } |
| 1647 | |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1648 | static void emit_hunk_header(struct emit_callback *ecbdata, |
| 1649 | const char *line, int len) |
| 1650 | { |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 1651 | const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT); |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1652 | const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO); |
| 1653 | const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO); |
| 1654 | const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1655 | const char *reverse = ecbdata->color_diff ? GIT_COLOR_REVERSE : ""; |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1656 | static const char atat[2] = { '@', '@' }; |
| 1657 | const char *cp, *ep; |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1658 | struct strbuf msgbuf = STRBUF_INIT; |
| 1659 | int org_len = len; |
| 1660 | int i = 1; |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1661 | |
| 1662 | /* |
| 1663 | * As a hunk header must begin with "@@ -<old>, +<new> @@", |
| 1664 | * it always is at least 10 bytes long. |
| 1665 | */ |
| 1666 | if (len < 10 || |
| 1667 | memcmp(line, atat, 2) || |
| 1668 | !(ep = memmem(line + 2, len - 2, atat, 2))) { |
Stefan Beller | c64b420 | 2017-06-29 17:06:50 -0700 | [diff] [blame] | 1669 | emit_diff_symbol(ecbdata->opt, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1670 | DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0); |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1671 | return; |
| 1672 | } |
| 1673 | ep += 2; /* skip over @@ */ |
| 1674 | |
| 1675 | /* The hunk header in fraginfo color */ |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1676 | if (ecbdata->opt->flags.dual_color_diffed_diffs) |
| 1677 | strbuf_addstr(&msgbuf, reverse); |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1678 | strbuf_addstr(&msgbuf, frag); |
Thomas Gummerer | 430be36 | 2019-07-11 17:08:48 +0100 | [diff] [blame] | 1679 | if (ecbdata->opt->flags.suppress_hunk_header_line_count) |
| 1680 | strbuf_add(&msgbuf, atat, sizeof(atat)); |
| 1681 | else |
| 1682 | strbuf_add(&msgbuf, line, ep - line); |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1683 | strbuf_addstr(&msgbuf, reset); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1684 | |
| 1685 | /* |
| 1686 | * trailing "\r\n" |
| 1687 | */ |
| 1688 | for ( ; i < 3; i++) |
| 1689 | if (line[len - i] == '\r' || line[len - i] == '\n') |
| 1690 | len--; |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1691 | |
| 1692 | /* blank before the func header */ |
| 1693 | for (cp = ep; ep - line < len; ep++) |
| 1694 | if (*ep != ' ' && *ep != '\t') |
| 1695 | break; |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1696 | if (ep != cp) { |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 1697 | strbuf_addstr(&msgbuf, context); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1698 | strbuf_add(&msgbuf, cp, ep - cp); |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1699 | strbuf_addstr(&msgbuf, reset); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1700 | } |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1701 | |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1702 | if (ep < line + len) { |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1703 | strbuf_addstr(&msgbuf, func); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1704 | strbuf_add(&msgbuf, ep, line + len - ep); |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1705 | strbuf_addstr(&msgbuf, reset); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | strbuf_add(&msgbuf, line + len, org_len - len); |
Stefan Beller | dfb7728 | 2017-06-29 17:06:47 -0700 | [diff] [blame] | 1709 | strbuf_complete_line(&msgbuf); |
Stefan Beller | 68abc6f | 2017-06-29 17:06:51 -0700 | [diff] [blame] | 1710 | emit_diff_symbol(ecbdata->opt, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1711 | DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1712 | strbuf_release(&msgbuf); |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1713 | } |
| 1714 | |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 1715 | static struct diff_tempfile *claim_diff_tempfile(void) |
| 1716 | { |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 1717 | int i; |
| 1718 | for (i = 0; i < ARRAY_SIZE(diff_temp); i++) |
| 1719 | if (!diff_temp[i].name) |
| 1720 | return diff_temp + i; |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 1721 | BUG("diff is failing to clean up its tempfiles"); |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 1722 | } |
| 1723 | |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 1724 | static void remove_tempfile(void) |
| 1725 | { |
| 1726 | int i; |
Nazri Ramliy | a8344ab | 2009-02-12 21:36:14 +0800 | [diff] [blame] | 1727 | for (i = 0; i < ARRAY_SIZE(diff_temp); i++) { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 1728 | if (is_tempfile_active(diff_temp[i].tempfile)) |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 1729 | delete_tempfile(&diff_temp[i].tempfile); |
Nazri Ramliy | a8344ab | 2009-02-12 21:36:14 +0800 | [diff] [blame] | 1730 | diff_temp[i].name = NULL; |
| 1731 | } |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 1732 | } |
| 1733 | |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1734 | static void add_line_count(struct strbuf *out, int count) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1735 | { |
| 1736 | switch (count) { |
| 1737 | case 0: |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1738 | strbuf_addstr(out, "0,0"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1739 | break; |
| 1740 | case 1: |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1741 | strbuf_addstr(out, "1"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1742 | break; |
| 1743 | default: |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1744 | strbuf_addf(out, "1,%d", count); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1745 | break; |
| 1746 | } |
| 1747 | } |
| 1748 | |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1749 | static void emit_rewrite_lines(struct emit_callback *ecb, |
| 1750 | int prefix, const char *data, int size) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1751 | { |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1752 | const char *endp = NULL; |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1753 | |
| 1754 | while (0 < size) { |
| 1755 | int len; |
| 1756 | |
| 1757 | endp = memchr(data, '\n', size); |
| 1758 | len = endp ? (endp - data + 1) : size; |
| 1759 | if (prefix != '+') { |
| 1760 | ecb->lno_in_preimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1761 | emit_del_line(ecb, data, len); |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1762 | } else { |
| 1763 | ecb->lno_in_postimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1764 | emit_add_line(ecb, data, len); |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 1765 | } |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1766 | size -= len; |
| 1767 | data += len; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1768 | } |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 1769 | if (!endp) |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1770 | emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | static void emit_rewrite_diff(const char *name_a, |
| 1774 | const char *name_b, |
| 1775 | struct diff_filespec *one, |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 1776 | struct diff_filespec *two, |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 1777 | struct userdiff_driver *textconv_one, |
| 1778 | struct userdiff_driver *textconv_two, |
Johannes Schindelin | eab9a40 | 2007-12-18 19:32:14 +0000 | [diff] [blame] | 1779 | struct diff_options *o) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1780 | { |
| 1781 | int lc_a, lc_b; |
Junio C Hamano | d562509 | 2007-12-26 17:13:36 -0800 | [diff] [blame] | 1782 | static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT; |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 1783 | const char *a_prefix, *b_prefix; |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 1784 | char *data_one, *data_two; |
Jeff King | 3aa1f7c | 2008-12-09 03:13:21 -0500 | [diff] [blame] | 1785 | size_t size_one, size_two; |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1786 | struct emit_callback ecbdata; |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1787 | struct strbuf out = STRBUF_INIT; |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 1788 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 1789 | if (diff_mnemonic_prefix && o->flags.reverse_diff) { |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 1790 | a_prefix = o->b_prefix; |
| 1791 | b_prefix = o->a_prefix; |
| 1792 | } else { |
| 1793 | a_prefix = o->a_prefix; |
| 1794 | b_prefix = o->b_prefix; |
| 1795 | } |
Junio C Hamano | 1a9eb3b | 2006-09-22 16:17:58 -0700 | [diff] [blame] | 1796 | |
Junio C Hamano | 8a13bec | 2007-02-24 01:42:06 -0800 | [diff] [blame] | 1797 | name_a += (*name_a == '/'); |
| 1798 | name_b += (*name_b == '/'); |
Junio C Hamano | 1a9eb3b | 2006-09-22 16:17:58 -0700 | [diff] [blame] | 1799 | |
Junio C Hamano | d562509 | 2007-12-26 17:13:36 -0800 | [diff] [blame] | 1800 | strbuf_reset(&a_name); |
| 1801 | strbuf_reset(&b_name); |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 1802 | quote_two_c_style(&a_name, a_prefix, name_a, 0); |
| 1803 | quote_two_c_style(&b_name, b_prefix, name_b, 0); |
Junio C Hamano | d562509 | 2007-12-26 17:13:36 -0800 | [diff] [blame] | 1804 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 1805 | size_one = fill_textconv(o->repo, textconv_one, one, &data_one); |
| 1806 | size_two = fill_textconv(o->repo, textconv_two, two, &data_two); |
Jeff King | 3aa1f7c | 2008-12-09 03:13:21 -0500 | [diff] [blame] | 1807 | |
Junio C Hamano | d91ba8f | 2009-09-15 11:21:10 -0700 | [diff] [blame] | 1808 | memset(&ecbdata, 0, sizeof(ecbdata)); |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 1809 | ecbdata.color_diff = want_color(o->use_color); |
Nguyễn Thái Ngọc Duy | 26d024e | 2018-09-21 17:57:37 +0200 | [diff] [blame] | 1810 | ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b); |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 1811 | ecbdata.opt = o; |
Junio C Hamano | d91ba8f | 2009-09-15 11:21:10 -0700 | [diff] [blame] | 1812 | if (ecbdata.ws_rule & WS_BLANK_AT_EOF) { |
| 1813 | mmfile_t mf1, mf2; |
| 1814 | mf1.ptr = (char *)data_one; |
| 1815 | mf2.ptr = (char *)data_two; |
| 1816 | mf1.size = size_one; |
| 1817 | mf2.size = size_two; |
| 1818 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
| 1819 | } |
| 1820 | ecbdata.lno_in_preimage = 1; |
| 1821 | ecbdata.lno_in_postimage = 1; |
| 1822 | |
Jeff King | 3aa1f7c | 2008-12-09 03:13:21 -0500 | [diff] [blame] | 1823 | lc_a = count_lines(data_one, size_one); |
| 1824 | lc_b = count_lines(data_two, size_two); |
Stefan Beller | 3ee8b7b | 2017-06-29 17:06:56 -0700 | [diff] [blame] | 1825 | |
| 1826 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS, |
| 1827 | a_name.buf, a_name.len, 0); |
| 1828 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS, |
| 1829 | b_name.buf, b_name.len, 0); |
| 1830 | |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1831 | strbuf_addstr(&out, "@@ -"); |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 1832 | if (!o->irreversible_delete) |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1833 | add_line_count(&out, lc_a); |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 1834 | else |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1835 | strbuf_addstr(&out, "?,?"); |
| 1836 | strbuf_addstr(&out, " +"); |
| 1837 | add_line_count(&out, lc_b); |
| 1838 | strbuf_addstr(&out, " @@\n"); |
| 1839 | emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0); |
| 1840 | strbuf_release(&out); |
| 1841 | |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 1842 | if (lc_a && !o->irreversible_delete) |
Junio C Hamano | d91ba8f | 2009-09-15 11:21:10 -0700 | [diff] [blame] | 1843 | emit_rewrite_lines(&ecbdata, '-', data_one, size_one); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1844 | if (lc_b) |
Junio C Hamano | d91ba8f | 2009-09-15 11:21:10 -0700 | [diff] [blame] | 1845 | emit_rewrite_lines(&ecbdata, '+', data_two, size_two); |
Jeff King | b76c056 | 2010-04-01 20:04:14 -0400 | [diff] [blame] | 1846 | if (textconv_one) |
Junio C Hamano | aed6ca5 | 2010-04-08 23:30:49 -0700 | [diff] [blame] | 1847 | free((char *)data_one); |
Jeff King | b76c056 | 2010-04-01 20:04:14 -0400 | [diff] [blame] | 1848 | if (textconv_two) |
Junio C Hamano | aed6ca5 | 2010-04-08 23:30:49 -0700 | [diff] [blame] | 1849 | free((char *)data_two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1850 | } |
| 1851 | |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1852 | struct diff_words_buffer { |
| 1853 | mmfile_t text; |
Ramsay Jones | 071bcaa | 2017-09-21 17:49:38 +0100 | [diff] [blame] | 1854 | unsigned long alloc; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 1855 | struct diff_words_orig { |
| 1856 | const char *begin, *end; |
| 1857 | } *orig; |
| 1858 | int orig_nr, orig_alloc; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1859 | }; |
| 1860 | |
| 1861 | static void diff_words_append(char *line, unsigned long len, |
| 1862 | struct diff_words_buffer *buffer) |
| 1863 | { |
Johannes Schindelin | 23c1575 | 2009-01-17 17:29:43 +0100 | [diff] [blame] | 1864 | ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc); |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1865 | line++; |
| 1866 | len--; |
| 1867 | memcpy(buffer->text.ptr + buffer->text.size, line, len); |
| 1868 | buffer->text.size += len; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 1869 | buffer->text.ptr[buffer->text.size] = '\0'; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1870 | } |
| 1871 | |
Jonathan Nieder | 9cba13c | 2011-03-16 02:08:34 -0500 | [diff] [blame] | 1872 | struct diff_words_style_elem { |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1873 | const char *prefix; |
| 1874 | const char *suffix; |
| 1875 | const char *color; /* NULL; filled in by the setup code if |
| 1876 | * color is enabled */ |
| 1877 | }; |
| 1878 | |
Jonathan Nieder | 9cba13c | 2011-03-16 02:08:34 -0500 | [diff] [blame] | 1879 | struct diff_words_style { |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1880 | enum diff_words_type type; |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 1881 | struct diff_words_style_elem new_word, old_word, ctx; |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1882 | const char *newline; |
| 1883 | }; |
| 1884 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 1885 | static struct diff_words_style diff_words_styles[] = { |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1886 | { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" }, |
| 1887 | { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" }, |
| 1888 | { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" } |
| 1889 | }; |
| 1890 | |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1891 | struct diff_words_data { |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1892 | struct diff_words_buffer minus, plus; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 1893 | const char *current_plus; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1894 | int last_minus; |
| 1895 | struct diff_options *opt; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 1896 | regex_t *word_regex; |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1897 | enum diff_words_type type; |
| 1898 | struct diff_words_style *style; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1899 | }; |
| 1900 | |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1901 | static int fn_out_diff_words_write_helper(struct diff_options *o, |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1902 | struct diff_words_style_elem *st_el, |
| 1903 | const char *newline, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1904 | size_t count, const char *buf) |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1905 | { |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1906 | int print = 0; |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1907 | struct strbuf sb = STRBUF_INIT; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1908 | |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1909 | while (count) { |
| 1910 | char *p = memchr(buf, '\n', count); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1911 | if (print) |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1912 | strbuf_addstr(&sb, diff_line_prefix(o)); |
| 1913 | |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1914 | if (p != buf) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1915 | const char *reset = st_el->color && *st_el->color ? |
| 1916 | GIT_COLOR_RESET : NULL; |
| 1917 | if (st_el->color && *st_el->color) |
| 1918 | strbuf_addstr(&sb, st_el->color); |
| 1919 | strbuf_addstr(&sb, st_el->prefix); |
| 1920 | strbuf_add(&sb, buf, p ? p - buf : count); |
| 1921 | strbuf_addstr(&sb, st_el->suffix); |
| 1922 | if (reset) |
| 1923 | strbuf_addstr(&sb, reset); |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1924 | } |
| 1925 | if (!p) |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1926 | goto out; |
| 1927 | |
| 1928 | strbuf_addstr(&sb, newline); |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1929 | count -= p + 1 - buf; |
| 1930 | buf = p + 1; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1931 | print = 1; |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1932 | if (count) { |
| 1933 | emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF, |
| 1934 | sb.buf, sb.len, 0); |
| 1935 | strbuf_reset(&sb); |
| 1936 | } |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1937 | } |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1938 | |
| 1939 | out: |
| 1940 | if (sb.len) |
| 1941 | emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF, |
| 1942 | sb.buf, sb.len, 0); |
| 1943 | strbuf_release(&sb); |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1944 | return 0; |
| 1945 | } |
| 1946 | |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1947 | /* |
| 1948 | * '--color-words' algorithm can be described as: |
| 1949 | * |
René Genz | 5621760 | 2017-04-30 17:48:27 +0200 | [diff] [blame] | 1950 | * 1. collect the minus/plus lines of a diff hunk, divided into |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1951 | * minus-lines and plus-lines; |
| 1952 | * |
| 1953 | * 2. break both minus-lines and plus-lines into words and |
| 1954 | * place them into two mmfile_t with one word for each line; |
| 1955 | * |
| 1956 | * 3. use xdiff to run diff on the two mmfile_t to get the words level diff; |
| 1957 | * |
| 1958 | * And for the common parts of the both file, we output the plus side text. |
| 1959 | * diff_words->current_plus is used to trace the current position of the plus file |
| 1960 | * which printed. diff_words->last_minus is used to trace the last minus word |
| 1961 | * printed. |
| 1962 | * |
| 1963 | * For '--graph' to work with '--color-words', we need to output the graph prefix |
| 1964 | * on each line of color words output. Generally, there are two conditions on |
| 1965 | * which we should output the prefix. |
| 1966 | * |
| 1967 | * 1. diff_words->last_minus == 0 && |
| 1968 | * diff_words->current_plus == diff_words->plus.text.ptr |
| 1969 | * |
| 1970 | * that is: the plus text must start as a new line, and if there is no minus |
| 1971 | * word printed, a graph prefix must be printed. |
| 1972 | * |
| 1973 | * 2. diff_words->current_plus > diff_words->plus.text.ptr && |
| 1974 | * *(diff_words->current_plus - 1) == '\n' |
| 1975 | * |
| 1976 | * that is: a graph prefix must be printed following a '\n' |
| 1977 | */ |
| 1978 | static int color_words_output_graph_prefix(struct diff_words_data *diff_words) |
| 1979 | { |
| 1980 | if ((diff_words->last_minus == 0 && |
| 1981 | diff_words->current_plus == diff_words->plus.text.ptr) || |
| 1982 | (diff_words->current_plus > diff_words->plus.text.ptr && |
| 1983 | *(diff_words->current_plus - 1) == '\n')) { |
| 1984 | return 1; |
| 1985 | } else { |
| 1986 | return 0; |
| 1987 | } |
| 1988 | } |
| 1989 | |
Jeff King | 7c61e25 | 2018-11-02 02:37:18 -0400 | [diff] [blame] | 1990 | static void fn_out_diff_words_aux(void *priv, |
| 1991 | long minus_first, long minus_len, |
| 1992 | long plus_first, long plus_len, |
Jeff King | 61bdc7c | 2022-12-13 06:13:48 -0500 | [diff] [blame] | 1993 | const char *func UNUSED, long funclen UNUSED) |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1994 | { |
| 1995 | struct diff_words_data *diff_words = priv; |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1996 | struct diff_words_style *style = diff_words->style; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 1997 | const char *minus_begin, *minus_end, *plus_begin, *plus_end; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1998 | struct diff_options *opt = diff_words->opt; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 1999 | const char *line_prefix; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2000 | |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2001 | assert(opt); |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2002 | line_prefix = diff_line_prefix(opt); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2003 | |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2004 | /* POSIX requires that first be decremented by one if len == 0... */ |
| 2005 | if (minus_len) { |
| 2006 | minus_begin = diff_words->minus.orig[minus_first].begin; |
| 2007 | minus_end = |
| 2008 | diff_words->minus.orig[minus_first + minus_len - 1].end; |
| 2009 | } else |
| 2010 | minus_begin = minus_end = |
| 2011 | diff_words->minus.orig[minus_first].end; |
| 2012 | |
| 2013 | if (plus_len) { |
| 2014 | plus_begin = diff_words->plus.orig[plus_first].begin; |
| 2015 | plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end; |
| 2016 | } else |
| 2017 | plus_begin = plus_end = diff_words->plus.orig[plus_first].end; |
| 2018 | |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2019 | if (color_words_output_graph_prefix(diff_words)) { |
| 2020 | fputs(line_prefix, diff_words->opt->file); |
| 2021 | } |
| 2022 | if (diff_words->current_plus != plus_begin) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2023 | fn_out_diff_words_write_helper(diff_words->opt, |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 2024 | &style->ctx, style->newline, |
| 2025 | plus_begin - diff_words->current_plus, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2026 | diff_words->current_plus); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2027 | } |
| 2028 | if (minus_begin != minus_end) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2029 | fn_out_diff_words_write_helper(diff_words->opt, |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2030 | &style->old_word, style->newline, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2031 | minus_end - minus_begin, minus_begin); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2032 | } |
| 2033 | if (plus_begin != plus_end) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2034 | fn_out_diff_words_write_helper(diff_words->opt, |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2035 | &style->new_word, style->newline, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2036 | plus_end - plus_begin, plus_begin); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2037 | } |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2038 | |
| 2039 | diff_words->current_plus = plus_end; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2040 | diff_words->last_minus = minus_first; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2041 | } |
| 2042 | |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2043 | /* This function starts looking at *begin, and returns 0 iff a word was found. */ |
| 2044 | static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex, |
| 2045 | int *begin, int *end) |
| 2046 | { |
Phillip Wood | 0324e8f | 2021-05-04 09:27:34 +0000 | [diff] [blame] | 2047 | while (word_regex && *begin < buffer->size) { |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2048 | regmatch_t match[1]; |
Johannes Schindelin | b7d36ff | 2016-09-21 20:24:14 +0200 | [diff] [blame] | 2049 | if (!regexec_buf(word_regex, buffer->ptr + *begin, |
| 2050 | buffer->size - *begin, 1, match, 0)) { |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2051 | char *p = memchr(buffer->ptr + *begin + match[0].rm_so, |
| 2052 | '\n', match[0].rm_eo - match[0].rm_so); |
| 2053 | *end = p ? p - buffer->ptr : match[0].rm_eo + *begin; |
| 2054 | *begin += match[0].rm_so; |
Phillip Wood | 0324e8f | 2021-05-04 09:27:34 +0000 | [diff] [blame] | 2055 | if (*begin == *end) |
| 2056 | (*begin)++; |
| 2057 | else |
| 2058 | return *begin > *end; |
| 2059 | } else { |
| 2060 | return -1; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2061 | } |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2062 | } |
| 2063 | |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2064 | /* find the next word */ |
| 2065 | while (*begin < buffer->size && isspace(buffer->ptr[*begin])) |
| 2066 | (*begin)++; |
| 2067 | if (*begin >= buffer->size) |
| 2068 | return -1; |
| 2069 | |
| 2070 | /* find the end of the word */ |
| 2071 | *end = *begin + 1; |
| 2072 | while (*end < buffer->size && !isspace(buffer->ptr[*end])) |
| 2073 | (*end)++; |
| 2074 | |
| 2075 | return 0; |
| 2076 | } |
| 2077 | |
Johannes Schindelin | 23c1575 | 2009-01-17 17:29:43 +0100 | [diff] [blame] | 2078 | /* |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2079 | * This function splits the words in buffer->text, stores the list with |
| 2080 | * newline separator into out, and saves the offsets of the original words |
| 2081 | * in buffer->orig. |
Johannes Schindelin | 23c1575 | 2009-01-17 17:29:43 +0100 | [diff] [blame] | 2082 | */ |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2083 | static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out, |
| 2084 | regex_t *word_regex) |
Johannes Schindelin | 23c1575 | 2009-01-17 17:29:43 +0100 | [diff] [blame] | 2085 | { |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2086 | int i, j; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2087 | long alloc = 0; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2088 | |
| 2089 | out->size = 0; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2090 | out->ptr = NULL; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2091 | |
| 2092 | /* fake an empty "0th" word */ |
| 2093 | ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc); |
| 2094 | buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr; |
| 2095 | buffer->orig_nr = 1; |
| 2096 | |
| 2097 | for (i = 0; i < buffer->text.size; i++) { |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2098 | if (find_word_boundaries(&buffer->text, word_regex, &i, &j)) |
| 2099 | return; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2100 | |
| 2101 | /* store original boundaries */ |
| 2102 | ALLOC_GROW(buffer->orig, buffer->orig_nr + 1, |
| 2103 | buffer->orig_alloc); |
| 2104 | buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i; |
| 2105 | buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j; |
| 2106 | buffer->orig_nr++; |
| 2107 | |
| 2108 | /* store one word */ |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2109 | ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2110 | memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i); |
| 2111 | out->ptr[out->size + j - i] = '\n'; |
| 2112 | out->size += j - i + 1; |
| 2113 | |
| 2114 | i = j - 1; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2115 | } |
| 2116 | } |
| 2117 | |
| 2118 | /* this executes the word diff on the accumulated buffers */ |
| 2119 | static void diff_words_show(struct diff_words_data *diff_words) |
| 2120 | { |
| 2121 | xpparam_t xpp; |
| 2122 | xdemitconf_t xecfg; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2123 | mmfile_t minus, plus; |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 2124 | struct diff_words_style *style = diff_words->style; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2125 | |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2126 | struct diff_options *opt = diff_words->opt; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2127 | const char *line_prefix; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2128 | |
| 2129 | assert(opt); |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2130 | line_prefix = diff_line_prefix(opt); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2131 | |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2132 | /* special case: only removal */ |
| 2133 | if (!diff_words->plus.text.size) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2134 | emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF, |
| 2135 | line_prefix, strlen(line_prefix), 0); |
| 2136 | fn_out_diff_words_write_helper(diff_words->opt, |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2137 | &style->old_word, style->newline, |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2138 | diff_words->minus.text.size, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2139 | diff_words->minus.text.ptr); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2140 | diff_words->minus.text.size = 0; |
| 2141 | return; |
| 2142 | } |
| 2143 | |
| 2144 | diff_words->current_plus = diff_words->plus.text.ptr; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2145 | diff_words->last_minus = 0; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2146 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 2147 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 2148 | memset(&xecfg, 0, sizeof(xecfg)); |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2149 | diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex); |
| 2150 | diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex); |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 2151 | xpp.flags = 0; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2152 | /* as only the hunk header will be parsed, we need a 0-context */ |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2153 | xecfg.ctxlen = 0; |
Jeff King | 7c61e25 | 2018-11-02 02:37:18 -0400 | [diff] [blame] | 2154 | if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, NULL, |
Jeff King | 9346d6d | 2018-11-02 02:35:45 -0400 | [diff] [blame] | 2155 | diff_words, &xpp, &xecfg)) |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 2156 | die("unable to generate word diff"); |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2157 | free(minus.ptr); |
| 2158 | free(plus.ptr); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2159 | if (diff_words->current_plus != diff_words->plus.text.ptr + |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2160 | diff_words->plus.text.size) { |
| 2161 | if (color_words_output_graph_prefix(diff_words)) |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2162 | emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF, |
| 2163 | line_prefix, strlen(line_prefix), 0); |
| 2164 | fn_out_diff_words_write_helper(diff_words->opt, |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 2165 | &style->ctx, style->newline, |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2166 | diff_words->plus.text.ptr + diff_words->plus.text.size |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2167 | - diff_words->current_plus, diff_words->current_plus); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2168 | } |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2169 | diff_words->minus.text.size = diff_words->plus.text.size = 0; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2170 | } |
| 2171 | |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2172 | /* In "color-words" mode, show word-diff of words accumulated in the buffer */ |
| 2173 | static void diff_words_flush(struct emit_callback *ecbdata) |
| 2174 | { |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 2175 | struct diff_options *wo = ecbdata->diff_words->opt; |
| 2176 | |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2177 | if (ecbdata->diff_words->minus.text.size || |
| 2178 | ecbdata->diff_words->plus.text.size) |
| 2179 | diff_words_show(ecbdata->diff_words); |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 2180 | |
| 2181 | if (wo->emitted_symbols) { |
| 2182 | struct diff_options *o = ecbdata->opt; |
| 2183 | struct emitted_diff_symbols *wol = wo->emitted_symbols; |
| 2184 | int i; |
| 2185 | |
| 2186 | /* |
| 2187 | * NEEDSWORK: |
| 2188 | * Instead of appending each, concat all words to a line? |
| 2189 | */ |
| 2190 | for (i = 0; i < wol->nr; i++) |
| 2191 | append_emitted_diff_symbol(o, &wol->buf[i]); |
| 2192 | |
| 2193 | for (i = 0; i < wol->nr; i++) |
| 2194 | free((void *)wol->buf[i].line); |
| 2195 | |
| 2196 | wol->nr = 0; |
| 2197 | } |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2198 | } |
| 2199 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2200 | static void diff_filespec_load_driver(struct diff_filespec *one, |
| 2201 | struct index_state *istate) |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2202 | { |
| 2203 | /* Use already-loaded driver */ |
| 2204 | if (one->driver) |
| 2205 | return; |
| 2206 | |
| 2207 | if (S_ISREG(one->mode)) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2208 | one->driver = userdiff_find_by_path(istate, one->path); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2209 | |
| 2210 | /* Fallback to default settings */ |
| 2211 | if (!one->driver) |
| 2212 | one->driver = userdiff_find_by_name("default"); |
| 2213 | } |
| 2214 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2215 | static const char *userdiff_word_regex(struct diff_filespec *one, |
| 2216 | struct index_state *istate) |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2217 | { |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2218 | diff_filespec_load_driver(one, istate); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2219 | return one->driver->word_regex; |
| 2220 | } |
| 2221 | |
| 2222 | static void init_diff_words_data(struct emit_callback *ecbdata, |
Thomas Rast | 6440d34 | 2012-03-14 19:24:09 +0100 | [diff] [blame] | 2223 | struct diff_options *orig_opts, |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2224 | struct diff_filespec *one, |
| 2225 | struct diff_filespec *two) |
| 2226 | { |
| 2227 | int i; |
Thomas Rast | 6440d34 | 2012-03-14 19:24:09 +0100 | [diff] [blame] | 2228 | struct diff_options *o = xmalloc(sizeof(struct diff_options)); |
| 2229 | memcpy(o, orig_opts, sizeof(struct diff_options)); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2230 | |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 2231 | CALLOC_ARRAY(ecbdata->diff_words, 1); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2232 | ecbdata->diff_words->type = o->word_diff; |
| 2233 | ecbdata->diff_words->opt = o; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 2234 | |
| 2235 | if (orig_opts->emitted_symbols) |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 2236 | CALLOC_ARRAY(o->emitted_symbols, 1); |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 2237 | |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2238 | if (!o->word_regex) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2239 | o->word_regex = userdiff_word_regex(one, o->repo->index); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2240 | if (!o->word_regex) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2241 | o->word_regex = userdiff_word_regex(two, o->repo->index); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2242 | if (!o->word_regex) |
| 2243 | o->word_regex = diff_word_regex_cfg; |
| 2244 | if (o->word_regex) { |
| 2245 | ecbdata->diff_words->word_regex = (regex_t *) |
| 2246 | xmalloc(sizeof(regex_t)); |
| 2247 | if (regcomp(ecbdata->diff_words->word_regex, |
| 2248 | o->word_regex, |
| 2249 | REG_EXTENDED | REG_NEWLINE)) |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 2250 | die("invalid regular expression: %s", |
| 2251 | o->word_regex); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2252 | } |
| 2253 | for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) { |
| 2254 | if (o->word_diff == diff_words_styles[i].type) { |
| 2255 | ecbdata->diff_words->style = |
| 2256 | &diff_words_styles[i]; |
| 2257 | break; |
| 2258 | } |
| 2259 | } |
| 2260 | if (want_color(o->use_color)) { |
| 2261 | struct diff_words_style *st = ecbdata->diff_words->style; |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2262 | st->old_word.color = diff_get_color_opt(o, DIFF_FILE_OLD); |
| 2263 | st->new_word.color = diff_get_color_opt(o, DIFF_FILE_NEW); |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 2264 | st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2265 | } |
| 2266 | } |
| 2267 | |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2268 | static void free_diff_words_data(struct emit_callback *ecbdata) |
| 2269 | { |
| 2270 | if (ecbdata->diff_words) { |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2271 | diff_words_flush(ecbdata); |
Ævar Arnfjörð Bjarmason | 77e56d5 | 2022-03-17 15:55:35 +0100 | [diff] [blame] | 2272 | free_emitted_diff_symbols(ecbdata->diff_words->opt->emitted_symbols); |
Thomas Rast | 6440d34 | 2012-03-14 19:24:09 +0100 | [diff] [blame] | 2273 | free (ecbdata->diff_words->opt); |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 2274 | free (ecbdata->diff_words->minus.text.ptr); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2275 | free (ecbdata->diff_words->minus.orig); |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 2276 | free (ecbdata->diff_words->plus.text.ptr); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2277 | free (ecbdata->diff_words->plus.orig); |
Brandon Casey | ef5644e | 2010-09-09 14:02:45 -0500 | [diff] [blame] | 2278 | if (ecbdata->diff_words->word_regex) { |
| 2279 | regfree(ecbdata->diff_words->word_regex); |
| 2280 | free(ecbdata->diff_words->word_regex); |
| 2281 | } |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 2282 | FREE_AND_NULL(ecbdata->diff_words); |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2283 | } |
| 2284 | } |
| 2285 | |
Jeff King | ce43697 | 2006-07-23 05:24:18 -0400 | [diff] [blame] | 2286 | const char *diff_get_color(int diff_use_color, enum color_diff ix) |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2287 | { |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 2288 | if (want_color(diff_use_color)) |
Linus Torvalds | 50f575f | 2006-06-22 13:53:31 -0700 | [diff] [blame] | 2289 | return diff_colors[ix]; |
| 2290 | return ""; |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2291 | } |
| 2292 | |
John Keeping | f192223 | 2013-02-07 20:15:26 +0000 | [diff] [blame] | 2293 | const char *diff_line_prefix(struct diff_options *opt) |
| 2294 | { |
| 2295 | struct strbuf *msgbuf; |
| 2296 | if (!opt->output_prefix) |
| 2297 | return ""; |
| 2298 | |
| 2299 | msgbuf = opt->output_prefix(opt, opt->output_prefix_data); |
| 2300 | return msgbuf->buf; |
| 2301 | } |
| 2302 | |
Jeff King | 19b9046 | 2019-02-14 00:48:27 -0500 | [diff] [blame] | 2303 | static unsigned long sane_truncate_line(char *line, unsigned long len) |
Junio C Hamano | 2370781 | 2008-01-02 01:50:11 -0800 | [diff] [blame] | 2304 | { |
| 2305 | const char *cp; |
| 2306 | unsigned long allot; |
| 2307 | size_t l = len; |
| 2308 | |
Junio C Hamano | 2370781 | 2008-01-02 01:50:11 -0800 | [diff] [blame] | 2309 | cp = line; |
| 2310 | allot = l; |
| 2311 | while (0 < l) { |
| 2312 | (void) utf8_width(&cp, &l); |
| 2313 | if (!cp) |
| 2314 | break; /* truncated in the middle? */ |
| 2315 | } |
| 2316 | return allot - l; |
| 2317 | } |
| 2318 | |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2319 | static void find_lno(const char *line, struct emit_callback *ecbdata) |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 2320 | { |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2321 | const char *p; |
| 2322 | ecbdata->lno_in_preimage = 0; |
| 2323 | ecbdata->lno_in_postimage = 0; |
| 2324 | p = strchr(line, '-'); |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 2325 | if (!p) |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2326 | return; /* cannot happen */ |
| 2327 | ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10); |
| 2328 | p = strchr(p, '+'); |
| 2329 | if (!p) |
| 2330 | return; /* cannot happen */ |
| 2331 | ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10); |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 2332 | } |
| 2333 | |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2334 | static int fn_out_consume(void *priv, char *line, unsigned long len) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2335 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2336 | struct emit_callback *ecbdata = priv; |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 2337 | struct diff_options *o = ecbdata->opt; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2338 | |
Stefan Beller | ba16233 | 2016-09-07 16:36:46 -0700 | [diff] [blame] | 2339 | o->found_changes = 1; |
| 2340 | |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 2341 | if (ecbdata->header) { |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 2342 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 2343 | ecbdata->header->buf, ecbdata->header->len, 0); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 2344 | strbuf_reset(ecbdata->header); |
| 2345 | ecbdata->header = NULL; |
| 2346 | } |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 2347 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2348 | if (ecbdata->label_path[0]) { |
Stefan Beller | 3ee8b7b | 2017-06-29 17:06:56 -0700 | [diff] [blame] | 2349 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS, |
| 2350 | ecbdata->label_path[0], |
| 2351 | strlen(ecbdata->label_path[0]), 0); |
| 2352 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS, |
| 2353 | ecbdata->label_path[1], |
| 2354 | strlen(ecbdata->label_path[1]), 0); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2355 | ecbdata->label_path[0] = ecbdata->label_path[1] = NULL; |
| 2356 | } |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2357 | |
Jim Meyering | a624eaa | 2008-08-15 13:39:26 +0200 | [diff] [blame] | 2358 | if (diff_suppress_blank_empty |
| 2359 | && len == 2 && line[0] == ' ' && line[1] == '\n') { |
| 2360 | line[0] = '\n'; |
| 2361 | len = 1; |
| 2362 | } |
| 2363 | |
Junio C Hamano | b8d9c1a | 2009-09-03 23:59:25 -0700 | [diff] [blame] | 2364 | if (line[0] == '@') { |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2365 | if (ecbdata->diff_words) |
| 2366 | diff_words_flush(ecbdata); |
Jeff King | 19b9046 | 2019-02-14 00:48:27 -0500 | [diff] [blame] | 2367 | len = sane_truncate_line(line, len); |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2368 | find_lno(line, ecbdata); |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 2369 | emit_hunk_header(ecbdata, line, len); |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2370 | return 0; |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2371 | } |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2372 | |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2373 | if (ecbdata->diff_words) { |
Stefan Beller | ff95867 | 2017-06-29 17:06:54 -0700 | [diff] [blame] | 2374 | enum diff_symbol s = |
| 2375 | ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ? |
| 2376 | DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2377 | if (line[0] == '-') { |
| 2378 | diff_words_append(line, len, |
| 2379 | &ecbdata->diff_words->minus); |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2380 | return 0; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2381 | } else if (line[0] == '+') { |
| 2382 | diff_words_append(line, len, |
| 2383 | &ecbdata->diff_words->plus); |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2384 | return 0; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 2385 | } else if (starts_with(line, "\\ ")) { |
Thomas Rast | c7c2bc0 | 2012-01-12 12:15:33 +0100 | [diff] [blame] | 2386 | /* |
| 2387 | * Eat the "no newline at eof" marker as if we |
| 2388 | * saw a "+" or "-" line with nothing on it, |
| 2389 | * and return without diff_words_flush() to |
| 2390 | * defer processing. If this is the end of |
| 2391 | * preimage, more "+" lines may come after it. |
| 2392 | */ |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2393 | return 0; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2394 | } |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2395 | diff_words_flush(ecbdata); |
Stefan Beller | ff95867 | 2017-06-29 17:06:54 -0700 | [diff] [blame] | 2396 | emit_diff_symbol(o, s, line, len, 0); |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2397 | return 0; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2398 | } |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2399 | |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2400 | switch (line[0]) { |
| 2401 | case '+': |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2402 | ecbdata->lno_in_postimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 2403 | emit_add_line(ecbdata, line + 1, len - 1); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2404 | break; |
| 2405 | case '-': |
| 2406 | ecbdata->lno_in_preimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 2407 | emit_del_line(ecbdata, line + 1, len - 1); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2408 | break; |
| 2409 | case ' ': |
| 2410 | ecbdata->lno_in_postimage++; |
| 2411 | ecbdata->lno_in_preimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 2412 | emit_context_line(ecbdata, line + 1, len - 1); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2413 | break; |
| 2414 | default: |
| 2415 | /* incomplete line at the end */ |
| 2416 | ecbdata->lno_in_preimage++; |
Stefan Beller | f2bb121 | 2017-06-29 17:06:55 -0700 | [diff] [blame] | 2417 | emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE, |
| 2418 | line, len, 0); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2419 | break; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2420 | } |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2421 | return 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2422 | } |
| 2423 | |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2424 | static void pprint_rename(struct strbuf *name, const char *a, const char *b) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2425 | { |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2426 | const char *old_name = a; |
| 2427 | const char *new_name = b; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2428 | int pfx_length, sfx_length; |
Thomas Rast | dd281f0 | 2013-02-26 21:47:01 +0100 | [diff] [blame] | 2429 | int pfx_adjust_for_slash; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2430 | int len_a = strlen(a); |
| 2431 | int len_b = strlen(b); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2432 | int a_midlen, b_midlen; |
Alexandre Julliard | e5bfbf9 | 2007-02-10 15:39:00 +0100 | [diff] [blame] | 2433 | int qlen_a = quote_c_style(a, NULL, NULL, 0); |
| 2434 | int qlen_b = quote_c_style(b, NULL, NULL, 0); |
| 2435 | |
| 2436 | if (qlen_a || qlen_b) { |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2437 | quote_c_style(a, name, NULL, 0); |
| 2438 | strbuf_addstr(name, " => "); |
| 2439 | quote_c_style(b, name, NULL, 0); |
| 2440 | return; |
Alexandre Julliard | e5bfbf9 | 2007-02-10 15:39:00 +0100 | [diff] [blame] | 2441 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2442 | |
| 2443 | /* Find common prefix */ |
| 2444 | pfx_length = 0; |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2445 | while (*old_name && *new_name && *old_name == *new_name) { |
| 2446 | if (*old_name == '/') |
| 2447 | pfx_length = old_name - a + 1; |
| 2448 | old_name++; |
| 2449 | new_name++; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | /* Find common suffix */ |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2453 | old_name = a + len_a; |
| 2454 | new_name = b + len_b; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2455 | sfx_length = 0; |
Antoine Pelisse | d020e27 | 2013-02-23 17:48:45 +0100 | [diff] [blame] | 2456 | /* |
Thomas Rast | dd281f0 | 2013-02-26 21:47:01 +0100 | [diff] [blame] | 2457 | * If there is a common prefix, it must end in a slash. In |
| 2458 | * that case we let this loop run 1 into the prefix to see the |
| 2459 | * same slash. |
| 2460 | * |
| 2461 | * If there is no common prefix, we cannot do this as it would |
| 2462 | * underrun the input strings. |
Antoine Pelisse | d020e27 | 2013-02-23 17:48:45 +0100 | [diff] [blame] | 2463 | */ |
Thomas Rast | dd281f0 | 2013-02-26 21:47:01 +0100 | [diff] [blame] | 2464 | pfx_adjust_for_slash = (pfx_length ? 1 : 0); |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2465 | while (a + pfx_length - pfx_adjust_for_slash <= old_name && |
| 2466 | b + pfx_length - pfx_adjust_for_slash <= new_name && |
| 2467 | *old_name == *new_name) { |
| 2468 | if (*old_name == '/') |
| 2469 | sfx_length = len_a - (old_name - a); |
| 2470 | old_name--; |
| 2471 | new_name--; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | /* |
| 2475 | * pfx{mid-a => mid-b}sfx |
| 2476 | * {pfx-a => pfx-b}sfx |
| 2477 | * pfx{sfx-a => sfx-b} |
| 2478 | * name-a => name-b |
| 2479 | */ |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2480 | a_midlen = len_a - pfx_length - sfx_length; |
| 2481 | b_midlen = len_b - pfx_length - sfx_length; |
| 2482 | if (a_midlen < 0) |
| 2483 | a_midlen = 0; |
| 2484 | if (b_midlen < 0) |
| 2485 | b_midlen = 0; |
Junio C Hamano | cc908b8 | 2006-05-14 22:07:28 -0700 | [diff] [blame] | 2486 | |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2487 | strbuf_grow(name, pfx_length + a_midlen + b_midlen + sfx_length + 7); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2488 | if (pfx_length + sfx_length) { |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2489 | strbuf_add(name, a, pfx_length); |
| 2490 | strbuf_addch(name, '{'); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2491 | } |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2492 | strbuf_add(name, a + pfx_length, a_midlen); |
| 2493 | strbuf_addstr(name, " => "); |
| 2494 | strbuf_add(name, b + pfx_length, b_midlen); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2495 | if (pfx_length + sfx_length) { |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2496 | strbuf_addch(name, '}'); |
| 2497 | strbuf_add(name, a + len_a - sfx_length, sfx_length); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2498 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2499 | } |
| 2500 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2501 | static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat, |
| 2502 | const char *name_a, |
| 2503 | const char *name_b) |
| 2504 | { |
| 2505 | struct diffstat_file *x; |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 2506 | CALLOC_ARRAY(x, 1); |
Dmitry S. Dolzhenko | 4c960a4 | 2014-03-04 02:31:53 +0400 | [diff] [blame] | 2507 | ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2508 | diffstat->files[diffstat->nr++] = x; |
| 2509 | if (name_b) { |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2510 | x->from_name = xstrdup(name_a); |
| 2511 | x->name = xstrdup(name_b); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2512 | x->is_renamed = 1; |
| 2513 | } |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2514 | else { |
| 2515 | x->from_name = NULL; |
Shawn Pearce | 9befac4 | 2006-09-02 00:16:31 -0400 | [diff] [blame] | 2516 | x->name = xstrdup(name_a); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2517 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2518 | return x; |
| 2519 | } |
| 2520 | |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2521 | static int diffstat_consume(void *priv, char *line, unsigned long len) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2522 | { |
| 2523 | struct diffstat_t *diffstat = priv; |
| 2524 | struct diffstat_file *x = diffstat->files[diffstat->nr - 1]; |
| 2525 | |
Jeff King | 0e5a87e | 2022-10-17 21:01:17 -0400 | [diff] [blame] | 2526 | if (!len) |
| 2527 | BUG("xdiff fed us an empty line"); |
| 2528 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2529 | if (line[0] == '+') |
| 2530 | x->added++; |
| 2531 | else if (line[0] == '-') |
| 2532 | x->deleted++; |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2533 | return 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2534 | } |
| 2535 | |
Johannes Schindelin | 698ce6f | 2006-05-20 15:40:29 +0200 | [diff] [blame] | 2536 | const char mime_boundary_leader[] = "------------"; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2537 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2538 | static int scale_linear(int it, int width, int max_change) |
| 2539 | { |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2540 | if (!it) |
| 2541 | return 0; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2542 | /* |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2543 | * make sure that at least one '-' or '+' is printed if |
| 2544 | * there is any change to this path. The easiest way is to |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 2545 | * scale linearly as if the allotted width is one column shorter |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2546 | * than it is, and then add 1 to the result. |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2547 | */ |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2548 | return 1 + (it * (width - 1) / max_change); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2549 | } |
| 2550 | |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2551 | static void show_graph(struct strbuf *out, char ch, int cnt, |
| 2552 | const char *set, const char *reset) |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2553 | { |
| 2554 | if (cnt <= 0) |
| 2555 | return; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2556 | strbuf_addstr(out, set); |
| 2557 | strbuf_addchars(out, ch, cnt); |
| 2558 | strbuf_addstr(out, reset); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2559 | } |
| 2560 | |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2561 | static void fill_print_name(struct diffstat_file *file) |
| 2562 | { |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2563 | struct strbuf pname = STRBUF_INIT; |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2564 | |
| 2565 | if (file->print_name) |
| 2566 | return; |
| 2567 | |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2568 | if (file->is_renamed) |
| 2569 | pprint_rename(&pname, file->from_name, file->name); |
| 2570 | else |
| 2571 | quote_c_style(file->name, &pname, NULL, 0); |
| 2572 | |
Nguyễn Thái Ngọc Duy | ddf88fa | 2018-02-24 21:09:59 +0700 | [diff] [blame] | 2573 | if (file->comments) |
| 2574 | strbuf_addf(&pname, " (%s)", file->comments); |
| 2575 | |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2576 | file->print_name = strbuf_detach(&pname, NULL); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2577 | } |
| 2578 | |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2579 | static void print_stat_summary_inserts_deletes(struct diff_options *options, |
| 2580 | int files, int insertions, int deletions) |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2581 | { |
| 2582 | struct strbuf sb = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2583 | |
| 2584 | if (!files) { |
| 2585 | assert(insertions == 0 && deletions == 0); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2586 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES, |
| 2587 | NULL, 0, 0); |
| 2588 | return; |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | strbuf_addf(&sb, |
Nguyễn Thái Ngọc Duy | 218adaa | 2012-09-13 21:16:26 +0700 | [diff] [blame] | 2592 | (files == 1) ? " %d file changed" : " %d files changed", |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2593 | files); |
| 2594 | |
| 2595 | /* |
| 2596 | * For binary diff, the caller may want to print "x files |
| 2597 | * changed" with insertions == 0 && deletions == 0. |
| 2598 | * |
| 2599 | * Not omitting "0 insertions(+), 0 deletions(-)" in this case |
| 2600 | * is probably less confusing (i.e skip over "2 files changed |
| 2601 | * but nothing about added/removed lines? Is this a bug in Git?"). |
| 2602 | */ |
| 2603 | if (insertions || deletions == 0) { |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2604 | strbuf_addf(&sb, |
Nguyễn Thái Ngọc Duy | 218adaa | 2012-09-13 21:16:26 +0700 | [diff] [blame] | 2605 | (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)", |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2606 | insertions); |
| 2607 | } |
| 2608 | |
| 2609 | if (deletions || insertions == 0) { |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2610 | strbuf_addf(&sb, |
Nguyễn Thái Ngọc Duy | 218adaa | 2012-09-13 21:16:26 +0700 | [diff] [blame] | 2611 | (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)", |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2612 | deletions); |
| 2613 | } |
| 2614 | strbuf_addch(&sb, '\n'); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2615 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES, |
| 2616 | sb.buf, sb.len, 0); |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2617 | strbuf_release(&sb); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | void print_stat_summary(FILE *fp, int files, |
| 2621 | int insertions, int deletions) |
| 2622 | { |
| 2623 | struct diff_options o; |
| 2624 | memset(&o, 0, sizeof(o)); |
| 2625 | o.file = fp; |
| 2626 | |
| 2627 | print_stat_summary_inserts_deletes(&o, files, insertions, deletions); |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2628 | } |
| 2629 | |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 2630 | static void show_stats(struct diffstat_t *data, struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2631 | { |
Benjamin Kramer | eb3a9dd | 2009-03-07 21:02:10 +0100 | [diff] [blame] | 2632 | int i, len, add, del, adds = 0, dels = 0; |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2633 | uintmax_t max_change = 0, max_len = 0; |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2634 | int total_files = data->nr, count; |
| 2635 | int width, name_width, graph_width, number_width = 0, bin_width = 0; |
Johannes Schindelin | c0aa335 | 2011-03-22 13:50:08 +0100 | [diff] [blame] | 2636 | const char *reset, *add_c, *del_c; |
Junio C Hamano | e5f85df | 2011-05-27 21:50:39 -0700 | [diff] [blame] | 2637 | int extra_shown = 0; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2638 | const char *line_prefix = diff_line_prefix(options); |
| 2639 | struct strbuf out = STRBUF_INIT; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2640 | |
| 2641 | if (data->nr == 0) |
| 2642 | return; |
| 2643 | |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2644 | count = options->stat_count ? options->stat_count : data->nr; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2645 | |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 2646 | reset = diff_get_color_opt(options, DIFF_RESET); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 2647 | add_c = diff_get_color_opt(options, DIFF_FILE_NEW); |
| 2648 | del_c = diff_get_color_opt(options, DIFF_FILE_OLD); |
Junio C Hamano | 785f743 | 2006-09-26 18:59:41 -0700 | [diff] [blame] | 2649 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2650 | /* |
| 2651 | * Find the longest filename and max number of changes |
| 2652 | */ |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2653 | for (i = 0; (i < count) && (i < data->nr); i++) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2654 | struct diffstat_file *file = data->files[i]; |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2655 | uintmax_t change = file->added + file->deleted; |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2656 | |
| 2657 | if (!file->is_interesting && (change == 0)) { |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2658 | count++; /* not shown == room for one more */ |
Michael J Gruber | 358e460 | 2011-05-27 14:36:40 +0200 | [diff] [blame] | 2659 | continue; |
| 2660 | } |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2661 | fill_print_name(file); |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2662 | len = utf8_strwidth(file->print_name); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2663 | if (max_len < len) |
| 2664 | max_len = len; |
| 2665 | |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2666 | if (file->is_unmerged) { |
| 2667 | /* "Unmerged" is 8 characters */ |
| 2668 | bin_width = bin_width < 8 ? 8 : bin_width; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2669 | continue; |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2670 | } |
| 2671 | if (file->is_binary) { |
| 2672 | /* "Bin XXX -> YYY bytes" */ |
| 2673 | int w = 14 + decimal_width(file->added) |
| 2674 | + decimal_width(file->deleted); |
| 2675 | bin_width = bin_width < w ? w : bin_width; |
| 2676 | /* Display change counts aligned with "Bin" */ |
| 2677 | number_width = 3; |
| 2678 | continue; |
| 2679 | } |
| 2680 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2681 | if (max_change < change) |
| 2682 | max_change = change; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2683 | } |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2684 | count = i; /* where we can stop scanning in data->files[] */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2685 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2686 | /* |
| 2687 | * We have width = stat_width or term_columns() columns total. |
| 2688 | * We want a maximum of min(max_len, stat_name_width) for the name part. |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 2689 | * We want a maximum of min(max_change, stat_graph_width) for the +- part. |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2690 | * We also need 1 for " " and 4 + decimal_width(max_change) |
| 2691 | * for " | NNNN " and one the empty column at the end, altogether |
| 2692 | * 6 + decimal_width(max_change). |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2693 | * |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2694 | * If there's not enough space, we will use the smaller of |
| 2695 | * stat_name_width (if set) and 5/8*width for the filename, |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 2696 | * and the rest for constant elements + graph part, but no more |
| 2697 | * than stat_graph_width for the graph part. |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2698 | * (5/8 gives 50 for filename and 30 for the constant parts + graph |
| 2699 | * for the standard terminal size). |
| 2700 | * |
| 2701 | * In other words: stat_width limits the maximum width, and |
| 2702 | * stat_name_width fixes the maximum width of the filename, |
| 2703 | * and is also used to divide available columns if there |
| 2704 | * aren't enough. |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2705 | * |
| 2706 | * Binary files are displayed with "Bin XXX -> YYY bytes" |
| 2707 | * instead of the change count and graph. This part is treated |
| 2708 | * similarly to the graph part, except that it is not |
Stefano Lattarini | 41ccfdd | 2013-04-12 00:36:10 +0200 | [diff] [blame] | 2709 | * "scaled". If total width is too small to accommodate the |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2710 | * guaranteed minimum width of the filename part and the |
| 2711 | * separators and this message, this message will "overflow" |
| 2712 | * making the line longer than the maximum width. |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2713 | */ |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2714 | |
Junio C Hamano | ce8529b | 2022-10-21 14:53:25 -0700 | [diff] [blame] | 2715 | /* |
| 2716 | * NEEDSWORK: line_prefix is often used for "log --graph" output |
| 2717 | * and contains ANSI-colored string. utf8_strnwidth() should be |
| 2718 | * used to correctly count the display width instead of strlen(). |
| 2719 | */ |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2720 | if (options->stat_width == -1) |
Junio C Hamano | cd48dad | 2016-08-31 16:27:19 -0700 | [diff] [blame] | 2721 | width = term_columns() - strlen(line_prefix); |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2722 | else |
| 2723 | width = options->stat_width ? options->stat_width : 80; |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2724 | number_width = decimal_width(max_change) > number_width ? |
| 2725 | decimal_width(max_change) : number_width; |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2726 | |
Dragan Simic | bd48adc | 2023-09-11 17:39:44 +0200 | [diff] [blame] | 2727 | if (options->stat_name_width == -1) |
| 2728 | options->stat_name_width = diff_stat_name_width; |
Zbigniew Jędrzejewski-Szmek | df44483 | 2012-03-01 13:26:46 +0100 | [diff] [blame] | 2729 | if (options->stat_graph_width == -1) |
| 2730 | options->stat_graph_width = diff_stat_graph_width; |
| 2731 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2732 | /* |
Dragan Simic | bd48adc | 2023-09-11 17:39:44 +0200 | [diff] [blame] | 2733 | * Guarantee 3/8*16 == 6 for the graph part |
| 2734 | * and 5/8*16 == 10 for the filename part |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2735 | */ |
| 2736 | if (width < 16 + 6 + number_width) |
| 2737 | width = 16 + 6 + number_width; |
| 2738 | |
| 2739 | /* |
| 2740 | * First assign sizes that are wanted, ignoring available width. |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2741 | * strlen("Bin XXX -> YYY bytes") == bin_width, and the part |
| 2742 | * starting from "XXX" should fit in graph_width. |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2743 | */ |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2744 | graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4; |
| 2745 | if (options->stat_graph_width && |
| 2746 | options->stat_graph_width < graph_width) |
| 2747 | graph_width = options->stat_graph_width; |
| 2748 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2749 | name_width = (options->stat_name_width > 0 && |
| 2750 | options->stat_name_width < max_len) ? |
| 2751 | options->stat_name_width : max_len; |
| 2752 | |
| 2753 | /* |
| 2754 | * Adjust adjustable widths not to exceed maximum width |
| 2755 | */ |
| 2756 | if (name_width + number_width + 6 + graph_width > width) { |
Lucian Poston | 678c574 | 2012-04-18 14:12:18 -0700 | [diff] [blame] | 2757 | if (graph_width > width * 3/8 - number_width - 6) { |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2758 | graph_width = width * 3/8 - number_width - 6; |
Lucian Poston | 678c574 | 2012-04-18 14:12:18 -0700 | [diff] [blame] | 2759 | if (graph_width < 6) |
| 2760 | graph_width = 6; |
| 2761 | } |
| 2762 | |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 2763 | if (options->stat_graph_width && |
| 2764 | graph_width > options->stat_graph_width) |
| 2765 | graph_width = options->stat_graph_width; |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2766 | if (name_width > width - number_width - 6 - graph_width) |
| 2767 | name_width = width - number_width - 6 - graph_width; |
| 2768 | else |
| 2769 | graph_width = width - number_width - 6 - name_width; |
| 2770 | } |
| 2771 | |
| 2772 | /* |
| 2773 | * From here name_width is the width of the name area, |
| 2774 | * and graph_width is the width of the graph area. |
| 2775 | * max_change is used to scale graph properly. |
| 2776 | */ |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2777 | for (i = 0; i < count; i++) { |
Timo Hirvonen | d2543b8 | 2006-06-24 20:20:32 +0300 | [diff] [blame] | 2778 | const char *prefix = ""; |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2779 | struct diffstat_file *file = data->files[i]; |
| 2780 | char *name = file->print_name; |
| 2781 | uintmax_t added = file->added; |
| 2782 | uintmax_t deleted = file->deleted; |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2783 | int name_len, padding; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2784 | |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2785 | if (!file->is_interesting && (added + deleted == 0)) |
Michael J Gruber | 358e460 | 2011-05-27 14:36:40 +0200 | [diff] [blame] | 2786 | continue; |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2787 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2788 | /* |
| 2789 | * "scale" the filename |
| 2790 | */ |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2791 | len = name_width; |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2792 | name_len = utf8_strwidth(name); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2793 | if (name_width < name_len) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2794 | char *slash; |
| 2795 | prefix = "..."; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2796 | len -= 3; |
Junio C Hamano | ce8529b | 2022-10-21 14:53:25 -0700 | [diff] [blame] | 2797 | /* |
| 2798 | * NEEDSWORK: (name_len - len) counts the display |
| 2799 | * width, which would be shorter than the byte |
| 2800 | * length of the corresponding substring. |
| 2801 | * Advancing "name" by that number of bytes does |
| 2802 | * *NOT* skip over that many columns, so it is |
| 2803 | * very likely that chomping the pathname at the |
| 2804 | * slash we will find starting from "name" will |
| 2805 | * leave the resulting string still too long. |
| 2806 | */ |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2807 | name += name_len - len; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2808 | slash = strchr(name, '/'); |
| 2809 | if (slash) |
| 2810 | name = slash; |
| 2811 | } |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2812 | padding = len - utf8_strwidth(name); |
| 2813 | if (padding < 0) |
| 2814 | padding = 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2815 | |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2816 | if (file->is_binary) { |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2817 | strbuf_addf(&out, " %s%s%*s | %*s", |
| 2818 | prefix, name, padding, "", |
| 2819 | number_width, "Bin"); |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2820 | if (!added && !deleted) { |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2821 | strbuf_addch(&out, '\n'); |
| 2822 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 2823 | out.buf, out.len, 0); |
| 2824 | strbuf_reset(&out); |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2825 | continue; |
| 2826 | } |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2827 | strbuf_addf(&out, " %s%"PRIuMAX"%s", |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2828 | del_c, deleted, reset); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2829 | strbuf_addstr(&out, " -> "); |
| 2830 | strbuf_addf(&out, "%s%"PRIuMAX"%s", |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2831 | add_c, added, reset); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2832 | strbuf_addstr(&out, " bytes\n"); |
| 2833 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 2834 | out.buf, out.len, 0); |
| 2835 | strbuf_reset(&out); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2836 | continue; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2837 | } |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2838 | else if (file->is_unmerged) { |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2839 | strbuf_addf(&out, " %s%s%*s | %*s", |
| 2840 | prefix, name, padding, "", |
Peter Grayson | 209d9cb | 2022-12-14 12:41:51 -0500 | [diff] [blame] | 2841 | number_width, "Unmerged\n"); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2842 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 2843 | out.buf, out.len, 0); |
| 2844 | strbuf_reset(&out); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2845 | continue; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2846 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2847 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2848 | /* |
| 2849 | * scale the add/delete |
| 2850 | */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2851 | add = added; |
| 2852 | del = deleted; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2853 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2854 | if (graph_width <= max_change) { |
Stefan Beller | d3c9cf3 | 2013-07-14 23:35:49 +0200 | [diff] [blame] | 2855 | int total = scale_linear(add + del, graph_width, max_change); |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2856 | if (total < 2 && add && del) |
| 2857 | /* width >= 2 due to the sanity check */ |
| 2858 | total = 2; |
| 2859 | if (add < del) { |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2860 | add = scale_linear(add, graph_width, max_change); |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2861 | del = total - add; |
| 2862 | } else { |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2863 | del = scale_linear(del, graph_width, max_change); |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2864 | add = total - del; |
| 2865 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2866 | } |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2867 | strbuf_addf(&out, " %s%s%*s | %*"PRIuMAX"%s", |
| 2868 | prefix, name, padding, "", |
| 2869 | number_width, added + deleted, |
| 2870 | added + deleted ? " " : ""); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2871 | show_graph(&out, '+', add, add_c, reset); |
| 2872 | show_graph(&out, '-', del, del_c, reset); |
| 2873 | strbuf_addch(&out, '\n'); |
| 2874 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 2875 | out.buf, out.len, 0); |
| 2876 | strbuf_reset(&out); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2877 | } |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2878 | |
| 2879 | for (i = 0; i < data->nr; i++) { |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2880 | struct diffstat_file *file = data->files[i]; |
| 2881 | uintmax_t added = file->added; |
| 2882 | uintmax_t deleted = file->deleted; |
Junio C Hamano | 82dfc2c | 2012-11-27 12:05:10 -0800 | [diff] [blame] | 2883 | |
| 2884 | if (file->is_unmerged || |
| 2885 | (!file->is_interesting && (added + deleted == 0))) { |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2886 | total_files--; |
| 2887 | continue; |
| 2888 | } |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2889 | |
Junio C Hamano | 82dfc2c | 2012-11-27 12:05:10 -0800 | [diff] [blame] | 2890 | if (!file->is_binary) { |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2891 | adds += added; |
| 2892 | dels += deleted; |
| 2893 | } |
| 2894 | if (i < count) |
| 2895 | continue; |
Junio C Hamano | e5f85df | 2011-05-27 21:50:39 -0700 | [diff] [blame] | 2896 | if (!extra_shown) |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2897 | emit_diff_symbol(options, |
| 2898 | DIFF_SYMBOL_STATS_SUMMARY_ABBREV, |
| 2899 | NULL, 0, 0); |
Junio C Hamano | e5f85df | 2011-05-27 21:50:39 -0700 | [diff] [blame] | 2900 | extra_shown = 1; |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2901 | } |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2902 | |
| 2903 | print_stat_summary_inserts_deletes(options, total_files, adds, dels); |
Rene Scharfe | 5a61201 | 2017-08-30 19:49:43 +0200 | [diff] [blame] | 2904 | strbuf_release(&out); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2905 | } |
| 2906 | |
Felipe Contreras | 2775d92 | 2009-10-11 23:46:11 +0300 | [diff] [blame] | 2907 | static void show_shortstats(struct diffstat_t *data, struct diff_options *options) |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2908 | { |
| 2909 | int i, adds = 0, dels = 0, total_files = data->nr; |
| 2910 | |
| 2911 | if (data->nr == 0) |
| 2912 | return; |
| 2913 | |
| 2914 | for (i = 0; i < data->nr; i++) { |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2915 | int added = data->files[i]->added; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2916 | int deleted = data->files[i]->deleted; |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2917 | |
Junio C Hamano | 20c8cde | 2012-11-27 14:19:36 -0800 | [diff] [blame] | 2918 | if (data->files[i]->is_unmerged || |
| 2919 | (!data->files[i]->is_interesting && (added + deleted == 0))) { |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2920 | total_files--; |
Alexander Strasser | de9658b | 2012-06-15 23:50:30 +0200 | [diff] [blame] | 2921 | } else if (!data->files[i]->is_binary) { /* don't count bytes */ |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2922 | adds += added; |
| 2923 | dels += deleted; |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2924 | } |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2925 | } |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2926 | print_stat_summary_inserts_deletes(options, total_files, adds, dels); |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2927 | } |
| 2928 | |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 2929 | static void show_numstat(struct diffstat_t *data, struct diff_options *options) |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 2930 | { |
| 2931 | int i; |
| 2932 | |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2933 | if (data->nr == 0) |
| 2934 | return; |
| 2935 | |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 2936 | for (i = 0; i < data->nr; i++) { |
| 2937 | struct diffstat_file *file = data->files[i]; |
| 2938 | |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2939 | fprintf(options->file, "%s", diff_line_prefix(options)); |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 2940 | |
Junio C Hamano | bfddbc5 | 2006-12-10 13:50:59 -0800 | [diff] [blame] | 2941 | if (file->is_binary) |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2942 | fprintf(options->file, "-\t-\t"); |
Junio C Hamano | bfddbc5 | 2006-12-10 13:50:59 -0800 | [diff] [blame] | 2943 | else |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2944 | fprintf(options->file, |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2945 | "%"PRIuMAX"\t%"PRIuMAX"\t", |
| 2946 | file->added, file->deleted); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2947 | if (options->line_termination) { |
| 2948 | fill_print_name(file); |
| 2949 | if (!file->is_renamed) |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2950 | write_name_quoted(file->name, options->file, |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2951 | options->line_termination); |
| 2952 | else { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2953 | fputs(file->print_name, options->file); |
| 2954 | putc(options->line_termination, options->file); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2955 | } |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2956 | } else { |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2957 | if (file->is_renamed) { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2958 | putc('\0', options->file); |
| 2959 | write_name_quoted(file->from_name, options->file, '\0'); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2960 | } |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2961 | write_name_quoted(file->name, options->file, '\0'); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2962 | } |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 2963 | } |
| 2964 | } |
| 2965 | |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 2966 | struct dirstat_file { |
| 2967 | const char *name; |
| 2968 | unsigned long changed; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2969 | }; |
| 2970 | |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 2971 | struct dirstat_dir { |
| 2972 | struct dirstat_file *files; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 2973 | int alloc, nr, permille, cumulative; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 2974 | }; |
| 2975 | |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 2976 | static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir, |
| 2977 | unsigned long changed, const char *base, int baselen) |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2978 | { |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 2979 | unsigned long sum_changes = 0; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2980 | unsigned int sources = 0; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2981 | const char *line_prefix = diff_line_prefix(opt); |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2982 | |
| 2983 | while (dir->nr) { |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 2984 | struct dirstat_file *f = dir->files; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2985 | int namelen = strlen(f->name); |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 2986 | unsigned long changes; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2987 | char *slash; |
| 2988 | |
| 2989 | if (namelen < baselen) |
| 2990 | break; |
| 2991 | if (memcmp(f->name, base, baselen)) |
| 2992 | break; |
| 2993 | slash = strchr(f->name + baselen, '/'); |
| 2994 | if (slash) { |
| 2995 | int newbaselen = slash + 1 - f->name; |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 2996 | changes = gather_dirstat(opt, dir, changed, f->name, newbaselen); |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2997 | sources++; |
| 2998 | } else { |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 2999 | changes = f->changed; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3000 | dir->files++; |
| 3001 | dir->nr--; |
| 3002 | sources += 2; |
| 3003 | } |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 3004 | sum_changes += changes; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3005 | } |
| 3006 | |
| 3007 | /* |
| 3008 | * We don't report dirstat's for |
| 3009 | * - the top level |
| 3010 | * - or cases where everything came from a single directory |
| 3011 | * under this directory (sources == 1). |
| 3012 | */ |
| 3013 | if (baselen && sources != 1) { |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 3014 | if (sum_changes) { |
| 3015 | int permille = sum_changes * 1000 / changed; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 3016 | if (permille >= dir->permille) { |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 3017 | fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix, |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 3018 | permille / 10, permille % 10, baselen, base); |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3019 | if (!dir->cumulative) |
| 3020 | return 0; |
| 3021 | } |
| 3022 | } |
| 3023 | } |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 3024 | return sum_changes; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3025 | } |
| 3026 | |
Linus Torvalds | 441bca0 | 2008-08-28 16:19:08 -0700 | [diff] [blame] | 3027 | static int dirstat_compare(const void *_a, const void *_b) |
| 3028 | { |
| 3029 | const struct dirstat_file *a = _a; |
| 3030 | const struct dirstat_file *b = _b; |
| 3031 | return strcmp(a->name, b->name); |
| 3032 | } |
| 3033 | |
Junio C Hamano | 34a9489 | 2023-05-05 14:19:16 -0700 | [diff] [blame] | 3034 | static void conclude_dirstat(struct diff_options *options, |
| 3035 | struct dirstat_dir *dir, |
| 3036 | unsigned long changed) |
| 3037 | { |
Junio C Hamano | 8397398 | 2023-05-05 14:19:17 -0700 | [diff] [blame] | 3038 | struct dirstat_file *to_free = dir->files; |
Junio C Hamano | 34a9489 | 2023-05-05 14:19:16 -0700 | [diff] [blame] | 3039 | |
Junio C Hamano | 8397398 | 2023-05-05 14:19:17 -0700 | [diff] [blame] | 3040 | if (!changed) { |
| 3041 | /* This can happen even with many files, if everything was renames */ |
| 3042 | ; |
| 3043 | } else { |
| 3044 | /* Show all directories with more than x% of the changes */ |
| 3045 | QSORT(dir->files, dir->nr, dirstat_compare); |
| 3046 | gather_dirstat(options, dir, changed, "", 0); |
| 3047 | } |
| 3048 | |
| 3049 | free(to_free); |
Junio C Hamano | 34a9489 | 2023-05-05 14:19:16 -0700 | [diff] [blame] | 3050 | } |
| 3051 | |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3052 | static void show_dirstat(struct diff_options *options) |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3053 | { |
| 3054 | int i; |
| 3055 | unsigned long changed; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3056 | struct dirstat_dir dir; |
| 3057 | struct diff_queue_struct *q = &diff_queued_diff; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3058 | |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3059 | dir.files = NULL; |
| 3060 | dir.alloc = 0; |
| 3061 | dir.nr = 0; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 3062 | dir.permille = options->dirstat_permille; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3063 | dir.cumulative = options->flags.dirstat_cumulative; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3064 | |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3065 | changed = 0; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3066 | for (i = 0; i < q->nr; i++) { |
| 3067 | struct diff_filepair *p = q->queue[i]; |
| 3068 | const char *name; |
| 3069 | unsigned long copied, added, damage; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3070 | struct diff_populate_filespec_options dpf_options = { |
| 3071 | .check_size_only = 1, |
| 3072 | }; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3073 | |
Johan Herland | 2ca8671 | 2011-04-12 11:24:34 +0200 | [diff] [blame] | 3074 | name = p->two->path ? p->two->path : p->one->path; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3075 | |
Jeff King | d9f62df | 2018-08-28 17:23:03 -0400 | [diff] [blame] | 3076 | if (p->one->oid_valid && p->two->oid_valid && |
| 3077 | oideq(&p->one->oid, &p->two->oid)) { |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3078 | /* |
| 3079 | * The SHA1 has not changed, so pre-/post-content is |
| 3080 | * identical. We can therefore skip looking at the |
| 3081 | * file contents altogether. |
| 3082 | */ |
| 3083 | damage = 0; |
| 3084 | goto found_damage; |
| 3085 | } |
| 3086 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3087 | if (options->flags.dirstat_by_file) { |
Johan Herland | 0133dab | 2011-04-11 00:48:51 +0200 | [diff] [blame] | 3088 | /* |
| 3089 | * In --dirstat-by-file mode, we don't really need to |
| 3090 | * look at the actual file contents at all. |
| 3091 | * The fact that the SHA1 changed is enough for us to |
| 3092 | * add this file to the list of results |
| 3093 | * (with each file contributing equal damage). |
| 3094 | */ |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3095 | damage = 1; |
Johan Herland | 0133dab | 2011-04-11 00:48:51 +0200 | [diff] [blame] | 3096 | goto found_damage; |
| 3097 | } |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3098 | |
| 3099 | if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3100 | diff_populate_filespec(options->repo, p->one, NULL); |
| 3101 | diff_populate_filespec(options->repo, p->two, NULL); |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3102 | diffcore_count_changes(options->repo, |
| 3103 | p->one, p->two, NULL, NULL, |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3104 | &copied, &added); |
| 3105 | diff_free_filespec_data(p->one); |
| 3106 | diff_free_filespec_data(p->two); |
| 3107 | } else if (DIFF_FILE_VALID(p->one)) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3108 | diff_populate_filespec(options->repo, p->one, &dpf_options); |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3109 | copied = added = 0; |
| 3110 | diff_free_filespec_data(p->one); |
| 3111 | } else if (DIFF_FILE_VALID(p->two)) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3112 | diff_populate_filespec(options->repo, p->two, &dpf_options); |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3113 | copied = 0; |
| 3114 | added = p->two->size; |
| 3115 | diff_free_filespec_data(p->two); |
| 3116 | } else |
Junio C Hamano | 2b0b551 | 2008-02-24 17:37:15 -0800 | [diff] [blame] | 3117 | continue; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3118 | |
| 3119 | /* |
| 3120 | * Original minus copied is the removed material, |
| 3121 | * added is the new material. They are both damages |
Johan Herland | 0133dab | 2011-04-11 00:48:51 +0200 | [diff] [blame] | 3122 | * made to the preimage. |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3123 | * If the resulting damage is zero, we know that |
| 3124 | * diffcore_count_changes() considers the two entries to |
Jeff King | d9f62df | 2018-08-28 17:23:03 -0400 | [diff] [blame] | 3125 | * be identical, but since the oid changed, we |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3126 | * know that there must have been _some_ kind of change, |
| 3127 | * so we force all entries to have damage > 0. |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3128 | */ |
| 3129 | damage = (p->one->size - copied) + added; |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3130 | if (!damage) |
Heikki Orsila | fd33777 | 2008-09-05 22:27:35 +0300 | [diff] [blame] | 3131 | damage = 1; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3132 | |
Johan Herland | 0133dab | 2011-04-11 00:48:51 +0200 | [diff] [blame] | 3133 | found_damage: |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3134 | ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc); |
| 3135 | dir.files[dir.nr].name = name; |
| 3136 | dir.files[dir.nr].changed = damage; |
| 3137 | changed += damage; |
| 3138 | dir.nr++; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3139 | } |
| 3140 | |
Junio C Hamano | 34a9489 | 2023-05-05 14:19:16 -0700 | [diff] [blame] | 3141 | conclude_dirstat(options, &dir, changed); |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3142 | } |
| 3143 | |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 3144 | static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options) |
| 3145 | { |
| 3146 | int i; |
| 3147 | unsigned long changed; |
| 3148 | struct dirstat_dir dir; |
| 3149 | |
| 3150 | if (data->nr == 0) |
| 3151 | return; |
| 3152 | |
| 3153 | dir.files = NULL; |
| 3154 | dir.alloc = 0; |
| 3155 | dir.nr = 0; |
| 3156 | dir.permille = options->dirstat_permille; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3157 | dir.cumulative = options->flags.dirstat_cumulative; |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 3158 | |
| 3159 | changed = 0; |
| 3160 | for (i = 0; i < data->nr; i++) { |
| 3161 | struct diffstat_file *file = data->files[i]; |
| 3162 | unsigned long damage = file->added + file->deleted; |
| 3163 | if (file->is_binary) |
| 3164 | /* |
| 3165 | * binary files counts bytes, not lines. Must find some |
| 3166 | * way to normalize binary bytes vs. textual lines. |
| 3167 | * The following heuristic assumes that there are 64 |
| 3168 | * bytes per "line". |
| 3169 | * This is stupid and ugly, but very cheap... |
| 3170 | */ |
René Scharfe | 42c78a2 | 2017-07-08 12:35:35 +0200 | [diff] [blame] | 3171 | damage = DIV_ROUND_UP(damage, 64); |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 3172 | ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc); |
| 3173 | dir.files[dir.nr].name = file->name; |
| 3174 | dir.files[dir.nr].changed = damage; |
| 3175 | changed += damage; |
| 3176 | dir.nr++; |
| 3177 | } |
| 3178 | |
Junio C Hamano | 34a9489 | 2023-05-05 14:19:16 -0700 | [diff] [blame] | 3179 | conclude_dirstat(options, &dir, changed); |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 3180 | } |
| 3181 | |
Matthew Rogers | 1cf3d5d | 2020-08-20 00:41:32 +0000 | [diff] [blame] | 3182 | static void free_diffstat_file(struct diffstat_file *f) |
| 3183 | { |
| 3184 | free(f->print_name); |
| 3185 | free(f->name); |
| 3186 | free(f->from_name); |
| 3187 | free(f); |
| 3188 | } |
| 3189 | |
Daniel Ferreira | e4cb659 | 2019-11-13 12:40:58 +0000 | [diff] [blame] | 3190 | void free_diffstat_info(struct diffstat_t *diffstat) |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 3191 | { |
| 3192 | int i; |
Matthew Rogers | 1cf3d5d | 2020-08-20 00:41:32 +0000 | [diff] [blame] | 3193 | for (i = 0; i < diffstat->nr; i++) |
| 3194 | free_diffstat_file(diffstat->files[i]); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 3195 | free(diffstat->files); |
| 3196 | } |
| 3197 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3198 | struct checkdiff_t { |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3199 | const char *filename; |
Junio C Hamano | 1ba111d | 2008-06-26 15:36:34 -0700 | [diff] [blame] | 3200 | int lineno; |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3201 | int conflict_marker_size; |
Junio C Hamano | 1ba111d | 2008-06-26 15:36:34 -0700 | [diff] [blame] | 3202 | struct diff_options *o; |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 3203 | unsigned ws_rule; |
Wincent Colaiuta | 62c6489 | 2007-12-13 21:24:52 +0100 | [diff] [blame] | 3204 | unsigned status; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3205 | }; |
| 3206 | |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3207 | static int is_conflict_marker(const char *line, int marker_size, unsigned long len) |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3208 | { |
| 3209 | char firstchar; |
| 3210 | int cnt; |
| 3211 | |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3212 | if (len < marker_size + 1) |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3213 | return 0; |
| 3214 | firstchar = line[0]; |
| 3215 | switch (firstchar) { |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3216 | case '=': case '>': case '<': case '|': |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3217 | break; |
| 3218 | default: |
| 3219 | return 0; |
| 3220 | } |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3221 | for (cnt = 1; cnt < marker_size; cnt++) |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3222 | if (line[cnt] != firstchar) |
| 3223 | return 0; |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 3224 | /* line[1] through line[marker_size-1] are same as firstchar */ |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3225 | if (len < marker_size + 1 || !isspace(line[marker_size])) |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3226 | return 0; |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3227 | return 1; |
| 3228 | } |
| 3229 | |
Jeff King | 75ab763 | 2018-11-02 02:39:03 -0400 | [diff] [blame] | 3230 | static void checkdiff_consume_hunk(void *priv, |
Jeff King | 61bdc7c | 2022-12-13 06:13:48 -0500 | [diff] [blame] | 3231 | long ob UNUSED, long on UNUSED, |
| 3232 | long nb, long nn UNUSED, |
| 3233 | const char *func UNUSED, long funclen UNUSED) |
Jeff King | 75ab763 | 2018-11-02 02:39:03 -0400 | [diff] [blame] | 3234 | |
| 3235 | { |
| 3236 | struct checkdiff_t *data = priv; |
| 3237 | data->lineno = nb - 1; |
| 3238 | } |
| 3239 | |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 3240 | static int checkdiff_consume(void *priv, char *line, unsigned long len) |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3241 | { |
| 3242 | struct checkdiff_t *data = priv; |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3243 | int marker_size = data->conflict_marker_size; |
Jeff King | f1c9626 | 2011-08-17 22:03:12 -0700 | [diff] [blame] | 3244 | const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE); |
| 3245 | const char *reset = diff_get_color(data->o->use_color, DIFF_RESET); |
| 3246 | const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 3247 | char *err; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 3248 | const char *line_prefix; |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 3249 | |
| 3250 | assert(data->o); |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 3251 | line_prefix = diff_line_prefix(data->o); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3252 | |
| 3253 | if (line[0] == '+') { |
Junio C Hamano | 18374e5 | 2008-06-26 13:16:33 -0700 | [diff] [blame] | 3254 | unsigned bad; |
Junio C Hamano | 0ef617f | 2008-02-15 20:30:05 -0800 | [diff] [blame] | 3255 | data->lineno++; |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3256 | if (is_conflict_marker(line + 1, marker_size, len - 1)) { |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3257 | data->status |= 1; |
| 3258 | fprintf(data->o->file, |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 3259 | "%s%s:%d: leftover conflict marker\n", |
| 3260 | line_prefix, data->filename, data->lineno); |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3261 | } |
Junio C Hamano | 8f8841e | 2008-06-26 15:35:21 -0700 | [diff] [blame] | 3262 | bad = ws_check(line + 1, len - 1, data->ws_rule); |
Junio C Hamano | 18374e5 | 2008-06-26 13:16:33 -0700 | [diff] [blame] | 3263 | if (!bad) |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 3264 | return 0; |
Junio C Hamano | 18374e5 | 2008-06-26 13:16:33 -0700 | [diff] [blame] | 3265 | data->status |= bad; |
| 3266 | err = whitespace_error_string(bad); |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 3267 | fprintf(data->o->file, "%s%s:%d: %s.\n", |
| 3268 | line_prefix, data->filename, data->lineno, err); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 3269 | free(err); |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 3270 | emit_line(data->o, set, reset, line, 1); |
Junio C Hamano | 8f8841e | 2008-06-26 15:35:21 -0700 | [diff] [blame] | 3271 | ws_check_emit(line + 1, len - 1, data->ws_rule, |
Junio C Hamano | 1ba111d | 2008-06-26 15:36:34 -0700 | [diff] [blame] | 3272 | data->o->file, set, reset, ws); |
Junio C Hamano | 877f23c | 2008-06-26 15:36:59 -0700 | [diff] [blame] | 3273 | } else if (line[0] == ' ') { |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3274 | data->lineno++; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3275 | } |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 3276 | return 0; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3277 | } |
| 3278 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3279 | static unsigned char *deflate_it(char *data, |
| 3280 | unsigned long size, |
| 3281 | unsigned long *result_size) |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3282 | { |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3283 | int bound; |
| 3284 | unsigned char *deflated; |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 3285 | git_zstream stream; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3286 | |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 3287 | git_deflate_init(&stream, zlib_compression_level); |
Junio C Hamano | 225a6f1 | 2011-06-10 11:18:17 -0700 | [diff] [blame] | 3288 | bound = git_deflate_bound(&stream, size); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3289 | deflated = xmalloc(bound); |
| 3290 | stream.next_out = deflated; |
| 3291 | stream.avail_out = bound; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3292 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3293 | stream.next_in = (unsigned char *)data; |
| 3294 | stream.avail_in = size; |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 3295 | while (git_deflate(&stream, Z_FINISH) == Z_OK) |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3296 | ; /* nothing */ |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 3297 | git_deflate_end(&stream); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3298 | *result_size = stream.total_out; |
| 3299 | return deflated; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3300 | } |
| 3301 | |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3302 | static void emit_binary_diff_body(struct diff_options *o, |
| 3303 | mmfile_t *one, mmfile_t *two) |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3304 | { |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3305 | void *cp; |
| 3306 | void *delta; |
| 3307 | void *deflated; |
| 3308 | void *data; |
| 3309 | unsigned long orig_size; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3310 | unsigned long delta_size; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3311 | unsigned long deflate_size; |
| 3312 | unsigned long data_size; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3313 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3314 | /* We could do deflated delta, or we could do just deflated two, |
| 3315 | * whichever is smaller. |
| 3316 | */ |
| 3317 | delta = NULL; |
| 3318 | deflated = deflate_it(two->ptr, two->size, &deflate_size); |
| 3319 | if (one->size && two->size) { |
| 3320 | delta = diff_delta(one->ptr, one->size, |
| 3321 | two->ptr, two->size, |
| 3322 | &delta_size, deflate_size); |
| 3323 | if (delta) { |
| 3324 | void *to_free = delta; |
| 3325 | orig_size = delta_size; |
| 3326 | delta = deflate_it(delta, delta_size, &delta_size); |
| 3327 | free(to_free); |
| 3328 | } |
| 3329 | } |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3330 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3331 | if (delta && delta_size < deflate_size) { |
Torsten Bögershausen | ca473ce | 2018-11-11 08:05:04 +0100 | [diff] [blame] | 3332 | char *s = xstrfmt("%"PRIuMAX , (uintmax_t)orig_size); |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3333 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA, |
| 3334 | s, strlen(s), 0); |
| 3335 | free(s); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3336 | free(deflated); |
| 3337 | data = delta; |
| 3338 | data_size = delta_size; |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3339 | } else { |
| 3340 | char *s = xstrfmt("%lu", two->size); |
| 3341 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL, |
| 3342 | s, strlen(s), 0); |
| 3343 | free(s); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3344 | free(delta); |
| 3345 | data = deflated; |
| 3346 | data_size = deflate_size; |
| 3347 | } |
| 3348 | |
| 3349 | /* emit data encoded in base85 */ |
| 3350 | cp = data; |
| 3351 | while (data_size) { |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3352 | int len; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3353 | int bytes = (52 < data_size) ? 52 : data_size; |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3354 | char line[71]; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3355 | data_size -= bytes; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3356 | if (bytes <= 26) |
| 3357 | line[0] = bytes + 'A' - 1; |
| 3358 | else |
| 3359 | line[0] = bytes - 26 + 'a' - 1; |
| 3360 | encode_85(line + 1, cp, bytes); |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 3361 | cp = (char *) cp + bytes; |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3362 | |
| 3363 | len = strlen(line); |
| 3364 | line[len++] = '\n'; |
| 3365 | line[len] = '\0'; |
| 3366 | |
| 3367 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY, |
| 3368 | line, len, 0); |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3369 | } |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3370 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3371 | free(data); |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3372 | } |
| 3373 | |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3374 | static void emit_binary_diff(struct diff_options *o, |
| 3375 | mmfile_t *one, mmfile_t *two) |
Junio C Hamano | d4c452f | 2006-08-16 16:08:14 -0700 | [diff] [blame] | 3376 | { |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3377 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0); |
| 3378 | emit_binary_diff_body(o, one, two); |
| 3379 | emit_binary_diff_body(o, two, one); |
Junio C Hamano | d4c452f | 2006-08-16 16:08:14 -0700 | [diff] [blame] | 3380 | } |
| 3381 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3382 | int diff_filespec_is_binary(struct repository *r, |
| 3383 | struct diff_filespec *one) |
Junio C Hamano | 29a3eef | 2007-07-06 00:18:54 -0700 | [diff] [blame] | 3384 | { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3385 | struct diff_populate_filespec_options dpf_options = { |
| 3386 | .check_binary = 1, |
| 3387 | }; |
| 3388 | |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3389 | if (one->is_binary == -1) { |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3390 | diff_filespec_load_driver(one, r->index); |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3391 | if (one->driver->binary != -1) |
| 3392 | one->is_binary = one->driver->binary; |
| 3393 | else { |
| 3394 | if (!one->data && DIFF_FILE_VALID(one)) |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3395 | diff_populate_filespec(r, one, &dpf_options); |
Nguyễn Thái Ngọc Duy | 6bf3b81 | 2014-08-16 10:08:05 +0700 | [diff] [blame] | 3396 | if (one->is_binary == -1 && one->data) |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3397 | one->is_binary = buffer_is_binary(one->data, |
| 3398 | one->size); |
| 3399 | if (one->is_binary == -1) |
| 3400 | one->is_binary = 0; |
| 3401 | } |
| 3402 | } |
Junio C Hamano | 29a3eef | 2007-07-06 00:18:54 -0700 | [diff] [blame] | 3403 | return one->is_binary; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3404 | } |
| 3405 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3406 | static const struct userdiff_funcname * |
| 3407 | diff_funcname_pattern(struct diff_options *o, struct diff_filespec *one) |
Junio C Hamano | f258475 | 2007-07-06 00:45:10 -0700 | [diff] [blame] | 3408 | { |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3409 | diff_filespec_load_driver(one, o->repo->index); |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3410 | return one->driver->funcname.pattern ? &one->driver->funcname : NULL; |
Junio C Hamano | f258475 | 2007-07-06 00:45:10 -0700 | [diff] [blame] | 3411 | } |
| 3412 | |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3413 | void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b) |
| 3414 | { |
| 3415 | if (!options->a_prefix) |
| 3416 | options->a_prefix = a; |
| 3417 | if (!options->b_prefix) |
| 3418 | options->b_prefix = b; |
| 3419 | } |
| 3420 | |
Jeff King | 6799aad | 2023-03-09 01:07:06 -0500 | [diff] [blame] | 3421 | void diff_set_noprefix(struct diff_options *options) |
| 3422 | { |
| 3423 | options->a_prefix = options->b_prefix = ""; |
| 3424 | } |
| 3425 | |
| 3426 | void diff_set_default_prefix(struct diff_options *options) |
| 3427 | { |
| 3428 | options->a_prefix = "a/"; |
| 3429 | options->b_prefix = "b/"; |
| 3430 | } |
| 3431 | |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 3432 | struct userdiff_driver *get_textconv(struct repository *r, |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3433 | struct diff_filespec *one) |
Jeff King | 04427ac | 2008-10-26 00:44:53 -0400 | [diff] [blame] | 3434 | { |
| 3435 | if (!DIFF_FILE_VALID(one)) |
| 3436 | return NULL; |
Jeff King | d391c0f | 2010-09-21 17:01:24 -0400 | [diff] [blame] | 3437 | |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 3438 | diff_filespec_load_driver(one, r->index); |
| 3439 | return userdiff_get_textconv(r, one->driver); |
Jeff King | 04427ac | 2008-10-26 00:44:53 -0400 | [diff] [blame] | 3440 | } |
| 3441 | |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 3442 | static struct string_list *additional_headers(struct diff_options *o, |
| 3443 | const char *path) |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3444 | { |
| 3445 | if (!o->additional_path_headers) |
| 3446 | return NULL; |
| 3447 | return strmap_get(o->additional_path_headers, path); |
| 3448 | } |
| 3449 | |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 3450 | static void add_formatted_header(struct strbuf *msg, |
| 3451 | const char *header, |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3452 | const char *line_prefix, |
| 3453 | const char *meta, |
| 3454 | const char *reset) |
| 3455 | { |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 3456 | const char *next, *newline; |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3457 | |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 3458 | for (next = header; *next; next = newline) { |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3459 | newline = strchrnul(next, '\n'); |
| 3460 | strbuf_addf(msg, "%s%s%.*s%s\n", line_prefix, meta, |
| 3461 | (int)(newline - next), next, reset); |
| 3462 | if (*newline) |
| 3463 | newline++; |
| 3464 | } |
| 3465 | } |
| 3466 | |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 3467 | static void add_formatted_headers(struct strbuf *msg, |
| 3468 | struct string_list *more_headers, |
| 3469 | const char *line_prefix, |
| 3470 | const char *meta, |
| 3471 | const char *reset) |
| 3472 | { |
| 3473 | int i; |
| 3474 | |
| 3475 | for (i = 0; i < more_headers->nr; i++) |
| 3476 | add_formatted_header(msg, more_headers->items[i].string, |
| 3477 | line_prefix, meta, reset); |
| 3478 | } |
| 3479 | |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3480 | static int diff_filepair_is_phoney(struct diff_filespec *one, |
| 3481 | struct diff_filespec *two) |
| 3482 | { |
| 3483 | /* |
| 3484 | * This function specifically looks for pairs injected by |
| 3485 | * create_filepairs_for_header_only_notifications(). Such |
| 3486 | * pairs are "phoney" in that they do not represent any |
| 3487 | * content or even mode difference, but were inserted because |
| 3488 | * diff_queued_diff previously had no pair associated with |
| 3489 | * that path but we needed some pair to avoid losing the |
| 3490 | * "remerge CONFLICT" header associated with the path. |
| 3491 | */ |
| 3492 | return !DIFF_FILE_VALID(one) && !DIFF_FILE_VALID(two); |
| 3493 | } |
| 3494 | |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 3495 | static int set_diff_algorithm(struct diff_options *opts, |
| 3496 | const char *alg) |
| 3497 | { |
| 3498 | long value = parse_algorithm_value(alg); |
| 3499 | |
| 3500 | if (value < 0) |
| 3501 | return -1; |
| 3502 | |
| 3503 | /* clear out previous settings */ |
| 3504 | DIFF_XDL_CLR(opts, NEED_MINIMAL); |
| 3505 | opts->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK; |
| 3506 | opts->xdl_opts |= value; |
| 3507 | |
| 3508 | return 0; |
| 3509 | } |
| 3510 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3511 | static void builtin_diff(const char *name_a, |
| 3512 | const char *name_b, |
| 3513 | struct diff_filespec *one, |
| 3514 | struct diff_filespec *two, |
| 3515 | const char *xfrm_msg, |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3516 | int must_show_header, |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3517 | struct diff_options *o, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3518 | int complete_rewrite) |
| 3519 | { |
| 3520 | mmfile_t mf1, mf2; |
| 3521 | const char *lbl[2]; |
| 3522 | char *a_one, *b_two; |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3523 | const char *meta = diff_get_color_opt(o, DIFF_METAINFO); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 3524 | const char *reset = diff_get_color_opt(o, DIFF_RESET); |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3525 | const char *a_prefix, *b_prefix; |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 3526 | struct userdiff_driver *textconv_one = NULL; |
| 3527 | struct userdiff_driver *textconv_two = NULL; |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3528 | struct strbuf header = STRBUF_INIT; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 3529 | const char *line_prefix = diff_line_prefix(o); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3530 | |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3531 | diff_set_mnemonic_prefix(o, "a/", "b/"); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3532 | if (o->flags.reverse_diff) { |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3533 | a_prefix = o->b_prefix; |
| 3534 | b_prefix = o->a_prefix; |
| 3535 | } else { |
| 3536 | a_prefix = o->a_prefix; |
| 3537 | b_prefix = o->b_prefix; |
| 3538 | } |
| 3539 | |
Jacob Keller | 61cfbc0 | 2016-08-31 16:27:21 -0700 | [diff] [blame] | 3540 | if (o->submodule_format == DIFF_SUBMODULE_LOG && |
| 3541 | (!one->mode || S_ISGITLINK(one->mode)) && |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3542 | (!two->mode || S_ISGITLINK(two->mode)) && |
| 3543 | (!diff_filepair_is_phoney(one, two))) { |
Shourya Shukla | 180b154 | 2020-08-13 01:14:02 +0530 | [diff] [blame] | 3544 | show_submodule_diff_summary(o, one->path ? one->path : two->path, |
Jacob Keller | 602a283 | 2016-08-31 16:27:23 -0700 | [diff] [blame] | 3545 | &one->oid, &two->oid, |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 3546 | two->dirty_submodule); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3547 | return; |
Jacob Keller | fd47ae6 | 2016-08-31 16:27:25 -0700 | [diff] [blame] | 3548 | } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF && |
| 3549 | (!one->mode || S_ISGITLINK(one->mode)) && |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3550 | (!two->mode || S_ISGITLINK(two->mode)) && |
| 3551 | (!diff_filepair_is_phoney(one, two))) { |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 3552 | show_submodule_inline_diff(o, one->path ? one->path : two->path, |
Jacob Keller | fd47ae6 | 2016-08-31 16:27:25 -0700 | [diff] [blame] | 3553 | &one->oid, &two->oid, |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 3554 | two->dirty_submodule); |
Jacob Keller | fd47ae6 | 2016-08-31 16:27:25 -0700 | [diff] [blame] | 3555 | return; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3556 | } |
| 3557 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3558 | if (o->flags.allow_textconv) { |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 3559 | textconv_one = get_textconv(o->repo, one); |
| 3560 | textconv_two = get_textconv(o->repo, two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3561 | } |
| 3562 | |
Linus Torvalds | 71b989e | 2008-10-05 15:35:15 -0400 | [diff] [blame] | 3563 | /* Never use a non-valid filename anywhere if at all possible */ |
| 3564 | name_a = DIFF_FILE_VALID(one) ? name_a : name_b; |
| 3565 | name_b = DIFF_FILE_VALID(two) ? name_b : name_a; |
| 3566 | |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3567 | a_one = quote_two(a_prefix, name_a + (*name_a == '/')); |
| 3568 | b_two = quote_two(b_prefix, name_b + (*name_b == '/')); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3569 | lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null"; |
| 3570 | lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null"; |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3571 | if (diff_filepair_is_phoney(one, two)) { |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3572 | /* |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3573 | * We should only reach this point for pairs generated from |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3574 | * create_filepairs_for_header_only_notifications(). For |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3575 | * these, we want to avoid the "/dev/null" special casing |
| 3576 | * above, because we do not want such pairs shown as either |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3577 | * "new file" or "deleted file" below. |
| 3578 | */ |
| 3579 | lbl[0] = a_one; |
| 3580 | lbl[1] = b_two; |
| 3581 | } |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3582 | strbuf_addf(&header, "%s%sdiff --git %s %s%s\n", line_prefix, meta, a_one, b_two, reset); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3583 | if (lbl[0][0] == '/') { |
| 3584 | /* /dev/null */ |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3585 | strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 3586 | if (xfrm_msg) |
| 3587 | strbuf_addstr(&header, xfrm_msg); |
Junio C Hamano | c9a3e72 | 2023-08-18 16:59:29 -0700 | [diff] [blame] | 3588 | o->found_changes = 1; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3589 | must_show_header = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3590 | } |
| 3591 | else if (lbl[1][0] == '/') { |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3592 | strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 3593 | if (xfrm_msg) |
| 3594 | strbuf_addstr(&header, xfrm_msg); |
Junio C Hamano | c9a3e72 | 2023-08-18 16:59:29 -0700 | [diff] [blame] | 3595 | o->found_changes = 1; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3596 | must_show_header = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3597 | } |
| 3598 | else { |
| 3599 | if (one->mode != two->mode) { |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3600 | strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset); |
| 3601 | strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset); |
Junio C Hamano | c9a3e72 | 2023-08-18 16:59:29 -0700 | [diff] [blame] | 3602 | o->found_changes = 1; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3603 | must_show_header = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3604 | } |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 3605 | if (xfrm_msg) |
| 3606 | strbuf_addstr(&header, xfrm_msg); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3607 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3608 | /* |
| 3609 | * we do not run diff between different kind |
| 3610 | * of objects. |
| 3611 | */ |
| 3612 | if ((one->mode ^ two->mode) & S_IFMT) |
| 3613 | goto free_ab_and_return; |
Jeff King | 0c01857 | 2008-12-09 03:12:28 -0500 | [diff] [blame] | 3614 | if (complete_rewrite && |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3615 | (textconv_one || !diff_filespec_is_binary(o->repo, one)) && |
| 3616 | (textconv_two || !diff_filespec_is_binary(o->repo, two))) { |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3617 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3618 | header.buf, header.len, 0); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3619 | strbuf_reset(&header); |
Jeff King | 3aa1f7c | 2008-12-09 03:13:21 -0500 | [diff] [blame] | 3620 | emit_rewrite_diff(name_a, name_b, one, two, |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3621 | textconv_one, textconv_two, o); |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 3622 | o->found_changes = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3623 | goto free_ab_and_return; |
| 3624 | } |
| 3625 | } |
| 3626 | |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 3627 | if (o->irreversible_delete && lbl[1][0] == '/') { |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3628 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, |
| 3629 | header.len, 0); |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 3630 | strbuf_reset(&header); |
| 3631 | goto free_ab_and_return; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3632 | } else if (!o->flags.text && |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3633 | ( (!textconv_one && diff_filespec_is_binary(o->repo, one)) || |
| 3634 | (!textconv_two && diff_filespec_is_binary(o->repo, two)) )) { |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 3635 | struct strbuf sb = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | 1aaf69e | 2014-08-16 10:08:06 +0700 | [diff] [blame] | 3636 | if (!one->data && !two->data && |
| 3637 | S_ISREG(one->mode) && S_ISREG(two->mode) && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3638 | !o->flags.binary) { |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 3639 | if (oideq(&one->oid, &two->oid)) { |
Nguyễn Thái Ngọc Duy | 1aaf69e | 2014-08-16 10:08:06 +0700 | [diff] [blame] | 3640 | if (must_show_header) |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3641 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3642 | header.buf, header.len, |
| 3643 | 0); |
Nguyễn Thái Ngọc Duy | 1aaf69e | 2014-08-16 10:08:06 +0700 | [diff] [blame] | 3644 | goto free_ab_and_return; |
| 3645 | } |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3646 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3647 | header.buf, header.len, 0); |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 3648 | strbuf_addf(&sb, "%sBinary files %s and %s differ\n", |
| 3649 | diff_line_prefix(o), lbl[0], lbl[1]); |
| 3650 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES, |
| 3651 | sb.buf, sb.len, 0); |
| 3652 | strbuf_release(&sb); |
Nguyễn Thái Ngọc Duy | 1aaf69e | 2014-08-16 10:08:06 +0700 | [diff] [blame] | 3653 | goto free_ab_and_return; |
| 3654 | } |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3655 | if (fill_mmfile(o->repo, &mf1, one) < 0 || |
| 3656 | fill_mmfile(o->repo, &mf2, two) < 0) |
Jeff King | b337398 | 2010-04-01 20:14:24 -0400 | [diff] [blame] | 3657 | die("unable to read files to diff"); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3658 | /* Quite common confusing case */ |
| 3659 | if (mf1.size == mf2.size && |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3660 | !memcmp(mf1.ptr, mf2.ptr, mf1.size)) { |
| 3661 | if (must_show_header) |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3662 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3663 | header.buf, header.len, 0); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3664 | goto free_ab_and_return; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3665 | } |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3666 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3667 | strbuf_reset(&header); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3668 | if (o->flags.binary) |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3669 | emit_binary_diff(o, &mf1, &mf2); |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 3670 | else { |
| 3671 | strbuf_addf(&sb, "%sBinary files %s and %s differ\n", |
| 3672 | diff_line_prefix(o), lbl[0], lbl[1]); |
| 3673 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES, |
| 3674 | sb.buf, sb.len, 0); |
| 3675 | strbuf_release(&sb); |
| 3676 | } |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 3677 | o->found_changes = 1; |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 3678 | } else { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3679 | /* Crazy xdl interfaces.. */ |
Jeff King | 0da0e92 | 2019-01-11 17:17:22 -0500 | [diff] [blame] | 3680 | const char *diffopts; |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 3681 | const char *v; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3682 | xpparam_t xpp; |
| 3683 | xdemitconf_t xecfg; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3684 | struct emit_callback ecbdata; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 3685 | const struct userdiff_funcname *pe; |
Junio C Hamano | f258475 | 2007-07-06 00:45:10 -0700 | [diff] [blame] | 3686 | |
Junio C Hamano | b3f01ff | 2012-02-29 18:14:16 -0800 | [diff] [blame] | 3687 | if (must_show_header) { |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3688 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3689 | header.buf, header.len, 0); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3690 | strbuf_reset(&header); |
| 3691 | } |
| 3692 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 3693 | mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr); |
| 3694 | mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr); |
Jeff King | 04427ac | 2008-10-26 00:44:53 -0400 | [diff] [blame] | 3695 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3696 | pe = diff_funcname_pattern(o, one); |
Brandon Casey | 45e7ca0 | 2008-09-18 17:40:48 -0500 | [diff] [blame] | 3697 | if (!pe) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3698 | pe = diff_funcname_pattern(o, two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3699 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 3700 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 3701 | memset(&xecfg, 0, sizeof(xecfg)); |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 3702 | memset(&ecbdata, 0, sizeof(ecbdata)); |
Johannes Schindelin | 1cdde29 | 2018-08-13 04:33:11 -0700 | [diff] [blame] | 3703 | if (o->flags.suppress_diff_headers) |
| 3704 | lbl[0] = NULL; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3705 | ecbdata.label_path = lbl; |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 3706 | ecbdata.color_diff = want_color(o->use_color); |
Nguyễn Thái Ngọc Duy | 26d024e | 2018-09-21 17:57:37 +0200 | [diff] [blame] | 3707 | ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b); |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 3708 | if (ecbdata.ws_rule & WS_BLANK_AT_EOF) |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 3709 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 3710 | ecbdata.opt = o; |
Johannes Schindelin | 1cdde29 | 2018-08-13 04:33:11 -0700 | [diff] [blame] | 3711 | if (header.len && !o->flags.suppress_diff_headers) |
| 3712 | ecbdata.header = &header; |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 3713 | xpp.flags = o->xdl_opts; |
Michał Kępień | 296d4a9 | 2020-10-20 08:48:09 +0200 | [diff] [blame] | 3714 | xpp.ignore_regex = o->ignore_regex; |
| 3715 | xpp.ignore_regex_nr = o->ignore_regex_nr; |
Jonathan Tan | 2477ab2 | 2017-11-27 11:47:47 -0800 | [diff] [blame] | 3716 | xpp.anchors = o->anchors; |
| 3717 | xpp.anchors_nr = o->anchors_nr; |
Linus Torvalds | ee1e541 | 2006-05-13 13:23:48 -0700 | [diff] [blame] | 3718 | xecfg.ctxlen = o->context; |
René Scharfe | 6d0e674 | 2008-12-28 19:45:32 +0100 | [diff] [blame] | 3719 | xecfg.interhunkctxlen = o->interhunkcontext; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3720 | xecfg.flags = XDL_EMIT_FUNCNAMES; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3721 | if (o->flags.funccontext) |
René Scharfe | 14937c2 | 2011-10-09 13:36:57 +0200 | [diff] [blame] | 3722 | xecfg.flags |= XDL_EMIT_FUNCCONTEXT; |
Brandon Casey | 45e7ca0 | 2008-09-18 17:40:48 -0500 | [diff] [blame] | 3723 | if (pe) |
Brandon Casey | a013585 | 2008-09-18 17:42:48 -0500 | [diff] [blame] | 3724 | xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags); |
Jeff King | 0da0e92 | 2019-01-11 17:17:22 -0500 | [diff] [blame] | 3725 | |
| 3726 | diffopts = getenv("GIT_DIFF_OPTS"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3727 | if (!diffopts) |
| 3728 | ; |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 3729 | else if (skip_prefix(diffopts, "--unified=", &v)) |
| 3730 | xecfg.ctxlen = strtoul(v, NULL, 10); |
| 3731 | else if (skip_prefix(diffopts, "-u", &v)) |
| 3732 | xecfg.ctxlen = strtoul(v, NULL, 10); |
Jeff King | 0da0e92 | 2019-01-11 17:17:22 -0500 | [diff] [blame] | 3733 | |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 3734 | if (o->word_diff) |
| 3735 | init_diff_words_data(&ecbdata, o, one, two); |
Jeff King | 9346d6d | 2018-11-02 02:35:45 -0400 | [diff] [blame] | 3736 | if (xdi_diff_outf(&mf1, &mf2, NULL, fn_out_consume, |
| 3737 | &ecbdata, &xpp, &xecfg)) |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 3738 | die("unable to generate diff for %s", one->path); |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 3739 | if (o->word_diff) |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 3740 | free_diff_words_data(&ecbdata); |
Jeff King | 04427ac | 2008-10-26 00:44:53 -0400 | [diff] [blame] | 3741 | if (textconv_one) |
| 3742 | free(mf1.ptr); |
| 3743 | if (textconv_two) |
| 3744 | free(mf2.ptr); |
René Scharfe | 8cfe5f1 | 2009-07-02 00:01:43 +0200 | [diff] [blame] | 3745 | xdiff_clear_find_func(&xecfg); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3746 | } |
| 3747 | |
| 3748 | free_ab_and_return: |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3749 | strbuf_release(&header); |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3750 | diff_free_filespec_data(one); |
| 3751 | diff_free_filespec_data(two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3752 | free(a_one); |
| 3753 | free(b_two); |
| 3754 | return; |
| 3755 | } |
| 3756 | |
Nguyễn Thái Ngọc Duy | ddf88fa | 2018-02-24 21:09:59 +0700 | [diff] [blame] | 3757 | static char *get_compact_summary(const struct diff_filepair *p, int is_renamed) |
| 3758 | { |
| 3759 | if (!is_renamed) { |
| 3760 | if (p->status == DIFF_STATUS_ADDED) { |
| 3761 | if (S_ISLNK(p->two->mode)) |
| 3762 | return "new +l"; |
| 3763 | else if ((p->two->mode & 0777) == 0755) |
| 3764 | return "new +x"; |
| 3765 | else |
| 3766 | return "new"; |
| 3767 | } else if (p->status == DIFF_STATUS_DELETED) |
| 3768 | return "gone"; |
| 3769 | } |
| 3770 | if (S_ISLNK(p->one->mode) && !S_ISLNK(p->two->mode)) |
| 3771 | return "mode -l"; |
| 3772 | else if (!S_ISLNK(p->one->mode) && S_ISLNK(p->two->mode)) |
| 3773 | return "mode +l"; |
| 3774 | else if ((p->one->mode & 0777) == 0644 && |
| 3775 | (p->two->mode & 0777) == 0755) |
| 3776 | return "mode +x"; |
| 3777 | else if ((p->one->mode & 0777) == 0755 && |
| 3778 | (p->two->mode & 0777) == 0644) |
| 3779 | return "mode -x"; |
| 3780 | return NULL; |
| 3781 | } |
| 3782 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3783 | static void builtin_diffstat(const char *name_a, const char *name_b, |
| 3784 | struct diff_filespec *one, |
| 3785 | struct diff_filespec *two, |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 3786 | struct diffstat_t *diffstat, |
Johannes Schindelin | 0d21efa | 2006-06-14 17:40:23 +0200 | [diff] [blame] | 3787 | struct diff_options *o, |
Linus Torvalds | 74faaa1 | 2012-10-17 10:00:37 -0700 | [diff] [blame] | 3788 | struct diff_filepair *p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3789 | { |
| 3790 | mmfile_t mf1, mf2; |
| 3791 | struct diffstat_file *data; |
Thomas Guyot-Sionnest | ff0c7fa | 2020-09-24 03:41:41 -0400 | [diff] [blame] | 3792 | int may_differ; |
Linus Torvalds | 74faaa1 | 2012-10-17 10:00:37 -0700 | [diff] [blame] | 3793 | int complete_rewrite = 0; |
| 3794 | |
| 3795 | if (!DIFF_PAIR_UNMERGED(p)) { |
| 3796 | if (p->status == DIFF_STATUS_MODIFIED && p->score) |
| 3797 | complete_rewrite = 1; |
| 3798 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3799 | |
| 3800 | data = diffstat_add(diffstat, name_a, name_b); |
Junio C Hamano | 99bfd40 | 2012-11-27 11:17:14 -0800 | [diff] [blame] | 3801 | data->is_interesting = p->status != DIFF_STATUS_UNKNOWN; |
Nguyễn Thái Ngọc Duy | ddf88fa | 2018-02-24 21:09:59 +0700 | [diff] [blame] | 3802 | if (o->flags.stat_with_summary) |
| 3803 | data->comments = get_compact_summary(p, data->is_renamed); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3804 | |
| 3805 | if (!one || !two) { |
| 3806 | data->is_unmerged = 1; |
| 3807 | return; |
| 3808 | } |
Jeff King | ded0abc | 2011-02-19 03:04:56 -0500 | [diff] [blame] | 3809 | |
Thomas Guyot-Sionnest | ff0c7fa | 2020-09-24 03:41:41 -0400 | [diff] [blame] | 3810 | /* saves some reads if true, not a guarantee of diff outcome */ |
| 3811 | may_differ = !(one->oid_valid && two->oid_valid && |
| 3812 | oideq(&one->oid, &two->oid)); |
Zbigniew Jędrzejewski-Szmek | 352ca4e | 2012-05-01 19:10:15 +0200 | [diff] [blame] | 3813 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3814 | if (diff_filespec_is_binary(o->repo, one) || |
| 3815 | diff_filespec_is_binary(o->repo, two)) { |
Jeff King | ded0abc | 2011-02-19 03:04:56 -0500 | [diff] [blame] | 3816 | data->is_binary = 1; |
Thomas Guyot-Sionnest | ff0c7fa | 2020-09-24 03:41:41 -0400 | [diff] [blame] | 3817 | if (!may_differ) { |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 3818 | data->added = 0; |
| 3819 | data->deleted = 0; |
| 3820 | } else { |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3821 | data->added = diff_filespec_size(o->repo, two); |
| 3822 | data->deleted = diff_filespec_size(o->repo, one); |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 3823 | } |
Jeff King | ded0abc | 2011-02-19 03:04:56 -0500 | [diff] [blame] | 3824 | } |
| 3825 | |
| 3826 | else if (complete_rewrite) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3827 | diff_populate_filespec(o->repo, one, NULL); |
| 3828 | diff_populate_filespec(o->repo, two, NULL); |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 3829 | data->deleted = count_lines(one->data, one->size); |
| 3830 | data->added = count_lines(two->data, two->size); |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 3831 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3832 | |
Thomas Guyot-Sionnest | ff0c7fa | 2020-09-24 03:41:41 -0400 | [diff] [blame] | 3833 | else if (may_differ) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3834 | /* Crazy xdl interfaces.. */ |
| 3835 | xpparam_t xpp; |
| 3836 | xdemitconf_t xecfg; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3837 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3838 | if (fill_mmfile(o->repo, &mf1, one) < 0 || |
| 3839 | fill_mmfile(o->repo, &mf2, two) < 0) |
Jeff King | ded0abc | 2011-02-19 03:04:56 -0500 | [diff] [blame] | 3840 | die("unable to read files to diff"); |
| 3841 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 3842 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 3843 | memset(&xecfg, 0, sizeof(xecfg)); |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 3844 | xpp.flags = o->xdl_opts; |
Michał Kępień | 296d4a9 | 2020-10-20 08:48:09 +0200 | [diff] [blame] | 3845 | xpp.ignore_regex = o->ignore_regex; |
| 3846 | xpp.ignore_regex_nr = o->ignore_regex_nr; |
Jonathan Tan | 2477ab2 | 2017-11-27 11:47:47 -0800 | [diff] [blame] | 3847 | xpp.anchors = o->anchors; |
| 3848 | xpp.anchors_nr = o->anchors_nr; |
Junio C Hamano | f01cae9 | 2011-09-22 10:54:47 -0700 | [diff] [blame] | 3849 | xecfg.ctxlen = o->context; |
| 3850 | xecfg.interhunkctxlen = o->interhunkcontext; |
Ævar Arnfjörð Bjarmason | 5d93460 | 2021-04-12 19:15:29 +0200 | [diff] [blame] | 3851 | xecfg.flags = XDL_EMIT_NO_HUNK_HDR; |
| 3852 | if (xdi_diff_outf(&mf1, &mf2, NULL, |
Jeff King | 3b40a09 | 2018-11-02 02:36:06 -0400 | [diff] [blame] | 3853 | diffstat_consume, diffstat, &xpp, &xecfg)) |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 3854 | die("unable to generate diffstat for %s", one->path); |
Matthew Rogers | 1cf3d5d | 2020-08-20 00:41:32 +0000 | [diff] [blame] | 3855 | |
| 3856 | if (DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two)) { |
| 3857 | struct diffstat_file *file = |
| 3858 | diffstat->files[diffstat->nr - 1]; |
| 3859 | /* |
| 3860 | * Omit diffstats of modified files where nothing changed. |
Thomas Guyot-Sionnest | ff0c7fa | 2020-09-24 03:41:41 -0400 | [diff] [blame] | 3861 | * Even if may_differ, this might be the case due to |
Matthew Rogers | 1cf3d5d | 2020-08-20 00:41:32 +0000 | [diff] [blame] | 3862 | * ignoring whitespace changes, etc. |
| 3863 | * |
| 3864 | * But note that we special-case additions, deletions, |
| 3865 | * renames, and mode changes as adding an empty file, |
| 3866 | * for example is still of interest. |
| 3867 | */ |
| 3868 | if ((p->status == DIFF_STATUS_MODIFIED) |
| 3869 | && !file->added |
| 3870 | && !file->deleted |
| 3871 | && one->mode == two->mode) { |
| 3872 | free_diffstat_file(file); |
| 3873 | diffstat->nr--; |
| 3874 | } |
| 3875 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3876 | } |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3877 | |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3878 | diff_free_filespec_data(one); |
| 3879 | diff_free_filespec_data(two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3880 | } |
| 3881 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3882 | static void builtin_checkdiff(const char *name_a, const char *name_b, |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 3883 | const char *attr_path, |
Junio C Hamano | 5ff10dd | 2008-06-26 15:34:54 -0700 | [diff] [blame] | 3884 | struct diff_filespec *one, |
| 3885 | struct diff_filespec *two, |
| 3886 | struct diff_options *o) |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3887 | { |
| 3888 | mmfile_t mf1, mf2; |
| 3889 | struct checkdiff_t data; |
| 3890 | |
| 3891 | if (!two) |
| 3892 | return; |
| 3893 | |
| 3894 | memset(&data, 0, sizeof(data)); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3895 | data.filename = name_b ? name_b : name_a; |
| 3896 | data.lineno = 0; |
Junio C Hamano | 1ba111d | 2008-06-26 15:36:34 -0700 | [diff] [blame] | 3897 | data.o = o; |
Nguyễn Thái Ngọc Duy | 26d024e | 2018-09-21 17:57:37 +0200 | [diff] [blame] | 3898 | data.ws_rule = whitespace_rule(o->repo->index, attr_path); |
Nguyễn Thái Ngọc Duy | 32eaa46 | 2018-09-21 17:57:27 +0200 | [diff] [blame] | 3899 | data.conflict_marker_size = ll_merge_marker_size(o->repo->index, attr_path); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3900 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3901 | if (fill_mmfile(o->repo, &mf1, one) < 0 || |
| 3902 | fill_mmfile(o->repo, &mf2, two) < 0) |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3903 | die("unable to read files to diff"); |
| 3904 | |
Junio C Hamano | 5ff10dd | 2008-06-26 15:34:54 -0700 | [diff] [blame] | 3905 | /* |
| 3906 | * All the other codepaths check both sides, but not checking |
| 3907 | * the "old" side here is deliberate. We are checking the newly |
| 3908 | * introduced changes, and as long as the "new" side is text, we |
| 3909 | * can and should check what it introduces. |
| 3910 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3911 | if (diff_filespec_is_binary(o->repo, two)) |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3912 | goto free_and_return; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3913 | else { |
| 3914 | /* Crazy xdl interfaces.. */ |
| 3915 | xpparam_t xpp; |
| 3916 | xdemitconf_t xecfg; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3917 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 3918 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 3919 | memset(&xecfg, 0, sizeof(xecfg)); |
Junio C Hamano | c35539e | 2008-08-20 11:47:55 -0700 | [diff] [blame] | 3920 | xecfg.ctxlen = 1; /* at least one context line */ |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 3921 | xpp.flags = 0; |
Jeff King | 75ab763 | 2018-11-02 02:39:03 -0400 | [diff] [blame] | 3922 | if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume_hunk, |
| 3923 | checkdiff_consume, &data, |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 3924 | &xpp, &xecfg)) |
| 3925 | die("unable to generate checkdiff for %s", one->path); |
Junio C Hamano | 877f23c | 2008-06-26 15:36:59 -0700 | [diff] [blame] | 3926 | |
Junio C Hamano | 467babf | 2009-09-03 23:39:43 -0700 | [diff] [blame] | 3927 | if (data.ws_rule & WS_BLANK_AT_EOF) { |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 3928 | struct emit_callback ecbdata; |
| 3929 | int blank_at_eof; |
| 3930 | |
| 3931 | ecbdata.ws_rule = data.ws_rule; |
| 3932 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
Christoph Mallon | 8837d33 | 2010-10-10 19:24:06 +0200 | [diff] [blame] | 3933 | blank_at_eof = ecbdata.blank_at_eof_in_postimage; |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 3934 | |
Junio C Hamano | 467babf | 2009-09-03 23:39:43 -0700 | [diff] [blame] | 3935 | if (blank_at_eof) { |
| 3936 | static char *err; |
| 3937 | if (!err) |
| 3938 | err = whitespace_error_string(WS_BLANK_AT_EOF); |
| 3939 | fprintf(o->file, "%s:%d: %s.\n", |
| 3940 | data.filename, blank_at_eof, err); |
| 3941 | data.status = 1; /* report errors */ |
| 3942 | } |
Junio C Hamano | 877f23c | 2008-06-26 15:36:59 -0700 | [diff] [blame] | 3943 | } |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3944 | } |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3945 | free_and_return: |
| 3946 | diff_free_filespec_data(one); |
| 3947 | diff_free_filespec_data(two); |
Wincent Colaiuta | 62c6489 | 2007-12-13 21:24:52 +0100 | [diff] [blame] | 3948 | if (data.status) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3949 | o->flags.check_failed = 1; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3950 | } |
| 3951 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3952 | struct diff_filespec *alloc_filespec(const char *path) |
| 3953 | { |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 3954 | struct diff_filespec *spec; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3955 | |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 3956 | FLEXPTR_ALLOC_STR(spec, path, path); |
Linus Torvalds | 9fb8841 | 2007-10-25 11:19:10 -0700 | [diff] [blame] | 3957 | spec->count = 1; |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3958 | spec->is_binary = -1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3959 | return spec; |
| 3960 | } |
| 3961 | |
Linus Torvalds | 9fb8841 | 2007-10-25 11:19:10 -0700 | [diff] [blame] | 3962 | void free_filespec(struct diff_filespec *spec) |
| 3963 | { |
| 3964 | if (!--spec->count) { |
| 3965 | diff_free_filespec_data(spec); |
| 3966 | free(spec); |
| 3967 | } |
| 3968 | } |
| 3969 | |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 3970 | void fill_filespec(struct diff_filespec *spec, const struct object_id *oid, |
| 3971 | int oid_valid, unsigned short mode) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3972 | { |
| 3973 | if (mode) { |
| 3974 | spec->mode = canon_mode(mode); |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 3975 | oidcpy(&spec->oid, oid); |
| 3976 | spec->oid_valid = oid_valid; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3977 | } |
| 3978 | } |
| 3979 | |
| 3980 | /* |
Jakub Narebski | 5adf317 | 2007-05-26 00:37:40 +0200 | [diff] [blame] | 3981 | * Given a name and sha1 pair, if the index tells us the file in |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3982 | * the work tree has that object contents, return true, so that |
| 3983 | * prepare_temp_file() does not have to inflate and extract. |
| 3984 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3985 | static int reuse_worktree_file(struct index_state *istate, |
| 3986 | const char *name, |
| 3987 | const struct object_id *oid, |
| 3988 | int want_file) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3989 | { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 3990 | const struct cache_entry *ce; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3991 | struct stat st; |
| 3992 | int pos, len; |
| 3993 | |
Junio C Hamano | 150115a | 2009-03-22 15:26:07 -0700 | [diff] [blame] | 3994 | /* |
| 3995 | * We do not read the cache ourselves here, because the |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3996 | * benchmark with my previous version that always reads cache |
| 3997 | * shows that it makes things worse for diff-tree comparing |
| 3998 | * two linux-2.6 kernel trees in an already checked out work |
| 3999 | * tree. This is because most diff-tree comparisons deal with |
| 4000 | * only a small number of files, while reading the cache is |
| 4001 | * expensive for a large project, and its cost outweighs the |
| 4002 | * savings we get by not inflating the object to a temporary |
| 4003 | * file. Practically, this code only helps when we are used |
| 4004 | * by diff-cache --cached, which does read the cache before |
| 4005 | * calling us. |
| 4006 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4007 | if (!istate->cache) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4008 | return 0; |
| 4009 | |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 4010 | /* We want to avoid the working directory if our caller |
| 4011 | * doesn't need the data in a normal file, this system |
| 4012 | * is rather slow with its stat/open/mmap/close syscalls, |
| 4013 | * and the object is contained in a pack file. The pack |
| 4014 | * is probably already open and will be faster to obtain |
| 4015 | * the data through than the working directory. Loose |
| 4016 | * objects however would tend to be slower as they need |
| 4017 | * to be individually opened and inflated. |
| 4018 | */ |
brian m. carlson | 14c3c80 | 2018-05-02 00:25:33 +0000 | [diff] [blame] | 4019 | if (!FAST_WORKING_DIRECTORY && !want_file && has_object_pack(oid)) |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 4020 | return 0; |
| 4021 | |
Jeff King | 06dec43 | 2016-07-22 11:27:53 -0400 | [diff] [blame] | 4022 | /* |
| 4023 | * Similarly, if we'd have to convert the file contents anyway, that |
| 4024 | * makes the optimization not worthwhile. |
| 4025 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4026 | if (!want_file && would_convert_to_git(istate, name)) |
Jeff King | 06dec43 | 2016-07-22 11:27:53 -0400 | [diff] [blame] | 4027 | return 0; |
| 4028 | |
Derrick Stolee | ad90da7 | 2021-09-08 11:23:56 +0000 | [diff] [blame] | 4029 | /* |
| 4030 | * If this path does not match our sparse-checkout definition, |
| 4031 | * then the file will not be in the working directory. |
| 4032 | */ |
| 4033 | if (!path_in_sparse_checkout(name, istate)) |
| 4034 | return 0; |
| 4035 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4036 | len = strlen(name); |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4037 | pos = index_name_pos(istate, name, len); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4038 | if (pos < 0) |
| 4039 | return 0; |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4040 | ce = istate->cache[pos]; |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 4041 | |
| 4042 | /* |
| 4043 | * This is not the sha1 we are looking for, or |
| 4044 | * unreusable because it is not a regular file. |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4045 | */ |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 4046 | if (!oideq(oid, &ce->oid) || !S_ISREG(ce->ce_mode)) |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 4047 | return 0; |
| 4048 | |
| 4049 | /* |
Junio C Hamano | 150115a | 2009-03-22 15:26:07 -0700 | [diff] [blame] | 4050 | * If ce is marked as "assume unchanged", there is no |
| 4051 | * guarantee that work tree matches what we are looking for. |
| 4052 | */ |
Nguyễn Thái Ngọc Duy | b4d1690 | 2009-08-20 20:46:58 +0700 | [diff] [blame] | 4053 | if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) |
Junio C Hamano | 150115a | 2009-03-22 15:26:07 -0700 | [diff] [blame] | 4054 | return 0; |
| 4055 | |
| 4056 | /* |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 4057 | * If ce matches the file in the work tree, we can reuse it. |
| 4058 | */ |
| 4059 | if (ce_uptodate(ce) || |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4060 | (!lstat(name, &st) && !ie_match_stat(istate, ce, &st, 0))) |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 4061 | return 1; |
| 4062 | |
| 4063 | return 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4064 | } |
| 4065 | |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 4066 | static int diff_populate_gitlink(struct diff_filespec *s, int size_only) |
| 4067 | { |
Jeff King | b1ddfb9 | 2016-02-22 17:45:08 -0500 | [diff] [blame] | 4068 | struct strbuf buf = STRBUF_INIT; |
| 4069 | char *dirty = ""; |
Junio C Hamano | 8e08b41 | 2010-01-16 18:42:53 +0100 | [diff] [blame] | 4070 | |
| 4071 | /* Are we looking at the work tree? */ |
Jens Lehmann | 85adbf2 | 2010-03-12 22:23:52 +0100 | [diff] [blame] | 4072 | if (s->dirty_submodule) |
Junio C Hamano | 8e08b41 | 2010-01-16 18:42:53 +0100 | [diff] [blame] | 4073 | dirty = "-dirty"; |
| 4074 | |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 4075 | strbuf_addf(&buf, "Subproject commit %s%s\n", |
| 4076 | oid_to_hex(&s->oid), dirty); |
Jeff King | b1ddfb9 | 2016-02-22 17:45:08 -0500 | [diff] [blame] | 4077 | s->size = buf.len; |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 4078 | if (size_only) { |
| 4079 | s->data = NULL; |
Jeff King | b1ddfb9 | 2016-02-22 17:45:08 -0500 | [diff] [blame] | 4080 | strbuf_release(&buf); |
| 4081 | } else { |
| 4082 | s->data = strbuf_detach(&buf, NULL); |
| 4083 | s->should_free = 1; |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 4084 | } |
| 4085 | return 0; |
| 4086 | } |
| 4087 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4088 | /* |
| 4089 | * While doing rename detection and pickaxe operation, we may need to |
| 4090 | * grab the data for the blob (or file) for our own in-core comparison. |
| 4091 | * diff_filespec has data and size fields for this purpose. |
| 4092 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4093 | int diff_populate_filespec(struct repository *r, |
| 4094 | struct diff_filespec *s, |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4095 | const struct diff_populate_filespec_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4096 | { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4097 | int size_only = options ? options->check_size_only : 0; |
| 4098 | int check_binary = options ? options->check_binary : 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4099 | int err = 0; |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 4100 | int conv_flags = global_conv_flags_eol; |
Junio C Hamano | 5430bb2 | 2013-06-24 14:35:04 -0700 | [diff] [blame] | 4101 | /* |
| 4102 | * demote FAIL to WARN to allow inspecting the situation |
| 4103 | * instead of refusing. |
| 4104 | */ |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 4105 | if (conv_flags & CONV_EOL_RNDTRP_DIE) |
| 4106 | conv_flags = CONV_EOL_RNDTRP_WARN; |
Junio C Hamano | 5430bb2 | 2013-06-24 14:35:04 -0700 | [diff] [blame] | 4107 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4108 | if (!DIFF_FILE_VALID(s)) |
| 4109 | die("internal error: asking to populate invalid file."); |
| 4110 | if (S_ISDIR(s->mode)) |
| 4111 | return -1; |
| 4112 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4113 | if (s->data) |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 4114 | return 0; |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 4115 | |
Junio C Hamano | 6e0b8ed | 2007-05-07 01:14:21 -0700 | [diff] [blame] | 4116 | if (size_only && 0 < s->size) |
| 4117 | return 0; |
| 4118 | |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 4119 | if (S_ISGITLINK(s->mode)) |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 4120 | return diff_populate_gitlink(s, size_only); |
| 4121 | |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4122 | if (!s->oid_valid || |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4123 | reuse_worktree_file(r->index, s->path, &s->oid, 0)) { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 4124 | struct strbuf buf = STRBUF_INIT; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4125 | struct stat st; |
| 4126 | int fd; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 4127 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4128 | if (lstat(s->path, &st) < 0) { |
Andrey Okoshkin | 10e0ca8 | 2017-10-27 12:33:25 +0300 | [diff] [blame] | 4129 | err_empty: |
| 4130 | err = -1; |
| 4131 | empty: |
| 4132 | s->data = (char *)""; |
| 4133 | s->size = 0; |
| 4134 | return err; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4135 | } |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 4136 | s->size = xsize_t(st.st_size); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4137 | if (!s->size) |
| 4138 | goto empty; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4139 | if (S_ISLNK(st.st_mode)) { |
Linus Torvalds | cf219d8 | 2008-12-17 10:26:13 -0800 | [diff] [blame] | 4140 | struct strbuf sb = STRBUF_INIT; |
| 4141 | |
| 4142 | if (strbuf_readlink(&sb, s->path, s->size)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4143 | goto err_empty; |
René Scharfe | 0956a6d | 2008-12-18 17:56:51 +0100 | [diff] [blame] | 4144 | s->size = sb.len; |
| 4145 | s->data = strbuf_detach(&sb, NULL); |
Linus Torvalds | cf219d8 | 2008-12-17 10:26:13 -0800 | [diff] [blame] | 4146 | s->should_free = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4147 | return 0; |
| 4148 | } |
Junio C Hamano | 12426e1 | 2017-03-01 18:04:44 +0100 | [diff] [blame] | 4149 | |
| 4150 | /* |
| 4151 | * Even if the caller would be happy with getting |
| 4152 | * only the size, we cannot return early at this |
| 4153 | * point if the path requires us to run the content |
| 4154 | * conversion. |
| 4155 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4156 | if (size_only && !would_convert_to_git(r->index, s->path)) |
Linus Torvalds | cf219d8 | 2008-12-17 10:26:13 -0800 | [diff] [blame] | 4157 | return 0; |
Junio C Hamano | 12426e1 | 2017-03-01 18:04:44 +0100 | [diff] [blame] | 4158 | |
| 4159 | /* |
| 4160 | * Note: this check uses xsize_t(st.st_size) that may |
| 4161 | * not be the true size of the blob after it goes |
| 4162 | * through convert_to_git(). This may not strictly be |
| 4163 | * correct, but the whole point of big_file_threshold |
| 4164 | * and is_binary check being that we want to avoid |
| 4165 | * opening the file and inspecting the contents, this |
| 4166 | * is probably fine. |
| 4167 | */ |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4168 | if (check_binary && |
Nguyễn Thái Ngọc Duy | 6bf3b81 | 2014-08-16 10:08:05 +0700 | [diff] [blame] | 4169 | s->size > big_file_threshold && s->is_binary == -1) { |
| 4170 | s->is_binary = 1; |
| 4171 | return 0; |
| 4172 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4173 | fd = open(s->path, O_RDONLY); |
| 4174 | if (fd < 0) |
| 4175 | goto err_empty; |
Shawn O. Pearce | c4712e4 | 2006-12-24 00:47:23 -0500 | [diff] [blame] | 4176 | s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4177 | close(fd); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4178 | s->should_munmap = 1; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 4179 | |
| 4180 | /* |
| 4181 | * Convert from working tree format to canonical git format |
| 4182 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4183 | if (convert_to_git(r->index, s->path, s->data, s->size, &buf, conv_flags)) { |
René Scharfe | c32f749 | 2007-10-21 11:23:49 +0200 | [diff] [blame] | 4184 | size_t size = 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 4185 | munmap(s->data, s->size); |
| 4186 | s->should_munmap = 0; |
René Scharfe | c32f749 | 2007-10-21 11:23:49 +0200 | [diff] [blame] | 4187 | s->data = strbuf_detach(&buf, &size); |
| 4188 | s->size = size; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 4189 | s->should_free = 1; |
| 4190 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4191 | } |
| 4192 | else { |
Jonathan Tan | c14b6f8 | 2020-04-07 15:11:42 -0700 | [diff] [blame] | 4193 | struct object_info info = { |
| 4194 | .sizep = &s->size |
| 4195 | }; |
| 4196 | |
| 4197 | if (!(size_only || check_binary)) |
| 4198 | /* |
| 4199 | * Set contentp, since there is no chance that merely |
| 4200 | * the size is sufficient. |
| 4201 | */ |
| 4202 | info.contentp = &s->data; |
| 4203 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 4204 | if (options && options->missing_object_cb) { |
| 4205 | if (!oid_object_info_extended(r, &s->oid, &info, |
| 4206 | OBJECT_INFO_LOOKUP_REPLACE | |
| 4207 | OBJECT_INFO_SKIP_FETCH_OBJECT)) |
| 4208 | goto object_read; |
| 4209 | options->missing_object_cb(options->missing_object_data); |
| 4210 | } |
Jonathan Tan | c14b6f8 | 2020-04-07 15:11:42 -0700 | [diff] [blame] | 4211 | if (oid_object_info_extended(r, &s->oid, &info, |
| 4212 | OBJECT_INFO_LOOKUP_REPLACE)) |
| 4213 | die("unable to read %s", oid_to_hex(&s->oid)); |
| 4214 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 4215 | object_read: |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4216 | if (size_only || check_binary) { |
Nguyễn Thái Ngọc Duy | 6bf3b81 | 2014-08-16 10:08:05 +0700 | [diff] [blame] | 4217 | if (size_only) |
| 4218 | return 0; |
| 4219 | if (s->size > big_file_threshold && s->is_binary == -1) { |
| 4220 | s->is_binary = 1; |
| 4221 | return 0; |
| 4222 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4223 | } |
Jonathan Tan | c14b6f8 | 2020-04-07 15:11:42 -0700 | [diff] [blame] | 4224 | if (!info.contentp) { |
| 4225 | info.contentp = &s->data; |
| 4226 | if (oid_object_info_extended(r, &s->oid, &info, |
| 4227 | OBJECT_INFO_LOOKUP_REPLACE)) |
| 4228 | die("unable to read %s", oid_to_hex(&s->oid)); |
| 4229 | } |
Nguyễn Thái Ngọc Duy | 6bf3b81 | 2014-08-16 10:08:05 +0700 | [diff] [blame] | 4230 | s->should_free = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4231 | } |
| 4232 | return 0; |
| 4233 | } |
| 4234 | |
Junio C Hamano | 8ae92e6 | 2007-10-02 21:01:03 -0700 | [diff] [blame] | 4235 | void diff_free_filespec_blob(struct diff_filespec *s) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4236 | { |
| 4237 | if (s->should_free) |
| 4238 | free(s->data); |
| 4239 | else if (s->should_munmap) |
| 4240 | munmap(s->data, s->size); |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 4241 | |
| 4242 | if (s->should_free || s->should_munmap) { |
| 4243 | s->should_free = s->should_munmap = 0; |
| 4244 | s->data = NULL; |
| 4245 | } |
Jeff King | eede7b7 | 2007-09-25 15:29:42 -0400 | [diff] [blame] | 4246 | } |
| 4247 | |
| 4248 | void diff_free_filespec_data(struct diff_filespec *s) |
| 4249 | { |
Jinoh Kang | 2469593 | 2020-11-06 17:14:52 +0000 | [diff] [blame] | 4250 | if (!s) |
| 4251 | return; |
| 4252 | |
Junio C Hamano | 8ae92e6 | 2007-10-02 21:01:03 -0700 | [diff] [blame] | 4253 | diff_free_filespec_blob(s); |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 4254 | FREE_AND_NULL(s->cnt_data); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4255 | } |
| 4256 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4257 | static void prep_temp_blob(struct index_state *istate, |
| 4258 | const char *path, struct diff_tempfile *temp, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4259 | void *blob, |
| 4260 | unsigned long size, |
brian m. carlson | 09bdff2 | 2016-06-24 23:09:29 +0000 | [diff] [blame] | 4261 | const struct object_id *oid, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4262 | int mode) |
| 4263 | { |
Johannes Schindelin | 4e218f5 | 2009-03-21 12:42:52 +0100 | [diff] [blame] | 4264 | struct strbuf buf = STRBUF_INIT; |
David Aguilar | 003b33a | 2009-05-31 01:35:52 -0700 | [diff] [blame] | 4265 | char *path_dup = xstrdup(path); |
| 4266 | const char *base = basename(path_dup); |
brian m. carlson | c397aac | 2020-03-16 18:05:03 +0000 | [diff] [blame] | 4267 | struct checkout_metadata meta; |
| 4268 | |
| 4269 | init_checkout_metadata(&meta, NULL, NULL, oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4270 | |
René Scharfe | 8af7593 | 2022-04-20 22:30:10 +0200 | [diff] [blame] | 4271 | temp->tempfile = mks_tempfile_dt("git-blob-XXXXXX", base); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 4272 | if (!temp->tempfile) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 4273 | die_errno("unable to create temp-file"); |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4274 | if (convert_to_working_tree(istate, path, |
brian m. carlson | c397aac | 2020-03-16 18:05:03 +0000 | [diff] [blame] | 4275 | (const char *)blob, (size_t)size, &buf, &meta)) { |
Johannes Schindelin | 4e218f5 | 2009-03-21 12:42:52 +0100 | [diff] [blame] | 4276 | blob = buf.buf; |
| 4277 | size = buf.len; |
| 4278 | } |
Junio C Hamano | c50424a | 2017-09-25 15:24:06 +0900 | [diff] [blame] | 4279 | if (write_in_full(temp->tempfile->fd, blob, size) < 0 || |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 4280 | close_tempfile_gently(temp->tempfile)) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 4281 | die_errno("unable to write temp-file"); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 4282 | temp->name = get_tempfile_path(temp->tempfile); |
brian m. carlson | 09bdff2 | 2016-06-24 23:09:29 +0000 | [diff] [blame] | 4283 | oid_to_hex_r(temp->hex, oid); |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 4284 | xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode); |
Johannes Schindelin | 4e218f5 | 2009-03-21 12:42:52 +0100 | [diff] [blame] | 4285 | strbuf_release(&buf); |
David Aguilar | 003b33a | 2009-05-31 01:35:52 -0700 | [diff] [blame] | 4286 | free(path_dup); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4287 | } |
| 4288 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4289 | static struct diff_tempfile *prepare_temp_file(struct repository *r, |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4290 | struct diff_filespec *one) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4291 | { |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 4292 | struct diff_tempfile *temp = claim_diff_tempfile(); |
| 4293 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4294 | if (!DIFF_FILE_VALID(one)) { |
| 4295 | not_a_valid_file: |
| 4296 | /* A '-' entry produces this for file-2, and |
| 4297 | * a '+' entry produces this for file-1. |
| 4298 | */ |
| 4299 | temp->name = "/dev/null"; |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 4300 | xsnprintf(temp->hex, sizeof(temp->hex), "."); |
| 4301 | xsnprintf(temp->mode, sizeof(temp->mode), "."); |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 4302 | return temp; |
| 4303 | } |
| 4304 | |
Thomas Rast | aba4727 | 2014-02-16 17:52:34 +0100 | [diff] [blame] | 4305 | if (!S_ISGITLINK(one->mode) && |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4306 | (!one->oid_valid || |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4307 | reuse_worktree_file(r->index, one->path, &one->oid, 1))) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4308 | struct stat st; |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4309 | if (lstat(one->path, &st) < 0) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4310 | if (errno == ENOENT) |
| 4311 | goto not_a_valid_file; |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4312 | die_errno("stat(%s)", one->path); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4313 | } |
| 4314 | if (S_ISLNK(st.st_mode)) { |
Jeff King | 3cd7388 | 2009-05-25 06:46:09 -0400 | [diff] [blame] | 4315 | struct strbuf sb = STRBUF_INIT; |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4316 | if (strbuf_readlink(&sb, one->path, st.st_size) < 0) |
| 4317 | die_errno("readlink(%s)", one->path); |
| 4318 | prep_temp_blob(r->index, one->path, temp, sb.buf, sb.len, |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4319 | (one->oid_valid ? |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 4320 | &one->oid : null_oid()), |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4321 | (one->oid_valid ? |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4322 | one->mode : S_IFLNK)); |
Jeff King | 3cd7388 | 2009-05-25 06:46:09 -0400 | [diff] [blame] | 4323 | strbuf_release(&sb); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4324 | } |
| 4325 | else { |
| 4326 | /* we can borrow from the file in the work tree */ |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4327 | temp->name = one->path; |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4328 | if (!one->oid_valid) |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 4329 | oid_to_hex_r(temp->hex, null_oid()); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4330 | else |
René Scharfe | 2490574 | 2017-01-28 23:03:03 +0100 | [diff] [blame] | 4331 | oid_to_hex_r(temp->hex, &one->oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4332 | /* Even though we may sometimes borrow the |
| 4333 | * contents from the work tree, we always want |
| 4334 | * one->mode. mode is trustworthy even when |
Brandon Williams | 7401415 | 2017-05-30 10:30:52 -0700 | [diff] [blame] | 4335 | * !(one->oid_valid), as long as |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4336 | * DIFF_FILE_VALID(one). |
| 4337 | */ |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 4338 | xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4339 | } |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 4340 | return temp; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4341 | } |
| 4342 | else { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4343 | if (diff_populate_filespec(r, one, NULL)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4344 | die("cannot read data blob for %s", one->path); |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4345 | prep_temp_blob(r->index, one->path, temp, |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4346 | one->data, one->size, |
brian m. carlson | 09bdff2 | 2016-06-24 23:09:29 +0000 | [diff] [blame] | 4347 | &one->oid, one->mode); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4348 | } |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 4349 | return temp; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4350 | } |
| 4351 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4352 | static void add_external_diff_name(struct repository *r, |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 4353 | struct strvec *argv, |
Jeff King | f3efe78 | 2014-04-19 15:22:25 -0400 | [diff] [blame] | 4354 | struct diff_filespec *df) |
| 4355 | { |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4356 | struct diff_tempfile *temp = prepare_temp_file(r, df); |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 4357 | strvec_push(argv, temp->name); |
| 4358 | strvec_push(argv, temp->hex); |
| 4359 | strvec_push(argv, temp->mode); |
Jeff King | f3efe78 | 2014-04-19 15:22:25 -0400 | [diff] [blame] | 4360 | } |
| 4361 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4362 | /* An external diff command takes: |
| 4363 | * |
| 4364 | * diff-cmd name infile1 infile1-sha1 infile1-mode \ |
| 4365 | * infile2 infile2-sha1 infile2-mode [ rename-to ] |
| 4366 | * |
| 4367 | */ |
| 4368 | static void run_external_diff(const char *pgm, |
| 4369 | const char *name, |
| 4370 | const char *other, |
| 4371 | struct diff_filespec *one, |
| 4372 | struct diff_filespec *two, |
| 4373 | const char *xfrm_msg, |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4374 | struct diff_options *o) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4375 | { |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 4376 | struct child_process cmd = CHILD_PROCESS_INIT; |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4377 | struct diff_queue_struct *q = &diff_queued_diff; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4378 | |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 4379 | strvec_push(&cmd.args, pgm); |
| 4380 | strvec_push(&cmd.args, name); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4381 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4382 | if (one && two) { |
Jeff King | de8f14e | 2023-01-06 06:04:18 -0500 | [diff] [blame] | 4383 | add_external_diff_name(o->repo, &cmd.args, one); |
| 4384 | add_external_diff_name(o->repo, &cmd.args, two); |
| 4385 | if (other) { |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 4386 | strvec_push(&cmd.args, other); |
Jeff King | 85a9a63 | 2024-01-28 20:57:08 -0500 | [diff] [blame] | 4387 | if (xfrm_msg) |
| 4388 | strvec_push(&cmd.args, xfrm_msg); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4389 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4390 | } |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4391 | |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 4392 | strvec_pushf(&cmd.env, "GIT_DIFF_PATH_COUNTER=%d", |
| 4393 | ++o->diff_path_counter); |
| 4394 | strvec_pushf(&cmd.env, "GIT_DIFF_PATH_TOTAL=%d", q->nr); |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4395 | |
Johannes Schindelin | 3aef54e | 2019-07-11 01:23:41 -0700 | [diff] [blame] | 4396 | diff_free_filespec_data(one); |
| 4397 | diff_free_filespec_data(two); |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 4398 | cmd.use_shell = 1; |
| 4399 | if (run_command(&cmd)) |
Jeff King | 89294d1 | 2014-04-19 15:19:19 -0400 | [diff] [blame] | 4400 | die(_("external diff died, stopping at %s"), name); |
| 4401 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4402 | remove_tempfile(); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4403 | } |
| 4404 | |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4405 | static int similarity_index(struct diff_filepair *p) |
| 4406 | { |
| 4407 | return p->score * 100 / MAX_SCORE; |
| 4408 | } |
| 4409 | |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 4410 | static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev) |
| 4411 | { |
| 4412 | if (startup_info->have_repository) |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 4413 | return repo_find_unique_abbrev(the_repository, oid, abbrev); |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 4414 | else { |
| 4415 | char *hex = oid_to_hex(oid); |
Junio C Hamano | 0d9c527 | 2016-10-27 14:58:48 -0700 | [diff] [blame] | 4416 | if (abbrev < 0) |
| 4417 | abbrev = FALLBACK_DEFAULT_ABBREV; |
brian m. carlson | 02afca1 | 2018-07-16 01:28:05 +0000 | [diff] [blame] | 4418 | if (abbrev > the_hash_algo->hexsz) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 4419 | BUG("oid abbreviation out of range: %d", abbrev); |
Jack Bates | 43d1948 | 2016-12-06 09:56:14 -0700 | [diff] [blame] | 4420 | if (abbrev) |
| 4421 | hex[abbrev] = '\0'; |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 4422 | return hex; |
| 4423 | } |
| 4424 | } |
| 4425 | |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4426 | static void fill_metainfo(struct strbuf *msg, |
| 4427 | const char *name, |
| 4428 | const char *other, |
| 4429 | struct diff_filespec *one, |
| 4430 | struct diff_filespec *two, |
| 4431 | struct diff_options *o, |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4432 | struct diff_filepair *p, |
Junio C Hamano | 5977744 | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4433 | int *must_show_header, |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4434 | int use_color) |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4435 | { |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4436 | const char *set = diff_get_color(use_color, DIFF_METAINFO); |
| 4437 | const char *reset = diff_get_color(use_color, DIFF_RESET); |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 4438 | const char *line_prefix = diff_line_prefix(o); |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 4439 | struct string_list *more_headers = NULL; |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 4440 | |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 4441 | *must_show_header = 1; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4442 | strbuf_init(msg, PATH_MAX * 2 + 300); |
| 4443 | switch (p->status) { |
| 4444 | case DIFF_STATUS_COPIED: |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4445 | strbuf_addf(msg, "%s%ssimilarity index %d%%", |
| 4446 | line_prefix, set, similarity_index(p)); |
| 4447 | strbuf_addf(msg, "%s\n%s%scopy from ", |
| 4448 | reset, line_prefix, set); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4449 | quote_c_style(name, msg, NULL, 0); |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4450 | strbuf_addf(msg, "%s\n%s%scopy to ", reset, line_prefix, set); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4451 | quote_c_style(other, msg, NULL, 0); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4452 | strbuf_addf(msg, "%s\n", reset); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4453 | break; |
| 4454 | case DIFF_STATUS_RENAMED: |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4455 | strbuf_addf(msg, "%s%ssimilarity index %d%%", |
| 4456 | line_prefix, set, similarity_index(p)); |
| 4457 | strbuf_addf(msg, "%s\n%s%srename from ", |
| 4458 | reset, line_prefix, set); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4459 | quote_c_style(name, msg, NULL, 0); |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4460 | strbuf_addf(msg, "%s\n%s%srename to ", |
| 4461 | reset, line_prefix, set); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4462 | quote_c_style(other, msg, NULL, 0); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4463 | strbuf_addf(msg, "%s\n", reset); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4464 | break; |
| 4465 | case DIFF_STATUS_MODIFIED: |
| 4466 | if (p->score) { |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4467 | strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n", |
| 4468 | line_prefix, |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4469 | set, similarity_index(p), reset); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4470 | break; |
| 4471 | } |
| 4472 | /* fallthru */ |
| 4473 | default: |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 4474 | *must_show_header = 0; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4475 | } |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 4476 | if ((more_headers = additional_headers(o, name))) { |
| 4477 | add_formatted_headers(msg, more_headers, |
| 4478 | line_prefix, set, reset); |
| 4479 | *must_show_header = 1; |
| 4480 | } |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 4481 | if (one && two && !oideq(&one->oid, &two->oid)) { |
brian m. carlson | de1d81d | 2018-05-02 00:25:52 +0000 | [diff] [blame] | 4482 | const unsigned hexsz = the_hash_algo->hexsz; |
Đoàn Trần Công Danh | 3046c7f | 2020-08-21 18:51:47 +0700 | [diff] [blame] | 4483 | int abbrev = o->abbrev ? o->abbrev : DEFAULT_ABBREV; |
| 4484 | |
| 4485 | if (o->flags.full_index) |
| 4486 | abbrev = hexsz; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4487 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4488 | if (o->flags.binary) { |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4489 | mmfile_t mf; |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4490 | if ((!fill_mmfile(o->repo, &mf, one) && |
| 4491 | diff_filespec_is_binary(o->repo, one)) || |
| 4492 | (!fill_mmfile(o->repo, &mf, two) && |
| 4493 | diff_filespec_is_binary(o->repo, two))) |
brian m. carlson | de1d81d | 2018-05-02 00:25:52 +0000 | [diff] [blame] | 4494 | abbrev = hexsz; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4495 | } |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 4496 | strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set, |
| 4497 | diff_abbrev_oid(&one->oid, abbrev), |
| 4498 | diff_abbrev_oid(&two->oid, abbrev)); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4499 | if (one->mode == two->mode) |
| 4500 | strbuf_addf(msg, " %06o", one->mode); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4501 | strbuf_addf(msg, "%s\n", reset); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4502 | } |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4503 | } |
| 4504 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4505 | static void run_diff_cmd(const char *pgm, |
| 4506 | const char *name, |
| 4507 | const char *other, |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4508 | const char *attr_path, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4509 | struct diff_filespec *one, |
| 4510 | struct diff_filespec *two, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4511 | struct strbuf *msg, |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 4512 | struct diff_options *o, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4513 | struct diff_filepair *p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4514 | { |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4515 | const char *xfrm_msg = NULL; |
| 4516 | int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 4517 | int must_show_header = 0; |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4518 | struct userdiff_driver *drv = NULL; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4519 | |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4520 | if (o->flags.allow_external || !o->ignore_driver_algorithm) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 4521 | drv = userdiff_find_by_path(o->repo->index, attr_path); |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4522 | |
| 4523 | if (o->flags.allow_external && drv && drv->external) |
| 4524 | pgm = drv->external; |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 4525 | |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4526 | if (msg) { |
| 4527 | /* |
| 4528 | * don't use colors when the header is intended for an |
| 4529 | * external diff driver |
| 4530 | */ |
| 4531 | fill_metainfo(msg, name, other, one, two, o, p, |
Junio C Hamano | 5977744 | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4532 | &must_show_header, |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 4533 | want_color(o->use_color) && !pgm); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4534 | xfrm_msg = msg->len ? msg->buf : NULL; |
| 4535 | } |
| 4536 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4537 | if (pgm) { |
Jeff King | 4bc1792 | 2019-02-14 00:49:42 -0500 | [diff] [blame] | 4538 | run_external_diff(pgm, name, other, one, two, xfrm_msg, o); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4539 | return; |
| 4540 | } |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4541 | if (one && two) { |
| 4542 | if (!o->ignore_driver_algorithm && drv && drv->algorithm) |
| 4543 | set_diff_algorithm(o, drv->algorithm); |
| 4544 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4545 | builtin_diff(name, other ? other : name, |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 4546 | one, two, xfrm_msg, must_show_header, |
| 4547 | o, complete_rewrite); |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4548 | } else { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 4549 | fprintf(o->file, "* Unmerged path %s\n", name); |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4550 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4551 | } |
| 4552 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4553 | static void diff_fill_oid_info(struct diff_filespec *one, struct index_state *istate) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4554 | { |
| 4555 | if (DIFF_FILE_VALID(one)) { |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4556 | if (!one->oid_valid) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4557 | struct stat st; |
Junio C Hamano | 4682d85 | 2012-06-27 20:14:47 -0700 | [diff] [blame] | 4558 | if (one->is_stdin) { |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 4559 | oidclr(&one->oid); |
Johannes Schindelin | 5332b2a | 2007-02-25 23:36:10 +0100 | [diff] [blame] | 4560 | return; |
| 4561 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4562 | if (lstat(one->path, &st) < 0) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 4563 | die_errno("stat '%s'", one->path); |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4564 | if (index_path(istate, &one->oid, one->path, &st, 0)) |
Alexander Potashev | d753070 | 2009-01-04 21:38:41 +0300 | [diff] [blame] | 4565 | die("cannot hash %s", one->path); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4566 | } |
| 4567 | } |
| 4568 | else |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 4569 | oidclr(&one->oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4570 | } |
| 4571 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4572 | static void strip_prefix(int prefix_length, const char **namep, const char **otherp) |
| 4573 | { |
| 4574 | /* Strip the prefix but do not molest /dev/null and absolute paths */ |
Johannes Sixt | ffd04e9 | 2018-10-19 18:58:07 +0200 | [diff] [blame] | 4575 | if (*namep && !is_absolute_path(*namep)) { |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4576 | *namep += prefix_length; |
Jakub Narebski | d8faea9 | 2010-08-09 10:50:53 -0400 | [diff] [blame] | 4577 | if (**namep == '/') |
| 4578 | ++*namep; |
| 4579 | } |
Johannes Sixt | ffd04e9 | 2018-10-19 18:58:07 +0200 | [diff] [blame] | 4580 | if (*otherp && !is_absolute_path(*otherp)) { |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4581 | *otherp += prefix_length; |
Jakub Narebski | d8faea9 | 2010-08-09 10:50:53 -0400 | [diff] [blame] | 4582 | if (**otherp == '/') |
| 4583 | ++*otherp; |
| 4584 | } |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4585 | } |
| 4586 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4587 | static void run_diff(struct diff_filepair *p, struct diff_options *o) |
| 4588 | { |
| 4589 | const char *pgm = external_diff(); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 4590 | struct strbuf msg; |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 4591 | struct diff_filespec *one = p->one; |
| 4592 | struct diff_filespec *two = p->two; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4593 | const char *name; |
| 4594 | const char *other; |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4595 | const char *attr_path; |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 4596 | |
Stefan Beller | f2d2a5d | 2017-06-29 17:06:46 -0700 | [diff] [blame] | 4597 | name = one->path; |
| 4598 | other = (strcmp(name, two->path) ? two->path : NULL); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4599 | attr_path = name; |
| 4600 | if (o->prefix_length) |
| 4601 | strip_prefix(o->prefix_length, &name, &other); |
| 4602 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4603 | if (!o->flags.allow_external) |
Junio C Hamano | bd8c1a9 | 2012-07-17 22:08:59 -0700 | [diff] [blame] | 4604 | pgm = NULL; |
| 4605 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4606 | if (DIFF_PAIR_UNMERGED(p)) { |
| 4607 | run_diff_cmd(pgm, name, NULL, attr_path, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4608 | NULL, NULL, NULL, o, p); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4609 | return; |
| 4610 | } |
| 4611 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4612 | diff_fill_oid_info(one, o->repo->index); |
| 4613 | diff_fill_oid_info(two, o->repo->index); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4614 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4615 | if (!pgm && |
| 4616 | DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) && |
| 4617 | (S_IFMT & one->mode) != (S_IFMT & two->mode)) { |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4618 | /* |
| 4619 | * a filepair that changes between file and symlink |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4620 | * needs to be split into deletion and creation. |
| 4621 | */ |
| 4622 | struct diff_filespec *null = alloc_filespec(two->path); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4623 | run_diff_cmd(NULL, name, other, attr_path, |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4624 | one, null, &msg, |
| 4625 | o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4626 | free(null); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4627 | strbuf_release(&msg); |
| 4628 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4629 | null = alloc_filespec(one->path); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4630 | run_diff_cmd(NULL, name, other, attr_path, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4631 | null, two, &msg, o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4632 | free(null); |
| 4633 | } |
| 4634 | else |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4635 | run_diff_cmd(pgm, name, other, attr_path, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4636 | one, two, &msg, o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4637 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 4638 | strbuf_release(&msg); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | static void run_diffstat(struct diff_filepair *p, struct diff_options *o, |
| 4642 | struct diffstat_t *diffstat) |
| 4643 | { |
| 4644 | const char *name; |
| 4645 | const char *other; |
| 4646 | |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4647 | if (!o->ignore_driver_algorithm) { |
| 4648 | struct userdiff_driver *drv = userdiff_find_by_path(o->repo->index, |
| 4649 | p->one->path); |
| 4650 | |
| 4651 | if (drv && drv->algorithm) |
| 4652 | set_diff_algorithm(o, drv->algorithm); |
| 4653 | } |
| 4654 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4655 | if (DIFF_PAIR_UNMERGED(p)) { |
| 4656 | /* unmerged */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4657 | builtin_diffstat(p->one->path, NULL, NULL, NULL, |
| 4658 | diffstat, o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4659 | return; |
| 4660 | } |
| 4661 | |
| 4662 | name = p->one->path; |
| 4663 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); |
| 4664 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4665 | if (o->prefix_length) |
| 4666 | strip_prefix(o->prefix_length, &name, &other); |
| 4667 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4668 | diff_fill_oid_info(p->one, o->repo->index); |
| 4669 | diff_fill_oid_info(p->two, o->repo->index); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4670 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4671 | builtin_diffstat(name, other, p->one, p->two, |
| 4672 | diffstat, o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4673 | } |
| 4674 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4675 | static void run_checkdiff(struct diff_filepair *p, struct diff_options *o) |
| 4676 | { |
| 4677 | const char *name; |
| 4678 | const char *other; |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4679 | const char *attr_path; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4680 | |
| 4681 | if (DIFF_PAIR_UNMERGED(p)) { |
| 4682 | /* unmerged */ |
| 4683 | return; |
| 4684 | } |
| 4685 | |
| 4686 | name = p->one->path; |
| 4687 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4688 | attr_path = other ? other : name; |
| 4689 | |
| 4690 | if (o->prefix_length) |
| 4691 | strip_prefix(o->prefix_length, &name, &other); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4692 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4693 | diff_fill_oid_info(p->one, o->repo->index); |
| 4694 | diff_fill_oid_info(p->two, o->repo->index); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4695 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4696 | builtin_checkdiff(name, other, attr_path, p->one, p->two, o); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4697 | } |
| 4698 | |
Nguyễn Thái Ngọc Duy | e675765 | 2018-09-21 17:57:24 +0200 | [diff] [blame] | 4699 | void repo_diff_setup(struct repository *r, struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4700 | { |
Johannes Schindelin | be4f2b4 | 2010-08-05 10:49:55 +0200 | [diff] [blame] | 4701 | memcpy(options, &default_diff_options, sizeof(*options)); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 4702 | |
| 4703 | options->file = stdout; |
Nguyễn Thái Ngọc Duy | e675765 | 2018-09-21 17:57:24 +0200 | [diff] [blame] | 4704 | options->repo = r; |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 4705 | |
Stefan Beller | 7648b79 | 2018-08-17 13:43:52 -0700 | [diff] [blame] | 4706 | options->output_indicators[OUTPUT_INDICATOR_NEW] = '+'; |
| 4707 | options->output_indicators[OUTPUT_INDICATOR_OLD] = '-'; |
| 4708 | options->output_indicators[OUTPUT_INDICATOR_CONTEXT] = ' '; |
Jack Bates | 43d1948 | 2016-12-06 09:56:14 -0700 | [diff] [blame] | 4709 | options->abbrev = DEFAULT_ABBREV; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4710 | options->line_termination = '\n'; |
| 4711 | options->break_opt = -1; |
| 4712 | options->rename_limit = -1; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 4713 | options->dirstat_permille = diff_dirstat_permille_default; |
Jeff Muizelaar | 6468a4e | 2012-09-27 15:12:52 -0400 | [diff] [blame] | 4714 | options->context = diff_context_default; |
Vegard Nossum | c488867 | 2017-01-12 13:21:11 +0100 | [diff] [blame] | 4715 | options->interhunkcontext = diff_interhunk_context_default; |
Junio C Hamano | a17505f | 2016-10-04 15:26:27 -0700 | [diff] [blame] | 4716 | options->ws_error_highlight = ws_error_highlight_default; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4717 | options->flags.rename_empty = 1; |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 4718 | options->flags.relative_name = diff_relative; |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 4719 | options->objfind = NULL; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4720 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 4721 | /* pathchange left =NULL by default */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4722 | options->change = diff_change; |
| 4723 | options->add_remove = diff_addremove; |
Jeff King | f1c9626 | 2011-08-17 22:03:12 -0700 | [diff] [blame] | 4724 | options->use_color = diff_use_color_default; |
Eric Wong | b68ea12 | 2006-07-07 04:01:23 -0700 | [diff] [blame] | 4725 | options->detect_rename = diff_detect_rename_default; |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 4726 | options->xdl_opts |= diff_algorithm; |
Michael Haggerty | 433860f | 2016-09-05 11:44:51 +0200 | [diff] [blame] | 4727 | if (diff_indent_heuristic) |
| 4728 | DIFF_XDL_SET(options, INDENT_HEURISTIC); |
Johannes Schindelin | eab9a40 | 2007-12-18 19:32:14 +0000 | [diff] [blame] | 4729 | |
Samuel Bronson | 6d8940b | 2013-12-18 19:08:12 -0500 | [diff] [blame] | 4730 | options->orderfile = diff_order_file_cfg; |
| 4731 | |
Sangeeta Jain | 8ef9312 | 2020-11-10 14:09:00 +0530 | [diff] [blame] | 4732 | if (!options->flags.ignore_submodule_set) |
| 4733 | options->flags.ignore_untracked_in_submodules = 1; |
| 4734 | |
Eli Collins | f89504d | 2010-05-02 19:03:41 -0700 | [diff] [blame] | 4735 | if (diff_no_prefix) { |
Jeff King | 6799aad | 2023-03-09 01:07:06 -0500 | [diff] [blame] | 4736 | diff_set_noprefix(options); |
Eli Collins | f89504d | 2010-05-02 19:03:41 -0700 | [diff] [blame] | 4737 | } else if (!diff_mnemonic_prefix) { |
Jeff King | 6799aad | 2023-03-09 01:07:06 -0500 | [diff] [blame] | 4738 | diff_set_default_prefix(options); |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 4739 | } |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 4740 | |
| 4741 | options->color_moved = diff_color_moved_default; |
Stefan Beller | 626c0b5 | 2018-07-18 12:31:56 -0700 | [diff] [blame] | 4742 | options->color_moved_ws_handling = diff_color_moved_ws_default; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4743 | } |
| 4744 | |
Johannes Schindelin | 4d4d4ea | 2022-01-28 12:02:49 +0000 | [diff] [blame] | 4745 | static const char diff_status_letters[] = { |
| 4746 | DIFF_STATUS_ADDED, |
| 4747 | DIFF_STATUS_COPIED, |
| 4748 | DIFF_STATUS_DELETED, |
| 4749 | DIFF_STATUS_MODIFIED, |
| 4750 | DIFF_STATUS_RENAMED, |
| 4751 | DIFF_STATUS_TYPE_CHANGED, |
| 4752 | DIFF_STATUS_UNKNOWN, |
| 4753 | DIFF_STATUS_UNMERGED, |
| 4754 | DIFF_STATUS_FILTER_AON, |
| 4755 | DIFF_STATUS_FILTER_BROKEN, |
| 4756 | '\0', |
| 4757 | }; |
| 4758 | |
| 4759 | static unsigned int filter_bit['Z' + 1]; |
| 4760 | |
| 4761 | static void prepare_filter_bits(void) |
| 4762 | { |
| 4763 | int i; |
| 4764 | |
| 4765 | if (!filter_bit[DIFF_STATUS_ADDED]) { |
| 4766 | for (i = 0; diff_status_letters[i]; i++) |
| 4767 | filter_bit[(int) diff_status_letters[i]] = (1 << i); |
| 4768 | } |
| 4769 | } |
| 4770 | |
| 4771 | static unsigned filter_bit_tst(char status, const struct diff_options *opt) |
| 4772 | { |
| 4773 | return opt->filter & filter_bit[(int) status]; |
| 4774 | } |
| 4775 | |
| 4776 | unsigned diff_filter_bit(char status) |
| 4777 | { |
| 4778 | prepare_filter_bits(); |
| 4779 | return filter_bit[(int) status]; |
| 4780 | } |
| 4781 | |
Jeff King | 9eac595 | 2023-06-01 13:41:06 -0400 | [diff] [blame] | 4782 | int diff_check_follow_pathspec(struct pathspec *ps, int die_on_error) |
| 4783 | { |
Jeff King | 8260bc5 | 2023-06-01 13:43:51 -0400 | [diff] [blame] | 4784 | unsigned forbidden_magic; |
| 4785 | |
Jeff King | 9eac595 | 2023-06-01 13:41:06 -0400 | [diff] [blame] | 4786 | if (ps->nr != 1) { |
| 4787 | if (die_on_error) |
| 4788 | die(_("--follow requires exactly one pathspec")); |
| 4789 | return 0; |
| 4790 | } |
Jeff King | 8260bc5 | 2023-06-01 13:43:51 -0400 | [diff] [blame] | 4791 | |
| 4792 | forbidden_magic = ps->items[0].magic & ~(PATHSPEC_FROMTOP | |
| 4793 | PATHSPEC_LITERAL); |
| 4794 | if (forbidden_magic) { |
| 4795 | if (die_on_error) { |
| 4796 | struct strbuf sb = STRBUF_INIT; |
| 4797 | pathspec_magic_names(forbidden_magic, &sb); |
| 4798 | die(_("pathspec magic not supported by --follow: %s"), |
| 4799 | sb.buf); |
| 4800 | } |
| 4801 | return 0; |
| 4802 | } |
| 4803 | |
Jeff King | 9eac595 | 2023-06-01 13:41:06 -0400 | [diff] [blame] | 4804 | return 1; |
| 4805 | } |
| 4806 | |
Thomas Rast | 2845265 | 2012-08-03 14:16:24 +0200 | [diff] [blame] | 4807 | void diff_setup_done(struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4808 | { |
Stefan Beller | 4d8c51a | 2018-01-04 14:50:44 -0800 | [diff] [blame] | 4809 | unsigned check_mask = DIFF_FORMAT_NAME | |
| 4810 | DIFF_FORMAT_NAME_STATUS | |
| 4811 | DIFF_FORMAT_CHECKDIFF | |
| 4812 | DIFF_FORMAT_NO_OUTPUT; |
brian m. carlson | de1d81d | 2018-05-02 00:25:52 +0000 | [diff] [blame] | 4813 | /* |
| 4814 | * This must be signed because we're comparing against a potentially |
| 4815 | * negative value. |
| 4816 | */ |
| 4817 | const int hexsz = the_hash_algo->hexsz; |
Timo Hirvonen | d7de00f | 2006-06-24 20:26:49 +0300 | [diff] [blame] | 4818 | |
Junio C Hamano | 6c37400 | 2013-05-10 17:10:11 +0200 | [diff] [blame] | 4819 | if (options->set_default) |
| 4820 | options->set_default(options); |
| 4821 | |
Stefan Beller | 4d8c51a | 2018-01-04 14:50:44 -0800 | [diff] [blame] | 4822 | if (HAS_MULTI_BITS(options->output_format & check_mask)) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 4823 | die(_("options '%s', '%s', '%s', and '%s' cannot be used together"), |
| 4824 | "--name-only", "--name-status", "--check", "-s"); |
Timo Hirvonen | d7de00f | 2006-06-24 20:26:49 +0300 | [diff] [blame] | 4825 | |
Stefan Beller | 5e50525 | 2018-01-04 14:50:43 -0800 | [diff] [blame] | 4826 | if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 4827 | die(_("options '%s', '%s', and '%s' cannot be used together"), |
| 4828 | "-G", "-S", "--find-object"); |
Stefan Beller | 5e50525 | 2018-01-04 14:50:43 -0800 | [diff] [blame] | 4829 | |
Ævar Arnfjörð Bjarmason | 188e9e2 | 2021-04-12 19:15:14 +0200 | [diff] [blame] | 4830 | if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_G_REGEX_MASK)) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 4831 | die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s'"), |
| 4832 | "-G", "--pickaxe-regex", "--pickaxe-regex", "-S"); |
Ævar Arnfjörð Bjarmason | 188e9e2 | 2021-04-12 19:15:14 +0200 | [diff] [blame] | 4833 | |
Ævar Arnfjörð Bjarmason | d26ec88 | 2021-04-12 19:15:15 +0200 | [diff] [blame] | 4834 | if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK)) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 4835 | die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s' and '%s'"), |
| 4836 | "--pickaxe-all", "--find-object", "--pickaxe-all", "-G", "-S"); |
Ævar Arnfjörð Bjarmason | d26ec88 | 2021-04-12 19:15:15 +0200 | [diff] [blame] | 4837 | |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 4838 | /* |
| 4839 | * Most of the time we can say "there are changes" |
| 4840 | * only by checking if there are changed paths, but |
| 4841 | * --ignore-whitespace* options force us to look |
Jim Meyering | 97bf2a0 | 2009-08-30 22:27:02 +0200 | [diff] [blame] | 4842 | * inside contents. |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 4843 | */ |
| 4844 | |
Junio C Hamano | 50f0439 | 2020-12-16 17:27:13 -0800 | [diff] [blame] | 4845 | if ((options->xdl_opts & XDF_WHITESPACE_FLAGS) || |
| 4846 | options->ignore_regex_nr) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4847 | options->flags.diff_from_contents = 1; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 4848 | else |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4849 | options->flags.diff_from_contents = 0; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 4850 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4851 | if (options->flags.find_copies_harder) |
Junio C Hamano | 03b9d56 | 2006-08-09 13:17:19 -0700 | [diff] [blame] | 4852 | options->detect_rename = DIFF_DETECT_COPY; |
| 4853 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4854 | if (!options->flags.relative_name) |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4855 | options->prefix = NULL; |
| 4856 | if (options->prefix) |
| 4857 | options->prefix_length = strlen(options->prefix); |
| 4858 | else |
| 4859 | options->prefix_length = 0; |
| 4860 | |
Junio C Hamano | a64f8b2 | 2023-08-18 16:59:32 -0700 | [diff] [blame] | 4861 | /* |
| 4862 | * --name-only, --name-status, --checkdiff, and -s |
| 4863 | * turn other output format off. |
| 4864 | */ |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4865 | if (options->output_format & (DIFF_FORMAT_NAME | |
| 4866 | DIFF_FORMAT_NAME_STATUS | |
| 4867 | DIFF_FORMAT_CHECKDIFF | |
| 4868 | DIFF_FORMAT_NO_OUTPUT)) |
| 4869 | options->output_format &= ~(DIFF_FORMAT_RAW | |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 4870 | DIFF_FORMAT_NUMSTAT | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4871 | DIFF_FORMAT_DIFFSTAT | |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 4872 | DIFF_FORMAT_SHORTSTAT | |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 4873 | DIFF_FORMAT_DIRSTAT | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4874 | DIFF_FORMAT_SUMMARY | |
| 4875 | DIFF_FORMAT_PATCH); |
| 4876 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4877 | /* |
| 4878 | * These cases always need recursive; we do not drop caller-supplied |
| 4879 | * recursive bits for other formats here. |
| 4880 | */ |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4881 | if (options->output_format & (DIFF_FORMAT_PATCH | |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 4882 | DIFF_FORMAT_NUMSTAT | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4883 | DIFF_FORMAT_DIFFSTAT | |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 4884 | DIFF_FORMAT_SHORTSTAT | |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 4885 | DIFF_FORMAT_DIRSTAT | |
Johannes Schindelin | d7014dc | 2006-10-03 23:09:56 +0200 | [diff] [blame] | 4886 | DIFF_FORMAT_SUMMARY | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4887 | DIFF_FORMAT_CHECKDIFF)) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4888 | options->flags.recursive = 1; |
Junio C Hamano | 5e36354 | 2006-05-22 00:31:02 -0700 | [diff] [blame] | 4889 | /* |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 4890 | * Also pickaxe would not work very well if you do not say recursive |
Junio C Hamano | 5e36354 | 2006-05-22 00:31:02 -0700 | [diff] [blame] | 4891 | */ |
Stefan Beller | cf63051 | 2018-01-04 14:50:41 -0800 | [diff] [blame] | 4892 | if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4893 | options->flags.recursive = 1; |
Jens Lehmann | ae6d5c1 | 2010-03-11 22:50:25 +0100 | [diff] [blame] | 4894 | /* |
| 4895 | * When patches are generated, submodules diffed against the work tree |
| 4896 | * must be checked for dirtiness too so it can be shown in the output |
| 4897 | */ |
| 4898 | if (options->output_format & DIFF_FORMAT_PATCH) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4899 | options->flags.dirty_submodules = 1; |
Junio C Hamano | 5e36354 | 2006-05-22 00:31:02 -0700 | [diff] [blame] | 4900 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4901 | if (options->detect_rename && options->rename_limit < 0) |
| 4902 | options->rename_limit = diff_rename_limit_default; |
brian m. carlson | de1d81d | 2018-05-02 00:25:52 +0000 | [diff] [blame] | 4903 | if (hexsz < options->abbrev) |
| 4904 | options->abbrev = hexsz; /* full */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4905 | |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 4906 | /* |
| 4907 | * It does not make sense to show the first hit we happened |
| 4908 | * to have found. It does not make sense not to return with |
| 4909 | * exit code in such a case either. |
| 4910 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4911 | if (options->flags.quick) { |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 4912 | options->output_format = DIFF_FORMAT_NO_OUTPUT; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4913 | options->flags.exit_with_status = 1; |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 4914 | } |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4915 | |
Jeff King | 0ea7d5b | 2013-12-16 15:02:21 -0500 | [diff] [blame] | 4916 | options->diff_path_counter = 0; |
Junio C Hamano | b0e2c99 | 2014-06-16 10:07:09 -0700 | [diff] [blame] | 4917 | |
Jeff King | 9eac595 | 2023-06-01 13:41:06 -0400 | [diff] [blame] | 4918 | if (options->flags.follow_renames) |
| 4919 | diff_check_follow_pathspec(&options->pathspec, 1); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 4920 | |
| 4921 | if (!options->use_color || external_diff()) |
| 4922 | options->color_moved = 0; |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 4923 | |
Johannes Schindelin | 75408ca | 2022-01-28 12:02:50 +0000 | [diff] [blame] | 4924 | if (options->filter_not) { |
| 4925 | if (!options->filter) |
| 4926 | options->filter = ~filter_bit[DIFF_STATUS_FILTER_AON]; |
| 4927 | options->filter &= ~options->filter_not; |
| 4928 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4929 | } |
| 4930 | |
Matthieu Moy | dea007f | 2010-08-05 10:22:52 +0200 | [diff] [blame] | 4931 | int parse_long_opt(const char *opt, const char **argv, |
| 4932 | const char **optarg) |
| 4933 | { |
| 4934 | const char *arg = argv[0]; |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 4935 | if (!skip_prefix(arg, "--", &arg)) |
Matthieu Moy | dea007f | 2010-08-05 10:22:52 +0200 | [diff] [blame] | 4936 | return 0; |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 4937 | if (!skip_prefix(arg, opt, &arg)) |
Matthieu Moy | dea007f | 2010-08-05 10:22:52 +0200 | [diff] [blame] | 4938 | return 0; |
Nicolas Vigier | b0d12fc | 2013-10-31 12:08:28 +0100 | [diff] [blame] | 4939 | if (*arg == '=') { /* stuck form: --option=value */ |
Matthieu Moy | dea007f | 2010-08-05 10:22:52 +0200 | [diff] [blame] | 4940 | *optarg = arg + 1; |
| 4941 | return 1; |
| 4942 | } |
| 4943 | if (*arg != '\0') |
| 4944 | return 0; |
| 4945 | /* separate form: --option value */ |
| 4946 | if (!argv[1]) |
| 4947 | die("Option '--%s' requires a value", opt); |
| 4948 | *optarg = argv[1]; |
| 4949 | return 2; |
| 4950 | } |
| 4951 | |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4952 | static int diff_opt_stat(const struct option *opt, const char *value, int unset) |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4953 | { |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4954 | struct diff_options *options = opt->value; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4955 | int width = options->stat_width; |
| 4956 | int name_width = options->stat_name_width; |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 4957 | int graph_width = options->stat_graph_width; |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 4958 | int count = options->stat_count; |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4959 | char *end; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4960 | |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4961 | BUG_ON_OPT_NEG(unset); |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4962 | |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4963 | if (!strcmp(opt->long_name, "stat")) { |
| 4964 | if (value) { |
| 4965 | width = strtoul(value, &end, 10); |
| 4966 | if (*end == ',') |
| 4967 | name_width = strtoul(end+1, &end, 10); |
| 4968 | if (*end == ',') |
| 4969 | count = strtoul(end+1, &end, 10); |
| 4970 | if (*end) |
| 4971 | return error(_("invalid --stat value: %s"), value); |
Matthieu Moy | 1e57208 | 2010-08-05 10:22:54 +0200 | [diff] [blame] | 4972 | } |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4973 | } else if (!strcmp(opt->long_name, "stat-width")) { |
| 4974 | width = strtoul(value, &end, 10); |
| 4975 | if (*end) |
| 4976 | return error(_("%s expects a numerical value"), |
| 4977 | opt->long_name); |
| 4978 | } else if (!strcmp(opt->long_name, "stat-name-width")) { |
| 4979 | name_width = strtoul(value, &end, 10); |
| 4980 | if (*end) |
| 4981 | return error(_("%s expects a numerical value"), |
| 4982 | opt->long_name); |
| 4983 | } else if (!strcmp(opt->long_name, "stat-graph-width")) { |
| 4984 | graph_width = strtoul(value, &end, 10); |
| 4985 | if (*end) |
| 4986 | return error(_("%s expects a numerical value"), |
| 4987 | opt->long_name); |
| 4988 | } else if (!strcmp(opt->long_name, "stat-count")) { |
| 4989 | count = strtoul(value, &end, 10); |
| 4990 | if (*end) |
| 4991 | return error(_("%s expects a numerical value"), |
| 4992 | opt->long_name); |
| 4993 | } else |
| 4994 | BUG("%s should not get here", opt->long_name); |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4995 | |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 4996 | options->output_format &= ~DIFF_FORMAT_NO_OUTPUT; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4997 | options->output_format |= DIFF_FORMAT_DIFFSTAT; |
| 4998 | options->stat_name_width = name_width; |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 4999 | options->stat_graph_width = graph_width; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 5000 | options->stat_width = width; |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 5001 | options->stat_count = count; |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 5002 | return 0; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 5003 | } |
| 5004 | |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 5005 | static int parse_dirstat_opt(struct diff_options *options, const char *params) |
| 5006 | { |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 5007 | struct strbuf errmsg = STRBUF_INIT; |
| 5008 | if (parse_dirstat_params(options, params, &errmsg)) |
Johan Herland | 7478ac5 | 2011-04-29 11:36:23 +0200 | [diff] [blame] | 5009 | die(_("Failed to parse --dirstat/-X option parameter:\n%s"), |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 5010 | errmsg.buf); |
| 5011 | strbuf_release(&errmsg); |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 5012 | /* |
| 5013 | * The caller knows a dirstat-related option is given from the command |
| 5014 | * line; allow it to say "return this_function();" |
| 5015 | */ |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5016 | options->output_format &= ~DIFF_FORMAT_NO_OUTPUT; |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 5017 | options->output_format |= DIFF_FORMAT_DIRSTAT; |
| 5018 | return 1; |
| 5019 | } |
| 5020 | |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 5021 | static int diff_opt_diff_filter(const struct option *option, |
| 5022 | const char *optarg, int unset) |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5023 | { |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 5024 | struct diff_options *opt = option->value; |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5025 | int i, optch; |
| 5026 | |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 5027 | BUG_ON_OPT_NEG(unset); |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5028 | prepare_filter_bits(); |
Junio C Hamano | 7f2ea5f | 2013-07-17 16:19:19 -0700 | [diff] [blame] | 5029 | |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5030 | for (i = 0; (optch = optarg[i]) != '\0'; i++) { |
| 5031 | unsigned int bit; |
Junio C Hamano | 7f2ea5f | 2013-07-17 16:19:19 -0700 | [diff] [blame] | 5032 | int negate; |
| 5033 | |
| 5034 | if ('a' <= optch && optch <= 'z') { |
| 5035 | negate = 1; |
| 5036 | optch = toupper(optch); |
| 5037 | } else { |
| 5038 | negate = 0; |
| 5039 | } |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5040 | |
| 5041 | bit = (0 <= optch && optch <= 'Z') ? filter_bit[optch] : 0; |
| 5042 | if (!bit) |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 5043 | return error(_("unknown change class '%c' in --diff-filter=%s"), |
| 5044 | optarg[i], optarg); |
Junio C Hamano | 7f2ea5f | 2013-07-17 16:19:19 -0700 | [diff] [blame] | 5045 | if (negate) |
Johannes Schindelin | 75408ca | 2022-01-28 12:02:50 +0000 | [diff] [blame] | 5046 | opt->filter_not |= bit; |
Junio C Hamano | 7f2ea5f | 2013-07-17 16:19:19 -0700 | [diff] [blame] | 5047 | else |
| 5048 | opt->filter |= bit; |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5049 | } |
| 5050 | return 0; |
| 5051 | } |
| 5052 | |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 5053 | static void enable_patch_output(int *fmt) |
| 5054 | { |
Matthieu Moy | 71482d3 | 2013-07-16 10:05:37 +0200 | [diff] [blame] | 5055 | *fmt &= ~DIFF_FORMAT_NO_OUTPUT; |
| 5056 | *fmt |= DIFF_FORMAT_PATCH; |
| 5057 | } |
| 5058 | |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 5059 | static int diff_opt_ws_error_highlight(const struct option *option, |
| 5060 | const char *arg, int unset) |
Junio C Hamano | b8767f7 | 2015-05-26 10:11:28 -0700 | [diff] [blame] | 5061 | { |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 5062 | struct diff_options *opt = option->value; |
Junio C Hamano | 077965f | 2016-10-04 15:03:33 -0700 | [diff] [blame] | 5063 | int val = parse_ws_error_highlight(arg); |
Junio C Hamano | b8767f7 | 2015-05-26 10:11:28 -0700 | [diff] [blame] | 5064 | |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 5065 | BUG_ON_OPT_NEG(unset); |
| 5066 | if (val < 0) |
| 5067 | return error(_("unknown value after ws-error-highlight=%.*s"), |
| 5068 | -1 - val, arg); |
Junio C Hamano | b8767f7 | 2015-05-26 10:11:28 -0700 | [diff] [blame] | 5069 | opt->ws_error_highlight = val; |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 5070 | return 0; |
Junio C Hamano | b8767f7 | 2015-05-26 10:11:28 -0700 | [diff] [blame] | 5071 | } |
| 5072 | |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5073 | static int diff_opt_find_object(const struct option *option, |
| 5074 | const char *arg, int unset) |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 5075 | { |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5076 | struct diff_options *opt = option->value; |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 5077 | struct object_id oid; |
| 5078 | |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5079 | BUG_ON_OPT_NEG(unset); |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 5080 | if (repo_get_oid(the_repository, arg, &oid)) |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5081 | return error(_("unable to resolve '%s'"), arg); |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 5082 | |
| 5083 | if (!opt->objfind) |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 5084 | CALLOC_ARRAY(opt->objfind, 1); |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 5085 | |
| 5086 | opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND; |
| 5087 | opt->flags.recursive = 1; |
| 5088 | opt->flags.tree_in_recursive = 1; |
| 5089 | oidset_insert(opt->objfind, &oid); |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5090 | return 0; |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 5091 | } |
| 5092 | |
Nguyễn Thái Ngọc Duy | df84a43 | 2019-03-05 19:30:11 +0700 | [diff] [blame] | 5093 | static int diff_opt_anchored(const struct option *opt, |
| 5094 | const char *arg, int unset) |
| 5095 | { |
| 5096 | struct diff_options *options = opt->value; |
| 5097 | |
| 5098 | BUG_ON_OPT_NEG(unset); |
| 5099 | options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF); |
| 5100 | ALLOC_GROW(options->anchors, options->anchors_nr + 1, |
| 5101 | options->anchors_alloc); |
| 5102 | options->anchors[options->anchors_nr++] = xstrdup(arg); |
| 5103 | return 0; |
| 5104 | } |
| 5105 | |
Nguyễn Thái Ngọc Duy | 6d9af6f | 2019-03-05 19:30:12 +0700 | [diff] [blame] | 5106 | static int diff_opt_binary(const struct option *opt, |
| 5107 | const char *arg, int unset) |
| 5108 | { |
| 5109 | struct diff_options *options = opt->value; |
| 5110 | |
| 5111 | BUG_ON_OPT_NEG(unset); |
| 5112 | BUG_ON_OPT_ARG(arg); |
| 5113 | enable_patch_output(&options->output_format); |
| 5114 | options->flags.binary = 1; |
| 5115 | return 0; |
| 5116 | } |
| 5117 | |
Nguyễn Thái Ngọc Duy | ced4e17 | 2019-02-21 18:16:13 +0700 | [diff] [blame] | 5118 | static int diff_opt_break_rewrites(const struct option *opt, |
| 5119 | const char *arg, int unset) |
| 5120 | { |
| 5121 | int *break_opt = opt->value; |
| 5122 | int opt1, opt2; |
| 5123 | |
| 5124 | BUG_ON_OPT_NEG(unset); |
| 5125 | if (!arg) |
| 5126 | arg = ""; |
| 5127 | opt1 = parse_rename_score(&arg); |
| 5128 | if (*arg == 0) |
| 5129 | opt2 = 0; |
| 5130 | else if (*arg != '/') |
| 5131 | return error(_("%s expects <n>/<m> form"), opt->long_name); |
| 5132 | else { |
| 5133 | arg++; |
| 5134 | opt2 = parse_rename_score(&arg); |
| 5135 | } |
| 5136 | if (*arg != 0) |
| 5137 | return error(_("%s expects <n>/<m> form"), opt->long_name); |
| 5138 | *break_opt = opt1 | (opt2 << 16); |
| 5139 | return 0; |
| 5140 | } |
| 5141 | |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5142 | static int diff_opt_char(const struct option *opt, |
| 5143 | const char *arg, int unset) |
| 5144 | { |
| 5145 | char *value = opt->value; |
| 5146 | |
| 5147 | BUG_ON_OPT_NEG(unset); |
| 5148 | if (arg[1]) |
| 5149 | return error(_("%s expects a character, got '%s'"), |
| 5150 | opt->long_name, arg); |
| 5151 | *value = arg[0]; |
| 5152 | return 0; |
| 5153 | } |
| 5154 | |
Nguyễn Thái Ngọc Duy | 59311a9 | 2019-03-24 15:20:09 +0700 | [diff] [blame] | 5155 | static int diff_opt_color_moved(const struct option *opt, |
| 5156 | const char *arg, int unset) |
| 5157 | { |
| 5158 | struct diff_options *options = opt->value; |
| 5159 | |
| 5160 | if (unset) { |
| 5161 | options->color_moved = COLOR_MOVED_NO; |
| 5162 | } else if (!arg) { |
| 5163 | if (diff_color_moved_default) |
| 5164 | options->color_moved = diff_color_moved_default; |
| 5165 | if (options->color_moved == COLOR_MOVED_NO) |
| 5166 | options->color_moved = COLOR_MOVED_DEFAULT; |
| 5167 | } else { |
| 5168 | int cm = parse_color_moved(arg); |
| 5169 | if (cm < 0) |
| 5170 | return error(_("bad --color-moved argument: %s"), arg); |
| 5171 | options->color_moved = cm; |
| 5172 | } |
| 5173 | return 0; |
| 5174 | } |
| 5175 | |
Nguyễn Thái Ngọc Duy | 8ce2020 | 2019-03-24 15:20:10 +0700 | [diff] [blame] | 5176 | static int diff_opt_color_moved_ws(const struct option *opt, |
| 5177 | const char *arg, int unset) |
| 5178 | { |
| 5179 | struct diff_options *options = opt->value; |
| 5180 | unsigned cm; |
| 5181 | |
Nguyễn Thái Ngọc Duy | bb98729 | 2019-03-24 15:20:11 +0700 | [diff] [blame] | 5182 | if (unset) { |
| 5183 | options->color_moved_ws_handling = 0; |
| 5184 | return 0; |
| 5185 | } |
| 5186 | |
Nguyễn Thái Ngọc Duy | 8ce2020 | 2019-03-24 15:20:10 +0700 | [diff] [blame] | 5187 | cm = parse_color_moved_ws(arg); |
| 5188 | if (cm & COLOR_MOVED_WS_ERROR) |
| 5189 | return error(_("invalid mode '%s' in --color-moved-ws"), arg); |
| 5190 | options->color_moved_ws_handling = cm; |
| 5191 | return 0; |
| 5192 | } |
| 5193 | |
Nguyễn Thái Ngọc Duy | 212db69 | 2019-03-05 19:30:20 +0700 | [diff] [blame] | 5194 | static int diff_opt_color_words(const struct option *opt, |
| 5195 | const char *arg, int unset) |
| 5196 | { |
| 5197 | struct diff_options *options = opt->value; |
| 5198 | |
| 5199 | BUG_ON_OPT_NEG(unset); |
| 5200 | options->use_color = 1; |
| 5201 | options->word_diff = DIFF_WORDS_COLOR; |
| 5202 | options->word_regex = arg; |
| 5203 | return 0; |
| 5204 | } |
| 5205 | |
Nguyễn Thái Ngọc Duy | 7d7942b | 2019-02-21 18:16:11 +0700 | [diff] [blame] | 5206 | static int diff_opt_compact_summary(const struct option *opt, |
| 5207 | const char *arg, int unset) |
| 5208 | { |
| 5209 | struct diff_options *options = opt->value; |
| 5210 | |
| 5211 | BUG_ON_OPT_ARG(arg); |
| 5212 | if (unset) { |
| 5213 | options->flags.stat_with_summary = 0; |
| 5214 | } else { |
| 5215 | options->flags.stat_with_summary = 1; |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5216 | options->output_format &= ~DIFF_FORMAT_NO_OUTPUT; |
Nguyễn Thái Ngọc Duy | 7d7942b | 2019-02-21 18:16:11 +0700 | [diff] [blame] | 5217 | options->output_format |= DIFF_FORMAT_DIFFSTAT; |
| 5218 | } |
| 5219 | return 0; |
| 5220 | } |
| 5221 | |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5222 | static int diff_opt_diff_algorithm(const struct option *opt, |
| 5223 | const char *arg, int unset) |
| 5224 | { |
| 5225 | struct diff_options *options = opt->value; |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5226 | |
| 5227 | BUG_ON_OPT_NEG(unset); |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 5228 | |
| 5229 | if (set_diff_algorithm(options, arg)) |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5230 | return error(_("option diff-algorithm accepts \"myers\", " |
| 5231 | "\"minimal\", \"patience\" and \"histogram\"")); |
| 5232 | |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 5233 | options->ignore_driver_algorithm = 1; |
| 5234 | |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 5235 | return 0; |
| 5236 | } |
| 5237 | |
| 5238 | static int diff_opt_diff_algorithm_no_arg(const struct option *opt, |
| 5239 | const char *arg, int unset) |
| 5240 | { |
| 5241 | struct diff_options *options = opt->value; |
| 5242 | |
| 5243 | BUG_ON_OPT_NEG(unset); |
| 5244 | BUG_ON_OPT_ARG(arg); |
| 5245 | |
| 5246 | if (set_diff_algorithm(options, opt->long_name)) |
| 5247 | BUG("available diff algorithms include \"myers\", " |
| 5248 | "\"minimal\", \"patience\" and \"histogram\""); |
| 5249 | |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 5250 | options->ignore_driver_algorithm = 1; |
| 5251 | |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5252 | return 0; |
| 5253 | } |
| 5254 | |
Nguyễn Thái Ngọc Duy | 4ce7aab | 2019-02-21 18:16:03 +0700 | [diff] [blame] | 5255 | static int diff_opt_dirstat(const struct option *opt, |
| 5256 | const char *arg, int unset) |
| 5257 | { |
| 5258 | struct diff_options *options = opt->value; |
| 5259 | |
| 5260 | BUG_ON_OPT_NEG(unset); |
| 5261 | if (!strcmp(opt->long_name, "cumulative")) { |
| 5262 | if (arg) |
| 5263 | BUG("how come --cumulative take a value?"); |
| 5264 | arg = "cumulative"; |
| 5265 | } else if (!strcmp(opt->long_name, "dirstat-by-file")) |
| 5266 | parse_dirstat_opt(options, "files"); |
| 5267 | parse_dirstat_opt(options, arg ? arg : ""); |
| 5268 | return 0; |
| 5269 | } |
| 5270 | |
Nguyễn Thái Ngọc Duy | 7f64850 | 2019-02-21 18:16:16 +0700 | [diff] [blame] | 5271 | static int diff_opt_find_copies(const struct option *opt, |
| 5272 | const char *arg, int unset) |
| 5273 | { |
| 5274 | struct diff_options *options = opt->value; |
| 5275 | |
| 5276 | BUG_ON_OPT_NEG(unset); |
| 5277 | if (!arg) |
| 5278 | arg = ""; |
| 5279 | options->rename_score = parse_rename_score(&arg); |
| 5280 | if (*arg != 0) |
| 5281 | return error(_("invalid argument to %s"), opt->long_name); |
| 5282 | |
| 5283 | if (options->detect_rename == DIFF_DETECT_COPY) |
| 5284 | options->flags.find_copies_harder = 1; |
| 5285 | else |
| 5286 | options->detect_rename = DIFF_DETECT_COPY; |
| 5287 | |
| 5288 | return 0; |
| 5289 | } |
| 5290 | |
Nguyễn Thái Ngọc Duy | f476308 | 2019-02-21 18:16:14 +0700 | [diff] [blame] | 5291 | static int diff_opt_find_renames(const struct option *opt, |
| 5292 | const char *arg, int unset) |
| 5293 | { |
| 5294 | struct diff_options *options = opt->value; |
| 5295 | |
| 5296 | BUG_ON_OPT_NEG(unset); |
| 5297 | if (!arg) |
| 5298 | arg = ""; |
| 5299 | options->rename_score = parse_rename_score(&arg); |
| 5300 | if (*arg != 0) |
| 5301 | return error(_("invalid argument to %s"), opt->long_name); |
| 5302 | |
| 5303 | options->detect_rename = DIFF_DETECT_RENAME; |
| 5304 | return 0; |
| 5305 | } |
| 5306 | |
Nguyễn Thái Ngọc Duy | 1e9250b | 2019-03-05 19:30:16 +0700 | [diff] [blame] | 5307 | static int diff_opt_follow(const struct option *opt, |
| 5308 | const char *arg, int unset) |
| 5309 | { |
| 5310 | struct diff_options *options = opt->value; |
| 5311 | |
| 5312 | BUG_ON_OPT_ARG(arg); |
| 5313 | if (unset) { |
| 5314 | options->flags.follow_renames = 0; |
| 5315 | options->flags.default_follow_renames = 0; |
| 5316 | } else { |
| 5317 | options->flags.follow_renames = 1; |
| 5318 | } |
| 5319 | return 0; |
| 5320 | } |
| 5321 | |
Nguyễn Thái Ngọc Duy | b680ee1 | 2019-03-05 19:30:25 +0700 | [diff] [blame] | 5322 | static int diff_opt_ignore_submodules(const struct option *opt, |
| 5323 | const char *arg, int unset) |
| 5324 | { |
| 5325 | struct diff_options *options = opt->value; |
| 5326 | |
| 5327 | BUG_ON_OPT_NEG(unset); |
| 5328 | if (!arg) |
| 5329 | arg = "all"; |
| 5330 | options->flags.override_submodule_config = 1; |
| 5331 | handle_ignore_submodules_arg(options, arg); |
| 5332 | return 0; |
| 5333 | } |
| 5334 | |
Nguyễn Thái Ngọc Duy | 2f81cf9 | 2019-03-24 15:20:06 +0700 | [diff] [blame] | 5335 | static int diff_opt_line_prefix(const struct option *opt, |
| 5336 | const char *optarg, int unset) |
| 5337 | { |
| 5338 | struct diff_options *options = opt->value; |
| 5339 | |
| 5340 | BUG_ON_OPT_NEG(unset); |
| 5341 | options->line_prefix = optarg; |
| 5342 | options->line_prefix_length = strlen(options->line_prefix); |
| 5343 | graph_setup_line_prefix(options); |
| 5344 | return 0; |
| 5345 | } |
| 5346 | |
Nguyễn Thái Ngọc Duy | 11c659d | 2019-03-24 15:20:07 +0700 | [diff] [blame] | 5347 | static int diff_opt_no_prefix(const struct option *opt, |
| 5348 | const char *optarg, int unset) |
| 5349 | { |
| 5350 | struct diff_options *options = opt->value; |
| 5351 | |
| 5352 | BUG_ON_OPT_NEG(unset); |
| 5353 | BUG_ON_OPT_ARG(optarg); |
Jeff King | 6799aad | 2023-03-09 01:07:06 -0500 | [diff] [blame] | 5354 | diff_set_noprefix(options); |
Nguyễn Thái Ngọc Duy | 11c659d | 2019-03-24 15:20:07 +0700 | [diff] [blame] | 5355 | return 0; |
| 5356 | } |
| 5357 | |
Jeff King | b39a569 | 2023-03-09 01:09:54 -0500 | [diff] [blame] | 5358 | static int diff_opt_default_prefix(const struct option *opt, |
| 5359 | const char *optarg, int unset) |
| 5360 | { |
| 5361 | struct diff_options *options = opt->value; |
| 5362 | |
| 5363 | BUG_ON_OPT_NEG(unset); |
| 5364 | BUG_ON_OPT_ARG(optarg); |
| 5365 | diff_set_default_prefix(options); |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 5366 | return 0; |
| 5367 | } |
| 5368 | |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5369 | static enum parse_opt_result diff_opt_output(struct parse_opt_ctx_t *ctx, |
| 5370 | const struct option *opt, |
| 5371 | const char *arg, int unset) |
| 5372 | { |
| 5373 | struct diff_options *options = opt->value; |
| 5374 | char *path; |
| 5375 | |
| 5376 | BUG_ON_OPT_NEG(unset); |
| 5377 | path = prefix_filename(ctx->prefix, arg); |
| 5378 | options->file = xfopen(path, "w"); |
| 5379 | options->close_file = 1; |
| 5380 | if (options->use_color != GIT_COLOR_ALWAYS) |
| 5381 | options->use_color = GIT_COLOR_NEVER; |
| 5382 | free(path); |
| 5383 | return 0; |
| 5384 | } |
| 5385 | |
Nguyễn Thái Ngọc Duy | 31fd640 | 2019-03-05 19:30:08 +0700 | [diff] [blame] | 5386 | static int diff_opt_patience(const struct option *opt, |
| 5387 | const char *arg, int unset) |
| 5388 | { |
| 5389 | struct diff_options *options = opt->value; |
| 5390 | int i; |
| 5391 | |
| 5392 | BUG_ON_OPT_NEG(unset); |
| 5393 | BUG_ON_OPT_ARG(arg); |
Nguyễn Thái Ngọc Duy | 31fd640 | 2019-03-05 19:30:08 +0700 | [diff] [blame] | 5394 | /* |
| 5395 | * Both --patience and --anchored use PATIENCE_DIFF |
| 5396 | * internally, so remove any anchors previously |
| 5397 | * specified. |
| 5398 | */ |
| 5399 | for (i = 0; i < options->anchors_nr; i++) |
| 5400 | free(options->anchors[i]); |
| 5401 | options->anchors_nr = 0; |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 5402 | options->ignore_driver_algorithm = 1; |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 5403 | |
| 5404 | return set_diff_algorithm(options, "patience"); |
Nguyễn Thái Ngọc Duy | 31fd640 | 2019-03-05 19:30:08 +0700 | [diff] [blame] | 5405 | } |
| 5406 | |
Michał Kępień | 296d4a9 | 2020-10-20 08:48:09 +0200 | [diff] [blame] | 5407 | static int diff_opt_ignore_regex(const struct option *opt, |
| 5408 | const char *arg, int unset) |
| 5409 | { |
| 5410 | struct diff_options *options = opt->value; |
| 5411 | regex_t *regex; |
| 5412 | |
| 5413 | BUG_ON_OPT_NEG(unset); |
| 5414 | regex = xmalloc(sizeof(*regex)); |
| 5415 | if (regcomp(regex, arg, REG_EXTENDED | REG_NEWLINE)) |
| 5416 | return error(_("invalid regex given to -I: '%s'"), arg); |
| 5417 | ALLOC_GROW(options->ignore_regex, options->ignore_regex_nr + 1, |
| 5418 | options->ignore_regex_alloc); |
| 5419 | options->ignore_regex[options->ignore_regex_nr++] = regex; |
| 5420 | return 0; |
| 5421 | } |
| 5422 | |
Nguyễn Thái Ngọc Duy | a41cfb3 | 2019-03-24 15:19:59 +0700 | [diff] [blame] | 5423 | static int diff_opt_pickaxe_regex(const struct option *opt, |
| 5424 | const char *arg, int unset) |
| 5425 | { |
| 5426 | struct diff_options *options = opt->value; |
| 5427 | |
| 5428 | BUG_ON_OPT_NEG(unset); |
| 5429 | options->pickaxe = arg; |
| 5430 | options->pickaxe_opts |= DIFF_PICKAXE_KIND_G; |
| 5431 | return 0; |
| 5432 | } |
| 5433 | |
| 5434 | static int diff_opt_pickaxe_string(const struct option *opt, |
| 5435 | const char *arg, int unset) |
| 5436 | { |
| 5437 | struct diff_options *options = opt->value; |
| 5438 | |
| 5439 | BUG_ON_OPT_NEG(unset); |
| 5440 | options->pickaxe = arg; |
| 5441 | options->pickaxe_opts |= DIFF_PICKAXE_KIND_S; |
| 5442 | return 0; |
| 5443 | } |
| 5444 | |
Nguyễn Thái Ngọc Duy | 0b1c5b5 | 2019-02-21 18:16:19 +0700 | [diff] [blame] | 5445 | static int diff_opt_relative(const struct option *opt, |
| 5446 | const char *arg, int unset) |
| 5447 | { |
| 5448 | struct diff_options *options = opt->value; |
| 5449 | |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 5450 | options->flags.relative_name = !unset; |
Nguyễn Thái Ngọc Duy | 0b1c5b5 | 2019-02-21 18:16:19 +0700 | [diff] [blame] | 5451 | if (arg) |
| 5452 | options->prefix = arg; |
| 5453 | return 0; |
| 5454 | } |
| 5455 | |
Nguyễn Thái Ngọc Duy | 125dcea | 2019-03-05 19:30:26 +0700 | [diff] [blame] | 5456 | static int diff_opt_submodule(const struct option *opt, |
| 5457 | const char *arg, int unset) |
| 5458 | { |
| 5459 | struct diff_options *options = opt->value; |
| 5460 | |
| 5461 | BUG_ON_OPT_NEG(unset); |
| 5462 | if (!arg) |
| 5463 | arg = "log"; |
| 5464 | if (parse_submodule_params(options, arg)) |
| 5465 | return error(_("failed to parse --submodule option parameter: '%s'"), |
| 5466 | arg); |
| 5467 | return 0; |
| 5468 | } |
| 5469 | |
Nguyễn Thái Ngọc Duy | 8ab7697 | 2019-03-05 19:30:24 +0700 | [diff] [blame] | 5470 | static int diff_opt_textconv(const struct option *opt, |
| 5471 | const char *arg, int unset) |
| 5472 | { |
| 5473 | struct diff_options *options = opt->value; |
| 5474 | |
| 5475 | BUG_ON_OPT_ARG(arg); |
| 5476 | if (unset) { |
| 5477 | options->flags.allow_textconv = 0; |
| 5478 | } else { |
| 5479 | options->flags.allow_textconv = 1; |
| 5480 | options->flags.textconv_set_via_cmdline = 1; |
| 5481 | } |
| 5482 | return 0; |
| 5483 | } |
| 5484 | |
Nguyễn Thái Ngọc Duy | d473e2e | 2019-01-27 07:35:33 +0700 | [diff] [blame] | 5485 | static int diff_opt_unified(const struct option *opt, |
| 5486 | const char *arg, int unset) |
| 5487 | { |
| 5488 | struct diff_options *options = opt->value; |
| 5489 | char *s; |
| 5490 | |
| 5491 | BUG_ON_OPT_NEG(unset); |
| 5492 | |
Nguyễn Thái Ngọc Duy | 8ef0519 | 2019-05-29 16:11:15 +0700 | [diff] [blame] | 5493 | if (arg) { |
| 5494 | options->context = strtol(arg, &s, 10); |
| 5495 | if (*s) |
| 5496 | return error(_("%s expects a numerical value"), "--unified"); |
| 5497 | } |
Nguyễn Thái Ngọc Duy | d473e2e | 2019-01-27 07:35:33 +0700 | [diff] [blame] | 5498 | enable_patch_output(&options->output_format); |
| 5499 | |
| 5500 | return 0; |
| 5501 | } |
| 5502 | |
Nguyễn Thái Ngọc Duy | e9fb39b | 2019-03-05 19:30:18 +0700 | [diff] [blame] | 5503 | static int diff_opt_word_diff(const struct option *opt, |
| 5504 | const char *arg, int unset) |
| 5505 | { |
| 5506 | struct diff_options *options = opt->value; |
| 5507 | |
| 5508 | BUG_ON_OPT_NEG(unset); |
| 5509 | if (arg) { |
| 5510 | if (!strcmp(arg, "plain")) |
| 5511 | options->word_diff = DIFF_WORDS_PLAIN; |
| 5512 | else if (!strcmp(arg, "color")) { |
| 5513 | options->use_color = 1; |
| 5514 | options->word_diff = DIFF_WORDS_COLOR; |
| 5515 | } |
| 5516 | else if (!strcmp(arg, "porcelain")) |
| 5517 | options->word_diff = DIFF_WORDS_PORCELAIN; |
| 5518 | else if (!strcmp(arg, "none")) |
| 5519 | options->word_diff = DIFF_WORDS_NONE; |
| 5520 | else |
| 5521 | return error(_("bad --word-diff argument: %s"), arg); |
| 5522 | } else { |
| 5523 | if (options->word_diff == DIFF_WORDS_NONE) |
| 5524 | options->word_diff = DIFF_WORDS_PLAIN; |
| 5525 | } |
| 5526 | return 0; |
| 5527 | } |
| 5528 | |
Nguyễn Thái Ngọc Duy | 797df11 | 2019-03-05 19:30:19 +0700 | [diff] [blame] | 5529 | static int diff_opt_word_diff_regex(const struct option *opt, |
| 5530 | const char *arg, int unset) |
| 5531 | { |
| 5532 | struct diff_options *options = opt->value; |
| 5533 | |
| 5534 | BUG_ON_OPT_NEG(unset); |
| 5535 | if (options->word_diff == DIFF_WORDS_NONE) |
| 5536 | options->word_diff = DIFF_WORDS_PLAIN; |
| 5537 | options->word_regex = arg; |
| 5538 | return 0; |
| 5539 | } |
| 5540 | |
Junio C Hamano | 1eb4136 | 2021-02-11 11:57:50 -0800 | [diff] [blame] | 5541 | static int diff_opt_rotate_to(const struct option *opt, const char *arg, int unset) |
| 5542 | { |
| 5543 | struct diff_options *options = opt->value; |
| 5544 | |
| 5545 | BUG_ON_OPT_NEG(unset); |
| 5546 | if (!strcmp(opt->long_name, "skip-to")) |
| 5547 | options->skip_instead_of_rotate = 1; |
| 5548 | else |
| 5549 | options->skip_instead_of_rotate = 0; |
| 5550 | options->rotate_to = arg; |
| 5551 | return 0; |
| 5552 | } |
| 5553 | |
Philippe Blain | 0a86803 | 2023-06-26 16:24:48 +0000 | [diff] [blame] | 5554 | /* |
| 5555 | * Consider adding new flags to __git_diff_common_options |
| 5556 | * in contrib/completion/git-completion.bash |
| 5557 | */ |
René Scharfe | c5630c4 | 2022-12-01 23:49:11 +0100 | [diff] [blame] | 5558 | struct option *add_diff_options(const struct option *opts, |
| 5559 | struct diff_options *options) |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5560 | { |
| 5561 | struct option parseopts[] = { |
Nguyễn Thái Ngọc Duy | cc013c2 | 2019-01-27 07:35:32 +0700 | [diff] [blame] | 5562 | OPT_GROUP(N_("Diff output format options")), |
| 5563 | OPT_BITOP('p', "patch", &options->output_format, |
| 5564 | N_("generate patch"), |
| 5565 | DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5566 | OPT_SET_INT('s', "no-patch", &options->output_format, |
| 5567 | N_("suppress diff output"), DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | cc013c2 | 2019-01-27 07:35:32 +0700 | [diff] [blame] | 5568 | OPT_BITOP('u', NULL, &options->output_format, |
| 5569 | N_("generate patch"), |
| 5570 | DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | d473e2e | 2019-01-27 07:35:33 +0700 | [diff] [blame] | 5571 | OPT_CALLBACK_F('U', "unified", options, N_("<n>"), |
| 5572 | N_("generate diffs with <n> lines context"), |
Junio C Hamano | 20aa7c5 | 2019-05-30 10:50:44 -0700 | [diff] [blame] | 5573 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_unified), |
Nguyễn Thái Ngọc Duy | 7fd9a1b | 2019-01-27 07:35:34 +0700 | [diff] [blame] | 5574 | OPT_BOOL('W', "function-context", &options->flags.funccontext, |
| 5575 | N_("generate diffs with <n> lines context")), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5576 | OPT_BITOP(0, "raw", &options->output_format, |
Nguyễn Thái Ngọc Duy | ed88148 | 2019-01-27 07:35:35 +0700 | [diff] [blame] | 5577 | N_("generate the diff in raw format"), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5578 | DIFF_FORMAT_RAW, DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | c659f30 | 2019-02-16 18:36:35 +0700 | [diff] [blame] | 5579 | OPT_BITOP(0, "patch-with-raw", &options->output_format, |
| 5580 | N_("synonym for '-p --raw'"), |
| 5581 | DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW, |
| 5582 | DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | e550f58 | 2019-02-21 18:16:06 +0700 | [diff] [blame] | 5583 | OPT_BITOP(0, "patch-with-stat", &options->output_format, |
| 5584 | N_("synonym for '-p --stat'"), |
| 5585 | DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT, |
| 5586 | DIFF_FORMAT_NO_OUTPUT), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5587 | OPT_BITOP(0, "numstat", &options->output_format, |
Nguyễn Thái Ngọc Duy | e567362 | 2019-02-16 18:36:36 +0700 | [diff] [blame] | 5588 | N_("machine friendly --stat"), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5589 | DIFF_FORMAT_NUMSTAT, DIFF_FORMAT_NO_OUTPUT), |
| 5590 | OPT_BITOP(0, "shortstat", &options->output_format, |
Nguyễn Thái Ngọc Duy | e567362 | 2019-02-16 18:36:36 +0700 | [diff] [blame] | 5591 | N_("output only the last line of --stat"), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5592 | DIFF_FORMAT_SHORTSTAT, DIFF_FORMAT_NO_OUTPUT), |
Jiang Xin | 5e7013a | 2024-02-14 16:46:41 +0800 | [diff] [blame] | 5593 | OPT_CALLBACK_F('X', "dirstat", options, N_("<param1>,<param2>..."), |
Nguyễn Thái Ngọc Duy | 4ce7aab | 2019-02-21 18:16:03 +0700 | [diff] [blame] | 5594 | N_("output the distribution of relative amount of changes for each sub-directory"), |
| 5595 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5596 | diff_opt_dirstat), |
| 5597 | OPT_CALLBACK_F(0, "cumulative", options, NULL, |
| 5598 | N_("synonym for --dirstat=cumulative"), |
| 5599 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, |
| 5600 | diff_opt_dirstat), |
Jiang Xin | 5e7013a | 2024-02-14 16:46:41 +0800 | [diff] [blame] | 5601 | OPT_CALLBACK_F(0, "dirstat-by-file", options, N_("<param1>,<param2>..."), |
| 5602 | N_("synonym for --dirstat=files,<param1>,<param2>..."), |
Nguyễn Thái Ngọc Duy | 4ce7aab | 2019-02-21 18:16:03 +0700 | [diff] [blame] | 5603 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5604 | diff_opt_dirstat), |
Nguyễn Thái Ngọc Duy | fc6af3e | 2019-02-21 18:16:04 +0700 | [diff] [blame] | 5605 | OPT_BIT_F(0, "check", &options->output_format, |
| 5606 | N_("warn if changes introduce conflict markers or whitespace errors"), |
| 5607 | DIFF_FORMAT_CHECKDIFF, PARSE_OPT_NONEG), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5608 | OPT_BITOP(0, "summary", &options->output_format, |
Nguyễn Thái Ngọc Duy | 70a3041 | 2019-02-21 18:16:05 +0700 | [diff] [blame] | 5609 | N_("condensed summary such as creations, renames and mode changes"), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5610 | DIFF_FORMAT_SUMMARY, DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | 0e840e2 | 2019-02-21 18:16:07 +0700 | [diff] [blame] | 5611 | OPT_BIT_F(0, "name-only", &options->output_format, |
| 5612 | N_("show only names of changed files"), |
| 5613 | DIFF_FORMAT_NAME, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | a238747 | 2019-02-21 18:16:08 +0700 | [diff] [blame] | 5614 | OPT_BIT_F(0, "name-status", &options->output_format, |
| 5615 | N_("show only names and status of changed files"), |
| 5616 | DIFF_FORMAT_NAME_STATUS, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 5617 | OPT_CALLBACK_F(0, "stat", options, N_("<width>[,<name-width>[,<count>]]"), |
| 5618 | N_("generate diffstat"), |
| 5619 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_stat), |
| 5620 | OPT_CALLBACK_F(0, "stat-width", options, N_("<width>"), |
| 5621 | N_("generate diffstat with a given width"), |
| 5622 | PARSE_OPT_NONEG, diff_opt_stat), |
| 5623 | OPT_CALLBACK_F(0, "stat-name-width", options, N_("<width>"), |
| 5624 | N_("generate diffstat with a given name width"), |
| 5625 | PARSE_OPT_NONEG, diff_opt_stat), |
| 5626 | OPT_CALLBACK_F(0, "stat-graph-width", options, N_("<width>"), |
| 5627 | N_("generate diffstat with a given graph width"), |
| 5628 | PARSE_OPT_NONEG, diff_opt_stat), |
| 5629 | OPT_CALLBACK_F(0, "stat-count", options, N_("<count>"), |
| 5630 | N_("generate diffstat with limited lines"), |
| 5631 | PARSE_OPT_NONEG, diff_opt_stat), |
Nguyễn Thái Ngọc Duy | 7d7942b | 2019-02-21 18:16:11 +0700 | [diff] [blame] | 5632 | OPT_CALLBACK_F(0, "compact-summary", options, NULL, |
| 5633 | N_("generate compact summary in diffstat"), |
| 5634 | PARSE_OPT_NOARG, diff_opt_compact_summary), |
Nguyễn Thái Ngọc Duy | 6d9af6f | 2019-03-05 19:30:12 +0700 | [diff] [blame] | 5635 | OPT_CALLBACK_F(0, "binary", options, NULL, |
| 5636 | N_("output a binary diff that can be applied"), |
| 5637 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_binary), |
Nguyễn Thái Ngọc Duy | 4fe0167 | 2019-03-05 19:30:13 +0700 | [diff] [blame] | 5638 | OPT_BOOL(0, "full-index", &options->flags.full_index, |
| 5639 | N_("show full pre- and post-image object names on the \"index\" lines")), |
Nguyễn Thái Ngọc Duy | 8b81c26 | 2019-03-05 19:30:17 +0700 | [diff] [blame] | 5640 | OPT_COLOR_FLAG(0, "color", &options->use_color, |
| 5641 | N_("show colored diff")), |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 5642 | OPT_CALLBACK_F(0, "ws-error-highlight", options, N_("<kind>"), |
| 5643 | N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"), |
| 5644 | PARSE_OPT_NONEG, diff_opt_ws_error_highlight), |
Nguyễn Thái Ngọc Duy | 1a1eb16 | 2019-03-24 15:19:57 +0700 | [diff] [blame] | 5645 | OPT_SET_INT('z', NULL, &options->line_termination, |
| 5646 | N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"), |
| 5647 | 0), |
Nguyễn Thái Ngọc Duy | d877418 | 2019-03-24 15:20:04 +0700 | [diff] [blame] | 5648 | OPT__ABBREV(&options->abbrev), |
Nguyễn Thái Ngọc Duy | 31fba9d | 2019-03-24 15:20:05 +0700 | [diff] [blame] | 5649 | OPT_STRING_F(0, "src-prefix", &options->a_prefix, N_("<prefix>"), |
| 5650 | N_("show the given source prefix instead of \"a/\""), |
| 5651 | PARSE_OPT_NONEG), |
| 5652 | OPT_STRING_F(0, "dst-prefix", &options->b_prefix, N_("<prefix>"), |
Jiang Xin | 8a1569d | 2019-06-02 23:11:22 +0800 | [diff] [blame] | 5653 | N_("show the given destination prefix instead of \"b/\""), |
Nguyễn Thái Ngọc Duy | 31fba9d | 2019-03-24 15:20:05 +0700 | [diff] [blame] | 5654 | PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | 2f81cf9 | 2019-03-24 15:20:06 +0700 | [diff] [blame] | 5655 | OPT_CALLBACK_F(0, "line-prefix", options, N_("<prefix>"), |
| 5656 | N_("prepend an additional prefix to every line of output"), |
| 5657 | PARSE_OPT_NONEG, diff_opt_line_prefix), |
Nguyễn Thái Ngọc Duy | 11c659d | 2019-03-24 15:20:07 +0700 | [diff] [blame] | 5658 | OPT_CALLBACK_F(0, "no-prefix", options, NULL, |
| 5659 | N_("do not show any source or destination prefix"), |
| 5660 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_no_prefix), |
Jeff King | b39a569 | 2023-03-09 01:09:54 -0500 | [diff] [blame] | 5661 | OPT_CALLBACK_F(0, "default-prefix", options, NULL, |
| 5662 | N_("use default prefixes a/ and b/"), |
| 5663 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_default_prefix), |
Nguyễn Thái Ngọc Duy | 16ed6c9 | 2019-03-24 15:20:08 +0700 | [diff] [blame] | 5664 | OPT_INTEGER_F(0, "inter-hunk-context", &options->interhunkcontext, |
| 5665 | N_("show context between diff hunks up to the specified number of lines"), |
| 5666 | PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5667 | OPT_CALLBACK_F(0, "output-indicator-new", |
| 5668 | &options->output_indicators[OUTPUT_INDICATOR_NEW], |
| 5669 | N_("<char>"), |
| 5670 | N_("specify the character to indicate a new line instead of '+'"), |
| 5671 | PARSE_OPT_NONEG, diff_opt_char), |
| 5672 | OPT_CALLBACK_F(0, "output-indicator-old", |
| 5673 | &options->output_indicators[OUTPUT_INDICATOR_OLD], |
| 5674 | N_("<char>"), |
| 5675 | N_("specify the character to indicate an old line instead of '-'"), |
| 5676 | PARSE_OPT_NONEG, diff_opt_char), |
| 5677 | OPT_CALLBACK_F(0, "output-indicator-context", |
| 5678 | &options->output_indicators[OUTPUT_INDICATOR_CONTEXT], |
| 5679 | N_("<char>"), |
| 5680 | N_("specify the character to indicate a context instead of ' '"), |
| 5681 | PARSE_OPT_NONEG, diff_opt_char), |
| 5682 | |
Nguyễn Thái Ngọc Duy | ced4e17 | 2019-02-21 18:16:13 +0700 | [diff] [blame] | 5683 | OPT_GROUP(N_("Diff rename options")), |
| 5684 | OPT_CALLBACK_F('B', "break-rewrites", &options->break_opt, N_("<n>[/<m>]"), |
| 5685 | N_("break complete rewrite changes into pairs of delete and create"), |
| 5686 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5687 | diff_opt_break_rewrites), |
Nguyễn Thái Ngọc Duy | f476308 | 2019-02-21 18:16:14 +0700 | [diff] [blame] | 5688 | OPT_CALLBACK_F('M', "find-renames", options, N_("<n>"), |
| 5689 | N_("detect renames"), |
| 5690 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5691 | diff_opt_find_renames), |
Nguyễn Thái Ngọc Duy | 1e53329 | 2019-02-21 18:16:15 +0700 | [diff] [blame] | 5692 | OPT_SET_INT_F('D', "irreversible-delete", &options->irreversible_delete, |
| 5693 | N_("omit the preimage for deletes"), |
| 5694 | 1, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | 7f64850 | 2019-02-21 18:16:16 +0700 | [diff] [blame] | 5695 | OPT_CALLBACK_F('C', "find-copies", options, N_("<n>"), |
| 5696 | N_("detect copies"), |
| 5697 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5698 | diff_opt_find_copies), |
Nguyễn Thái Ngọc Duy | bdd4741 | 2019-02-21 18:16:17 +0700 | [diff] [blame] | 5699 | OPT_BOOL(0, "find-copies-harder", &options->flags.find_copies_harder, |
| 5700 | N_("use unmodified files as source to find copies")), |
Nguyễn Thái Ngọc Duy | cdc43eb | 2019-02-21 18:16:18 +0700 | [diff] [blame] | 5701 | OPT_SET_INT_F(0, "no-renames", &options->detect_rename, |
| 5702 | N_("disable rename detection"), |
| 5703 | 0, PARSE_OPT_NONEG), |
| 5704 | OPT_BOOL(0, "rename-empty", &options->flags.rename_empty, |
| 5705 | N_("use empty blobs as rename source")), |
Nguyễn Thái Ngọc Duy | 1e9250b | 2019-03-05 19:30:16 +0700 | [diff] [blame] | 5706 | OPT_CALLBACK_F(0, "follow", options, NULL, |
| 5707 | N_("continue listing the history of a file beyond renames"), |
| 5708 | PARSE_OPT_NOARG, diff_opt_follow), |
Nguyễn Thái Ngọc Duy | bffee74 | 2019-03-24 15:19:58 +0700 | [diff] [blame] | 5709 | OPT_INTEGER('l', NULL, &options->rename_limit, |
| 5710 | N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")), |
Nguyễn Thái Ngọc Duy | ced4e17 | 2019-02-21 18:16:13 +0700 | [diff] [blame] | 5711 | |
Nguyễn Thái Ngọc Duy | 2e75f92 | 2019-02-21 18:16:20 +0700 | [diff] [blame] | 5712 | OPT_GROUP(N_("Diff algorithm options")), |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 5713 | OPT_CALLBACK_F(0, "minimal", options, NULL, |
| 5714 | N_("produce the smallest possible diff"), |
| 5715 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, |
| 5716 | diff_opt_diff_algorithm_no_arg), |
Nguyễn Thái Ngọc Duy | 87649a1 | 2019-02-21 18:16:21 +0700 | [diff] [blame] | 5717 | OPT_BIT_F('w', "ignore-all-space", &options->xdl_opts, |
| 5718 | N_("ignore whitespace when comparing lines"), |
| 5719 | XDF_IGNORE_WHITESPACE, PARSE_OPT_NONEG), |
| 5720 | OPT_BIT_F('b', "ignore-space-change", &options->xdl_opts, |
| 5721 | N_("ignore changes in amount of whitespace"), |
| 5722 | XDF_IGNORE_WHITESPACE_CHANGE, PARSE_OPT_NONEG), |
| 5723 | OPT_BIT_F(0, "ignore-space-at-eol", &options->xdl_opts, |
| 5724 | N_("ignore changes in whitespace at EOL"), |
| 5725 | XDF_IGNORE_WHITESPACE_AT_EOL, PARSE_OPT_NONEG), |
| 5726 | OPT_BIT_F(0, "ignore-cr-at-eol", &options->xdl_opts, |
| 5727 | N_("ignore carrier-return at the end of line"), |
| 5728 | XDF_IGNORE_CR_AT_EOL, PARSE_OPT_NONEG), |
| 5729 | OPT_BIT_F(0, "ignore-blank-lines", &options->xdl_opts, |
| 5730 | N_("ignore changes whose lines are all blank"), |
| 5731 | XDF_IGNORE_BLANK_LINES, PARSE_OPT_NONEG), |
Michał Kępień | 296d4a9 | 2020-10-20 08:48:09 +0200 | [diff] [blame] | 5732 | OPT_CALLBACK_F('I', "ignore-matching-lines", options, N_("<regex>"), |
| 5733 | N_("ignore changes whose all lines match <regex>"), |
| 5734 | 0, diff_opt_ignore_regex), |
Nguyễn Thái Ngọc Duy | 06f7751 | 2019-03-05 19:30:07 +0700 | [diff] [blame] | 5735 | OPT_BIT(0, "indent-heuristic", &options->xdl_opts, |
| 5736 | N_("heuristic to shift diff hunk boundaries for easy reading"), |
| 5737 | XDF_INDENT_HEURISTIC), |
Nguyễn Thái Ngọc Duy | 31fd640 | 2019-03-05 19:30:08 +0700 | [diff] [blame] | 5738 | OPT_CALLBACK_F(0, "patience", options, NULL, |
| 5739 | N_("generate diff using the \"patience diff\" algorithm"), |
| 5740 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, |
| 5741 | diff_opt_patience), |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 5742 | OPT_CALLBACK_F(0, "histogram", options, NULL, |
| 5743 | N_("generate diff using the \"histogram diff\" algorithm"), |
| 5744 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, |
| 5745 | diff_opt_diff_algorithm_no_arg), |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5746 | OPT_CALLBACK_F(0, "diff-algorithm", options, N_("<algorithm>"), |
| 5747 | N_("choose a diff algorithm"), |
| 5748 | PARSE_OPT_NONEG, diff_opt_diff_algorithm), |
Nguyễn Thái Ngọc Duy | df84a43 | 2019-03-05 19:30:11 +0700 | [diff] [blame] | 5749 | OPT_CALLBACK_F(0, "anchored", options, N_("<text>"), |
| 5750 | N_("generate diff using the \"anchored diff\" algorithm"), |
| 5751 | PARSE_OPT_NONEG, diff_opt_anchored), |
Nguyễn Thái Ngọc Duy | e9fb39b | 2019-03-05 19:30:18 +0700 | [diff] [blame] | 5752 | OPT_CALLBACK_F(0, "word-diff", options, N_("<mode>"), |
| 5753 | N_("show word diff, using <mode> to delimit changed words"), |
| 5754 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_word_diff), |
Nguyễn Thái Ngọc Duy | 797df11 | 2019-03-05 19:30:19 +0700 | [diff] [blame] | 5755 | OPT_CALLBACK_F(0, "word-diff-regex", options, N_("<regex>"), |
| 5756 | N_("use <regex> to decide what a word is"), |
| 5757 | PARSE_OPT_NONEG, diff_opt_word_diff_regex), |
Nguyễn Thái Ngọc Duy | 212db69 | 2019-03-05 19:30:20 +0700 | [diff] [blame] | 5758 | OPT_CALLBACK_F(0, "color-words", options, N_("<regex>"), |
| 5759 | N_("equivalent to --word-diff=color --word-diff-regex=<regex>"), |
| 5760 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_color_words), |
Nguyễn Thái Ngọc Duy | 59311a9 | 2019-03-24 15:20:09 +0700 | [diff] [blame] | 5761 | OPT_CALLBACK_F(0, "color-moved", options, N_("<mode>"), |
Jean-Noël Avila | 8bcd8f4 | 2019-05-17 21:26:19 +0200 | [diff] [blame] | 5762 | N_("moved lines of code are colored differently"), |
Nguyễn Thái Ngọc Duy | 59311a9 | 2019-03-24 15:20:09 +0700 | [diff] [blame] | 5763 | PARSE_OPT_OPTARG, diff_opt_color_moved), |
Nguyễn Thái Ngọc Duy | 8ce2020 | 2019-03-24 15:20:10 +0700 | [diff] [blame] | 5764 | OPT_CALLBACK_F(0, "color-moved-ws", options, N_("<mode>"), |
| 5765 | N_("how white spaces are ignored in --color-moved"), |
Nguyễn Thái Ngọc Duy | bb98729 | 2019-03-24 15:20:11 +0700 | [diff] [blame] | 5766 | 0, diff_opt_color_moved_ws), |
Nguyễn Thái Ngọc Duy | 2e75f92 | 2019-02-21 18:16:20 +0700 | [diff] [blame] | 5767 | |
Jean-Noël Avila | 8bcd8f4 | 2019-05-17 21:26:19 +0200 | [diff] [blame] | 5768 | OPT_GROUP(N_("Other diff options")), |
Nguyễn Thái Ngọc Duy | 0b1c5b5 | 2019-02-21 18:16:19 +0700 | [diff] [blame] | 5769 | OPT_CALLBACK_F(0, "relative", options, N_("<prefix>"), |
| 5770 | N_("when run from subdir, exclude changes outside and show relative paths"), |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 5771 | PARSE_OPT_OPTARG, |
Nguyễn Thái Ngọc Duy | 0b1c5b5 | 2019-02-21 18:16:19 +0700 | [diff] [blame] | 5772 | diff_opt_relative), |
Nguyễn Thái Ngọc Duy | c37d4c0 | 2019-03-05 19:30:14 +0700 | [diff] [blame] | 5773 | OPT_BOOL('a', "text", &options->flags.text, |
| 5774 | N_("treat all files as text")), |
Nguyễn Thái Ngọc Duy | 4f99f29 | 2019-03-05 19:30:15 +0700 | [diff] [blame] | 5775 | OPT_BOOL('R', NULL, &options->flags.reverse_diff, |
| 5776 | N_("swap two inputs, reverse the diff")), |
Nguyễn Thái Ngọc Duy | 1086ea0 | 2019-03-05 19:30:21 +0700 | [diff] [blame] | 5777 | OPT_BOOL(0, "exit-code", &options->flags.exit_with_status, |
| 5778 | N_("exit with 1 if there were differences, 0 otherwise")), |
Nguyễn Thái Ngọc Duy | 9bbaf1c | 2019-03-05 19:30:22 +0700 | [diff] [blame] | 5779 | OPT_BOOL(0, "quiet", &options->flags.quick, |
| 5780 | N_("disable all output of the program")), |
Nguyễn Thái Ngọc Duy | 0cda100 | 2019-03-05 19:30:23 +0700 | [diff] [blame] | 5781 | OPT_BOOL(0, "ext-diff", &options->flags.allow_external, |
| 5782 | N_("allow an external diff helper to be executed")), |
Nguyễn Thái Ngọc Duy | 8ab7697 | 2019-03-05 19:30:24 +0700 | [diff] [blame] | 5783 | OPT_CALLBACK_F(0, "textconv", options, NULL, |
| 5784 | N_("run external text conversion filters when comparing binary files"), |
| 5785 | PARSE_OPT_NOARG, diff_opt_textconv), |
Nguyễn Thái Ngọc Duy | b680ee1 | 2019-03-05 19:30:25 +0700 | [diff] [blame] | 5786 | OPT_CALLBACK_F(0, "ignore-submodules", options, N_("<when>"), |
| 5787 | N_("ignore changes to submodules in the diff generation"), |
| 5788 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5789 | diff_opt_ignore_submodules), |
Nguyễn Thái Ngọc Duy | 125dcea | 2019-03-05 19:30:26 +0700 | [diff] [blame] | 5790 | OPT_CALLBACK_F(0, "submodule", options, N_("<format>"), |
| 5791 | N_("specify how differences in submodules are shown"), |
| 5792 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5793 | diff_opt_submodule), |
Nguyễn Thái Ngọc Duy | 96860e4 | 2019-03-24 15:19:56 +0700 | [diff] [blame] | 5794 | OPT_SET_INT_F(0, "ita-invisible-in-index", &options->ita_invisible_in_index, |
| 5795 | N_("hide 'git add -N' entries from the index"), |
| 5796 | 1, PARSE_OPT_NONEG), |
| 5797 | OPT_SET_INT_F(0, "ita-visible-in-index", &options->ita_invisible_in_index, |
| 5798 | N_("treat 'git add -N' entries as real in the index"), |
| 5799 | 0, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | a41cfb3 | 2019-03-24 15:19:59 +0700 | [diff] [blame] | 5800 | OPT_CALLBACK_F('S', NULL, options, N_("<string>"), |
| 5801 | N_("look for differences that change the number of occurrences of the specified string"), |
| 5802 | 0, diff_opt_pickaxe_string), |
| 5803 | OPT_CALLBACK_F('G', NULL, options, N_("<regex>"), |
| 5804 | N_("look for differences that change the number of occurrences of the specified regex"), |
| 5805 | 0, diff_opt_pickaxe_regex), |
Nguyễn Thái Ngọc Duy | 85f8e88 | 2019-03-24 15:20:00 +0700 | [diff] [blame] | 5806 | OPT_BIT_F(0, "pickaxe-all", &options->pickaxe_opts, |
| 5807 | N_("show all changes in the changeset with -S or -G"), |
| 5808 | DIFF_PICKAXE_ALL, PARSE_OPT_NONEG), |
| 5809 | OPT_BIT_F(0, "pickaxe-regex", &options->pickaxe_opts, |
| 5810 | N_("treat <string> in -S as extended POSIX regular expression"), |
| 5811 | DIFF_PICKAXE_REGEX, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | f731814 | 2019-03-24 15:20:01 +0700 | [diff] [blame] | 5812 | OPT_FILENAME('O', NULL, &options->orderfile, |
| 5813 | N_("control the order in which files appear in the output")), |
Junio C Hamano | 1eb4136 | 2021-02-11 11:57:50 -0800 | [diff] [blame] | 5814 | OPT_CALLBACK_F(0, "rotate-to", options, N_("<path>"), |
| 5815 | N_("show the change in the specified path first"), |
| 5816 | PARSE_OPT_NONEG, diff_opt_rotate_to), |
| 5817 | OPT_CALLBACK_F(0, "skip-to", options, N_("<path>"), |
| 5818 | N_("skip the output to the specified path"), |
| 5819 | PARSE_OPT_NONEG, diff_opt_rotate_to), |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5820 | OPT_CALLBACK_F(0, "find-object", options, N_("<object-id>"), |
| 5821 | N_("look for differences that change the number of occurrences of the specified object"), |
| 5822 | PARSE_OPT_NONEG, diff_opt_find_object), |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 5823 | OPT_CALLBACK_F(0, "diff-filter", options, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"), |
| 5824 | N_("select files by diff type"), |
| 5825 | PARSE_OPT_NONEG, diff_opt_diff_filter), |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5826 | { OPTION_CALLBACK, 0, "output", options, N_("<file>"), |
Abhradeep Chakraborty | 9e1f22c | 2022-02-23 14:27:34 +0000 | [diff] [blame] | 5827 | N_("output to a specific file"), |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5828 | PARSE_OPT_NONEG, NULL, 0, diff_opt_output }, |
| 5829 | |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5830 | OPT_END() |
| 5831 | }; |
| 5832 | |
René Scharfe | 189e97b | 2022-12-01 23:53:17 +0100 | [diff] [blame] | 5833 | return parse_options_concat(opts, parseopts); |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5834 | } |
| 5835 | |
Duy Nguyen | a97262c | 2016-01-21 18:48:44 +0700 | [diff] [blame] | 5836 | int diff_opt_parse(struct diff_options *options, |
| 5837 | const char **av, int ac, const char *prefix) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5838 | { |
René Scharfe | 6c6048f | 2022-12-01 23:51:21 +0100 | [diff] [blame] | 5839 | struct option no_options[] = { OPT_END() }; |
| 5840 | struct option *parseopts = add_diff_options(no_options, options); |
| 5841 | |
Duy Nguyen | a97262c | 2016-01-21 18:48:44 +0700 | [diff] [blame] | 5842 | if (!prefix) |
| 5843 | prefix = ""; |
| 5844 | |
René Scharfe | 6c6048f | 2022-12-01 23:51:21 +0100 | [diff] [blame] | 5845 | ac = parse_options(ac, av, prefix, parseopts, NULL, |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5846 | PARSE_OPT_KEEP_DASHDASH | |
SZEDER Gábor | 99d86d6 | 2022-08-19 18:03:57 +0200 | [diff] [blame] | 5847 | PARSE_OPT_KEEP_UNKNOWN_OPT | |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5848 | PARSE_OPT_NO_INTERNAL_HELP | |
| 5849 | PARSE_OPT_ONE_SHOT | |
| 5850 | PARSE_OPT_STOP_AT_NON_OPTION); |
René Scharfe | 6c6048f | 2022-12-01 23:51:21 +0100 | [diff] [blame] | 5851 | free(parseopts); |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5852 | |
Nguyễn Thái Ngọc Duy | 8ce2020 | 2019-03-24 15:20:10 +0700 | [diff] [blame] | 5853 | return ac; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5854 | } |
| 5855 | |
Kevin Ballard | 10ae752 | 2010-09-27 16:58:25 -0700 | [diff] [blame] | 5856 | int parse_rename_score(const char **cp_p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5857 | { |
| 5858 | unsigned long num, scale; |
| 5859 | int ch, dot; |
| 5860 | const char *cp = *cp_p; |
| 5861 | |
| 5862 | num = 0; |
| 5863 | scale = 1; |
| 5864 | dot = 0; |
Brian Gianforcaro | eeefa7c | 2009-09-01 01:35:10 -0400 | [diff] [blame] | 5865 | for (;;) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5866 | ch = *cp; |
| 5867 | if ( !dot && ch == '.' ) { |
| 5868 | scale = 1; |
| 5869 | dot = 1; |
| 5870 | } else if ( ch == '%' ) { |
| 5871 | scale = dot ? scale*100 : 100; |
| 5872 | cp++; /* % is always at the end */ |
| 5873 | break; |
| 5874 | } else if ( ch >= '0' && ch <= '9' ) { |
| 5875 | if ( scale < 100000 ) { |
| 5876 | scale *= 10; |
| 5877 | num = (num*10) + (ch-'0'); |
| 5878 | } |
| 5879 | } else { |
| 5880 | break; |
| 5881 | } |
| 5882 | cp++; |
| 5883 | } |
| 5884 | *cp_p = cp; |
| 5885 | |
| 5886 | /* user says num divided by scale and we say internally that |
| 5887 | * is MAX_SCORE * num / scale. |
| 5888 | */ |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 5889 | return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale)); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5890 | } |
| 5891 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5892 | struct diff_queue_struct diff_queued_diff; |
| 5893 | |
| 5894 | void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp) |
| 5895 | { |
Dmitry S. Dolzhenko | 4c960a4 | 2014-03-04 02:31:53 +0400 | [diff] [blame] | 5896 | ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5897 | queue->queue[queue->nr++] = dp; |
| 5898 | } |
| 5899 | |
| 5900 | struct diff_filepair *diff_queue(struct diff_queue_struct *queue, |
| 5901 | struct diff_filespec *one, |
| 5902 | struct diff_filespec *two) |
| 5903 | { |
Junio C Hamano | ef67768 | 2006-08-03 12:01:01 -0700 | [diff] [blame] | 5904 | struct diff_filepair *dp = xcalloc(1, sizeof(*dp)); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5905 | dp->one = one; |
| 5906 | dp->two = two; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5907 | if (queue) |
| 5908 | diff_q(queue, dp); |
| 5909 | return dp; |
| 5910 | } |
| 5911 | |
| 5912 | void diff_free_filepair(struct diff_filepair *p) |
| 5913 | { |
Linus Torvalds | 9fb8841 | 2007-10-25 11:19:10 -0700 | [diff] [blame] | 5914 | free_filespec(p->one); |
| 5915 | free_filespec(p->two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5916 | free(p); |
| 5917 | } |
| 5918 | |
SZEDER Gábor | 04ae000 | 2022-11-02 23:01:40 +0100 | [diff] [blame] | 5919 | void diff_free_queue(struct diff_queue_struct *q) |
| 5920 | { |
| 5921 | for (int i = 0; i < q->nr; i++) |
| 5922 | diff_free_filepair(q->queue[i]); |
| 5923 | free(q->queue); |
| 5924 | } |
| 5925 | |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5926 | const char *diff_aligned_abbrev(const struct object_id *oid, int len) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5927 | { |
| 5928 | int abblen; |
| 5929 | const char *abbrev; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5930 | |
Ann T Ropea | 7cb6ac1 | 2017-12-03 22:27:42 +0100 | [diff] [blame] | 5931 | /* Do we want all 40 hex characters? */ |
brian m. carlson | 02afca1 | 2018-07-16 01:28:05 +0000 | [diff] [blame] | 5932 | if (len == the_hash_algo->hexsz) |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5933 | return oid_to_hex(oid); |
| 5934 | |
Ann T Ropea | 7cb6ac1 | 2017-12-03 22:27:42 +0100 | [diff] [blame] | 5935 | /* An abbreviated value is fine, possibly followed by an ellipsis. */ |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 5936 | abbrev = diff_abbrev_oid(oid, len); |
Ann T Ropea | 7cb6ac1 | 2017-12-03 22:27:42 +0100 | [diff] [blame] | 5937 | |
| 5938 | if (!print_sha1_ellipsis()) |
| 5939 | return abbrev; |
| 5940 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5941 | abblen = strlen(abbrev); |
Junio C Hamano | d709f1f | 2016-09-30 10:42:05 -0700 | [diff] [blame] | 5942 | |
| 5943 | /* |
Ann T Ropea | 7cb6ac1 | 2017-12-03 22:27:42 +0100 | [diff] [blame] | 5944 | * In well-behaved cases, where the abbreviated result is the |
Junio C Hamano | d709f1f | 2016-09-30 10:42:05 -0700 | [diff] [blame] | 5945 | * same as the requested length, append three dots after the |
| 5946 | * abbreviation (hence the whole logic is limited to the case |
| 5947 | * where abblen < 37); when the actual abbreviated result is a |
| 5948 | * bit longer than the requested length, we reduce the number |
| 5949 | * of dots so that they match the well-behaved ones. However, |
| 5950 | * if the actual abbreviation is longer than the requested |
| 5951 | * length by more than three, we give up on aligning, and add |
| 5952 | * three dots anyway, to indicate that the output is not the |
| 5953 | * full object name. Yes, this may be suboptimal, but this |
| 5954 | * appears only in "diff --raw --abbrev" output and it is not |
| 5955 | * worth the effort to change it now. Note that this would |
| 5956 | * likely to work fine when the automatic sizing of default |
| 5957 | * abbreviation length is used--we would be fed -1 in "len" in |
| 5958 | * that case, and will end up always appending three-dots, but |
| 5959 | * the automatic sizing is supposed to give abblen that ensures |
| 5960 | * uniqueness across all objects (statistically speaking). |
| 5961 | */ |
brian m. carlson | 02afca1 | 2018-07-16 01:28:05 +0000 | [diff] [blame] | 5962 | if (abblen < the_hash_algo->hexsz - 3) { |
brian m. carlson | dc01505 | 2017-03-26 16:01:24 +0000 | [diff] [blame] | 5963 | static char hex[GIT_MAX_HEXSZ + 1]; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5964 | if (len < abblen && abblen <= len + 2) |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 5965 | xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, ".."); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5966 | else |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 5967 | xsnprintf(hex, sizeof(hex), "%s...", abbrev); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5968 | return hex; |
| 5969 | } |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5970 | |
| 5971 | return oid_to_hex(oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5972 | } |
| 5973 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5974 | static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5975 | { |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5976 | int line_termination = opt->line_termination; |
| 5977 | int inter_name_termination = line_termination ? '\t' : '\0'; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 5978 | |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 5979 | fprintf(opt->file, "%s", diff_line_prefix(opt)); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5980 | if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 5981 | fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode, |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5982 | diff_aligned_abbrev(&p->one->oid, opt->abbrev)); |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 5983 | fprintf(opt->file, "%s ", |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5984 | diff_aligned_abbrev(&p->two->oid, opt->abbrev)); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5985 | } |
| 5986 | if (p->score) { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 5987 | fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p), |
| 5988 | inter_name_termination); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5989 | } else { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 5990 | fprintf(opt->file, "%c%c", p->status, inter_name_termination); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5991 | } |
| 5992 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 5993 | if (p->status == DIFF_STATUS_COPIED || |
| 5994 | p->status == DIFF_STATUS_RENAMED) { |
| 5995 | const char *name_a, *name_b; |
| 5996 | name_a = p->one->path; |
| 5997 | name_b = p->two->path; |
| 5998 | strip_prefix(opt->prefix_length, &name_a, &name_b); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 5999 | write_name_quoted(name_a, opt->file, inter_name_termination); |
| 6000 | write_name_quoted(name_b, opt->file, line_termination); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6001 | } else { |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 6002 | const char *name_a, *name_b; |
| 6003 | name_a = p->one->mode ? p->one->path : p->two->path; |
| 6004 | name_b = NULL; |
| 6005 | strip_prefix(opt->prefix_length, &name_a, &name_b); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6006 | write_name_quoted(name_a, opt->file, line_termination); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6007 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6008 | } |
| 6009 | |
| 6010 | int diff_unmodified_pair(struct diff_filepair *p) |
| 6011 | { |
| 6012 | /* This function is written stricter than necessary to support |
| 6013 | * the currently implemented transformers, but the idea is to |
| 6014 | * let transformers to produce diff_filepairs any way they want, |
| 6015 | * and filter and clean them up here before producing the output. |
| 6016 | */ |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6017 | struct diff_filespec *one = p->one, *two = p->two; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6018 | |
| 6019 | if (DIFF_PAIR_UNMERGED(p)) |
| 6020 | return 0; /* unmerged is interesting */ |
| 6021 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6022 | /* deletion, addition, mode or type change |
| 6023 | * and rename are all interesting. |
| 6024 | */ |
| 6025 | if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) || |
| 6026 | DIFF_PAIR_MODE_CHANGED(p) || |
| 6027 | strcmp(one->path, two->path)) |
| 6028 | return 0; |
| 6029 | |
| 6030 | /* both are valid and point at the same path. that is, we are |
| 6031 | * dealing with a change. |
| 6032 | */ |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 6033 | if (one->oid_valid && two->oid_valid && |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 6034 | oideq(&one->oid, &two->oid) && |
Jens Lehmann | 85adbf2 | 2010-03-12 22:23:52 +0100 | [diff] [blame] | 6035 | !one->dirty_submodule && !two->dirty_submodule) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6036 | return 1; /* no change */ |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 6037 | if (!one->oid_valid && !two->oid_valid) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6038 | return 1; /* both look at the same file on the filesystem. */ |
| 6039 | return 0; |
| 6040 | } |
| 6041 | |
| 6042 | static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o) |
| 6043 | { |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6044 | int include_conflict_headers = |
| 6045 | (additional_headers(o, p->one->path) && |
Elijah Newren | 71a146d | 2022-09-02 03:53:29 +0000 | [diff] [blame] | 6046 | !o->pickaxe_opts && |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6047 | (!o->filter || filter_bit_tst(DIFF_STATUS_UNMERGED, o))); |
| 6048 | |
| 6049 | /* |
| 6050 | * Check if we can return early without showing a diff. Note that |
| 6051 | * diff_filepair only stores {oid, path, mode, is_valid} |
| 6052 | * information for each path, and thus diff_unmodified_pair() only |
| 6053 | * considers those bits of info. However, we do not want pairs |
| 6054 | * created by create_filepairs_for_header_only_notifications() |
| 6055 | * (which always look like unmodified pairs) to be ignored, so |
| 6056 | * return early if both p is unmodified AND we don't want to |
| 6057 | * include_conflict_headers. |
| 6058 | */ |
| 6059 | if (diff_unmodified_pair(p) && !include_conflict_headers) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6060 | return; |
| 6061 | |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6062 | /* Actually, we can also return early to avoid showing tree diffs */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6063 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 6064 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6065 | return; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6066 | |
| 6067 | run_diff(p, o); |
| 6068 | } |
| 6069 | |
| 6070 | static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o, |
| 6071 | struct diffstat_t *diffstat) |
| 6072 | { |
| 6073 | if (diff_unmodified_pair(p)) |
| 6074 | return; |
| 6075 | |
| 6076 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 6077 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
Yann Dirson | c3fced6 | 2010-10-24 23:03:10 +0200 | [diff] [blame] | 6078 | return; /* no useful stat for tree diffs */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6079 | |
| 6080 | run_diffstat(p, o, diffstat); |
| 6081 | } |
| 6082 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 6083 | static void diff_flush_checkdiff(struct diff_filepair *p, |
| 6084 | struct diff_options *o) |
| 6085 | { |
| 6086 | if (diff_unmodified_pair(p)) |
| 6087 | return; |
| 6088 | |
| 6089 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 6090 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
Yann Dirson | c3fced6 | 2010-10-24 23:03:10 +0200 | [diff] [blame] | 6091 | return; /* nothing to check in tree diffs */ |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 6092 | |
| 6093 | run_checkdiff(p, o); |
| 6094 | } |
| 6095 | |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6096 | int diff_queue_is_empty(struct diff_options *o) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6097 | { |
| 6098 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6099 | int i; |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6100 | int include_conflict_headers = |
| 6101 | (o->additional_path_headers && |
Elijah Newren | 67360b7 | 2022-09-02 03:53:30 +0000 | [diff] [blame] | 6102 | strmap_get_size(o->additional_path_headers) && |
Elijah Newren | 71a146d | 2022-09-02 03:53:29 +0000 | [diff] [blame] | 6103 | !o->pickaxe_opts && |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6104 | (!o->filter || filter_bit_tst(DIFF_STATUS_UNMERGED, o))); |
| 6105 | |
| 6106 | if (include_conflict_headers) |
| 6107 | return 0; |
| 6108 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6109 | for (i = 0; i < q->nr; i++) |
| 6110 | if (!diff_unmodified_pair(q->queue[i])) |
| 6111 | return 0; |
| 6112 | return 1; |
| 6113 | } |
| 6114 | |
| 6115 | #if DIFF_DEBUG |
| 6116 | void diff_debug_filespec(struct diff_filespec *s, int x, const char *one) |
| 6117 | { |
| 6118 | fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n", |
| 6119 | x, one ? one : "", |
| 6120 | s->path, |
| 6121 | DIFF_FILE_VALID(s) ? "valid" : "invalid", |
| 6122 | s->mode, |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 6123 | s->oid_valid ? oid_to_hex(&s->oid) : ""); |
Jeff King | 428d52a | 2014-01-16 20:21:59 -0500 | [diff] [blame] | 6124 | fprintf(stderr, "queue[%d] %s size %lu\n", |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6125 | x, one ? one : "", |
Jeff King | 428d52a | 2014-01-16 20:21:59 -0500 | [diff] [blame] | 6126 | s->size); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6127 | } |
| 6128 | |
| 6129 | void diff_debug_filepair(const struct diff_filepair *p, int i) |
| 6130 | { |
| 6131 | diff_debug_filespec(p->one, i, "one"); |
| 6132 | diff_debug_filespec(p->two, i, "two"); |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6133 | fprintf(stderr, "score %d, status %c rename_used %d broken %d\n", |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6134 | p->score, p->status ? p->status : '?', |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6135 | p->one->rename_used, p->broken_pair); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6136 | } |
| 6137 | |
| 6138 | void diff_debug_queue(const char *msg, struct diff_queue_struct *q) |
| 6139 | { |
| 6140 | int i; |
| 6141 | if (msg) |
| 6142 | fprintf(stderr, "%s\n", msg); |
| 6143 | fprintf(stderr, "q->nr = %d\n", q->nr); |
| 6144 | for (i = 0; i < q->nr; i++) { |
| 6145 | struct diff_filepair *p = q->queue[i]; |
| 6146 | diff_debug_filepair(p, i); |
| 6147 | } |
| 6148 | } |
| 6149 | #endif |
| 6150 | |
| 6151 | static void diff_resolve_rename_copy(void) |
| 6152 | { |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6153 | int i; |
| 6154 | struct diff_filepair *p; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6155 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6156 | |
| 6157 | diff_debug_queue("resolve-rename-copy", q); |
| 6158 | |
| 6159 | for (i = 0; i < q->nr; i++) { |
| 6160 | p = q->queue[i]; |
| 6161 | p->status = 0; /* undecided */ |
| 6162 | if (DIFF_PAIR_UNMERGED(p)) |
| 6163 | p->status = DIFF_STATUS_UNMERGED; |
| 6164 | else if (!DIFF_FILE_VALID(p->one)) |
| 6165 | p->status = DIFF_STATUS_ADDED; |
| 6166 | else if (!DIFF_FILE_VALID(p->two)) |
| 6167 | p->status = DIFF_STATUS_DELETED; |
| 6168 | else if (DIFF_PAIR_TYPE_CHANGED(p)) |
| 6169 | p->status = DIFF_STATUS_TYPE_CHANGED; |
| 6170 | |
| 6171 | /* from this point on, we are dealing with a pair |
| 6172 | * whose both sides are valid and of the same type, i.e. |
| 6173 | * either in-place edit or rename/copy edit. |
| 6174 | */ |
| 6175 | else if (DIFF_PAIR_RENAME(p)) { |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6176 | /* |
| 6177 | * A rename might have re-connected a broken |
| 6178 | * pair up, causing the pathnames to be the |
| 6179 | * same again. If so, that's not a rename at |
| 6180 | * all, just a modification.. |
| 6181 | * |
| 6182 | * Otherwise, see if this source was used for |
| 6183 | * multiple renames, in which case we decrement |
| 6184 | * the count, and call it a copy. |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6185 | */ |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6186 | if (!strcmp(p->one->path, p->two->path)) |
| 6187 | p->status = DIFF_STATUS_MODIFIED; |
| 6188 | else if (--p->one->rename_used > 0) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6189 | p->status = DIFF_STATUS_COPIED; |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6190 | else |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6191 | p->status = DIFF_STATUS_RENAMED; |
| 6192 | } |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 6193 | else if (!oideq(&p->one->oid, &p->two->oid) || |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 6194 | p->one->mode != p->two->mode || |
Jens Lehmann | 85adbf2 | 2010-03-12 22:23:52 +0100 | [diff] [blame] | 6195 | p->one->dirty_submodule || |
| 6196 | p->two->dirty_submodule || |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 6197 | is_null_oid(&p->one->oid)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6198 | p->status = DIFF_STATUS_MODIFIED; |
| 6199 | else { |
| 6200 | /* This is a "no-change" entry and should not |
| 6201 | * happen anymore, but prepare for broken callers. |
| 6202 | */ |
| 6203 | error("feeding unmodified %s to diffcore", |
| 6204 | p->one->path); |
| 6205 | p->status = DIFF_STATUS_UNKNOWN; |
| 6206 | } |
| 6207 | } |
| 6208 | diff_debug_queue("resolve-rename-copy done", q); |
| 6209 | } |
| 6210 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6211 | static int check_pair_status(struct diff_filepair *p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6212 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6213 | switch (p->status) { |
| 6214 | case DIFF_STATUS_UNKNOWN: |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6215 | return 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6216 | case 0: |
| 6217 | die("internal error in diff-resolve-rename-copy"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6218 | default: |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6219 | return 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6220 | } |
| 6221 | } |
| 6222 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6223 | static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt) |
| 6224 | { |
| 6225 | int fmt = opt->output_format; |
| 6226 | |
| 6227 | if (fmt & DIFF_FORMAT_CHECKDIFF) |
| 6228 | diff_flush_checkdiff(p, opt); |
| 6229 | else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) |
| 6230 | diff_flush_raw(p, opt); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 6231 | else if (fmt & DIFF_FORMAT_NAME) { |
| 6232 | const char *name_a, *name_b; |
| 6233 | name_a = p->two->path; |
| 6234 | name_b = NULL; |
| 6235 | strip_prefix(opt->prefix_length, &name_a, &name_b); |
Jeff King | f5022b5 | 2017-02-08 15:31:15 -0500 | [diff] [blame] | 6236 | fprintf(opt->file, "%s", diff_line_prefix(opt)); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6237 | write_name_quoted(name_a, opt->file, opt->line_termination); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 6238 | } |
Junio C Hamano | a64f8b2 | 2023-08-18 16:59:32 -0700 | [diff] [blame] | 6239 | |
| 6240 | opt->found_changes = 1; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6241 | } |
| 6242 | |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6243 | static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs) |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6244 | { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6245 | struct strbuf sb = STRBUF_INIT; |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6246 | if (fs->mode) |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6247 | strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6248 | else |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6249 | strbuf_addf(&sb, " %s ", newdelete); |
| 6250 | |
| 6251 | quote_c_style(fs->path, &sb, NULL, 0); |
| 6252 | strbuf_addch(&sb, '\n'); |
| 6253 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
| 6254 | sb.buf, sb.len, 0); |
| 6255 | strbuf_release(&sb); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6256 | } |
| 6257 | |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6258 | static void show_mode_change(struct diff_options *opt, struct diff_filepair *p, |
| 6259 | int show_name) |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6260 | { |
| 6261 | if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6262 | struct strbuf sb = STRBUF_INIT; |
| 6263 | strbuf_addf(&sb, " mode change %06o => %06o", |
| 6264 | p->one->mode, p->two->mode); |
Alexandre Julliard | 0d26a64 | 2007-02-10 15:37:48 +0100 | [diff] [blame] | 6265 | if (show_name) { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6266 | strbuf_addch(&sb, ' '); |
| 6267 | quote_c_style(p->two->path, &sb, NULL, 0); |
Alexandre Julliard | 0d26a64 | 2007-02-10 15:37:48 +0100 | [diff] [blame] | 6268 | } |
Stefan Beller | 58aaced | 2017-09-27 15:51:26 -0700 | [diff] [blame] | 6269 | strbuf_addch(&sb, '\n'); |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6270 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
| 6271 | sb.buf, sb.len, 0); |
| 6272 | strbuf_release(&sb); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6273 | } |
| 6274 | } |
| 6275 | |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6276 | static void show_rename_copy(struct diff_options *opt, const char *renamecopy, |
| 6277 | struct diff_filepair *p) |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6278 | { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6279 | struct strbuf sb = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 6280 | struct strbuf names = STRBUF_INIT; |
| 6281 | |
| 6282 | pprint_rename(&names, p->one->path, p->two->path); |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6283 | strbuf_addf(&sb, " %s %s (%d%%)\n", |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 6284 | renamecopy, names.buf, similarity_index(p)); |
| 6285 | strbuf_release(&names); |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6286 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
| 6287 | sb.buf, sb.len, 0); |
| 6288 | show_mode_change(opt, p, 0); |
Rene Scharfe | 348eda2 | 2017-08-30 19:49:42 +0200 | [diff] [blame] | 6289 | strbuf_release(&sb); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6290 | } |
| 6291 | |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 6292 | static void diff_summary(struct diff_options *opt, struct diff_filepair *p) |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6293 | { |
| 6294 | switch(p->status) { |
| 6295 | case DIFF_STATUS_DELETED: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6296 | show_file_mode_name(opt, "delete", p->one); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6297 | break; |
| 6298 | case DIFF_STATUS_ADDED: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6299 | show_file_mode_name(opt, "create", p->two); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6300 | break; |
| 6301 | case DIFF_STATUS_COPIED: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6302 | show_rename_copy(opt, "copy", p); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6303 | break; |
| 6304 | case DIFF_STATUS_RENAMED: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6305 | show_rename_copy(opt, "rename", p); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6306 | break; |
| 6307 | default: |
| 6308 | if (p->score) { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6309 | struct strbuf sb = STRBUF_INIT; |
| 6310 | strbuf_addstr(&sb, " rewrite "); |
| 6311 | quote_c_style(p->two->path, &sb, NULL, 0); |
| 6312 | strbuf_addf(&sb, " (%d%%)\n", similarity_index(p)); |
| 6313 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
| 6314 | sb.buf, sb.len, 0); |
Rene Scharfe | fa842d8 | 2017-08-30 19:49:41 +0200 | [diff] [blame] | 6315 | strbuf_release(&sb); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6316 | } |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6317 | show_mode_change(opt, p, !p->score); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6318 | break; |
| 6319 | } |
| 6320 | } |
| 6321 | |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6322 | struct patch_id_t { |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6323 | git_hash_ctx *ctx; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6324 | int patchlen; |
| 6325 | }; |
| 6326 | |
| 6327 | static int remove_space(char *line, int len) |
| 6328 | { |
| 6329 | int i; |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6330 | char *dst = line; |
| 6331 | unsigned char c; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6332 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6333 | for (i = 0; i < len; i++) |
| 6334 | if (!isspace((c = line[i]))) |
| 6335 | *dst++ = c; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6336 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6337 | return dst - line; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6338 | } |
| 6339 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6340 | void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx) |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6341 | { |
| 6342 | unsigned char hash[GIT_MAX_RAWSZ]; |
| 6343 | unsigned short carry = 0; |
| 6344 | int i; |
| 6345 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6346 | the_hash_algo->final_fn(hash, ctx); |
| 6347 | the_hash_algo->init_fn(ctx); |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6348 | /* 20-byte sum, with carry */ |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6349 | for (i = 0; i < the_hash_algo->rawsz; ++i) { |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6350 | carry += result->hash[i] + hash[i]; |
| 6351 | result->hash[i] = carry; |
| 6352 | carry >>= 8; |
| 6353 | } |
| 6354 | } |
| 6355 | |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 6356 | static int patch_id_consume(void *priv, char *line, unsigned long len) |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6357 | { |
| 6358 | struct patch_id_t *data = priv; |
| 6359 | int new_len; |
| 6360 | |
René Scharfe | 82a6201 | 2020-08-19 00:08:54 +0200 | [diff] [blame] | 6361 | if (len > 12 && starts_with(line, "\\ ")) |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 6362 | return 0; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6363 | new_len = remove_space(line, len); |
| 6364 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6365 | the_hash_algo->update_fn(data->ctx, line, new_len); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6366 | data->patchlen += new_len; |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 6367 | return 0; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6368 | } |
| 6369 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6370 | static void patch_id_add_string(git_hash_ctx *ctx, const char *str) |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6371 | { |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6372 | the_hash_algo->update_fn(ctx, str, strlen(str)); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6373 | } |
| 6374 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6375 | static void patch_id_add_mode(git_hash_ctx *ctx, unsigned mode) |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6376 | { |
| 6377 | /* large enough for 2^32 in octal */ |
| 6378 | char buf[12]; |
| 6379 | int len = xsnprintf(buf, sizeof(buf), "%06o", mode); |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6380 | the_hash_algo->update_fn(ctx, buf, len); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6381 | } |
| 6382 | |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6383 | /* returns 0 upon success, and writes result into oid */ |
Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 +0000 | [diff] [blame] | 6384 | static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only) |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6385 | { |
| 6386 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6387 | int i; |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6388 | git_hash_ctx ctx; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6389 | struct patch_id_t data; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6390 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6391 | the_hash_algo->init_fn(&ctx); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6392 | memset(&data, 0, sizeof(struct patch_id_t)); |
| 6393 | data.ctx = &ctx; |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6394 | oidclr(oid); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6395 | |
| 6396 | for (i = 0; i < q->nr; i++) { |
| 6397 | xpparam_t xpp; |
| 6398 | xdemitconf_t xecfg; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6399 | mmfile_t mf1, mf2; |
| 6400 | struct diff_filepair *p = q->queue[i]; |
| 6401 | int len1, len2; |
| 6402 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 6403 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 6404 | memset(&xecfg, 0, sizeof(xecfg)); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6405 | if (p->status == 0) |
| 6406 | return error("internal diff status error"); |
| 6407 | if (p->status == DIFF_STATUS_UNKNOWN) |
| 6408 | continue; |
| 6409 | if (diff_unmodified_pair(p)) |
| 6410 | continue; |
| 6411 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 6412 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
| 6413 | continue; |
| 6414 | if (DIFF_PAIR_UNMERGED(p)) |
| 6415 | continue; |
| 6416 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 6417 | diff_fill_oid_info(p->one, options->repo->index); |
| 6418 | diff_fill_oid_info(p->two, options->repo->index); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6419 | |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6420 | len1 = remove_space(p->one->path, strlen(p->one->path)); |
| 6421 | len2 = remove_space(p->two->path, strlen(p->two->path)); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6422 | patch_id_add_string(&ctx, "diff--git"); |
| 6423 | patch_id_add_string(&ctx, "a/"); |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6424 | the_hash_algo->update_fn(&ctx, p->one->path, len1); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6425 | patch_id_add_string(&ctx, "b/"); |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6426 | the_hash_algo->update_fn(&ctx, p->two->path, len2); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6427 | |
| 6428 | if (p->one->mode == 0) { |
| 6429 | patch_id_add_string(&ctx, "newfilemode"); |
| 6430 | patch_id_add_mode(&ctx, p->two->mode); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6431 | } else if (p->two->mode == 0) { |
| 6432 | patch_id_add_string(&ctx, "deletedfilemode"); |
| 6433 | patch_id_add_mode(&ctx, p->one->mode); |
Jerry Zhang | 93105ab | 2022-10-24 20:07:42 +0000 | [diff] [blame] | 6434 | } else if (p->one->mode != p->two->mode) { |
| 6435 | patch_id_add_string(&ctx, "oldmode"); |
| 6436 | patch_id_add_mode(&ctx, p->one->mode); |
| 6437 | patch_id_add_string(&ctx, "newmode"); |
| 6438 | patch_id_add_mode(&ctx, p->two->mode); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6439 | } |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6440 | |
Jerry Zhang | 0570be7 | 2022-10-24 20:07:39 +0000 | [diff] [blame] | 6441 | if (diff_header_only) { |
| 6442 | /* don't do anything since we're only populating header info */ |
| 6443 | } else if (diff_filespec_is_binary(options->repo, p->one) || |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6444 | diff_filespec_is_binary(options->repo, p->two)) { |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6445 | the_hash_algo->update_fn(&ctx, oid_to_hex(&p->one->oid), |
| 6446 | the_hash_algo->hexsz); |
| 6447 | the_hash_algo->update_fn(&ctx, oid_to_hex(&p->two->oid), |
| 6448 | the_hash_algo->hexsz); |
Jerry Zhang | 0570be7 | 2022-10-24 20:07:39 +0000 | [diff] [blame] | 6449 | } else { |
| 6450 | if (p->one->mode == 0) { |
| 6451 | patch_id_add_string(&ctx, "---/dev/null"); |
| 6452 | patch_id_add_string(&ctx, "+++b/"); |
| 6453 | the_hash_algo->update_fn(&ctx, p->two->path, len2); |
| 6454 | } else if (p->two->mode == 0) { |
| 6455 | patch_id_add_string(&ctx, "---a/"); |
| 6456 | the_hash_algo->update_fn(&ctx, p->one->path, len1); |
| 6457 | patch_id_add_string(&ctx, "+++/dev/null"); |
| 6458 | } else { |
| 6459 | patch_id_add_string(&ctx, "---a/"); |
| 6460 | the_hash_algo->update_fn(&ctx, p->one->path, len1); |
| 6461 | patch_id_add_string(&ctx, "+++b/"); |
| 6462 | the_hash_algo->update_fn(&ctx, p->two->path, len2); |
| 6463 | } |
| 6464 | |
| 6465 | if (fill_mmfile(options->repo, &mf1, p->one) < 0 || |
| 6466 | fill_mmfile(options->repo, &mf2, p->two) < 0) |
| 6467 | return error("unable to read files to diff"); |
| 6468 | xpp.flags = 0; |
| 6469 | xecfg.ctxlen = 3; |
| 6470 | xecfg.flags = XDL_EMIT_NO_HUNK_HDR; |
| 6471 | if (xdi_diff_outf(&mf1, &mf2, NULL, |
| 6472 | patch_id_consume, &data, &xpp, &xecfg)) |
| 6473 | return error("unable to generate patch-id diff for %s", |
| 6474 | p->one->path); |
Clemens Buchacher | 34597c1 | 2010-08-15 09:20:43 +0200 | [diff] [blame] | 6475 | } |
Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 +0000 | [diff] [blame] | 6476 | flush_one_hunk(oid, &ctx); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6477 | } |
| 6478 | |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6479 | return 0; |
| 6480 | } |
| 6481 | |
Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 +0000 | [diff] [blame] | 6482 | int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only) |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6483 | { |
| 6484 | struct diff_queue_struct *q = &diff_queued_diff; |
Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 +0000 | [diff] [blame] | 6485 | int result = diff_get_patch_id(options, oid, diff_header_only); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6486 | |
SZEDER Gábor | 586d8b5 | 2022-11-02 23:01:42 +0100 | [diff] [blame] | 6487 | diff_free_queue(q); |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 6488 | DIFF_QUEUE_CLEAR(q); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6489 | |
| 6490 | return result; |
| 6491 | } |
| 6492 | |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6493 | static int is_summary_empty(const struct diff_queue_struct *q) |
| 6494 | { |
| 6495 | int i; |
| 6496 | |
| 6497 | for (i = 0; i < q->nr; i++) { |
| 6498 | const struct diff_filepair *p = q->queue[i]; |
| 6499 | |
| 6500 | switch (p->status) { |
| 6501 | case DIFF_STATUS_DELETED: |
| 6502 | case DIFF_STATUS_ADDED: |
| 6503 | case DIFF_STATUS_COPIED: |
| 6504 | case DIFF_STATUS_RENAMED: |
| 6505 | return 0; |
| 6506 | default: |
| 6507 | if (p->score) |
| 6508 | return 0; |
| 6509 | if (p->one->mode && p->two->mode && |
| 6510 | p->one->mode != p->two->mode) |
| 6511 | return 0; |
| 6512 | break; |
| 6513 | } |
| 6514 | } |
| 6515 | return 1; |
| 6516 | } |
| 6517 | |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6518 | static const char rename_limit_warning[] = |
Elijah Newren | 05d2c61 | 2021-07-15 00:45:21 +0000 | [diff] [blame] | 6519 | N_("exhaustive rename detection was skipped due to too many files."); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6520 | |
| 6521 | static const char degrade_cc_to_c_warning[] = |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6522 | N_("only found copies from modified paths due to too many files."); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6523 | |
| 6524 | static const char rename_limit_advice[] = |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6525 | N_("you may want to set your %s variable to at least " |
| 6526 | "%d and retry the command."); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6527 | |
| 6528 | void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc) |
| 6529 | { |
Nguyễn Thái Ngọc Duy | 4e056c9 | 2018-01-16 16:23:49 +0700 | [diff] [blame] | 6530 | fflush(stdout); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6531 | if (degraded_cc) |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6532 | warning(_(degrade_cc_to_c_warning)); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6533 | else if (needed) |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6534 | warning(_(rename_limit_warning)); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6535 | else |
| 6536 | return; |
Elijah Newren | 9f7e4bf | 2017-11-13 12:15:59 -0800 | [diff] [blame] | 6537 | if (0 < needed) |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6538 | warning(_(rename_limit_advice), varname, needed); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6539 | } |
| 6540 | |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6541 | static void create_filepairs_for_header_only_notifications(struct diff_options *o) |
| 6542 | { |
| 6543 | struct strset present; |
| 6544 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6545 | struct hashmap_iter iter; |
| 6546 | struct strmap_entry *e; |
| 6547 | int i; |
| 6548 | |
| 6549 | strset_init_with_options(&present, /*pool*/ NULL, /*strdup*/ 0); |
| 6550 | |
| 6551 | /* |
| 6552 | * Find out which paths exist in diff_queued_diff, preferring |
| 6553 | * one->path for any pair that has multiple paths. |
| 6554 | */ |
| 6555 | for (i = 0; i < q->nr; i++) { |
| 6556 | struct diff_filepair *p = q->queue[i]; |
| 6557 | char *path = p->one->path ? p->one->path : p->two->path; |
| 6558 | |
| 6559 | if (strmap_contains(o->additional_path_headers, path)) |
| 6560 | strset_add(&present, path); |
| 6561 | } |
| 6562 | |
| 6563 | /* |
| 6564 | * Loop over paths in additional_path_headers; for each NOT already |
| 6565 | * in diff_queued_diff, create a synthetic filepair and insert that |
| 6566 | * into diff_queued_diff. |
| 6567 | */ |
| 6568 | strmap_for_each_entry(o->additional_path_headers, &iter, e) { |
| 6569 | if (!strset_contains(&present, e->key)) { |
| 6570 | struct diff_filespec *one, *two; |
| 6571 | struct diff_filepair *p; |
| 6572 | |
| 6573 | one = alloc_filespec(e->key); |
| 6574 | two = alloc_filespec(e->key); |
| 6575 | fill_filespec(one, null_oid(), 0, 0); |
| 6576 | fill_filespec(two, null_oid(), 0, 0); |
| 6577 | p = diff_queue(q, one, two); |
| 6578 | p->status = DIFF_STATUS_MODIFIED; |
| 6579 | } |
| 6580 | } |
| 6581 | |
| 6582 | /* Re-sort the filepairs */ |
| 6583 | diffcore_fix_diff_index(); |
| 6584 | |
| 6585 | /* Cleanup */ |
| 6586 | strset_clear(&present); |
| 6587 | } |
| 6588 | |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6589 | static void diff_flush_patch_all_file_pairs(struct diff_options *o) |
| 6590 | { |
| 6591 | int i; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6592 | static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT; |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6593 | struct diff_queue_struct *q = &diff_queued_diff; |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 6594 | |
| 6595 | if (WSEH_NEW & WS_RULE_MASK) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 6596 | BUG("WS rules bit mask overlaps with diff symbol flags"); |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 6597 | |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6598 | if (o->color_moved) |
| 6599 | o->emitted_symbols = &esm; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6600 | |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6601 | if (o->additional_path_headers) |
| 6602 | create_filepairs_for_header_only_notifications(o); |
| 6603 | |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6604 | for (i = 0; i < q->nr; i++) { |
| 6605 | struct diff_filepair *p = q->queue[i]; |
| 6606 | if (check_pair_status(p)) |
| 6607 | diff_flush_patch(p, o); |
| 6608 | } |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6609 | |
| 6610 | if (o->emitted_symbols) { |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6611 | if (o->color_moved) { |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 6612 | struct mem_pool entry_pool; |
| 6613 | struct moved_entry_list *entry_list; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6614 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 6615 | mem_pool_init(&entry_pool, 1024 * 1024); |
| 6616 | entry_list = add_lines_to_move_detection(o, |
| 6617 | &entry_pool); |
| 6618 | mark_color_as_moved(o, entry_list); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 6619 | if (o->color_moved == COLOR_MOVED_ZEBRA_DIM) |
| 6620 | dim_moved_lines(o); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6621 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 6622 | mem_pool_discard(&entry_pool, 0); |
| 6623 | free(entry_list); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6624 | } |
| 6625 | |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6626 | for (i = 0; i < esm.nr; i++) |
| 6627 | emit_diff_symbol_from_struct(o, &esm.buf[i]); |
| 6628 | |
| 6629 | for (i = 0; i < esm.nr; i++) |
| 6630 | free((void *)esm.buf[i].line); |
Jeff King | 48edf3a | 2019-01-24 07:32:41 -0500 | [diff] [blame] | 6631 | esm.nr = 0; |
| 6632 | |
| 6633 | o->emitted_symbols = NULL; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6634 | } |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6635 | } |
| 6636 | |
Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 11:45:34 +0100 | [diff] [blame] | 6637 | static void diff_free_file(struct diff_options *options) |
| 6638 | { |
| 6639 | if (options->close_file) |
| 6640 | fclose(options->file); |
| 6641 | } |
| 6642 | |
Ævar Arnfjörð Bjarmason | c45dc9c | 2021-02-11 11:45:35 +0100 | [diff] [blame] | 6643 | static void diff_free_ignore_regex(struct diff_options *options) |
| 6644 | { |
| 6645 | int i; |
| 6646 | |
| 6647 | for (i = 0; i < options->ignore_regex_nr; i++) { |
| 6648 | regfree(options->ignore_regex[i]); |
| 6649 | free(options->ignore_regex[i]); |
| 6650 | } |
| 6651 | free(options->ignore_regex); |
| 6652 | } |
| 6653 | |
Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 11:45:34 +0100 | [diff] [blame] | 6654 | void diff_free(struct diff_options *options) |
| 6655 | { |
| 6656 | if (options->no_free) |
| 6657 | return; |
| 6658 | |
| 6659 | diff_free_file(options); |
Ævar Arnfjörð Bjarmason | c45dc9c | 2021-02-11 11:45:35 +0100 | [diff] [blame] | 6660 | diff_free_ignore_regex(options); |
Ævar Arnfjörð Bjarmason | 244c272 | 2022-02-16 11:56:28 +0100 | [diff] [blame] | 6661 | clear_pathspec(&options->pathspec); |
Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 11:45:34 +0100 | [diff] [blame] | 6662 | } |
| 6663 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6664 | void diff_flush(struct diff_options *options) |
| 6665 | { |
| 6666 | struct diff_queue_struct *q = &diff_queued_diff; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6667 | int i, output_format = options->output_format; |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6668 | int separator = 0; |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 6669 | int dirstat_by_line = 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6670 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6671 | /* |
| 6672 | * Order: raw, stat, summary, patch |
| 6673 | * or: name/name-status/checkdiff (other bits clear) |
| 6674 | */ |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6675 | if (!q->nr && !options->additional_path_headers) |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6676 | goto free_queue; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6677 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6678 | if (output_format & (DIFF_FORMAT_RAW | |
| 6679 | DIFF_FORMAT_NAME | |
| 6680 | DIFF_FORMAT_NAME_STATUS | |
| 6681 | DIFF_FORMAT_CHECKDIFF)) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6682 | for (i = 0; i < q->nr; i++) { |
| 6683 | struct diff_filepair *p = q->queue[i]; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6684 | if (check_pair_status(p)) |
| 6685 | flush_one_pair(p, options); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6686 | } |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6687 | separator++; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6688 | } |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6689 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6690 | if (output_format & DIFF_FORMAT_DIRSTAT && options->flags.dirstat_by_line) |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 6691 | dirstat_by_line = 1; |
| 6692 | |
| 6693 | if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT) || |
| 6694 | dirstat_by_line) { |
Timo Hirvonen | 5e2b063 | 2006-06-25 14:28:19 +0300 | [diff] [blame] | 6695 | struct diffstat_t diffstat; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6696 | |
Daniel Ferreira | e4cb659 | 2019-11-13 12:40:58 +0000 | [diff] [blame] | 6697 | compute_diffstat(options, &diffstat, q); |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 6698 | if (output_format & DIFF_FORMAT_NUMSTAT) |
| 6699 | show_numstat(&diffstat, options); |
| 6700 | if (output_format & DIFF_FORMAT_DIFFSTAT) |
| 6701 | show_stats(&diffstat, options); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 6702 | if (output_format & DIFF_FORMAT_SHORTSTAT) |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6703 | show_shortstats(&diffstat, options); |
Mårten Kongstad | ab27389 | 2015-03-02 16:05:39 +0100 | [diff] [blame] | 6704 | if (output_format & DIFF_FORMAT_DIRSTAT && dirstat_by_line) |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 6705 | show_dirstat_by_line(&diffstat, options); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 6706 | free_diffstat_info(&diffstat); |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 6707 | separator++; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6708 | } |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 6709 | if ((output_format & DIFF_FORMAT_DIRSTAT) && !dirstat_by_line) |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 6710 | show_dirstat(options); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6711 | |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6712 | if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) { |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 6713 | for (i = 0; i < q->nr; i++) { |
| 6714 | diff_summary(options, q->queue[i]); |
| 6715 | } |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 6716 | separator++; |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6717 | } |
| 6718 | |
Junio C Hamano | 5f107ca | 2023-08-18 16:59:28 -0700 | [diff] [blame] | 6719 | if (output_format & DIFF_FORMAT_PATCH) { |
| 6720 | if (separator) { |
| 6721 | emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0); |
| 6722 | if (options->stat_sep) |
| 6723 | /* attach patch instead of inline */ |
| 6724 | emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP, |
| 6725 | NULL, 0, 0); |
| 6726 | } |
| 6727 | |
| 6728 | diff_flush_patch_all_file_pairs(options); |
| 6729 | } |
| 6730 | |
| 6731 | if (output_format & DIFF_FORMAT_CALLBACK) |
| 6732 | options->format_callback(q, options, options->format_callback_data); |
| 6733 | |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6734 | if (output_format & DIFF_FORMAT_NO_OUTPUT && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6735 | options->flags.exit_with_status && |
| 6736 | options->flags.diff_from_contents) { |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6737 | /* |
| 6738 | * run diff_flush_patch for the exit status. setting |
Ondřej Bílka | 749f763 | 2013-07-22 23:02:23 +0200 | [diff] [blame] | 6739 | * options->file to /dev/null should be safe, because we |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6740 | * aren't supposed to produce any output anyway. |
| 6741 | */ |
Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 11:45:34 +0100 | [diff] [blame] | 6742 | diff_free_file(options); |
Nguyễn Thái Ngọc Duy | 23a9e07 | 2017-05-03 17:16:46 +0700 | [diff] [blame] | 6743 | options->file = xfopen("/dev/null", "w"); |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6744 | options->close_file = 1; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6745 | options->color_moved = 0; |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6746 | for (i = 0; i < q->nr; i++) { |
| 6747 | struct diff_filepair *p = q->queue[i]; |
| 6748 | if (check_pair_status(p)) |
| 6749 | diff_flush_patch(p, options); |
| 6750 | if (options->found_changes) |
| 6751 | break; |
| 6752 | } |
| 6753 | } |
| 6754 | |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6755 | free_queue: |
SZEDER Gábor | 586d8b5 | 2022-11-02 23:01:42 +0100 | [diff] [blame] | 6756 | diff_free_queue(q); |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 6757 | DIFF_QUEUE_CLEAR(q); |
Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 11:45:34 +0100 | [diff] [blame] | 6758 | diff_free(options); |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6759 | |
| 6760 | /* |
Jim Meyering | 97bf2a0 | 2009-08-30 22:27:02 +0200 | [diff] [blame] | 6761 | * Report the content-level differences with HAS_CHANGES; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6762 | * diff_addremove/diff_change does not set the bit when |
| 6763 | * DIFF_FROM_CONTENTS is in effect (e.g. with -w). |
| 6764 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6765 | if (options->flags.diff_from_contents) { |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6766 | if (options->found_changes) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6767 | options->flags.has_changes = 1; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6768 | else |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6769 | options->flags.has_changes = 0; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6770 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6771 | } |
| 6772 | |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6773 | static int match_filter(const struct diff_options *options, const struct diff_filepair *p) |
| 6774 | { |
| 6775 | return (((p->status == DIFF_STATUS_MODIFIED) && |
| 6776 | ((p->score && |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6777 | filter_bit_tst(DIFF_STATUS_FILTER_BROKEN, options)) || |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6778 | (!p->score && |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6779 | filter_bit_tst(DIFF_STATUS_MODIFIED, options)))) || |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6780 | ((p->status != DIFF_STATUS_MODIFIED) && |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6781 | filter_bit_tst(p->status, options))); |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6782 | } |
| 6783 | |
Junio C Hamano | 949226f | 2013-07-17 14:19:24 -0700 | [diff] [blame] | 6784 | static void diffcore_apply_filter(struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6785 | { |
| 6786 | int i; |
| 6787 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6788 | struct diff_queue_struct outq; |
Junio C Hamano | 949226f | 2013-07-17 14:19:24 -0700 | [diff] [blame] | 6789 | |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 6790 | DIFF_QUEUE_CLEAR(&outq); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6791 | |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6792 | if (!options->filter) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6793 | return; |
| 6794 | |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6795 | if (filter_bit_tst(DIFF_STATUS_FILTER_AON, options)) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6796 | int found; |
| 6797 | for (i = found = 0; !found && i < q->nr; i++) { |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6798 | if (match_filter(options, q->queue[i])) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6799 | found++; |
| 6800 | } |
| 6801 | if (found) |
| 6802 | return; |
| 6803 | |
| 6804 | /* otherwise we will clear the whole queue |
| 6805 | * by copying the empty outq at the end of this |
| 6806 | * function, but first clear the current entries |
| 6807 | * in the queue. |
| 6808 | */ |
| 6809 | for (i = 0; i < q->nr; i++) |
| 6810 | diff_free_filepair(q->queue[i]); |
| 6811 | } |
| 6812 | else { |
| 6813 | /* Only the matching ones */ |
| 6814 | for (i = 0; i < q->nr; i++) { |
| 6815 | struct diff_filepair *p = q->queue[i]; |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6816 | if (match_filter(options, p)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6817 | diff_q(&outq, p); |
| 6818 | else |
| 6819 | diff_free_filepair(p); |
| 6820 | } |
| 6821 | } |
| 6822 | free(q->queue); |
| 6823 | *q = outq; |
| 6824 | } |
| 6825 | |
Sven Verdoolaege | 5701115 | 2007-09-08 12:30:22 +0200 | [diff] [blame] | 6826 | /* Check whether two filespecs with the same mode and size are identical */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6827 | static int diff_filespec_is_identical(struct repository *r, |
| 6828 | struct diff_filespec *one, |
Sven Verdoolaege | 5701115 | 2007-09-08 12:30:22 +0200 | [diff] [blame] | 6829 | struct diff_filespec *two) |
| 6830 | { |
Junio C Hamano | 2b459b4 | 2008-03-02 00:07:59 -0800 | [diff] [blame] | 6831 | if (S_ISGITLINK(one->mode)) |
| 6832 | return 0; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6833 | if (diff_populate_filespec(r, one, NULL)) |
Sven Verdoolaege | 5701115 | 2007-09-08 12:30:22 +0200 | [diff] [blame] | 6834 | return 0; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6835 | if (diff_populate_filespec(r, two, NULL)) |
Sven Verdoolaege | 5701115 | 2007-09-08 12:30:22 +0200 | [diff] [blame] | 6836 | return 0; |
| 6837 | return !memcmp(one->data, two->data, one->size); |
| 6838 | } |
| 6839 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6840 | static int diff_filespec_check_stat_unmatch(struct repository *r, |
| 6841 | struct diff_filepair *p) |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6842 | { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6843 | struct diff_populate_filespec_options dpf_options = { |
| 6844 | .check_size_only = 1, |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6845 | .missing_object_cb = diff_queued_diff_prefetch, |
| 6846 | .missing_object_data = r, |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6847 | }; |
| 6848 | |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 6849 | if (p->done_skip_stat_unmatch) |
| 6850 | return p->skip_stat_unmatch_result; |
| 6851 | |
| 6852 | p->done_skip_stat_unmatch = 1; |
| 6853 | p->skip_stat_unmatch_result = 0; |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6854 | /* |
| 6855 | * 1. Entries that come from stat info dirtiness |
| 6856 | * always have both sides (iow, not create/delete), |
| 6857 | * one side of the object name is unknown, with |
| 6858 | * the same mode and size. Keep the ones that |
| 6859 | * do not match these criteria. They have real |
| 6860 | * differences. |
| 6861 | * |
| 6862 | * 2. At this point, the file is known to be modified, |
| 6863 | * with the same mode and size, and the object |
| 6864 | * name of one side is unknown. Need to inspect |
| 6865 | * the identical contents. |
| 6866 | */ |
| 6867 | if (!DIFF_FILE_VALID(p->one) || /* (1) */ |
| 6868 | !DIFF_FILE_VALID(p->two) || |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 6869 | (p->one->oid_valid && p->two->oid_valid) || |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6870 | (p->one->mode != p->two->mode) || |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6871 | diff_populate_filespec(r, p->one, &dpf_options) || |
| 6872 | diff_populate_filespec(r, p->two, &dpf_options) || |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6873 | (p->one->size != p->two->size) || |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6874 | !diff_filespec_is_identical(r, p->one, p->two)) /* (2) */ |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 6875 | p->skip_stat_unmatch_result = 1; |
| 6876 | return p->skip_stat_unmatch_result; |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6877 | } |
| 6878 | |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6879 | static void diffcore_skip_stat_unmatch(struct diff_options *diffopt) |
| 6880 | { |
| 6881 | int i; |
| 6882 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6883 | struct diff_queue_struct outq; |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 6884 | DIFF_QUEUE_CLEAR(&outq); |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6885 | |
| 6886 | for (i = 0; i < q->nr; i++) { |
| 6887 | struct diff_filepair *p = q->queue[i]; |
| 6888 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6889 | if (diff_filespec_check_stat_unmatch(diffopt->repo, p)) |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6890 | diff_q(&outq, p); |
| 6891 | else { |
| 6892 | /* |
| 6893 | * The caller can subtract 1 from skip_stat_unmatch |
| 6894 | * to determine how many paths were dirty only |
| 6895 | * due to stat info mismatch. |
| 6896 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6897 | if (!diffopt->flags.no_index) |
René Scharfe | 6d2d9e8 | 2007-08-15 00:41:00 +0200 | [diff] [blame] | 6898 | diffopt->skip_stat_unmatch++; |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6899 | diff_free_filepair(p); |
| 6900 | } |
| 6901 | } |
| 6902 | free(q->queue); |
| 6903 | *q = outq; |
| 6904 | } |
| 6905 | |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 6906 | static int diffnamecmp(const void *a_, const void *b_) |
| 6907 | { |
| 6908 | const struct diff_filepair *a = *((const struct diff_filepair **)a_); |
| 6909 | const struct diff_filepair *b = *((const struct diff_filepair **)b_); |
| 6910 | const char *name_a, *name_b; |
| 6911 | |
| 6912 | name_a = a->one ? a->one->path : a->two->path; |
| 6913 | name_b = b->one ? b->one->path : b->two->path; |
| 6914 | return strcmp(name_a, name_b); |
| 6915 | } |
| 6916 | |
Jeff King | 784c0da | 2019-02-14 00:48:03 -0500 | [diff] [blame] | 6917 | void diffcore_fix_diff_index(void) |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 6918 | { |
| 6919 | struct diff_queue_struct *q = &diff_queued_diff; |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 6920 | QSORT(q->queue, q->nr, diffnamecmp); |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 6921 | } |
| 6922 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6923 | void diff_add_if_missing(struct repository *r, |
| 6924 | struct oid_array *to_fetch, |
| 6925 | const struct diff_filespec *filespec) |
Jonathan Tan | 7fbbcb2 | 2019-04-05 10:09:34 -0700 | [diff] [blame] | 6926 | { |
| 6927 | if (filespec && filespec->oid_valid && |
Jonathan Tan | a63694f | 2019-08-20 13:53:20 -0700 | [diff] [blame] | 6928 | !S_ISGITLINK(filespec->mode) && |
Jonathan Tan | 7fbbcb2 | 2019-04-05 10:09:34 -0700 | [diff] [blame] | 6929 | oid_object_info_extended(r, &filespec->oid, NULL, |
| 6930 | OBJECT_INFO_FOR_PREFETCH)) |
| 6931 | oid_array_append(to_fetch, &filespec->oid); |
| 6932 | } |
| 6933 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6934 | void diff_queued_diff_prefetch(void *repository) |
| 6935 | { |
| 6936 | struct repository *repo = repository; |
| 6937 | int i; |
| 6938 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6939 | struct oid_array to_fetch = OID_ARRAY_INIT; |
| 6940 | |
| 6941 | for (i = 0; i < q->nr; i++) { |
| 6942 | struct diff_filepair *p = q->queue[i]; |
| 6943 | diff_add_if_missing(repo, &to_fetch, p->one); |
| 6944 | diff_add_if_missing(repo, &to_fetch, p->two); |
| 6945 | } |
| 6946 | |
| 6947 | /* |
| 6948 | * NEEDSWORK: Consider deduplicating the OIDs sent. |
| 6949 | */ |
| 6950 | promisor_remote_get_direct(repo, to_fetch.oid, to_fetch.nr); |
| 6951 | |
| 6952 | oid_array_clear(&to_fetch); |
| 6953 | } |
| 6954 | |
Dragan Simic | 4ca7a3f | 2023-09-23 06:01:14 +0200 | [diff] [blame] | 6955 | void init_diffstat_widths(struct diff_options *options) |
| 6956 | { |
| 6957 | options->stat_width = -1; /* use full terminal width */ |
| 6958 | options->stat_name_width = -1; /* respect diff.statNameWidth config */ |
| 6959 | options->stat_graph_width = -1; /* respect diff.statGraphWidth config */ |
| 6960 | } |
| 6961 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6962 | void diffcore_std(struct diff_options *options) |
| 6963 | { |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6964 | int output_formats_to_prefetch = DIFF_FORMAT_DIFFSTAT | |
| 6965 | DIFF_FORMAT_NUMSTAT | |
| 6966 | DIFF_FORMAT_PATCH | |
| 6967 | DIFF_FORMAT_SHORTSTAT | |
| 6968 | DIFF_FORMAT_DIRSTAT; |
Jonathan Tan | 7fbbcb2 | 2019-04-05 10:09:34 -0700 | [diff] [blame] | 6969 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6970 | /* |
| 6971 | * Check if the user requested a blob-data-requiring diff output and/or |
| 6972 | * break-rewrite detection (which requires blob data). If yes, prefetch |
| 6973 | * the diff pairs. |
| 6974 | * |
| 6975 | * If no prefetching occurs, diffcore_rename() will prefetch if it |
| 6976 | * decides that it needs inexact rename detection. |
| 6977 | */ |
Ævar Arnfjörð Bjarmason | a5183d7 | 2023-03-28 15:58:53 +0200 | [diff] [blame] | 6978 | if (options->repo == the_repository && repo_has_promisor_remote(the_repository) && |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6979 | (options->output_format & output_formats_to_prefetch || |
| 6980 | options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)) |
| 6981 | diff_queued_diff_prefetch(options->repo); |
Jonathan Tan | 7fbbcb2 | 2019-04-05 10:09:34 -0700 | [diff] [blame] | 6982 | |
Kirill Smelkov | 7195fbf | 2014-02-24 20:21:51 +0400 | [diff] [blame] | 6983 | /* NOTE please keep the following in sync with diff_tree_combined() */ |
Junio C Hamano | 9d86535 | 2008-09-06 19:09:16 -0700 | [diff] [blame] | 6984 | if (options->skip_stat_unmatch) |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6985 | diffcore_skip_stat_unmatch(options); |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 6986 | if (!options->found_follow) { |
| 6987 | /* See try_to_follow_renames() in tree-diff.c */ |
| 6988 | if (options->break_opt != -1) |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6989 | diffcore_break(options->repo, |
| 6990 | options->break_opt); |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 6991 | if (options->detect_rename) |
| 6992 | diffcore_rename(options); |
| 6993 | if (options->break_opt != -1) |
| 6994 | diffcore_merge_broken(); |
| 6995 | } |
Stefan Beller | cf63051 | 2018-01-04 14:50:41 -0800 | [diff] [blame] | 6996 | if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) |
Junio C Hamano | 382f013 | 2010-08-31 13:44:39 -0700 | [diff] [blame] | 6997 | diffcore_pickaxe(options); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6998 | if (options->orderfile) |
| 6999 | diffcore_order(options->orderfile); |
Junio C Hamano | 1eb4136 | 2021-02-11 11:57:50 -0800 | [diff] [blame] | 7000 | if (options->rotate_to) |
| 7001 | diffcore_rotate(options); |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 7002 | if (!options->found_follow) |
| 7003 | /* See try_to_follow_renames() in tree-diff.c */ |
| 7004 | diff_resolve_rename_copy(); |
Junio C Hamano | 949226f | 2013-07-17 14:19:24 -0700 | [diff] [blame] | 7005 | diffcore_apply_filter(options); |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 7006 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7007 | if (diff_queued_diff.nr && !options->flags.diff_from_contents) |
| 7008 | options->flags.has_changes = 1; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 7009 | else |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7010 | options->flags.has_changes = 0; |
Bo Yang | 1da6175 | 2010-05-06 21:52:28 -0700 | [diff] [blame] | 7011 | |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 7012 | options->found_follow = 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7013 | } |
| 7014 | |
Jeff King | 5cc6b2d | 2023-08-21 16:20:46 -0400 | [diff] [blame] | 7015 | int diff_result_code(struct diff_options *opt) |
Junio C Hamano | da31b35 | 2007-12-13 23:40:27 -0800 | [diff] [blame] | 7016 | { |
| 7017 | int result = 0; |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 7018 | |
Max Nanasy | c9fc441 | 2013-03-21 12:53:38 -0700 | [diff] [blame] | 7019 | diff_warn_rename_limit("diff.renameLimit", |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 7020 | opt->needed_rename_limit, |
| 7021 | opt->degraded_cc_to_c); |
Jeff King | 5cc6b2d | 2023-08-21 16:20:46 -0400 | [diff] [blame] | 7022 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7023 | if (opt->flags.exit_with_status && |
| 7024 | opt->flags.has_changes) |
Junio C Hamano | da31b35 | 2007-12-13 23:40:27 -0800 | [diff] [blame] | 7025 | result |= 01; |
| 7026 | if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7027 | opt->flags.check_failed) |
Junio C Hamano | da31b35 | 2007-12-13 23:40:27 -0800 | [diff] [blame] | 7028 | result |= 02; |
| 7029 | return result; |
| 7030 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7031 | |
Junio C Hamano | 28b9264 | 2011-05-31 09:14:17 -0700 | [diff] [blame] | 7032 | int diff_can_quit_early(struct diff_options *opt) |
| 7033 | { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7034 | return (opt->flags.quick && |
Junio C Hamano | 28b9264 | 2011-05-31 09:14:17 -0700 | [diff] [blame] | 7035 | !opt->filter && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7036 | opt->flags.has_changes); |
Junio C Hamano | 28b9264 | 2011-05-31 09:14:17 -0700 | [diff] [blame] | 7037 | } |
| 7038 | |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 7039 | /* |
| 7040 | * Shall changes to this submodule be ignored? |
| 7041 | * |
| 7042 | * Submodule changes can be configured to be ignored separately for each path, |
| 7043 | * but that configuration can be overridden from the command line. |
| 7044 | */ |
| 7045 | static int is_submodule_ignored(const char *path, struct diff_options *options) |
| 7046 | { |
| 7047 | int ignored = 0; |
Brandon Williams | 02f2f56 | 2017-10-31 11:19:05 -0700 | [diff] [blame] | 7048 | struct diff_flags orig_flags = options->flags; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7049 | if (!options->flags.override_submodule_config) |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 7050 | set_diffopt_flags_from_submodule_config(options, path); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7051 | if (options->flags.ignore_submodules) |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 7052 | ignored = 1; |
| 7053 | options->flags = orig_flags; |
| 7054 | return ignored; |
| 7055 | } |
| 7056 | |
Daniel Ferreira | e4cb659 | 2019-11-13 12:40:58 +0000 | [diff] [blame] | 7057 | void compute_diffstat(struct diff_options *options, |
| 7058 | struct diffstat_t *diffstat, |
| 7059 | struct diff_queue_struct *q) |
| 7060 | { |
| 7061 | int i; |
| 7062 | |
| 7063 | memset(diffstat, 0, sizeof(struct diffstat_t)); |
| 7064 | for (i = 0; i < q->nr; i++) { |
| 7065 | struct diff_filepair *p = q->queue[i]; |
| 7066 | if (check_pair_status(p)) |
| 7067 | diff_flush_stat(p, options, diffstat); |
| 7068 | } |
Junio C Hamano | e8efd86 | 2023-08-18 16:59:30 -0700 | [diff] [blame] | 7069 | options->found_changes = !!diffstat->nr; |
Daniel Ferreira | e4cb659 | 2019-11-13 12:40:58 +0000 | [diff] [blame] | 7070 | } |
| 7071 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7072 | void diff_addremove(struct diff_options *options, |
| 7073 | int addremove, unsigned mode, |
Brandon Williams | c26022e | 2017-05-30 10:30:47 -0700 | [diff] [blame] | 7074 | const struct object_id *oid, |
| 7075 | int oid_valid, |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 7076 | const char *concatpath, unsigned dirty_submodule) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7077 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7078 | struct diff_filespec *one, *two; |
| 7079 | |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 7080 | if (S_ISGITLINK(mode) && is_submodule_ignored(concatpath, options)) |
Johannes Schindelin | 50fd9bd | 2008-05-14 18:03:31 +0100 | [diff] [blame] | 7081 | return; |
| 7082 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7083 | /* This may look odd, but it is a preparation for |
| 7084 | * feeding "there are unchanged files which should |
| 7085 | * not produce diffs, but when you are doing copy |
| 7086 | * detection you would need them, so here they are" |
| 7087 | * entries to the diff-core. They will be prefixed |
| 7088 | * with something like '=' or '*' (I haven't decided |
| 7089 | * which but should not make any difference). |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 7090 | * Feeding the same new and old to diff_change() |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7091 | * also has the same effect. |
| 7092 | * Before the final output happens, they are pruned after |
| 7093 | * merged into rename/copy pairs as appropriate. |
| 7094 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7095 | if (options->flags.reverse_diff) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7096 | addremove = (addremove == '+' ? '-' : |
| 7097 | addremove == '-' ? '+' : addremove); |
| 7098 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 7099 | if (options->prefix && |
| 7100 | strncmp(concatpath, options->prefix, options->prefix_length)) |
| 7101 | return; |
| 7102 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7103 | one = alloc_filespec(concatpath); |
| 7104 | two = alloc_filespec(concatpath); |
| 7105 | |
| 7106 | if (addremove != '+') |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 7107 | fill_filespec(one, oid, oid_valid, mode); |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 7108 | if (addremove != '-') { |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 7109 | fill_filespec(two, oid, oid_valid, mode); |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 7110 | two->dirty_submodule = dirty_submodule; |
| 7111 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7112 | |
| 7113 | diff_queue(&diff_queued_diff, one, two); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7114 | if (!options->flags.diff_from_contents) |
| 7115 | options->flags.has_changes = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7116 | } |
| 7117 | |
| 7118 | void diff_change(struct diff_options *options, |
| 7119 | unsigned old_mode, unsigned new_mode, |
Brandon Williams | 94a0097 | 2017-05-30 10:30:49 -0700 | [diff] [blame] | 7120 | const struct object_id *old_oid, |
| 7121 | const struct object_id *new_oid, |
| 7122 | int old_oid_valid, int new_oid_valid, |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 7123 | const char *concatpath, |
| 7124 | unsigned old_dirty_submodule, unsigned new_dirty_submodule) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7125 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7126 | struct diff_filespec *one, *two; |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 7127 | struct diff_filepair *p; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7128 | |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 7129 | if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) && |
| 7130 | is_submodule_ignored(concatpath, options)) |
Johannes Schindelin | 50fd9bd | 2008-05-14 18:03:31 +0100 | [diff] [blame] | 7131 | return; |
| 7132 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7133 | if (options->flags.reverse_diff) { |
René Scharfe | 35d803b | 2017-01-28 22:40:58 +0100 | [diff] [blame] | 7134 | SWAP(old_mode, new_mode); |
Brandon Williams | 94a0097 | 2017-05-30 10:30:49 -0700 | [diff] [blame] | 7135 | SWAP(old_oid, new_oid); |
| 7136 | SWAP(old_oid_valid, new_oid_valid); |
René Scharfe | 35d803b | 2017-01-28 22:40:58 +0100 | [diff] [blame] | 7137 | SWAP(old_dirty_submodule, new_dirty_submodule); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7138 | } |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 7139 | |
| 7140 | if (options->prefix && |
| 7141 | strncmp(concatpath, options->prefix, options->prefix_length)) |
| 7142 | return; |
| 7143 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7144 | one = alloc_filespec(concatpath); |
| 7145 | two = alloc_filespec(concatpath); |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 7146 | fill_filespec(one, old_oid, old_oid_valid, old_mode); |
| 7147 | fill_filespec(two, new_oid, new_oid_valid, new_mode); |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 7148 | one->dirty_submodule = old_dirty_submodule; |
| 7149 | two->dirty_submodule = new_dirty_submodule; |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 7150 | p = diff_queue(&diff_queued_diff, one, two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7151 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7152 | if (options->flags.diff_from_contents) |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 7153 | return; |
| 7154 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7155 | if (options->flags.quick && options->skip_stat_unmatch && |
Jeff King | d2d7fbe | 2020-06-01 16:22:18 -0400 | [diff] [blame] | 7156 | !diff_filespec_check_stat_unmatch(options->repo, p)) { |
| 7157 | diff_free_filespec_data(p->one); |
| 7158 | diff_free_filespec_data(p->two); |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 7159 | return; |
Jeff King | d2d7fbe | 2020-06-01 16:22:18 -0400 | [diff] [blame] | 7160 | } |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 7161 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7162 | options->flags.has_changes = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7163 | } |
| 7164 | |
Junio C Hamano | fa7b290 | 2011-04-22 16:05:58 -0700 | [diff] [blame] | 7165 | struct diff_filepair *diff_unmerge(struct diff_options *options, const char *path) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7166 | { |
Junio C Hamano | 76399c0 | 2011-04-22 15:55:55 -0700 | [diff] [blame] | 7167 | struct diff_filepair *pair; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7168 | struct diff_filespec *one, *two; |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 7169 | |
| 7170 | if (options->prefix && |
| 7171 | strncmp(path, options->prefix, options->prefix_length)) |
Junio C Hamano | 76399c0 | 2011-04-22 15:55:55 -0700 | [diff] [blame] | 7172 | return NULL; |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 7173 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7174 | one = alloc_filespec(path); |
| 7175 | two = alloc_filespec(path); |
Junio C Hamano | 76399c0 | 2011-04-22 15:55:55 -0700 | [diff] [blame] | 7176 | pair = diff_queue(&diff_queued_diff, one, two); |
| 7177 | pair->is_unmerged = 1; |
| 7178 | return pair; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7179 | } |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7180 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 7181 | static char *run_textconv(struct repository *r, |
| 7182 | const char *pgm, |
| 7183 | struct diff_filespec *spec, |
| 7184 | size_t *outsize) |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7185 | { |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 7186 | struct diff_tempfile *temp; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 7187 | struct child_process child = CHILD_PROCESS_INIT; |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7188 | struct strbuf buf = STRBUF_INIT; |
Johannes Sixt | da1fbed | 2010-03-30 19:36:03 +0200 | [diff] [blame] | 7189 | int err = 0; |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7190 | |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 7191 | temp = prepare_temp_file(r, spec); |
Ævar Arnfjörð Bjarmason | 7f14609 | 2021-11-25 23:52:21 +0100 | [diff] [blame] | 7192 | strvec_push(&child.args, pgm); |
| 7193 | strvec_push(&child.args, temp->name); |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7194 | |
Jeff King | 41a457e | 2009-12-30 06:01:09 -0500 | [diff] [blame] | 7195 | child.use_shell = 1; |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7196 | child.out = -1; |
Johannes Sixt | da1fbed | 2010-03-30 19:36:03 +0200 | [diff] [blame] | 7197 | if (start_command(&child)) { |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 7198 | remove_tempfile(); |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7199 | return NULL; |
| 7200 | } |
Johannes Sixt | da1fbed | 2010-03-30 19:36:03 +0200 | [diff] [blame] | 7201 | |
| 7202 | if (strbuf_read(&buf, child.out, 0) < 0) |
| 7203 | err = error("error reading from textconv command '%s'", pgm); |
Jeff King | 70d7099 | 2009-12-30 04:02:53 -0500 | [diff] [blame] | 7204 | close(child.out); |
Johannes Sixt | da1fbed | 2010-03-30 19:36:03 +0200 | [diff] [blame] | 7205 | |
| 7206 | if (finish_command(&child) || err) { |
| 7207 | strbuf_release(&buf); |
| 7208 | remove_tempfile(); |
| 7209 | return NULL; |
| 7210 | } |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 7211 | remove_tempfile(); |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7212 | |
| 7213 | return strbuf_detach(&buf, outsize); |
| 7214 | } |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7215 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 7216 | size_t fill_textconv(struct repository *r, |
| 7217 | struct userdiff_driver *driver, |
Axel Bonnet | a788d7d | 2010-06-07 17:23:36 +0200 | [diff] [blame] | 7218 | struct diff_filespec *df, |
| 7219 | char **outbuf) |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7220 | { |
| 7221 | size_t size; |
| 7222 | |
Jeff King | a64e6a4 | 2016-02-22 13:28:54 -0500 | [diff] [blame] | 7223 | if (!driver) { |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7224 | if (!DIFF_FILE_VALID(df)) { |
| 7225 | *outbuf = ""; |
| 7226 | return 0; |
| 7227 | } |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 7228 | if (diff_populate_filespec(r, df, NULL)) |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7229 | die("unable to read files to diff"); |
| 7230 | *outbuf = df->data; |
| 7231 | return df->size; |
| 7232 | } |
| 7233 | |
Jeff King | a64e6a4 | 2016-02-22 13:28:54 -0500 | [diff] [blame] | 7234 | if (!driver->textconv) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 7235 | BUG("fill_textconv called with non-textconv driver"); |
Jeff King | a64e6a4 | 2016-02-22 13:28:54 -0500 | [diff] [blame] | 7236 | |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 7237 | if (driver->textconv_cache && df->oid_valid) { |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 7238 | *outbuf = notes_cache_get(driver->textconv_cache, |
brian m. carlson | 569aa37 | 2017-05-06 22:09:58 +0000 | [diff] [blame] | 7239 | &df->oid, |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 7240 | &size); |
| 7241 | if (*outbuf) |
| 7242 | return size; |
| 7243 | } |
| 7244 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 7245 | *outbuf = run_textconv(r, driver->textconv, df, &size); |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7246 | if (!*outbuf) |
| 7247 | die("unable to read files to diff"); |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 7248 | |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 7249 | if (driver->textconv_cache && df->oid_valid) { |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 7250 | /* ignore errors, as we might be in a readonly repository */ |
brian m. carlson | 569aa37 | 2017-05-06 22:09:58 +0000 | [diff] [blame] | 7251 | notes_cache_put(driver->textconv_cache, &df->oid, *outbuf, |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 7252 | size); |
| 7253 | /* |
| 7254 | * we could save up changes and flush them all at the end, |
| 7255 | * but we would need an extra call after all diffing is done. |
| 7256 | * Since generating a cache entry is the slow path anyway, |
| 7257 | * this extra overhead probably isn't a big deal. |
| 7258 | */ |
| 7259 | notes_cache_write(driver->textconv_cache); |
| 7260 | } |
| 7261 | |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7262 | return size; |
| 7263 | } |
Nguyễn Thái Ngọc Duy | 4914c96 | 2012-10-26 22:53:52 +0700 | [diff] [blame] | 7264 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 7265 | int textconv_object(struct repository *r, |
| 7266 | const char *path, |
Jeff Smith | 3a35cb2 | 2017-05-24 00:15:10 -0500 | [diff] [blame] | 7267 | unsigned mode, |
| 7268 | const struct object_id *oid, |
| 7269 | int oid_valid, |
| 7270 | char **buf, |
| 7271 | unsigned long *buf_size) |
| 7272 | { |
| 7273 | struct diff_filespec *df; |
| 7274 | struct userdiff_driver *textconv; |
| 7275 | |
| 7276 | df = alloc_filespec(path); |
Junio C Hamano | a6f38c1 | 2017-06-19 12:38:44 -0700 | [diff] [blame] | 7277 | fill_filespec(df, oid, oid_valid, mode); |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 7278 | textconv = get_textconv(r, df); |
Jeff Smith | 3a35cb2 | 2017-05-24 00:15:10 -0500 | [diff] [blame] | 7279 | if (!textconv) { |
| 7280 | free_filespec(df); |
| 7281 | return 0; |
| 7282 | } |
| 7283 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 7284 | *buf_size = fill_textconv(r, textconv, df, buf); |
Jeff Smith | 3a35cb2 | 2017-05-24 00:15:10 -0500 | [diff] [blame] | 7285 | free_filespec(df); |
| 7286 | return 1; |
| 7287 | } |
| 7288 | |
Nguyễn Thái Ngọc Duy | 4914c96 | 2012-10-26 22:53:52 +0700 | [diff] [blame] | 7289 | void setup_diff_pager(struct diff_options *opt) |
| 7290 | { |
| 7291 | /* |
| 7292 | * If the user asked for our exit code, then either they want --quiet |
| 7293 | * or --exit-code. We should definitely not bother with a pager in the |
| 7294 | * former case, as we will generate no output. Since we still properly |
| 7295 | * report our exit code even when a pager is run, we _could_ run a |
| 7296 | * pager with --exit-code. But since we have not done so historically, |
| 7297 | * and because it is easy to find people oneline advising "git diff |
| 7298 | * --exit-code" in hooks and other scripts, we do not do so. |
| 7299 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7300 | if (!opt->flags.exit_with_status && |
Nguyễn Thái Ngọc Duy | 4914c96 | 2012-10-26 22:53:52 +0700 | [diff] [blame] | 7301 | check_pager_config("diff") != 0) |
| 7302 | setup_pager(); |
| 7303 | } |