blob: fe3abac79feb46c255aafb25bab97aeaea583859 [file] [log] [blame]
Junio C Hamano6973dca2006-04-21 23:57:45 -07001/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
Junio C Hamano6973dca2006-04-21 23:57:45 -07004#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07005#include "config.h"
Michael Haggerty284098f2015-08-12 19:12:01 +02006#include "tempfile.h"
Junio C Hamano6973dca2006-04-21 23:57:45 -07007#include "quote.h"
8#include "diff.h"
9#include "diffcore.h"
Junio C Hamano051308f2006-05-04 16:51:44 -070010#include "delta.h"
Junio C Hamano6973dca2006-04-21 23:57:45 -070011#include "xdiff-interface.h"
Jeff King7c92fe02006-09-08 04:03:18 -040012#include "color.h"
Junio C Hamano8c701242007-04-12 23:05:29 -070013#include "attr.h"
Johannes Sixtd5535ec2007-10-19 21:47:56 +020014#include "run-command.h"
Junio C Hamano23707812008-01-02 01:50:11 -080015#include "utf8.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070016#include "object-store.h"
Jeff Kingbe58e702008-10-05 17:43:21 -040017#include "userdiff.h"
Heiko Voigt851e18c2015-08-17 17:21:59 -070018#include "submodule-config.h"
Johannes Schindelin752c0c22009-10-19 14:38:32 +020019#include "submodule.h"
Stefan Beller2e2d5ac2017-06-30 13:53:07 -070020#include "hashmap.h"
Junio C Hamanoa757c642010-03-24 19:21:32 -070021#include "ll-merge.h"
Michael Haggerty02e8ca02012-10-28 17:50:54 +010022#include "string-list.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040023#include "strvec.h"
Jacob Keller660e1132016-08-31 16:27:20 -070024#include "graph.h"
Jonathan Tan150e3002017-08-18 15:20:36 -070025#include "packfile.h"
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +070026#include "parse-options.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +020027#include "help.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020028#include "promisor-remote.h"
Junio C Hamano6973dca2006-04-21 23:57:45 -070029
Shawn O. Pearce1510fea2006-12-14 06:15:57 -050030#ifdef NO_FAST_WORKING_DIRECTORY
31#define FAST_WORKING_DIRECTORY 0
32#else
33#define FAST_WORKING_DIRECTORY 1
34#endif
35
David Rientjes96f1e582006-08-15 10:23:48 -070036static int diff_detect_rename_default;
Stefan Beller33de7162017-05-08 12:03:38 -040037static int diff_indent_heuristic = 1;
Jeff King92c57e52011-02-19 05:21:28 -050038static int diff_rename_limit_default = 400;
Jim Meyeringa624eaa2008-08-15 13:39:26 +020039static int diff_suppress_blank_empty;
Junio C Hamanod2aea132012-09-15 13:59:59 -070040static int diff_use_color_default = -1;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -070041static int diff_color_moved_default;
Stefan Beller626c0b52018-07-18 12:31:56 -070042static int diff_color_moved_ws_default;
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -040043static int diff_context_default = 3;
Vegard Nossumc4888672017-01-12 13:21:11 +010044static int diff_interhunk_context_default;
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -060045static const char *diff_word_regex_cfg;
Johannes Schindelincbe02102007-12-17 13:42:20 +000046static const char *external_diff_cmd_cfg;
Samuel Bronson6d8940b2013-12-18 19:08:12 -050047static const char *diff_order_file_cfg;
Junio C Hamanoaecbf912007-08-31 13:13:42 -070048int diff_auto_refresh_index = 1;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -070049static int diff_mnemonic_prefix;
Eli Collinsf89504d2010-05-02 19:03:41 -070050static int diff_no_prefix;
Laurent Arnoudc28ded82020-05-22 12:46:18 +020051static int diff_relative;
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +010052static int diff_stat_graph_width;
Johan Herland712d2c72011-04-29 11:36:20 +020053static int diff_dirstat_permille_default = 30;
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +020054static struct diff_options default_diff_options;
Michal Privoznik07ab4de2013-01-16 08:51:57 +010055static long diff_algorithm;
Junio C Hamanoa17505f2016-10-04 15:26:27 -070056static unsigned ws_error_highlight_default = WSEH_NEW;
Junio C Hamano6973dca2006-04-21 23:57:45 -070057
Jeff King7c92fe02006-09-08 04:03:18 -040058static char diff_colors[][COLOR_MAXLEN] = {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010059 GIT_COLOR_RESET,
Jeff King8dbf3eb2015-05-27 16:48:46 -040060 GIT_COLOR_NORMAL, /* CONTEXT */
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010061 GIT_COLOR_BOLD, /* METAINFO */
62 GIT_COLOR_CYAN, /* FRAGINFO */
63 GIT_COLOR_RED, /* OLD */
64 GIT_COLOR_GREEN, /* NEW */
65 GIT_COLOR_YELLOW, /* COMMIT */
66 GIT_COLOR_BG_RED, /* WHITESPACE */
Bert Wesarg89cb73a2009-11-27 07:55:18 +010067 GIT_COLOR_NORMAL, /* FUNCINFO */
Stefan Beller86b452e2017-06-30 13:53:09 -070068 GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */
69 GIT_COLOR_BOLD_BLUE, /* OLD_MOVED ALTERNATIVE */
70 GIT_COLOR_FAINT, /* OLD_MOVED_DIM */
71 GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */
72 GIT_COLOR_BOLD_CYAN, /* NEW_MOVED */
73 GIT_COLOR_BOLD_YELLOW, /* NEW_MOVED ALTERNATIVE */
74 GIT_COLOR_FAINT, /* NEW_MOVED_DIM */
75 GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */
Johannes Schindelina7be92a2018-08-13 04:33:32 -070076 GIT_COLOR_FAINT, /* CONTEXT_DIM */
77 GIT_COLOR_FAINT_RED, /* OLD_DIM */
78 GIT_COLOR_FAINT_GREEN, /* NEW_DIM */
79 GIT_COLOR_BOLD, /* CONTEXT_BOLD */
80 GIT_COLOR_BOLD_RED, /* OLD_BOLD */
81 GIT_COLOR_BOLD_GREEN, /* NEW_BOLD */
Johannes Schindelincd112ce2006-06-13 18:45:44 +020082};
83
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +020084static const char *color_diff_slots[] = {
85 [DIFF_CONTEXT] = "context",
86 [DIFF_METAINFO] = "meta",
87 [DIFF_FRAGINFO] = "frag",
88 [DIFF_FILE_OLD] = "old",
89 [DIFF_FILE_NEW] = "new",
90 [DIFF_COMMIT] = "commit",
91 [DIFF_WHITESPACE] = "whitespace",
92 [DIFF_FUNCINFO] = "func",
93 [DIFF_FILE_OLD_MOVED] = "oldMoved",
94 [DIFF_FILE_OLD_MOVED_ALT] = "oldMovedAlternative",
95 [DIFF_FILE_OLD_MOVED_DIM] = "oldMovedDimmed",
96 [DIFF_FILE_OLD_MOVED_ALT_DIM] = "oldMovedAlternativeDimmed",
97 [DIFF_FILE_NEW_MOVED] = "newMoved",
98 [DIFF_FILE_NEW_MOVED_ALT] = "newMovedAlternative",
99 [DIFF_FILE_NEW_MOVED_DIM] = "newMovedDimmed",
100 [DIFF_FILE_NEW_MOVED_ALT_DIM] = "newMovedAlternativeDimmed",
Johannes Schindelina7be92a2018-08-13 04:33:32 -0700101 [DIFF_CONTEXT_DIM] = "contextDimmed",
102 [DIFF_FILE_OLD_DIM] = "oldDimmed",
103 [DIFF_FILE_NEW_DIM] = "newDimmed",
104 [DIFF_CONTEXT_BOLD] = "contextBold",
105 [DIFF_FILE_OLD_BOLD] = "oldBold",
106 [DIFF_FILE_NEW_BOLD] = "newBold",
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200107};
108
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +0200109define_list_config_array_extra(color_diff_slots, {"plain"});
110
Jeff King9e1a5eb2014-06-18 15:41:50 -0400111static int parse_diff_color_slot(const char *var)
Junio C Hamano801235c2006-06-24 04:06:23 -0700112{
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200113 if (!strcasecmp(var, "plain"))
Jeff King8dbf3eb2015-05-27 16:48:46 -0400114 return DIFF_CONTEXT;
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200115 return LOOKUP_CONFIG(color_diff_slots, var);
Junio C Hamano801235c2006-06-24 04:06:23 -0700116}
117
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100118static int parse_dirstat_params(struct diff_options *options, const char *params_string,
Johan Herland51670fc2011-04-29 11:36:22 +0200119 struct strbuf *errmsg)
Johan Herland333f3fb2011-04-29 11:36:18 +0200120{
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100121 char *params_copy = xstrdup(params_string);
122 struct string_list params = STRING_LIST_INIT_NODUP;
123 int ret = 0;
124 int i;
Johan Herland51670fc2011-04-29 11:36:22 +0200125
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100126 if (*params_copy)
127 string_list_split_in_place(&params, params_copy, ',', -1);
128 for (i = 0; i < params.nr; i++) {
129 const char *p = params.items[i].string;
130 if (!strcmp(p, "changes")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700131 options->flags.dirstat_by_line = 0;
132 options->flags.dirstat_by_file = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100133 } else if (!strcmp(p, "lines")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700134 options->flags.dirstat_by_line = 1;
135 options->flags.dirstat_by_file = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100136 } else if (!strcmp(p, "files")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700137 options->flags.dirstat_by_line = 0;
138 options->flags.dirstat_by_file = 1;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100139 } else if (!strcmp(p, "noncumulative")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700140 options->flags.dirstat_cumulative = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100141 } else if (!strcmp(p, "cumulative")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700142 options->flags.dirstat_cumulative = 1;
Johan Herland333f3fb2011-04-29 11:36:18 +0200143 } else if (isdigit(*p)) {
144 char *end;
Johan Herland51670fc2011-04-29 11:36:22 +0200145 int permille = strtoul(p, &end, 10) * 10;
146 if (*end == '.' && isdigit(*++end)) {
Johan Herland712d2c72011-04-29 11:36:20 +0200147 /* only use first digit */
Johan Herland51670fc2011-04-29 11:36:22 +0200148 permille += *end - '0';
Johan Herland712d2c72011-04-29 11:36:20 +0200149 /* .. and ignore any further digits */
Johan Herland51670fc2011-04-29 11:36:22 +0200150 while (isdigit(*++end))
Johan Herland712d2c72011-04-29 11:36:20 +0200151 ; /* nothing */
152 }
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100153 if (!*end)
Johan Herland51670fc2011-04-29 11:36:22 +0200154 options->dirstat_permille = permille;
155 else {
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100156 strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
157 p);
Johan Herland51670fc2011-04-29 11:36:22 +0200158 ret++;
159 }
160 } else {
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100161 strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p);
Johan Herland51670fc2011-04-29 11:36:22 +0200162 ret++;
Johan Herland333f3fb2011-04-29 11:36:18 +0200163 }
Johan Herland51670fc2011-04-29 11:36:22 +0200164
Johan Herland333f3fb2011-04-29 11:36:18 +0200165 }
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100166 string_list_clear(&params, 0);
167 free(params_copy);
Johan Herland51670fc2011-04-29 11:36:22 +0200168 return ret;
Johan Herland333f3fb2011-04-29 11:36:18 +0200169}
170
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530171static int parse_submodule_params(struct diff_options *options, const char *value)
172{
173 if (!strcmp(value, "log"))
Jacob Keller61cfbc02016-08-31 16:27:21 -0700174 options->submodule_format = DIFF_SUBMODULE_LOG;
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530175 else if (!strcmp(value, "short"))
Jacob Keller61cfbc02016-08-31 16:27:21 -0700176 options->submodule_format = DIFF_SUBMODULE_SHORT;
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700177 else if (!strcmp(value, "diff"))
178 options->submodule_format = DIFF_SUBMODULE_INLINE_DIFF;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700179 /*
180 * Please update $__git_diff_submodule_formats in
181 * git-completion.bash when you add new formats.
182 */
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530183 else
184 return -1;
185 return 0;
186}
187
Ben Peart85b46032018-05-02 16:01:14 +0000188int git_config_rename(const char *var, const char *value)
Linus Torvaldscced5fb2009-04-09 11:46:15 -0700189{
190 if (!value)
191 return DIFF_DETECT_RENAME;
192 if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy"))
193 return DIFF_DETECT_COPY;
194 return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0;
195}
196
Michal Privoznik07924d42013-01-16 08:51:58 +0100197long parse_algorithm_value(const char *value)
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100198{
199 if (!value)
200 return -1;
201 else if (!strcasecmp(value, "myers") || !strcasecmp(value, "default"))
202 return 0;
203 else if (!strcasecmp(value, "minimal"))
204 return XDF_NEED_MINIMAL;
205 else if (!strcasecmp(value, "patience"))
206 return XDF_PATIENCE_DIFF;
207 else if (!strcasecmp(value, "histogram"))
208 return XDF_HISTOGRAM_DIFF;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700209 /*
210 * Please update $__git_diff_algorithms in git-completion.bash
211 * when you add new algorithms.
212 */
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100213 return -1;
214}
215
Junio C Hamano0b4b42e2016-10-04 15:09:18 -0700216static int parse_one_token(const char **arg, const char *token)
217{
218 const char *rest;
219 if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) {
220 *arg = rest;
221 return 1;
222 }
223 return 0;
224}
225
226static int parse_ws_error_highlight(const char *arg)
227{
228 const char *orig_arg = arg;
229 unsigned val = 0;
230
231 while (*arg) {
232 if (parse_one_token(&arg, "none"))
233 val = 0;
234 else if (parse_one_token(&arg, "default"))
235 val = WSEH_NEW;
236 else if (parse_one_token(&arg, "all"))
237 val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT;
238 else if (parse_one_token(&arg, "new"))
239 val |= WSEH_NEW;
240 else if (parse_one_token(&arg, "old"))
241 val |= WSEH_OLD;
242 else if (parse_one_token(&arg, "context"))
243 val |= WSEH_CONTEXT;
244 else {
245 return -1 - (int)(arg - orig_arg);
246 }
247 if (*arg)
248 arg++;
249 }
250 return val;
251}
252
Junio C Hamanoe0e324a2007-07-07 01:49:58 -0700253/*
Junio C Hamano83ad63c2006-07-08 01:05:16 -0700254 * These are to give UI layer defaults.
255 * The core-level commands such as git-diff-files should
256 * never be affected by the setting of diff.renames
257 * the user happens to have in the configuration file.
258 */
Matthieu Moy5404c112016-02-25 09:59:21 +0100259void init_diff_ui_defaults(void)
260{
Nguyễn Thái Ngọc Duy06dba2b2017-12-27 17:18:35 +0700261 diff_detect_rename_default = DIFF_DETECT_RENAME;
Matthieu Moy5404c112016-02-25 09:59:21 +0100262}
263
Michael Haggerty5b162872016-09-05 11:44:53 +0200264int git_diff_heuristic_config(const char *var, const char *value, void *cb)
265{
Junio C Hamano3cde4e02016-12-23 12:32:22 -0800266 if (!strcmp(var, "diff.indentheuristic"))
Michael Haggerty5b162872016-09-05 11:44:53 +0200267 diff_indent_heuristic = git_config_bool(var, value);
Michael Haggerty5b162872016-09-05 11:44:53 +0200268 return 0;
269}
270
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700271static int parse_color_moved(const char *arg)
272{
273 switch (git_parse_maybe_bool(arg)) {
274 case 0:
275 return COLOR_MOVED_NO;
276 case 1:
277 return COLOR_MOVED_DEFAULT;
278 default:
279 break;
280 }
281
282 if (!strcmp(arg, "no"))
283 return COLOR_MOVED_NO;
Stefan Beller176841f2017-06-30 13:53:08 -0700284 else if (!strcmp(arg, "plain"))
285 return COLOR_MOVED_PLAIN;
Stefan Beller51da15e2018-07-16 16:05:39 -0700286 else if (!strcmp(arg, "blocks"))
287 return COLOR_MOVED_BLOCKS;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700288 else if (!strcmp(arg, "zebra"))
289 return COLOR_MOVED_ZEBRA;
290 else if (!strcmp(arg, "default"))
291 return COLOR_MOVED_DEFAULT;
Eric Sunshinee3f2f5f2018-07-24 17:58:45 -0400292 else if (!strcmp(arg, "dimmed-zebra"))
293 return COLOR_MOVED_ZEBRA_DIM;
Stefan Beller86b452e2017-06-30 13:53:09 -0700294 else if (!strcmp(arg, "dimmed_zebra"))
295 return COLOR_MOVED_ZEBRA_DIM;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700296 else
Junio C Hamano706b0b52018-08-15 15:08:22 -0700297 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700298}
299
Stefan Bellerd173e792018-11-13 13:33:57 -0800300static unsigned parse_color_moved_ws(const char *arg)
Stefan Bellerb3095712018-07-16 16:05:40 -0700301{
302 int ret = 0;
303 struct string_list l = STRING_LIST_INIT_DUP;
304 struct string_list_item *i;
305
306 string_list_split(&l, arg, ',', -1);
307
308 for_each_string_list_item(i, &l) {
309 struct strbuf sb = STRBUF_INIT;
310 strbuf_addstr(&sb, i->string);
311 strbuf_trim(&sb);
312
Phillip Woodb73bcba2018-11-23 11:16:52 +0000313 if (!strcmp(sb.buf, "no"))
314 ret = 0;
315 else if (!strcmp(sb.buf, "ignore-space-change"))
Stefan Bellerb3095712018-07-16 16:05:40 -0700316 ret |= XDF_IGNORE_WHITESPACE_CHANGE;
317 else if (!strcmp(sb.buf, "ignore-space-at-eol"))
318 ret |= XDF_IGNORE_WHITESPACE_AT_EOL;
319 else if (!strcmp(sb.buf, "ignore-all-space"))
320 ret |= XDF_IGNORE_WHITESPACE;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700321 else if (!strcmp(sb.buf, "allow-indentation-change"))
322 ret |= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE;
Stefan Bellerd173e792018-11-13 13:33:57 -0800323 else {
324 ret |= COLOR_MOVED_WS_ERROR;
325 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);
326 }
Stefan Bellerb3095712018-07-16 16:05:40 -0700327
328 strbuf_release(&sb);
329 }
330
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700331 if ((ret & COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) &&
Stefan Bellerd173e792018-11-13 13:33:57 -0800332 (ret & XDF_WHITESPACE_FLAGS)) {
Junio C Hamano15b07cb2019-01-29 12:47:53 -0800333 error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes"));
Stefan Bellerd173e792018-11-13 13:33:57 -0800334 ret |= COLOR_MOVED_WS_ERROR;
335 }
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700336
Stefan Bellerb3095712018-07-16 16:05:40 -0700337 string_list_clear(&l, 0);
338
339 return ret;
Jeff Kinge269eb72011-08-17 22:03:48 -0700340}
Junio C Hamano801235c2006-06-24 04:06:23 -0700341
342int git_diff_ui_config(const char *var, const char *value, void *cb)
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -0400343{
344 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
345 diff_use_color_default = git_config_colorbool(var, value);
346 return 0;
347 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700348 if (!strcmp(var, "diff.colormoved")) {
349 int cm = parse_color_moved(value);
350 if (cm < 0)
351 return -1;
352 diff_color_moved_default = cm;
353 return 0;
354 }
Stefan Beller626c0b52018-07-18 12:31:56 -0700355 if (!strcmp(var, "diff.colormovedws")) {
Stefan Bellerd173e792018-11-13 13:33:57 -0800356 unsigned cm = parse_color_moved_ws(value);
357 if (cm & COLOR_MOVED_WS_ERROR)
Stefan Beller626c0b52018-07-18 12:31:56 -0700358 return -1;
359 diff_color_moved_ws_default = cm;
360 return 0;
361 }
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -0400362 if (!strcmp(var, "diff.context")) {
363 diff_context_default = git_config_int(var, value);
364 if (diff_context_default < 0)
365 return -1;
366 return 0;
367 }
Vegard Nossumc4888672017-01-12 13:21:11 +0100368 if (!strcmp(var, "diff.interhunkcontext")) {
369 diff_interhunk_context_default = git_config_int(var, value);
370 if (diff_interhunk_context_default < 0)
371 return -1;
372 return 0;
373 }
Eric Wongb68ea122006-07-07 04:01:23 -0700374 if (!strcmp(var, "diff.renames")) {
Linus Torvaldscced5fb2009-04-09 11:46:15 -0700375 diff_detect_rename_default = git_config_rename(var, value);
Eric Wongb68ea122006-07-07 04:01:23 -0700376 return 0;
377 }
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700378 if (!strcmp(var, "diff.autorefreshindex")) {
379 diff_auto_refresh_index = git_config_bool(var, value);
380 return 0;
381 }
Junio C Hamanoa5a818e2008-08-18 20:08:09 -0700382 if (!strcmp(var, "diff.mnemonicprefix")) {
383 diff_mnemonic_prefix = git_config_bool(var, value);
384 return 0;
385 }
Eli Collinsf89504d2010-05-02 19:03:41 -0700386 if (!strcmp(var, "diff.noprefix")) {
387 diff_no_prefix = git_config_bool(var, value);
388 return 0;
389 }
Laurent Arnoudc28ded82020-05-22 12:46:18 +0200390 if (!strcmp(var, "diff.relative")) {
391 diff_relative = git_config_bool(var, value);
392 return 0;
393 }
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +0100394 if (!strcmp(var, "diff.statgraphwidth")) {
395 diff_stat_graph_width = git_config_int(var, value);
396 return 0;
397 }
Brian Hetrodaec8082008-07-05 01:24:43 -0400398 if (!strcmp(var, "diff.external"))
399 return git_config_string(&external_diff_cmd_cfg, var, value);
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -0600400 if (!strcmp(var, "diff.wordregex"))
401 return git_config_string(&diff_word_regex_cfg, var, value);
Samuel Bronson6d8940b2013-12-18 19:08:12 -0500402 if (!strcmp(var, "diff.orderfile"))
403 return git_config_pathname(&diff_order_file_cfg, var, value);
Junio C Hamanof1af60b2007-04-22 17:52:55 -0700404
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +0200405 if (!strcmp(var, "diff.ignoresubmodules"))
406 handle_ignore_submodules_arg(&default_diff_options, value);
407
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530408 if (!strcmp(var, "diff.submodule")) {
409 if (parse_submodule_params(&default_diff_options, value))
410 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
411 value);
412 return 0;
413 }
414
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100415 if (!strcmp(var, "diff.algorithm")) {
416 diff_algorithm = parse_algorithm_value(value);
417 if (diff_algorithm < 0)
418 return -1;
419 return 0;
420 }
421
Jeff King33c643b2017-10-13 13:24:31 -0400422 if (git_color_config(var, value, cb) < 0)
423 return -1;
424
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100425 return git_diff_basic_config(var, value, cb);
Jeff King9a1805a2008-01-04 03:59:34 -0500426}
427
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100428int git_diff_basic_config(const char *var, const char *value, void *cb)
Jeff King9a1805a2008-01-04 03:59:34 -0500429{
Jeff Kingae021d82014-06-18 15:47:50 -0400430 const char *name;
431
Linus Torvalds2b6ca6d2008-08-05 11:27:30 -0700432 if (!strcmp(var, "diff.renamelimit")) {
433 diff_rename_limit_default = git_config_int(var, value);
434 return 0;
435 }
436
Jeff King6680a082012-02-07 13:23:02 -0500437 if (userdiff_config(var, value) < 0)
438 return -1;
Jeff Kingc7534ef2008-10-26 00:45:55 -0400439
Jeff Kingae021d82014-06-18 15:47:50 -0400440 if (skip_prefix(var, "diff.color.", &name) ||
441 skip_prefix(var, "color.diff.", &name)) {
442 int slot = parse_diff_color_slot(name);
Jeff King8b8e8622009-12-12 07:25:24 -0500443 if (slot < 0)
444 return 0;
Junio C Hamano64f30e92008-02-11 10:53:56 -0800445 if (!value)
446 return config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 15:33:09 -0400447 return color_parse(value, diff_colors[slot]);
Junio C Hamano801235c2006-06-24 04:06:23 -0700448 }
Junio C Hamanof1af60b2007-04-22 17:52:55 -0700449
Jeff Kingda806352020-01-31 04:57:49 -0500450 if (!strcmp(var, "diff.wserrorhighlight")) {
451 int val = parse_ws_error_highlight(value);
452 if (val < 0)
453 return -1;
454 ws_error_highlight_default = val;
455 return 0;
456 }
457
Jim Meyeringa624eaa2008-08-15 13:39:26 +0200458 /* like GNU diff's --suppress-blank-empty option */
Johannes Schindelin950db872009-01-20 22:08:33 +0100459 if (!strcmp(var, "diff.suppressblankempty") ||
460 /* for backwards compatibility */
461 !strcmp(var, "diff.suppress-blank-empty")) {
Jim Meyeringa624eaa2008-08-15 13:39:26 +0200462 diff_suppress_blank_empty = git_config_bool(var, value);
463 return 0;
464 }
465
Johan Herland2d174952011-04-29 11:36:19 +0200466 if (!strcmp(var, "diff.dirstat")) {
Johan Herland51670fc2011-04-29 11:36:22 +0200467 struct strbuf errmsg = STRBUF_INIT;
Johan Herland712d2c72011-04-29 11:36:20 +0200468 default_diff_options.dirstat_permille = diff_dirstat_permille_default;
Johan Herland51670fc2011-04-29 11:36:22 +0200469 if (parse_dirstat_params(&default_diff_options, value, &errmsg))
Johan Herland7478ac52011-04-29 11:36:23 +0200470 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
Johan Herland51670fc2011-04-29 11:36:22 +0200471 errmsg.buf);
472 strbuf_release(&errmsg);
Johan Herland712d2c72011-04-29 11:36:20 +0200473 diff_dirstat_permille_default = default_diff_options.dirstat_permille;
Johan Herland2d174952011-04-29 11:36:19 +0200474 return 0;
475 }
476
Marc Branchaudcf5e7722017-05-08 12:03:36 -0400477 if (git_diff_heuristic_config(var, value, cb) < 0)
478 return -1;
479
Jeff King3e1dd172011-08-17 22:05:08 -0700480 return git_default_config(var, value, cb);
Junio C Hamano801235c2006-06-24 04:06:23 -0700481}
482
Junio C Hamano6973dca2006-04-21 23:57:45 -0700483static char *quote_two(const char *one, const char *two)
484{
Junio C Hamano7c37c972020-09-10 10:01:59 -0700485 int need_one = quote_c_style(one, NULL, NULL, CQUOTE_NODQ);
486 int need_two = quote_c_style(two, NULL, NULL, CQUOTE_NODQ);
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500487 struct strbuf res = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700488
489 if (need_one + need_two) {
Pierre Habouzit663af342007-09-20 00:42:15 +0200490 strbuf_addch(&res, '"');
Junio C Hamano7c37c972020-09-10 10:01:59 -0700491 quote_c_style(one, &res, NULL, CQUOTE_NODQ);
492 quote_c_style(two, &res, NULL, CQUOTE_NODQ);
Pierre Habouzit663af342007-09-20 00:42:15 +0200493 strbuf_addch(&res, '"');
494 } else {
495 strbuf_addstr(&res, one);
496 strbuf_addstr(&res, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700497 }
Pierre Habouzitb315c5c2007-09-27 12:58:23 +0200498 return strbuf_detach(&res, NULL);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700499}
500
501static const char *external_diff(void)
502{
503 static const char *external_diff_cmd = NULL;
504 static int done_preparing = 0;
505
506 if (done_preparing)
507 return external_diff_cmd;
Kim Gybels6776a842019-01-11 21:26:08 +0100508 external_diff_cmd = xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
Johannes Schindelincbe02102007-12-17 13:42:20 +0000509 if (!external_diff_cmd)
510 external_diff_cmd = external_diff_cmd_cfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700511 done_preparing = 1;
512 return external_diff_cmd;
513}
514
Michael Haggerty284098f2015-08-12 19:12:01 +0200515/*
516 * Keep track of files used for diffing. Sometimes such an entry
517 * refers to a temporary file, sometimes to an existing file, and
518 * sometimes to "/dev/null".
519 */
Junio C Hamano6973dca2006-04-21 23:57:45 -0700520static struct diff_tempfile {
Michael Haggerty284098f2015-08-12 19:12:01 +0200521 /*
522 * filename external diff should read from, or NULL if this
523 * entry is currently not in use:
524 */
525 const char *name;
526
brian m. carlsondc015052017-03-26 16:01:24 +0000527 char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano6973dca2006-04-21 23:57:45 -0700528 char mode[10];
Michael Haggerty284098f2015-08-12 19:12:01 +0200529
530 /*
531 * If this diff_tempfile instance refers to a temporary file,
532 * this tempfile object is used to manage its lifetime.
533 */
Jeff King076aa2c2017-09-05 08:15:08 -0400534 struct tempfile *tempfile;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700535} diff_temp[2];
536
Junio C Hamano6957eb92009-09-14 18:44:01 -0700537struct emit_callback {
Junio C Hamano6957eb92009-09-14 18:44:01 -0700538 int color_diff;
539 unsigned ws_rule;
540 int blank_at_eof_in_preimage;
541 int blank_at_eof_in_postimage;
542 int lno_in_preimage;
543 int lno_in_postimage;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700544 const char **label_path;
545 struct diff_words_data *diff_words;
Bo Yanga3c158d2010-05-26 15:08:02 +0800546 struct diff_options *opt;
Greg Bacon3e97c7c2009-11-19 15:12:24 -0600547 struct strbuf *header;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700548};
549
Junio C Hamano6973dca2006-04-21 23:57:45 -0700550static int count_lines(const char *data, int size)
551{
552 int count, ch, completely_empty = 1, nl_just_seen = 0;
553 count = 0;
554 while (0 < size--) {
555 ch = *data++;
556 if (ch == '\n') {
557 count++;
558 nl_just_seen = 1;
559 completely_empty = 0;
560 }
561 else {
562 nl_just_seen = 0;
563 completely_empty = 0;
564 }
565 }
566 if (completely_empty)
567 return 0;
568 if (!nl_just_seen)
569 count++; /* no trailing newline */
570 return count;
571}
572
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200573static int fill_mmfile(struct repository *r, mmfile_t *mf,
574 struct diff_filespec *one)
Junio C Hamano6957eb92009-09-14 18:44:01 -0700575{
576 if (!DIFF_FILE_VALID(one)) {
577 mf->ptr = (char *)""; /* does not matter */
578 mf->size = 0;
579 return 0;
580 }
Jonathan Tan1c37e862020-04-07 15:11:41 -0700581 else if (diff_populate_filespec(r, one, NULL))
Junio C Hamano6957eb92009-09-14 18:44:01 -0700582 return -1;
Junio C Hamanobb35fef2009-09-15 03:38:30 -0700583
Junio C Hamano6957eb92009-09-14 18:44:01 -0700584 mf->ptr = one->data;
585 mf->size = one->size;
586 return 0;
587}
588
Jeff Kingabb371a2011-02-19 03:16:32 -0500589/* like fill_mmfile, but only for size, so we can avoid retrieving blob */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200590static unsigned long diff_filespec_size(struct repository *r,
591 struct diff_filespec *one)
Jeff Kingabb371a2011-02-19 03:16:32 -0500592{
Jonathan Tan1c37e862020-04-07 15:11:41 -0700593 struct diff_populate_filespec_options dpf_options = {
594 .check_size_only = 1,
595 };
596
Jeff Kingabb371a2011-02-19 03:16:32 -0500597 if (!DIFF_FILE_VALID(one))
598 return 0;
Jonathan Tan1c37e862020-04-07 15:11:41 -0700599 diff_populate_filespec(r, one, &dpf_options);
Jeff Kingabb371a2011-02-19 03:16:32 -0500600 return one->size;
601}
602
Junio C Hamano6957eb92009-09-14 18:44:01 -0700603static int count_trailing_blank(mmfile_t *mf, unsigned ws_rule)
604{
605 char *ptr = mf->ptr;
606 long size = mf->size;
607 int cnt = 0;
608
609 if (!size)
610 return cnt;
611 ptr += size - 1; /* pointing at the very end */
612 if (*ptr != '\n')
613 ; /* incomplete line */
614 else
615 ptr--; /* skip the last LF */
616 while (mf->ptr < ptr) {
617 char *prev_eol;
618 for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--)
619 if (*prev_eol == '\n')
620 break;
621 if (!ws_blank_line(prev_eol + 1, ptr - prev_eol, ws_rule))
622 break;
623 cnt++;
624 ptr = prev_eol - 1;
625 }
626 return cnt;
627}
628
629static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
630 struct emit_callback *ecbdata)
631{
632 int l1, l2, at;
633 unsigned ws_rule = ecbdata->ws_rule;
634 l1 = count_trailing_blank(mf1, ws_rule);
635 l2 = count_trailing_blank(mf2, ws_rule);
636 if (l2 <= l1) {
637 ecbdata->blank_at_eof_in_preimage = 0;
638 ecbdata->blank_at_eof_in_postimage = 0;
639 return;
640 }
641 at = count_lines(mf1->ptr, mf1->size);
642 ecbdata->blank_at_eof_in_preimage = (at - l1) + 1;
643
644 at = count_lines(mf2->ptr, mf2->size);
645 ecbdata->blank_at_eof_in_postimage = (at - l2) + 1;
646}
647
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -0700648static void emit_line_0(struct diff_options *o,
Stefan Beller017ac452018-08-13 18:41:19 -0700649 const char *set_sign, const char *set, unsigned reverse, const char *reset,
Junio C Hamano250f7992009-09-14 18:44:01 -0700650 int first, const char *line, int len)
Junio C Hamano6957eb92009-09-14 18:44:01 -0700651{
652 int has_trailing_newline, has_trailing_carriage_return;
Stefan Beller44410672018-08-13 18:41:22 -0700653 int needs_reset = 0; /* at the end of the line */
Bo Yanga3c158d2010-05-26 15:08:02 +0800654 FILE *file = o->file;
655
Stefan Bellerf103a6f2018-08-13 18:41:21 -0700656 fputs(diff_line_prefix(o), file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700657
Stefan Beller44410672018-08-13 18:41:22 -0700658 has_trailing_newline = (len > 0 && line[len-1] == '\n');
659 if (has_trailing_newline)
660 len--;
661
662 has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
663 if (has_trailing_carriage_return)
664 len--;
665
666 if (!len && !first)
667 goto end_of_line;
668
669 if (reverse && want_color(o->use_color)) {
670 fputs(GIT_COLOR_REVERSE, file);
671 needs_reset = 1;
Junio C Hamano250f7992009-09-14 18:44:01 -0700672 }
Junio C Hamano6957eb92009-09-14 18:44:01 -0700673
Stefan Beller44410672018-08-13 18:41:22 -0700674 if (set_sign) {
675 fputs(set_sign, file);
676 needs_reset = 1;
Junio C Hamano06a47552009-11-27 22:04:10 -0800677 }
Stefan Beller44410672018-08-13 18:41:22 -0700678
Junio C Hamano6957eb92009-09-14 18:44:01 -0700679 if (first)
Stefan Beller44410672018-08-13 18:41:22 -0700680 fputc(first, file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700681
Stefan Beller44410672018-08-13 18:41:22 -0700682 if (!len)
683 goto end_of_line;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700684
Stefan Beller44410672018-08-13 18:41:22 -0700685 if (set) {
686 if (set_sign && set != set_sign)
687 fputs(reset, file);
Junio C Hamano250f7992009-09-14 18:44:01 -0700688 fputs(set, file);
Stefan Beller44410672018-08-13 18:41:22 -0700689 needs_reset = 1;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700690 }
Stefan Beller44410672018-08-13 18:41:22 -0700691 fwrite(line, len, 1, file);
692 needs_reset = 1; /* 'line' may contain color codes. */
693
694end_of_line:
695 if (needs_reset)
696 fputs(reset, file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700697 if (has_trailing_carriage_return)
698 fputc('\r', file);
699 if (has_trailing_newline)
700 fputc('\n', file);
701}
702
Bo Yanga3c158d2010-05-26 15:08:02 +0800703static void emit_line(struct diff_options *o, const char *set, const char *reset,
Junio C Hamano250f7992009-09-14 18:44:01 -0700704 const char *line, int len)
705{
Stefan Beller44410672018-08-13 18:41:22 -0700706 emit_line_0(o, set, NULL, 0, reset, 0, line, len);
Junio C Hamano250f7992009-09-14 18:44:01 -0700707}
708
Stefan Beller36a4cef2017-06-29 17:06:49 -0700709enum diff_symbol {
Stefan Beller4eed0eb2017-06-29 17:07:01 -0700710 DIFF_SYMBOL_BINARY_DIFF_HEADER,
711 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
712 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
713 DIFF_SYMBOL_BINARY_DIFF_BODY,
714 DIFF_SYMBOL_BINARY_DIFF_FOOTER,
Stefan Beller0911c472017-06-29 17:07:02 -0700715 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
716 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
717 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
718 DIFF_SYMBOL_STATS_LINE,
Stefan Bellerbd033292017-06-29 17:07:03 -0700719 DIFF_SYMBOL_WORD_DIFF,
Stefan Beller30b7e1e2017-06-29 17:07:04 -0700720 DIFF_SYMBOL_STAT_SEP,
Stefan Beller146fdb02017-06-29 17:07:05 -0700721 DIFF_SYMBOL_SUMMARY,
Stefan Bellerf3597132017-06-29 17:07:00 -0700722 DIFF_SYMBOL_SUBMODULE_ADD,
723 DIFF_SYMBOL_SUBMODULE_DEL,
724 DIFF_SYMBOL_SUBMODULE_UNTRACKED,
725 DIFF_SYMBOL_SUBMODULE_MODIFIED,
726 DIFF_SYMBOL_SUBMODULE_HEADER,
727 DIFF_SYMBOL_SUBMODULE_ERROR,
728 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH,
Stefan Beller5af6ea92017-06-29 17:06:59 -0700729 DIFF_SYMBOL_REWRITE_DIFF,
Stefan Beller4acaaa72017-06-29 17:06:58 -0700730 DIFF_SYMBOL_BINARY_FILES,
Stefan Bellera29b0a12017-06-29 17:06:57 -0700731 DIFF_SYMBOL_HEADER,
Stefan Beller3ee8b7b2017-06-29 17:06:56 -0700732 DIFF_SYMBOL_FILEPAIR_PLUS,
733 DIFF_SYMBOL_FILEPAIR_MINUS,
Stefan Bellerff958672017-06-29 17:06:54 -0700734 DIFF_SYMBOL_WORDS_PORCELAIN,
735 DIFF_SYMBOL_WORDS,
Stefan Beller091f8e22017-06-29 17:06:53 -0700736 DIFF_SYMBOL_CONTEXT,
Stefan Bellerf2bb1212017-06-29 17:06:55 -0700737 DIFF_SYMBOL_CONTEXT_INCOMPLETE,
Stefan Beller091f8e22017-06-29 17:06:53 -0700738 DIFF_SYMBOL_PLUS,
739 DIFF_SYMBOL_MINUS,
Stefan Bellerb9cbfde2017-06-29 17:06:52 -0700740 DIFF_SYMBOL_NO_LF_EOF,
Stefan Beller68abc6f2017-06-29 17:06:51 -0700741 DIFF_SYMBOL_CONTEXT_FRAGINFO,
Stefan Bellerc64b4202017-06-29 17:06:50 -0700742 DIFF_SYMBOL_CONTEXT_MARKER,
Stefan Beller36a4cef2017-06-29 17:06:49 -0700743 DIFF_SYMBOL_SEPARATOR
744};
Stefan Beller091f8e22017-06-29 17:06:53 -0700745/*
746 * Flags for content lines:
747 * 0..12 are whitespace rules
748 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
749 * 16 is marking if the line is blank at EOF
750 */
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700751#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
752#define DIFF_SYMBOL_MOVED_LINE (1<<17)
753#define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
Stefan Beller86b452e2017-06-30 13:53:09 -0700754#define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
Stefan Beller091f8e22017-06-29 17:06:53 -0700755#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
756
Stefan Bellere6e045f2017-06-29 17:07:06 -0700757/*
758 * This struct is used when we need to buffer the output of the diff output.
759 *
760 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
761 * into the pre/post image file. This pointer could be a union with the
762 * line pointer. By storing an offset into the file instead of the literal line,
763 * we can decrease the memory footprint for the buffered output. At first we
764 * may want to only have indirection for the content lines, but we could also
765 * enhance the state for emitting prefabricated lines, e.g. the similarity
766 * score line or hunk/file headers would only need to store a number or path
767 * and then the output can be constructed later on depending on state.
768 */
769struct emitted_diff_symbol {
770 const char *line;
771 int len;
772 int flags;
Phillip Wood21536d02018-11-23 11:16:57 +0000773 int indent_off; /* Offset to first non-whitespace character */
774 int indent_width; /* The visual width of the indentation */
Stefan Bellere6e045f2017-06-29 17:07:06 -0700775 enum diff_symbol s;
776};
777#define EMITTED_DIFF_SYMBOL_INIT {NULL}
778
779struct emitted_diff_symbols {
780 struct emitted_diff_symbol *buf;
781 int nr, alloc;
782};
783#define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0}
784
785static void append_emitted_diff_symbol(struct diff_options *o,
786 struct emitted_diff_symbol *e)
787{
788 struct emitted_diff_symbol *f;
789
790 ALLOC_GROW(o->emitted_symbols->buf,
791 o->emitted_symbols->nr + 1,
792 o->emitted_symbols->alloc);
793 f = &o->emitted_symbols->buf[o->emitted_symbols->nr++];
794
795 memcpy(f, e, sizeof(struct emitted_diff_symbol));
796 f->line = e->line ? xmemdupz(e->line, e->len) : NULL;
797}
798
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700799struct moved_entry {
800 struct hashmap_entry ent;
801 const struct emitted_diff_symbol *es;
802 struct moved_entry *next_line;
803};
804
Phillip Wood74d156f2018-10-04 11:07:41 +0100805struct moved_block {
806 struct moved_entry *match;
Phillip Wood21536d02018-11-23 11:16:57 +0000807 int wsd; /* The whitespace delta of this block */
Phillip Wood74d156f2018-10-04 11:07:41 +0100808};
809
810static void moved_block_clear(struct moved_block *b)
811{
Phillip Wood21536d02018-11-23 11:16:57 +0000812 memset(b, 0, sizeof(*b));
813}
814
Phillip Wood0cd51e92018-11-23 11:16:58 +0000815#define INDENT_BLANKLINE INT_MIN
816
Phillip Wood21536d02018-11-23 11:16:57 +0000817static void fill_es_indent_data(struct emitted_diff_symbol *es)
818{
Phillip Wood0cd51e92018-11-23 11:16:58 +0000819 unsigned int off = 0, i;
Phillip Wood21536d02018-11-23 11:16:57 +0000820 int width = 0, tab_width = es->flags & WS_TAB_WIDTH_MASK;
821 const char *s = es->line;
822 const int len = es->len;
823
824 /* skip any \v \f \r at start of indentation */
825 while (s[off] == '\f' || s[off] == '\v' ||
826 (s[off] == '\r' && off < len - 1))
827 off++;
828
829 /* calculate the visual width of indentation */
830 while(1) {
831 if (s[off] == ' ') {
832 width++;
833 off++;
834 } else if (s[off] == '\t') {
835 width += tab_width - (width % tab_width);
836 while (s[++off] == '\t')
837 width += tab_width;
838 } else {
839 break;
840 }
841 }
842
Phillip Wood0cd51e92018-11-23 11:16:58 +0000843 /* check if this line is blank */
844 for (i = off; i < len; i++)
845 if (!isspace(s[i]))
846 break;
847
848 if (i == len) {
849 es->indent_width = INDENT_BLANKLINE;
850 es->indent_off = len;
851 } else {
852 es->indent_off = off;
853 es->indent_width = width;
854 }
Phillip Wood74d156f2018-10-04 11:07:41 +0100855}
856
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700857static int compute_ws_delta(const struct emitted_diff_symbol *a,
Phillip Wood21536d02018-11-23 11:16:57 +0000858 const struct emitted_diff_symbol *b,
859 int *out)
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700860{
Phillip Wood21536d02018-11-23 11:16:57 +0000861 int a_len = a->len,
862 b_len = b->len,
863 a_off = a->indent_off,
864 a_width = a->indent_width,
865 b_off = b->indent_off,
866 b_width = b->indent_width;
867 int delta;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700868
Phillip Wood0cd51e92018-11-23 11:16:58 +0000869 if (a_width == INDENT_BLANKLINE && b_width == INDENT_BLANKLINE) {
870 *out = INDENT_BLANKLINE;
871 return 1;
872 }
873
Phillip Wood21536d02018-11-23 11:16:57 +0000874 if (a->s == DIFF_SYMBOL_PLUS)
875 delta = a_width - b_width;
876 else
877 delta = b_width - a_width;
878
879 if (a_len - a_off != b_len - b_off ||
880 memcmp(a->line + a_off, b->line + b_off, a_len - a_off))
Phillip Woodfe4516d2018-10-04 11:07:43 +0100881 return 0;
882
Phillip Wood21536d02018-11-23 11:16:57 +0000883 *out = delta;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700884
Phillip Woodfe4516d2018-10-04 11:07:43 +0100885 return 1;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700886}
887
888static int cmp_in_block_with_wsd(const struct diff_options *o,
889 const struct moved_entry *cur,
890 const struct moved_entry *match,
Phillip Wood74d156f2018-10-04 11:07:41 +0100891 struct moved_block *pmb,
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700892 int n)
893{
894 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
Phillip Wood7a4252c2018-11-23 11:16:56 +0000895 int al = cur->es->len, bl = match->es->len, cl = l->len;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700896 const char *a = cur->es->line,
897 *b = match->es->line,
898 *c = l->line;
Phillip Wood21536d02018-11-23 11:16:57 +0000899 int a_off = cur->es->indent_off,
900 a_width = cur->es->indent_width,
901 c_off = l->indent_off,
902 c_width = l->indent_width;
903 int delta;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700904
905 /*
Phillip Wood7a4252c2018-11-23 11:16:56 +0000906 * We need to check if 'cur' is equal to 'match'. As those
907 * are from the same (+/-) side, we do not need to adjust for
908 * indent changes. However these were found using fuzzy
909 * matching so we do have to check if they are equal. Here we
910 * just check the lengths. We delay calling memcmp() to check
911 * the contents until later as if the length comparison for a
912 * and c fails we can avoid the call all together.
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700913 */
Phillip Wood7a4252c2018-11-23 11:16:56 +0000914 if (al != bl)
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700915 return 1;
916
Phillip Wood0cd51e92018-11-23 11:16:58 +0000917 /* If 'l' and 'cur' are both blank then they match. */
918 if (a_width == INDENT_BLANKLINE && c_width == INDENT_BLANKLINE)
919 return 0;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700920
921 /*
Phillip Wood21536d02018-11-23 11:16:57 +0000922 * The indent changes of the block are known and stored in pmb->wsd;
923 * however we need to check if the indent changes of the current line
924 * match those of the current block and that the text of 'l' and 'cur'
925 * after the indentation match.
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700926 */
Phillip Wood21536d02018-11-23 11:16:57 +0000927 if (cur->es->s == DIFF_SYMBOL_PLUS)
928 delta = a_width - c_width;
929 else
930 delta = c_width - a_width;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700931
Phillip Wood0cd51e92018-11-23 11:16:58 +0000932 /*
933 * If the previous lines of this block were all blank then set its
934 * whitespace delta.
935 */
936 if (pmb->wsd == INDENT_BLANKLINE)
937 pmb->wsd = delta;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700938
Phillip Wood21536d02018-11-23 11:16:57 +0000939 return !(delta == pmb->wsd && al - a_off == cl - c_off &&
940 !memcmp(a, b, al) && !
941 memcmp(a + a_off, c + c_off, al - a_off));
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700942}
943
Stefan Belleree1df662018-07-16 16:05:38 -0700944static int moved_entry_cmp(const void *hashmap_cmp_fn_data,
Eric Wong939af162019-10-06 23:30:37 +0000945 const struct hashmap_entry *eptr,
946 const struct hashmap_entry *entry_or_key,
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700947 const void *keydata)
948{
Stefan Belleree1df662018-07-16 16:05:38 -0700949 const struct diff_options *diffopt = hashmap_cmp_fn_data;
Eric Wong939af162019-10-06 23:30:37 +0000950 const struct moved_entry *a, *b;
Stefan Bellerb3095712018-07-16 16:05:40 -0700951 unsigned flags = diffopt->color_moved_ws_handling
952 & XDF_WHITESPACE_FLAGS;
Stefan Belleree1df662018-07-16 16:05:38 -0700953
Eric Wong939af162019-10-06 23:30:37 +0000954 a = container_of(eptr, const struct moved_entry, ent);
955 b = container_of(entry_or_key, const struct moved_entry, ent);
956
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700957 if (diffopt->color_moved_ws_handling &
958 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
959 /*
960 * As there is not specific white space config given,
961 * we'd need to check for a new block, so ignore all
962 * white space. The setup of the white space
963 * configuration for the next block is done else where
964 */
965 flags |= XDF_IGNORE_WHITESPACE;
966
Stefan Beller01be97c2017-10-25 11:49:12 -0700967 return !xdiff_compare_lines(a->es->line, a->es->len,
968 b->es->line, b->es->len,
Stefan Bellerb3095712018-07-16 16:05:40 -0700969 flags);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700970}
971
972static struct moved_entry *prepare_entry(struct diff_options *o,
973 int line_no)
974{
975 struct moved_entry *ret = xmalloc(sizeof(*ret));
976 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];
Stefan Bellerb3095712018-07-16 16:05:40 -0700977 unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS;
Eric Wonge010a412019-10-06 23:30:24 +0000978 unsigned int hash = xdiff_hash_string(l->line, l->len, flags);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700979
Eric Wonge010a412019-10-06 23:30:24 +0000980 hashmap_entry_init(&ret->ent, hash);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700981 ret->es = l;
982 ret->next_line = NULL;
983
984 return ret;
985}
986
987static void add_lines_to_move_detection(struct diff_options *o,
988 struct hashmap *add_lines,
989 struct hashmap *del_lines)
990{
991 struct moved_entry *prev_line = NULL;
992
993 int n;
994 for (n = 0; n < o->emitted_symbols->nr; n++) {
995 struct hashmap *hm;
996 struct moved_entry *key;
997
998 switch (o->emitted_symbols->buf[n].s) {
999 case DIFF_SYMBOL_PLUS:
1000 hm = add_lines;
1001 break;
1002 case DIFF_SYMBOL_MINUS:
1003 hm = del_lines;
1004 break;
1005 default:
1006 prev_line = NULL;
1007 continue;
1008 }
1009
Phillip Wood21536d02018-11-23 11:16:57 +00001010 if (o->color_moved_ws_handling &
1011 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
1012 fill_es_indent_data(&o->emitted_symbols->buf[n]);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001013 key = prepare_entry(o, n);
1014 if (prev_line && prev_line->es->s == o->emitted_symbols->buf[n].s)
1015 prev_line->next_line = key;
1016
Eric Wongb94e5c12019-10-06 23:30:29 +00001017 hashmap_add(hm, &key->ent);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001018 prev_line = key;
1019 }
1020}
1021
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001022static void pmb_advance_or_null(struct diff_options *o,
1023 struct moved_entry *match,
1024 struct hashmap *hm,
Phillip Wood74d156f2018-10-04 11:07:41 +01001025 struct moved_block *pmb,
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001026 int pmb_nr)
1027{
1028 int i;
1029 for (i = 0; i < pmb_nr; i++) {
Phillip Wood74d156f2018-10-04 11:07:41 +01001030 struct moved_entry *prev = pmb[i].match;
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001031 struct moved_entry *cur = (prev && prev->next_line) ?
1032 prev->next_line : NULL;
Eric Wong939af162019-10-06 23:30:37 +00001033 if (cur && !hm->cmpfn(o, &cur->ent, &match->ent, NULL)) {
Phillip Wood74d156f2018-10-04 11:07:41 +01001034 pmb[i].match = cur;
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001035 } else {
Phillip Wood74d156f2018-10-04 11:07:41 +01001036 pmb[i].match = NULL;
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001037 }
1038 }
1039}
1040
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001041static void pmb_advance_or_null_multi_match(struct diff_options *o,
1042 struct moved_entry *match,
1043 struct hashmap *hm,
Phillip Wood74d156f2018-10-04 11:07:41 +01001044 struct moved_block *pmb,
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001045 int pmb_nr, int n)
1046{
1047 int i;
1048 char *got_match = xcalloc(1, pmb_nr);
1049
Eric Wong23dee692019-10-06 23:30:41 +00001050 hashmap_for_each_entry_from(hm, match, ent) {
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001051 for (i = 0; i < pmb_nr; i++) {
Phillip Wood74d156f2018-10-04 11:07:41 +01001052 struct moved_entry *prev = pmb[i].match;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001053 struct moved_entry *cur = (prev && prev->next_line) ?
1054 prev->next_line : NULL;
1055 if (!cur)
1056 continue;
Phillip Wood74d156f2018-10-04 11:07:41 +01001057 if (!cmp_in_block_with_wsd(o, cur, match, &pmb[i], n))
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001058 got_match[i] |= 1;
1059 }
1060 }
1061
1062 for (i = 0; i < pmb_nr; i++) {
1063 if (got_match[i]) {
Phillip Wood74d156f2018-10-04 11:07:41 +01001064 /* Advance to the next line */
1065 pmb[i].match = pmb[i].match->next_line;
Phillip Woodfab01ec2018-09-04 14:52:58 +01001066 } else {
Phillip Wood74d156f2018-10-04 11:07:41 +01001067 moved_block_clear(&pmb[i]);
Phillip Woodfab01ec2018-09-04 14:52:58 +01001068 }
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001069 }
Phillip Wood9c1a6c22018-10-04 11:07:44 +01001070
1071 free(got_match);
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001072}
1073
Phillip Wood74d156f2018-10-04 11:07:41 +01001074static int shrink_potential_moved_blocks(struct moved_block *pmb,
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001075 int pmb_nr)
1076{
1077 int lp, rp;
1078
1079 /* Shrink the set of potential block to the remaining running */
1080 for (lp = 0, rp = pmb_nr - 1; lp <= rp;) {
Phillip Wood74d156f2018-10-04 11:07:41 +01001081 while (lp < pmb_nr && pmb[lp].match)
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001082 lp++;
1083 /* lp points at the first NULL now */
1084
Phillip Wood74d156f2018-10-04 11:07:41 +01001085 while (rp > -1 && !pmb[rp].match)
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001086 rp--;
1087 /* rp points at the last non-NULL */
1088
1089 if (lp < pmb_nr && rp > -1 && lp < rp) {
1090 pmb[lp] = pmb[rp];
Phillip Wood21536d02018-11-23 11:16:57 +00001091 memset(&pmb[rp], 0, sizeof(pmb[rp]));
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001092 rp--;
1093 lp++;
1094 }
1095 }
1096
1097 /* Remember the number of running sets */
1098 return rp + 1;
1099}
1100
Jonathan Tan09153272017-08-15 18:27:38 -07001101/*
1102 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
1103 *
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001104 * Otherwise, if the last block has fewer alphanumeric characters than
1105 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
Jonathan Tan09153272017-08-15 18:27:38 -07001106 * that block.
1107 *
1108 * The last block consists of the (n - block_length)'th line up to but not
1109 * including the nth line.
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001110 *
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001111 * Returns 0 if the last block is empty or is unset by this function, non zero
1112 * otherwise.
1113 *
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001114 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
1115 * Think of a way to unify them.
Jonathan Tan09153272017-08-15 18:27:38 -07001116 */
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001117static int adjust_last_block(struct diff_options *o, int n, int block_length)
Jonathan Tan09153272017-08-15 18:27:38 -07001118{
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001119 int i, alnum_count = 0;
1120 if (o->color_moved == COLOR_MOVED_PLAIN)
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001121 return block_length;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001122 for (i = 1; i < block_length + 1; i++) {
1123 const char *c = o->emitted_symbols->buf[n - i].line;
1124 for (; *c; c++) {
1125 if (!isalnum(*c))
1126 continue;
1127 alnum_count++;
1128 if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT)
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001129 return 1;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001130 }
1131 }
Jonathan Tan09153272017-08-15 18:27:38 -07001132 for (i = 1; i < block_length + 1; i++)
1133 o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE;
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001134 return 0;
Jonathan Tan09153272017-08-15 18:27:38 -07001135}
1136
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001137/* Find blocks of moved code, delegate actual coloring decision to helper */
1138static void mark_color_as_moved(struct diff_options *o,
1139 struct hashmap *add_lines,
1140 struct hashmap *del_lines)
1141{
Phillip Wood74d156f2018-10-04 11:07:41 +01001142 struct moved_block *pmb = NULL; /* potentially moved blocks */
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001143 int pmb_nr = 0, pmb_alloc = 0;
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001144 int n, flipped_block = 0, block_length = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001145
1146
1147 for (n = 0; n < o->emitted_symbols->nr; n++) {
1148 struct hashmap *hm = NULL;
1149 struct moved_entry *key;
1150 struct moved_entry *match = NULL;
1151 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001152 enum diff_symbol last_symbol = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001153
1154 switch (l->s) {
1155 case DIFF_SYMBOL_PLUS:
1156 hm = del_lines;
1157 key = prepare_entry(o, n);
Eric Wong404ab782019-10-06 23:30:42 +00001158 match = hashmap_get_entry(hm, key, ent, NULL);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001159 free(key);
1160 break;
1161 case DIFF_SYMBOL_MINUS:
1162 hm = add_lines;
1163 key = prepare_entry(o, n);
Eric Wong404ab782019-10-06 23:30:42 +00001164 match = hashmap_get_entry(hm, key, ent, NULL);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001165 free(key);
1166 break;
1167 default:
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001168 flipped_block = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001169 }
1170
1171 if (!match) {
Phillip Wood74d156f2018-10-04 11:07:41 +01001172 int i;
1173
Jonathan Tan09153272017-08-15 18:27:38 -07001174 adjust_last_block(o, n, block_length);
Phillip Wood74d156f2018-10-04 11:07:41 +01001175 for(i = 0; i < pmb_nr; i++)
1176 moved_block_clear(&pmb[i]);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001177 pmb_nr = 0;
1178 block_length = 0;
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001179 flipped_block = 0;
1180 last_symbol = l->s;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001181 continue;
1182 }
1183
Phillip Wood2034b472018-11-23 11:16:54 +00001184 if (o->color_moved == COLOR_MOVED_PLAIN) {
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001185 last_symbol = l->s;
Phillip Wood2034b472018-11-23 11:16:54 +00001186 l->flags |= DIFF_SYMBOL_MOVED_LINE;
Stefan Beller176841f2017-06-30 13:53:08 -07001187 continue;
Phillip Wood2034b472018-11-23 11:16:54 +00001188 }
Stefan Beller176841f2017-06-30 13:53:08 -07001189
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001190 if (o->color_moved_ws_handling &
1191 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
1192 pmb_advance_or_null_multi_match(o, match, hm, pmb, pmb_nr, n);
1193 else
1194 pmb_advance_or_null(o, match, hm, pmb, pmb_nr);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001195
1196 pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);
1197
1198 if (pmb_nr == 0) {
1199 /*
1200 * The current line is the start of a new block.
1201 * Setup the set of potential blocks.
1202 */
Eric Wong23dee692019-10-06 23:30:41 +00001203 hashmap_for_each_entry_from(hm, match, ent) {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001204 ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001205 if (o->color_moved_ws_handling &
1206 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) {
Phillip Wood74d156f2018-10-04 11:07:41 +01001207 if (compute_ws_delta(l, match->es,
1208 &pmb[pmb_nr].wsd))
1209 pmb[pmb_nr++].match = match;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001210 } else {
Phillip Wood21536d02018-11-23 11:16:57 +00001211 pmb[pmb_nr].wsd = 0;
Phillip Wood74d156f2018-10-04 11:07:41 +01001212 pmb[pmb_nr++].match = match;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001213 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001214 }
1215
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001216 if (adjust_last_block(o, n, block_length) &&
1217 pmb_nr && last_symbol != l->s)
1218 flipped_block = (flipped_block + 1) % 2;
1219 else
1220 flipped_block = 0;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001221
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001222 block_length = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001223 }
1224
Phillip Wood2034b472018-11-23 11:16:54 +00001225 if (pmb_nr) {
1226 block_length++;
Phillip Wood2034b472018-11-23 11:16:54 +00001227 l->flags |= DIFF_SYMBOL_MOVED_LINE;
1228 if (flipped_block && o->color_moved != COLOR_MOVED_BLOCKS)
1229 l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
1230 }
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001231 last_symbol = l->s;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001232 }
Jonathan Tan09153272017-08-15 18:27:38 -07001233 adjust_last_block(o, n, block_length);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001234
Phillip Wood74d156f2018-10-04 11:07:41 +01001235 for(n = 0; n < pmb_nr; n++)
1236 moved_block_clear(&pmb[n]);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001237 free(pmb);
1238}
Stefan Bellere6e045f2017-06-29 17:07:06 -07001239
Stefan Beller86b452e2017-06-30 13:53:09 -07001240#define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1241 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
1242static void dim_moved_lines(struct diff_options *o)
1243{
1244 int n;
1245 for (n = 0; n < o->emitted_symbols->nr; n++) {
1246 struct emitted_diff_symbol *prev = (n != 0) ?
1247 &o->emitted_symbols->buf[n - 1] : NULL;
1248 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
1249 struct emitted_diff_symbol *next =
1250 (n < o->emitted_symbols->nr - 1) ?
1251 &o->emitted_symbols->buf[n + 1] : NULL;
1252
1253 /* Not a plus or minus line? */
1254 if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS)
1255 continue;
1256
1257 /* Not a moved line? */
1258 if (!(l->flags & DIFF_SYMBOL_MOVED_LINE))
1259 continue;
1260
1261 /*
1262 * If prev or next are not a plus or minus line,
1263 * pretend they don't exist
1264 */
1265 if (prev && prev->s != DIFF_SYMBOL_PLUS &&
1266 prev->s != DIFF_SYMBOL_MINUS)
1267 prev = NULL;
1268 if (next && next->s != DIFF_SYMBOL_PLUS &&
1269 next->s != DIFF_SYMBOL_MINUS)
1270 next = NULL;
1271
1272 /* Inside a block? */
1273 if ((prev &&
1274 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1275 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) &&
1276 (next &&
1277 (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1278 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) {
1279 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1280 continue;
1281 }
1282
1283 /* Check if we are at an interesting bound: */
1284 if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) &&
1285 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1286 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1287 continue;
1288 if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) &&
1289 (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1290 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1291 continue;
1292
1293 /*
1294 * The boundary to prev and next are not interesting,
1295 * so this line is not interesting as a whole
1296 */
1297 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1298 }
1299}
1300
Stefan Beller091f8e22017-06-29 17:06:53 -07001301static void emit_line_ws_markup(struct diff_options *o,
Stefan Beller9d1e16b2018-08-13 18:41:18 -07001302 const char *set_sign, const char *set,
1303 const char *reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001304 int sign_index, const char *line, int len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001305 unsigned ws_rule, int blank_at_eof)
1306{
1307 const char *ws = NULL;
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001308 int sign = o->output_indicators[sign_index];
Stefan Beller091f8e22017-06-29 17:06:53 -07001309
1310 if (o->ws_error_highlight & ws_rule) {
1311 ws = diff_get_color_opt(o, DIFF_WHITESPACE);
1312 if (!*ws)
1313 ws = NULL;
1314 }
1315
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001316 if (!ws && !set_sign)
Stefan Beller017ac452018-08-13 18:41:19 -07001317 emit_line_0(o, set, NULL, 0, reset, sign, line, len);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001318 else if (!ws) {
Stefan Beller29ef7592018-08-13 18:41:20 -07001319 emit_line_0(o, set_sign, set, !!set_sign, reset, sign, line, len);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001320 } else if (blank_at_eof)
Stefan Beller091f8e22017-06-29 17:06:53 -07001321 /* Blank line at EOF - paint '+' as well */
Stefan Beller017ac452018-08-13 18:41:19 -07001322 emit_line_0(o, ws, NULL, 0, reset, sign, line, len);
Stefan Beller091f8e22017-06-29 17:06:53 -07001323 else {
1324 /* Emit just the prefix, then the rest. */
Stefan Beller017ac452018-08-13 18:41:19 -07001325 emit_line_0(o, set_sign ? set_sign : set, NULL, !!set_sign, reset,
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001326 sign, "", 0);
Stefan Beller091f8e22017-06-29 17:06:53 -07001327 ws_check_emit(line, len, ws_rule,
1328 o->file, set, reset, ws);
1329 }
1330}
Stefan Beller36a4cef2017-06-29 17:06:49 -07001331
Stefan Bellere6e045f2017-06-29 17:07:06 -07001332static void emit_diff_symbol_from_struct(struct diff_options *o,
1333 struct emitted_diff_symbol *eds)
Stefan Beller36a4cef2017-06-29 17:06:49 -07001334{
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001335 static const char *nneof = " No newline at end of file\n";
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001336 const char *context, *reset, *set, *set_sign, *meta, *fraginfo;
Stefan Beller0911c472017-06-29 17:07:02 -07001337 struct strbuf sb = STRBUF_INIT;
Stefan Bellere6e045f2017-06-29 17:07:06 -07001338
1339 enum diff_symbol s = eds->s;
1340 const char *line = eds->line;
1341 int len = eds->len;
1342 unsigned flags = eds->flags;
1343
Stefan Beller36a4cef2017-06-29 17:06:49 -07001344 switch (s) {
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001345 case DIFF_SYMBOL_NO_LF_EOF:
1346 context = diff_get_color_opt(o, DIFF_CONTEXT);
1347 reset = diff_get_color_opt(o, DIFF_RESET);
1348 putc('\n', o->file);
Stefan Beller017ac452018-08-13 18:41:19 -07001349 emit_line_0(o, context, NULL, 0, reset, '\\',
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001350 nneof, strlen(nneof));
1351 break;
Stefan Bellerf3597132017-06-29 17:07:00 -07001352 case DIFF_SYMBOL_SUBMODULE_HEADER:
1353 case DIFF_SYMBOL_SUBMODULE_ERROR:
1354 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
Stefan Beller0911c472017-06-29 17:07:02 -07001355 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
Stefan Beller146fdb02017-06-29 17:07:05 -07001356 case DIFF_SYMBOL_SUMMARY:
Stefan Beller0911c472017-06-29 17:07:02 -07001357 case DIFF_SYMBOL_STATS_LINE:
Stefan Beller4eed0eb2017-06-29 17:07:01 -07001358 case DIFF_SYMBOL_BINARY_DIFF_BODY:
Stefan Beller68abc6f2017-06-29 17:06:51 -07001359 case DIFF_SYMBOL_CONTEXT_FRAGINFO:
1360 emit_line(o, "", "", line, len);
1361 break;
Stefan Bellerf2bb1212017-06-29 17:06:55 -07001362 case DIFF_SYMBOL_CONTEXT_INCOMPLETE:
Stefan Bellerc64b4202017-06-29 17:06:50 -07001363 case DIFF_SYMBOL_CONTEXT_MARKER:
1364 context = diff_get_color_opt(o, DIFF_CONTEXT);
1365 reset = diff_get_color_opt(o, DIFF_RESET);
1366 emit_line(o, context, reset, line, len);
1367 break;
Stefan Beller36a4cef2017-06-29 17:06:49 -07001368 case DIFF_SYMBOL_SEPARATOR:
1369 fprintf(o->file, "%s%c",
1370 diff_line_prefix(o),
1371 o->line_termination);
1372 break;
Stefan Beller091f8e22017-06-29 17:06:53 -07001373 case DIFF_SYMBOL_CONTEXT:
1374 set = diff_get_color_opt(o, DIFF_CONTEXT);
1375 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001376 set_sign = NULL;
1377 if (o->flags.dual_color_diffed_diffs) {
1378 char c = !len ? 0 : line[0];
1379
1380 if (c == '+')
1381 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1382 else if (c == '@')
1383 set = diff_get_color_opt(o, DIFF_FRAGINFO);
1384 else if (c == '-')
1385 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1386 }
Stefan Beller7648b792018-08-17 13:43:52 -07001387 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001388 OUTPUT_INDICATOR_CONTEXT, line, len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001389 flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
1390 break;
1391 case DIFF_SYMBOL_PLUS:
Stefan Beller86b452e2017-06-30 13:53:09 -07001392 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1393 DIFF_SYMBOL_MOVED_LINE_ALT |
1394 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1395 case DIFF_SYMBOL_MOVED_LINE |
1396 DIFF_SYMBOL_MOVED_LINE_ALT |
1397 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1398 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM);
1399 break;
1400 case DIFF_SYMBOL_MOVED_LINE |
1401 DIFF_SYMBOL_MOVED_LINE_ALT:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001402 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT);
Stefan Beller86b452e2017-06-30 13:53:09 -07001403 break;
1404 case DIFF_SYMBOL_MOVED_LINE |
1405 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1406 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM);
1407 break;
1408 case DIFF_SYMBOL_MOVED_LINE:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001409 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED);
Stefan Beller86b452e2017-06-30 13:53:09 -07001410 break;
1411 default:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001412 set = diff_get_color_opt(o, DIFF_FILE_NEW);
Stefan Beller86b452e2017-06-30 13:53:09 -07001413 }
Stefan Beller091f8e22017-06-29 17:06:53 -07001414 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001415 if (!o->flags.dual_color_diffed_diffs)
1416 set_sign = NULL;
1417 else {
1418 char c = !len ? 0 : line[0];
1419
1420 set_sign = set;
1421 if (c == '-')
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001422 set = diff_get_color_opt(o, DIFF_FILE_OLD_BOLD);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001423 else if (c == '@')
1424 set = diff_get_color_opt(o, DIFF_FRAGINFO);
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001425 else if (c == '+')
1426 set = diff_get_color_opt(o, DIFF_FILE_NEW_BOLD);
1427 else
1428 set = diff_get_color_opt(o, DIFF_CONTEXT_BOLD);
Johannes Schindelin0b91faa2018-08-13 04:33:24 -07001429 flags &= ~DIFF_SYMBOL_CONTENT_WS_MASK;
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001430 }
Stefan Beller7648b792018-08-17 13:43:52 -07001431 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001432 OUTPUT_INDICATOR_NEW, line, len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001433 flags & DIFF_SYMBOL_CONTENT_WS_MASK,
1434 flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
1435 break;
1436 case DIFF_SYMBOL_MINUS:
Stefan Beller86b452e2017-06-30 13:53:09 -07001437 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1438 DIFF_SYMBOL_MOVED_LINE_ALT |
1439 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1440 case DIFF_SYMBOL_MOVED_LINE |
1441 DIFF_SYMBOL_MOVED_LINE_ALT |
1442 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1443 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM);
1444 break;
1445 case DIFF_SYMBOL_MOVED_LINE |
1446 DIFF_SYMBOL_MOVED_LINE_ALT:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001447 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT);
Stefan Beller86b452e2017-06-30 13:53:09 -07001448 break;
1449 case DIFF_SYMBOL_MOVED_LINE |
1450 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1451 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM);
1452 break;
1453 case DIFF_SYMBOL_MOVED_LINE:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001454 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED);
Stefan Beller86b452e2017-06-30 13:53:09 -07001455 break;
1456 default:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001457 set = diff_get_color_opt(o, DIFF_FILE_OLD);
Stefan Beller86b452e2017-06-30 13:53:09 -07001458 }
Stefan Beller091f8e22017-06-29 17:06:53 -07001459 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001460 if (!o->flags.dual_color_diffed_diffs)
1461 set_sign = NULL;
1462 else {
1463 char c = !len ? 0 : line[0];
1464
1465 set_sign = set;
1466 if (c == '+')
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001467 set = diff_get_color_opt(o, DIFF_FILE_NEW_DIM);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001468 else if (c == '@')
1469 set = diff_get_color_opt(o, DIFF_FRAGINFO);
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001470 else if (c == '-')
1471 set = diff_get_color_opt(o, DIFF_FILE_OLD_DIM);
1472 else
1473 set = diff_get_color_opt(o, DIFF_CONTEXT_DIM);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001474 }
Stefan Beller7648b792018-08-17 13:43:52 -07001475 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001476 OUTPUT_INDICATOR_OLD, line, len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001477 flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
1478 break;
Stefan Bellerff958672017-06-29 17:06:54 -07001479 case DIFF_SYMBOL_WORDS_PORCELAIN:
1480 context = diff_get_color_opt(o, DIFF_CONTEXT);
1481 reset = diff_get_color_opt(o, DIFF_RESET);
1482 emit_line(o, context, reset, line, len);
1483 fputs("~\n", o->file);
1484 break;
1485 case DIFF_SYMBOL_WORDS:
1486 context = diff_get_color_opt(o, DIFF_CONTEXT);
1487 reset = diff_get_color_opt(o, DIFF_RESET);
1488 /*
1489 * Skip the prefix character, if any. With
1490 * diff_suppress_blank_empty, there may be
1491 * none.
1492 */
1493 if (line[0] != '\n') {
1494 line++;
1495 len--;
1496 }
1497 emit_line(o, context, reset, line, len);
1498 break;
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07001499 case DIFF_SYMBOL_FILEPAIR_PLUS:
1500 meta = diff_get_color_opt(o, DIFF_METAINFO);
1501 reset = diff_get_color_opt(o, DIFF_RESET);
1502 fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
1503 line, reset,
1504 strchr(line, ' ') ? "\t" : "");
1505 break;
1506 case DIFF_SYMBOL_FILEPAIR_MINUS:
1507 meta = diff_get_color_opt(o, DIFF_METAINFO);
1508 reset = diff_get_color_opt(o, DIFF_RESET);
1509 fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta,
1510 line, reset,
1511 strchr(line, ' ') ? "\t" : "");
1512 break;
Stefan Beller4acaaa72017-06-29 17:06:58 -07001513 case DIFF_SYMBOL_BINARY_FILES:
Stefan Bellera29b0a12017-06-29 17:06:57 -07001514 case DIFF_SYMBOL_HEADER:
1515 fprintf(o->file, "%s", line);
1516 break;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07001517 case DIFF_SYMBOL_BINARY_DIFF_HEADER:
1518 fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o));
1519 break;
1520 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA:
1521 fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line);
1522 break;
1523 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL:
1524 fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line);
1525 break;
1526 case DIFF_SYMBOL_BINARY_DIFF_FOOTER:
1527 fputs(diff_line_prefix(o), o->file);
1528 fputc('\n', o->file);
1529 break;
Stefan Beller5af6ea92017-06-29 17:06:59 -07001530 case DIFF_SYMBOL_REWRITE_DIFF:
1531 fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
1532 reset = diff_get_color_opt(o, DIFF_RESET);
1533 emit_line(o, fraginfo, reset, line, len);
1534 break;
Stefan Bellerf3597132017-06-29 17:07:00 -07001535 case DIFF_SYMBOL_SUBMODULE_ADD:
1536 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1537 reset = diff_get_color_opt(o, DIFF_RESET);
1538 emit_line(o, set, reset, line, len);
1539 break;
1540 case DIFF_SYMBOL_SUBMODULE_DEL:
1541 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1542 reset = diff_get_color_opt(o, DIFF_RESET);
1543 emit_line(o, set, reset, line, len);
1544 break;
1545 case DIFF_SYMBOL_SUBMODULE_UNTRACKED:
1546 fprintf(o->file, "%sSubmodule %s contains untracked content\n",
1547 diff_line_prefix(o), line);
1548 break;
1549 case DIFF_SYMBOL_SUBMODULE_MODIFIED:
1550 fprintf(o->file, "%sSubmodule %s contains modified content\n",
1551 diff_line_prefix(o), line);
1552 break;
Stefan Beller0911c472017-06-29 17:07:02 -07001553 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES:
1554 emit_line(o, "", "", " 0 files changed\n",
1555 strlen(" 0 files changed\n"));
1556 break;
1557 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV:
1558 emit_line(o, "", "", " ...\n", strlen(" ...\n"));
1559 break;
Stefan Bellerbd033292017-06-29 17:07:03 -07001560 case DIFF_SYMBOL_WORD_DIFF:
1561 fprintf(o->file, "%.*s", len, line);
1562 break;
Stefan Beller30b7e1e2017-06-29 17:07:04 -07001563 case DIFF_SYMBOL_STAT_SEP:
1564 fputs(o->stat_sep, o->file);
1565 break;
Stefan Beller36a4cef2017-06-29 17:06:49 -07001566 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +02001567 BUG("unknown diff symbol");
Stefan Beller36a4cef2017-06-29 17:06:49 -07001568 }
Stefan Beller0911c472017-06-29 17:07:02 -07001569 strbuf_release(&sb);
Stefan Beller36a4cef2017-06-29 17:06:49 -07001570}
1571
Stefan Bellere6e045f2017-06-29 17:07:06 -07001572static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
1573 const char *line, int len, unsigned flags)
1574{
Phillip Wood21536d02018-11-23 11:16:57 +00001575 struct emitted_diff_symbol e = {line, len, flags, 0, 0, s};
Stefan Bellere6e045f2017-06-29 17:07:06 -07001576
1577 if (o->emitted_symbols)
1578 append_emitted_diff_symbol(o, &e);
1579 else
1580 emit_diff_symbol_from_struct(o, &e);
1581}
1582
Stefan Bellerf3597132017-06-29 17:07:00 -07001583void diff_emit_submodule_del(struct diff_options *o, const char *line)
1584{
1585 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0);
1586}
1587
1588void diff_emit_submodule_add(struct diff_options *o, const char *line)
1589{
1590 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0);
1591}
1592
1593void diff_emit_submodule_untracked(struct diff_options *o, const char *path)
1594{
1595 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED,
1596 path, strlen(path), 0);
1597}
1598
1599void diff_emit_submodule_modified(struct diff_options *o, const char *path)
1600{
1601 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED,
1602 path, strlen(path), 0);
1603}
1604
1605void diff_emit_submodule_header(struct diff_options *o, const char *header)
1606{
1607 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER,
1608 header, strlen(header), 0);
1609}
1610
1611void diff_emit_submodule_error(struct diff_options *o, const char *err)
1612{
1613 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0);
1614}
1615
1616void diff_emit_submodule_pipethrough(struct diff_options *o,
1617 const char *line, int len)
1618{
1619 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0);
1620}
1621
Junio C Hamano6957eb92009-09-14 18:44:01 -07001622static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
1623{
1624 if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
1625 ecbdata->blank_at_eof_in_preimage &&
1626 ecbdata->blank_at_eof_in_postimage &&
1627 ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
1628 ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
1629 return 0;
Junio C Hamano018cff72009-09-14 18:44:01 -07001630 return ws_blank_line(line, len, ecbdata->ws_rule);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001631}
1632
Jeff Kinge04df612019-02-14 00:48:13 -05001633static void emit_add_line(struct emit_callback *ecbdata,
Junio C Hamano018cff72009-09-14 18:44:01 -07001634 const char *line, int len)
Junio C Hamano6957eb92009-09-14 18:44:01 -07001635{
Stefan Beller091f8e22017-06-29 17:06:53 -07001636 unsigned flags = WSEH_NEW | ecbdata->ws_rule;
1637 if (new_blank_line_at_eof(ecbdata, line, len))
1638 flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF;
1639
1640 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001641}
Junio C Hamano6957eb92009-09-14 18:44:01 -07001642
Jeff Kinge04df612019-02-14 00:48:13 -05001643static void emit_del_line(struct emit_callback *ecbdata,
Junio C Hamano0e383e12015-05-26 09:56:33 -07001644 const char *line, int len)
1645{
Stefan Beller091f8e22017-06-29 17:06:53 -07001646 unsigned flags = WSEH_OLD | ecbdata->ws_rule;
1647 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags);
Junio C Hamano0e383e12015-05-26 09:56:33 -07001648}
1649
Jeff Kinge04df612019-02-14 00:48:13 -05001650static void emit_context_line(struct emit_callback *ecbdata,
Junio C Hamano0e383e12015-05-26 09:56:33 -07001651 const char *line, int len)
1652{
Stefan Beller091f8e22017-06-29 17:06:53 -07001653 unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule;
1654 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001655}
1656
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001657static void emit_hunk_header(struct emit_callback *ecbdata,
1658 const char *line, int len)
1659{
Jeff King8dbf3eb2015-05-27 16:48:46 -04001660 const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001661 const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
1662 const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
1663 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001664 const char *reverse = ecbdata->color_diff ? GIT_COLOR_REVERSE : "";
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001665 static const char atat[2] = { '@', '@' };
1666 const char *cp, *ep;
Bo Yang2efcc972010-05-29 23:32:05 +08001667 struct strbuf msgbuf = STRBUF_INIT;
1668 int org_len = len;
1669 int i = 1;
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001670
1671 /*
1672 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1673 * it always is at least 10 bytes long.
1674 */
1675 if (len < 10 ||
1676 memcmp(line, atat, 2) ||
1677 !(ep = memmem(line + 2, len - 2, atat, 2))) {
Stefan Bellerc64b4202017-06-29 17:06:50 -07001678 emit_diff_symbol(ecbdata->opt,
Stefan Beller091f8e22017-06-29 17:06:53 -07001679 DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001680 return;
1681 }
1682 ep += 2; /* skip over @@ */
1683
1684 /* The hunk header in fraginfo color */
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001685 if (ecbdata->opt->flags.dual_color_diffed_diffs)
1686 strbuf_addstr(&msgbuf, reverse);
René Scharfecedc61a2014-07-17 01:38:18 +02001687 strbuf_addstr(&msgbuf, frag);
Thomas Gummerer430be362019-07-11 17:08:48 +01001688 if (ecbdata->opt->flags.suppress_hunk_header_line_count)
1689 strbuf_add(&msgbuf, atat, sizeof(atat));
1690 else
1691 strbuf_add(&msgbuf, line, ep - line);
René Scharfecedc61a2014-07-17 01:38:18 +02001692 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001693
1694 /*
1695 * trailing "\r\n"
1696 */
1697 for ( ; i < 3; i++)
1698 if (line[len - i] == '\r' || line[len - i] == '\n')
1699 len--;
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001700
1701 /* blank before the func header */
1702 for (cp = ep; ep - line < len; ep++)
1703 if (*ep != ' ' && *ep != '\t')
1704 break;
Bo Yang2efcc972010-05-29 23:32:05 +08001705 if (ep != cp) {
Jeff King8dbf3eb2015-05-27 16:48:46 -04001706 strbuf_addstr(&msgbuf, context);
Bo Yang2efcc972010-05-29 23:32:05 +08001707 strbuf_add(&msgbuf, cp, ep - cp);
René Scharfecedc61a2014-07-17 01:38:18 +02001708 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001709 }
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001710
Bo Yang2efcc972010-05-29 23:32:05 +08001711 if (ep < line + len) {
René Scharfecedc61a2014-07-17 01:38:18 +02001712 strbuf_addstr(&msgbuf, func);
Bo Yang2efcc972010-05-29 23:32:05 +08001713 strbuf_add(&msgbuf, ep, line + len - ep);
René Scharfecedc61a2014-07-17 01:38:18 +02001714 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001715 }
1716
1717 strbuf_add(&msgbuf, line + len, org_len - len);
Stefan Bellerdfb77282017-06-29 17:06:47 -07001718 strbuf_complete_line(&msgbuf);
Stefan Beller68abc6f2017-06-29 17:06:51 -07001719 emit_diff_symbol(ecbdata->opt,
Stefan Beller091f8e22017-06-29 17:06:53 -07001720 DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0);
Bo Yang2efcc972010-05-29 23:32:05 +08001721 strbuf_release(&msgbuf);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001722}
1723
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +01001724static struct diff_tempfile *claim_diff_tempfile(void)
1725{
Jeff King479b0ae2009-01-22 00:59:56 -05001726 int i;
1727 for (i = 0; i < ARRAY_SIZE(diff_temp); i++)
1728 if (!diff_temp[i].name)
1729 return diff_temp + i;
Johannes Schindelin033abf92018-05-02 11:38:39 +02001730 BUG("diff is failing to clean up its tempfiles");
Jeff King479b0ae2009-01-22 00:59:56 -05001731}
1732
Jeff King479b0ae2009-01-22 00:59:56 -05001733static void remove_tempfile(void)
1734{
1735 int i;
Nazri Ramliya8344ab2009-02-12 21:36:14 +08001736 for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
Jeff King076aa2c2017-09-05 08:15:08 -04001737 if (is_tempfile_active(diff_temp[i].tempfile))
Michael Haggerty284098f2015-08-12 19:12:01 +02001738 delete_tempfile(&diff_temp[i].tempfile);
Nazri Ramliya8344ab2009-02-12 21:36:14 +08001739 diff_temp[i].name = NULL;
1740 }
Jeff King479b0ae2009-01-22 00:59:56 -05001741}
1742
Stefan Beller5af6ea92017-06-29 17:06:59 -07001743static void add_line_count(struct strbuf *out, int count)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001744{
1745 switch (count) {
1746 case 0:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001747 strbuf_addstr(out, "0,0");
Junio C Hamano6973dca2006-04-21 23:57:45 -07001748 break;
1749 case 1:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001750 strbuf_addstr(out, "1");
Junio C Hamano6973dca2006-04-21 23:57:45 -07001751 break;
1752 default:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001753 strbuf_addf(out, "1,%d", count);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001754 break;
1755 }
1756}
1757
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001758static void emit_rewrite_lines(struct emit_callback *ecb,
1759 int prefix, const char *data, int size)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001760{
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001761 const char *endp = NULL;
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001762
1763 while (0 < size) {
1764 int len;
1765
1766 endp = memchr(data, '\n', size);
1767 len = endp ? (endp - data + 1) : size;
1768 if (prefix != '+') {
1769 ecb->lno_in_preimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05001770 emit_del_line(ecb, data, len);
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001771 } else {
1772 ecb->lno_in_postimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05001773 emit_add_line(ecb, data, len);
Johannes Schindelin13e36ec2007-02-20 15:08:46 +01001774 }
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001775 size -= len;
1776 data += len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07001777 }
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001778 if (!endp)
Stefan Beller091f8e22017-06-29 17:06:53 -07001779 emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001780}
1781
1782static void emit_rewrite_diff(const char *name_a,
1783 const char *name_b,
1784 struct diff_filespec *one,
Johannes Schindelin13e36ec2007-02-20 15:08:46 +01001785 struct diff_filespec *two,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04001786 struct userdiff_driver *textconv_one,
1787 struct userdiff_driver *textconv_two,
Johannes Schindelineab9a402007-12-18 19:32:14 +00001788 struct diff_options *o)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001789{
1790 int lc_a, lc_b;
Junio C Hamanod5625092007-12-26 17:13:36 -08001791 static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001792 const char *a_prefix, *b_prefix;
Jeff King840383b2010-04-01 20:09:26 -04001793 char *data_one, *data_two;
Jeff King3aa1f7c2008-12-09 03:13:21 -05001794 size_t size_one, size_two;
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001795 struct emit_callback ecbdata;
Stefan Beller5af6ea92017-06-29 17:06:59 -07001796 struct strbuf out = STRBUF_INIT;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001797
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001798 if (diff_mnemonic_prefix && o->flags.reverse_diff) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001799 a_prefix = o->b_prefix;
1800 b_prefix = o->a_prefix;
1801 } else {
1802 a_prefix = o->a_prefix;
1803 b_prefix = o->b_prefix;
1804 }
Junio C Hamano1a9eb3b2006-09-22 16:17:58 -07001805
Junio C Hamano8a13bec2007-02-24 01:42:06 -08001806 name_a += (*name_a == '/');
1807 name_b += (*name_b == '/');
Junio C Hamano1a9eb3b2006-09-22 16:17:58 -07001808
Junio C Hamanod5625092007-12-26 17:13:36 -08001809 strbuf_reset(&a_name);
1810 strbuf_reset(&b_name);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001811 quote_two_c_style(&a_name, a_prefix, name_a, 0);
1812 quote_two_c_style(&b_name, b_prefix, name_b, 0);
Junio C Hamanod5625092007-12-26 17:13:36 -08001813
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001814 size_one = fill_textconv(o->repo, textconv_one, one, &data_one);
1815 size_two = fill_textconv(o->repo, textconv_two, two, &data_two);
Jeff King3aa1f7c2008-12-09 03:13:21 -05001816
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001817 memset(&ecbdata, 0, sizeof(ecbdata));
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07001818 ecbdata.color_diff = want_color(o->use_color);
Nguyễn Thái Ngọc Duy26d024e2018-09-21 17:57:37 +02001819 ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
Bo Yanga3c158d2010-05-26 15:08:02 +08001820 ecbdata.opt = o;
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001821 if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
1822 mmfile_t mf1, mf2;
1823 mf1.ptr = (char *)data_one;
1824 mf2.ptr = (char *)data_two;
1825 mf1.size = size_one;
1826 mf2.size = size_two;
1827 check_blank_at_eof(&mf1, &mf2, &ecbdata);
1828 }
1829 ecbdata.lno_in_preimage = 1;
1830 ecbdata.lno_in_postimage = 1;
1831
Jeff King3aa1f7c2008-12-09 03:13:21 -05001832 lc_a = count_lines(data_one, size_one);
1833 lc_b = count_lines(data_two, size_two);
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07001834
1835 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
1836 a_name.buf, a_name.len, 0);
1837 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
1838 b_name.buf, b_name.len, 0);
1839
Stefan Beller5af6ea92017-06-29 17:06:59 -07001840 strbuf_addstr(&out, "@@ -");
Junio C Hamano467ddc12011-02-28 16:11:55 -08001841 if (!o->irreversible_delete)
Stefan Beller5af6ea92017-06-29 17:06:59 -07001842 add_line_count(&out, lc_a);
Junio C Hamano467ddc12011-02-28 16:11:55 -08001843 else
Stefan Beller5af6ea92017-06-29 17:06:59 -07001844 strbuf_addstr(&out, "?,?");
1845 strbuf_addstr(&out, " +");
1846 add_line_count(&out, lc_b);
1847 strbuf_addstr(&out, " @@\n");
1848 emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
1849 strbuf_release(&out);
1850
Junio C Hamano467ddc12011-02-28 16:11:55 -08001851 if (lc_a && !o->irreversible_delete)
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001852 emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001853 if (lc_b)
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001854 emit_rewrite_lines(&ecbdata, '+', data_two, size_two);
Jeff Kingb76c0562010-04-01 20:04:14 -04001855 if (textconv_one)
Junio C Hamanoaed6ca52010-04-08 23:30:49 -07001856 free((char *)data_one);
Jeff Kingb76c0562010-04-01 20:04:14 -04001857 if (textconv_two)
Junio C Hamanoaed6ca52010-04-08 23:30:49 -07001858 free((char *)data_two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001859}
1860
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001861struct diff_words_buffer {
1862 mmfile_t text;
Ramsay Jones071bcaa2017-09-21 17:49:38 +01001863 unsigned long alloc;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001864 struct diff_words_orig {
1865 const char *begin, *end;
1866 } *orig;
1867 int orig_nr, orig_alloc;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001868};
1869
1870static void diff_words_append(char *line, unsigned long len,
1871 struct diff_words_buffer *buffer)
1872{
Johannes Schindelin23c15752009-01-17 17:29:43 +01001873 ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc);
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001874 line++;
1875 len--;
1876 memcpy(buffer->text.ptr + buffer->text.size, line, len);
1877 buffer->text.size += len;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001878 buffer->text.ptr[buffer->text.size] = '\0';
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001879}
1880
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001881struct diff_words_style_elem {
Thomas Rast882749a2010-04-14 17:59:06 +02001882 const char *prefix;
1883 const char *suffix;
1884 const char *color; /* NULL; filled in by the setup code if
1885 * color is enabled */
1886};
1887
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001888struct diff_words_style {
Thomas Rast882749a2010-04-14 17:59:06 +02001889 enum diff_words_type type;
Brandon Williams63a01c32018-02-14 10:59:39 -08001890 struct diff_words_style_elem new_word, old_word, ctx;
Thomas Rast882749a2010-04-14 17:59:06 +02001891 const char *newline;
1892};
1893
Stephen Boydc2e86ad2011-03-22 00:51:05 -07001894static struct diff_words_style diff_words_styles[] = {
Thomas Rast882749a2010-04-14 17:59:06 +02001895 { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1896 { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1897 { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" }
1898};
1899
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001900struct diff_words_data {
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001901 struct diff_words_buffer minus, plus;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001902 const char *current_plus;
Bo Yang4297c0a2010-05-29 23:32:06 +08001903 int last_minus;
1904 struct diff_options *opt;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001905 regex_t *word_regex;
Thomas Rast882749a2010-04-14 17:59:06 +02001906 enum diff_words_type type;
1907 struct diff_words_style *style;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001908};
1909
Stefan Bellerbd033292017-06-29 17:07:03 -07001910static int fn_out_diff_words_write_helper(struct diff_options *o,
Thomas Rast882749a2010-04-14 17:59:06 +02001911 struct diff_words_style_elem *st_el,
1912 const char *newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07001913 size_t count, const char *buf)
Thomas Rast882749a2010-04-14 17:59:06 +02001914{
Bo Yang4297c0a2010-05-29 23:32:06 +08001915 int print = 0;
Stefan Bellerbd033292017-06-29 17:07:03 -07001916 struct strbuf sb = STRBUF_INIT;
Bo Yang4297c0a2010-05-29 23:32:06 +08001917
Thomas Rast882749a2010-04-14 17:59:06 +02001918 while (count) {
1919 char *p = memchr(buf, '\n', count);
Bo Yang4297c0a2010-05-29 23:32:06 +08001920 if (print)
Stefan Bellerbd033292017-06-29 17:07:03 -07001921 strbuf_addstr(&sb, diff_line_prefix(o));
1922
Thomas Rast882749a2010-04-14 17:59:06 +02001923 if (p != buf) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001924 const char *reset = st_el->color && *st_el->color ?
1925 GIT_COLOR_RESET : NULL;
1926 if (st_el->color && *st_el->color)
1927 strbuf_addstr(&sb, st_el->color);
1928 strbuf_addstr(&sb, st_el->prefix);
1929 strbuf_add(&sb, buf, p ? p - buf : count);
1930 strbuf_addstr(&sb, st_el->suffix);
1931 if (reset)
1932 strbuf_addstr(&sb, reset);
Thomas Rast882749a2010-04-14 17:59:06 +02001933 }
1934 if (!p)
Stefan Bellerbd033292017-06-29 17:07:03 -07001935 goto out;
1936
1937 strbuf_addstr(&sb, newline);
Thomas Rast882749a2010-04-14 17:59:06 +02001938 count -= p + 1 - buf;
1939 buf = p + 1;
Bo Yang4297c0a2010-05-29 23:32:06 +08001940 print = 1;
Stefan Bellerbd033292017-06-29 17:07:03 -07001941 if (count) {
1942 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1943 sb.buf, sb.len, 0);
1944 strbuf_reset(&sb);
1945 }
Thomas Rast882749a2010-04-14 17:59:06 +02001946 }
Stefan Bellerbd033292017-06-29 17:07:03 -07001947
1948out:
1949 if (sb.len)
1950 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1951 sb.buf, sb.len, 0);
1952 strbuf_release(&sb);
Thomas Rast882749a2010-04-14 17:59:06 +02001953 return 0;
1954}
1955
Bo Yang4297c0a2010-05-29 23:32:06 +08001956/*
1957 * '--color-words' algorithm can be described as:
1958 *
René Genz56217602017-04-30 17:48:27 +02001959 * 1. collect the minus/plus lines of a diff hunk, divided into
Bo Yang4297c0a2010-05-29 23:32:06 +08001960 * minus-lines and plus-lines;
1961 *
1962 * 2. break both minus-lines and plus-lines into words and
1963 * place them into two mmfile_t with one word for each line;
1964 *
1965 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1966 *
1967 * And for the common parts of the both file, we output the plus side text.
1968 * diff_words->current_plus is used to trace the current position of the plus file
1969 * which printed. diff_words->last_minus is used to trace the last minus word
1970 * printed.
1971 *
1972 * For '--graph' to work with '--color-words', we need to output the graph prefix
1973 * on each line of color words output. Generally, there are two conditions on
1974 * which we should output the prefix.
1975 *
1976 * 1. diff_words->last_minus == 0 &&
1977 * diff_words->current_plus == diff_words->plus.text.ptr
1978 *
1979 * that is: the plus text must start as a new line, and if there is no minus
1980 * word printed, a graph prefix must be printed.
1981 *
1982 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1983 * *(diff_words->current_plus - 1) == '\n'
1984 *
1985 * that is: a graph prefix must be printed following a '\n'
1986 */
1987static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
1988{
1989 if ((diff_words->last_minus == 0 &&
1990 diff_words->current_plus == diff_words->plus.text.ptr) ||
1991 (diff_words->current_plus > diff_words->plus.text.ptr &&
1992 *(diff_words->current_plus - 1) == '\n')) {
1993 return 1;
1994 } else {
1995 return 0;
1996 }
1997}
1998
Jeff King7c61e252018-11-02 02:37:18 -04001999static void fn_out_diff_words_aux(void *priv,
2000 long minus_first, long minus_len,
2001 long plus_first, long plus_len,
2002 const char *func, long funclen)
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002003{
2004 struct diff_words_data *diff_words = priv;
Thomas Rast882749a2010-04-14 17:59:06 +02002005 struct diff_words_style *style = diff_words->style;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002006 const char *minus_begin, *minus_end, *plus_begin, *plus_end;
Bo Yang4297c0a2010-05-29 23:32:06 +08002007 struct diff_options *opt = diff_words->opt;
John Keeping30997bb2013-02-07 20:15:27 +00002008 const char *line_prefix;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002009
Bo Yang4297c0a2010-05-29 23:32:06 +08002010 assert(opt);
John Keeping30997bb2013-02-07 20:15:27 +00002011 line_prefix = diff_line_prefix(opt);
Bo Yang4297c0a2010-05-29 23:32:06 +08002012
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002013 /* POSIX requires that first be decremented by one if len == 0... */
2014 if (minus_len) {
2015 minus_begin = diff_words->minus.orig[minus_first].begin;
2016 minus_end =
2017 diff_words->minus.orig[minus_first + minus_len - 1].end;
2018 } else
2019 minus_begin = minus_end =
2020 diff_words->minus.orig[minus_first].end;
2021
2022 if (plus_len) {
2023 plus_begin = diff_words->plus.orig[plus_first].begin;
2024 plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end;
2025 } else
2026 plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
2027
Bo Yang4297c0a2010-05-29 23:32:06 +08002028 if (color_words_output_graph_prefix(diff_words)) {
2029 fputs(line_prefix, diff_words->opt->file);
2030 }
2031 if (diff_words->current_plus != plus_begin) {
Stefan Bellerbd033292017-06-29 17:07:03 -07002032 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02002033 &style->ctx, style->newline,
2034 plus_begin - diff_words->current_plus,
Stefan Bellerbd033292017-06-29 17:07:03 -07002035 diff_words->current_plus);
Bo Yang4297c0a2010-05-29 23:32:06 +08002036 }
2037 if (minus_begin != minus_end) {
Stefan Bellerbd033292017-06-29 17:07:03 -07002038 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 10:59:39 -08002039 &style->old_word, style->newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07002040 minus_end - minus_begin, minus_begin);
Bo Yang4297c0a2010-05-29 23:32:06 +08002041 }
2042 if (plus_begin != plus_end) {
Stefan Bellerbd033292017-06-29 17:07:03 -07002043 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 10:59:39 -08002044 &style->new_word, style->newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07002045 plus_end - plus_begin, plus_begin);
Bo Yang4297c0a2010-05-29 23:32:06 +08002046 }
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002047
2048 diff_words->current_plus = plus_end;
Bo Yang4297c0a2010-05-29 23:32:06 +08002049 diff_words->last_minus = minus_first;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002050}
2051
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002052/* This function starts looking at *begin, and returns 0 iff a word was found. */
2053static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
2054 int *begin, int *end)
2055{
2056 if (word_regex && *begin < buffer->size) {
2057 regmatch_t match[1];
Johannes Schindelinb7d36ff2016-09-21 20:24:14 +02002058 if (!regexec_buf(word_regex, buffer->ptr + *begin,
2059 buffer->size - *begin, 1, match, 0)) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002060 char *p = memchr(buffer->ptr + *begin + match[0].rm_so,
2061 '\n', match[0].rm_eo - match[0].rm_so);
2062 *end = p ? p - buffer->ptr : match[0].rm_eo + *begin;
2063 *begin += match[0].rm_so;
2064 return *begin >= *end;
2065 }
2066 return -1;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002067 }
2068
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002069 /* find the next word */
2070 while (*begin < buffer->size && isspace(buffer->ptr[*begin]))
2071 (*begin)++;
2072 if (*begin >= buffer->size)
2073 return -1;
2074
2075 /* find the end of the word */
2076 *end = *begin + 1;
2077 while (*end < buffer->size && !isspace(buffer->ptr[*end]))
2078 (*end)++;
2079
2080 return 0;
2081}
2082
Johannes Schindelin23c15752009-01-17 17:29:43 +01002083/*
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002084 * This function splits the words in buffer->text, stores the list with
2085 * newline separator into out, and saves the offsets of the original words
2086 * in buffer->orig.
Johannes Schindelin23c15752009-01-17 17:29:43 +01002087 */
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002088static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out,
2089 regex_t *word_regex)
Johannes Schindelin23c15752009-01-17 17:29:43 +01002090{
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002091 int i, j;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002092 long alloc = 0;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002093
2094 out->size = 0;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002095 out->ptr = NULL;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002096
2097 /* fake an empty "0th" word */
2098 ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc);
2099 buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr;
2100 buffer->orig_nr = 1;
2101
2102 for (i = 0; i < buffer->text.size; i++) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002103 if (find_word_boundaries(&buffer->text, word_regex, &i, &j))
2104 return;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002105
2106 /* store original boundaries */
2107 ALLOC_GROW(buffer->orig, buffer->orig_nr + 1,
2108 buffer->orig_alloc);
2109 buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i;
2110 buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j;
2111 buffer->orig_nr++;
2112
2113 /* store one word */
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002114 ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002115 memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i);
2116 out->ptr[out->size + j - i] = '\n';
2117 out->size += j - i + 1;
2118
2119 i = j - 1;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002120 }
2121}
2122
2123/* this executes the word diff on the accumulated buffers */
2124static void diff_words_show(struct diff_words_data *diff_words)
2125{
2126 xpparam_t xpp;
2127 xdemitconf_t xecfg;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002128 mmfile_t minus, plus;
Thomas Rast882749a2010-04-14 17:59:06 +02002129 struct diff_words_style *style = diff_words->style;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002130
Bo Yang4297c0a2010-05-29 23:32:06 +08002131 struct diff_options *opt = diff_words->opt;
John Keeping30997bb2013-02-07 20:15:27 +00002132 const char *line_prefix;
Bo Yang4297c0a2010-05-29 23:32:06 +08002133
2134 assert(opt);
John Keeping30997bb2013-02-07 20:15:27 +00002135 line_prefix = diff_line_prefix(opt);
Bo Yang4297c0a2010-05-29 23:32:06 +08002136
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002137 /* special case: only removal */
2138 if (!diff_words->plus.text.size) {
Stefan Bellerbd033292017-06-29 17:07:03 -07002139 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
2140 line_prefix, strlen(line_prefix), 0);
2141 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 10:59:39 -08002142 &style->old_word, style->newline,
Bo Yang4297c0a2010-05-29 23:32:06 +08002143 diff_words->minus.text.size,
Stefan Bellerbd033292017-06-29 17:07:03 -07002144 diff_words->minus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002145 diff_words->minus.text.size = 0;
2146 return;
2147 }
2148
2149 diff_words->current_plus = diff_words->plus.text.ptr;
Bo Yang4297c0a2010-05-29 23:32:06 +08002150 diff_words->last_minus = 0;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002151
Brian Downing9ccd0a82008-10-25 15:30:37 +02002152 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01002153 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002154 diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex);
2155 diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex);
René Scharfe582aa002010-05-02 15:04:41 +02002156 xpp.flags = 0;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002157 /* as only the hunk header will be parsed, we need a 0-context */
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002158 xecfg.ctxlen = 0;
Jeff King7c61e252018-11-02 02:37:18 -04002159 if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, NULL,
Jeff King9346d6d2018-11-02 02:35:45 -04002160 diff_words, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04002161 die("unable to generate word diff");
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002162 free(minus.ptr);
2163 free(plus.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002164 if (diff_words->current_plus != diff_words->plus.text.ptr +
Bo Yang4297c0a2010-05-29 23:32:06 +08002165 diff_words->plus.text.size) {
2166 if (color_words_output_graph_prefix(diff_words))
Stefan Bellerbd033292017-06-29 17:07:03 -07002167 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
2168 line_prefix, strlen(line_prefix), 0);
2169 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02002170 &style->ctx, style->newline,
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002171 diff_words->plus.text.ptr + diff_words->plus.text.size
Stefan Bellerbd033292017-06-29 17:07:03 -07002172 - diff_words->current_plus, diff_words->current_plus);
Bo Yang4297c0a2010-05-29 23:32:06 +08002173 }
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002174 diff_words->minus.text.size = diff_words->plus.text.size = 0;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002175}
2176
Junio C Hamano76fd2822009-10-30 10:09:06 -07002177/* In "color-words" mode, show word-diff of words accumulated in the buffer */
2178static void diff_words_flush(struct emit_callback *ecbdata)
2179{
Stefan Bellere6e045f2017-06-29 17:07:06 -07002180 struct diff_options *wo = ecbdata->diff_words->opt;
2181
Junio C Hamano76fd2822009-10-30 10:09:06 -07002182 if (ecbdata->diff_words->minus.text.size ||
2183 ecbdata->diff_words->plus.text.size)
2184 diff_words_show(ecbdata->diff_words);
Stefan Bellere6e045f2017-06-29 17:07:06 -07002185
2186 if (wo->emitted_symbols) {
2187 struct diff_options *o = ecbdata->opt;
2188 struct emitted_diff_symbols *wol = wo->emitted_symbols;
2189 int i;
2190
2191 /*
2192 * NEEDSWORK:
2193 * Instead of appending each, concat all words to a line?
2194 */
2195 for (i = 0; i < wol->nr; i++)
2196 append_emitted_diff_symbol(o, &wol->buf[i]);
2197
2198 for (i = 0; i < wol->nr; i++)
2199 free((void *)wol->buf[i].line);
2200
2201 wol->nr = 0;
2202 }
Junio C Hamano76fd2822009-10-30 10:09:06 -07002203}
2204
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002205static void diff_filespec_load_driver(struct diff_filespec *one,
2206 struct index_state *istate)
Thomas Rast77d1a522012-03-14 19:24:08 +01002207{
2208 /* Use already-loaded driver */
2209 if (one->driver)
2210 return;
2211
2212 if (S_ISREG(one->mode))
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002213 one->driver = userdiff_find_by_path(istate, one->path);
Thomas Rast77d1a522012-03-14 19:24:08 +01002214
2215 /* Fallback to default settings */
2216 if (!one->driver)
2217 one->driver = userdiff_find_by_name("default");
2218}
2219
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002220static const char *userdiff_word_regex(struct diff_filespec *one,
2221 struct index_state *istate)
Thomas Rast77d1a522012-03-14 19:24:08 +01002222{
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002223 diff_filespec_load_driver(one, istate);
Thomas Rast77d1a522012-03-14 19:24:08 +01002224 return one->driver->word_regex;
2225}
2226
2227static void init_diff_words_data(struct emit_callback *ecbdata,
Thomas Rast6440d342012-03-14 19:24:09 +01002228 struct diff_options *orig_opts,
Thomas Rast77d1a522012-03-14 19:24:08 +01002229 struct diff_filespec *one,
2230 struct diff_filespec *two)
2231{
2232 int i;
Thomas Rast6440d342012-03-14 19:24:09 +01002233 struct diff_options *o = xmalloc(sizeof(struct diff_options));
2234 memcpy(o, orig_opts, sizeof(struct diff_options));
Thomas Rast77d1a522012-03-14 19:24:08 +01002235
René Scharfeca56dad2021-03-13 17:17:22 +01002236 CALLOC_ARRAY(ecbdata->diff_words, 1);
Thomas Rast77d1a522012-03-14 19:24:08 +01002237 ecbdata->diff_words->type = o->word_diff;
2238 ecbdata->diff_words->opt = o;
Stefan Bellere6e045f2017-06-29 17:07:06 -07002239
2240 if (orig_opts->emitted_symbols)
René Scharfeca56dad2021-03-13 17:17:22 +01002241 CALLOC_ARRAY(o->emitted_symbols, 1);
Stefan Bellere6e045f2017-06-29 17:07:06 -07002242
Thomas Rast77d1a522012-03-14 19:24:08 +01002243 if (!o->word_regex)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002244 o->word_regex = userdiff_word_regex(one, o->repo->index);
Thomas Rast77d1a522012-03-14 19:24:08 +01002245 if (!o->word_regex)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002246 o->word_regex = userdiff_word_regex(two, o->repo->index);
Thomas Rast77d1a522012-03-14 19:24:08 +01002247 if (!o->word_regex)
2248 o->word_regex = diff_word_regex_cfg;
2249 if (o->word_regex) {
2250 ecbdata->diff_words->word_regex = (regex_t *)
2251 xmalloc(sizeof(regex_t));
2252 if (regcomp(ecbdata->diff_words->word_regex,
2253 o->word_regex,
2254 REG_EXTENDED | REG_NEWLINE))
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02002255 die("invalid regular expression: %s",
2256 o->word_regex);
Thomas Rast77d1a522012-03-14 19:24:08 +01002257 }
2258 for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) {
2259 if (o->word_diff == diff_words_styles[i].type) {
2260 ecbdata->diff_words->style =
2261 &diff_words_styles[i];
2262 break;
2263 }
2264 }
2265 if (want_color(o->use_color)) {
2266 struct diff_words_style *st = ecbdata->diff_words->style;
Brandon Williams63a01c32018-02-14 10:59:39 -08002267 st->old_word.color = diff_get_color_opt(o, DIFF_FILE_OLD);
2268 st->new_word.color = diff_get_color_opt(o, DIFF_FILE_NEW);
Jeff King8dbf3eb2015-05-27 16:48:46 -04002269 st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT);
Thomas Rast77d1a522012-03-14 19:24:08 +01002270 }
2271}
2272
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002273static void free_diff_words_data(struct emit_callback *ecbdata)
2274{
2275 if (ecbdata->diff_words) {
Junio C Hamano76fd2822009-10-30 10:09:06 -07002276 diff_words_flush(ecbdata);
Stefan Bellere6e045f2017-06-29 17:07:06 -07002277 free (ecbdata->diff_words->opt->emitted_symbols);
Thomas Rast6440d342012-03-14 19:24:09 +01002278 free (ecbdata->diff_words->opt);
Jim Meyering8e0f7002008-01-31 18:26:32 +01002279 free (ecbdata->diff_words->minus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002280 free (ecbdata->diff_words->minus.orig);
Jim Meyering8e0f7002008-01-31 18:26:32 +01002281 free (ecbdata->diff_words->plus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002282 free (ecbdata->diff_words->plus.orig);
Brandon Caseyef5644e2010-09-09 14:02:45 -05002283 if (ecbdata->diff_words->word_regex) {
2284 regfree(ecbdata->diff_words->word_regex);
2285 free(ecbdata->diff_words->word_regex);
2286 }
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002287 FREE_AND_NULL(ecbdata->diff_words);
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002288 }
2289}
2290
Jeff Kingce436972006-07-23 05:24:18 -04002291const char *diff_get_color(int diff_use_color, enum color_diff ix)
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002292{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07002293 if (want_color(diff_use_color))
Linus Torvalds50f575f2006-06-22 13:53:31 -07002294 return diff_colors[ix];
2295 return "";
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002296}
2297
John Keepingf1922232013-02-07 20:15:26 +00002298const char *diff_line_prefix(struct diff_options *opt)
2299{
2300 struct strbuf *msgbuf;
2301 if (!opt->output_prefix)
2302 return "";
2303
2304 msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
2305 return msgbuf->buf;
2306}
2307
Jeff King19b90462019-02-14 00:48:27 -05002308static unsigned long sane_truncate_line(char *line, unsigned long len)
Junio C Hamano23707812008-01-02 01:50:11 -08002309{
2310 const char *cp;
2311 unsigned long allot;
2312 size_t l = len;
2313
Junio C Hamano23707812008-01-02 01:50:11 -08002314 cp = line;
2315 allot = l;
2316 while (0 < l) {
2317 (void) utf8_width(&cp, &l);
2318 if (!cp)
2319 break; /* truncated in the middle? */
2320 }
2321 return allot - l;
2322}
2323
Junio C Hamanod68fe262009-09-14 22:05:57 -07002324static void find_lno(const char *line, struct emit_callback *ecbdata)
Junio C Hamano690ed842009-09-04 00:41:15 -07002325{
Junio C Hamanod68fe262009-09-14 22:05:57 -07002326 const char *p;
2327 ecbdata->lno_in_preimage = 0;
2328 ecbdata->lno_in_postimage = 0;
2329 p = strchr(line, '-');
Junio C Hamano690ed842009-09-04 00:41:15 -07002330 if (!p)
Junio C Hamanod68fe262009-09-14 22:05:57 -07002331 return; /* cannot happen */
2332 ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10);
2333 p = strchr(p, '+');
2334 if (!p)
2335 return; /* cannot happen */
2336 ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10);
Junio C Hamano690ed842009-09-04 00:41:15 -07002337}
2338
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002339static int fn_out_consume(void *priv, char *line, unsigned long len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002340{
Junio C Hamano6973dca2006-04-21 23:57:45 -07002341 struct emit_callback *ecbdata = priv;
Bo Yang7be57612010-05-26 15:23:54 +08002342 struct diff_options *o = ecbdata->opt;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002343
Stefan Bellerba162332016-09-07 16:36:46 -07002344 o->found_changes = 1;
2345
Greg Bacon3e97c7c2009-11-19 15:12:24 -06002346 if (ecbdata->header) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07002347 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
2348 ecbdata->header->buf, ecbdata->header->len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06002349 strbuf_reset(ecbdata->header);
2350 ecbdata->header = NULL;
2351 }
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01002352
Junio C Hamano6973dca2006-04-21 23:57:45 -07002353 if (ecbdata->label_path[0]) {
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07002354 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
2355 ecbdata->label_path[0],
2356 strlen(ecbdata->label_path[0]), 0);
2357 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
2358 ecbdata->label_path[1],
2359 strlen(ecbdata->label_path[1]), 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002360 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
2361 }
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002362
Jim Meyeringa624eaa2008-08-15 13:39:26 +02002363 if (diff_suppress_blank_empty
2364 && len == 2 && line[0] == ' ' && line[1] == '\n') {
2365 line[0] = '\n';
2366 len = 1;
2367 }
2368
Junio C Hamanob8d9c1a2009-09-03 23:59:25 -07002369 if (line[0] == '@') {
Junio C Hamano76fd2822009-10-30 10:09:06 -07002370 if (ecbdata->diff_words)
2371 diff_words_flush(ecbdata);
Jeff King19b90462019-02-14 00:48:27 -05002372 len = sane_truncate_line(line, len);
Junio C Hamanod68fe262009-09-14 22:05:57 -07002373 find_lno(line, ecbdata);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01002374 emit_hunk_header(ecbdata, line, len);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002375 return 0;
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002376 }
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002377
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002378 if (ecbdata->diff_words) {
Stefan Bellerff958672017-06-29 17:06:54 -07002379 enum diff_symbol s =
2380 ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ?
2381 DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002382 if (line[0] == '-') {
2383 diff_words_append(line, len,
2384 &ecbdata->diff_words->minus);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002385 return 0;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002386 } else if (line[0] == '+') {
2387 diff_words_append(line, len,
2388 &ecbdata->diff_words->plus);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002389 return 0;
Christian Couder59556542013-11-30 21:55:40 +01002390 } else if (starts_with(line, "\\ ")) {
Thomas Rastc7c2bc02012-01-12 12:15:33 +01002391 /*
2392 * Eat the "no newline at eof" marker as if we
2393 * saw a "+" or "-" line with nothing on it,
2394 * and return without diff_words_flush() to
2395 * defer processing. If this is the end of
2396 * preimage, more "+" lines may come after it.
2397 */
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002398 return 0;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002399 }
Junio C Hamano76fd2822009-10-30 10:09:06 -07002400 diff_words_flush(ecbdata);
Stefan Bellerff958672017-06-29 17:06:54 -07002401 emit_diff_symbol(o, s, line, len, 0);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002402 return 0;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002403 }
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002404
Junio C Hamano0e383e12015-05-26 09:56:33 -07002405 switch (line[0]) {
2406 case '+':
Junio C Hamanod68fe262009-09-14 22:05:57 -07002407 ecbdata->lno_in_postimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05002408 emit_add_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002409 break;
2410 case '-':
2411 ecbdata->lno_in_preimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05002412 emit_del_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002413 break;
2414 case ' ':
2415 ecbdata->lno_in_postimage++;
2416 ecbdata->lno_in_preimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05002417 emit_context_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002418 break;
2419 default:
2420 /* incomplete line at the end */
2421 ecbdata->lno_in_preimage++;
Stefan Bellerf2bb1212017-06-29 17:06:55 -07002422 emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE,
2423 line, len, 0);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002424 break;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002425 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002426 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002427}
2428
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002429static void pprint_rename(struct strbuf *name, const char *a, const char *b)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002430{
Brandon Williams63a01c32018-02-14 10:59:39 -08002431 const char *old_name = a;
2432 const char *new_name = b;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002433 int pfx_length, sfx_length;
Thomas Rastdd281f02013-02-26 21:47:01 +01002434 int pfx_adjust_for_slash;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002435 int len_a = strlen(a);
2436 int len_b = strlen(b);
Pierre Habouzit663af342007-09-20 00:42:15 +02002437 int a_midlen, b_midlen;
Alexandre Julliarde5bfbf92007-02-10 15:39:00 +01002438 int qlen_a = quote_c_style(a, NULL, NULL, 0);
2439 int qlen_b = quote_c_style(b, NULL, NULL, 0);
2440
2441 if (qlen_a || qlen_b) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002442 quote_c_style(a, name, NULL, 0);
2443 strbuf_addstr(name, " => ");
2444 quote_c_style(b, name, NULL, 0);
2445 return;
Alexandre Julliarde5bfbf92007-02-10 15:39:00 +01002446 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002447
2448 /* Find common prefix */
2449 pfx_length = 0;
Brandon Williams63a01c32018-02-14 10:59:39 -08002450 while (*old_name && *new_name && *old_name == *new_name) {
2451 if (*old_name == '/')
2452 pfx_length = old_name - a + 1;
2453 old_name++;
2454 new_name++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002455 }
2456
2457 /* Find common suffix */
Brandon Williams63a01c32018-02-14 10:59:39 -08002458 old_name = a + len_a;
2459 new_name = b + len_b;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002460 sfx_length = 0;
Antoine Pelissed020e272013-02-23 17:48:45 +01002461 /*
Thomas Rastdd281f02013-02-26 21:47:01 +01002462 * If there is a common prefix, it must end in a slash. In
2463 * that case we let this loop run 1 into the prefix to see the
2464 * same slash.
2465 *
2466 * If there is no common prefix, we cannot do this as it would
2467 * underrun the input strings.
Antoine Pelissed020e272013-02-23 17:48:45 +01002468 */
Thomas Rastdd281f02013-02-26 21:47:01 +01002469 pfx_adjust_for_slash = (pfx_length ? 1 : 0);
Brandon Williams63a01c32018-02-14 10:59:39 -08002470 while (a + pfx_length - pfx_adjust_for_slash <= old_name &&
2471 b + pfx_length - pfx_adjust_for_slash <= new_name &&
2472 *old_name == *new_name) {
2473 if (*old_name == '/')
2474 sfx_length = len_a - (old_name - a);
2475 old_name--;
2476 new_name--;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002477 }
2478
2479 /*
2480 * pfx{mid-a => mid-b}sfx
2481 * {pfx-a => pfx-b}sfx
2482 * pfx{sfx-a => sfx-b}
2483 * name-a => name-b
2484 */
Pierre Habouzit663af342007-09-20 00:42:15 +02002485 a_midlen = len_a - pfx_length - sfx_length;
2486 b_midlen = len_b - pfx_length - sfx_length;
2487 if (a_midlen < 0)
2488 a_midlen = 0;
2489 if (b_midlen < 0)
2490 b_midlen = 0;
Junio C Hamanocc908b82006-05-14 22:07:28 -07002491
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002492 strbuf_grow(name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
Pierre Habouzit663af342007-09-20 00:42:15 +02002493 if (pfx_length + sfx_length) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002494 strbuf_add(name, a, pfx_length);
2495 strbuf_addch(name, '{');
Junio C Hamano6973dca2006-04-21 23:57:45 -07002496 }
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002497 strbuf_add(name, a + pfx_length, a_midlen);
2498 strbuf_addstr(name, " => ");
2499 strbuf_add(name, b + pfx_length, b_midlen);
Pierre Habouzit663af342007-09-20 00:42:15 +02002500 if (pfx_length + sfx_length) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002501 strbuf_addch(name, '}');
2502 strbuf_add(name, a + len_a - sfx_length, sfx_length);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002503 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002504}
2505
Junio C Hamano6973dca2006-04-21 23:57:45 -07002506static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
2507 const char *name_a,
2508 const char *name_b)
2509{
2510 struct diffstat_file *x;
René Scharfeca56dad2021-03-13 17:17:22 +01002511 CALLOC_ARRAY(x, 1);
Dmitry S. Dolzhenko4c960a42014-03-04 02:31:53 +04002512 ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002513 diffstat->files[diffstat->nr++] = x;
2514 if (name_b) {
Junio C Hamanof6046522007-12-11 23:46:30 -08002515 x->from_name = xstrdup(name_a);
2516 x->name = xstrdup(name_b);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002517 x->is_renamed = 1;
2518 }
Junio C Hamanof6046522007-12-11 23:46:30 -08002519 else {
2520 x->from_name = NULL;
Shawn Pearce9befac42006-09-02 00:16:31 -04002521 x->name = xstrdup(name_a);
Junio C Hamanof6046522007-12-11 23:46:30 -08002522 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002523 return x;
2524}
2525
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002526static int diffstat_consume(void *priv, char *line, unsigned long len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002527{
2528 struct diffstat_t *diffstat = priv;
2529 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
2530
2531 if (line[0] == '+')
2532 x->added++;
2533 else if (line[0] == '-')
2534 x->deleted++;
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002535 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002536}
2537
Johannes Schindelin698ce6f2006-05-20 15:40:29 +02002538const char mime_boundary_leader[] = "------------";
Junio C Hamano6973dca2006-04-21 23:57:45 -07002539
Junio C Hamanoa2540022006-09-26 18:53:02 -07002540static int scale_linear(int it, int width, int max_change)
2541{
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002542 if (!it)
2543 return 0;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002544 /*
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002545 * make sure that at least one '-' or '+' is printed if
2546 * there is any change to this path. The easiest way is to
Elijah Newren15beaaa2019-11-05 17:07:23 +00002547 * scale linearly as if the allotted width is one column shorter
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002548 * than it is, and then add 1 to the result.
Junio C Hamanoa2540022006-09-26 18:53:02 -07002549 */
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002550 return 1 + (it * (width - 1) / max_change);
Junio C Hamanoa2540022006-09-26 18:53:02 -07002551}
2552
Stefan Beller0911c472017-06-29 17:07:02 -07002553static void show_graph(struct strbuf *out, char ch, int cnt,
2554 const char *set, const char *reset)
Junio C Hamanoa2540022006-09-26 18:53:02 -07002555{
2556 if (cnt <= 0)
2557 return;
Stefan Beller0911c472017-06-29 17:07:02 -07002558 strbuf_addstr(out, set);
2559 strbuf_addchars(out, ch, cnt);
2560 strbuf_addstr(out, reset);
Junio C Hamanoa2540022006-09-26 18:53:02 -07002561}
2562
Junio C Hamanof6046522007-12-11 23:46:30 -08002563static void fill_print_name(struct diffstat_file *file)
2564{
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002565 struct strbuf pname = STRBUF_INIT;
Junio C Hamanof6046522007-12-11 23:46:30 -08002566
2567 if (file->print_name)
2568 return;
2569
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002570 if (file->is_renamed)
2571 pprint_rename(&pname, file->from_name, file->name);
2572 else
2573 quote_c_style(file->name, &pname, NULL, 0);
2574
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 21:09:59 +07002575 if (file->comments)
2576 strbuf_addf(&pname, " (%s)", file->comments);
2577
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002578 file->print_name = strbuf_detach(&pname, NULL);
Junio C Hamanof6046522007-12-11 23:46:30 -08002579}
2580
Stefan Beller0911c472017-06-29 17:07:02 -07002581static void print_stat_summary_inserts_deletes(struct diff_options *options,
2582 int files, int insertions, int deletions)
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002583{
2584 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002585
2586 if (!files) {
2587 assert(insertions == 0 && deletions == 0);
Stefan Beller0911c472017-06-29 17:07:02 -07002588 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
2589 NULL, 0, 0);
2590 return;
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002591 }
2592
2593 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002594 (files == 1) ? " %d file changed" : " %d files changed",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002595 files);
2596
2597 /*
2598 * For binary diff, the caller may want to print "x files
2599 * changed" with insertions == 0 && deletions == 0.
2600 *
2601 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2602 * is probably less confusing (i.e skip over "2 files changed
2603 * but nothing about added/removed lines? Is this a bug in Git?").
2604 */
2605 if (insertions || deletions == 0) {
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002606 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002607 (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002608 insertions);
2609 }
2610
2611 if (deletions || insertions == 0) {
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002612 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002613 (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002614 deletions);
2615 }
2616 strbuf_addch(&sb, '\n');
Stefan Beller0911c472017-06-29 17:07:02 -07002617 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
2618 sb.buf, sb.len, 0);
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002619 strbuf_release(&sb);
Stefan Beller0911c472017-06-29 17:07:02 -07002620}
2621
2622void print_stat_summary(FILE *fp, int files,
2623 int insertions, int deletions)
2624{
2625 struct diff_options o;
2626 memset(&o, 0, sizeof(o));
2627 o.file = fp;
2628
2629 print_stat_summary_inserts_deletes(&o, files, insertions, deletions);
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002630}
2631
Felipe Contreras4b25d092009-05-01 12:06:36 +03002632static void show_stats(struct diffstat_t *data, struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002633{
Benjamin Kramereb3a9dd2009-03-07 21:02:10 +01002634 int i, len, add, del, adds = 0, dels = 0;
Jeff King0974c112010-04-17 13:41:08 -04002635 uintmax_t max_change = 0, max_len = 0;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002636 int total_files = data->nr, count;
2637 int width, name_width, graph_width, number_width = 0, bin_width = 0;
Johannes Schindelinc0aa3352011-03-22 13:50:08 +01002638 const char *reset, *add_c, *del_c;
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002639 int extra_shown = 0;
Stefan Beller0911c472017-06-29 17:07:02 -07002640 const char *line_prefix = diff_line_prefix(options);
2641 struct strbuf out = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002642
2643 if (data->nr == 0)
2644 return;
2645
Michael J Gruber808e1db2011-05-27 14:36:41 +02002646 count = options->stat_count ? options->stat_count : data->nr;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002647
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01002648 reset = diff_get_color_opt(options, DIFF_RESET);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01002649 add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
2650 del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
Junio C Hamano785f7432006-09-26 18:59:41 -07002651
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002652 /*
2653 * Find the longest filename and max number of changes
2654 */
Michael J Gruber808e1db2011-05-27 14:36:41 +02002655 for (i = 0; (i < count) && (i < data->nr); i++) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002656 struct diffstat_file *file = data->files[i];
Jeff King0974c112010-04-17 13:41:08 -04002657 uintmax_t change = file->added + file->deleted;
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002658
2659 if (!file->is_interesting && (change == 0)) {
Michael J Gruber808e1db2011-05-27 14:36:41 +02002660 count++; /* not shown == room for one more */
Michael J Gruber358e4602011-05-27 14:36:40 +02002661 continue;
2662 }
Junio C Hamanof6046522007-12-11 23:46:30 -08002663 fill_print_name(file);
2664 len = strlen(file->print_name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002665 if (max_len < len)
2666 max_len = len;
2667
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002668 if (file->is_unmerged) {
2669 /* "Unmerged" is 8 characters */
2670 bin_width = bin_width < 8 ? 8 : bin_width;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002671 continue;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002672 }
2673 if (file->is_binary) {
2674 /* "Bin XXX -> YYY bytes" */
2675 int w = 14 + decimal_width(file->added)
2676 + decimal_width(file->deleted);
2677 bin_width = bin_width < w ? w : bin_width;
2678 /* Display change counts aligned with "Bin" */
2679 number_width = 3;
2680 continue;
2681 }
2682
Junio C Hamanoa2540022006-09-26 18:53:02 -07002683 if (max_change < change)
2684 max_change = change;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002685 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002686 count = i; /* where we can stop scanning in data->files[] */
Junio C Hamano6973dca2006-04-21 23:57:45 -07002687
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002688 /*
2689 * We have width = stat_width or term_columns() columns total.
2690 * We want a maximum of min(max_len, stat_name_width) for the name part.
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002691 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002692 * We also need 1 for " " and 4 + decimal_width(max_change)
2693 * for " | NNNN " and one the empty column at the end, altogether
2694 * 6 + decimal_width(max_change).
Junio C Hamanoa2540022006-09-26 18:53:02 -07002695 *
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002696 * If there's not enough space, we will use the smaller of
2697 * stat_name_width (if set) and 5/8*width for the filename,
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002698 * and the rest for constant elements + graph part, but no more
2699 * than stat_graph_width for the graph part.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002700 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2701 * for the standard terminal size).
2702 *
2703 * In other words: stat_width limits the maximum width, and
2704 * stat_name_width fixes the maximum width of the filename,
2705 * and is also used to divide available columns if there
2706 * aren't enough.
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002707 *
2708 * Binary files are displayed with "Bin XXX -> YYY bytes"
2709 * instead of the change count and graph. This part is treated
2710 * similarly to the graph part, except that it is not
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +02002711 * "scaled". If total width is too small to accommodate the
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002712 * guaranteed minimum width of the filename part and the
2713 * separators and this message, this message will "overflow"
2714 * making the line longer than the maximum width.
Junio C Hamanoa2540022006-09-26 18:53:02 -07002715 */
Junio C Hamanoa2540022006-09-26 18:53:02 -07002716
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002717 if (options->stat_width == -1)
Junio C Hamanocd48dad2016-08-31 16:27:19 -07002718 width = term_columns() - strlen(line_prefix);
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002719 else
2720 width = options->stat_width ? options->stat_width : 80;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002721 number_width = decimal_width(max_change) > number_width ?
2722 decimal_width(max_change) : number_width;
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002723
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +01002724 if (options->stat_graph_width == -1)
2725 options->stat_graph_width = diff_stat_graph_width;
2726
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002727 /*
2728 * Guarantee 3/8*16==6 for the graph part
2729 * and 5/8*16==10 for the filename part
2730 */
2731 if (width < 16 + 6 + number_width)
2732 width = 16 + 6 + number_width;
2733
2734 /*
2735 * First assign sizes that are wanted, ignoring available width.
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002736 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2737 * starting from "XXX" should fit in graph_width.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002738 */
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002739 graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4;
2740 if (options->stat_graph_width &&
2741 options->stat_graph_width < graph_width)
2742 graph_width = options->stat_graph_width;
2743
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002744 name_width = (options->stat_name_width > 0 &&
2745 options->stat_name_width < max_len) ?
2746 options->stat_name_width : max_len;
2747
2748 /*
2749 * Adjust adjustable widths not to exceed maximum width
2750 */
2751 if (name_width + number_width + 6 + graph_width > width) {
Lucian Poston678c5742012-04-18 14:12:18 -07002752 if (graph_width > width * 3/8 - number_width - 6) {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002753 graph_width = width * 3/8 - number_width - 6;
Lucian Poston678c5742012-04-18 14:12:18 -07002754 if (graph_width < 6)
2755 graph_width = 6;
2756 }
2757
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002758 if (options->stat_graph_width &&
2759 graph_width > options->stat_graph_width)
2760 graph_width = options->stat_graph_width;
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002761 if (name_width > width - number_width - 6 - graph_width)
2762 name_width = width - number_width - 6 - graph_width;
2763 else
2764 graph_width = width - number_width - 6 - name_width;
2765 }
2766
2767 /*
2768 * From here name_width is the width of the name area,
2769 * and graph_width is the width of the graph area.
2770 * max_change is used to scale graph properly.
2771 */
Michael J Gruber808e1db2011-05-27 14:36:41 +02002772 for (i = 0; i < count; i++) {
Timo Hirvonend2543b82006-06-24 20:20:32 +03002773 const char *prefix = "";
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002774 struct diffstat_file *file = data->files[i];
2775 char *name = file->print_name;
2776 uintmax_t added = file->added;
2777 uintmax_t deleted = file->deleted;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002778 int name_len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002779
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002780 if (!file->is_interesting && (added + deleted == 0))
Michael J Gruber358e4602011-05-27 14:36:40 +02002781 continue;
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002782
Junio C Hamano6973dca2006-04-21 23:57:45 -07002783 /*
2784 * "scale" the filename
2785 */
Junio C Hamanoa2540022006-09-26 18:53:02 -07002786 len = name_width;
2787 name_len = strlen(name);
2788 if (name_width < name_len) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002789 char *slash;
2790 prefix = "...";
Junio C Hamanoa2540022006-09-26 18:53:02 -07002791 len -= 3;
2792 name += name_len - len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002793 slash = strchr(name, '/');
2794 if (slash)
2795 name = slash;
2796 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002797
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002798 if (file->is_binary) {
Stefan Beller0911c472017-06-29 17:07:02 -07002799 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2800 strbuf_addf(&out, " %*s", number_width, "Bin");
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002801 if (!added && !deleted) {
Stefan Beller0911c472017-06-29 17:07:02 -07002802 strbuf_addch(&out, '\n');
2803 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2804 out.buf, out.len, 0);
2805 strbuf_reset(&out);
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002806 continue;
2807 }
Stefan Beller0911c472017-06-29 17:07:02 -07002808 strbuf_addf(&out, " %s%"PRIuMAX"%s",
Jeff King0974c112010-04-17 13:41:08 -04002809 del_c, deleted, reset);
Stefan Beller0911c472017-06-29 17:07:02 -07002810 strbuf_addstr(&out, " -> ");
2811 strbuf_addf(&out, "%s%"PRIuMAX"%s",
Jeff King0974c112010-04-17 13:41:08 -04002812 add_c, added, reset);
Stefan Beller0911c472017-06-29 17:07:02 -07002813 strbuf_addstr(&out, " bytes\n");
2814 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2815 out.buf, out.len, 0);
2816 strbuf_reset(&out);
Junio C Hamanof6046522007-12-11 23:46:30 -08002817 continue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002818 }
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002819 else if (file->is_unmerged) {
Stefan Beller0911c472017-06-29 17:07:02 -07002820 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2821 strbuf_addstr(&out, " Unmerged\n");
2822 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2823 out.buf, out.len, 0);
2824 strbuf_reset(&out);
Junio C Hamanof6046522007-12-11 23:46:30 -08002825 continue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002826 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002827
Junio C Hamanoa2540022006-09-26 18:53:02 -07002828 /*
2829 * scale the add/delete
2830 */
Junio C Hamano6973dca2006-04-21 23:57:45 -07002831 add = added;
2832 del = deleted;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002833
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002834 if (graph_width <= max_change) {
Stefan Bellerd3c9cf32013-07-14 23:35:49 +02002835 int total = scale_linear(add + del, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002836 if (total < 2 && add && del)
2837 /* width >= 2 due to the sanity check */
2838 total = 2;
2839 if (add < del) {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002840 add = scale_linear(add, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002841 del = total - add;
2842 } else {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002843 del = scale_linear(del, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002844 add = total - del;
2845 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002846 }
Stefan Beller0911c472017-06-29 17:07:02 -07002847 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2848 strbuf_addf(&out, " %*"PRIuMAX"%s",
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002849 number_width, added + deleted,
2850 added + deleted ? " " : "");
Stefan Beller0911c472017-06-29 17:07:02 -07002851 show_graph(&out, '+', add, add_c, reset);
2852 show_graph(&out, '-', del, del_c, reset);
2853 strbuf_addch(&out, '\n');
2854 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2855 out.buf, out.len, 0);
2856 strbuf_reset(&out);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002857 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002858
2859 for (i = 0; i < data->nr; i++) {
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002860 struct diffstat_file *file = data->files[i];
2861 uintmax_t added = file->added;
2862 uintmax_t deleted = file->deleted;
Junio C Hamano82dfc2c2012-11-27 12:05:10 -08002863
2864 if (file->is_unmerged ||
2865 (!file->is_interesting && (added + deleted == 0))) {
Michael J Gruber808e1db2011-05-27 14:36:41 +02002866 total_files--;
2867 continue;
2868 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002869
Junio C Hamano82dfc2c2012-11-27 12:05:10 -08002870 if (!file->is_binary) {
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002871 adds += added;
2872 dels += deleted;
2873 }
2874 if (i < count)
2875 continue;
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002876 if (!extra_shown)
Stefan Beller0911c472017-06-29 17:07:02 -07002877 emit_diff_symbol(options,
2878 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
2879 NULL, 0, 0);
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002880 extra_shown = 1;
Michael J Gruber808e1db2011-05-27 14:36:41 +02002881 }
Stefan Beller0911c472017-06-29 17:07:02 -07002882
2883 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
Rene Scharfe5a612012017-08-30 19:49:43 +02002884 strbuf_release(&out);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002885}
2886
Felipe Contreras2775d922009-10-11 23:46:11 +03002887static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002888{
2889 int i, adds = 0, dels = 0, total_files = data->nr;
2890
2891 if (data->nr == 0)
2892 return;
2893
2894 for (i = 0; i < data->nr; i++) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002895 int added = data->files[i]->added;
Stefan Beller0911c472017-06-29 17:07:02 -07002896 int deleted = data->files[i]->deleted;
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002897
Junio C Hamano20c8cde2012-11-27 14:19:36 -08002898 if (data->files[i]->is_unmerged ||
2899 (!data->files[i]->is_interesting && (added + deleted == 0))) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002900 total_files--;
Alexander Strasserde9658b2012-06-15 23:50:30 +02002901 } else if (!data->files[i]->is_binary) { /* don't count bytes */
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002902 adds += added;
2903 dels += deleted;
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002904 }
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002905 }
Stefan Beller0911c472017-06-29 17:07:02 -07002906 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002907}
2908
Felipe Contreras4b25d092009-05-01 12:06:36 +03002909static void show_numstat(struct diffstat_t *data, struct diff_options *options)
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002910{
2911 int i;
2912
Junio C Hamanof6046522007-12-11 23:46:30 -08002913 if (data->nr == 0)
2914 return;
2915
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002916 for (i = 0; i < data->nr; i++) {
2917 struct diffstat_file *file = data->files[i];
2918
John Keeping30997bb2013-02-07 20:15:27 +00002919 fprintf(options->file, "%s", diff_line_prefix(options));
Bo Yang7be57612010-05-26 15:23:54 +08002920
Junio C Hamanobfddbc52006-12-10 13:50:59 -08002921 if (file->is_binary)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002922 fprintf(options->file, "-\t-\t");
Junio C Hamanobfddbc52006-12-10 13:50:59 -08002923 else
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002924 fprintf(options->file,
Jeff King0974c112010-04-17 13:41:08 -04002925 "%"PRIuMAX"\t%"PRIuMAX"\t",
2926 file->added, file->deleted);
Junio C Hamanof6046522007-12-11 23:46:30 -08002927 if (options->line_termination) {
2928 fill_print_name(file);
2929 if (!file->is_renamed)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002930 write_name_quoted(file->name, options->file,
Junio C Hamanof6046522007-12-11 23:46:30 -08002931 options->line_termination);
2932 else {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002933 fputs(file->print_name, options->file);
2934 putc(options->line_termination, options->file);
Junio C Hamanof6046522007-12-11 23:46:30 -08002935 }
Pierre Habouzit663af342007-09-20 00:42:15 +02002936 } else {
Junio C Hamanof6046522007-12-11 23:46:30 -08002937 if (file->is_renamed) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002938 putc('\0', options->file);
2939 write_name_quoted(file->from_name, options->file, '\0');
Junio C Hamanof6046522007-12-11 23:46:30 -08002940 }
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002941 write_name_quoted(file->name, options->file, '\0');
Pierre Habouzit663af342007-09-20 00:42:15 +02002942 }
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002943 }
2944}
2945
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002946struct dirstat_file {
2947 const char *name;
2948 unsigned long changed;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002949};
2950
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002951struct dirstat_dir {
2952 struct dirstat_file *files;
Johan Herland712d2c72011-04-29 11:36:20 +02002953 int alloc, nr, permille, cumulative;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002954};
2955
Bo Yang7be57612010-05-26 15:23:54 +08002956static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
2957 unsigned long changed, const char *base, int baselen)
Linus Torvalds7df7c012008-02-12 13:26:31 -08002958{
Brandon Williams585c0e22018-02-14 10:59:28 -08002959 unsigned long sum_changes = 0;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002960 unsigned int sources = 0;
John Keeping30997bb2013-02-07 20:15:27 +00002961 const char *line_prefix = diff_line_prefix(opt);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002962
2963 while (dir->nr) {
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002964 struct dirstat_file *f = dir->files;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002965 int namelen = strlen(f->name);
Brandon Williams585c0e22018-02-14 10:59:28 -08002966 unsigned long changes;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002967 char *slash;
2968
2969 if (namelen < baselen)
2970 break;
2971 if (memcmp(f->name, base, baselen))
2972 break;
2973 slash = strchr(f->name + baselen, '/');
2974 if (slash) {
2975 int newbaselen = slash + 1 - f->name;
Brandon Williams585c0e22018-02-14 10:59:28 -08002976 changes = gather_dirstat(opt, dir, changed, f->name, newbaselen);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002977 sources++;
2978 } else {
Brandon Williams585c0e22018-02-14 10:59:28 -08002979 changes = f->changed;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002980 dir->files++;
2981 dir->nr--;
2982 sources += 2;
2983 }
Brandon Williams585c0e22018-02-14 10:59:28 -08002984 sum_changes += changes;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002985 }
2986
2987 /*
2988 * We don't report dirstat's for
2989 * - the top level
2990 * - or cases where everything came from a single directory
2991 * under this directory (sources == 1).
2992 */
2993 if (baselen && sources != 1) {
Brandon Williams585c0e22018-02-14 10:59:28 -08002994 if (sum_changes) {
2995 int permille = sum_changes * 1000 / changed;
Johan Herland712d2c72011-04-29 11:36:20 +02002996 if (permille >= dir->permille) {
Bo Yang7be57612010-05-26 15:23:54 +08002997 fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix,
Johan Herland712d2c72011-04-29 11:36:20 +02002998 permille / 10, permille % 10, baselen, base);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002999 if (!dir->cumulative)
3000 return 0;
3001 }
3002 }
3003 }
Brandon Williams585c0e22018-02-14 10:59:28 -08003004 return sum_changes;
Linus Torvalds7df7c012008-02-12 13:26:31 -08003005}
3006
Linus Torvalds441bca02008-08-28 16:19:08 -07003007static int dirstat_compare(const void *_a, const void *_b)
3008{
3009 const struct dirstat_file *a = _a;
3010 const struct dirstat_file *b = _b;
3011 return strcmp(a->name, b->name);
3012}
3013
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003014static void show_dirstat(struct diff_options *options)
Linus Torvalds7df7c012008-02-12 13:26:31 -08003015{
3016 int i;
3017 unsigned long changed;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003018 struct dirstat_dir dir;
3019 struct diff_queue_struct *q = &diff_queued_diff;
Linus Torvalds7df7c012008-02-12 13:26:31 -08003020
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003021 dir.files = NULL;
3022 dir.alloc = 0;
3023 dir.nr = 0;
Johan Herland712d2c72011-04-29 11:36:20 +02003024 dir.permille = options->dirstat_permille;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003025 dir.cumulative = options->flags.dirstat_cumulative;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003026
Linus Torvalds7df7c012008-02-12 13:26:31 -08003027 changed = 0;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003028 for (i = 0; i < q->nr; i++) {
3029 struct diff_filepair *p = q->queue[i];
3030 const char *name;
3031 unsigned long copied, added, damage;
Jonathan Tan1c37e862020-04-07 15:11:41 -07003032 struct diff_populate_filespec_options dpf_options = {
3033 .check_size_only = 1,
3034 };
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003035
Johan Herland2ca86712011-04-12 11:24:34 +02003036 name = p->two->path ? p->two->path : p->one->path;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003037
Jeff Kingd9f62df2018-08-28 17:23:03 -04003038 if (p->one->oid_valid && p->two->oid_valid &&
3039 oideq(&p->one->oid, &p->two->oid)) {
Johan Herland2ff3a802011-04-11 00:48:52 +02003040 /*
3041 * The SHA1 has not changed, so pre-/post-content is
3042 * identical. We can therefore skip looking at the
3043 * file contents altogether.
3044 */
3045 damage = 0;
3046 goto found_damage;
3047 }
3048
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003049 if (options->flags.dirstat_by_file) {
Johan Herland0133dab2011-04-11 00:48:51 +02003050 /*
3051 * In --dirstat-by-file mode, we don't really need to
3052 * look at the actual file contents at all.
3053 * The fact that the SHA1 changed is enough for us to
3054 * add this file to the list of results
3055 * (with each file contributing equal damage).
3056 */
Johan Herland2ff3a802011-04-11 00:48:52 +02003057 damage = 1;
Johan Herland0133dab2011-04-11 00:48:51 +02003058 goto found_damage;
3059 }
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003060
3061 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003062 diff_populate_filespec(options->repo, p->one, NULL);
3063 diff_populate_filespec(options->repo, p->two, NULL);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003064 diffcore_count_changes(options->repo,
3065 p->one, p->two, NULL, NULL,
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003066 &copied, &added);
3067 diff_free_filespec_data(p->one);
3068 diff_free_filespec_data(p->two);
3069 } else if (DIFF_FILE_VALID(p->one)) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003070 diff_populate_filespec(options->repo, p->one, &dpf_options);
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003071 copied = added = 0;
3072 diff_free_filespec_data(p->one);
3073 } else if (DIFF_FILE_VALID(p->two)) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003074 diff_populate_filespec(options->repo, p->two, &dpf_options);
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003075 copied = 0;
3076 added = p->two->size;
3077 diff_free_filespec_data(p->two);
3078 } else
Junio C Hamano2b0b5512008-02-24 17:37:15 -08003079 continue;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003080
3081 /*
3082 * Original minus copied is the removed material,
3083 * added is the new material. They are both damages
Johan Herland0133dab2011-04-11 00:48:51 +02003084 * made to the preimage.
Johan Herland2ff3a802011-04-11 00:48:52 +02003085 * If the resulting damage is zero, we know that
3086 * diffcore_count_changes() considers the two entries to
Jeff Kingd9f62df2018-08-28 17:23:03 -04003087 * be identical, but since the oid changed, we
Johan Herland2ff3a802011-04-11 00:48:52 +02003088 * know that there must have been _some_ kind of change,
3089 * so we force all entries to have damage > 0.
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003090 */
3091 damage = (p->one->size - copied) + added;
Johan Herland2ff3a802011-04-11 00:48:52 +02003092 if (!damage)
Heikki Orsilafd337772008-09-05 22:27:35 +03003093 damage = 1;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003094
Johan Herland0133dab2011-04-11 00:48:51 +02003095found_damage:
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003096 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
3097 dir.files[dir.nr].name = name;
3098 dir.files[dir.nr].changed = damage;
3099 changed += damage;
3100 dir.nr++;
Linus Torvalds7df7c012008-02-12 13:26:31 -08003101 }
3102
3103 /* This can happen even with many files, if everything was renames */
3104 if (!changed)
3105 return;
3106
3107 /* Show all directories with more than x% of the changes */
René Scharfe9ed0d8d2016-09-29 17:27:31 +02003108 QSORT(dir.files, dir.nr, dirstat_compare);
Bo Yang7be57612010-05-26 15:23:54 +08003109 gather_dirstat(options, &dir, changed, "", 0);
Linus Torvalds7df7c012008-02-12 13:26:31 -08003110}
3111
Johan Herland1c57a622011-04-29 11:36:21 +02003112static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options)
3113{
3114 int i;
3115 unsigned long changed;
3116 struct dirstat_dir dir;
3117
3118 if (data->nr == 0)
3119 return;
3120
3121 dir.files = NULL;
3122 dir.alloc = 0;
3123 dir.nr = 0;
3124 dir.permille = options->dirstat_permille;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003125 dir.cumulative = options->flags.dirstat_cumulative;
Johan Herland1c57a622011-04-29 11:36:21 +02003126
3127 changed = 0;
3128 for (i = 0; i < data->nr; i++) {
3129 struct diffstat_file *file = data->files[i];
3130 unsigned long damage = file->added + file->deleted;
3131 if (file->is_binary)
3132 /*
3133 * binary files counts bytes, not lines. Must find some
3134 * way to normalize binary bytes vs. textual lines.
3135 * The following heuristic assumes that there are 64
3136 * bytes per "line".
3137 * This is stupid and ugly, but very cheap...
3138 */
René Scharfe42c78a22017-07-08 12:35:35 +02003139 damage = DIV_ROUND_UP(damage, 64);
Johan Herland1c57a622011-04-29 11:36:21 +02003140 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
3141 dir.files[dir.nr].name = file->name;
3142 dir.files[dir.nr].changed = damage;
3143 changed += damage;
3144 dir.nr++;
3145 }
3146
3147 /* This can happen even with many files, if everything was renames */
3148 if (!changed)
3149 return;
3150
3151 /* Show all directories with more than x% of the changes */
René Scharfe9ed0d8d2016-09-29 17:27:31 +02003152 QSORT(dir.files, dir.nr, dirstat_compare);
Johan Herland1c57a622011-04-29 11:36:21 +02003153 gather_dirstat(options, &dir, changed, "", 0);
3154}
3155
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003156static void free_diffstat_file(struct diffstat_file *f)
3157{
3158 free(f->print_name);
3159 free(f->name);
3160 free(f->from_name);
3161 free(f);
3162}
3163
Daniel Ferreirae4cb6592019-11-13 12:40:58 +00003164void free_diffstat_info(struct diffstat_t *diffstat)
Junio C Hamanof6046522007-12-11 23:46:30 -08003165{
3166 int i;
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003167 for (i = 0; i < diffstat->nr; i++)
3168 free_diffstat_file(diffstat->files[i]);
Junio C Hamanof6046522007-12-11 23:46:30 -08003169 free(diffstat->files);
3170}
3171
Johannes Schindelin88246892006-05-20 23:43:13 +02003172struct checkdiff_t {
Johannes Schindelin88246892006-05-20 23:43:13 +02003173 const char *filename;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003174 int lineno;
Junio C Hamanoa757c642010-03-24 19:21:32 -07003175 int conflict_marker_size;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003176 struct diff_options *o;
Junio C Hamanocf1b7862007-12-06 00:14:14 -08003177 unsigned ws_rule;
Wincent Colaiuta62c64892007-12-13 21:24:52 +01003178 unsigned status;
Johannes Schindelin88246892006-05-20 23:43:13 +02003179};
3180
Junio C Hamanoa757c642010-03-24 19:21:32 -07003181static int is_conflict_marker(const char *line, int marker_size, unsigned long len)
Junio C Hamano04954042008-06-26 15:37:21 -07003182{
3183 char firstchar;
3184 int cnt;
3185
Junio C Hamanoa757c642010-03-24 19:21:32 -07003186 if (len < marker_size + 1)
Junio C Hamano04954042008-06-26 15:37:21 -07003187 return 0;
3188 firstchar = line[0];
3189 switch (firstchar) {
Junio C Hamanoa757c642010-03-24 19:21:32 -07003190 case '=': case '>': case '<': case '|':
Junio C Hamano04954042008-06-26 15:37:21 -07003191 break;
3192 default:
3193 return 0;
3194 }
Junio C Hamanoa757c642010-03-24 19:21:32 -07003195 for (cnt = 1; cnt < marker_size; cnt++)
Junio C Hamano04954042008-06-26 15:37:21 -07003196 if (line[cnt] != firstchar)
3197 return 0;
Elijah Newren15beaaa2019-11-05 17:07:23 +00003198 /* line[1] through line[marker_size-1] are same as firstchar */
Junio C Hamanoa757c642010-03-24 19:21:32 -07003199 if (len < marker_size + 1 || !isspace(line[marker_size]))
Junio C Hamano04954042008-06-26 15:37:21 -07003200 return 0;
Junio C Hamano04954042008-06-26 15:37:21 -07003201 return 1;
3202}
3203
Jeff King75ab7632018-11-02 02:39:03 -04003204static void checkdiff_consume_hunk(void *priv,
3205 long ob, long on, long nb, long nn,
3206 const char *func, long funclen)
3207
3208{
3209 struct checkdiff_t *data = priv;
3210 data->lineno = nb - 1;
3211}
3212
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02003213static int checkdiff_consume(void *priv, char *line, unsigned long len)
Johannes Schindelin88246892006-05-20 23:43:13 +02003214{
3215 struct checkdiff_t *data = priv;
Junio C Hamanoa757c642010-03-24 19:21:32 -07003216 int marker_size = data->conflict_marker_size;
Jeff Kingf1c96262011-08-17 22:03:12 -07003217 const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE);
3218 const char *reset = diff_get_color(data->o->use_color, DIFF_RESET);
3219 const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +01003220 char *err;
John Keeping30997bb2013-02-07 20:15:27 +00003221 const char *line_prefix;
Bo Yang7be57612010-05-26 15:23:54 +08003222
3223 assert(data->o);
John Keeping30997bb2013-02-07 20:15:27 +00003224 line_prefix = diff_line_prefix(data->o);
Johannes Schindelin88246892006-05-20 23:43:13 +02003225
3226 if (line[0] == '+') {
Junio C Hamano18374e52008-06-26 13:16:33 -07003227 unsigned bad;
Junio C Hamano0ef617f2008-02-15 20:30:05 -08003228 data->lineno++;
Junio C Hamanoa757c642010-03-24 19:21:32 -07003229 if (is_conflict_marker(line + 1, marker_size, len - 1)) {
Junio C Hamano04954042008-06-26 15:37:21 -07003230 data->status |= 1;
3231 fprintf(data->o->file,
Bo Yang7be57612010-05-26 15:23:54 +08003232 "%s%s:%d: leftover conflict marker\n",
3233 line_prefix, data->filename, data->lineno);
Junio C Hamano04954042008-06-26 15:37:21 -07003234 }
Junio C Hamano8f8841e2008-06-26 15:35:21 -07003235 bad = ws_check(line + 1, len - 1, data->ws_rule);
Junio C Hamano18374e52008-06-26 13:16:33 -07003236 if (!bad)
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02003237 return 0;
Junio C Hamano18374e52008-06-26 13:16:33 -07003238 data->status |= bad;
3239 err = whitespace_error_string(bad);
Bo Yang7be57612010-05-26 15:23:54 +08003240 fprintf(data->o->file, "%s%s:%d: %s.\n",
3241 line_prefix, data->filename, data->lineno, err);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +01003242 free(err);
Bo Yanga3c158d2010-05-26 15:08:02 +08003243 emit_line(data->o, set, reset, line, 1);
Junio C Hamano8f8841e2008-06-26 15:35:21 -07003244 ws_check_emit(line + 1, len - 1, data->ws_rule,
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003245 data->o->file, set, reset, ws);
Junio C Hamano877f23c2008-06-26 15:36:59 -07003246 } else if (line[0] == ' ') {
Johannes Schindelin88246892006-05-20 23:43:13 +02003247 data->lineno++;
Johannes Schindelin88246892006-05-20 23:43:13 +02003248 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02003249 return 0;
Johannes Schindelin88246892006-05-20 23:43:13 +02003250}
3251
Junio C Hamano06606262006-05-05 02:41:53 -07003252static unsigned char *deflate_it(char *data,
3253 unsigned long size,
3254 unsigned long *result_size)
Junio C Hamano051308f2006-05-04 16:51:44 -07003255{
Junio C Hamano06606262006-05-05 02:41:53 -07003256 int bound;
3257 unsigned char *deflated;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07003258 git_zstream stream;
Junio C Hamano051308f2006-05-04 16:51:44 -07003259
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003260 git_deflate_init(&stream, zlib_compression_level);
Junio C Hamano225a6f12011-06-10 11:18:17 -07003261 bound = git_deflate_bound(&stream, size);
Junio C Hamano06606262006-05-05 02:41:53 -07003262 deflated = xmalloc(bound);
3263 stream.next_out = deflated;
3264 stream.avail_out = bound;
Junio C Hamano051308f2006-05-04 16:51:44 -07003265
Junio C Hamano06606262006-05-05 02:41:53 -07003266 stream.next_in = (unsigned char *)data;
3267 stream.avail_in = size;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003268 while (git_deflate(&stream, Z_FINISH) == Z_OK)
Junio C Hamano06606262006-05-05 02:41:53 -07003269 ; /* nothing */
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003270 git_deflate_end(&stream);
Junio C Hamano06606262006-05-05 02:41:53 -07003271 *result_size = stream.total_out;
3272 return deflated;
Junio C Hamano051308f2006-05-04 16:51:44 -07003273}
3274
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003275static void emit_binary_diff_body(struct diff_options *o,
3276 mmfile_t *one, mmfile_t *two)
Junio C Hamano051308f2006-05-04 16:51:44 -07003277{
Junio C Hamano06606262006-05-05 02:41:53 -07003278 void *cp;
3279 void *delta;
3280 void *deflated;
3281 void *data;
3282 unsigned long orig_size;
Junio C Hamano051308f2006-05-04 16:51:44 -07003283 unsigned long delta_size;
Junio C Hamano06606262006-05-05 02:41:53 -07003284 unsigned long deflate_size;
3285 unsigned long data_size;
Junio C Hamano051308f2006-05-04 16:51:44 -07003286
Junio C Hamano06606262006-05-05 02:41:53 -07003287 /* We could do deflated delta, or we could do just deflated two,
3288 * whichever is smaller.
3289 */
3290 delta = NULL;
3291 deflated = deflate_it(two->ptr, two->size, &deflate_size);
3292 if (one->size && two->size) {
3293 delta = diff_delta(one->ptr, one->size,
3294 two->ptr, two->size,
3295 &delta_size, deflate_size);
3296 if (delta) {
3297 void *to_free = delta;
3298 orig_size = delta_size;
3299 delta = deflate_it(delta, delta_size, &delta_size);
3300 free(to_free);
3301 }
3302 }
Junio C Hamano051308f2006-05-04 16:51:44 -07003303
Junio C Hamano06606262006-05-05 02:41:53 -07003304 if (delta && delta_size < deflate_size) {
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01003305 char *s = xstrfmt("%"PRIuMAX , (uintmax_t)orig_size);
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003306 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
3307 s, strlen(s), 0);
3308 free(s);
Junio C Hamano06606262006-05-05 02:41:53 -07003309 free(deflated);
3310 data = delta;
3311 data_size = delta_size;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003312 } else {
3313 char *s = xstrfmt("%lu", two->size);
3314 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
3315 s, strlen(s), 0);
3316 free(s);
Junio C Hamano06606262006-05-05 02:41:53 -07003317 free(delta);
3318 data = deflated;
3319 data_size = deflate_size;
3320 }
3321
3322 /* emit data encoded in base85 */
3323 cp = data;
3324 while (data_size) {
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003325 int len;
Junio C Hamano06606262006-05-05 02:41:53 -07003326 int bytes = (52 < data_size) ? 52 : data_size;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003327 char line[71];
Junio C Hamano06606262006-05-05 02:41:53 -07003328 data_size -= bytes;
Junio C Hamano051308f2006-05-04 16:51:44 -07003329 if (bytes <= 26)
3330 line[0] = bytes + 'A' - 1;
3331 else
3332 line[0] = bytes - 26 + 'a' - 1;
3333 encode_85(line + 1, cp, bytes);
Florian Forster1d7f1712006-06-18 17:18:09 +02003334 cp = (char *) cp + bytes;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003335
3336 len = strlen(line);
3337 line[len++] = '\n';
3338 line[len] = '\0';
3339
3340 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY,
3341 line, len, 0);
Junio C Hamano051308f2006-05-04 16:51:44 -07003342 }
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003343 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0);
Junio C Hamano06606262006-05-05 02:41:53 -07003344 free(data);
Junio C Hamano051308f2006-05-04 16:51:44 -07003345}
3346
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003347static void emit_binary_diff(struct diff_options *o,
3348 mmfile_t *one, mmfile_t *two)
Junio C Hamanod4c452f2006-08-16 16:08:14 -07003349{
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003350 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0);
3351 emit_binary_diff_body(o, one, two);
3352 emit_binary_diff_body(o, two, one);
Junio C Hamanod4c452f2006-08-16 16:08:14 -07003353}
3354
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003355int diff_filespec_is_binary(struct repository *r,
3356 struct diff_filespec *one)
Junio C Hamano29a3eef2007-07-06 00:18:54 -07003357{
Jonathan Tan1c37e862020-04-07 15:11:41 -07003358 struct diff_populate_filespec_options dpf_options = {
3359 .check_binary = 1,
3360 };
3361
Jeff King122aa6f2008-10-05 17:43:36 -04003362 if (one->is_binary == -1) {
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003363 diff_filespec_load_driver(one, r->index);
Jeff King122aa6f2008-10-05 17:43:36 -04003364 if (one->driver->binary != -1)
3365 one->is_binary = one->driver->binary;
3366 else {
3367 if (!one->data && DIFF_FILE_VALID(one))
Jonathan Tan1c37e862020-04-07 15:11:41 -07003368 diff_populate_filespec(r, one, &dpf_options);
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003369 if (one->is_binary == -1 && one->data)
Jeff King122aa6f2008-10-05 17:43:36 -04003370 one->is_binary = buffer_is_binary(one->data,
3371 one->size);
3372 if (one->is_binary == -1)
3373 one->is_binary = 0;
3374 }
3375 }
Junio C Hamano29a3eef2007-07-06 00:18:54 -07003376 return one->is_binary;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003377}
3378
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003379static const struct userdiff_funcname *
3380diff_funcname_pattern(struct diff_options *o, struct diff_filespec *one)
Junio C Hamanof2584752007-07-06 00:45:10 -07003381{
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003382 diff_filespec_load_driver(one, o->repo->index);
Jeff King122aa6f2008-10-05 17:43:36 -04003383 return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
Junio C Hamanof2584752007-07-06 00:45:10 -07003384}
3385
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003386void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
3387{
3388 if (!options->a_prefix)
3389 options->a_prefix = a;
3390 if (!options->b_prefix)
3391 options->b_prefix = b;
3392}
3393
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01003394struct userdiff_driver *get_textconv(struct repository *r,
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003395 struct diff_filespec *one)
Jeff King04427ac2008-10-26 00:44:53 -04003396{
3397 if (!DIFF_FILE_VALID(one))
3398 return NULL;
Jeff Kingd391c0f2010-09-21 17:01:24 -04003399
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01003400 diff_filespec_load_driver(one, r->index);
3401 return userdiff_get_textconv(r, one->driver);
Jeff King04427ac2008-10-26 00:44:53 -04003402}
3403
Junio C Hamano6973dca2006-04-21 23:57:45 -07003404static void builtin_diff(const char *name_a,
3405 const char *name_b,
3406 struct diff_filespec *one,
3407 struct diff_filespec *two,
3408 const char *xfrm_msg,
Christian Couder296c6bb2010-05-26 04:50:12 +02003409 int must_show_header,
Junio C Hamano051308f2006-05-04 16:51:44 -07003410 struct diff_options *o,
Junio C Hamano6973dca2006-04-21 23:57:45 -07003411 int complete_rewrite)
3412{
3413 mmfile_t mf1, mf2;
3414 const char *lbl[2];
3415 char *a_one, *b_two;
Jeff Kingd9c552f2012-11-13 21:12:46 +05303416 const char *meta = diff_get_color_opt(o, DIFF_METAINFO);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01003417 const char *reset = diff_get_color_opt(o, DIFF_RESET);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003418 const char *a_prefix, *b_prefix;
Jeff Kingd9bae1a2010-04-01 20:12:15 -04003419 struct userdiff_driver *textconv_one = NULL;
3420 struct userdiff_driver *textconv_two = NULL;
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003421 struct strbuf header = STRBUF_INIT;
John Keeping30997bb2013-02-07 20:15:27 +00003422 const char *line_prefix = diff_line_prefix(o);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003423
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003424 diff_set_mnemonic_prefix(o, "a/", "b/");
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003425 if (o->flags.reverse_diff) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003426 a_prefix = o->b_prefix;
3427 b_prefix = o->a_prefix;
3428 } else {
3429 a_prefix = o->a_prefix;
3430 b_prefix = o->b_prefix;
3431 }
3432
Jacob Keller61cfbc02016-08-31 16:27:21 -07003433 if (o->submodule_format == DIFF_SUBMODULE_LOG &&
3434 (!one->mode || S_ISGITLINK(one->mode)) &&
3435 (!two->mode || S_ISGITLINK(two->mode))) {
Shourya Shukla180b1542020-08-13 01:14:02 +05303436 show_submodule_diff_summary(o, one->path ? one->path : two->path,
Jacob Keller602a2832016-08-31 16:27:23 -07003437 &one->oid, &two->oid,
Stefan Bellerf3597132017-06-29 17:07:00 -07003438 two->dirty_submodule);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003439 return;
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003440 } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF &&
3441 (!one->mode || S_ISGITLINK(one->mode)) &&
3442 (!two->mode || S_ISGITLINK(two->mode))) {
Stefan Bellerf3597132017-06-29 17:07:00 -07003443 show_submodule_inline_diff(o, one->path ? one->path : two->path,
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003444 &one->oid, &two->oid,
Stefan Bellerf3597132017-06-29 17:07:00 -07003445 two->dirty_submodule);
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003446 return;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003447 }
3448
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003449 if (o->flags.allow_textconv) {
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01003450 textconv_one = get_textconv(o->repo, one);
3451 textconv_two = get_textconv(o->repo, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003452 }
3453
Linus Torvalds71b989e2008-10-05 15:35:15 -04003454 /* Never use a non-valid filename anywhere if at all possible */
3455 name_a = DIFF_FILE_VALID(one) ? name_a : name_b;
3456 name_b = DIFF_FILE_VALID(two) ? name_b : name_a;
3457
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003458 a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
3459 b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
Junio C Hamano6973dca2006-04-21 23:57:45 -07003460 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
3461 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
Jeff Kingd9c552f2012-11-13 21:12:46 +05303462 strbuf_addf(&header, "%s%sdiff --git %s %s%s\n", line_prefix, meta, a_one, b_two, reset);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003463 if (lbl[0][0] == '/') {
3464 /* /dev/null */
Jeff Kingd9c552f2012-11-13 21:12:46 +05303465 strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset);
Bert Wesarg37466442010-05-04 00:38:07 +02003466 if (xfrm_msg)
3467 strbuf_addstr(&header, xfrm_msg);
Christian Couder296c6bb2010-05-26 04:50:12 +02003468 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003469 }
3470 else if (lbl[1][0] == '/') {
Jeff Kingd9c552f2012-11-13 21:12:46 +05303471 strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset);
Bert Wesarg37466442010-05-04 00:38:07 +02003472 if (xfrm_msg)
3473 strbuf_addstr(&header, xfrm_msg);
Christian Couder296c6bb2010-05-26 04:50:12 +02003474 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003475 }
3476 else {
3477 if (one->mode != two->mode) {
Jeff Kingd9c552f2012-11-13 21:12:46 +05303478 strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset);
3479 strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset);
Christian Couder296c6bb2010-05-26 04:50:12 +02003480 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003481 }
Bert Wesarg37466442010-05-04 00:38:07 +02003482 if (xfrm_msg)
3483 strbuf_addstr(&header, xfrm_msg);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003484
Junio C Hamano6973dca2006-04-21 23:57:45 -07003485 /*
3486 * we do not run diff between different kind
3487 * of objects.
3488 */
3489 if ((one->mode ^ two->mode) & S_IFMT)
3490 goto free_ab_and_return;
Jeff King0c018572008-12-09 03:12:28 -05003491 if (complete_rewrite &&
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003492 (textconv_one || !diff_filespec_is_binary(o->repo, one)) &&
3493 (textconv_two || !diff_filespec_is_binary(o->repo, two))) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003494 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3495 header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003496 strbuf_reset(&header);
Jeff King3aa1f7c2008-12-09 03:13:21 -05003497 emit_rewrite_diff(name_a, name_b, one, two,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003498 textconv_one, textconv_two, o);
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01003499 o->found_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003500 goto free_ab_and_return;
3501 }
3502 }
3503
Junio C Hamano467ddc12011-02-28 16:11:55 -08003504 if (o->irreversible_delete && lbl[1][0] == '/') {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003505 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf,
3506 header.len, 0);
Junio C Hamano467ddc12011-02-28 16:11:55 -08003507 strbuf_reset(&header);
3508 goto free_ab_and_return;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003509 } else if (!o->flags.text &&
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003510 ( (!textconv_one && diff_filespec_is_binary(o->repo, one)) ||
3511 (!textconv_two && diff_filespec_is_binary(o->repo, two)) )) {
Stefan Beller4acaaa72017-06-29 17:06:58 -07003512 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003513 if (!one->data && !two->data &&
3514 S_ISREG(one->mode) && S_ISREG(two->mode) &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003515 !o->flags.binary) {
Jeff King4a7e27e2018-08-28 17:22:40 -04003516 if (oideq(&one->oid, &two->oid)) {
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003517 if (must_show_header)
Stefan Bellera29b0a12017-06-29 17:06:57 -07003518 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3519 header.buf, header.len,
3520 0);
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003521 goto free_ab_and_return;
3522 }
Stefan Bellera29b0a12017-06-29 17:06:57 -07003523 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3524 header.buf, header.len, 0);
Stefan Beller4acaaa72017-06-29 17:06:58 -07003525 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3526 diff_line_prefix(o), lbl[0], lbl[1]);
3527 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3528 sb.buf, sb.len, 0);
3529 strbuf_release(&sb);
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003530 goto free_ab_and_return;
3531 }
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003532 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3533 fill_mmfile(o->repo, &mf2, two) < 0)
Jeff Kingb3373982010-04-01 20:14:24 -04003534 die("unable to read files to diff");
Junio C Hamano06606262006-05-05 02:41:53 -07003535 /* Quite common confusing case */
3536 if (mf1.size == mf2.size &&
Christian Couder296c6bb2010-05-26 04:50:12 +02003537 !memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
3538 if (must_show_header)
Stefan Bellera29b0a12017-06-29 17:06:57 -07003539 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3540 header.buf, header.len, 0);
Junio C Hamano06606262006-05-05 02:41:53 -07003541 goto free_ab_and_return;
Christian Couder296c6bb2010-05-26 04:50:12 +02003542 }
Stefan Bellera29b0a12017-06-29 17:06:57 -07003543 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003544 strbuf_reset(&header);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003545 if (o->flags.binary)
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003546 emit_binary_diff(o, &mf1, &mf2);
Stefan Beller4acaaa72017-06-29 17:06:58 -07003547 else {
3548 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3549 diff_line_prefix(o), lbl[0], lbl[1]);
3550 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3551 sb.buf, sb.len, 0);
3552 strbuf_release(&sb);
3553 }
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01003554 o->found_changes = 1;
Junio C Hamano467ddc12011-02-28 16:11:55 -08003555 } else {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003556 /* Crazy xdl interfaces.. */
Jeff King0da0e922019-01-11 17:17:22 -05003557 const char *diffopts;
Jeff Kingae021d82014-06-18 15:47:50 -04003558 const char *v;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003559 xpparam_t xpp;
3560 xdemitconf_t xecfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003561 struct emit_callback ecbdata;
Jeff Kingbe58e702008-10-05 17:43:21 -04003562 const struct userdiff_funcname *pe;
Junio C Hamanof2584752007-07-06 00:45:10 -07003563
Junio C Hamanob3f01ff2012-02-29 18:14:16 -08003564 if (must_show_header) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003565 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3566 header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003567 strbuf_reset(&header);
3568 }
3569
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02003570 mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr);
3571 mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr);
Jeff King04427ac2008-10-26 00:44:53 -04003572
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003573 pe = diff_funcname_pattern(o, one);
Brandon Casey45e7ca02008-09-18 17:40:48 -05003574 if (!pe)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003575 pe = diff_funcname_pattern(o, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003576
Brian Downing9ccd0a82008-10-25 15:30:37 +02003577 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003578 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelincd112ce2006-06-13 18:45:44 +02003579 memset(&ecbdata, 0, sizeof(ecbdata));
Johannes Schindelin1cdde292018-08-13 04:33:11 -07003580 if (o->flags.suppress_diff_headers)
3581 lbl[0] = NULL;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003582 ecbdata.label_path = lbl;
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07003583 ecbdata.color_diff = want_color(o->use_color);
Nguyễn Thái Ngọc Duy26d024e2018-09-21 17:57:37 +02003584 ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
Junio C Hamano690ed842009-09-04 00:41:15 -07003585 if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
Junio C Hamanod68fe262009-09-14 22:05:57 -07003586 check_blank_at_eof(&mf1, &mf2, &ecbdata);
Bo Yanga3c158d2010-05-26 15:08:02 +08003587 ecbdata.opt = o;
Johannes Schindelin1cdde292018-08-13 04:33:11 -07003588 if (header.len && !o->flags.suppress_diff_headers)
3589 ecbdata.header = &header;
René Scharfe582aa002010-05-02 15:04:41 +02003590 xpp.flags = o->xdl_opts;
Michał Kępień296d4a92020-10-20 08:48:09 +02003591 xpp.ignore_regex = o->ignore_regex;
3592 xpp.ignore_regex_nr = o->ignore_regex_nr;
Jonathan Tan2477ab22017-11-27 11:47:47 -08003593 xpp.anchors = o->anchors;
3594 xpp.anchors_nr = o->anchors_nr;
Linus Torvaldsee1e5412006-05-13 13:23:48 -07003595 xecfg.ctxlen = o->context;
René Scharfe6d0e6742008-12-28 19:45:32 +01003596 xecfg.interhunkctxlen = o->interhunkcontext;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003597 xecfg.flags = XDL_EMIT_FUNCNAMES;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003598 if (o->flags.funccontext)
René Scharfe14937c22011-10-09 13:36:57 +02003599 xecfg.flags |= XDL_EMIT_FUNCCONTEXT;
Brandon Casey45e7ca02008-09-18 17:40:48 -05003600 if (pe)
Brandon Caseya0135852008-09-18 17:42:48 -05003601 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
Jeff King0da0e922019-01-11 17:17:22 -05003602
3603 diffopts = getenv("GIT_DIFF_OPTS");
Junio C Hamano6973dca2006-04-21 23:57:45 -07003604 if (!diffopts)
3605 ;
Jeff Kingae021d82014-06-18 15:47:50 -04003606 else if (skip_prefix(diffopts, "--unified=", &v))
3607 xecfg.ctxlen = strtoul(v, NULL, 10);
3608 else if (skip_prefix(diffopts, "-u", &v))
3609 xecfg.ctxlen = strtoul(v, NULL, 10);
Jeff King0da0e922019-01-11 17:17:22 -05003610
Thomas Rast77d1a522012-03-14 19:24:08 +01003611 if (o->word_diff)
3612 init_diff_words_data(&ecbdata, o, one, two);
Jeff King9346d6d2018-11-02 02:35:45 -04003613 if (xdi_diff_outf(&mf1, &mf2, NULL, fn_out_consume,
3614 &ecbdata, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04003615 die("unable to generate diff for %s", one->path);
Thomas Rast882749a2010-04-14 17:59:06 +02003616 if (o->word_diff)
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02003617 free_diff_words_data(&ecbdata);
Jeff King04427ac2008-10-26 00:44:53 -04003618 if (textconv_one)
3619 free(mf1.ptr);
3620 if (textconv_two)
3621 free(mf2.ptr);
René Scharfe8cfe5f12009-07-02 00:01:43 +02003622 xdiff_clear_find_func(&xecfg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003623 }
3624
3625 free_ab_and_return:
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003626 strbuf_release(&header);
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003627 diff_free_filespec_data(one);
3628 diff_free_filespec_data(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003629 free(a_one);
3630 free(b_two);
3631 return;
3632}
3633
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 21:09:59 +07003634static char *get_compact_summary(const struct diff_filepair *p, int is_renamed)
3635{
3636 if (!is_renamed) {
3637 if (p->status == DIFF_STATUS_ADDED) {
3638 if (S_ISLNK(p->two->mode))
3639 return "new +l";
3640 else if ((p->two->mode & 0777) == 0755)
3641 return "new +x";
3642 else
3643 return "new";
3644 } else if (p->status == DIFF_STATUS_DELETED)
3645 return "gone";
3646 }
3647 if (S_ISLNK(p->one->mode) && !S_ISLNK(p->two->mode))
3648 return "mode -l";
3649 else if (!S_ISLNK(p->one->mode) && S_ISLNK(p->two->mode))
3650 return "mode +l";
3651 else if ((p->one->mode & 0777) == 0644 &&
3652 (p->two->mode & 0777) == 0755)
3653 return "mode +x";
3654 else if ((p->one->mode & 0777) == 0755 &&
3655 (p->two->mode & 0777) == 0644)
3656 return "mode -x";
3657 return NULL;
3658}
3659
Junio C Hamano6973dca2006-04-21 23:57:45 -07003660static void builtin_diffstat(const char *name_a, const char *name_b,
3661 struct diff_filespec *one,
3662 struct diff_filespec *two,
Junio C Hamano710158e2006-04-25 23:40:09 -07003663 struct diffstat_t *diffstat,
Johannes Schindelin0d21efa2006-06-14 17:40:23 +02003664 struct diff_options *o,
Linus Torvalds74faaa12012-10-17 10:00:37 -07003665 struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003666{
3667 mmfile_t mf1, mf2;
3668 struct diffstat_file *data;
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003669 int may_differ;
Linus Torvalds74faaa12012-10-17 10:00:37 -07003670 int complete_rewrite = 0;
3671
3672 if (!DIFF_PAIR_UNMERGED(p)) {
3673 if (p->status == DIFF_STATUS_MODIFIED && p->score)
3674 complete_rewrite = 1;
3675 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003676
3677 data = diffstat_add(diffstat, name_a, name_b);
Junio C Hamano99bfd402012-11-27 11:17:14 -08003678 data->is_interesting = p->status != DIFF_STATUS_UNKNOWN;
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 21:09:59 +07003679 if (o->flags.stat_with_summary)
3680 data->comments = get_compact_summary(p, data->is_renamed);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003681
3682 if (!one || !two) {
3683 data->is_unmerged = 1;
3684 return;
3685 }
Jeff Kingded0abc2011-02-19 03:04:56 -05003686
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003687 /* saves some reads if true, not a guarantee of diff outcome */
3688 may_differ = !(one->oid_valid && two->oid_valid &&
3689 oideq(&one->oid, &two->oid));
Zbigniew Jędrzejewski-Szmek352ca4e2012-05-01 19:10:15 +02003690
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003691 if (diff_filespec_is_binary(o->repo, one) ||
3692 diff_filespec_is_binary(o->repo, two)) {
Jeff Kingded0abc2011-02-19 03:04:56 -05003693 data->is_binary = 1;
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003694 if (!may_differ) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02003695 data->added = 0;
3696 data->deleted = 0;
3697 } else {
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003698 data->added = diff_filespec_size(o->repo, two);
3699 data->deleted = diff_filespec_size(o->repo, one);
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02003700 }
Jeff Kingded0abc2011-02-19 03:04:56 -05003701 }
3702
3703 else if (complete_rewrite) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003704 diff_populate_filespec(o->repo, one, NULL);
3705 diff_populate_filespec(o->repo, two, NULL);
Junio C Hamano710158e2006-04-25 23:40:09 -07003706 data->deleted = count_lines(one->data, one->size);
3707 data->added = count_lines(two->data, two->size);
Junio C Hamano710158e2006-04-25 23:40:09 -07003708 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003709
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003710 else if (may_differ) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003711 /* Crazy xdl interfaces.. */
3712 xpparam_t xpp;
3713 xdemitconf_t xecfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003714
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003715 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3716 fill_mmfile(o->repo, &mf2, two) < 0)
Jeff Kingded0abc2011-02-19 03:04:56 -05003717 die("unable to read files to diff");
3718
Brian Downing9ccd0a82008-10-25 15:30:37 +02003719 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003720 memset(&xecfg, 0, sizeof(xecfg));
René Scharfe582aa002010-05-02 15:04:41 +02003721 xpp.flags = o->xdl_opts;
Michał Kępień296d4a92020-10-20 08:48:09 +02003722 xpp.ignore_regex = o->ignore_regex;
3723 xpp.ignore_regex_nr = o->ignore_regex_nr;
Jonathan Tan2477ab22017-11-27 11:47:47 -08003724 xpp.anchors = o->anchors;
3725 xpp.anchors_nr = o->anchors_nr;
Junio C Hamanof01cae92011-09-22 10:54:47 -07003726 xecfg.ctxlen = o->context;
3727 xecfg.interhunkctxlen = o->interhunkcontext;
Ævar Arnfjörð Bjarmason5d934602021-04-12 19:15:29 +02003728 xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
3729 if (xdi_diff_outf(&mf1, &mf2, NULL,
Jeff King3b40a092018-11-02 02:36:06 -04003730 diffstat_consume, diffstat, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04003731 die("unable to generate diffstat for %s", one->path);
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003732
3733 if (DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two)) {
3734 struct diffstat_file *file =
3735 diffstat->files[diffstat->nr - 1];
3736 /*
3737 * Omit diffstats of modified files where nothing changed.
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003738 * Even if may_differ, this might be the case due to
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003739 * ignoring whitespace changes, etc.
3740 *
3741 * But note that we special-case additions, deletions,
3742 * renames, and mode changes as adding an empty file,
3743 * for example is still of interest.
3744 */
3745 if ((p->status == DIFF_STATUS_MODIFIED)
3746 && !file->added
3747 && !file->deleted
3748 && one->mode == two->mode) {
3749 free_diffstat_file(file);
3750 diffstat->nr--;
3751 }
3752 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003753 }
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003754
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003755 diff_free_filespec_data(one);
3756 diff_free_filespec_data(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003757}
3758
Johannes Schindelin88246892006-05-20 23:43:13 +02003759static void builtin_checkdiff(const char *name_a, const char *name_b,
Junio C Hamanocd676a52008-02-12 14:26:02 -08003760 const char *attr_path,
Junio C Hamano5ff10dd2008-06-26 15:34:54 -07003761 struct diff_filespec *one,
3762 struct diff_filespec *two,
3763 struct diff_options *o)
Johannes Schindelin88246892006-05-20 23:43:13 +02003764{
3765 mmfile_t mf1, mf2;
3766 struct checkdiff_t data;
3767
3768 if (!two)
3769 return;
3770
3771 memset(&data, 0, sizeof(data));
Johannes Schindelin88246892006-05-20 23:43:13 +02003772 data.filename = name_b ? name_b : name_a;
3773 data.lineno = 0;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003774 data.o = o;
Nguyễn Thái Ngọc Duy26d024e2018-09-21 17:57:37 +02003775 data.ws_rule = whitespace_rule(o->repo->index, attr_path);
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +02003776 data.conflict_marker_size = ll_merge_marker_size(o->repo->index, attr_path);
Johannes Schindelin88246892006-05-20 23:43:13 +02003777
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003778 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3779 fill_mmfile(o->repo, &mf2, two) < 0)
Johannes Schindelin88246892006-05-20 23:43:13 +02003780 die("unable to read files to diff");
3781
Junio C Hamano5ff10dd2008-06-26 15:34:54 -07003782 /*
3783 * All the other codepaths check both sides, but not checking
3784 * the "old" side here is deliberate. We are checking the newly
3785 * introduced changes, and as long as the "new" side is text, we
3786 * can and should check what it introduces.
3787 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003788 if (diff_filespec_is_binary(o->repo, two))
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003789 goto free_and_return;
Johannes Schindelin88246892006-05-20 23:43:13 +02003790 else {
3791 /* Crazy xdl interfaces.. */
3792 xpparam_t xpp;
3793 xdemitconf_t xecfg;
Johannes Schindelin88246892006-05-20 23:43:13 +02003794
Brian Downing9ccd0a82008-10-25 15:30:37 +02003795 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003796 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanoc35539e2008-08-20 11:47:55 -07003797 xecfg.ctxlen = 1; /* at least one context line */
René Scharfe582aa002010-05-02 15:04:41 +02003798 xpp.flags = 0;
Jeff King75ab7632018-11-02 02:39:03 -04003799 if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume_hunk,
3800 checkdiff_consume, &data,
Jeff King3efb9882015-09-24 19:12:23 -04003801 &xpp, &xecfg))
3802 die("unable to generate checkdiff for %s", one->path);
Junio C Hamano877f23c2008-06-26 15:36:59 -07003803
Junio C Hamano467babf2009-09-03 23:39:43 -07003804 if (data.ws_rule & WS_BLANK_AT_EOF) {
Junio C Hamanod68fe262009-09-14 22:05:57 -07003805 struct emit_callback ecbdata;
3806 int blank_at_eof;
3807
3808 ecbdata.ws_rule = data.ws_rule;
3809 check_blank_at_eof(&mf1, &mf2, &ecbdata);
Christoph Mallon8837d332010-10-10 19:24:06 +02003810 blank_at_eof = ecbdata.blank_at_eof_in_postimage;
Junio C Hamanod68fe262009-09-14 22:05:57 -07003811
Junio C Hamano467babf2009-09-03 23:39:43 -07003812 if (blank_at_eof) {
3813 static char *err;
3814 if (!err)
3815 err = whitespace_error_string(WS_BLANK_AT_EOF);
3816 fprintf(o->file, "%s:%d: %s.\n",
3817 data.filename, blank_at_eof, err);
3818 data.status = 1; /* report errors */
3819 }
Junio C Hamano877f23c2008-06-26 15:36:59 -07003820 }
Johannes Schindelin88246892006-05-20 23:43:13 +02003821 }
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003822 free_and_return:
3823 diff_free_filespec_data(one);
3824 diff_free_filespec_data(two);
Wincent Colaiuta62c64892007-12-13 21:24:52 +01003825 if (data.status)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003826 o->flags.check_failed = 1;
Johannes Schindelin88246892006-05-20 23:43:13 +02003827}
3828
Junio C Hamano6973dca2006-04-21 23:57:45 -07003829struct diff_filespec *alloc_filespec(const char *path)
3830{
Jeff King96ffc062016-02-22 17:44:32 -05003831 struct diff_filespec *spec;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003832
Jeff King96ffc062016-02-22 17:44:32 -05003833 FLEXPTR_ALLOC_STR(spec, path, path);
Linus Torvalds9fb88412007-10-25 11:19:10 -07003834 spec->count = 1;
Jeff King122aa6f2008-10-05 17:43:36 -04003835 spec->is_binary = -1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003836 return spec;
3837}
3838
Linus Torvalds9fb88412007-10-25 11:19:10 -07003839void free_filespec(struct diff_filespec *spec)
3840{
3841 if (!--spec->count) {
3842 diff_free_filespec_data(spec);
3843 free(spec);
3844 }
3845}
3846
Brandon Williamsf9704c22017-05-30 10:30:50 -07003847void fill_filespec(struct diff_filespec *spec, const struct object_id *oid,
3848 int oid_valid, unsigned short mode)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003849{
3850 if (mode) {
3851 spec->mode = canon_mode(mode);
Brandon Williamsf9704c22017-05-30 10:30:50 -07003852 oidcpy(&spec->oid, oid);
3853 spec->oid_valid = oid_valid;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003854 }
3855}
3856
3857/*
Jakub Narebski5adf3172007-05-26 00:37:40 +02003858 * Given a name and sha1 pair, if the index tells us the file in
Junio C Hamano6973dca2006-04-21 23:57:45 -07003859 * the work tree has that object contents, return true, so that
3860 * prepare_temp_file() does not have to inflate and extract.
3861 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003862static int reuse_worktree_file(struct index_state *istate,
3863 const char *name,
3864 const struct object_id *oid,
3865 int want_file)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003866{
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07003867 const struct cache_entry *ce;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003868 struct stat st;
3869 int pos, len;
3870
Junio C Hamano150115a2009-03-22 15:26:07 -07003871 /*
3872 * We do not read the cache ourselves here, because the
Junio C Hamano6973dca2006-04-21 23:57:45 -07003873 * benchmark with my previous version that always reads cache
3874 * shows that it makes things worse for diff-tree comparing
3875 * two linux-2.6 kernel trees in an already checked out work
3876 * tree. This is because most diff-tree comparisons deal with
3877 * only a small number of files, while reading the cache is
3878 * expensive for a large project, and its cost outweighs the
3879 * savings we get by not inflating the object to a temporary
3880 * file. Practically, this code only helps when we are used
3881 * by diff-cache --cached, which does read the cache before
3882 * calling us.
3883 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003884 if (!istate->cache)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003885 return 0;
3886
Shawn O. Pearce1510fea2006-12-14 06:15:57 -05003887 /* We want to avoid the working directory if our caller
3888 * doesn't need the data in a normal file, this system
3889 * is rather slow with its stat/open/mmap/close syscalls,
3890 * and the object is contained in a pack file. The pack
3891 * is probably already open and will be faster to obtain
3892 * the data through than the working directory. Loose
3893 * objects however would tend to be slower as they need
3894 * to be individually opened and inflated.
3895 */
brian m. carlson14c3c802018-05-02 00:25:33 +00003896 if (!FAST_WORKING_DIRECTORY && !want_file && has_object_pack(oid))
Shawn O. Pearce1510fea2006-12-14 06:15:57 -05003897 return 0;
3898
Jeff King06dec432016-07-22 11:27:53 -04003899 /*
3900 * Similarly, if we'd have to convert the file contents anyway, that
3901 * makes the optimization not worthwhile.
3902 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003903 if (!want_file && would_convert_to_git(istate, name))
Jeff King06dec432016-07-22 11:27:53 -04003904 return 0;
3905
Junio C Hamano6973dca2006-04-21 23:57:45 -07003906 len = strlen(name);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003907 pos = index_name_pos(istate, name, len);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003908 if (pos < 0)
3909 return 0;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003910 ce = istate->cache[pos];
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003911
3912 /*
3913 * This is not the sha1 we are looking for, or
3914 * unreusable because it is not a regular file.
Junio C Hamano6973dca2006-04-21 23:57:45 -07003915 */
Jeff King9001dc22018-08-28 17:22:48 -04003916 if (!oideq(oid, &ce->oid) || !S_ISREG(ce->ce_mode))
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003917 return 0;
3918
3919 /*
Junio C Hamano150115a2009-03-22 15:26:07 -07003920 * If ce is marked as "assume unchanged", there is no
3921 * guarantee that work tree matches what we are looking for.
3922 */
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +07003923 if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
Junio C Hamano150115a2009-03-22 15:26:07 -07003924 return 0;
3925
3926 /*
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003927 * If ce matches the file in the work tree, we can reuse it.
3928 */
3929 if (ce_uptodate(ce) ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003930 (!lstat(name, &st) && !ie_match_stat(istate, ce, &st, 0)))
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003931 return 1;
3932
3933 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003934}
3935
Linus Torvalds04786752007-04-15 11:14:28 -07003936static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
3937{
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003938 struct strbuf buf = STRBUF_INIT;
3939 char *dirty = "";
Junio C Hamano8e08b412010-01-16 18:42:53 +01003940
3941 /* Are we looking at the work tree? */
Jens Lehmann85adbf22010-03-12 22:23:52 +01003942 if (s->dirty_submodule)
Junio C Hamano8e08b412010-01-16 18:42:53 +01003943 dirty = "-dirty";
3944
brian m. carlsona0d12c42016-06-24 23:09:23 +00003945 strbuf_addf(&buf, "Subproject commit %s%s\n",
3946 oid_to_hex(&s->oid), dirty);
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003947 s->size = buf.len;
Linus Torvalds04786752007-04-15 11:14:28 -07003948 if (size_only) {
3949 s->data = NULL;
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003950 strbuf_release(&buf);
3951 } else {
3952 s->data = strbuf_detach(&buf, NULL);
3953 s->should_free = 1;
Linus Torvalds04786752007-04-15 11:14:28 -07003954 }
3955 return 0;
3956}
3957
Junio C Hamano6973dca2006-04-21 23:57:45 -07003958/*
3959 * While doing rename detection and pickaxe operation, we may need to
3960 * grab the data for the blob (or file) for our own in-core comparison.
3961 * diff_filespec has data and size fields for this purpose.
3962 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003963int diff_populate_filespec(struct repository *r,
3964 struct diff_filespec *s,
Jonathan Tan1c37e862020-04-07 15:11:41 -07003965 const struct diff_populate_filespec_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003966{
Jonathan Tan1c37e862020-04-07 15:11:41 -07003967 int size_only = options ? options->check_size_only : 0;
3968 int check_binary = options ? options->check_binary : 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003969 int err = 0;
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01003970 int conv_flags = global_conv_flags_eol;
Junio C Hamano5430bb22013-06-24 14:35:04 -07003971 /*
3972 * demote FAIL to WARN to allow inspecting the situation
3973 * instead of refusing.
3974 */
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01003975 if (conv_flags & CONV_EOL_RNDTRP_DIE)
3976 conv_flags = CONV_EOL_RNDTRP_WARN;
Junio C Hamano5430bb22013-06-24 14:35:04 -07003977
Junio C Hamano6973dca2006-04-21 23:57:45 -07003978 if (!DIFF_FILE_VALID(s))
3979 die("internal error: asking to populate invalid file.");
3980 if (S_ISDIR(s->mode))
3981 return -1;
3982
Junio C Hamano6973dca2006-04-21 23:57:45 -07003983 if (s->data)
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003984 return 0;
Linus Torvalds04786752007-04-15 11:14:28 -07003985
Junio C Hamano6e0b8ed2007-05-07 01:14:21 -07003986 if (size_only && 0 < s->size)
3987 return 0;
3988
Martin Waitz302b9282007-05-21 22:08:28 +02003989 if (S_ISGITLINK(s->mode))
Linus Torvalds04786752007-04-15 11:14:28 -07003990 return diff_populate_gitlink(s, size_only);
3991
brian m. carlson41c95602016-06-24 23:09:24 +00003992 if (!s->oid_valid ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003993 reuse_worktree_file(r->index, s->path, &s->oid, 0)) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05003994 struct strbuf buf = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003995 struct stat st;
3996 int fd;
Linus Torvalds6c510be2007-02-13 11:07:23 -08003997
Junio C Hamano6973dca2006-04-21 23:57:45 -07003998 if (lstat(s->path, &st) < 0) {
Andrey Okoshkin10e0ca82017-10-27 12:33:25 +03003999 err_empty:
4000 err = -1;
4001 empty:
4002 s->data = (char *)"";
4003 s->size = 0;
4004 return err;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004005 }
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05004006 s->size = xsize_t(st.st_size);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004007 if (!s->size)
4008 goto empty;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004009 if (S_ISLNK(st.st_mode)) {
Linus Torvaldscf219d82008-12-17 10:26:13 -08004010 struct strbuf sb = STRBUF_INIT;
4011
4012 if (strbuf_readlink(&sb, s->path, s->size))
Junio C Hamano6973dca2006-04-21 23:57:45 -07004013 goto err_empty;
René Scharfe0956a6d2008-12-18 17:56:51 +01004014 s->size = sb.len;
4015 s->data = strbuf_detach(&sb, NULL);
Linus Torvaldscf219d82008-12-17 10:26:13 -08004016 s->should_free = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004017 return 0;
4018 }
Junio C Hamano12426e12017-03-01 18:04:44 +01004019
4020 /*
4021 * Even if the caller would be happy with getting
4022 * only the size, we cannot return early at this
4023 * point if the path requires us to run the content
4024 * conversion.
4025 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004026 if (size_only && !would_convert_to_git(r->index, s->path))
Linus Torvaldscf219d82008-12-17 10:26:13 -08004027 return 0;
Junio C Hamano12426e12017-03-01 18:04:44 +01004028
4029 /*
4030 * Note: this check uses xsize_t(st.st_size) that may
4031 * not be the true size of the blob after it goes
4032 * through convert_to_git(). This may not strictly be
4033 * correct, but the whole point of big_file_threshold
4034 * and is_binary check being that we want to avoid
4035 * opening the file and inspecting the contents, this
4036 * is probably fine.
4037 */
Jonathan Tan1c37e862020-04-07 15:11:41 -07004038 if (check_binary &&
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07004039 s->size > big_file_threshold && s->is_binary == -1) {
4040 s->is_binary = 1;
4041 return 0;
4042 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004043 fd = open(s->path, O_RDONLY);
4044 if (fd < 0)
4045 goto err_empty;
Shawn O. Pearcec4712e42006-12-24 00:47:23 -05004046 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004047 close(fd);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004048 s->should_munmap = 1;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004049
4050 /*
4051 * Convert from working tree format to canonical git format
4052 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004053 if (convert_to_git(r->index, s->path, s->data, s->size, &buf, conv_flags)) {
René Scharfec32f7492007-10-21 11:23:49 +02004054 size_t size = 0;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004055 munmap(s->data, s->size);
4056 s->should_munmap = 0;
René Scharfec32f7492007-10-21 11:23:49 +02004057 s->data = strbuf_detach(&buf, &size);
4058 s->size = size;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004059 s->should_free = 1;
4060 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004061 }
4062 else {
Jonathan Tanc14b6f82020-04-07 15:11:42 -07004063 struct object_info info = {
4064 .sizep = &s->size
4065 };
4066
4067 if (!(size_only || check_binary))
4068 /*
4069 * Set contentp, since there is no chance that merely
4070 * the size is sufficient.
4071 */
4072 info.contentp = &s->data;
4073
Jonathan Tan95acf112020-04-07 15:11:43 -07004074 if (options && options->missing_object_cb) {
4075 if (!oid_object_info_extended(r, &s->oid, &info,
4076 OBJECT_INFO_LOOKUP_REPLACE |
4077 OBJECT_INFO_SKIP_FETCH_OBJECT))
4078 goto object_read;
4079 options->missing_object_cb(options->missing_object_data);
4080 }
Jonathan Tanc14b6f82020-04-07 15:11:42 -07004081 if (oid_object_info_extended(r, &s->oid, &info,
4082 OBJECT_INFO_LOOKUP_REPLACE))
4083 die("unable to read %s", oid_to_hex(&s->oid));
4084
Jonathan Tan95acf112020-04-07 15:11:43 -07004085object_read:
Jonathan Tan1c37e862020-04-07 15:11:41 -07004086 if (size_only || check_binary) {
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07004087 if (size_only)
4088 return 0;
4089 if (s->size > big_file_threshold && s->is_binary == -1) {
4090 s->is_binary = 1;
4091 return 0;
4092 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004093 }
Jonathan Tanc14b6f82020-04-07 15:11:42 -07004094 if (!info.contentp) {
4095 info.contentp = &s->data;
4096 if (oid_object_info_extended(r, &s->oid, &info,
4097 OBJECT_INFO_LOOKUP_REPLACE))
4098 die("unable to read %s", oid_to_hex(&s->oid));
4099 }
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07004100 s->should_free = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004101 }
4102 return 0;
4103}
4104
Junio C Hamano8ae92e62007-10-02 21:01:03 -07004105void diff_free_filespec_blob(struct diff_filespec *s)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004106{
4107 if (s->should_free)
4108 free(s->data);
4109 else if (s->should_munmap)
4110 munmap(s->data, s->size);
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07004111
4112 if (s->should_free || s->should_munmap) {
4113 s->should_free = s->should_munmap = 0;
4114 s->data = NULL;
4115 }
Jeff Kingeede7b72007-09-25 15:29:42 -04004116}
4117
4118void diff_free_filespec_data(struct diff_filespec *s)
4119{
Jinoh Kang24695932020-11-06 17:14:52 +00004120 if (!s)
4121 return;
4122
Junio C Hamano8ae92e62007-10-02 21:01:03 -07004123 diff_free_filespec_blob(s);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00004124 FREE_AND_NULL(s->cnt_data);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004125}
4126
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004127static void prep_temp_blob(struct index_state *istate,
4128 const char *path, struct diff_tempfile *temp,
Junio C Hamano6973dca2006-04-21 23:57:45 -07004129 void *blob,
4130 unsigned long size,
brian m. carlson09bdff22016-06-24 23:09:29 +00004131 const struct object_id *oid,
Junio C Hamano6973dca2006-04-21 23:57:45 -07004132 int mode)
4133{
Johannes Schindelin4e218f52009-03-21 12:42:52 +01004134 struct strbuf buf = STRBUF_INIT;
Brandon Williamsc2a46a72018-02-14 10:59:54 -08004135 struct strbuf tempfile = STRBUF_INIT;
David Aguilar003b33a2009-05-31 01:35:52 -07004136 char *path_dup = xstrdup(path);
4137 const char *base = basename(path_dup);
brian m. carlsonc397aac2020-03-16 18:05:03 +00004138 struct checkout_metadata meta;
4139
4140 init_checkout_metadata(&meta, NULL, NULL, oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004141
David Aguilar003b33a2009-05-31 01:35:52 -07004142 /* Generate "XXXXXX_basename.ext" */
Brandon Williamsc2a46a72018-02-14 10:59:54 -08004143 strbuf_addstr(&tempfile, "XXXXXX_");
4144 strbuf_addstr(&tempfile, base);
David Aguilar003b33a2009-05-31 01:35:52 -07004145
Brandon Williamsc2a46a72018-02-14 10:59:54 -08004146 temp->tempfile = mks_tempfile_ts(tempfile.buf, strlen(base) + 1);
Jeff King076aa2c2017-09-05 08:15:08 -04004147 if (!temp->tempfile)
Thomas Rastd824cbb2009-06-27 17:58:46 +02004148 die_errno("unable to create temp-file");
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004149 if (convert_to_working_tree(istate, path,
brian m. carlsonc397aac2020-03-16 18:05:03 +00004150 (const char *)blob, (size_t)size, &buf, &meta)) {
Johannes Schindelin4e218f52009-03-21 12:42:52 +01004151 blob = buf.buf;
4152 size = buf.len;
4153 }
Junio C Hamanoc50424a2017-09-25 15:24:06 +09004154 if (write_in_full(temp->tempfile->fd, blob, size) < 0 ||
Jeff King076aa2c2017-09-05 08:15:08 -04004155 close_tempfile_gently(temp->tempfile))
Thomas Rast0721c312009-06-27 17:58:47 +02004156 die_errno("unable to write temp-file");
Jeff King076aa2c2017-09-05 08:15:08 -04004157 temp->name = get_tempfile_path(temp->tempfile);
brian m. carlson09bdff22016-06-24 23:09:29 +00004158 oid_to_hex_r(temp->hex, oid);
Jeff King5096d492015-09-24 17:06:08 -04004159 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode);
Johannes Schindelin4e218f52009-03-21 12:42:52 +01004160 strbuf_release(&buf);
Brandon Williamsc2a46a72018-02-14 10:59:54 -08004161 strbuf_release(&tempfile);
David Aguilar003b33a2009-05-31 01:35:52 -07004162 free(path_dup);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004163}
4164
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004165static struct diff_tempfile *prepare_temp_file(struct repository *r,
4166 const char *name,
4167 struct diff_filespec *one)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004168{
Jeff King479b0ae2009-01-22 00:59:56 -05004169 struct diff_tempfile *temp = claim_diff_tempfile();
4170
Junio C Hamano6973dca2006-04-21 23:57:45 -07004171 if (!DIFF_FILE_VALID(one)) {
4172 not_a_valid_file:
4173 /* A '-' entry produces this for file-2, and
4174 * a '+' entry produces this for file-1.
4175 */
4176 temp->name = "/dev/null";
Jeff King5096d492015-09-24 17:06:08 -04004177 xsnprintf(temp->hex, sizeof(temp->hex), ".");
4178 xsnprintf(temp->mode, sizeof(temp->mode), ".");
Jeff King479b0ae2009-01-22 00:59:56 -05004179 return temp;
4180 }
4181
Thomas Rastaba47272014-02-16 17:52:34 +01004182 if (!S_ISGITLINK(one->mode) &&
brian m. carlson41c95602016-06-24 23:09:24 +00004183 (!one->oid_valid ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004184 reuse_worktree_file(r->index, name, &one->oid, 1))) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07004185 struct stat st;
4186 if (lstat(name, &st) < 0) {
4187 if (errno == ENOENT)
4188 goto not_a_valid_file;
Thomas Rastd824cbb2009-06-27 17:58:46 +02004189 die_errno("stat(%s)", name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004190 }
4191 if (S_ISLNK(st.st_mode)) {
Jeff King3cd73882009-05-25 06:46:09 -04004192 struct strbuf sb = STRBUF_INIT;
4193 if (strbuf_readlink(&sb, name, st.st_size) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02004194 die_errno("readlink(%s)", name);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004195 prep_temp_blob(r->index, name, temp, sb.buf, sb.len,
brian m. carlson41c95602016-06-24 23:09:24 +00004196 (one->oid_valid ?
brian m. carlson09bdff22016-06-24 23:09:29 +00004197 &one->oid : &null_oid),
brian m. carlson41c95602016-06-24 23:09:24 +00004198 (one->oid_valid ?
Junio C Hamano6973dca2006-04-21 23:57:45 -07004199 one->mode : S_IFLNK));
Jeff King3cd73882009-05-25 06:46:09 -04004200 strbuf_release(&sb);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004201 }
4202 else {
4203 /* we can borrow from the file in the work tree */
4204 temp->name = name;
brian m. carlson41c95602016-06-24 23:09:24 +00004205 if (!one->oid_valid)
Brandon Williams74014152017-05-30 10:30:52 -07004206 oid_to_hex_r(temp->hex, &null_oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004207 else
René Scharfe24905742017-01-28 23:03:03 +01004208 oid_to_hex_r(temp->hex, &one->oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004209 /* Even though we may sometimes borrow the
4210 * contents from the work tree, we always want
4211 * one->mode. mode is trustworthy even when
Brandon Williams74014152017-05-30 10:30:52 -07004212 * !(one->oid_valid), as long as
Junio C Hamano6973dca2006-04-21 23:57:45 -07004213 * DIFF_FILE_VALID(one).
4214 */
Jeff King5096d492015-09-24 17:06:08 -04004215 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004216 }
Jeff King479b0ae2009-01-22 00:59:56 -05004217 return temp;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004218 }
4219 else {
Jonathan Tan1c37e862020-04-07 15:11:41 -07004220 if (diff_populate_filespec(r, one, NULL))
Junio C Hamano6973dca2006-04-21 23:57:45 -07004221 die("cannot read data blob for %s", one->path);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004222 prep_temp_blob(r->index, name, temp,
4223 one->data, one->size,
brian m. carlson09bdff22016-06-24 23:09:29 +00004224 &one->oid, one->mode);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004225 }
Jeff King479b0ae2009-01-22 00:59:56 -05004226 return temp;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004227}
4228
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004229static void add_external_diff_name(struct repository *r,
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004230 struct strvec *argv,
Jeff Kingf3efe782014-04-19 15:22:25 -04004231 const char *name,
4232 struct diff_filespec *df)
4233{
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004234 struct diff_tempfile *temp = prepare_temp_file(r, name, df);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004235 strvec_push(argv, temp->name);
4236 strvec_push(argv, temp->hex);
4237 strvec_push(argv, temp->mode);
Jeff Kingf3efe782014-04-19 15:22:25 -04004238}
4239
Junio C Hamano6973dca2006-04-21 23:57:45 -07004240/* An external diff command takes:
4241 *
4242 * diff-cmd name infile1 infile1-sha1 infile1-mode \
4243 * infile2 infile2-sha1 infile2-mode [ rename-to ]
4244 *
4245 */
4246static void run_external_diff(const char *pgm,
4247 const char *name,
4248 const char *other,
4249 struct diff_filespec *one,
4250 struct diff_filespec *two,
4251 const char *xfrm_msg,
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004252 struct diff_options *o)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004253{
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004254 struct strvec argv = STRVEC_INIT;
4255 struct strvec env = STRVEC_INIT;
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004256 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004257
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004258 strvec_push(&argv, pgm);
4259 strvec_push(&argv, name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004260
Junio C Hamano6973dca2006-04-21 23:57:45 -07004261 if (one && two) {
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004262 add_external_diff_name(o->repo, &argv, name, one);
Jeff Kingf3efe782014-04-19 15:22:25 -04004263 if (!other)
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004264 add_external_diff_name(o->repo, &argv, name, two);
Jeff Kingf3efe782014-04-19 15:22:25 -04004265 else {
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004266 add_external_diff_name(o->repo, &argv, other, two);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004267 strvec_push(&argv, other);
4268 strvec_push(&argv, xfrm_msg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004269 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004270 }
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004271
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004272 strvec_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter);
4273 strvec_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr);
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004274
Johannes Schindelin3aef54e2019-07-11 01:23:41 -07004275 diff_free_filespec_data(one);
4276 diff_free_filespec_data(two);
Jeff Kingd70a9eb2020-07-28 20:37:20 -04004277 if (run_command_v_opt_cd_env(argv.v, RUN_USING_SHELL, NULL, env.v))
Jeff King89294d12014-04-19 15:19:19 -04004278 die(_("external diff died, stopping at %s"), name);
4279
Junio C Hamano6973dca2006-04-21 23:57:45 -07004280 remove_tempfile();
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004281 strvec_clear(&argv);
4282 strvec_clear(&env);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004283}
4284
Junio C Hamanob67b9612009-01-26 00:08:24 -08004285static int similarity_index(struct diff_filepair *p)
4286{
4287 return p->score * 100 / MAX_SCORE;
4288}
4289
Jeff King4f036662016-10-20 02:21:25 -04004290static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev)
4291{
4292 if (startup_info->have_repository)
brian m. carlsonaab95832018-03-12 02:27:30 +00004293 return find_unique_abbrev(oid, abbrev);
Jeff King4f036662016-10-20 02:21:25 -04004294 else {
4295 char *hex = oid_to_hex(oid);
Junio C Hamano0d9c5272016-10-27 14:58:48 -07004296 if (abbrev < 0)
4297 abbrev = FALLBACK_DEFAULT_ABBREV;
brian m. carlson02afca12018-07-16 01:28:05 +00004298 if (abbrev > the_hash_algo->hexsz)
Johannes Schindelin033abf92018-05-02 11:38:39 +02004299 BUG("oid abbreviation out of range: %d", abbrev);
Jack Bates43d19482016-12-06 09:56:14 -07004300 if (abbrev)
4301 hex[abbrev] = '\0';
Jeff King4f036662016-10-20 02:21:25 -04004302 return hex;
4303 }
4304}
4305
Junio C Hamanob67b9612009-01-26 00:08:24 -08004306static void fill_metainfo(struct strbuf *msg,
4307 const char *name,
4308 const char *other,
4309 struct diff_filespec *one,
4310 struct diff_filespec *two,
4311 struct diff_options *o,
Bert Wesarg37466442010-05-04 00:38:07 +02004312 struct diff_filepair *p,
Junio C Hamano59777442010-06-18 11:16:57 -07004313 int *must_show_header,
Bert Wesarg37466442010-05-04 00:38:07 +02004314 int use_color)
Junio C Hamanob67b9612009-01-26 00:08:24 -08004315{
Bert Wesarg37466442010-05-04 00:38:07 +02004316 const char *set = diff_get_color(use_color, DIFF_METAINFO);
4317 const char *reset = diff_get_color(use_color, DIFF_RESET);
John Keeping30997bb2013-02-07 20:15:27 +00004318 const char *line_prefix = diff_line_prefix(o);
Bo Yang7be57612010-05-26 15:23:54 +08004319
Christian Couder296c6bb2010-05-26 04:50:12 +02004320 *must_show_header = 1;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004321 strbuf_init(msg, PATH_MAX * 2 + 300);
4322 switch (p->status) {
4323 case DIFF_STATUS_COPIED:
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004324 strbuf_addf(msg, "%s%ssimilarity index %d%%",
4325 line_prefix, set, similarity_index(p));
4326 strbuf_addf(msg, "%s\n%s%scopy from ",
4327 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004328 quote_c_style(name, msg, NULL, 0);
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004329 strbuf_addf(msg, "%s\n%s%scopy to ", reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004330 quote_c_style(other, msg, NULL, 0);
Bert Wesarg37466442010-05-04 00:38:07 +02004331 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004332 break;
4333 case DIFF_STATUS_RENAMED:
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004334 strbuf_addf(msg, "%s%ssimilarity index %d%%",
4335 line_prefix, set, similarity_index(p));
4336 strbuf_addf(msg, "%s\n%s%srename from ",
4337 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004338 quote_c_style(name, msg, NULL, 0);
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004339 strbuf_addf(msg, "%s\n%s%srename to ",
4340 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004341 quote_c_style(other, msg, NULL, 0);
Bert Wesarg37466442010-05-04 00:38:07 +02004342 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004343 break;
4344 case DIFF_STATUS_MODIFIED:
4345 if (p->score) {
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004346 strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n",
4347 line_prefix,
Bert Wesarg37466442010-05-04 00:38:07 +02004348 set, similarity_index(p), reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004349 break;
4350 }
4351 /* fallthru */
4352 default:
Christian Couder296c6bb2010-05-26 04:50:12 +02004353 *must_show_header = 0;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004354 }
Jeff King9001dc22018-08-28 17:22:48 -04004355 if (one && two && !oideq(&one->oid, &two->oid)) {
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004356 const unsigned hexsz = the_hash_algo->hexsz;
Đoàn Trần Công Danh3046c7f2020-08-21 18:51:47 +07004357 int abbrev = o->abbrev ? o->abbrev : DEFAULT_ABBREV;
4358
4359 if (o->flags.full_index)
4360 abbrev = hexsz;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004361
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004362 if (o->flags.binary) {
Junio C Hamanob67b9612009-01-26 00:08:24 -08004363 mmfile_t mf;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004364 if ((!fill_mmfile(o->repo, &mf, one) &&
4365 diff_filespec_is_binary(o->repo, one)) ||
4366 (!fill_mmfile(o->repo, &mf, two) &&
4367 diff_filespec_is_binary(o->repo, two)))
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004368 abbrev = hexsz;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004369 }
Jeff King4f036662016-10-20 02:21:25 -04004370 strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set,
4371 diff_abbrev_oid(&one->oid, abbrev),
4372 diff_abbrev_oid(&two->oid, abbrev));
Junio C Hamanob67b9612009-01-26 00:08:24 -08004373 if (one->mode == two->mode)
4374 strbuf_addf(msg, " %06o", one->mode);
Bert Wesarg37466442010-05-04 00:38:07 +02004375 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004376 }
Junio C Hamanob67b9612009-01-26 00:08:24 -08004377}
4378
Junio C Hamano6973dca2006-04-21 23:57:45 -07004379static void run_diff_cmd(const char *pgm,
4380 const char *name,
4381 const char *other,
Junio C Hamanocd676a52008-02-12 14:26:02 -08004382 const char *attr_path,
Junio C Hamano6973dca2006-04-21 23:57:45 -07004383 struct diff_filespec *one,
4384 struct diff_filespec *two,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004385 struct strbuf *msg,
Junio C Hamano051308f2006-05-04 16:51:44 -07004386 struct diff_options *o,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004387 struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004388{
Junio C Hamanob67b9612009-01-26 00:08:24 -08004389 const char *xfrm_msg = NULL;
4390 int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
Christian Couder296c6bb2010-05-26 04:50:12 +02004391 int must_show_header = 0;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004392
Junio C Hamanobd8c1a92012-07-17 22:08:59 -07004393
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004394 if (o->flags.allow_external) {
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02004395 struct userdiff_driver *drv;
4396
4397 drv = userdiff_find_by_path(o->repo->index, attr_path);
Jeff Kingbe58e702008-10-05 17:43:21 -04004398 if (drv && drv->external)
4399 pgm = drv->external;
Junio C Hamanof1af60b2007-04-22 17:52:55 -07004400 }
4401
Bert Wesarg37466442010-05-04 00:38:07 +02004402 if (msg) {
4403 /*
4404 * don't use colors when the header is intended for an
4405 * external diff driver
4406 */
4407 fill_metainfo(msg, name, other, one, two, o, p,
Junio C Hamano59777442010-06-18 11:16:57 -07004408 &must_show_header,
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07004409 want_color(o->use_color) && !pgm);
Bert Wesarg37466442010-05-04 00:38:07 +02004410 xfrm_msg = msg->len ? msg->buf : NULL;
4411 }
4412
Junio C Hamano6973dca2006-04-21 23:57:45 -07004413 if (pgm) {
Jeff King4bc17922019-02-14 00:49:42 -05004414 run_external_diff(pgm, name, other, one, two, xfrm_msg, o);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004415 return;
4416 }
4417 if (one && two)
4418 builtin_diff(name, other ? other : name,
Christian Couder296c6bb2010-05-26 04:50:12 +02004419 one, two, xfrm_msg, must_show_header,
4420 o, complete_rewrite);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004421 else
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004422 fprintf(o->file, "* Unmerged path %s\n", name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004423}
4424
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004425static void diff_fill_oid_info(struct diff_filespec *one, struct index_state *istate)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004426{
4427 if (DIFF_FILE_VALID(one)) {
brian m. carlson41c95602016-06-24 23:09:24 +00004428 if (!one->oid_valid) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07004429 struct stat st;
Junio C Hamano4682d852012-06-27 20:14:47 -07004430 if (one->is_stdin) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00004431 oidclr(&one->oid);
Johannes Schindelin5332b2a2007-02-25 23:36:10 +01004432 return;
4433 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004434 if (lstat(one->path, &st) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02004435 die_errno("stat '%s'", one->path);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004436 if (index_path(istate, &one->oid, one->path, &st, 0))
Alexander Potashevd7530702009-01-04 21:38:41 +03004437 die("cannot hash %s", one->path);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004438 }
4439 }
4440 else
brian m. carlsona0d12c42016-06-24 23:09:23 +00004441 oidclr(&one->oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004442}
4443
Junio C Hamanocd676a52008-02-12 14:26:02 -08004444static void strip_prefix(int prefix_length, const char **namep, const char **otherp)
4445{
4446 /* Strip the prefix but do not molest /dev/null and absolute paths */
Johannes Sixtffd04e92018-10-19 18:58:07 +02004447 if (*namep && !is_absolute_path(*namep)) {
Junio C Hamanocd676a52008-02-12 14:26:02 -08004448 *namep += prefix_length;
Jakub Narebskid8faea92010-08-09 10:50:53 -04004449 if (**namep == '/')
4450 ++*namep;
4451 }
Johannes Sixtffd04e92018-10-19 18:58:07 +02004452 if (*otherp && !is_absolute_path(*otherp)) {
Junio C Hamanocd676a52008-02-12 14:26:02 -08004453 *otherp += prefix_length;
Jakub Narebskid8faea92010-08-09 10:50:53 -04004454 if (**otherp == '/')
4455 ++*otherp;
4456 }
Junio C Hamanocd676a52008-02-12 14:26:02 -08004457}
4458
Junio C Hamano6973dca2006-04-21 23:57:45 -07004459static void run_diff(struct diff_filepair *p, struct diff_options *o)
4460{
4461 const char *pgm = external_diff();
Pierre Habouzit663af342007-09-20 00:42:15 +02004462 struct strbuf msg;
Pierre Habouzit663af342007-09-20 00:42:15 +02004463 struct diff_filespec *one = p->one;
4464 struct diff_filespec *two = p->two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004465 const char *name;
4466 const char *other;
Junio C Hamanocd676a52008-02-12 14:26:02 -08004467 const char *attr_path;
Pierre Habouzit663af342007-09-20 00:42:15 +02004468
Stefan Bellerf2d2a5d2017-06-29 17:06:46 -07004469 name = one->path;
4470 other = (strcmp(name, two->path) ? two->path : NULL);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004471 attr_path = name;
4472 if (o->prefix_length)
4473 strip_prefix(o->prefix_length, &name, &other);
4474
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004475 if (!o->flags.allow_external)
Junio C Hamanobd8c1a92012-07-17 22:08:59 -07004476 pgm = NULL;
4477
Junio C Hamanocd676a52008-02-12 14:26:02 -08004478 if (DIFF_PAIR_UNMERGED(p)) {
4479 run_diff_cmd(pgm, name, NULL, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004480 NULL, NULL, NULL, o, p);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004481 return;
4482 }
4483
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004484 diff_fill_oid_info(one, o->repo->index);
4485 diff_fill_oid_info(two, o->repo->index);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004486
Junio C Hamano6973dca2006-04-21 23:57:45 -07004487 if (!pgm &&
4488 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
4489 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
Junio C Hamanob67b9612009-01-26 00:08:24 -08004490 /*
4491 * a filepair that changes between file and symlink
Junio C Hamano6973dca2006-04-21 23:57:45 -07004492 * needs to be split into deletion and creation.
4493 */
4494 struct diff_filespec *null = alloc_filespec(two->path);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004495 run_diff_cmd(NULL, name, other, attr_path,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004496 one, null, &msg,
4497 o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004498 free(null);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004499 strbuf_release(&msg);
4500
Junio C Hamano6973dca2006-04-21 23:57:45 -07004501 null = alloc_filespec(one->path);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004502 run_diff_cmd(NULL, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004503 null, two, &msg, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004504 free(null);
4505 }
4506 else
Junio C Hamanocd676a52008-02-12 14:26:02 -08004507 run_diff_cmd(pgm, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004508 one, two, &msg, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004509
Pierre Habouzit663af342007-09-20 00:42:15 +02004510 strbuf_release(&msg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004511}
4512
4513static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
4514 struct diffstat_t *diffstat)
4515{
4516 const char *name;
4517 const char *other;
4518
4519 if (DIFF_PAIR_UNMERGED(p)) {
4520 /* unmerged */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004521 builtin_diffstat(p->one->path, NULL, NULL, NULL,
4522 diffstat, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004523 return;
4524 }
4525
4526 name = p->one->path;
4527 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
4528
Junio C Hamanocd676a52008-02-12 14:26:02 -08004529 if (o->prefix_length)
4530 strip_prefix(o->prefix_length, &name, &other);
4531
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004532 diff_fill_oid_info(p->one, o->repo->index);
4533 diff_fill_oid_info(p->two, o->repo->index);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004534
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004535 builtin_diffstat(name, other, p->one, p->two,
4536 diffstat, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004537}
4538
Johannes Schindelin88246892006-05-20 23:43:13 +02004539static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
4540{
4541 const char *name;
4542 const char *other;
Junio C Hamanocd676a52008-02-12 14:26:02 -08004543 const char *attr_path;
Johannes Schindelin88246892006-05-20 23:43:13 +02004544
4545 if (DIFF_PAIR_UNMERGED(p)) {
4546 /* unmerged */
4547 return;
4548 }
4549
4550 name = p->one->path;
4551 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004552 attr_path = other ? other : name;
4553
4554 if (o->prefix_length)
4555 strip_prefix(o->prefix_length, &name, &other);
Johannes Schindelin88246892006-05-20 23:43:13 +02004556
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004557 diff_fill_oid_info(p->one, o->repo->index);
4558 diff_fill_oid_info(p->two, o->repo->index);
Johannes Schindelin88246892006-05-20 23:43:13 +02004559
Junio C Hamanocd676a52008-02-12 14:26:02 -08004560 builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
Johannes Schindelin88246892006-05-20 23:43:13 +02004561}
4562
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07004563static void prep_parse_options(struct diff_options *options);
4564
Nguyễn Thái Ngọc Duye6757652018-09-21 17:57:24 +02004565void repo_diff_setup(struct repository *r, struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004566{
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +02004567 memcpy(options, &default_diff_options, sizeof(*options));
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004568
4569 options->file = stdout;
Nguyễn Thái Ngọc Duye6757652018-09-21 17:57:24 +02004570 options->repo = r;
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004571
Stefan Beller7648b792018-08-17 13:43:52 -07004572 options->output_indicators[OUTPUT_INDICATOR_NEW] = '+';
4573 options->output_indicators[OUTPUT_INDICATOR_OLD] = '-';
4574 options->output_indicators[OUTPUT_INDICATOR_CONTEXT] = ' ';
Jack Bates43d19482016-12-06 09:56:14 -07004575 options->abbrev = DEFAULT_ABBREV;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004576 options->line_termination = '\n';
4577 options->break_opt = -1;
4578 options->rename_limit = -1;
Johan Herland712d2c72011-04-29 11:36:20 +02004579 options->dirstat_permille = diff_dirstat_permille_default;
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -04004580 options->context = diff_context_default;
Vegard Nossumc4888672017-01-12 13:21:11 +01004581 options->interhunkcontext = diff_interhunk_context_default;
Junio C Hamanoa17505f2016-10-04 15:26:27 -07004582 options->ws_error_highlight = ws_error_highlight_default;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004583 options->flags.rename_empty = 1;
Laurent Arnoudc28ded82020-05-22 12:46:18 +02004584 options->flags.relative_name = diff_relative;
Stefan Beller15af58c2018-01-04 14:50:42 -08004585 options->objfind = NULL;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004586
Kirill Smelkov72441af2014-04-07 01:46:26 +04004587 /* pathchange left =NULL by default */
Junio C Hamano6973dca2006-04-21 23:57:45 -07004588 options->change = diff_change;
4589 options->add_remove = diff_addremove;
Jeff Kingf1c96262011-08-17 22:03:12 -07004590 options->use_color = diff_use_color_default;
Eric Wongb68ea122006-07-07 04:01:23 -07004591 options->detect_rename = diff_detect_rename_default;
Michal Privoznik07ab4de2013-01-16 08:51:57 +01004592 options->xdl_opts |= diff_algorithm;
Michael Haggerty433860f2016-09-05 11:44:51 +02004593 if (diff_indent_heuristic)
4594 DIFF_XDL_SET(options, INDENT_HEURISTIC);
Johannes Schindelineab9a402007-12-18 19:32:14 +00004595
Samuel Bronson6d8940b2013-12-18 19:08:12 -05004596 options->orderfile = diff_order_file_cfg;
4597
Sangeeta Jain8ef93122020-11-10 14:09:00 +05304598 if (!options->flags.ignore_submodule_set)
4599 options->flags.ignore_untracked_in_submodules = 1;
4600
Eli Collinsf89504d2010-05-02 19:03:41 -07004601 if (diff_no_prefix) {
4602 options->a_prefix = options->b_prefix = "";
4603 } else if (!diff_mnemonic_prefix) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07004604 options->a_prefix = "a/";
4605 options->b_prefix = "b/";
4606 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07004607
4608 options->color_moved = diff_color_moved_default;
Stefan Beller626c0b52018-07-18 12:31:56 -07004609 options->color_moved_ws_handling = diff_color_moved_ws_default;
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07004610
4611 prep_parse_options(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004612}
4613
Thomas Rast28452652012-08-03 14:16:24 +02004614void diff_setup_done(struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004615{
Stefan Beller4d8c51a2018-01-04 14:50:44 -08004616 unsigned check_mask = DIFF_FORMAT_NAME |
4617 DIFF_FORMAT_NAME_STATUS |
4618 DIFF_FORMAT_CHECKDIFF |
4619 DIFF_FORMAT_NO_OUTPUT;
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004620 /*
4621 * This must be signed because we're comparing against a potentially
4622 * negative value.
4623 */
4624 const int hexsz = the_hash_algo->hexsz;
Timo Hirvonend7de00f2006-06-24 20:26:49 +03004625
Junio C Hamano6c374002013-05-10 17:10:11 +02004626 if (options->set_default)
4627 options->set_default(options);
4628
Stefan Beller4d8c51a2018-01-04 14:50:44 -08004629 if (HAS_MULTI_BITS(options->output_format & check_mask))
Jean-Noël AVILAa2f05c92016-09-20 21:04:27 +02004630 die(_("--name-only, --name-status, --check and -s are mutually exclusive"));
Timo Hirvonend7de00f2006-06-24 20:26:49 +03004631
Stefan Beller5e505252018-01-04 14:50:43 -08004632 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
4633 die(_("-G, -S and --find-object are mutually exclusive"));
4634
Ævar Arnfjörð Bjarmason188e9e22021-04-12 19:15:14 +02004635 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_G_REGEX_MASK))
4636 die(_("-G and --pickaxe-regex are mutually exclusive, use --pickaxe-regex with -S"));
4637
Ævar Arnfjörð Bjarmasond26ec882021-04-12 19:15:15 +02004638 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK))
4639 die(_("---pickaxe-all and --find-object are mutually exclusive, use --pickaxe-all with -G and -S"));
4640
Junio C Hamanof2451942009-05-22 12:45:29 -07004641 /*
4642 * Most of the time we can say "there are changes"
4643 * only by checking if there are changed paths, but
4644 * --ignore-whitespace* options force us to look
Jim Meyering97bf2a02009-08-30 22:27:02 +02004645 * inside contents.
Junio C Hamanof2451942009-05-22 12:45:29 -07004646 */
4647
Junio C Hamano50f04392020-12-16 17:27:13 -08004648 if ((options->xdl_opts & XDF_WHITESPACE_FLAGS) ||
4649 options->ignore_regex_nr)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004650 options->flags.diff_from_contents = 1;
Junio C Hamanof2451942009-05-22 12:45:29 -07004651 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004652 options->flags.diff_from_contents = 0;
Junio C Hamanof2451942009-05-22 12:45:29 -07004653
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004654 if (options->flags.find_copies_harder)
Junio C Hamano03b9d562006-08-09 13:17:19 -07004655 options->detect_rename = DIFF_DETECT_COPY;
4656
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004657 if (!options->flags.relative_name)
Junio C Hamanocd676a52008-02-12 14:26:02 -08004658 options->prefix = NULL;
4659 if (options->prefix)
4660 options->prefix_length = strlen(options->prefix);
4661 else
4662 options->prefix_length = 0;
4663
Timo Hirvonenc6744342006-06-24 20:21:53 +03004664 if (options->output_format & (DIFF_FORMAT_NAME |
4665 DIFF_FORMAT_NAME_STATUS |
4666 DIFF_FORMAT_CHECKDIFF |
4667 DIFF_FORMAT_NO_OUTPUT))
4668 options->output_format &= ~(DIFF_FORMAT_RAW |
Junio C Hamano74e2abe2006-10-12 03:01:00 -07004669 DIFF_FORMAT_NUMSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004670 DIFF_FORMAT_DIFFSTAT |
Nicolas Pitreebd124c2006-12-14 23:15:44 -05004671 DIFF_FORMAT_SHORTSTAT |
Linus Torvalds7df7c012008-02-12 13:26:31 -08004672 DIFF_FORMAT_DIRSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004673 DIFF_FORMAT_SUMMARY |
4674 DIFF_FORMAT_PATCH);
4675
Junio C Hamano6973dca2006-04-21 23:57:45 -07004676 /*
4677 * These cases always need recursive; we do not drop caller-supplied
4678 * recursive bits for other formats here.
4679 */
Timo Hirvonenc6744342006-06-24 20:21:53 +03004680 if (options->output_format & (DIFF_FORMAT_PATCH |
Junio C Hamano74e2abe2006-10-12 03:01:00 -07004681 DIFF_FORMAT_NUMSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004682 DIFF_FORMAT_DIFFSTAT |
Nicolas Pitreebd124c2006-12-14 23:15:44 -05004683 DIFF_FORMAT_SHORTSTAT |
Linus Torvalds7df7c012008-02-12 13:26:31 -08004684 DIFF_FORMAT_DIRSTAT |
Johannes Schindelind7014dc2006-10-03 23:09:56 +02004685 DIFF_FORMAT_SUMMARY |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004686 DIFF_FORMAT_CHECKDIFF))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004687 options->flags.recursive = 1;
Junio C Hamano5e363542006-05-22 00:31:02 -07004688 /*
Junio C Hamano3969cf72006-06-27 15:08:19 -07004689 * Also pickaxe would not work very well if you do not say recursive
Junio C Hamano5e363542006-05-22 00:31:02 -07004690 */
Stefan Bellercf630512018-01-04 14:50:41 -08004691 if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004692 options->flags.recursive = 1;
Jens Lehmannae6d5c12010-03-11 22:50:25 +01004693 /*
4694 * When patches are generated, submodules diffed against the work tree
4695 * must be checked for dirtiness too so it can be shown in the output
4696 */
4697 if (options->output_format & DIFF_FORMAT_PATCH)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004698 options->flags.dirty_submodules = 1;
Junio C Hamano5e363542006-05-22 00:31:02 -07004699
Junio C Hamano6973dca2006-04-21 23:57:45 -07004700 if (options->detect_rename && options->rename_limit < 0)
4701 options->rename_limit = diff_rename_limit_default;
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004702 if (hexsz < options->abbrev)
4703 options->abbrev = hexsz; /* full */
Junio C Hamano6973dca2006-04-21 23:57:45 -07004704
Junio C Hamano68aacb22007-03-14 11:12:13 -07004705 /*
4706 * It does not make sense to show the first hit we happened
4707 * to have found. It does not make sense not to return with
4708 * exit code in such a case either.
4709 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004710 if (options->flags.quick) {
Junio C Hamano68aacb22007-03-14 11:12:13 -07004711 options->output_format = DIFF_FORMAT_NO_OUTPUT;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004712 options->flags.exit_with_status = 1;
Junio C Hamano68aacb22007-03-14 11:12:13 -07004713 }
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004714
Jeff King0ea7d5b2013-12-16 15:02:21 -05004715 options->diff_path_counter = 0;
Junio C Hamanob0e2c992014-06-16 10:07:09 -07004716
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004717 if (options->flags.follow_renames && options->pathspec.nr != 1)
Jeff Kingdd63f162014-05-20 02:49:20 -04004718 die(_("--follow requires exactly one pathspec"));
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07004719
4720 if (!options->use_color || external_diff())
4721 options->color_moved = 0;
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07004722
4723 FREE_AND_NULL(options->parseopts);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004724}
4725
Matthieu Moydea007f2010-08-05 10:22:52 +02004726int parse_long_opt(const char *opt, const char **argv,
4727 const char **optarg)
4728{
4729 const char *arg = argv[0];
Jeff King95b567c2014-06-18 15:48:29 -04004730 if (!skip_prefix(arg, "--", &arg))
Matthieu Moydea007f2010-08-05 10:22:52 +02004731 return 0;
Jeff King95b567c2014-06-18 15:48:29 -04004732 if (!skip_prefix(arg, opt, &arg))
Matthieu Moydea007f2010-08-05 10:22:52 +02004733 return 0;
Nicolas Vigierb0d12fc2013-10-31 12:08:28 +01004734 if (*arg == '=') { /* stuck form: --option=value */
Matthieu Moydea007f2010-08-05 10:22:52 +02004735 *optarg = arg + 1;
4736 return 1;
4737 }
4738 if (*arg != '\0')
4739 return 0;
4740 /* separate form: --option value */
4741 if (!argv[1])
4742 die("Option '--%s' requires a value", opt);
4743 *optarg = argv[1];
4744 return 2;
4745}
4746
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004747static int diff_opt_stat(const struct option *opt, const char *value, int unset)
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004748{
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004749 struct diff_options *options = opt->value;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004750 int width = options->stat_width;
4751 int name_width = options->stat_name_width;
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004752 int graph_width = options->stat_graph_width;
Michael J Gruber808e1db2011-05-27 14:36:41 +02004753 int count = options->stat_count;
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004754 char *end;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004755
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004756 BUG_ON_OPT_NEG(unset);
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004757
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004758 if (!strcmp(opt->long_name, "stat")) {
4759 if (value) {
4760 width = strtoul(value, &end, 10);
4761 if (*end == ',')
4762 name_width = strtoul(end+1, &end, 10);
4763 if (*end == ',')
4764 count = strtoul(end+1, &end, 10);
4765 if (*end)
4766 return error(_("invalid --stat value: %s"), value);
Matthieu Moy1e572082010-08-05 10:22:54 +02004767 }
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004768 } else if (!strcmp(opt->long_name, "stat-width")) {
4769 width = strtoul(value, &end, 10);
4770 if (*end)
4771 return error(_("%s expects a numerical value"),
4772 opt->long_name);
4773 } else if (!strcmp(opt->long_name, "stat-name-width")) {
4774 name_width = strtoul(value, &end, 10);
4775 if (*end)
4776 return error(_("%s expects a numerical value"),
4777 opt->long_name);
4778 } else if (!strcmp(opt->long_name, "stat-graph-width")) {
4779 graph_width = strtoul(value, &end, 10);
4780 if (*end)
4781 return error(_("%s expects a numerical value"),
4782 opt->long_name);
4783 } else if (!strcmp(opt->long_name, "stat-count")) {
4784 count = strtoul(value, &end, 10);
4785 if (*end)
4786 return error(_("%s expects a numerical value"),
4787 opt->long_name);
4788 } else
4789 BUG("%s should not get here", opt->long_name);
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004790
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004791 options->output_format |= DIFF_FORMAT_DIFFSTAT;
4792 options->stat_name_width = name_width;
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004793 options->stat_graph_width = graph_width;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004794 options->stat_width = width;
Michael J Gruber808e1db2011-05-27 14:36:41 +02004795 options->stat_count = count;
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004796 return 0;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004797}
4798
Johan Herland333f3fb2011-04-29 11:36:18 +02004799static int parse_dirstat_opt(struct diff_options *options, const char *params)
4800{
Johan Herland51670fc2011-04-29 11:36:22 +02004801 struct strbuf errmsg = STRBUF_INIT;
4802 if (parse_dirstat_params(options, params, &errmsg))
Johan Herland7478ac52011-04-29 11:36:23 +02004803 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
Johan Herland51670fc2011-04-29 11:36:22 +02004804 errmsg.buf);
4805 strbuf_release(&errmsg);
Johan Herland333f3fb2011-04-29 11:36:18 +02004806 /*
4807 * The caller knows a dirstat-related option is given from the command
4808 * line; allow it to say "return this_function();"
4809 */
4810 options->output_format |= DIFF_FORMAT_DIRSTAT;
4811 return 1;
4812}
4813
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004814static const char diff_status_letters[] = {
4815 DIFF_STATUS_ADDED,
4816 DIFF_STATUS_COPIED,
4817 DIFF_STATUS_DELETED,
4818 DIFF_STATUS_MODIFIED,
4819 DIFF_STATUS_RENAMED,
4820 DIFF_STATUS_TYPE_CHANGED,
4821 DIFF_STATUS_UNKNOWN,
4822 DIFF_STATUS_UNMERGED,
4823 DIFF_STATUS_FILTER_AON,
4824 DIFF_STATUS_FILTER_BROKEN,
4825 '\0',
4826};
4827
4828static unsigned int filter_bit['Z' + 1];
4829
4830static void prepare_filter_bits(void)
4831{
4832 int i;
4833
4834 if (!filter_bit[DIFF_STATUS_ADDED]) {
4835 for (i = 0; diff_status_letters[i]; i++)
4836 filter_bit[(int) diff_status_letters[i]] = (1 << i);
4837 }
4838}
4839
4840static unsigned filter_bit_tst(char status, const struct diff_options *opt)
4841{
4842 return opt->filter & filter_bit[(int) status];
4843}
4844
Nguyễn Thái Ngọc Duycdb53302019-03-24 15:20:14 +07004845unsigned diff_filter_bit(char status)
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004846{
Nguyễn Thái Ngọc Duycdb53302019-03-24 15:20:14 +07004847 prepare_filter_bits();
4848 return filter_bit[(int) status];
4849}
4850
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004851static int diff_opt_diff_filter(const struct option *option,
4852 const char *optarg, int unset)
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004853{
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004854 struct diff_options *opt = option->value;
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004855 int i, optch;
4856
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004857 BUG_ON_OPT_NEG(unset);
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004858 prepare_filter_bits();
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004859
4860 /*
4861 * If there is a negation e.g. 'd' in the input, and we haven't
4862 * initialized the filter field with another --diff-filter, start
4863 * from full set of bits, except for AON.
4864 */
4865 if (!opt->filter) {
4866 for (i = 0; (optch = optarg[i]) != '\0'; i++) {
4867 if (optch < 'a' || 'z' < optch)
4868 continue;
4869 opt->filter = (1 << (ARRAY_SIZE(diff_status_letters) - 1)) - 1;
4870 opt->filter &= ~filter_bit[DIFF_STATUS_FILTER_AON];
4871 break;
4872 }
4873 }
4874
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004875 for (i = 0; (optch = optarg[i]) != '\0'; i++) {
4876 unsigned int bit;
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004877 int negate;
4878
4879 if ('a' <= optch && optch <= 'z') {
4880 negate = 1;
4881 optch = toupper(optch);
4882 } else {
4883 negate = 0;
4884 }
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004885
4886 bit = (0 <= optch && optch <= 'Z') ? filter_bit[optch] : 0;
4887 if (!bit)
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004888 return error(_("unknown change class '%c' in --diff-filter=%s"),
4889 optarg[i], optarg);
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004890 if (negate)
4891 opt->filter &= ~bit;
4892 else
4893 opt->filter |= bit;
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004894 }
4895 return 0;
4896}
4897
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +01004898static void enable_patch_output(int *fmt)
4899{
Matthieu Moy71482d32013-07-16 10:05:37 +02004900 *fmt &= ~DIFF_FORMAT_NO_OUTPUT;
4901 *fmt |= DIFF_FORMAT_PATCH;
4902}
4903
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07004904static int diff_opt_ws_error_highlight(const struct option *option,
4905 const char *arg, int unset)
Junio C Hamanob8767f72015-05-26 10:11:28 -07004906{
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07004907 struct diff_options *opt = option->value;
Junio C Hamano077965f2016-10-04 15:03:33 -07004908 int val = parse_ws_error_highlight(arg);
Junio C Hamanob8767f72015-05-26 10:11:28 -07004909
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07004910 BUG_ON_OPT_NEG(unset);
4911 if (val < 0)
4912 return error(_("unknown value after ws-error-highlight=%.*s"),
4913 -1 - val, arg);
Junio C Hamanob8767f72015-05-26 10:11:28 -07004914 opt->ws_error_highlight = val;
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07004915 return 0;
Junio C Hamanob8767f72015-05-26 10:11:28 -07004916}
4917
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004918static int diff_opt_find_object(const struct option *option,
4919 const char *arg, int unset)
Stefan Beller15af58c2018-01-04 14:50:42 -08004920{
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004921 struct diff_options *opt = option->value;
Stefan Beller15af58c2018-01-04 14:50:42 -08004922 struct object_id oid;
4923
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004924 BUG_ON_OPT_NEG(unset);
Stefan Beller15af58c2018-01-04 14:50:42 -08004925 if (get_oid(arg, &oid))
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004926 return error(_("unable to resolve '%s'"), arg);
Stefan Beller15af58c2018-01-04 14:50:42 -08004927
4928 if (!opt->objfind)
René Scharfeca56dad2021-03-13 17:17:22 +01004929 CALLOC_ARRAY(opt->objfind, 1);
Stefan Beller15af58c2018-01-04 14:50:42 -08004930
4931 opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND;
4932 opt->flags.recursive = 1;
4933 opt->flags.tree_in_recursive = 1;
4934 oidset_insert(opt->objfind, &oid);
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004935 return 0;
Stefan Beller15af58c2018-01-04 14:50:42 -08004936}
4937
Nguyễn Thái Ngọc Duydf84a432019-03-05 19:30:11 +07004938static int diff_opt_anchored(const struct option *opt,
4939 const char *arg, int unset)
4940{
4941 struct diff_options *options = opt->value;
4942
4943 BUG_ON_OPT_NEG(unset);
4944 options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
4945 ALLOC_GROW(options->anchors, options->anchors_nr + 1,
4946 options->anchors_alloc);
4947 options->anchors[options->anchors_nr++] = xstrdup(arg);
4948 return 0;
4949}
4950
Nguyễn Thái Ngọc Duy6d9af6f2019-03-05 19:30:12 +07004951static int diff_opt_binary(const struct option *opt,
4952 const char *arg, int unset)
4953{
4954 struct diff_options *options = opt->value;
4955
4956 BUG_ON_OPT_NEG(unset);
4957 BUG_ON_OPT_ARG(arg);
4958 enable_patch_output(&options->output_format);
4959 options->flags.binary = 1;
4960 return 0;
4961}
4962
Nguyễn Thái Ngọc Duyced4e172019-02-21 18:16:13 +07004963static int diff_opt_break_rewrites(const struct option *opt,
4964 const char *arg, int unset)
4965{
4966 int *break_opt = opt->value;
4967 int opt1, opt2;
4968
4969 BUG_ON_OPT_NEG(unset);
4970 if (!arg)
4971 arg = "";
4972 opt1 = parse_rename_score(&arg);
4973 if (*arg == 0)
4974 opt2 = 0;
4975 else if (*arg != '/')
4976 return error(_("%s expects <n>/<m> form"), opt->long_name);
4977 else {
4978 arg++;
4979 opt2 = parse_rename_score(&arg);
4980 }
4981 if (*arg != 0)
4982 return error(_("%s expects <n>/<m> form"), opt->long_name);
4983 *break_opt = opt1 | (opt2 << 16);
4984 return 0;
4985}
4986
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07004987static int diff_opt_char(const struct option *opt,
4988 const char *arg, int unset)
4989{
4990 char *value = opt->value;
4991
4992 BUG_ON_OPT_NEG(unset);
4993 if (arg[1])
4994 return error(_("%s expects a character, got '%s'"),
4995 opt->long_name, arg);
4996 *value = arg[0];
4997 return 0;
4998}
4999
Nguyễn Thái Ngọc Duy59311a92019-03-24 15:20:09 +07005000static int diff_opt_color_moved(const struct option *opt,
5001 const char *arg, int unset)
5002{
5003 struct diff_options *options = opt->value;
5004
5005 if (unset) {
5006 options->color_moved = COLOR_MOVED_NO;
5007 } else if (!arg) {
5008 if (diff_color_moved_default)
5009 options->color_moved = diff_color_moved_default;
5010 if (options->color_moved == COLOR_MOVED_NO)
5011 options->color_moved = COLOR_MOVED_DEFAULT;
5012 } else {
5013 int cm = parse_color_moved(arg);
5014 if (cm < 0)
5015 return error(_("bad --color-moved argument: %s"), arg);
5016 options->color_moved = cm;
5017 }
5018 return 0;
5019}
5020
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005021static int diff_opt_color_moved_ws(const struct option *opt,
5022 const char *arg, int unset)
5023{
5024 struct diff_options *options = opt->value;
5025 unsigned cm;
5026
Nguyễn Thái Ngọc Duybb987292019-03-24 15:20:11 +07005027 if (unset) {
5028 options->color_moved_ws_handling = 0;
5029 return 0;
5030 }
5031
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005032 cm = parse_color_moved_ws(arg);
5033 if (cm & COLOR_MOVED_WS_ERROR)
5034 return error(_("invalid mode '%s' in --color-moved-ws"), arg);
5035 options->color_moved_ws_handling = cm;
5036 return 0;
5037}
5038
Nguyễn Thái Ngọc Duy212db692019-03-05 19:30:20 +07005039static int diff_opt_color_words(const struct option *opt,
5040 const char *arg, int unset)
5041{
5042 struct diff_options *options = opt->value;
5043
5044 BUG_ON_OPT_NEG(unset);
5045 options->use_color = 1;
5046 options->word_diff = DIFF_WORDS_COLOR;
5047 options->word_regex = arg;
5048 return 0;
5049}
5050
Nguyễn Thái Ngọc Duy7d7942b2019-02-21 18:16:11 +07005051static int diff_opt_compact_summary(const struct option *opt,
5052 const char *arg, int unset)
5053{
5054 struct diff_options *options = opt->value;
5055
5056 BUG_ON_OPT_ARG(arg);
5057 if (unset) {
5058 options->flags.stat_with_summary = 0;
5059 } else {
5060 options->flags.stat_with_summary = 1;
5061 options->output_format |= DIFF_FORMAT_DIFFSTAT;
5062 }
5063 return 0;
5064}
5065
Nguyễn Thái Ngọc Duy10f35b12019-03-05 19:30:10 +07005066static int diff_opt_diff_algorithm(const struct option *opt,
5067 const char *arg, int unset)
5068{
5069 struct diff_options *options = opt->value;
5070 long value = parse_algorithm_value(arg);
5071
5072 BUG_ON_OPT_NEG(unset);
5073 if (value < 0)
5074 return error(_("option diff-algorithm accepts \"myers\", "
5075 "\"minimal\", \"patience\" and \"histogram\""));
5076
5077 /* clear out previous settings */
5078 DIFF_XDL_CLR(options, NEED_MINIMAL);
5079 options->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
5080 options->xdl_opts |= value;
5081 return 0;
5082}
5083
Nguyễn Thái Ngọc Duy4ce7aab2019-02-21 18:16:03 +07005084static int diff_opt_dirstat(const struct option *opt,
5085 const char *arg, int unset)
5086{
5087 struct diff_options *options = opt->value;
5088
5089 BUG_ON_OPT_NEG(unset);
5090 if (!strcmp(opt->long_name, "cumulative")) {
5091 if (arg)
5092 BUG("how come --cumulative take a value?");
5093 arg = "cumulative";
5094 } else if (!strcmp(opt->long_name, "dirstat-by-file"))
5095 parse_dirstat_opt(options, "files");
5096 parse_dirstat_opt(options, arg ? arg : "");
5097 return 0;
5098}
5099
Nguyễn Thái Ngọc Duy7f648502019-02-21 18:16:16 +07005100static int diff_opt_find_copies(const struct option *opt,
5101 const char *arg, int unset)
5102{
5103 struct diff_options *options = opt->value;
5104
5105 BUG_ON_OPT_NEG(unset);
5106 if (!arg)
5107 arg = "";
5108 options->rename_score = parse_rename_score(&arg);
5109 if (*arg != 0)
5110 return error(_("invalid argument to %s"), opt->long_name);
5111
5112 if (options->detect_rename == DIFF_DETECT_COPY)
5113 options->flags.find_copies_harder = 1;
5114 else
5115 options->detect_rename = DIFF_DETECT_COPY;
5116
5117 return 0;
5118}
5119
Nguyễn Thái Ngọc Duyf4763082019-02-21 18:16:14 +07005120static int diff_opt_find_renames(const struct option *opt,
5121 const char *arg, int unset)
5122{
5123 struct diff_options *options = opt->value;
5124
5125 BUG_ON_OPT_NEG(unset);
5126 if (!arg)
5127 arg = "";
5128 options->rename_score = parse_rename_score(&arg);
5129 if (*arg != 0)
5130 return error(_("invalid argument to %s"), opt->long_name);
5131
5132 options->detect_rename = DIFF_DETECT_RENAME;
5133 return 0;
5134}
5135
Nguyễn Thái Ngọc Duy1e9250b2019-03-05 19:30:16 +07005136static int diff_opt_follow(const struct option *opt,
5137 const char *arg, int unset)
5138{
5139 struct diff_options *options = opt->value;
5140
5141 BUG_ON_OPT_ARG(arg);
5142 if (unset) {
5143 options->flags.follow_renames = 0;
5144 options->flags.default_follow_renames = 0;
5145 } else {
5146 options->flags.follow_renames = 1;
5147 }
5148 return 0;
5149}
5150
Nguyễn Thái Ngọc Duyb680ee12019-03-05 19:30:25 +07005151static int diff_opt_ignore_submodules(const struct option *opt,
5152 const char *arg, int unset)
5153{
5154 struct diff_options *options = opt->value;
5155
5156 BUG_ON_OPT_NEG(unset);
5157 if (!arg)
5158 arg = "all";
5159 options->flags.override_submodule_config = 1;
5160 handle_ignore_submodules_arg(options, arg);
5161 return 0;
5162}
5163
Nguyễn Thái Ngọc Duy2f81cf92019-03-24 15:20:06 +07005164static int diff_opt_line_prefix(const struct option *opt,
5165 const char *optarg, int unset)
5166{
5167 struct diff_options *options = opt->value;
5168
5169 BUG_ON_OPT_NEG(unset);
5170 options->line_prefix = optarg;
5171 options->line_prefix_length = strlen(options->line_prefix);
5172 graph_setup_line_prefix(options);
5173 return 0;
5174}
5175
Nguyễn Thái Ngọc Duy11c659d2019-03-24 15:20:07 +07005176static int diff_opt_no_prefix(const struct option *opt,
5177 const char *optarg, int unset)
5178{
5179 struct diff_options *options = opt->value;
5180
5181 BUG_ON_OPT_NEG(unset);
5182 BUG_ON_OPT_ARG(optarg);
5183 options->a_prefix = "";
5184 options->b_prefix = "";
5185 return 0;
5186}
5187
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005188static enum parse_opt_result diff_opt_output(struct parse_opt_ctx_t *ctx,
5189 const struct option *opt,
5190 const char *arg, int unset)
5191{
5192 struct diff_options *options = opt->value;
5193 char *path;
5194
5195 BUG_ON_OPT_NEG(unset);
5196 path = prefix_filename(ctx->prefix, arg);
5197 options->file = xfopen(path, "w");
5198 options->close_file = 1;
5199 if (options->use_color != GIT_COLOR_ALWAYS)
5200 options->use_color = GIT_COLOR_NEVER;
5201 free(path);
5202 return 0;
5203}
5204
Nguyễn Thái Ngọc Duy31fd6402019-03-05 19:30:08 +07005205static int diff_opt_patience(const struct option *opt,
5206 const char *arg, int unset)
5207{
5208 struct diff_options *options = opt->value;
5209 int i;
5210
5211 BUG_ON_OPT_NEG(unset);
5212 BUG_ON_OPT_ARG(arg);
5213 options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
5214 /*
5215 * Both --patience and --anchored use PATIENCE_DIFF
5216 * internally, so remove any anchors previously
5217 * specified.
5218 */
5219 for (i = 0; i < options->anchors_nr; i++)
5220 free(options->anchors[i]);
5221 options->anchors_nr = 0;
5222 return 0;
5223}
5224
Michał Kępień296d4a92020-10-20 08:48:09 +02005225static int diff_opt_ignore_regex(const struct option *opt,
5226 const char *arg, int unset)
5227{
5228 struct diff_options *options = opt->value;
5229 regex_t *regex;
5230
5231 BUG_ON_OPT_NEG(unset);
5232 regex = xmalloc(sizeof(*regex));
5233 if (regcomp(regex, arg, REG_EXTENDED | REG_NEWLINE))
5234 return error(_("invalid regex given to -I: '%s'"), arg);
5235 ALLOC_GROW(options->ignore_regex, options->ignore_regex_nr + 1,
5236 options->ignore_regex_alloc);
5237 options->ignore_regex[options->ignore_regex_nr++] = regex;
5238 return 0;
5239}
5240
Nguyễn Thái Ngọc Duya41cfb32019-03-24 15:19:59 +07005241static int diff_opt_pickaxe_regex(const struct option *opt,
5242 const char *arg, int unset)
5243{
5244 struct diff_options *options = opt->value;
5245
5246 BUG_ON_OPT_NEG(unset);
5247 options->pickaxe = arg;
5248 options->pickaxe_opts |= DIFF_PICKAXE_KIND_G;
5249 return 0;
5250}
5251
5252static int diff_opt_pickaxe_string(const struct option *opt,
5253 const char *arg, int unset)
5254{
5255 struct diff_options *options = opt->value;
5256
5257 BUG_ON_OPT_NEG(unset);
5258 options->pickaxe = arg;
5259 options->pickaxe_opts |= DIFF_PICKAXE_KIND_S;
5260 return 0;
5261}
5262
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005263static int diff_opt_relative(const struct option *opt,
5264 const char *arg, int unset)
5265{
5266 struct diff_options *options = opt->value;
5267
Laurent Arnoudc28ded82020-05-22 12:46:18 +02005268 options->flags.relative_name = !unset;
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005269 if (arg)
5270 options->prefix = arg;
5271 return 0;
5272}
5273
Nguyễn Thái Ngọc Duy125dcea2019-03-05 19:30:26 +07005274static int diff_opt_submodule(const struct option *opt,
5275 const char *arg, int unset)
5276{
5277 struct diff_options *options = opt->value;
5278
5279 BUG_ON_OPT_NEG(unset);
5280 if (!arg)
5281 arg = "log";
5282 if (parse_submodule_params(options, arg))
5283 return error(_("failed to parse --submodule option parameter: '%s'"),
5284 arg);
5285 return 0;
5286}
5287
Nguyễn Thái Ngọc Duy8ab76972019-03-05 19:30:24 +07005288static int diff_opt_textconv(const struct option *opt,
5289 const char *arg, int unset)
5290{
5291 struct diff_options *options = opt->value;
5292
5293 BUG_ON_OPT_ARG(arg);
5294 if (unset) {
5295 options->flags.allow_textconv = 0;
5296 } else {
5297 options->flags.allow_textconv = 1;
5298 options->flags.textconv_set_via_cmdline = 1;
5299 }
5300 return 0;
5301}
5302
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 07:35:33 +07005303static int diff_opt_unified(const struct option *opt,
5304 const char *arg, int unset)
5305{
5306 struct diff_options *options = opt->value;
5307 char *s;
5308
5309 BUG_ON_OPT_NEG(unset);
5310
Nguyễn Thái Ngọc Duy8ef05192019-05-29 16:11:15 +07005311 if (arg) {
5312 options->context = strtol(arg, &s, 10);
5313 if (*s)
5314 return error(_("%s expects a numerical value"), "--unified");
5315 }
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 07:35:33 +07005316 enable_patch_output(&options->output_format);
5317
5318 return 0;
5319}
5320
Nguyễn Thái Ngọc Duye9fb39b2019-03-05 19:30:18 +07005321static int diff_opt_word_diff(const struct option *opt,
5322 const char *arg, int unset)
5323{
5324 struct diff_options *options = opt->value;
5325
5326 BUG_ON_OPT_NEG(unset);
5327 if (arg) {
5328 if (!strcmp(arg, "plain"))
5329 options->word_diff = DIFF_WORDS_PLAIN;
5330 else if (!strcmp(arg, "color")) {
5331 options->use_color = 1;
5332 options->word_diff = DIFF_WORDS_COLOR;
5333 }
5334 else if (!strcmp(arg, "porcelain"))
5335 options->word_diff = DIFF_WORDS_PORCELAIN;
5336 else if (!strcmp(arg, "none"))
5337 options->word_diff = DIFF_WORDS_NONE;
5338 else
5339 return error(_("bad --word-diff argument: %s"), arg);
5340 } else {
5341 if (options->word_diff == DIFF_WORDS_NONE)
5342 options->word_diff = DIFF_WORDS_PLAIN;
5343 }
5344 return 0;
5345}
5346
Nguyễn Thái Ngọc Duy797df112019-03-05 19:30:19 +07005347static int diff_opt_word_diff_regex(const struct option *opt,
5348 const char *arg, int unset)
5349{
5350 struct diff_options *options = opt->value;
5351
5352 BUG_ON_OPT_NEG(unset);
5353 if (options->word_diff == DIFF_WORDS_NONE)
5354 options->word_diff = DIFF_WORDS_PLAIN;
5355 options->word_regex = arg;
5356 return 0;
5357}
5358
Junio C Hamano1eb41362021-02-11 11:57:50 -08005359static int diff_opt_rotate_to(const struct option *opt, const char *arg, int unset)
5360{
5361 struct diff_options *options = opt->value;
5362
5363 BUG_ON_OPT_NEG(unset);
5364 if (!strcmp(opt->long_name, "skip-to"))
5365 options->skip_instead_of_rotate = 1;
5366 else
5367 options->skip_instead_of_rotate = 0;
5368 options->rotate_to = arg;
5369 return 0;
5370}
5371
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005372static void prep_parse_options(struct diff_options *options)
5373{
5374 struct option parseopts[] = {
Nguyễn Thái Ngọc Duycc013c22019-01-27 07:35:32 +07005375 OPT_GROUP(N_("Diff output format options")),
5376 OPT_BITOP('p', "patch", &options->output_format,
5377 N_("generate patch"),
5378 DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duye01df7a2019-02-21 18:16:09 +07005379 OPT_BIT_F('s', "no-patch", &options->output_format,
5380 N_("suppress diff output"),
5381 DIFF_FORMAT_NO_OUTPUT, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duycc013c22019-01-27 07:35:32 +07005382 OPT_BITOP('u', NULL, &options->output_format,
5383 N_("generate patch"),
5384 DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 07:35:33 +07005385 OPT_CALLBACK_F('U', "unified", options, N_("<n>"),
5386 N_("generate diffs with <n> lines context"),
Junio C Hamano20aa7c52019-05-30 10:50:44 -07005387 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_unified),
Nguyễn Thái Ngọc Duy7fd9a1b2019-01-27 07:35:34 +07005388 OPT_BOOL('W', "function-context", &options->flags.funccontext,
5389 N_("generate diffs with <n> lines context")),
Nguyễn Thái Ngọc Duyed881482019-01-27 07:35:35 +07005390 OPT_BIT_F(0, "raw", &options->output_format,
5391 N_("generate the diff in raw format"),
5392 DIFF_FORMAT_RAW, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyc659f302019-02-16 18:36:35 +07005393 OPT_BITOP(0, "patch-with-raw", &options->output_format,
5394 N_("synonym for '-p --raw'"),
5395 DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW,
5396 DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duye550f582019-02-21 18:16:06 +07005397 OPT_BITOP(0, "patch-with-stat", &options->output_format,
5398 N_("synonym for '-p --stat'"),
5399 DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT,
5400 DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duye5673622019-02-16 18:36:36 +07005401 OPT_BIT_F(0, "numstat", &options->output_format,
5402 N_("machine friendly --stat"),
5403 DIFF_FORMAT_NUMSTAT, PARSE_OPT_NONEG),
5404 OPT_BIT_F(0, "shortstat", &options->output_format,
5405 N_("output only the last line of --stat"),
5406 DIFF_FORMAT_SHORTSTAT, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy4ce7aab2019-02-21 18:16:03 +07005407 OPT_CALLBACK_F('X', "dirstat", options, N_("<param1,param2>..."),
5408 N_("output the distribution of relative amount of changes for each sub-directory"),
5409 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5410 diff_opt_dirstat),
5411 OPT_CALLBACK_F(0, "cumulative", options, NULL,
5412 N_("synonym for --dirstat=cumulative"),
5413 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
5414 diff_opt_dirstat),
5415 OPT_CALLBACK_F(0, "dirstat-by-file", options, N_("<param1,param2>..."),
5416 N_("synonym for --dirstat=files,param1,param2..."),
5417 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5418 diff_opt_dirstat),
Nguyễn Thái Ngọc Duyfc6af3e2019-02-21 18:16:04 +07005419 OPT_BIT_F(0, "check", &options->output_format,
5420 N_("warn if changes introduce conflict markers or whitespace errors"),
5421 DIFF_FORMAT_CHECKDIFF, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy70a30412019-02-21 18:16:05 +07005422 OPT_BIT_F(0, "summary", &options->output_format,
5423 N_("condensed summary such as creations, renames and mode changes"),
5424 DIFF_FORMAT_SUMMARY, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy0e840e22019-02-21 18:16:07 +07005425 OPT_BIT_F(0, "name-only", &options->output_format,
5426 N_("show only names of changed files"),
5427 DIFF_FORMAT_NAME, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duya2387472019-02-21 18:16:08 +07005428 OPT_BIT_F(0, "name-status", &options->output_format,
5429 N_("show only names and status of changed files"),
5430 DIFF_FORMAT_NAME_STATUS, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07005431 OPT_CALLBACK_F(0, "stat", options, N_("<width>[,<name-width>[,<count>]]"),
5432 N_("generate diffstat"),
5433 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_stat),
5434 OPT_CALLBACK_F(0, "stat-width", options, N_("<width>"),
5435 N_("generate diffstat with a given width"),
5436 PARSE_OPT_NONEG, diff_opt_stat),
5437 OPT_CALLBACK_F(0, "stat-name-width", options, N_("<width>"),
5438 N_("generate diffstat with a given name width"),
5439 PARSE_OPT_NONEG, diff_opt_stat),
5440 OPT_CALLBACK_F(0, "stat-graph-width", options, N_("<width>"),
5441 N_("generate diffstat with a given graph width"),
5442 PARSE_OPT_NONEG, diff_opt_stat),
5443 OPT_CALLBACK_F(0, "stat-count", options, N_("<count>"),
5444 N_("generate diffstat with limited lines"),
5445 PARSE_OPT_NONEG, diff_opt_stat),
Nguyễn Thái Ngọc Duy7d7942b2019-02-21 18:16:11 +07005446 OPT_CALLBACK_F(0, "compact-summary", options, NULL,
5447 N_("generate compact summary in diffstat"),
5448 PARSE_OPT_NOARG, diff_opt_compact_summary),
Nguyễn Thái Ngọc Duy6d9af6f2019-03-05 19:30:12 +07005449 OPT_CALLBACK_F(0, "binary", options, NULL,
5450 N_("output a binary diff that can be applied"),
5451 PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_binary),
Nguyễn Thái Ngọc Duy4fe01672019-03-05 19:30:13 +07005452 OPT_BOOL(0, "full-index", &options->flags.full_index,
5453 N_("show full pre- and post-image object names on the \"index\" lines")),
Nguyễn Thái Ngọc Duy8b81c262019-03-05 19:30:17 +07005454 OPT_COLOR_FLAG(0, "color", &options->use_color,
5455 N_("show colored diff")),
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07005456 OPT_CALLBACK_F(0, "ws-error-highlight", options, N_("<kind>"),
5457 N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
5458 PARSE_OPT_NONEG, diff_opt_ws_error_highlight),
Nguyễn Thái Ngọc Duy1a1eb162019-03-24 15:19:57 +07005459 OPT_SET_INT('z', NULL, &options->line_termination,
5460 N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
5461 0),
Nguyễn Thái Ngọc Duyd8774182019-03-24 15:20:04 +07005462 OPT__ABBREV(&options->abbrev),
Nguyễn Thái Ngọc Duy31fba9d2019-03-24 15:20:05 +07005463 OPT_STRING_F(0, "src-prefix", &options->a_prefix, N_("<prefix>"),
5464 N_("show the given source prefix instead of \"a/\""),
5465 PARSE_OPT_NONEG),
5466 OPT_STRING_F(0, "dst-prefix", &options->b_prefix, N_("<prefix>"),
Jiang Xin8a1569d2019-06-02 23:11:22 +08005467 N_("show the given destination prefix instead of \"b/\""),
Nguyễn Thái Ngọc Duy31fba9d2019-03-24 15:20:05 +07005468 PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy2f81cf92019-03-24 15:20:06 +07005469 OPT_CALLBACK_F(0, "line-prefix", options, N_("<prefix>"),
5470 N_("prepend an additional prefix to every line of output"),
5471 PARSE_OPT_NONEG, diff_opt_line_prefix),
Nguyễn Thái Ngọc Duy11c659d2019-03-24 15:20:07 +07005472 OPT_CALLBACK_F(0, "no-prefix", options, NULL,
5473 N_("do not show any source or destination prefix"),
5474 PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_no_prefix),
Nguyễn Thái Ngọc Duy16ed6c92019-03-24 15:20:08 +07005475 OPT_INTEGER_F(0, "inter-hunk-context", &options->interhunkcontext,
5476 N_("show context between diff hunks up to the specified number of lines"),
5477 PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005478 OPT_CALLBACK_F(0, "output-indicator-new",
5479 &options->output_indicators[OUTPUT_INDICATOR_NEW],
5480 N_("<char>"),
5481 N_("specify the character to indicate a new line instead of '+'"),
5482 PARSE_OPT_NONEG, diff_opt_char),
5483 OPT_CALLBACK_F(0, "output-indicator-old",
5484 &options->output_indicators[OUTPUT_INDICATOR_OLD],
5485 N_("<char>"),
5486 N_("specify the character to indicate an old line instead of '-'"),
5487 PARSE_OPT_NONEG, diff_opt_char),
5488 OPT_CALLBACK_F(0, "output-indicator-context",
5489 &options->output_indicators[OUTPUT_INDICATOR_CONTEXT],
5490 N_("<char>"),
5491 N_("specify the character to indicate a context instead of ' '"),
5492 PARSE_OPT_NONEG, diff_opt_char),
5493
Nguyễn Thái Ngọc Duyced4e172019-02-21 18:16:13 +07005494 OPT_GROUP(N_("Diff rename options")),
5495 OPT_CALLBACK_F('B', "break-rewrites", &options->break_opt, N_("<n>[/<m>]"),
5496 N_("break complete rewrite changes into pairs of delete and create"),
5497 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5498 diff_opt_break_rewrites),
Nguyễn Thái Ngọc Duyf4763082019-02-21 18:16:14 +07005499 OPT_CALLBACK_F('M', "find-renames", options, N_("<n>"),
5500 N_("detect renames"),
5501 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5502 diff_opt_find_renames),
Nguyễn Thái Ngọc Duy1e533292019-02-21 18:16:15 +07005503 OPT_SET_INT_F('D', "irreversible-delete", &options->irreversible_delete,
5504 N_("omit the preimage for deletes"),
5505 1, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy7f648502019-02-21 18:16:16 +07005506 OPT_CALLBACK_F('C', "find-copies", options, N_("<n>"),
5507 N_("detect copies"),
5508 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5509 diff_opt_find_copies),
Nguyễn Thái Ngọc Duybdd47412019-02-21 18:16:17 +07005510 OPT_BOOL(0, "find-copies-harder", &options->flags.find_copies_harder,
5511 N_("use unmodified files as source to find copies")),
Nguyễn Thái Ngọc Duycdc43eb2019-02-21 18:16:18 +07005512 OPT_SET_INT_F(0, "no-renames", &options->detect_rename,
5513 N_("disable rename detection"),
5514 0, PARSE_OPT_NONEG),
5515 OPT_BOOL(0, "rename-empty", &options->flags.rename_empty,
5516 N_("use empty blobs as rename source")),
Nguyễn Thái Ngọc Duy1e9250b2019-03-05 19:30:16 +07005517 OPT_CALLBACK_F(0, "follow", options, NULL,
5518 N_("continue listing the history of a file beyond renames"),
5519 PARSE_OPT_NOARG, diff_opt_follow),
Nguyễn Thái Ngọc Duybffee742019-03-24 15:19:58 +07005520 OPT_INTEGER('l', NULL, &options->rename_limit,
5521 N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")),
Nguyễn Thái Ngọc Duyced4e172019-02-21 18:16:13 +07005522
Nguyễn Thái Ngọc Duy2e75f922019-02-21 18:16:20 +07005523 OPT_GROUP(N_("Diff algorithm options")),
5524 OPT_BIT(0, "minimal", &options->xdl_opts,
5525 N_("produce the smallest possible diff"),
5526 XDF_NEED_MINIMAL),
Nguyễn Thái Ngọc Duy87649a12019-02-21 18:16:21 +07005527 OPT_BIT_F('w', "ignore-all-space", &options->xdl_opts,
5528 N_("ignore whitespace when comparing lines"),
5529 XDF_IGNORE_WHITESPACE, PARSE_OPT_NONEG),
5530 OPT_BIT_F('b', "ignore-space-change", &options->xdl_opts,
5531 N_("ignore changes in amount of whitespace"),
5532 XDF_IGNORE_WHITESPACE_CHANGE, PARSE_OPT_NONEG),
5533 OPT_BIT_F(0, "ignore-space-at-eol", &options->xdl_opts,
5534 N_("ignore changes in whitespace at EOL"),
5535 XDF_IGNORE_WHITESPACE_AT_EOL, PARSE_OPT_NONEG),
5536 OPT_BIT_F(0, "ignore-cr-at-eol", &options->xdl_opts,
5537 N_("ignore carrier-return at the end of line"),
5538 XDF_IGNORE_CR_AT_EOL, PARSE_OPT_NONEG),
5539 OPT_BIT_F(0, "ignore-blank-lines", &options->xdl_opts,
5540 N_("ignore changes whose lines are all blank"),
5541 XDF_IGNORE_BLANK_LINES, PARSE_OPT_NONEG),
Michał Kępień296d4a92020-10-20 08:48:09 +02005542 OPT_CALLBACK_F('I', "ignore-matching-lines", options, N_("<regex>"),
5543 N_("ignore changes whose all lines match <regex>"),
5544 0, diff_opt_ignore_regex),
Nguyễn Thái Ngọc Duy06f77512019-03-05 19:30:07 +07005545 OPT_BIT(0, "indent-heuristic", &options->xdl_opts,
5546 N_("heuristic to shift diff hunk boundaries for easy reading"),
5547 XDF_INDENT_HEURISTIC),
Nguyễn Thái Ngọc Duy31fd6402019-03-05 19:30:08 +07005548 OPT_CALLBACK_F(0, "patience", options, NULL,
5549 N_("generate diff using the \"patience diff\" algorithm"),
5550 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
5551 diff_opt_patience),
Nguyễn Thái Ngọc Duyf1e68ef2019-03-05 19:30:09 +07005552 OPT_BITOP(0, "histogram", &options->xdl_opts,
5553 N_("generate diff using the \"histogram diff\" algorithm"),
5554 XDF_HISTOGRAM_DIFF, XDF_DIFF_ALGORITHM_MASK),
Nguyễn Thái Ngọc Duy10f35b12019-03-05 19:30:10 +07005555 OPT_CALLBACK_F(0, "diff-algorithm", options, N_("<algorithm>"),
5556 N_("choose a diff algorithm"),
5557 PARSE_OPT_NONEG, diff_opt_diff_algorithm),
Nguyễn Thái Ngọc Duydf84a432019-03-05 19:30:11 +07005558 OPT_CALLBACK_F(0, "anchored", options, N_("<text>"),
5559 N_("generate diff using the \"anchored diff\" algorithm"),
5560 PARSE_OPT_NONEG, diff_opt_anchored),
Nguyễn Thái Ngọc Duye9fb39b2019-03-05 19:30:18 +07005561 OPT_CALLBACK_F(0, "word-diff", options, N_("<mode>"),
5562 N_("show word diff, using <mode> to delimit changed words"),
5563 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_word_diff),
Nguyễn Thái Ngọc Duy797df112019-03-05 19:30:19 +07005564 OPT_CALLBACK_F(0, "word-diff-regex", options, N_("<regex>"),
5565 N_("use <regex> to decide what a word is"),
5566 PARSE_OPT_NONEG, diff_opt_word_diff_regex),
Nguyễn Thái Ngọc Duy212db692019-03-05 19:30:20 +07005567 OPT_CALLBACK_F(0, "color-words", options, N_("<regex>"),
5568 N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
5569 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_color_words),
Nguyễn Thái Ngọc Duy59311a92019-03-24 15:20:09 +07005570 OPT_CALLBACK_F(0, "color-moved", options, N_("<mode>"),
Jean-Noël Avila8bcd8f42019-05-17 21:26:19 +02005571 N_("moved lines of code are colored differently"),
Nguyễn Thái Ngọc Duy59311a92019-03-24 15:20:09 +07005572 PARSE_OPT_OPTARG, diff_opt_color_moved),
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005573 OPT_CALLBACK_F(0, "color-moved-ws", options, N_("<mode>"),
5574 N_("how white spaces are ignored in --color-moved"),
Nguyễn Thái Ngọc Duybb987292019-03-24 15:20:11 +07005575 0, diff_opt_color_moved_ws),
Nguyễn Thái Ngọc Duy2e75f922019-02-21 18:16:20 +07005576
Jean-Noël Avila8bcd8f42019-05-17 21:26:19 +02005577 OPT_GROUP(N_("Other diff options")),
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005578 OPT_CALLBACK_F(0, "relative", options, N_("<prefix>"),
5579 N_("when run from subdir, exclude changes outside and show relative paths"),
Laurent Arnoudc28ded82020-05-22 12:46:18 +02005580 PARSE_OPT_OPTARG,
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005581 diff_opt_relative),
Nguyễn Thái Ngọc Duyc37d4c02019-03-05 19:30:14 +07005582 OPT_BOOL('a', "text", &options->flags.text,
5583 N_("treat all files as text")),
Nguyễn Thái Ngọc Duy4f99f292019-03-05 19:30:15 +07005584 OPT_BOOL('R', NULL, &options->flags.reverse_diff,
5585 N_("swap two inputs, reverse the diff")),
Nguyễn Thái Ngọc Duy1086ea02019-03-05 19:30:21 +07005586 OPT_BOOL(0, "exit-code", &options->flags.exit_with_status,
5587 N_("exit with 1 if there were differences, 0 otherwise")),
Nguyễn Thái Ngọc Duy9bbaf1c2019-03-05 19:30:22 +07005588 OPT_BOOL(0, "quiet", &options->flags.quick,
5589 N_("disable all output of the program")),
Nguyễn Thái Ngọc Duy0cda1002019-03-05 19:30:23 +07005590 OPT_BOOL(0, "ext-diff", &options->flags.allow_external,
5591 N_("allow an external diff helper to be executed")),
Nguyễn Thái Ngọc Duy8ab76972019-03-05 19:30:24 +07005592 OPT_CALLBACK_F(0, "textconv", options, NULL,
5593 N_("run external text conversion filters when comparing binary files"),
5594 PARSE_OPT_NOARG, diff_opt_textconv),
Nguyễn Thái Ngọc Duyb680ee12019-03-05 19:30:25 +07005595 OPT_CALLBACK_F(0, "ignore-submodules", options, N_("<when>"),
5596 N_("ignore changes to submodules in the diff generation"),
5597 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5598 diff_opt_ignore_submodules),
Nguyễn Thái Ngọc Duy125dcea2019-03-05 19:30:26 +07005599 OPT_CALLBACK_F(0, "submodule", options, N_("<format>"),
5600 N_("specify how differences in submodules are shown"),
5601 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5602 diff_opt_submodule),
Nguyễn Thái Ngọc Duy96860e42019-03-24 15:19:56 +07005603 OPT_SET_INT_F(0, "ita-invisible-in-index", &options->ita_invisible_in_index,
5604 N_("hide 'git add -N' entries from the index"),
5605 1, PARSE_OPT_NONEG),
5606 OPT_SET_INT_F(0, "ita-visible-in-index", &options->ita_invisible_in_index,
5607 N_("treat 'git add -N' entries as real in the index"),
5608 0, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duya41cfb32019-03-24 15:19:59 +07005609 OPT_CALLBACK_F('S', NULL, options, N_("<string>"),
5610 N_("look for differences that change the number of occurrences of the specified string"),
5611 0, diff_opt_pickaxe_string),
5612 OPT_CALLBACK_F('G', NULL, options, N_("<regex>"),
5613 N_("look for differences that change the number of occurrences of the specified regex"),
5614 0, diff_opt_pickaxe_regex),
Nguyễn Thái Ngọc Duy85f8e882019-03-24 15:20:00 +07005615 OPT_BIT_F(0, "pickaxe-all", &options->pickaxe_opts,
5616 N_("show all changes in the changeset with -S or -G"),
5617 DIFF_PICKAXE_ALL, PARSE_OPT_NONEG),
5618 OPT_BIT_F(0, "pickaxe-regex", &options->pickaxe_opts,
5619 N_("treat <string> in -S as extended POSIX regular expression"),
5620 DIFF_PICKAXE_REGEX, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyf7318142019-03-24 15:20:01 +07005621 OPT_FILENAME('O', NULL, &options->orderfile,
5622 N_("control the order in which files appear in the output")),
Junio C Hamano1eb41362021-02-11 11:57:50 -08005623 OPT_CALLBACK_F(0, "rotate-to", options, N_("<path>"),
5624 N_("show the change in the specified path first"),
5625 PARSE_OPT_NONEG, diff_opt_rotate_to),
5626 OPT_CALLBACK_F(0, "skip-to", options, N_("<path>"),
5627 N_("skip the output to the specified path"),
5628 PARSE_OPT_NONEG, diff_opt_rotate_to),
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07005629 OPT_CALLBACK_F(0, "find-object", options, N_("<object-id>"),
5630 N_("look for differences that change the number of occurrences of the specified object"),
5631 PARSE_OPT_NONEG, diff_opt_find_object),
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07005632 OPT_CALLBACK_F(0, "diff-filter", options, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
5633 N_("select files by diff type"),
5634 PARSE_OPT_NONEG, diff_opt_diff_filter),
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005635 { OPTION_CALLBACK, 0, "output", options, N_("<file>"),
5636 N_("Output to a specific file"),
5637 PARSE_OPT_NONEG, NULL, 0, diff_opt_output },
5638
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005639 OPT_END()
5640 };
5641
5642 ALLOC_ARRAY(options->parseopts, ARRAY_SIZE(parseopts));
5643 memcpy(options->parseopts, parseopts, sizeof(parseopts));
5644}
5645
Duy Nguyena97262c2016-01-21 18:48:44 +07005646int diff_opt_parse(struct diff_options *options,
5647 const char **av, int ac, const char *prefix)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005648{
Duy Nguyena97262c2016-01-21 18:48:44 +07005649 if (!prefix)
5650 prefix = "";
5651
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005652 ac = parse_options(ac, av, prefix, options->parseopts, NULL,
5653 PARSE_OPT_KEEP_DASHDASH |
5654 PARSE_OPT_KEEP_UNKNOWN |
5655 PARSE_OPT_NO_INTERNAL_HELP |
5656 PARSE_OPT_ONE_SHOT |
5657 PARSE_OPT_STOP_AT_NON_OPTION);
5658
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005659 return ac;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005660}
5661
Kevin Ballard10ae7522010-09-27 16:58:25 -07005662int parse_rename_score(const char **cp_p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005663{
5664 unsigned long num, scale;
5665 int ch, dot;
5666 const char *cp = *cp_p;
5667
5668 num = 0;
5669 scale = 1;
5670 dot = 0;
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -04005671 for (;;) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07005672 ch = *cp;
5673 if ( !dot && ch == '.' ) {
5674 scale = 1;
5675 dot = 1;
5676 } else if ( ch == '%' ) {
5677 scale = dot ? scale*100 : 100;
5678 cp++; /* % is always at the end */
5679 break;
5680 } else if ( ch >= '0' && ch <= '9' ) {
5681 if ( scale < 100000 ) {
5682 scale *= 10;
5683 num = (num*10) + (ch-'0');
5684 }
5685 } else {
5686 break;
5687 }
5688 cp++;
5689 }
5690 *cp_p = cp;
5691
5692 /* user says num divided by scale and we say internally that
5693 * is MAX_SCORE * num / scale.
5694 */
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05005695 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
Junio C Hamano6973dca2006-04-21 23:57:45 -07005696}
5697
Junio C Hamano6973dca2006-04-21 23:57:45 -07005698struct diff_queue_struct diff_queued_diff;
5699
5700void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
5701{
Dmitry S. Dolzhenko4c960a42014-03-04 02:31:53 +04005702 ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005703 queue->queue[queue->nr++] = dp;
5704}
5705
5706struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
5707 struct diff_filespec *one,
5708 struct diff_filespec *two)
5709{
Junio C Hamanoef677682006-08-03 12:01:01 -07005710 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
Junio C Hamano6973dca2006-04-21 23:57:45 -07005711 dp->one = one;
5712 dp->two = two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005713 if (queue)
5714 diff_q(queue, dp);
5715 return dp;
5716}
5717
5718void diff_free_filepair(struct diff_filepair *p)
5719{
Linus Torvalds9fb88412007-10-25 11:19:10 -07005720 free_filespec(p->one);
5721 free_filespec(p->two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005722 free(p);
5723}
5724
Jeff Kingd6cece52016-10-20 02:20:07 -04005725const char *diff_aligned_abbrev(const struct object_id *oid, int len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005726{
5727 int abblen;
5728 const char *abbrev;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005729
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005730 /* Do we want all 40 hex characters? */
brian m. carlson02afca12018-07-16 01:28:05 +00005731 if (len == the_hash_algo->hexsz)
Jeff Kingd6cece52016-10-20 02:20:07 -04005732 return oid_to_hex(oid);
5733
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005734 /* An abbreviated value is fine, possibly followed by an ellipsis. */
Jeff King4f036662016-10-20 02:21:25 -04005735 abbrev = diff_abbrev_oid(oid, len);
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005736
5737 if (!print_sha1_ellipsis())
5738 return abbrev;
5739
Junio C Hamano6973dca2006-04-21 23:57:45 -07005740 abblen = strlen(abbrev);
Junio C Hamanod709f1f2016-09-30 10:42:05 -07005741
5742 /*
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005743 * In well-behaved cases, where the abbreviated result is the
Junio C Hamanod709f1f2016-09-30 10:42:05 -07005744 * same as the requested length, append three dots after the
5745 * abbreviation (hence the whole logic is limited to the case
5746 * where abblen < 37); when the actual abbreviated result is a
5747 * bit longer than the requested length, we reduce the number
5748 * of dots so that they match the well-behaved ones. However,
5749 * if the actual abbreviation is longer than the requested
5750 * length by more than three, we give up on aligning, and add
5751 * three dots anyway, to indicate that the output is not the
5752 * full object name. Yes, this may be suboptimal, but this
5753 * appears only in "diff --raw --abbrev" output and it is not
5754 * worth the effort to change it now. Note that this would
5755 * likely to work fine when the automatic sizing of default
5756 * abbreviation length is used--we would be fed -1 in "len" in
5757 * that case, and will end up always appending three-dots, but
5758 * the automatic sizing is supposed to give abblen that ensures
5759 * uniqueness across all objects (statistically speaking).
5760 */
brian m. carlson02afca12018-07-16 01:28:05 +00005761 if (abblen < the_hash_algo->hexsz - 3) {
brian m. carlsondc015052017-03-26 16:01:24 +00005762 static char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano6973dca2006-04-21 23:57:45 -07005763 if (len < abblen && abblen <= len + 2)
Jeff King5096d492015-09-24 17:06:08 -04005764 xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");
Junio C Hamano6973dca2006-04-21 23:57:45 -07005765 else
Jeff King5096d492015-09-24 17:06:08 -04005766 xsnprintf(hex, sizeof(hex), "%s...", abbrev);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005767 return hex;
5768 }
Jeff Kingd6cece52016-10-20 02:20:07 -04005769
5770 return oid_to_hex(oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005771}
5772
Pierre Habouzit663af342007-09-20 00:42:15 +02005773static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005774{
Pierre Habouzit663af342007-09-20 00:42:15 +02005775 int line_termination = opt->line_termination;
5776 int inter_name_termination = line_termination ? '\t' : '\0';
Timo Hirvonenc6744342006-06-24 20:21:53 +03005777
John Keeping30997bb2013-02-07 20:15:27 +00005778 fprintf(opt->file, "%s", diff_line_prefix(opt));
Pierre Habouzit663af342007-09-20 00:42:15 +02005779 if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005780 fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
Jeff Kingd6cece52016-10-20 02:20:07 -04005781 diff_aligned_abbrev(&p->one->oid, opt->abbrev));
brian m. carlsona0d12c42016-06-24 23:09:23 +00005782 fprintf(opt->file, "%s ",
Jeff Kingd6cece52016-10-20 02:20:07 -04005783 diff_aligned_abbrev(&p->two->oid, opt->abbrev));
Pierre Habouzit663af342007-09-20 00:42:15 +02005784 }
5785 if (p->score) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005786 fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),
5787 inter_name_termination);
Pierre Habouzit663af342007-09-20 00:42:15 +02005788 } else {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005789 fprintf(opt->file, "%c%c", p->status, inter_name_termination);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005790 }
5791
Junio C Hamanocd676a52008-02-12 14:26:02 -08005792 if (p->status == DIFF_STATUS_COPIED ||
5793 p->status == DIFF_STATUS_RENAMED) {
5794 const char *name_a, *name_b;
5795 name_a = p->one->path;
5796 name_b = p->two->path;
5797 strip_prefix(opt->prefix_length, &name_a, &name_b);
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005798 write_name_quoted(name_a, opt->file, inter_name_termination);
5799 write_name_quoted(name_b, opt->file, line_termination);
Pierre Habouzit663af342007-09-20 00:42:15 +02005800 } else {
Junio C Hamanocd676a52008-02-12 14:26:02 -08005801 const char *name_a, *name_b;
5802 name_a = p->one->mode ? p->one->path : p->two->path;
5803 name_b = NULL;
5804 strip_prefix(opt->prefix_length, &name_a, &name_b);
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005805 write_name_quoted(name_a, opt->file, line_termination);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005806 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07005807}
5808
5809int diff_unmodified_pair(struct diff_filepair *p)
5810{
5811 /* This function is written stricter than necessary to support
5812 * the currently implemented transformers, but the idea is to
5813 * let transformers to produce diff_filepairs any way they want,
5814 * and filter and clean them up here before producing the output.
5815 */
Pierre Habouzit663af342007-09-20 00:42:15 +02005816 struct diff_filespec *one = p->one, *two = p->two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005817
5818 if (DIFF_PAIR_UNMERGED(p))
5819 return 0; /* unmerged is interesting */
5820
Junio C Hamano6973dca2006-04-21 23:57:45 -07005821 /* deletion, addition, mode or type change
5822 * and rename are all interesting.
5823 */
5824 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
5825 DIFF_PAIR_MODE_CHANGED(p) ||
5826 strcmp(one->path, two->path))
5827 return 0;
5828
5829 /* both are valid and point at the same path. that is, we are
5830 * dealing with a change.
5831 */
brian m. carlson41c95602016-06-24 23:09:24 +00005832 if (one->oid_valid && two->oid_valid &&
Jeff King4a7e27e2018-08-28 17:22:40 -04005833 oideq(&one->oid, &two->oid) &&
Jens Lehmann85adbf22010-03-12 22:23:52 +01005834 !one->dirty_submodule && !two->dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005835 return 1; /* no change */
brian m. carlson41c95602016-06-24 23:09:24 +00005836 if (!one->oid_valid && !two->oid_valid)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005837 return 1; /* both look at the same file on the filesystem. */
5838 return 0;
5839}
5840
5841static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
5842{
5843 if (diff_unmodified_pair(p))
5844 return;
5845
5846 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5847 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
5848 return; /* no tree diffs in patch format */
5849
5850 run_diff(p, o);
5851}
5852
5853static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
5854 struct diffstat_t *diffstat)
5855{
5856 if (diff_unmodified_pair(p))
5857 return;
5858
5859 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5860 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Yann Dirsonc3fced62010-10-24 23:03:10 +02005861 return; /* no useful stat for tree diffs */
Junio C Hamano6973dca2006-04-21 23:57:45 -07005862
5863 run_diffstat(p, o, diffstat);
5864}
5865
Johannes Schindelin88246892006-05-20 23:43:13 +02005866static void diff_flush_checkdiff(struct diff_filepair *p,
5867 struct diff_options *o)
5868{
5869 if (diff_unmodified_pair(p))
5870 return;
5871
5872 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5873 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Yann Dirsonc3fced62010-10-24 23:03:10 +02005874 return; /* nothing to check in tree diffs */
Johannes Schindelin88246892006-05-20 23:43:13 +02005875
5876 run_checkdiff(p, o);
5877}
5878
Junio C Hamano6973dca2006-04-21 23:57:45 -07005879int diff_queue_is_empty(void)
5880{
5881 struct diff_queue_struct *q = &diff_queued_diff;
5882 int i;
5883 for (i = 0; i < q->nr; i++)
5884 if (!diff_unmodified_pair(q->queue[i]))
5885 return 0;
5886 return 1;
5887}
5888
5889#if DIFF_DEBUG
5890void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
5891{
5892 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
5893 x, one ? one : "",
5894 s->path,
5895 DIFF_FILE_VALID(s) ? "valid" : "invalid",
5896 s->mode,
brian m. carlson41c95602016-06-24 23:09:24 +00005897 s->oid_valid ? oid_to_hex(&s->oid) : "");
Jeff King428d52a2014-01-16 20:21:59 -05005898 fprintf(stderr, "queue[%d] %s size %lu\n",
Junio C Hamano6973dca2006-04-21 23:57:45 -07005899 x, one ? one : "",
Jeff King428d52a2014-01-16 20:21:59 -05005900 s->size);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005901}
5902
5903void diff_debug_filepair(const struct diff_filepair *p, int i)
5904{
5905 diff_debug_filespec(p->one, i, "one");
5906 diff_debug_filespec(p->two, i, "two");
Linus Torvalds64479712007-10-25 11:20:56 -07005907 fprintf(stderr, "score %d, status %c rename_used %d broken %d\n",
Junio C Hamano6973dca2006-04-21 23:57:45 -07005908 p->score, p->status ? p->status : '?',
Linus Torvalds64479712007-10-25 11:20:56 -07005909 p->one->rename_used, p->broken_pair);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005910}
5911
5912void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
5913{
5914 int i;
5915 if (msg)
5916 fprintf(stderr, "%s\n", msg);
5917 fprintf(stderr, "q->nr = %d\n", q->nr);
5918 for (i = 0; i < q->nr; i++) {
5919 struct diff_filepair *p = q->queue[i];
5920 diff_debug_filepair(p, i);
5921 }
5922}
5923#endif
5924
5925static void diff_resolve_rename_copy(void)
5926{
Linus Torvalds64479712007-10-25 11:20:56 -07005927 int i;
5928 struct diff_filepair *p;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005929 struct diff_queue_struct *q = &diff_queued_diff;
5930
5931 diff_debug_queue("resolve-rename-copy", q);
5932
5933 for (i = 0; i < q->nr; i++) {
5934 p = q->queue[i];
5935 p->status = 0; /* undecided */
5936 if (DIFF_PAIR_UNMERGED(p))
5937 p->status = DIFF_STATUS_UNMERGED;
5938 else if (!DIFF_FILE_VALID(p->one))
5939 p->status = DIFF_STATUS_ADDED;
5940 else if (!DIFF_FILE_VALID(p->two))
5941 p->status = DIFF_STATUS_DELETED;
5942 else if (DIFF_PAIR_TYPE_CHANGED(p))
5943 p->status = DIFF_STATUS_TYPE_CHANGED;
5944
5945 /* from this point on, we are dealing with a pair
5946 * whose both sides are valid and of the same type, i.e.
5947 * either in-place edit or rename/copy edit.
5948 */
5949 else if (DIFF_PAIR_RENAME(p)) {
Linus Torvalds64479712007-10-25 11:20:56 -07005950 /*
5951 * A rename might have re-connected a broken
5952 * pair up, causing the pathnames to be the
5953 * same again. If so, that's not a rename at
5954 * all, just a modification..
5955 *
5956 * Otherwise, see if this source was used for
5957 * multiple renames, in which case we decrement
5958 * the count, and call it a copy.
Junio C Hamano6973dca2006-04-21 23:57:45 -07005959 */
Linus Torvalds64479712007-10-25 11:20:56 -07005960 if (!strcmp(p->one->path, p->two->path))
5961 p->status = DIFF_STATUS_MODIFIED;
5962 else if (--p->one->rename_used > 0)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005963 p->status = DIFF_STATUS_COPIED;
Linus Torvalds64479712007-10-25 11:20:56 -07005964 else
Junio C Hamano6973dca2006-04-21 23:57:45 -07005965 p->status = DIFF_STATUS_RENAMED;
5966 }
Jeff King9001dc22018-08-28 17:22:48 -04005967 else if (!oideq(&p->one->oid, &p->two->oid) ||
Johannes Schindelind516c2d2007-02-22 21:50:10 +01005968 p->one->mode != p->two->mode ||
Jens Lehmann85adbf22010-03-12 22:23:52 +01005969 p->one->dirty_submodule ||
5970 p->two->dirty_submodule ||
brian m. carlsona0d12c42016-06-24 23:09:23 +00005971 is_null_oid(&p->one->oid))
Junio C Hamano6973dca2006-04-21 23:57:45 -07005972 p->status = DIFF_STATUS_MODIFIED;
5973 else {
5974 /* This is a "no-change" entry and should not
5975 * happen anymore, but prepare for broken callers.
5976 */
5977 error("feeding unmodified %s to diffcore",
5978 p->one->path);
5979 p->status = DIFF_STATUS_UNKNOWN;
5980 }
5981 }
5982 diff_debug_queue("resolve-rename-copy done", q);
5983}
5984
Timo Hirvonenc6744342006-06-24 20:21:53 +03005985static int check_pair_status(struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005986{
Junio C Hamano6973dca2006-04-21 23:57:45 -07005987 switch (p->status) {
5988 case DIFF_STATUS_UNKNOWN:
Timo Hirvonenc6744342006-06-24 20:21:53 +03005989 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005990 case 0:
5991 die("internal error in diff-resolve-rename-copy");
Junio C Hamano6973dca2006-04-21 23:57:45 -07005992 default:
Timo Hirvonenc6744342006-06-24 20:21:53 +03005993 return 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005994 }
5995}
5996
Timo Hirvonenc6744342006-06-24 20:21:53 +03005997static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
5998{
5999 int fmt = opt->output_format;
6000
6001 if (fmt & DIFF_FORMAT_CHECKDIFF)
6002 diff_flush_checkdiff(p, opt);
6003 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
6004 diff_flush_raw(p, opt);
Junio C Hamanocd676a52008-02-12 14:26:02 -08006005 else if (fmt & DIFF_FORMAT_NAME) {
6006 const char *name_a, *name_b;
6007 name_a = p->two->path;
6008 name_b = NULL;
6009 strip_prefix(opt->prefix_length, &name_a, &name_b);
Jeff Kingf5022b52017-02-08 15:31:15 -05006010 fprintf(opt->file, "%s", diff_line_prefix(opt));
Daniel Barkalowc0c77732008-03-09 22:43:39 -04006011 write_name_quoted(name_a, opt->file, opt->line_termination);
Junio C Hamanocd676a52008-02-12 14:26:02 -08006012 }
Timo Hirvonenc6744342006-06-24 20:21:53 +03006013}
6014
Stefan Beller146fdb02017-06-29 17:07:05 -07006015static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs)
Sean4bbd2612006-05-14 08:13:49 -04006016{
Stefan Beller146fdb02017-06-29 17:07:05 -07006017 struct strbuf sb = STRBUF_INIT;
Sean4bbd2612006-05-14 08:13:49 -04006018 if (fs->mode)
Stefan Beller146fdb02017-06-29 17:07:05 -07006019 strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode);
Sean4bbd2612006-05-14 08:13:49 -04006020 else
Stefan Beller146fdb02017-06-29 17:07:05 -07006021 strbuf_addf(&sb, " %s ", newdelete);
6022
6023 quote_c_style(fs->path, &sb, NULL, 0);
6024 strbuf_addch(&sb, '\n');
6025 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6026 sb.buf, sb.len, 0);
6027 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04006028}
6029
Stefan Beller146fdb02017-06-29 17:07:05 -07006030static void show_mode_change(struct diff_options *opt, struct diff_filepair *p,
6031 int show_name)
Sean4bbd2612006-05-14 08:13:49 -04006032{
6033 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
Stefan Beller146fdb02017-06-29 17:07:05 -07006034 struct strbuf sb = STRBUF_INIT;
6035 strbuf_addf(&sb, " mode change %06o => %06o",
6036 p->one->mode, p->two->mode);
Alexandre Julliard0d26a642007-02-10 15:37:48 +01006037 if (show_name) {
Stefan Beller146fdb02017-06-29 17:07:05 -07006038 strbuf_addch(&sb, ' ');
6039 quote_c_style(p->two->path, &sb, NULL, 0);
Alexandre Julliard0d26a642007-02-10 15:37:48 +01006040 }
Stefan Beller58aaced2017-09-27 15:51:26 -07006041 strbuf_addch(&sb, '\n');
Stefan Beller146fdb02017-06-29 17:07:05 -07006042 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6043 sb.buf, sb.len, 0);
6044 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04006045 }
6046}
6047
Stefan Beller146fdb02017-06-29 17:07:05 -07006048static void show_rename_copy(struct diff_options *opt, const char *renamecopy,
6049 struct diff_filepair *p)
Sean4bbd2612006-05-14 08:13:49 -04006050{
Stefan Beller146fdb02017-06-29 17:07:05 -07006051 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07006052 struct strbuf names = STRBUF_INIT;
6053
6054 pprint_rename(&names, p->one->path, p->two->path);
Stefan Beller146fdb02017-06-29 17:07:05 -07006055 strbuf_addf(&sb, " %s %s (%d%%)\n",
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07006056 renamecopy, names.buf, similarity_index(p));
6057 strbuf_release(&names);
Stefan Beller146fdb02017-06-29 17:07:05 -07006058 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6059 sb.buf, sb.len, 0);
6060 show_mode_change(opt, p, 0);
Rene Scharfe348eda22017-08-30 19:49:42 +02006061 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04006062}
6063
Bo Yang7be57612010-05-26 15:23:54 +08006064static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
Sean4bbd2612006-05-14 08:13:49 -04006065{
6066 switch(p->status) {
6067 case DIFF_STATUS_DELETED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006068 show_file_mode_name(opt, "delete", p->one);
Sean4bbd2612006-05-14 08:13:49 -04006069 break;
6070 case DIFF_STATUS_ADDED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006071 show_file_mode_name(opt, "create", p->two);
Sean4bbd2612006-05-14 08:13:49 -04006072 break;
6073 case DIFF_STATUS_COPIED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006074 show_rename_copy(opt, "copy", p);
Sean4bbd2612006-05-14 08:13:49 -04006075 break;
6076 case DIFF_STATUS_RENAMED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006077 show_rename_copy(opt, "rename", p);
Sean4bbd2612006-05-14 08:13:49 -04006078 break;
6079 default:
6080 if (p->score) {
Stefan Beller146fdb02017-06-29 17:07:05 -07006081 struct strbuf sb = STRBUF_INIT;
6082 strbuf_addstr(&sb, " rewrite ");
6083 quote_c_style(p->two->path, &sb, NULL, 0);
6084 strbuf_addf(&sb, " (%d%%)\n", similarity_index(p));
6085 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6086 sb.buf, sb.len, 0);
Rene Scharfefa842d82017-08-30 19:49:41 +02006087 strbuf_release(&sb);
Pierre Habouzit663af342007-09-20 00:42:15 +02006088 }
Stefan Beller146fdb02017-06-29 17:07:05 -07006089 show_mode_change(opt, p, !p->score);
Sean4bbd2612006-05-14 08:13:49 -04006090 break;
6091 }
6092}
6093
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006094struct patch_id_t {
brian m. carlson36261e42019-08-18 20:04:03 +00006095 git_hash_ctx *ctx;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006096 int patchlen;
6097};
6098
6099static int remove_space(char *line, int len)
6100{
6101 int i;
Pierre Habouzit663af342007-09-20 00:42:15 +02006102 char *dst = line;
6103 unsigned char c;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006104
Pierre Habouzit663af342007-09-20 00:42:15 +02006105 for (i = 0; i < len; i++)
6106 if (!isspace((c = line[i])))
6107 *dst++ = c;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006108
Pierre Habouzit663af342007-09-20 00:42:15 +02006109 return dst - line;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006110}
6111
brian m. carlson36261e42019-08-18 20:04:03 +00006112void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx)
Stephen Boyda8f68552019-04-26 16:51:57 -07006113{
6114 unsigned char hash[GIT_MAX_RAWSZ];
6115 unsigned short carry = 0;
6116 int i;
6117
brian m. carlson36261e42019-08-18 20:04:03 +00006118 the_hash_algo->final_fn(hash, ctx);
6119 the_hash_algo->init_fn(ctx);
Stephen Boyda8f68552019-04-26 16:51:57 -07006120 /* 20-byte sum, with carry */
brian m. carlson36261e42019-08-18 20:04:03 +00006121 for (i = 0; i < the_hash_algo->rawsz; ++i) {
Stephen Boyda8f68552019-04-26 16:51:57 -07006122 carry += result->hash[i] + hash[i];
6123 result->hash[i] = carry;
6124 carry >>= 8;
6125 }
6126}
6127
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02006128static int patch_id_consume(void *priv, char *line, unsigned long len)
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006129{
6130 struct patch_id_t *data = priv;
6131 int new_len;
6132
René Scharfe82a62012020-08-19 00:08:54 +02006133 if (len > 12 && starts_with(line, "\\ "))
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02006134 return 0;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006135 new_len = remove_space(line, len);
6136
brian m. carlson36261e42019-08-18 20:04:03 +00006137 the_hash_algo->update_fn(data->ctx, line, new_len);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006138 data->patchlen += new_len;
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02006139 return 0;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006140}
6141
brian m. carlson36261e42019-08-18 20:04:03 +00006142static void patch_id_add_string(git_hash_ctx *ctx, const char *str)
Jeff King977db6b2017-03-30 14:26:05 -04006143{
brian m. carlson36261e42019-08-18 20:04:03 +00006144 the_hash_algo->update_fn(ctx, str, strlen(str));
Jeff King977db6b2017-03-30 14:26:05 -04006145}
6146
brian m. carlson36261e42019-08-18 20:04:03 +00006147static void patch_id_add_mode(git_hash_ctx *ctx, unsigned mode)
Jeff King977db6b2017-03-30 14:26:05 -04006148{
6149 /* large enough for 2^32 in octal */
6150 char buf[12];
6151 int len = xsnprintf(buf, sizeof(buf), "%06o", mode);
brian m. carlson36261e42019-08-18 20:04:03 +00006152 the_hash_algo->update_fn(ctx, buf, len);
Jeff King977db6b2017-03-30 14:26:05 -04006153}
6154
Stephen Boyda8f68552019-04-26 16:51:57 -07006155/* returns 0 upon success, and writes result into oid */
6156static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only, int stable)
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006157{
6158 struct diff_queue_struct *q = &diff_queued_diff;
6159 int i;
brian m. carlson36261e42019-08-18 20:04:03 +00006160 git_hash_ctx ctx;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006161 struct patch_id_t data;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006162
brian m. carlson36261e42019-08-18 20:04:03 +00006163 the_hash_algo->init_fn(&ctx);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006164 memset(&data, 0, sizeof(struct patch_id_t));
6165 data.ctx = &ctx;
Stephen Boyda8f68552019-04-26 16:51:57 -07006166 oidclr(oid);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006167
6168 for (i = 0; i < q->nr; i++) {
6169 xpparam_t xpp;
6170 xdemitconf_t xecfg;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006171 mmfile_t mf1, mf2;
6172 struct diff_filepair *p = q->queue[i];
6173 int len1, len2;
6174
Brian Downing9ccd0a82008-10-25 15:30:37 +02006175 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01006176 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006177 if (p->status == 0)
6178 return error("internal diff status error");
6179 if (p->status == DIFF_STATUS_UNKNOWN)
6180 continue;
6181 if (diff_unmodified_pair(p))
6182 continue;
6183 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
6184 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
6185 continue;
6186 if (DIFF_PAIR_UNMERGED(p))
6187 continue;
6188
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02006189 diff_fill_oid_info(p->one, options->repo->index);
6190 diff_fill_oid_info(p->two, options->repo->index);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006191
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006192 len1 = remove_space(p->one->path, strlen(p->one->path));
6193 len2 = remove_space(p->two->path, strlen(p->two->path));
Jeff King977db6b2017-03-30 14:26:05 -04006194 patch_id_add_string(&ctx, "diff--git");
6195 patch_id_add_string(&ctx, "a/");
brian m. carlson36261e42019-08-18 20:04:03 +00006196 the_hash_algo->update_fn(&ctx, p->one->path, len1);
Jeff King977db6b2017-03-30 14:26:05 -04006197 patch_id_add_string(&ctx, "b/");
brian m. carlson36261e42019-08-18 20:04:03 +00006198 the_hash_algo->update_fn(&ctx, p->two->path, len2);
Jeff King977db6b2017-03-30 14:26:05 -04006199
6200 if (p->one->mode == 0) {
6201 patch_id_add_string(&ctx, "newfilemode");
6202 patch_id_add_mode(&ctx, p->two->mode);
6203 patch_id_add_string(&ctx, "---/dev/null");
6204 patch_id_add_string(&ctx, "+++b/");
brian m. carlson36261e42019-08-18 20:04:03 +00006205 the_hash_algo->update_fn(&ctx, p->two->path, len2);
Jeff King977db6b2017-03-30 14:26:05 -04006206 } else if (p->two->mode == 0) {
6207 patch_id_add_string(&ctx, "deletedfilemode");
6208 patch_id_add_mode(&ctx, p->one->mode);
6209 patch_id_add_string(&ctx, "---a/");
brian m. carlson36261e42019-08-18 20:04:03 +00006210 the_hash_algo->update_fn(&ctx, p->one->path, len1);
Jeff King977db6b2017-03-30 14:26:05 -04006211 patch_id_add_string(&ctx, "+++/dev/null");
6212 } else {
6213 patch_id_add_string(&ctx, "---a/");
brian m. carlson36261e42019-08-18 20:04:03 +00006214 the_hash_algo->update_fn(&ctx, p->one->path, len1);
Jeff King977db6b2017-03-30 14:26:05 -04006215 patch_id_add_string(&ctx, "+++b/");
brian m. carlson36261e42019-08-18 20:04:03 +00006216 the_hash_algo->update_fn(&ctx, p->two->path, len2);
Jeff King977db6b2017-03-30 14:26:05 -04006217 }
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006218
Kevin Willford3e8e32c2016-07-29 12:19:19 -04006219 if (diff_header_only)
6220 continue;
6221
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006222 if (fill_mmfile(options->repo, &mf1, p->one) < 0 ||
6223 fill_mmfile(options->repo, &mf2, p->two) < 0)
Kevin Willford3e8e32c2016-07-29 12:19:19 -04006224 return error("unable to read files to diff");
6225
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006226 if (diff_filespec_is_binary(options->repo, p->one) ||
6227 diff_filespec_is_binary(options->repo, p->two)) {
brian m. carlson36261e42019-08-18 20:04:03 +00006228 the_hash_algo->update_fn(&ctx, oid_to_hex(&p->one->oid),
6229 the_hash_algo->hexsz);
6230 the_hash_algo->update_fn(&ctx, oid_to_hex(&p->two->oid),
6231 the_hash_algo->hexsz);
Clemens Buchacher34597c12010-08-15 09:20:43 +02006232 continue;
6233 }
6234
René Scharfe582aa002010-05-02 15:04:41 +02006235 xpp.flags = 0;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006236 xecfg.ctxlen = 3;
Ævar Arnfjörð Bjarmason5d934602021-04-12 19:15:29 +02006237 xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
6238 if (xdi_diff_outf(&mf1, &mf2, NULL,
Jeff Kingb1357392018-11-02 02:36:36 -04006239 patch_id_consume, &data, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04006240 return error("unable to generate patch-id diff for %s",
6241 p->one->path);
Stephen Boyda8f68552019-04-26 16:51:57 -07006242
6243 if (stable)
6244 flush_one_hunk(oid, &ctx);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006245 }
6246
Stephen Boyda8f68552019-04-26 16:51:57 -07006247 if (!stable)
brian m. carlson36261e42019-08-18 20:04:03 +00006248 the_hash_algo->final_fn(oid->hash, &ctx);
Stephen Boyda8f68552019-04-26 16:51:57 -07006249
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006250 return 0;
6251}
6252
Stephen Boyda8f68552019-04-26 16:51:57 -07006253int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only, int stable)
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006254{
6255 struct diff_queue_struct *q = &diff_queued_diff;
6256 int i;
Stephen Boyda8f68552019-04-26 16:51:57 -07006257 int result = diff_get_patch_id(options, oid, diff_header_only, stable);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006258
6259 for (i = 0; i < q->nr; i++)
6260 diff_free_filepair(q->queue[i]);
6261
6262 free(q->queue);
Bo Yang9ca5df92010-05-06 21:52:27 -07006263 DIFF_QUEUE_CLEAR(q);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006264
6265 return result;
6266}
6267
Timo Hirvonen946c3782006-06-27 15:09:17 +03006268static int is_summary_empty(const struct diff_queue_struct *q)
6269{
6270 int i;
6271
6272 for (i = 0; i < q->nr; i++) {
6273 const struct diff_filepair *p = q->queue[i];
6274
6275 switch (p->status) {
6276 case DIFF_STATUS_DELETED:
6277 case DIFF_STATUS_ADDED:
6278 case DIFF_STATUS_COPIED:
6279 case DIFF_STATUS_RENAMED:
6280 return 0;
6281 default:
6282 if (p->score)
6283 return 0;
6284 if (p->one->mode && p->two->mode &&
6285 p->one->mode != p->two->mode)
6286 return 0;
6287 break;
6288 }
6289 }
6290 return 1;
6291}
6292
Junio C Hamanof31027c2011-01-06 13:50:06 -08006293static const char rename_limit_warning[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00006294N_("inexact rename detection was skipped due to too many files.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08006295
6296static const char degrade_cc_to_c_warning[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00006297N_("only found copies from modified paths due to too many files.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08006298
6299static const char rename_limit_advice[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00006300N_("you may want to set your %s variable to at least "
6301 "%d and retry the command.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08006302
6303void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)
6304{
Nguyễn Thái Ngọc Duy4e056c92018-01-16 16:23:49 +07006305 fflush(stdout);
Junio C Hamanof31027c2011-01-06 13:50:06 -08006306 if (degraded_cc)
Vasco Almeidadb424972016-10-17 13:15:29 +00006307 warning(_(degrade_cc_to_c_warning));
Junio C Hamanof31027c2011-01-06 13:50:06 -08006308 else if (needed)
Vasco Almeidadb424972016-10-17 13:15:29 +00006309 warning(_(rename_limit_warning));
Junio C Hamanof31027c2011-01-06 13:50:06 -08006310 else
6311 return;
Elijah Newren9f7e4bf2017-11-13 12:15:59 -08006312 if (0 < needed)
Vasco Almeidadb424972016-10-17 13:15:29 +00006313 warning(_(rename_limit_advice), varname, needed);
Junio C Hamanof31027c2011-01-06 13:50:06 -08006314}
6315
Stefan Bellerec331502017-06-29 17:06:48 -07006316static void diff_flush_patch_all_file_pairs(struct diff_options *o)
6317{
6318 int i;
Stefan Bellere6e045f2017-06-29 17:07:06 -07006319 static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT;
Stefan Bellerec331502017-06-29 17:06:48 -07006320 struct diff_queue_struct *q = &diff_queued_diff;
Stefan Beller091f8e22017-06-29 17:06:53 -07006321
6322 if (WSEH_NEW & WS_RULE_MASK)
Johannes Schindelin033abf92018-05-02 11:38:39 +02006323 BUG("WS rules bit mask overlaps with diff symbol flags");
Stefan Beller091f8e22017-06-29 17:06:53 -07006324
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006325 if (o->color_moved)
6326 o->emitted_symbols = &esm;
Stefan Bellere6e045f2017-06-29 17:07:06 -07006327
Stefan Bellerec331502017-06-29 17:06:48 -07006328 for (i = 0; i < q->nr; i++) {
6329 struct diff_filepair *p = q->queue[i];
6330 if (check_pair_status(p))
6331 diff_flush_patch(p, o);
6332 }
Stefan Bellere6e045f2017-06-29 17:07:06 -07006333
6334 if (o->emitted_symbols) {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006335 if (o->color_moved) {
6336 struct hashmap add_lines, del_lines;
6337
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07006338 if (o->color_moved_ws_handling &
6339 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
6340 o->color_moved_ws_handling |= XDF_IGNORE_WHITESPACE;
6341
Stefan Belleree1df662018-07-16 16:05:38 -07006342 hashmap_init(&del_lines, moved_entry_cmp, o, 0);
6343 hashmap_init(&add_lines, moved_entry_cmp, o, 0);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006344
6345 add_lines_to_move_detection(o, &add_lines, &del_lines);
6346 mark_color_as_moved(o, &add_lines, &del_lines);
Stefan Beller86b452e2017-06-30 13:53:09 -07006347 if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
6348 dim_moved_lines(o);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006349
Elijah Newren6da1a252020-11-02 18:55:05 +00006350 hashmap_clear_and_free(&add_lines, struct moved_entry,
Eric Wongc8e424c2019-10-06 23:30:40 +00006351 ent);
Elijah Newren6da1a252020-11-02 18:55:05 +00006352 hashmap_clear_and_free(&del_lines, struct moved_entry,
Eric Wongc8e424c2019-10-06 23:30:40 +00006353 ent);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006354 }
6355
Stefan Bellere6e045f2017-06-29 17:07:06 -07006356 for (i = 0; i < esm.nr; i++)
6357 emit_diff_symbol_from_struct(o, &esm.buf[i]);
6358
6359 for (i = 0; i < esm.nr; i++)
6360 free((void *)esm.buf[i].line);
Jeff King48edf3a2019-01-24 07:32:41 -05006361 esm.nr = 0;
6362
6363 o->emitted_symbols = NULL;
Stefan Bellere6e045f2017-06-29 17:07:06 -07006364 }
Stefan Bellerec331502017-06-29 17:06:48 -07006365}
6366
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006367static void diff_free_file(struct diff_options *options)
6368{
6369 if (options->close_file)
6370 fclose(options->file);
6371}
6372
Ævar Arnfjörð Bjarmasonc45dc9c2021-02-11 11:45:35 +01006373static void diff_free_ignore_regex(struct diff_options *options)
6374{
6375 int i;
6376
6377 for (i = 0; i < options->ignore_regex_nr; i++) {
6378 regfree(options->ignore_regex[i]);
6379 free(options->ignore_regex[i]);
6380 }
6381 free(options->ignore_regex);
6382}
6383
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006384void diff_free(struct diff_options *options)
6385{
6386 if (options->no_free)
6387 return;
6388
6389 diff_free_file(options);
Ævar Arnfjörð Bjarmasonc45dc9c2021-02-11 11:45:35 +01006390 diff_free_ignore_regex(options);
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006391}
6392
Junio C Hamano6973dca2006-04-21 23:57:45 -07006393void diff_flush(struct diff_options *options)
6394{
6395 struct diff_queue_struct *q = &diff_queued_diff;
Timo Hirvonenc6744342006-06-24 20:21:53 +03006396 int i, output_format = options->output_format;
Timo Hirvonen946c3782006-06-27 15:09:17 +03006397 int separator = 0;
Johan Herland1c57a622011-04-29 11:36:21 +02006398 int dirstat_by_line = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006399
Timo Hirvonenc6744342006-06-24 20:21:53 +03006400 /*
6401 * Order: raw, stat, summary, patch
6402 * or: name/name-status/checkdiff (other bits clear)
6403 */
Timo Hirvonen946c3782006-06-27 15:09:17 +03006404 if (!q->nr)
6405 goto free_queue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006406
Timo Hirvonenc6744342006-06-24 20:21:53 +03006407 if (output_format & (DIFF_FORMAT_RAW |
6408 DIFF_FORMAT_NAME |
6409 DIFF_FORMAT_NAME_STATUS |
6410 DIFF_FORMAT_CHECKDIFF)) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07006411 for (i = 0; i < q->nr; i++) {
6412 struct diff_filepair *p = q->queue[i];
Timo Hirvonenc6744342006-06-24 20:21:53 +03006413 if (check_pair_status(p))
6414 flush_one_pair(p, options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006415 }
Timo Hirvonen946c3782006-06-27 15:09:17 +03006416 separator++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006417 }
Timo Hirvonenc6744342006-06-24 20:21:53 +03006418
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006419 if (output_format & DIFF_FORMAT_DIRSTAT && options->flags.dirstat_by_line)
Johan Herland1c57a622011-04-29 11:36:21 +02006420 dirstat_by_line = 1;
6421
6422 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT) ||
6423 dirstat_by_line) {
Timo Hirvonen5e2b0632006-06-25 14:28:19 +03006424 struct diffstat_t diffstat;
Timo Hirvonenc6744342006-06-24 20:21:53 +03006425
Daniel Ferreirae4cb6592019-11-13 12:40:58 +00006426 compute_diffstat(options, &diffstat, q);
Junio C Hamano74e2abe2006-10-12 03:01:00 -07006427 if (output_format & DIFF_FORMAT_NUMSTAT)
6428 show_numstat(&diffstat, options);
6429 if (output_format & DIFF_FORMAT_DIFFSTAT)
6430 show_stats(&diffstat, options);
Junio C Hamanof6046522007-12-11 23:46:30 -08006431 if (output_format & DIFF_FORMAT_SHORTSTAT)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04006432 show_shortstats(&diffstat, options);
Mårten Kongstadab273892015-03-02 16:05:39 +01006433 if (output_format & DIFF_FORMAT_DIRSTAT && dirstat_by_line)
Johan Herland1c57a622011-04-29 11:36:21 +02006434 show_dirstat_by_line(&diffstat, options);
Junio C Hamanof6046522007-12-11 23:46:30 -08006435 free_diffstat_info(&diffstat);
Junio C Hamano3969cf72006-06-27 15:08:19 -07006436 separator++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006437 }
Johan Herland1c57a622011-04-29 11:36:21 +02006438 if ((output_format & DIFF_FORMAT_DIRSTAT) && !dirstat_by_line)
Junio C Hamanoc04a7152008-02-12 17:06:58 -08006439 show_dirstat(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006440
Timo Hirvonen946c3782006-06-27 15:09:17 +03006441 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
Bo Yang7be57612010-05-26 15:23:54 +08006442 for (i = 0; i < q->nr; i++) {
6443 diff_summary(options, q->queue[i]);
6444 }
Junio C Hamano3969cf72006-06-27 15:08:19 -07006445 separator++;
Sean4bbd2612006-05-14 08:13:49 -04006446 }
6447
Larry D'Anna6977c252010-02-16 01:55:21 -05006448 if (output_format & DIFF_FORMAT_NO_OUTPUT &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006449 options->flags.exit_with_status &&
6450 options->flags.diff_from_contents) {
Larry D'Anna6977c252010-02-16 01:55:21 -05006451 /*
6452 * run diff_flush_patch for the exit status. setting
Ondřej Bílka749f7632013-07-22 23:02:23 +02006453 * options->file to /dev/null should be safe, because we
Larry D'Anna6977c252010-02-16 01:55:21 -05006454 * aren't supposed to produce any output anyway.
6455 */
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006456 diff_free_file(options);
Nguyễn Thái Ngọc Duy23a9e072017-05-03 17:16:46 +07006457 options->file = xfopen("/dev/null", "w");
Larry D'Anna6977c252010-02-16 01:55:21 -05006458 options->close_file = 1;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006459 options->color_moved = 0;
Larry D'Anna6977c252010-02-16 01:55:21 -05006460 for (i = 0; i < q->nr; i++) {
6461 struct diff_filepair *p = q->queue[i];
6462 if (check_pair_status(p))
6463 diff_flush_patch(p, options);
6464 if (options->found_changes)
6465 break;
6466 }
6467 }
6468
Timo Hirvonenc6744342006-06-24 20:21:53 +03006469 if (output_format & DIFF_FORMAT_PATCH) {
Timo Hirvonen946c3782006-06-27 15:09:17 +03006470 if (separator) {
Stefan Beller091f8e22017-06-29 17:06:53 -07006471 emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0);
Stefan Beller30b7e1e2017-06-29 17:07:04 -07006472 if (options->stat_sep)
Timo Hirvonen946c3782006-06-27 15:09:17 +03006473 /* attach patch instead of inline */
Stefan Beller30b7e1e2017-06-29 17:07:04 -07006474 emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP,
6475 NULL, 0, 0);
Timo Hirvonenc6744342006-06-24 20:21:53 +03006476 }
6477
Stefan Bellerec331502017-06-29 17:06:48 -07006478 diff_flush_patch_all_file_pairs(options);
Timo Hirvonenc6744342006-06-24 20:21:53 +03006479 }
6480
Jeff King04245582006-09-07 02:35:42 -04006481 if (output_format & DIFF_FORMAT_CALLBACK)
6482 options->format_callback(q, options, options->format_callback_data);
6483
Timo Hirvonenc6744342006-06-24 20:21:53 +03006484 for (i = 0; i < q->nr; i++)
6485 diff_free_filepair(q->queue[i]);
Timo Hirvonen946c3782006-06-27 15:09:17 +03006486free_queue:
Junio C Hamano6973dca2006-04-21 23:57:45 -07006487 free(q->queue);
Bo Yang9ca5df92010-05-06 21:52:27 -07006488 DIFF_QUEUE_CLEAR(q);
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006489 diff_free(options);
Junio C Hamanof2451942009-05-22 12:45:29 -07006490
6491 /*
Jim Meyering97bf2a02009-08-30 22:27:02 +02006492 * Report the content-level differences with HAS_CHANGES;
Junio C Hamanof2451942009-05-22 12:45:29 -07006493 * diff_addremove/diff_change does not set the bit when
6494 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
6495 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006496 if (options->flags.diff_from_contents) {
Junio C Hamanof2451942009-05-22 12:45:29 -07006497 if (options->found_changes)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006498 options->flags.has_changes = 1;
Junio C Hamanof2451942009-05-22 12:45:29 -07006499 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006500 options->flags.has_changes = 0;
Junio C Hamanof2451942009-05-22 12:45:29 -07006501 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07006502}
6503
Junio C Hamano08578fa2013-07-17 15:09:34 -07006504static int match_filter(const struct diff_options *options, const struct diff_filepair *p)
6505{
6506 return (((p->status == DIFF_STATUS_MODIFIED) &&
6507 ((p->score &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006508 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN, options)) ||
Junio C Hamano08578fa2013-07-17 15:09:34 -07006509 (!p->score &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006510 filter_bit_tst(DIFF_STATUS_MODIFIED, options)))) ||
Junio C Hamano08578fa2013-07-17 15:09:34 -07006511 ((p->status != DIFF_STATUS_MODIFIED) &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006512 filter_bit_tst(p->status, options)));
Junio C Hamano08578fa2013-07-17 15:09:34 -07006513}
6514
Junio C Hamano949226f2013-07-17 14:19:24 -07006515static void diffcore_apply_filter(struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006516{
6517 int i;
6518 struct diff_queue_struct *q = &diff_queued_diff;
6519 struct diff_queue_struct outq;
Junio C Hamano949226f2013-07-17 14:19:24 -07006520
Bo Yang9ca5df92010-05-06 21:52:27 -07006521 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006522
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006523 if (!options->filter)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006524 return;
6525
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006526 if (filter_bit_tst(DIFF_STATUS_FILTER_AON, options)) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07006527 int found;
6528 for (i = found = 0; !found && i < q->nr; i++) {
Junio C Hamano08578fa2013-07-17 15:09:34 -07006529 if (match_filter(options, q->queue[i]))
Junio C Hamano6973dca2006-04-21 23:57:45 -07006530 found++;
6531 }
6532 if (found)
6533 return;
6534
6535 /* otherwise we will clear the whole queue
6536 * by copying the empty outq at the end of this
6537 * function, but first clear the current entries
6538 * in the queue.
6539 */
6540 for (i = 0; i < q->nr; i++)
6541 diff_free_filepair(q->queue[i]);
6542 }
6543 else {
6544 /* Only the matching ones */
6545 for (i = 0; i < q->nr; i++) {
6546 struct diff_filepair *p = q->queue[i];
Junio C Hamano08578fa2013-07-17 15:09:34 -07006547 if (match_filter(options, p))
Junio C Hamano6973dca2006-04-21 23:57:45 -07006548 diff_q(&outq, p);
6549 else
6550 diff_free_filepair(p);
6551 }
6552 }
6553 free(q->queue);
6554 *q = outq;
6555}
6556
Sven Verdoolaege57011152007-09-08 12:30:22 +02006557/* Check whether two filespecs with the same mode and size are identical */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006558static int diff_filespec_is_identical(struct repository *r,
6559 struct diff_filespec *one,
Sven Verdoolaege57011152007-09-08 12:30:22 +02006560 struct diff_filespec *two)
6561{
Junio C Hamano2b459b42008-03-02 00:07:59 -08006562 if (S_ISGITLINK(one->mode))
6563 return 0;
Jonathan Tan1c37e862020-04-07 15:11:41 -07006564 if (diff_populate_filespec(r, one, NULL))
Sven Verdoolaege57011152007-09-08 12:30:22 +02006565 return 0;
Jonathan Tan1c37e862020-04-07 15:11:41 -07006566 if (diff_populate_filespec(r, two, NULL))
Sven Verdoolaege57011152007-09-08 12:30:22 +02006567 return 0;
6568 return !memcmp(one->data, two->data, one->size);
6569}
6570
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006571static int diff_filespec_check_stat_unmatch(struct repository *r,
6572 struct diff_filepair *p)
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006573{
Jonathan Tan1c37e862020-04-07 15:11:41 -07006574 struct diff_populate_filespec_options dpf_options = {
6575 .check_size_only = 1,
Jonathan Tan95acf112020-04-07 15:11:43 -07006576 .missing_object_cb = diff_queued_diff_prefetch,
6577 .missing_object_data = r,
Jonathan Tan1c37e862020-04-07 15:11:41 -07006578 };
6579
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006580 if (p->done_skip_stat_unmatch)
6581 return p->skip_stat_unmatch_result;
6582
6583 p->done_skip_stat_unmatch = 1;
6584 p->skip_stat_unmatch_result = 0;
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006585 /*
6586 * 1. Entries that come from stat info dirtiness
6587 * always have both sides (iow, not create/delete),
6588 * one side of the object name is unknown, with
6589 * the same mode and size. Keep the ones that
6590 * do not match these criteria. They have real
6591 * differences.
6592 *
6593 * 2. At this point, the file is known to be modified,
6594 * with the same mode and size, and the object
6595 * name of one side is unknown. Need to inspect
6596 * the identical contents.
6597 */
6598 if (!DIFF_FILE_VALID(p->one) || /* (1) */
6599 !DIFF_FILE_VALID(p->two) ||
brian m. carlson41c95602016-06-24 23:09:24 +00006600 (p->one->oid_valid && p->two->oid_valid) ||
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006601 (p->one->mode != p->two->mode) ||
Jonathan Tan1c37e862020-04-07 15:11:41 -07006602 diff_populate_filespec(r, p->one, &dpf_options) ||
6603 diff_populate_filespec(r, p->two, &dpf_options) ||
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006604 (p->one->size != p->two->size) ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006605 !diff_filespec_is_identical(r, p->one, p->two)) /* (2) */
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006606 p->skip_stat_unmatch_result = 1;
6607 return p->skip_stat_unmatch_result;
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006608}
6609
Junio C Hamanofb132272007-08-03 13:33:31 -07006610static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
6611{
6612 int i;
6613 struct diff_queue_struct *q = &diff_queued_diff;
6614 struct diff_queue_struct outq;
Bo Yang9ca5df92010-05-06 21:52:27 -07006615 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamanofb132272007-08-03 13:33:31 -07006616
6617 for (i = 0; i < q->nr; i++) {
6618 struct diff_filepair *p = q->queue[i];
6619
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006620 if (diff_filespec_check_stat_unmatch(diffopt->repo, p))
Junio C Hamanofb132272007-08-03 13:33:31 -07006621 diff_q(&outq, p);
6622 else {
6623 /*
6624 * The caller can subtract 1 from skip_stat_unmatch
6625 * to determine how many paths were dirty only
6626 * due to stat info mismatch.
6627 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006628 if (!diffopt->flags.no_index)
René Scharfe6d2d9e82007-08-15 00:41:00 +02006629 diffopt->skip_stat_unmatch++;
Junio C Hamanofb132272007-08-03 13:33:31 -07006630 diff_free_filepair(p);
6631 }
6632 }
6633 free(q->queue);
6634 *q = outq;
6635}
6636
Junio C Hamano730f7282009-09-20 00:03:39 -07006637static int diffnamecmp(const void *a_, const void *b_)
6638{
6639 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
6640 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
6641 const char *name_a, *name_b;
6642
6643 name_a = a->one ? a->one->path : a->two->path;
6644 name_b = b->one ? b->one->path : b->two->path;
6645 return strcmp(name_a, name_b);
6646}
6647
Jeff King784c0da2019-02-14 00:48:03 -05006648void diffcore_fix_diff_index(void)
Junio C Hamano730f7282009-09-20 00:03:39 -07006649{
6650 struct diff_queue_struct *q = &diff_queued_diff;
René Scharfe9ed0d8d2016-09-29 17:27:31 +02006651 QSORT(q->queue, q->nr, diffnamecmp);
Junio C Hamano730f7282009-09-20 00:03:39 -07006652}
6653
Jonathan Tan95acf112020-04-07 15:11:43 -07006654void diff_add_if_missing(struct repository *r,
6655 struct oid_array *to_fetch,
6656 const struct diff_filespec *filespec)
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006657{
6658 if (filespec && filespec->oid_valid &&
Jonathan Tana63694f2019-08-20 13:53:20 -07006659 !S_ISGITLINK(filespec->mode) &&
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006660 oid_object_info_extended(r, &filespec->oid, NULL,
6661 OBJECT_INFO_FOR_PREFETCH))
6662 oid_array_append(to_fetch, &filespec->oid);
6663}
6664
Jonathan Tan95acf112020-04-07 15:11:43 -07006665void diff_queued_diff_prefetch(void *repository)
6666{
6667 struct repository *repo = repository;
6668 int i;
6669 struct diff_queue_struct *q = &diff_queued_diff;
6670 struct oid_array to_fetch = OID_ARRAY_INIT;
6671
6672 for (i = 0; i < q->nr; i++) {
6673 struct diff_filepair *p = q->queue[i];
6674 diff_add_if_missing(repo, &to_fetch, p->one);
6675 diff_add_if_missing(repo, &to_fetch, p->two);
6676 }
6677
6678 /*
6679 * NEEDSWORK: Consider deduplicating the OIDs sent.
6680 */
6681 promisor_remote_get_direct(repo, to_fetch.oid, to_fetch.nr);
6682
6683 oid_array_clear(&to_fetch);
6684}
6685
Junio C Hamano6973dca2006-04-21 23:57:45 -07006686void diffcore_std(struct diff_options *options)
6687{
Jonathan Tan95acf112020-04-07 15:11:43 -07006688 int output_formats_to_prefetch = DIFF_FORMAT_DIFFSTAT |
6689 DIFF_FORMAT_NUMSTAT |
6690 DIFF_FORMAT_PATCH |
6691 DIFF_FORMAT_SHORTSTAT |
6692 DIFF_FORMAT_DIRSTAT;
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006693
Jonathan Tan95acf112020-04-07 15:11:43 -07006694 /*
6695 * Check if the user requested a blob-data-requiring diff output and/or
6696 * break-rewrite detection (which requires blob data). If yes, prefetch
6697 * the diff pairs.
6698 *
6699 * If no prefetching occurs, diffcore_rename() will prefetch if it
6700 * decides that it needs inexact rename detection.
6701 */
6702 if (options->repo == the_repository && has_promisor_remote() &&
6703 (options->output_format & output_formats_to_prefetch ||
6704 options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
6705 diff_queued_diff_prefetch(options->repo);
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006706
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04006707 /* NOTE please keep the following in sync with diff_tree_combined() */
Junio C Hamano9d865352008-09-06 19:09:16 -07006708 if (options->skip_stat_unmatch)
Junio C Hamanofb132272007-08-03 13:33:31 -07006709 diffcore_skip_stat_unmatch(options);
Junio C Hamano44c48a92010-08-13 12:17:45 -07006710 if (!options->found_follow) {
6711 /* See try_to_follow_renames() in tree-diff.c */
6712 if (options->break_opt != -1)
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006713 diffcore_break(options->repo,
6714 options->break_opt);
Junio C Hamano44c48a92010-08-13 12:17:45 -07006715 if (options->detect_rename)
6716 diffcore_rename(options);
6717 if (options->break_opt != -1)
6718 diffcore_merge_broken();
6719 }
Stefan Bellercf630512018-01-04 14:50:41 -08006720 if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
Junio C Hamano382f0132010-08-31 13:44:39 -07006721 diffcore_pickaxe(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006722 if (options->orderfile)
6723 diffcore_order(options->orderfile);
Junio C Hamano1eb41362021-02-11 11:57:50 -08006724 if (options->rotate_to)
6725 diffcore_rotate(options);
Junio C Hamano44c48a92010-08-13 12:17:45 -07006726 if (!options->found_follow)
6727 /* See try_to_follow_renames() in tree-diff.c */
6728 diff_resolve_rename_copy();
Junio C Hamano949226f2013-07-17 14:19:24 -07006729 diffcore_apply_filter(options);
Junio C Hamano68aacb22007-03-14 11:12:13 -07006730
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006731 if (diff_queued_diff.nr && !options->flags.diff_from_contents)
6732 options->flags.has_changes = 1;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01006733 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006734 options->flags.has_changes = 0;
Bo Yang1da61752010-05-06 21:52:28 -07006735
Junio C Hamano44c48a92010-08-13 12:17:45 -07006736 options->found_follow = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006737}
6738
Junio C Hamanoda31b352007-12-13 23:40:27 -08006739int diff_result_code(struct diff_options *opt, int status)
6740{
6741 int result = 0;
Junio C Hamanof31027c2011-01-06 13:50:06 -08006742
Max Nanasyc9fc4412013-03-21 12:53:38 -07006743 diff_warn_rename_limit("diff.renameLimit",
Junio C Hamanof31027c2011-01-06 13:50:06 -08006744 opt->needed_rename_limit,
6745 opt->degraded_cc_to_c);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006746 if (!opt->flags.exit_with_status &&
Junio C Hamanoda31b352007-12-13 23:40:27 -08006747 !(opt->output_format & DIFF_FORMAT_CHECKDIFF))
6748 return status;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006749 if (opt->flags.exit_with_status &&
6750 opt->flags.has_changes)
Junio C Hamanoda31b352007-12-13 23:40:27 -08006751 result |= 01;
6752 if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006753 opt->flags.check_failed)
Junio C Hamanoda31b352007-12-13 23:40:27 -08006754 result |= 02;
6755 return result;
6756}
Junio C Hamano6973dca2006-04-21 23:57:45 -07006757
Junio C Hamano28b92642011-05-31 09:14:17 -07006758int diff_can_quit_early(struct diff_options *opt)
6759{
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006760 return (opt->flags.quick &&
Junio C Hamano28b92642011-05-31 09:14:17 -07006761 !opt->filter &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006762 opt->flags.has_changes);
Junio C Hamano28b92642011-05-31 09:14:17 -07006763}
6764
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006765/*
6766 * Shall changes to this submodule be ignored?
6767 *
6768 * Submodule changes can be configured to be ignored separately for each path,
6769 * but that configuration can be overridden from the command line.
6770 */
6771static int is_submodule_ignored(const char *path, struct diff_options *options)
6772{
6773 int ignored = 0;
Brandon Williams02f2f562017-10-31 11:19:05 -07006774 struct diff_flags orig_flags = options->flags;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006775 if (!options->flags.override_submodule_config)
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006776 set_diffopt_flags_from_submodule_config(options, path);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006777 if (options->flags.ignore_submodules)
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006778 ignored = 1;
6779 options->flags = orig_flags;
6780 return ignored;
6781}
6782
Daniel Ferreirae4cb6592019-11-13 12:40:58 +00006783void compute_diffstat(struct diff_options *options,
6784 struct diffstat_t *diffstat,
6785 struct diff_queue_struct *q)
6786{
6787 int i;
6788
6789 memset(diffstat, 0, sizeof(struct diffstat_t));
6790 for (i = 0; i < q->nr; i++) {
6791 struct diff_filepair *p = q->queue[i];
6792 if (check_pair_status(p))
6793 diff_flush_stat(p, options, diffstat);
6794 }
6795}
6796
Junio C Hamano6973dca2006-04-21 23:57:45 -07006797void diff_addremove(struct diff_options *options,
6798 int addremove, unsigned mode,
Brandon Williamsc26022e2017-05-30 10:30:47 -07006799 const struct object_id *oid,
6800 int oid_valid,
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006801 const char *concatpath, unsigned dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006802{
Junio C Hamano6973dca2006-04-21 23:57:45 -07006803 struct diff_filespec *one, *two;
6804
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006805 if (S_ISGITLINK(mode) && is_submodule_ignored(concatpath, options))
Johannes Schindelin50fd9bd2008-05-14 18:03:31 +01006806 return;
6807
Junio C Hamano6973dca2006-04-21 23:57:45 -07006808 /* This may look odd, but it is a preparation for
6809 * feeding "there are unchanged files which should
6810 * not produce diffs, but when you are doing copy
6811 * detection you would need them, so here they are"
6812 * entries to the diff-core. They will be prefixed
6813 * with something like '=' or '*' (I haven't decided
6814 * which but should not make any difference).
Junio C Hamanoa6080a02007-06-07 00:04:01 -07006815 * Feeding the same new and old to diff_change()
Junio C Hamano6973dca2006-04-21 23:57:45 -07006816 * also has the same effect.
6817 * Before the final output happens, they are pruned after
6818 * merged into rename/copy pairs as appropriate.
6819 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006820 if (options->flags.reverse_diff)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006821 addremove = (addremove == '+' ? '-' :
6822 addremove == '-' ? '+' : addremove);
6823
Junio C Hamanocd676a52008-02-12 14:26:02 -08006824 if (options->prefix &&
6825 strncmp(concatpath, options->prefix, options->prefix_length))
6826 return;
6827
Junio C Hamano6973dca2006-04-21 23:57:45 -07006828 one = alloc_filespec(concatpath);
6829 two = alloc_filespec(concatpath);
6830
6831 if (addremove != '+')
Brandon Williamsf9704c22017-05-30 10:30:50 -07006832 fill_filespec(one, oid, oid_valid, mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006833 if (addremove != '-') {
Brandon Williamsf9704c22017-05-30 10:30:50 -07006834 fill_filespec(two, oid, oid_valid, mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006835 two->dirty_submodule = dirty_submodule;
6836 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07006837
6838 diff_queue(&diff_queued_diff, one, two);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006839 if (!options->flags.diff_from_contents)
6840 options->flags.has_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006841}
6842
6843void diff_change(struct diff_options *options,
6844 unsigned old_mode, unsigned new_mode,
Brandon Williams94a00972017-05-30 10:30:49 -07006845 const struct object_id *old_oid,
6846 const struct object_id *new_oid,
6847 int old_oid_valid, int new_oid_valid,
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006848 const char *concatpath,
6849 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006850{
Junio C Hamano6973dca2006-04-21 23:57:45 -07006851 struct diff_filespec *one, *two;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006852 struct diff_filepair *p;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006853
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006854 if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) &&
6855 is_submodule_ignored(concatpath, options))
Johannes Schindelin50fd9bd2008-05-14 18:03:31 +01006856 return;
6857
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006858 if (options->flags.reverse_diff) {
René Scharfe35d803b2017-01-28 22:40:58 +01006859 SWAP(old_mode, new_mode);
Brandon Williams94a00972017-05-30 10:30:49 -07006860 SWAP(old_oid, new_oid);
6861 SWAP(old_oid_valid, new_oid_valid);
René Scharfe35d803b2017-01-28 22:40:58 +01006862 SWAP(old_dirty_submodule, new_dirty_submodule);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006863 }
Junio C Hamanocd676a52008-02-12 14:26:02 -08006864
6865 if (options->prefix &&
6866 strncmp(concatpath, options->prefix, options->prefix_length))
6867 return;
6868
Junio C Hamano6973dca2006-04-21 23:57:45 -07006869 one = alloc_filespec(concatpath);
6870 two = alloc_filespec(concatpath);
Brandon Williamsf9704c22017-05-30 10:30:50 -07006871 fill_filespec(one, old_oid, old_oid_valid, old_mode);
6872 fill_filespec(two, new_oid, new_oid_valid, new_mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006873 one->dirty_submodule = old_dirty_submodule;
6874 two->dirty_submodule = new_dirty_submodule;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006875 p = diff_queue(&diff_queued_diff, one, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006876
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006877 if (options->flags.diff_from_contents)
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006878 return;
6879
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006880 if (options->flags.quick && options->skip_stat_unmatch &&
Jeff Kingd2d7fbe2020-06-01 16:22:18 -04006881 !diff_filespec_check_stat_unmatch(options->repo, p)) {
6882 diff_free_filespec_data(p->one);
6883 diff_free_filespec_data(p->two);
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006884 return;
Jeff Kingd2d7fbe2020-06-01 16:22:18 -04006885 }
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006886
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006887 options->flags.has_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006888}
6889
Junio C Hamanofa7b2902011-04-22 16:05:58 -07006890struct diff_filepair *diff_unmerge(struct diff_options *options, const char *path)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006891{
Junio C Hamano76399c02011-04-22 15:55:55 -07006892 struct diff_filepair *pair;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006893 struct diff_filespec *one, *two;
Junio C Hamanocd676a52008-02-12 14:26:02 -08006894
6895 if (options->prefix &&
6896 strncmp(path, options->prefix, options->prefix_length))
Junio C Hamano76399c02011-04-22 15:55:55 -07006897 return NULL;
Junio C Hamanocd676a52008-02-12 14:26:02 -08006898
Junio C Hamano6973dca2006-04-21 23:57:45 -07006899 one = alloc_filespec(path);
6900 two = alloc_filespec(path);
Junio C Hamano76399c02011-04-22 15:55:55 -07006901 pair = diff_queue(&diff_queued_diff, one, two);
6902 pair->is_unmerged = 1;
6903 return pair;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006904}
Jeff King9cb92c32008-10-05 17:43:45 -04006905
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006906static char *run_textconv(struct repository *r,
6907 const char *pgm,
6908 struct diff_filespec *spec,
6909 size_t *outsize)
Jeff King9cb92c32008-10-05 17:43:45 -04006910{
Jeff King479b0ae2009-01-22 00:59:56 -05006911 struct diff_tempfile *temp;
Jeff King9cb92c32008-10-05 17:43:45 -04006912 const char *argv[3];
6913 const char **arg = argv;
René Scharfed3180272014-08-19 21:09:35 +02006914 struct child_process child = CHILD_PROCESS_INIT;
Jeff King9cb92c32008-10-05 17:43:45 -04006915 struct strbuf buf = STRBUF_INIT;
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006916 int err = 0;
Jeff King9cb92c32008-10-05 17:43:45 -04006917
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006918 temp = prepare_temp_file(r, spec->path, spec);
Jeff King9cb92c32008-10-05 17:43:45 -04006919 *arg++ = pgm;
Jeff King479b0ae2009-01-22 00:59:56 -05006920 *arg++ = temp->name;
Jeff King9cb92c32008-10-05 17:43:45 -04006921 *arg = NULL;
6922
Jeff King41a457e2009-12-30 06:01:09 -05006923 child.use_shell = 1;
Jeff King9cb92c32008-10-05 17:43:45 -04006924 child.argv = argv;
6925 child.out = -1;
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006926 if (start_command(&child)) {
Jeff King479b0ae2009-01-22 00:59:56 -05006927 remove_tempfile();
Jeff King9cb92c32008-10-05 17:43:45 -04006928 return NULL;
6929 }
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006930
6931 if (strbuf_read(&buf, child.out, 0) < 0)
6932 err = error("error reading from textconv command '%s'", pgm);
Jeff King70d70992009-12-30 04:02:53 -05006933 close(child.out);
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006934
6935 if (finish_command(&child) || err) {
6936 strbuf_release(&buf);
6937 remove_tempfile();
6938 return NULL;
6939 }
Jeff King479b0ae2009-01-22 00:59:56 -05006940 remove_tempfile();
Jeff King9cb92c32008-10-05 17:43:45 -04006941
6942 return strbuf_detach(&buf, outsize);
6943}
Jeff King840383b2010-04-01 20:09:26 -04006944
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02006945size_t fill_textconv(struct repository *r,
6946 struct userdiff_driver *driver,
Axel Bonneta788d7d2010-06-07 17:23:36 +02006947 struct diff_filespec *df,
6948 char **outbuf)
Jeff King840383b2010-04-01 20:09:26 -04006949{
6950 size_t size;
6951
Jeff Kinga64e6a42016-02-22 13:28:54 -05006952 if (!driver) {
Jeff King840383b2010-04-01 20:09:26 -04006953 if (!DIFF_FILE_VALID(df)) {
6954 *outbuf = "";
6955 return 0;
6956 }
Jonathan Tan1c37e862020-04-07 15:11:41 -07006957 if (diff_populate_filespec(r, df, NULL))
Jeff King840383b2010-04-01 20:09:26 -04006958 die("unable to read files to diff");
6959 *outbuf = df->data;
6960 return df->size;
6961 }
6962
Jeff Kinga64e6a42016-02-22 13:28:54 -05006963 if (!driver->textconv)
Johannes Schindelin033abf92018-05-02 11:38:39 +02006964 BUG("fill_textconv called with non-textconv driver");
Jeff Kinga64e6a42016-02-22 13:28:54 -05006965
brian m. carlson41c95602016-06-24 23:09:24 +00006966 if (driver->textconv_cache && df->oid_valid) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00006967 *outbuf = notes_cache_get(driver->textconv_cache,
brian m. carlson569aa372017-05-06 22:09:58 +00006968 &df->oid,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006969 &size);
6970 if (*outbuf)
6971 return size;
6972 }
6973
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006974 *outbuf = run_textconv(r, driver->textconv, df, &size);
Jeff King840383b2010-04-01 20:09:26 -04006975 if (!*outbuf)
6976 die("unable to read files to diff");
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006977
brian m. carlson41c95602016-06-24 23:09:24 +00006978 if (driver->textconv_cache && df->oid_valid) {
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006979 /* ignore errors, as we might be in a readonly repository */
brian m. carlson569aa372017-05-06 22:09:58 +00006980 notes_cache_put(driver->textconv_cache, &df->oid, *outbuf,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006981 size);
6982 /*
6983 * we could save up changes and flush them all at the end,
6984 * but we would need an extra call after all diffing is done.
6985 * Since generating a cache entry is the slow path anyway,
6986 * this extra overhead probably isn't a big deal.
6987 */
6988 notes_cache_write(driver->textconv_cache);
6989 }
6990
Jeff King840383b2010-04-01 20:09:26 -04006991 return size;
6992}
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07006993
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02006994int textconv_object(struct repository *r,
6995 const char *path,
Jeff Smith3a35cb22017-05-24 00:15:10 -05006996 unsigned mode,
6997 const struct object_id *oid,
6998 int oid_valid,
6999 char **buf,
7000 unsigned long *buf_size)
7001{
7002 struct diff_filespec *df;
7003 struct userdiff_driver *textconv;
7004
7005 df = alloc_filespec(path);
Junio C Hamanoa6f38c12017-06-19 12:38:44 -07007006 fill_filespec(df, oid, oid_valid, mode);
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01007007 textconv = get_textconv(r, df);
Jeff Smith3a35cb22017-05-24 00:15:10 -05007008 if (!textconv) {
7009 free_filespec(df);
7010 return 0;
7011 }
7012
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02007013 *buf_size = fill_textconv(r, textconv, df, buf);
Jeff Smith3a35cb22017-05-24 00:15:10 -05007014 free_filespec(df);
7015 return 1;
7016}
7017
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07007018void setup_diff_pager(struct diff_options *opt)
7019{
7020 /*
7021 * If the user asked for our exit code, then either they want --quiet
7022 * or --exit-code. We should definitely not bother with a pager in the
7023 * former case, as we will generate no output. Since we still properly
7024 * report our exit code even when a pager is run, we _could_ run a
7025 * pager with --exit-code. But since we have not done so historically,
7026 * and because it is easy to find people oneline advising "git diff
7027 * --exit-code" in hooks and other scripts, we do not do so.
7028 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07007029 if (!opt->flags.exit_with_status &&
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07007030 check_pager_config("diff") != 0)
7031 setup_pager();
7032}