blob: c862771a58939f0cd2a20b2056c8d93b17c7be26 [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"
Phillip Wood72962e82021-12-09 10:30:09 +000021#include "mem-pool.h"
Junio C Hamanoa757c642010-03-24 19:21:32 -070022#include "ll-merge.h"
Michael Haggerty02e8ca02012-10-28 17:50:54 +010023#include "string-list.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040024#include "strvec.h"
Jacob Keller660e1132016-08-31 16:27:20 -070025#include "graph.h"
Jonathan Tan150e3002017-08-18 15:20:36 -070026#include "packfile.h"
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +070027#include "parse-options.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +020028#include "help.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020029#include "promisor-remote.h"
Derrick Stoleead90da72021-09-08 11:23:56 +000030#include "dir.h"
Junio C Hamano6973dca2006-04-21 23:57:45 -070031
Shawn O. Pearce1510fea2006-12-14 06:15:57 -050032#ifdef NO_FAST_WORKING_DIRECTORY
33#define FAST_WORKING_DIRECTORY 0
34#else
35#define FAST_WORKING_DIRECTORY 1
36#endif
37
David Rientjes96f1e582006-08-15 10:23:48 -070038static int diff_detect_rename_default;
Stefan Beller33de7162017-05-08 12:03:38 -040039static int diff_indent_heuristic = 1;
Elijah Newren94b82d52021-07-15 00:45:24 +000040static int diff_rename_limit_default = 1000;
Jim Meyeringa624eaa2008-08-15 13:39:26 +020041static int diff_suppress_blank_empty;
Junio C Hamanod2aea132012-09-15 13:59:59 -070042static int diff_use_color_default = -1;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -070043static int diff_color_moved_default;
Stefan Beller626c0b52018-07-18 12:31:56 -070044static int diff_color_moved_ws_default;
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -040045static int diff_context_default = 3;
Vegard Nossumc4888672017-01-12 13:21:11 +010046static int diff_interhunk_context_default;
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -060047static const char *diff_word_regex_cfg;
Johannes Schindelincbe02102007-12-17 13:42:20 +000048static const char *external_diff_cmd_cfg;
Samuel Bronson6d8940b2013-12-18 19:08:12 -050049static const char *diff_order_file_cfg;
Junio C Hamanoaecbf912007-08-31 13:13:42 -070050int diff_auto_refresh_index = 1;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -070051static int diff_mnemonic_prefix;
Eli Collinsf89504d2010-05-02 19:03:41 -070052static int diff_no_prefix;
Laurent Arnoudc28ded82020-05-22 12:46:18 +020053static int diff_relative;
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +010054static int diff_stat_graph_width;
Johan Herland712d2c72011-04-29 11:36:20 +020055static int diff_dirstat_permille_default = 30;
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +020056static struct diff_options default_diff_options;
Michal Privoznik07ab4de2013-01-16 08:51:57 +010057static long diff_algorithm;
Junio C Hamanoa17505f2016-10-04 15:26:27 -070058static unsigned ws_error_highlight_default = WSEH_NEW;
Junio C Hamano6973dca2006-04-21 23:57:45 -070059
Jeff King7c92fe02006-09-08 04:03:18 -040060static char diff_colors[][COLOR_MAXLEN] = {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010061 GIT_COLOR_RESET,
Jeff King8dbf3eb2015-05-27 16:48:46 -040062 GIT_COLOR_NORMAL, /* CONTEXT */
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010063 GIT_COLOR_BOLD, /* METAINFO */
64 GIT_COLOR_CYAN, /* FRAGINFO */
65 GIT_COLOR_RED, /* OLD */
66 GIT_COLOR_GREEN, /* NEW */
67 GIT_COLOR_YELLOW, /* COMMIT */
68 GIT_COLOR_BG_RED, /* WHITESPACE */
Bert Wesarg89cb73a2009-11-27 07:55:18 +010069 GIT_COLOR_NORMAL, /* FUNCINFO */
Stefan Beller86b452e2017-06-30 13:53:09 -070070 GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */
71 GIT_COLOR_BOLD_BLUE, /* OLD_MOVED ALTERNATIVE */
72 GIT_COLOR_FAINT, /* OLD_MOVED_DIM */
73 GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */
74 GIT_COLOR_BOLD_CYAN, /* NEW_MOVED */
75 GIT_COLOR_BOLD_YELLOW, /* NEW_MOVED ALTERNATIVE */
76 GIT_COLOR_FAINT, /* NEW_MOVED_DIM */
77 GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */
Johannes Schindelina7be92a2018-08-13 04:33:32 -070078 GIT_COLOR_FAINT, /* CONTEXT_DIM */
79 GIT_COLOR_FAINT_RED, /* OLD_DIM */
80 GIT_COLOR_FAINT_GREEN, /* NEW_DIM */
81 GIT_COLOR_BOLD, /* CONTEXT_BOLD */
82 GIT_COLOR_BOLD_RED, /* OLD_BOLD */
83 GIT_COLOR_BOLD_GREEN, /* NEW_BOLD */
Johannes Schindelincd112ce2006-06-13 18:45:44 +020084};
85
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +020086static const char *color_diff_slots[] = {
87 [DIFF_CONTEXT] = "context",
88 [DIFF_METAINFO] = "meta",
89 [DIFF_FRAGINFO] = "frag",
90 [DIFF_FILE_OLD] = "old",
91 [DIFF_FILE_NEW] = "new",
92 [DIFF_COMMIT] = "commit",
93 [DIFF_WHITESPACE] = "whitespace",
94 [DIFF_FUNCINFO] = "func",
95 [DIFF_FILE_OLD_MOVED] = "oldMoved",
96 [DIFF_FILE_OLD_MOVED_ALT] = "oldMovedAlternative",
97 [DIFF_FILE_OLD_MOVED_DIM] = "oldMovedDimmed",
98 [DIFF_FILE_OLD_MOVED_ALT_DIM] = "oldMovedAlternativeDimmed",
99 [DIFF_FILE_NEW_MOVED] = "newMoved",
100 [DIFF_FILE_NEW_MOVED_ALT] = "newMovedAlternative",
101 [DIFF_FILE_NEW_MOVED_DIM] = "newMovedDimmed",
102 [DIFF_FILE_NEW_MOVED_ALT_DIM] = "newMovedAlternativeDimmed",
Johannes Schindelina7be92a2018-08-13 04:33:32 -0700103 [DIFF_CONTEXT_DIM] = "contextDimmed",
104 [DIFF_FILE_OLD_DIM] = "oldDimmed",
105 [DIFF_FILE_NEW_DIM] = "newDimmed",
106 [DIFF_CONTEXT_BOLD] = "contextBold",
107 [DIFF_FILE_OLD_BOLD] = "oldBold",
108 [DIFF_FILE_NEW_BOLD] = "newBold",
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200109};
110
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +0200111define_list_config_array_extra(color_diff_slots, {"plain"});
112
Jeff King9e1a5eb2014-06-18 15:41:50 -0400113static int parse_diff_color_slot(const char *var)
Junio C Hamano801235c2006-06-24 04:06:23 -0700114{
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200115 if (!strcasecmp(var, "plain"))
Jeff King8dbf3eb2015-05-27 16:48:46 -0400116 return DIFF_CONTEXT;
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200117 return LOOKUP_CONFIG(color_diff_slots, var);
Junio C Hamano801235c2006-06-24 04:06:23 -0700118}
119
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100120static int parse_dirstat_params(struct diff_options *options, const char *params_string,
Johan Herland51670fc2011-04-29 11:36:22 +0200121 struct strbuf *errmsg)
Johan Herland333f3fb2011-04-29 11:36:18 +0200122{
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100123 char *params_copy = xstrdup(params_string);
124 struct string_list params = STRING_LIST_INIT_NODUP;
125 int ret = 0;
126 int i;
Johan Herland51670fc2011-04-29 11:36:22 +0200127
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100128 if (*params_copy)
129 string_list_split_in_place(&params, params_copy, ',', -1);
130 for (i = 0; i < params.nr; i++) {
131 const char *p = params.items[i].string;
132 if (!strcmp(p, "changes")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700133 options->flags.dirstat_by_line = 0;
134 options->flags.dirstat_by_file = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100135 } else if (!strcmp(p, "lines")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700136 options->flags.dirstat_by_line = 1;
137 options->flags.dirstat_by_file = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100138 } else if (!strcmp(p, "files")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700139 options->flags.dirstat_by_line = 0;
140 options->flags.dirstat_by_file = 1;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100141 } else if (!strcmp(p, "noncumulative")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700142 options->flags.dirstat_cumulative = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100143 } else if (!strcmp(p, "cumulative")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700144 options->flags.dirstat_cumulative = 1;
Johan Herland333f3fb2011-04-29 11:36:18 +0200145 } else if (isdigit(*p)) {
146 char *end;
Johan Herland51670fc2011-04-29 11:36:22 +0200147 int permille = strtoul(p, &end, 10) * 10;
148 if (*end == '.' && isdigit(*++end)) {
Johan Herland712d2c72011-04-29 11:36:20 +0200149 /* only use first digit */
Johan Herland51670fc2011-04-29 11:36:22 +0200150 permille += *end - '0';
Johan Herland712d2c72011-04-29 11:36:20 +0200151 /* .. and ignore any further digits */
Johan Herland51670fc2011-04-29 11:36:22 +0200152 while (isdigit(*++end))
Johan Herland712d2c72011-04-29 11:36:20 +0200153 ; /* nothing */
154 }
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100155 if (!*end)
Johan Herland51670fc2011-04-29 11:36:22 +0200156 options->dirstat_permille = permille;
157 else {
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100158 strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
159 p);
Johan Herland51670fc2011-04-29 11:36:22 +0200160 ret++;
161 }
162 } else {
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100163 strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p);
Johan Herland51670fc2011-04-29 11:36:22 +0200164 ret++;
Johan Herland333f3fb2011-04-29 11:36:18 +0200165 }
Johan Herland51670fc2011-04-29 11:36:22 +0200166
Johan Herland333f3fb2011-04-29 11:36:18 +0200167 }
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100168 string_list_clear(&params, 0);
169 free(params_copy);
Johan Herland51670fc2011-04-29 11:36:22 +0200170 return ret;
Johan Herland333f3fb2011-04-29 11:36:18 +0200171}
172
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530173static int parse_submodule_params(struct diff_options *options, const char *value)
174{
175 if (!strcmp(value, "log"))
Jacob Keller61cfbc02016-08-31 16:27:21 -0700176 options->submodule_format = DIFF_SUBMODULE_LOG;
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530177 else if (!strcmp(value, "short"))
Jacob Keller61cfbc02016-08-31 16:27:21 -0700178 options->submodule_format = DIFF_SUBMODULE_SHORT;
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700179 else if (!strcmp(value, "diff"))
180 options->submodule_format = DIFF_SUBMODULE_INLINE_DIFF;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700181 /*
182 * Please update $__git_diff_submodule_formats in
183 * git-completion.bash when you add new formats.
184 */
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530185 else
186 return -1;
187 return 0;
188}
189
Ben Peart85b46032018-05-02 16:01:14 +0000190int git_config_rename(const char *var, const char *value)
Linus Torvaldscced5fb2009-04-09 11:46:15 -0700191{
192 if (!value)
193 return DIFF_DETECT_RENAME;
194 if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy"))
195 return DIFF_DETECT_COPY;
196 return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0;
197}
198
Michal Privoznik07924d42013-01-16 08:51:58 +0100199long parse_algorithm_value(const char *value)
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100200{
201 if (!value)
202 return -1;
203 else if (!strcasecmp(value, "myers") || !strcasecmp(value, "default"))
204 return 0;
205 else if (!strcasecmp(value, "minimal"))
206 return XDF_NEED_MINIMAL;
207 else if (!strcasecmp(value, "patience"))
208 return XDF_PATIENCE_DIFF;
209 else if (!strcasecmp(value, "histogram"))
210 return XDF_HISTOGRAM_DIFF;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700211 /*
212 * Please update $__git_diff_algorithms in git-completion.bash
213 * when you add new algorithms.
214 */
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100215 return -1;
216}
217
Junio C Hamano0b4b42e2016-10-04 15:09:18 -0700218static int parse_one_token(const char **arg, const char *token)
219{
220 const char *rest;
221 if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) {
222 *arg = rest;
223 return 1;
224 }
225 return 0;
226}
227
228static int parse_ws_error_highlight(const char *arg)
229{
230 const char *orig_arg = arg;
231 unsigned val = 0;
232
233 while (*arg) {
234 if (parse_one_token(&arg, "none"))
235 val = 0;
236 else if (parse_one_token(&arg, "default"))
237 val = WSEH_NEW;
238 else if (parse_one_token(&arg, "all"))
239 val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT;
240 else if (parse_one_token(&arg, "new"))
241 val |= WSEH_NEW;
242 else if (parse_one_token(&arg, "old"))
243 val |= WSEH_OLD;
244 else if (parse_one_token(&arg, "context"))
245 val |= WSEH_CONTEXT;
246 else {
247 return -1 - (int)(arg - orig_arg);
248 }
249 if (*arg)
250 arg++;
251 }
252 return val;
253}
254
Junio C Hamanoe0e324a2007-07-07 01:49:58 -0700255/*
Junio C Hamano83ad63c2006-07-08 01:05:16 -0700256 * These are to give UI layer defaults.
257 * The core-level commands such as git-diff-files should
258 * never be affected by the setting of diff.renames
259 * the user happens to have in the configuration file.
260 */
Matthieu Moy5404c112016-02-25 09:59:21 +0100261void init_diff_ui_defaults(void)
262{
Nguyễn Thái Ngọc Duy06dba2b2017-12-27 17:18:35 +0700263 diff_detect_rename_default = DIFF_DETECT_RENAME;
Matthieu Moy5404c112016-02-25 09:59:21 +0100264}
265
Michael Haggerty5b162872016-09-05 11:44:53 +0200266int git_diff_heuristic_config(const char *var, const char *value, void *cb)
267{
Junio C Hamano3cde4e02016-12-23 12:32:22 -0800268 if (!strcmp(var, "diff.indentheuristic"))
Michael Haggerty5b162872016-09-05 11:44:53 +0200269 diff_indent_heuristic = git_config_bool(var, value);
Michael Haggerty5b162872016-09-05 11:44:53 +0200270 return 0;
271}
272
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700273static int parse_color_moved(const char *arg)
274{
275 switch (git_parse_maybe_bool(arg)) {
276 case 0:
277 return COLOR_MOVED_NO;
278 case 1:
279 return COLOR_MOVED_DEFAULT;
280 default:
281 break;
282 }
283
284 if (!strcmp(arg, "no"))
285 return COLOR_MOVED_NO;
Stefan Beller176841f2017-06-30 13:53:08 -0700286 else if (!strcmp(arg, "plain"))
287 return COLOR_MOVED_PLAIN;
Stefan Beller51da15e2018-07-16 16:05:39 -0700288 else if (!strcmp(arg, "blocks"))
289 return COLOR_MOVED_BLOCKS;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700290 else if (!strcmp(arg, "zebra"))
291 return COLOR_MOVED_ZEBRA;
292 else if (!strcmp(arg, "default"))
293 return COLOR_MOVED_DEFAULT;
Eric Sunshinee3f2f5f2018-07-24 17:58:45 -0400294 else if (!strcmp(arg, "dimmed-zebra"))
295 return COLOR_MOVED_ZEBRA_DIM;
Stefan Beller86b452e2017-06-30 13:53:09 -0700296 else if (!strcmp(arg, "dimmed_zebra"))
297 return COLOR_MOVED_ZEBRA_DIM;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700298 else
Junio C Hamano706b0b52018-08-15 15:08:22 -0700299 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700300}
301
Stefan Bellerd173e792018-11-13 13:33:57 -0800302static unsigned parse_color_moved_ws(const char *arg)
Stefan Bellerb3095712018-07-16 16:05:40 -0700303{
304 int ret = 0;
305 struct string_list l = STRING_LIST_INIT_DUP;
306 struct string_list_item *i;
307
308 string_list_split(&l, arg, ',', -1);
309
310 for_each_string_list_item(i, &l) {
311 struct strbuf sb = STRBUF_INIT;
312 strbuf_addstr(&sb, i->string);
313 strbuf_trim(&sb);
314
Phillip Woodb73bcba2018-11-23 11:16:52 +0000315 if (!strcmp(sb.buf, "no"))
316 ret = 0;
317 else if (!strcmp(sb.buf, "ignore-space-change"))
Stefan Bellerb3095712018-07-16 16:05:40 -0700318 ret |= XDF_IGNORE_WHITESPACE_CHANGE;
319 else if (!strcmp(sb.buf, "ignore-space-at-eol"))
320 ret |= XDF_IGNORE_WHITESPACE_AT_EOL;
321 else if (!strcmp(sb.buf, "ignore-all-space"))
322 ret |= XDF_IGNORE_WHITESPACE;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700323 else if (!strcmp(sb.buf, "allow-indentation-change"))
324 ret |= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE;
Stefan Bellerd173e792018-11-13 13:33:57 -0800325 else {
326 ret |= COLOR_MOVED_WS_ERROR;
327 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);
328 }
Stefan Bellerb3095712018-07-16 16:05:40 -0700329
330 strbuf_release(&sb);
331 }
332
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700333 if ((ret & COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) &&
Stefan Bellerd173e792018-11-13 13:33:57 -0800334 (ret & XDF_WHITESPACE_FLAGS)) {
Junio C Hamano15b07cb2019-01-29 12:47:53 -0800335 error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes"));
Stefan Bellerd173e792018-11-13 13:33:57 -0800336 ret |= COLOR_MOVED_WS_ERROR;
337 }
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700338
Stefan Bellerb3095712018-07-16 16:05:40 -0700339 string_list_clear(&l, 0);
340
341 return ret;
Jeff Kinge269eb72011-08-17 22:03:48 -0700342}
Junio C Hamano801235c2006-06-24 04:06:23 -0700343
344int git_diff_ui_config(const char *var, const char *value, void *cb)
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -0400345{
346 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
347 diff_use_color_default = git_config_colorbool(var, value);
348 return 0;
349 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700350 if (!strcmp(var, "diff.colormoved")) {
351 int cm = parse_color_moved(value);
352 if (cm < 0)
353 return -1;
354 diff_color_moved_default = cm;
355 return 0;
356 }
Stefan Beller626c0b52018-07-18 12:31:56 -0700357 if (!strcmp(var, "diff.colormovedws")) {
Stefan Bellerd173e792018-11-13 13:33:57 -0800358 unsigned cm = parse_color_moved_ws(value);
359 if (cm & COLOR_MOVED_WS_ERROR)
Stefan Beller626c0b52018-07-18 12:31:56 -0700360 return -1;
361 diff_color_moved_ws_default = cm;
362 return 0;
363 }
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -0400364 if (!strcmp(var, "diff.context")) {
365 diff_context_default = git_config_int(var, value);
366 if (diff_context_default < 0)
367 return -1;
368 return 0;
369 }
Vegard Nossumc4888672017-01-12 13:21:11 +0100370 if (!strcmp(var, "diff.interhunkcontext")) {
371 diff_interhunk_context_default = git_config_int(var, value);
372 if (diff_interhunk_context_default < 0)
373 return -1;
374 return 0;
375 }
Eric Wongb68ea122006-07-07 04:01:23 -0700376 if (!strcmp(var, "diff.renames")) {
Linus Torvaldscced5fb2009-04-09 11:46:15 -0700377 diff_detect_rename_default = git_config_rename(var, value);
Eric Wongb68ea122006-07-07 04:01:23 -0700378 return 0;
379 }
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700380 if (!strcmp(var, "diff.autorefreshindex")) {
381 diff_auto_refresh_index = git_config_bool(var, value);
382 return 0;
383 }
Junio C Hamanoa5a818e2008-08-18 20:08:09 -0700384 if (!strcmp(var, "diff.mnemonicprefix")) {
385 diff_mnemonic_prefix = git_config_bool(var, value);
386 return 0;
387 }
Eli Collinsf89504d2010-05-02 19:03:41 -0700388 if (!strcmp(var, "diff.noprefix")) {
389 diff_no_prefix = git_config_bool(var, value);
390 return 0;
391 }
Laurent Arnoudc28ded82020-05-22 12:46:18 +0200392 if (!strcmp(var, "diff.relative")) {
393 diff_relative = git_config_bool(var, value);
394 return 0;
395 }
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +0100396 if (!strcmp(var, "diff.statgraphwidth")) {
397 diff_stat_graph_width = git_config_int(var, value);
398 return 0;
399 }
Brian Hetrodaec8082008-07-05 01:24:43 -0400400 if (!strcmp(var, "diff.external"))
401 return git_config_string(&external_diff_cmd_cfg, var, value);
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -0600402 if (!strcmp(var, "diff.wordregex"))
403 return git_config_string(&diff_word_regex_cfg, var, value);
Samuel Bronson6d8940b2013-12-18 19:08:12 -0500404 if (!strcmp(var, "diff.orderfile"))
405 return git_config_pathname(&diff_order_file_cfg, var, value);
Junio C Hamanof1af60b2007-04-22 17:52:55 -0700406
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +0200407 if (!strcmp(var, "diff.ignoresubmodules"))
408 handle_ignore_submodules_arg(&default_diff_options, value);
409
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530410 if (!strcmp(var, "diff.submodule")) {
411 if (parse_submodule_params(&default_diff_options, value))
412 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
413 value);
414 return 0;
415 }
416
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100417 if (!strcmp(var, "diff.algorithm")) {
418 diff_algorithm = parse_algorithm_value(value);
419 if (diff_algorithm < 0)
420 return -1;
421 return 0;
422 }
423
Jeff King33c643b2017-10-13 13:24:31 -0400424 if (git_color_config(var, value, cb) < 0)
425 return -1;
426
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100427 return git_diff_basic_config(var, value, cb);
Jeff King9a1805a2008-01-04 03:59:34 -0500428}
429
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100430int git_diff_basic_config(const char *var, const char *value, void *cb)
Jeff King9a1805a2008-01-04 03:59:34 -0500431{
Jeff Kingae021d82014-06-18 15:47:50 -0400432 const char *name;
433
Linus Torvalds2b6ca6d2008-08-05 11:27:30 -0700434 if (!strcmp(var, "diff.renamelimit")) {
435 diff_rename_limit_default = git_config_int(var, value);
436 return 0;
437 }
438
Jeff King6680a082012-02-07 13:23:02 -0500439 if (userdiff_config(var, value) < 0)
440 return -1;
Jeff Kingc7534ef2008-10-26 00:45:55 -0400441
Jeff Kingae021d82014-06-18 15:47:50 -0400442 if (skip_prefix(var, "diff.color.", &name) ||
443 skip_prefix(var, "color.diff.", &name)) {
444 int slot = parse_diff_color_slot(name);
Jeff King8b8e8622009-12-12 07:25:24 -0500445 if (slot < 0)
446 return 0;
Junio C Hamano64f30e92008-02-11 10:53:56 -0800447 if (!value)
448 return config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 15:33:09 -0400449 return color_parse(value, diff_colors[slot]);
Junio C Hamano801235c2006-06-24 04:06:23 -0700450 }
Junio C Hamanof1af60b2007-04-22 17:52:55 -0700451
Jeff Kingda806352020-01-31 04:57:49 -0500452 if (!strcmp(var, "diff.wserrorhighlight")) {
453 int val = parse_ws_error_highlight(value);
454 if (val < 0)
455 return -1;
456 ws_error_highlight_default = val;
457 return 0;
458 }
459
Jim Meyeringa624eaa2008-08-15 13:39:26 +0200460 /* like GNU diff's --suppress-blank-empty option */
Johannes Schindelin950db872009-01-20 22:08:33 +0100461 if (!strcmp(var, "diff.suppressblankempty") ||
462 /* for backwards compatibility */
463 !strcmp(var, "diff.suppress-blank-empty")) {
Jim Meyeringa624eaa2008-08-15 13:39:26 +0200464 diff_suppress_blank_empty = git_config_bool(var, value);
465 return 0;
466 }
467
Johan Herland2d174952011-04-29 11:36:19 +0200468 if (!strcmp(var, "diff.dirstat")) {
Johan Herland51670fc2011-04-29 11:36:22 +0200469 struct strbuf errmsg = STRBUF_INIT;
Johan Herland712d2c72011-04-29 11:36:20 +0200470 default_diff_options.dirstat_permille = diff_dirstat_permille_default;
Johan Herland51670fc2011-04-29 11:36:22 +0200471 if (parse_dirstat_params(&default_diff_options, value, &errmsg))
Johan Herland7478ac52011-04-29 11:36:23 +0200472 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
Johan Herland51670fc2011-04-29 11:36:22 +0200473 errmsg.buf);
474 strbuf_release(&errmsg);
Johan Herland712d2c72011-04-29 11:36:20 +0200475 diff_dirstat_permille_default = default_diff_options.dirstat_permille;
Johan Herland2d174952011-04-29 11:36:19 +0200476 return 0;
477 }
478
Marc Branchaudcf5e7722017-05-08 12:03:36 -0400479 if (git_diff_heuristic_config(var, value, cb) < 0)
480 return -1;
481
Jeff King3e1dd172011-08-17 22:05:08 -0700482 return git_default_config(var, value, cb);
Junio C Hamano801235c2006-06-24 04:06:23 -0700483}
484
Junio C Hamano6973dca2006-04-21 23:57:45 -0700485static char *quote_two(const char *one, const char *two)
486{
Junio C Hamano7c37c972020-09-10 10:01:59 -0700487 int need_one = quote_c_style(one, NULL, NULL, CQUOTE_NODQ);
488 int need_two = quote_c_style(two, NULL, NULL, CQUOTE_NODQ);
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500489 struct strbuf res = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700490
491 if (need_one + need_two) {
Pierre Habouzit663af342007-09-20 00:42:15 +0200492 strbuf_addch(&res, '"');
Junio C Hamano7c37c972020-09-10 10:01:59 -0700493 quote_c_style(one, &res, NULL, CQUOTE_NODQ);
494 quote_c_style(two, &res, NULL, CQUOTE_NODQ);
Pierre Habouzit663af342007-09-20 00:42:15 +0200495 strbuf_addch(&res, '"');
496 } else {
497 strbuf_addstr(&res, one);
498 strbuf_addstr(&res, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700499 }
Pierre Habouzitb315c5c2007-09-27 12:58:23 +0200500 return strbuf_detach(&res, NULL);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700501}
502
503static const char *external_diff(void)
504{
505 static const char *external_diff_cmd = NULL;
506 static int done_preparing = 0;
507
508 if (done_preparing)
509 return external_diff_cmd;
Kim Gybels6776a842019-01-11 21:26:08 +0100510 external_diff_cmd = xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
Johannes Schindelincbe02102007-12-17 13:42:20 +0000511 if (!external_diff_cmd)
512 external_diff_cmd = external_diff_cmd_cfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700513 done_preparing = 1;
514 return external_diff_cmd;
515}
516
Michael Haggerty284098f2015-08-12 19:12:01 +0200517/*
518 * Keep track of files used for diffing. Sometimes such an entry
519 * refers to a temporary file, sometimes to an existing file, and
520 * sometimes to "/dev/null".
521 */
Junio C Hamano6973dca2006-04-21 23:57:45 -0700522static struct diff_tempfile {
Michael Haggerty284098f2015-08-12 19:12:01 +0200523 /*
524 * filename external diff should read from, or NULL if this
525 * entry is currently not in use:
526 */
527 const char *name;
528
brian m. carlsondc015052017-03-26 16:01:24 +0000529 char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano6973dca2006-04-21 23:57:45 -0700530 char mode[10];
Michael Haggerty284098f2015-08-12 19:12:01 +0200531
532 /*
533 * If this diff_tempfile instance refers to a temporary file,
534 * this tempfile object is used to manage its lifetime.
535 */
Jeff King076aa2c2017-09-05 08:15:08 -0400536 struct tempfile *tempfile;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700537} diff_temp[2];
538
Junio C Hamano6957eb92009-09-14 18:44:01 -0700539struct emit_callback {
Junio C Hamano6957eb92009-09-14 18:44:01 -0700540 int color_diff;
541 unsigned ws_rule;
542 int blank_at_eof_in_preimage;
543 int blank_at_eof_in_postimage;
544 int lno_in_preimage;
545 int lno_in_postimage;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700546 const char **label_path;
547 struct diff_words_data *diff_words;
Bo Yanga3c158d2010-05-26 15:08:02 +0800548 struct diff_options *opt;
Greg Bacon3e97c7c2009-11-19 15:12:24 -0600549 struct strbuf *header;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700550};
551
Junio C Hamano6973dca2006-04-21 23:57:45 -0700552static int count_lines(const char *data, int size)
553{
554 int count, ch, completely_empty = 1, nl_just_seen = 0;
555 count = 0;
556 while (0 < size--) {
557 ch = *data++;
558 if (ch == '\n') {
559 count++;
560 nl_just_seen = 1;
561 completely_empty = 0;
562 }
563 else {
564 nl_just_seen = 0;
565 completely_empty = 0;
566 }
567 }
568 if (completely_empty)
569 return 0;
570 if (!nl_just_seen)
571 count++; /* no trailing newline */
572 return count;
573}
574
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200575static int fill_mmfile(struct repository *r, mmfile_t *mf,
576 struct diff_filespec *one)
Junio C Hamano6957eb92009-09-14 18:44:01 -0700577{
578 if (!DIFF_FILE_VALID(one)) {
579 mf->ptr = (char *)""; /* does not matter */
580 mf->size = 0;
581 return 0;
582 }
Jonathan Tan1c37e862020-04-07 15:11:41 -0700583 else if (diff_populate_filespec(r, one, NULL))
Junio C Hamano6957eb92009-09-14 18:44:01 -0700584 return -1;
Junio C Hamanobb35fef2009-09-15 03:38:30 -0700585
Junio C Hamano6957eb92009-09-14 18:44:01 -0700586 mf->ptr = one->data;
587 mf->size = one->size;
588 return 0;
589}
590
Jeff Kingabb371a2011-02-19 03:16:32 -0500591/* 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 +0200592static unsigned long diff_filespec_size(struct repository *r,
593 struct diff_filespec *one)
Jeff Kingabb371a2011-02-19 03:16:32 -0500594{
Jonathan Tan1c37e862020-04-07 15:11:41 -0700595 struct diff_populate_filespec_options dpf_options = {
596 .check_size_only = 1,
597 };
598
Jeff Kingabb371a2011-02-19 03:16:32 -0500599 if (!DIFF_FILE_VALID(one))
600 return 0;
Jonathan Tan1c37e862020-04-07 15:11:41 -0700601 diff_populate_filespec(r, one, &dpf_options);
Jeff Kingabb371a2011-02-19 03:16:32 -0500602 return one->size;
603}
604
Junio C Hamano6957eb92009-09-14 18:44:01 -0700605static int count_trailing_blank(mmfile_t *mf, unsigned ws_rule)
606{
607 char *ptr = mf->ptr;
608 long size = mf->size;
609 int cnt = 0;
610
611 if (!size)
612 return cnt;
613 ptr += size - 1; /* pointing at the very end */
614 if (*ptr != '\n')
615 ; /* incomplete line */
616 else
617 ptr--; /* skip the last LF */
618 while (mf->ptr < ptr) {
619 char *prev_eol;
620 for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--)
621 if (*prev_eol == '\n')
622 break;
623 if (!ws_blank_line(prev_eol + 1, ptr - prev_eol, ws_rule))
624 break;
625 cnt++;
626 ptr = prev_eol - 1;
627 }
628 return cnt;
629}
630
631static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
632 struct emit_callback *ecbdata)
633{
634 int l1, l2, at;
635 unsigned ws_rule = ecbdata->ws_rule;
636 l1 = count_trailing_blank(mf1, ws_rule);
637 l2 = count_trailing_blank(mf2, ws_rule);
638 if (l2 <= l1) {
639 ecbdata->blank_at_eof_in_preimage = 0;
640 ecbdata->blank_at_eof_in_postimage = 0;
641 return;
642 }
643 at = count_lines(mf1->ptr, mf1->size);
644 ecbdata->blank_at_eof_in_preimage = (at - l1) + 1;
645
646 at = count_lines(mf2->ptr, mf2->size);
647 ecbdata->blank_at_eof_in_postimage = (at - l2) + 1;
648}
649
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -0700650static void emit_line_0(struct diff_options *o,
Stefan Beller017ac452018-08-13 18:41:19 -0700651 const char *set_sign, const char *set, unsigned reverse, const char *reset,
Junio C Hamano250f7992009-09-14 18:44:01 -0700652 int first, const char *line, int len)
Junio C Hamano6957eb92009-09-14 18:44:01 -0700653{
654 int has_trailing_newline, has_trailing_carriage_return;
Stefan Beller44410672018-08-13 18:41:22 -0700655 int needs_reset = 0; /* at the end of the line */
Bo Yanga3c158d2010-05-26 15:08:02 +0800656 FILE *file = o->file;
657
Stefan Bellerf103a6f2018-08-13 18:41:21 -0700658 fputs(diff_line_prefix(o), file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700659
Stefan Beller44410672018-08-13 18:41:22 -0700660 has_trailing_newline = (len > 0 && line[len-1] == '\n');
661 if (has_trailing_newline)
662 len--;
663
664 has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
665 if (has_trailing_carriage_return)
666 len--;
667
668 if (!len && !first)
669 goto end_of_line;
670
671 if (reverse && want_color(o->use_color)) {
672 fputs(GIT_COLOR_REVERSE, file);
673 needs_reset = 1;
Junio C Hamano250f7992009-09-14 18:44:01 -0700674 }
Junio C Hamano6957eb92009-09-14 18:44:01 -0700675
Stefan Beller44410672018-08-13 18:41:22 -0700676 if (set_sign) {
677 fputs(set_sign, file);
678 needs_reset = 1;
Junio C Hamano06a47552009-11-27 22:04:10 -0800679 }
Stefan Beller44410672018-08-13 18:41:22 -0700680
Junio C Hamano6957eb92009-09-14 18:44:01 -0700681 if (first)
Stefan Beller44410672018-08-13 18:41:22 -0700682 fputc(first, file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700683
Stefan Beller44410672018-08-13 18:41:22 -0700684 if (!len)
685 goto end_of_line;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700686
Stefan Beller44410672018-08-13 18:41:22 -0700687 if (set) {
688 if (set_sign && set != set_sign)
689 fputs(reset, file);
Junio C Hamano250f7992009-09-14 18:44:01 -0700690 fputs(set, file);
Stefan Beller44410672018-08-13 18:41:22 -0700691 needs_reset = 1;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700692 }
Stefan Beller44410672018-08-13 18:41:22 -0700693 fwrite(line, len, 1, file);
694 needs_reset = 1; /* 'line' may contain color codes. */
695
696end_of_line:
697 if (needs_reset)
698 fputs(reset, file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700699 if (has_trailing_carriage_return)
700 fputc('\r', file);
701 if (has_trailing_newline)
702 fputc('\n', file);
703}
704
Bo Yanga3c158d2010-05-26 15:08:02 +0800705static void emit_line(struct diff_options *o, const char *set, const char *reset,
Junio C Hamano250f7992009-09-14 18:44:01 -0700706 const char *line, int len)
707{
Stefan Beller44410672018-08-13 18:41:22 -0700708 emit_line_0(o, set, NULL, 0, reset, 0, line, len);
Junio C Hamano250f7992009-09-14 18:44:01 -0700709}
710
Stefan Beller36a4cef2017-06-29 17:06:49 -0700711enum diff_symbol {
Stefan Beller4eed0eb2017-06-29 17:07:01 -0700712 DIFF_SYMBOL_BINARY_DIFF_HEADER,
713 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
714 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
715 DIFF_SYMBOL_BINARY_DIFF_BODY,
716 DIFF_SYMBOL_BINARY_DIFF_FOOTER,
Stefan Beller0911c472017-06-29 17:07:02 -0700717 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
718 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
719 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
720 DIFF_SYMBOL_STATS_LINE,
Stefan Bellerbd033292017-06-29 17:07:03 -0700721 DIFF_SYMBOL_WORD_DIFF,
Stefan Beller30b7e1e2017-06-29 17:07:04 -0700722 DIFF_SYMBOL_STAT_SEP,
Stefan Beller146fdb02017-06-29 17:07:05 -0700723 DIFF_SYMBOL_SUMMARY,
Stefan Bellerf3597132017-06-29 17:07:00 -0700724 DIFF_SYMBOL_SUBMODULE_ADD,
725 DIFF_SYMBOL_SUBMODULE_DEL,
726 DIFF_SYMBOL_SUBMODULE_UNTRACKED,
727 DIFF_SYMBOL_SUBMODULE_MODIFIED,
728 DIFF_SYMBOL_SUBMODULE_HEADER,
729 DIFF_SYMBOL_SUBMODULE_ERROR,
730 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH,
Stefan Beller5af6ea92017-06-29 17:06:59 -0700731 DIFF_SYMBOL_REWRITE_DIFF,
Stefan Beller4acaaa72017-06-29 17:06:58 -0700732 DIFF_SYMBOL_BINARY_FILES,
Stefan Bellera29b0a12017-06-29 17:06:57 -0700733 DIFF_SYMBOL_HEADER,
Stefan Beller3ee8b7b2017-06-29 17:06:56 -0700734 DIFF_SYMBOL_FILEPAIR_PLUS,
735 DIFF_SYMBOL_FILEPAIR_MINUS,
Stefan Bellerff958672017-06-29 17:06:54 -0700736 DIFF_SYMBOL_WORDS_PORCELAIN,
737 DIFF_SYMBOL_WORDS,
Stefan Beller091f8e22017-06-29 17:06:53 -0700738 DIFF_SYMBOL_CONTEXT,
Stefan Bellerf2bb1212017-06-29 17:06:55 -0700739 DIFF_SYMBOL_CONTEXT_INCOMPLETE,
Stefan Beller091f8e22017-06-29 17:06:53 -0700740 DIFF_SYMBOL_PLUS,
741 DIFF_SYMBOL_MINUS,
Stefan Bellerb9cbfde2017-06-29 17:06:52 -0700742 DIFF_SYMBOL_NO_LF_EOF,
Stefan Beller68abc6f2017-06-29 17:06:51 -0700743 DIFF_SYMBOL_CONTEXT_FRAGINFO,
Stefan Bellerc64b4202017-06-29 17:06:50 -0700744 DIFF_SYMBOL_CONTEXT_MARKER,
Stefan Beller36a4cef2017-06-29 17:06:49 -0700745 DIFF_SYMBOL_SEPARATOR
746};
Stefan Beller091f8e22017-06-29 17:06:53 -0700747/*
748 * Flags for content lines:
749 * 0..12 are whitespace rules
750 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
751 * 16 is marking if the line is blank at EOF
752 */
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700753#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
754#define DIFF_SYMBOL_MOVED_LINE (1<<17)
755#define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
Stefan Beller86b452e2017-06-30 13:53:09 -0700756#define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
Stefan Beller091f8e22017-06-29 17:06:53 -0700757#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
758
Stefan Bellere6e045f2017-06-29 17:07:06 -0700759/*
760 * This struct is used when we need to buffer the output of the diff output.
761 *
762 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
763 * into the pre/post image file. This pointer could be a union with the
764 * line pointer. By storing an offset into the file instead of the literal line,
765 * we can decrease the memory footprint for the buffered output. At first we
766 * may want to only have indirection for the content lines, but we could also
767 * enhance the state for emitting prefabricated lines, e.g. the similarity
768 * score line or hunk/file headers would only need to store a number or path
769 * and then the output can be constructed later on depending on state.
770 */
771struct emitted_diff_symbol {
772 const char *line;
773 int len;
774 int flags;
Phillip Wood21536d02018-11-23 11:16:57 +0000775 int indent_off; /* Offset to first non-whitespace character */
776 int indent_width; /* The visual width of the indentation */
Phillip Wood72962e82021-12-09 10:30:09 +0000777 unsigned id;
Stefan Bellere6e045f2017-06-29 17:07:06 -0700778 enum diff_symbol s;
779};
Ævar Arnfjörð Bjarmason9865b6e2021-09-27 14:54:25 +0200780#define EMITTED_DIFF_SYMBOL_INIT { 0 }
Stefan Bellere6e045f2017-06-29 17:07:06 -0700781
782struct emitted_diff_symbols {
783 struct emitted_diff_symbol *buf;
784 int nr, alloc;
785};
Ævar Arnfjörð Bjarmason9865b6e2021-09-27 14:54:25 +0200786#define EMITTED_DIFF_SYMBOLS_INIT { 0 }
Stefan Bellere6e045f2017-06-29 17:07:06 -0700787
788static void append_emitted_diff_symbol(struct diff_options *o,
789 struct emitted_diff_symbol *e)
790{
791 struct emitted_diff_symbol *f;
792
793 ALLOC_GROW(o->emitted_symbols->buf,
794 o->emitted_symbols->nr + 1,
795 o->emitted_symbols->alloc);
796 f = &o->emitted_symbols->buf[o->emitted_symbols->nr++];
797
798 memcpy(f, e, sizeof(struct emitted_diff_symbol));
799 f->line = e->line ? xmemdupz(e->line, e->len) : NULL;
800}
801
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700802struct moved_entry {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700803 const struct emitted_diff_symbol *es;
804 struct moved_entry *next_line;
Phillip Wood72962e82021-12-09 10:30:09 +0000805 struct moved_entry *next_match;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700806};
807
Phillip Wood74d156f2018-10-04 11:07:41 +0100808struct moved_block {
809 struct moved_entry *match;
Phillip Wood21536d02018-11-23 11:16:57 +0000810 int wsd; /* The whitespace delta of this block */
Phillip Wood74d156f2018-10-04 11:07:41 +0100811};
812
Phillip Wood0cd51e92018-11-23 11:16:58 +0000813#define INDENT_BLANKLINE INT_MIN
814
Phillip Wood21536d02018-11-23 11:16:57 +0000815static void fill_es_indent_data(struct emitted_diff_symbol *es)
816{
Phillip Wood0cd51e92018-11-23 11:16:58 +0000817 unsigned int off = 0, i;
Phillip Wood21536d02018-11-23 11:16:57 +0000818 int width = 0, tab_width = es->flags & WS_TAB_WIDTH_MASK;
819 const char *s = es->line;
820 const int len = es->len;
821
822 /* skip any \v \f \r at start of indentation */
823 while (s[off] == '\f' || s[off] == '\v' ||
824 (s[off] == '\r' && off < len - 1))
825 off++;
826
827 /* calculate the visual width of indentation */
828 while(1) {
829 if (s[off] == ' ') {
830 width++;
831 off++;
832 } else if (s[off] == '\t') {
833 width += tab_width - (width % tab_width);
834 while (s[++off] == '\t')
835 width += tab_width;
836 } else {
837 break;
838 }
839 }
840
Phillip Wood0cd51e92018-11-23 11:16:58 +0000841 /* check if this line is blank */
842 for (i = off; i < len; i++)
843 if (!isspace(s[i]))
844 break;
845
846 if (i == len) {
847 es->indent_width = INDENT_BLANKLINE;
848 es->indent_off = len;
849 } else {
850 es->indent_off = off;
851 es->indent_width = width;
852 }
Phillip Wood74d156f2018-10-04 11:07:41 +0100853}
854
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700855static int compute_ws_delta(const struct emitted_diff_symbol *a,
Phillip Wood25e61902021-12-09 10:30:07 +0000856 const struct emitted_diff_symbol *b)
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700857{
Phillip Wood25e61902021-12-09 10:30:07 +0000858 int a_width = a->indent_width,
Phillip Wood21536d02018-11-23 11:16:57 +0000859 b_width = b->indent_width;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700860
Phillip Wood25e61902021-12-09 10:30:07 +0000861 if (a_width == INDENT_BLANKLINE && b_width == INDENT_BLANKLINE)
862 return INDENT_BLANKLINE;
Phillip Wood0cd51e92018-11-23 11:16:58 +0000863
Phillip Wood25e61902021-12-09 10:30:07 +0000864 return a_width - b_width;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700865}
866
Phillip Wood52d14e12021-12-09 10:30:02 +0000867static int cmp_in_block_with_wsd(const struct moved_entry *cur,
868 const struct emitted_diff_symbol *l,
869 struct moved_block *pmb)
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700870{
Phillip Wood72962e82021-12-09 10:30:09 +0000871 int a_width = cur->es->indent_width, b_width = l->indent_width;
Phillip Wood21536d02018-11-23 11:16:57 +0000872 int delta;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700873
Phillip Wood72962e82021-12-09 10:30:09 +0000874 /* The text of each line must match */
875 if (cur->es->id != l->id)
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700876 return 1;
877
Phillip Wood72962e82021-12-09 10:30:09 +0000878 /*
879 * If 'l' and 'cur' are both blank then we don't need to check the
880 * indent. We only need to check cur as we know the strings match.
881 * */
882 if (a_width == INDENT_BLANKLINE)
Phillip Wood0cd51e92018-11-23 11:16:58 +0000883 return 0;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700884
885 /*
Phillip Wood21536d02018-11-23 11:16:57 +0000886 * The indent changes of the block are known and stored in pmb->wsd;
887 * however we need to check if the indent changes of the current line
Phillip Wood72962e82021-12-09 10:30:09 +0000888 * match those of the current block.
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700889 */
Phillip Wood52d14e12021-12-09 10:30:02 +0000890 delta = b_width - a_width;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700891
Phillip Wood0cd51e92018-11-23 11:16:58 +0000892 /*
893 * If the previous lines of this block were all blank then set its
894 * whitespace delta.
895 */
896 if (pmb->wsd == INDENT_BLANKLINE)
897 pmb->wsd = delta;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700898
Phillip Wood72962e82021-12-09 10:30:09 +0000899 return delta != pmb->wsd;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700900}
901
Phillip Wood72962e82021-12-09 10:30:09 +0000902struct interned_diff_symbol {
903 struct hashmap_entry ent;
904 struct emitted_diff_symbol *es;
905};
906
907static int interned_diff_symbol_cmp(const void *hashmap_cmp_fn_data,
908 const struct hashmap_entry *eptr,
909 const struct hashmap_entry *entry_or_key,
910 const void *keydata)
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700911{
Stefan Belleree1df662018-07-16 16:05:38 -0700912 const struct diff_options *diffopt = hashmap_cmp_fn_data;
Phillip Wood25e61902021-12-09 10:30:07 +0000913 const struct emitted_diff_symbol *a, *b;
Stefan Bellerb3095712018-07-16 16:05:40 -0700914 unsigned flags = diffopt->color_moved_ws_handling
915 & XDF_WHITESPACE_FLAGS;
Stefan Belleree1df662018-07-16 16:05:38 -0700916
Phillip Wood72962e82021-12-09 10:30:09 +0000917 a = container_of(eptr, const struct interned_diff_symbol, ent)->es;
918 b = container_of(entry_or_key, const struct interned_diff_symbol, ent)->es;
Eric Wong939af162019-10-06 23:30:37 +0000919
Phillip Wood25e61902021-12-09 10:30:07 +0000920 return !xdiff_compare_lines(a->line + a->indent_off,
921 a->len - a->indent_off,
922 b->line + b->indent_off,
923 b->len - b->indent_off, flags);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700924}
925
Phillip Wood72962e82021-12-09 10:30:09 +0000926static void prepare_entry(struct diff_options *o, struct emitted_diff_symbol *l,
927 struct interned_diff_symbol *s)
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700928{
Stefan Bellerb3095712018-07-16 16:05:40 -0700929 unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS;
Phillip Wood25e61902021-12-09 10:30:07 +0000930 unsigned int hash = xdiff_hash_string(l->line + l->indent_off,
931 l->len - l->indent_off, flags);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700932
Phillip Wood72962e82021-12-09 10:30:09 +0000933 hashmap_entry_init(&s->ent, hash);
934 s->es = l;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700935}
936
Phillip Wood72962e82021-12-09 10:30:09 +0000937struct moved_entry_list {
938 struct moved_entry *add, *del;
939};
940
941static struct moved_entry_list *add_lines_to_move_detection(struct diff_options *o,
942 struct mem_pool *entry_mem_pool)
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700943{
944 struct moved_entry *prev_line = NULL;
Phillip Wood72962e82021-12-09 10:30:09 +0000945 struct mem_pool interned_pool;
946 struct hashmap interned_map;
947 struct moved_entry_list *entry_list = NULL;
948 size_t entry_list_alloc = 0;
949 unsigned id = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700950 int n;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700951
Phillip Wood72962e82021-12-09 10:30:09 +0000952 hashmap_init(&interned_map, interned_diff_symbol_cmp, o, 8096);
953 mem_pool_init(&interned_pool, 1024 * 1024);
954
955 for (n = 0; n < o->emitted_symbols->nr; n++) {
956 struct interned_diff_symbol key;
957 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
958 struct interned_diff_symbol *s;
959 struct moved_entry *entry;
960
961 if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS) {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700962 prev_line = NULL;
963 continue;
964 }
965
Phillip Wood21536d02018-11-23 11:16:57 +0000966 if (o->color_moved_ws_handling &
967 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
Phillip Wood72962e82021-12-09 10:30:09 +0000968 fill_es_indent_data(l);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700969
Phillip Wood72962e82021-12-09 10:30:09 +0000970 prepare_entry(o, l, &key);
971 s = hashmap_get_entry(&interned_map, &key, ent, &key.ent);
972 if (s) {
973 l->id = s->es->id;
974 } else {
975 l->id = id;
976 ALLOC_GROW_BY(entry_list, id, 1, entry_list_alloc);
977 hashmap_add(&interned_map,
978 memcpy(mem_pool_alloc(&interned_pool,
979 sizeof(key)),
980 &key, sizeof(key)));
981 }
982 entry = mem_pool_alloc(entry_mem_pool, sizeof(*entry));
983 entry->es = l;
984 entry->next_line = NULL;
985 if (prev_line && prev_line->es->s == l->s)
986 prev_line->next_line = entry;
987 prev_line = entry;
988 if (l->s == DIFF_SYMBOL_PLUS) {
989 entry->next_match = entry_list[l->id].add;
990 entry_list[l->id].add = entry;
991 } else {
992 entry->next_match = entry_list[l->id].del;
993 entry_list[l->id].del = entry;
994 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700995 }
Phillip Wood72962e82021-12-09 10:30:09 +0000996
997 hashmap_clear(&interned_map);
998 mem_pool_discard(&interned_pool, 0);
999
1000 return entry_list;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001001}
1002
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001003static void pmb_advance_or_null(struct diff_options *o,
Phillip Wood08fba102021-12-09 10:30:03 +00001004 struct emitted_diff_symbol *l,
Phillip Wood74d156f2018-10-04 11:07:41 +01001005 struct moved_block *pmb,
Phillip Wood0e488f12021-12-09 10:30:05 +00001006 int *pmb_nr)
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001007{
Phillip Wood0e488f12021-12-09 10:30:05 +00001008 int i, j;
Phillip Wood08fba102021-12-09 10:30:03 +00001009
Phillip Wood0e488f12021-12-09 10:30:05 +00001010 for (i = 0, j = 0; i < *pmb_nr; i++) {
Phillip Woodff046a02021-12-09 10:30:04 +00001011 int match;
Phillip Wood74d156f2018-10-04 11:07:41 +01001012 struct moved_entry *prev = pmb[i].match;
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001013 struct moved_entry *cur = (prev && prev->next_line) ?
1014 prev->next_line : NULL;
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001015
Phillip Woodff046a02021-12-09 10:30:04 +00001016 if (o->color_moved_ws_handling &
1017 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
1018 match = cur &&
1019 !cmp_in_block_with_wsd(cur, l, &pmb[i]);
1020 else
Phillip Wood72962e82021-12-09 10:30:09 +00001021 match = cur && cur->es->id == l->id;
1022
Phillip Wood0e488f12021-12-09 10:30:05 +00001023 if (match) {
1024 pmb[j] = pmb[i];
1025 pmb[j++].match = cur;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001026 }
1027 }
Phillip Wood0e488f12021-12-09 10:30:05 +00001028 *pmb_nr = j;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001029}
1030
Phillip Wood7dfe4272021-12-09 10:29:57 +00001031static void fill_potential_moved_blocks(struct diff_options *o,
Phillip Wood7dfe4272021-12-09 10:29:57 +00001032 struct moved_entry *match,
1033 struct emitted_diff_symbol *l,
1034 struct moved_block **pmb_p,
1035 int *pmb_alloc_p, int *pmb_nr_p)
1036
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001037{
Phillip Wood7dfe4272021-12-09 10:29:57 +00001038 struct moved_block *pmb = *pmb_p;
1039 int pmb_alloc = *pmb_alloc_p, pmb_nr = *pmb_nr_p;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001040
Phillip Wood7dfe4272021-12-09 10:29:57 +00001041 /*
1042 * The current line is the start of a new block.
1043 * Setup the set of potential blocks.
1044 */
Phillip Wood72962e82021-12-09 10:30:09 +00001045 for (; match; match = match->next_match) {
Phillip Wood7dfe4272021-12-09 10:29:57 +00001046 ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
1047 if (o->color_moved_ws_handling &
Phillip Wood25e61902021-12-09 10:30:07 +00001048 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
1049 pmb[pmb_nr].wsd = compute_ws_delta(l, match->es);
1050 else
Phillip Wood7dfe4272021-12-09 10:29:57 +00001051 pmb[pmb_nr].wsd = 0;
Phillip Wood25e61902021-12-09 10:30:07 +00001052 pmb[pmb_nr++].match = match;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001053 }
1054
Phillip Wood7dfe4272021-12-09 10:29:57 +00001055 *pmb_p = pmb;
1056 *pmb_alloc_p = pmb_alloc;
1057 *pmb_nr_p = pmb_nr;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001058}
1059
Jonathan Tan09153272017-08-15 18:27:38 -07001060/*
1061 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
1062 *
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001063 * Otherwise, if the last block has fewer alphanumeric characters than
1064 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
Jonathan Tan09153272017-08-15 18:27:38 -07001065 * that block.
1066 *
1067 * The last block consists of the (n - block_length)'th line up to but not
1068 * including the nth line.
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001069 *
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001070 * Returns 0 if the last block is empty or is unset by this function, non zero
1071 * otherwise.
1072 *
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001073 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
1074 * Think of a way to unify them.
Jonathan Tan09153272017-08-15 18:27:38 -07001075 */
Phillip Woodbea084b2021-12-09 10:29:56 +00001076#define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1077 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001078static int adjust_last_block(struct diff_options *o, int n, int block_length)
Jonathan Tan09153272017-08-15 18:27:38 -07001079{
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001080 int i, alnum_count = 0;
1081 if (o->color_moved == COLOR_MOVED_PLAIN)
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001082 return block_length;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001083 for (i = 1; i < block_length + 1; i++) {
1084 const char *c = o->emitted_symbols->buf[n - i].line;
1085 for (; *c; c++) {
1086 if (!isalnum(*c))
1087 continue;
1088 alnum_count++;
1089 if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT)
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001090 return 1;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001091 }
1092 }
Jonathan Tan09153272017-08-15 18:27:38 -07001093 for (i = 1; i < block_length + 1; i++)
Phillip Woodbea084b2021-12-09 10:29:56 +00001094 o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK;
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001095 return 0;
Jonathan Tan09153272017-08-15 18:27:38 -07001096}
1097
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001098/* Find blocks of moved code, delegate actual coloring decision to helper */
1099static void mark_color_as_moved(struct diff_options *o,
Phillip Wood72962e82021-12-09 10:30:09 +00001100 struct moved_entry_list *entry_list)
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001101{
Phillip Wood74d156f2018-10-04 11:07:41 +01001102 struct moved_block *pmb = NULL; /* potentially moved blocks */
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001103 int pmb_nr = 0, pmb_alloc = 0;
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001104 int n, flipped_block = 0, block_length = 0;
Phillip Woodeb893522021-12-09 10:30:00 +00001105 enum diff_symbol moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001106
1107
1108 for (n = 0; n < o->emitted_symbols->nr; n++) {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001109 struct moved_entry *match = NULL;
1110 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001111
1112 switch (l->s) {
1113 case DIFF_SYMBOL_PLUS:
Phillip Wood72962e82021-12-09 10:30:09 +00001114 match = entry_list[l->id].del;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001115 break;
1116 case DIFF_SYMBOL_MINUS:
Phillip Wood72962e82021-12-09 10:30:09 +00001117 match = entry_list[l->id].add;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001118 break;
1119 default:
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001120 flipped_block = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001121 }
1122
Phillip Woodeb893522021-12-09 10:30:00 +00001123 if (pmb_nr && (!match || l->s != moved_symbol)) {
Phillip Wood09906582021-12-09 10:29:58 +00001124 if (!adjust_last_block(o, n, block_length) &&
1125 block_length > 1) {
1126 /*
1127 * Rewind in case there is another match
1128 * starting at the second line of the block
1129 */
1130 match = NULL;
1131 n -= block_length;
1132 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001133 pmb_nr = 0;
1134 block_length = 0;
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001135 flipped_block = 0;
Phillip Woodeb893522021-12-09 10:30:00 +00001136 }
1137 if (!match) {
1138 moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001139 continue;
1140 }
1141
Phillip Wood2034b472018-11-23 11:16:54 +00001142 if (o->color_moved == COLOR_MOVED_PLAIN) {
1143 l->flags |= DIFF_SYMBOL_MOVED_LINE;
Stefan Beller176841f2017-06-30 13:53:08 -07001144 continue;
Phillip Wood2034b472018-11-23 11:16:54 +00001145 }
Stefan Beller176841f2017-06-30 13:53:08 -07001146
Phillip Wood0e488f12021-12-09 10:30:05 +00001147 pmb_advance_or_null(o, l, pmb, &pmb_nr);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001148
1149 if (pmb_nr == 0) {
Phillip Wood09906582021-12-09 10:29:58 +00001150 int contiguous = adjust_last_block(o, n, block_length);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001151
Phillip Wood09906582021-12-09 10:29:58 +00001152 if (!contiguous && block_length > 1)
1153 /*
1154 * Rewind in case there is another match
1155 * starting at the second line of the block
1156 */
1157 n -= block_length;
1158 else
Phillip Wood72962e82021-12-09 10:30:09 +00001159 fill_potential_moved_blocks(o, match, l,
Phillip Wood09906582021-12-09 10:29:58 +00001160 &pmb, &pmb_alloc,
1161 &pmb_nr);
1162
Phillip Woodeb893522021-12-09 10:30:00 +00001163 if (contiguous && pmb_nr && moved_symbol == l->s)
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001164 flipped_block = (flipped_block + 1) % 2;
1165 else
1166 flipped_block = 0;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001167
Phillip Woodeb893522021-12-09 10:30:00 +00001168 if (pmb_nr)
1169 moved_symbol = l->s;
1170 else
1171 moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER;
1172
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001173 block_length = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001174 }
1175
Phillip Wood2034b472018-11-23 11:16:54 +00001176 if (pmb_nr) {
1177 block_length++;
Phillip Wood2034b472018-11-23 11:16:54 +00001178 l->flags |= DIFF_SYMBOL_MOVED_LINE;
1179 if (flipped_block && o->color_moved != COLOR_MOVED_BLOCKS)
1180 l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
1181 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001182 }
Jonathan Tan09153272017-08-15 18:27:38 -07001183 adjust_last_block(o, n, block_length);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001184
1185 free(pmb);
1186}
Stefan Bellere6e045f2017-06-29 17:07:06 -07001187
Stefan Beller86b452e2017-06-30 13:53:09 -07001188static void dim_moved_lines(struct diff_options *o)
1189{
1190 int n;
1191 for (n = 0; n < o->emitted_symbols->nr; n++) {
1192 struct emitted_diff_symbol *prev = (n != 0) ?
1193 &o->emitted_symbols->buf[n - 1] : NULL;
1194 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
1195 struct emitted_diff_symbol *next =
1196 (n < o->emitted_symbols->nr - 1) ?
1197 &o->emitted_symbols->buf[n + 1] : NULL;
1198
1199 /* Not a plus or minus line? */
1200 if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS)
1201 continue;
1202
1203 /* Not a moved line? */
1204 if (!(l->flags & DIFF_SYMBOL_MOVED_LINE))
1205 continue;
1206
1207 /*
1208 * If prev or next are not a plus or minus line,
1209 * pretend they don't exist
1210 */
1211 if (prev && prev->s != DIFF_SYMBOL_PLUS &&
1212 prev->s != DIFF_SYMBOL_MINUS)
1213 prev = NULL;
1214 if (next && next->s != DIFF_SYMBOL_PLUS &&
1215 next->s != DIFF_SYMBOL_MINUS)
1216 next = NULL;
1217
1218 /* Inside a block? */
1219 if ((prev &&
1220 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1221 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) &&
1222 (next &&
1223 (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1224 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) {
1225 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1226 continue;
1227 }
1228
1229 /* Check if we are at an interesting bound: */
1230 if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) &&
1231 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1232 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1233 continue;
1234 if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) &&
1235 (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1236 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1237 continue;
1238
1239 /*
1240 * The boundary to prev and next are not interesting,
1241 * so this line is not interesting as a whole
1242 */
1243 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1244 }
1245}
1246
Stefan Beller091f8e22017-06-29 17:06:53 -07001247static void emit_line_ws_markup(struct diff_options *o,
Stefan Beller9d1e16b2018-08-13 18:41:18 -07001248 const char *set_sign, const char *set,
1249 const char *reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001250 int sign_index, const char *line, int len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001251 unsigned ws_rule, int blank_at_eof)
1252{
1253 const char *ws = NULL;
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001254 int sign = o->output_indicators[sign_index];
Stefan Beller091f8e22017-06-29 17:06:53 -07001255
1256 if (o->ws_error_highlight & ws_rule) {
1257 ws = diff_get_color_opt(o, DIFF_WHITESPACE);
1258 if (!*ws)
1259 ws = NULL;
1260 }
1261
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001262 if (!ws && !set_sign)
Stefan Beller017ac452018-08-13 18:41:19 -07001263 emit_line_0(o, set, NULL, 0, reset, sign, line, len);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001264 else if (!ws) {
Stefan Beller29ef7592018-08-13 18:41:20 -07001265 emit_line_0(o, set_sign, set, !!set_sign, reset, sign, line, len);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001266 } else if (blank_at_eof)
Stefan Beller091f8e22017-06-29 17:06:53 -07001267 /* Blank line at EOF - paint '+' as well */
Stefan Beller017ac452018-08-13 18:41:19 -07001268 emit_line_0(o, ws, NULL, 0, reset, sign, line, len);
Stefan Beller091f8e22017-06-29 17:06:53 -07001269 else {
1270 /* Emit just the prefix, then the rest. */
Stefan Beller017ac452018-08-13 18:41:19 -07001271 emit_line_0(o, set_sign ? set_sign : set, NULL, !!set_sign, reset,
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001272 sign, "", 0);
Stefan Beller091f8e22017-06-29 17:06:53 -07001273 ws_check_emit(line, len, ws_rule,
1274 o->file, set, reset, ws);
1275 }
1276}
Stefan Beller36a4cef2017-06-29 17:06:49 -07001277
Stefan Bellere6e045f2017-06-29 17:07:06 -07001278static void emit_diff_symbol_from_struct(struct diff_options *o,
1279 struct emitted_diff_symbol *eds)
Stefan Beller36a4cef2017-06-29 17:06:49 -07001280{
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001281 static const char *nneof = " No newline at end of file\n";
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001282 const char *context, *reset, *set, *set_sign, *meta, *fraginfo;
Stefan Beller0911c472017-06-29 17:07:02 -07001283 struct strbuf sb = STRBUF_INIT;
Stefan Bellere6e045f2017-06-29 17:07:06 -07001284
1285 enum diff_symbol s = eds->s;
1286 const char *line = eds->line;
1287 int len = eds->len;
1288 unsigned flags = eds->flags;
1289
Stefan Beller36a4cef2017-06-29 17:06:49 -07001290 switch (s) {
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001291 case DIFF_SYMBOL_NO_LF_EOF:
1292 context = diff_get_color_opt(o, DIFF_CONTEXT);
1293 reset = diff_get_color_opt(o, DIFF_RESET);
1294 putc('\n', o->file);
Stefan Beller017ac452018-08-13 18:41:19 -07001295 emit_line_0(o, context, NULL, 0, reset, '\\',
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001296 nneof, strlen(nneof));
1297 break;
Stefan Bellerf3597132017-06-29 17:07:00 -07001298 case DIFF_SYMBOL_SUBMODULE_HEADER:
1299 case DIFF_SYMBOL_SUBMODULE_ERROR:
1300 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
Stefan Beller0911c472017-06-29 17:07:02 -07001301 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
Stefan Beller146fdb02017-06-29 17:07:05 -07001302 case DIFF_SYMBOL_SUMMARY:
Stefan Beller0911c472017-06-29 17:07:02 -07001303 case DIFF_SYMBOL_STATS_LINE:
Stefan Beller4eed0eb2017-06-29 17:07:01 -07001304 case DIFF_SYMBOL_BINARY_DIFF_BODY:
Stefan Beller68abc6f2017-06-29 17:06:51 -07001305 case DIFF_SYMBOL_CONTEXT_FRAGINFO:
1306 emit_line(o, "", "", line, len);
1307 break;
Stefan Bellerf2bb1212017-06-29 17:06:55 -07001308 case DIFF_SYMBOL_CONTEXT_INCOMPLETE:
Stefan Bellerc64b4202017-06-29 17:06:50 -07001309 case DIFF_SYMBOL_CONTEXT_MARKER:
1310 context = diff_get_color_opt(o, DIFF_CONTEXT);
1311 reset = diff_get_color_opt(o, DIFF_RESET);
1312 emit_line(o, context, reset, line, len);
1313 break;
Stefan Beller36a4cef2017-06-29 17:06:49 -07001314 case DIFF_SYMBOL_SEPARATOR:
1315 fprintf(o->file, "%s%c",
1316 diff_line_prefix(o),
1317 o->line_termination);
1318 break;
Stefan Beller091f8e22017-06-29 17:06:53 -07001319 case DIFF_SYMBOL_CONTEXT:
1320 set = diff_get_color_opt(o, DIFF_CONTEXT);
1321 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001322 set_sign = NULL;
1323 if (o->flags.dual_color_diffed_diffs) {
1324 char c = !len ? 0 : line[0];
1325
1326 if (c == '+')
1327 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1328 else if (c == '@')
1329 set = diff_get_color_opt(o, DIFF_FRAGINFO);
1330 else if (c == '-')
1331 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1332 }
Stefan Beller7648b792018-08-17 13:43:52 -07001333 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001334 OUTPUT_INDICATOR_CONTEXT, line, len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001335 flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
1336 break;
1337 case DIFF_SYMBOL_PLUS:
Stefan Beller86b452e2017-06-30 13:53:09 -07001338 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1339 DIFF_SYMBOL_MOVED_LINE_ALT |
1340 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1341 case DIFF_SYMBOL_MOVED_LINE |
1342 DIFF_SYMBOL_MOVED_LINE_ALT |
1343 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1344 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM);
1345 break;
1346 case DIFF_SYMBOL_MOVED_LINE |
1347 DIFF_SYMBOL_MOVED_LINE_ALT:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001348 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT);
Stefan Beller86b452e2017-06-30 13:53:09 -07001349 break;
1350 case DIFF_SYMBOL_MOVED_LINE |
1351 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1352 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM);
1353 break;
1354 case DIFF_SYMBOL_MOVED_LINE:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001355 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED);
Stefan Beller86b452e2017-06-30 13:53:09 -07001356 break;
1357 default:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001358 set = diff_get_color_opt(o, DIFF_FILE_NEW);
Stefan Beller86b452e2017-06-30 13:53:09 -07001359 }
Stefan Beller091f8e22017-06-29 17:06:53 -07001360 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001361 if (!o->flags.dual_color_diffed_diffs)
1362 set_sign = NULL;
1363 else {
1364 char c = !len ? 0 : line[0];
1365
1366 set_sign = set;
1367 if (c == '-')
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001368 set = diff_get_color_opt(o, DIFF_FILE_OLD_BOLD);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001369 else if (c == '@')
1370 set = diff_get_color_opt(o, DIFF_FRAGINFO);
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001371 else if (c == '+')
1372 set = diff_get_color_opt(o, DIFF_FILE_NEW_BOLD);
1373 else
1374 set = diff_get_color_opt(o, DIFF_CONTEXT_BOLD);
Johannes Schindelin0b91faa2018-08-13 04:33:24 -07001375 flags &= ~DIFF_SYMBOL_CONTENT_WS_MASK;
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001376 }
Stefan Beller7648b792018-08-17 13:43:52 -07001377 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001378 OUTPUT_INDICATOR_NEW, line, len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001379 flags & DIFF_SYMBOL_CONTENT_WS_MASK,
1380 flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
1381 break;
1382 case DIFF_SYMBOL_MINUS:
Stefan Beller86b452e2017-06-30 13:53:09 -07001383 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1384 DIFF_SYMBOL_MOVED_LINE_ALT |
1385 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1386 case DIFF_SYMBOL_MOVED_LINE |
1387 DIFF_SYMBOL_MOVED_LINE_ALT |
1388 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1389 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM);
1390 break;
1391 case DIFF_SYMBOL_MOVED_LINE |
1392 DIFF_SYMBOL_MOVED_LINE_ALT:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001393 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT);
Stefan Beller86b452e2017-06-30 13:53:09 -07001394 break;
1395 case DIFF_SYMBOL_MOVED_LINE |
1396 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1397 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM);
1398 break;
1399 case DIFF_SYMBOL_MOVED_LINE:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001400 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED);
Stefan Beller86b452e2017-06-30 13:53:09 -07001401 break;
1402 default:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001403 set = diff_get_color_opt(o, DIFF_FILE_OLD);
Stefan Beller86b452e2017-06-30 13:53:09 -07001404 }
Stefan Beller091f8e22017-06-29 17:06:53 -07001405 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001406 if (!o->flags.dual_color_diffed_diffs)
1407 set_sign = NULL;
1408 else {
1409 char c = !len ? 0 : line[0];
1410
1411 set_sign = set;
1412 if (c == '+')
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001413 set = diff_get_color_opt(o, DIFF_FILE_NEW_DIM);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001414 else if (c == '@')
1415 set = diff_get_color_opt(o, DIFF_FRAGINFO);
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001416 else if (c == '-')
1417 set = diff_get_color_opt(o, DIFF_FILE_OLD_DIM);
1418 else
1419 set = diff_get_color_opt(o, DIFF_CONTEXT_DIM);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001420 }
Stefan Beller7648b792018-08-17 13:43:52 -07001421 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001422 OUTPUT_INDICATOR_OLD, line, len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001423 flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
1424 break;
Stefan Bellerff958672017-06-29 17:06:54 -07001425 case DIFF_SYMBOL_WORDS_PORCELAIN:
1426 context = diff_get_color_opt(o, DIFF_CONTEXT);
1427 reset = diff_get_color_opt(o, DIFF_RESET);
1428 emit_line(o, context, reset, line, len);
1429 fputs("~\n", o->file);
1430 break;
1431 case DIFF_SYMBOL_WORDS:
1432 context = diff_get_color_opt(o, DIFF_CONTEXT);
1433 reset = diff_get_color_opt(o, DIFF_RESET);
1434 /*
1435 * Skip the prefix character, if any. With
1436 * diff_suppress_blank_empty, there may be
1437 * none.
1438 */
1439 if (line[0] != '\n') {
1440 line++;
1441 len--;
1442 }
1443 emit_line(o, context, reset, line, len);
1444 break;
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07001445 case DIFF_SYMBOL_FILEPAIR_PLUS:
1446 meta = diff_get_color_opt(o, DIFF_METAINFO);
1447 reset = diff_get_color_opt(o, DIFF_RESET);
1448 fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
1449 line, reset,
1450 strchr(line, ' ') ? "\t" : "");
1451 break;
1452 case DIFF_SYMBOL_FILEPAIR_MINUS:
1453 meta = diff_get_color_opt(o, DIFF_METAINFO);
1454 reset = diff_get_color_opt(o, DIFF_RESET);
1455 fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta,
1456 line, reset,
1457 strchr(line, ' ') ? "\t" : "");
1458 break;
Stefan Beller4acaaa72017-06-29 17:06:58 -07001459 case DIFF_SYMBOL_BINARY_FILES:
Stefan Bellera29b0a12017-06-29 17:06:57 -07001460 case DIFF_SYMBOL_HEADER:
1461 fprintf(o->file, "%s", line);
1462 break;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07001463 case DIFF_SYMBOL_BINARY_DIFF_HEADER:
1464 fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o));
1465 break;
1466 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA:
1467 fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line);
1468 break;
1469 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL:
1470 fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line);
1471 break;
1472 case DIFF_SYMBOL_BINARY_DIFF_FOOTER:
1473 fputs(diff_line_prefix(o), o->file);
1474 fputc('\n', o->file);
1475 break;
Stefan Beller5af6ea92017-06-29 17:06:59 -07001476 case DIFF_SYMBOL_REWRITE_DIFF:
1477 fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
1478 reset = diff_get_color_opt(o, DIFF_RESET);
1479 emit_line(o, fraginfo, reset, line, len);
1480 break;
Stefan Bellerf3597132017-06-29 17:07:00 -07001481 case DIFF_SYMBOL_SUBMODULE_ADD:
1482 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1483 reset = diff_get_color_opt(o, DIFF_RESET);
1484 emit_line(o, set, reset, line, len);
1485 break;
1486 case DIFF_SYMBOL_SUBMODULE_DEL:
1487 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1488 reset = diff_get_color_opt(o, DIFF_RESET);
1489 emit_line(o, set, reset, line, len);
1490 break;
1491 case DIFF_SYMBOL_SUBMODULE_UNTRACKED:
1492 fprintf(o->file, "%sSubmodule %s contains untracked content\n",
1493 diff_line_prefix(o), line);
1494 break;
1495 case DIFF_SYMBOL_SUBMODULE_MODIFIED:
1496 fprintf(o->file, "%sSubmodule %s contains modified content\n",
1497 diff_line_prefix(o), line);
1498 break;
Stefan Beller0911c472017-06-29 17:07:02 -07001499 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES:
1500 emit_line(o, "", "", " 0 files changed\n",
1501 strlen(" 0 files changed\n"));
1502 break;
1503 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV:
1504 emit_line(o, "", "", " ...\n", strlen(" ...\n"));
1505 break;
Stefan Bellerbd033292017-06-29 17:07:03 -07001506 case DIFF_SYMBOL_WORD_DIFF:
1507 fprintf(o->file, "%.*s", len, line);
1508 break;
Stefan Beller30b7e1e2017-06-29 17:07:04 -07001509 case DIFF_SYMBOL_STAT_SEP:
1510 fputs(o->stat_sep, o->file);
1511 break;
Stefan Beller36a4cef2017-06-29 17:06:49 -07001512 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +02001513 BUG("unknown diff symbol");
Stefan Beller36a4cef2017-06-29 17:06:49 -07001514 }
Stefan Beller0911c472017-06-29 17:07:02 -07001515 strbuf_release(&sb);
Stefan Beller36a4cef2017-06-29 17:06:49 -07001516}
1517
Stefan Bellere6e045f2017-06-29 17:07:06 -07001518static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
1519 const char *line, int len, unsigned flags)
1520{
Phillip Woodb4a5c5c2021-12-09 10:30:08 +00001521 struct emitted_diff_symbol e = {
1522 .line = line, .len = len, .flags = flags, .s = s
1523 };
Stefan Bellere6e045f2017-06-29 17:07:06 -07001524
1525 if (o->emitted_symbols)
1526 append_emitted_diff_symbol(o, &e);
1527 else
1528 emit_diff_symbol_from_struct(o, &e);
1529}
1530
Stefan Bellerf3597132017-06-29 17:07:00 -07001531void diff_emit_submodule_del(struct diff_options *o, const char *line)
1532{
1533 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0);
1534}
1535
1536void diff_emit_submodule_add(struct diff_options *o, const char *line)
1537{
1538 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0);
1539}
1540
1541void diff_emit_submodule_untracked(struct diff_options *o, const char *path)
1542{
1543 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED,
1544 path, strlen(path), 0);
1545}
1546
1547void diff_emit_submodule_modified(struct diff_options *o, const char *path)
1548{
1549 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED,
1550 path, strlen(path), 0);
1551}
1552
1553void diff_emit_submodule_header(struct diff_options *o, const char *header)
1554{
1555 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER,
1556 header, strlen(header), 0);
1557}
1558
1559void diff_emit_submodule_error(struct diff_options *o, const char *err)
1560{
1561 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0);
1562}
1563
1564void diff_emit_submodule_pipethrough(struct diff_options *o,
1565 const char *line, int len)
1566{
1567 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0);
1568}
1569
Junio C Hamano6957eb92009-09-14 18:44:01 -07001570static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
1571{
1572 if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
1573 ecbdata->blank_at_eof_in_preimage &&
1574 ecbdata->blank_at_eof_in_postimage &&
1575 ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
1576 ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
1577 return 0;
Junio C Hamano018cff72009-09-14 18:44:01 -07001578 return ws_blank_line(line, len, ecbdata->ws_rule);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001579}
1580
Jeff Kinge04df612019-02-14 00:48:13 -05001581static void emit_add_line(struct emit_callback *ecbdata,
Junio C Hamano018cff72009-09-14 18:44:01 -07001582 const char *line, int len)
Junio C Hamano6957eb92009-09-14 18:44:01 -07001583{
Stefan Beller091f8e22017-06-29 17:06:53 -07001584 unsigned flags = WSEH_NEW | ecbdata->ws_rule;
1585 if (new_blank_line_at_eof(ecbdata, line, len))
1586 flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF;
1587
1588 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001589}
Junio C Hamano6957eb92009-09-14 18:44:01 -07001590
Jeff Kinge04df612019-02-14 00:48:13 -05001591static void emit_del_line(struct emit_callback *ecbdata,
Junio C Hamano0e383e12015-05-26 09:56:33 -07001592 const char *line, int len)
1593{
Stefan Beller091f8e22017-06-29 17:06:53 -07001594 unsigned flags = WSEH_OLD | ecbdata->ws_rule;
1595 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags);
Junio C Hamano0e383e12015-05-26 09:56:33 -07001596}
1597
Jeff Kinge04df612019-02-14 00:48:13 -05001598static void emit_context_line(struct emit_callback *ecbdata,
Junio C Hamano0e383e12015-05-26 09:56:33 -07001599 const char *line, int len)
1600{
Stefan Beller091f8e22017-06-29 17:06:53 -07001601 unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule;
1602 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001603}
1604
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001605static void emit_hunk_header(struct emit_callback *ecbdata,
1606 const char *line, int len)
1607{
Jeff King8dbf3eb2015-05-27 16:48:46 -04001608 const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001609 const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
1610 const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
1611 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001612 const char *reverse = ecbdata->color_diff ? GIT_COLOR_REVERSE : "";
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001613 static const char atat[2] = { '@', '@' };
1614 const char *cp, *ep;
Bo Yang2efcc972010-05-29 23:32:05 +08001615 struct strbuf msgbuf = STRBUF_INIT;
1616 int org_len = len;
1617 int i = 1;
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001618
1619 /*
1620 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1621 * it always is at least 10 bytes long.
1622 */
1623 if (len < 10 ||
1624 memcmp(line, atat, 2) ||
1625 !(ep = memmem(line + 2, len - 2, atat, 2))) {
Stefan Bellerc64b4202017-06-29 17:06:50 -07001626 emit_diff_symbol(ecbdata->opt,
Stefan Beller091f8e22017-06-29 17:06:53 -07001627 DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001628 return;
1629 }
1630 ep += 2; /* skip over @@ */
1631
1632 /* The hunk header in fraginfo color */
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001633 if (ecbdata->opt->flags.dual_color_diffed_diffs)
1634 strbuf_addstr(&msgbuf, reverse);
René Scharfecedc61a2014-07-17 01:38:18 +02001635 strbuf_addstr(&msgbuf, frag);
Thomas Gummerer430be362019-07-11 17:08:48 +01001636 if (ecbdata->opt->flags.suppress_hunk_header_line_count)
1637 strbuf_add(&msgbuf, atat, sizeof(atat));
1638 else
1639 strbuf_add(&msgbuf, line, ep - line);
René Scharfecedc61a2014-07-17 01:38:18 +02001640 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001641
1642 /*
1643 * trailing "\r\n"
1644 */
1645 for ( ; i < 3; i++)
1646 if (line[len - i] == '\r' || line[len - i] == '\n')
1647 len--;
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001648
1649 /* blank before the func header */
1650 for (cp = ep; ep - line < len; ep++)
1651 if (*ep != ' ' && *ep != '\t')
1652 break;
Bo Yang2efcc972010-05-29 23:32:05 +08001653 if (ep != cp) {
Jeff King8dbf3eb2015-05-27 16:48:46 -04001654 strbuf_addstr(&msgbuf, context);
Bo Yang2efcc972010-05-29 23:32:05 +08001655 strbuf_add(&msgbuf, cp, ep - cp);
René Scharfecedc61a2014-07-17 01:38:18 +02001656 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001657 }
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001658
Bo Yang2efcc972010-05-29 23:32:05 +08001659 if (ep < line + len) {
René Scharfecedc61a2014-07-17 01:38:18 +02001660 strbuf_addstr(&msgbuf, func);
Bo Yang2efcc972010-05-29 23:32:05 +08001661 strbuf_add(&msgbuf, ep, line + len - ep);
René Scharfecedc61a2014-07-17 01:38:18 +02001662 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001663 }
1664
1665 strbuf_add(&msgbuf, line + len, org_len - len);
Stefan Bellerdfb77282017-06-29 17:06:47 -07001666 strbuf_complete_line(&msgbuf);
Stefan Beller68abc6f2017-06-29 17:06:51 -07001667 emit_diff_symbol(ecbdata->opt,
Stefan Beller091f8e22017-06-29 17:06:53 -07001668 DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0);
Bo Yang2efcc972010-05-29 23:32:05 +08001669 strbuf_release(&msgbuf);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001670}
1671
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +01001672static struct diff_tempfile *claim_diff_tempfile(void)
1673{
Jeff King479b0ae2009-01-22 00:59:56 -05001674 int i;
1675 for (i = 0; i < ARRAY_SIZE(diff_temp); i++)
1676 if (!diff_temp[i].name)
1677 return diff_temp + i;
Johannes Schindelin033abf92018-05-02 11:38:39 +02001678 BUG("diff is failing to clean up its tempfiles");
Jeff King479b0ae2009-01-22 00:59:56 -05001679}
1680
Jeff King479b0ae2009-01-22 00:59:56 -05001681static void remove_tempfile(void)
1682{
1683 int i;
Nazri Ramliya8344ab2009-02-12 21:36:14 +08001684 for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
Jeff King076aa2c2017-09-05 08:15:08 -04001685 if (is_tempfile_active(diff_temp[i].tempfile))
Michael Haggerty284098f2015-08-12 19:12:01 +02001686 delete_tempfile(&diff_temp[i].tempfile);
Nazri Ramliya8344ab2009-02-12 21:36:14 +08001687 diff_temp[i].name = NULL;
1688 }
Jeff King479b0ae2009-01-22 00:59:56 -05001689}
1690
Stefan Beller5af6ea92017-06-29 17:06:59 -07001691static void add_line_count(struct strbuf *out, int count)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001692{
1693 switch (count) {
1694 case 0:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001695 strbuf_addstr(out, "0,0");
Junio C Hamano6973dca2006-04-21 23:57:45 -07001696 break;
1697 case 1:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001698 strbuf_addstr(out, "1");
Junio C Hamano6973dca2006-04-21 23:57:45 -07001699 break;
1700 default:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001701 strbuf_addf(out, "1,%d", count);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001702 break;
1703 }
1704}
1705
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001706static void emit_rewrite_lines(struct emit_callback *ecb,
1707 int prefix, const char *data, int size)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001708{
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001709 const char *endp = NULL;
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001710
1711 while (0 < size) {
1712 int len;
1713
1714 endp = memchr(data, '\n', size);
1715 len = endp ? (endp - data + 1) : size;
1716 if (prefix != '+') {
1717 ecb->lno_in_preimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05001718 emit_del_line(ecb, data, len);
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001719 } else {
1720 ecb->lno_in_postimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05001721 emit_add_line(ecb, data, len);
Johannes Schindelin13e36ec2007-02-20 15:08:46 +01001722 }
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001723 size -= len;
1724 data += len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07001725 }
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001726 if (!endp)
Stefan Beller091f8e22017-06-29 17:06:53 -07001727 emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001728}
1729
1730static void emit_rewrite_diff(const char *name_a,
1731 const char *name_b,
1732 struct diff_filespec *one,
Johannes Schindelin13e36ec2007-02-20 15:08:46 +01001733 struct diff_filespec *two,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04001734 struct userdiff_driver *textconv_one,
1735 struct userdiff_driver *textconv_two,
Johannes Schindelineab9a402007-12-18 19:32:14 +00001736 struct diff_options *o)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001737{
1738 int lc_a, lc_b;
Junio C Hamanod5625092007-12-26 17:13:36 -08001739 static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001740 const char *a_prefix, *b_prefix;
Jeff King840383b2010-04-01 20:09:26 -04001741 char *data_one, *data_two;
Jeff King3aa1f7c2008-12-09 03:13:21 -05001742 size_t size_one, size_two;
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001743 struct emit_callback ecbdata;
Stefan Beller5af6ea92017-06-29 17:06:59 -07001744 struct strbuf out = STRBUF_INIT;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001745
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001746 if (diff_mnemonic_prefix && o->flags.reverse_diff) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001747 a_prefix = o->b_prefix;
1748 b_prefix = o->a_prefix;
1749 } else {
1750 a_prefix = o->a_prefix;
1751 b_prefix = o->b_prefix;
1752 }
Junio C Hamano1a9eb3b2006-09-22 16:17:58 -07001753
Junio C Hamano8a13bec2007-02-24 01:42:06 -08001754 name_a += (*name_a == '/');
1755 name_b += (*name_b == '/');
Junio C Hamano1a9eb3b2006-09-22 16:17:58 -07001756
Junio C Hamanod5625092007-12-26 17:13:36 -08001757 strbuf_reset(&a_name);
1758 strbuf_reset(&b_name);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001759 quote_two_c_style(&a_name, a_prefix, name_a, 0);
1760 quote_two_c_style(&b_name, b_prefix, name_b, 0);
Junio C Hamanod5625092007-12-26 17:13:36 -08001761
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001762 size_one = fill_textconv(o->repo, textconv_one, one, &data_one);
1763 size_two = fill_textconv(o->repo, textconv_two, two, &data_two);
Jeff King3aa1f7c2008-12-09 03:13:21 -05001764
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001765 memset(&ecbdata, 0, sizeof(ecbdata));
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07001766 ecbdata.color_diff = want_color(o->use_color);
Nguyễn Thái Ngọc Duy26d024e2018-09-21 17:57:37 +02001767 ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
Bo Yanga3c158d2010-05-26 15:08:02 +08001768 ecbdata.opt = o;
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001769 if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
1770 mmfile_t mf1, mf2;
1771 mf1.ptr = (char *)data_one;
1772 mf2.ptr = (char *)data_two;
1773 mf1.size = size_one;
1774 mf2.size = size_two;
1775 check_blank_at_eof(&mf1, &mf2, &ecbdata);
1776 }
1777 ecbdata.lno_in_preimage = 1;
1778 ecbdata.lno_in_postimage = 1;
1779
Jeff King3aa1f7c2008-12-09 03:13:21 -05001780 lc_a = count_lines(data_one, size_one);
1781 lc_b = count_lines(data_two, size_two);
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07001782
1783 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
1784 a_name.buf, a_name.len, 0);
1785 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
1786 b_name.buf, b_name.len, 0);
1787
Stefan Beller5af6ea92017-06-29 17:06:59 -07001788 strbuf_addstr(&out, "@@ -");
Junio C Hamano467ddc12011-02-28 16:11:55 -08001789 if (!o->irreversible_delete)
Stefan Beller5af6ea92017-06-29 17:06:59 -07001790 add_line_count(&out, lc_a);
Junio C Hamano467ddc12011-02-28 16:11:55 -08001791 else
Stefan Beller5af6ea92017-06-29 17:06:59 -07001792 strbuf_addstr(&out, "?,?");
1793 strbuf_addstr(&out, " +");
1794 add_line_count(&out, lc_b);
1795 strbuf_addstr(&out, " @@\n");
1796 emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
1797 strbuf_release(&out);
1798
Junio C Hamano467ddc12011-02-28 16:11:55 -08001799 if (lc_a && !o->irreversible_delete)
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001800 emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001801 if (lc_b)
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001802 emit_rewrite_lines(&ecbdata, '+', data_two, size_two);
Jeff Kingb76c0562010-04-01 20:04:14 -04001803 if (textconv_one)
Junio C Hamanoaed6ca52010-04-08 23:30:49 -07001804 free((char *)data_one);
Jeff Kingb76c0562010-04-01 20:04:14 -04001805 if (textconv_two)
Junio C Hamanoaed6ca52010-04-08 23:30:49 -07001806 free((char *)data_two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001807}
1808
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001809struct diff_words_buffer {
1810 mmfile_t text;
Ramsay Jones071bcaa2017-09-21 17:49:38 +01001811 unsigned long alloc;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001812 struct diff_words_orig {
1813 const char *begin, *end;
1814 } *orig;
1815 int orig_nr, orig_alloc;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001816};
1817
1818static void diff_words_append(char *line, unsigned long len,
1819 struct diff_words_buffer *buffer)
1820{
Johannes Schindelin23c15752009-01-17 17:29:43 +01001821 ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc);
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001822 line++;
1823 len--;
1824 memcpy(buffer->text.ptr + buffer->text.size, line, len);
1825 buffer->text.size += len;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001826 buffer->text.ptr[buffer->text.size] = '\0';
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001827}
1828
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001829struct diff_words_style_elem {
Thomas Rast882749a2010-04-14 17:59:06 +02001830 const char *prefix;
1831 const char *suffix;
1832 const char *color; /* NULL; filled in by the setup code if
1833 * color is enabled */
1834};
1835
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001836struct diff_words_style {
Thomas Rast882749a2010-04-14 17:59:06 +02001837 enum diff_words_type type;
Brandon Williams63a01c32018-02-14 10:59:39 -08001838 struct diff_words_style_elem new_word, old_word, ctx;
Thomas Rast882749a2010-04-14 17:59:06 +02001839 const char *newline;
1840};
1841
Stephen Boydc2e86ad2011-03-22 00:51:05 -07001842static struct diff_words_style diff_words_styles[] = {
Thomas Rast882749a2010-04-14 17:59:06 +02001843 { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1844 { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1845 { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" }
1846};
1847
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001848struct diff_words_data {
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001849 struct diff_words_buffer minus, plus;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001850 const char *current_plus;
Bo Yang4297c0a2010-05-29 23:32:06 +08001851 int last_minus;
1852 struct diff_options *opt;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001853 regex_t *word_regex;
Thomas Rast882749a2010-04-14 17:59:06 +02001854 enum diff_words_type type;
1855 struct diff_words_style *style;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001856};
1857
Stefan Bellerbd033292017-06-29 17:07:03 -07001858static int fn_out_diff_words_write_helper(struct diff_options *o,
Thomas Rast882749a2010-04-14 17:59:06 +02001859 struct diff_words_style_elem *st_el,
1860 const char *newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07001861 size_t count, const char *buf)
Thomas Rast882749a2010-04-14 17:59:06 +02001862{
Bo Yang4297c0a2010-05-29 23:32:06 +08001863 int print = 0;
Stefan Bellerbd033292017-06-29 17:07:03 -07001864 struct strbuf sb = STRBUF_INIT;
Bo Yang4297c0a2010-05-29 23:32:06 +08001865
Thomas Rast882749a2010-04-14 17:59:06 +02001866 while (count) {
1867 char *p = memchr(buf, '\n', count);
Bo Yang4297c0a2010-05-29 23:32:06 +08001868 if (print)
Stefan Bellerbd033292017-06-29 17:07:03 -07001869 strbuf_addstr(&sb, diff_line_prefix(o));
1870
Thomas Rast882749a2010-04-14 17:59:06 +02001871 if (p != buf) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001872 const char *reset = st_el->color && *st_el->color ?
1873 GIT_COLOR_RESET : NULL;
1874 if (st_el->color && *st_el->color)
1875 strbuf_addstr(&sb, st_el->color);
1876 strbuf_addstr(&sb, st_el->prefix);
1877 strbuf_add(&sb, buf, p ? p - buf : count);
1878 strbuf_addstr(&sb, st_el->suffix);
1879 if (reset)
1880 strbuf_addstr(&sb, reset);
Thomas Rast882749a2010-04-14 17:59:06 +02001881 }
1882 if (!p)
Stefan Bellerbd033292017-06-29 17:07:03 -07001883 goto out;
1884
1885 strbuf_addstr(&sb, newline);
Thomas Rast882749a2010-04-14 17:59:06 +02001886 count -= p + 1 - buf;
1887 buf = p + 1;
Bo Yang4297c0a2010-05-29 23:32:06 +08001888 print = 1;
Stefan Bellerbd033292017-06-29 17:07:03 -07001889 if (count) {
1890 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1891 sb.buf, sb.len, 0);
1892 strbuf_reset(&sb);
1893 }
Thomas Rast882749a2010-04-14 17:59:06 +02001894 }
Stefan Bellerbd033292017-06-29 17:07:03 -07001895
1896out:
1897 if (sb.len)
1898 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1899 sb.buf, sb.len, 0);
1900 strbuf_release(&sb);
Thomas Rast882749a2010-04-14 17:59:06 +02001901 return 0;
1902}
1903
Bo Yang4297c0a2010-05-29 23:32:06 +08001904/*
1905 * '--color-words' algorithm can be described as:
1906 *
René Genz56217602017-04-30 17:48:27 +02001907 * 1. collect the minus/plus lines of a diff hunk, divided into
Bo Yang4297c0a2010-05-29 23:32:06 +08001908 * minus-lines and plus-lines;
1909 *
1910 * 2. break both minus-lines and plus-lines into words and
1911 * place them into two mmfile_t with one word for each line;
1912 *
1913 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1914 *
1915 * And for the common parts of the both file, we output the plus side text.
1916 * diff_words->current_plus is used to trace the current position of the plus file
1917 * which printed. diff_words->last_minus is used to trace the last minus word
1918 * printed.
1919 *
1920 * For '--graph' to work with '--color-words', we need to output the graph prefix
1921 * on each line of color words output. Generally, there are two conditions on
1922 * which we should output the prefix.
1923 *
1924 * 1. diff_words->last_minus == 0 &&
1925 * diff_words->current_plus == diff_words->plus.text.ptr
1926 *
1927 * that is: the plus text must start as a new line, and if there is no minus
1928 * word printed, a graph prefix must be printed.
1929 *
1930 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1931 * *(diff_words->current_plus - 1) == '\n'
1932 *
1933 * that is: a graph prefix must be printed following a '\n'
1934 */
1935static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
1936{
1937 if ((diff_words->last_minus == 0 &&
1938 diff_words->current_plus == diff_words->plus.text.ptr) ||
1939 (diff_words->current_plus > diff_words->plus.text.ptr &&
1940 *(diff_words->current_plus - 1) == '\n')) {
1941 return 1;
1942 } else {
1943 return 0;
1944 }
1945}
1946
Jeff King7c61e252018-11-02 02:37:18 -04001947static void fn_out_diff_words_aux(void *priv,
1948 long minus_first, long minus_len,
1949 long plus_first, long plus_len,
1950 const char *func, long funclen)
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001951{
1952 struct diff_words_data *diff_words = priv;
Thomas Rast882749a2010-04-14 17:59:06 +02001953 struct diff_words_style *style = diff_words->style;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001954 const char *minus_begin, *minus_end, *plus_begin, *plus_end;
Bo Yang4297c0a2010-05-29 23:32:06 +08001955 struct diff_options *opt = diff_words->opt;
John Keeping30997bb2013-02-07 20:15:27 +00001956 const char *line_prefix;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001957
Bo Yang4297c0a2010-05-29 23:32:06 +08001958 assert(opt);
John Keeping30997bb2013-02-07 20:15:27 +00001959 line_prefix = diff_line_prefix(opt);
Bo Yang4297c0a2010-05-29 23:32:06 +08001960
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001961 /* POSIX requires that first be decremented by one if len == 0... */
1962 if (minus_len) {
1963 minus_begin = diff_words->minus.orig[minus_first].begin;
1964 minus_end =
1965 diff_words->minus.orig[minus_first + minus_len - 1].end;
1966 } else
1967 minus_begin = minus_end =
1968 diff_words->minus.orig[minus_first].end;
1969
1970 if (plus_len) {
1971 plus_begin = diff_words->plus.orig[plus_first].begin;
1972 plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end;
1973 } else
1974 plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
1975
Bo Yang4297c0a2010-05-29 23:32:06 +08001976 if (color_words_output_graph_prefix(diff_words)) {
1977 fputs(line_prefix, diff_words->opt->file);
1978 }
1979 if (diff_words->current_plus != plus_begin) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001980 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02001981 &style->ctx, style->newline,
1982 plus_begin - diff_words->current_plus,
Stefan Bellerbd033292017-06-29 17:07:03 -07001983 diff_words->current_plus);
Bo Yang4297c0a2010-05-29 23:32:06 +08001984 }
1985 if (minus_begin != minus_end) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001986 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 10:59:39 -08001987 &style->old_word, style->newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07001988 minus_end - minus_begin, minus_begin);
Bo Yang4297c0a2010-05-29 23:32:06 +08001989 }
1990 if (plus_begin != plus_end) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001991 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 10:59:39 -08001992 &style->new_word, style->newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07001993 plus_end - plus_begin, plus_begin);
Bo Yang4297c0a2010-05-29 23:32:06 +08001994 }
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001995
1996 diff_words->current_plus = plus_end;
Bo Yang4297c0a2010-05-29 23:32:06 +08001997 diff_words->last_minus = minus_first;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001998}
1999
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002000/* This function starts looking at *begin, and returns 0 iff a word was found. */
2001static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
2002 int *begin, int *end)
2003{
Phillip Wood0324e8f2021-05-04 09:27:34 +00002004 while (word_regex && *begin < buffer->size) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002005 regmatch_t match[1];
Johannes Schindelinb7d36ff2016-09-21 20:24:14 +02002006 if (!regexec_buf(word_regex, buffer->ptr + *begin,
2007 buffer->size - *begin, 1, match, 0)) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002008 char *p = memchr(buffer->ptr + *begin + match[0].rm_so,
2009 '\n', match[0].rm_eo - match[0].rm_so);
2010 *end = p ? p - buffer->ptr : match[0].rm_eo + *begin;
2011 *begin += match[0].rm_so;
Phillip Wood0324e8f2021-05-04 09:27:34 +00002012 if (*begin == *end)
2013 (*begin)++;
2014 else
2015 return *begin > *end;
2016 } else {
2017 return -1;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002018 }
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002019 }
2020
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002021 /* find the next word */
2022 while (*begin < buffer->size && isspace(buffer->ptr[*begin]))
2023 (*begin)++;
2024 if (*begin >= buffer->size)
2025 return -1;
2026
2027 /* find the end of the word */
2028 *end = *begin + 1;
2029 while (*end < buffer->size && !isspace(buffer->ptr[*end]))
2030 (*end)++;
2031
2032 return 0;
2033}
2034
Johannes Schindelin23c15752009-01-17 17:29:43 +01002035/*
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002036 * This function splits the words in buffer->text, stores the list with
2037 * newline separator into out, and saves the offsets of the original words
2038 * in buffer->orig.
Johannes Schindelin23c15752009-01-17 17:29:43 +01002039 */
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002040static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out,
2041 regex_t *word_regex)
Johannes Schindelin23c15752009-01-17 17:29:43 +01002042{
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002043 int i, j;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002044 long alloc = 0;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002045
2046 out->size = 0;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002047 out->ptr = NULL;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002048
2049 /* fake an empty "0th" word */
2050 ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc);
2051 buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr;
2052 buffer->orig_nr = 1;
2053
2054 for (i = 0; i < buffer->text.size; i++) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002055 if (find_word_boundaries(&buffer->text, word_regex, &i, &j))
2056 return;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002057
2058 /* store original boundaries */
2059 ALLOC_GROW(buffer->orig, buffer->orig_nr + 1,
2060 buffer->orig_alloc);
2061 buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i;
2062 buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j;
2063 buffer->orig_nr++;
2064
2065 /* store one word */
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002066 ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002067 memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i);
2068 out->ptr[out->size + j - i] = '\n';
2069 out->size += j - i + 1;
2070
2071 i = j - 1;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002072 }
2073}
2074
2075/* this executes the word diff on the accumulated buffers */
2076static void diff_words_show(struct diff_words_data *diff_words)
2077{
2078 xpparam_t xpp;
2079 xdemitconf_t xecfg;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002080 mmfile_t minus, plus;
Thomas Rast882749a2010-04-14 17:59:06 +02002081 struct diff_words_style *style = diff_words->style;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002082
Bo Yang4297c0a2010-05-29 23:32:06 +08002083 struct diff_options *opt = diff_words->opt;
John Keeping30997bb2013-02-07 20:15:27 +00002084 const char *line_prefix;
Bo Yang4297c0a2010-05-29 23:32:06 +08002085
2086 assert(opt);
John Keeping30997bb2013-02-07 20:15:27 +00002087 line_prefix = diff_line_prefix(opt);
Bo Yang4297c0a2010-05-29 23:32:06 +08002088
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002089 /* special case: only removal */
2090 if (!diff_words->plus.text.size) {
Stefan Bellerbd033292017-06-29 17:07:03 -07002091 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
2092 line_prefix, strlen(line_prefix), 0);
2093 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 10:59:39 -08002094 &style->old_word, style->newline,
Bo Yang4297c0a2010-05-29 23:32:06 +08002095 diff_words->minus.text.size,
Stefan Bellerbd033292017-06-29 17:07:03 -07002096 diff_words->minus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002097 diff_words->minus.text.size = 0;
2098 return;
2099 }
2100
2101 diff_words->current_plus = diff_words->plus.text.ptr;
Bo Yang4297c0a2010-05-29 23:32:06 +08002102 diff_words->last_minus = 0;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002103
Brian Downing9ccd0a82008-10-25 15:30:37 +02002104 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01002105 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002106 diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex);
2107 diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex);
René Scharfe582aa002010-05-02 15:04:41 +02002108 xpp.flags = 0;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002109 /* as only the hunk header will be parsed, we need a 0-context */
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002110 xecfg.ctxlen = 0;
Jeff King7c61e252018-11-02 02:37:18 -04002111 if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, NULL,
Jeff King9346d6d2018-11-02 02:35:45 -04002112 diff_words, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04002113 die("unable to generate word diff");
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002114 free(minus.ptr);
2115 free(plus.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002116 if (diff_words->current_plus != diff_words->plus.text.ptr +
Bo Yang4297c0a2010-05-29 23:32:06 +08002117 diff_words->plus.text.size) {
2118 if (color_words_output_graph_prefix(diff_words))
Stefan Bellerbd033292017-06-29 17:07:03 -07002119 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
2120 line_prefix, strlen(line_prefix), 0);
2121 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02002122 &style->ctx, style->newline,
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002123 diff_words->plus.text.ptr + diff_words->plus.text.size
Stefan Bellerbd033292017-06-29 17:07:03 -07002124 - diff_words->current_plus, diff_words->current_plus);
Bo Yang4297c0a2010-05-29 23:32:06 +08002125 }
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002126 diff_words->minus.text.size = diff_words->plus.text.size = 0;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002127}
2128
Junio C Hamano76fd2822009-10-30 10:09:06 -07002129/* In "color-words" mode, show word-diff of words accumulated in the buffer */
2130static void diff_words_flush(struct emit_callback *ecbdata)
2131{
Stefan Bellere6e045f2017-06-29 17:07:06 -07002132 struct diff_options *wo = ecbdata->diff_words->opt;
2133
Junio C Hamano76fd2822009-10-30 10:09:06 -07002134 if (ecbdata->diff_words->minus.text.size ||
2135 ecbdata->diff_words->plus.text.size)
2136 diff_words_show(ecbdata->diff_words);
Stefan Bellere6e045f2017-06-29 17:07:06 -07002137
2138 if (wo->emitted_symbols) {
2139 struct diff_options *o = ecbdata->opt;
2140 struct emitted_diff_symbols *wol = wo->emitted_symbols;
2141 int i;
2142
2143 /*
2144 * NEEDSWORK:
2145 * Instead of appending each, concat all words to a line?
2146 */
2147 for (i = 0; i < wol->nr; i++)
2148 append_emitted_diff_symbol(o, &wol->buf[i]);
2149
2150 for (i = 0; i < wol->nr; i++)
2151 free((void *)wol->buf[i].line);
2152
2153 wol->nr = 0;
2154 }
Junio C Hamano76fd2822009-10-30 10:09:06 -07002155}
2156
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002157static void diff_filespec_load_driver(struct diff_filespec *one,
2158 struct index_state *istate)
Thomas Rast77d1a522012-03-14 19:24:08 +01002159{
2160 /* Use already-loaded driver */
2161 if (one->driver)
2162 return;
2163
2164 if (S_ISREG(one->mode))
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002165 one->driver = userdiff_find_by_path(istate, one->path);
Thomas Rast77d1a522012-03-14 19:24:08 +01002166
2167 /* Fallback to default settings */
2168 if (!one->driver)
2169 one->driver = userdiff_find_by_name("default");
2170}
2171
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002172static const char *userdiff_word_regex(struct diff_filespec *one,
2173 struct index_state *istate)
Thomas Rast77d1a522012-03-14 19:24:08 +01002174{
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002175 diff_filespec_load_driver(one, istate);
Thomas Rast77d1a522012-03-14 19:24:08 +01002176 return one->driver->word_regex;
2177}
2178
2179static void init_diff_words_data(struct emit_callback *ecbdata,
Thomas Rast6440d342012-03-14 19:24:09 +01002180 struct diff_options *orig_opts,
Thomas Rast77d1a522012-03-14 19:24:08 +01002181 struct diff_filespec *one,
2182 struct diff_filespec *two)
2183{
2184 int i;
Thomas Rast6440d342012-03-14 19:24:09 +01002185 struct diff_options *o = xmalloc(sizeof(struct diff_options));
2186 memcpy(o, orig_opts, sizeof(struct diff_options));
Thomas Rast77d1a522012-03-14 19:24:08 +01002187
René Scharfeca56dad2021-03-13 17:17:22 +01002188 CALLOC_ARRAY(ecbdata->diff_words, 1);
Thomas Rast77d1a522012-03-14 19:24:08 +01002189 ecbdata->diff_words->type = o->word_diff;
2190 ecbdata->diff_words->opt = o;
Stefan Bellere6e045f2017-06-29 17:07:06 -07002191
2192 if (orig_opts->emitted_symbols)
René Scharfeca56dad2021-03-13 17:17:22 +01002193 CALLOC_ARRAY(o->emitted_symbols, 1);
Stefan Bellere6e045f2017-06-29 17:07:06 -07002194
Thomas Rast77d1a522012-03-14 19:24:08 +01002195 if (!o->word_regex)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002196 o->word_regex = userdiff_word_regex(one, o->repo->index);
Thomas Rast77d1a522012-03-14 19:24:08 +01002197 if (!o->word_regex)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002198 o->word_regex = userdiff_word_regex(two, o->repo->index);
Thomas Rast77d1a522012-03-14 19:24:08 +01002199 if (!o->word_regex)
2200 o->word_regex = diff_word_regex_cfg;
2201 if (o->word_regex) {
2202 ecbdata->diff_words->word_regex = (regex_t *)
2203 xmalloc(sizeof(regex_t));
2204 if (regcomp(ecbdata->diff_words->word_regex,
2205 o->word_regex,
2206 REG_EXTENDED | REG_NEWLINE))
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02002207 die("invalid regular expression: %s",
2208 o->word_regex);
Thomas Rast77d1a522012-03-14 19:24:08 +01002209 }
2210 for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) {
2211 if (o->word_diff == diff_words_styles[i].type) {
2212 ecbdata->diff_words->style =
2213 &diff_words_styles[i];
2214 break;
2215 }
2216 }
2217 if (want_color(o->use_color)) {
2218 struct diff_words_style *st = ecbdata->diff_words->style;
Brandon Williams63a01c32018-02-14 10:59:39 -08002219 st->old_word.color = diff_get_color_opt(o, DIFF_FILE_OLD);
2220 st->new_word.color = diff_get_color_opt(o, DIFF_FILE_NEW);
Jeff King8dbf3eb2015-05-27 16:48:46 -04002221 st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT);
Thomas Rast77d1a522012-03-14 19:24:08 +01002222 }
2223}
2224
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002225static void free_diff_words_data(struct emit_callback *ecbdata)
2226{
2227 if (ecbdata->diff_words) {
Junio C Hamano76fd2822009-10-30 10:09:06 -07002228 diff_words_flush(ecbdata);
Stefan Bellere6e045f2017-06-29 17:07:06 -07002229 free (ecbdata->diff_words->opt->emitted_symbols);
Thomas Rast6440d342012-03-14 19:24:09 +01002230 free (ecbdata->diff_words->opt);
Jim Meyering8e0f7002008-01-31 18:26:32 +01002231 free (ecbdata->diff_words->minus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002232 free (ecbdata->diff_words->minus.orig);
Jim Meyering8e0f7002008-01-31 18:26:32 +01002233 free (ecbdata->diff_words->plus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002234 free (ecbdata->diff_words->plus.orig);
Brandon Caseyef5644e2010-09-09 14:02:45 -05002235 if (ecbdata->diff_words->word_regex) {
2236 regfree(ecbdata->diff_words->word_regex);
2237 free(ecbdata->diff_words->word_regex);
2238 }
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002239 FREE_AND_NULL(ecbdata->diff_words);
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002240 }
2241}
2242
Jeff Kingce436972006-07-23 05:24:18 -04002243const char *diff_get_color(int diff_use_color, enum color_diff ix)
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002244{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07002245 if (want_color(diff_use_color))
Linus Torvalds50f575f2006-06-22 13:53:31 -07002246 return diff_colors[ix];
2247 return "";
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002248}
2249
John Keepingf1922232013-02-07 20:15:26 +00002250const char *diff_line_prefix(struct diff_options *opt)
2251{
2252 struct strbuf *msgbuf;
2253 if (!opt->output_prefix)
2254 return "";
2255
2256 msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
2257 return msgbuf->buf;
2258}
2259
Jeff King19b90462019-02-14 00:48:27 -05002260static unsigned long sane_truncate_line(char *line, unsigned long len)
Junio C Hamano23707812008-01-02 01:50:11 -08002261{
2262 const char *cp;
2263 unsigned long allot;
2264 size_t l = len;
2265
Junio C Hamano23707812008-01-02 01:50:11 -08002266 cp = line;
2267 allot = l;
2268 while (0 < l) {
2269 (void) utf8_width(&cp, &l);
2270 if (!cp)
2271 break; /* truncated in the middle? */
2272 }
2273 return allot - l;
2274}
2275
Junio C Hamanod68fe262009-09-14 22:05:57 -07002276static void find_lno(const char *line, struct emit_callback *ecbdata)
Junio C Hamano690ed842009-09-04 00:41:15 -07002277{
Junio C Hamanod68fe262009-09-14 22:05:57 -07002278 const char *p;
2279 ecbdata->lno_in_preimage = 0;
2280 ecbdata->lno_in_postimage = 0;
2281 p = strchr(line, '-');
Junio C Hamano690ed842009-09-04 00:41:15 -07002282 if (!p)
Junio C Hamanod68fe262009-09-14 22:05:57 -07002283 return; /* cannot happen */
2284 ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10);
2285 p = strchr(p, '+');
2286 if (!p)
2287 return; /* cannot happen */
2288 ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10);
Junio C Hamano690ed842009-09-04 00:41:15 -07002289}
2290
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002291static int fn_out_consume(void *priv, char *line, unsigned long len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002292{
Junio C Hamano6973dca2006-04-21 23:57:45 -07002293 struct emit_callback *ecbdata = priv;
Bo Yang7be57612010-05-26 15:23:54 +08002294 struct diff_options *o = ecbdata->opt;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002295
Stefan Bellerba162332016-09-07 16:36:46 -07002296 o->found_changes = 1;
2297
Greg Bacon3e97c7c2009-11-19 15:12:24 -06002298 if (ecbdata->header) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07002299 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
2300 ecbdata->header->buf, ecbdata->header->len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06002301 strbuf_reset(ecbdata->header);
2302 ecbdata->header = NULL;
2303 }
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01002304
Junio C Hamano6973dca2006-04-21 23:57:45 -07002305 if (ecbdata->label_path[0]) {
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07002306 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
2307 ecbdata->label_path[0],
2308 strlen(ecbdata->label_path[0]), 0);
2309 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
2310 ecbdata->label_path[1],
2311 strlen(ecbdata->label_path[1]), 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002312 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
2313 }
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002314
Jim Meyeringa624eaa2008-08-15 13:39:26 +02002315 if (diff_suppress_blank_empty
2316 && len == 2 && line[0] == ' ' && line[1] == '\n') {
2317 line[0] = '\n';
2318 len = 1;
2319 }
2320
Junio C Hamanob8d9c1a2009-09-03 23:59:25 -07002321 if (line[0] == '@') {
Junio C Hamano76fd2822009-10-30 10:09:06 -07002322 if (ecbdata->diff_words)
2323 diff_words_flush(ecbdata);
Jeff King19b90462019-02-14 00:48:27 -05002324 len = sane_truncate_line(line, len);
Junio C Hamanod68fe262009-09-14 22:05:57 -07002325 find_lno(line, ecbdata);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01002326 emit_hunk_header(ecbdata, line, len);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002327 return 0;
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002328 }
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002329
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002330 if (ecbdata->diff_words) {
Stefan Bellerff958672017-06-29 17:06:54 -07002331 enum diff_symbol s =
2332 ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ?
2333 DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002334 if (line[0] == '-') {
2335 diff_words_append(line, len,
2336 &ecbdata->diff_words->minus);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002337 return 0;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002338 } else if (line[0] == '+') {
2339 diff_words_append(line, len,
2340 &ecbdata->diff_words->plus);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002341 return 0;
Christian Couder59556542013-11-30 21:55:40 +01002342 } else if (starts_with(line, "\\ ")) {
Thomas Rastc7c2bc02012-01-12 12:15:33 +01002343 /*
2344 * Eat the "no newline at eof" marker as if we
2345 * saw a "+" or "-" line with nothing on it,
2346 * and return without diff_words_flush() to
2347 * defer processing. If this is the end of
2348 * preimage, more "+" lines may come after it.
2349 */
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002350 return 0;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002351 }
Junio C Hamano76fd2822009-10-30 10:09:06 -07002352 diff_words_flush(ecbdata);
Stefan Bellerff958672017-06-29 17:06:54 -07002353 emit_diff_symbol(o, s, line, len, 0);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002354 return 0;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002355 }
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002356
Junio C Hamano0e383e12015-05-26 09:56:33 -07002357 switch (line[0]) {
2358 case '+':
Junio C Hamanod68fe262009-09-14 22:05:57 -07002359 ecbdata->lno_in_postimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05002360 emit_add_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002361 break;
2362 case '-':
2363 ecbdata->lno_in_preimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05002364 emit_del_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002365 break;
2366 case ' ':
2367 ecbdata->lno_in_postimage++;
2368 ecbdata->lno_in_preimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05002369 emit_context_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002370 break;
2371 default:
2372 /* incomplete line at the end */
2373 ecbdata->lno_in_preimage++;
Stefan Bellerf2bb1212017-06-29 17:06:55 -07002374 emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE,
2375 line, len, 0);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002376 break;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002377 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002378 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002379}
2380
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002381static void pprint_rename(struct strbuf *name, const char *a, const char *b)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002382{
Brandon Williams63a01c32018-02-14 10:59:39 -08002383 const char *old_name = a;
2384 const char *new_name = b;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002385 int pfx_length, sfx_length;
Thomas Rastdd281f02013-02-26 21:47:01 +01002386 int pfx_adjust_for_slash;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002387 int len_a = strlen(a);
2388 int len_b = strlen(b);
Pierre Habouzit663af342007-09-20 00:42:15 +02002389 int a_midlen, b_midlen;
Alexandre Julliarde5bfbf92007-02-10 15:39:00 +01002390 int qlen_a = quote_c_style(a, NULL, NULL, 0);
2391 int qlen_b = quote_c_style(b, NULL, NULL, 0);
2392
2393 if (qlen_a || qlen_b) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002394 quote_c_style(a, name, NULL, 0);
2395 strbuf_addstr(name, " => ");
2396 quote_c_style(b, name, NULL, 0);
2397 return;
Alexandre Julliarde5bfbf92007-02-10 15:39:00 +01002398 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002399
2400 /* Find common prefix */
2401 pfx_length = 0;
Brandon Williams63a01c32018-02-14 10:59:39 -08002402 while (*old_name && *new_name && *old_name == *new_name) {
2403 if (*old_name == '/')
2404 pfx_length = old_name - a + 1;
2405 old_name++;
2406 new_name++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002407 }
2408
2409 /* Find common suffix */
Brandon Williams63a01c32018-02-14 10:59:39 -08002410 old_name = a + len_a;
2411 new_name = b + len_b;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002412 sfx_length = 0;
Antoine Pelissed020e272013-02-23 17:48:45 +01002413 /*
Thomas Rastdd281f02013-02-26 21:47:01 +01002414 * If there is a common prefix, it must end in a slash. In
2415 * that case we let this loop run 1 into the prefix to see the
2416 * same slash.
2417 *
2418 * If there is no common prefix, we cannot do this as it would
2419 * underrun the input strings.
Antoine Pelissed020e272013-02-23 17:48:45 +01002420 */
Thomas Rastdd281f02013-02-26 21:47:01 +01002421 pfx_adjust_for_slash = (pfx_length ? 1 : 0);
Brandon Williams63a01c32018-02-14 10:59:39 -08002422 while (a + pfx_length - pfx_adjust_for_slash <= old_name &&
2423 b + pfx_length - pfx_adjust_for_slash <= new_name &&
2424 *old_name == *new_name) {
2425 if (*old_name == '/')
2426 sfx_length = len_a - (old_name - a);
2427 old_name--;
2428 new_name--;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002429 }
2430
2431 /*
2432 * pfx{mid-a => mid-b}sfx
2433 * {pfx-a => pfx-b}sfx
2434 * pfx{sfx-a => sfx-b}
2435 * name-a => name-b
2436 */
Pierre Habouzit663af342007-09-20 00:42:15 +02002437 a_midlen = len_a - pfx_length - sfx_length;
2438 b_midlen = len_b - pfx_length - sfx_length;
2439 if (a_midlen < 0)
2440 a_midlen = 0;
2441 if (b_midlen < 0)
2442 b_midlen = 0;
Junio C Hamanocc908b82006-05-14 22:07:28 -07002443
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002444 strbuf_grow(name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
Pierre Habouzit663af342007-09-20 00:42:15 +02002445 if (pfx_length + sfx_length) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002446 strbuf_add(name, a, pfx_length);
2447 strbuf_addch(name, '{');
Junio C Hamano6973dca2006-04-21 23:57:45 -07002448 }
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002449 strbuf_add(name, a + pfx_length, a_midlen);
2450 strbuf_addstr(name, " => ");
2451 strbuf_add(name, b + pfx_length, b_midlen);
Pierre Habouzit663af342007-09-20 00:42:15 +02002452 if (pfx_length + sfx_length) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002453 strbuf_addch(name, '}');
2454 strbuf_add(name, a + len_a - sfx_length, sfx_length);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002455 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002456}
2457
Junio C Hamano6973dca2006-04-21 23:57:45 -07002458static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
2459 const char *name_a,
2460 const char *name_b)
2461{
2462 struct diffstat_file *x;
René Scharfeca56dad2021-03-13 17:17:22 +01002463 CALLOC_ARRAY(x, 1);
Dmitry S. Dolzhenko4c960a42014-03-04 02:31:53 +04002464 ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002465 diffstat->files[diffstat->nr++] = x;
2466 if (name_b) {
Junio C Hamanof6046522007-12-11 23:46:30 -08002467 x->from_name = xstrdup(name_a);
2468 x->name = xstrdup(name_b);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002469 x->is_renamed = 1;
2470 }
Junio C Hamanof6046522007-12-11 23:46:30 -08002471 else {
2472 x->from_name = NULL;
Shawn Pearce9befac42006-09-02 00:16:31 -04002473 x->name = xstrdup(name_a);
Junio C Hamanof6046522007-12-11 23:46:30 -08002474 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002475 return x;
2476}
2477
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002478static int diffstat_consume(void *priv, char *line, unsigned long len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002479{
2480 struct diffstat_t *diffstat = priv;
2481 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
2482
2483 if (line[0] == '+')
2484 x->added++;
2485 else if (line[0] == '-')
2486 x->deleted++;
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002487 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002488}
2489
Johannes Schindelin698ce6f2006-05-20 15:40:29 +02002490const char mime_boundary_leader[] = "------------";
Junio C Hamano6973dca2006-04-21 23:57:45 -07002491
Junio C Hamanoa2540022006-09-26 18:53:02 -07002492static int scale_linear(int it, int width, int max_change)
2493{
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002494 if (!it)
2495 return 0;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002496 /*
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002497 * make sure that at least one '-' or '+' is printed if
2498 * there is any change to this path. The easiest way is to
Elijah Newren15beaaa2019-11-05 17:07:23 +00002499 * scale linearly as if the allotted width is one column shorter
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002500 * than it is, and then add 1 to the result.
Junio C Hamanoa2540022006-09-26 18:53:02 -07002501 */
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002502 return 1 + (it * (width - 1) / max_change);
Junio C Hamanoa2540022006-09-26 18:53:02 -07002503}
2504
Stefan Beller0911c472017-06-29 17:07:02 -07002505static void show_graph(struct strbuf *out, char ch, int cnt,
2506 const char *set, const char *reset)
Junio C Hamanoa2540022006-09-26 18:53:02 -07002507{
2508 if (cnt <= 0)
2509 return;
Stefan Beller0911c472017-06-29 17:07:02 -07002510 strbuf_addstr(out, set);
2511 strbuf_addchars(out, ch, cnt);
2512 strbuf_addstr(out, reset);
Junio C Hamanoa2540022006-09-26 18:53:02 -07002513}
2514
Junio C Hamanof6046522007-12-11 23:46:30 -08002515static void fill_print_name(struct diffstat_file *file)
2516{
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002517 struct strbuf pname = STRBUF_INIT;
Junio C Hamanof6046522007-12-11 23:46:30 -08002518
2519 if (file->print_name)
2520 return;
2521
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002522 if (file->is_renamed)
2523 pprint_rename(&pname, file->from_name, file->name);
2524 else
2525 quote_c_style(file->name, &pname, NULL, 0);
2526
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 21:09:59 +07002527 if (file->comments)
2528 strbuf_addf(&pname, " (%s)", file->comments);
2529
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002530 file->print_name = strbuf_detach(&pname, NULL);
Junio C Hamanof6046522007-12-11 23:46:30 -08002531}
2532
Stefan Beller0911c472017-06-29 17:07:02 -07002533static void print_stat_summary_inserts_deletes(struct diff_options *options,
2534 int files, int insertions, int deletions)
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002535{
2536 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002537
2538 if (!files) {
2539 assert(insertions == 0 && deletions == 0);
Stefan Beller0911c472017-06-29 17:07:02 -07002540 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
2541 NULL, 0, 0);
2542 return;
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002543 }
2544
2545 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002546 (files == 1) ? " %d file changed" : " %d files changed",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002547 files);
2548
2549 /*
2550 * For binary diff, the caller may want to print "x files
2551 * changed" with insertions == 0 && deletions == 0.
2552 *
2553 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2554 * is probably less confusing (i.e skip over "2 files changed
2555 * but nothing about added/removed lines? Is this a bug in Git?").
2556 */
2557 if (insertions || deletions == 0) {
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002558 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002559 (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002560 insertions);
2561 }
2562
2563 if (deletions || insertions == 0) {
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002564 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002565 (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002566 deletions);
2567 }
2568 strbuf_addch(&sb, '\n');
Stefan Beller0911c472017-06-29 17:07:02 -07002569 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
2570 sb.buf, sb.len, 0);
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002571 strbuf_release(&sb);
Stefan Beller0911c472017-06-29 17:07:02 -07002572}
2573
2574void print_stat_summary(FILE *fp, int files,
2575 int insertions, int deletions)
2576{
2577 struct diff_options o;
2578 memset(&o, 0, sizeof(o));
2579 o.file = fp;
2580
2581 print_stat_summary_inserts_deletes(&o, files, insertions, deletions);
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002582}
2583
Felipe Contreras4b25d092009-05-01 12:06:36 +03002584static void show_stats(struct diffstat_t *data, struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002585{
Benjamin Kramereb3a9dd2009-03-07 21:02:10 +01002586 int i, len, add, del, adds = 0, dels = 0;
Jeff King0974c112010-04-17 13:41:08 -04002587 uintmax_t max_change = 0, max_len = 0;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002588 int total_files = data->nr, count;
2589 int width, name_width, graph_width, number_width = 0, bin_width = 0;
Johannes Schindelinc0aa3352011-03-22 13:50:08 +01002590 const char *reset, *add_c, *del_c;
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002591 int extra_shown = 0;
Stefan Beller0911c472017-06-29 17:07:02 -07002592 const char *line_prefix = diff_line_prefix(options);
2593 struct strbuf out = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002594
2595 if (data->nr == 0)
2596 return;
2597
Michael J Gruber808e1db2011-05-27 14:36:41 +02002598 count = options->stat_count ? options->stat_count : data->nr;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002599
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01002600 reset = diff_get_color_opt(options, DIFF_RESET);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01002601 add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
2602 del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
Junio C Hamano785f7432006-09-26 18:59:41 -07002603
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002604 /*
2605 * Find the longest filename and max number of changes
2606 */
Michael J Gruber808e1db2011-05-27 14:36:41 +02002607 for (i = 0; (i < count) && (i < data->nr); i++) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002608 struct diffstat_file *file = data->files[i];
Jeff King0974c112010-04-17 13:41:08 -04002609 uintmax_t change = file->added + file->deleted;
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002610
2611 if (!file->is_interesting && (change == 0)) {
Michael J Gruber808e1db2011-05-27 14:36:41 +02002612 count++; /* not shown == room for one more */
Michael J Gruber358e4602011-05-27 14:36:40 +02002613 continue;
2614 }
Junio C Hamanof6046522007-12-11 23:46:30 -08002615 fill_print_name(file);
2616 len = strlen(file->print_name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002617 if (max_len < len)
2618 max_len = len;
2619
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002620 if (file->is_unmerged) {
2621 /* "Unmerged" is 8 characters */
2622 bin_width = bin_width < 8 ? 8 : bin_width;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002623 continue;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002624 }
2625 if (file->is_binary) {
2626 /* "Bin XXX -> YYY bytes" */
2627 int w = 14 + decimal_width(file->added)
2628 + decimal_width(file->deleted);
2629 bin_width = bin_width < w ? w : bin_width;
2630 /* Display change counts aligned with "Bin" */
2631 number_width = 3;
2632 continue;
2633 }
2634
Junio C Hamanoa2540022006-09-26 18:53:02 -07002635 if (max_change < change)
2636 max_change = change;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002637 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002638 count = i; /* where we can stop scanning in data->files[] */
Junio C Hamano6973dca2006-04-21 23:57:45 -07002639
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002640 /*
2641 * We have width = stat_width or term_columns() columns total.
2642 * We want a maximum of min(max_len, stat_name_width) for the name part.
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002643 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002644 * We also need 1 for " " and 4 + decimal_width(max_change)
2645 * for " | NNNN " and one the empty column at the end, altogether
2646 * 6 + decimal_width(max_change).
Junio C Hamanoa2540022006-09-26 18:53:02 -07002647 *
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002648 * If there's not enough space, we will use the smaller of
2649 * stat_name_width (if set) and 5/8*width for the filename,
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002650 * and the rest for constant elements + graph part, but no more
2651 * than stat_graph_width for the graph part.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002652 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2653 * for the standard terminal size).
2654 *
2655 * In other words: stat_width limits the maximum width, and
2656 * stat_name_width fixes the maximum width of the filename,
2657 * and is also used to divide available columns if there
2658 * aren't enough.
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002659 *
2660 * Binary files are displayed with "Bin XXX -> YYY bytes"
2661 * instead of the change count and graph. This part is treated
2662 * similarly to the graph part, except that it is not
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +02002663 * "scaled". If total width is too small to accommodate the
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002664 * guaranteed minimum width of the filename part and the
2665 * separators and this message, this message will "overflow"
2666 * making the line longer than the maximum width.
Junio C Hamanoa2540022006-09-26 18:53:02 -07002667 */
Junio C Hamanoa2540022006-09-26 18:53:02 -07002668
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002669 if (options->stat_width == -1)
Junio C Hamanocd48dad2016-08-31 16:27:19 -07002670 width = term_columns() - strlen(line_prefix);
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002671 else
2672 width = options->stat_width ? options->stat_width : 80;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002673 number_width = decimal_width(max_change) > number_width ?
2674 decimal_width(max_change) : number_width;
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002675
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +01002676 if (options->stat_graph_width == -1)
2677 options->stat_graph_width = diff_stat_graph_width;
2678
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002679 /*
2680 * Guarantee 3/8*16==6 for the graph part
2681 * and 5/8*16==10 for the filename part
2682 */
2683 if (width < 16 + 6 + number_width)
2684 width = 16 + 6 + number_width;
2685
2686 /*
2687 * First assign sizes that are wanted, ignoring available width.
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002688 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2689 * starting from "XXX" should fit in graph_width.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002690 */
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002691 graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4;
2692 if (options->stat_graph_width &&
2693 options->stat_graph_width < graph_width)
2694 graph_width = options->stat_graph_width;
2695
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002696 name_width = (options->stat_name_width > 0 &&
2697 options->stat_name_width < max_len) ?
2698 options->stat_name_width : max_len;
2699
2700 /*
2701 * Adjust adjustable widths not to exceed maximum width
2702 */
2703 if (name_width + number_width + 6 + graph_width > width) {
Lucian Poston678c5742012-04-18 14:12:18 -07002704 if (graph_width > width * 3/8 - number_width - 6) {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002705 graph_width = width * 3/8 - number_width - 6;
Lucian Poston678c5742012-04-18 14:12:18 -07002706 if (graph_width < 6)
2707 graph_width = 6;
2708 }
2709
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002710 if (options->stat_graph_width &&
2711 graph_width > options->stat_graph_width)
2712 graph_width = options->stat_graph_width;
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002713 if (name_width > width - number_width - 6 - graph_width)
2714 name_width = width - number_width - 6 - graph_width;
2715 else
2716 graph_width = width - number_width - 6 - name_width;
2717 }
2718
2719 /*
2720 * From here name_width is the width of the name area,
2721 * and graph_width is the width of the graph area.
2722 * max_change is used to scale graph properly.
2723 */
Michael J Gruber808e1db2011-05-27 14:36:41 +02002724 for (i = 0; i < count; i++) {
Timo Hirvonend2543b82006-06-24 20:20:32 +03002725 const char *prefix = "";
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002726 struct diffstat_file *file = data->files[i];
2727 char *name = file->print_name;
2728 uintmax_t added = file->added;
2729 uintmax_t deleted = file->deleted;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002730 int name_len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002731
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002732 if (!file->is_interesting && (added + deleted == 0))
Michael J Gruber358e4602011-05-27 14:36:40 +02002733 continue;
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002734
Junio C Hamano6973dca2006-04-21 23:57:45 -07002735 /*
2736 * "scale" the filename
2737 */
Junio C Hamanoa2540022006-09-26 18:53:02 -07002738 len = name_width;
2739 name_len = strlen(name);
2740 if (name_width < name_len) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002741 char *slash;
2742 prefix = "...";
Junio C Hamanoa2540022006-09-26 18:53:02 -07002743 len -= 3;
2744 name += name_len - len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002745 slash = strchr(name, '/');
2746 if (slash)
2747 name = slash;
2748 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002749
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002750 if (file->is_binary) {
Stefan Beller0911c472017-06-29 17:07:02 -07002751 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2752 strbuf_addf(&out, " %*s", number_width, "Bin");
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002753 if (!added && !deleted) {
Stefan Beller0911c472017-06-29 17:07:02 -07002754 strbuf_addch(&out, '\n');
2755 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2756 out.buf, out.len, 0);
2757 strbuf_reset(&out);
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002758 continue;
2759 }
Stefan Beller0911c472017-06-29 17:07:02 -07002760 strbuf_addf(&out, " %s%"PRIuMAX"%s",
Jeff King0974c112010-04-17 13:41:08 -04002761 del_c, deleted, reset);
Stefan Beller0911c472017-06-29 17:07:02 -07002762 strbuf_addstr(&out, " -> ");
2763 strbuf_addf(&out, "%s%"PRIuMAX"%s",
Jeff King0974c112010-04-17 13:41:08 -04002764 add_c, added, reset);
Stefan Beller0911c472017-06-29 17:07:02 -07002765 strbuf_addstr(&out, " bytes\n");
2766 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2767 out.buf, out.len, 0);
2768 strbuf_reset(&out);
Junio C Hamanof6046522007-12-11 23:46:30 -08002769 continue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002770 }
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002771 else if (file->is_unmerged) {
Stefan Beller0911c472017-06-29 17:07:02 -07002772 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2773 strbuf_addstr(&out, " Unmerged\n");
2774 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2775 out.buf, out.len, 0);
2776 strbuf_reset(&out);
Junio C Hamanof6046522007-12-11 23:46:30 -08002777 continue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002778 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002779
Junio C Hamanoa2540022006-09-26 18:53:02 -07002780 /*
2781 * scale the add/delete
2782 */
Junio C Hamano6973dca2006-04-21 23:57:45 -07002783 add = added;
2784 del = deleted;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002785
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002786 if (graph_width <= max_change) {
Stefan Bellerd3c9cf32013-07-14 23:35:49 +02002787 int total = scale_linear(add + del, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002788 if (total < 2 && add && del)
2789 /* width >= 2 due to the sanity check */
2790 total = 2;
2791 if (add < del) {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002792 add = scale_linear(add, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002793 del = total - add;
2794 } else {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002795 del = scale_linear(del, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002796 add = total - del;
2797 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002798 }
Stefan Beller0911c472017-06-29 17:07:02 -07002799 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2800 strbuf_addf(&out, " %*"PRIuMAX"%s",
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002801 number_width, added + deleted,
2802 added + deleted ? " " : "");
Stefan Beller0911c472017-06-29 17:07:02 -07002803 show_graph(&out, '+', add, add_c, reset);
2804 show_graph(&out, '-', del, del_c, reset);
2805 strbuf_addch(&out, '\n');
2806 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2807 out.buf, out.len, 0);
2808 strbuf_reset(&out);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002809 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002810
2811 for (i = 0; i < data->nr; i++) {
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002812 struct diffstat_file *file = data->files[i];
2813 uintmax_t added = file->added;
2814 uintmax_t deleted = file->deleted;
Junio C Hamano82dfc2c2012-11-27 12:05:10 -08002815
2816 if (file->is_unmerged ||
2817 (!file->is_interesting && (added + deleted == 0))) {
Michael J Gruber808e1db2011-05-27 14:36:41 +02002818 total_files--;
2819 continue;
2820 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002821
Junio C Hamano82dfc2c2012-11-27 12:05:10 -08002822 if (!file->is_binary) {
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002823 adds += added;
2824 dels += deleted;
2825 }
2826 if (i < count)
2827 continue;
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002828 if (!extra_shown)
Stefan Beller0911c472017-06-29 17:07:02 -07002829 emit_diff_symbol(options,
2830 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
2831 NULL, 0, 0);
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002832 extra_shown = 1;
Michael J Gruber808e1db2011-05-27 14:36:41 +02002833 }
Stefan Beller0911c472017-06-29 17:07:02 -07002834
2835 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
Rene Scharfe5a612012017-08-30 19:49:43 +02002836 strbuf_release(&out);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002837}
2838
Felipe Contreras2775d922009-10-11 23:46:11 +03002839static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002840{
2841 int i, adds = 0, dels = 0, total_files = data->nr;
2842
2843 if (data->nr == 0)
2844 return;
2845
2846 for (i = 0; i < data->nr; i++) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002847 int added = data->files[i]->added;
Stefan Beller0911c472017-06-29 17:07:02 -07002848 int deleted = data->files[i]->deleted;
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002849
Junio C Hamano20c8cde2012-11-27 14:19:36 -08002850 if (data->files[i]->is_unmerged ||
2851 (!data->files[i]->is_interesting && (added + deleted == 0))) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002852 total_files--;
Alexander Strasserde9658b2012-06-15 23:50:30 +02002853 } else if (!data->files[i]->is_binary) { /* don't count bytes */
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002854 adds += added;
2855 dels += deleted;
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002856 }
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002857 }
Stefan Beller0911c472017-06-29 17:07:02 -07002858 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002859}
2860
Felipe Contreras4b25d092009-05-01 12:06:36 +03002861static void show_numstat(struct diffstat_t *data, struct diff_options *options)
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002862{
2863 int i;
2864
Junio C Hamanof6046522007-12-11 23:46:30 -08002865 if (data->nr == 0)
2866 return;
2867
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002868 for (i = 0; i < data->nr; i++) {
2869 struct diffstat_file *file = data->files[i];
2870
John Keeping30997bb2013-02-07 20:15:27 +00002871 fprintf(options->file, "%s", diff_line_prefix(options));
Bo Yang7be57612010-05-26 15:23:54 +08002872
Junio C Hamanobfddbc52006-12-10 13:50:59 -08002873 if (file->is_binary)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002874 fprintf(options->file, "-\t-\t");
Junio C Hamanobfddbc52006-12-10 13:50:59 -08002875 else
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002876 fprintf(options->file,
Jeff King0974c112010-04-17 13:41:08 -04002877 "%"PRIuMAX"\t%"PRIuMAX"\t",
2878 file->added, file->deleted);
Junio C Hamanof6046522007-12-11 23:46:30 -08002879 if (options->line_termination) {
2880 fill_print_name(file);
2881 if (!file->is_renamed)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002882 write_name_quoted(file->name, options->file,
Junio C Hamanof6046522007-12-11 23:46:30 -08002883 options->line_termination);
2884 else {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002885 fputs(file->print_name, options->file);
2886 putc(options->line_termination, options->file);
Junio C Hamanof6046522007-12-11 23:46:30 -08002887 }
Pierre Habouzit663af342007-09-20 00:42:15 +02002888 } else {
Junio C Hamanof6046522007-12-11 23:46:30 -08002889 if (file->is_renamed) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002890 putc('\0', options->file);
2891 write_name_quoted(file->from_name, options->file, '\0');
Junio C Hamanof6046522007-12-11 23:46:30 -08002892 }
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002893 write_name_quoted(file->name, options->file, '\0');
Pierre Habouzit663af342007-09-20 00:42:15 +02002894 }
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002895 }
2896}
2897
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002898struct dirstat_file {
2899 const char *name;
2900 unsigned long changed;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002901};
2902
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002903struct dirstat_dir {
2904 struct dirstat_file *files;
Johan Herland712d2c72011-04-29 11:36:20 +02002905 int alloc, nr, permille, cumulative;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002906};
2907
Bo Yang7be57612010-05-26 15:23:54 +08002908static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
2909 unsigned long changed, const char *base, int baselen)
Linus Torvalds7df7c012008-02-12 13:26:31 -08002910{
Brandon Williams585c0e22018-02-14 10:59:28 -08002911 unsigned long sum_changes = 0;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002912 unsigned int sources = 0;
John Keeping30997bb2013-02-07 20:15:27 +00002913 const char *line_prefix = diff_line_prefix(opt);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002914
2915 while (dir->nr) {
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002916 struct dirstat_file *f = dir->files;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002917 int namelen = strlen(f->name);
Brandon Williams585c0e22018-02-14 10:59:28 -08002918 unsigned long changes;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002919 char *slash;
2920
2921 if (namelen < baselen)
2922 break;
2923 if (memcmp(f->name, base, baselen))
2924 break;
2925 slash = strchr(f->name + baselen, '/');
2926 if (slash) {
2927 int newbaselen = slash + 1 - f->name;
Brandon Williams585c0e22018-02-14 10:59:28 -08002928 changes = gather_dirstat(opt, dir, changed, f->name, newbaselen);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002929 sources++;
2930 } else {
Brandon Williams585c0e22018-02-14 10:59:28 -08002931 changes = f->changed;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002932 dir->files++;
2933 dir->nr--;
2934 sources += 2;
2935 }
Brandon Williams585c0e22018-02-14 10:59:28 -08002936 sum_changes += changes;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002937 }
2938
2939 /*
2940 * We don't report dirstat's for
2941 * - the top level
2942 * - or cases where everything came from a single directory
2943 * under this directory (sources == 1).
2944 */
2945 if (baselen && sources != 1) {
Brandon Williams585c0e22018-02-14 10:59:28 -08002946 if (sum_changes) {
2947 int permille = sum_changes * 1000 / changed;
Johan Herland712d2c72011-04-29 11:36:20 +02002948 if (permille >= dir->permille) {
Bo Yang7be57612010-05-26 15:23:54 +08002949 fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix,
Johan Herland712d2c72011-04-29 11:36:20 +02002950 permille / 10, permille % 10, baselen, base);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002951 if (!dir->cumulative)
2952 return 0;
2953 }
2954 }
2955 }
Brandon Williams585c0e22018-02-14 10:59:28 -08002956 return sum_changes;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002957}
2958
Linus Torvalds441bca02008-08-28 16:19:08 -07002959static int dirstat_compare(const void *_a, const void *_b)
2960{
2961 const struct dirstat_file *a = _a;
2962 const struct dirstat_file *b = _b;
2963 return strcmp(a->name, b->name);
2964}
2965
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002966static void show_dirstat(struct diff_options *options)
Linus Torvalds7df7c012008-02-12 13:26:31 -08002967{
2968 int i;
2969 unsigned long changed;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002970 struct dirstat_dir dir;
2971 struct diff_queue_struct *q = &diff_queued_diff;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002972
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002973 dir.files = NULL;
2974 dir.alloc = 0;
2975 dir.nr = 0;
Johan Herland712d2c72011-04-29 11:36:20 +02002976 dir.permille = options->dirstat_permille;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07002977 dir.cumulative = options->flags.dirstat_cumulative;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002978
Linus Torvalds7df7c012008-02-12 13:26:31 -08002979 changed = 0;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002980 for (i = 0; i < q->nr; i++) {
2981 struct diff_filepair *p = q->queue[i];
2982 const char *name;
2983 unsigned long copied, added, damage;
Jonathan Tan1c37e862020-04-07 15:11:41 -07002984 struct diff_populate_filespec_options dpf_options = {
2985 .check_size_only = 1,
2986 };
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002987
Johan Herland2ca86712011-04-12 11:24:34 +02002988 name = p->two->path ? p->two->path : p->one->path;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002989
Jeff Kingd9f62df2018-08-28 17:23:03 -04002990 if (p->one->oid_valid && p->two->oid_valid &&
2991 oideq(&p->one->oid, &p->two->oid)) {
Johan Herland2ff3a802011-04-11 00:48:52 +02002992 /*
2993 * The SHA1 has not changed, so pre-/post-content is
2994 * identical. We can therefore skip looking at the
2995 * file contents altogether.
2996 */
2997 damage = 0;
2998 goto found_damage;
2999 }
3000
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003001 if (options->flags.dirstat_by_file) {
Johan Herland0133dab2011-04-11 00:48:51 +02003002 /*
3003 * In --dirstat-by-file mode, we don't really need to
3004 * look at the actual file contents at all.
3005 * The fact that the SHA1 changed is enough for us to
3006 * add this file to the list of results
3007 * (with each file contributing equal damage).
3008 */
Johan Herland2ff3a802011-04-11 00:48:52 +02003009 damage = 1;
Johan Herland0133dab2011-04-11 00:48:51 +02003010 goto found_damage;
3011 }
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003012
3013 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003014 diff_populate_filespec(options->repo, p->one, NULL);
3015 diff_populate_filespec(options->repo, p->two, NULL);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003016 diffcore_count_changes(options->repo,
3017 p->one, p->two, NULL, NULL,
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003018 &copied, &added);
3019 diff_free_filespec_data(p->one);
3020 diff_free_filespec_data(p->two);
3021 } else if (DIFF_FILE_VALID(p->one)) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003022 diff_populate_filespec(options->repo, p->one, &dpf_options);
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003023 copied = added = 0;
3024 diff_free_filespec_data(p->one);
3025 } else if (DIFF_FILE_VALID(p->two)) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003026 diff_populate_filespec(options->repo, p->two, &dpf_options);
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003027 copied = 0;
3028 added = p->two->size;
3029 diff_free_filespec_data(p->two);
3030 } else
Junio C Hamano2b0b5512008-02-24 17:37:15 -08003031 continue;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003032
3033 /*
3034 * Original minus copied is the removed material,
3035 * added is the new material. They are both damages
Johan Herland0133dab2011-04-11 00:48:51 +02003036 * made to the preimage.
Johan Herland2ff3a802011-04-11 00:48:52 +02003037 * If the resulting damage is zero, we know that
3038 * diffcore_count_changes() considers the two entries to
Jeff Kingd9f62df2018-08-28 17:23:03 -04003039 * be identical, but since the oid changed, we
Johan Herland2ff3a802011-04-11 00:48:52 +02003040 * know that there must have been _some_ kind of change,
3041 * so we force all entries to have damage > 0.
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003042 */
3043 damage = (p->one->size - copied) + added;
Johan Herland2ff3a802011-04-11 00:48:52 +02003044 if (!damage)
Heikki Orsilafd337772008-09-05 22:27:35 +03003045 damage = 1;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003046
Johan Herland0133dab2011-04-11 00:48:51 +02003047found_damage:
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003048 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
3049 dir.files[dir.nr].name = name;
3050 dir.files[dir.nr].changed = damage;
3051 changed += damage;
3052 dir.nr++;
Linus Torvalds7df7c012008-02-12 13:26:31 -08003053 }
3054
3055 /* This can happen even with many files, if everything was renames */
3056 if (!changed)
3057 return;
3058
3059 /* Show all directories with more than x% of the changes */
René Scharfe9ed0d8d2016-09-29 17:27:31 +02003060 QSORT(dir.files, dir.nr, dirstat_compare);
Bo Yang7be57612010-05-26 15:23:54 +08003061 gather_dirstat(options, &dir, changed, "", 0);
Linus Torvalds7df7c012008-02-12 13:26:31 -08003062}
3063
Johan Herland1c57a622011-04-29 11:36:21 +02003064static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options)
3065{
3066 int i;
3067 unsigned long changed;
3068 struct dirstat_dir dir;
3069
3070 if (data->nr == 0)
3071 return;
3072
3073 dir.files = NULL;
3074 dir.alloc = 0;
3075 dir.nr = 0;
3076 dir.permille = options->dirstat_permille;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003077 dir.cumulative = options->flags.dirstat_cumulative;
Johan Herland1c57a622011-04-29 11:36:21 +02003078
3079 changed = 0;
3080 for (i = 0; i < data->nr; i++) {
3081 struct diffstat_file *file = data->files[i];
3082 unsigned long damage = file->added + file->deleted;
3083 if (file->is_binary)
3084 /*
3085 * binary files counts bytes, not lines. Must find some
3086 * way to normalize binary bytes vs. textual lines.
3087 * The following heuristic assumes that there are 64
3088 * bytes per "line".
3089 * This is stupid and ugly, but very cheap...
3090 */
René Scharfe42c78a22017-07-08 12:35:35 +02003091 damage = DIV_ROUND_UP(damage, 64);
Johan Herland1c57a622011-04-29 11:36:21 +02003092 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
3093 dir.files[dir.nr].name = file->name;
3094 dir.files[dir.nr].changed = damage;
3095 changed += damage;
3096 dir.nr++;
3097 }
3098
3099 /* This can happen even with many files, if everything was renames */
3100 if (!changed)
3101 return;
3102
3103 /* Show all directories with more than x% of the changes */
René Scharfe9ed0d8d2016-09-29 17:27:31 +02003104 QSORT(dir.files, dir.nr, dirstat_compare);
Johan Herland1c57a622011-04-29 11:36:21 +02003105 gather_dirstat(options, &dir, changed, "", 0);
3106}
3107
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003108static void free_diffstat_file(struct diffstat_file *f)
3109{
3110 free(f->print_name);
3111 free(f->name);
3112 free(f->from_name);
3113 free(f);
3114}
3115
Daniel Ferreirae4cb6592019-11-13 12:40:58 +00003116void free_diffstat_info(struct diffstat_t *diffstat)
Junio C Hamanof6046522007-12-11 23:46:30 -08003117{
3118 int i;
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003119 for (i = 0; i < diffstat->nr; i++)
3120 free_diffstat_file(diffstat->files[i]);
Junio C Hamanof6046522007-12-11 23:46:30 -08003121 free(diffstat->files);
3122}
3123
Johannes Schindelin88246892006-05-20 23:43:13 +02003124struct checkdiff_t {
Johannes Schindelin88246892006-05-20 23:43:13 +02003125 const char *filename;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003126 int lineno;
Junio C Hamanoa757c642010-03-24 19:21:32 -07003127 int conflict_marker_size;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003128 struct diff_options *o;
Junio C Hamanocf1b7862007-12-06 00:14:14 -08003129 unsigned ws_rule;
Wincent Colaiuta62c64892007-12-13 21:24:52 +01003130 unsigned status;
Johannes Schindelin88246892006-05-20 23:43:13 +02003131};
3132
Junio C Hamanoa757c642010-03-24 19:21:32 -07003133static int is_conflict_marker(const char *line, int marker_size, unsigned long len)
Junio C Hamano04954042008-06-26 15:37:21 -07003134{
3135 char firstchar;
3136 int cnt;
3137
Junio C Hamanoa757c642010-03-24 19:21:32 -07003138 if (len < marker_size + 1)
Junio C Hamano04954042008-06-26 15:37:21 -07003139 return 0;
3140 firstchar = line[0];
3141 switch (firstchar) {
Junio C Hamanoa757c642010-03-24 19:21:32 -07003142 case '=': case '>': case '<': case '|':
Junio C Hamano04954042008-06-26 15:37:21 -07003143 break;
3144 default:
3145 return 0;
3146 }
Junio C Hamanoa757c642010-03-24 19:21:32 -07003147 for (cnt = 1; cnt < marker_size; cnt++)
Junio C Hamano04954042008-06-26 15:37:21 -07003148 if (line[cnt] != firstchar)
3149 return 0;
Elijah Newren15beaaa2019-11-05 17:07:23 +00003150 /* line[1] through line[marker_size-1] are same as firstchar */
Junio C Hamanoa757c642010-03-24 19:21:32 -07003151 if (len < marker_size + 1 || !isspace(line[marker_size]))
Junio C Hamano04954042008-06-26 15:37:21 -07003152 return 0;
Junio C Hamano04954042008-06-26 15:37:21 -07003153 return 1;
3154}
3155
Jeff King75ab7632018-11-02 02:39:03 -04003156static void checkdiff_consume_hunk(void *priv,
3157 long ob, long on, long nb, long nn,
3158 const char *func, long funclen)
3159
3160{
3161 struct checkdiff_t *data = priv;
3162 data->lineno = nb - 1;
3163}
3164
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02003165static int checkdiff_consume(void *priv, char *line, unsigned long len)
Johannes Schindelin88246892006-05-20 23:43:13 +02003166{
3167 struct checkdiff_t *data = priv;
Junio C Hamanoa757c642010-03-24 19:21:32 -07003168 int marker_size = data->conflict_marker_size;
Jeff Kingf1c96262011-08-17 22:03:12 -07003169 const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE);
3170 const char *reset = diff_get_color(data->o->use_color, DIFF_RESET);
3171 const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +01003172 char *err;
John Keeping30997bb2013-02-07 20:15:27 +00003173 const char *line_prefix;
Bo Yang7be57612010-05-26 15:23:54 +08003174
3175 assert(data->o);
John Keeping30997bb2013-02-07 20:15:27 +00003176 line_prefix = diff_line_prefix(data->o);
Johannes Schindelin88246892006-05-20 23:43:13 +02003177
3178 if (line[0] == '+') {
Junio C Hamano18374e52008-06-26 13:16:33 -07003179 unsigned bad;
Junio C Hamano0ef617f2008-02-15 20:30:05 -08003180 data->lineno++;
Junio C Hamanoa757c642010-03-24 19:21:32 -07003181 if (is_conflict_marker(line + 1, marker_size, len - 1)) {
Junio C Hamano04954042008-06-26 15:37:21 -07003182 data->status |= 1;
3183 fprintf(data->o->file,
Bo Yang7be57612010-05-26 15:23:54 +08003184 "%s%s:%d: leftover conflict marker\n",
3185 line_prefix, data->filename, data->lineno);
Junio C Hamano04954042008-06-26 15:37:21 -07003186 }
Junio C Hamano8f8841e2008-06-26 15:35:21 -07003187 bad = ws_check(line + 1, len - 1, data->ws_rule);
Junio C Hamano18374e52008-06-26 13:16:33 -07003188 if (!bad)
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02003189 return 0;
Junio C Hamano18374e52008-06-26 13:16:33 -07003190 data->status |= bad;
3191 err = whitespace_error_string(bad);
Bo Yang7be57612010-05-26 15:23:54 +08003192 fprintf(data->o->file, "%s%s:%d: %s.\n",
3193 line_prefix, data->filename, data->lineno, err);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +01003194 free(err);
Bo Yanga3c158d2010-05-26 15:08:02 +08003195 emit_line(data->o, set, reset, line, 1);
Junio C Hamano8f8841e2008-06-26 15:35:21 -07003196 ws_check_emit(line + 1, len - 1, data->ws_rule,
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003197 data->o->file, set, reset, ws);
Junio C Hamano877f23c2008-06-26 15:36:59 -07003198 } else if (line[0] == ' ') {
Johannes Schindelin88246892006-05-20 23:43:13 +02003199 data->lineno++;
Johannes Schindelin88246892006-05-20 23:43:13 +02003200 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02003201 return 0;
Johannes Schindelin88246892006-05-20 23:43:13 +02003202}
3203
Junio C Hamano06606262006-05-05 02:41:53 -07003204static unsigned char *deflate_it(char *data,
3205 unsigned long size,
3206 unsigned long *result_size)
Junio C Hamano051308f2006-05-04 16:51:44 -07003207{
Junio C Hamano06606262006-05-05 02:41:53 -07003208 int bound;
3209 unsigned char *deflated;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07003210 git_zstream stream;
Junio C Hamano051308f2006-05-04 16:51:44 -07003211
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003212 git_deflate_init(&stream, zlib_compression_level);
Junio C Hamano225a6f12011-06-10 11:18:17 -07003213 bound = git_deflate_bound(&stream, size);
Junio C Hamano06606262006-05-05 02:41:53 -07003214 deflated = xmalloc(bound);
3215 stream.next_out = deflated;
3216 stream.avail_out = bound;
Junio C Hamano051308f2006-05-04 16:51:44 -07003217
Junio C Hamano06606262006-05-05 02:41:53 -07003218 stream.next_in = (unsigned char *)data;
3219 stream.avail_in = size;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003220 while (git_deflate(&stream, Z_FINISH) == Z_OK)
Junio C Hamano06606262006-05-05 02:41:53 -07003221 ; /* nothing */
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003222 git_deflate_end(&stream);
Junio C Hamano06606262006-05-05 02:41:53 -07003223 *result_size = stream.total_out;
3224 return deflated;
Junio C Hamano051308f2006-05-04 16:51:44 -07003225}
3226
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003227static void emit_binary_diff_body(struct diff_options *o,
3228 mmfile_t *one, mmfile_t *two)
Junio C Hamano051308f2006-05-04 16:51:44 -07003229{
Junio C Hamano06606262006-05-05 02:41:53 -07003230 void *cp;
3231 void *delta;
3232 void *deflated;
3233 void *data;
3234 unsigned long orig_size;
Junio C Hamano051308f2006-05-04 16:51:44 -07003235 unsigned long delta_size;
Junio C Hamano06606262006-05-05 02:41:53 -07003236 unsigned long deflate_size;
3237 unsigned long data_size;
Junio C Hamano051308f2006-05-04 16:51:44 -07003238
Junio C Hamano06606262006-05-05 02:41:53 -07003239 /* We could do deflated delta, or we could do just deflated two,
3240 * whichever is smaller.
3241 */
3242 delta = NULL;
3243 deflated = deflate_it(two->ptr, two->size, &deflate_size);
3244 if (one->size && two->size) {
3245 delta = diff_delta(one->ptr, one->size,
3246 two->ptr, two->size,
3247 &delta_size, deflate_size);
3248 if (delta) {
3249 void *to_free = delta;
3250 orig_size = delta_size;
3251 delta = deflate_it(delta, delta_size, &delta_size);
3252 free(to_free);
3253 }
3254 }
Junio C Hamano051308f2006-05-04 16:51:44 -07003255
Junio C Hamano06606262006-05-05 02:41:53 -07003256 if (delta && delta_size < deflate_size) {
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01003257 char *s = xstrfmt("%"PRIuMAX , (uintmax_t)orig_size);
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003258 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
3259 s, strlen(s), 0);
3260 free(s);
Junio C Hamano06606262006-05-05 02:41:53 -07003261 free(deflated);
3262 data = delta;
3263 data_size = delta_size;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003264 } else {
3265 char *s = xstrfmt("%lu", two->size);
3266 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
3267 s, strlen(s), 0);
3268 free(s);
Junio C Hamano06606262006-05-05 02:41:53 -07003269 free(delta);
3270 data = deflated;
3271 data_size = deflate_size;
3272 }
3273
3274 /* emit data encoded in base85 */
3275 cp = data;
3276 while (data_size) {
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003277 int len;
Junio C Hamano06606262006-05-05 02:41:53 -07003278 int bytes = (52 < data_size) ? 52 : data_size;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003279 char line[71];
Junio C Hamano06606262006-05-05 02:41:53 -07003280 data_size -= bytes;
Junio C Hamano051308f2006-05-04 16:51:44 -07003281 if (bytes <= 26)
3282 line[0] = bytes + 'A' - 1;
3283 else
3284 line[0] = bytes - 26 + 'a' - 1;
3285 encode_85(line + 1, cp, bytes);
Florian Forster1d7f1712006-06-18 17:18:09 +02003286 cp = (char *) cp + bytes;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003287
3288 len = strlen(line);
3289 line[len++] = '\n';
3290 line[len] = '\0';
3291
3292 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY,
3293 line, len, 0);
Junio C Hamano051308f2006-05-04 16:51:44 -07003294 }
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003295 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0);
Junio C Hamano06606262006-05-05 02:41:53 -07003296 free(data);
Junio C Hamano051308f2006-05-04 16:51:44 -07003297}
3298
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003299static void emit_binary_diff(struct diff_options *o,
3300 mmfile_t *one, mmfile_t *two)
Junio C Hamanod4c452f2006-08-16 16:08:14 -07003301{
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003302 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0);
3303 emit_binary_diff_body(o, one, two);
3304 emit_binary_diff_body(o, two, one);
Junio C Hamanod4c452f2006-08-16 16:08:14 -07003305}
3306
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003307int diff_filespec_is_binary(struct repository *r,
3308 struct diff_filespec *one)
Junio C Hamano29a3eef2007-07-06 00:18:54 -07003309{
Jonathan Tan1c37e862020-04-07 15:11:41 -07003310 struct diff_populate_filespec_options dpf_options = {
3311 .check_binary = 1,
3312 };
3313
Jeff King122aa6f2008-10-05 17:43:36 -04003314 if (one->is_binary == -1) {
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003315 diff_filespec_load_driver(one, r->index);
Jeff King122aa6f2008-10-05 17:43:36 -04003316 if (one->driver->binary != -1)
3317 one->is_binary = one->driver->binary;
3318 else {
3319 if (!one->data && DIFF_FILE_VALID(one))
Jonathan Tan1c37e862020-04-07 15:11:41 -07003320 diff_populate_filespec(r, one, &dpf_options);
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003321 if (one->is_binary == -1 && one->data)
Jeff King122aa6f2008-10-05 17:43:36 -04003322 one->is_binary = buffer_is_binary(one->data,
3323 one->size);
3324 if (one->is_binary == -1)
3325 one->is_binary = 0;
3326 }
3327 }
Junio C Hamano29a3eef2007-07-06 00:18:54 -07003328 return one->is_binary;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003329}
3330
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003331static const struct userdiff_funcname *
3332diff_funcname_pattern(struct diff_options *o, struct diff_filespec *one)
Junio C Hamanof2584752007-07-06 00:45:10 -07003333{
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003334 diff_filespec_load_driver(one, o->repo->index);
Jeff King122aa6f2008-10-05 17:43:36 -04003335 return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
Junio C Hamanof2584752007-07-06 00:45:10 -07003336}
3337
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003338void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
3339{
3340 if (!options->a_prefix)
3341 options->a_prefix = a;
3342 if (!options->b_prefix)
3343 options->b_prefix = b;
3344}
3345
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01003346struct userdiff_driver *get_textconv(struct repository *r,
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003347 struct diff_filespec *one)
Jeff King04427ac2008-10-26 00:44:53 -04003348{
3349 if (!DIFF_FILE_VALID(one))
3350 return NULL;
Jeff Kingd391c0f2010-09-21 17:01:24 -04003351
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01003352 diff_filespec_load_driver(one, r->index);
3353 return userdiff_get_textconv(r, one->driver);
Jeff King04427ac2008-10-26 00:44:53 -04003354}
3355
Junio C Hamano6973dca2006-04-21 23:57:45 -07003356static void builtin_diff(const char *name_a,
3357 const char *name_b,
3358 struct diff_filespec *one,
3359 struct diff_filespec *two,
3360 const char *xfrm_msg,
Christian Couder296c6bb2010-05-26 04:50:12 +02003361 int must_show_header,
Junio C Hamano051308f2006-05-04 16:51:44 -07003362 struct diff_options *o,
Junio C Hamano6973dca2006-04-21 23:57:45 -07003363 int complete_rewrite)
3364{
3365 mmfile_t mf1, mf2;
3366 const char *lbl[2];
3367 char *a_one, *b_two;
Jeff Kingd9c552f2012-11-13 21:12:46 +05303368 const char *meta = diff_get_color_opt(o, DIFF_METAINFO);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01003369 const char *reset = diff_get_color_opt(o, DIFF_RESET);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003370 const char *a_prefix, *b_prefix;
Jeff Kingd9bae1a2010-04-01 20:12:15 -04003371 struct userdiff_driver *textconv_one = NULL;
3372 struct userdiff_driver *textconv_two = NULL;
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003373 struct strbuf header = STRBUF_INIT;
John Keeping30997bb2013-02-07 20:15:27 +00003374 const char *line_prefix = diff_line_prefix(o);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003375
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003376 diff_set_mnemonic_prefix(o, "a/", "b/");
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003377 if (o->flags.reverse_diff) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003378 a_prefix = o->b_prefix;
3379 b_prefix = o->a_prefix;
3380 } else {
3381 a_prefix = o->a_prefix;
3382 b_prefix = o->b_prefix;
3383 }
3384
Jacob Keller61cfbc02016-08-31 16:27:21 -07003385 if (o->submodule_format == DIFF_SUBMODULE_LOG &&
3386 (!one->mode || S_ISGITLINK(one->mode)) &&
3387 (!two->mode || S_ISGITLINK(two->mode))) {
Shourya Shukla180b1542020-08-13 01:14:02 +05303388 show_submodule_diff_summary(o, one->path ? one->path : two->path,
Jacob Keller602a2832016-08-31 16:27:23 -07003389 &one->oid, &two->oid,
Stefan Bellerf3597132017-06-29 17:07:00 -07003390 two->dirty_submodule);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003391 return;
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003392 } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF &&
3393 (!one->mode || S_ISGITLINK(one->mode)) &&
3394 (!two->mode || S_ISGITLINK(two->mode))) {
Stefan Bellerf3597132017-06-29 17:07:00 -07003395 show_submodule_inline_diff(o, one->path ? one->path : two->path,
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003396 &one->oid, &two->oid,
Stefan Bellerf3597132017-06-29 17:07:00 -07003397 two->dirty_submodule);
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003398 return;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003399 }
3400
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003401 if (o->flags.allow_textconv) {
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01003402 textconv_one = get_textconv(o->repo, one);
3403 textconv_two = get_textconv(o->repo, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003404 }
3405
Linus Torvalds71b989e2008-10-05 15:35:15 -04003406 /* Never use a non-valid filename anywhere if at all possible */
3407 name_a = DIFF_FILE_VALID(one) ? name_a : name_b;
3408 name_b = DIFF_FILE_VALID(two) ? name_b : name_a;
3409
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003410 a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
3411 b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
Junio C Hamano6973dca2006-04-21 23:57:45 -07003412 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
3413 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
Jeff Kingd9c552f2012-11-13 21:12:46 +05303414 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 -07003415 if (lbl[0][0] == '/') {
3416 /* /dev/null */
Jeff Kingd9c552f2012-11-13 21:12:46 +05303417 strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset);
Bert Wesarg37466442010-05-04 00:38:07 +02003418 if (xfrm_msg)
3419 strbuf_addstr(&header, xfrm_msg);
Christian Couder296c6bb2010-05-26 04:50:12 +02003420 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003421 }
3422 else if (lbl[1][0] == '/') {
Jeff Kingd9c552f2012-11-13 21:12:46 +05303423 strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset);
Bert Wesarg37466442010-05-04 00:38:07 +02003424 if (xfrm_msg)
3425 strbuf_addstr(&header, xfrm_msg);
Christian Couder296c6bb2010-05-26 04:50:12 +02003426 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003427 }
3428 else {
3429 if (one->mode != two->mode) {
Jeff Kingd9c552f2012-11-13 21:12:46 +05303430 strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset);
3431 strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset);
Christian Couder296c6bb2010-05-26 04:50:12 +02003432 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003433 }
Bert Wesarg37466442010-05-04 00:38:07 +02003434 if (xfrm_msg)
3435 strbuf_addstr(&header, xfrm_msg);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003436
Junio C Hamano6973dca2006-04-21 23:57:45 -07003437 /*
3438 * we do not run diff between different kind
3439 * of objects.
3440 */
3441 if ((one->mode ^ two->mode) & S_IFMT)
3442 goto free_ab_and_return;
Jeff King0c018572008-12-09 03:12:28 -05003443 if (complete_rewrite &&
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003444 (textconv_one || !diff_filespec_is_binary(o->repo, one)) &&
3445 (textconv_two || !diff_filespec_is_binary(o->repo, two))) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003446 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3447 header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003448 strbuf_reset(&header);
Jeff King3aa1f7c2008-12-09 03:13:21 -05003449 emit_rewrite_diff(name_a, name_b, one, two,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003450 textconv_one, textconv_two, o);
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01003451 o->found_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003452 goto free_ab_and_return;
3453 }
3454 }
3455
Junio C Hamano467ddc12011-02-28 16:11:55 -08003456 if (o->irreversible_delete && lbl[1][0] == '/') {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003457 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf,
3458 header.len, 0);
Junio C Hamano467ddc12011-02-28 16:11:55 -08003459 strbuf_reset(&header);
3460 goto free_ab_and_return;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003461 } else if (!o->flags.text &&
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003462 ( (!textconv_one && diff_filespec_is_binary(o->repo, one)) ||
3463 (!textconv_two && diff_filespec_is_binary(o->repo, two)) )) {
Stefan Beller4acaaa72017-06-29 17:06:58 -07003464 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003465 if (!one->data && !two->data &&
3466 S_ISREG(one->mode) && S_ISREG(two->mode) &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003467 !o->flags.binary) {
Jeff King4a7e27e2018-08-28 17:22:40 -04003468 if (oideq(&one->oid, &two->oid)) {
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003469 if (must_show_header)
Stefan Bellera29b0a12017-06-29 17:06:57 -07003470 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3471 header.buf, header.len,
3472 0);
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003473 goto free_ab_and_return;
3474 }
Stefan Bellera29b0a12017-06-29 17:06:57 -07003475 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3476 header.buf, header.len, 0);
Stefan Beller4acaaa72017-06-29 17:06:58 -07003477 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3478 diff_line_prefix(o), lbl[0], lbl[1]);
3479 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3480 sb.buf, sb.len, 0);
3481 strbuf_release(&sb);
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003482 goto free_ab_and_return;
3483 }
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003484 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3485 fill_mmfile(o->repo, &mf2, two) < 0)
Jeff Kingb3373982010-04-01 20:14:24 -04003486 die("unable to read files to diff");
Junio C Hamano06606262006-05-05 02:41:53 -07003487 /* Quite common confusing case */
3488 if (mf1.size == mf2.size &&
Christian Couder296c6bb2010-05-26 04:50:12 +02003489 !memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
3490 if (must_show_header)
Stefan Bellera29b0a12017-06-29 17:06:57 -07003491 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3492 header.buf, header.len, 0);
Junio C Hamano06606262006-05-05 02:41:53 -07003493 goto free_ab_and_return;
Christian Couder296c6bb2010-05-26 04:50:12 +02003494 }
Stefan Bellera29b0a12017-06-29 17:06:57 -07003495 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003496 strbuf_reset(&header);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003497 if (o->flags.binary)
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003498 emit_binary_diff(o, &mf1, &mf2);
Stefan Beller4acaaa72017-06-29 17:06:58 -07003499 else {
3500 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3501 diff_line_prefix(o), lbl[0], lbl[1]);
3502 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3503 sb.buf, sb.len, 0);
3504 strbuf_release(&sb);
3505 }
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01003506 o->found_changes = 1;
Junio C Hamano467ddc12011-02-28 16:11:55 -08003507 } else {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003508 /* Crazy xdl interfaces.. */
Jeff King0da0e922019-01-11 17:17:22 -05003509 const char *diffopts;
Jeff Kingae021d82014-06-18 15:47:50 -04003510 const char *v;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003511 xpparam_t xpp;
3512 xdemitconf_t xecfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003513 struct emit_callback ecbdata;
Jeff Kingbe58e702008-10-05 17:43:21 -04003514 const struct userdiff_funcname *pe;
Junio C Hamanof2584752007-07-06 00:45:10 -07003515
Junio C Hamanob3f01ff2012-02-29 18:14:16 -08003516 if (must_show_header) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003517 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3518 header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003519 strbuf_reset(&header);
3520 }
3521
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02003522 mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr);
3523 mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr);
Jeff King04427ac2008-10-26 00:44:53 -04003524
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003525 pe = diff_funcname_pattern(o, one);
Brandon Casey45e7ca02008-09-18 17:40:48 -05003526 if (!pe)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003527 pe = diff_funcname_pattern(o, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003528
Brian Downing9ccd0a82008-10-25 15:30:37 +02003529 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003530 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelincd112ce2006-06-13 18:45:44 +02003531 memset(&ecbdata, 0, sizeof(ecbdata));
Johannes Schindelin1cdde292018-08-13 04:33:11 -07003532 if (o->flags.suppress_diff_headers)
3533 lbl[0] = NULL;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003534 ecbdata.label_path = lbl;
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07003535 ecbdata.color_diff = want_color(o->use_color);
Nguyễn Thái Ngọc Duy26d024e2018-09-21 17:57:37 +02003536 ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
Junio C Hamano690ed842009-09-04 00:41:15 -07003537 if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
Junio C Hamanod68fe262009-09-14 22:05:57 -07003538 check_blank_at_eof(&mf1, &mf2, &ecbdata);
Bo Yanga3c158d2010-05-26 15:08:02 +08003539 ecbdata.opt = o;
Johannes Schindelin1cdde292018-08-13 04:33:11 -07003540 if (header.len && !o->flags.suppress_diff_headers)
3541 ecbdata.header = &header;
René Scharfe582aa002010-05-02 15:04:41 +02003542 xpp.flags = o->xdl_opts;
Michał Kępień296d4a92020-10-20 08:48:09 +02003543 xpp.ignore_regex = o->ignore_regex;
3544 xpp.ignore_regex_nr = o->ignore_regex_nr;
Jonathan Tan2477ab22017-11-27 11:47:47 -08003545 xpp.anchors = o->anchors;
3546 xpp.anchors_nr = o->anchors_nr;
Linus Torvaldsee1e5412006-05-13 13:23:48 -07003547 xecfg.ctxlen = o->context;
René Scharfe6d0e6742008-12-28 19:45:32 +01003548 xecfg.interhunkctxlen = o->interhunkcontext;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003549 xecfg.flags = XDL_EMIT_FUNCNAMES;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003550 if (o->flags.funccontext)
René Scharfe14937c22011-10-09 13:36:57 +02003551 xecfg.flags |= XDL_EMIT_FUNCCONTEXT;
Brandon Casey45e7ca02008-09-18 17:40:48 -05003552 if (pe)
Brandon Caseya0135852008-09-18 17:42:48 -05003553 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
Jeff King0da0e922019-01-11 17:17:22 -05003554
3555 diffopts = getenv("GIT_DIFF_OPTS");
Junio C Hamano6973dca2006-04-21 23:57:45 -07003556 if (!diffopts)
3557 ;
Jeff Kingae021d82014-06-18 15:47:50 -04003558 else if (skip_prefix(diffopts, "--unified=", &v))
3559 xecfg.ctxlen = strtoul(v, NULL, 10);
3560 else if (skip_prefix(diffopts, "-u", &v))
3561 xecfg.ctxlen = strtoul(v, NULL, 10);
Jeff King0da0e922019-01-11 17:17:22 -05003562
Thomas Rast77d1a522012-03-14 19:24:08 +01003563 if (o->word_diff)
3564 init_diff_words_data(&ecbdata, o, one, two);
Jeff King9346d6d2018-11-02 02:35:45 -04003565 if (xdi_diff_outf(&mf1, &mf2, NULL, fn_out_consume,
3566 &ecbdata, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04003567 die("unable to generate diff for %s", one->path);
Thomas Rast882749a2010-04-14 17:59:06 +02003568 if (o->word_diff)
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02003569 free_diff_words_data(&ecbdata);
Jeff King04427ac2008-10-26 00:44:53 -04003570 if (textconv_one)
3571 free(mf1.ptr);
3572 if (textconv_two)
3573 free(mf2.ptr);
René Scharfe8cfe5f12009-07-02 00:01:43 +02003574 xdiff_clear_find_func(&xecfg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003575 }
3576
3577 free_ab_and_return:
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003578 strbuf_release(&header);
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003579 diff_free_filespec_data(one);
3580 diff_free_filespec_data(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003581 free(a_one);
3582 free(b_two);
3583 return;
3584}
3585
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 21:09:59 +07003586static char *get_compact_summary(const struct diff_filepair *p, int is_renamed)
3587{
3588 if (!is_renamed) {
3589 if (p->status == DIFF_STATUS_ADDED) {
3590 if (S_ISLNK(p->two->mode))
3591 return "new +l";
3592 else if ((p->two->mode & 0777) == 0755)
3593 return "new +x";
3594 else
3595 return "new";
3596 } else if (p->status == DIFF_STATUS_DELETED)
3597 return "gone";
3598 }
3599 if (S_ISLNK(p->one->mode) && !S_ISLNK(p->two->mode))
3600 return "mode -l";
3601 else if (!S_ISLNK(p->one->mode) && S_ISLNK(p->two->mode))
3602 return "mode +l";
3603 else if ((p->one->mode & 0777) == 0644 &&
3604 (p->two->mode & 0777) == 0755)
3605 return "mode +x";
3606 else if ((p->one->mode & 0777) == 0755 &&
3607 (p->two->mode & 0777) == 0644)
3608 return "mode -x";
3609 return NULL;
3610}
3611
Junio C Hamano6973dca2006-04-21 23:57:45 -07003612static void builtin_diffstat(const char *name_a, const char *name_b,
3613 struct diff_filespec *one,
3614 struct diff_filespec *two,
Junio C Hamano710158e2006-04-25 23:40:09 -07003615 struct diffstat_t *diffstat,
Johannes Schindelin0d21efa2006-06-14 17:40:23 +02003616 struct diff_options *o,
Linus Torvalds74faaa12012-10-17 10:00:37 -07003617 struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003618{
3619 mmfile_t mf1, mf2;
3620 struct diffstat_file *data;
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003621 int may_differ;
Linus Torvalds74faaa12012-10-17 10:00:37 -07003622 int complete_rewrite = 0;
3623
3624 if (!DIFF_PAIR_UNMERGED(p)) {
3625 if (p->status == DIFF_STATUS_MODIFIED && p->score)
3626 complete_rewrite = 1;
3627 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003628
3629 data = diffstat_add(diffstat, name_a, name_b);
Junio C Hamano99bfd402012-11-27 11:17:14 -08003630 data->is_interesting = p->status != DIFF_STATUS_UNKNOWN;
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 21:09:59 +07003631 if (o->flags.stat_with_summary)
3632 data->comments = get_compact_summary(p, data->is_renamed);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003633
3634 if (!one || !two) {
3635 data->is_unmerged = 1;
3636 return;
3637 }
Jeff Kingded0abc2011-02-19 03:04:56 -05003638
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003639 /* saves some reads if true, not a guarantee of diff outcome */
3640 may_differ = !(one->oid_valid && two->oid_valid &&
3641 oideq(&one->oid, &two->oid));
Zbigniew Jędrzejewski-Szmek352ca4e2012-05-01 19:10:15 +02003642
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003643 if (diff_filespec_is_binary(o->repo, one) ||
3644 diff_filespec_is_binary(o->repo, two)) {
Jeff Kingded0abc2011-02-19 03:04:56 -05003645 data->is_binary = 1;
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003646 if (!may_differ) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02003647 data->added = 0;
3648 data->deleted = 0;
3649 } else {
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003650 data->added = diff_filespec_size(o->repo, two);
3651 data->deleted = diff_filespec_size(o->repo, one);
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02003652 }
Jeff Kingded0abc2011-02-19 03:04:56 -05003653 }
3654
3655 else if (complete_rewrite) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003656 diff_populate_filespec(o->repo, one, NULL);
3657 diff_populate_filespec(o->repo, two, NULL);
Junio C Hamano710158e2006-04-25 23:40:09 -07003658 data->deleted = count_lines(one->data, one->size);
3659 data->added = count_lines(two->data, two->size);
Junio C Hamano710158e2006-04-25 23:40:09 -07003660 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003661
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003662 else if (may_differ) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003663 /* Crazy xdl interfaces.. */
3664 xpparam_t xpp;
3665 xdemitconf_t xecfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003666
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003667 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3668 fill_mmfile(o->repo, &mf2, two) < 0)
Jeff Kingded0abc2011-02-19 03:04:56 -05003669 die("unable to read files to diff");
3670
Brian Downing9ccd0a82008-10-25 15:30:37 +02003671 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003672 memset(&xecfg, 0, sizeof(xecfg));
René Scharfe582aa002010-05-02 15:04:41 +02003673 xpp.flags = o->xdl_opts;
Michał Kępień296d4a92020-10-20 08:48:09 +02003674 xpp.ignore_regex = o->ignore_regex;
3675 xpp.ignore_regex_nr = o->ignore_regex_nr;
Jonathan Tan2477ab22017-11-27 11:47:47 -08003676 xpp.anchors = o->anchors;
3677 xpp.anchors_nr = o->anchors_nr;
Junio C Hamanof01cae92011-09-22 10:54:47 -07003678 xecfg.ctxlen = o->context;
3679 xecfg.interhunkctxlen = o->interhunkcontext;
Ævar Arnfjörð Bjarmason5d934602021-04-12 19:15:29 +02003680 xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
3681 if (xdi_diff_outf(&mf1, &mf2, NULL,
Jeff King3b40a092018-11-02 02:36:06 -04003682 diffstat_consume, diffstat, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04003683 die("unable to generate diffstat for %s", one->path);
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003684
3685 if (DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two)) {
3686 struct diffstat_file *file =
3687 diffstat->files[diffstat->nr - 1];
3688 /*
3689 * Omit diffstats of modified files where nothing changed.
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003690 * Even if may_differ, this might be the case due to
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003691 * ignoring whitespace changes, etc.
3692 *
3693 * But note that we special-case additions, deletions,
3694 * renames, and mode changes as adding an empty file,
3695 * for example is still of interest.
3696 */
3697 if ((p->status == DIFF_STATUS_MODIFIED)
3698 && !file->added
3699 && !file->deleted
3700 && one->mode == two->mode) {
3701 free_diffstat_file(file);
3702 diffstat->nr--;
3703 }
3704 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003705 }
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003706
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003707 diff_free_filespec_data(one);
3708 diff_free_filespec_data(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003709}
3710
Johannes Schindelin88246892006-05-20 23:43:13 +02003711static void builtin_checkdiff(const char *name_a, const char *name_b,
Junio C Hamanocd676a52008-02-12 14:26:02 -08003712 const char *attr_path,
Junio C Hamano5ff10dd2008-06-26 15:34:54 -07003713 struct diff_filespec *one,
3714 struct diff_filespec *two,
3715 struct diff_options *o)
Johannes Schindelin88246892006-05-20 23:43:13 +02003716{
3717 mmfile_t mf1, mf2;
3718 struct checkdiff_t data;
3719
3720 if (!two)
3721 return;
3722
3723 memset(&data, 0, sizeof(data));
Johannes Schindelin88246892006-05-20 23:43:13 +02003724 data.filename = name_b ? name_b : name_a;
3725 data.lineno = 0;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003726 data.o = o;
Nguyễn Thái Ngọc Duy26d024e2018-09-21 17:57:37 +02003727 data.ws_rule = whitespace_rule(o->repo->index, attr_path);
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +02003728 data.conflict_marker_size = ll_merge_marker_size(o->repo->index, attr_path);
Johannes Schindelin88246892006-05-20 23:43:13 +02003729
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003730 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3731 fill_mmfile(o->repo, &mf2, two) < 0)
Johannes Schindelin88246892006-05-20 23:43:13 +02003732 die("unable to read files to diff");
3733
Junio C Hamano5ff10dd2008-06-26 15:34:54 -07003734 /*
3735 * All the other codepaths check both sides, but not checking
3736 * the "old" side here is deliberate. We are checking the newly
3737 * introduced changes, and as long as the "new" side is text, we
3738 * can and should check what it introduces.
3739 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003740 if (diff_filespec_is_binary(o->repo, two))
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003741 goto free_and_return;
Johannes Schindelin88246892006-05-20 23:43:13 +02003742 else {
3743 /* Crazy xdl interfaces.. */
3744 xpparam_t xpp;
3745 xdemitconf_t xecfg;
Johannes Schindelin88246892006-05-20 23:43:13 +02003746
Brian Downing9ccd0a82008-10-25 15:30:37 +02003747 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003748 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanoc35539e2008-08-20 11:47:55 -07003749 xecfg.ctxlen = 1; /* at least one context line */
René Scharfe582aa002010-05-02 15:04:41 +02003750 xpp.flags = 0;
Jeff King75ab7632018-11-02 02:39:03 -04003751 if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume_hunk,
3752 checkdiff_consume, &data,
Jeff King3efb9882015-09-24 19:12:23 -04003753 &xpp, &xecfg))
3754 die("unable to generate checkdiff for %s", one->path);
Junio C Hamano877f23c2008-06-26 15:36:59 -07003755
Junio C Hamano467babf2009-09-03 23:39:43 -07003756 if (data.ws_rule & WS_BLANK_AT_EOF) {
Junio C Hamanod68fe262009-09-14 22:05:57 -07003757 struct emit_callback ecbdata;
3758 int blank_at_eof;
3759
3760 ecbdata.ws_rule = data.ws_rule;
3761 check_blank_at_eof(&mf1, &mf2, &ecbdata);
Christoph Mallon8837d332010-10-10 19:24:06 +02003762 blank_at_eof = ecbdata.blank_at_eof_in_postimage;
Junio C Hamanod68fe262009-09-14 22:05:57 -07003763
Junio C Hamano467babf2009-09-03 23:39:43 -07003764 if (blank_at_eof) {
3765 static char *err;
3766 if (!err)
3767 err = whitespace_error_string(WS_BLANK_AT_EOF);
3768 fprintf(o->file, "%s:%d: %s.\n",
3769 data.filename, blank_at_eof, err);
3770 data.status = 1; /* report errors */
3771 }
Junio C Hamano877f23c2008-06-26 15:36:59 -07003772 }
Johannes Schindelin88246892006-05-20 23:43:13 +02003773 }
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003774 free_and_return:
3775 diff_free_filespec_data(one);
3776 diff_free_filespec_data(two);
Wincent Colaiuta62c64892007-12-13 21:24:52 +01003777 if (data.status)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003778 o->flags.check_failed = 1;
Johannes Schindelin88246892006-05-20 23:43:13 +02003779}
3780
Junio C Hamano6973dca2006-04-21 23:57:45 -07003781struct diff_filespec *alloc_filespec(const char *path)
3782{
Jeff King96ffc062016-02-22 17:44:32 -05003783 struct diff_filespec *spec;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003784
Jeff King96ffc062016-02-22 17:44:32 -05003785 FLEXPTR_ALLOC_STR(spec, path, path);
Linus Torvalds9fb88412007-10-25 11:19:10 -07003786 spec->count = 1;
Jeff King122aa6f2008-10-05 17:43:36 -04003787 spec->is_binary = -1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003788 return spec;
3789}
3790
Linus Torvalds9fb88412007-10-25 11:19:10 -07003791void free_filespec(struct diff_filespec *spec)
3792{
3793 if (!--spec->count) {
3794 diff_free_filespec_data(spec);
3795 free(spec);
3796 }
3797}
3798
Brandon Williamsf9704c22017-05-30 10:30:50 -07003799void fill_filespec(struct diff_filespec *spec, const struct object_id *oid,
3800 int oid_valid, unsigned short mode)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003801{
3802 if (mode) {
3803 spec->mode = canon_mode(mode);
Brandon Williamsf9704c22017-05-30 10:30:50 -07003804 oidcpy(&spec->oid, oid);
3805 spec->oid_valid = oid_valid;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003806 }
3807}
3808
3809/*
Jakub Narebski5adf3172007-05-26 00:37:40 +02003810 * Given a name and sha1 pair, if the index tells us the file in
Junio C Hamano6973dca2006-04-21 23:57:45 -07003811 * the work tree has that object contents, return true, so that
3812 * prepare_temp_file() does not have to inflate and extract.
3813 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003814static int reuse_worktree_file(struct index_state *istate,
3815 const char *name,
3816 const struct object_id *oid,
3817 int want_file)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003818{
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07003819 const struct cache_entry *ce;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003820 struct stat st;
3821 int pos, len;
3822
Junio C Hamano150115a2009-03-22 15:26:07 -07003823 /*
3824 * We do not read the cache ourselves here, because the
Junio C Hamano6973dca2006-04-21 23:57:45 -07003825 * benchmark with my previous version that always reads cache
3826 * shows that it makes things worse for diff-tree comparing
3827 * two linux-2.6 kernel trees in an already checked out work
3828 * tree. This is because most diff-tree comparisons deal with
3829 * only a small number of files, while reading the cache is
3830 * expensive for a large project, and its cost outweighs the
3831 * savings we get by not inflating the object to a temporary
3832 * file. Practically, this code only helps when we are used
3833 * by diff-cache --cached, which does read the cache before
3834 * calling us.
3835 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003836 if (!istate->cache)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003837 return 0;
3838
Shawn O. Pearce1510fea2006-12-14 06:15:57 -05003839 /* We want to avoid the working directory if our caller
3840 * doesn't need the data in a normal file, this system
3841 * is rather slow with its stat/open/mmap/close syscalls,
3842 * and the object is contained in a pack file. The pack
3843 * is probably already open and will be faster to obtain
3844 * the data through than the working directory. Loose
3845 * objects however would tend to be slower as they need
3846 * to be individually opened and inflated.
3847 */
brian m. carlson14c3c802018-05-02 00:25:33 +00003848 if (!FAST_WORKING_DIRECTORY && !want_file && has_object_pack(oid))
Shawn O. Pearce1510fea2006-12-14 06:15:57 -05003849 return 0;
3850
Jeff King06dec432016-07-22 11:27:53 -04003851 /*
3852 * Similarly, if we'd have to convert the file contents anyway, that
3853 * makes the optimization not worthwhile.
3854 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003855 if (!want_file && would_convert_to_git(istate, name))
Jeff King06dec432016-07-22 11:27:53 -04003856 return 0;
3857
Derrick Stoleead90da72021-09-08 11:23:56 +00003858 /*
3859 * If this path does not match our sparse-checkout definition,
3860 * then the file will not be in the working directory.
3861 */
3862 if (!path_in_sparse_checkout(name, istate))
3863 return 0;
3864
Junio C Hamano6973dca2006-04-21 23:57:45 -07003865 len = strlen(name);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003866 pos = index_name_pos(istate, name, len);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003867 if (pos < 0)
3868 return 0;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003869 ce = istate->cache[pos];
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003870
3871 /*
3872 * This is not the sha1 we are looking for, or
3873 * unreusable because it is not a regular file.
Junio C Hamano6973dca2006-04-21 23:57:45 -07003874 */
Jeff King9001dc22018-08-28 17:22:48 -04003875 if (!oideq(oid, &ce->oid) || !S_ISREG(ce->ce_mode))
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003876 return 0;
3877
3878 /*
Junio C Hamano150115a2009-03-22 15:26:07 -07003879 * If ce is marked as "assume unchanged", there is no
3880 * guarantee that work tree matches what we are looking for.
3881 */
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +07003882 if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
Junio C Hamano150115a2009-03-22 15:26:07 -07003883 return 0;
3884
3885 /*
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003886 * If ce matches the file in the work tree, we can reuse it.
3887 */
3888 if (ce_uptodate(ce) ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003889 (!lstat(name, &st) && !ie_match_stat(istate, ce, &st, 0)))
Junio C Hamanoeadb5832008-01-18 23:45:24 -08003890 return 1;
3891
3892 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003893}
3894
Linus Torvalds04786752007-04-15 11:14:28 -07003895static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
3896{
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003897 struct strbuf buf = STRBUF_INIT;
3898 char *dirty = "";
Junio C Hamano8e08b412010-01-16 18:42:53 +01003899
3900 /* Are we looking at the work tree? */
Jens Lehmann85adbf22010-03-12 22:23:52 +01003901 if (s->dirty_submodule)
Junio C Hamano8e08b412010-01-16 18:42:53 +01003902 dirty = "-dirty";
3903
brian m. carlsona0d12c42016-06-24 23:09:23 +00003904 strbuf_addf(&buf, "Subproject commit %s%s\n",
3905 oid_to_hex(&s->oid), dirty);
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003906 s->size = buf.len;
Linus Torvalds04786752007-04-15 11:14:28 -07003907 if (size_only) {
3908 s->data = NULL;
Jeff Kingb1ddfb92016-02-22 17:45:08 -05003909 strbuf_release(&buf);
3910 } else {
3911 s->data = strbuf_detach(&buf, NULL);
3912 s->should_free = 1;
Linus Torvalds04786752007-04-15 11:14:28 -07003913 }
3914 return 0;
3915}
3916
Junio C Hamano6973dca2006-04-21 23:57:45 -07003917/*
3918 * While doing rename detection and pickaxe operation, we may need to
3919 * grab the data for the blob (or file) for our own in-core comparison.
3920 * diff_filespec has data and size fields for this purpose.
3921 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003922int diff_populate_filespec(struct repository *r,
3923 struct diff_filespec *s,
Jonathan Tan1c37e862020-04-07 15:11:41 -07003924 const struct diff_populate_filespec_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003925{
Jonathan Tan1c37e862020-04-07 15:11:41 -07003926 int size_only = options ? options->check_size_only : 0;
3927 int check_binary = options ? options->check_binary : 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003928 int err = 0;
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01003929 int conv_flags = global_conv_flags_eol;
Junio C Hamano5430bb22013-06-24 14:35:04 -07003930 /*
3931 * demote FAIL to WARN to allow inspecting the situation
3932 * instead of refusing.
3933 */
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01003934 if (conv_flags & CONV_EOL_RNDTRP_DIE)
3935 conv_flags = CONV_EOL_RNDTRP_WARN;
Junio C Hamano5430bb22013-06-24 14:35:04 -07003936
Junio C Hamano6973dca2006-04-21 23:57:45 -07003937 if (!DIFF_FILE_VALID(s))
3938 die("internal error: asking to populate invalid file.");
3939 if (S_ISDIR(s->mode))
3940 return -1;
3941
Junio C Hamano6973dca2006-04-21 23:57:45 -07003942 if (s->data)
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003943 return 0;
Linus Torvalds04786752007-04-15 11:14:28 -07003944
Junio C Hamano6e0b8ed2007-05-07 01:14:21 -07003945 if (size_only && 0 < s->size)
3946 return 0;
3947
Martin Waitz302b9282007-05-21 22:08:28 +02003948 if (S_ISGITLINK(s->mode))
Linus Torvalds04786752007-04-15 11:14:28 -07003949 return diff_populate_gitlink(s, size_only);
3950
brian m. carlson41c95602016-06-24 23:09:24 +00003951 if (!s->oid_valid ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003952 reuse_worktree_file(r->index, s->path, &s->oid, 0)) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05003953 struct strbuf buf = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003954 struct stat st;
3955 int fd;
Linus Torvalds6c510be2007-02-13 11:07:23 -08003956
Junio C Hamano6973dca2006-04-21 23:57:45 -07003957 if (lstat(s->path, &st) < 0) {
Andrey Okoshkin10e0ca82017-10-27 12:33:25 +03003958 err_empty:
3959 err = -1;
3960 empty:
3961 s->data = (char *)"";
3962 s->size = 0;
3963 return err;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003964 }
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05003965 s->size = xsize_t(st.st_size);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003966 if (!s->size)
3967 goto empty;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003968 if (S_ISLNK(st.st_mode)) {
Linus Torvaldscf219d82008-12-17 10:26:13 -08003969 struct strbuf sb = STRBUF_INIT;
3970
3971 if (strbuf_readlink(&sb, s->path, s->size))
Junio C Hamano6973dca2006-04-21 23:57:45 -07003972 goto err_empty;
René Scharfe0956a6d2008-12-18 17:56:51 +01003973 s->size = sb.len;
3974 s->data = strbuf_detach(&sb, NULL);
Linus Torvaldscf219d82008-12-17 10:26:13 -08003975 s->should_free = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003976 return 0;
3977 }
Junio C Hamano12426e12017-03-01 18:04:44 +01003978
3979 /*
3980 * Even if the caller would be happy with getting
3981 * only the size, we cannot return early at this
3982 * point if the path requires us to run the content
3983 * conversion.
3984 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003985 if (size_only && !would_convert_to_git(r->index, s->path))
Linus Torvaldscf219d82008-12-17 10:26:13 -08003986 return 0;
Junio C Hamano12426e12017-03-01 18:04:44 +01003987
3988 /*
3989 * Note: this check uses xsize_t(st.st_size) that may
3990 * not be the true size of the blob after it goes
3991 * through convert_to_git(). This may not strictly be
3992 * correct, but the whole point of big_file_threshold
3993 * and is_binary check being that we want to avoid
3994 * opening the file and inspecting the contents, this
3995 * is probably fine.
3996 */
Jonathan Tan1c37e862020-04-07 15:11:41 -07003997 if (check_binary &&
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003998 s->size > big_file_threshold && s->is_binary == -1) {
3999 s->is_binary = 1;
4000 return 0;
4001 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004002 fd = open(s->path, O_RDONLY);
4003 if (fd < 0)
4004 goto err_empty;
Shawn O. Pearcec4712e42006-12-24 00:47:23 -05004005 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004006 close(fd);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004007 s->should_munmap = 1;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004008
4009 /*
4010 * Convert from working tree format to canonical git format
4011 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004012 if (convert_to_git(r->index, s->path, s->data, s->size, &buf, conv_flags)) {
René Scharfec32f7492007-10-21 11:23:49 +02004013 size_t size = 0;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004014 munmap(s->data, s->size);
4015 s->should_munmap = 0;
René Scharfec32f7492007-10-21 11:23:49 +02004016 s->data = strbuf_detach(&buf, &size);
4017 s->size = size;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004018 s->should_free = 1;
4019 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004020 }
4021 else {
Jonathan Tanc14b6f82020-04-07 15:11:42 -07004022 struct object_info info = {
4023 .sizep = &s->size
4024 };
4025
4026 if (!(size_only || check_binary))
4027 /*
4028 * Set contentp, since there is no chance that merely
4029 * the size is sufficient.
4030 */
4031 info.contentp = &s->data;
4032
Jonathan Tan95acf112020-04-07 15:11:43 -07004033 if (options && options->missing_object_cb) {
4034 if (!oid_object_info_extended(r, &s->oid, &info,
4035 OBJECT_INFO_LOOKUP_REPLACE |
4036 OBJECT_INFO_SKIP_FETCH_OBJECT))
4037 goto object_read;
4038 options->missing_object_cb(options->missing_object_data);
4039 }
Jonathan Tanc14b6f82020-04-07 15:11:42 -07004040 if (oid_object_info_extended(r, &s->oid, &info,
4041 OBJECT_INFO_LOOKUP_REPLACE))
4042 die("unable to read %s", oid_to_hex(&s->oid));
4043
Jonathan Tan95acf112020-04-07 15:11:43 -07004044object_read:
Jonathan Tan1c37e862020-04-07 15:11:41 -07004045 if (size_only || check_binary) {
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07004046 if (size_only)
4047 return 0;
4048 if (s->size > big_file_threshold && s->is_binary == -1) {
4049 s->is_binary = 1;
4050 return 0;
4051 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004052 }
Jonathan Tanc14b6f82020-04-07 15:11:42 -07004053 if (!info.contentp) {
4054 info.contentp = &s->data;
4055 if (oid_object_info_extended(r, &s->oid, &info,
4056 OBJECT_INFO_LOOKUP_REPLACE))
4057 die("unable to read %s", oid_to_hex(&s->oid));
4058 }
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07004059 s->should_free = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004060 }
4061 return 0;
4062}
4063
Junio C Hamano8ae92e62007-10-02 21:01:03 -07004064void diff_free_filespec_blob(struct diff_filespec *s)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004065{
4066 if (s->should_free)
4067 free(s->data);
4068 else if (s->should_munmap)
4069 munmap(s->data, s->size);
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07004070
4071 if (s->should_free || s->should_munmap) {
4072 s->should_free = s->should_munmap = 0;
4073 s->data = NULL;
4074 }
Jeff Kingeede7b72007-09-25 15:29:42 -04004075}
4076
4077void diff_free_filespec_data(struct diff_filespec *s)
4078{
Jinoh Kang24695932020-11-06 17:14:52 +00004079 if (!s)
4080 return;
4081
Junio C Hamano8ae92e62007-10-02 21:01:03 -07004082 diff_free_filespec_blob(s);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00004083 FREE_AND_NULL(s->cnt_data);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004084}
4085
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004086static void prep_temp_blob(struct index_state *istate,
4087 const char *path, struct diff_tempfile *temp,
Junio C Hamano6973dca2006-04-21 23:57:45 -07004088 void *blob,
4089 unsigned long size,
brian m. carlson09bdff22016-06-24 23:09:29 +00004090 const struct object_id *oid,
Junio C Hamano6973dca2006-04-21 23:57:45 -07004091 int mode)
4092{
Johannes Schindelin4e218f52009-03-21 12:42:52 +01004093 struct strbuf buf = STRBUF_INIT;
Brandon Williamsc2a46a72018-02-14 10:59:54 -08004094 struct strbuf tempfile = STRBUF_INIT;
David Aguilar003b33a2009-05-31 01:35:52 -07004095 char *path_dup = xstrdup(path);
4096 const char *base = basename(path_dup);
brian m. carlsonc397aac2020-03-16 18:05:03 +00004097 struct checkout_metadata meta;
4098
4099 init_checkout_metadata(&meta, NULL, NULL, oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004100
David Aguilar003b33a2009-05-31 01:35:52 -07004101 /* Generate "XXXXXX_basename.ext" */
Brandon Williamsc2a46a72018-02-14 10:59:54 -08004102 strbuf_addstr(&tempfile, "XXXXXX_");
4103 strbuf_addstr(&tempfile, base);
David Aguilar003b33a2009-05-31 01:35:52 -07004104
Brandon Williamsc2a46a72018-02-14 10:59:54 -08004105 temp->tempfile = mks_tempfile_ts(tempfile.buf, strlen(base) + 1);
Jeff King076aa2c2017-09-05 08:15:08 -04004106 if (!temp->tempfile)
Thomas Rastd824cbb2009-06-27 17:58:46 +02004107 die_errno("unable to create temp-file");
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004108 if (convert_to_working_tree(istate, path,
brian m. carlsonc397aac2020-03-16 18:05:03 +00004109 (const char *)blob, (size_t)size, &buf, &meta)) {
Johannes Schindelin4e218f52009-03-21 12:42:52 +01004110 blob = buf.buf;
4111 size = buf.len;
4112 }
Junio C Hamanoc50424a2017-09-25 15:24:06 +09004113 if (write_in_full(temp->tempfile->fd, blob, size) < 0 ||
Jeff King076aa2c2017-09-05 08:15:08 -04004114 close_tempfile_gently(temp->tempfile))
Thomas Rast0721c312009-06-27 17:58:47 +02004115 die_errno("unable to write temp-file");
Jeff King076aa2c2017-09-05 08:15:08 -04004116 temp->name = get_tempfile_path(temp->tempfile);
brian m. carlson09bdff22016-06-24 23:09:29 +00004117 oid_to_hex_r(temp->hex, oid);
Jeff King5096d492015-09-24 17:06:08 -04004118 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode);
Johannes Schindelin4e218f52009-03-21 12:42:52 +01004119 strbuf_release(&buf);
Brandon Williamsc2a46a72018-02-14 10:59:54 -08004120 strbuf_release(&tempfile);
David Aguilar003b33a2009-05-31 01:35:52 -07004121 free(path_dup);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004122}
4123
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004124static struct diff_tempfile *prepare_temp_file(struct repository *r,
4125 const char *name,
4126 struct diff_filespec *one)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004127{
Jeff King479b0ae2009-01-22 00:59:56 -05004128 struct diff_tempfile *temp = claim_diff_tempfile();
4129
Junio C Hamano6973dca2006-04-21 23:57:45 -07004130 if (!DIFF_FILE_VALID(one)) {
4131 not_a_valid_file:
4132 /* A '-' entry produces this for file-2, and
4133 * a '+' entry produces this for file-1.
4134 */
4135 temp->name = "/dev/null";
Jeff King5096d492015-09-24 17:06:08 -04004136 xsnprintf(temp->hex, sizeof(temp->hex), ".");
4137 xsnprintf(temp->mode, sizeof(temp->mode), ".");
Jeff King479b0ae2009-01-22 00:59:56 -05004138 return temp;
4139 }
4140
Thomas Rastaba47272014-02-16 17:52:34 +01004141 if (!S_ISGITLINK(one->mode) &&
brian m. carlson41c95602016-06-24 23:09:24 +00004142 (!one->oid_valid ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004143 reuse_worktree_file(r->index, name, &one->oid, 1))) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07004144 struct stat st;
4145 if (lstat(name, &st) < 0) {
4146 if (errno == ENOENT)
4147 goto not_a_valid_file;
Thomas Rastd824cbb2009-06-27 17:58:46 +02004148 die_errno("stat(%s)", name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004149 }
4150 if (S_ISLNK(st.st_mode)) {
Jeff King3cd73882009-05-25 06:46:09 -04004151 struct strbuf sb = STRBUF_INIT;
4152 if (strbuf_readlink(&sb, name, st.st_size) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02004153 die_errno("readlink(%s)", name);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004154 prep_temp_blob(r->index, name, temp, sb.buf, sb.len,
brian m. carlson41c95602016-06-24 23:09:24 +00004155 (one->oid_valid ?
brian m. carlson14228442021-04-26 01:02:56 +00004156 &one->oid : null_oid()),
brian m. carlson41c95602016-06-24 23:09:24 +00004157 (one->oid_valid ?
Junio C Hamano6973dca2006-04-21 23:57:45 -07004158 one->mode : S_IFLNK));
Jeff King3cd73882009-05-25 06:46:09 -04004159 strbuf_release(&sb);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004160 }
4161 else {
4162 /* we can borrow from the file in the work tree */
4163 temp->name = name;
brian m. carlson41c95602016-06-24 23:09:24 +00004164 if (!one->oid_valid)
brian m. carlson14228442021-04-26 01:02:56 +00004165 oid_to_hex_r(temp->hex, null_oid());
Junio C Hamano6973dca2006-04-21 23:57:45 -07004166 else
René Scharfe24905742017-01-28 23:03:03 +01004167 oid_to_hex_r(temp->hex, &one->oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004168 /* Even though we may sometimes borrow the
4169 * contents from the work tree, we always want
4170 * one->mode. mode is trustworthy even when
Brandon Williams74014152017-05-30 10:30:52 -07004171 * !(one->oid_valid), as long as
Junio C Hamano6973dca2006-04-21 23:57:45 -07004172 * DIFF_FILE_VALID(one).
4173 */
Jeff King5096d492015-09-24 17:06:08 -04004174 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004175 }
Jeff King479b0ae2009-01-22 00:59:56 -05004176 return temp;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004177 }
4178 else {
Jonathan Tan1c37e862020-04-07 15:11:41 -07004179 if (diff_populate_filespec(r, one, NULL))
Junio C Hamano6973dca2006-04-21 23:57:45 -07004180 die("cannot read data blob for %s", one->path);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004181 prep_temp_blob(r->index, name, temp,
4182 one->data, one->size,
brian m. carlson09bdff22016-06-24 23:09:29 +00004183 &one->oid, one->mode);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004184 }
Jeff King479b0ae2009-01-22 00:59:56 -05004185 return temp;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004186}
4187
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004188static void add_external_diff_name(struct repository *r,
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004189 struct strvec *argv,
Jeff Kingf3efe782014-04-19 15:22:25 -04004190 const char *name,
4191 struct diff_filespec *df)
4192{
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004193 struct diff_tempfile *temp = prepare_temp_file(r, name, df);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004194 strvec_push(argv, temp->name);
4195 strvec_push(argv, temp->hex);
4196 strvec_push(argv, temp->mode);
Jeff Kingf3efe782014-04-19 15:22:25 -04004197}
4198
Junio C Hamano6973dca2006-04-21 23:57:45 -07004199/* An external diff command takes:
4200 *
4201 * diff-cmd name infile1 infile1-sha1 infile1-mode \
4202 * infile2 infile2-sha1 infile2-mode [ rename-to ]
4203 *
4204 */
4205static void run_external_diff(const char *pgm,
4206 const char *name,
4207 const char *other,
4208 struct diff_filespec *one,
4209 struct diff_filespec *two,
4210 const char *xfrm_msg,
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004211 struct diff_options *o)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004212{
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004213 struct strvec argv = STRVEC_INIT;
4214 struct strvec env = STRVEC_INIT;
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004215 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004216
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004217 strvec_push(&argv, pgm);
4218 strvec_push(&argv, name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004219
Junio C Hamano6973dca2006-04-21 23:57:45 -07004220 if (one && two) {
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004221 add_external_diff_name(o->repo, &argv, name, one);
Jeff Kingf3efe782014-04-19 15:22:25 -04004222 if (!other)
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004223 add_external_diff_name(o->repo, &argv, name, two);
Jeff Kingf3efe782014-04-19 15:22:25 -04004224 else {
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004225 add_external_diff_name(o->repo, &argv, other, two);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004226 strvec_push(&argv, other);
4227 strvec_push(&argv, xfrm_msg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004228 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004229 }
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004230
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004231 strvec_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter);
4232 strvec_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr);
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004233
Johannes Schindelin3aef54e2019-07-11 01:23:41 -07004234 diff_free_filespec_data(one);
4235 diff_free_filespec_data(two);
Jeff Kingd70a9eb2020-07-28 20:37:20 -04004236 if (run_command_v_opt_cd_env(argv.v, RUN_USING_SHELL, NULL, env.v))
Jeff King89294d12014-04-19 15:19:19 -04004237 die(_("external diff died, stopping at %s"), name);
4238
Junio C Hamano6973dca2006-04-21 23:57:45 -07004239 remove_tempfile();
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004240 strvec_clear(&argv);
4241 strvec_clear(&env);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004242}
4243
Junio C Hamanob67b9612009-01-26 00:08:24 -08004244static int similarity_index(struct diff_filepair *p)
4245{
4246 return p->score * 100 / MAX_SCORE;
4247}
4248
Jeff King4f036662016-10-20 02:21:25 -04004249static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev)
4250{
4251 if (startup_info->have_repository)
brian m. carlsonaab95832018-03-12 02:27:30 +00004252 return find_unique_abbrev(oid, abbrev);
Jeff King4f036662016-10-20 02:21:25 -04004253 else {
4254 char *hex = oid_to_hex(oid);
Junio C Hamano0d9c5272016-10-27 14:58:48 -07004255 if (abbrev < 0)
4256 abbrev = FALLBACK_DEFAULT_ABBREV;
brian m. carlson02afca12018-07-16 01:28:05 +00004257 if (abbrev > the_hash_algo->hexsz)
Johannes Schindelin033abf92018-05-02 11:38:39 +02004258 BUG("oid abbreviation out of range: %d", abbrev);
Jack Bates43d19482016-12-06 09:56:14 -07004259 if (abbrev)
4260 hex[abbrev] = '\0';
Jeff King4f036662016-10-20 02:21:25 -04004261 return hex;
4262 }
4263}
4264
Junio C Hamanob67b9612009-01-26 00:08:24 -08004265static void fill_metainfo(struct strbuf *msg,
4266 const char *name,
4267 const char *other,
4268 struct diff_filespec *one,
4269 struct diff_filespec *two,
4270 struct diff_options *o,
Bert Wesarg37466442010-05-04 00:38:07 +02004271 struct diff_filepair *p,
Junio C Hamano59777442010-06-18 11:16:57 -07004272 int *must_show_header,
Bert Wesarg37466442010-05-04 00:38:07 +02004273 int use_color)
Junio C Hamanob67b9612009-01-26 00:08:24 -08004274{
Bert Wesarg37466442010-05-04 00:38:07 +02004275 const char *set = diff_get_color(use_color, DIFF_METAINFO);
4276 const char *reset = diff_get_color(use_color, DIFF_RESET);
John Keeping30997bb2013-02-07 20:15:27 +00004277 const char *line_prefix = diff_line_prefix(o);
Bo Yang7be57612010-05-26 15:23:54 +08004278
Christian Couder296c6bb2010-05-26 04:50:12 +02004279 *must_show_header = 1;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004280 strbuf_init(msg, PATH_MAX * 2 + 300);
4281 switch (p->status) {
4282 case DIFF_STATUS_COPIED:
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004283 strbuf_addf(msg, "%s%ssimilarity index %d%%",
4284 line_prefix, set, similarity_index(p));
4285 strbuf_addf(msg, "%s\n%s%scopy from ",
4286 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004287 quote_c_style(name, msg, NULL, 0);
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004288 strbuf_addf(msg, "%s\n%s%scopy to ", reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004289 quote_c_style(other, msg, NULL, 0);
Bert Wesarg37466442010-05-04 00:38:07 +02004290 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004291 break;
4292 case DIFF_STATUS_RENAMED:
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004293 strbuf_addf(msg, "%s%ssimilarity index %d%%",
4294 line_prefix, set, similarity_index(p));
4295 strbuf_addf(msg, "%s\n%s%srename from ",
4296 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004297 quote_c_style(name, msg, NULL, 0);
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004298 strbuf_addf(msg, "%s\n%s%srename to ",
4299 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004300 quote_c_style(other, msg, NULL, 0);
Bert Wesarg37466442010-05-04 00:38:07 +02004301 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004302 break;
4303 case DIFF_STATUS_MODIFIED:
4304 if (p->score) {
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004305 strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n",
4306 line_prefix,
Bert Wesarg37466442010-05-04 00:38:07 +02004307 set, similarity_index(p), reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004308 break;
4309 }
4310 /* fallthru */
4311 default:
Christian Couder296c6bb2010-05-26 04:50:12 +02004312 *must_show_header = 0;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004313 }
Jeff King9001dc22018-08-28 17:22:48 -04004314 if (one && two && !oideq(&one->oid, &two->oid)) {
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004315 const unsigned hexsz = the_hash_algo->hexsz;
Đoàn Trần Công Danh3046c7f2020-08-21 18:51:47 +07004316 int abbrev = o->abbrev ? o->abbrev : DEFAULT_ABBREV;
4317
4318 if (o->flags.full_index)
4319 abbrev = hexsz;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004320
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004321 if (o->flags.binary) {
Junio C Hamanob67b9612009-01-26 00:08:24 -08004322 mmfile_t mf;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004323 if ((!fill_mmfile(o->repo, &mf, one) &&
4324 diff_filespec_is_binary(o->repo, one)) ||
4325 (!fill_mmfile(o->repo, &mf, two) &&
4326 diff_filespec_is_binary(o->repo, two)))
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004327 abbrev = hexsz;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004328 }
Jeff King4f036662016-10-20 02:21:25 -04004329 strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set,
4330 diff_abbrev_oid(&one->oid, abbrev),
4331 diff_abbrev_oid(&two->oid, abbrev));
Junio C Hamanob67b9612009-01-26 00:08:24 -08004332 if (one->mode == two->mode)
4333 strbuf_addf(msg, " %06o", one->mode);
Bert Wesarg37466442010-05-04 00:38:07 +02004334 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004335 }
Junio C Hamanob67b9612009-01-26 00:08:24 -08004336}
4337
Junio C Hamano6973dca2006-04-21 23:57:45 -07004338static void run_diff_cmd(const char *pgm,
4339 const char *name,
4340 const char *other,
Junio C Hamanocd676a52008-02-12 14:26:02 -08004341 const char *attr_path,
Junio C Hamano6973dca2006-04-21 23:57:45 -07004342 struct diff_filespec *one,
4343 struct diff_filespec *two,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004344 struct strbuf *msg,
Junio C Hamano051308f2006-05-04 16:51:44 -07004345 struct diff_options *o,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004346 struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004347{
Junio C Hamanob67b9612009-01-26 00:08:24 -08004348 const char *xfrm_msg = NULL;
4349 int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
Christian Couder296c6bb2010-05-26 04:50:12 +02004350 int must_show_header = 0;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004351
Junio C Hamanobd8c1a92012-07-17 22:08:59 -07004352
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004353 if (o->flags.allow_external) {
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02004354 struct userdiff_driver *drv;
4355
4356 drv = userdiff_find_by_path(o->repo->index, attr_path);
Jeff Kingbe58e702008-10-05 17:43:21 -04004357 if (drv && drv->external)
4358 pgm = drv->external;
Junio C Hamanof1af60b2007-04-22 17:52:55 -07004359 }
4360
Bert Wesarg37466442010-05-04 00:38:07 +02004361 if (msg) {
4362 /*
4363 * don't use colors when the header is intended for an
4364 * external diff driver
4365 */
4366 fill_metainfo(msg, name, other, one, two, o, p,
Junio C Hamano59777442010-06-18 11:16:57 -07004367 &must_show_header,
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07004368 want_color(o->use_color) && !pgm);
Bert Wesarg37466442010-05-04 00:38:07 +02004369 xfrm_msg = msg->len ? msg->buf : NULL;
4370 }
4371
Junio C Hamano6973dca2006-04-21 23:57:45 -07004372 if (pgm) {
Jeff King4bc17922019-02-14 00:49:42 -05004373 run_external_diff(pgm, name, other, one, two, xfrm_msg, o);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004374 return;
4375 }
4376 if (one && two)
4377 builtin_diff(name, other ? other : name,
Christian Couder296c6bb2010-05-26 04:50:12 +02004378 one, two, xfrm_msg, must_show_header,
4379 o, complete_rewrite);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004380 else
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004381 fprintf(o->file, "* Unmerged path %s\n", name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004382}
4383
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004384static void diff_fill_oid_info(struct diff_filespec *one, struct index_state *istate)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004385{
4386 if (DIFF_FILE_VALID(one)) {
brian m. carlson41c95602016-06-24 23:09:24 +00004387 if (!one->oid_valid) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07004388 struct stat st;
Junio C Hamano4682d852012-06-27 20:14:47 -07004389 if (one->is_stdin) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00004390 oidclr(&one->oid);
Johannes Schindelin5332b2a2007-02-25 23:36:10 +01004391 return;
4392 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004393 if (lstat(one->path, &st) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02004394 die_errno("stat '%s'", one->path);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004395 if (index_path(istate, &one->oid, one->path, &st, 0))
Alexander Potashevd7530702009-01-04 21:38:41 +03004396 die("cannot hash %s", one->path);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004397 }
4398 }
4399 else
brian m. carlsona0d12c42016-06-24 23:09:23 +00004400 oidclr(&one->oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004401}
4402
Junio C Hamanocd676a52008-02-12 14:26:02 -08004403static void strip_prefix(int prefix_length, const char **namep, const char **otherp)
4404{
4405 /* Strip the prefix but do not molest /dev/null and absolute paths */
Johannes Sixtffd04e92018-10-19 18:58:07 +02004406 if (*namep && !is_absolute_path(*namep)) {
Junio C Hamanocd676a52008-02-12 14:26:02 -08004407 *namep += prefix_length;
Jakub Narebskid8faea92010-08-09 10:50:53 -04004408 if (**namep == '/')
4409 ++*namep;
4410 }
Johannes Sixtffd04e92018-10-19 18:58:07 +02004411 if (*otherp && !is_absolute_path(*otherp)) {
Junio C Hamanocd676a52008-02-12 14:26:02 -08004412 *otherp += prefix_length;
Jakub Narebskid8faea92010-08-09 10:50:53 -04004413 if (**otherp == '/')
4414 ++*otherp;
4415 }
Junio C Hamanocd676a52008-02-12 14:26:02 -08004416}
4417
Junio C Hamano6973dca2006-04-21 23:57:45 -07004418static void run_diff(struct diff_filepair *p, struct diff_options *o)
4419{
4420 const char *pgm = external_diff();
Pierre Habouzit663af342007-09-20 00:42:15 +02004421 struct strbuf msg;
Pierre Habouzit663af342007-09-20 00:42:15 +02004422 struct diff_filespec *one = p->one;
4423 struct diff_filespec *two = p->two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004424 const char *name;
4425 const char *other;
Junio C Hamanocd676a52008-02-12 14:26:02 -08004426 const char *attr_path;
Pierre Habouzit663af342007-09-20 00:42:15 +02004427
Stefan Bellerf2d2a5d2017-06-29 17:06:46 -07004428 name = one->path;
4429 other = (strcmp(name, two->path) ? two->path : NULL);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004430 attr_path = name;
4431 if (o->prefix_length)
4432 strip_prefix(o->prefix_length, &name, &other);
4433
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004434 if (!o->flags.allow_external)
Junio C Hamanobd8c1a92012-07-17 22:08:59 -07004435 pgm = NULL;
4436
Junio C Hamanocd676a52008-02-12 14:26:02 -08004437 if (DIFF_PAIR_UNMERGED(p)) {
4438 run_diff_cmd(pgm, name, NULL, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004439 NULL, NULL, NULL, o, p);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004440 return;
4441 }
4442
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004443 diff_fill_oid_info(one, o->repo->index);
4444 diff_fill_oid_info(two, o->repo->index);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004445
Junio C Hamano6973dca2006-04-21 23:57:45 -07004446 if (!pgm &&
4447 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
4448 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
Junio C Hamanob67b9612009-01-26 00:08:24 -08004449 /*
4450 * a filepair that changes between file and symlink
Junio C Hamano6973dca2006-04-21 23:57:45 -07004451 * needs to be split into deletion and creation.
4452 */
4453 struct diff_filespec *null = alloc_filespec(two->path);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004454 run_diff_cmd(NULL, name, other, attr_path,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004455 one, null, &msg,
4456 o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004457 free(null);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004458 strbuf_release(&msg);
4459
Junio C Hamano6973dca2006-04-21 23:57:45 -07004460 null = alloc_filespec(one->path);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004461 run_diff_cmd(NULL, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004462 null, two, &msg, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004463 free(null);
4464 }
4465 else
Junio C Hamanocd676a52008-02-12 14:26:02 -08004466 run_diff_cmd(pgm, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004467 one, two, &msg, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004468
Pierre Habouzit663af342007-09-20 00:42:15 +02004469 strbuf_release(&msg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004470}
4471
4472static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
4473 struct diffstat_t *diffstat)
4474{
4475 const char *name;
4476 const char *other;
4477
4478 if (DIFF_PAIR_UNMERGED(p)) {
4479 /* unmerged */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004480 builtin_diffstat(p->one->path, NULL, NULL, NULL,
4481 diffstat, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004482 return;
4483 }
4484
4485 name = p->one->path;
4486 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
4487
Junio C Hamanocd676a52008-02-12 14:26:02 -08004488 if (o->prefix_length)
4489 strip_prefix(o->prefix_length, &name, &other);
4490
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004491 diff_fill_oid_info(p->one, o->repo->index);
4492 diff_fill_oid_info(p->two, o->repo->index);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004493
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004494 builtin_diffstat(name, other, p->one, p->two,
4495 diffstat, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004496}
4497
Johannes Schindelin88246892006-05-20 23:43:13 +02004498static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
4499{
4500 const char *name;
4501 const char *other;
Junio C Hamanocd676a52008-02-12 14:26:02 -08004502 const char *attr_path;
Johannes Schindelin88246892006-05-20 23:43:13 +02004503
4504 if (DIFF_PAIR_UNMERGED(p)) {
4505 /* unmerged */
4506 return;
4507 }
4508
4509 name = p->one->path;
4510 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004511 attr_path = other ? other : name;
4512
4513 if (o->prefix_length)
4514 strip_prefix(o->prefix_length, &name, &other);
Johannes Schindelin88246892006-05-20 23:43:13 +02004515
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004516 diff_fill_oid_info(p->one, o->repo->index);
4517 diff_fill_oid_info(p->two, o->repo->index);
Johannes Schindelin88246892006-05-20 23:43:13 +02004518
Junio C Hamanocd676a52008-02-12 14:26:02 -08004519 builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
Johannes Schindelin88246892006-05-20 23:43:13 +02004520}
4521
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07004522static void prep_parse_options(struct diff_options *options);
4523
Nguyễn Thái Ngọc Duye6757652018-09-21 17:57:24 +02004524void repo_diff_setup(struct repository *r, struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004525{
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +02004526 memcpy(options, &default_diff_options, sizeof(*options));
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004527
4528 options->file = stdout;
Nguyễn Thái Ngọc Duye6757652018-09-21 17:57:24 +02004529 options->repo = r;
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004530
Stefan Beller7648b792018-08-17 13:43:52 -07004531 options->output_indicators[OUTPUT_INDICATOR_NEW] = '+';
4532 options->output_indicators[OUTPUT_INDICATOR_OLD] = '-';
4533 options->output_indicators[OUTPUT_INDICATOR_CONTEXT] = ' ';
Jack Bates43d19482016-12-06 09:56:14 -07004534 options->abbrev = DEFAULT_ABBREV;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004535 options->line_termination = '\n';
4536 options->break_opt = -1;
4537 options->rename_limit = -1;
Johan Herland712d2c72011-04-29 11:36:20 +02004538 options->dirstat_permille = diff_dirstat_permille_default;
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -04004539 options->context = diff_context_default;
Vegard Nossumc4888672017-01-12 13:21:11 +01004540 options->interhunkcontext = diff_interhunk_context_default;
Junio C Hamanoa17505f2016-10-04 15:26:27 -07004541 options->ws_error_highlight = ws_error_highlight_default;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004542 options->flags.rename_empty = 1;
Laurent Arnoudc28ded82020-05-22 12:46:18 +02004543 options->flags.relative_name = diff_relative;
Stefan Beller15af58c2018-01-04 14:50:42 -08004544 options->objfind = NULL;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004545
Kirill Smelkov72441af2014-04-07 01:46:26 +04004546 /* pathchange left =NULL by default */
Junio C Hamano6973dca2006-04-21 23:57:45 -07004547 options->change = diff_change;
4548 options->add_remove = diff_addremove;
Jeff Kingf1c96262011-08-17 22:03:12 -07004549 options->use_color = diff_use_color_default;
Eric Wongb68ea122006-07-07 04:01:23 -07004550 options->detect_rename = diff_detect_rename_default;
Michal Privoznik07ab4de2013-01-16 08:51:57 +01004551 options->xdl_opts |= diff_algorithm;
Michael Haggerty433860f2016-09-05 11:44:51 +02004552 if (diff_indent_heuristic)
4553 DIFF_XDL_SET(options, INDENT_HEURISTIC);
Johannes Schindelineab9a402007-12-18 19:32:14 +00004554
Samuel Bronson6d8940b2013-12-18 19:08:12 -05004555 options->orderfile = diff_order_file_cfg;
4556
Sangeeta Jain8ef93122020-11-10 14:09:00 +05304557 if (!options->flags.ignore_submodule_set)
4558 options->flags.ignore_untracked_in_submodules = 1;
4559
Eli Collinsf89504d2010-05-02 19:03:41 -07004560 if (diff_no_prefix) {
4561 options->a_prefix = options->b_prefix = "";
4562 } else if (!diff_mnemonic_prefix) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07004563 options->a_prefix = "a/";
4564 options->b_prefix = "b/";
4565 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07004566
4567 options->color_moved = diff_color_moved_default;
Stefan Beller626c0b52018-07-18 12:31:56 -07004568 options->color_moved_ws_handling = diff_color_moved_ws_default;
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07004569
4570 prep_parse_options(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004571}
4572
Thomas Rast28452652012-08-03 14:16:24 +02004573void diff_setup_done(struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004574{
Stefan Beller4d8c51a2018-01-04 14:50:44 -08004575 unsigned check_mask = DIFF_FORMAT_NAME |
4576 DIFF_FORMAT_NAME_STATUS |
4577 DIFF_FORMAT_CHECKDIFF |
4578 DIFF_FORMAT_NO_OUTPUT;
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004579 /*
4580 * This must be signed because we're comparing against a potentially
4581 * negative value.
4582 */
4583 const int hexsz = the_hash_algo->hexsz;
Timo Hirvonend7de00f2006-06-24 20:26:49 +03004584
Junio C Hamano6c374002013-05-10 17:10:11 +02004585 if (options->set_default)
4586 options->set_default(options);
4587
Stefan Beller4d8c51a2018-01-04 14:50:44 -08004588 if (HAS_MULTI_BITS(options->output_format & check_mask))
Jean-Noël Avila246cac82022-01-05 20:02:24 +00004589 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
4590 "--name-only", "--name-status", "--check", "-s");
Timo Hirvonend7de00f2006-06-24 20:26:49 +03004591
Stefan Beller5e505252018-01-04 14:50:43 -08004592 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
Jean-Noël Avila246cac82022-01-05 20:02:24 +00004593 die(_("options '%s', '%s', and '%s' cannot be used together"),
4594 "-G", "-S", "--find-object");
Stefan Beller5e505252018-01-04 14:50:43 -08004595
Ævar Arnfjörð Bjarmason188e9e22021-04-12 19:15:14 +02004596 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_G_REGEX_MASK))
Jean-Noël Avila246cac82022-01-05 20:02:24 +00004597 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s'"),
4598 "-G", "--pickaxe-regex", "--pickaxe-regex", "-S");
Ævar Arnfjörð Bjarmason188e9e22021-04-12 19:15:14 +02004599
Ævar Arnfjörð Bjarmasond26ec882021-04-12 19:15:15 +02004600 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK))
Jean-Noël Avila246cac82022-01-05 20:02:24 +00004601 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s' and '%s'"),
4602 "--pickaxe-all", "--find-object", "--pickaxe-all", "-G", "-S");
Ævar Arnfjörð Bjarmasond26ec882021-04-12 19:15:15 +02004603
Junio C Hamanof2451942009-05-22 12:45:29 -07004604 /*
4605 * Most of the time we can say "there are changes"
4606 * only by checking if there are changed paths, but
4607 * --ignore-whitespace* options force us to look
Jim Meyering97bf2a02009-08-30 22:27:02 +02004608 * inside contents.
Junio C Hamanof2451942009-05-22 12:45:29 -07004609 */
4610
Junio C Hamano50f04392020-12-16 17:27:13 -08004611 if ((options->xdl_opts & XDF_WHITESPACE_FLAGS) ||
4612 options->ignore_regex_nr)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004613 options->flags.diff_from_contents = 1;
Junio C Hamanof2451942009-05-22 12:45:29 -07004614 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004615 options->flags.diff_from_contents = 0;
Junio C Hamanof2451942009-05-22 12:45:29 -07004616
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004617 if (options->flags.find_copies_harder)
Junio C Hamano03b9d562006-08-09 13:17:19 -07004618 options->detect_rename = DIFF_DETECT_COPY;
4619
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004620 if (!options->flags.relative_name)
Junio C Hamanocd676a52008-02-12 14:26:02 -08004621 options->prefix = NULL;
4622 if (options->prefix)
4623 options->prefix_length = strlen(options->prefix);
4624 else
4625 options->prefix_length = 0;
4626
Timo Hirvonenc6744342006-06-24 20:21:53 +03004627 if (options->output_format & (DIFF_FORMAT_NAME |
4628 DIFF_FORMAT_NAME_STATUS |
4629 DIFF_FORMAT_CHECKDIFF |
4630 DIFF_FORMAT_NO_OUTPUT))
4631 options->output_format &= ~(DIFF_FORMAT_RAW |
Junio C Hamano74e2abe2006-10-12 03:01:00 -07004632 DIFF_FORMAT_NUMSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004633 DIFF_FORMAT_DIFFSTAT |
Nicolas Pitreebd124c2006-12-14 23:15:44 -05004634 DIFF_FORMAT_SHORTSTAT |
Linus Torvalds7df7c012008-02-12 13:26:31 -08004635 DIFF_FORMAT_DIRSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004636 DIFF_FORMAT_SUMMARY |
4637 DIFF_FORMAT_PATCH);
4638
Junio C Hamano6973dca2006-04-21 23:57:45 -07004639 /*
4640 * These cases always need recursive; we do not drop caller-supplied
4641 * recursive bits for other formats here.
4642 */
Timo Hirvonenc6744342006-06-24 20:21:53 +03004643 if (options->output_format & (DIFF_FORMAT_PATCH |
Junio C Hamano74e2abe2006-10-12 03:01:00 -07004644 DIFF_FORMAT_NUMSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004645 DIFF_FORMAT_DIFFSTAT |
Nicolas Pitreebd124c2006-12-14 23:15:44 -05004646 DIFF_FORMAT_SHORTSTAT |
Linus Torvalds7df7c012008-02-12 13:26:31 -08004647 DIFF_FORMAT_DIRSTAT |
Johannes Schindelind7014dc2006-10-03 23:09:56 +02004648 DIFF_FORMAT_SUMMARY |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004649 DIFF_FORMAT_CHECKDIFF))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004650 options->flags.recursive = 1;
Junio C Hamano5e363542006-05-22 00:31:02 -07004651 /*
Junio C Hamano3969cf72006-06-27 15:08:19 -07004652 * Also pickaxe would not work very well if you do not say recursive
Junio C Hamano5e363542006-05-22 00:31:02 -07004653 */
Stefan Bellercf630512018-01-04 14:50:41 -08004654 if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004655 options->flags.recursive = 1;
Jens Lehmannae6d5c12010-03-11 22:50:25 +01004656 /*
4657 * When patches are generated, submodules diffed against the work tree
4658 * must be checked for dirtiness too so it can be shown in the output
4659 */
4660 if (options->output_format & DIFF_FORMAT_PATCH)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004661 options->flags.dirty_submodules = 1;
Junio C Hamano5e363542006-05-22 00:31:02 -07004662
Junio C Hamano6973dca2006-04-21 23:57:45 -07004663 if (options->detect_rename && options->rename_limit < 0)
4664 options->rename_limit = diff_rename_limit_default;
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004665 if (hexsz < options->abbrev)
4666 options->abbrev = hexsz; /* full */
Junio C Hamano6973dca2006-04-21 23:57:45 -07004667
Junio C Hamano68aacb22007-03-14 11:12:13 -07004668 /*
4669 * It does not make sense to show the first hit we happened
4670 * to have found. It does not make sense not to return with
4671 * exit code in such a case either.
4672 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004673 if (options->flags.quick) {
Junio C Hamano68aacb22007-03-14 11:12:13 -07004674 options->output_format = DIFF_FORMAT_NO_OUTPUT;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004675 options->flags.exit_with_status = 1;
Junio C Hamano68aacb22007-03-14 11:12:13 -07004676 }
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004677
Jeff King0ea7d5b2013-12-16 15:02:21 -05004678 options->diff_path_counter = 0;
Junio C Hamanob0e2c992014-06-16 10:07:09 -07004679
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004680 if (options->flags.follow_renames && options->pathspec.nr != 1)
Jeff Kingdd63f162014-05-20 02:49:20 -04004681 die(_("--follow requires exactly one pathspec"));
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07004682
4683 if (!options->use_color || external_diff())
4684 options->color_moved = 0;
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07004685
4686 FREE_AND_NULL(options->parseopts);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004687}
4688
Matthieu Moydea007f2010-08-05 10:22:52 +02004689int parse_long_opt(const char *opt, const char **argv,
4690 const char **optarg)
4691{
4692 const char *arg = argv[0];
Jeff King95b567c2014-06-18 15:48:29 -04004693 if (!skip_prefix(arg, "--", &arg))
Matthieu Moydea007f2010-08-05 10:22:52 +02004694 return 0;
Jeff King95b567c2014-06-18 15:48:29 -04004695 if (!skip_prefix(arg, opt, &arg))
Matthieu Moydea007f2010-08-05 10:22:52 +02004696 return 0;
Nicolas Vigierb0d12fc2013-10-31 12:08:28 +01004697 if (*arg == '=') { /* stuck form: --option=value */
Matthieu Moydea007f2010-08-05 10:22:52 +02004698 *optarg = arg + 1;
4699 return 1;
4700 }
4701 if (*arg != '\0')
4702 return 0;
4703 /* separate form: --option value */
4704 if (!argv[1])
4705 die("Option '--%s' requires a value", opt);
4706 *optarg = argv[1];
4707 return 2;
4708}
4709
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004710static int diff_opt_stat(const struct option *opt, const char *value, int unset)
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004711{
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004712 struct diff_options *options = opt->value;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004713 int width = options->stat_width;
4714 int name_width = options->stat_name_width;
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004715 int graph_width = options->stat_graph_width;
Michael J Gruber808e1db2011-05-27 14:36:41 +02004716 int count = options->stat_count;
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004717 char *end;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004718
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004719 BUG_ON_OPT_NEG(unset);
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004720
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004721 if (!strcmp(opt->long_name, "stat")) {
4722 if (value) {
4723 width = strtoul(value, &end, 10);
4724 if (*end == ',')
4725 name_width = strtoul(end+1, &end, 10);
4726 if (*end == ',')
4727 count = strtoul(end+1, &end, 10);
4728 if (*end)
4729 return error(_("invalid --stat value: %s"), value);
Matthieu Moy1e572082010-08-05 10:22:54 +02004730 }
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004731 } else if (!strcmp(opt->long_name, "stat-width")) {
4732 width = strtoul(value, &end, 10);
4733 if (*end)
4734 return error(_("%s expects a numerical value"),
4735 opt->long_name);
4736 } else if (!strcmp(opt->long_name, "stat-name-width")) {
4737 name_width = strtoul(value, &end, 10);
4738 if (*end)
4739 return error(_("%s expects a numerical value"),
4740 opt->long_name);
4741 } else if (!strcmp(opt->long_name, "stat-graph-width")) {
4742 graph_width = strtoul(value, &end, 10);
4743 if (*end)
4744 return error(_("%s expects a numerical value"),
4745 opt->long_name);
4746 } else if (!strcmp(opt->long_name, "stat-count")) {
4747 count = strtoul(value, &end, 10);
4748 if (*end)
4749 return error(_("%s expects a numerical value"),
4750 opt->long_name);
4751 } else
4752 BUG("%s should not get here", opt->long_name);
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004753
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004754 options->output_format |= DIFF_FORMAT_DIFFSTAT;
4755 options->stat_name_width = name_width;
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004756 options->stat_graph_width = graph_width;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004757 options->stat_width = width;
Michael J Gruber808e1db2011-05-27 14:36:41 +02004758 options->stat_count = count;
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004759 return 0;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004760}
4761
Johan Herland333f3fb2011-04-29 11:36:18 +02004762static int parse_dirstat_opt(struct diff_options *options, const char *params)
4763{
Johan Herland51670fc2011-04-29 11:36:22 +02004764 struct strbuf errmsg = STRBUF_INIT;
4765 if (parse_dirstat_params(options, params, &errmsg))
Johan Herland7478ac52011-04-29 11:36:23 +02004766 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
Johan Herland51670fc2011-04-29 11:36:22 +02004767 errmsg.buf);
4768 strbuf_release(&errmsg);
Johan Herland333f3fb2011-04-29 11:36:18 +02004769 /*
4770 * The caller knows a dirstat-related option is given from the command
4771 * line; allow it to say "return this_function();"
4772 */
4773 options->output_format |= DIFF_FORMAT_DIRSTAT;
4774 return 1;
4775}
4776
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004777static const char diff_status_letters[] = {
4778 DIFF_STATUS_ADDED,
4779 DIFF_STATUS_COPIED,
4780 DIFF_STATUS_DELETED,
4781 DIFF_STATUS_MODIFIED,
4782 DIFF_STATUS_RENAMED,
4783 DIFF_STATUS_TYPE_CHANGED,
4784 DIFF_STATUS_UNKNOWN,
4785 DIFF_STATUS_UNMERGED,
4786 DIFF_STATUS_FILTER_AON,
4787 DIFF_STATUS_FILTER_BROKEN,
4788 '\0',
4789};
4790
4791static unsigned int filter_bit['Z' + 1];
4792
4793static void prepare_filter_bits(void)
4794{
4795 int i;
4796
4797 if (!filter_bit[DIFF_STATUS_ADDED]) {
4798 for (i = 0; diff_status_letters[i]; i++)
4799 filter_bit[(int) diff_status_letters[i]] = (1 << i);
4800 }
4801}
4802
4803static unsigned filter_bit_tst(char status, const struct diff_options *opt)
4804{
4805 return opt->filter & filter_bit[(int) status];
4806}
4807
Nguyễn Thái Ngọc Duycdb53302019-03-24 15:20:14 +07004808unsigned diff_filter_bit(char status)
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004809{
Nguyễn Thái Ngọc Duycdb53302019-03-24 15:20:14 +07004810 prepare_filter_bits();
4811 return filter_bit[(int) status];
4812}
4813
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004814static int diff_opt_diff_filter(const struct option *option,
4815 const char *optarg, int unset)
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004816{
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004817 struct diff_options *opt = option->value;
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004818 int i, optch;
4819
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004820 BUG_ON_OPT_NEG(unset);
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004821 prepare_filter_bits();
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004822
4823 /*
4824 * If there is a negation e.g. 'd' in the input, and we haven't
4825 * initialized the filter field with another --diff-filter, start
4826 * from full set of bits, except for AON.
4827 */
4828 if (!opt->filter) {
4829 for (i = 0; (optch = optarg[i]) != '\0'; i++) {
4830 if (optch < 'a' || 'z' < optch)
4831 continue;
4832 opt->filter = (1 << (ARRAY_SIZE(diff_status_letters) - 1)) - 1;
4833 opt->filter &= ~filter_bit[DIFF_STATUS_FILTER_AON];
4834 break;
4835 }
4836 }
4837
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004838 for (i = 0; (optch = optarg[i]) != '\0'; i++) {
4839 unsigned int bit;
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004840 int negate;
4841
4842 if ('a' <= optch && optch <= 'z') {
4843 negate = 1;
4844 optch = toupper(optch);
4845 } else {
4846 negate = 0;
4847 }
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004848
4849 bit = (0 <= optch && optch <= 'Z') ? filter_bit[optch] : 0;
4850 if (!bit)
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004851 return error(_("unknown change class '%c' in --diff-filter=%s"),
4852 optarg[i], optarg);
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004853 if (negate)
4854 opt->filter &= ~bit;
4855 else
4856 opt->filter |= bit;
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004857 }
4858 return 0;
4859}
4860
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +01004861static void enable_patch_output(int *fmt)
4862{
Matthieu Moy71482d32013-07-16 10:05:37 +02004863 *fmt &= ~DIFF_FORMAT_NO_OUTPUT;
4864 *fmt |= DIFF_FORMAT_PATCH;
4865}
4866
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07004867static int diff_opt_ws_error_highlight(const struct option *option,
4868 const char *arg, int unset)
Junio C Hamanob8767f72015-05-26 10:11:28 -07004869{
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07004870 struct diff_options *opt = option->value;
Junio C Hamano077965f2016-10-04 15:03:33 -07004871 int val = parse_ws_error_highlight(arg);
Junio C Hamanob8767f72015-05-26 10:11:28 -07004872
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07004873 BUG_ON_OPT_NEG(unset);
4874 if (val < 0)
4875 return error(_("unknown value after ws-error-highlight=%.*s"),
4876 -1 - val, arg);
Junio C Hamanob8767f72015-05-26 10:11:28 -07004877 opt->ws_error_highlight = val;
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07004878 return 0;
Junio C Hamanob8767f72015-05-26 10:11:28 -07004879}
4880
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004881static int diff_opt_find_object(const struct option *option,
4882 const char *arg, int unset)
Stefan Beller15af58c2018-01-04 14:50:42 -08004883{
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004884 struct diff_options *opt = option->value;
Stefan Beller15af58c2018-01-04 14:50:42 -08004885 struct object_id oid;
4886
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004887 BUG_ON_OPT_NEG(unset);
Stefan Beller15af58c2018-01-04 14:50:42 -08004888 if (get_oid(arg, &oid))
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004889 return error(_("unable to resolve '%s'"), arg);
Stefan Beller15af58c2018-01-04 14:50:42 -08004890
4891 if (!opt->objfind)
René Scharfeca56dad2021-03-13 17:17:22 +01004892 CALLOC_ARRAY(opt->objfind, 1);
Stefan Beller15af58c2018-01-04 14:50:42 -08004893
4894 opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND;
4895 opt->flags.recursive = 1;
4896 opt->flags.tree_in_recursive = 1;
4897 oidset_insert(opt->objfind, &oid);
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07004898 return 0;
Stefan Beller15af58c2018-01-04 14:50:42 -08004899}
4900
Nguyễn Thái Ngọc Duydf84a432019-03-05 19:30:11 +07004901static int diff_opt_anchored(const struct option *opt,
4902 const char *arg, int unset)
4903{
4904 struct diff_options *options = opt->value;
4905
4906 BUG_ON_OPT_NEG(unset);
4907 options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
4908 ALLOC_GROW(options->anchors, options->anchors_nr + 1,
4909 options->anchors_alloc);
4910 options->anchors[options->anchors_nr++] = xstrdup(arg);
4911 return 0;
4912}
4913
Nguyễn Thái Ngọc Duy6d9af6f2019-03-05 19:30:12 +07004914static int diff_opt_binary(const struct option *opt,
4915 const char *arg, int unset)
4916{
4917 struct diff_options *options = opt->value;
4918
4919 BUG_ON_OPT_NEG(unset);
4920 BUG_ON_OPT_ARG(arg);
4921 enable_patch_output(&options->output_format);
4922 options->flags.binary = 1;
4923 return 0;
4924}
4925
Nguyễn Thái Ngọc Duyced4e172019-02-21 18:16:13 +07004926static int diff_opt_break_rewrites(const struct option *opt,
4927 const char *arg, int unset)
4928{
4929 int *break_opt = opt->value;
4930 int opt1, opt2;
4931
4932 BUG_ON_OPT_NEG(unset);
4933 if (!arg)
4934 arg = "";
4935 opt1 = parse_rename_score(&arg);
4936 if (*arg == 0)
4937 opt2 = 0;
4938 else if (*arg != '/')
4939 return error(_("%s expects <n>/<m> form"), opt->long_name);
4940 else {
4941 arg++;
4942 opt2 = parse_rename_score(&arg);
4943 }
4944 if (*arg != 0)
4945 return error(_("%s expects <n>/<m> form"), opt->long_name);
4946 *break_opt = opt1 | (opt2 << 16);
4947 return 0;
4948}
4949
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07004950static int diff_opt_char(const struct option *opt,
4951 const char *arg, int unset)
4952{
4953 char *value = opt->value;
4954
4955 BUG_ON_OPT_NEG(unset);
4956 if (arg[1])
4957 return error(_("%s expects a character, got '%s'"),
4958 opt->long_name, arg);
4959 *value = arg[0];
4960 return 0;
4961}
4962
Nguyễn Thái Ngọc Duy59311a92019-03-24 15:20:09 +07004963static int diff_opt_color_moved(const struct option *opt,
4964 const char *arg, int unset)
4965{
4966 struct diff_options *options = opt->value;
4967
4968 if (unset) {
4969 options->color_moved = COLOR_MOVED_NO;
4970 } else if (!arg) {
4971 if (diff_color_moved_default)
4972 options->color_moved = diff_color_moved_default;
4973 if (options->color_moved == COLOR_MOVED_NO)
4974 options->color_moved = COLOR_MOVED_DEFAULT;
4975 } else {
4976 int cm = parse_color_moved(arg);
4977 if (cm < 0)
4978 return error(_("bad --color-moved argument: %s"), arg);
4979 options->color_moved = cm;
4980 }
4981 return 0;
4982}
4983
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07004984static int diff_opt_color_moved_ws(const struct option *opt,
4985 const char *arg, int unset)
4986{
4987 struct diff_options *options = opt->value;
4988 unsigned cm;
4989
Nguyễn Thái Ngọc Duybb987292019-03-24 15:20:11 +07004990 if (unset) {
4991 options->color_moved_ws_handling = 0;
4992 return 0;
4993 }
4994
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07004995 cm = parse_color_moved_ws(arg);
4996 if (cm & COLOR_MOVED_WS_ERROR)
4997 return error(_("invalid mode '%s' in --color-moved-ws"), arg);
4998 options->color_moved_ws_handling = cm;
4999 return 0;
5000}
5001
Nguyễn Thái Ngọc Duy212db692019-03-05 19:30:20 +07005002static int diff_opt_color_words(const struct option *opt,
5003 const char *arg, int unset)
5004{
5005 struct diff_options *options = opt->value;
5006
5007 BUG_ON_OPT_NEG(unset);
5008 options->use_color = 1;
5009 options->word_diff = DIFF_WORDS_COLOR;
5010 options->word_regex = arg;
5011 return 0;
5012}
5013
Nguyễn Thái Ngọc Duy7d7942b2019-02-21 18:16:11 +07005014static int diff_opt_compact_summary(const struct option *opt,
5015 const char *arg, int unset)
5016{
5017 struct diff_options *options = opt->value;
5018
5019 BUG_ON_OPT_ARG(arg);
5020 if (unset) {
5021 options->flags.stat_with_summary = 0;
5022 } else {
5023 options->flags.stat_with_summary = 1;
5024 options->output_format |= DIFF_FORMAT_DIFFSTAT;
5025 }
5026 return 0;
5027}
5028
Nguyễn Thái Ngọc Duy10f35b12019-03-05 19:30:10 +07005029static int diff_opt_diff_algorithm(const struct option *opt,
5030 const char *arg, int unset)
5031{
5032 struct diff_options *options = opt->value;
5033 long value = parse_algorithm_value(arg);
5034
5035 BUG_ON_OPT_NEG(unset);
5036 if (value < 0)
5037 return error(_("option diff-algorithm accepts \"myers\", "
5038 "\"minimal\", \"patience\" and \"histogram\""));
5039
5040 /* clear out previous settings */
5041 DIFF_XDL_CLR(options, NEED_MINIMAL);
5042 options->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
5043 options->xdl_opts |= value;
5044 return 0;
5045}
5046
Nguyễn Thái Ngọc Duy4ce7aab2019-02-21 18:16:03 +07005047static int diff_opt_dirstat(const struct option *opt,
5048 const char *arg, int unset)
5049{
5050 struct diff_options *options = opt->value;
5051
5052 BUG_ON_OPT_NEG(unset);
5053 if (!strcmp(opt->long_name, "cumulative")) {
5054 if (arg)
5055 BUG("how come --cumulative take a value?");
5056 arg = "cumulative";
5057 } else if (!strcmp(opt->long_name, "dirstat-by-file"))
5058 parse_dirstat_opt(options, "files");
5059 parse_dirstat_opt(options, arg ? arg : "");
5060 return 0;
5061}
5062
Nguyễn Thái Ngọc Duy7f648502019-02-21 18:16:16 +07005063static int diff_opt_find_copies(const struct option *opt,
5064 const char *arg, int unset)
5065{
5066 struct diff_options *options = opt->value;
5067
5068 BUG_ON_OPT_NEG(unset);
5069 if (!arg)
5070 arg = "";
5071 options->rename_score = parse_rename_score(&arg);
5072 if (*arg != 0)
5073 return error(_("invalid argument to %s"), opt->long_name);
5074
5075 if (options->detect_rename == DIFF_DETECT_COPY)
5076 options->flags.find_copies_harder = 1;
5077 else
5078 options->detect_rename = DIFF_DETECT_COPY;
5079
5080 return 0;
5081}
5082
Nguyễn Thái Ngọc Duyf4763082019-02-21 18:16:14 +07005083static int diff_opt_find_renames(const struct option *opt,
5084 const char *arg, int unset)
5085{
5086 struct diff_options *options = opt->value;
5087
5088 BUG_ON_OPT_NEG(unset);
5089 if (!arg)
5090 arg = "";
5091 options->rename_score = parse_rename_score(&arg);
5092 if (*arg != 0)
5093 return error(_("invalid argument to %s"), opt->long_name);
5094
5095 options->detect_rename = DIFF_DETECT_RENAME;
5096 return 0;
5097}
5098
Nguyễn Thái Ngọc Duy1e9250b2019-03-05 19:30:16 +07005099static int diff_opt_follow(const struct option *opt,
5100 const char *arg, int unset)
5101{
5102 struct diff_options *options = opt->value;
5103
5104 BUG_ON_OPT_ARG(arg);
5105 if (unset) {
5106 options->flags.follow_renames = 0;
5107 options->flags.default_follow_renames = 0;
5108 } else {
5109 options->flags.follow_renames = 1;
5110 }
5111 return 0;
5112}
5113
Nguyễn Thái Ngọc Duyb680ee12019-03-05 19:30:25 +07005114static int diff_opt_ignore_submodules(const struct option *opt,
5115 const char *arg, int unset)
5116{
5117 struct diff_options *options = opt->value;
5118
5119 BUG_ON_OPT_NEG(unset);
5120 if (!arg)
5121 arg = "all";
5122 options->flags.override_submodule_config = 1;
5123 handle_ignore_submodules_arg(options, arg);
5124 return 0;
5125}
5126
Nguyễn Thái Ngọc Duy2f81cf92019-03-24 15:20:06 +07005127static int diff_opt_line_prefix(const struct option *opt,
5128 const char *optarg, int unset)
5129{
5130 struct diff_options *options = opt->value;
5131
5132 BUG_ON_OPT_NEG(unset);
5133 options->line_prefix = optarg;
5134 options->line_prefix_length = strlen(options->line_prefix);
5135 graph_setup_line_prefix(options);
5136 return 0;
5137}
5138
Nguyễn Thái Ngọc Duy11c659d2019-03-24 15:20:07 +07005139static int diff_opt_no_prefix(const struct option *opt,
5140 const char *optarg, int unset)
5141{
5142 struct diff_options *options = opt->value;
5143
5144 BUG_ON_OPT_NEG(unset);
5145 BUG_ON_OPT_ARG(optarg);
5146 options->a_prefix = "";
5147 options->b_prefix = "";
5148 return 0;
5149}
5150
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005151static enum parse_opt_result diff_opt_output(struct parse_opt_ctx_t *ctx,
5152 const struct option *opt,
5153 const char *arg, int unset)
5154{
5155 struct diff_options *options = opt->value;
5156 char *path;
5157
5158 BUG_ON_OPT_NEG(unset);
5159 path = prefix_filename(ctx->prefix, arg);
5160 options->file = xfopen(path, "w");
5161 options->close_file = 1;
5162 if (options->use_color != GIT_COLOR_ALWAYS)
5163 options->use_color = GIT_COLOR_NEVER;
5164 free(path);
5165 return 0;
5166}
5167
Nguyễn Thái Ngọc Duy31fd6402019-03-05 19:30:08 +07005168static int diff_opt_patience(const struct option *opt,
5169 const char *arg, int unset)
5170{
5171 struct diff_options *options = opt->value;
5172 int i;
5173
5174 BUG_ON_OPT_NEG(unset);
5175 BUG_ON_OPT_ARG(arg);
5176 options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
5177 /*
5178 * Both --patience and --anchored use PATIENCE_DIFF
5179 * internally, so remove any anchors previously
5180 * specified.
5181 */
5182 for (i = 0; i < options->anchors_nr; i++)
5183 free(options->anchors[i]);
5184 options->anchors_nr = 0;
5185 return 0;
5186}
5187
Michał Kępień296d4a92020-10-20 08:48:09 +02005188static int diff_opt_ignore_regex(const struct option *opt,
5189 const char *arg, int unset)
5190{
5191 struct diff_options *options = opt->value;
5192 regex_t *regex;
5193
5194 BUG_ON_OPT_NEG(unset);
5195 regex = xmalloc(sizeof(*regex));
5196 if (regcomp(regex, arg, REG_EXTENDED | REG_NEWLINE))
5197 return error(_("invalid regex given to -I: '%s'"), arg);
5198 ALLOC_GROW(options->ignore_regex, options->ignore_regex_nr + 1,
5199 options->ignore_regex_alloc);
5200 options->ignore_regex[options->ignore_regex_nr++] = regex;
5201 return 0;
5202}
5203
Nguyễn Thái Ngọc Duya41cfb32019-03-24 15:19:59 +07005204static int diff_opt_pickaxe_regex(const struct option *opt,
5205 const char *arg, int unset)
5206{
5207 struct diff_options *options = opt->value;
5208
5209 BUG_ON_OPT_NEG(unset);
5210 options->pickaxe = arg;
5211 options->pickaxe_opts |= DIFF_PICKAXE_KIND_G;
5212 return 0;
5213}
5214
5215static int diff_opt_pickaxe_string(const struct option *opt,
5216 const char *arg, int unset)
5217{
5218 struct diff_options *options = opt->value;
5219
5220 BUG_ON_OPT_NEG(unset);
5221 options->pickaxe = arg;
5222 options->pickaxe_opts |= DIFF_PICKAXE_KIND_S;
5223 return 0;
5224}
5225
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005226static int diff_opt_relative(const struct option *opt,
5227 const char *arg, int unset)
5228{
5229 struct diff_options *options = opt->value;
5230
Laurent Arnoudc28ded82020-05-22 12:46:18 +02005231 options->flags.relative_name = !unset;
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005232 if (arg)
5233 options->prefix = arg;
5234 return 0;
5235}
5236
Nguyễn Thái Ngọc Duy125dcea2019-03-05 19:30:26 +07005237static int diff_opt_submodule(const struct option *opt,
5238 const char *arg, int unset)
5239{
5240 struct diff_options *options = opt->value;
5241
5242 BUG_ON_OPT_NEG(unset);
5243 if (!arg)
5244 arg = "log";
5245 if (parse_submodule_params(options, arg))
5246 return error(_("failed to parse --submodule option parameter: '%s'"),
5247 arg);
5248 return 0;
5249}
5250
Nguyễn Thái Ngọc Duy8ab76972019-03-05 19:30:24 +07005251static int diff_opt_textconv(const struct option *opt,
5252 const char *arg, int unset)
5253{
5254 struct diff_options *options = opt->value;
5255
5256 BUG_ON_OPT_ARG(arg);
5257 if (unset) {
5258 options->flags.allow_textconv = 0;
5259 } else {
5260 options->flags.allow_textconv = 1;
5261 options->flags.textconv_set_via_cmdline = 1;
5262 }
5263 return 0;
5264}
5265
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 07:35:33 +07005266static int diff_opt_unified(const struct option *opt,
5267 const char *arg, int unset)
5268{
5269 struct diff_options *options = opt->value;
5270 char *s;
5271
5272 BUG_ON_OPT_NEG(unset);
5273
Nguyễn Thái Ngọc Duy8ef05192019-05-29 16:11:15 +07005274 if (arg) {
5275 options->context = strtol(arg, &s, 10);
5276 if (*s)
5277 return error(_("%s expects a numerical value"), "--unified");
5278 }
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 07:35:33 +07005279 enable_patch_output(&options->output_format);
5280
5281 return 0;
5282}
5283
Nguyễn Thái Ngọc Duye9fb39b2019-03-05 19:30:18 +07005284static int diff_opt_word_diff(const struct option *opt,
5285 const char *arg, int unset)
5286{
5287 struct diff_options *options = opt->value;
5288
5289 BUG_ON_OPT_NEG(unset);
5290 if (arg) {
5291 if (!strcmp(arg, "plain"))
5292 options->word_diff = DIFF_WORDS_PLAIN;
5293 else if (!strcmp(arg, "color")) {
5294 options->use_color = 1;
5295 options->word_diff = DIFF_WORDS_COLOR;
5296 }
5297 else if (!strcmp(arg, "porcelain"))
5298 options->word_diff = DIFF_WORDS_PORCELAIN;
5299 else if (!strcmp(arg, "none"))
5300 options->word_diff = DIFF_WORDS_NONE;
5301 else
5302 return error(_("bad --word-diff argument: %s"), arg);
5303 } else {
5304 if (options->word_diff == DIFF_WORDS_NONE)
5305 options->word_diff = DIFF_WORDS_PLAIN;
5306 }
5307 return 0;
5308}
5309
Nguyễn Thái Ngọc Duy797df112019-03-05 19:30:19 +07005310static int diff_opt_word_diff_regex(const struct option *opt,
5311 const char *arg, int unset)
5312{
5313 struct diff_options *options = opt->value;
5314
5315 BUG_ON_OPT_NEG(unset);
5316 if (options->word_diff == DIFF_WORDS_NONE)
5317 options->word_diff = DIFF_WORDS_PLAIN;
5318 options->word_regex = arg;
5319 return 0;
5320}
5321
Junio C Hamano1eb41362021-02-11 11:57:50 -08005322static int diff_opt_rotate_to(const struct option *opt, const char *arg, int unset)
5323{
5324 struct diff_options *options = opt->value;
5325
5326 BUG_ON_OPT_NEG(unset);
5327 if (!strcmp(opt->long_name, "skip-to"))
5328 options->skip_instead_of_rotate = 1;
5329 else
5330 options->skip_instead_of_rotate = 0;
5331 options->rotate_to = arg;
5332 return 0;
5333}
5334
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005335static void prep_parse_options(struct diff_options *options)
5336{
5337 struct option parseopts[] = {
Nguyễn Thái Ngọc Duycc013c22019-01-27 07:35:32 +07005338 OPT_GROUP(N_("Diff output format options")),
5339 OPT_BITOP('p', "patch", &options->output_format,
5340 N_("generate patch"),
5341 DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duye01df7a2019-02-21 18:16:09 +07005342 OPT_BIT_F('s', "no-patch", &options->output_format,
5343 N_("suppress diff output"),
5344 DIFF_FORMAT_NO_OUTPUT, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duycc013c22019-01-27 07:35:32 +07005345 OPT_BITOP('u', NULL, &options->output_format,
5346 N_("generate patch"),
5347 DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 07:35:33 +07005348 OPT_CALLBACK_F('U', "unified", options, N_("<n>"),
5349 N_("generate diffs with <n> lines context"),
Junio C Hamano20aa7c52019-05-30 10:50:44 -07005350 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_unified),
Nguyễn Thái Ngọc Duy7fd9a1b2019-01-27 07:35:34 +07005351 OPT_BOOL('W', "function-context", &options->flags.funccontext,
5352 N_("generate diffs with <n> lines context")),
Nguyễn Thái Ngọc Duyed881482019-01-27 07:35:35 +07005353 OPT_BIT_F(0, "raw", &options->output_format,
5354 N_("generate the diff in raw format"),
5355 DIFF_FORMAT_RAW, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyc659f302019-02-16 18:36:35 +07005356 OPT_BITOP(0, "patch-with-raw", &options->output_format,
5357 N_("synonym for '-p --raw'"),
5358 DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW,
5359 DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duye550f582019-02-21 18:16:06 +07005360 OPT_BITOP(0, "patch-with-stat", &options->output_format,
5361 N_("synonym for '-p --stat'"),
5362 DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT,
5363 DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duye5673622019-02-16 18:36:36 +07005364 OPT_BIT_F(0, "numstat", &options->output_format,
5365 N_("machine friendly --stat"),
5366 DIFF_FORMAT_NUMSTAT, PARSE_OPT_NONEG),
5367 OPT_BIT_F(0, "shortstat", &options->output_format,
5368 N_("output only the last line of --stat"),
5369 DIFF_FORMAT_SHORTSTAT, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy4ce7aab2019-02-21 18:16:03 +07005370 OPT_CALLBACK_F('X', "dirstat", options, N_("<param1,param2>..."),
5371 N_("output the distribution of relative amount of changes for each sub-directory"),
5372 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5373 diff_opt_dirstat),
5374 OPT_CALLBACK_F(0, "cumulative", options, NULL,
5375 N_("synonym for --dirstat=cumulative"),
5376 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
5377 diff_opt_dirstat),
5378 OPT_CALLBACK_F(0, "dirstat-by-file", options, N_("<param1,param2>..."),
5379 N_("synonym for --dirstat=files,param1,param2..."),
5380 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5381 diff_opt_dirstat),
Nguyễn Thái Ngọc Duyfc6af3e2019-02-21 18:16:04 +07005382 OPT_BIT_F(0, "check", &options->output_format,
5383 N_("warn if changes introduce conflict markers or whitespace errors"),
5384 DIFF_FORMAT_CHECKDIFF, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy70a30412019-02-21 18:16:05 +07005385 OPT_BIT_F(0, "summary", &options->output_format,
5386 N_("condensed summary such as creations, renames and mode changes"),
5387 DIFF_FORMAT_SUMMARY, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy0e840e22019-02-21 18:16:07 +07005388 OPT_BIT_F(0, "name-only", &options->output_format,
5389 N_("show only names of changed files"),
5390 DIFF_FORMAT_NAME, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duya2387472019-02-21 18:16:08 +07005391 OPT_BIT_F(0, "name-status", &options->output_format,
5392 N_("show only names and status of changed files"),
5393 DIFF_FORMAT_NAME_STATUS, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07005394 OPT_CALLBACK_F(0, "stat", options, N_("<width>[,<name-width>[,<count>]]"),
5395 N_("generate diffstat"),
5396 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_stat),
5397 OPT_CALLBACK_F(0, "stat-width", options, N_("<width>"),
5398 N_("generate diffstat with a given width"),
5399 PARSE_OPT_NONEG, diff_opt_stat),
5400 OPT_CALLBACK_F(0, "stat-name-width", options, N_("<width>"),
5401 N_("generate diffstat with a given name width"),
5402 PARSE_OPT_NONEG, diff_opt_stat),
5403 OPT_CALLBACK_F(0, "stat-graph-width", options, N_("<width>"),
5404 N_("generate diffstat with a given graph width"),
5405 PARSE_OPT_NONEG, diff_opt_stat),
5406 OPT_CALLBACK_F(0, "stat-count", options, N_("<count>"),
5407 N_("generate diffstat with limited lines"),
5408 PARSE_OPT_NONEG, diff_opt_stat),
Nguyễn Thái Ngọc Duy7d7942b2019-02-21 18:16:11 +07005409 OPT_CALLBACK_F(0, "compact-summary", options, NULL,
5410 N_("generate compact summary in diffstat"),
5411 PARSE_OPT_NOARG, diff_opt_compact_summary),
Nguyễn Thái Ngọc Duy6d9af6f2019-03-05 19:30:12 +07005412 OPT_CALLBACK_F(0, "binary", options, NULL,
5413 N_("output a binary diff that can be applied"),
5414 PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_binary),
Nguyễn Thái Ngọc Duy4fe01672019-03-05 19:30:13 +07005415 OPT_BOOL(0, "full-index", &options->flags.full_index,
5416 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 +07005417 OPT_COLOR_FLAG(0, "color", &options->use_color,
5418 N_("show colored diff")),
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07005419 OPT_CALLBACK_F(0, "ws-error-highlight", options, N_("<kind>"),
5420 N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
5421 PARSE_OPT_NONEG, diff_opt_ws_error_highlight),
Nguyễn Thái Ngọc Duy1a1eb162019-03-24 15:19:57 +07005422 OPT_SET_INT('z', NULL, &options->line_termination,
5423 N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
5424 0),
Nguyễn Thái Ngọc Duyd8774182019-03-24 15:20:04 +07005425 OPT__ABBREV(&options->abbrev),
Nguyễn Thái Ngọc Duy31fba9d2019-03-24 15:20:05 +07005426 OPT_STRING_F(0, "src-prefix", &options->a_prefix, N_("<prefix>"),
5427 N_("show the given source prefix instead of \"a/\""),
5428 PARSE_OPT_NONEG),
5429 OPT_STRING_F(0, "dst-prefix", &options->b_prefix, N_("<prefix>"),
Jiang Xin8a1569d2019-06-02 23:11:22 +08005430 N_("show the given destination prefix instead of \"b/\""),
Nguyễn Thái Ngọc Duy31fba9d2019-03-24 15:20:05 +07005431 PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy2f81cf92019-03-24 15:20:06 +07005432 OPT_CALLBACK_F(0, "line-prefix", options, N_("<prefix>"),
5433 N_("prepend an additional prefix to every line of output"),
5434 PARSE_OPT_NONEG, diff_opt_line_prefix),
Nguyễn Thái Ngọc Duy11c659d2019-03-24 15:20:07 +07005435 OPT_CALLBACK_F(0, "no-prefix", options, NULL,
5436 N_("do not show any source or destination prefix"),
5437 PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_no_prefix),
Nguyễn Thái Ngọc Duy16ed6c92019-03-24 15:20:08 +07005438 OPT_INTEGER_F(0, "inter-hunk-context", &options->interhunkcontext,
5439 N_("show context between diff hunks up to the specified number of lines"),
5440 PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005441 OPT_CALLBACK_F(0, "output-indicator-new",
5442 &options->output_indicators[OUTPUT_INDICATOR_NEW],
5443 N_("<char>"),
5444 N_("specify the character to indicate a new line instead of '+'"),
5445 PARSE_OPT_NONEG, diff_opt_char),
5446 OPT_CALLBACK_F(0, "output-indicator-old",
5447 &options->output_indicators[OUTPUT_INDICATOR_OLD],
5448 N_("<char>"),
5449 N_("specify the character to indicate an old line instead of '-'"),
5450 PARSE_OPT_NONEG, diff_opt_char),
5451 OPT_CALLBACK_F(0, "output-indicator-context",
5452 &options->output_indicators[OUTPUT_INDICATOR_CONTEXT],
5453 N_("<char>"),
5454 N_("specify the character to indicate a context instead of ' '"),
5455 PARSE_OPT_NONEG, diff_opt_char),
5456
Nguyễn Thái Ngọc Duyced4e172019-02-21 18:16:13 +07005457 OPT_GROUP(N_("Diff rename options")),
5458 OPT_CALLBACK_F('B', "break-rewrites", &options->break_opt, N_("<n>[/<m>]"),
5459 N_("break complete rewrite changes into pairs of delete and create"),
5460 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5461 diff_opt_break_rewrites),
Nguyễn Thái Ngọc Duyf4763082019-02-21 18:16:14 +07005462 OPT_CALLBACK_F('M', "find-renames", options, N_("<n>"),
5463 N_("detect renames"),
5464 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5465 diff_opt_find_renames),
Nguyễn Thái Ngọc Duy1e533292019-02-21 18:16:15 +07005466 OPT_SET_INT_F('D', "irreversible-delete", &options->irreversible_delete,
5467 N_("omit the preimage for deletes"),
5468 1, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy7f648502019-02-21 18:16:16 +07005469 OPT_CALLBACK_F('C', "find-copies", options, N_("<n>"),
5470 N_("detect copies"),
5471 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5472 diff_opt_find_copies),
Nguyễn Thái Ngọc Duybdd47412019-02-21 18:16:17 +07005473 OPT_BOOL(0, "find-copies-harder", &options->flags.find_copies_harder,
5474 N_("use unmodified files as source to find copies")),
Nguyễn Thái Ngọc Duycdc43eb2019-02-21 18:16:18 +07005475 OPT_SET_INT_F(0, "no-renames", &options->detect_rename,
5476 N_("disable rename detection"),
5477 0, PARSE_OPT_NONEG),
5478 OPT_BOOL(0, "rename-empty", &options->flags.rename_empty,
5479 N_("use empty blobs as rename source")),
Nguyễn Thái Ngọc Duy1e9250b2019-03-05 19:30:16 +07005480 OPT_CALLBACK_F(0, "follow", options, NULL,
5481 N_("continue listing the history of a file beyond renames"),
5482 PARSE_OPT_NOARG, diff_opt_follow),
Nguyễn Thái Ngọc Duybffee742019-03-24 15:19:58 +07005483 OPT_INTEGER('l', NULL, &options->rename_limit,
5484 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 +07005485
Nguyễn Thái Ngọc Duy2e75f922019-02-21 18:16:20 +07005486 OPT_GROUP(N_("Diff algorithm options")),
5487 OPT_BIT(0, "minimal", &options->xdl_opts,
5488 N_("produce the smallest possible diff"),
5489 XDF_NEED_MINIMAL),
Nguyễn Thái Ngọc Duy87649a12019-02-21 18:16:21 +07005490 OPT_BIT_F('w', "ignore-all-space", &options->xdl_opts,
5491 N_("ignore whitespace when comparing lines"),
5492 XDF_IGNORE_WHITESPACE, PARSE_OPT_NONEG),
5493 OPT_BIT_F('b', "ignore-space-change", &options->xdl_opts,
5494 N_("ignore changes in amount of whitespace"),
5495 XDF_IGNORE_WHITESPACE_CHANGE, PARSE_OPT_NONEG),
5496 OPT_BIT_F(0, "ignore-space-at-eol", &options->xdl_opts,
5497 N_("ignore changes in whitespace at EOL"),
5498 XDF_IGNORE_WHITESPACE_AT_EOL, PARSE_OPT_NONEG),
5499 OPT_BIT_F(0, "ignore-cr-at-eol", &options->xdl_opts,
5500 N_("ignore carrier-return at the end of line"),
5501 XDF_IGNORE_CR_AT_EOL, PARSE_OPT_NONEG),
5502 OPT_BIT_F(0, "ignore-blank-lines", &options->xdl_opts,
5503 N_("ignore changes whose lines are all blank"),
5504 XDF_IGNORE_BLANK_LINES, PARSE_OPT_NONEG),
Michał Kępień296d4a92020-10-20 08:48:09 +02005505 OPT_CALLBACK_F('I', "ignore-matching-lines", options, N_("<regex>"),
5506 N_("ignore changes whose all lines match <regex>"),
5507 0, diff_opt_ignore_regex),
Nguyễn Thái Ngọc Duy06f77512019-03-05 19:30:07 +07005508 OPT_BIT(0, "indent-heuristic", &options->xdl_opts,
5509 N_("heuristic to shift diff hunk boundaries for easy reading"),
5510 XDF_INDENT_HEURISTIC),
Nguyễn Thái Ngọc Duy31fd6402019-03-05 19:30:08 +07005511 OPT_CALLBACK_F(0, "patience", options, NULL,
5512 N_("generate diff using the \"patience diff\" algorithm"),
5513 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
5514 diff_opt_patience),
Nguyễn Thái Ngọc Duyf1e68ef2019-03-05 19:30:09 +07005515 OPT_BITOP(0, "histogram", &options->xdl_opts,
5516 N_("generate diff using the \"histogram diff\" algorithm"),
5517 XDF_HISTOGRAM_DIFF, XDF_DIFF_ALGORITHM_MASK),
Nguyễn Thái Ngọc Duy10f35b12019-03-05 19:30:10 +07005518 OPT_CALLBACK_F(0, "diff-algorithm", options, N_("<algorithm>"),
5519 N_("choose a diff algorithm"),
5520 PARSE_OPT_NONEG, diff_opt_diff_algorithm),
Nguyễn Thái Ngọc Duydf84a432019-03-05 19:30:11 +07005521 OPT_CALLBACK_F(0, "anchored", options, N_("<text>"),
5522 N_("generate diff using the \"anchored diff\" algorithm"),
5523 PARSE_OPT_NONEG, diff_opt_anchored),
Nguyễn Thái Ngọc Duye9fb39b2019-03-05 19:30:18 +07005524 OPT_CALLBACK_F(0, "word-diff", options, N_("<mode>"),
5525 N_("show word diff, using <mode> to delimit changed words"),
5526 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_word_diff),
Nguyễn Thái Ngọc Duy797df112019-03-05 19:30:19 +07005527 OPT_CALLBACK_F(0, "word-diff-regex", options, N_("<regex>"),
5528 N_("use <regex> to decide what a word is"),
5529 PARSE_OPT_NONEG, diff_opt_word_diff_regex),
Nguyễn Thái Ngọc Duy212db692019-03-05 19:30:20 +07005530 OPT_CALLBACK_F(0, "color-words", options, N_("<regex>"),
5531 N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
5532 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_color_words),
Nguyễn Thái Ngọc Duy59311a92019-03-24 15:20:09 +07005533 OPT_CALLBACK_F(0, "color-moved", options, N_("<mode>"),
Jean-Noël Avila8bcd8f42019-05-17 21:26:19 +02005534 N_("moved lines of code are colored differently"),
Nguyễn Thái Ngọc Duy59311a92019-03-24 15:20:09 +07005535 PARSE_OPT_OPTARG, diff_opt_color_moved),
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005536 OPT_CALLBACK_F(0, "color-moved-ws", options, N_("<mode>"),
5537 N_("how white spaces are ignored in --color-moved"),
Nguyễn Thái Ngọc Duybb987292019-03-24 15:20:11 +07005538 0, diff_opt_color_moved_ws),
Nguyễn Thái Ngọc Duy2e75f922019-02-21 18:16:20 +07005539
Jean-Noël Avila8bcd8f42019-05-17 21:26:19 +02005540 OPT_GROUP(N_("Other diff options")),
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005541 OPT_CALLBACK_F(0, "relative", options, N_("<prefix>"),
5542 N_("when run from subdir, exclude changes outside and show relative paths"),
Laurent Arnoudc28ded82020-05-22 12:46:18 +02005543 PARSE_OPT_OPTARG,
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005544 diff_opt_relative),
Nguyễn Thái Ngọc Duyc37d4c02019-03-05 19:30:14 +07005545 OPT_BOOL('a', "text", &options->flags.text,
5546 N_("treat all files as text")),
Nguyễn Thái Ngọc Duy4f99f292019-03-05 19:30:15 +07005547 OPT_BOOL('R', NULL, &options->flags.reverse_diff,
5548 N_("swap two inputs, reverse the diff")),
Nguyễn Thái Ngọc Duy1086ea02019-03-05 19:30:21 +07005549 OPT_BOOL(0, "exit-code", &options->flags.exit_with_status,
5550 N_("exit with 1 if there were differences, 0 otherwise")),
Nguyễn Thái Ngọc Duy9bbaf1c2019-03-05 19:30:22 +07005551 OPT_BOOL(0, "quiet", &options->flags.quick,
5552 N_("disable all output of the program")),
Nguyễn Thái Ngọc Duy0cda1002019-03-05 19:30:23 +07005553 OPT_BOOL(0, "ext-diff", &options->flags.allow_external,
5554 N_("allow an external diff helper to be executed")),
Nguyễn Thái Ngọc Duy8ab76972019-03-05 19:30:24 +07005555 OPT_CALLBACK_F(0, "textconv", options, NULL,
5556 N_("run external text conversion filters when comparing binary files"),
5557 PARSE_OPT_NOARG, diff_opt_textconv),
Nguyễn Thái Ngọc Duyb680ee12019-03-05 19:30:25 +07005558 OPT_CALLBACK_F(0, "ignore-submodules", options, N_("<when>"),
5559 N_("ignore changes to submodules in the diff generation"),
5560 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5561 diff_opt_ignore_submodules),
Nguyễn Thái Ngọc Duy125dcea2019-03-05 19:30:26 +07005562 OPT_CALLBACK_F(0, "submodule", options, N_("<format>"),
5563 N_("specify how differences in submodules are shown"),
5564 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5565 diff_opt_submodule),
Nguyễn Thái Ngọc Duy96860e42019-03-24 15:19:56 +07005566 OPT_SET_INT_F(0, "ita-invisible-in-index", &options->ita_invisible_in_index,
5567 N_("hide 'git add -N' entries from the index"),
5568 1, PARSE_OPT_NONEG),
5569 OPT_SET_INT_F(0, "ita-visible-in-index", &options->ita_invisible_in_index,
5570 N_("treat 'git add -N' entries as real in the index"),
5571 0, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duya41cfb32019-03-24 15:19:59 +07005572 OPT_CALLBACK_F('S', NULL, options, N_("<string>"),
5573 N_("look for differences that change the number of occurrences of the specified string"),
5574 0, diff_opt_pickaxe_string),
5575 OPT_CALLBACK_F('G', NULL, options, N_("<regex>"),
5576 N_("look for differences that change the number of occurrences of the specified regex"),
5577 0, diff_opt_pickaxe_regex),
Nguyễn Thái Ngọc Duy85f8e882019-03-24 15:20:00 +07005578 OPT_BIT_F(0, "pickaxe-all", &options->pickaxe_opts,
5579 N_("show all changes in the changeset with -S or -G"),
5580 DIFF_PICKAXE_ALL, PARSE_OPT_NONEG),
5581 OPT_BIT_F(0, "pickaxe-regex", &options->pickaxe_opts,
5582 N_("treat <string> in -S as extended POSIX regular expression"),
5583 DIFF_PICKAXE_REGEX, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyf7318142019-03-24 15:20:01 +07005584 OPT_FILENAME('O', NULL, &options->orderfile,
5585 N_("control the order in which files appear in the output")),
Junio C Hamano1eb41362021-02-11 11:57:50 -08005586 OPT_CALLBACK_F(0, "rotate-to", options, N_("<path>"),
5587 N_("show the change in the specified path first"),
5588 PARSE_OPT_NONEG, diff_opt_rotate_to),
5589 OPT_CALLBACK_F(0, "skip-to", options, N_("<path>"),
5590 N_("skip the output to the specified path"),
5591 PARSE_OPT_NONEG, diff_opt_rotate_to),
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07005592 OPT_CALLBACK_F(0, "find-object", options, N_("<object-id>"),
5593 N_("look for differences that change the number of occurrences of the specified object"),
5594 PARSE_OPT_NONEG, diff_opt_find_object),
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07005595 OPT_CALLBACK_F(0, "diff-filter", options, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
5596 N_("select files by diff type"),
5597 PARSE_OPT_NONEG, diff_opt_diff_filter),
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005598 { OPTION_CALLBACK, 0, "output", options, N_("<file>"),
5599 N_("Output to a specific file"),
5600 PARSE_OPT_NONEG, NULL, 0, diff_opt_output },
5601
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005602 OPT_END()
5603 };
5604
5605 ALLOC_ARRAY(options->parseopts, ARRAY_SIZE(parseopts));
5606 memcpy(options->parseopts, parseopts, sizeof(parseopts));
5607}
5608
Duy Nguyena97262c2016-01-21 18:48:44 +07005609int diff_opt_parse(struct diff_options *options,
5610 const char **av, int ac, const char *prefix)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005611{
Duy Nguyena97262c2016-01-21 18:48:44 +07005612 if (!prefix)
5613 prefix = "";
5614
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005615 ac = parse_options(ac, av, prefix, options->parseopts, NULL,
5616 PARSE_OPT_KEEP_DASHDASH |
5617 PARSE_OPT_KEEP_UNKNOWN |
5618 PARSE_OPT_NO_INTERNAL_HELP |
5619 PARSE_OPT_ONE_SHOT |
5620 PARSE_OPT_STOP_AT_NON_OPTION);
5621
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005622 return ac;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005623}
5624
Kevin Ballard10ae7522010-09-27 16:58:25 -07005625int parse_rename_score(const char **cp_p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005626{
5627 unsigned long num, scale;
5628 int ch, dot;
5629 const char *cp = *cp_p;
5630
5631 num = 0;
5632 scale = 1;
5633 dot = 0;
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -04005634 for (;;) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07005635 ch = *cp;
5636 if ( !dot && ch == '.' ) {
5637 scale = 1;
5638 dot = 1;
5639 } else if ( ch == '%' ) {
5640 scale = dot ? scale*100 : 100;
5641 cp++; /* % is always at the end */
5642 break;
5643 } else if ( ch >= '0' && ch <= '9' ) {
5644 if ( scale < 100000 ) {
5645 scale *= 10;
5646 num = (num*10) + (ch-'0');
5647 }
5648 } else {
5649 break;
5650 }
5651 cp++;
5652 }
5653 *cp_p = cp;
5654
5655 /* user says num divided by scale and we say internally that
5656 * is MAX_SCORE * num / scale.
5657 */
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05005658 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
Junio C Hamano6973dca2006-04-21 23:57:45 -07005659}
5660
Junio C Hamano6973dca2006-04-21 23:57:45 -07005661struct diff_queue_struct diff_queued_diff;
5662
5663void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
5664{
Dmitry S. Dolzhenko4c960a42014-03-04 02:31:53 +04005665 ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005666 queue->queue[queue->nr++] = dp;
5667}
5668
5669struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
5670 struct diff_filespec *one,
5671 struct diff_filespec *two)
5672{
Junio C Hamanoef677682006-08-03 12:01:01 -07005673 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
Junio C Hamano6973dca2006-04-21 23:57:45 -07005674 dp->one = one;
5675 dp->two = two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005676 if (queue)
5677 diff_q(queue, dp);
5678 return dp;
5679}
5680
5681void diff_free_filepair(struct diff_filepair *p)
5682{
Linus Torvalds9fb88412007-10-25 11:19:10 -07005683 free_filespec(p->one);
5684 free_filespec(p->two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005685 free(p);
5686}
5687
Jeff Kingd6cece52016-10-20 02:20:07 -04005688const char *diff_aligned_abbrev(const struct object_id *oid, int len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005689{
5690 int abblen;
5691 const char *abbrev;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005692
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005693 /* Do we want all 40 hex characters? */
brian m. carlson02afca12018-07-16 01:28:05 +00005694 if (len == the_hash_algo->hexsz)
Jeff Kingd6cece52016-10-20 02:20:07 -04005695 return oid_to_hex(oid);
5696
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005697 /* An abbreviated value is fine, possibly followed by an ellipsis. */
Jeff King4f036662016-10-20 02:21:25 -04005698 abbrev = diff_abbrev_oid(oid, len);
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005699
5700 if (!print_sha1_ellipsis())
5701 return abbrev;
5702
Junio C Hamano6973dca2006-04-21 23:57:45 -07005703 abblen = strlen(abbrev);
Junio C Hamanod709f1f2016-09-30 10:42:05 -07005704
5705 /*
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005706 * In well-behaved cases, where the abbreviated result is the
Junio C Hamanod709f1f2016-09-30 10:42:05 -07005707 * same as the requested length, append three dots after the
5708 * abbreviation (hence the whole logic is limited to the case
5709 * where abblen < 37); when the actual abbreviated result is a
5710 * bit longer than the requested length, we reduce the number
5711 * of dots so that they match the well-behaved ones. However,
5712 * if the actual abbreviation is longer than the requested
5713 * length by more than three, we give up on aligning, and add
5714 * three dots anyway, to indicate that the output is not the
5715 * full object name. Yes, this may be suboptimal, but this
5716 * appears only in "diff --raw --abbrev" output and it is not
5717 * worth the effort to change it now. Note that this would
5718 * likely to work fine when the automatic sizing of default
5719 * abbreviation length is used--we would be fed -1 in "len" in
5720 * that case, and will end up always appending three-dots, but
5721 * the automatic sizing is supposed to give abblen that ensures
5722 * uniqueness across all objects (statistically speaking).
5723 */
brian m. carlson02afca12018-07-16 01:28:05 +00005724 if (abblen < the_hash_algo->hexsz - 3) {
brian m. carlsondc015052017-03-26 16:01:24 +00005725 static char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano6973dca2006-04-21 23:57:45 -07005726 if (len < abblen && abblen <= len + 2)
Jeff King5096d492015-09-24 17:06:08 -04005727 xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");
Junio C Hamano6973dca2006-04-21 23:57:45 -07005728 else
Jeff King5096d492015-09-24 17:06:08 -04005729 xsnprintf(hex, sizeof(hex), "%s...", abbrev);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005730 return hex;
5731 }
Jeff Kingd6cece52016-10-20 02:20:07 -04005732
5733 return oid_to_hex(oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005734}
5735
Pierre Habouzit663af342007-09-20 00:42:15 +02005736static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005737{
Pierre Habouzit663af342007-09-20 00:42:15 +02005738 int line_termination = opt->line_termination;
5739 int inter_name_termination = line_termination ? '\t' : '\0';
Timo Hirvonenc6744342006-06-24 20:21:53 +03005740
John Keeping30997bb2013-02-07 20:15:27 +00005741 fprintf(opt->file, "%s", diff_line_prefix(opt));
Pierre Habouzit663af342007-09-20 00:42:15 +02005742 if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005743 fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
Jeff Kingd6cece52016-10-20 02:20:07 -04005744 diff_aligned_abbrev(&p->one->oid, opt->abbrev));
brian m. carlsona0d12c42016-06-24 23:09:23 +00005745 fprintf(opt->file, "%s ",
Jeff Kingd6cece52016-10-20 02:20:07 -04005746 diff_aligned_abbrev(&p->two->oid, opt->abbrev));
Pierre Habouzit663af342007-09-20 00:42:15 +02005747 }
5748 if (p->score) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005749 fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),
5750 inter_name_termination);
Pierre Habouzit663af342007-09-20 00:42:15 +02005751 } else {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005752 fprintf(opt->file, "%c%c", p->status, inter_name_termination);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005753 }
5754
Junio C Hamanocd676a52008-02-12 14:26:02 -08005755 if (p->status == DIFF_STATUS_COPIED ||
5756 p->status == DIFF_STATUS_RENAMED) {
5757 const char *name_a, *name_b;
5758 name_a = p->one->path;
5759 name_b = p->two->path;
5760 strip_prefix(opt->prefix_length, &name_a, &name_b);
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005761 write_name_quoted(name_a, opt->file, inter_name_termination);
5762 write_name_quoted(name_b, opt->file, line_termination);
Pierre Habouzit663af342007-09-20 00:42:15 +02005763 } else {
Junio C Hamanocd676a52008-02-12 14:26:02 -08005764 const char *name_a, *name_b;
5765 name_a = p->one->mode ? p->one->path : p->two->path;
5766 name_b = NULL;
5767 strip_prefix(opt->prefix_length, &name_a, &name_b);
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005768 write_name_quoted(name_a, opt->file, line_termination);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005769 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07005770}
5771
5772int diff_unmodified_pair(struct diff_filepair *p)
5773{
5774 /* This function is written stricter than necessary to support
5775 * the currently implemented transformers, but the idea is to
5776 * let transformers to produce diff_filepairs any way they want,
5777 * and filter and clean them up here before producing the output.
5778 */
Pierre Habouzit663af342007-09-20 00:42:15 +02005779 struct diff_filespec *one = p->one, *two = p->two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005780
5781 if (DIFF_PAIR_UNMERGED(p))
5782 return 0; /* unmerged is interesting */
5783
Junio C Hamano6973dca2006-04-21 23:57:45 -07005784 /* deletion, addition, mode or type change
5785 * and rename are all interesting.
5786 */
5787 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
5788 DIFF_PAIR_MODE_CHANGED(p) ||
5789 strcmp(one->path, two->path))
5790 return 0;
5791
5792 /* both are valid and point at the same path. that is, we are
5793 * dealing with a change.
5794 */
brian m. carlson41c95602016-06-24 23:09:24 +00005795 if (one->oid_valid && two->oid_valid &&
Jeff King4a7e27e2018-08-28 17:22:40 -04005796 oideq(&one->oid, &two->oid) &&
Jens Lehmann85adbf22010-03-12 22:23:52 +01005797 !one->dirty_submodule && !two->dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005798 return 1; /* no change */
brian m. carlson41c95602016-06-24 23:09:24 +00005799 if (!one->oid_valid && !two->oid_valid)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005800 return 1; /* both look at the same file on the filesystem. */
5801 return 0;
5802}
5803
5804static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
5805{
5806 if (diff_unmodified_pair(p))
5807 return;
5808
5809 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5810 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
5811 return; /* no tree diffs in patch format */
5812
5813 run_diff(p, o);
5814}
5815
5816static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
5817 struct diffstat_t *diffstat)
5818{
5819 if (diff_unmodified_pair(p))
5820 return;
5821
5822 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5823 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Yann Dirsonc3fced62010-10-24 23:03:10 +02005824 return; /* no useful stat for tree diffs */
Junio C Hamano6973dca2006-04-21 23:57:45 -07005825
5826 run_diffstat(p, o, diffstat);
5827}
5828
Johannes Schindelin88246892006-05-20 23:43:13 +02005829static void diff_flush_checkdiff(struct diff_filepair *p,
5830 struct diff_options *o)
5831{
5832 if (diff_unmodified_pair(p))
5833 return;
5834
5835 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5836 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Yann Dirsonc3fced62010-10-24 23:03:10 +02005837 return; /* nothing to check in tree diffs */
Johannes Schindelin88246892006-05-20 23:43:13 +02005838
5839 run_checkdiff(p, o);
5840}
5841
Junio C Hamano6973dca2006-04-21 23:57:45 -07005842int diff_queue_is_empty(void)
5843{
5844 struct diff_queue_struct *q = &diff_queued_diff;
5845 int i;
5846 for (i = 0; i < q->nr; i++)
5847 if (!diff_unmodified_pair(q->queue[i]))
5848 return 0;
5849 return 1;
5850}
5851
5852#if DIFF_DEBUG
5853void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
5854{
5855 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
5856 x, one ? one : "",
5857 s->path,
5858 DIFF_FILE_VALID(s) ? "valid" : "invalid",
5859 s->mode,
brian m. carlson41c95602016-06-24 23:09:24 +00005860 s->oid_valid ? oid_to_hex(&s->oid) : "");
Jeff King428d52a2014-01-16 20:21:59 -05005861 fprintf(stderr, "queue[%d] %s size %lu\n",
Junio C Hamano6973dca2006-04-21 23:57:45 -07005862 x, one ? one : "",
Jeff King428d52a2014-01-16 20:21:59 -05005863 s->size);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005864}
5865
5866void diff_debug_filepair(const struct diff_filepair *p, int i)
5867{
5868 diff_debug_filespec(p->one, i, "one");
5869 diff_debug_filespec(p->two, i, "two");
Linus Torvalds64479712007-10-25 11:20:56 -07005870 fprintf(stderr, "score %d, status %c rename_used %d broken %d\n",
Junio C Hamano6973dca2006-04-21 23:57:45 -07005871 p->score, p->status ? p->status : '?',
Linus Torvalds64479712007-10-25 11:20:56 -07005872 p->one->rename_used, p->broken_pair);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005873}
5874
5875void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
5876{
5877 int i;
5878 if (msg)
5879 fprintf(stderr, "%s\n", msg);
5880 fprintf(stderr, "q->nr = %d\n", q->nr);
5881 for (i = 0; i < q->nr; i++) {
5882 struct diff_filepair *p = q->queue[i];
5883 diff_debug_filepair(p, i);
5884 }
5885}
5886#endif
5887
5888static void diff_resolve_rename_copy(void)
5889{
Linus Torvalds64479712007-10-25 11:20:56 -07005890 int i;
5891 struct diff_filepair *p;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005892 struct diff_queue_struct *q = &diff_queued_diff;
5893
5894 diff_debug_queue("resolve-rename-copy", q);
5895
5896 for (i = 0; i < q->nr; i++) {
5897 p = q->queue[i];
5898 p->status = 0; /* undecided */
5899 if (DIFF_PAIR_UNMERGED(p))
5900 p->status = DIFF_STATUS_UNMERGED;
5901 else if (!DIFF_FILE_VALID(p->one))
5902 p->status = DIFF_STATUS_ADDED;
5903 else if (!DIFF_FILE_VALID(p->two))
5904 p->status = DIFF_STATUS_DELETED;
5905 else if (DIFF_PAIR_TYPE_CHANGED(p))
5906 p->status = DIFF_STATUS_TYPE_CHANGED;
5907
5908 /* from this point on, we are dealing with a pair
5909 * whose both sides are valid and of the same type, i.e.
5910 * either in-place edit or rename/copy edit.
5911 */
5912 else if (DIFF_PAIR_RENAME(p)) {
Linus Torvalds64479712007-10-25 11:20:56 -07005913 /*
5914 * A rename might have re-connected a broken
5915 * pair up, causing the pathnames to be the
5916 * same again. If so, that's not a rename at
5917 * all, just a modification..
5918 *
5919 * Otherwise, see if this source was used for
5920 * multiple renames, in which case we decrement
5921 * the count, and call it a copy.
Junio C Hamano6973dca2006-04-21 23:57:45 -07005922 */
Linus Torvalds64479712007-10-25 11:20:56 -07005923 if (!strcmp(p->one->path, p->two->path))
5924 p->status = DIFF_STATUS_MODIFIED;
5925 else if (--p->one->rename_used > 0)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005926 p->status = DIFF_STATUS_COPIED;
Linus Torvalds64479712007-10-25 11:20:56 -07005927 else
Junio C Hamano6973dca2006-04-21 23:57:45 -07005928 p->status = DIFF_STATUS_RENAMED;
5929 }
Jeff King9001dc22018-08-28 17:22:48 -04005930 else if (!oideq(&p->one->oid, &p->two->oid) ||
Johannes Schindelind516c2d2007-02-22 21:50:10 +01005931 p->one->mode != p->two->mode ||
Jens Lehmann85adbf22010-03-12 22:23:52 +01005932 p->one->dirty_submodule ||
5933 p->two->dirty_submodule ||
brian m. carlsona0d12c42016-06-24 23:09:23 +00005934 is_null_oid(&p->one->oid))
Junio C Hamano6973dca2006-04-21 23:57:45 -07005935 p->status = DIFF_STATUS_MODIFIED;
5936 else {
5937 /* This is a "no-change" entry and should not
5938 * happen anymore, but prepare for broken callers.
5939 */
5940 error("feeding unmodified %s to diffcore",
5941 p->one->path);
5942 p->status = DIFF_STATUS_UNKNOWN;
5943 }
5944 }
5945 diff_debug_queue("resolve-rename-copy done", q);
5946}
5947
Timo Hirvonenc6744342006-06-24 20:21:53 +03005948static int check_pair_status(struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005949{
Junio C Hamano6973dca2006-04-21 23:57:45 -07005950 switch (p->status) {
5951 case DIFF_STATUS_UNKNOWN:
Timo Hirvonenc6744342006-06-24 20:21:53 +03005952 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005953 case 0:
5954 die("internal error in diff-resolve-rename-copy");
Junio C Hamano6973dca2006-04-21 23:57:45 -07005955 default:
Timo Hirvonenc6744342006-06-24 20:21:53 +03005956 return 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005957 }
5958}
5959
Timo Hirvonenc6744342006-06-24 20:21:53 +03005960static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
5961{
5962 int fmt = opt->output_format;
5963
5964 if (fmt & DIFF_FORMAT_CHECKDIFF)
5965 diff_flush_checkdiff(p, opt);
5966 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
5967 diff_flush_raw(p, opt);
Junio C Hamanocd676a52008-02-12 14:26:02 -08005968 else if (fmt & DIFF_FORMAT_NAME) {
5969 const char *name_a, *name_b;
5970 name_a = p->two->path;
5971 name_b = NULL;
5972 strip_prefix(opt->prefix_length, &name_a, &name_b);
Jeff Kingf5022b52017-02-08 15:31:15 -05005973 fprintf(opt->file, "%s", diff_line_prefix(opt));
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005974 write_name_quoted(name_a, opt->file, opt->line_termination);
Junio C Hamanocd676a52008-02-12 14:26:02 -08005975 }
Timo Hirvonenc6744342006-06-24 20:21:53 +03005976}
5977
Stefan Beller146fdb02017-06-29 17:07:05 -07005978static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs)
Sean4bbd2612006-05-14 08:13:49 -04005979{
Stefan Beller146fdb02017-06-29 17:07:05 -07005980 struct strbuf sb = STRBUF_INIT;
Sean4bbd2612006-05-14 08:13:49 -04005981 if (fs->mode)
Stefan Beller146fdb02017-06-29 17:07:05 -07005982 strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode);
Sean4bbd2612006-05-14 08:13:49 -04005983 else
Stefan Beller146fdb02017-06-29 17:07:05 -07005984 strbuf_addf(&sb, " %s ", newdelete);
5985
5986 quote_c_style(fs->path, &sb, NULL, 0);
5987 strbuf_addch(&sb, '\n');
5988 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
5989 sb.buf, sb.len, 0);
5990 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04005991}
5992
Stefan Beller146fdb02017-06-29 17:07:05 -07005993static void show_mode_change(struct diff_options *opt, struct diff_filepair *p,
5994 int show_name)
Sean4bbd2612006-05-14 08:13:49 -04005995{
5996 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
Stefan Beller146fdb02017-06-29 17:07:05 -07005997 struct strbuf sb = STRBUF_INIT;
5998 strbuf_addf(&sb, " mode change %06o => %06o",
5999 p->one->mode, p->two->mode);
Alexandre Julliard0d26a642007-02-10 15:37:48 +01006000 if (show_name) {
Stefan Beller146fdb02017-06-29 17:07:05 -07006001 strbuf_addch(&sb, ' ');
6002 quote_c_style(p->two->path, &sb, NULL, 0);
Alexandre Julliard0d26a642007-02-10 15:37:48 +01006003 }
Stefan Beller58aaced2017-09-27 15:51:26 -07006004 strbuf_addch(&sb, '\n');
Stefan Beller146fdb02017-06-29 17:07:05 -07006005 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6006 sb.buf, sb.len, 0);
6007 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04006008 }
6009}
6010
Stefan Beller146fdb02017-06-29 17:07:05 -07006011static void show_rename_copy(struct diff_options *opt, const char *renamecopy,
6012 struct diff_filepair *p)
Sean4bbd2612006-05-14 08:13:49 -04006013{
Stefan Beller146fdb02017-06-29 17:07:05 -07006014 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07006015 struct strbuf names = STRBUF_INIT;
6016
6017 pprint_rename(&names, p->one->path, p->two->path);
Stefan Beller146fdb02017-06-29 17:07:05 -07006018 strbuf_addf(&sb, " %s %s (%d%%)\n",
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07006019 renamecopy, names.buf, similarity_index(p));
6020 strbuf_release(&names);
Stefan Beller146fdb02017-06-29 17:07:05 -07006021 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6022 sb.buf, sb.len, 0);
6023 show_mode_change(opt, p, 0);
Rene Scharfe348eda22017-08-30 19:49:42 +02006024 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04006025}
6026
Bo Yang7be57612010-05-26 15:23:54 +08006027static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
Sean4bbd2612006-05-14 08:13:49 -04006028{
6029 switch(p->status) {
6030 case DIFF_STATUS_DELETED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006031 show_file_mode_name(opt, "delete", p->one);
Sean4bbd2612006-05-14 08:13:49 -04006032 break;
6033 case DIFF_STATUS_ADDED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006034 show_file_mode_name(opt, "create", p->two);
Sean4bbd2612006-05-14 08:13:49 -04006035 break;
6036 case DIFF_STATUS_COPIED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006037 show_rename_copy(opt, "copy", p);
Sean4bbd2612006-05-14 08:13:49 -04006038 break;
6039 case DIFF_STATUS_RENAMED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006040 show_rename_copy(opt, "rename", p);
Sean4bbd2612006-05-14 08:13:49 -04006041 break;
6042 default:
6043 if (p->score) {
Stefan Beller146fdb02017-06-29 17:07:05 -07006044 struct strbuf sb = STRBUF_INIT;
6045 strbuf_addstr(&sb, " rewrite ");
6046 quote_c_style(p->two->path, &sb, NULL, 0);
6047 strbuf_addf(&sb, " (%d%%)\n", similarity_index(p));
6048 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6049 sb.buf, sb.len, 0);
Rene Scharfefa842d82017-08-30 19:49:41 +02006050 strbuf_release(&sb);
Pierre Habouzit663af342007-09-20 00:42:15 +02006051 }
Stefan Beller146fdb02017-06-29 17:07:05 -07006052 show_mode_change(opt, p, !p->score);
Sean4bbd2612006-05-14 08:13:49 -04006053 break;
6054 }
6055}
6056
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006057struct patch_id_t {
brian m. carlson36261e42019-08-18 20:04:03 +00006058 git_hash_ctx *ctx;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006059 int patchlen;
6060};
6061
6062static int remove_space(char *line, int len)
6063{
6064 int i;
Pierre Habouzit663af342007-09-20 00:42:15 +02006065 char *dst = line;
6066 unsigned char c;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006067
Pierre Habouzit663af342007-09-20 00:42:15 +02006068 for (i = 0; i < len; i++)
6069 if (!isspace((c = line[i])))
6070 *dst++ = c;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006071
Pierre Habouzit663af342007-09-20 00:42:15 +02006072 return dst - line;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006073}
6074
brian m. carlson36261e42019-08-18 20:04:03 +00006075void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx)
Stephen Boyda8f68552019-04-26 16:51:57 -07006076{
6077 unsigned char hash[GIT_MAX_RAWSZ];
6078 unsigned short carry = 0;
6079 int i;
6080
brian m. carlson36261e42019-08-18 20:04:03 +00006081 the_hash_algo->final_fn(hash, ctx);
6082 the_hash_algo->init_fn(ctx);
Stephen Boyda8f68552019-04-26 16:51:57 -07006083 /* 20-byte sum, with carry */
brian m. carlson36261e42019-08-18 20:04:03 +00006084 for (i = 0; i < the_hash_algo->rawsz; ++i) {
Stephen Boyda8f68552019-04-26 16:51:57 -07006085 carry += result->hash[i] + hash[i];
6086 result->hash[i] = carry;
6087 carry >>= 8;
6088 }
6089}
6090
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02006091static int patch_id_consume(void *priv, char *line, unsigned long len)
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006092{
6093 struct patch_id_t *data = priv;
6094 int new_len;
6095
René Scharfe82a62012020-08-19 00:08:54 +02006096 if (len > 12 && starts_with(line, "\\ "))
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02006097 return 0;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006098 new_len = remove_space(line, len);
6099
brian m. carlson36261e42019-08-18 20:04:03 +00006100 the_hash_algo->update_fn(data->ctx, line, new_len);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006101 data->patchlen += new_len;
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02006102 return 0;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006103}
6104
brian m. carlson36261e42019-08-18 20:04:03 +00006105static void patch_id_add_string(git_hash_ctx *ctx, const char *str)
Jeff King977db6b2017-03-30 14:26:05 -04006106{
brian m. carlson36261e42019-08-18 20:04:03 +00006107 the_hash_algo->update_fn(ctx, str, strlen(str));
Jeff King977db6b2017-03-30 14:26:05 -04006108}
6109
brian m. carlson36261e42019-08-18 20:04:03 +00006110static void patch_id_add_mode(git_hash_ctx *ctx, unsigned mode)
Jeff King977db6b2017-03-30 14:26:05 -04006111{
6112 /* large enough for 2^32 in octal */
6113 char buf[12];
6114 int len = xsnprintf(buf, sizeof(buf), "%06o", mode);
brian m. carlson36261e42019-08-18 20:04:03 +00006115 the_hash_algo->update_fn(ctx, buf, len);
Jeff King977db6b2017-03-30 14:26:05 -04006116}
6117
Stephen Boyda8f68552019-04-26 16:51:57 -07006118/* returns 0 upon success, and writes result into oid */
6119static 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 +02006120{
6121 struct diff_queue_struct *q = &diff_queued_diff;
6122 int i;
brian m. carlson36261e42019-08-18 20:04:03 +00006123 git_hash_ctx ctx;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006124 struct patch_id_t data;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006125
brian m. carlson36261e42019-08-18 20:04:03 +00006126 the_hash_algo->init_fn(&ctx);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006127 memset(&data, 0, sizeof(struct patch_id_t));
6128 data.ctx = &ctx;
Stephen Boyda8f68552019-04-26 16:51:57 -07006129 oidclr(oid);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006130
6131 for (i = 0; i < q->nr; i++) {
6132 xpparam_t xpp;
6133 xdemitconf_t xecfg;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006134 mmfile_t mf1, mf2;
6135 struct diff_filepair *p = q->queue[i];
6136 int len1, len2;
6137
Brian Downing9ccd0a82008-10-25 15:30:37 +02006138 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01006139 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006140 if (p->status == 0)
6141 return error("internal diff status error");
6142 if (p->status == DIFF_STATUS_UNKNOWN)
6143 continue;
6144 if (diff_unmodified_pair(p))
6145 continue;
6146 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
6147 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
6148 continue;
6149 if (DIFF_PAIR_UNMERGED(p))
6150 continue;
6151
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02006152 diff_fill_oid_info(p->one, options->repo->index);
6153 diff_fill_oid_info(p->two, options->repo->index);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006154
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006155 len1 = remove_space(p->one->path, strlen(p->one->path));
6156 len2 = remove_space(p->two->path, strlen(p->two->path));
Jeff King977db6b2017-03-30 14:26:05 -04006157 patch_id_add_string(&ctx, "diff--git");
6158 patch_id_add_string(&ctx, "a/");
brian m. carlson36261e42019-08-18 20:04:03 +00006159 the_hash_algo->update_fn(&ctx, p->one->path, len1);
Jeff King977db6b2017-03-30 14:26:05 -04006160 patch_id_add_string(&ctx, "b/");
brian m. carlson36261e42019-08-18 20:04:03 +00006161 the_hash_algo->update_fn(&ctx, p->two->path, len2);
Jeff King977db6b2017-03-30 14:26:05 -04006162
6163 if (p->one->mode == 0) {
6164 patch_id_add_string(&ctx, "newfilemode");
6165 patch_id_add_mode(&ctx, p->two->mode);
6166 patch_id_add_string(&ctx, "---/dev/null");
6167 patch_id_add_string(&ctx, "+++b/");
brian m. carlson36261e42019-08-18 20:04:03 +00006168 the_hash_algo->update_fn(&ctx, p->two->path, len2);
Jeff King977db6b2017-03-30 14:26:05 -04006169 } else if (p->two->mode == 0) {
6170 patch_id_add_string(&ctx, "deletedfilemode");
6171 patch_id_add_mode(&ctx, p->one->mode);
6172 patch_id_add_string(&ctx, "---a/");
brian m. carlson36261e42019-08-18 20:04:03 +00006173 the_hash_algo->update_fn(&ctx, p->one->path, len1);
Jeff King977db6b2017-03-30 14:26:05 -04006174 patch_id_add_string(&ctx, "+++/dev/null");
6175 } else {
6176 patch_id_add_string(&ctx, "---a/");
brian m. carlson36261e42019-08-18 20:04:03 +00006177 the_hash_algo->update_fn(&ctx, p->one->path, len1);
Jeff King977db6b2017-03-30 14:26:05 -04006178 patch_id_add_string(&ctx, "+++b/");
brian m. carlson36261e42019-08-18 20:04:03 +00006179 the_hash_algo->update_fn(&ctx, p->two->path, len2);
Jeff King977db6b2017-03-30 14:26:05 -04006180 }
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006181
Kevin Willford3e8e32c2016-07-29 12:19:19 -04006182 if (diff_header_only)
6183 continue;
6184
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006185 if (fill_mmfile(options->repo, &mf1, p->one) < 0 ||
6186 fill_mmfile(options->repo, &mf2, p->two) < 0)
Kevin Willford3e8e32c2016-07-29 12:19:19 -04006187 return error("unable to read files to diff");
6188
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006189 if (diff_filespec_is_binary(options->repo, p->one) ||
6190 diff_filespec_is_binary(options->repo, p->two)) {
brian m. carlson36261e42019-08-18 20:04:03 +00006191 the_hash_algo->update_fn(&ctx, oid_to_hex(&p->one->oid),
6192 the_hash_algo->hexsz);
6193 the_hash_algo->update_fn(&ctx, oid_to_hex(&p->two->oid),
6194 the_hash_algo->hexsz);
Clemens Buchacher34597c12010-08-15 09:20:43 +02006195 continue;
6196 }
6197
René Scharfe582aa002010-05-02 15:04:41 +02006198 xpp.flags = 0;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006199 xecfg.ctxlen = 3;
Ævar Arnfjörð Bjarmason5d934602021-04-12 19:15:29 +02006200 xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
6201 if (xdi_diff_outf(&mf1, &mf2, NULL,
Jeff Kingb1357392018-11-02 02:36:36 -04006202 patch_id_consume, &data, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04006203 return error("unable to generate patch-id diff for %s",
6204 p->one->path);
Stephen Boyda8f68552019-04-26 16:51:57 -07006205
6206 if (stable)
6207 flush_one_hunk(oid, &ctx);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006208 }
6209
Stephen Boyda8f68552019-04-26 16:51:57 -07006210 if (!stable)
brian m. carlson5951bf42021-04-26 01:02:53 +00006211 the_hash_algo->final_oid_fn(oid, &ctx);
Stephen Boyda8f68552019-04-26 16:51:57 -07006212
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006213 return 0;
6214}
6215
Stephen Boyda8f68552019-04-26 16:51:57 -07006216int 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 +02006217{
6218 struct diff_queue_struct *q = &diff_queued_diff;
6219 int i;
Stephen Boyda8f68552019-04-26 16:51:57 -07006220 int result = diff_get_patch_id(options, oid, diff_header_only, stable);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006221
6222 for (i = 0; i < q->nr; i++)
6223 diff_free_filepair(q->queue[i]);
6224
6225 free(q->queue);
Bo Yang9ca5df92010-05-06 21:52:27 -07006226 DIFF_QUEUE_CLEAR(q);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006227
6228 return result;
6229}
6230
Timo Hirvonen946c3782006-06-27 15:09:17 +03006231static int is_summary_empty(const struct diff_queue_struct *q)
6232{
6233 int i;
6234
6235 for (i = 0; i < q->nr; i++) {
6236 const struct diff_filepair *p = q->queue[i];
6237
6238 switch (p->status) {
6239 case DIFF_STATUS_DELETED:
6240 case DIFF_STATUS_ADDED:
6241 case DIFF_STATUS_COPIED:
6242 case DIFF_STATUS_RENAMED:
6243 return 0;
6244 default:
6245 if (p->score)
6246 return 0;
6247 if (p->one->mode && p->two->mode &&
6248 p->one->mode != p->two->mode)
6249 return 0;
6250 break;
6251 }
6252 }
6253 return 1;
6254}
6255
Junio C Hamanof31027c2011-01-06 13:50:06 -08006256static const char rename_limit_warning[] =
Elijah Newren05d2c612021-07-15 00:45:21 +00006257N_("exhaustive rename detection was skipped due to too many files.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08006258
6259static const char degrade_cc_to_c_warning[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00006260N_("only found copies from modified paths due to too many files.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08006261
6262static const char rename_limit_advice[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00006263N_("you may want to set your %s variable to at least "
6264 "%d and retry the command.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08006265
6266void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)
6267{
Nguyễn Thái Ngọc Duy4e056c92018-01-16 16:23:49 +07006268 fflush(stdout);
Junio C Hamanof31027c2011-01-06 13:50:06 -08006269 if (degraded_cc)
Vasco Almeidadb424972016-10-17 13:15:29 +00006270 warning(_(degrade_cc_to_c_warning));
Junio C Hamanof31027c2011-01-06 13:50:06 -08006271 else if (needed)
Vasco Almeidadb424972016-10-17 13:15:29 +00006272 warning(_(rename_limit_warning));
Junio C Hamanof31027c2011-01-06 13:50:06 -08006273 else
6274 return;
Elijah Newren9f7e4bf2017-11-13 12:15:59 -08006275 if (0 < needed)
Vasco Almeidadb424972016-10-17 13:15:29 +00006276 warning(_(rename_limit_advice), varname, needed);
Junio C Hamanof31027c2011-01-06 13:50:06 -08006277}
6278
Stefan Bellerec331502017-06-29 17:06:48 -07006279static void diff_flush_patch_all_file_pairs(struct diff_options *o)
6280{
6281 int i;
Stefan Bellere6e045f2017-06-29 17:07:06 -07006282 static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT;
Stefan Bellerec331502017-06-29 17:06:48 -07006283 struct diff_queue_struct *q = &diff_queued_diff;
Stefan Beller091f8e22017-06-29 17:06:53 -07006284
6285 if (WSEH_NEW & WS_RULE_MASK)
Johannes Schindelin033abf92018-05-02 11:38:39 +02006286 BUG("WS rules bit mask overlaps with diff symbol flags");
Stefan Beller091f8e22017-06-29 17:06:53 -07006287
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006288 if (o->color_moved)
6289 o->emitted_symbols = &esm;
Stefan Bellere6e045f2017-06-29 17:07:06 -07006290
Stefan Bellerec331502017-06-29 17:06:48 -07006291 for (i = 0; i < q->nr; i++) {
6292 struct diff_filepair *p = q->queue[i];
6293 if (check_pair_status(p))
6294 diff_flush_patch(p, o);
6295 }
Stefan Bellere6e045f2017-06-29 17:07:06 -07006296
6297 if (o->emitted_symbols) {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006298 if (o->color_moved) {
Phillip Wood72962e82021-12-09 10:30:09 +00006299 struct mem_pool entry_pool;
6300 struct moved_entry_list *entry_list;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006301
Phillip Wood72962e82021-12-09 10:30:09 +00006302 mem_pool_init(&entry_pool, 1024 * 1024);
6303 entry_list = add_lines_to_move_detection(o,
6304 &entry_pool);
6305 mark_color_as_moved(o, entry_list);
Stefan Beller86b452e2017-06-30 13:53:09 -07006306 if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
6307 dim_moved_lines(o);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006308
Phillip Wood72962e82021-12-09 10:30:09 +00006309 mem_pool_discard(&entry_pool, 0);
6310 free(entry_list);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006311 }
6312
Stefan Bellere6e045f2017-06-29 17:07:06 -07006313 for (i = 0; i < esm.nr; i++)
6314 emit_diff_symbol_from_struct(o, &esm.buf[i]);
6315
6316 for (i = 0; i < esm.nr; i++)
6317 free((void *)esm.buf[i].line);
Jeff King48edf3a2019-01-24 07:32:41 -05006318 esm.nr = 0;
6319
6320 o->emitted_symbols = NULL;
Stefan Bellere6e045f2017-06-29 17:07:06 -07006321 }
Stefan Bellerec331502017-06-29 17:06:48 -07006322}
6323
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006324static void diff_free_file(struct diff_options *options)
6325{
6326 if (options->close_file)
6327 fclose(options->file);
6328}
6329
Ævar Arnfjörð Bjarmasonc45dc9c2021-02-11 11:45:35 +01006330static void diff_free_ignore_regex(struct diff_options *options)
6331{
6332 int i;
6333
6334 for (i = 0; i < options->ignore_regex_nr; i++) {
6335 regfree(options->ignore_regex[i]);
6336 free(options->ignore_regex[i]);
6337 }
6338 free(options->ignore_regex);
6339}
6340
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006341void diff_free(struct diff_options *options)
6342{
6343 if (options->no_free)
6344 return;
6345
6346 diff_free_file(options);
Ævar Arnfjörð Bjarmasonc45dc9c2021-02-11 11:45:35 +01006347 diff_free_ignore_regex(options);
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006348}
6349
Junio C Hamano6973dca2006-04-21 23:57:45 -07006350void diff_flush(struct diff_options *options)
6351{
6352 struct diff_queue_struct *q = &diff_queued_diff;
Timo Hirvonenc6744342006-06-24 20:21:53 +03006353 int i, output_format = options->output_format;
Timo Hirvonen946c3782006-06-27 15:09:17 +03006354 int separator = 0;
Johan Herland1c57a622011-04-29 11:36:21 +02006355 int dirstat_by_line = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006356
Timo Hirvonenc6744342006-06-24 20:21:53 +03006357 /*
6358 * Order: raw, stat, summary, patch
6359 * or: name/name-status/checkdiff (other bits clear)
6360 */
Timo Hirvonen946c3782006-06-27 15:09:17 +03006361 if (!q->nr)
6362 goto free_queue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006363
Timo Hirvonenc6744342006-06-24 20:21:53 +03006364 if (output_format & (DIFF_FORMAT_RAW |
6365 DIFF_FORMAT_NAME |
6366 DIFF_FORMAT_NAME_STATUS |
6367 DIFF_FORMAT_CHECKDIFF)) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07006368 for (i = 0; i < q->nr; i++) {
6369 struct diff_filepair *p = q->queue[i];
Timo Hirvonenc6744342006-06-24 20:21:53 +03006370 if (check_pair_status(p))
6371 flush_one_pair(p, options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006372 }
Timo Hirvonen946c3782006-06-27 15:09:17 +03006373 separator++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006374 }
Timo Hirvonenc6744342006-06-24 20:21:53 +03006375
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006376 if (output_format & DIFF_FORMAT_DIRSTAT && options->flags.dirstat_by_line)
Johan Herland1c57a622011-04-29 11:36:21 +02006377 dirstat_by_line = 1;
6378
6379 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT) ||
6380 dirstat_by_line) {
Timo Hirvonen5e2b0632006-06-25 14:28:19 +03006381 struct diffstat_t diffstat;
Timo Hirvonenc6744342006-06-24 20:21:53 +03006382
Daniel Ferreirae4cb6592019-11-13 12:40:58 +00006383 compute_diffstat(options, &diffstat, q);
Junio C Hamano74e2abe2006-10-12 03:01:00 -07006384 if (output_format & DIFF_FORMAT_NUMSTAT)
6385 show_numstat(&diffstat, options);
6386 if (output_format & DIFF_FORMAT_DIFFSTAT)
6387 show_stats(&diffstat, options);
Junio C Hamanof6046522007-12-11 23:46:30 -08006388 if (output_format & DIFF_FORMAT_SHORTSTAT)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04006389 show_shortstats(&diffstat, options);
Mårten Kongstadab273892015-03-02 16:05:39 +01006390 if (output_format & DIFF_FORMAT_DIRSTAT && dirstat_by_line)
Johan Herland1c57a622011-04-29 11:36:21 +02006391 show_dirstat_by_line(&diffstat, options);
Junio C Hamanof6046522007-12-11 23:46:30 -08006392 free_diffstat_info(&diffstat);
Junio C Hamano3969cf72006-06-27 15:08:19 -07006393 separator++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006394 }
Johan Herland1c57a622011-04-29 11:36:21 +02006395 if ((output_format & DIFF_FORMAT_DIRSTAT) && !dirstat_by_line)
Junio C Hamanoc04a7152008-02-12 17:06:58 -08006396 show_dirstat(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006397
Timo Hirvonen946c3782006-06-27 15:09:17 +03006398 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
Bo Yang7be57612010-05-26 15:23:54 +08006399 for (i = 0; i < q->nr; i++) {
6400 diff_summary(options, q->queue[i]);
6401 }
Junio C Hamano3969cf72006-06-27 15:08:19 -07006402 separator++;
Sean4bbd2612006-05-14 08:13:49 -04006403 }
6404
Larry D'Anna6977c252010-02-16 01:55:21 -05006405 if (output_format & DIFF_FORMAT_NO_OUTPUT &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006406 options->flags.exit_with_status &&
6407 options->flags.diff_from_contents) {
Larry D'Anna6977c252010-02-16 01:55:21 -05006408 /*
6409 * run diff_flush_patch for the exit status. setting
Ondřej Bílka749f7632013-07-22 23:02:23 +02006410 * options->file to /dev/null should be safe, because we
Larry D'Anna6977c252010-02-16 01:55:21 -05006411 * aren't supposed to produce any output anyway.
6412 */
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006413 diff_free_file(options);
Nguyễn Thái Ngọc Duy23a9e072017-05-03 17:16:46 +07006414 options->file = xfopen("/dev/null", "w");
Larry D'Anna6977c252010-02-16 01:55:21 -05006415 options->close_file = 1;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006416 options->color_moved = 0;
Larry D'Anna6977c252010-02-16 01:55:21 -05006417 for (i = 0; i < q->nr; i++) {
6418 struct diff_filepair *p = q->queue[i];
6419 if (check_pair_status(p))
6420 diff_flush_patch(p, options);
6421 if (options->found_changes)
6422 break;
6423 }
6424 }
6425
Timo Hirvonenc6744342006-06-24 20:21:53 +03006426 if (output_format & DIFF_FORMAT_PATCH) {
Timo Hirvonen946c3782006-06-27 15:09:17 +03006427 if (separator) {
Stefan Beller091f8e22017-06-29 17:06:53 -07006428 emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0);
Stefan Beller30b7e1e2017-06-29 17:07:04 -07006429 if (options->stat_sep)
Timo Hirvonen946c3782006-06-27 15:09:17 +03006430 /* attach patch instead of inline */
Stefan Beller30b7e1e2017-06-29 17:07:04 -07006431 emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP,
6432 NULL, 0, 0);
Timo Hirvonenc6744342006-06-24 20:21:53 +03006433 }
6434
Stefan Bellerec331502017-06-29 17:06:48 -07006435 diff_flush_patch_all_file_pairs(options);
Timo Hirvonenc6744342006-06-24 20:21:53 +03006436 }
6437
Jeff King04245582006-09-07 02:35:42 -04006438 if (output_format & DIFF_FORMAT_CALLBACK)
6439 options->format_callback(q, options, options->format_callback_data);
6440
Timo Hirvonenc6744342006-06-24 20:21:53 +03006441 for (i = 0; i < q->nr; i++)
6442 diff_free_filepair(q->queue[i]);
Timo Hirvonen946c3782006-06-27 15:09:17 +03006443free_queue:
Junio C Hamano6973dca2006-04-21 23:57:45 -07006444 free(q->queue);
Bo Yang9ca5df92010-05-06 21:52:27 -07006445 DIFF_QUEUE_CLEAR(q);
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006446 diff_free(options);
Junio C Hamanof2451942009-05-22 12:45:29 -07006447
6448 /*
Jim Meyering97bf2a02009-08-30 22:27:02 +02006449 * Report the content-level differences with HAS_CHANGES;
Junio C Hamanof2451942009-05-22 12:45:29 -07006450 * diff_addremove/diff_change does not set the bit when
6451 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
6452 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006453 if (options->flags.diff_from_contents) {
Junio C Hamanof2451942009-05-22 12:45:29 -07006454 if (options->found_changes)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006455 options->flags.has_changes = 1;
Junio C Hamanof2451942009-05-22 12:45:29 -07006456 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006457 options->flags.has_changes = 0;
Junio C Hamanof2451942009-05-22 12:45:29 -07006458 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07006459}
6460
Junio C Hamano08578fa2013-07-17 15:09:34 -07006461static int match_filter(const struct diff_options *options, const struct diff_filepair *p)
6462{
6463 return (((p->status == DIFF_STATUS_MODIFIED) &&
6464 ((p->score &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006465 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN, options)) ||
Junio C Hamano08578fa2013-07-17 15:09:34 -07006466 (!p->score &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006467 filter_bit_tst(DIFF_STATUS_MODIFIED, options)))) ||
Junio C Hamano08578fa2013-07-17 15:09:34 -07006468 ((p->status != DIFF_STATUS_MODIFIED) &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006469 filter_bit_tst(p->status, options)));
Junio C Hamano08578fa2013-07-17 15:09:34 -07006470}
6471
Junio C Hamano949226f2013-07-17 14:19:24 -07006472static void diffcore_apply_filter(struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006473{
6474 int i;
6475 struct diff_queue_struct *q = &diff_queued_diff;
6476 struct diff_queue_struct outq;
Junio C Hamano949226f2013-07-17 14:19:24 -07006477
Bo Yang9ca5df92010-05-06 21:52:27 -07006478 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006479
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006480 if (!options->filter)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006481 return;
6482
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006483 if (filter_bit_tst(DIFF_STATUS_FILTER_AON, options)) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07006484 int found;
6485 for (i = found = 0; !found && i < q->nr; i++) {
Junio C Hamano08578fa2013-07-17 15:09:34 -07006486 if (match_filter(options, q->queue[i]))
Junio C Hamano6973dca2006-04-21 23:57:45 -07006487 found++;
6488 }
6489 if (found)
6490 return;
6491
6492 /* otherwise we will clear the whole queue
6493 * by copying the empty outq at the end of this
6494 * function, but first clear the current entries
6495 * in the queue.
6496 */
6497 for (i = 0; i < q->nr; i++)
6498 diff_free_filepair(q->queue[i]);
6499 }
6500 else {
6501 /* Only the matching ones */
6502 for (i = 0; i < q->nr; i++) {
6503 struct diff_filepair *p = q->queue[i];
Junio C Hamano08578fa2013-07-17 15:09:34 -07006504 if (match_filter(options, p))
Junio C Hamano6973dca2006-04-21 23:57:45 -07006505 diff_q(&outq, p);
6506 else
6507 diff_free_filepair(p);
6508 }
6509 }
6510 free(q->queue);
6511 *q = outq;
6512}
6513
Sven Verdoolaege57011152007-09-08 12:30:22 +02006514/* Check whether two filespecs with the same mode and size are identical */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006515static int diff_filespec_is_identical(struct repository *r,
6516 struct diff_filespec *one,
Sven Verdoolaege57011152007-09-08 12:30:22 +02006517 struct diff_filespec *two)
6518{
Junio C Hamano2b459b42008-03-02 00:07:59 -08006519 if (S_ISGITLINK(one->mode))
6520 return 0;
Jonathan Tan1c37e862020-04-07 15:11:41 -07006521 if (diff_populate_filespec(r, one, NULL))
Sven Verdoolaege57011152007-09-08 12:30:22 +02006522 return 0;
Jonathan Tan1c37e862020-04-07 15:11:41 -07006523 if (diff_populate_filespec(r, two, NULL))
Sven Verdoolaege57011152007-09-08 12:30:22 +02006524 return 0;
6525 return !memcmp(one->data, two->data, one->size);
6526}
6527
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006528static int diff_filespec_check_stat_unmatch(struct repository *r,
6529 struct diff_filepair *p)
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006530{
Jonathan Tan1c37e862020-04-07 15:11:41 -07006531 struct diff_populate_filespec_options dpf_options = {
6532 .check_size_only = 1,
Jonathan Tan95acf112020-04-07 15:11:43 -07006533 .missing_object_cb = diff_queued_diff_prefetch,
6534 .missing_object_data = r,
Jonathan Tan1c37e862020-04-07 15:11:41 -07006535 };
6536
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006537 if (p->done_skip_stat_unmatch)
6538 return p->skip_stat_unmatch_result;
6539
6540 p->done_skip_stat_unmatch = 1;
6541 p->skip_stat_unmatch_result = 0;
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006542 /*
6543 * 1. Entries that come from stat info dirtiness
6544 * always have both sides (iow, not create/delete),
6545 * one side of the object name is unknown, with
6546 * the same mode and size. Keep the ones that
6547 * do not match these criteria. They have real
6548 * differences.
6549 *
6550 * 2. At this point, the file is known to be modified,
6551 * with the same mode and size, and the object
6552 * name of one side is unknown. Need to inspect
6553 * the identical contents.
6554 */
6555 if (!DIFF_FILE_VALID(p->one) || /* (1) */
6556 !DIFF_FILE_VALID(p->two) ||
brian m. carlson41c95602016-06-24 23:09:24 +00006557 (p->one->oid_valid && p->two->oid_valid) ||
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006558 (p->one->mode != p->two->mode) ||
Jonathan Tan1c37e862020-04-07 15:11:41 -07006559 diff_populate_filespec(r, p->one, &dpf_options) ||
6560 diff_populate_filespec(r, p->two, &dpf_options) ||
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006561 (p->one->size != p->two->size) ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006562 !diff_filespec_is_identical(r, p->one, p->two)) /* (2) */
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006563 p->skip_stat_unmatch_result = 1;
6564 return p->skip_stat_unmatch_result;
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006565}
6566
Junio C Hamanofb132272007-08-03 13:33:31 -07006567static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
6568{
6569 int i;
6570 struct diff_queue_struct *q = &diff_queued_diff;
6571 struct diff_queue_struct outq;
Bo Yang9ca5df92010-05-06 21:52:27 -07006572 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamanofb132272007-08-03 13:33:31 -07006573
6574 for (i = 0; i < q->nr; i++) {
6575 struct diff_filepair *p = q->queue[i];
6576
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006577 if (diff_filespec_check_stat_unmatch(diffopt->repo, p))
Junio C Hamanofb132272007-08-03 13:33:31 -07006578 diff_q(&outq, p);
6579 else {
6580 /*
6581 * The caller can subtract 1 from skip_stat_unmatch
6582 * to determine how many paths were dirty only
6583 * due to stat info mismatch.
6584 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006585 if (!diffopt->flags.no_index)
René Scharfe6d2d9e82007-08-15 00:41:00 +02006586 diffopt->skip_stat_unmatch++;
Junio C Hamanofb132272007-08-03 13:33:31 -07006587 diff_free_filepair(p);
6588 }
6589 }
6590 free(q->queue);
6591 *q = outq;
6592}
6593
Junio C Hamano730f7282009-09-20 00:03:39 -07006594static int diffnamecmp(const void *a_, const void *b_)
6595{
6596 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
6597 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
6598 const char *name_a, *name_b;
6599
6600 name_a = a->one ? a->one->path : a->two->path;
6601 name_b = b->one ? b->one->path : b->two->path;
6602 return strcmp(name_a, name_b);
6603}
6604
Jeff King784c0da2019-02-14 00:48:03 -05006605void diffcore_fix_diff_index(void)
Junio C Hamano730f7282009-09-20 00:03:39 -07006606{
6607 struct diff_queue_struct *q = &diff_queued_diff;
René Scharfe9ed0d8d2016-09-29 17:27:31 +02006608 QSORT(q->queue, q->nr, diffnamecmp);
Junio C Hamano730f7282009-09-20 00:03:39 -07006609}
6610
Jonathan Tan95acf112020-04-07 15:11:43 -07006611void diff_add_if_missing(struct repository *r,
6612 struct oid_array *to_fetch,
6613 const struct diff_filespec *filespec)
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006614{
6615 if (filespec && filespec->oid_valid &&
Jonathan Tana63694f2019-08-20 13:53:20 -07006616 !S_ISGITLINK(filespec->mode) &&
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006617 oid_object_info_extended(r, &filespec->oid, NULL,
6618 OBJECT_INFO_FOR_PREFETCH))
6619 oid_array_append(to_fetch, &filespec->oid);
6620}
6621
Jonathan Tan95acf112020-04-07 15:11:43 -07006622void diff_queued_diff_prefetch(void *repository)
6623{
6624 struct repository *repo = repository;
6625 int i;
6626 struct diff_queue_struct *q = &diff_queued_diff;
6627 struct oid_array to_fetch = OID_ARRAY_INIT;
6628
6629 for (i = 0; i < q->nr; i++) {
6630 struct diff_filepair *p = q->queue[i];
6631 diff_add_if_missing(repo, &to_fetch, p->one);
6632 diff_add_if_missing(repo, &to_fetch, p->two);
6633 }
6634
6635 /*
6636 * NEEDSWORK: Consider deduplicating the OIDs sent.
6637 */
6638 promisor_remote_get_direct(repo, to_fetch.oid, to_fetch.nr);
6639
6640 oid_array_clear(&to_fetch);
6641}
6642
Junio C Hamano6973dca2006-04-21 23:57:45 -07006643void diffcore_std(struct diff_options *options)
6644{
Jonathan Tan95acf112020-04-07 15:11:43 -07006645 int output_formats_to_prefetch = DIFF_FORMAT_DIFFSTAT |
6646 DIFF_FORMAT_NUMSTAT |
6647 DIFF_FORMAT_PATCH |
6648 DIFF_FORMAT_SHORTSTAT |
6649 DIFF_FORMAT_DIRSTAT;
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006650
Jonathan Tan95acf112020-04-07 15:11:43 -07006651 /*
6652 * Check if the user requested a blob-data-requiring diff output and/or
6653 * break-rewrite detection (which requires blob data). If yes, prefetch
6654 * the diff pairs.
6655 *
6656 * If no prefetching occurs, diffcore_rename() will prefetch if it
6657 * decides that it needs inexact rename detection.
6658 */
6659 if (options->repo == the_repository && has_promisor_remote() &&
6660 (options->output_format & output_formats_to_prefetch ||
6661 options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
6662 diff_queued_diff_prefetch(options->repo);
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006663
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04006664 /* NOTE please keep the following in sync with diff_tree_combined() */
Junio C Hamano9d865352008-09-06 19:09:16 -07006665 if (options->skip_stat_unmatch)
Junio C Hamanofb132272007-08-03 13:33:31 -07006666 diffcore_skip_stat_unmatch(options);
Junio C Hamano44c48a92010-08-13 12:17:45 -07006667 if (!options->found_follow) {
6668 /* See try_to_follow_renames() in tree-diff.c */
6669 if (options->break_opt != -1)
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006670 diffcore_break(options->repo,
6671 options->break_opt);
Junio C Hamano44c48a92010-08-13 12:17:45 -07006672 if (options->detect_rename)
6673 diffcore_rename(options);
6674 if (options->break_opt != -1)
6675 diffcore_merge_broken();
6676 }
Stefan Bellercf630512018-01-04 14:50:41 -08006677 if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
Junio C Hamano382f0132010-08-31 13:44:39 -07006678 diffcore_pickaxe(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006679 if (options->orderfile)
6680 diffcore_order(options->orderfile);
Junio C Hamano1eb41362021-02-11 11:57:50 -08006681 if (options->rotate_to)
6682 diffcore_rotate(options);
Junio C Hamano44c48a92010-08-13 12:17:45 -07006683 if (!options->found_follow)
6684 /* See try_to_follow_renames() in tree-diff.c */
6685 diff_resolve_rename_copy();
Junio C Hamano949226f2013-07-17 14:19:24 -07006686 diffcore_apply_filter(options);
Junio C Hamano68aacb22007-03-14 11:12:13 -07006687
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006688 if (diff_queued_diff.nr && !options->flags.diff_from_contents)
6689 options->flags.has_changes = 1;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01006690 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006691 options->flags.has_changes = 0;
Bo Yang1da61752010-05-06 21:52:28 -07006692
Junio C Hamano44c48a92010-08-13 12:17:45 -07006693 options->found_follow = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006694}
6695
Junio C Hamanoda31b352007-12-13 23:40:27 -08006696int diff_result_code(struct diff_options *opt, int status)
6697{
6698 int result = 0;
Junio C Hamanof31027c2011-01-06 13:50:06 -08006699
Max Nanasyc9fc4412013-03-21 12:53:38 -07006700 diff_warn_rename_limit("diff.renameLimit",
Junio C Hamanof31027c2011-01-06 13:50:06 -08006701 opt->needed_rename_limit,
6702 opt->degraded_cc_to_c);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006703 if (!opt->flags.exit_with_status &&
Junio C Hamanoda31b352007-12-13 23:40:27 -08006704 !(opt->output_format & DIFF_FORMAT_CHECKDIFF))
6705 return status;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006706 if (opt->flags.exit_with_status &&
6707 opt->flags.has_changes)
Junio C Hamanoda31b352007-12-13 23:40:27 -08006708 result |= 01;
6709 if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006710 opt->flags.check_failed)
Junio C Hamanoda31b352007-12-13 23:40:27 -08006711 result |= 02;
6712 return result;
6713}
Junio C Hamano6973dca2006-04-21 23:57:45 -07006714
Junio C Hamano28b92642011-05-31 09:14:17 -07006715int diff_can_quit_early(struct diff_options *opt)
6716{
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006717 return (opt->flags.quick &&
Junio C Hamano28b92642011-05-31 09:14:17 -07006718 !opt->filter &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006719 opt->flags.has_changes);
Junio C Hamano28b92642011-05-31 09:14:17 -07006720}
6721
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006722/*
6723 * Shall changes to this submodule be ignored?
6724 *
6725 * Submodule changes can be configured to be ignored separately for each path,
6726 * but that configuration can be overridden from the command line.
6727 */
6728static int is_submodule_ignored(const char *path, struct diff_options *options)
6729{
6730 int ignored = 0;
Brandon Williams02f2f562017-10-31 11:19:05 -07006731 struct diff_flags orig_flags = options->flags;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006732 if (!options->flags.override_submodule_config)
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006733 set_diffopt_flags_from_submodule_config(options, path);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006734 if (options->flags.ignore_submodules)
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006735 ignored = 1;
6736 options->flags = orig_flags;
6737 return ignored;
6738}
6739
Daniel Ferreirae4cb6592019-11-13 12:40:58 +00006740void compute_diffstat(struct diff_options *options,
6741 struct diffstat_t *diffstat,
6742 struct diff_queue_struct *q)
6743{
6744 int i;
6745
6746 memset(diffstat, 0, sizeof(struct diffstat_t));
6747 for (i = 0; i < q->nr; i++) {
6748 struct diff_filepair *p = q->queue[i];
6749 if (check_pair_status(p))
6750 diff_flush_stat(p, options, diffstat);
6751 }
6752}
6753
Junio C Hamano6973dca2006-04-21 23:57:45 -07006754void diff_addremove(struct diff_options *options,
6755 int addremove, unsigned mode,
Brandon Williamsc26022e2017-05-30 10:30:47 -07006756 const struct object_id *oid,
6757 int oid_valid,
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006758 const char *concatpath, unsigned dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006759{
Junio C Hamano6973dca2006-04-21 23:57:45 -07006760 struct diff_filespec *one, *two;
6761
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006762 if (S_ISGITLINK(mode) && is_submodule_ignored(concatpath, options))
Johannes Schindelin50fd9bd2008-05-14 18:03:31 +01006763 return;
6764
Junio C Hamano6973dca2006-04-21 23:57:45 -07006765 /* This may look odd, but it is a preparation for
6766 * feeding "there are unchanged files which should
6767 * not produce diffs, but when you are doing copy
6768 * detection you would need them, so here they are"
6769 * entries to the diff-core. They will be prefixed
6770 * with something like '=' or '*' (I haven't decided
6771 * which but should not make any difference).
Junio C Hamanoa6080a02007-06-07 00:04:01 -07006772 * Feeding the same new and old to diff_change()
Junio C Hamano6973dca2006-04-21 23:57:45 -07006773 * also has the same effect.
6774 * Before the final output happens, they are pruned after
6775 * merged into rename/copy pairs as appropriate.
6776 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006777 if (options->flags.reverse_diff)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006778 addremove = (addremove == '+' ? '-' :
6779 addremove == '-' ? '+' : addremove);
6780
Junio C Hamanocd676a52008-02-12 14:26:02 -08006781 if (options->prefix &&
6782 strncmp(concatpath, options->prefix, options->prefix_length))
6783 return;
6784
Junio C Hamano6973dca2006-04-21 23:57:45 -07006785 one = alloc_filespec(concatpath);
6786 two = alloc_filespec(concatpath);
6787
6788 if (addremove != '+')
Brandon Williamsf9704c22017-05-30 10:30:50 -07006789 fill_filespec(one, oid, oid_valid, mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006790 if (addremove != '-') {
Brandon Williamsf9704c22017-05-30 10:30:50 -07006791 fill_filespec(two, oid, oid_valid, mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006792 two->dirty_submodule = dirty_submodule;
6793 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07006794
6795 diff_queue(&diff_queued_diff, one, two);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006796 if (!options->flags.diff_from_contents)
6797 options->flags.has_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006798}
6799
6800void diff_change(struct diff_options *options,
6801 unsigned old_mode, unsigned new_mode,
Brandon Williams94a00972017-05-30 10:30:49 -07006802 const struct object_id *old_oid,
6803 const struct object_id *new_oid,
6804 int old_oid_valid, int new_oid_valid,
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006805 const char *concatpath,
6806 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006807{
Junio C Hamano6973dca2006-04-21 23:57:45 -07006808 struct diff_filespec *one, *two;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006809 struct diff_filepair *p;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006810
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006811 if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) &&
6812 is_submodule_ignored(concatpath, options))
Johannes Schindelin50fd9bd2008-05-14 18:03:31 +01006813 return;
6814
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006815 if (options->flags.reverse_diff) {
René Scharfe35d803b2017-01-28 22:40:58 +01006816 SWAP(old_mode, new_mode);
Brandon Williams94a00972017-05-30 10:30:49 -07006817 SWAP(old_oid, new_oid);
6818 SWAP(old_oid_valid, new_oid_valid);
René Scharfe35d803b2017-01-28 22:40:58 +01006819 SWAP(old_dirty_submodule, new_dirty_submodule);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006820 }
Junio C Hamanocd676a52008-02-12 14:26:02 -08006821
6822 if (options->prefix &&
6823 strncmp(concatpath, options->prefix, options->prefix_length))
6824 return;
6825
Junio C Hamano6973dca2006-04-21 23:57:45 -07006826 one = alloc_filespec(concatpath);
6827 two = alloc_filespec(concatpath);
Brandon Williamsf9704c22017-05-30 10:30:50 -07006828 fill_filespec(one, old_oid, old_oid_valid, old_mode);
6829 fill_filespec(two, new_oid, new_oid_valid, new_mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01006830 one->dirty_submodule = old_dirty_submodule;
6831 two->dirty_submodule = new_dirty_submodule;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006832 p = diff_queue(&diff_queued_diff, one, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006833
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006834 if (options->flags.diff_from_contents)
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006835 return;
6836
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006837 if (options->flags.quick && options->skip_stat_unmatch &&
Jeff Kingd2d7fbe2020-06-01 16:22:18 -04006838 !diff_filespec_check_stat_unmatch(options->repo, p)) {
6839 diff_free_filespec_data(p->one);
6840 diff_free_filespec_data(p->two);
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006841 return;
Jeff Kingd2d7fbe2020-06-01 16:22:18 -04006842 }
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006843
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006844 options->flags.has_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006845}
6846
Junio C Hamanofa7b2902011-04-22 16:05:58 -07006847struct diff_filepair *diff_unmerge(struct diff_options *options, const char *path)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006848{
Junio C Hamano76399c02011-04-22 15:55:55 -07006849 struct diff_filepair *pair;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006850 struct diff_filespec *one, *two;
Junio C Hamanocd676a52008-02-12 14:26:02 -08006851
6852 if (options->prefix &&
6853 strncmp(path, options->prefix, options->prefix_length))
Junio C Hamano76399c02011-04-22 15:55:55 -07006854 return NULL;
Junio C Hamanocd676a52008-02-12 14:26:02 -08006855
Junio C Hamano6973dca2006-04-21 23:57:45 -07006856 one = alloc_filespec(path);
6857 two = alloc_filespec(path);
Junio C Hamano76399c02011-04-22 15:55:55 -07006858 pair = diff_queue(&diff_queued_diff, one, two);
6859 pair->is_unmerged = 1;
6860 return pair;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006861}
Jeff King9cb92c32008-10-05 17:43:45 -04006862
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006863static char *run_textconv(struct repository *r,
6864 const char *pgm,
6865 struct diff_filespec *spec,
6866 size_t *outsize)
Jeff King9cb92c32008-10-05 17:43:45 -04006867{
Jeff King479b0ae2009-01-22 00:59:56 -05006868 struct diff_tempfile *temp;
René Scharfed3180272014-08-19 21:09:35 +02006869 struct child_process child = CHILD_PROCESS_INIT;
Jeff King9cb92c32008-10-05 17:43:45 -04006870 struct strbuf buf = STRBUF_INIT;
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006871 int err = 0;
Jeff King9cb92c32008-10-05 17:43:45 -04006872
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006873 temp = prepare_temp_file(r, spec->path, spec);
Ævar Arnfjörð Bjarmason7f146092021-11-25 23:52:21 +01006874 strvec_push(&child.args, pgm);
6875 strvec_push(&child.args, temp->name);
Jeff King9cb92c32008-10-05 17:43:45 -04006876
Jeff King41a457e2009-12-30 06:01:09 -05006877 child.use_shell = 1;
Jeff King9cb92c32008-10-05 17:43:45 -04006878 child.out = -1;
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006879 if (start_command(&child)) {
Jeff King479b0ae2009-01-22 00:59:56 -05006880 remove_tempfile();
Jeff King9cb92c32008-10-05 17:43:45 -04006881 return NULL;
6882 }
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006883
6884 if (strbuf_read(&buf, child.out, 0) < 0)
6885 err = error("error reading from textconv command '%s'", pgm);
Jeff King70d70992009-12-30 04:02:53 -05006886 close(child.out);
Johannes Sixtda1fbed2010-03-30 19:36:03 +02006887
6888 if (finish_command(&child) || err) {
6889 strbuf_release(&buf);
6890 remove_tempfile();
6891 return NULL;
6892 }
Jeff King479b0ae2009-01-22 00:59:56 -05006893 remove_tempfile();
Jeff King9cb92c32008-10-05 17:43:45 -04006894
6895 return strbuf_detach(&buf, outsize);
6896}
Jeff King840383b2010-04-01 20:09:26 -04006897
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02006898size_t fill_textconv(struct repository *r,
6899 struct userdiff_driver *driver,
Axel Bonneta788d7d2010-06-07 17:23:36 +02006900 struct diff_filespec *df,
6901 char **outbuf)
Jeff King840383b2010-04-01 20:09:26 -04006902{
6903 size_t size;
6904
Jeff Kinga64e6a42016-02-22 13:28:54 -05006905 if (!driver) {
Jeff King840383b2010-04-01 20:09:26 -04006906 if (!DIFF_FILE_VALID(df)) {
6907 *outbuf = "";
6908 return 0;
6909 }
Jonathan Tan1c37e862020-04-07 15:11:41 -07006910 if (diff_populate_filespec(r, df, NULL))
Jeff King840383b2010-04-01 20:09:26 -04006911 die("unable to read files to diff");
6912 *outbuf = df->data;
6913 return df->size;
6914 }
6915
Jeff Kinga64e6a42016-02-22 13:28:54 -05006916 if (!driver->textconv)
Johannes Schindelin033abf92018-05-02 11:38:39 +02006917 BUG("fill_textconv called with non-textconv driver");
Jeff Kinga64e6a42016-02-22 13:28:54 -05006918
brian m. carlson41c95602016-06-24 23:09:24 +00006919 if (driver->textconv_cache && df->oid_valid) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00006920 *outbuf = notes_cache_get(driver->textconv_cache,
brian m. carlson569aa372017-05-06 22:09:58 +00006921 &df->oid,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006922 &size);
6923 if (*outbuf)
6924 return size;
6925 }
6926
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006927 *outbuf = run_textconv(r, driver->textconv, df, &size);
Jeff King840383b2010-04-01 20:09:26 -04006928 if (!*outbuf)
6929 die("unable to read files to diff");
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006930
brian m. carlson41c95602016-06-24 23:09:24 +00006931 if (driver->textconv_cache && df->oid_valid) {
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006932 /* ignore errors, as we might be in a readonly repository */
brian m. carlson569aa372017-05-06 22:09:58 +00006933 notes_cache_put(driver->textconv_cache, &df->oid, *outbuf,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04006934 size);
6935 /*
6936 * we could save up changes and flush them all at the end,
6937 * but we would need an extra call after all diffing is done.
6938 * Since generating a cache entry is the slow path anyway,
6939 * this extra overhead probably isn't a big deal.
6940 */
6941 notes_cache_write(driver->textconv_cache);
6942 }
6943
Jeff King840383b2010-04-01 20:09:26 -04006944 return size;
6945}
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07006946
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02006947int textconv_object(struct repository *r,
6948 const char *path,
Jeff Smith3a35cb22017-05-24 00:15:10 -05006949 unsigned mode,
6950 const struct object_id *oid,
6951 int oid_valid,
6952 char **buf,
6953 unsigned long *buf_size)
6954{
6955 struct diff_filespec *df;
6956 struct userdiff_driver *textconv;
6957
6958 df = alloc_filespec(path);
Junio C Hamanoa6f38c12017-06-19 12:38:44 -07006959 fill_filespec(df, oid, oid_valid, mode);
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01006960 textconv = get_textconv(r, df);
Jeff Smith3a35cb22017-05-24 00:15:10 -05006961 if (!textconv) {
6962 free_filespec(df);
6963 return 0;
6964 }
6965
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02006966 *buf_size = fill_textconv(r, textconv, df, buf);
Jeff Smith3a35cb22017-05-24 00:15:10 -05006967 free_filespec(df);
6968 return 1;
6969}
6970
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07006971void setup_diff_pager(struct diff_options *opt)
6972{
6973 /*
6974 * If the user asked for our exit code, then either they want --quiet
6975 * or --exit-code. We should definitely not bother with a pager in the
6976 * former case, as we will generate no output. Since we still properly
6977 * report our exit code even when a pager is run, we _could_ run a
6978 * pager with --exit-code. But since we have not done so historically,
6979 * and because it is easy to find people oneline advising "git diff
6980 * --exit-code" in hooks and other scripts, we do not do so.
6981 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006982 if (!opt->flags.exit_with_status &&
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07006983 check_pager_config("diff") != 0)
6984 setup_pager();
6985}