blob: 3c88c37908d6b13665ff7cd88abd46141485c891 [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"
Elijah Newren0b027f62023-03-21 06:25:58 +00005#include "abspath.h"
Elijah Newren36bf1952023-02-24 00:09:24 +00006#include "alloc.h"
Elijah Newren9b5041f2023-04-22 20:17:13 +00007#include "base85.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07008#include "config.h"
Elijah Newren73359a92023-04-11 03:00:40 +00009#include "convert.h"
Elijah Newren32a8f512023-03-21 06:26:03 +000010#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +000011#include "gettext.h"
Michael Haggerty284098f2015-08-12 19:12:01 +020012#include "tempfile.h"
Junio C Hamano6973dca2006-04-21 23:57:45 -070013#include "quote.h"
14#include "diff.h"
15#include "diffcore.h"
Junio C Hamano051308f2006-05-04 16:51:44 -070016#include "delta.h"
Elijah Newren41771fa2023-02-24 00:09:27 +000017#include "hex.h"
Junio C Hamano6973dca2006-04-21 23:57:45 -070018#include "xdiff-interface.h"
Jeff King7c92fe02006-09-08 04:03:18 -040019#include "color.h"
Junio C Hamano8c701242007-04-12 23:05:29 -070020#include "attr.h"
Johannes Sixtd5535ec2007-10-19 21:47:56 +020021#include "run-command.h"
Junio C Hamano23707812008-01-02 01:50:11 -080022#include "utf8.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070023#include "object-store.h"
Jeff Kingbe58e702008-10-05 17:43:21 -040024#include "userdiff.h"
Heiko Voigt851e18c2015-08-17 17:21:59 -070025#include "submodule-config.h"
Johannes Schindelin752c0c22009-10-19 14:38:32 +020026#include "submodule.h"
Stefan Beller2e2d5ac2017-06-30 13:53:07 -070027#include "hashmap.h"
Phillip Wood72962e82021-12-09 10:30:09 +000028#include "mem-pool.h"
Junio C Hamanoa757c642010-03-24 19:21:32 -070029#include "ll-merge.h"
Michael Haggerty02e8ca02012-10-28 17:50:54 +010030#include "string-list.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040031#include "strvec.h"
Jacob Keller660e1132016-08-31 16:27:20 -070032#include "graph.h"
Elijah Newren6f2d7432023-04-11 03:00:42 +000033#include "oid-array.h"
Jonathan Tan150e3002017-08-18 15:20:36 -070034#include "packfile.h"
Elijah Newrenca4eed72023-04-11 00:41:59 -070035#include "pager.h"
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +070036#include "parse-options.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +020037#include "help.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020038#include "promisor-remote.h"
Derrick Stoleead90da72021-09-08 11:23:56 +000039#include "dir.h"
Elijah Newren87bed172023-04-11 00:41:53 -070040#include "object-file.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070041#include "object-name.h"
Elijah Newrene38da482023-03-21 06:26:05 +000042#include "setup.h"
Elijah Newren95433ee2022-02-02 02:37:34 +000043#include "strmap.h"
Elijah Newren64122312023-04-22 20:17:16 +000044#include "ws.h"
Elijah Newrend5ebb502023-03-21 06:26:01 +000045#include "wrapper.h"
Junio C Hamano6973dca2006-04-21 23:57:45 -070046
Shawn O. Pearce1510fea2006-12-14 06:15:57 -050047#ifdef NO_FAST_WORKING_DIRECTORY
48#define FAST_WORKING_DIRECTORY 0
49#else
50#define FAST_WORKING_DIRECTORY 1
51#endif
52
David Rientjes96f1e582006-08-15 10:23:48 -070053static int diff_detect_rename_default;
Stefan Beller33de7162017-05-08 12:03:38 -040054static int diff_indent_heuristic = 1;
Elijah Newren94b82d52021-07-15 00:45:24 +000055static int diff_rename_limit_default = 1000;
Jim Meyeringa624eaa2008-08-15 13:39:26 +020056static int diff_suppress_blank_empty;
Junio C Hamanod2aea132012-09-15 13:59:59 -070057static int diff_use_color_default = -1;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -070058static int diff_color_moved_default;
Stefan Beller626c0b52018-07-18 12:31:56 -070059static int diff_color_moved_ws_default;
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -040060static int diff_context_default = 3;
Vegard Nossumc4888672017-01-12 13:21:11 +010061static int diff_interhunk_context_default;
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -060062static const char *diff_word_regex_cfg;
Johannes Schindelincbe02102007-12-17 13:42:20 +000063static const char *external_diff_cmd_cfg;
Samuel Bronson6d8940b2013-12-18 19:08:12 -050064static const char *diff_order_file_cfg;
Junio C Hamanoaecbf912007-08-31 13:13:42 -070065int diff_auto_refresh_index = 1;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -070066static int diff_mnemonic_prefix;
Eli Collinsf89504d2010-05-02 19:03:41 -070067static int diff_no_prefix;
Laurent Arnoudc28ded82020-05-22 12:46:18 +020068static int diff_relative;
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +010069static int diff_stat_graph_width;
Johan Herland712d2c72011-04-29 11:36:20 +020070static int diff_dirstat_permille_default = 30;
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +020071static struct diff_options default_diff_options;
Michal Privoznik07ab4de2013-01-16 08:51:57 +010072static long diff_algorithm;
Junio C Hamanoa17505f2016-10-04 15:26:27 -070073static unsigned ws_error_highlight_default = WSEH_NEW;
Junio C Hamano6973dca2006-04-21 23:57:45 -070074
Jeff King7c92fe02006-09-08 04:03:18 -040075static char diff_colors[][COLOR_MAXLEN] = {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010076 GIT_COLOR_RESET,
Jeff King8dbf3eb2015-05-27 16:48:46 -040077 GIT_COLOR_NORMAL, /* CONTEXT */
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010078 GIT_COLOR_BOLD, /* METAINFO */
79 GIT_COLOR_CYAN, /* FRAGINFO */
80 GIT_COLOR_RED, /* OLD */
81 GIT_COLOR_GREEN, /* NEW */
82 GIT_COLOR_YELLOW, /* COMMIT */
83 GIT_COLOR_BG_RED, /* WHITESPACE */
Bert Wesarg89cb73a2009-11-27 07:55:18 +010084 GIT_COLOR_NORMAL, /* FUNCINFO */
Stefan Beller86b452e2017-06-30 13:53:09 -070085 GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */
86 GIT_COLOR_BOLD_BLUE, /* OLD_MOVED ALTERNATIVE */
87 GIT_COLOR_FAINT, /* OLD_MOVED_DIM */
88 GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */
89 GIT_COLOR_BOLD_CYAN, /* NEW_MOVED */
90 GIT_COLOR_BOLD_YELLOW, /* NEW_MOVED ALTERNATIVE */
91 GIT_COLOR_FAINT, /* NEW_MOVED_DIM */
92 GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */
Johannes Schindelina7be92a2018-08-13 04:33:32 -070093 GIT_COLOR_FAINT, /* CONTEXT_DIM */
94 GIT_COLOR_FAINT_RED, /* OLD_DIM */
95 GIT_COLOR_FAINT_GREEN, /* NEW_DIM */
96 GIT_COLOR_BOLD, /* CONTEXT_BOLD */
97 GIT_COLOR_BOLD_RED, /* OLD_BOLD */
98 GIT_COLOR_BOLD_GREEN, /* NEW_BOLD */
Johannes Schindelincd112ce2006-06-13 18:45:44 +020099};
100
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200101static const char *color_diff_slots[] = {
102 [DIFF_CONTEXT] = "context",
103 [DIFF_METAINFO] = "meta",
104 [DIFF_FRAGINFO] = "frag",
105 [DIFF_FILE_OLD] = "old",
106 [DIFF_FILE_NEW] = "new",
107 [DIFF_COMMIT] = "commit",
108 [DIFF_WHITESPACE] = "whitespace",
109 [DIFF_FUNCINFO] = "func",
110 [DIFF_FILE_OLD_MOVED] = "oldMoved",
111 [DIFF_FILE_OLD_MOVED_ALT] = "oldMovedAlternative",
112 [DIFF_FILE_OLD_MOVED_DIM] = "oldMovedDimmed",
113 [DIFF_FILE_OLD_MOVED_ALT_DIM] = "oldMovedAlternativeDimmed",
114 [DIFF_FILE_NEW_MOVED] = "newMoved",
115 [DIFF_FILE_NEW_MOVED_ALT] = "newMovedAlternative",
116 [DIFF_FILE_NEW_MOVED_DIM] = "newMovedDimmed",
117 [DIFF_FILE_NEW_MOVED_ALT_DIM] = "newMovedAlternativeDimmed",
Johannes Schindelina7be92a2018-08-13 04:33:32 -0700118 [DIFF_CONTEXT_DIM] = "contextDimmed",
119 [DIFF_FILE_OLD_DIM] = "oldDimmed",
120 [DIFF_FILE_NEW_DIM] = "newDimmed",
121 [DIFF_CONTEXT_BOLD] = "contextBold",
122 [DIFF_FILE_OLD_BOLD] = "oldBold",
123 [DIFF_FILE_NEW_BOLD] = "newBold",
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200124};
125
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +0200126define_list_config_array_extra(color_diff_slots, {"plain"});
127
Jeff King9e1a5eb2014-06-18 15:41:50 -0400128static int parse_diff_color_slot(const char *var)
Junio C Hamano801235c2006-06-24 04:06:23 -0700129{
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200130 if (!strcasecmp(var, "plain"))
Jeff King8dbf3eb2015-05-27 16:48:46 -0400131 return DIFF_CONTEXT;
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200132 return LOOKUP_CONFIG(color_diff_slots, var);
Junio C Hamano801235c2006-06-24 04:06:23 -0700133}
134
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100135static int parse_dirstat_params(struct diff_options *options, const char *params_string,
Johan Herland51670fc2011-04-29 11:36:22 +0200136 struct strbuf *errmsg)
Johan Herland333f3fb2011-04-29 11:36:18 +0200137{
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100138 char *params_copy = xstrdup(params_string);
139 struct string_list params = STRING_LIST_INIT_NODUP;
140 int ret = 0;
141 int i;
Johan Herland51670fc2011-04-29 11:36:22 +0200142
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100143 if (*params_copy)
Taylor Blau52acddf2023-04-24 18:20:10 -0400144 string_list_split_in_place(&params, params_copy, ",", -1);
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100145 for (i = 0; i < params.nr; i++) {
146 const char *p = params.items[i].string;
147 if (!strcmp(p, "changes")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700148 options->flags.dirstat_by_line = 0;
149 options->flags.dirstat_by_file = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100150 } else if (!strcmp(p, "lines")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700151 options->flags.dirstat_by_line = 1;
152 options->flags.dirstat_by_file = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100153 } else if (!strcmp(p, "files")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700154 options->flags.dirstat_by_line = 0;
155 options->flags.dirstat_by_file = 1;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100156 } else if (!strcmp(p, "noncumulative")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700157 options->flags.dirstat_cumulative = 0;
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100158 } else if (!strcmp(p, "cumulative")) {
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700159 options->flags.dirstat_cumulative = 1;
Johan Herland333f3fb2011-04-29 11:36:18 +0200160 } else if (isdigit(*p)) {
161 char *end;
Johan Herland51670fc2011-04-29 11:36:22 +0200162 int permille = strtoul(p, &end, 10) * 10;
163 if (*end == '.' && isdigit(*++end)) {
Johan Herland712d2c72011-04-29 11:36:20 +0200164 /* only use first digit */
Johan Herland51670fc2011-04-29 11:36:22 +0200165 permille += *end - '0';
Johan Herland712d2c72011-04-29 11:36:20 +0200166 /* .. and ignore any further digits */
Johan Herland51670fc2011-04-29 11:36:22 +0200167 while (isdigit(*++end))
Johan Herland712d2c72011-04-29 11:36:20 +0200168 ; /* nothing */
169 }
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100170 if (!*end)
Johan Herland51670fc2011-04-29 11:36:22 +0200171 options->dirstat_permille = permille;
172 else {
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100173 strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
174 p);
Johan Herland51670fc2011-04-29 11:36:22 +0200175 ret++;
176 }
177 } else {
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100178 strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p);
Johan Herland51670fc2011-04-29 11:36:22 +0200179 ret++;
Johan Herland333f3fb2011-04-29 11:36:18 +0200180 }
Johan Herland51670fc2011-04-29 11:36:22 +0200181
Johan Herland333f3fb2011-04-29 11:36:18 +0200182 }
Michael Haggerty02e8ca02012-10-28 17:50:54 +0100183 string_list_clear(&params, 0);
184 free(params_copy);
Johan Herland51670fc2011-04-29 11:36:22 +0200185 return ret;
Johan Herland333f3fb2011-04-29 11:36:18 +0200186}
187
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530188static int parse_submodule_params(struct diff_options *options, const char *value)
189{
190 if (!strcmp(value, "log"))
Jacob Keller61cfbc02016-08-31 16:27:21 -0700191 options->submodule_format = DIFF_SUBMODULE_LOG;
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530192 else if (!strcmp(value, "short"))
Jacob Keller61cfbc02016-08-31 16:27:21 -0700193 options->submodule_format = DIFF_SUBMODULE_SHORT;
Jacob Kellerfd47ae62016-08-31 16:27:25 -0700194 else if (!strcmp(value, "diff"))
195 options->submodule_format = DIFF_SUBMODULE_INLINE_DIFF;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700196 /*
197 * Please update $__git_diff_submodule_formats in
198 * git-completion.bash when you add new formats.
199 */
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530200 else
201 return -1;
202 return 0;
203}
204
Ben Peart85b46032018-05-02 16:01:14 +0000205int git_config_rename(const char *var, const char *value)
Linus Torvaldscced5fb2009-04-09 11:46:15 -0700206{
207 if (!value)
208 return DIFF_DETECT_RENAME;
209 if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy"))
210 return DIFF_DETECT_COPY;
211 return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0;
212}
213
Michal Privoznik07924d42013-01-16 08:51:58 +0100214long parse_algorithm_value(const char *value)
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100215{
216 if (!value)
217 return -1;
218 else if (!strcasecmp(value, "myers") || !strcasecmp(value, "default"))
219 return 0;
220 else if (!strcasecmp(value, "minimal"))
221 return XDF_NEED_MINIMAL;
222 else if (!strcasecmp(value, "patience"))
223 return XDF_PATIENCE_DIFF;
224 else if (!strcasecmp(value, "histogram"))
225 return XDF_HISTOGRAM_DIFF;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700226 /*
227 * Please update $__git_diff_algorithms in git-completion.bash
228 * when you add new algorithms.
229 */
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100230 return -1;
231}
232
Junio C Hamano0b4b42e2016-10-04 15:09:18 -0700233static int parse_one_token(const char **arg, const char *token)
234{
235 const char *rest;
236 if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) {
237 *arg = rest;
238 return 1;
239 }
240 return 0;
241}
242
243static int parse_ws_error_highlight(const char *arg)
244{
245 const char *orig_arg = arg;
246 unsigned val = 0;
247
248 while (*arg) {
249 if (parse_one_token(&arg, "none"))
250 val = 0;
251 else if (parse_one_token(&arg, "default"))
252 val = WSEH_NEW;
253 else if (parse_one_token(&arg, "all"))
254 val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT;
255 else if (parse_one_token(&arg, "new"))
256 val |= WSEH_NEW;
257 else if (parse_one_token(&arg, "old"))
258 val |= WSEH_OLD;
259 else if (parse_one_token(&arg, "context"))
260 val |= WSEH_CONTEXT;
261 else {
262 return -1 - (int)(arg - orig_arg);
263 }
264 if (*arg)
265 arg++;
266 }
267 return val;
268}
269
Junio C Hamanoe0e324a2007-07-07 01:49:58 -0700270/*
Junio C Hamano83ad63c2006-07-08 01:05:16 -0700271 * These are to give UI layer defaults.
272 * The core-level commands such as git-diff-files should
273 * never be affected by the setting of diff.renames
274 * the user happens to have in the configuration file.
275 */
Matthieu Moy5404c112016-02-25 09:59:21 +0100276void init_diff_ui_defaults(void)
277{
Nguyễn Thái Ngọc Duy06dba2b2017-12-27 17:18:35 +0700278 diff_detect_rename_default = DIFF_DETECT_RENAME;
Matthieu Moy5404c112016-02-25 09:59:21 +0100279}
280
Jeff King783a86c2022-08-19 06:08:44 -0400281int git_diff_heuristic_config(const char *var, const char *value,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200282 void *cb UNUSED)
Michael Haggerty5b162872016-09-05 11:44:53 +0200283{
Junio C Hamano3cde4e02016-12-23 12:32:22 -0800284 if (!strcmp(var, "diff.indentheuristic"))
Michael Haggerty5b162872016-09-05 11:44:53 +0200285 diff_indent_heuristic = git_config_bool(var, value);
Michael Haggerty5b162872016-09-05 11:44:53 +0200286 return 0;
287}
288
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700289static int parse_color_moved(const char *arg)
290{
291 switch (git_parse_maybe_bool(arg)) {
292 case 0:
293 return COLOR_MOVED_NO;
294 case 1:
295 return COLOR_MOVED_DEFAULT;
296 default:
297 break;
298 }
299
300 if (!strcmp(arg, "no"))
301 return COLOR_MOVED_NO;
Stefan Beller176841f2017-06-30 13:53:08 -0700302 else if (!strcmp(arg, "plain"))
303 return COLOR_MOVED_PLAIN;
Stefan Beller51da15e2018-07-16 16:05:39 -0700304 else if (!strcmp(arg, "blocks"))
305 return COLOR_MOVED_BLOCKS;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700306 else if (!strcmp(arg, "zebra"))
307 return COLOR_MOVED_ZEBRA;
308 else if (!strcmp(arg, "default"))
309 return COLOR_MOVED_DEFAULT;
Eric Sunshinee3f2f5f2018-07-24 17:58:45 -0400310 else if (!strcmp(arg, "dimmed-zebra"))
311 return COLOR_MOVED_ZEBRA_DIM;
Stefan Beller86b452e2017-06-30 13:53:09 -0700312 else if (!strcmp(arg, "dimmed_zebra"))
313 return COLOR_MOVED_ZEBRA_DIM;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700314 else
Junio C Hamano706b0b52018-08-15 15:08:22 -0700315 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700316}
317
Stefan Bellerd173e792018-11-13 13:33:57 -0800318static unsigned parse_color_moved_ws(const char *arg)
Stefan Bellerb3095712018-07-16 16:05:40 -0700319{
320 int ret = 0;
321 struct string_list l = STRING_LIST_INIT_DUP;
322 struct string_list_item *i;
323
324 string_list_split(&l, arg, ',', -1);
325
326 for_each_string_list_item(i, &l) {
327 struct strbuf sb = STRBUF_INIT;
328 strbuf_addstr(&sb, i->string);
329 strbuf_trim(&sb);
330
Phillip Woodb73bcba2018-11-23 11:16:52 +0000331 if (!strcmp(sb.buf, "no"))
332 ret = 0;
333 else if (!strcmp(sb.buf, "ignore-space-change"))
Stefan Bellerb3095712018-07-16 16:05:40 -0700334 ret |= XDF_IGNORE_WHITESPACE_CHANGE;
335 else if (!strcmp(sb.buf, "ignore-space-at-eol"))
336 ret |= XDF_IGNORE_WHITESPACE_AT_EOL;
337 else if (!strcmp(sb.buf, "ignore-all-space"))
338 ret |= XDF_IGNORE_WHITESPACE;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700339 else if (!strcmp(sb.buf, "allow-indentation-change"))
340 ret |= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE;
Stefan Bellerd173e792018-11-13 13:33:57 -0800341 else {
342 ret |= COLOR_MOVED_WS_ERROR;
343 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);
344 }
Stefan Bellerb3095712018-07-16 16:05:40 -0700345
346 strbuf_release(&sb);
347 }
348
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700349 if ((ret & COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) &&
Stefan Bellerd173e792018-11-13 13:33:57 -0800350 (ret & XDF_WHITESPACE_FLAGS)) {
Junio C Hamano15b07cb2019-01-29 12:47:53 -0800351 error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes"));
Stefan Bellerd173e792018-11-13 13:33:57 -0800352 ret |= COLOR_MOVED_WS_ERROR;
353 }
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700354
Stefan Bellerb3095712018-07-16 16:05:40 -0700355 string_list_clear(&l, 0);
356
357 return ret;
Jeff Kinge269eb72011-08-17 22:03:48 -0700358}
Junio C Hamano801235c2006-06-24 04:06:23 -0700359
360int git_diff_ui_config(const char *var, const char *value, void *cb)
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -0400361{
362 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
363 diff_use_color_default = git_config_colorbool(var, value);
364 return 0;
365 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700366 if (!strcmp(var, "diff.colormoved")) {
367 int cm = parse_color_moved(value);
368 if (cm < 0)
369 return -1;
370 diff_color_moved_default = cm;
371 return 0;
372 }
Stefan Beller626c0b52018-07-18 12:31:56 -0700373 if (!strcmp(var, "diff.colormovedws")) {
Stefan Bellerd173e792018-11-13 13:33:57 -0800374 unsigned cm = parse_color_moved_ws(value);
375 if (cm & COLOR_MOVED_WS_ERROR)
Stefan Beller626c0b52018-07-18 12:31:56 -0700376 return -1;
377 diff_color_moved_ws_default = cm;
378 return 0;
379 }
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -0400380 if (!strcmp(var, "diff.context")) {
381 diff_context_default = git_config_int(var, value);
382 if (diff_context_default < 0)
383 return -1;
384 return 0;
385 }
Vegard Nossumc4888672017-01-12 13:21:11 +0100386 if (!strcmp(var, "diff.interhunkcontext")) {
387 diff_interhunk_context_default = git_config_int(var, value);
388 if (diff_interhunk_context_default < 0)
389 return -1;
390 return 0;
391 }
Eric Wongb68ea122006-07-07 04:01:23 -0700392 if (!strcmp(var, "diff.renames")) {
Linus Torvaldscced5fb2009-04-09 11:46:15 -0700393 diff_detect_rename_default = git_config_rename(var, value);
Eric Wongb68ea122006-07-07 04:01:23 -0700394 return 0;
395 }
Junio C Hamanoaecbf912007-08-31 13:13:42 -0700396 if (!strcmp(var, "diff.autorefreshindex")) {
397 diff_auto_refresh_index = git_config_bool(var, value);
398 return 0;
399 }
Junio C Hamanoa5a818e2008-08-18 20:08:09 -0700400 if (!strcmp(var, "diff.mnemonicprefix")) {
401 diff_mnemonic_prefix = git_config_bool(var, value);
402 return 0;
403 }
Eli Collinsf89504d2010-05-02 19:03:41 -0700404 if (!strcmp(var, "diff.noprefix")) {
405 diff_no_prefix = git_config_bool(var, value);
406 return 0;
407 }
Laurent Arnoudc28ded82020-05-22 12:46:18 +0200408 if (!strcmp(var, "diff.relative")) {
409 diff_relative = git_config_bool(var, value);
410 return 0;
411 }
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +0100412 if (!strcmp(var, "diff.statgraphwidth")) {
413 diff_stat_graph_width = git_config_int(var, value);
414 return 0;
415 }
Brian Hetrodaec8082008-07-05 01:24:43 -0400416 if (!strcmp(var, "diff.external"))
417 return git_config_string(&external_diff_cmd_cfg, var, value);
Boyd Stephen Smith Jr98a4d872009-01-20 21:46:57 -0600418 if (!strcmp(var, "diff.wordregex"))
419 return git_config_string(&diff_word_regex_cfg, var, value);
Samuel Bronson6d8940b2013-12-18 19:08:12 -0500420 if (!strcmp(var, "diff.orderfile"))
421 return git_config_pathname(&diff_order_file_cfg, var, value);
Junio C Hamanof1af60b2007-04-22 17:52:55 -0700422
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +0200423 if (!strcmp(var, "diff.ignoresubmodules"))
424 handle_ignore_submodules_arg(&default_diff_options, value);
425
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +0530426 if (!strcmp(var, "diff.submodule")) {
427 if (parse_submodule_params(&default_diff_options, value))
428 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
429 value);
430 return 0;
431 }
432
Michal Privoznik07ab4de2013-01-16 08:51:57 +0100433 if (!strcmp(var, "diff.algorithm")) {
434 diff_algorithm = parse_algorithm_value(value);
435 if (diff_algorithm < 0)
436 return -1;
437 return 0;
438 }
439
Jeff King33c643b2017-10-13 13:24:31 -0400440 if (git_color_config(var, value, cb) < 0)
441 return -1;
442
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100443 return git_diff_basic_config(var, value, cb);
Jeff King9a1805a2008-01-04 03:59:34 -0500444}
445
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100446int git_diff_basic_config(const char *var, const char *value, void *cb)
Jeff King9a1805a2008-01-04 03:59:34 -0500447{
Jeff Kingae021d82014-06-18 15:47:50 -0400448 const char *name;
449
Linus Torvalds2b6ca6d2008-08-05 11:27:30 -0700450 if (!strcmp(var, "diff.renamelimit")) {
451 diff_rename_limit_default = git_config_int(var, value);
452 return 0;
453 }
454
Jeff King6680a082012-02-07 13:23:02 -0500455 if (userdiff_config(var, value) < 0)
456 return -1;
Jeff Kingc7534ef2008-10-26 00:45:55 -0400457
Jeff Kingae021d82014-06-18 15:47:50 -0400458 if (skip_prefix(var, "diff.color.", &name) ||
459 skip_prefix(var, "color.diff.", &name)) {
460 int slot = parse_diff_color_slot(name);
Jeff King8b8e8622009-12-12 07:25:24 -0500461 if (slot < 0)
462 return 0;
Junio C Hamano64f30e92008-02-11 10:53:56 -0800463 if (!value)
464 return config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 15:33:09 -0400465 return color_parse(value, diff_colors[slot]);
Junio C Hamano801235c2006-06-24 04:06:23 -0700466 }
Junio C Hamanof1af60b2007-04-22 17:52:55 -0700467
Jeff Kingda806352020-01-31 04:57:49 -0500468 if (!strcmp(var, "diff.wserrorhighlight")) {
469 int val = parse_ws_error_highlight(value);
470 if (val < 0)
471 return -1;
472 ws_error_highlight_default = val;
473 return 0;
474 }
475
Jim Meyeringa624eaa2008-08-15 13:39:26 +0200476 /* like GNU diff's --suppress-blank-empty option */
Johannes Schindelin950db872009-01-20 22:08:33 +0100477 if (!strcmp(var, "diff.suppressblankempty") ||
478 /* for backwards compatibility */
479 !strcmp(var, "diff.suppress-blank-empty")) {
Jim Meyeringa624eaa2008-08-15 13:39:26 +0200480 diff_suppress_blank_empty = git_config_bool(var, value);
481 return 0;
482 }
483
Johan Herland2d174952011-04-29 11:36:19 +0200484 if (!strcmp(var, "diff.dirstat")) {
Johan Herland51670fc2011-04-29 11:36:22 +0200485 struct strbuf errmsg = STRBUF_INIT;
Johan Herland712d2c72011-04-29 11:36:20 +0200486 default_diff_options.dirstat_permille = diff_dirstat_permille_default;
Johan Herland51670fc2011-04-29 11:36:22 +0200487 if (parse_dirstat_params(&default_diff_options, value, &errmsg))
Johan Herland7478ac52011-04-29 11:36:23 +0200488 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
Johan Herland51670fc2011-04-29 11:36:22 +0200489 errmsg.buf);
490 strbuf_release(&errmsg);
Johan Herland712d2c72011-04-29 11:36:20 +0200491 diff_dirstat_permille_default = default_diff_options.dirstat_permille;
Johan Herland2d174952011-04-29 11:36:19 +0200492 return 0;
493 }
494
Marc Branchaudcf5e7722017-05-08 12:03:36 -0400495 if (git_diff_heuristic_config(var, value, cb) < 0)
496 return -1;
497
Jeff King3e1dd172011-08-17 22:05:08 -0700498 return git_default_config(var, value, cb);
Junio C Hamano801235c2006-06-24 04:06:23 -0700499}
500
Junio C Hamano6973dca2006-04-21 23:57:45 -0700501static char *quote_two(const char *one, const char *two)
502{
Junio C Hamano7c37c972020-09-10 10:01:59 -0700503 int need_one = quote_c_style(one, NULL, NULL, CQUOTE_NODQ);
504 int need_two = quote_c_style(two, NULL, NULL, CQUOTE_NODQ);
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500505 struct strbuf res = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700506
507 if (need_one + need_two) {
Pierre Habouzit663af342007-09-20 00:42:15 +0200508 strbuf_addch(&res, '"');
Junio C Hamano7c37c972020-09-10 10:01:59 -0700509 quote_c_style(one, &res, NULL, CQUOTE_NODQ);
510 quote_c_style(two, &res, NULL, CQUOTE_NODQ);
Pierre Habouzit663af342007-09-20 00:42:15 +0200511 strbuf_addch(&res, '"');
512 } else {
513 strbuf_addstr(&res, one);
514 strbuf_addstr(&res, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700515 }
Pierre Habouzitb315c5c2007-09-27 12:58:23 +0200516 return strbuf_detach(&res, NULL);
Junio C Hamano6973dca2006-04-21 23:57:45 -0700517}
518
519static const char *external_diff(void)
520{
521 static const char *external_diff_cmd = NULL;
522 static int done_preparing = 0;
523
524 if (done_preparing)
525 return external_diff_cmd;
Kim Gybels6776a842019-01-11 21:26:08 +0100526 external_diff_cmd = xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
Johannes Schindelincbe02102007-12-17 13:42:20 +0000527 if (!external_diff_cmd)
528 external_diff_cmd = external_diff_cmd_cfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700529 done_preparing = 1;
530 return external_diff_cmd;
531}
532
Michael Haggerty284098f2015-08-12 19:12:01 +0200533/*
534 * Keep track of files used for diffing. Sometimes such an entry
535 * refers to a temporary file, sometimes to an existing file, and
536 * sometimes to "/dev/null".
537 */
Junio C Hamano6973dca2006-04-21 23:57:45 -0700538static struct diff_tempfile {
Michael Haggerty284098f2015-08-12 19:12:01 +0200539 /*
540 * filename external diff should read from, or NULL if this
541 * entry is currently not in use:
542 */
543 const char *name;
544
brian m. carlsondc015052017-03-26 16:01:24 +0000545 char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano6973dca2006-04-21 23:57:45 -0700546 char mode[10];
Michael Haggerty284098f2015-08-12 19:12:01 +0200547
548 /*
549 * If this diff_tempfile instance refers to a temporary file,
550 * this tempfile object is used to manage its lifetime.
551 */
Jeff King076aa2c2017-09-05 08:15:08 -0400552 struct tempfile *tempfile;
Junio C Hamano6973dca2006-04-21 23:57:45 -0700553} diff_temp[2];
554
Junio C Hamano6957eb92009-09-14 18:44:01 -0700555struct emit_callback {
Junio C Hamano6957eb92009-09-14 18:44:01 -0700556 int color_diff;
557 unsigned ws_rule;
558 int blank_at_eof_in_preimage;
559 int blank_at_eof_in_postimage;
560 int lno_in_preimage;
561 int lno_in_postimage;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700562 const char **label_path;
563 struct diff_words_data *diff_words;
Bo Yanga3c158d2010-05-26 15:08:02 +0800564 struct diff_options *opt;
Greg Bacon3e97c7c2009-11-19 15:12:24 -0600565 struct strbuf *header;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700566};
567
Junio C Hamano6973dca2006-04-21 23:57:45 -0700568static int count_lines(const char *data, int size)
569{
570 int count, ch, completely_empty = 1, nl_just_seen = 0;
571 count = 0;
572 while (0 < size--) {
573 ch = *data++;
574 if (ch == '\n') {
575 count++;
576 nl_just_seen = 1;
577 completely_empty = 0;
578 }
579 else {
580 nl_just_seen = 0;
581 completely_empty = 0;
582 }
583 }
584 if (completely_empty)
585 return 0;
586 if (!nl_just_seen)
587 count++; /* no trailing newline */
588 return count;
589}
590
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +0200591static int fill_mmfile(struct repository *r, mmfile_t *mf,
592 struct diff_filespec *one)
Junio C Hamano6957eb92009-09-14 18:44:01 -0700593{
594 if (!DIFF_FILE_VALID(one)) {
595 mf->ptr = (char *)""; /* does not matter */
596 mf->size = 0;
597 return 0;
598 }
Jonathan Tan1c37e862020-04-07 15:11:41 -0700599 else if (diff_populate_filespec(r, one, NULL))
Junio C Hamano6957eb92009-09-14 18:44:01 -0700600 return -1;
Junio C Hamanobb35fef2009-09-15 03:38:30 -0700601
Junio C Hamano6957eb92009-09-14 18:44:01 -0700602 mf->ptr = one->data;
603 mf->size = one->size;
604 return 0;
605}
606
Jeff Kingabb371a2011-02-19 03:16:32 -0500607/* 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 +0200608static unsigned long diff_filespec_size(struct repository *r,
609 struct diff_filespec *one)
Jeff Kingabb371a2011-02-19 03:16:32 -0500610{
Jonathan Tan1c37e862020-04-07 15:11:41 -0700611 struct diff_populate_filespec_options dpf_options = {
612 .check_size_only = 1,
613 };
614
Jeff Kingabb371a2011-02-19 03:16:32 -0500615 if (!DIFF_FILE_VALID(one))
616 return 0;
Jonathan Tan1c37e862020-04-07 15:11:41 -0700617 diff_populate_filespec(r, one, &dpf_options);
Jeff Kingabb371a2011-02-19 03:16:32 -0500618 return one->size;
619}
620
Jeff Kingc5224f02022-12-13 06:12:58 -0500621static int count_trailing_blank(mmfile_t *mf)
Junio C Hamano6957eb92009-09-14 18:44:01 -0700622{
623 char *ptr = mf->ptr;
624 long size = mf->size;
625 int cnt = 0;
626
627 if (!size)
628 return cnt;
629 ptr += size - 1; /* pointing at the very end */
630 if (*ptr != '\n')
631 ; /* incomplete line */
632 else
633 ptr--; /* skip the last LF */
634 while (mf->ptr < ptr) {
635 char *prev_eol;
636 for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--)
637 if (*prev_eol == '\n')
638 break;
Jeff Kingc5224f02022-12-13 06:12:58 -0500639 if (!ws_blank_line(prev_eol + 1, ptr - prev_eol))
Junio C Hamano6957eb92009-09-14 18:44:01 -0700640 break;
641 cnt++;
642 ptr = prev_eol - 1;
643 }
644 return cnt;
645}
646
647static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
648 struct emit_callback *ecbdata)
649{
650 int l1, l2, at;
Jeff Kingc5224f02022-12-13 06:12:58 -0500651 l1 = count_trailing_blank(mf1);
652 l2 = count_trailing_blank(mf2);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700653 if (l2 <= l1) {
654 ecbdata->blank_at_eof_in_preimage = 0;
655 ecbdata->blank_at_eof_in_postimage = 0;
656 return;
657 }
658 at = count_lines(mf1->ptr, mf1->size);
659 ecbdata->blank_at_eof_in_preimage = (at - l1) + 1;
660
661 at = count_lines(mf2->ptr, mf2->size);
662 ecbdata->blank_at_eof_in_postimage = (at - l2) + 1;
663}
664
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -0700665static void emit_line_0(struct diff_options *o,
Stefan Beller017ac452018-08-13 18:41:19 -0700666 const char *set_sign, const char *set, unsigned reverse, const char *reset,
Junio C Hamano250f7992009-09-14 18:44:01 -0700667 int first, const char *line, int len)
Junio C Hamano6957eb92009-09-14 18:44:01 -0700668{
669 int has_trailing_newline, has_trailing_carriage_return;
Stefan Beller44410672018-08-13 18:41:22 -0700670 int needs_reset = 0; /* at the end of the line */
Bo Yanga3c158d2010-05-26 15:08:02 +0800671 FILE *file = o->file;
672
Stefan Bellerf103a6f2018-08-13 18:41:21 -0700673 fputs(diff_line_prefix(o), file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700674
Stefan Beller44410672018-08-13 18:41:22 -0700675 has_trailing_newline = (len > 0 && line[len-1] == '\n');
676 if (has_trailing_newline)
677 len--;
678
679 has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
680 if (has_trailing_carriage_return)
681 len--;
682
683 if (!len && !first)
684 goto end_of_line;
685
686 if (reverse && want_color(o->use_color)) {
687 fputs(GIT_COLOR_REVERSE, file);
688 needs_reset = 1;
Junio C Hamano250f7992009-09-14 18:44:01 -0700689 }
Junio C Hamano6957eb92009-09-14 18:44:01 -0700690
Stefan Beller44410672018-08-13 18:41:22 -0700691 if (set_sign) {
692 fputs(set_sign, file);
693 needs_reset = 1;
Junio C Hamano06a47552009-11-27 22:04:10 -0800694 }
Stefan Beller44410672018-08-13 18:41:22 -0700695
Junio C Hamano6957eb92009-09-14 18:44:01 -0700696 if (first)
Stefan Beller44410672018-08-13 18:41:22 -0700697 fputc(first, file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700698
Stefan Beller44410672018-08-13 18:41:22 -0700699 if (!len)
700 goto end_of_line;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700701
Stefan Beller44410672018-08-13 18:41:22 -0700702 if (set) {
703 if (set_sign && set != set_sign)
704 fputs(reset, file);
Junio C Hamano250f7992009-09-14 18:44:01 -0700705 fputs(set, file);
Stefan Beller44410672018-08-13 18:41:22 -0700706 needs_reset = 1;
Junio C Hamano6957eb92009-09-14 18:44:01 -0700707 }
Stefan Beller44410672018-08-13 18:41:22 -0700708 fwrite(line, len, 1, file);
709 needs_reset = 1; /* 'line' may contain color codes. */
710
711end_of_line:
712 if (needs_reset)
713 fputs(reset, file);
Junio C Hamano6957eb92009-09-14 18:44:01 -0700714 if (has_trailing_carriage_return)
715 fputc('\r', file);
716 if (has_trailing_newline)
717 fputc('\n', file);
718}
719
Bo Yanga3c158d2010-05-26 15:08:02 +0800720static void emit_line(struct diff_options *o, const char *set, const char *reset,
Junio C Hamano250f7992009-09-14 18:44:01 -0700721 const char *line, int len)
722{
Stefan Beller44410672018-08-13 18:41:22 -0700723 emit_line_0(o, set, NULL, 0, reset, 0, line, len);
Junio C Hamano250f7992009-09-14 18:44:01 -0700724}
725
Stefan Beller36a4cef2017-06-29 17:06:49 -0700726enum diff_symbol {
Stefan Beller4eed0eb2017-06-29 17:07:01 -0700727 DIFF_SYMBOL_BINARY_DIFF_HEADER,
728 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
729 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
730 DIFF_SYMBOL_BINARY_DIFF_BODY,
731 DIFF_SYMBOL_BINARY_DIFF_FOOTER,
Stefan Beller0911c472017-06-29 17:07:02 -0700732 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
733 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
734 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
735 DIFF_SYMBOL_STATS_LINE,
Stefan Bellerbd033292017-06-29 17:07:03 -0700736 DIFF_SYMBOL_WORD_DIFF,
Stefan Beller30b7e1e2017-06-29 17:07:04 -0700737 DIFF_SYMBOL_STAT_SEP,
Stefan Beller146fdb02017-06-29 17:07:05 -0700738 DIFF_SYMBOL_SUMMARY,
Stefan Bellerf3597132017-06-29 17:07:00 -0700739 DIFF_SYMBOL_SUBMODULE_ADD,
740 DIFF_SYMBOL_SUBMODULE_DEL,
741 DIFF_SYMBOL_SUBMODULE_UNTRACKED,
742 DIFF_SYMBOL_SUBMODULE_MODIFIED,
743 DIFF_SYMBOL_SUBMODULE_HEADER,
744 DIFF_SYMBOL_SUBMODULE_ERROR,
745 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH,
Stefan Beller5af6ea92017-06-29 17:06:59 -0700746 DIFF_SYMBOL_REWRITE_DIFF,
Stefan Beller4acaaa72017-06-29 17:06:58 -0700747 DIFF_SYMBOL_BINARY_FILES,
Stefan Bellera29b0a12017-06-29 17:06:57 -0700748 DIFF_SYMBOL_HEADER,
Stefan Beller3ee8b7b2017-06-29 17:06:56 -0700749 DIFF_SYMBOL_FILEPAIR_PLUS,
750 DIFF_SYMBOL_FILEPAIR_MINUS,
Stefan Bellerff958672017-06-29 17:06:54 -0700751 DIFF_SYMBOL_WORDS_PORCELAIN,
752 DIFF_SYMBOL_WORDS,
Stefan Beller091f8e22017-06-29 17:06:53 -0700753 DIFF_SYMBOL_CONTEXT,
Stefan Bellerf2bb1212017-06-29 17:06:55 -0700754 DIFF_SYMBOL_CONTEXT_INCOMPLETE,
Stefan Beller091f8e22017-06-29 17:06:53 -0700755 DIFF_SYMBOL_PLUS,
756 DIFF_SYMBOL_MINUS,
Stefan Bellerb9cbfde2017-06-29 17:06:52 -0700757 DIFF_SYMBOL_NO_LF_EOF,
Stefan Beller68abc6f2017-06-29 17:06:51 -0700758 DIFF_SYMBOL_CONTEXT_FRAGINFO,
Stefan Bellerc64b4202017-06-29 17:06:50 -0700759 DIFF_SYMBOL_CONTEXT_MARKER,
Stefan Beller36a4cef2017-06-29 17:06:49 -0700760 DIFF_SYMBOL_SEPARATOR
761};
Stefan Beller091f8e22017-06-29 17:06:53 -0700762/*
763 * Flags for content lines:
764 * 0..12 are whitespace rules
765 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
766 * 16 is marking if the line is blank at EOF
767 */
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700768#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
769#define DIFF_SYMBOL_MOVED_LINE (1<<17)
770#define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
Stefan Beller86b452e2017-06-30 13:53:09 -0700771#define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
Stefan Beller091f8e22017-06-29 17:06:53 -0700772#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
773
Stefan Bellere6e045f2017-06-29 17:07:06 -0700774/*
775 * This struct is used when we need to buffer the output of the diff output.
776 *
777 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
778 * into the pre/post image file. This pointer could be a union with the
779 * line pointer. By storing an offset into the file instead of the literal line,
780 * we can decrease the memory footprint for the buffered output. At first we
781 * may want to only have indirection for the content lines, but we could also
782 * enhance the state for emitting prefabricated lines, e.g. the similarity
783 * score line or hunk/file headers would only need to store a number or path
784 * and then the output can be constructed later on depending on state.
785 */
786struct emitted_diff_symbol {
787 const char *line;
788 int len;
789 int flags;
Phillip Wood21536d02018-11-23 11:16:57 +0000790 int indent_off; /* Offset to first non-whitespace character */
791 int indent_width; /* The visual width of the indentation */
Phillip Wood72962e82021-12-09 10:30:09 +0000792 unsigned id;
Stefan Bellere6e045f2017-06-29 17:07:06 -0700793 enum diff_symbol s;
794};
Ævar Arnfjörð Bjarmason9865b6e2021-09-27 14:54:25 +0200795#define EMITTED_DIFF_SYMBOL_INIT { 0 }
Stefan Bellere6e045f2017-06-29 17:07:06 -0700796
797struct emitted_diff_symbols {
798 struct emitted_diff_symbol *buf;
799 int nr, alloc;
800};
Ævar Arnfjörð Bjarmason9865b6e2021-09-27 14:54:25 +0200801#define EMITTED_DIFF_SYMBOLS_INIT { 0 }
Stefan Bellere6e045f2017-06-29 17:07:06 -0700802
803static void append_emitted_diff_symbol(struct diff_options *o,
804 struct emitted_diff_symbol *e)
805{
806 struct emitted_diff_symbol *f;
807
808 ALLOC_GROW(o->emitted_symbols->buf,
809 o->emitted_symbols->nr + 1,
810 o->emitted_symbols->alloc);
811 f = &o->emitted_symbols->buf[o->emitted_symbols->nr++];
812
813 memcpy(f, e, sizeof(struct emitted_diff_symbol));
814 f->line = e->line ? xmemdupz(e->line, e->len) : NULL;
815}
816
Ævar Arnfjörð Bjarmason77e56d52022-03-17 15:55:35 +0100817static void free_emitted_diff_symbols(struct emitted_diff_symbols *e)
818{
819 if (!e)
820 return;
821 free(e->buf);
822 free(e);
823}
824
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700825struct moved_entry {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700826 const struct emitted_diff_symbol *es;
827 struct moved_entry *next_line;
Phillip Wood72962e82021-12-09 10:30:09 +0000828 struct moved_entry *next_match;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700829};
830
Phillip Wood74d156f2018-10-04 11:07:41 +0100831struct moved_block {
832 struct moved_entry *match;
Phillip Wood21536d02018-11-23 11:16:57 +0000833 int wsd; /* The whitespace delta of this block */
Phillip Wood74d156f2018-10-04 11:07:41 +0100834};
835
Phillip Wood0cd51e92018-11-23 11:16:58 +0000836#define INDENT_BLANKLINE INT_MIN
837
Phillip Wood21536d02018-11-23 11:16:57 +0000838static void fill_es_indent_data(struct emitted_diff_symbol *es)
839{
Phillip Wood0cd51e92018-11-23 11:16:58 +0000840 unsigned int off = 0, i;
Phillip Wood21536d02018-11-23 11:16:57 +0000841 int width = 0, tab_width = es->flags & WS_TAB_WIDTH_MASK;
842 const char *s = es->line;
843 const int len = es->len;
844
845 /* skip any \v \f \r at start of indentation */
846 while (s[off] == '\f' || s[off] == '\v' ||
847 (s[off] == '\r' && off < len - 1))
848 off++;
849
850 /* calculate the visual width of indentation */
851 while(1) {
852 if (s[off] == ' ') {
853 width++;
854 off++;
855 } else if (s[off] == '\t') {
856 width += tab_width - (width % tab_width);
857 while (s[++off] == '\t')
858 width += tab_width;
859 } else {
860 break;
861 }
862 }
863
Phillip Wood0cd51e92018-11-23 11:16:58 +0000864 /* check if this line is blank */
865 for (i = off; i < len; i++)
866 if (!isspace(s[i]))
867 break;
868
869 if (i == len) {
870 es->indent_width = INDENT_BLANKLINE;
871 es->indent_off = len;
872 } else {
873 es->indent_off = off;
874 es->indent_width = width;
875 }
Phillip Wood74d156f2018-10-04 11:07:41 +0100876}
877
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700878static int compute_ws_delta(const struct emitted_diff_symbol *a,
Phillip Wood25e61902021-12-09 10:30:07 +0000879 const struct emitted_diff_symbol *b)
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700880{
Phillip Wood25e61902021-12-09 10:30:07 +0000881 int a_width = a->indent_width,
Phillip Wood21536d02018-11-23 11:16:57 +0000882 b_width = b->indent_width;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700883
Phillip Wood25e61902021-12-09 10:30:07 +0000884 if (a_width == INDENT_BLANKLINE && b_width == INDENT_BLANKLINE)
885 return INDENT_BLANKLINE;
Phillip Wood0cd51e92018-11-23 11:16:58 +0000886
Phillip Wood25e61902021-12-09 10:30:07 +0000887 return a_width - b_width;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700888}
889
Phillip Wood52d14e12021-12-09 10:30:02 +0000890static int cmp_in_block_with_wsd(const struct moved_entry *cur,
891 const struct emitted_diff_symbol *l,
892 struct moved_block *pmb)
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700893{
Phillip Wood72962e82021-12-09 10:30:09 +0000894 int a_width = cur->es->indent_width, b_width = l->indent_width;
Phillip Wood21536d02018-11-23 11:16:57 +0000895 int delta;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700896
Phillip Wood72962e82021-12-09 10:30:09 +0000897 /* The text of each line must match */
898 if (cur->es->id != l->id)
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700899 return 1;
900
Phillip Wood72962e82021-12-09 10:30:09 +0000901 /*
902 * If 'l' and 'cur' are both blank then we don't need to check the
903 * indent. We only need to check cur as we know the strings match.
904 * */
905 if (a_width == INDENT_BLANKLINE)
Phillip Wood0cd51e92018-11-23 11:16:58 +0000906 return 0;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700907
908 /*
Phillip Wood21536d02018-11-23 11:16:57 +0000909 * The indent changes of the block are known and stored in pmb->wsd;
910 * however we need to check if the indent changes of the current line
Phillip Wood72962e82021-12-09 10:30:09 +0000911 * match those of the current block.
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700912 */
Phillip Wood52d14e12021-12-09 10:30:02 +0000913 delta = b_width - a_width;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700914
Phillip Wood0cd51e92018-11-23 11:16:58 +0000915 /*
916 * If the previous lines of this block were all blank then set its
917 * whitespace delta.
918 */
919 if (pmb->wsd == INDENT_BLANKLINE)
920 pmb->wsd = delta;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700921
Phillip Wood72962e82021-12-09 10:30:09 +0000922 return delta != pmb->wsd;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -0700923}
924
Phillip Wood72962e82021-12-09 10:30:09 +0000925struct interned_diff_symbol {
926 struct hashmap_entry ent;
927 struct emitted_diff_symbol *es;
928};
929
930static int interned_diff_symbol_cmp(const void *hashmap_cmp_fn_data,
931 const struct hashmap_entry *eptr,
932 const struct hashmap_entry *entry_or_key,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200933 const void *keydata UNUSED)
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700934{
Stefan Belleree1df662018-07-16 16:05:38 -0700935 const struct diff_options *diffopt = hashmap_cmp_fn_data;
Phillip Wood25e61902021-12-09 10:30:07 +0000936 const struct emitted_diff_symbol *a, *b;
Stefan Bellerb3095712018-07-16 16:05:40 -0700937 unsigned flags = diffopt->color_moved_ws_handling
938 & XDF_WHITESPACE_FLAGS;
Stefan Belleree1df662018-07-16 16:05:38 -0700939
Phillip Wood72962e82021-12-09 10:30:09 +0000940 a = container_of(eptr, const struct interned_diff_symbol, ent)->es;
941 b = container_of(entry_or_key, const struct interned_diff_symbol, ent)->es;
Eric Wong939af162019-10-06 23:30:37 +0000942
Phillip Wood25e61902021-12-09 10:30:07 +0000943 return !xdiff_compare_lines(a->line + a->indent_off,
944 a->len - a->indent_off,
945 b->line + b->indent_off,
946 b->len - b->indent_off, flags);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700947}
948
Phillip Wood72962e82021-12-09 10:30:09 +0000949static void prepare_entry(struct diff_options *o, struct emitted_diff_symbol *l,
950 struct interned_diff_symbol *s)
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700951{
Stefan Bellerb3095712018-07-16 16:05:40 -0700952 unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS;
Phillip Wood25e61902021-12-09 10:30:07 +0000953 unsigned int hash = xdiff_hash_string(l->line + l->indent_off,
954 l->len - l->indent_off, flags);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700955
Phillip Wood72962e82021-12-09 10:30:09 +0000956 hashmap_entry_init(&s->ent, hash);
957 s->es = l;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700958}
959
Phillip Wood72962e82021-12-09 10:30:09 +0000960struct moved_entry_list {
961 struct moved_entry *add, *del;
962};
963
964static struct moved_entry_list *add_lines_to_move_detection(struct diff_options *o,
965 struct mem_pool *entry_mem_pool)
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700966{
967 struct moved_entry *prev_line = NULL;
Phillip Wood72962e82021-12-09 10:30:09 +0000968 struct mem_pool interned_pool;
969 struct hashmap interned_map;
970 struct moved_entry_list *entry_list = NULL;
971 size_t entry_list_alloc = 0;
972 unsigned id = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700973 int n;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700974
Phillip Wood72962e82021-12-09 10:30:09 +0000975 hashmap_init(&interned_map, interned_diff_symbol_cmp, o, 8096);
976 mem_pool_init(&interned_pool, 1024 * 1024);
977
978 for (n = 0; n < o->emitted_symbols->nr; n++) {
979 struct interned_diff_symbol key;
980 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
981 struct interned_diff_symbol *s;
982 struct moved_entry *entry;
983
984 if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS) {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700985 prev_line = NULL;
986 continue;
987 }
988
Phillip Wood21536d02018-11-23 11:16:57 +0000989 if (o->color_moved_ws_handling &
990 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
Phillip Wood72962e82021-12-09 10:30:09 +0000991 fill_es_indent_data(l);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -0700992
Phillip Wood72962e82021-12-09 10:30:09 +0000993 prepare_entry(o, l, &key);
994 s = hashmap_get_entry(&interned_map, &key, ent, &key.ent);
995 if (s) {
996 l->id = s->es->id;
997 } else {
998 l->id = id;
999 ALLOC_GROW_BY(entry_list, id, 1, entry_list_alloc);
1000 hashmap_add(&interned_map,
1001 memcpy(mem_pool_alloc(&interned_pool,
1002 sizeof(key)),
1003 &key, sizeof(key)));
1004 }
1005 entry = mem_pool_alloc(entry_mem_pool, sizeof(*entry));
1006 entry->es = l;
1007 entry->next_line = NULL;
1008 if (prev_line && prev_line->es->s == l->s)
1009 prev_line->next_line = entry;
1010 prev_line = entry;
1011 if (l->s == DIFF_SYMBOL_PLUS) {
1012 entry->next_match = entry_list[l->id].add;
1013 entry_list[l->id].add = entry;
1014 } else {
1015 entry->next_match = entry_list[l->id].del;
1016 entry_list[l->id].del = entry;
1017 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001018 }
Phillip Wood72962e82021-12-09 10:30:09 +00001019
1020 hashmap_clear(&interned_map);
1021 mem_pool_discard(&interned_pool, 0);
1022
1023 return entry_list;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001024}
1025
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001026static void pmb_advance_or_null(struct diff_options *o,
Phillip Wood08fba102021-12-09 10:30:03 +00001027 struct emitted_diff_symbol *l,
Phillip Wood74d156f2018-10-04 11:07:41 +01001028 struct moved_block *pmb,
Phillip Wood0e488f12021-12-09 10:30:05 +00001029 int *pmb_nr)
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001030{
Phillip Wood0e488f12021-12-09 10:30:05 +00001031 int i, j;
Phillip Wood08fba102021-12-09 10:30:03 +00001032
Phillip Wood0e488f12021-12-09 10:30:05 +00001033 for (i = 0, j = 0; i < *pmb_nr; i++) {
Phillip Woodff046a02021-12-09 10:30:04 +00001034 int match;
Phillip Wood74d156f2018-10-04 11:07:41 +01001035 struct moved_entry *prev = pmb[i].match;
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001036 struct moved_entry *cur = (prev && prev->next_line) ?
1037 prev->next_line : NULL;
Stefan Bellere2fe6ab2018-07-16 16:05:41 -07001038
Phillip Woodff046a02021-12-09 10:30:04 +00001039 if (o->color_moved_ws_handling &
1040 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
1041 match = cur &&
1042 !cmp_in_block_with_wsd(cur, l, &pmb[i]);
1043 else
Phillip Wood72962e82021-12-09 10:30:09 +00001044 match = cur && cur->es->id == l->id;
1045
Phillip Wood0e488f12021-12-09 10:30:05 +00001046 if (match) {
1047 pmb[j] = pmb[i];
1048 pmb[j++].match = cur;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001049 }
1050 }
Phillip Wood0e488f12021-12-09 10:30:05 +00001051 *pmb_nr = j;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001052}
1053
Phillip Wood7dfe4272021-12-09 10:29:57 +00001054static void fill_potential_moved_blocks(struct diff_options *o,
Phillip Wood7dfe4272021-12-09 10:29:57 +00001055 struct moved_entry *match,
1056 struct emitted_diff_symbol *l,
1057 struct moved_block **pmb_p,
1058 int *pmb_alloc_p, int *pmb_nr_p)
1059
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001060{
Phillip Wood7dfe4272021-12-09 10:29:57 +00001061 struct moved_block *pmb = *pmb_p;
1062 int pmb_alloc = *pmb_alloc_p, pmb_nr = *pmb_nr_p;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001063
Phillip Wood7dfe4272021-12-09 10:29:57 +00001064 /*
1065 * The current line is the start of a new block.
1066 * Setup the set of potential blocks.
1067 */
Phillip Wood72962e82021-12-09 10:30:09 +00001068 for (; match; match = match->next_match) {
Phillip Wood7dfe4272021-12-09 10:29:57 +00001069 ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
1070 if (o->color_moved_ws_handling &
Phillip Wood25e61902021-12-09 10:30:07 +00001071 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
1072 pmb[pmb_nr].wsd = compute_ws_delta(l, match->es);
1073 else
Phillip Wood7dfe4272021-12-09 10:29:57 +00001074 pmb[pmb_nr].wsd = 0;
Phillip Wood25e61902021-12-09 10:30:07 +00001075 pmb[pmb_nr++].match = match;
Stefan Bellerca1f4ae2018-07-18 12:31:55 -07001076 }
1077
Phillip Wood7dfe4272021-12-09 10:29:57 +00001078 *pmb_p = pmb;
1079 *pmb_alloc_p = pmb_alloc;
1080 *pmb_nr_p = pmb_nr;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001081}
1082
Jonathan Tan09153272017-08-15 18:27:38 -07001083/*
1084 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
1085 *
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001086 * Otherwise, if the last block has fewer alphanumeric characters than
1087 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
Jonathan Tan09153272017-08-15 18:27:38 -07001088 * that block.
1089 *
1090 * The last block consists of the (n - block_length)'th line up to but not
1091 * including the nth line.
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001092 *
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001093 * Returns 0 if the last block is empty or is unset by this function, non zero
1094 * otherwise.
1095 *
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001096 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
1097 * Think of a way to unify them.
Jonathan Tan09153272017-08-15 18:27:38 -07001098 */
Phillip Woodbea084b2021-12-09 10:29:56 +00001099#define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1100 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001101static int adjust_last_block(struct diff_options *o, int n, int block_length)
Jonathan Tan09153272017-08-15 18:27:38 -07001102{
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001103 int i, alnum_count = 0;
1104 if (o->color_moved == COLOR_MOVED_PLAIN)
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001105 return block_length;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001106 for (i = 1; i < block_length + 1; i++) {
1107 const char *c = o->emitted_symbols->buf[n - i].line;
1108 for (; *c; c++) {
1109 if (!isalnum(*c))
1110 continue;
1111 alnum_count++;
1112 if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT)
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001113 return 1;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001114 }
1115 }
Jonathan Tan09153272017-08-15 18:27:38 -07001116 for (i = 1; i < block_length + 1; i++)
Phillip Woodbea084b2021-12-09 10:29:56 +00001117 o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK;
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001118 return 0;
Jonathan Tan09153272017-08-15 18:27:38 -07001119}
1120
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001121/* Find blocks of moved code, delegate actual coloring decision to helper */
1122static void mark_color_as_moved(struct diff_options *o,
Phillip Wood72962e82021-12-09 10:30:09 +00001123 struct moved_entry_list *entry_list)
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001124{
Phillip Wood74d156f2018-10-04 11:07:41 +01001125 struct moved_block *pmb = NULL; /* potentially moved blocks */
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001126 int pmb_nr = 0, pmb_alloc = 0;
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001127 int n, flipped_block = 0, block_length = 0;
Phillip Woodeb893522021-12-09 10:30:00 +00001128 enum diff_symbol moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001129
1130
1131 for (n = 0; n < o->emitted_symbols->nr; n++) {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001132 struct moved_entry *match = NULL;
1133 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001134
1135 switch (l->s) {
1136 case DIFF_SYMBOL_PLUS:
Phillip Wood72962e82021-12-09 10:30:09 +00001137 match = entry_list[l->id].del;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001138 break;
1139 case DIFF_SYMBOL_MINUS:
Phillip Wood72962e82021-12-09 10:30:09 +00001140 match = entry_list[l->id].add;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001141 break;
1142 default:
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001143 flipped_block = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001144 }
1145
Phillip Woodeb893522021-12-09 10:30:00 +00001146 if (pmb_nr && (!match || l->s != moved_symbol)) {
Phillip Wood09906582021-12-09 10:29:58 +00001147 if (!adjust_last_block(o, n, block_length) &&
1148 block_length > 1) {
1149 /*
1150 * Rewind in case there is another match
1151 * starting at the second line of the block
1152 */
1153 match = NULL;
1154 n -= block_length;
1155 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001156 pmb_nr = 0;
1157 block_length = 0;
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001158 flipped_block = 0;
Phillip Woodeb893522021-12-09 10:30:00 +00001159 }
1160 if (!match) {
1161 moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001162 continue;
1163 }
1164
Phillip Wood2034b472018-11-23 11:16:54 +00001165 if (o->color_moved == COLOR_MOVED_PLAIN) {
1166 l->flags |= DIFF_SYMBOL_MOVED_LINE;
Stefan Beller176841f2017-06-30 13:53:08 -07001167 continue;
Phillip Wood2034b472018-11-23 11:16:54 +00001168 }
Stefan Beller176841f2017-06-30 13:53:08 -07001169
Phillip Wood0e488f12021-12-09 10:30:05 +00001170 pmb_advance_or_null(o, l, pmb, &pmb_nr);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001171
1172 if (pmb_nr == 0) {
Phillip Wood09906582021-12-09 10:29:58 +00001173 int contiguous = adjust_last_block(o, n, block_length);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001174
Phillip Wood09906582021-12-09 10:29:58 +00001175 if (!contiguous && block_length > 1)
1176 /*
1177 * Rewind in case there is another match
1178 * starting at the second line of the block
1179 */
1180 n -= block_length;
1181 else
Phillip Wood72962e82021-12-09 10:30:09 +00001182 fill_potential_moved_blocks(o, match, l,
Phillip Wood09906582021-12-09 10:29:58 +00001183 &pmb, &pmb_alloc,
1184 &pmb_nr);
1185
Phillip Woodeb893522021-12-09 10:30:00 +00001186 if (contiguous && pmb_nr && moved_symbol == l->s)
Phillip Woodb0a2ba42018-11-23 11:16:55 +00001187 flipped_block = (flipped_block + 1) % 2;
1188 else
1189 flipped_block = 0;
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001190
Phillip Woodeb893522021-12-09 10:30:00 +00001191 if (pmb_nr)
1192 moved_symbol = l->s;
1193 else
1194 moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER;
1195
Jonathan Tanf0b8fb62017-08-15 18:27:39 -07001196 block_length = 0;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001197 }
1198
Phillip Wood2034b472018-11-23 11:16:54 +00001199 if (pmb_nr) {
1200 block_length++;
Phillip Wood2034b472018-11-23 11:16:54 +00001201 l->flags |= DIFF_SYMBOL_MOVED_LINE;
1202 if (flipped_block && o->color_moved != COLOR_MOVED_BLOCKS)
1203 l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
1204 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001205 }
Jonathan Tan09153272017-08-15 18:27:38 -07001206 adjust_last_block(o, n, block_length);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001207
1208 free(pmb);
1209}
Stefan Bellere6e045f2017-06-29 17:07:06 -07001210
Stefan Beller86b452e2017-06-30 13:53:09 -07001211static void dim_moved_lines(struct diff_options *o)
1212{
1213 int n;
1214 for (n = 0; n < o->emitted_symbols->nr; n++) {
1215 struct emitted_diff_symbol *prev = (n != 0) ?
1216 &o->emitted_symbols->buf[n - 1] : NULL;
1217 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
1218 struct emitted_diff_symbol *next =
1219 (n < o->emitted_symbols->nr - 1) ?
1220 &o->emitted_symbols->buf[n + 1] : NULL;
1221
1222 /* Not a plus or minus line? */
1223 if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS)
1224 continue;
1225
1226 /* Not a moved line? */
1227 if (!(l->flags & DIFF_SYMBOL_MOVED_LINE))
1228 continue;
1229
1230 /*
1231 * If prev or next are not a plus or minus line,
1232 * pretend they don't exist
1233 */
1234 if (prev && prev->s != DIFF_SYMBOL_PLUS &&
1235 prev->s != DIFF_SYMBOL_MINUS)
1236 prev = NULL;
1237 if (next && next->s != DIFF_SYMBOL_PLUS &&
1238 next->s != DIFF_SYMBOL_MINUS)
1239 next = NULL;
1240
1241 /* Inside a block? */
1242 if ((prev &&
1243 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1244 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) &&
1245 (next &&
1246 (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1247 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) {
1248 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1249 continue;
1250 }
1251
1252 /* Check if we are at an interesting bound: */
1253 if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) &&
1254 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1255 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1256 continue;
1257 if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) &&
1258 (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1259 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1260 continue;
1261
1262 /*
1263 * The boundary to prev and next are not interesting,
1264 * so this line is not interesting as a whole
1265 */
1266 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1267 }
1268}
1269
Stefan Beller091f8e22017-06-29 17:06:53 -07001270static void emit_line_ws_markup(struct diff_options *o,
Stefan Beller9d1e16b2018-08-13 18:41:18 -07001271 const char *set_sign, const char *set,
1272 const char *reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001273 int sign_index, const char *line, int len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001274 unsigned ws_rule, int blank_at_eof)
1275{
1276 const char *ws = NULL;
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001277 int sign = o->output_indicators[sign_index];
Stefan Beller091f8e22017-06-29 17:06:53 -07001278
1279 if (o->ws_error_highlight & ws_rule) {
1280 ws = diff_get_color_opt(o, DIFF_WHITESPACE);
1281 if (!*ws)
1282 ws = NULL;
1283 }
1284
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001285 if (!ws && !set_sign)
Stefan Beller017ac452018-08-13 18:41:19 -07001286 emit_line_0(o, set, NULL, 0, reset, sign, line, len);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001287 else if (!ws) {
Stefan Beller29ef7592018-08-13 18:41:20 -07001288 emit_line_0(o, set_sign, set, !!set_sign, reset, sign, line, len);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001289 } else if (blank_at_eof)
Stefan Beller091f8e22017-06-29 17:06:53 -07001290 /* Blank line at EOF - paint '+' as well */
Stefan Beller017ac452018-08-13 18:41:19 -07001291 emit_line_0(o, ws, NULL, 0, reset, sign, line, len);
Stefan Beller091f8e22017-06-29 17:06:53 -07001292 else {
1293 /* Emit just the prefix, then the rest. */
Stefan Beller017ac452018-08-13 18:41:19 -07001294 emit_line_0(o, set_sign ? set_sign : set, NULL, !!set_sign, reset,
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001295 sign, "", 0);
Stefan Beller091f8e22017-06-29 17:06:53 -07001296 ws_check_emit(line, len, ws_rule,
1297 o->file, set, reset, ws);
1298 }
1299}
Stefan Beller36a4cef2017-06-29 17:06:49 -07001300
Stefan Bellere6e045f2017-06-29 17:07:06 -07001301static void emit_diff_symbol_from_struct(struct diff_options *o,
1302 struct emitted_diff_symbol *eds)
Stefan Beller36a4cef2017-06-29 17:06:49 -07001303{
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001304 static const char *nneof = " No newline at end of file\n";
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001305 const char *context, *reset, *set, *set_sign, *meta, *fraginfo;
Stefan Bellere6e045f2017-06-29 17:07:06 -07001306
1307 enum diff_symbol s = eds->s;
1308 const char *line = eds->line;
1309 int len = eds->len;
1310 unsigned flags = eds->flags;
1311
Stefan Beller36a4cef2017-06-29 17:06:49 -07001312 switch (s) {
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001313 case DIFF_SYMBOL_NO_LF_EOF:
1314 context = diff_get_color_opt(o, DIFF_CONTEXT);
1315 reset = diff_get_color_opt(o, DIFF_RESET);
1316 putc('\n', o->file);
Stefan Beller017ac452018-08-13 18:41:19 -07001317 emit_line_0(o, context, NULL, 0, reset, '\\',
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001318 nneof, strlen(nneof));
1319 break;
Stefan Bellerf3597132017-06-29 17:07:00 -07001320 case DIFF_SYMBOL_SUBMODULE_HEADER:
1321 case DIFF_SYMBOL_SUBMODULE_ERROR:
1322 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
Stefan Beller0911c472017-06-29 17:07:02 -07001323 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
Stefan Beller146fdb02017-06-29 17:07:05 -07001324 case DIFF_SYMBOL_SUMMARY:
Stefan Beller0911c472017-06-29 17:07:02 -07001325 case DIFF_SYMBOL_STATS_LINE:
Stefan Beller4eed0eb2017-06-29 17:07:01 -07001326 case DIFF_SYMBOL_BINARY_DIFF_BODY:
Stefan Beller68abc6f2017-06-29 17:06:51 -07001327 case DIFF_SYMBOL_CONTEXT_FRAGINFO:
1328 emit_line(o, "", "", line, len);
1329 break;
Stefan Bellerf2bb1212017-06-29 17:06:55 -07001330 case DIFF_SYMBOL_CONTEXT_INCOMPLETE:
Stefan Bellerc64b4202017-06-29 17:06:50 -07001331 case DIFF_SYMBOL_CONTEXT_MARKER:
1332 context = diff_get_color_opt(o, DIFF_CONTEXT);
1333 reset = diff_get_color_opt(o, DIFF_RESET);
1334 emit_line(o, context, reset, line, len);
1335 break;
Stefan Beller36a4cef2017-06-29 17:06:49 -07001336 case DIFF_SYMBOL_SEPARATOR:
1337 fprintf(o->file, "%s%c",
1338 diff_line_prefix(o),
1339 o->line_termination);
1340 break;
Stefan Beller091f8e22017-06-29 17:06:53 -07001341 case DIFF_SYMBOL_CONTEXT:
1342 set = diff_get_color_opt(o, DIFF_CONTEXT);
1343 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001344 set_sign = NULL;
1345 if (o->flags.dual_color_diffed_diffs) {
1346 char c = !len ? 0 : line[0];
1347
1348 if (c == '+')
1349 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1350 else if (c == '@')
1351 set = diff_get_color_opt(o, DIFF_FRAGINFO);
1352 else if (c == '-')
1353 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1354 }
Stefan Beller7648b792018-08-17 13:43:52 -07001355 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001356 OUTPUT_INDICATOR_CONTEXT, line, len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001357 flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
1358 break;
1359 case DIFF_SYMBOL_PLUS:
Stefan Beller86b452e2017-06-30 13:53:09 -07001360 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1361 DIFF_SYMBOL_MOVED_LINE_ALT |
1362 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1363 case DIFF_SYMBOL_MOVED_LINE |
1364 DIFF_SYMBOL_MOVED_LINE_ALT |
1365 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1366 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM);
1367 break;
1368 case DIFF_SYMBOL_MOVED_LINE |
1369 DIFF_SYMBOL_MOVED_LINE_ALT:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001370 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT);
Stefan Beller86b452e2017-06-30 13:53:09 -07001371 break;
1372 case DIFF_SYMBOL_MOVED_LINE |
1373 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1374 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM);
1375 break;
1376 case DIFF_SYMBOL_MOVED_LINE:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001377 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED);
Stefan Beller86b452e2017-06-30 13:53:09 -07001378 break;
1379 default:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001380 set = diff_get_color_opt(o, DIFF_FILE_NEW);
Stefan Beller86b452e2017-06-30 13:53:09 -07001381 }
Stefan Beller091f8e22017-06-29 17:06:53 -07001382 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001383 if (!o->flags.dual_color_diffed_diffs)
1384 set_sign = NULL;
1385 else {
1386 char c = !len ? 0 : line[0];
1387
1388 set_sign = set;
1389 if (c == '-')
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001390 set = diff_get_color_opt(o, DIFF_FILE_OLD_BOLD);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001391 else if (c == '@')
1392 set = diff_get_color_opt(o, DIFF_FRAGINFO);
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001393 else if (c == '+')
1394 set = diff_get_color_opt(o, DIFF_FILE_NEW_BOLD);
1395 else
1396 set = diff_get_color_opt(o, DIFF_CONTEXT_BOLD);
Johannes Schindelin0b91faa2018-08-13 04:33:24 -07001397 flags &= ~DIFF_SYMBOL_CONTENT_WS_MASK;
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001398 }
Stefan Beller7648b792018-08-17 13:43:52 -07001399 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001400 OUTPUT_INDICATOR_NEW, line, len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001401 flags & DIFF_SYMBOL_CONTENT_WS_MASK,
1402 flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
1403 break;
1404 case DIFF_SYMBOL_MINUS:
Stefan Beller86b452e2017-06-30 13:53:09 -07001405 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1406 DIFF_SYMBOL_MOVED_LINE_ALT |
1407 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1408 case DIFF_SYMBOL_MOVED_LINE |
1409 DIFF_SYMBOL_MOVED_LINE_ALT |
1410 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1411 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM);
1412 break;
1413 case DIFF_SYMBOL_MOVED_LINE |
1414 DIFF_SYMBOL_MOVED_LINE_ALT:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001415 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT);
Stefan Beller86b452e2017-06-30 13:53:09 -07001416 break;
1417 case DIFF_SYMBOL_MOVED_LINE |
1418 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1419 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM);
1420 break;
1421 case DIFF_SYMBOL_MOVED_LINE:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001422 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED);
Stefan Beller86b452e2017-06-30 13:53:09 -07001423 break;
1424 default:
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07001425 set = diff_get_color_opt(o, DIFF_FILE_OLD);
Stefan Beller86b452e2017-06-30 13:53:09 -07001426 }
Stefan Beller091f8e22017-06-29 17:06:53 -07001427 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001428 if (!o->flags.dual_color_diffed_diffs)
1429 set_sign = NULL;
1430 else {
1431 char c = !len ? 0 : line[0];
1432
1433 set_sign = set;
1434 if (c == '+')
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001435 set = diff_get_color_opt(o, DIFF_FILE_NEW_DIM);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001436 else if (c == '@')
1437 set = diff_get_color_opt(o, DIFF_FRAGINFO);
Johannes Schindelina7be92a2018-08-13 04:33:32 -07001438 else if (c == '-')
1439 set = diff_get_color_opt(o, DIFF_FILE_OLD_DIM);
1440 else
1441 set = diff_get_color_opt(o, DIFF_CONTEXT_DIM);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001442 }
Stefan Beller7648b792018-08-17 13:43:52 -07001443 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 16:24:59 -07001444 OUTPUT_INDICATOR_OLD, line, len,
Stefan Beller091f8e22017-06-29 17:06:53 -07001445 flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
1446 break;
Stefan Bellerff958672017-06-29 17:06:54 -07001447 case DIFF_SYMBOL_WORDS_PORCELAIN:
1448 context = diff_get_color_opt(o, DIFF_CONTEXT);
1449 reset = diff_get_color_opt(o, DIFF_RESET);
1450 emit_line(o, context, reset, line, len);
1451 fputs("~\n", o->file);
1452 break;
1453 case DIFF_SYMBOL_WORDS:
1454 context = diff_get_color_opt(o, DIFF_CONTEXT);
1455 reset = diff_get_color_opt(o, DIFF_RESET);
1456 /*
1457 * Skip the prefix character, if any. With
1458 * diff_suppress_blank_empty, there may be
1459 * none.
1460 */
1461 if (line[0] != '\n') {
1462 line++;
1463 len--;
1464 }
1465 emit_line(o, context, reset, line, len);
1466 break;
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07001467 case DIFF_SYMBOL_FILEPAIR_PLUS:
1468 meta = diff_get_color_opt(o, DIFF_METAINFO);
1469 reset = diff_get_color_opt(o, DIFF_RESET);
1470 fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
1471 line, reset,
1472 strchr(line, ' ') ? "\t" : "");
1473 break;
1474 case DIFF_SYMBOL_FILEPAIR_MINUS:
1475 meta = diff_get_color_opt(o, DIFF_METAINFO);
1476 reset = diff_get_color_opt(o, DIFF_RESET);
1477 fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta,
1478 line, reset,
1479 strchr(line, ' ') ? "\t" : "");
1480 break;
Stefan Beller4acaaa72017-06-29 17:06:58 -07001481 case DIFF_SYMBOL_BINARY_FILES:
Stefan Bellera29b0a12017-06-29 17:06:57 -07001482 case DIFF_SYMBOL_HEADER:
1483 fprintf(o->file, "%s", line);
1484 break;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07001485 case DIFF_SYMBOL_BINARY_DIFF_HEADER:
1486 fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o));
1487 break;
1488 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA:
1489 fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line);
1490 break;
1491 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL:
1492 fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line);
1493 break;
1494 case DIFF_SYMBOL_BINARY_DIFF_FOOTER:
1495 fputs(diff_line_prefix(o), o->file);
1496 fputc('\n', o->file);
1497 break;
Stefan Beller5af6ea92017-06-29 17:06:59 -07001498 case DIFF_SYMBOL_REWRITE_DIFF:
1499 fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
1500 reset = diff_get_color_opt(o, DIFF_RESET);
1501 emit_line(o, fraginfo, reset, line, len);
1502 break;
Stefan Bellerf3597132017-06-29 17:07:00 -07001503 case DIFF_SYMBOL_SUBMODULE_ADD:
1504 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1505 reset = diff_get_color_opt(o, DIFF_RESET);
1506 emit_line(o, set, reset, line, len);
1507 break;
1508 case DIFF_SYMBOL_SUBMODULE_DEL:
1509 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1510 reset = diff_get_color_opt(o, DIFF_RESET);
1511 emit_line(o, set, reset, line, len);
1512 break;
1513 case DIFF_SYMBOL_SUBMODULE_UNTRACKED:
1514 fprintf(o->file, "%sSubmodule %s contains untracked content\n",
1515 diff_line_prefix(o), line);
1516 break;
1517 case DIFF_SYMBOL_SUBMODULE_MODIFIED:
1518 fprintf(o->file, "%sSubmodule %s contains modified content\n",
1519 diff_line_prefix(o), line);
1520 break;
Stefan Beller0911c472017-06-29 17:07:02 -07001521 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES:
1522 emit_line(o, "", "", " 0 files changed\n",
1523 strlen(" 0 files changed\n"));
1524 break;
1525 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV:
1526 emit_line(o, "", "", " ...\n", strlen(" ...\n"));
1527 break;
Stefan Bellerbd033292017-06-29 17:07:03 -07001528 case DIFF_SYMBOL_WORD_DIFF:
1529 fprintf(o->file, "%.*s", len, line);
1530 break;
Stefan Beller30b7e1e2017-06-29 17:07:04 -07001531 case DIFF_SYMBOL_STAT_SEP:
1532 fputs(o->stat_sep, o->file);
1533 break;
Stefan Beller36a4cef2017-06-29 17:06:49 -07001534 default:
Johannes Schindelin033abf92018-05-02 11:38:39 +02001535 BUG("unknown diff symbol");
Stefan Beller36a4cef2017-06-29 17:06:49 -07001536 }
1537}
1538
Stefan Bellere6e045f2017-06-29 17:07:06 -07001539static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
1540 const char *line, int len, unsigned flags)
1541{
Phillip Woodb4a5c5c2021-12-09 10:30:08 +00001542 struct emitted_diff_symbol e = {
1543 .line = line, .len = len, .flags = flags, .s = s
1544 };
Stefan Bellere6e045f2017-06-29 17:07:06 -07001545
1546 if (o->emitted_symbols)
1547 append_emitted_diff_symbol(o, &e);
1548 else
1549 emit_diff_symbol_from_struct(o, &e);
1550}
1551
Stefan Bellerf3597132017-06-29 17:07:00 -07001552void diff_emit_submodule_del(struct diff_options *o, const char *line)
1553{
1554 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0);
1555}
1556
1557void diff_emit_submodule_add(struct diff_options *o, const char *line)
1558{
1559 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0);
1560}
1561
1562void diff_emit_submodule_untracked(struct diff_options *o, const char *path)
1563{
1564 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED,
1565 path, strlen(path), 0);
1566}
1567
1568void diff_emit_submodule_modified(struct diff_options *o, const char *path)
1569{
1570 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED,
1571 path, strlen(path), 0);
1572}
1573
1574void diff_emit_submodule_header(struct diff_options *o, const char *header)
1575{
1576 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER,
1577 header, strlen(header), 0);
1578}
1579
1580void diff_emit_submodule_error(struct diff_options *o, const char *err)
1581{
1582 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0);
1583}
1584
1585void diff_emit_submodule_pipethrough(struct diff_options *o,
1586 const char *line, int len)
1587{
1588 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0);
1589}
1590
Junio C Hamano6957eb92009-09-14 18:44:01 -07001591static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
1592{
1593 if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
1594 ecbdata->blank_at_eof_in_preimage &&
1595 ecbdata->blank_at_eof_in_postimage &&
1596 ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
1597 ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
1598 return 0;
Jeff Kingc5224f02022-12-13 06:12:58 -05001599 return ws_blank_line(line, len);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001600}
1601
Jeff Kinge04df612019-02-14 00:48:13 -05001602static void emit_add_line(struct emit_callback *ecbdata,
Junio C Hamano018cff72009-09-14 18:44:01 -07001603 const char *line, int len)
Junio C Hamano6957eb92009-09-14 18:44:01 -07001604{
Stefan Beller091f8e22017-06-29 17:06:53 -07001605 unsigned flags = WSEH_NEW | ecbdata->ws_rule;
1606 if (new_blank_line_at_eof(ecbdata, line, len))
1607 flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF;
1608
1609 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001610}
Junio C Hamano6957eb92009-09-14 18:44:01 -07001611
Jeff Kinge04df612019-02-14 00:48:13 -05001612static void emit_del_line(struct emit_callback *ecbdata,
Junio C Hamano0e383e12015-05-26 09:56:33 -07001613 const char *line, int len)
1614{
Stefan Beller091f8e22017-06-29 17:06:53 -07001615 unsigned flags = WSEH_OLD | ecbdata->ws_rule;
1616 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags);
Junio C Hamano0e383e12015-05-26 09:56:33 -07001617}
1618
Jeff Kinge04df612019-02-14 00:48:13 -05001619static void emit_context_line(struct emit_callback *ecbdata,
Junio C Hamano0e383e12015-05-26 09:56:33 -07001620 const char *line, int len)
1621{
Stefan Beller091f8e22017-06-29 17:06:53 -07001622 unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule;
1623 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags);
Junio C Hamano6957eb92009-09-14 18:44:01 -07001624}
1625
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001626static void emit_hunk_header(struct emit_callback *ecbdata,
1627 const char *line, int len)
1628{
Jeff King8dbf3eb2015-05-27 16:48:46 -04001629 const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001630 const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
1631 const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
1632 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001633 const char *reverse = ecbdata->color_diff ? GIT_COLOR_REVERSE : "";
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001634 static const char atat[2] = { '@', '@' };
1635 const char *cp, *ep;
Bo Yang2efcc972010-05-29 23:32:05 +08001636 struct strbuf msgbuf = STRBUF_INIT;
1637 int org_len = len;
1638 int i = 1;
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001639
1640 /*
1641 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1642 * it always is at least 10 bytes long.
1643 */
1644 if (len < 10 ||
1645 memcmp(line, atat, 2) ||
1646 !(ep = memmem(line + 2, len - 2, atat, 2))) {
Stefan Bellerc64b4202017-06-29 17:06:50 -07001647 emit_diff_symbol(ecbdata->opt,
Stefan Beller091f8e22017-06-29 17:06:53 -07001648 DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001649 return;
1650 }
1651 ep += 2; /* skip over @@ */
1652
1653 /* The hunk header in fraginfo color */
Johannes Schindelinf7c3b4e2018-08-13 04:33:20 -07001654 if (ecbdata->opt->flags.dual_color_diffed_diffs)
1655 strbuf_addstr(&msgbuf, reverse);
René Scharfecedc61a2014-07-17 01:38:18 +02001656 strbuf_addstr(&msgbuf, frag);
Thomas Gummerer430be362019-07-11 17:08:48 +01001657 if (ecbdata->opt->flags.suppress_hunk_header_line_count)
1658 strbuf_add(&msgbuf, atat, sizeof(atat));
1659 else
1660 strbuf_add(&msgbuf, line, ep - line);
René Scharfecedc61a2014-07-17 01:38:18 +02001661 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001662
1663 /*
1664 * trailing "\r\n"
1665 */
1666 for ( ; i < 3; i++)
1667 if (line[len - i] == '\r' || line[len - i] == '\n')
1668 len--;
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001669
1670 /* blank before the func header */
1671 for (cp = ep; ep - line < len; ep++)
1672 if (*ep != ' ' && *ep != '\t')
1673 break;
Bo Yang2efcc972010-05-29 23:32:05 +08001674 if (ep != cp) {
Jeff King8dbf3eb2015-05-27 16:48:46 -04001675 strbuf_addstr(&msgbuf, context);
Bo Yang2efcc972010-05-29 23:32:05 +08001676 strbuf_add(&msgbuf, cp, ep - cp);
René Scharfecedc61a2014-07-17 01:38:18 +02001677 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001678 }
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001679
Bo Yang2efcc972010-05-29 23:32:05 +08001680 if (ep < line + len) {
René Scharfecedc61a2014-07-17 01:38:18 +02001681 strbuf_addstr(&msgbuf, func);
Bo Yang2efcc972010-05-29 23:32:05 +08001682 strbuf_add(&msgbuf, ep, line + len - ep);
René Scharfecedc61a2014-07-17 01:38:18 +02001683 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 23:32:05 +08001684 }
1685
1686 strbuf_add(&msgbuf, line + len, org_len - len);
Stefan Bellerdfb77282017-06-29 17:06:47 -07001687 strbuf_complete_line(&msgbuf);
Stefan Beller68abc6f2017-06-29 17:06:51 -07001688 emit_diff_symbol(ecbdata->opt,
Stefan Beller091f8e22017-06-29 17:06:53 -07001689 DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0);
Bo Yang2efcc972010-05-29 23:32:05 +08001690 strbuf_release(&msgbuf);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01001691}
1692
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +01001693static struct diff_tempfile *claim_diff_tempfile(void)
1694{
Jeff King479b0ae2009-01-22 00:59:56 -05001695 int i;
1696 for (i = 0; i < ARRAY_SIZE(diff_temp); i++)
1697 if (!diff_temp[i].name)
1698 return diff_temp + i;
Johannes Schindelin033abf92018-05-02 11:38:39 +02001699 BUG("diff is failing to clean up its tempfiles");
Jeff King479b0ae2009-01-22 00:59:56 -05001700}
1701
Jeff King479b0ae2009-01-22 00:59:56 -05001702static void remove_tempfile(void)
1703{
1704 int i;
Nazri Ramliya8344ab2009-02-12 21:36:14 +08001705 for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
Jeff King076aa2c2017-09-05 08:15:08 -04001706 if (is_tempfile_active(diff_temp[i].tempfile))
Michael Haggerty284098f2015-08-12 19:12:01 +02001707 delete_tempfile(&diff_temp[i].tempfile);
Nazri Ramliya8344ab2009-02-12 21:36:14 +08001708 diff_temp[i].name = NULL;
1709 }
Jeff King479b0ae2009-01-22 00:59:56 -05001710}
1711
Stefan Beller5af6ea92017-06-29 17:06:59 -07001712static void add_line_count(struct strbuf *out, int count)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001713{
1714 switch (count) {
1715 case 0:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001716 strbuf_addstr(out, "0,0");
Junio C Hamano6973dca2006-04-21 23:57:45 -07001717 break;
1718 case 1:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001719 strbuf_addstr(out, "1");
Junio C Hamano6973dca2006-04-21 23:57:45 -07001720 break;
1721 default:
Stefan Beller5af6ea92017-06-29 17:06:59 -07001722 strbuf_addf(out, "1,%d", count);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001723 break;
1724 }
1725}
1726
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001727static void emit_rewrite_lines(struct emit_callback *ecb,
1728 int prefix, const char *data, int size)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001729{
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001730 const char *endp = NULL;
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001731
1732 while (0 < size) {
1733 int len;
1734
1735 endp = memchr(data, '\n', size);
1736 len = endp ? (endp - data + 1) : size;
1737 if (prefix != '+') {
1738 ecb->lno_in_preimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05001739 emit_del_line(ecb, data, len);
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001740 } else {
1741 ecb->lno_in_postimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05001742 emit_add_line(ecb, data, len);
Johannes Schindelin13e36ec2007-02-20 15:08:46 +01001743 }
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001744 size -= len;
1745 data += len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07001746 }
Stefan Bellerb9cbfde2017-06-29 17:06:52 -07001747 if (!endp)
Stefan Beller091f8e22017-06-29 17:06:53 -07001748 emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001749}
1750
1751static void emit_rewrite_diff(const char *name_a,
1752 const char *name_b,
1753 struct diff_filespec *one,
Johannes Schindelin13e36ec2007-02-20 15:08:46 +01001754 struct diff_filespec *two,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04001755 struct userdiff_driver *textconv_one,
1756 struct userdiff_driver *textconv_two,
Johannes Schindelineab9a402007-12-18 19:32:14 +00001757 struct diff_options *o)
Junio C Hamano6973dca2006-04-21 23:57:45 -07001758{
1759 int lc_a, lc_b;
Junio C Hamanod5625092007-12-26 17:13:36 -08001760 static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001761 const char *a_prefix, *b_prefix;
Jeff King840383b2010-04-01 20:09:26 -04001762 char *data_one, *data_two;
Jeff King3aa1f7c2008-12-09 03:13:21 -05001763 size_t size_one, size_two;
Junio C Hamano7f7ee2f2009-09-14 18:44:01 -07001764 struct emit_callback ecbdata;
Stefan Beller5af6ea92017-06-29 17:06:59 -07001765 struct strbuf out = STRBUF_INIT;
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001766
Brandon Williams0d1e0e72017-10-31 11:19:11 -07001767 if (diff_mnemonic_prefix && o->flags.reverse_diff) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001768 a_prefix = o->b_prefix;
1769 b_prefix = o->a_prefix;
1770 } else {
1771 a_prefix = o->a_prefix;
1772 b_prefix = o->b_prefix;
1773 }
Junio C Hamano1a9eb3b2006-09-22 16:17:58 -07001774
Junio C Hamano8a13bec2007-02-24 01:42:06 -08001775 name_a += (*name_a == '/');
1776 name_b += (*name_b == '/');
Junio C Hamano1a9eb3b2006-09-22 16:17:58 -07001777
Junio C Hamanod5625092007-12-26 17:13:36 -08001778 strbuf_reset(&a_name);
1779 strbuf_reset(&b_name);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07001780 quote_two_c_style(&a_name, a_prefix, name_a, 0);
1781 quote_two_c_style(&b_name, b_prefix, name_b, 0);
Junio C Hamanod5625092007-12-26 17:13:36 -08001782
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02001783 size_one = fill_textconv(o->repo, textconv_one, one, &data_one);
1784 size_two = fill_textconv(o->repo, textconv_two, two, &data_two);
Jeff King3aa1f7c2008-12-09 03:13:21 -05001785
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001786 memset(&ecbdata, 0, sizeof(ecbdata));
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07001787 ecbdata.color_diff = want_color(o->use_color);
Nguyễn Thái Ngọc Duy26d024e2018-09-21 17:57:37 +02001788 ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
Bo Yanga3c158d2010-05-26 15:08:02 +08001789 ecbdata.opt = o;
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001790 if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
1791 mmfile_t mf1, mf2;
1792 mf1.ptr = (char *)data_one;
1793 mf2.ptr = (char *)data_two;
1794 mf1.size = size_one;
1795 mf2.size = size_two;
1796 check_blank_at_eof(&mf1, &mf2, &ecbdata);
1797 }
1798 ecbdata.lno_in_preimage = 1;
1799 ecbdata.lno_in_postimage = 1;
1800
Jeff King3aa1f7c2008-12-09 03:13:21 -05001801 lc_a = count_lines(data_one, size_one);
1802 lc_b = count_lines(data_two, size_two);
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07001803
1804 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
1805 a_name.buf, a_name.len, 0);
1806 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
1807 b_name.buf, b_name.len, 0);
1808
Stefan Beller5af6ea92017-06-29 17:06:59 -07001809 strbuf_addstr(&out, "@@ -");
Junio C Hamano467ddc12011-02-28 16:11:55 -08001810 if (!o->irreversible_delete)
Stefan Beller5af6ea92017-06-29 17:06:59 -07001811 add_line_count(&out, lc_a);
Junio C Hamano467ddc12011-02-28 16:11:55 -08001812 else
Stefan Beller5af6ea92017-06-29 17:06:59 -07001813 strbuf_addstr(&out, "?,?");
1814 strbuf_addstr(&out, " +");
1815 add_line_count(&out, lc_b);
1816 strbuf_addstr(&out, " @@\n");
1817 emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
1818 strbuf_release(&out);
1819
Junio C Hamano467ddc12011-02-28 16:11:55 -08001820 if (lc_a && !o->irreversible_delete)
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001821 emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001822 if (lc_b)
Junio C Hamanod91ba8f2009-09-15 11:21:10 -07001823 emit_rewrite_lines(&ecbdata, '+', data_two, size_two);
Jeff Kingb76c0562010-04-01 20:04:14 -04001824 if (textconv_one)
Junio C Hamanoaed6ca52010-04-08 23:30:49 -07001825 free((char *)data_one);
Jeff Kingb76c0562010-04-01 20:04:14 -04001826 if (textconv_two)
Junio C Hamanoaed6ca52010-04-08 23:30:49 -07001827 free((char *)data_two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07001828}
1829
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001830struct diff_words_buffer {
1831 mmfile_t text;
Ramsay Jones071bcaa2017-09-21 17:49:38 +01001832 unsigned long alloc;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001833 struct diff_words_orig {
1834 const char *begin, *end;
1835 } *orig;
1836 int orig_nr, orig_alloc;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001837};
1838
1839static void diff_words_append(char *line, unsigned long len,
1840 struct diff_words_buffer *buffer)
1841{
Johannes Schindelin23c15752009-01-17 17:29:43 +01001842 ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc);
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001843 line++;
1844 len--;
1845 memcpy(buffer->text.ptr + buffer->text.size, line, len);
1846 buffer->text.size += len;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001847 buffer->text.ptr[buffer->text.size] = '\0';
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001848}
1849
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001850struct diff_words_style_elem {
Thomas Rast882749a2010-04-14 17:59:06 +02001851 const char *prefix;
1852 const char *suffix;
1853 const char *color; /* NULL; filled in by the setup code if
1854 * color is enabled */
1855};
1856
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001857struct diff_words_style {
Thomas Rast882749a2010-04-14 17:59:06 +02001858 enum diff_words_type type;
Brandon Williams63a01c32018-02-14 10:59:39 -08001859 struct diff_words_style_elem new_word, old_word, ctx;
Thomas Rast882749a2010-04-14 17:59:06 +02001860 const char *newline;
1861};
1862
Stephen Boydc2e86ad2011-03-22 00:51:05 -07001863static struct diff_words_style diff_words_styles[] = {
Thomas Rast882749a2010-04-14 17:59:06 +02001864 { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1865 { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1866 { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" }
1867};
1868
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001869struct diff_words_data {
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001870 struct diff_words_buffer minus, plus;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001871 const char *current_plus;
Bo Yang4297c0a2010-05-29 23:32:06 +08001872 int last_minus;
1873 struct diff_options *opt;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01001874 regex_t *word_regex;
Thomas Rast882749a2010-04-14 17:59:06 +02001875 enum diff_words_type type;
1876 struct diff_words_style *style;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001877};
1878
Stefan Bellerbd033292017-06-29 17:07:03 -07001879static int fn_out_diff_words_write_helper(struct diff_options *o,
Thomas Rast882749a2010-04-14 17:59:06 +02001880 struct diff_words_style_elem *st_el,
1881 const char *newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07001882 size_t count, const char *buf)
Thomas Rast882749a2010-04-14 17:59:06 +02001883{
Bo Yang4297c0a2010-05-29 23:32:06 +08001884 int print = 0;
Stefan Bellerbd033292017-06-29 17:07:03 -07001885 struct strbuf sb = STRBUF_INIT;
Bo Yang4297c0a2010-05-29 23:32:06 +08001886
Thomas Rast882749a2010-04-14 17:59:06 +02001887 while (count) {
1888 char *p = memchr(buf, '\n', count);
Bo Yang4297c0a2010-05-29 23:32:06 +08001889 if (print)
Stefan Bellerbd033292017-06-29 17:07:03 -07001890 strbuf_addstr(&sb, diff_line_prefix(o));
1891
Thomas Rast882749a2010-04-14 17:59:06 +02001892 if (p != buf) {
Stefan Bellerbd033292017-06-29 17:07:03 -07001893 const char *reset = st_el->color && *st_el->color ?
1894 GIT_COLOR_RESET : NULL;
1895 if (st_el->color && *st_el->color)
1896 strbuf_addstr(&sb, st_el->color);
1897 strbuf_addstr(&sb, st_el->prefix);
1898 strbuf_add(&sb, buf, p ? p - buf : count);
1899 strbuf_addstr(&sb, st_el->suffix);
1900 if (reset)
1901 strbuf_addstr(&sb, reset);
Thomas Rast882749a2010-04-14 17:59:06 +02001902 }
1903 if (!p)
Stefan Bellerbd033292017-06-29 17:07:03 -07001904 goto out;
1905
1906 strbuf_addstr(&sb, newline);
Thomas Rast882749a2010-04-14 17:59:06 +02001907 count -= p + 1 - buf;
1908 buf = p + 1;
Bo Yang4297c0a2010-05-29 23:32:06 +08001909 print = 1;
Stefan Bellerbd033292017-06-29 17:07:03 -07001910 if (count) {
1911 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1912 sb.buf, sb.len, 0);
1913 strbuf_reset(&sb);
1914 }
Thomas Rast882749a2010-04-14 17:59:06 +02001915 }
Stefan Bellerbd033292017-06-29 17:07:03 -07001916
1917out:
1918 if (sb.len)
1919 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1920 sb.buf, sb.len, 0);
1921 strbuf_release(&sb);
Thomas Rast882749a2010-04-14 17:59:06 +02001922 return 0;
1923}
1924
Bo Yang4297c0a2010-05-29 23:32:06 +08001925/*
1926 * '--color-words' algorithm can be described as:
1927 *
René Genz56217602017-04-30 17:48:27 +02001928 * 1. collect the minus/plus lines of a diff hunk, divided into
Bo Yang4297c0a2010-05-29 23:32:06 +08001929 * minus-lines and plus-lines;
1930 *
1931 * 2. break both minus-lines and plus-lines into words and
1932 * place them into two mmfile_t with one word for each line;
1933 *
1934 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1935 *
1936 * And for the common parts of the both file, we output the plus side text.
1937 * diff_words->current_plus is used to trace the current position of the plus file
1938 * which printed. diff_words->last_minus is used to trace the last minus word
1939 * printed.
1940 *
1941 * For '--graph' to work with '--color-words', we need to output the graph prefix
1942 * on each line of color words output. Generally, there are two conditions on
1943 * which we should output the prefix.
1944 *
1945 * 1. diff_words->last_minus == 0 &&
1946 * diff_words->current_plus == diff_words->plus.text.ptr
1947 *
1948 * that is: the plus text must start as a new line, and if there is no minus
1949 * word printed, a graph prefix must be printed.
1950 *
1951 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1952 * *(diff_words->current_plus - 1) == '\n'
1953 *
1954 * that is: a graph prefix must be printed following a '\n'
1955 */
1956static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
1957{
1958 if ((diff_words->last_minus == 0 &&
1959 diff_words->current_plus == diff_words->plus.text.ptr) ||
1960 (diff_words->current_plus > diff_words->plus.text.ptr &&
1961 *(diff_words->current_plus - 1) == '\n')) {
1962 return 1;
1963 } else {
1964 return 0;
1965 }
1966}
1967
Jeff King7c61e252018-11-02 02:37:18 -04001968static void fn_out_diff_words_aux(void *priv,
1969 long minus_first, long minus_len,
1970 long plus_first, long plus_len,
Jeff King61bdc7c2022-12-13 06:13:48 -05001971 const char *func UNUSED, long funclen UNUSED)
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001972{
1973 struct diff_words_data *diff_words = priv;
Thomas Rast882749a2010-04-14 17:59:06 +02001974 struct diff_words_style *style = diff_words->style;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001975 const char *minus_begin, *minus_end, *plus_begin, *plus_end;
Bo Yang4297c0a2010-05-29 23:32:06 +08001976 struct diff_options *opt = diff_words->opt;
John Keeping30997bb2013-02-07 20:15:27 +00001977 const char *line_prefix;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02001978
Bo Yang4297c0a2010-05-29 23:32:06 +08001979 assert(opt);
John Keeping30997bb2013-02-07 20:15:27 +00001980 line_prefix = diff_line_prefix(opt);
Bo Yang4297c0a2010-05-29 23:32:06 +08001981
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01001982 /* POSIX requires that first be decremented by one if len == 0... */
1983 if (minus_len) {
1984 minus_begin = diff_words->minus.orig[minus_first].begin;
1985 minus_end =
1986 diff_words->minus.orig[minus_first + minus_len - 1].end;
1987 } else
1988 minus_begin = minus_end =
1989 diff_words->minus.orig[minus_first].end;
1990
1991 if (plus_len) {
1992 plus_begin = diff_words->plus.orig[plus_first].begin;
1993 plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end;
1994 } else
1995 plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
1996
Bo Yang4297c0a2010-05-29 23:32:06 +08001997 if (color_words_output_graph_prefix(diff_words)) {
1998 fputs(line_prefix, diff_words->opt->file);
1999 }
2000 if (diff_words->current_plus != plus_begin) {
Stefan Bellerbd033292017-06-29 17:07:03 -07002001 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02002002 &style->ctx, style->newline,
2003 plus_begin - diff_words->current_plus,
Stefan Bellerbd033292017-06-29 17:07:03 -07002004 diff_words->current_plus);
Bo Yang4297c0a2010-05-29 23:32:06 +08002005 }
2006 if (minus_begin != minus_end) {
Stefan Bellerbd033292017-06-29 17:07:03 -07002007 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 10:59:39 -08002008 &style->old_word, style->newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07002009 minus_end - minus_begin, minus_begin);
Bo Yang4297c0a2010-05-29 23:32:06 +08002010 }
2011 if (plus_begin != plus_end) {
Stefan Bellerbd033292017-06-29 17:07:03 -07002012 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 10:59:39 -08002013 &style->new_word, style->newline,
Stefan Bellerbd033292017-06-29 17:07:03 -07002014 plus_end - plus_begin, plus_begin);
Bo Yang4297c0a2010-05-29 23:32:06 +08002015 }
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002016
2017 diff_words->current_plus = plus_end;
Bo Yang4297c0a2010-05-29 23:32:06 +08002018 diff_words->last_minus = minus_first;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002019}
2020
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002021/* This function starts looking at *begin, and returns 0 iff a word was found. */
2022static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
2023 int *begin, int *end)
2024{
Phillip Wood0324e8f2021-05-04 09:27:34 +00002025 while (word_regex && *begin < buffer->size) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002026 regmatch_t match[1];
Johannes Schindelinb7d36ff2016-09-21 20:24:14 +02002027 if (!regexec_buf(word_regex, buffer->ptr + *begin,
2028 buffer->size - *begin, 1, match, 0)) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002029 char *p = memchr(buffer->ptr + *begin + match[0].rm_so,
2030 '\n', match[0].rm_eo - match[0].rm_so);
2031 *end = p ? p - buffer->ptr : match[0].rm_eo + *begin;
2032 *begin += match[0].rm_so;
Phillip Wood0324e8f2021-05-04 09:27:34 +00002033 if (*begin == *end)
2034 (*begin)++;
2035 else
2036 return *begin > *end;
2037 } else {
2038 return -1;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002039 }
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002040 }
2041
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002042 /* find the next word */
2043 while (*begin < buffer->size && isspace(buffer->ptr[*begin]))
2044 (*begin)++;
2045 if (*begin >= buffer->size)
2046 return -1;
2047
2048 /* find the end of the word */
2049 *end = *begin + 1;
2050 while (*end < buffer->size && !isspace(buffer->ptr[*end]))
2051 (*end)++;
2052
2053 return 0;
2054}
2055
Johannes Schindelin23c15752009-01-17 17:29:43 +01002056/*
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002057 * This function splits the words in buffer->text, stores the list with
2058 * newline separator into out, and saves the offsets of the original words
2059 * in buffer->orig.
Johannes Schindelin23c15752009-01-17 17:29:43 +01002060 */
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002061static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out,
2062 regex_t *word_regex)
Johannes Schindelin23c15752009-01-17 17:29:43 +01002063{
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002064 int i, j;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002065 long alloc = 0;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002066
2067 out->size = 0;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002068 out->ptr = NULL;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002069
2070 /* fake an empty "0th" word */
2071 ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc);
2072 buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr;
2073 buffer->orig_nr = 1;
2074
2075 for (i = 0; i < buffer->text.size; i++) {
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002076 if (find_word_boundaries(&buffer->text, word_regex, &i, &j))
2077 return;
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002078
2079 /* store original boundaries */
2080 ALLOC_GROW(buffer->orig, buffer->orig_nr + 1,
2081 buffer->orig_alloc);
2082 buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i;
2083 buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j;
2084 buffer->orig_nr++;
2085
2086 /* store one word */
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002087 ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002088 memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i);
2089 out->ptr[out->size + j - i] = '\n';
2090 out->size += j - i + 1;
2091
2092 i = j - 1;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002093 }
2094}
2095
2096/* this executes the word diff on the accumulated buffers */
2097static void diff_words_show(struct diff_words_data *diff_words)
2098{
2099 xpparam_t xpp;
2100 xdemitconf_t xecfg;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002101 mmfile_t minus, plus;
Thomas Rast882749a2010-04-14 17:59:06 +02002102 struct diff_words_style *style = diff_words->style;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002103
Bo Yang4297c0a2010-05-29 23:32:06 +08002104 struct diff_options *opt = diff_words->opt;
John Keeping30997bb2013-02-07 20:15:27 +00002105 const char *line_prefix;
Bo Yang4297c0a2010-05-29 23:32:06 +08002106
2107 assert(opt);
John Keeping30997bb2013-02-07 20:15:27 +00002108 line_prefix = diff_line_prefix(opt);
Bo Yang4297c0a2010-05-29 23:32:06 +08002109
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002110 /* special case: only removal */
2111 if (!diff_words->plus.text.size) {
Stefan Bellerbd033292017-06-29 17:07:03 -07002112 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
2113 line_prefix, strlen(line_prefix), 0);
2114 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 10:59:39 -08002115 &style->old_word, style->newline,
Bo Yang4297c0a2010-05-29 23:32:06 +08002116 diff_words->minus.text.size,
Stefan Bellerbd033292017-06-29 17:07:03 -07002117 diff_words->minus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002118 diff_words->minus.text.size = 0;
2119 return;
2120 }
2121
2122 diff_words->current_plus = diff_words->plus.text.ptr;
Bo Yang4297c0a2010-05-29 23:32:06 +08002123 diff_words->last_minus = 0;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002124
Brian Downing9ccd0a82008-10-25 15:30:37 +02002125 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01002126 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002127 diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex);
2128 diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex);
René Scharfe582aa002010-05-02 15:04:41 +02002129 xpp.flags = 0;
Johannes Schindelin2b6a5412009-01-17 17:29:45 +01002130 /* as only the hunk header will be parsed, we need a 0-context */
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002131 xecfg.ctxlen = 0;
Jeff King7c61e252018-11-02 02:37:18 -04002132 if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, NULL,
Jeff King9346d6d2018-11-02 02:35:45 -04002133 diff_words, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04002134 die("unable to generate word diff");
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002135 free(minus.ptr);
2136 free(plus.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002137 if (diff_words->current_plus != diff_words->plus.text.ptr +
Bo Yang4297c0a2010-05-29 23:32:06 +08002138 diff_words->plus.text.size) {
2139 if (color_words_output_graph_prefix(diff_words))
Stefan Bellerbd033292017-06-29 17:07:03 -07002140 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
2141 line_prefix, strlen(line_prefix), 0);
2142 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 17:59:06 +02002143 &style->ctx, style->newline,
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002144 diff_words->plus.text.ptr + diff_words->plus.text.size
Stefan Bellerbd033292017-06-29 17:07:03 -07002145 - diff_words->current_plus, diff_words->current_plus);
Bo Yang4297c0a2010-05-29 23:32:06 +08002146 }
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002147 diff_words->minus.text.size = diff_words->plus.text.size = 0;
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002148}
2149
Junio C Hamano76fd2822009-10-30 10:09:06 -07002150/* In "color-words" mode, show word-diff of words accumulated in the buffer */
2151static void diff_words_flush(struct emit_callback *ecbdata)
2152{
Stefan Bellere6e045f2017-06-29 17:07:06 -07002153 struct diff_options *wo = ecbdata->diff_words->opt;
2154
Junio C Hamano76fd2822009-10-30 10:09:06 -07002155 if (ecbdata->diff_words->minus.text.size ||
2156 ecbdata->diff_words->plus.text.size)
2157 diff_words_show(ecbdata->diff_words);
Stefan Bellere6e045f2017-06-29 17:07:06 -07002158
2159 if (wo->emitted_symbols) {
2160 struct diff_options *o = ecbdata->opt;
2161 struct emitted_diff_symbols *wol = wo->emitted_symbols;
2162 int i;
2163
2164 /*
2165 * NEEDSWORK:
2166 * Instead of appending each, concat all words to a line?
2167 */
2168 for (i = 0; i < wol->nr; i++)
2169 append_emitted_diff_symbol(o, &wol->buf[i]);
2170
2171 for (i = 0; i < wol->nr; i++)
2172 free((void *)wol->buf[i].line);
2173
2174 wol->nr = 0;
2175 }
Junio C Hamano76fd2822009-10-30 10:09:06 -07002176}
2177
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002178static void diff_filespec_load_driver(struct diff_filespec *one,
2179 struct index_state *istate)
Thomas Rast77d1a522012-03-14 19:24:08 +01002180{
2181 /* Use already-loaded driver */
2182 if (one->driver)
2183 return;
2184
2185 if (S_ISREG(one->mode))
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002186 one->driver = userdiff_find_by_path(istate, one->path);
Thomas Rast77d1a522012-03-14 19:24:08 +01002187
2188 /* Fallback to default settings */
2189 if (!one->driver)
2190 one->driver = userdiff_find_by_name("default");
2191}
2192
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002193static const char *userdiff_word_regex(struct diff_filespec *one,
2194 struct index_state *istate)
Thomas Rast77d1a522012-03-14 19:24:08 +01002195{
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002196 diff_filespec_load_driver(one, istate);
Thomas Rast77d1a522012-03-14 19:24:08 +01002197 return one->driver->word_regex;
2198}
2199
2200static void init_diff_words_data(struct emit_callback *ecbdata,
Thomas Rast6440d342012-03-14 19:24:09 +01002201 struct diff_options *orig_opts,
Thomas Rast77d1a522012-03-14 19:24:08 +01002202 struct diff_filespec *one,
2203 struct diff_filespec *two)
2204{
2205 int i;
Thomas Rast6440d342012-03-14 19:24:09 +01002206 struct diff_options *o = xmalloc(sizeof(struct diff_options));
2207 memcpy(o, orig_opts, sizeof(struct diff_options));
Thomas Rast77d1a522012-03-14 19:24:08 +01002208
René Scharfeca56dad2021-03-13 17:17:22 +01002209 CALLOC_ARRAY(ecbdata->diff_words, 1);
Thomas Rast77d1a522012-03-14 19:24:08 +01002210 ecbdata->diff_words->type = o->word_diff;
2211 ecbdata->diff_words->opt = o;
Stefan Bellere6e045f2017-06-29 17:07:06 -07002212
2213 if (orig_opts->emitted_symbols)
René Scharfeca56dad2021-03-13 17:17:22 +01002214 CALLOC_ARRAY(o->emitted_symbols, 1);
Stefan Bellere6e045f2017-06-29 17:07:06 -07002215
Thomas Rast77d1a522012-03-14 19:24:08 +01002216 if (!o->word_regex)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002217 o->word_regex = userdiff_word_regex(one, o->repo->index);
Thomas Rast77d1a522012-03-14 19:24:08 +01002218 if (!o->word_regex)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02002219 o->word_regex = userdiff_word_regex(two, o->repo->index);
Thomas Rast77d1a522012-03-14 19:24:08 +01002220 if (!o->word_regex)
2221 o->word_regex = diff_word_regex_cfg;
2222 if (o->word_regex) {
2223 ecbdata->diff_words->word_regex = (regex_t *)
2224 xmalloc(sizeof(regex_t));
2225 if (regcomp(ecbdata->diff_words->word_regex,
2226 o->word_regex,
2227 REG_EXTENDED | REG_NEWLINE))
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02002228 die("invalid regular expression: %s",
2229 o->word_regex);
Thomas Rast77d1a522012-03-14 19:24:08 +01002230 }
2231 for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) {
2232 if (o->word_diff == diff_words_styles[i].type) {
2233 ecbdata->diff_words->style =
2234 &diff_words_styles[i];
2235 break;
2236 }
2237 }
2238 if (want_color(o->use_color)) {
2239 struct diff_words_style *st = ecbdata->diff_words->style;
Brandon Williams63a01c32018-02-14 10:59:39 -08002240 st->old_word.color = diff_get_color_opt(o, DIFF_FILE_OLD);
2241 st->new_word.color = diff_get_color_opt(o, DIFF_FILE_NEW);
Jeff King8dbf3eb2015-05-27 16:48:46 -04002242 st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT);
Thomas Rast77d1a522012-03-14 19:24:08 +01002243 }
2244}
2245
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002246static void free_diff_words_data(struct emit_callback *ecbdata)
2247{
2248 if (ecbdata->diff_words) {
Junio C Hamano76fd2822009-10-30 10:09:06 -07002249 diff_words_flush(ecbdata);
Ævar Arnfjörð Bjarmason77e56d52022-03-17 15:55:35 +01002250 free_emitted_diff_symbols(ecbdata->diff_words->opt->emitted_symbols);
Thomas Rast6440d342012-03-14 19:24:09 +01002251 free (ecbdata->diff_words->opt);
Jim Meyering8e0f7002008-01-31 18:26:32 +01002252 free (ecbdata->diff_words->minus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002253 free (ecbdata->diff_words->minus.orig);
Jim Meyering8e0f7002008-01-31 18:26:32 +01002254 free (ecbdata->diff_words->plus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 17:29:44 +01002255 free (ecbdata->diff_words->plus.orig);
Brandon Caseyef5644e2010-09-09 14:02:45 -05002256 if (ecbdata->diff_words->word_regex) {
2257 regfree(ecbdata->diff_words->word_regex);
2258 free(ecbdata->diff_words->word_regex);
2259 }
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002260 FREE_AND_NULL(ecbdata->diff_words);
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02002261 }
2262}
2263
Jeff Kingce436972006-07-23 05:24:18 -04002264const char *diff_get_color(int diff_use_color, enum color_diff ix)
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002265{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07002266 if (want_color(diff_use_color))
Linus Torvalds50f575f2006-06-22 13:53:31 -07002267 return diff_colors[ix];
2268 return "";
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002269}
2270
John Keepingf1922232013-02-07 20:15:26 +00002271const char *diff_line_prefix(struct diff_options *opt)
2272{
2273 struct strbuf *msgbuf;
2274 if (!opt->output_prefix)
2275 return "";
2276
2277 msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
2278 return msgbuf->buf;
2279}
2280
Jeff King19b90462019-02-14 00:48:27 -05002281static unsigned long sane_truncate_line(char *line, unsigned long len)
Junio C Hamano23707812008-01-02 01:50:11 -08002282{
2283 const char *cp;
2284 unsigned long allot;
2285 size_t l = len;
2286
Junio C Hamano23707812008-01-02 01:50:11 -08002287 cp = line;
2288 allot = l;
2289 while (0 < l) {
2290 (void) utf8_width(&cp, &l);
2291 if (!cp)
2292 break; /* truncated in the middle? */
2293 }
2294 return allot - l;
2295}
2296
Junio C Hamanod68fe262009-09-14 22:05:57 -07002297static void find_lno(const char *line, struct emit_callback *ecbdata)
Junio C Hamano690ed842009-09-04 00:41:15 -07002298{
Junio C Hamanod68fe262009-09-14 22:05:57 -07002299 const char *p;
2300 ecbdata->lno_in_preimage = 0;
2301 ecbdata->lno_in_postimage = 0;
2302 p = strchr(line, '-');
Junio C Hamano690ed842009-09-04 00:41:15 -07002303 if (!p)
Junio C Hamanod68fe262009-09-14 22:05:57 -07002304 return; /* cannot happen */
2305 ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10);
2306 p = strchr(p, '+');
2307 if (!p)
2308 return; /* cannot happen */
2309 ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10);
Junio C Hamano690ed842009-09-04 00:41:15 -07002310}
2311
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002312static int fn_out_consume(void *priv, char *line, unsigned long len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002313{
Junio C Hamano6973dca2006-04-21 23:57:45 -07002314 struct emit_callback *ecbdata = priv;
Bo Yang7be57612010-05-26 15:23:54 +08002315 struct diff_options *o = ecbdata->opt;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002316
Stefan Bellerba162332016-09-07 16:36:46 -07002317 o->found_changes = 1;
2318
Greg Bacon3e97c7c2009-11-19 15:12:24 -06002319 if (ecbdata->header) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07002320 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
2321 ecbdata->header->buf, ecbdata->header->len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06002322 strbuf_reset(ecbdata->header);
2323 ecbdata->header = NULL;
2324 }
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01002325
Junio C Hamano6973dca2006-04-21 23:57:45 -07002326 if (ecbdata->label_path[0]) {
Stefan Beller3ee8b7b2017-06-29 17:06:56 -07002327 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
2328 ecbdata->label_path[0],
2329 strlen(ecbdata->label_path[0]), 0);
2330 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
2331 ecbdata->label_path[1],
2332 strlen(ecbdata->label_path[1]), 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002333 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
2334 }
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002335
Jim Meyeringa624eaa2008-08-15 13:39:26 +02002336 if (diff_suppress_blank_empty
2337 && len == 2 && line[0] == ' ' && line[1] == '\n') {
2338 line[0] = '\n';
2339 len = 1;
2340 }
2341
Junio C Hamanob8d9c1a2009-09-03 23:59:25 -07002342 if (line[0] == '@') {
Junio C Hamano76fd2822009-10-30 10:09:06 -07002343 if (ecbdata->diff_words)
2344 diff_words_flush(ecbdata);
Jeff King19b90462019-02-14 00:48:27 -05002345 len = sane_truncate_line(line, len);
Junio C Hamanod68fe262009-09-14 22:05:57 -07002346 find_lno(line, ecbdata);
Bert Wesarg89cb73a2009-11-27 07:55:18 +01002347 emit_hunk_header(ecbdata, line, len);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002348 return 0;
Johannes Schindelincd112ce2006-06-13 18:45:44 +02002349 }
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002350
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002351 if (ecbdata->diff_words) {
Stefan Bellerff958672017-06-29 17:06:54 -07002352 enum diff_symbol s =
2353 ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ?
2354 DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002355 if (line[0] == '-') {
2356 diff_words_append(line, len,
2357 &ecbdata->diff_words->minus);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002358 return 0;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002359 } else if (line[0] == '+') {
2360 diff_words_append(line, len,
2361 &ecbdata->diff_words->plus);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002362 return 0;
Christian Couder59556542013-11-30 21:55:40 +01002363 } else if (starts_with(line, "\\ ")) {
Thomas Rastc7c2bc02012-01-12 12:15:33 +01002364 /*
2365 * Eat the "no newline at eof" marker as if we
2366 * saw a "+" or "-" line with nothing on it,
2367 * and return without diff_words_flush() to
2368 * defer processing. If this is the end of
2369 * preimage, more "+" lines may come after it.
2370 */
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002371 return 0;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002372 }
Junio C Hamano76fd2822009-10-30 10:09:06 -07002373 diff_words_flush(ecbdata);
Stefan Bellerff958672017-06-29 17:06:54 -07002374 emit_diff_symbol(o, s, line, len, 0);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002375 return 0;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002376 }
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002377
Junio C Hamano0e383e12015-05-26 09:56:33 -07002378 switch (line[0]) {
2379 case '+':
Junio C Hamanod68fe262009-09-14 22:05:57 -07002380 ecbdata->lno_in_postimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05002381 emit_add_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002382 break;
2383 case '-':
2384 ecbdata->lno_in_preimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05002385 emit_del_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002386 break;
2387 case ' ':
2388 ecbdata->lno_in_postimage++;
2389 ecbdata->lno_in_preimage++;
Jeff Kinge04df612019-02-14 00:48:13 -05002390 emit_context_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002391 break;
2392 default:
2393 /* incomplete line at the end */
2394 ecbdata->lno_in_preimage++;
Stefan Bellerf2bb1212017-06-29 17:06:55 -07002395 emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE,
2396 line, len, 0);
Junio C Hamano0e383e12015-05-26 09:56:33 -07002397 break;
Junio C Hamano448c3ef2006-09-22 22:48:39 -07002398 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002399 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002400}
2401
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002402static void pprint_rename(struct strbuf *name, const char *a, const char *b)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002403{
Brandon Williams63a01c32018-02-14 10:59:39 -08002404 const char *old_name = a;
2405 const char *new_name = b;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002406 int pfx_length, sfx_length;
Thomas Rastdd281f02013-02-26 21:47:01 +01002407 int pfx_adjust_for_slash;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002408 int len_a = strlen(a);
2409 int len_b = strlen(b);
Pierre Habouzit663af342007-09-20 00:42:15 +02002410 int a_midlen, b_midlen;
Alexandre Julliarde5bfbf92007-02-10 15:39:00 +01002411 int qlen_a = quote_c_style(a, NULL, NULL, 0);
2412 int qlen_b = quote_c_style(b, NULL, NULL, 0);
2413
2414 if (qlen_a || qlen_b) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002415 quote_c_style(a, name, NULL, 0);
2416 strbuf_addstr(name, " => ");
2417 quote_c_style(b, name, NULL, 0);
2418 return;
Alexandre Julliarde5bfbf92007-02-10 15:39:00 +01002419 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002420
2421 /* Find common prefix */
2422 pfx_length = 0;
Brandon Williams63a01c32018-02-14 10:59:39 -08002423 while (*old_name && *new_name && *old_name == *new_name) {
2424 if (*old_name == '/')
2425 pfx_length = old_name - a + 1;
2426 old_name++;
2427 new_name++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002428 }
2429
2430 /* Find common suffix */
Brandon Williams63a01c32018-02-14 10:59:39 -08002431 old_name = a + len_a;
2432 new_name = b + len_b;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002433 sfx_length = 0;
Antoine Pelissed020e272013-02-23 17:48:45 +01002434 /*
Thomas Rastdd281f02013-02-26 21:47:01 +01002435 * If there is a common prefix, it must end in a slash. In
2436 * that case we let this loop run 1 into the prefix to see the
2437 * same slash.
2438 *
2439 * If there is no common prefix, we cannot do this as it would
2440 * underrun the input strings.
Antoine Pelissed020e272013-02-23 17:48:45 +01002441 */
Thomas Rastdd281f02013-02-26 21:47:01 +01002442 pfx_adjust_for_slash = (pfx_length ? 1 : 0);
Brandon Williams63a01c32018-02-14 10:59:39 -08002443 while (a + pfx_length - pfx_adjust_for_slash <= old_name &&
2444 b + pfx_length - pfx_adjust_for_slash <= new_name &&
2445 *old_name == *new_name) {
2446 if (*old_name == '/')
2447 sfx_length = len_a - (old_name - a);
2448 old_name--;
2449 new_name--;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002450 }
2451
2452 /*
2453 * pfx{mid-a => mid-b}sfx
2454 * {pfx-a => pfx-b}sfx
2455 * pfx{sfx-a => sfx-b}
2456 * name-a => name-b
2457 */
Pierre Habouzit663af342007-09-20 00:42:15 +02002458 a_midlen = len_a - pfx_length - sfx_length;
2459 b_midlen = len_b - pfx_length - sfx_length;
2460 if (a_midlen < 0)
2461 a_midlen = 0;
2462 if (b_midlen < 0)
2463 b_midlen = 0;
Junio C Hamanocc908b82006-05-14 22:07:28 -07002464
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002465 strbuf_grow(name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
Pierre Habouzit663af342007-09-20 00:42:15 +02002466 if (pfx_length + sfx_length) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002467 strbuf_add(name, a, pfx_length);
2468 strbuf_addch(name, '{');
Junio C Hamano6973dca2006-04-21 23:57:45 -07002469 }
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002470 strbuf_add(name, a + pfx_length, a_midlen);
2471 strbuf_addstr(name, " => ");
2472 strbuf_add(name, b + pfx_length, b_midlen);
Pierre Habouzit663af342007-09-20 00:42:15 +02002473 if (pfx_length + sfx_length) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002474 strbuf_addch(name, '}');
2475 strbuf_add(name, a + len_a - sfx_length, sfx_length);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002476 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002477}
2478
Junio C Hamano6973dca2006-04-21 23:57:45 -07002479static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
2480 const char *name_a,
2481 const char *name_b)
2482{
2483 struct diffstat_file *x;
René Scharfeca56dad2021-03-13 17:17:22 +01002484 CALLOC_ARRAY(x, 1);
Dmitry S. Dolzhenko4c960a42014-03-04 02:31:53 +04002485 ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002486 diffstat->files[diffstat->nr++] = x;
2487 if (name_b) {
Junio C Hamanof6046522007-12-11 23:46:30 -08002488 x->from_name = xstrdup(name_a);
2489 x->name = xstrdup(name_b);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002490 x->is_renamed = 1;
2491 }
Junio C Hamanof6046522007-12-11 23:46:30 -08002492 else {
2493 x->from_name = NULL;
Shawn Pearce9befac42006-09-02 00:16:31 -04002494 x->name = xstrdup(name_a);
Junio C Hamanof6046522007-12-11 23:46:30 -08002495 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002496 return x;
2497}
2498
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002499static int diffstat_consume(void *priv, char *line, unsigned long len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002500{
2501 struct diffstat_t *diffstat = priv;
2502 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
2503
Jeff King0e5a87e2022-10-17 21:01:17 -04002504 if (!len)
2505 BUG("xdiff fed us an empty line");
2506
Junio C Hamano6973dca2006-04-21 23:57:45 -07002507 if (line[0] == '+')
2508 x->added++;
2509 else if (line[0] == '-')
2510 x->deleted++;
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02002511 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002512}
2513
Johannes Schindelin698ce6f2006-05-20 15:40:29 +02002514const char mime_boundary_leader[] = "------------";
Junio C Hamano6973dca2006-04-21 23:57:45 -07002515
Junio C Hamanoa2540022006-09-26 18:53:02 -07002516static int scale_linear(int it, int width, int max_change)
2517{
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002518 if (!it)
2519 return 0;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002520 /*
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002521 * make sure that at least one '-' or '+' is printed if
2522 * there is any change to this path. The easiest way is to
Elijah Newren15beaaa2019-11-05 17:07:23 +00002523 * scale linearly as if the allotted width is one column shorter
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002524 * than it is, and then add 1 to the result.
Junio C Hamanoa2540022006-09-26 18:53:02 -07002525 */
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002526 return 1 + (it * (width - 1) / max_change);
Junio C Hamanoa2540022006-09-26 18:53:02 -07002527}
2528
Stefan Beller0911c472017-06-29 17:07:02 -07002529static void show_graph(struct strbuf *out, char ch, int cnt,
2530 const char *set, const char *reset)
Junio C Hamanoa2540022006-09-26 18:53:02 -07002531{
2532 if (cnt <= 0)
2533 return;
Stefan Beller0911c472017-06-29 17:07:02 -07002534 strbuf_addstr(out, set);
2535 strbuf_addchars(out, ch, cnt);
2536 strbuf_addstr(out, reset);
Junio C Hamanoa2540022006-09-26 18:53:02 -07002537}
2538
Junio C Hamanof6046522007-12-11 23:46:30 -08002539static void fill_print_name(struct diffstat_file *file)
2540{
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002541 struct strbuf pname = STRBUF_INIT;
Junio C Hamanof6046522007-12-11 23:46:30 -08002542
2543 if (file->print_name)
2544 return;
2545
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002546 if (file->is_renamed)
2547 pprint_rename(&pname, file->from_name, file->name);
2548 else
2549 quote_c_style(file->name, &pname, NULL, 0);
2550
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 21:09:59 +07002551 if (file->comments)
2552 strbuf_addf(&pname, " (%s)", file->comments);
2553
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07002554 file->print_name = strbuf_detach(&pname, NULL);
Junio C Hamanof6046522007-12-11 23:46:30 -08002555}
2556
Stefan Beller0911c472017-06-29 17:07:02 -07002557static void print_stat_summary_inserts_deletes(struct diff_options *options,
2558 int files, int insertions, int deletions)
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002559{
2560 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002561
2562 if (!files) {
2563 assert(insertions == 0 && deletions == 0);
Stefan Beller0911c472017-06-29 17:07:02 -07002564 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
2565 NULL, 0, 0);
2566 return;
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002567 }
2568
2569 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002570 (files == 1) ? " %d file changed" : " %d files changed",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002571 files);
2572
2573 /*
2574 * For binary diff, the caller may want to print "x files
2575 * changed" with insertions == 0 && deletions == 0.
2576 *
2577 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2578 * is probably less confusing (i.e skip over "2 files changed
2579 * but nothing about added/removed lines? Is this a bug in Git?").
2580 */
2581 if (insertions || deletions == 0) {
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002582 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002583 (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002584 insertions);
2585 }
2586
2587 if (deletions || insertions == 0) {
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002588 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 21:16:26 +07002589 (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)",
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002590 deletions);
2591 }
2592 strbuf_addch(&sb, '\n');
Stefan Beller0911c472017-06-29 17:07:02 -07002593 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
2594 sb.buf, sb.len, 0);
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002595 strbuf_release(&sb);
Stefan Beller0911c472017-06-29 17:07:02 -07002596}
2597
2598void print_stat_summary(FILE *fp, int files,
2599 int insertions, int deletions)
2600{
2601 struct diff_options o;
2602 memset(&o, 0, sizeof(o));
2603 o.file = fp;
2604
2605 print_stat_summary_inserts_deletes(&o, files, insertions, deletions);
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +07002606}
2607
Felipe Contreras4b25d092009-05-01 12:06:36 +03002608static void show_stats(struct diffstat_t *data, struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07002609{
Benjamin Kramereb3a9dd2009-03-07 21:02:10 +01002610 int i, len, add, del, adds = 0, dels = 0;
Jeff King0974c112010-04-17 13:41:08 -04002611 uintmax_t max_change = 0, max_len = 0;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002612 int total_files = data->nr, count;
2613 int width, name_width, graph_width, number_width = 0, bin_width = 0;
Johannes Schindelinc0aa3352011-03-22 13:50:08 +01002614 const char *reset, *add_c, *del_c;
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002615 int extra_shown = 0;
Stefan Beller0911c472017-06-29 17:07:02 -07002616 const char *line_prefix = diff_line_prefix(options);
2617 struct strbuf out = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002618
2619 if (data->nr == 0)
2620 return;
2621
Michael J Gruber808e1db2011-05-27 14:36:41 +02002622 count = options->stat_count ? options->stat_count : data->nr;
Junio C Hamanoa2540022006-09-26 18:53:02 -07002623
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01002624 reset = diff_get_color_opt(options, DIFF_RESET);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01002625 add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
2626 del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
Junio C Hamano785f7432006-09-26 18:59:41 -07002627
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002628 /*
2629 * Find the longest filename and max number of changes
2630 */
Michael J Gruber808e1db2011-05-27 14:36:41 +02002631 for (i = 0; (i < count) && (i < data->nr); i++) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002632 struct diffstat_file *file = data->files[i];
Jeff King0974c112010-04-17 13:41:08 -04002633 uintmax_t change = file->added + file->deleted;
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002634
2635 if (!file->is_interesting && (change == 0)) {
Michael J Gruber808e1db2011-05-27 14:36:41 +02002636 count++; /* not shown == room for one more */
Michael J Gruber358e4602011-05-27 14:36:40 +02002637 continue;
2638 }
Junio C Hamanof6046522007-12-11 23:46:30 -08002639 fill_print_name(file);
Torsten Bögershausen12fc4ad2022-09-14 17:13:33 +02002640 len = utf8_strwidth(file->print_name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002641 if (max_len < len)
2642 max_len = len;
2643
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002644 if (file->is_unmerged) {
2645 /* "Unmerged" is 8 characters */
2646 bin_width = bin_width < 8 ? 8 : bin_width;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002647 continue;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002648 }
2649 if (file->is_binary) {
2650 /* "Bin XXX -> YYY bytes" */
2651 int w = 14 + decimal_width(file->added)
2652 + decimal_width(file->deleted);
2653 bin_width = bin_width < w ? w : bin_width;
2654 /* Display change counts aligned with "Bin" */
2655 number_width = 3;
2656 continue;
2657 }
2658
Junio C Hamanoa2540022006-09-26 18:53:02 -07002659 if (max_change < change)
2660 max_change = change;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002661 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002662 count = i; /* where we can stop scanning in data->files[] */
Junio C Hamano6973dca2006-04-21 23:57:45 -07002663
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002664 /*
2665 * We have width = stat_width or term_columns() columns total.
2666 * We want a maximum of min(max_len, stat_name_width) for the name part.
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002667 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002668 * We also need 1 for " " and 4 + decimal_width(max_change)
2669 * for " | NNNN " and one the empty column at the end, altogether
2670 * 6 + decimal_width(max_change).
Junio C Hamanoa2540022006-09-26 18:53:02 -07002671 *
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002672 * If there's not enough space, we will use the smaller of
2673 * stat_name_width (if set) and 5/8*width for the filename,
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002674 * and the rest for constant elements + graph part, but no more
2675 * than stat_graph_width for the graph part.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002676 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2677 * for the standard terminal size).
2678 *
2679 * In other words: stat_width limits the maximum width, and
2680 * stat_name_width fixes the maximum width of the filename,
2681 * and is also used to divide available columns if there
2682 * aren't enough.
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002683 *
2684 * Binary files are displayed with "Bin XXX -> YYY bytes"
2685 * instead of the change count and graph. This part is treated
2686 * similarly to the graph part, except that it is not
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +02002687 * "scaled". If total width is too small to accommodate the
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002688 * guaranteed minimum width of the filename part and the
2689 * separators and this message, this message will "overflow"
2690 * making the line longer than the maximum width.
Junio C Hamanoa2540022006-09-26 18:53:02 -07002691 */
Junio C Hamanoa2540022006-09-26 18:53:02 -07002692
Junio C Hamanoce8529b2022-10-21 14:53:25 -07002693 /*
2694 * NEEDSWORK: line_prefix is often used for "log --graph" output
2695 * and contains ANSI-colored string. utf8_strnwidth() should be
2696 * used to correctly count the display width instead of strlen().
2697 */
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002698 if (options->stat_width == -1)
Junio C Hamanocd48dad2016-08-31 16:27:19 -07002699 width = term_columns() - strlen(line_prefix);
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002700 else
2701 width = options->stat_width ? options->stat_width : 80;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002702 number_width = decimal_width(max_change) > number_width ?
2703 decimal_width(max_change) : number_width;
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002704
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +01002705 if (options->stat_graph_width == -1)
2706 options->stat_graph_width = diff_stat_graph_width;
2707
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002708 /*
2709 * Guarantee 3/8*16==6 for the graph part
2710 * and 5/8*16==10 for the filename part
2711 */
2712 if (width < 16 + 6 + number_width)
2713 width = 16 + 6 + number_width;
2714
2715 /*
2716 * First assign sizes that are wanted, ignoring available width.
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002717 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2718 * starting from "XXX" should fit in graph_width.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002719 */
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +02002720 graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4;
2721 if (options->stat_graph_width &&
2722 options->stat_graph_width < graph_width)
2723 graph_width = options->stat_graph_width;
2724
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002725 name_width = (options->stat_name_width > 0 &&
2726 options->stat_name_width < max_len) ?
2727 options->stat_name_width : max_len;
2728
2729 /*
2730 * Adjust adjustable widths not to exceed maximum width
2731 */
2732 if (name_width + number_width + 6 + graph_width > width) {
Lucian Poston678c5742012-04-18 14:12:18 -07002733 if (graph_width > width * 3/8 - number_width - 6) {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002734 graph_width = width * 3/8 - number_width - 6;
Lucian Poston678c5742012-04-18 14:12:18 -07002735 if (graph_width < 6)
2736 graph_width = 6;
2737 }
2738
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01002739 if (options->stat_graph_width &&
2740 graph_width > options->stat_graph_width)
2741 graph_width = options->stat_graph_width;
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002742 if (name_width > width - number_width - 6 - graph_width)
2743 name_width = width - number_width - 6 - graph_width;
2744 else
2745 graph_width = width - number_width - 6 - name_width;
2746 }
2747
2748 /*
2749 * From here name_width is the width of the name area,
2750 * and graph_width is the width of the graph area.
2751 * max_change is used to scale graph properly.
2752 */
Michael J Gruber808e1db2011-05-27 14:36:41 +02002753 for (i = 0; i < count; i++) {
Timo Hirvonend2543b82006-06-24 20:20:32 +03002754 const char *prefix = "";
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002755 struct diffstat_file *file = data->files[i];
2756 char *name = file->print_name;
2757 uintmax_t added = file->added;
2758 uintmax_t deleted = file->deleted;
Torsten Bögershausen12fc4ad2022-09-14 17:13:33 +02002759 int name_len, padding;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002760
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002761 if (!file->is_interesting && (added + deleted == 0))
Michael J Gruber358e4602011-05-27 14:36:40 +02002762 continue;
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002763
Junio C Hamano6973dca2006-04-21 23:57:45 -07002764 /*
2765 * "scale" the filename
2766 */
Junio C Hamanoa2540022006-09-26 18:53:02 -07002767 len = name_width;
Torsten Bögershausen12fc4ad2022-09-14 17:13:33 +02002768 name_len = utf8_strwidth(name);
Junio C Hamanoa2540022006-09-26 18:53:02 -07002769 if (name_width < name_len) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07002770 char *slash;
2771 prefix = "...";
Junio C Hamanoa2540022006-09-26 18:53:02 -07002772 len -= 3;
Junio C Hamanoce8529b2022-10-21 14:53:25 -07002773 /*
2774 * NEEDSWORK: (name_len - len) counts the display
2775 * width, which would be shorter than the byte
2776 * length of the corresponding substring.
2777 * Advancing "name" by that number of bytes does
2778 * *NOT* skip over that many columns, so it is
2779 * very likely that chomping the pathname at the
2780 * slash we will find starting from "name" will
2781 * leave the resulting string still too long.
2782 */
Junio C Hamanoa2540022006-09-26 18:53:02 -07002783 name += name_len - len;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002784 slash = strchr(name, '/');
2785 if (slash)
2786 name = slash;
2787 }
Torsten Bögershausen12fc4ad2022-09-14 17:13:33 +02002788 padding = len - utf8_strwidth(name);
2789 if (padding < 0)
2790 padding = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002791
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002792 if (file->is_binary) {
Torsten Bögershausen12fc4ad2022-09-14 17:13:33 +02002793 strbuf_addf(&out, " %s%s%*s | %*s",
2794 prefix, name, padding, "",
2795 number_width, "Bin");
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002796 if (!added && !deleted) {
Stefan Beller0911c472017-06-29 17:07:02 -07002797 strbuf_addch(&out, '\n');
2798 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2799 out.buf, out.len, 0);
2800 strbuf_reset(&out);
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002801 continue;
2802 }
Stefan Beller0911c472017-06-29 17:07:02 -07002803 strbuf_addf(&out, " %s%"PRIuMAX"%s",
Jeff King0974c112010-04-17 13:41:08 -04002804 del_c, deleted, reset);
Stefan Beller0911c472017-06-29 17:07:02 -07002805 strbuf_addstr(&out, " -> ");
2806 strbuf_addf(&out, "%s%"PRIuMAX"%s",
Jeff King0974c112010-04-17 13:41:08 -04002807 add_c, added, reset);
Stefan Beller0911c472017-06-29 17:07:02 -07002808 strbuf_addstr(&out, " bytes\n");
2809 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2810 out.buf, out.len, 0);
2811 strbuf_reset(&out);
Junio C Hamanof6046522007-12-11 23:46:30 -08002812 continue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002813 }
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002814 else if (file->is_unmerged) {
Torsten Bögershausen12fc4ad2022-09-14 17:13:33 +02002815 strbuf_addf(&out, " %s%s%*s | %*s",
2816 prefix, name, padding, "",
Peter Grayson209d9cb2022-12-14 12:41:51 -05002817 number_width, "Unmerged\n");
Stefan Beller0911c472017-06-29 17:07:02 -07002818 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2819 out.buf, out.len, 0);
2820 strbuf_reset(&out);
Junio C Hamanof6046522007-12-11 23:46:30 -08002821 continue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002822 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002823
Junio C Hamanoa2540022006-09-26 18:53:02 -07002824 /*
2825 * scale the add/delete
2826 */
Junio C Hamano6973dca2006-04-21 23:57:45 -07002827 add = added;
2828 del = deleted;
Junio C Hamano6973dca2006-04-21 23:57:45 -07002829
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002830 if (graph_width <= max_change) {
Stefan Bellerd3c9cf32013-07-14 23:35:49 +02002831 int total = scale_linear(add + del, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002832 if (total < 2 && add && del)
2833 /* width >= 2 due to the sanity check */
2834 total = 2;
2835 if (add < del) {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002836 add = scale_linear(add, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002837 del = total - add;
2838 } else {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 13:26:43 +01002839 del = scale_linear(del, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 13:49:11 -08002840 add = total - del;
2841 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07002842 }
Torsten Bögershausen12fc4ad2022-09-14 17:13:33 +02002843 strbuf_addf(&out, " %s%s%*s | %*"PRIuMAX"%s",
2844 prefix, name, padding, "",
2845 number_width, added + deleted,
2846 added + deleted ? " " : "");
Stefan Beller0911c472017-06-29 17:07:02 -07002847 show_graph(&out, '+', add, add_c, reset);
2848 show_graph(&out, '-', del, del_c, reset);
2849 strbuf_addch(&out, '\n');
2850 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2851 out.buf, out.len, 0);
2852 strbuf_reset(&out);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002853 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002854
2855 for (i = 0; i < data->nr; i++) {
Junio C Hamanoaf0ed812012-11-27 11:24:54 -08002856 struct diffstat_file *file = data->files[i];
2857 uintmax_t added = file->added;
2858 uintmax_t deleted = file->deleted;
Junio C Hamano82dfc2c2012-11-27 12:05:10 -08002859
2860 if (file->is_unmerged ||
2861 (!file->is_interesting && (added + deleted == 0))) {
Michael J Gruber808e1db2011-05-27 14:36:41 +02002862 total_files--;
2863 continue;
2864 }
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002865
Junio C Hamano82dfc2c2012-11-27 12:05:10 -08002866 if (!file->is_binary) {
Junio C Hamanoa20d3c02012-11-27 11:47:46 -08002867 adds += added;
2868 dels += deleted;
2869 }
2870 if (i < count)
2871 continue;
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002872 if (!extra_shown)
Stefan Beller0911c472017-06-29 17:07:02 -07002873 emit_diff_symbol(options,
2874 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
2875 NULL, 0, 0);
Junio C Hamanoe5f85df2011-05-27 21:50:39 -07002876 extra_shown = 1;
Michael J Gruber808e1db2011-05-27 14:36:41 +02002877 }
Stefan Beller0911c472017-06-29 17:07:02 -07002878
2879 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
Rene Scharfe5a612012017-08-30 19:49:43 +02002880 strbuf_release(&out);
Junio C Hamano6973dca2006-04-21 23:57:45 -07002881}
2882
Felipe Contreras2775d922009-10-11 23:46:11 +03002883static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002884{
2885 int i, adds = 0, dels = 0, total_files = data->nr;
2886
2887 if (data->nr == 0)
2888 return;
2889
2890 for (i = 0; i < data->nr; i++) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002891 int added = data->files[i]->added;
Stefan Beller0911c472017-06-29 17:07:02 -07002892 int deleted = data->files[i]->deleted;
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002893
Junio C Hamano20c8cde2012-11-27 14:19:36 -08002894 if (data->files[i]->is_unmerged ||
2895 (!data->files[i]->is_interesting && (added + deleted == 0))) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002896 total_files--;
Alexander Strasserde9658b2012-06-15 23:50:30 +02002897 } else if (!data->files[i]->is_binary) { /* don't count bytes */
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02002898 adds += added;
2899 dels += deleted;
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002900 }
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002901 }
Stefan Beller0911c472017-06-29 17:07:02 -07002902 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
Nicolas Pitreebd124c2006-12-14 23:15:44 -05002903}
2904
Felipe Contreras4b25d092009-05-01 12:06:36 +03002905static void show_numstat(struct diffstat_t *data, struct diff_options *options)
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002906{
2907 int i;
2908
Junio C Hamanof6046522007-12-11 23:46:30 -08002909 if (data->nr == 0)
2910 return;
2911
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002912 for (i = 0; i < data->nr; i++) {
2913 struct diffstat_file *file = data->files[i];
2914
John Keeping30997bb2013-02-07 20:15:27 +00002915 fprintf(options->file, "%s", diff_line_prefix(options));
Bo Yang7be57612010-05-26 15:23:54 +08002916
Junio C Hamanobfddbc52006-12-10 13:50:59 -08002917 if (file->is_binary)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002918 fprintf(options->file, "-\t-\t");
Junio C Hamanobfddbc52006-12-10 13:50:59 -08002919 else
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002920 fprintf(options->file,
Jeff King0974c112010-04-17 13:41:08 -04002921 "%"PRIuMAX"\t%"PRIuMAX"\t",
2922 file->added, file->deleted);
Junio C Hamanof6046522007-12-11 23:46:30 -08002923 if (options->line_termination) {
2924 fill_print_name(file);
2925 if (!file->is_renamed)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002926 write_name_quoted(file->name, options->file,
Junio C Hamanof6046522007-12-11 23:46:30 -08002927 options->line_termination);
2928 else {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002929 fputs(file->print_name, options->file);
2930 putc(options->line_termination, options->file);
Junio C Hamanof6046522007-12-11 23:46:30 -08002931 }
Pierre Habouzit663af342007-09-20 00:42:15 +02002932 } else {
Junio C Hamanof6046522007-12-11 23:46:30 -08002933 if (file->is_renamed) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002934 putc('\0', options->file);
2935 write_name_quoted(file->from_name, options->file, '\0');
Junio C Hamanof6046522007-12-11 23:46:30 -08002936 }
Daniel Barkalowc0c77732008-03-09 22:43:39 -04002937 write_name_quoted(file->name, options->file, '\0');
Pierre Habouzit663af342007-09-20 00:42:15 +02002938 }
Junio C Hamano74e2abe2006-10-12 03:01:00 -07002939 }
2940}
2941
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002942struct dirstat_file {
2943 const char *name;
2944 unsigned long changed;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002945};
2946
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002947struct dirstat_dir {
2948 struct dirstat_file *files;
Johan Herland712d2c72011-04-29 11:36:20 +02002949 int alloc, nr, permille, cumulative;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002950};
2951
Bo Yang7be57612010-05-26 15:23:54 +08002952static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
2953 unsigned long changed, const char *base, int baselen)
Linus Torvalds7df7c012008-02-12 13:26:31 -08002954{
Brandon Williams585c0e22018-02-14 10:59:28 -08002955 unsigned long sum_changes = 0;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002956 unsigned int sources = 0;
John Keeping30997bb2013-02-07 20:15:27 +00002957 const char *line_prefix = diff_line_prefix(opt);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002958
2959 while (dir->nr) {
Junio C Hamanoc04a7152008-02-12 17:06:58 -08002960 struct dirstat_file *f = dir->files;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002961 int namelen = strlen(f->name);
Brandon Williams585c0e22018-02-14 10:59:28 -08002962 unsigned long changes;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002963 char *slash;
2964
2965 if (namelen < baselen)
2966 break;
2967 if (memcmp(f->name, base, baselen))
2968 break;
2969 slash = strchr(f->name + baselen, '/');
2970 if (slash) {
2971 int newbaselen = slash + 1 - f->name;
Brandon Williams585c0e22018-02-14 10:59:28 -08002972 changes = gather_dirstat(opt, dir, changed, f->name, newbaselen);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002973 sources++;
2974 } else {
Brandon Williams585c0e22018-02-14 10:59:28 -08002975 changes = f->changed;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002976 dir->files++;
2977 dir->nr--;
2978 sources += 2;
2979 }
Brandon Williams585c0e22018-02-14 10:59:28 -08002980 sum_changes += changes;
Linus Torvalds7df7c012008-02-12 13:26:31 -08002981 }
2982
2983 /*
2984 * We don't report dirstat's for
2985 * - the top level
2986 * - or cases where everything came from a single directory
2987 * under this directory (sources == 1).
2988 */
2989 if (baselen && sources != 1) {
Brandon Williams585c0e22018-02-14 10:59:28 -08002990 if (sum_changes) {
2991 int permille = sum_changes * 1000 / changed;
Johan Herland712d2c72011-04-29 11:36:20 +02002992 if (permille >= dir->permille) {
Bo Yang7be57612010-05-26 15:23:54 +08002993 fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix,
Johan Herland712d2c72011-04-29 11:36:20 +02002994 permille / 10, permille % 10, baselen, base);
Linus Torvalds7df7c012008-02-12 13:26:31 -08002995 if (!dir->cumulative)
2996 return 0;
2997 }
2998 }
2999 }
Brandon Williams585c0e22018-02-14 10:59:28 -08003000 return sum_changes;
Linus Torvalds7df7c012008-02-12 13:26:31 -08003001}
3002
Linus Torvalds441bca02008-08-28 16:19:08 -07003003static int dirstat_compare(const void *_a, const void *_b)
3004{
3005 const struct dirstat_file *a = _a;
3006 const struct dirstat_file *b = _b;
3007 return strcmp(a->name, b->name);
3008}
3009
Junio C Hamano34a94892023-05-05 14:19:16 -07003010static void conclude_dirstat(struct diff_options *options,
3011 struct dirstat_dir *dir,
3012 unsigned long changed)
3013{
Junio C Hamano83973982023-05-05 14:19:17 -07003014 struct dirstat_file *to_free = dir->files;
Junio C Hamano34a94892023-05-05 14:19:16 -07003015
Junio C Hamano83973982023-05-05 14:19:17 -07003016 if (!changed) {
3017 /* This can happen even with many files, if everything was renames */
3018 ;
3019 } else {
3020 /* Show all directories with more than x% of the changes */
3021 QSORT(dir->files, dir->nr, dirstat_compare);
3022 gather_dirstat(options, dir, changed, "", 0);
3023 }
3024
3025 free(to_free);
Junio C Hamano34a94892023-05-05 14:19:16 -07003026}
3027
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003028static void show_dirstat(struct diff_options *options)
Linus Torvalds7df7c012008-02-12 13:26:31 -08003029{
3030 int i;
3031 unsigned long changed;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003032 struct dirstat_dir dir;
3033 struct diff_queue_struct *q = &diff_queued_diff;
Linus Torvalds7df7c012008-02-12 13:26:31 -08003034
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003035 dir.files = NULL;
3036 dir.alloc = 0;
3037 dir.nr = 0;
Johan Herland712d2c72011-04-29 11:36:20 +02003038 dir.permille = options->dirstat_permille;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003039 dir.cumulative = options->flags.dirstat_cumulative;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003040
Linus Torvalds7df7c012008-02-12 13:26:31 -08003041 changed = 0;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003042 for (i = 0; i < q->nr; i++) {
3043 struct diff_filepair *p = q->queue[i];
3044 const char *name;
3045 unsigned long copied, added, damage;
Jonathan Tan1c37e862020-04-07 15:11:41 -07003046 struct diff_populate_filespec_options dpf_options = {
3047 .check_size_only = 1,
3048 };
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003049
Johan Herland2ca86712011-04-12 11:24:34 +02003050 name = p->two->path ? p->two->path : p->one->path;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003051
Jeff Kingd9f62df2018-08-28 17:23:03 -04003052 if (p->one->oid_valid && p->two->oid_valid &&
3053 oideq(&p->one->oid, &p->two->oid)) {
Johan Herland2ff3a802011-04-11 00:48:52 +02003054 /*
3055 * The SHA1 has not changed, so pre-/post-content is
3056 * identical. We can therefore skip looking at the
3057 * file contents altogether.
3058 */
3059 damage = 0;
3060 goto found_damage;
3061 }
3062
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003063 if (options->flags.dirstat_by_file) {
Johan Herland0133dab2011-04-11 00:48:51 +02003064 /*
3065 * In --dirstat-by-file mode, we don't really need to
3066 * look at the actual file contents at all.
3067 * The fact that the SHA1 changed is enough for us to
3068 * add this file to the list of results
3069 * (with each file contributing equal damage).
3070 */
Johan Herland2ff3a802011-04-11 00:48:52 +02003071 damage = 1;
Johan Herland0133dab2011-04-11 00:48:51 +02003072 goto found_damage;
3073 }
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003074
3075 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003076 diff_populate_filespec(options->repo, p->one, NULL);
3077 diff_populate_filespec(options->repo, p->two, NULL);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003078 diffcore_count_changes(options->repo,
3079 p->one, p->two, NULL, NULL,
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003080 &copied, &added);
3081 diff_free_filespec_data(p->one);
3082 diff_free_filespec_data(p->two);
3083 } else if (DIFF_FILE_VALID(p->one)) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003084 diff_populate_filespec(options->repo, p->one, &dpf_options);
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003085 copied = added = 0;
3086 diff_free_filespec_data(p->one);
3087 } else if (DIFF_FILE_VALID(p->two)) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003088 diff_populate_filespec(options->repo, p->two, &dpf_options);
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003089 copied = 0;
3090 added = p->two->size;
3091 diff_free_filespec_data(p->two);
3092 } else
Junio C Hamano2b0b5512008-02-24 17:37:15 -08003093 continue;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003094
3095 /*
3096 * Original minus copied is the removed material,
3097 * added is the new material. They are both damages
Johan Herland0133dab2011-04-11 00:48:51 +02003098 * made to the preimage.
Johan Herland2ff3a802011-04-11 00:48:52 +02003099 * If the resulting damage is zero, we know that
3100 * diffcore_count_changes() considers the two entries to
Jeff Kingd9f62df2018-08-28 17:23:03 -04003101 * be identical, but since the oid changed, we
Johan Herland2ff3a802011-04-11 00:48:52 +02003102 * know that there must have been _some_ kind of change,
3103 * so we force all entries to have damage > 0.
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003104 */
3105 damage = (p->one->size - copied) + added;
Johan Herland2ff3a802011-04-11 00:48:52 +02003106 if (!damage)
Heikki Orsilafd337772008-09-05 22:27:35 +03003107 damage = 1;
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003108
Johan Herland0133dab2011-04-11 00:48:51 +02003109found_damage:
Junio C Hamanoc04a7152008-02-12 17:06:58 -08003110 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
3111 dir.files[dir.nr].name = name;
3112 dir.files[dir.nr].changed = damage;
3113 changed += damage;
3114 dir.nr++;
Linus Torvalds7df7c012008-02-12 13:26:31 -08003115 }
3116
Junio C Hamano34a94892023-05-05 14:19:16 -07003117 conclude_dirstat(options, &dir, changed);
Linus Torvalds7df7c012008-02-12 13:26:31 -08003118}
3119
Johan Herland1c57a622011-04-29 11:36:21 +02003120static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options)
3121{
3122 int i;
3123 unsigned long changed;
3124 struct dirstat_dir dir;
3125
3126 if (data->nr == 0)
3127 return;
3128
3129 dir.files = NULL;
3130 dir.alloc = 0;
3131 dir.nr = 0;
3132 dir.permille = options->dirstat_permille;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003133 dir.cumulative = options->flags.dirstat_cumulative;
Johan Herland1c57a622011-04-29 11:36:21 +02003134
3135 changed = 0;
3136 for (i = 0; i < data->nr; i++) {
3137 struct diffstat_file *file = data->files[i];
3138 unsigned long damage = file->added + file->deleted;
3139 if (file->is_binary)
3140 /*
3141 * binary files counts bytes, not lines. Must find some
3142 * way to normalize binary bytes vs. textual lines.
3143 * The following heuristic assumes that there are 64
3144 * bytes per "line".
3145 * This is stupid and ugly, but very cheap...
3146 */
René Scharfe42c78a22017-07-08 12:35:35 +02003147 damage = DIV_ROUND_UP(damage, 64);
Johan Herland1c57a622011-04-29 11:36:21 +02003148 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
3149 dir.files[dir.nr].name = file->name;
3150 dir.files[dir.nr].changed = damage;
3151 changed += damage;
3152 dir.nr++;
3153 }
3154
Junio C Hamano34a94892023-05-05 14:19:16 -07003155 conclude_dirstat(options, &dir, changed);
Johan Herland1c57a622011-04-29 11:36:21 +02003156}
3157
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003158static void free_diffstat_file(struct diffstat_file *f)
3159{
3160 free(f->print_name);
3161 free(f->name);
3162 free(f->from_name);
3163 free(f);
3164}
3165
Daniel Ferreirae4cb6592019-11-13 12:40:58 +00003166void free_diffstat_info(struct diffstat_t *diffstat)
Junio C Hamanof6046522007-12-11 23:46:30 -08003167{
3168 int i;
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003169 for (i = 0; i < diffstat->nr; i++)
3170 free_diffstat_file(diffstat->files[i]);
Junio C Hamanof6046522007-12-11 23:46:30 -08003171 free(diffstat->files);
3172}
3173
Johannes Schindelin88246892006-05-20 23:43:13 +02003174struct checkdiff_t {
Johannes Schindelin88246892006-05-20 23:43:13 +02003175 const char *filename;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003176 int lineno;
Junio C Hamanoa757c642010-03-24 19:21:32 -07003177 int conflict_marker_size;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003178 struct diff_options *o;
Junio C Hamanocf1b7862007-12-06 00:14:14 -08003179 unsigned ws_rule;
Wincent Colaiuta62c64892007-12-13 21:24:52 +01003180 unsigned status;
Johannes Schindelin88246892006-05-20 23:43:13 +02003181};
3182
Junio C Hamanoa757c642010-03-24 19:21:32 -07003183static int is_conflict_marker(const char *line, int marker_size, unsigned long len)
Junio C Hamano04954042008-06-26 15:37:21 -07003184{
3185 char firstchar;
3186 int cnt;
3187
Junio C Hamanoa757c642010-03-24 19:21:32 -07003188 if (len < marker_size + 1)
Junio C Hamano04954042008-06-26 15:37:21 -07003189 return 0;
3190 firstchar = line[0];
3191 switch (firstchar) {
Junio C Hamanoa757c642010-03-24 19:21:32 -07003192 case '=': case '>': case '<': case '|':
Junio C Hamano04954042008-06-26 15:37:21 -07003193 break;
3194 default:
3195 return 0;
3196 }
Junio C Hamanoa757c642010-03-24 19:21:32 -07003197 for (cnt = 1; cnt < marker_size; cnt++)
Junio C Hamano04954042008-06-26 15:37:21 -07003198 if (line[cnt] != firstchar)
3199 return 0;
Elijah Newren15beaaa2019-11-05 17:07:23 +00003200 /* line[1] through line[marker_size-1] are same as firstchar */
Junio C Hamanoa757c642010-03-24 19:21:32 -07003201 if (len < marker_size + 1 || !isspace(line[marker_size]))
Junio C Hamano04954042008-06-26 15:37:21 -07003202 return 0;
Junio C Hamano04954042008-06-26 15:37:21 -07003203 return 1;
3204}
3205
Jeff King75ab7632018-11-02 02:39:03 -04003206static void checkdiff_consume_hunk(void *priv,
Jeff King61bdc7c2022-12-13 06:13:48 -05003207 long ob UNUSED, long on UNUSED,
3208 long nb, long nn UNUSED,
3209 const char *func UNUSED, long funclen UNUSED)
Jeff King75ab7632018-11-02 02:39:03 -04003210
3211{
3212 struct checkdiff_t *data = priv;
3213 data->lineno = nb - 1;
3214}
3215
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02003216static int checkdiff_consume(void *priv, char *line, unsigned long len)
Johannes Schindelin88246892006-05-20 23:43:13 +02003217{
3218 struct checkdiff_t *data = priv;
Junio C Hamanoa757c642010-03-24 19:21:32 -07003219 int marker_size = data->conflict_marker_size;
Jeff Kingf1c96262011-08-17 22:03:12 -07003220 const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE);
3221 const char *reset = diff_get_color(data->o->use_color, DIFF_RESET);
3222 const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +01003223 char *err;
John Keeping30997bb2013-02-07 20:15:27 +00003224 const char *line_prefix;
Bo Yang7be57612010-05-26 15:23:54 +08003225
3226 assert(data->o);
John Keeping30997bb2013-02-07 20:15:27 +00003227 line_prefix = diff_line_prefix(data->o);
Johannes Schindelin88246892006-05-20 23:43:13 +02003228
3229 if (line[0] == '+') {
Junio C Hamano18374e52008-06-26 13:16:33 -07003230 unsigned bad;
Junio C Hamano0ef617f2008-02-15 20:30:05 -08003231 data->lineno++;
Junio C Hamanoa757c642010-03-24 19:21:32 -07003232 if (is_conflict_marker(line + 1, marker_size, len - 1)) {
Junio C Hamano04954042008-06-26 15:37:21 -07003233 data->status |= 1;
3234 fprintf(data->o->file,
Bo Yang7be57612010-05-26 15:23:54 +08003235 "%s%s:%d: leftover conflict marker\n",
3236 line_prefix, data->filename, data->lineno);
Junio C Hamano04954042008-06-26 15:37:21 -07003237 }
Junio C Hamano8f8841e2008-06-26 15:35:21 -07003238 bad = ws_check(line + 1, len - 1, data->ws_rule);
Junio C Hamano18374e52008-06-26 13:16:33 -07003239 if (!bad)
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02003240 return 0;
Junio C Hamano18374e52008-06-26 13:16:33 -07003241 data->status |= bad;
3242 err = whitespace_error_string(bad);
Bo Yang7be57612010-05-26 15:23:54 +08003243 fprintf(data->o->file, "%s%s:%d: %s.\n",
3244 line_prefix, data->filename, data->lineno, err);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +01003245 free(err);
Bo Yanga3c158d2010-05-26 15:08:02 +08003246 emit_line(data->o, set, reset, line, 1);
Junio C Hamano8f8841e2008-06-26 15:35:21 -07003247 ws_check_emit(line + 1, len - 1, data->ws_rule,
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003248 data->o->file, set, reset, ws);
Junio C Hamano877f23c2008-06-26 15:36:59 -07003249 } else if (line[0] == ' ') {
Johannes Schindelin88246892006-05-20 23:43:13 +02003250 data->lineno++;
Johannes Schindelin88246892006-05-20 23:43:13 +02003251 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02003252 return 0;
Johannes Schindelin88246892006-05-20 23:43:13 +02003253}
3254
Junio C Hamano06606262006-05-05 02:41:53 -07003255static unsigned char *deflate_it(char *data,
3256 unsigned long size,
3257 unsigned long *result_size)
Junio C Hamano051308f2006-05-04 16:51:44 -07003258{
Junio C Hamano06606262006-05-05 02:41:53 -07003259 int bound;
3260 unsigned char *deflated;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07003261 git_zstream stream;
Junio C Hamano051308f2006-05-04 16:51:44 -07003262
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003263 git_deflate_init(&stream, zlib_compression_level);
Junio C Hamano225a6f12011-06-10 11:18:17 -07003264 bound = git_deflate_bound(&stream, size);
Junio C Hamano06606262006-05-05 02:41:53 -07003265 deflated = xmalloc(bound);
3266 stream.next_out = deflated;
3267 stream.avail_out = bound;
Junio C Hamano051308f2006-05-04 16:51:44 -07003268
Junio C Hamano06606262006-05-05 02:41:53 -07003269 stream.next_in = (unsigned char *)data;
3270 stream.avail_in = size;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003271 while (git_deflate(&stream, Z_FINISH) == Z_OK)
Junio C Hamano06606262006-05-05 02:41:53 -07003272 ; /* nothing */
Junio C Hamano55bb5c92011-06-10 10:55:10 -07003273 git_deflate_end(&stream);
Junio C Hamano06606262006-05-05 02:41:53 -07003274 *result_size = stream.total_out;
3275 return deflated;
Junio C Hamano051308f2006-05-04 16:51:44 -07003276}
3277
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003278static void emit_binary_diff_body(struct diff_options *o,
3279 mmfile_t *one, mmfile_t *two)
Junio C Hamano051308f2006-05-04 16:51:44 -07003280{
Junio C Hamano06606262006-05-05 02:41:53 -07003281 void *cp;
3282 void *delta;
3283 void *deflated;
3284 void *data;
3285 unsigned long orig_size;
Junio C Hamano051308f2006-05-04 16:51:44 -07003286 unsigned long delta_size;
Junio C Hamano06606262006-05-05 02:41:53 -07003287 unsigned long deflate_size;
3288 unsigned long data_size;
Junio C Hamano051308f2006-05-04 16:51:44 -07003289
Junio C Hamano06606262006-05-05 02:41:53 -07003290 /* We could do deflated delta, or we could do just deflated two,
3291 * whichever is smaller.
3292 */
3293 delta = NULL;
3294 deflated = deflate_it(two->ptr, two->size, &deflate_size);
3295 if (one->size && two->size) {
3296 delta = diff_delta(one->ptr, one->size,
3297 two->ptr, two->size,
3298 &delta_size, deflate_size);
3299 if (delta) {
3300 void *to_free = delta;
3301 orig_size = delta_size;
3302 delta = deflate_it(delta, delta_size, &delta_size);
3303 free(to_free);
3304 }
3305 }
Junio C Hamano051308f2006-05-04 16:51:44 -07003306
Junio C Hamano06606262006-05-05 02:41:53 -07003307 if (delta && delta_size < deflate_size) {
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01003308 char *s = xstrfmt("%"PRIuMAX , (uintmax_t)orig_size);
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003309 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
3310 s, strlen(s), 0);
3311 free(s);
Junio C Hamano06606262006-05-05 02:41:53 -07003312 free(deflated);
3313 data = delta;
3314 data_size = delta_size;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003315 } else {
3316 char *s = xstrfmt("%lu", two->size);
3317 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
3318 s, strlen(s), 0);
3319 free(s);
Junio C Hamano06606262006-05-05 02:41:53 -07003320 free(delta);
3321 data = deflated;
3322 data_size = deflate_size;
3323 }
3324
3325 /* emit data encoded in base85 */
3326 cp = data;
3327 while (data_size) {
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003328 int len;
Junio C Hamano06606262006-05-05 02:41:53 -07003329 int bytes = (52 < data_size) ? 52 : data_size;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003330 char line[71];
Junio C Hamano06606262006-05-05 02:41:53 -07003331 data_size -= bytes;
Junio C Hamano051308f2006-05-04 16:51:44 -07003332 if (bytes <= 26)
3333 line[0] = bytes + 'A' - 1;
3334 else
3335 line[0] = bytes - 26 + 'a' - 1;
3336 encode_85(line + 1, cp, bytes);
Florian Forster1d7f1712006-06-18 17:18:09 +02003337 cp = (char *) cp + bytes;
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003338
3339 len = strlen(line);
3340 line[len++] = '\n';
3341 line[len] = '\0';
3342
3343 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY,
3344 line, len, 0);
Junio C Hamano051308f2006-05-04 16:51:44 -07003345 }
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003346 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0);
Junio C Hamano06606262006-05-05 02:41:53 -07003347 free(data);
Junio C Hamano051308f2006-05-04 16:51:44 -07003348}
3349
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003350static void emit_binary_diff(struct diff_options *o,
3351 mmfile_t *one, mmfile_t *two)
Junio C Hamanod4c452f2006-08-16 16:08:14 -07003352{
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003353 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0);
3354 emit_binary_diff_body(o, one, two);
3355 emit_binary_diff_body(o, two, one);
Junio C Hamanod4c452f2006-08-16 16:08:14 -07003356}
3357
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003358int diff_filespec_is_binary(struct repository *r,
3359 struct diff_filespec *one)
Junio C Hamano29a3eef2007-07-06 00:18:54 -07003360{
Jonathan Tan1c37e862020-04-07 15:11:41 -07003361 struct diff_populate_filespec_options dpf_options = {
3362 .check_binary = 1,
3363 };
3364
Jeff King122aa6f2008-10-05 17:43:36 -04003365 if (one->is_binary == -1) {
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003366 diff_filespec_load_driver(one, r->index);
Jeff King122aa6f2008-10-05 17:43:36 -04003367 if (one->driver->binary != -1)
3368 one->is_binary = one->driver->binary;
3369 else {
3370 if (!one->data && DIFF_FILE_VALID(one))
Jonathan Tan1c37e862020-04-07 15:11:41 -07003371 diff_populate_filespec(r, one, &dpf_options);
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07003372 if (one->is_binary == -1 && one->data)
Jeff King122aa6f2008-10-05 17:43:36 -04003373 one->is_binary = buffer_is_binary(one->data,
3374 one->size);
3375 if (one->is_binary == -1)
3376 one->is_binary = 0;
3377 }
3378 }
Junio C Hamano29a3eef2007-07-06 00:18:54 -07003379 return one->is_binary;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003380}
3381
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003382static const struct userdiff_funcname *
3383diff_funcname_pattern(struct diff_options *o, struct diff_filespec *one)
Junio C Hamanof2584752007-07-06 00:45:10 -07003384{
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003385 diff_filespec_load_driver(one, o->repo->index);
Jeff King122aa6f2008-10-05 17:43:36 -04003386 return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
Junio C Hamanof2584752007-07-06 00:45:10 -07003387}
3388
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003389void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
3390{
3391 if (!options->a_prefix)
3392 options->a_prefix = a;
3393 if (!options->b_prefix)
3394 options->b_prefix = b;
3395}
3396
Jeff King6799aad2023-03-09 01:07:06 -05003397void diff_set_noprefix(struct diff_options *options)
3398{
3399 options->a_prefix = options->b_prefix = "";
3400}
3401
3402void diff_set_default_prefix(struct diff_options *options)
3403{
3404 options->a_prefix = "a/";
3405 options->b_prefix = "b/";
3406}
3407
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01003408struct userdiff_driver *get_textconv(struct repository *r,
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003409 struct diff_filespec *one)
Jeff King04427ac2008-10-26 00:44:53 -04003410{
3411 if (!DIFF_FILE_VALID(one))
3412 return NULL;
Jeff Kingd391c0f2010-09-21 17:01:24 -04003413
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01003414 diff_filespec_load_driver(one, r->index);
3415 return userdiff_get_textconv(r, one->driver);
Jeff King04427ac2008-10-26 00:44:53 -04003416}
3417
Johannes Schindelin2715e8a2022-06-18 00:20:55 +00003418static struct string_list *additional_headers(struct diff_options *o,
3419 const char *path)
Elijah Newren95433ee2022-02-02 02:37:34 +00003420{
3421 if (!o->additional_path_headers)
3422 return NULL;
3423 return strmap_get(o->additional_path_headers, path);
3424}
3425
Johannes Schindelin2715e8a2022-06-18 00:20:55 +00003426static void add_formatted_header(struct strbuf *msg,
3427 const char *header,
Elijah Newren95433ee2022-02-02 02:37:34 +00003428 const char *line_prefix,
3429 const char *meta,
3430 const char *reset)
3431{
Johannes Schindelin2715e8a2022-06-18 00:20:55 +00003432 const char *next, *newline;
Elijah Newren95433ee2022-02-02 02:37:34 +00003433
Johannes Schindelin2715e8a2022-06-18 00:20:55 +00003434 for (next = header; *next; next = newline) {
Elijah Newren95433ee2022-02-02 02:37:34 +00003435 newline = strchrnul(next, '\n');
3436 strbuf_addf(msg, "%s%s%.*s%s\n", line_prefix, meta,
3437 (int)(newline - next), next, reset);
3438 if (*newline)
3439 newline++;
3440 }
3441}
3442
Johannes Schindelin2715e8a2022-06-18 00:20:55 +00003443static void add_formatted_headers(struct strbuf *msg,
3444 struct string_list *more_headers,
3445 const char *line_prefix,
3446 const char *meta,
3447 const char *reset)
3448{
3449 int i;
3450
3451 for (i = 0; i < more_headers->nr; i++)
3452 add_formatted_header(msg, more_headers->items[i].string,
3453 line_prefix, meta, reset);
3454}
3455
Elijah Newren9b080912022-09-02 03:53:28 +00003456static int diff_filepair_is_phoney(struct diff_filespec *one,
3457 struct diff_filespec *two)
3458{
3459 /*
3460 * This function specifically looks for pairs injected by
3461 * create_filepairs_for_header_only_notifications(). Such
3462 * pairs are "phoney" in that they do not represent any
3463 * content or even mode difference, but were inserted because
3464 * diff_queued_diff previously had no pair associated with
3465 * that path but we needed some pair to avoid losing the
3466 * "remerge CONFLICT" header associated with the path.
3467 */
3468 return !DIFF_FILE_VALID(one) && !DIFF_FILE_VALID(two);
3469}
3470
John Cai11e95e12023-02-20 21:04:41 +00003471static int set_diff_algorithm(struct diff_options *opts,
3472 const char *alg)
3473{
3474 long value = parse_algorithm_value(alg);
3475
3476 if (value < 0)
3477 return -1;
3478
3479 /* clear out previous settings */
3480 DIFF_XDL_CLR(opts, NEED_MINIMAL);
3481 opts->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
3482 opts->xdl_opts |= value;
3483
3484 return 0;
3485}
3486
Junio C Hamano6973dca2006-04-21 23:57:45 -07003487static void builtin_diff(const char *name_a,
3488 const char *name_b,
3489 struct diff_filespec *one,
3490 struct diff_filespec *two,
3491 const char *xfrm_msg,
Christian Couder296c6bb2010-05-26 04:50:12 +02003492 int must_show_header,
Junio C Hamano051308f2006-05-04 16:51:44 -07003493 struct diff_options *o,
Junio C Hamano6973dca2006-04-21 23:57:45 -07003494 int complete_rewrite)
3495{
3496 mmfile_t mf1, mf2;
3497 const char *lbl[2];
3498 char *a_one, *b_two;
Jeff Kingd9c552f2012-11-13 21:12:46 +05303499 const char *meta = diff_get_color_opt(o, DIFF_METAINFO);
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01003500 const char *reset = diff_get_color_opt(o, DIFF_RESET);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003501 const char *a_prefix, *b_prefix;
Jeff Kingd9bae1a2010-04-01 20:12:15 -04003502 struct userdiff_driver *textconv_one = NULL;
3503 struct userdiff_driver *textconv_two = NULL;
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003504 struct strbuf header = STRBUF_INIT;
John Keeping30997bb2013-02-07 20:15:27 +00003505 const char *line_prefix = diff_line_prefix(o);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003506
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003507 diff_set_mnemonic_prefix(o, "a/", "b/");
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003508 if (o->flags.reverse_diff) {
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003509 a_prefix = o->b_prefix;
3510 b_prefix = o->a_prefix;
3511 } else {
3512 a_prefix = o->a_prefix;
3513 b_prefix = o->b_prefix;
3514 }
3515
Jacob Keller61cfbc02016-08-31 16:27:21 -07003516 if (o->submodule_format == DIFF_SUBMODULE_LOG &&
3517 (!one->mode || S_ISGITLINK(one->mode)) &&
Elijah Newren9b080912022-09-02 03:53:28 +00003518 (!two->mode || S_ISGITLINK(two->mode)) &&
3519 (!diff_filepair_is_phoney(one, two))) {
Shourya Shukla180b1542020-08-13 01:14:02 +05303520 show_submodule_diff_summary(o, one->path ? one->path : two->path,
Jacob Keller602a2832016-08-31 16:27:23 -07003521 &one->oid, &two->oid,
Stefan Bellerf3597132017-06-29 17:07:00 -07003522 two->dirty_submodule);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003523 return;
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003524 } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF &&
3525 (!one->mode || S_ISGITLINK(one->mode)) &&
Elijah Newren9b080912022-09-02 03:53:28 +00003526 (!two->mode || S_ISGITLINK(two->mode)) &&
3527 (!diff_filepair_is_phoney(one, two))) {
Stefan Bellerf3597132017-06-29 17:07:00 -07003528 show_submodule_inline_diff(o, one->path ? one->path : two->path,
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003529 &one->oid, &two->oid,
Stefan Bellerf3597132017-06-29 17:07:00 -07003530 two->dirty_submodule);
Jacob Kellerfd47ae62016-08-31 16:27:25 -07003531 return;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003532 }
3533
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003534 if (o->flags.allow_textconv) {
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01003535 textconv_one = get_textconv(o->repo, one);
3536 textconv_two = get_textconv(o->repo, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003537 }
3538
Linus Torvalds71b989e2008-10-05 15:35:15 -04003539 /* Never use a non-valid filename anywhere if at all possible */
3540 name_a = DIFF_FILE_VALID(one) ? name_a : name_b;
3541 name_b = DIFF_FILE_VALID(two) ? name_b : name_a;
3542
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07003543 a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
3544 b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
Junio C Hamano6973dca2006-04-21 23:57:45 -07003545 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
3546 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
Elijah Newren9b080912022-09-02 03:53:28 +00003547 if (diff_filepair_is_phoney(one, two)) {
Elijah Newren95433ee2022-02-02 02:37:34 +00003548 /*
Elijah Newren9b080912022-09-02 03:53:28 +00003549 * We should only reach this point for pairs generated from
Elijah Newren95433ee2022-02-02 02:37:34 +00003550 * create_filepairs_for_header_only_notifications(). For
Elijah Newren9b080912022-09-02 03:53:28 +00003551 * these, we want to avoid the "/dev/null" special casing
3552 * above, because we do not want such pairs shown as either
Elijah Newren95433ee2022-02-02 02:37:34 +00003553 * "new file" or "deleted file" below.
3554 */
3555 lbl[0] = a_one;
3556 lbl[1] = b_two;
3557 }
Jeff Kingd9c552f2012-11-13 21:12:46 +05303558 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 -07003559 if (lbl[0][0] == '/') {
3560 /* /dev/null */
Jeff Kingd9c552f2012-11-13 21:12:46 +05303561 strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset);
Bert Wesarg37466442010-05-04 00:38:07 +02003562 if (xfrm_msg)
3563 strbuf_addstr(&header, xfrm_msg);
Christian Couder296c6bb2010-05-26 04:50:12 +02003564 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003565 }
3566 else if (lbl[1][0] == '/') {
Jeff Kingd9c552f2012-11-13 21:12:46 +05303567 strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset);
Bert Wesarg37466442010-05-04 00:38:07 +02003568 if (xfrm_msg)
3569 strbuf_addstr(&header, xfrm_msg);
Christian Couder296c6bb2010-05-26 04:50:12 +02003570 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003571 }
3572 else {
3573 if (one->mode != two->mode) {
Jeff Kingd9c552f2012-11-13 21:12:46 +05303574 strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset);
3575 strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset);
Christian Couder296c6bb2010-05-26 04:50:12 +02003576 must_show_header = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003577 }
Bert Wesarg37466442010-05-04 00:38:07 +02003578 if (xfrm_msg)
3579 strbuf_addstr(&header, xfrm_msg);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003580
Junio C Hamano6973dca2006-04-21 23:57:45 -07003581 /*
3582 * we do not run diff between different kind
3583 * of objects.
3584 */
3585 if ((one->mode ^ two->mode) & S_IFMT)
3586 goto free_ab_and_return;
Jeff King0c018572008-12-09 03:12:28 -05003587 if (complete_rewrite &&
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003588 (textconv_one || !diff_filespec_is_binary(o->repo, one)) &&
3589 (textconv_two || !diff_filespec_is_binary(o->repo, two))) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003590 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3591 header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003592 strbuf_reset(&header);
Jeff King3aa1f7c2008-12-09 03:13:21 -05003593 emit_rewrite_diff(name_a, name_b, one, two,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003594 textconv_one, textconv_two, o);
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01003595 o->found_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003596 goto free_ab_and_return;
3597 }
3598 }
3599
Junio C Hamano467ddc12011-02-28 16:11:55 -08003600 if (o->irreversible_delete && lbl[1][0] == '/') {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003601 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf,
3602 header.len, 0);
Junio C Hamano467ddc12011-02-28 16:11:55 -08003603 strbuf_reset(&header);
3604 goto free_ab_and_return;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003605 } else if (!o->flags.text &&
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003606 ( (!textconv_one && diff_filespec_is_binary(o->repo, one)) ||
3607 (!textconv_two && diff_filespec_is_binary(o->repo, two)) )) {
Stefan Beller4acaaa72017-06-29 17:06:58 -07003608 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003609 if (!one->data && !two->data &&
3610 S_ISREG(one->mode) && S_ISREG(two->mode) &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003611 !o->flags.binary) {
Jeff King4a7e27e2018-08-28 17:22:40 -04003612 if (oideq(&one->oid, &two->oid)) {
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003613 if (must_show_header)
Stefan Bellera29b0a12017-06-29 17:06:57 -07003614 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3615 header.buf, header.len,
3616 0);
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003617 goto free_ab_and_return;
3618 }
Stefan Bellera29b0a12017-06-29 17:06:57 -07003619 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3620 header.buf, header.len, 0);
Stefan Beller4acaaa72017-06-29 17:06:58 -07003621 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3622 diff_line_prefix(o), lbl[0], lbl[1]);
3623 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3624 sb.buf, sb.len, 0);
3625 strbuf_release(&sb);
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 10:08:06 +07003626 goto free_ab_and_return;
3627 }
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003628 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3629 fill_mmfile(o->repo, &mf2, two) < 0)
Jeff Kingb3373982010-04-01 20:14:24 -04003630 die("unable to read files to diff");
Junio C Hamano06606262006-05-05 02:41:53 -07003631 /* Quite common confusing case */
3632 if (mf1.size == mf2.size &&
Christian Couder296c6bb2010-05-26 04:50:12 +02003633 !memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
3634 if (must_show_header)
Stefan Bellera29b0a12017-06-29 17:06:57 -07003635 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3636 header.buf, header.len, 0);
Junio C Hamano06606262006-05-05 02:41:53 -07003637 goto free_ab_and_return;
Christian Couder296c6bb2010-05-26 04:50:12 +02003638 }
Stefan Bellera29b0a12017-06-29 17:06:57 -07003639 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003640 strbuf_reset(&header);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003641 if (o->flags.binary)
Stefan Beller4eed0eb2017-06-29 17:07:01 -07003642 emit_binary_diff(o, &mf1, &mf2);
Stefan Beller4acaaa72017-06-29 17:06:58 -07003643 else {
3644 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3645 diff_line_prefix(o), lbl[0], lbl[1]);
3646 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3647 sb.buf, sb.len, 0);
3648 strbuf_release(&sb);
3649 }
Johannes Schindelin34a5e1a2007-02-25 23:34:54 +01003650 o->found_changes = 1;
Junio C Hamano467ddc12011-02-28 16:11:55 -08003651 } else {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003652 /* Crazy xdl interfaces.. */
Jeff King0da0e922019-01-11 17:17:22 -05003653 const char *diffopts;
Jeff Kingae021d82014-06-18 15:47:50 -04003654 const char *v;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003655 xpparam_t xpp;
3656 xdemitconf_t xecfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003657 struct emit_callback ecbdata;
Jeff Kingbe58e702008-10-05 17:43:21 -04003658 const struct userdiff_funcname *pe;
Junio C Hamanof2584752007-07-06 00:45:10 -07003659
Junio C Hamanob3f01ff2012-02-29 18:14:16 -08003660 if (must_show_header) {
Stefan Bellera29b0a12017-06-29 17:06:57 -07003661 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3662 header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003663 strbuf_reset(&header);
3664 }
3665
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02003666 mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr);
3667 mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr);
Jeff King04427ac2008-10-26 00:44:53 -04003668
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003669 pe = diff_funcname_pattern(o, one);
Brandon Casey45e7ca02008-09-18 17:40:48 -05003670 if (!pe)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02003671 pe = diff_funcname_pattern(o, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003672
Brian Downing9ccd0a82008-10-25 15:30:37 +02003673 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003674 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelincd112ce2006-06-13 18:45:44 +02003675 memset(&ecbdata, 0, sizeof(ecbdata));
Johannes Schindelin1cdde292018-08-13 04:33:11 -07003676 if (o->flags.suppress_diff_headers)
3677 lbl[0] = NULL;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003678 ecbdata.label_path = lbl;
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07003679 ecbdata.color_diff = want_color(o->use_color);
Nguyễn Thái Ngọc Duy26d024e2018-09-21 17:57:37 +02003680 ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
Junio C Hamano690ed842009-09-04 00:41:15 -07003681 if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
Junio C Hamanod68fe262009-09-14 22:05:57 -07003682 check_blank_at_eof(&mf1, &mf2, &ecbdata);
Bo Yanga3c158d2010-05-26 15:08:02 +08003683 ecbdata.opt = o;
Johannes Schindelin1cdde292018-08-13 04:33:11 -07003684 if (header.len && !o->flags.suppress_diff_headers)
3685 ecbdata.header = &header;
René Scharfe582aa002010-05-02 15:04:41 +02003686 xpp.flags = o->xdl_opts;
Michał Kępień296d4a92020-10-20 08:48:09 +02003687 xpp.ignore_regex = o->ignore_regex;
3688 xpp.ignore_regex_nr = o->ignore_regex_nr;
Jonathan Tan2477ab22017-11-27 11:47:47 -08003689 xpp.anchors = o->anchors;
3690 xpp.anchors_nr = o->anchors_nr;
Linus Torvaldsee1e5412006-05-13 13:23:48 -07003691 xecfg.ctxlen = o->context;
René Scharfe6d0e6742008-12-28 19:45:32 +01003692 xecfg.interhunkctxlen = o->interhunkcontext;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003693 xecfg.flags = XDL_EMIT_FUNCNAMES;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003694 if (o->flags.funccontext)
René Scharfe14937c22011-10-09 13:36:57 +02003695 xecfg.flags |= XDL_EMIT_FUNCCONTEXT;
Brandon Casey45e7ca02008-09-18 17:40:48 -05003696 if (pe)
Brandon Caseya0135852008-09-18 17:42:48 -05003697 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
Jeff King0da0e922019-01-11 17:17:22 -05003698
3699 diffopts = getenv("GIT_DIFF_OPTS");
Junio C Hamano6973dca2006-04-21 23:57:45 -07003700 if (!diffopts)
3701 ;
Jeff Kingae021d82014-06-18 15:47:50 -04003702 else if (skip_prefix(diffopts, "--unified=", &v))
3703 xecfg.ctxlen = strtoul(v, NULL, 10);
3704 else if (skip_prefix(diffopts, "-u", &v))
3705 xecfg.ctxlen = strtoul(v, NULL, 10);
Jeff King0da0e922019-01-11 17:17:22 -05003706
Thomas Rast77d1a522012-03-14 19:24:08 +01003707 if (o->word_diff)
3708 init_diff_words_data(&ecbdata, o, one, two);
Jeff King9346d6d2018-11-02 02:35:45 -04003709 if (xdi_diff_outf(&mf1, &mf2, NULL, fn_out_consume,
3710 &ecbdata, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04003711 die("unable to generate diff for %s", one->path);
Thomas Rast882749a2010-04-14 17:59:06 +02003712 if (o->word_diff)
Johannes Schindelinf59a59e2006-07-28 23:56:15 +02003713 free_diff_words_data(&ecbdata);
Jeff King04427ac2008-10-26 00:44:53 -04003714 if (textconv_one)
3715 free(mf1.ptr);
3716 if (textconv_two)
3717 free(mf2.ptr);
René Scharfe8cfe5f12009-07-02 00:01:43 +02003718 xdiff_clear_find_func(&xecfg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003719 }
3720
3721 free_ab_and_return:
Greg Bacon3e97c7c2009-11-19 15:12:24 -06003722 strbuf_release(&header);
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003723 diff_free_filespec_data(one);
3724 diff_free_filespec_data(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003725 free(a_one);
3726 free(b_two);
3727 return;
3728}
3729
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 21:09:59 +07003730static char *get_compact_summary(const struct diff_filepair *p, int is_renamed)
3731{
3732 if (!is_renamed) {
3733 if (p->status == DIFF_STATUS_ADDED) {
3734 if (S_ISLNK(p->two->mode))
3735 return "new +l";
3736 else if ((p->two->mode & 0777) == 0755)
3737 return "new +x";
3738 else
3739 return "new";
3740 } else if (p->status == DIFF_STATUS_DELETED)
3741 return "gone";
3742 }
3743 if (S_ISLNK(p->one->mode) && !S_ISLNK(p->two->mode))
3744 return "mode -l";
3745 else if (!S_ISLNK(p->one->mode) && S_ISLNK(p->two->mode))
3746 return "mode +l";
3747 else if ((p->one->mode & 0777) == 0644 &&
3748 (p->two->mode & 0777) == 0755)
3749 return "mode +x";
3750 else if ((p->one->mode & 0777) == 0755 &&
3751 (p->two->mode & 0777) == 0644)
3752 return "mode -x";
3753 return NULL;
3754}
3755
Junio C Hamano6973dca2006-04-21 23:57:45 -07003756static void builtin_diffstat(const char *name_a, const char *name_b,
3757 struct diff_filespec *one,
3758 struct diff_filespec *two,
Junio C Hamano710158e2006-04-25 23:40:09 -07003759 struct diffstat_t *diffstat,
Johannes Schindelin0d21efa2006-06-14 17:40:23 +02003760 struct diff_options *o,
Linus Torvalds74faaa12012-10-17 10:00:37 -07003761 struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003762{
3763 mmfile_t mf1, mf2;
3764 struct diffstat_file *data;
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003765 int may_differ;
Linus Torvalds74faaa12012-10-17 10:00:37 -07003766 int complete_rewrite = 0;
3767
3768 if (!DIFF_PAIR_UNMERGED(p)) {
3769 if (p->status == DIFF_STATUS_MODIFIED && p->score)
3770 complete_rewrite = 1;
3771 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003772
3773 data = diffstat_add(diffstat, name_a, name_b);
Junio C Hamano99bfd402012-11-27 11:17:14 -08003774 data->is_interesting = p->status != DIFF_STATUS_UNKNOWN;
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 21:09:59 +07003775 if (o->flags.stat_with_summary)
3776 data->comments = get_compact_summary(p, data->is_renamed);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003777
3778 if (!one || !two) {
3779 data->is_unmerged = 1;
3780 return;
3781 }
Jeff Kingded0abc2011-02-19 03:04:56 -05003782
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003783 /* saves some reads if true, not a guarantee of diff outcome */
3784 may_differ = !(one->oid_valid && two->oid_valid &&
3785 oideq(&one->oid, &two->oid));
Zbigniew Jędrzejewski-Szmek352ca4e2012-05-01 19:10:15 +02003786
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003787 if (diff_filespec_is_binary(o->repo, one) ||
3788 diff_filespec_is_binary(o->repo, two)) {
Jeff Kingded0abc2011-02-19 03:04:56 -05003789 data->is_binary = 1;
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003790 if (!may_differ) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02003791 data->added = 0;
3792 data->deleted = 0;
3793 } else {
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003794 data->added = diff_filespec_size(o->repo, two);
3795 data->deleted = diff_filespec_size(o->repo, one);
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 19:10:14 +02003796 }
Jeff Kingded0abc2011-02-19 03:04:56 -05003797 }
3798
3799 else if (complete_rewrite) {
Jonathan Tan1c37e862020-04-07 15:11:41 -07003800 diff_populate_filespec(o->repo, one, NULL);
3801 diff_populate_filespec(o->repo, two, NULL);
Junio C Hamano710158e2006-04-25 23:40:09 -07003802 data->deleted = count_lines(one->data, one->size);
3803 data->added = count_lines(two->data, two->size);
Junio C Hamano710158e2006-04-25 23:40:09 -07003804 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003805
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003806 else if (may_differ) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07003807 /* Crazy xdl interfaces.. */
3808 xpparam_t xpp;
3809 xdemitconf_t xecfg;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003810
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003811 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3812 fill_mmfile(o->repo, &mf2, two) < 0)
Jeff Kingded0abc2011-02-19 03:04:56 -05003813 die("unable to read files to diff");
3814
Brian Downing9ccd0a82008-10-25 15:30:37 +02003815 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003816 memset(&xecfg, 0, sizeof(xecfg));
René Scharfe582aa002010-05-02 15:04:41 +02003817 xpp.flags = o->xdl_opts;
Michał Kępień296d4a92020-10-20 08:48:09 +02003818 xpp.ignore_regex = o->ignore_regex;
3819 xpp.ignore_regex_nr = o->ignore_regex_nr;
Jonathan Tan2477ab22017-11-27 11:47:47 -08003820 xpp.anchors = o->anchors;
3821 xpp.anchors_nr = o->anchors_nr;
Junio C Hamanof01cae92011-09-22 10:54:47 -07003822 xecfg.ctxlen = o->context;
3823 xecfg.interhunkctxlen = o->interhunkcontext;
Ævar Arnfjörð Bjarmason5d934602021-04-12 19:15:29 +02003824 xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
3825 if (xdi_diff_outf(&mf1, &mf2, NULL,
Jeff King3b40a092018-11-02 02:36:06 -04003826 diffstat_consume, diffstat, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -04003827 die("unable to generate diffstat for %s", one->path);
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003828
3829 if (DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two)) {
3830 struct diffstat_file *file =
3831 diffstat->files[diffstat->nr - 1];
3832 /*
3833 * Omit diffstats of modified files where nothing changed.
Thomas Guyot-Sionnestff0c7fa2020-09-24 03:41:41 -04003834 * Even if may_differ, this might be the case due to
Matthew Rogers1cf3d5d2020-08-20 00:41:32 +00003835 * ignoring whitespace changes, etc.
3836 *
3837 * But note that we special-case additions, deletions,
3838 * renames, and mode changes as adding an empty file,
3839 * for example is still of interest.
3840 */
3841 if ((p->status == DIFF_STATUS_MODIFIED)
3842 && !file->added
3843 && !file->deleted
3844 && one->mode == two->mode) {
3845 free_diffstat_file(file);
3846 diffstat->nr--;
3847 }
3848 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07003849 }
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003850
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003851 diff_free_filespec_data(one);
3852 diff_free_filespec_data(two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07003853}
3854
Johannes Schindelin88246892006-05-20 23:43:13 +02003855static void builtin_checkdiff(const char *name_a, const char *name_b,
Junio C Hamanocd676a52008-02-12 14:26:02 -08003856 const char *attr_path,
Junio C Hamano5ff10dd2008-06-26 15:34:54 -07003857 struct diff_filespec *one,
3858 struct diff_filespec *two,
3859 struct diff_options *o)
Johannes Schindelin88246892006-05-20 23:43:13 +02003860{
3861 mmfile_t mf1, mf2;
3862 struct checkdiff_t data;
3863
3864 if (!two)
3865 return;
3866
3867 memset(&data, 0, sizeof(data));
Johannes Schindelin88246892006-05-20 23:43:13 +02003868 data.filename = name_b ? name_b : name_a;
3869 data.lineno = 0;
Junio C Hamano1ba111d2008-06-26 15:36:34 -07003870 data.o = o;
Nguyễn Thái Ngọc Duy26d024e2018-09-21 17:57:37 +02003871 data.ws_rule = whitespace_rule(o->repo->index, attr_path);
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +02003872 data.conflict_marker_size = ll_merge_marker_size(o->repo->index, attr_path);
Johannes Schindelin88246892006-05-20 23:43:13 +02003873
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003874 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3875 fill_mmfile(o->repo, &mf2, two) < 0)
Johannes Schindelin88246892006-05-20 23:43:13 +02003876 die("unable to read files to diff");
3877
Junio C Hamano5ff10dd2008-06-26 15:34:54 -07003878 /*
3879 * All the other codepaths check both sides, but not checking
3880 * the "old" side here is deliberate. We are checking the newly
3881 * introduced changes, and as long as the "new" side is text, we
3882 * can and should check what it introduces.
3883 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003884 if (diff_filespec_is_binary(o->repo, two))
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003885 goto free_and_return;
Johannes Schindelin88246892006-05-20 23:43:13 +02003886 else {
3887 /* Crazy xdl interfaces.. */
3888 xpparam_t xpp;
3889 xdemitconf_t xecfg;
Johannes Schindelin88246892006-05-20 23:43:13 +02003890
Brian Downing9ccd0a82008-10-25 15:30:37 +02003891 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01003892 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanoc35539e2008-08-20 11:47:55 -07003893 xecfg.ctxlen = 1; /* at least one context line */
René Scharfe582aa002010-05-02 15:04:41 +02003894 xpp.flags = 0;
Jeff King75ab7632018-11-02 02:39:03 -04003895 if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume_hunk,
3896 checkdiff_consume, &data,
Jeff King3efb9882015-09-24 19:12:23 -04003897 &xpp, &xecfg))
3898 die("unable to generate checkdiff for %s", one->path);
Junio C Hamano877f23c2008-06-26 15:36:59 -07003899
Junio C Hamano467babf2009-09-03 23:39:43 -07003900 if (data.ws_rule & WS_BLANK_AT_EOF) {
Junio C Hamanod68fe262009-09-14 22:05:57 -07003901 struct emit_callback ecbdata;
3902 int blank_at_eof;
3903
3904 ecbdata.ws_rule = data.ws_rule;
3905 check_blank_at_eof(&mf1, &mf2, &ecbdata);
Christoph Mallon8837d332010-10-10 19:24:06 +02003906 blank_at_eof = ecbdata.blank_at_eof_in_postimage;
Junio C Hamanod68fe262009-09-14 22:05:57 -07003907
Junio C Hamano467babf2009-09-03 23:39:43 -07003908 if (blank_at_eof) {
3909 static char *err;
3910 if (!err)
3911 err = whitespace_error_string(WS_BLANK_AT_EOF);
3912 fprintf(o->file, "%s:%d: %s.\n",
3913 data.filename, blank_at_eof, err);
3914 data.status = 1; /* report errors */
3915 }
Junio C Hamano877f23c2008-06-26 15:36:59 -07003916 }
Johannes Schindelin88246892006-05-20 23:43:13 +02003917 }
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07003918 free_and_return:
3919 diff_free_filespec_data(one);
3920 diff_free_filespec_data(two);
Wincent Colaiuta62c64892007-12-13 21:24:52 +01003921 if (data.status)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07003922 o->flags.check_failed = 1;
Johannes Schindelin88246892006-05-20 23:43:13 +02003923}
3924
Junio C Hamano6973dca2006-04-21 23:57:45 -07003925struct diff_filespec *alloc_filespec(const char *path)
3926{
Jeff King96ffc062016-02-22 17:44:32 -05003927 struct diff_filespec *spec;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003928
Jeff King96ffc062016-02-22 17:44:32 -05003929 FLEXPTR_ALLOC_STR(spec, path, path);
Linus Torvalds9fb88412007-10-25 11:19:10 -07003930 spec->count = 1;
Jeff King122aa6f2008-10-05 17:43:36 -04003931 spec->is_binary = -1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003932 return spec;
3933}
3934
Linus Torvalds9fb88412007-10-25 11:19:10 -07003935void free_filespec(struct diff_filespec *spec)
3936{
3937 if (!--spec->count) {
3938 diff_free_filespec_data(spec);
3939 free(spec);
3940 }
3941}
3942
Brandon Williamsf9704c22017-05-30 10:30:50 -07003943void fill_filespec(struct diff_filespec *spec, const struct object_id *oid,
3944 int oid_valid, unsigned short mode)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003945{
3946 if (mode) {
3947 spec->mode = canon_mode(mode);
Brandon Williamsf9704c22017-05-30 10:30:50 -07003948 oidcpy(&spec->oid, oid);
3949 spec->oid_valid = oid_valid;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003950 }
3951}
3952
3953/*
Jakub Narebski5adf3172007-05-26 00:37:40 +02003954 * Given a name and sha1 pair, if the index tells us the file in
Junio C Hamano6973dca2006-04-21 23:57:45 -07003955 * the work tree has that object contents, return true, so that
3956 * prepare_temp_file() does not have to inflate and extract.
3957 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003958static int reuse_worktree_file(struct index_state *istate,
3959 const char *name,
3960 const struct object_id *oid,
3961 int want_file)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003962{
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +07003963 const struct cache_entry *ce;
Junio C Hamano6973dca2006-04-21 23:57:45 -07003964 struct stat st;
3965 int pos, len;
3966
Junio C Hamano150115a2009-03-22 15:26:07 -07003967 /*
3968 * We do not read the cache ourselves here, because the
Junio C Hamano6973dca2006-04-21 23:57:45 -07003969 * benchmark with my previous version that always reads cache
3970 * shows that it makes things worse for diff-tree comparing
3971 * two linux-2.6 kernel trees in an already checked out work
3972 * tree. This is because most diff-tree comparisons deal with
3973 * only a small number of files, while reading the cache is
3974 * expensive for a large project, and its cost outweighs the
3975 * savings we get by not inflating the object to a temporary
3976 * file. Practically, this code only helps when we are used
3977 * by diff-cache --cached, which does read the cache before
3978 * calling us.
3979 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003980 if (!istate->cache)
Junio C Hamano6973dca2006-04-21 23:57:45 -07003981 return 0;
3982
Shawn O. Pearce1510fea2006-12-14 06:15:57 -05003983 /* We want to avoid the working directory if our caller
3984 * doesn't need the data in a normal file, this system
3985 * is rather slow with its stat/open/mmap/close syscalls,
3986 * and the object is contained in a pack file. The pack
3987 * is probably already open and will be faster to obtain
3988 * the data through than the working directory. Loose
3989 * objects however would tend to be slower as they need
3990 * to be individually opened and inflated.
3991 */
brian m. carlson14c3c802018-05-02 00:25:33 +00003992 if (!FAST_WORKING_DIRECTORY && !want_file && has_object_pack(oid))
Shawn O. Pearce1510fea2006-12-14 06:15:57 -05003993 return 0;
3994
Jeff King06dec432016-07-22 11:27:53 -04003995 /*
3996 * Similarly, if we'd have to convert the file contents anyway, that
3997 * makes the optimization not worthwhile.
3998 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02003999 if (!want_file && would_convert_to_git(istate, name))
Jeff King06dec432016-07-22 11:27:53 -04004000 return 0;
4001
Derrick Stoleead90da72021-09-08 11:23:56 +00004002 /*
4003 * If this path does not match our sparse-checkout definition,
4004 * then the file will not be in the working directory.
4005 */
4006 if (!path_in_sparse_checkout(name, istate))
4007 return 0;
4008
Junio C Hamano6973dca2006-04-21 23:57:45 -07004009 len = strlen(name);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004010 pos = index_name_pos(istate, name, len);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004011 if (pos < 0)
4012 return 0;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004013 ce = istate->cache[pos];
Junio C Hamanoeadb5832008-01-18 23:45:24 -08004014
4015 /*
4016 * This is not the sha1 we are looking for, or
4017 * unreusable because it is not a regular file.
Junio C Hamano6973dca2006-04-21 23:57:45 -07004018 */
Jeff King9001dc22018-08-28 17:22:48 -04004019 if (!oideq(oid, &ce->oid) || !S_ISREG(ce->ce_mode))
Junio C Hamanoeadb5832008-01-18 23:45:24 -08004020 return 0;
4021
4022 /*
Junio C Hamano150115a2009-03-22 15:26:07 -07004023 * If ce is marked as "assume unchanged", there is no
4024 * guarantee that work tree matches what we are looking for.
4025 */
Nguyễn Thái Ngọc Duyb4d16902009-08-20 20:46:58 +07004026 if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
Junio C Hamano150115a2009-03-22 15:26:07 -07004027 return 0;
4028
4029 /*
Junio C Hamanoeadb5832008-01-18 23:45:24 -08004030 * If ce matches the file in the work tree, we can reuse it.
4031 */
4032 if (ce_uptodate(ce) ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004033 (!lstat(name, &st) && !ie_match_stat(istate, ce, &st, 0)))
Junio C Hamanoeadb5832008-01-18 23:45:24 -08004034 return 1;
4035
4036 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004037}
4038
Linus Torvalds04786752007-04-15 11:14:28 -07004039static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
4040{
Jeff Kingb1ddfb92016-02-22 17:45:08 -05004041 struct strbuf buf = STRBUF_INIT;
4042 char *dirty = "";
Junio C Hamano8e08b412010-01-16 18:42:53 +01004043
4044 /* Are we looking at the work tree? */
Jens Lehmann85adbf22010-03-12 22:23:52 +01004045 if (s->dirty_submodule)
Junio C Hamano8e08b412010-01-16 18:42:53 +01004046 dirty = "-dirty";
4047
brian m. carlsona0d12c42016-06-24 23:09:23 +00004048 strbuf_addf(&buf, "Subproject commit %s%s\n",
4049 oid_to_hex(&s->oid), dirty);
Jeff Kingb1ddfb92016-02-22 17:45:08 -05004050 s->size = buf.len;
Linus Torvalds04786752007-04-15 11:14:28 -07004051 if (size_only) {
4052 s->data = NULL;
Jeff Kingb1ddfb92016-02-22 17:45:08 -05004053 strbuf_release(&buf);
4054 } else {
4055 s->data = strbuf_detach(&buf, NULL);
4056 s->should_free = 1;
Linus Torvalds04786752007-04-15 11:14:28 -07004057 }
4058 return 0;
4059}
4060
Junio C Hamano6973dca2006-04-21 23:57:45 -07004061/*
4062 * While doing rename detection and pickaxe operation, we may need to
4063 * grab the data for the blob (or file) for our own in-core comparison.
4064 * diff_filespec has data and size fields for this purpose.
4065 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004066int diff_populate_filespec(struct repository *r,
4067 struct diff_filespec *s,
Jonathan Tan1c37e862020-04-07 15:11:41 -07004068 const struct diff_populate_filespec_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004069{
Jonathan Tan1c37e862020-04-07 15:11:41 -07004070 int size_only = options ? options->check_size_only : 0;
4071 int check_binary = options ? options->check_binary : 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004072 int err = 0;
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01004073 int conv_flags = global_conv_flags_eol;
Junio C Hamano5430bb22013-06-24 14:35:04 -07004074 /*
4075 * demote FAIL to WARN to allow inspecting the situation
4076 * instead of refusing.
4077 */
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01004078 if (conv_flags & CONV_EOL_RNDTRP_DIE)
4079 conv_flags = CONV_EOL_RNDTRP_WARN;
Junio C Hamano5430bb22013-06-24 14:35:04 -07004080
Junio C Hamano6973dca2006-04-21 23:57:45 -07004081 if (!DIFF_FILE_VALID(s))
4082 die("internal error: asking to populate invalid file.");
4083 if (S_ISDIR(s->mode))
4084 return -1;
4085
Junio C Hamano6973dca2006-04-21 23:57:45 -07004086 if (s->data)
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07004087 return 0;
Linus Torvalds04786752007-04-15 11:14:28 -07004088
Junio C Hamano6e0b8ed2007-05-07 01:14:21 -07004089 if (size_only && 0 < s->size)
4090 return 0;
4091
Martin Waitz302b9282007-05-21 22:08:28 +02004092 if (S_ISGITLINK(s->mode))
Linus Torvalds04786752007-04-15 11:14:28 -07004093 return diff_populate_gitlink(s, size_only);
4094
brian m. carlson41c95602016-06-24 23:09:24 +00004095 if (!s->oid_valid ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004096 reuse_worktree_file(r->index, s->path, &s->oid, 0)) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05004097 struct strbuf buf = STRBUF_INIT;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004098 struct stat st;
4099 int fd;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004100
Junio C Hamano6973dca2006-04-21 23:57:45 -07004101 if (lstat(s->path, &st) < 0) {
Andrey Okoshkin10e0ca82017-10-27 12:33:25 +03004102 err_empty:
4103 err = -1;
4104 empty:
4105 s->data = (char *)"";
4106 s->size = 0;
4107 return err;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004108 }
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05004109 s->size = xsize_t(st.st_size);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004110 if (!s->size)
4111 goto empty;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004112 if (S_ISLNK(st.st_mode)) {
Linus Torvaldscf219d82008-12-17 10:26:13 -08004113 struct strbuf sb = STRBUF_INIT;
4114
4115 if (strbuf_readlink(&sb, s->path, s->size))
Junio C Hamano6973dca2006-04-21 23:57:45 -07004116 goto err_empty;
René Scharfe0956a6d2008-12-18 17:56:51 +01004117 s->size = sb.len;
4118 s->data = strbuf_detach(&sb, NULL);
Linus Torvaldscf219d82008-12-17 10:26:13 -08004119 s->should_free = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004120 return 0;
4121 }
Junio C Hamano12426e12017-03-01 18:04:44 +01004122
4123 /*
4124 * Even if the caller would be happy with getting
4125 * only the size, we cannot return early at this
4126 * point if the path requires us to run the content
4127 * conversion.
4128 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004129 if (size_only && !would_convert_to_git(r->index, s->path))
Linus Torvaldscf219d82008-12-17 10:26:13 -08004130 return 0;
Junio C Hamano12426e12017-03-01 18:04:44 +01004131
4132 /*
4133 * Note: this check uses xsize_t(st.st_size) that may
4134 * not be the true size of the blob after it goes
4135 * through convert_to_git(). This may not strictly be
4136 * correct, but the whole point of big_file_threshold
4137 * and is_binary check being that we want to avoid
4138 * opening the file and inspecting the contents, this
4139 * is probably fine.
4140 */
Jonathan Tan1c37e862020-04-07 15:11:41 -07004141 if (check_binary &&
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07004142 s->size > big_file_threshold && s->is_binary == -1) {
4143 s->is_binary = 1;
4144 return 0;
4145 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004146 fd = open(s->path, O_RDONLY);
4147 if (fd < 0)
4148 goto err_empty;
Shawn O. Pearcec4712e42006-12-24 00:47:23 -05004149 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004150 close(fd);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004151 s->should_munmap = 1;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004152
4153 /*
4154 * Convert from working tree format to canonical git format
4155 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004156 if (convert_to_git(r->index, s->path, s->data, s->size, &buf, conv_flags)) {
René Scharfec32f7492007-10-21 11:23:49 +02004157 size_t size = 0;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004158 munmap(s->data, s->size);
4159 s->should_munmap = 0;
René Scharfec32f7492007-10-21 11:23:49 +02004160 s->data = strbuf_detach(&buf, &size);
4161 s->size = size;
Linus Torvalds6c510be2007-02-13 11:07:23 -08004162 s->should_free = 1;
4163 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004164 }
4165 else {
Jonathan Tanc14b6f82020-04-07 15:11:42 -07004166 struct object_info info = {
4167 .sizep = &s->size
4168 };
4169
4170 if (!(size_only || check_binary))
4171 /*
4172 * Set contentp, since there is no chance that merely
4173 * the size is sufficient.
4174 */
4175 info.contentp = &s->data;
4176
Jonathan Tan95acf112020-04-07 15:11:43 -07004177 if (options && options->missing_object_cb) {
4178 if (!oid_object_info_extended(r, &s->oid, &info,
4179 OBJECT_INFO_LOOKUP_REPLACE |
4180 OBJECT_INFO_SKIP_FETCH_OBJECT))
4181 goto object_read;
4182 options->missing_object_cb(options->missing_object_data);
4183 }
Jonathan Tanc14b6f82020-04-07 15:11:42 -07004184 if (oid_object_info_extended(r, &s->oid, &info,
4185 OBJECT_INFO_LOOKUP_REPLACE))
4186 die("unable to read %s", oid_to_hex(&s->oid));
4187
Jonathan Tan95acf112020-04-07 15:11:43 -07004188object_read:
Jonathan Tan1c37e862020-04-07 15:11:41 -07004189 if (size_only || check_binary) {
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07004190 if (size_only)
4191 return 0;
4192 if (s->size > big_file_threshold && s->is_binary == -1) {
4193 s->is_binary = 1;
4194 return 0;
4195 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004196 }
Jonathan Tanc14b6f82020-04-07 15:11:42 -07004197 if (!info.contentp) {
4198 info.contentp = &s->data;
4199 if (oid_object_info_extended(r, &s->oid, &info,
4200 OBJECT_INFO_LOOKUP_REPLACE))
4201 die("unable to read %s", oid_to_hex(&s->oid));
4202 }
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +07004203 s->should_free = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004204 }
4205 return 0;
4206}
4207
Junio C Hamano8ae92e62007-10-02 21:01:03 -07004208void diff_free_filespec_blob(struct diff_filespec *s)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004209{
4210 if (s->should_free)
4211 free(s->data);
4212 else if (s->should_munmap)
4213 munmap(s->data, s->size);
Junio C Hamanofc3abdf2007-05-03 13:05:48 -07004214
4215 if (s->should_free || s->should_munmap) {
4216 s->should_free = s->should_munmap = 0;
4217 s->data = NULL;
4218 }
Jeff Kingeede7b72007-09-25 15:29:42 -04004219}
4220
4221void diff_free_filespec_data(struct diff_filespec *s)
4222{
Jinoh Kang24695932020-11-06 17:14:52 +00004223 if (!s)
4224 return;
4225
Junio C Hamano8ae92e62007-10-02 21:01:03 -07004226 diff_free_filespec_blob(s);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00004227 FREE_AND_NULL(s->cnt_data);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004228}
4229
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004230static void prep_temp_blob(struct index_state *istate,
4231 const char *path, struct diff_tempfile *temp,
Junio C Hamano6973dca2006-04-21 23:57:45 -07004232 void *blob,
4233 unsigned long size,
brian m. carlson09bdff22016-06-24 23:09:29 +00004234 const struct object_id *oid,
Junio C Hamano6973dca2006-04-21 23:57:45 -07004235 int mode)
4236{
Johannes Schindelin4e218f52009-03-21 12:42:52 +01004237 struct strbuf buf = STRBUF_INIT;
David Aguilar003b33a2009-05-31 01:35:52 -07004238 char *path_dup = xstrdup(path);
4239 const char *base = basename(path_dup);
brian m. carlsonc397aac2020-03-16 18:05:03 +00004240 struct checkout_metadata meta;
4241
4242 init_checkout_metadata(&meta, NULL, NULL, oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004243
René Scharfe8af75932022-04-20 22:30:10 +02004244 temp->tempfile = mks_tempfile_dt("git-blob-XXXXXX", base);
Jeff King076aa2c2017-09-05 08:15:08 -04004245 if (!temp->tempfile)
Thomas Rastd824cbb2009-06-27 17:58:46 +02004246 die_errno("unable to create temp-file");
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004247 if (convert_to_working_tree(istate, path,
brian m. carlsonc397aac2020-03-16 18:05:03 +00004248 (const char *)blob, (size_t)size, &buf, &meta)) {
Johannes Schindelin4e218f52009-03-21 12:42:52 +01004249 blob = buf.buf;
4250 size = buf.len;
4251 }
Junio C Hamanoc50424a2017-09-25 15:24:06 +09004252 if (write_in_full(temp->tempfile->fd, blob, size) < 0 ||
Jeff King076aa2c2017-09-05 08:15:08 -04004253 close_tempfile_gently(temp->tempfile))
Thomas Rast0721c312009-06-27 17:58:47 +02004254 die_errno("unable to write temp-file");
Jeff King076aa2c2017-09-05 08:15:08 -04004255 temp->name = get_tempfile_path(temp->tempfile);
brian m. carlson09bdff22016-06-24 23:09:29 +00004256 oid_to_hex_r(temp->hex, oid);
Jeff King5096d492015-09-24 17:06:08 -04004257 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode);
Johannes Schindelin4e218f52009-03-21 12:42:52 +01004258 strbuf_release(&buf);
David Aguilar003b33a2009-05-31 01:35:52 -07004259 free(path_dup);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004260}
4261
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004262static struct diff_tempfile *prepare_temp_file(struct repository *r,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004263 struct diff_filespec *one)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004264{
Jeff King479b0ae2009-01-22 00:59:56 -05004265 struct diff_tempfile *temp = claim_diff_tempfile();
4266
Junio C Hamano6973dca2006-04-21 23:57:45 -07004267 if (!DIFF_FILE_VALID(one)) {
4268 not_a_valid_file:
4269 /* A '-' entry produces this for file-2, and
4270 * a '+' entry produces this for file-1.
4271 */
4272 temp->name = "/dev/null";
Jeff King5096d492015-09-24 17:06:08 -04004273 xsnprintf(temp->hex, sizeof(temp->hex), ".");
4274 xsnprintf(temp->mode, sizeof(temp->mode), ".");
Jeff King479b0ae2009-01-22 00:59:56 -05004275 return temp;
4276 }
4277
Thomas Rastaba47272014-02-16 17:52:34 +01004278 if (!S_ISGITLINK(one->mode) &&
brian m. carlson41c95602016-06-24 23:09:24 +00004279 (!one->oid_valid ||
Jeff Kingf034bb12023-01-06 06:05:00 -05004280 reuse_worktree_file(r->index, one->path, &one->oid, 1))) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07004281 struct stat st;
Jeff Kingf034bb12023-01-06 06:05:00 -05004282 if (lstat(one->path, &st) < 0) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07004283 if (errno == ENOENT)
4284 goto not_a_valid_file;
Jeff Kingf034bb12023-01-06 06:05:00 -05004285 die_errno("stat(%s)", one->path);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004286 }
4287 if (S_ISLNK(st.st_mode)) {
Jeff King3cd73882009-05-25 06:46:09 -04004288 struct strbuf sb = STRBUF_INIT;
Jeff Kingf034bb12023-01-06 06:05:00 -05004289 if (strbuf_readlink(&sb, one->path, st.st_size) < 0)
4290 die_errno("readlink(%s)", one->path);
4291 prep_temp_blob(r->index, one->path, temp, sb.buf, sb.len,
brian m. carlson41c95602016-06-24 23:09:24 +00004292 (one->oid_valid ?
brian m. carlson14228442021-04-26 01:02:56 +00004293 &one->oid : null_oid()),
brian m. carlson41c95602016-06-24 23:09:24 +00004294 (one->oid_valid ?
Junio C Hamano6973dca2006-04-21 23:57:45 -07004295 one->mode : S_IFLNK));
Jeff King3cd73882009-05-25 06:46:09 -04004296 strbuf_release(&sb);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004297 }
4298 else {
4299 /* we can borrow from the file in the work tree */
Jeff Kingf034bb12023-01-06 06:05:00 -05004300 temp->name = one->path;
brian m. carlson41c95602016-06-24 23:09:24 +00004301 if (!one->oid_valid)
brian m. carlson14228442021-04-26 01:02:56 +00004302 oid_to_hex_r(temp->hex, null_oid());
Junio C Hamano6973dca2006-04-21 23:57:45 -07004303 else
René Scharfe24905742017-01-28 23:03:03 +01004304 oid_to_hex_r(temp->hex, &one->oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004305 /* Even though we may sometimes borrow the
4306 * contents from the work tree, we always want
4307 * one->mode. mode is trustworthy even when
Brandon Williams74014152017-05-30 10:30:52 -07004308 * !(one->oid_valid), as long as
Junio C Hamano6973dca2006-04-21 23:57:45 -07004309 * DIFF_FILE_VALID(one).
4310 */
Jeff King5096d492015-09-24 17:06:08 -04004311 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004312 }
Jeff King479b0ae2009-01-22 00:59:56 -05004313 return temp;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004314 }
4315 else {
Jonathan Tan1c37e862020-04-07 15:11:41 -07004316 if (diff_populate_filespec(r, one, NULL))
Junio C Hamano6973dca2006-04-21 23:57:45 -07004317 die("cannot read data blob for %s", one->path);
Jeff Kingf034bb12023-01-06 06:05:00 -05004318 prep_temp_blob(r->index, one->path, temp,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004319 one->data, one->size,
brian m. carlson09bdff22016-06-24 23:09:29 +00004320 &one->oid, one->mode);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004321 }
Jeff King479b0ae2009-01-22 00:59:56 -05004322 return temp;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004323}
4324
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004325static void add_external_diff_name(struct repository *r,
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004326 struct strvec *argv,
Jeff Kingf3efe782014-04-19 15:22:25 -04004327 struct diff_filespec *df)
4328{
Jeff Kingf034bb12023-01-06 06:05:00 -05004329 struct diff_tempfile *temp = prepare_temp_file(r, df);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04004330 strvec_push(argv, temp->name);
4331 strvec_push(argv, temp->hex);
4332 strvec_push(argv, temp->mode);
Jeff Kingf3efe782014-04-19 15:22:25 -04004333}
4334
Junio C Hamano6973dca2006-04-21 23:57:45 -07004335/* An external diff command takes:
4336 *
4337 * diff-cmd name infile1 infile1-sha1 infile1-mode \
4338 * infile2 infile2-sha1 infile2-mode [ rename-to ]
4339 *
4340 */
4341static void run_external_diff(const char *pgm,
4342 const char *name,
4343 const char *other,
4344 struct diff_filespec *one,
4345 struct diff_filespec *two,
4346 const char *xfrm_msg,
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004347 struct diff_options *o)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004348{
René Scharfe0e906732022-10-30 12:51:14 +01004349 struct child_process cmd = CHILD_PROCESS_INIT;
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004350 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004351
René Scharfe0e906732022-10-30 12:51:14 +01004352 strvec_push(&cmd.args, pgm);
4353 strvec_push(&cmd.args, name);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004354
Junio C Hamano6973dca2006-04-21 23:57:45 -07004355 if (one && two) {
Jeff Kingde8f14e2023-01-06 06:04:18 -05004356 add_external_diff_name(o->repo, &cmd.args, one);
4357 add_external_diff_name(o->repo, &cmd.args, two);
4358 if (other) {
René Scharfe0e906732022-10-30 12:51:14 +01004359 strvec_push(&cmd.args, other);
4360 strvec_push(&cmd.args, xfrm_msg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004361 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004362 }
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004363
René Scharfe0e906732022-10-30 12:51:14 +01004364 strvec_pushf(&cmd.env, "GIT_DIFF_PATH_COUNTER=%d",
4365 ++o->diff_path_counter);
4366 strvec_pushf(&cmd.env, "GIT_DIFF_PATH_TOTAL=%d", q->nr);
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004367
Johannes Schindelin3aef54e2019-07-11 01:23:41 -07004368 diff_free_filespec_data(one);
4369 diff_free_filespec_data(two);
René Scharfe0e906732022-10-30 12:51:14 +01004370 cmd.use_shell = 1;
4371 if (run_command(&cmd))
Jeff King89294d12014-04-19 15:19:19 -04004372 die(_("external diff died, stopping at %s"), name);
4373
Junio C Hamano6973dca2006-04-21 23:57:45 -07004374 remove_tempfile();
Junio C Hamano6973dca2006-04-21 23:57:45 -07004375}
4376
Junio C Hamanob67b9612009-01-26 00:08:24 -08004377static int similarity_index(struct diff_filepair *p)
4378{
4379 return p->score * 100 / MAX_SCORE;
4380}
4381
Jeff King4f036662016-10-20 02:21:25 -04004382static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev)
4383{
4384 if (startup_info->have_repository)
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02004385 return repo_find_unique_abbrev(the_repository, oid, abbrev);
Jeff King4f036662016-10-20 02:21:25 -04004386 else {
4387 char *hex = oid_to_hex(oid);
Junio C Hamano0d9c5272016-10-27 14:58:48 -07004388 if (abbrev < 0)
4389 abbrev = FALLBACK_DEFAULT_ABBREV;
brian m. carlson02afca12018-07-16 01:28:05 +00004390 if (abbrev > the_hash_algo->hexsz)
Johannes Schindelin033abf92018-05-02 11:38:39 +02004391 BUG("oid abbreviation out of range: %d", abbrev);
Jack Bates43d19482016-12-06 09:56:14 -07004392 if (abbrev)
4393 hex[abbrev] = '\0';
Jeff King4f036662016-10-20 02:21:25 -04004394 return hex;
4395 }
4396}
4397
Junio C Hamanob67b9612009-01-26 00:08:24 -08004398static void fill_metainfo(struct strbuf *msg,
4399 const char *name,
4400 const char *other,
4401 struct diff_filespec *one,
4402 struct diff_filespec *two,
4403 struct diff_options *o,
Bert Wesarg37466442010-05-04 00:38:07 +02004404 struct diff_filepair *p,
Junio C Hamano59777442010-06-18 11:16:57 -07004405 int *must_show_header,
Bert Wesarg37466442010-05-04 00:38:07 +02004406 int use_color)
Junio C Hamanob67b9612009-01-26 00:08:24 -08004407{
Bert Wesarg37466442010-05-04 00:38:07 +02004408 const char *set = diff_get_color(use_color, DIFF_METAINFO);
4409 const char *reset = diff_get_color(use_color, DIFF_RESET);
John Keeping30997bb2013-02-07 20:15:27 +00004410 const char *line_prefix = diff_line_prefix(o);
Johannes Schindelin2715e8a2022-06-18 00:20:55 +00004411 struct string_list *more_headers = NULL;
Bo Yang7be57612010-05-26 15:23:54 +08004412
Christian Couder296c6bb2010-05-26 04:50:12 +02004413 *must_show_header = 1;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004414 strbuf_init(msg, PATH_MAX * 2 + 300);
4415 switch (p->status) {
4416 case DIFF_STATUS_COPIED:
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004417 strbuf_addf(msg, "%s%ssimilarity index %d%%",
4418 line_prefix, set, similarity_index(p));
4419 strbuf_addf(msg, "%s\n%s%scopy from ",
4420 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004421 quote_c_style(name, msg, NULL, 0);
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004422 strbuf_addf(msg, "%s\n%s%scopy to ", reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004423 quote_c_style(other, msg, NULL, 0);
Bert Wesarg37466442010-05-04 00:38:07 +02004424 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004425 break;
4426 case DIFF_STATUS_RENAMED:
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004427 strbuf_addf(msg, "%s%ssimilarity index %d%%",
4428 line_prefix, set, similarity_index(p));
4429 strbuf_addf(msg, "%s\n%s%srename from ",
4430 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004431 quote_c_style(name, msg, NULL, 0);
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004432 strbuf_addf(msg, "%s\n%s%srename to ",
4433 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004434 quote_c_style(other, msg, NULL, 0);
Bert Wesarg37466442010-05-04 00:38:07 +02004435 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004436 break;
4437 case DIFF_STATUS_MODIFIED:
4438 if (p->score) {
Junio C Hamano98ad90f2010-06-18 11:16:57 -07004439 strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n",
4440 line_prefix,
Bert Wesarg37466442010-05-04 00:38:07 +02004441 set, similarity_index(p), reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004442 break;
4443 }
4444 /* fallthru */
4445 default:
Christian Couder296c6bb2010-05-26 04:50:12 +02004446 *must_show_header = 0;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004447 }
Elijah Newren95433ee2022-02-02 02:37:34 +00004448 if ((more_headers = additional_headers(o, name))) {
4449 add_formatted_headers(msg, more_headers,
4450 line_prefix, set, reset);
4451 *must_show_header = 1;
4452 }
Jeff King9001dc22018-08-28 17:22:48 -04004453 if (one && two && !oideq(&one->oid, &two->oid)) {
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004454 const unsigned hexsz = the_hash_algo->hexsz;
Đoàn Trần Công Danh3046c7f2020-08-21 18:51:47 +07004455 int abbrev = o->abbrev ? o->abbrev : DEFAULT_ABBREV;
4456
4457 if (o->flags.full_index)
4458 abbrev = hexsz;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004459
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004460 if (o->flags.binary) {
Junio C Hamanob67b9612009-01-26 00:08:24 -08004461 mmfile_t mf;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004462 if ((!fill_mmfile(o->repo, &mf, one) &&
4463 diff_filespec_is_binary(o->repo, one)) ||
4464 (!fill_mmfile(o->repo, &mf, two) &&
4465 diff_filespec_is_binary(o->repo, two)))
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004466 abbrev = hexsz;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004467 }
Jeff King4f036662016-10-20 02:21:25 -04004468 strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set,
4469 diff_abbrev_oid(&one->oid, abbrev),
4470 diff_abbrev_oid(&two->oid, abbrev));
Junio C Hamanob67b9612009-01-26 00:08:24 -08004471 if (one->mode == two->mode)
4472 strbuf_addf(msg, " %06o", one->mode);
Bert Wesarg37466442010-05-04 00:38:07 +02004473 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004474 }
Junio C Hamanob67b9612009-01-26 00:08:24 -08004475}
4476
Junio C Hamano6973dca2006-04-21 23:57:45 -07004477static void run_diff_cmd(const char *pgm,
4478 const char *name,
4479 const char *other,
Junio C Hamanocd676a52008-02-12 14:26:02 -08004480 const char *attr_path,
Junio C Hamano6973dca2006-04-21 23:57:45 -07004481 struct diff_filespec *one,
4482 struct diff_filespec *two,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004483 struct strbuf *msg,
Junio C Hamano051308f2006-05-04 16:51:44 -07004484 struct diff_options *o,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004485 struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004486{
Junio C Hamanob67b9612009-01-26 00:08:24 -08004487 const char *xfrm_msg = NULL;
4488 int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
Christian Couder296c6bb2010-05-26 04:50:12 +02004489 int must_show_header = 0;
John Caia4cf9002023-02-20 21:04:42 +00004490 struct userdiff_driver *drv = NULL;
Junio C Hamanob67b9612009-01-26 00:08:24 -08004491
John Caia4cf9002023-02-20 21:04:42 +00004492 if (o->flags.allow_external || !o->ignore_driver_algorithm)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 17:57:33 +02004493 drv = userdiff_find_by_path(o->repo->index, attr_path);
John Caia4cf9002023-02-20 21:04:42 +00004494
4495 if (o->flags.allow_external && drv && drv->external)
4496 pgm = drv->external;
Junio C Hamanof1af60b2007-04-22 17:52:55 -07004497
Bert Wesarg37466442010-05-04 00:38:07 +02004498 if (msg) {
4499 /*
4500 * don't use colors when the header is intended for an
4501 * external diff driver
4502 */
4503 fill_metainfo(msg, name, other, one, two, o, p,
Junio C Hamano59777442010-06-18 11:16:57 -07004504 &must_show_header,
Jeff Kingdaa0c3d2011-08-17 22:04:23 -07004505 want_color(o->use_color) && !pgm);
Bert Wesarg37466442010-05-04 00:38:07 +02004506 xfrm_msg = msg->len ? msg->buf : NULL;
4507 }
4508
Junio C Hamano6973dca2006-04-21 23:57:45 -07004509 if (pgm) {
Jeff King4bc17922019-02-14 00:49:42 -05004510 run_external_diff(pgm, name, other, one, two, xfrm_msg, o);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004511 return;
4512 }
John Caia4cf9002023-02-20 21:04:42 +00004513 if (one && two) {
4514 if (!o->ignore_driver_algorithm && drv && drv->algorithm)
4515 set_diff_algorithm(o, drv->algorithm);
4516
Junio C Hamano6973dca2006-04-21 23:57:45 -07004517 builtin_diff(name, other ? other : name,
Christian Couder296c6bb2010-05-26 04:50:12 +02004518 one, two, xfrm_msg, must_show_header,
4519 o, complete_rewrite);
John Caia4cf9002023-02-20 21:04:42 +00004520 } else {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004521 fprintf(o->file, "* Unmerged path %s\n", name);
John Caia4cf9002023-02-20 21:04:42 +00004522 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004523}
4524
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004525static void diff_fill_oid_info(struct diff_filespec *one, struct index_state *istate)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004526{
4527 if (DIFF_FILE_VALID(one)) {
brian m. carlson41c95602016-06-24 23:09:24 +00004528 if (!one->oid_valid) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07004529 struct stat st;
Junio C Hamano4682d852012-06-27 20:14:47 -07004530 if (one->is_stdin) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00004531 oidclr(&one->oid);
Johannes Schindelin5332b2a2007-02-25 23:36:10 +01004532 return;
4533 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004534 if (lstat(one->path, &st) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +02004535 die_errno("stat '%s'", one->path);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004536 if (index_path(istate, &one->oid, one->path, &st, 0))
Alexander Potashevd7530702009-01-04 21:38:41 +03004537 die("cannot hash %s", one->path);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004538 }
4539 }
4540 else
brian m. carlsona0d12c42016-06-24 23:09:23 +00004541 oidclr(&one->oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004542}
4543
Junio C Hamanocd676a52008-02-12 14:26:02 -08004544static void strip_prefix(int prefix_length, const char **namep, const char **otherp)
4545{
4546 /* Strip the prefix but do not molest /dev/null and absolute paths */
Johannes Sixtffd04e92018-10-19 18:58:07 +02004547 if (*namep && !is_absolute_path(*namep)) {
Junio C Hamanocd676a52008-02-12 14:26:02 -08004548 *namep += prefix_length;
Jakub Narebskid8faea92010-08-09 10:50:53 -04004549 if (**namep == '/')
4550 ++*namep;
4551 }
Johannes Sixtffd04e92018-10-19 18:58:07 +02004552 if (*otherp && !is_absolute_path(*otherp)) {
Junio C Hamanocd676a52008-02-12 14:26:02 -08004553 *otherp += prefix_length;
Jakub Narebskid8faea92010-08-09 10:50:53 -04004554 if (**otherp == '/')
4555 ++*otherp;
4556 }
Junio C Hamanocd676a52008-02-12 14:26:02 -08004557}
4558
Junio C Hamano6973dca2006-04-21 23:57:45 -07004559static void run_diff(struct diff_filepair *p, struct diff_options *o)
4560{
4561 const char *pgm = external_diff();
Pierre Habouzit663af342007-09-20 00:42:15 +02004562 struct strbuf msg;
Pierre Habouzit663af342007-09-20 00:42:15 +02004563 struct diff_filespec *one = p->one;
4564 struct diff_filespec *two = p->two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004565 const char *name;
4566 const char *other;
Junio C Hamanocd676a52008-02-12 14:26:02 -08004567 const char *attr_path;
Pierre Habouzit663af342007-09-20 00:42:15 +02004568
Stefan Bellerf2d2a5d2017-06-29 17:06:46 -07004569 name = one->path;
4570 other = (strcmp(name, two->path) ? two->path : NULL);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004571 attr_path = name;
4572 if (o->prefix_length)
4573 strip_prefix(o->prefix_length, &name, &other);
4574
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004575 if (!o->flags.allow_external)
Junio C Hamanobd8c1a92012-07-17 22:08:59 -07004576 pgm = NULL;
4577
Junio C Hamanocd676a52008-02-12 14:26:02 -08004578 if (DIFF_PAIR_UNMERGED(p)) {
4579 run_diff_cmd(pgm, name, NULL, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004580 NULL, NULL, NULL, o, p);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004581 return;
4582 }
4583
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004584 diff_fill_oid_info(one, o->repo->index);
4585 diff_fill_oid_info(two, o->repo->index);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004586
Junio C Hamano6973dca2006-04-21 23:57:45 -07004587 if (!pgm &&
4588 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
4589 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
Junio C Hamanob67b9612009-01-26 00:08:24 -08004590 /*
4591 * a filepair that changes between file and symlink
Junio C Hamano6973dca2006-04-21 23:57:45 -07004592 * needs to be split into deletion and creation.
4593 */
4594 struct diff_filespec *null = alloc_filespec(two->path);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004595 run_diff_cmd(NULL, name, other, attr_path,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004596 one, null, &msg,
4597 o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004598 free(null);
Junio C Hamanob67b9612009-01-26 00:08:24 -08004599 strbuf_release(&msg);
4600
Junio C Hamano6973dca2006-04-21 23:57:45 -07004601 null = alloc_filespec(one->path);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004602 run_diff_cmd(NULL, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004603 null, two, &msg, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004604 free(null);
4605 }
4606 else
Junio C Hamanocd676a52008-02-12 14:26:02 -08004607 run_diff_cmd(pgm, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 00:08:24 -08004608 one, two, &msg, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004609
Pierre Habouzit663af342007-09-20 00:42:15 +02004610 strbuf_release(&msg);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004611}
4612
4613static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
4614 struct diffstat_t *diffstat)
4615{
4616 const char *name;
4617 const char *other;
4618
John Caia4cf9002023-02-20 21:04:42 +00004619 if (!o->ignore_driver_algorithm) {
4620 struct userdiff_driver *drv = userdiff_find_by_path(o->repo->index,
4621 p->one->path);
4622
4623 if (drv && drv->algorithm)
4624 set_diff_algorithm(o, drv->algorithm);
4625 }
4626
Junio C Hamano6973dca2006-04-21 23:57:45 -07004627 if (DIFF_PAIR_UNMERGED(p)) {
4628 /* unmerged */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004629 builtin_diffstat(p->one->path, NULL, NULL, NULL,
4630 diffstat, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004631 return;
4632 }
4633
4634 name = p->one->path;
4635 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
4636
Junio C Hamanocd676a52008-02-12 14:26:02 -08004637 if (o->prefix_length)
4638 strip_prefix(o->prefix_length, &name, &other);
4639
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004640 diff_fill_oid_info(p->one, o->repo->index);
4641 diff_fill_oid_info(p->two, o->repo->index);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004642
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02004643 builtin_diffstat(name, other, p->one, p->two,
4644 diffstat, o, p);
Junio C Hamano6973dca2006-04-21 23:57:45 -07004645}
4646
Johannes Schindelin88246892006-05-20 23:43:13 +02004647static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
4648{
4649 const char *name;
4650 const char *other;
Junio C Hamanocd676a52008-02-12 14:26:02 -08004651 const char *attr_path;
Johannes Schindelin88246892006-05-20 23:43:13 +02004652
4653 if (DIFF_PAIR_UNMERGED(p)) {
4654 /* unmerged */
4655 return;
4656 }
4657
4658 name = p->one->path;
4659 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
Junio C Hamanocd676a52008-02-12 14:26:02 -08004660 attr_path = other ? other : name;
4661
4662 if (o->prefix_length)
4663 strip_prefix(o->prefix_length, &name, &other);
Johannes Schindelin88246892006-05-20 23:43:13 +02004664
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02004665 diff_fill_oid_info(p->one, o->repo->index);
4666 diff_fill_oid_info(p->two, o->repo->index);
Johannes Schindelin88246892006-05-20 23:43:13 +02004667
Junio C Hamanocd676a52008-02-12 14:26:02 -08004668 builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
Johannes Schindelin88246892006-05-20 23:43:13 +02004669}
4670
Nguyễn Thái Ngọc Duye6757652018-09-21 17:57:24 +02004671void repo_diff_setup(struct repository *r, struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004672{
Johannes Schindelinbe4f2b42010-08-05 10:49:55 +02004673 memcpy(options, &default_diff_options, sizeof(*options));
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004674
4675 options->file = stdout;
Nguyễn Thái Ngọc Duye6757652018-09-21 17:57:24 +02004676 options->repo = r;
Daniel Barkalowc0c77732008-03-09 22:43:39 -04004677
Stefan Beller7648b792018-08-17 13:43:52 -07004678 options->output_indicators[OUTPUT_INDICATOR_NEW] = '+';
4679 options->output_indicators[OUTPUT_INDICATOR_OLD] = '-';
4680 options->output_indicators[OUTPUT_INDICATOR_CONTEXT] = ' ';
Jack Bates43d19482016-12-06 09:56:14 -07004681 options->abbrev = DEFAULT_ABBREV;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004682 options->line_termination = '\n';
4683 options->break_opt = -1;
4684 options->rename_limit = -1;
Johan Herland712d2c72011-04-29 11:36:20 +02004685 options->dirstat_permille = diff_dirstat_permille_default;
Jeff Muizelaar6468a4e2012-09-27 15:12:52 -04004686 options->context = diff_context_default;
Vegard Nossumc4888672017-01-12 13:21:11 +01004687 options->interhunkcontext = diff_interhunk_context_default;
Junio C Hamanoa17505f2016-10-04 15:26:27 -07004688 options->ws_error_highlight = ws_error_highlight_default;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004689 options->flags.rename_empty = 1;
Laurent Arnoudc28ded82020-05-22 12:46:18 +02004690 options->flags.relative_name = diff_relative;
Stefan Beller15af58c2018-01-04 14:50:42 -08004691 options->objfind = NULL;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004692
Kirill Smelkov72441af2014-04-07 01:46:26 +04004693 /* pathchange left =NULL by default */
Junio C Hamano6973dca2006-04-21 23:57:45 -07004694 options->change = diff_change;
4695 options->add_remove = diff_addremove;
Jeff Kingf1c96262011-08-17 22:03:12 -07004696 options->use_color = diff_use_color_default;
Eric Wongb68ea122006-07-07 04:01:23 -07004697 options->detect_rename = diff_detect_rename_default;
Michal Privoznik07ab4de2013-01-16 08:51:57 +01004698 options->xdl_opts |= diff_algorithm;
Michael Haggerty433860f2016-09-05 11:44:51 +02004699 if (diff_indent_heuristic)
4700 DIFF_XDL_SET(options, INDENT_HEURISTIC);
Johannes Schindelineab9a402007-12-18 19:32:14 +00004701
Samuel Bronson6d8940b2013-12-18 19:08:12 -05004702 options->orderfile = diff_order_file_cfg;
4703
Sangeeta Jain8ef93122020-11-10 14:09:00 +05304704 if (!options->flags.ignore_submodule_set)
4705 options->flags.ignore_untracked_in_submodules = 1;
4706
Eli Collinsf89504d2010-05-02 19:03:41 -07004707 if (diff_no_prefix) {
Jeff King6799aad2023-03-09 01:07:06 -05004708 diff_set_noprefix(options);
Eli Collinsf89504d2010-05-02 19:03:41 -07004709 } else if (!diff_mnemonic_prefix) {
Jeff King6799aad2023-03-09 01:07:06 -05004710 diff_set_default_prefix(options);
Junio C Hamanoa5a818e2008-08-18 20:08:09 -07004711 }
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07004712
4713 options->color_moved = diff_color_moved_default;
Stefan Beller626c0b52018-07-18 12:31:56 -07004714 options->color_moved_ws_handling = diff_color_moved_ws_default;
Junio C Hamano6973dca2006-04-21 23:57:45 -07004715}
4716
Johannes Schindelin4d4d4ea2022-01-28 12:02:49 +00004717static const char diff_status_letters[] = {
4718 DIFF_STATUS_ADDED,
4719 DIFF_STATUS_COPIED,
4720 DIFF_STATUS_DELETED,
4721 DIFF_STATUS_MODIFIED,
4722 DIFF_STATUS_RENAMED,
4723 DIFF_STATUS_TYPE_CHANGED,
4724 DIFF_STATUS_UNKNOWN,
4725 DIFF_STATUS_UNMERGED,
4726 DIFF_STATUS_FILTER_AON,
4727 DIFF_STATUS_FILTER_BROKEN,
4728 '\0',
4729};
4730
4731static unsigned int filter_bit['Z' + 1];
4732
4733static void prepare_filter_bits(void)
4734{
4735 int i;
4736
4737 if (!filter_bit[DIFF_STATUS_ADDED]) {
4738 for (i = 0; diff_status_letters[i]; i++)
4739 filter_bit[(int) diff_status_letters[i]] = (1 << i);
4740 }
4741}
4742
4743static unsigned filter_bit_tst(char status, const struct diff_options *opt)
4744{
4745 return opt->filter & filter_bit[(int) status];
4746}
4747
4748unsigned diff_filter_bit(char status)
4749{
4750 prepare_filter_bits();
4751 return filter_bit[(int) status];
4752}
4753
Thomas Rast28452652012-08-03 14:16:24 +02004754void diff_setup_done(struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07004755{
Stefan Beller4d8c51a2018-01-04 14:50:44 -08004756 unsigned check_mask = DIFF_FORMAT_NAME |
4757 DIFF_FORMAT_NAME_STATUS |
4758 DIFF_FORMAT_CHECKDIFF |
4759 DIFF_FORMAT_NO_OUTPUT;
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004760 /*
4761 * This must be signed because we're comparing against a potentially
4762 * negative value.
4763 */
4764 const int hexsz = the_hash_algo->hexsz;
Timo Hirvonend7de00f2006-06-24 20:26:49 +03004765
Junio C Hamano6c374002013-05-10 17:10:11 +02004766 if (options->set_default)
4767 options->set_default(options);
4768
Stefan Beller4d8c51a2018-01-04 14:50:44 -08004769 if (HAS_MULTI_BITS(options->output_format & check_mask))
Jean-Noël Avila246cac82022-01-05 20:02:24 +00004770 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
4771 "--name-only", "--name-status", "--check", "-s");
Timo Hirvonend7de00f2006-06-24 20:26:49 +03004772
Stefan Beller5e505252018-01-04 14:50:43 -08004773 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
Jean-Noël Avila246cac82022-01-05 20:02:24 +00004774 die(_("options '%s', '%s', and '%s' cannot be used together"),
4775 "-G", "-S", "--find-object");
Stefan Beller5e505252018-01-04 14:50:43 -08004776
Ævar Arnfjörð Bjarmason188e9e22021-04-12 19:15:14 +02004777 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_G_REGEX_MASK))
Jean-Noël Avila246cac82022-01-05 20:02:24 +00004778 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s'"),
4779 "-G", "--pickaxe-regex", "--pickaxe-regex", "-S");
Ævar Arnfjörð Bjarmason188e9e22021-04-12 19:15:14 +02004780
Ævar Arnfjörð Bjarmasond26ec882021-04-12 19:15:15 +02004781 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK))
Jean-Noël Avila246cac82022-01-05 20:02:24 +00004782 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s' and '%s'"),
4783 "--pickaxe-all", "--find-object", "--pickaxe-all", "-G", "-S");
Ævar Arnfjörð Bjarmasond26ec882021-04-12 19:15:15 +02004784
Junio C Hamanof2451942009-05-22 12:45:29 -07004785 /*
4786 * Most of the time we can say "there are changes"
4787 * only by checking if there are changed paths, but
4788 * --ignore-whitespace* options force us to look
Jim Meyering97bf2a02009-08-30 22:27:02 +02004789 * inside contents.
Junio C Hamanof2451942009-05-22 12:45:29 -07004790 */
4791
Junio C Hamano50f04392020-12-16 17:27:13 -08004792 if ((options->xdl_opts & XDF_WHITESPACE_FLAGS) ||
4793 options->ignore_regex_nr)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004794 options->flags.diff_from_contents = 1;
Junio C Hamanof2451942009-05-22 12:45:29 -07004795 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004796 options->flags.diff_from_contents = 0;
Junio C Hamanof2451942009-05-22 12:45:29 -07004797
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004798 if (options->flags.find_copies_harder)
Junio C Hamano03b9d562006-08-09 13:17:19 -07004799 options->detect_rename = DIFF_DETECT_COPY;
4800
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004801 if (!options->flags.relative_name)
Junio C Hamanocd676a52008-02-12 14:26:02 -08004802 options->prefix = NULL;
4803 if (options->prefix)
4804 options->prefix_length = strlen(options->prefix);
4805 else
4806 options->prefix_length = 0;
4807
Timo Hirvonenc6744342006-06-24 20:21:53 +03004808 if (options->output_format & (DIFF_FORMAT_NAME |
4809 DIFF_FORMAT_NAME_STATUS |
4810 DIFF_FORMAT_CHECKDIFF |
4811 DIFF_FORMAT_NO_OUTPUT))
4812 options->output_format &= ~(DIFF_FORMAT_RAW |
Junio C Hamano74e2abe2006-10-12 03:01:00 -07004813 DIFF_FORMAT_NUMSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004814 DIFF_FORMAT_DIFFSTAT |
Nicolas Pitreebd124c2006-12-14 23:15:44 -05004815 DIFF_FORMAT_SHORTSTAT |
Linus Torvalds7df7c012008-02-12 13:26:31 -08004816 DIFF_FORMAT_DIRSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004817 DIFF_FORMAT_SUMMARY |
4818 DIFF_FORMAT_PATCH);
4819
Junio C Hamano6973dca2006-04-21 23:57:45 -07004820 /*
4821 * These cases always need recursive; we do not drop caller-supplied
4822 * recursive bits for other formats here.
4823 */
Timo Hirvonenc6744342006-06-24 20:21:53 +03004824 if (options->output_format & (DIFF_FORMAT_PATCH |
Junio C Hamano74e2abe2006-10-12 03:01:00 -07004825 DIFF_FORMAT_NUMSTAT |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004826 DIFF_FORMAT_DIFFSTAT |
Nicolas Pitreebd124c2006-12-14 23:15:44 -05004827 DIFF_FORMAT_SHORTSTAT |
Linus Torvalds7df7c012008-02-12 13:26:31 -08004828 DIFF_FORMAT_DIRSTAT |
Johannes Schindelind7014dc2006-10-03 23:09:56 +02004829 DIFF_FORMAT_SUMMARY |
Timo Hirvonenc6744342006-06-24 20:21:53 +03004830 DIFF_FORMAT_CHECKDIFF))
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004831 options->flags.recursive = 1;
Junio C Hamano5e363542006-05-22 00:31:02 -07004832 /*
Junio C Hamano3969cf72006-06-27 15:08:19 -07004833 * Also pickaxe would not work very well if you do not say recursive
Junio C Hamano5e363542006-05-22 00:31:02 -07004834 */
Stefan Bellercf630512018-01-04 14:50:41 -08004835 if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004836 options->flags.recursive = 1;
Jens Lehmannae6d5c12010-03-11 22:50:25 +01004837 /*
4838 * When patches are generated, submodules diffed against the work tree
4839 * must be checked for dirtiness too so it can be shown in the output
4840 */
4841 if (options->output_format & DIFF_FORMAT_PATCH)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004842 options->flags.dirty_submodules = 1;
Junio C Hamano5e363542006-05-22 00:31:02 -07004843
Junio C Hamano6973dca2006-04-21 23:57:45 -07004844 if (options->detect_rename && options->rename_limit < 0)
4845 options->rename_limit = diff_rename_limit_default;
brian m. carlsonde1d81d2018-05-02 00:25:52 +00004846 if (hexsz < options->abbrev)
4847 options->abbrev = hexsz; /* full */
Junio C Hamano6973dca2006-04-21 23:57:45 -07004848
Junio C Hamano68aacb22007-03-14 11:12:13 -07004849 /*
4850 * It does not make sense to show the first hit we happened
4851 * to have found. It does not make sense not to return with
4852 * exit code in such a case either.
4853 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004854 if (options->flags.quick) {
Junio C Hamano68aacb22007-03-14 11:12:13 -07004855 options->output_format = DIFF_FORMAT_NO_OUTPUT;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004856 options->flags.exit_with_status = 1;
Junio C Hamano68aacb22007-03-14 11:12:13 -07004857 }
Zoltan Klingeree7fb0b2013-12-06 10:38:46 +11004858
Jeff King0ea7d5b2013-12-16 15:02:21 -05004859 options->diff_path_counter = 0;
Junio C Hamanob0e2c992014-06-16 10:07:09 -07004860
Brandon Williams0d1e0e72017-10-31 11:19:11 -07004861 if (options->flags.follow_renames && options->pathspec.nr != 1)
Jeff Kingdd63f162014-05-20 02:49:20 -04004862 die(_("--follow requires exactly one pathspec"));
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07004863
4864 if (!options->use_color || external_diff())
4865 options->color_moved = 0;
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07004866
Johannes Schindelin75408ca2022-01-28 12:02:50 +00004867 if (options->filter_not) {
4868 if (!options->filter)
4869 options->filter = ~filter_bit[DIFF_STATUS_FILTER_AON];
4870 options->filter &= ~options->filter_not;
4871 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07004872}
4873
Matthieu Moydea007f2010-08-05 10:22:52 +02004874int parse_long_opt(const char *opt, const char **argv,
4875 const char **optarg)
4876{
4877 const char *arg = argv[0];
Jeff King95b567c2014-06-18 15:48:29 -04004878 if (!skip_prefix(arg, "--", &arg))
Matthieu Moydea007f2010-08-05 10:22:52 +02004879 return 0;
Jeff King95b567c2014-06-18 15:48:29 -04004880 if (!skip_prefix(arg, opt, &arg))
Matthieu Moydea007f2010-08-05 10:22:52 +02004881 return 0;
Nicolas Vigierb0d12fc2013-10-31 12:08:28 +01004882 if (*arg == '=') { /* stuck form: --option=value */
Matthieu Moydea007f2010-08-05 10:22:52 +02004883 *optarg = arg + 1;
4884 return 1;
4885 }
4886 if (*arg != '\0')
4887 return 0;
4888 /* separate form: --option value */
4889 if (!argv[1])
4890 die("Option '--%s' requires a value", opt);
4891 *optarg = argv[1];
4892 return 2;
4893}
4894
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004895static int diff_opt_stat(const struct option *opt, const char *value, int unset)
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004896{
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004897 struct diff_options *options = opt->value;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004898 int width = options->stat_width;
4899 int name_width = options->stat_name_width;
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004900 int graph_width = options->stat_graph_width;
Michael J Gruber808e1db2011-05-27 14:36:41 +02004901 int count = options->stat_count;
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004902 char *end;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004903
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004904 BUG_ON_OPT_NEG(unset);
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004905
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004906 if (!strcmp(opt->long_name, "stat")) {
4907 if (value) {
4908 width = strtoul(value, &end, 10);
4909 if (*end == ',')
4910 name_width = strtoul(end+1, &end, 10);
4911 if (*end == ',')
4912 count = strtoul(end+1, &end, 10);
4913 if (*end)
4914 return error(_("invalid --stat value: %s"), value);
Matthieu Moy1e572082010-08-05 10:22:54 +02004915 }
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004916 } else if (!strcmp(opt->long_name, "stat-width")) {
4917 width = strtoul(value, &end, 10);
4918 if (*end)
4919 return error(_("%s expects a numerical value"),
4920 opt->long_name);
4921 } else if (!strcmp(opt->long_name, "stat-name-width")) {
4922 name_width = strtoul(value, &end, 10);
4923 if (*end)
4924 return error(_("%s expects a numerical value"),
4925 opt->long_name);
4926 } else if (!strcmp(opt->long_name, "stat-graph-width")) {
4927 graph_width = strtoul(value, &end, 10);
4928 if (*end)
4929 return error(_("%s expects a numerical value"),
4930 opt->long_name);
4931 } else if (!strcmp(opt->long_name, "stat-count")) {
4932 count = strtoul(value, &end, 10);
4933 if (*end)
4934 return error(_("%s expects a numerical value"),
4935 opt->long_name);
4936 } else
4937 BUG("%s should not get here", opt->long_name);
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004938
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004939 options->output_format |= DIFF_FORMAT_DIFFSTAT;
4940 options->stat_name_width = name_width;
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 13:26:45 +01004941 options->stat_graph_width = graph_width;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004942 options->stat_width = width;
Michael J Gruber808e1db2011-05-27 14:36:41 +02004943 options->stat_count = count;
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07004944 return 0;
Jonathan Nieder4d7f7a42010-08-05 10:22:53 +02004945}
4946
Johan Herland333f3fb2011-04-29 11:36:18 +02004947static int parse_dirstat_opt(struct diff_options *options, const char *params)
4948{
Johan Herland51670fc2011-04-29 11:36:22 +02004949 struct strbuf errmsg = STRBUF_INIT;
4950 if (parse_dirstat_params(options, params, &errmsg))
Johan Herland7478ac52011-04-29 11:36:23 +02004951 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
Johan Herland51670fc2011-04-29 11:36:22 +02004952 errmsg.buf);
4953 strbuf_release(&errmsg);
Johan Herland333f3fb2011-04-29 11:36:18 +02004954 /*
4955 * The caller knows a dirstat-related option is given from the command
4956 * line; allow it to say "return this_function();"
4957 */
4958 options->output_format |= DIFF_FORMAT_DIRSTAT;
4959 return 1;
4960}
4961
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004962static int diff_opt_diff_filter(const struct option *option,
4963 const char *optarg, int unset)
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004964{
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004965 struct diff_options *opt = option->value;
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004966 int i, optch;
4967
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004968 BUG_ON_OPT_NEG(unset);
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004969 prepare_filter_bits();
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004970
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004971 for (i = 0; (optch = optarg[i]) != '\0'; i++) {
4972 unsigned int bit;
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004973 int negate;
4974
4975 if ('a' <= optch && optch <= 'z') {
4976 negate = 1;
4977 optch = toupper(optch);
4978 } else {
4979 negate = 0;
4980 }
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004981
4982 bit = (0 <= optch && optch <= 'Z') ? filter_bit[optch] : 0;
4983 if (!bit)
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07004984 return error(_("unknown change class '%c' in --diff-filter=%s"),
4985 optarg[i], optarg);
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004986 if (negate)
Johannes Schindelin75408ca2022-01-28 12:02:50 +00004987 opt->filter_not |= bit;
Junio C Hamano7f2ea5f2013-07-17 16:19:19 -07004988 else
4989 opt->filter |= bit;
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07004990 }
4991 return 0;
4992}
4993
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +01004994static void enable_patch_output(int *fmt)
4995{
Matthieu Moy71482d32013-07-16 10:05:37 +02004996 *fmt &= ~DIFF_FORMAT_NO_OUTPUT;
4997 *fmt |= DIFF_FORMAT_PATCH;
4998}
4999
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07005000static int diff_opt_ws_error_highlight(const struct option *option,
5001 const char *arg, int unset)
Junio C Hamanob8767f72015-05-26 10:11:28 -07005002{
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07005003 struct diff_options *opt = option->value;
Junio C Hamano077965f2016-10-04 15:03:33 -07005004 int val = parse_ws_error_highlight(arg);
Junio C Hamanob8767f72015-05-26 10:11:28 -07005005
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07005006 BUG_ON_OPT_NEG(unset);
5007 if (val < 0)
5008 return error(_("unknown value after ws-error-highlight=%.*s"),
5009 -1 - val, arg);
Junio C Hamanob8767f72015-05-26 10:11:28 -07005010 opt->ws_error_highlight = val;
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07005011 return 0;
Junio C Hamanob8767f72015-05-26 10:11:28 -07005012}
5013
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07005014static int diff_opt_find_object(const struct option *option,
5015 const char *arg, int unset)
Stefan Beller15af58c2018-01-04 14:50:42 -08005016{
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07005017 struct diff_options *opt = option->value;
Stefan Beller15af58c2018-01-04 14:50:42 -08005018 struct object_id oid;
5019
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07005020 BUG_ON_OPT_NEG(unset);
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02005021 if (repo_get_oid(the_repository, arg, &oid))
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07005022 return error(_("unable to resolve '%s'"), arg);
Stefan Beller15af58c2018-01-04 14:50:42 -08005023
5024 if (!opt->objfind)
René Scharfeca56dad2021-03-13 17:17:22 +01005025 CALLOC_ARRAY(opt->objfind, 1);
Stefan Beller15af58c2018-01-04 14:50:42 -08005026
5027 opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND;
5028 opt->flags.recursive = 1;
5029 opt->flags.tree_in_recursive = 1;
5030 oidset_insert(opt->objfind, &oid);
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07005031 return 0;
Stefan Beller15af58c2018-01-04 14:50:42 -08005032}
5033
Nguyễn Thái Ngọc Duydf84a432019-03-05 19:30:11 +07005034static int diff_opt_anchored(const struct option *opt,
5035 const char *arg, int unset)
5036{
5037 struct diff_options *options = opt->value;
5038
5039 BUG_ON_OPT_NEG(unset);
5040 options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
5041 ALLOC_GROW(options->anchors, options->anchors_nr + 1,
5042 options->anchors_alloc);
5043 options->anchors[options->anchors_nr++] = xstrdup(arg);
5044 return 0;
5045}
5046
Nguyễn Thái Ngọc Duy6d9af6f2019-03-05 19:30:12 +07005047static int diff_opt_binary(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 BUG_ON_OPT_ARG(arg);
5054 enable_patch_output(&options->output_format);
5055 options->flags.binary = 1;
5056 return 0;
5057}
5058
Nguyễn Thái Ngọc Duyced4e172019-02-21 18:16:13 +07005059static int diff_opt_break_rewrites(const struct option *opt,
5060 const char *arg, int unset)
5061{
5062 int *break_opt = opt->value;
5063 int opt1, opt2;
5064
5065 BUG_ON_OPT_NEG(unset);
5066 if (!arg)
5067 arg = "";
5068 opt1 = parse_rename_score(&arg);
5069 if (*arg == 0)
5070 opt2 = 0;
5071 else if (*arg != '/')
5072 return error(_("%s expects <n>/<m> form"), opt->long_name);
5073 else {
5074 arg++;
5075 opt2 = parse_rename_score(&arg);
5076 }
5077 if (*arg != 0)
5078 return error(_("%s expects <n>/<m> form"), opt->long_name);
5079 *break_opt = opt1 | (opt2 << 16);
5080 return 0;
5081}
5082
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005083static int diff_opt_char(const struct option *opt,
5084 const char *arg, int unset)
5085{
5086 char *value = opt->value;
5087
5088 BUG_ON_OPT_NEG(unset);
5089 if (arg[1])
5090 return error(_("%s expects a character, got '%s'"),
5091 opt->long_name, arg);
5092 *value = arg[0];
5093 return 0;
5094}
5095
Nguyễn Thái Ngọc Duy59311a92019-03-24 15:20:09 +07005096static int diff_opt_color_moved(const struct option *opt,
5097 const char *arg, int unset)
5098{
5099 struct diff_options *options = opt->value;
5100
5101 if (unset) {
5102 options->color_moved = COLOR_MOVED_NO;
5103 } else if (!arg) {
5104 if (diff_color_moved_default)
5105 options->color_moved = diff_color_moved_default;
5106 if (options->color_moved == COLOR_MOVED_NO)
5107 options->color_moved = COLOR_MOVED_DEFAULT;
5108 } else {
5109 int cm = parse_color_moved(arg);
5110 if (cm < 0)
5111 return error(_("bad --color-moved argument: %s"), arg);
5112 options->color_moved = cm;
5113 }
5114 return 0;
5115}
5116
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005117static int diff_opt_color_moved_ws(const struct option *opt,
5118 const char *arg, int unset)
5119{
5120 struct diff_options *options = opt->value;
5121 unsigned cm;
5122
Nguyễn Thái Ngọc Duybb987292019-03-24 15:20:11 +07005123 if (unset) {
5124 options->color_moved_ws_handling = 0;
5125 return 0;
5126 }
5127
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005128 cm = parse_color_moved_ws(arg);
5129 if (cm & COLOR_MOVED_WS_ERROR)
5130 return error(_("invalid mode '%s' in --color-moved-ws"), arg);
5131 options->color_moved_ws_handling = cm;
5132 return 0;
5133}
5134
Nguyễn Thái Ngọc Duy212db692019-03-05 19:30:20 +07005135static int diff_opt_color_words(const struct option *opt,
5136 const char *arg, int unset)
5137{
5138 struct diff_options *options = opt->value;
5139
5140 BUG_ON_OPT_NEG(unset);
5141 options->use_color = 1;
5142 options->word_diff = DIFF_WORDS_COLOR;
5143 options->word_regex = arg;
5144 return 0;
5145}
5146
Nguyễn Thái Ngọc Duy7d7942b2019-02-21 18:16:11 +07005147static int diff_opt_compact_summary(const struct option *opt,
5148 const char *arg, int unset)
5149{
5150 struct diff_options *options = opt->value;
5151
5152 BUG_ON_OPT_ARG(arg);
5153 if (unset) {
5154 options->flags.stat_with_summary = 0;
5155 } else {
5156 options->flags.stat_with_summary = 1;
5157 options->output_format |= DIFF_FORMAT_DIFFSTAT;
5158 }
5159 return 0;
5160}
5161
Nguyễn Thái Ngọc Duy10f35b12019-03-05 19:30:10 +07005162static int diff_opt_diff_algorithm(const struct option *opt,
5163 const char *arg, int unset)
5164{
5165 struct diff_options *options = opt->value;
Nguyễn Thái Ngọc Duy10f35b12019-03-05 19:30:10 +07005166
5167 BUG_ON_OPT_NEG(unset);
John Cai11e95e12023-02-20 21:04:41 +00005168
5169 if (set_diff_algorithm(options, arg))
Nguyễn Thái Ngọc Duy10f35b12019-03-05 19:30:10 +07005170 return error(_("option diff-algorithm accepts \"myers\", "
5171 "\"minimal\", \"patience\" and \"histogram\""));
5172
John Caia4cf9002023-02-20 21:04:42 +00005173 options->ignore_driver_algorithm = 1;
5174
John Cai11e95e12023-02-20 21:04:41 +00005175 return 0;
5176}
5177
5178static int diff_opt_diff_algorithm_no_arg(const struct option *opt,
5179 const char *arg, int unset)
5180{
5181 struct diff_options *options = opt->value;
5182
5183 BUG_ON_OPT_NEG(unset);
5184 BUG_ON_OPT_ARG(arg);
5185
5186 if (set_diff_algorithm(options, opt->long_name))
5187 BUG("available diff algorithms include \"myers\", "
5188 "\"minimal\", \"patience\" and \"histogram\"");
5189
John Caia4cf9002023-02-20 21:04:42 +00005190 options->ignore_driver_algorithm = 1;
5191
Nguyễn Thái Ngọc Duy10f35b12019-03-05 19:30:10 +07005192 return 0;
5193}
5194
Nguyễn Thái Ngọc Duy4ce7aab2019-02-21 18:16:03 +07005195static int diff_opt_dirstat(const struct option *opt,
5196 const char *arg, int unset)
5197{
5198 struct diff_options *options = opt->value;
5199
5200 BUG_ON_OPT_NEG(unset);
5201 if (!strcmp(opt->long_name, "cumulative")) {
5202 if (arg)
5203 BUG("how come --cumulative take a value?");
5204 arg = "cumulative";
5205 } else if (!strcmp(opt->long_name, "dirstat-by-file"))
5206 parse_dirstat_opt(options, "files");
5207 parse_dirstat_opt(options, arg ? arg : "");
5208 return 0;
5209}
5210
Nguyễn Thái Ngọc Duy7f648502019-02-21 18:16:16 +07005211static int diff_opt_find_copies(const struct option *opt,
5212 const char *arg, int unset)
5213{
5214 struct diff_options *options = opt->value;
5215
5216 BUG_ON_OPT_NEG(unset);
5217 if (!arg)
5218 arg = "";
5219 options->rename_score = parse_rename_score(&arg);
5220 if (*arg != 0)
5221 return error(_("invalid argument to %s"), opt->long_name);
5222
5223 if (options->detect_rename == DIFF_DETECT_COPY)
5224 options->flags.find_copies_harder = 1;
5225 else
5226 options->detect_rename = DIFF_DETECT_COPY;
5227
5228 return 0;
5229}
5230
Nguyễn Thái Ngọc Duyf4763082019-02-21 18:16:14 +07005231static int diff_opt_find_renames(const struct option *opt,
5232 const char *arg, int unset)
5233{
5234 struct diff_options *options = opt->value;
5235
5236 BUG_ON_OPT_NEG(unset);
5237 if (!arg)
5238 arg = "";
5239 options->rename_score = parse_rename_score(&arg);
5240 if (*arg != 0)
5241 return error(_("invalid argument to %s"), opt->long_name);
5242
5243 options->detect_rename = DIFF_DETECT_RENAME;
5244 return 0;
5245}
5246
Nguyễn Thái Ngọc Duy1e9250b2019-03-05 19:30:16 +07005247static int diff_opt_follow(const struct option *opt,
5248 const char *arg, int unset)
5249{
5250 struct diff_options *options = opt->value;
5251
5252 BUG_ON_OPT_ARG(arg);
5253 if (unset) {
5254 options->flags.follow_renames = 0;
5255 options->flags.default_follow_renames = 0;
5256 } else {
5257 options->flags.follow_renames = 1;
5258 }
5259 return 0;
5260}
5261
Nguyễn Thái Ngọc Duyb680ee12019-03-05 19:30:25 +07005262static int diff_opt_ignore_submodules(const struct option *opt,
5263 const char *arg, int unset)
5264{
5265 struct diff_options *options = opt->value;
5266
5267 BUG_ON_OPT_NEG(unset);
5268 if (!arg)
5269 arg = "all";
5270 options->flags.override_submodule_config = 1;
5271 handle_ignore_submodules_arg(options, arg);
5272 return 0;
5273}
5274
Nguyễn Thái Ngọc Duy2f81cf92019-03-24 15:20:06 +07005275static int diff_opt_line_prefix(const struct option *opt,
5276 const char *optarg, int unset)
5277{
5278 struct diff_options *options = opt->value;
5279
5280 BUG_ON_OPT_NEG(unset);
5281 options->line_prefix = optarg;
5282 options->line_prefix_length = strlen(options->line_prefix);
5283 graph_setup_line_prefix(options);
5284 return 0;
5285}
5286
Nguyễn Thái Ngọc Duy11c659d2019-03-24 15:20:07 +07005287static int diff_opt_no_prefix(const struct option *opt,
5288 const char *optarg, int unset)
5289{
5290 struct diff_options *options = opt->value;
5291
5292 BUG_ON_OPT_NEG(unset);
5293 BUG_ON_OPT_ARG(optarg);
Jeff King6799aad2023-03-09 01:07:06 -05005294 diff_set_noprefix(options);
Nguyễn Thái Ngọc Duy11c659d2019-03-24 15:20:07 +07005295 return 0;
5296}
5297
Jeff Kingb39a5692023-03-09 01:09:54 -05005298static int diff_opt_default_prefix(const struct option *opt,
5299 const char *optarg, int unset)
5300{
5301 struct diff_options *options = opt->value;
5302
5303 BUG_ON_OPT_NEG(unset);
5304 BUG_ON_OPT_ARG(optarg);
5305 diff_set_default_prefix(options);
Ramkumar Ramachandrac47ef572012-11-13 21:12:45 +05305306 return 0;
5307}
5308
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005309static enum parse_opt_result diff_opt_output(struct parse_opt_ctx_t *ctx,
5310 const struct option *opt,
5311 const char *arg, int unset)
5312{
5313 struct diff_options *options = opt->value;
5314 char *path;
5315
5316 BUG_ON_OPT_NEG(unset);
5317 path = prefix_filename(ctx->prefix, arg);
5318 options->file = xfopen(path, "w");
5319 options->close_file = 1;
5320 if (options->use_color != GIT_COLOR_ALWAYS)
5321 options->use_color = GIT_COLOR_NEVER;
5322 free(path);
5323 return 0;
5324}
5325
Nguyễn Thái Ngọc Duy31fd6402019-03-05 19:30:08 +07005326static int diff_opt_patience(const struct option *opt,
5327 const char *arg, int unset)
5328{
5329 struct diff_options *options = opt->value;
5330 int i;
5331
5332 BUG_ON_OPT_NEG(unset);
5333 BUG_ON_OPT_ARG(arg);
Nguyễn Thái Ngọc Duy31fd6402019-03-05 19:30:08 +07005334 /*
5335 * Both --patience and --anchored use PATIENCE_DIFF
5336 * internally, so remove any anchors previously
5337 * specified.
5338 */
5339 for (i = 0; i < options->anchors_nr; i++)
5340 free(options->anchors[i]);
5341 options->anchors_nr = 0;
John Caia4cf9002023-02-20 21:04:42 +00005342 options->ignore_driver_algorithm = 1;
John Cai11e95e12023-02-20 21:04:41 +00005343
5344 return set_diff_algorithm(options, "patience");
Nguyễn Thái Ngọc Duy31fd6402019-03-05 19:30:08 +07005345}
5346
Michał Kępień296d4a92020-10-20 08:48:09 +02005347static int diff_opt_ignore_regex(const struct option *opt,
5348 const char *arg, int unset)
5349{
5350 struct diff_options *options = opt->value;
5351 regex_t *regex;
5352
5353 BUG_ON_OPT_NEG(unset);
5354 regex = xmalloc(sizeof(*regex));
5355 if (regcomp(regex, arg, REG_EXTENDED | REG_NEWLINE))
5356 return error(_("invalid regex given to -I: '%s'"), arg);
5357 ALLOC_GROW(options->ignore_regex, options->ignore_regex_nr + 1,
5358 options->ignore_regex_alloc);
5359 options->ignore_regex[options->ignore_regex_nr++] = regex;
5360 return 0;
5361}
5362
Nguyễn Thái Ngọc Duya41cfb32019-03-24 15:19:59 +07005363static int diff_opt_pickaxe_regex(const struct option *opt,
5364 const char *arg, int unset)
5365{
5366 struct diff_options *options = opt->value;
5367
5368 BUG_ON_OPT_NEG(unset);
5369 options->pickaxe = arg;
5370 options->pickaxe_opts |= DIFF_PICKAXE_KIND_G;
5371 return 0;
5372}
5373
5374static int diff_opt_pickaxe_string(const struct option *opt,
5375 const char *arg, int unset)
5376{
5377 struct diff_options *options = opt->value;
5378
5379 BUG_ON_OPT_NEG(unset);
5380 options->pickaxe = arg;
5381 options->pickaxe_opts |= DIFF_PICKAXE_KIND_S;
5382 return 0;
5383}
5384
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005385static int diff_opt_relative(const struct option *opt,
5386 const char *arg, int unset)
5387{
5388 struct diff_options *options = opt->value;
5389
Laurent Arnoudc28ded82020-05-22 12:46:18 +02005390 options->flags.relative_name = !unset;
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005391 if (arg)
5392 options->prefix = arg;
5393 return 0;
5394}
5395
Nguyễn Thái Ngọc Duy125dcea2019-03-05 19:30:26 +07005396static int diff_opt_submodule(const struct option *opt,
5397 const char *arg, int unset)
5398{
5399 struct diff_options *options = opt->value;
5400
5401 BUG_ON_OPT_NEG(unset);
5402 if (!arg)
5403 arg = "log";
5404 if (parse_submodule_params(options, arg))
5405 return error(_("failed to parse --submodule option parameter: '%s'"),
5406 arg);
5407 return 0;
5408}
5409
Nguyễn Thái Ngọc Duy8ab76972019-03-05 19:30:24 +07005410static int diff_opt_textconv(const struct option *opt,
5411 const char *arg, int unset)
5412{
5413 struct diff_options *options = opt->value;
5414
5415 BUG_ON_OPT_ARG(arg);
5416 if (unset) {
5417 options->flags.allow_textconv = 0;
5418 } else {
5419 options->flags.allow_textconv = 1;
5420 options->flags.textconv_set_via_cmdline = 1;
5421 }
5422 return 0;
5423}
5424
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 07:35:33 +07005425static int diff_opt_unified(const struct option *opt,
5426 const char *arg, int unset)
5427{
5428 struct diff_options *options = opt->value;
5429 char *s;
5430
5431 BUG_ON_OPT_NEG(unset);
5432
Nguyễn Thái Ngọc Duy8ef05192019-05-29 16:11:15 +07005433 if (arg) {
5434 options->context = strtol(arg, &s, 10);
5435 if (*s)
5436 return error(_("%s expects a numerical value"), "--unified");
5437 }
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 07:35:33 +07005438 enable_patch_output(&options->output_format);
5439
5440 return 0;
5441}
5442
Nguyễn Thái Ngọc Duye9fb39b2019-03-05 19:30:18 +07005443static int diff_opt_word_diff(const struct option *opt,
5444 const char *arg, int unset)
5445{
5446 struct diff_options *options = opt->value;
5447
5448 BUG_ON_OPT_NEG(unset);
5449 if (arg) {
5450 if (!strcmp(arg, "plain"))
5451 options->word_diff = DIFF_WORDS_PLAIN;
5452 else if (!strcmp(arg, "color")) {
5453 options->use_color = 1;
5454 options->word_diff = DIFF_WORDS_COLOR;
5455 }
5456 else if (!strcmp(arg, "porcelain"))
5457 options->word_diff = DIFF_WORDS_PORCELAIN;
5458 else if (!strcmp(arg, "none"))
5459 options->word_diff = DIFF_WORDS_NONE;
5460 else
5461 return error(_("bad --word-diff argument: %s"), arg);
5462 } else {
5463 if (options->word_diff == DIFF_WORDS_NONE)
5464 options->word_diff = DIFF_WORDS_PLAIN;
5465 }
5466 return 0;
5467}
5468
Nguyễn Thái Ngọc Duy797df112019-03-05 19:30:19 +07005469static int diff_opt_word_diff_regex(const struct option *opt,
5470 const char *arg, int unset)
5471{
5472 struct diff_options *options = opt->value;
5473
5474 BUG_ON_OPT_NEG(unset);
5475 if (options->word_diff == DIFF_WORDS_NONE)
5476 options->word_diff = DIFF_WORDS_PLAIN;
5477 options->word_regex = arg;
5478 return 0;
5479}
5480
Junio C Hamano1eb41362021-02-11 11:57:50 -08005481static int diff_opt_rotate_to(const struct option *opt, const char *arg, int unset)
5482{
5483 struct diff_options *options = opt->value;
5484
5485 BUG_ON_OPT_NEG(unset);
5486 if (!strcmp(opt->long_name, "skip-to"))
5487 options->skip_instead_of_rotate = 1;
5488 else
5489 options->skip_instead_of_rotate = 0;
5490 options->rotate_to = arg;
5491 return 0;
5492}
5493
René Scharfec5630c42022-12-01 23:49:11 +01005494struct option *add_diff_options(const struct option *opts,
5495 struct diff_options *options)
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005496{
5497 struct option parseopts[] = {
Nguyễn Thái Ngọc Duycc013c22019-01-27 07:35:32 +07005498 OPT_GROUP(N_("Diff output format options")),
5499 OPT_BITOP('p', "patch", &options->output_format,
5500 N_("generate patch"),
5501 DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duye01df7a2019-02-21 18:16:09 +07005502 OPT_BIT_F('s', "no-patch", &options->output_format,
5503 N_("suppress diff output"),
5504 DIFF_FORMAT_NO_OUTPUT, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duycc013c22019-01-27 07:35:32 +07005505 OPT_BITOP('u', NULL, &options->output_format,
5506 N_("generate patch"),
5507 DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 07:35:33 +07005508 OPT_CALLBACK_F('U', "unified", options, N_("<n>"),
5509 N_("generate diffs with <n> lines context"),
Junio C Hamano20aa7c52019-05-30 10:50:44 -07005510 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_unified),
Nguyễn Thái Ngọc Duy7fd9a1b2019-01-27 07:35:34 +07005511 OPT_BOOL('W', "function-context", &options->flags.funccontext,
5512 N_("generate diffs with <n> lines context")),
Nguyễn Thái Ngọc Duyed881482019-01-27 07:35:35 +07005513 OPT_BIT_F(0, "raw", &options->output_format,
5514 N_("generate the diff in raw format"),
5515 DIFF_FORMAT_RAW, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyc659f302019-02-16 18:36:35 +07005516 OPT_BITOP(0, "patch-with-raw", &options->output_format,
5517 N_("synonym for '-p --raw'"),
5518 DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW,
5519 DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duye550f582019-02-21 18:16:06 +07005520 OPT_BITOP(0, "patch-with-stat", &options->output_format,
5521 N_("synonym for '-p --stat'"),
5522 DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT,
5523 DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duye5673622019-02-16 18:36:36 +07005524 OPT_BIT_F(0, "numstat", &options->output_format,
5525 N_("machine friendly --stat"),
5526 DIFF_FORMAT_NUMSTAT, PARSE_OPT_NONEG),
5527 OPT_BIT_F(0, "shortstat", &options->output_format,
5528 N_("output only the last line of --stat"),
5529 DIFF_FORMAT_SHORTSTAT, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy4ce7aab2019-02-21 18:16:03 +07005530 OPT_CALLBACK_F('X', "dirstat", options, N_("<param1,param2>..."),
5531 N_("output the distribution of relative amount of changes for each sub-directory"),
5532 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5533 diff_opt_dirstat),
5534 OPT_CALLBACK_F(0, "cumulative", options, NULL,
5535 N_("synonym for --dirstat=cumulative"),
5536 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
5537 diff_opt_dirstat),
5538 OPT_CALLBACK_F(0, "dirstat-by-file", options, N_("<param1,param2>..."),
5539 N_("synonym for --dirstat=files,param1,param2..."),
5540 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5541 diff_opt_dirstat),
Nguyễn Thái Ngọc Duyfc6af3e2019-02-21 18:16:04 +07005542 OPT_BIT_F(0, "check", &options->output_format,
5543 N_("warn if changes introduce conflict markers or whitespace errors"),
5544 DIFF_FORMAT_CHECKDIFF, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy70a30412019-02-21 18:16:05 +07005545 OPT_BIT_F(0, "summary", &options->output_format,
5546 N_("condensed summary such as creations, renames and mode changes"),
5547 DIFF_FORMAT_SUMMARY, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy0e840e22019-02-21 18:16:07 +07005548 OPT_BIT_F(0, "name-only", &options->output_format,
5549 N_("show only names of changed files"),
5550 DIFF_FORMAT_NAME, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duya2387472019-02-21 18:16:08 +07005551 OPT_BIT_F(0, "name-status", &options->output_format,
5552 N_("show only names and status of changed files"),
5553 DIFF_FORMAT_NAME_STATUS, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy84b50892019-02-21 18:16:10 +07005554 OPT_CALLBACK_F(0, "stat", options, N_("<width>[,<name-width>[,<count>]]"),
5555 N_("generate diffstat"),
5556 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_stat),
5557 OPT_CALLBACK_F(0, "stat-width", options, N_("<width>"),
5558 N_("generate diffstat with a given width"),
5559 PARSE_OPT_NONEG, diff_opt_stat),
5560 OPT_CALLBACK_F(0, "stat-name-width", options, N_("<width>"),
5561 N_("generate diffstat with a given name width"),
5562 PARSE_OPT_NONEG, diff_opt_stat),
5563 OPT_CALLBACK_F(0, "stat-graph-width", options, N_("<width>"),
5564 N_("generate diffstat with a given graph width"),
5565 PARSE_OPT_NONEG, diff_opt_stat),
5566 OPT_CALLBACK_F(0, "stat-count", options, N_("<count>"),
5567 N_("generate diffstat with limited lines"),
5568 PARSE_OPT_NONEG, diff_opt_stat),
Nguyễn Thái Ngọc Duy7d7942b2019-02-21 18:16:11 +07005569 OPT_CALLBACK_F(0, "compact-summary", options, NULL,
5570 N_("generate compact summary in diffstat"),
5571 PARSE_OPT_NOARG, diff_opt_compact_summary),
Nguyễn Thái Ngọc Duy6d9af6f2019-03-05 19:30:12 +07005572 OPT_CALLBACK_F(0, "binary", options, NULL,
5573 N_("output a binary diff that can be applied"),
5574 PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_binary),
Nguyễn Thái Ngọc Duy4fe01672019-03-05 19:30:13 +07005575 OPT_BOOL(0, "full-index", &options->flags.full_index,
5576 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 +07005577 OPT_COLOR_FLAG(0, "color", &options->use_color,
5578 N_("show colored diff")),
Nguyễn Thái Ngọc Duy5a749d92019-03-24 15:19:55 +07005579 OPT_CALLBACK_F(0, "ws-error-highlight", options, N_("<kind>"),
5580 N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
5581 PARSE_OPT_NONEG, diff_opt_ws_error_highlight),
Nguyễn Thái Ngọc Duy1a1eb162019-03-24 15:19:57 +07005582 OPT_SET_INT('z', NULL, &options->line_termination,
5583 N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
5584 0),
Nguyễn Thái Ngọc Duyd8774182019-03-24 15:20:04 +07005585 OPT__ABBREV(&options->abbrev),
Nguyễn Thái Ngọc Duy31fba9d2019-03-24 15:20:05 +07005586 OPT_STRING_F(0, "src-prefix", &options->a_prefix, N_("<prefix>"),
5587 N_("show the given source prefix instead of \"a/\""),
5588 PARSE_OPT_NONEG),
5589 OPT_STRING_F(0, "dst-prefix", &options->b_prefix, N_("<prefix>"),
Jiang Xin8a1569d2019-06-02 23:11:22 +08005590 N_("show the given destination prefix instead of \"b/\""),
Nguyễn Thái Ngọc Duy31fba9d2019-03-24 15:20:05 +07005591 PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy2f81cf92019-03-24 15:20:06 +07005592 OPT_CALLBACK_F(0, "line-prefix", options, N_("<prefix>"),
5593 N_("prepend an additional prefix to every line of output"),
5594 PARSE_OPT_NONEG, diff_opt_line_prefix),
Nguyễn Thái Ngọc Duy11c659d2019-03-24 15:20:07 +07005595 OPT_CALLBACK_F(0, "no-prefix", options, NULL,
5596 N_("do not show any source or destination prefix"),
5597 PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_no_prefix),
Jeff Kingb39a5692023-03-09 01:09:54 -05005598 OPT_CALLBACK_F(0, "default-prefix", options, NULL,
5599 N_("use default prefixes a/ and b/"),
5600 PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_default_prefix),
Nguyễn Thái Ngọc Duy16ed6c92019-03-24 15:20:08 +07005601 OPT_INTEGER_F(0, "inter-hunk-context", &options->interhunkcontext,
5602 N_("show context between diff hunks up to the specified number of lines"),
5603 PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005604 OPT_CALLBACK_F(0, "output-indicator-new",
5605 &options->output_indicators[OUTPUT_INDICATOR_NEW],
5606 N_("<char>"),
5607 N_("specify the character to indicate a new line instead of '+'"),
5608 PARSE_OPT_NONEG, diff_opt_char),
5609 OPT_CALLBACK_F(0, "output-indicator-old",
5610 &options->output_indicators[OUTPUT_INDICATOR_OLD],
5611 N_("<char>"),
5612 N_("specify the character to indicate an old line instead of '-'"),
5613 PARSE_OPT_NONEG, diff_opt_char),
5614 OPT_CALLBACK_F(0, "output-indicator-context",
5615 &options->output_indicators[OUTPUT_INDICATOR_CONTEXT],
5616 N_("<char>"),
5617 N_("specify the character to indicate a context instead of ' '"),
5618 PARSE_OPT_NONEG, diff_opt_char),
5619
Nguyễn Thái Ngọc Duyced4e172019-02-21 18:16:13 +07005620 OPT_GROUP(N_("Diff rename options")),
5621 OPT_CALLBACK_F('B', "break-rewrites", &options->break_opt, N_("<n>[/<m>]"),
5622 N_("break complete rewrite changes into pairs of delete and create"),
5623 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5624 diff_opt_break_rewrites),
Nguyễn Thái Ngọc Duyf4763082019-02-21 18:16:14 +07005625 OPT_CALLBACK_F('M', "find-renames", options, N_("<n>"),
5626 N_("detect renames"),
5627 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5628 diff_opt_find_renames),
Nguyễn Thái Ngọc Duy1e533292019-02-21 18:16:15 +07005629 OPT_SET_INT_F('D', "irreversible-delete", &options->irreversible_delete,
5630 N_("omit the preimage for deletes"),
5631 1, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy7f648502019-02-21 18:16:16 +07005632 OPT_CALLBACK_F('C', "find-copies", options, N_("<n>"),
5633 N_("detect copies"),
5634 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5635 diff_opt_find_copies),
Nguyễn Thái Ngọc Duybdd47412019-02-21 18:16:17 +07005636 OPT_BOOL(0, "find-copies-harder", &options->flags.find_copies_harder,
5637 N_("use unmodified files as source to find copies")),
Nguyễn Thái Ngọc Duycdc43eb2019-02-21 18:16:18 +07005638 OPT_SET_INT_F(0, "no-renames", &options->detect_rename,
5639 N_("disable rename detection"),
5640 0, PARSE_OPT_NONEG),
5641 OPT_BOOL(0, "rename-empty", &options->flags.rename_empty,
5642 N_("use empty blobs as rename source")),
Nguyễn Thái Ngọc Duy1e9250b2019-03-05 19:30:16 +07005643 OPT_CALLBACK_F(0, "follow", options, NULL,
5644 N_("continue listing the history of a file beyond renames"),
5645 PARSE_OPT_NOARG, diff_opt_follow),
Nguyễn Thái Ngọc Duybffee742019-03-24 15:19:58 +07005646 OPT_INTEGER('l', NULL, &options->rename_limit,
5647 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 +07005648
Nguyễn Thái Ngọc Duy2e75f922019-02-21 18:16:20 +07005649 OPT_GROUP(N_("Diff algorithm options")),
John Cai11e95e12023-02-20 21:04:41 +00005650 OPT_CALLBACK_F(0, "minimal", options, NULL,
5651 N_("produce the smallest possible diff"),
5652 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
5653 diff_opt_diff_algorithm_no_arg),
Nguyễn Thái Ngọc Duy87649a12019-02-21 18:16:21 +07005654 OPT_BIT_F('w', "ignore-all-space", &options->xdl_opts,
5655 N_("ignore whitespace when comparing lines"),
5656 XDF_IGNORE_WHITESPACE, PARSE_OPT_NONEG),
5657 OPT_BIT_F('b', "ignore-space-change", &options->xdl_opts,
5658 N_("ignore changes in amount of whitespace"),
5659 XDF_IGNORE_WHITESPACE_CHANGE, PARSE_OPT_NONEG),
5660 OPT_BIT_F(0, "ignore-space-at-eol", &options->xdl_opts,
5661 N_("ignore changes in whitespace at EOL"),
5662 XDF_IGNORE_WHITESPACE_AT_EOL, PARSE_OPT_NONEG),
5663 OPT_BIT_F(0, "ignore-cr-at-eol", &options->xdl_opts,
5664 N_("ignore carrier-return at the end of line"),
5665 XDF_IGNORE_CR_AT_EOL, PARSE_OPT_NONEG),
5666 OPT_BIT_F(0, "ignore-blank-lines", &options->xdl_opts,
5667 N_("ignore changes whose lines are all blank"),
5668 XDF_IGNORE_BLANK_LINES, PARSE_OPT_NONEG),
Michał Kępień296d4a92020-10-20 08:48:09 +02005669 OPT_CALLBACK_F('I', "ignore-matching-lines", options, N_("<regex>"),
5670 N_("ignore changes whose all lines match <regex>"),
5671 0, diff_opt_ignore_regex),
Nguyễn Thái Ngọc Duy06f77512019-03-05 19:30:07 +07005672 OPT_BIT(0, "indent-heuristic", &options->xdl_opts,
5673 N_("heuristic to shift diff hunk boundaries for easy reading"),
5674 XDF_INDENT_HEURISTIC),
Nguyễn Thái Ngọc Duy31fd6402019-03-05 19:30:08 +07005675 OPT_CALLBACK_F(0, "patience", options, NULL,
5676 N_("generate diff using the \"patience diff\" algorithm"),
5677 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
5678 diff_opt_patience),
John Cai11e95e12023-02-20 21:04:41 +00005679 OPT_CALLBACK_F(0, "histogram", options, NULL,
5680 N_("generate diff using the \"histogram diff\" algorithm"),
5681 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
5682 diff_opt_diff_algorithm_no_arg),
Nguyễn Thái Ngọc Duy10f35b12019-03-05 19:30:10 +07005683 OPT_CALLBACK_F(0, "diff-algorithm", options, N_("<algorithm>"),
5684 N_("choose a diff algorithm"),
5685 PARSE_OPT_NONEG, diff_opt_diff_algorithm),
Nguyễn Thái Ngọc Duydf84a432019-03-05 19:30:11 +07005686 OPT_CALLBACK_F(0, "anchored", options, N_("<text>"),
5687 N_("generate diff using the \"anchored diff\" algorithm"),
5688 PARSE_OPT_NONEG, diff_opt_anchored),
Nguyễn Thái Ngọc Duye9fb39b2019-03-05 19:30:18 +07005689 OPT_CALLBACK_F(0, "word-diff", options, N_("<mode>"),
5690 N_("show word diff, using <mode> to delimit changed words"),
5691 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_word_diff),
Nguyễn Thái Ngọc Duy797df112019-03-05 19:30:19 +07005692 OPT_CALLBACK_F(0, "word-diff-regex", options, N_("<regex>"),
5693 N_("use <regex> to decide what a word is"),
5694 PARSE_OPT_NONEG, diff_opt_word_diff_regex),
Nguyễn Thái Ngọc Duy212db692019-03-05 19:30:20 +07005695 OPT_CALLBACK_F(0, "color-words", options, N_("<regex>"),
5696 N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
5697 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_color_words),
Nguyễn Thái Ngọc Duy59311a92019-03-24 15:20:09 +07005698 OPT_CALLBACK_F(0, "color-moved", options, N_("<mode>"),
Jean-Noël Avila8bcd8f42019-05-17 21:26:19 +02005699 N_("moved lines of code are colored differently"),
Nguyễn Thái Ngọc Duy59311a92019-03-24 15:20:09 +07005700 PARSE_OPT_OPTARG, diff_opt_color_moved),
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005701 OPT_CALLBACK_F(0, "color-moved-ws", options, N_("<mode>"),
5702 N_("how white spaces are ignored in --color-moved"),
Nguyễn Thái Ngọc Duybb987292019-03-24 15:20:11 +07005703 0, diff_opt_color_moved_ws),
Nguyễn Thái Ngọc Duy2e75f922019-02-21 18:16:20 +07005704
Jean-Noël Avila8bcd8f42019-05-17 21:26:19 +02005705 OPT_GROUP(N_("Other diff options")),
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005706 OPT_CALLBACK_F(0, "relative", options, N_("<prefix>"),
5707 N_("when run from subdir, exclude changes outside and show relative paths"),
Laurent Arnoudc28ded82020-05-22 12:46:18 +02005708 PARSE_OPT_OPTARG,
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 18:16:19 +07005709 diff_opt_relative),
Nguyễn Thái Ngọc Duyc37d4c02019-03-05 19:30:14 +07005710 OPT_BOOL('a', "text", &options->flags.text,
5711 N_("treat all files as text")),
Nguyễn Thái Ngọc Duy4f99f292019-03-05 19:30:15 +07005712 OPT_BOOL('R', NULL, &options->flags.reverse_diff,
5713 N_("swap two inputs, reverse the diff")),
Nguyễn Thái Ngọc Duy1086ea02019-03-05 19:30:21 +07005714 OPT_BOOL(0, "exit-code", &options->flags.exit_with_status,
5715 N_("exit with 1 if there were differences, 0 otherwise")),
Nguyễn Thái Ngọc Duy9bbaf1c2019-03-05 19:30:22 +07005716 OPT_BOOL(0, "quiet", &options->flags.quick,
5717 N_("disable all output of the program")),
Nguyễn Thái Ngọc Duy0cda1002019-03-05 19:30:23 +07005718 OPT_BOOL(0, "ext-diff", &options->flags.allow_external,
5719 N_("allow an external diff helper to be executed")),
Nguyễn Thái Ngọc Duy8ab76972019-03-05 19:30:24 +07005720 OPT_CALLBACK_F(0, "textconv", options, NULL,
5721 N_("run external text conversion filters when comparing binary files"),
5722 PARSE_OPT_NOARG, diff_opt_textconv),
Nguyễn Thái Ngọc Duyb680ee12019-03-05 19:30:25 +07005723 OPT_CALLBACK_F(0, "ignore-submodules", options, N_("<when>"),
5724 N_("ignore changes to submodules in the diff generation"),
5725 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5726 diff_opt_ignore_submodules),
Nguyễn Thái Ngọc Duy125dcea2019-03-05 19:30:26 +07005727 OPT_CALLBACK_F(0, "submodule", options, N_("<format>"),
5728 N_("specify how differences in submodules are shown"),
5729 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5730 diff_opt_submodule),
Nguyễn Thái Ngọc Duy96860e42019-03-24 15:19:56 +07005731 OPT_SET_INT_F(0, "ita-invisible-in-index", &options->ita_invisible_in_index,
5732 N_("hide 'git add -N' entries from the index"),
5733 1, PARSE_OPT_NONEG),
5734 OPT_SET_INT_F(0, "ita-visible-in-index", &options->ita_invisible_in_index,
5735 N_("treat 'git add -N' entries as real in the index"),
5736 0, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duya41cfb32019-03-24 15:19:59 +07005737 OPT_CALLBACK_F('S', NULL, options, N_("<string>"),
5738 N_("look for differences that change the number of occurrences of the specified string"),
5739 0, diff_opt_pickaxe_string),
5740 OPT_CALLBACK_F('G', NULL, options, N_("<regex>"),
5741 N_("look for differences that change the number of occurrences of the specified regex"),
5742 0, diff_opt_pickaxe_regex),
Nguyễn Thái Ngọc Duy85f8e882019-03-24 15:20:00 +07005743 OPT_BIT_F(0, "pickaxe-all", &options->pickaxe_opts,
5744 N_("show all changes in the changeset with -S or -G"),
5745 DIFF_PICKAXE_ALL, PARSE_OPT_NONEG),
5746 OPT_BIT_F(0, "pickaxe-regex", &options->pickaxe_opts,
5747 N_("treat <string> in -S as extended POSIX regular expression"),
5748 DIFF_PICKAXE_REGEX, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyf7318142019-03-24 15:20:01 +07005749 OPT_FILENAME('O', NULL, &options->orderfile,
5750 N_("control the order in which files appear in the output")),
Junio C Hamano1eb41362021-02-11 11:57:50 -08005751 OPT_CALLBACK_F(0, "rotate-to", options, N_("<path>"),
5752 N_("show the change in the specified path first"),
5753 PARSE_OPT_NONEG, diff_opt_rotate_to),
5754 OPT_CALLBACK_F(0, "skip-to", options, N_("<path>"),
5755 N_("skip the output to the specified path"),
5756 PARSE_OPT_NONEG, diff_opt_rotate_to),
Nguyễn Thái Ngọc Duya75f28c2019-03-24 15:20:02 +07005757 OPT_CALLBACK_F(0, "find-object", options, N_("<object-id>"),
5758 N_("look for differences that change the number of occurrences of the specified object"),
5759 PARSE_OPT_NONEG, diff_opt_find_object),
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 15:20:03 +07005760 OPT_CALLBACK_F(0, "diff-filter", options, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
5761 N_("select files by diff type"),
5762 PARSE_OPT_NONEG, diff_opt_diff_filter),
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005763 { OPTION_CALLBACK, 0, "output", options, N_("<file>"),
Abhradeep Chakraborty9e1f22c2022-02-23 14:27:34 +00005764 N_("output to a specific file"),
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 18:16:12 +07005765 PARSE_OPT_NONEG, NULL, 0, diff_opt_output },
5766
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005767 OPT_END()
5768 };
5769
René Scharfe189e97b2022-12-01 23:53:17 +01005770 return parse_options_concat(opts, parseopts);
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005771}
5772
Duy Nguyena97262c2016-01-21 18:48:44 +07005773int diff_opt_parse(struct diff_options *options,
5774 const char **av, int ac, const char *prefix)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005775{
René Scharfe6c6048f2022-12-01 23:51:21 +01005776 struct option no_options[] = { OPT_END() };
5777 struct option *parseopts = add_diff_options(no_options, options);
5778
Duy Nguyena97262c2016-01-21 18:48:44 +07005779 if (!prefix)
5780 prefix = "";
5781
René Scharfe6c6048f2022-12-01 23:51:21 +01005782 ac = parse_options(ac, av, prefix, parseopts, NULL,
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005783 PARSE_OPT_KEEP_DASHDASH |
SZEDER Gábor99d86d62022-08-19 18:03:57 +02005784 PARSE_OPT_KEEP_UNKNOWN_OPT |
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005785 PARSE_OPT_NO_INTERNAL_HELP |
5786 PARSE_OPT_ONE_SHOT |
5787 PARSE_OPT_STOP_AT_NON_OPTION);
René Scharfe6c6048f2022-12-01 23:51:21 +01005788 free(parseopts);
Nguyễn Thái Ngọc Duy4a288472019-01-27 07:35:31 +07005789
Nguyễn Thái Ngọc Duy8ce20202019-03-24 15:20:10 +07005790 return ac;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005791}
5792
Kevin Ballard10ae7522010-09-27 16:58:25 -07005793int parse_rename_score(const char **cp_p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005794{
5795 unsigned long num, scale;
5796 int ch, dot;
5797 const char *cp = *cp_p;
5798
5799 num = 0;
5800 scale = 1;
5801 dot = 0;
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -04005802 for (;;) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07005803 ch = *cp;
5804 if ( !dot && ch == '.' ) {
5805 scale = 1;
5806 dot = 1;
5807 } else if ( ch == '%' ) {
5808 scale = dot ? scale*100 : 100;
5809 cp++; /* % is always at the end */
5810 break;
5811 } else if ( ch >= '0' && ch <= '9' ) {
5812 if ( scale < 100000 ) {
5813 scale *= 10;
5814 num = (num*10) + (ch-'0');
5815 }
5816 } else {
5817 break;
5818 }
5819 cp++;
5820 }
5821 *cp_p = cp;
5822
5823 /* user says num divided by scale and we say internally that
5824 * is MAX_SCORE * num / scale.
5825 */
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -05005826 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
Junio C Hamano6973dca2006-04-21 23:57:45 -07005827}
5828
Junio C Hamano6973dca2006-04-21 23:57:45 -07005829struct diff_queue_struct diff_queued_diff;
5830
5831void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
5832{
Dmitry S. Dolzhenko4c960a42014-03-04 02:31:53 +04005833 ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005834 queue->queue[queue->nr++] = dp;
5835}
5836
5837struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
5838 struct diff_filespec *one,
5839 struct diff_filespec *two)
5840{
Junio C Hamanoef677682006-08-03 12:01:01 -07005841 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
Junio C Hamano6973dca2006-04-21 23:57:45 -07005842 dp->one = one;
5843 dp->two = two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005844 if (queue)
5845 diff_q(queue, dp);
5846 return dp;
5847}
5848
5849void diff_free_filepair(struct diff_filepair *p)
5850{
Linus Torvalds9fb88412007-10-25 11:19:10 -07005851 free_filespec(p->one);
5852 free_filespec(p->two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005853 free(p);
5854}
5855
SZEDER Gábor04ae0002022-11-02 23:01:40 +01005856void diff_free_queue(struct diff_queue_struct *q)
5857{
5858 for (int i = 0; i < q->nr; i++)
5859 diff_free_filepair(q->queue[i]);
5860 free(q->queue);
5861}
5862
Jeff Kingd6cece52016-10-20 02:20:07 -04005863const char *diff_aligned_abbrev(const struct object_id *oid, int len)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005864{
5865 int abblen;
5866 const char *abbrev;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005867
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005868 /* Do we want all 40 hex characters? */
brian m. carlson02afca12018-07-16 01:28:05 +00005869 if (len == the_hash_algo->hexsz)
Jeff Kingd6cece52016-10-20 02:20:07 -04005870 return oid_to_hex(oid);
5871
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005872 /* An abbreviated value is fine, possibly followed by an ellipsis. */
Jeff King4f036662016-10-20 02:21:25 -04005873 abbrev = diff_abbrev_oid(oid, len);
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005874
5875 if (!print_sha1_ellipsis())
5876 return abbrev;
5877
Junio C Hamano6973dca2006-04-21 23:57:45 -07005878 abblen = strlen(abbrev);
Junio C Hamanod709f1f2016-09-30 10:42:05 -07005879
5880 /*
Ann T Ropea7cb6ac12017-12-03 22:27:42 +01005881 * In well-behaved cases, where the abbreviated result is the
Junio C Hamanod709f1f2016-09-30 10:42:05 -07005882 * same as the requested length, append three dots after the
5883 * abbreviation (hence the whole logic is limited to the case
5884 * where abblen < 37); when the actual abbreviated result is a
5885 * bit longer than the requested length, we reduce the number
5886 * of dots so that they match the well-behaved ones. However,
5887 * if the actual abbreviation is longer than the requested
5888 * length by more than three, we give up on aligning, and add
5889 * three dots anyway, to indicate that the output is not the
5890 * full object name. Yes, this may be suboptimal, but this
5891 * appears only in "diff --raw --abbrev" output and it is not
5892 * worth the effort to change it now. Note that this would
5893 * likely to work fine when the automatic sizing of default
5894 * abbreviation length is used--we would be fed -1 in "len" in
5895 * that case, and will end up always appending three-dots, but
5896 * the automatic sizing is supposed to give abblen that ensures
5897 * uniqueness across all objects (statistically speaking).
5898 */
brian m. carlson02afca12018-07-16 01:28:05 +00005899 if (abblen < the_hash_algo->hexsz - 3) {
brian m. carlsondc015052017-03-26 16:01:24 +00005900 static char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano6973dca2006-04-21 23:57:45 -07005901 if (len < abblen && abblen <= len + 2)
Jeff King5096d492015-09-24 17:06:08 -04005902 xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");
Junio C Hamano6973dca2006-04-21 23:57:45 -07005903 else
Jeff King5096d492015-09-24 17:06:08 -04005904 xsnprintf(hex, sizeof(hex), "%s...", abbrev);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005905 return hex;
5906 }
Jeff Kingd6cece52016-10-20 02:20:07 -04005907
5908 return oid_to_hex(oid);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005909}
5910
Pierre Habouzit663af342007-09-20 00:42:15 +02005911static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005912{
Pierre Habouzit663af342007-09-20 00:42:15 +02005913 int line_termination = opt->line_termination;
5914 int inter_name_termination = line_termination ? '\t' : '\0';
Timo Hirvonenc6744342006-06-24 20:21:53 +03005915
John Keeping30997bb2013-02-07 20:15:27 +00005916 fprintf(opt->file, "%s", diff_line_prefix(opt));
Pierre Habouzit663af342007-09-20 00:42:15 +02005917 if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005918 fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
Jeff Kingd6cece52016-10-20 02:20:07 -04005919 diff_aligned_abbrev(&p->one->oid, opt->abbrev));
brian m. carlsona0d12c42016-06-24 23:09:23 +00005920 fprintf(opt->file, "%s ",
Jeff Kingd6cece52016-10-20 02:20:07 -04005921 diff_aligned_abbrev(&p->two->oid, opt->abbrev));
Pierre Habouzit663af342007-09-20 00:42:15 +02005922 }
5923 if (p->score) {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005924 fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),
5925 inter_name_termination);
Pierre Habouzit663af342007-09-20 00:42:15 +02005926 } else {
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005927 fprintf(opt->file, "%c%c", p->status, inter_name_termination);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005928 }
5929
Junio C Hamanocd676a52008-02-12 14:26:02 -08005930 if (p->status == DIFF_STATUS_COPIED ||
5931 p->status == DIFF_STATUS_RENAMED) {
5932 const char *name_a, *name_b;
5933 name_a = p->one->path;
5934 name_b = p->two->path;
5935 strip_prefix(opt->prefix_length, &name_a, &name_b);
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005936 write_name_quoted(name_a, opt->file, inter_name_termination);
5937 write_name_quoted(name_b, opt->file, line_termination);
Pierre Habouzit663af342007-09-20 00:42:15 +02005938 } else {
Junio C Hamanocd676a52008-02-12 14:26:02 -08005939 const char *name_a, *name_b;
5940 name_a = p->one->mode ? p->one->path : p->two->path;
5941 name_b = NULL;
5942 strip_prefix(opt->prefix_length, &name_a, &name_b);
Daniel Barkalowc0c77732008-03-09 22:43:39 -04005943 write_name_quoted(name_a, opt->file, line_termination);
Junio C Hamano6973dca2006-04-21 23:57:45 -07005944 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07005945}
5946
5947int diff_unmodified_pair(struct diff_filepair *p)
5948{
5949 /* This function is written stricter than necessary to support
5950 * the currently implemented transformers, but the idea is to
5951 * let transformers to produce diff_filepairs any way they want,
5952 * and filter and clean them up here before producing the output.
5953 */
Pierre Habouzit663af342007-09-20 00:42:15 +02005954 struct diff_filespec *one = p->one, *two = p->two;
Junio C Hamano6973dca2006-04-21 23:57:45 -07005955
5956 if (DIFF_PAIR_UNMERGED(p))
5957 return 0; /* unmerged is interesting */
5958
Junio C Hamano6973dca2006-04-21 23:57:45 -07005959 /* deletion, addition, mode or type change
5960 * and rename are all interesting.
5961 */
5962 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
5963 DIFF_PAIR_MODE_CHANGED(p) ||
5964 strcmp(one->path, two->path))
5965 return 0;
5966
5967 /* both are valid and point at the same path. that is, we are
5968 * dealing with a change.
5969 */
brian m. carlson41c95602016-06-24 23:09:24 +00005970 if (one->oid_valid && two->oid_valid &&
Jeff King4a7e27e2018-08-28 17:22:40 -04005971 oideq(&one->oid, &two->oid) &&
Jens Lehmann85adbf22010-03-12 22:23:52 +01005972 !one->dirty_submodule && !two->dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005973 return 1; /* no change */
brian m. carlson41c95602016-06-24 23:09:24 +00005974 if (!one->oid_valid && !two->oid_valid)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005975 return 1; /* both look at the same file on the filesystem. */
5976 return 0;
5977}
5978
5979static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
5980{
Elijah Newren95433ee2022-02-02 02:37:34 +00005981 int include_conflict_headers =
5982 (additional_headers(o, p->one->path) &&
Elijah Newren71a146d2022-09-02 03:53:29 +00005983 !o->pickaxe_opts &&
Elijah Newren95433ee2022-02-02 02:37:34 +00005984 (!o->filter || filter_bit_tst(DIFF_STATUS_UNMERGED, o)));
5985
5986 /*
5987 * Check if we can return early without showing a diff. Note that
5988 * diff_filepair only stores {oid, path, mode, is_valid}
5989 * information for each path, and thus diff_unmodified_pair() only
5990 * considers those bits of info. However, we do not want pairs
5991 * created by create_filepairs_for_header_only_notifications()
5992 * (which always look like unmodified pairs) to be ignored, so
5993 * return early if both p is unmodified AND we don't want to
5994 * include_conflict_headers.
5995 */
5996 if (diff_unmodified_pair(p) && !include_conflict_headers)
Junio C Hamano6973dca2006-04-21 23:57:45 -07005997 return;
5998
Elijah Newren95433ee2022-02-02 02:37:34 +00005999 /* Actually, we can also return early to avoid showing tree diffs */
Junio C Hamano6973dca2006-04-21 23:57:45 -07006000 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
6001 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Elijah Newren95433ee2022-02-02 02:37:34 +00006002 return;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006003
6004 run_diff(p, o);
6005}
6006
6007static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
6008 struct diffstat_t *diffstat)
6009{
6010 if (diff_unmodified_pair(p))
6011 return;
6012
6013 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
6014 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Yann Dirsonc3fced62010-10-24 23:03:10 +02006015 return; /* no useful stat for tree diffs */
Junio C Hamano6973dca2006-04-21 23:57:45 -07006016
6017 run_diffstat(p, o, diffstat);
6018}
6019
Johannes Schindelin88246892006-05-20 23:43:13 +02006020static void diff_flush_checkdiff(struct diff_filepair *p,
6021 struct diff_options *o)
6022{
6023 if (diff_unmodified_pair(p))
6024 return;
6025
6026 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
6027 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Yann Dirsonc3fced62010-10-24 23:03:10 +02006028 return; /* nothing to check in tree diffs */
Johannes Schindelin88246892006-05-20 23:43:13 +02006029
6030 run_checkdiff(p, o);
6031}
6032
Elijah Newren95433ee2022-02-02 02:37:34 +00006033int diff_queue_is_empty(struct diff_options *o)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006034{
6035 struct diff_queue_struct *q = &diff_queued_diff;
6036 int i;
Elijah Newren95433ee2022-02-02 02:37:34 +00006037 int include_conflict_headers =
6038 (o->additional_path_headers &&
Elijah Newren67360b72022-09-02 03:53:30 +00006039 strmap_get_size(o->additional_path_headers) &&
Elijah Newren71a146d2022-09-02 03:53:29 +00006040 !o->pickaxe_opts &&
Elijah Newren95433ee2022-02-02 02:37:34 +00006041 (!o->filter || filter_bit_tst(DIFF_STATUS_UNMERGED, o)));
6042
6043 if (include_conflict_headers)
6044 return 0;
6045
Junio C Hamano6973dca2006-04-21 23:57:45 -07006046 for (i = 0; i < q->nr; i++)
6047 if (!diff_unmodified_pair(q->queue[i]))
6048 return 0;
6049 return 1;
6050}
6051
6052#if DIFF_DEBUG
6053void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
6054{
6055 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
6056 x, one ? one : "",
6057 s->path,
6058 DIFF_FILE_VALID(s) ? "valid" : "invalid",
6059 s->mode,
brian m. carlson41c95602016-06-24 23:09:24 +00006060 s->oid_valid ? oid_to_hex(&s->oid) : "");
Jeff King428d52a2014-01-16 20:21:59 -05006061 fprintf(stderr, "queue[%d] %s size %lu\n",
Junio C Hamano6973dca2006-04-21 23:57:45 -07006062 x, one ? one : "",
Jeff King428d52a2014-01-16 20:21:59 -05006063 s->size);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006064}
6065
6066void diff_debug_filepair(const struct diff_filepair *p, int i)
6067{
6068 diff_debug_filespec(p->one, i, "one");
6069 diff_debug_filespec(p->two, i, "two");
Linus Torvalds64479712007-10-25 11:20:56 -07006070 fprintf(stderr, "score %d, status %c rename_used %d broken %d\n",
Junio C Hamano6973dca2006-04-21 23:57:45 -07006071 p->score, p->status ? p->status : '?',
Linus Torvalds64479712007-10-25 11:20:56 -07006072 p->one->rename_used, p->broken_pair);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006073}
6074
6075void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
6076{
6077 int i;
6078 if (msg)
6079 fprintf(stderr, "%s\n", msg);
6080 fprintf(stderr, "q->nr = %d\n", q->nr);
6081 for (i = 0; i < q->nr; i++) {
6082 struct diff_filepair *p = q->queue[i];
6083 diff_debug_filepair(p, i);
6084 }
6085}
6086#endif
6087
6088static void diff_resolve_rename_copy(void)
6089{
Linus Torvalds64479712007-10-25 11:20:56 -07006090 int i;
6091 struct diff_filepair *p;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006092 struct diff_queue_struct *q = &diff_queued_diff;
6093
6094 diff_debug_queue("resolve-rename-copy", q);
6095
6096 for (i = 0; i < q->nr; i++) {
6097 p = q->queue[i];
6098 p->status = 0; /* undecided */
6099 if (DIFF_PAIR_UNMERGED(p))
6100 p->status = DIFF_STATUS_UNMERGED;
6101 else if (!DIFF_FILE_VALID(p->one))
6102 p->status = DIFF_STATUS_ADDED;
6103 else if (!DIFF_FILE_VALID(p->two))
6104 p->status = DIFF_STATUS_DELETED;
6105 else if (DIFF_PAIR_TYPE_CHANGED(p))
6106 p->status = DIFF_STATUS_TYPE_CHANGED;
6107
6108 /* from this point on, we are dealing with a pair
6109 * whose both sides are valid and of the same type, i.e.
6110 * either in-place edit or rename/copy edit.
6111 */
6112 else if (DIFF_PAIR_RENAME(p)) {
Linus Torvalds64479712007-10-25 11:20:56 -07006113 /*
6114 * A rename might have re-connected a broken
6115 * pair up, causing the pathnames to be the
6116 * same again. If so, that's not a rename at
6117 * all, just a modification..
6118 *
6119 * Otherwise, see if this source was used for
6120 * multiple renames, in which case we decrement
6121 * the count, and call it a copy.
Junio C Hamano6973dca2006-04-21 23:57:45 -07006122 */
Linus Torvalds64479712007-10-25 11:20:56 -07006123 if (!strcmp(p->one->path, p->two->path))
6124 p->status = DIFF_STATUS_MODIFIED;
6125 else if (--p->one->rename_used > 0)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006126 p->status = DIFF_STATUS_COPIED;
Linus Torvalds64479712007-10-25 11:20:56 -07006127 else
Junio C Hamano6973dca2006-04-21 23:57:45 -07006128 p->status = DIFF_STATUS_RENAMED;
6129 }
Jeff King9001dc22018-08-28 17:22:48 -04006130 else if (!oideq(&p->one->oid, &p->two->oid) ||
Johannes Schindelind516c2d2007-02-22 21:50:10 +01006131 p->one->mode != p->two->mode ||
Jens Lehmann85adbf22010-03-12 22:23:52 +01006132 p->one->dirty_submodule ||
6133 p->two->dirty_submodule ||
brian m. carlsona0d12c42016-06-24 23:09:23 +00006134 is_null_oid(&p->one->oid))
Junio C Hamano6973dca2006-04-21 23:57:45 -07006135 p->status = DIFF_STATUS_MODIFIED;
6136 else {
6137 /* This is a "no-change" entry and should not
6138 * happen anymore, but prepare for broken callers.
6139 */
6140 error("feeding unmodified %s to diffcore",
6141 p->one->path);
6142 p->status = DIFF_STATUS_UNKNOWN;
6143 }
6144 }
6145 diff_debug_queue("resolve-rename-copy done", q);
6146}
6147
Timo Hirvonenc6744342006-06-24 20:21:53 +03006148static int check_pair_status(struct diff_filepair *p)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006149{
Junio C Hamano6973dca2006-04-21 23:57:45 -07006150 switch (p->status) {
6151 case DIFF_STATUS_UNKNOWN:
Timo Hirvonenc6744342006-06-24 20:21:53 +03006152 return 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006153 case 0:
6154 die("internal error in diff-resolve-rename-copy");
Junio C Hamano6973dca2006-04-21 23:57:45 -07006155 default:
Timo Hirvonenc6744342006-06-24 20:21:53 +03006156 return 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006157 }
6158}
6159
Timo Hirvonenc6744342006-06-24 20:21:53 +03006160static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
6161{
6162 int fmt = opt->output_format;
6163
6164 if (fmt & DIFF_FORMAT_CHECKDIFF)
6165 diff_flush_checkdiff(p, opt);
6166 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
6167 diff_flush_raw(p, opt);
Junio C Hamanocd676a52008-02-12 14:26:02 -08006168 else if (fmt & DIFF_FORMAT_NAME) {
6169 const char *name_a, *name_b;
6170 name_a = p->two->path;
6171 name_b = NULL;
6172 strip_prefix(opt->prefix_length, &name_a, &name_b);
Jeff Kingf5022b52017-02-08 15:31:15 -05006173 fprintf(opt->file, "%s", diff_line_prefix(opt));
Daniel Barkalowc0c77732008-03-09 22:43:39 -04006174 write_name_quoted(name_a, opt->file, opt->line_termination);
Junio C Hamanocd676a52008-02-12 14:26:02 -08006175 }
Timo Hirvonenc6744342006-06-24 20:21:53 +03006176}
6177
Stefan Beller146fdb02017-06-29 17:07:05 -07006178static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs)
Sean4bbd2612006-05-14 08:13:49 -04006179{
Stefan Beller146fdb02017-06-29 17:07:05 -07006180 struct strbuf sb = STRBUF_INIT;
Sean4bbd2612006-05-14 08:13:49 -04006181 if (fs->mode)
Stefan Beller146fdb02017-06-29 17:07:05 -07006182 strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode);
Sean4bbd2612006-05-14 08:13:49 -04006183 else
Stefan Beller146fdb02017-06-29 17:07:05 -07006184 strbuf_addf(&sb, " %s ", newdelete);
6185
6186 quote_c_style(fs->path, &sb, NULL, 0);
6187 strbuf_addch(&sb, '\n');
6188 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6189 sb.buf, sb.len, 0);
6190 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04006191}
6192
Stefan Beller146fdb02017-06-29 17:07:05 -07006193static void show_mode_change(struct diff_options *opt, struct diff_filepair *p,
6194 int show_name)
Sean4bbd2612006-05-14 08:13:49 -04006195{
6196 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
Stefan Beller146fdb02017-06-29 17:07:05 -07006197 struct strbuf sb = STRBUF_INIT;
6198 strbuf_addf(&sb, " mode change %06o => %06o",
6199 p->one->mode, p->two->mode);
Alexandre Julliard0d26a642007-02-10 15:37:48 +01006200 if (show_name) {
Stefan Beller146fdb02017-06-29 17:07:05 -07006201 strbuf_addch(&sb, ' ');
6202 quote_c_style(p->two->path, &sb, NULL, 0);
Alexandre Julliard0d26a642007-02-10 15:37:48 +01006203 }
Stefan Beller58aaced2017-09-27 15:51:26 -07006204 strbuf_addch(&sb, '\n');
Stefan Beller146fdb02017-06-29 17:07:05 -07006205 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6206 sb.buf, sb.len, 0);
6207 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04006208 }
6209}
6210
Stefan Beller146fdb02017-06-29 17:07:05 -07006211static void show_rename_copy(struct diff_options *opt, const char *renamecopy,
6212 struct diff_filepair *p)
Sean4bbd2612006-05-14 08:13:49 -04006213{
Stefan Beller146fdb02017-06-29 17:07:05 -07006214 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07006215 struct strbuf names = STRBUF_INIT;
6216
6217 pprint_rename(&names, p->one->path, p->two->path);
Stefan Beller146fdb02017-06-29 17:07:05 -07006218 strbuf_addf(&sb, " %s %s (%d%%)\n",
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 20:02:20 +07006219 renamecopy, names.buf, similarity_index(p));
6220 strbuf_release(&names);
Stefan Beller146fdb02017-06-29 17:07:05 -07006221 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6222 sb.buf, sb.len, 0);
6223 show_mode_change(opt, p, 0);
Rene Scharfe348eda22017-08-30 19:49:42 +02006224 strbuf_release(&sb);
Sean4bbd2612006-05-14 08:13:49 -04006225}
6226
Bo Yang7be57612010-05-26 15:23:54 +08006227static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
Sean4bbd2612006-05-14 08:13:49 -04006228{
6229 switch(p->status) {
6230 case DIFF_STATUS_DELETED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006231 show_file_mode_name(opt, "delete", p->one);
Sean4bbd2612006-05-14 08:13:49 -04006232 break;
6233 case DIFF_STATUS_ADDED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006234 show_file_mode_name(opt, "create", p->two);
Sean4bbd2612006-05-14 08:13:49 -04006235 break;
6236 case DIFF_STATUS_COPIED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006237 show_rename_copy(opt, "copy", p);
Sean4bbd2612006-05-14 08:13:49 -04006238 break;
6239 case DIFF_STATUS_RENAMED:
Stefan Beller146fdb02017-06-29 17:07:05 -07006240 show_rename_copy(opt, "rename", p);
Sean4bbd2612006-05-14 08:13:49 -04006241 break;
6242 default:
6243 if (p->score) {
Stefan Beller146fdb02017-06-29 17:07:05 -07006244 struct strbuf sb = STRBUF_INIT;
6245 strbuf_addstr(&sb, " rewrite ");
6246 quote_c_style(p->two->path, &sb, NULL, 0);
6247 strbuf_addf(&sb, " (%d%%)\n", similarity_index(p));
6248 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6249 sb.buf, sb.len, 0);
Rene Scharfefa842d82017-08-30 19:49:41 +02006250 strbuf_release(&sb);
Pierre Habouzit663af342007-09-20 00:42:15 +02006251 }
Stefan Beller146fdb02017-06-29 17:07:05 -07006252 show_mode_change(opt, p, !p->score);
Sean4bbd2612006-05-14 08:13:49 -04006253 break;
6254 }
6255}
6256
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006257struct patch_id_t {
brian m. carlson36261e42019-08-18 20:04:03 +00006258 git_hash_ctx *ctx;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006259 int patchlen;
6260};
6261
6262static int remove_space(char *line, int len)
6263{
6264 int i;
Pierre Habouzit663af342007-09-20 00:42:15 +02006265 char *dst = line;
6266 unsigned char c;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006267
Pierre Habouzit663af342007-09-20 00:42:15 +02006268 for (i = 0; i < len; i++)
6269 if (!isspace((c = line[i])))
6270 *dst++ = c;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006271
Pierre Habouzit663af342007-09-20 00:42:15 +02006272 return dst - line;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006273}
6274
brian m. carlson36261e42019-08-18 20:04:03 +00006275void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx)
Stephen Boyda8f68552019-04-26 16:51:57 -07006276{
6277 unsigned char hash[GIT_MAX_RAWSZ];
6278 unsigned short carry = 0;
6279 int i;
6280
brian m. carlson36261e42019-08-18 20:04:03 +00006281 the_hash_algo->final_fn(hash, ctx);
6282 the_hash_algo->init_fn(ctx);
Stephen Boyda8f68552019-04-26 16:51:57 -07006283 /* 20-byte sum, with carry */
brian m. carlson36261e42019-08-18 20:04:03 +00006284 for (i = 0; i < the_hash_algo->rawsz; ++i) {
Stephen Boyda8f68552019-04-26 16:51:57 -07006285 carry += result->hash[i] + hash[i];
6286 result->hash[i] = carry;
6287 carry >>= 8;
6288 }
6289}
6290
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02006291static int patch_id_consume(void *priv, char *line, unsigned long len)
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006292{
6293 struct patch_id_t *data = priv;
6294 int new_len;
6295
René Scharfe82a62012020-08-19 00:08:54 +02006296 if (len > 12 && starts_with(line, "\\ "))
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02006297 return 0;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006298 new_len = remove_space(line, len);
6299
brian m. carlson36261e42019-08-18 20:04:03 +00006300 the_hash_algo->update_fn(data->ctx, line, new_len);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006301 data->patchlen += new_len;
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +02006302 return 0;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006303}
6304
brian m. carlson36261e42019-08-18 20:04:03 +00006305static void patch_id_add_string(git_hash_ctx *ctx, const char *str)
Jeff King977db6b2017-03-30 14:26:05 -04006306{
brian m. carlson36261e42019-08-18 20:04:03 +00006307 the_hash_algo->update_fn(ctx, str, strlen(str));
Jeff King977db6b2017-03-30 14:26:05 -04006308}
6309
brian m. carlson36261e42019-08-18 20:04:03 +00006310static void patch_id_add_mode(git_hash_ctx *ctx, unsigned mode)
Jeff King977db6b2017-03-30 14:26:05 -04006311{
6312 /* large enough for 2^32 in octal */
6313 char buf[12];
6314 int len = xsnprintf(buf, sizeof(buf), "%06o", mode);
brian m. carlson36261e42019-08-18 20:04:03 +00006315 the_hash_algo->update_fn(ctx, buf, len);
Jeff King977db6b2017-03-30 14:26:05 -04006316}
6317
Stephen Boyda8f68552019-04-26 16:51:57 -07006318/* returns 0 upon success, and writes result into oid */
Jerry Zhang51276c12022-10-24 20:07:40 +00006319static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only)
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006320{
6321 struct diff_queue_struct *q = &diff_queued_diff;
6322 int i;
brian m. carlson36261e42019-08-18 20:04:03 +00006323 git_hash_ctx ctx;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006324 struct patch_id_t data;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006325
brian m. carlson36261e42019-08-18 20:04:03 +00006326 the_hash_algo->init_fn(&ctx);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006327 memset(&data, 0, sizeof(struct patch_id_t));
6328 data.ctx = &ctx;
Stephen Boyda8f68552019-04-26 16:51:57 -07006329 oidclr(oid);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006330
6331 for (i = 0; i < q->nr; i++) {
6332 xpparam_t xpp;
6333 xdemitconf_t xecfg;
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006334 mmfile_t mf1, mf2;
6335 struct diff_filepair *p = q->queue[i];
6336 int len1, len2;
6337
Brian Downing9ccd0a82008-10-25 15:30:37 +02006338 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 19:05:46 +01006339 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006340 if (p->status == 0)
6341 return error("internal diff status error");
6342 if (p->status == DIFF_STATUS_UNKNOWN)
6343 continue;
6344 if (diff_unmodified_pair(p))
6345 continue;
6346 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
6347 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
6348 continue;
6349 if (DIFF_PAIR_UNMERGED(p))
6350 continue;
6351
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02006352 diff_fill_oid_info(p->one, options->repo->index);
6353 diff_fill_oid_info(p->two, options->repo->index);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006354
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006355 len1 = remove_space(p->one->path, strlen(p->one->path));
6356 len2 = remove_space(p->two->path, strlen(p->two->path));
Jeff King977db6b2017-03-30 14:26:05 -04006357 patch_id_add_string(&ctx, "diff--git");
6358 patch_id_add_string(&ctx, "a/");
brian m. carlson36261e42019-08-18 20:04:03 +00006359 the_hash_algo->update_fn(&ctx, p->one->path, len1);
Jeff King977db6b2017-03-30 14:26:05 -04006360 patch_id_add_string(&ctx, "b/");
brian m. carlson36261e42019-08-18 20:04:03 +00006361 the_hash_algo->update_fn(&ctx, p->two->path, len2);
Jeff King977db6b2017-03-30 14:26:05 -04006362
6363 if (p->one->mode == 0) {
6364 patch_id_add_string(&ctx, "newfilemode");
6365 patch_id_add_mode(&ctx, p->two->mode);
Jeff King977db6b2017-03-30 14:26:05 -04006366 } else if (p->two->mode == 0) {
6367 patch_id_add_string(&ctx, "deletedfilemode");
6368 patch_id_add_mode(&ctx, p->one->mode);
Jerry Zhang93105ab2022-10-24 20:07:42 +00006369 } else if (p->one->mode != p->two->mode) {
6370 patch_id_add_string(&ctx, "oldmode");
6371 patch_id_add_mode(&ctx, p->one->mode);
6372 patch_id_add_string(&ctx, "newmode");
6373 patch_id_add_mode(&ctx, p->two->mode);
Jeff King977db6b2017-03-30 14:26:05 -04006374 }
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006375
Jerry Zhang0570be72022-10-24 20:07:39 +00006376 if (diff_header_only) {
6377 /* don't do anything since we're only populating header info */
6378 } else if (diff_filespec_is_binary(options->repo, p->one) ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006379 diff_filespec_is_binary(options->repo, p->two)) {
brian m. carlson36261e42019-08-18 20:04:03 +00006380 the_hash_algo->update_fn(&ctx, oid_to_hex(&p->one->oid),
6381 the_hash_algo->hexsz);
6382 the_hash_algo->update_fn(&ctx, oid_to_hex(&p->two->oid),
6383 the_hash_algo->hexsz);
Jerry Zhang0570be72022-10-24 20:07:39 +00006384 } else {
6385 if (p->one->mode == 0) {
6386 patch_id_add_string(&ctx, "---/dev/null");
6387 patch_id_add_string(&ctx, "+++b/");
6388 the_hash_algo->update_fn(&ctx, p->two->path, len2);
6389 } else if (p->two->mode == 0) {
6390 patch_id_add_string(&ctx, "---a/");
6391 the_hash_algo->update_fn(&ctx, p->one->path, len1);
6392 patch_id_add_string(&ctx, "+++/dev/null");
6393 } else {
6394 patch_id_add_string(&ctx, "---a/");
6395 the_hash_algo->update_fn(&ctx, p->one->path, len1);
6396 patch_id_add_string(&ctx, "+++b/");
6397 the_hash_algo->update_fn(&ctx, p->two->path, len2);
6398 }
6399
6400 if (fill_mmfile(options->repo, &mf1, p->one) < 0 ||
6401 fill_mmfile(options->repo, &mf2, p->two) < 0)
6402 return error("unable to read files to diff");
6403 xpp.flags = 0;
6404 xecfg.ctxlen = 3;
6405 xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
6406 if (xdi_diff_outf(&mf1, &mf2, NULL,
6407 patch_id_consume, &data, &xpp, &xecfg))
6408 return error("unable to generate patch-id diff for %s",
6409 p->one->path);
Clemens Buchacher34597c12010-08-15 09:20:43 +02006410 }
Jerry Zhang51276c12022-10-24 20:07:40 +00006411 flush_one_hunk(oid, &ctx);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006412 }
6413
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006414 return 0;
6415}
6416
Jerry Zhang51276c12022-10-24 20:07:40 +00006417int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only)
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006418{
6419 struct diff_queue_struct *q = &diff_queued_diff;
Jerry Zhang51276c12022-10-24 20:07:40 +00006420 int result = diff_get_patch_id(options, oid, diff_header_only);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006421
SZEDER Gábor586d8b52022-11-02 23:01:42 +01006422 diff_free_queue(q);
Bo Yang9ca5df92010-05-06 21:52:27 -07006423 DIFF_QUEUE_CLEAR(q);
Johannes Schindelinfcb3d0a2006-06-25 03:51:08 +02006424
6425 return result;
6426}
6427
Timo Hirvonen946c3782006-06-27 15:09:17 +03006428static int is_summary_empty(const struct diff_queue_struct *q)
6429{
6430 int i;
6431
6432 for (i = 0; i < q->nr; i++) {
6433 const struct diff_filepair *p = q->queue[i];
6434
6435 switch (p->status) {
6436 case DIFF_STATUS_DELETED:
6437 case DIFF_STATUS_ADDED:
6438 case DIFF_STATUS_COPIED:
6439 case DIFF_STATUS_RENAMED:
6440 return 0;
6441 default:
6442 if (p->score)
6443 return 0;
6444 if (p->one->mode && p->two->mode &&
6445 p->one->mode != p->two->mode)
6446 return 0;
6447 break;
6448 }
6449 }
6450 return 1;
6451}
6452
Junio C Hamanof31027c2011-01-06 13:50:06 -08006453static const char rename_limit_warning[] =
Elijah Newren05d2c612021-07-15 00:45:21 +00006454N_("exhaustive rename detection was skipped due to too many files.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08006455
6456static const char degrade_cc_to_c_warning[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00006457N_("only found copies from modified paths due to too many files.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08006458
6459static const char rename_limit_advice[] =
Vasco Almeidadb424972016-10-17 13:15:29 +00006460N_("you may want to set your %s variable to at least "
6461 "%d and retry the command.");
Junio C Hamanof31027c2011-01-06 13:50:06 -08006462
6463void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)
6464{
Nguyễn Thái Ngọc Duy4e056c92018-01-16 16:23:49 +07006465 fflush(stdout);
Junio C Hamanof31027c2011-01-06 13:50:06 -08006466 if (degraded_cc)
Vasco Almeidadb424972016-10-17 13:15:29 +00006467 warning(_(degrade_cc_to_c_warning));
Junio C Hamanof31027c2011-01-06 13:50:06 -08006468 else if (needed)
Vasco Almeidadb424972016-10-17 13:15:29 +00006469 warning(_(rename_limit_warning));
Junio C Hamanof31027c2011-01-06 13:50:06 -08006470 else
6471 return;
Elijah Newren9f7e4bf2017-11-13 12:15:59 -08006472 if (0 < needed)
Vasco Almeidadb424972016-10-17 13:15:29 +00006473 warning(_(rename_limit_advice), varname, needed);
Junio C Hamanof31027c2011-01-06 13:50:06 -08006474}
6475
Elijah Newren95433ee2022-02-02 02:37:34 +00006476static void create_filepairs_for_header_only_notifications(struct diff_options *o)
6477{
6478 struct strset present;
6479 struct diff_queue_struct *q = &diff_queued_diff;
6480 struct hashmap_iter iter;
6481 struct strmap_entry *e;
6482 int i;
6483
6484 strset_init_with_options(&present, /*pool*/ NULL, /*strdup*/ 0);
6485
6486 /*
6487 * Find out which paths exist in diff_queued_diff, preferring
6488 * one->path for any pair that has multiple paths.
6489 */
6490 for (i = 0; i < q->nr; i++) {
6491 struct diff_filepair *p = q->queue[i];
6492 char *path = p->one->path ? p->one->path : p->two->path;
6493
6494 if (strmap_contains(o->additional_path_headers, path))
6495 strset_add(&present, path);
6496 }
6497
6498 /*
6499 * Loop over paths in additional_path_headers; for each NOT already
6500 * in diff_queued_diff, create a synthetic filepair and insert that
6501 * into diff_queued_diff.
6502 */
6503 strmap_for_each_entry(o->additional_path_headers, &iter, e) {
6504 if (!strset_contains(&present, e->key)) {
6505 struct diff_filespec *one, *two;
6506 struct diff_filepair *p;
6507
6508 one = alloc_filespec(e->key);
6509 two = alloc_filespec(e->key);
6510 fill_filespec(one, null_oid(), 0, 0);
6511 fill_filespec(two, null_oid(), 0, 0);
6512 p = diff_queue(q, one, two);
6513 p->status = DIFF_STATUS_MODIFIED;
6514 }
6515 }
6516
6517 /* Re-sort the filepairs */
6518 diffcore_fix_diff_index();
6519
6520 /* Cleanup */
6521 strset_clear(&present);
6522}
6523
Stefan Bellerec331502017-06-29 17:06:48 -07006524static void diff_flush_patch_all_file_pairs(struct diff_options *o)
6525{
6526 int i;
Stefan Bellere6e045f2017-06-29 17:07:06 -07006527 static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT;
Stefan Bellerec331502017-06-29 17:06:48 -07006528 struct diff_queue_struct *q = &diff_queued_diff;
Stefan Beller091f8e22017-06-29 17:06:53 -07006529
6530 if (WSEH_NEW & WS_RULE_MASK)
Johannes Schindelin033abf92018-05-02 11:38:39 +02006531 BUG("WS rules bit mask overlaps with diff symbol flags");
Stefan Beller091f8e22017-06-29 17:06:53 -07006532
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006533 if (o->color_moved)
6534 o->emitted_symbols = &esm;
Stefan Bellere6e045f2017-06-29 17:07:06 -07006535
Elijah Newren95433ee2022-02-02 02:37:34 +00006536 if (o->additional_path_headers)
6537 create_filepairs_for_header_only_notifications(o);
6538
Stefan Bellerec331502017-06-29 17:06:48 -07006539 for (i = 0; i < q->nr; i++) {
6540 struct diff_filepair *p = q->queue[i];
6541 if (check_pair_status(p))
6542 diff_flush_patch(p, o);
6543 }
Stefan Bellere6e045f2017-06-29 17:07:06 -07006544
6545 if (o->emitted_symbols) {
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006546 if (o->color_moved) {
Phillip Wood72962e82021-12-09 10:30:09 +00006547 struct mem_pool entry_pool;
6548 struct moved_entry_list *entry_list;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006549
Phillip Wood72962e82021-12-09 10:30:09 +00006550 mem_pool_init(&entry_pool, 1024 * 1024);
6551 entry_list = add_lines_to_move_detection(o,
6552 &entry_pool);
6553 mark_color_as_moved(o, entry_list);
Stefan Beller86b452e2017-06-30 13:53:09 -07006554 if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
6555 dim_moved_lines(o);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006556
Phillip Wood72962e82021-12-09 10:30:09 +00006557 mem_pool_discard(&entry_pool, 0);
6558 free(entry_list);
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006559 }
6560
Stefan Bellere6e045f2017-06-29 17:07:06 -07006561 for (i = 0; i < esm.nr; i++)
6562 emit_diff_symbol_from_struct(o, &esm.buf[i]);
6563
6564 for (i = 0; i < esm.nr; i++)
6565 free((void *)esm.buf[i].line);
Jeff King48edf3a2019-01-24 07:32:41 -05006566 esm.nr = 0;
6567
6568 o->emitted_symbols = NULL;
Stefan Bellere6e045f2017-06-29 17:07:06 -07006569 }
Stefan Bellerec331502017-06-29 17:06:48 -07006570}
6571
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006572static void diff_free_file(struct diff_options *options)
6573{
6574 if (options->close_file)
6575 fclose(options->file);
6576}
6577
Ævar Arnfjörð Bjarmasonc45dc9c2021-02-11 11:45:35 +01006578static void diff_free_ignore_regex(struct diff_options *options)
6579{
6580 int i;
6581
6582 for (i = 0; i < options->ignore_regex_nr; i++) {
6583 regfree(options->ignore_regex[i]);
6584 free(options->ignore_regex[i]);
6585 }
6586 free(options->ignore_regex);
6587}
6588
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006589void diff_free(struct diff_options *options)
6590{
6591 if (options->no_free)
6592 return;
6593
6594 diff_free_file(options);
Ævar Arnfjörð Bjarmasonc45dc9c2021-02-11 11:45:35 +01006595 diff_free_ignore_regex(options);
Ævar Arnfjörð Bjarmason244c2722022-02-16 11:56:28 +01006596 clear_pathspec(&options->pathspec);
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006597}
6598
Junio C Hamano6973dca2006-04-21 23:57:45 -07006599void diff_flush(struct diff_options *options)
6600{
6601 struct diff_queue_struct *q = &diff_queued_diff;
Timo Hirvonenc6744342006-06-24 20:21:53 +03006602 int i, output_format = options->output_format;
Timo Hirvonen946c3782006-06-27 15:09:17 +03006603 int separator = 0;
Johan Herland1c57a622011-04-29 11:36:21 +02006604 int dirstat_by_line = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006605
Timo Hirvonenc6744342006-06-24 20:21:53 +03006606 /*
6607 * Order: raw, stat, summary, patch
6608 * or: name/name-status/checkdiff (other bits clear)
6609 */
Elijah Newren95433ee2022-02-02 02:37:34 +00006610 if (!q->nr && !options->additional_path_headers)
Timo Hirvonen946c3782006-06-27 15:09:17 +03006611 goto free_queue;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006612
Timo Hirvonenc6744342006-06-24 20:21:53 +03006613 if (output_format & (DIFF_FORMAT_RAW |
6614 DIFF_FORMAT_NAME |
6615 DIFF_FORMAT_NAME_STATUS |
6616 DIFF_FORMAT_CHECKDIFF)) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07006617 for (i = 0; i < q->nr; i++) {
6618 struct diff_filepair *p = q->queue[i];
Timo Hirvonenc6744342006-06-24 20:21:53 +03006619 if (check_pair_status(p))
6620 flush_one_pair(p, options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006621 }
Timo Hirvonen946c3782006-06-27 15:09:17 +03006622 separator++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006623 }
Timo Hirvonenc6744342006-06-24 20:21:53 +03006624
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006625 if (output_format & DIFF_FORMAT_DIRSTAT && options->flags.dirstat_by_line)
Johan Herland1c57a622011-04-29 11:36:21 +02006626 dirstat_by_line = 1;
6627
6628 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT) ||
6629 dirstat_by_line) {
Timo Hirvonen5e2b0632006-06-25 14:28:19 +03006630 struct diffstat_t diffstat;
Timo Hirvonenc6744342006-06-24 20:21:53 +03006631
Daniel Ferreirae4cb6592019-11-13 12:40:58 +00006632 compute_diffstat(options, &diffstat, q);
Junio C Hamano74e2abe2006-10-12 03:01:00 -07006633 if (output_format & DIFF_FORMAT_NUMSTAT)
6634 show_numstat(&diffstat, options);
6635 if (output_format & DIFF_FORMAT_DIFFSTAT)
6636 show_stats(&diffstat, options);
Junio C Hamanof6046522007-12-11 23:46:30 -08006637 if (output_format & DIFF_FORMAT_SHORTSTAT)
Daniel Barkalowc0c77732008-03-09 22:43:39 -04006638 show_shortstats(&diffstat, options);
Mårten Kongstadab273892015-03-02 16:05:39 +01006639 if (output_format & DIFF_FORMAT_DIRSTAT && dirstat_by_line)
Johan Herland1c57a622011-04-29 11:36:21 +02006640 show_dirstat_by_line(&diffstat, options);
Junio C Hamanof6046522007-12-11 23:46:30 -08006641 free_diffstat_info(&diffstat);
Junio C Hamano3969cf72006-06-27 15:08:19 -07006642 separator++;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006643 }
Johan Herland1c57a622011-04-29 11:36:21 +02006644 if ((output_format & DIFF_FORMAT_DIRSTAT) && !dirstat_by_line)
Junio C Hamanoc04a7152008-02-12 17:06:58 -08006645 show_dirstat(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006646
Timo Hirvonen946c3782006-06-27 15:09:17 +03006647 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
Bo Yang7be57612010-05-26 15:23:54 +08006648 for (i = 0; i < q->nr; i++) {
6649 diff_summary(options, q->queue[i]);
6650 }
Junio C Hamano3969cf72006-06-27 15:08:19 -07006651 separator++;
Sean4bbd2612006-05-14 08:13:49 -04006652 }
6653
Larry D'Anna6977c252010-02-16 01:55:21 -05006654 if (output_format & DIFF_FORMAT_NO_OUTPUT &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006655 options->flags.exit_with_status &&
6656 options->flags.diff_from_contents) {
Larry D'Anna6977c252010-02-16 01:55:21 -05006657 /*
6658 * run diff_flush_patch for the exit status. setting
Ondřej Bílka749f7632013-07-22 23:02:23 +02006659 * options->file to /dev/null should be safe, because we
Larry D'Anna6977c252010-02-16 01:55:21 -05006660 * aren't supposed to produce any output anyway.
6661 */
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006662 diff_free_file(options);
Nguyễn Thái Ngọc Duy23a9e072017-05-03 17:16:46 +07006663 options->file = xfopen("/dev/null", "w");
Larry D'Anna6977c252010-02-16 01:55:21 -05006664 options->close_file = 1;
Stefan Beller2e2d5ac2017-06-30 13:53:07 -07006665 options->color_moved = 0;
Larry D'Anna6977c252010-02-16 01:55:21 -05006666 for (i = 0; i < q->nr; i++) {
6667 struct diff_filepair *p = q->queue[i];
6668 if (check_pair_status(p))
6669 diff_flush_patch(p, options);
6670 if (options->found_changes)
6671 break;
6672 }
6673 }
6674
Timo Hirvonenc6744342006-06-24 20:21:53 +03006675 if (output_format & DIFF_FORMAT_PATCH) {
Timo Hirvonen946c3782006-06-27 15:09:17 +03006676 if (separator) {
Stefan Beller091f8e22017-06-29 17:06:53 -07006677 emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0);
Stefan Beller30b7e1e2017-06-29 17:07:04 -07006678 if (options->stat_sep)
Timo Hirvonen946c3782006-06-27 15:09:17 +03006679 /* attach patch instead of inline */
Stefan Beller30b7e1e2017-06-29 17:07:04 -07006680 emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP,
6681 NULL, 0, 0);
Timo Hirvonenc6744342006-06-24 20:21:53 +03006682 }
6683
Stefan Bellerec331502017-06-29 17:06:48 -07006684 diff_flush_patch_all_file_pairs(options);
Timo Hirvonenc6744342006-06-24 20:21:53 +03006685 }
6686
Jeff King04245582006-09-07 02:35:42 -04006687 if (output_format & DIFF_FORMAT_CALLBACK)
6688 options->format_callback(q, options, options->format_callback_data);
6689
Timo Hirvonen946c3782006-06-27 15:09:17 +03006690free_queue:
SZEDER Gábor586d8b52022-11-02 23:01:42 +01006691 diff_free_queue(q);
Bo Yang9ca5df92010-05-06 21:52:27 -07006692 DIFF_QUEUE_CLEAR(q);
Ævar Arnfjörð Bjarmasone900d492021-02-11 11:45:34 +01006693 diff_free(options);
Junio C Hamanof2451942009-05-22 12:45:29 -07006694
6695 /*
Jim Meyering97bf2a02009-08-30 22:27:02 +02006696 * Report the content-level differences with HAS_CHANGES;
Junio C Hamanof2451942009-05-22 12:45:29 -07006697 * diff_addremove/diff_change does not set the bit when
6698 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
6699 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006700 if (options->flags.diff_from_contents) {
Junio C Hamanof2451942009-05-22 12:45:29 -07006701 if (options->found_changes)
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006702 options->flags.has_changes = 1;
Junio C Hamanof2451942009-05-22 12:45:29 -07006703 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006704 options->flags.has_changes = 0;
Junio C Hamanof2451942009-05-22 12:45:29 -07006705 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07006706}
6707
Junio C Hamano08578fa2013-07-17 15:09:34 -07006708static int match_filter(const struct diff_options *options, const struct diff_filepair *p)
6709{
6710 return (((p->status == DIFF_STATUS_MODIFIED) &&
6711 ((p->score &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006712 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN, options)) ||
Junio C Hamano08578fa2013-07-17 15:09:34 -07006713 (!p->score &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006714 filter_bit_tst(DIFF_STATUS_MODIFIED, options)))) ||
Junio C Hamano08578fa2013-07-17 15:09:34 -07006715 ((p->status != DIFF_STATUS_MODIFIED) &&
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006716 filter_bit_tst(p->status, options)));
Junio C Hamano08578fa2013-07-17 15:09:34 -07006717}
6718
Junio C Hamano949226f2013-07-17 14:19:24 -07006719static void diffcore_apply_filter(struct diff_options *options)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006720{
6721 int i;
6722 struct diff_queue_struct *q = &diff_queued_diff;
6723 struct diff_queue_struct outq;
Junio C Hamano949226f2013-07-17 14:19:24 -07006724
Bo Yang9ca5df92010-05-06 21:52:27 -07006725 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006726
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006727 if (!options->filter)
Junio C Hamano6973dca2006-04-21 23:57:45 -07006728 return;
6729
Junio C Hamano1ecc1cb2013-07-17 15:05:46 -07006730 if (filter_bit_tst(DIFF_STATUS_FILTER_AON, options)) {
Junio C Hamano6973dca2006-04-21 23:57:45 -07006731 int found;
6732 for (i = found = 0; !found && i < q->nr; i++) {
Junio C Hamano08578fa2013-07-17 15:09:34 -07006733 if (match_filter(options, q->queue[i]))
Junio C Hamano6973dca2006-04-21 23:57:45 -07006734 found++;
6735 }
6736 if (found)
6737 return;
6738
6739 /* otherwise we will clear the whole queue
6740 * by copying the empty outq at the end of this
6741 * function, but first clear the current entries
6742 * in the queue.
6743 */
6744 for (i = 0; i < q->nr; i++)
6745 diff_free_filepair(q->queue[i]);
6746 }
6747 else {
6748 /* Only the matching ones */
6749 for (i = 0; i < q->nr; i++) {
6750 struct diff_filepair *p = q->queue[i];
Junio C Hamano08578fa2013-07-17 15:09:34 -07006751 if (match_filter(options, p))
Junio C Hamano6973dca2006-04-21 23:57:45 -07006752 diff_q(&outq, p);
6753 else
6754 diff_free_filepair(p);
6755 }
6756 }
6757 free(q->queue);
6758 *q = outq;
6759}
6760
Sven Verdoolaege57011152007-09-08 12:30:22 +02006761/* Check whether two filespecs with the same mode and size are identical */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006762static int diff_filespec_is_identical(struct repository *r,
6763 struct diff_filespec *one,
Sven Verdoolaege57011152007-09-08 12:30:22 +02006764 struct diff_filespec *two)
6765{
Junio C Hamano2b459b42008-03-02 00:07:59 -08006766 if (S_ISGITLINK(one->mode))
6767 return 0;
Jonathan Tan1c37e862020-04-07 15:11:41 -07006768 if (diff_populate_filespec(r, one, NULL))
Sven Verdoolaege57011152007-09-08 12:30:22 +02006769 return 0;
Jonathan Tan1c37e862020-04-07 15:11:41 -07006770 if (diff_populate_filespec(r, two, NULL))
Sven Verdoolaege57011152007-09-08 12:30:22 +02006771 return 0;
6772 return !memcmp(one->data, two->data, one->size);
6773}
6774
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006775static int diff_filespec_check_stat_unmatch(struct repository *r,
6776 struct diff_filepair *p)
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006777{
Jonathan Tan1c37e862020-04-07 15:11:41 -07006778 struct diff_populate_filespec_options dpf_options = {
6779 .check_size_only = 1,
Jonathan Tan95acf112020-04-07 15:11:43 -07006780 .missing_object_cb = diff_queued_diff_prefetch,
6781 .missing_object_data = r,
Jonathan Tan1c37e862020-04-07 15:11:41 -07006782 };
6783
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006784 if (p->done_skip_stat_unmatch)
6785 return p->skip_stat_unmatch_result;
6786
6787 p->done_skip_stat_unmatch = 1;
6788 p->skip_stat_unmatch_result = 0;
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006789 /*
6790 * 1. Entries that come from stat info dirtiness
6791 * always have both sides (iow, not create/delete),
6792 * one side of the object name is unknown, with
6793 * the same mode and size. Keep the ones that
6794 * do not match these criteria. They have real
6795 * differences.
6796 *
6797 * 2. At this point, the file is known to be modified,
6798 * with the same mode and size, and the object
6799 * name of one side is unknown. Need to inspect
6800 * the identical contents.
6801 */
6802 if (!DIFF_FILE_VALID(p->one) || /* (1) */
6803 !DIFF_FILE_VALID(p->two) ||
brian m. carlson41c95602016-06-24 23:09:24 +00006804 (p->one->oid_valid && p->two->oid_valid) ||
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006805 (p->one->mode != p->two->mode) ||
Jonathan Tan1c37e862020-04-07 15:11:41 -07006806 diff_populate_filespec(r, p->one, &dpf_options) ||
6807 diff_populate_filespec(r, p->two, &dpf_options) ||
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006808 (p->one->size != p->two->size) ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006809 !diff_filespec_is_identical(r, p->one, p->two)) /* (2) */
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07006810 p->skip_stat_unmatch_result = 1;
6811 return p->skip_stat_unmatch_result;
Nguyễn Thái Ngọc Duyfceb9072014-01-25 13:46:49 +07006812}
6813
Junio C Hamanofb132272007-08-03 13:33:31 -07006814static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
6815{
6816 int i;
6817 struct diff_queue_struct *q = &diff_queued_diff;
6818 struct diff_queue_struct outq;
Bo Yang9ca5df92010-05-06 21:52:27 -07006819 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamanofb132272007-08-03 13:33:31 -07006820
6821 for (i = 0; i < q->nr; i++) {
6822 struct diff_filepair *p = q->queue[i];
6823
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006824 if (diff_filespec_check_stat_unmatch(diffopt->repo, p))
Junio C Hamanofb132272007-08-03 13:33:31 -07006825 diff_q(&outq, p);
6826 else {
6827 /*
6828 * The caller can subtract 1 from skip_stat_unmatch
6829 * to determine how many paths were dirty only
6830 * due to stat info mismatch.
6831 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006832 if (!diffopt->flags.no_index)
René Scharfe6d2d9e82007-08-15 00:41:00 +02006833 diffopt->skip_stat_unmatch++;
Junio C Hamanofb132272007-08-03 13:33:31 -07006834 diff_free_filepair(p);
6835 }
6836 }
6837 free(q->queue);
6838 *q = outq;
6839}
6840
Junio C Hamano730f7282009-09-20 00:03:39 -07006841static int diffnamecmp(const void *a_, const void *b_)
6842{
6843 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
6844 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
6845 const char *name_a, *name_b;
6846
6847 name_a = a->one ? a->one->path : a->two->path;
6848 name_b = b->one ? b->one->path : b->two->path;
6849 return strcmp(name_a, name_b);
6850}
6851
Jeff King784c0da2019-02-14 00:48:03 -05006852void diffcore_fix_diff_index(void)
Junio C Hamano730f7282009-09-20 00:03:39 -07006853{
6854 struct diff_queue_struct *q = &diff_queued_diff;
René Scharfe9ed0d8d2016-09-29 17:27:31 +02006855 QSORT(q->queue, q->nr, diffnamecmp);
Junio C Hamano730f7282009-09-20 00:03:39 -07006856}
6857
Jonathan Tan95acf112020-04-07 15:11:43 -07006858void diff_add_if_missing(struct repository *r,
6859 struct oid_array *to_fetch,
6860 const struct diff_filespec *filespec)
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006861{
6862 if (filespec && filespec->oid_valid &&
Jonathan Tana63694f2019-08-20 13:53:20 -07006863 !S_ISGITLINK(filespec->mode) &&
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006864 oid_object_info_extended(r, &filespec->oid, NULL,
6865 OBJECT_INFO_FOR_PREFETCH))
6866 oid_array_append(to_fetch, &filespec->oid);
6867}
6868
Jonathan Tan95acf112020-04-07 15:11:43 -07006869void diff_queued_diff_prefetch(void *repository)
6870{
6871 struct repository *repo = repository;
6872 int i;
6873 struct diff_queue_struct *q = &diff_queued_diff;
6874 struct oid_array to_fetch = OID_ARRAY_INIT;
6875
6876 for (i = 0; i < q->nr; i++) {
6877 struct diff_filepair *p = q->queue[i];
6878 diff_add_if_missing(repo, &to_fetch, p->one);
6879 diff_add_if_missing(repo, &to_fetch, p->two);
6880 }
6881
6882 /*
6883 * NEEDSWORK: Consider deduplicating the OIDs sent.
6884 */
6885 promisor_remote_get_direct(repo, to_fetch.oid, to_fetch.nr);
6886
6887 oid_array_clear(&to_fetch);
6888}
6889
Junio C Hamano6973dca2006-04-21 23:57:45 -07006890void diffcore_std(struct diff_options *options)
6891{
Jonathan Tan95acf112020-04-07 15:11:43 -07006892 int output_formats_to_prefetch = DIFF_FORMAT_DIFFSTAT |
6893 DIFF_FORMAT_NUMSTAT |
6894 DIFF_FORMAT_PATCH |
6895 DIFF_FORMAT_SHORTSTAT |
6896 DIFF_FORMAT_DIRSTAT;
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006897
Jonathan Tan95acf112020-04-07 15:11:43 -07006898 /*
6899 * Check if the user requested a blob-data-requiring diff output and/or
6900 * break-rewrite detection (which requires blob data). If yes, prefetch
6901 * the diff pairs.
6902 *
6903 * If no prefetching occurs, diffcore_rename() will prefetch if it
6904 * decides that it needs inexact rename detection.
6905 */
Ævar Arnfjörð Bjarmasona5183d72023-03-28 15:58:53 +02006906 if (options->repo == the_repository && repo_has_promisor_remote(the_repository) &&
Jonathan Tan95acf112020-04-07 15:11:43 -07006907 (options->output_format & output_formats_to_prefetch ||
6908 options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
6909 diff_queued_diff_prefetch(options->repo);
Jonathan Tan7fbbcb22019-04-05 10:09:34 -07006910
Kirill Smelkov7195fbf2014-02-24 20:21:51 +04006911 /* NOTE please keep the following in sync with diff_tree_combined() */
Junio C Hamano9d865352008-09-06 19:09:16 -07006912 if (options->skip_stat_unmatch)
Junio C Hamanofb132272007-08-03 13:33:31 -07006913 diffcore_skip_stat_unmatch(options);
Junio C Hamano44c48a92010-08-13 12:17:45 -07006914 if (!options->found_follow) {
6915 /* See try_to_follow_renames() in tree-diff.c */
6916 if (options->break_opt != -1)
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02006917 diffcore_break(options->repo,
6918 options->break_opt);
Junio C Hamano44c48a92010-08-13 12:17:45 -07006919 if (options->detect_rename)
6920 diffcore_rename(options);
6921 if (options->break_opt != -1)
6922 diffcore_merge_broken();
6923 }
Stefan Bellercf630512018-01-04 14:50:41 -08006924 if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
Junio C Hamano382f0132010-08-31 13:44:39 -07006925 diffcore_pickaxe(options);
Junio C Hamano6973dca2006-04-21 23:57:45 -07006926 if (options->orderfile)
6927 diffcore_order(options->orderfile);
Junio C Hamano1eb41362021-02-11 11:57:50 -08006928 if (options->rotate_to)
6929 diffcore_rotate(options);
Junio C Hamano44c48a92010-08-13 12:17:45 -07006930 if (!options->found_follow)
6931 /* See try_to_follow_renames() in tree-diff.c */
6932 diff_resolve_rename_copy();
Junio C Hamano949226f2013-07-17 14:19:24 -07006933 diffcore_apply_filter(options);
Junio C Hamano68aacb22007-03-14 11:12:13 -07006934
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006935 if (diff_queued_diff.nr && !options->flags.diff_from_contents)
6936 options->flags.has_changes = 1;
Pierre Habouzit8f67f8a2007-11-10 20:05:14 +01006937 else
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006938 options->flags.has_changes = 0;
Bo Yang1da61752010-05-06 21:52:28 -07006939
Junio C Hamano44c48a92010-08-13 12:17:45 -07006940 options->found_follow = 0;
Junio C Hamano6973dca2006-04-21 23:57:45 -07006941}
6942
Junio C Hamanoda31b352007-12-13 23:40:27 -08006943int diff_result_code(struct diff_options *opt, int status)
6944{
6945 int result = 0;
Junio C Hamanof31027c2011-01-06 13:50:06 -08006946
Max Nanasyc9fc4412013-03-21 12:53:38 -07006947 diff_warn_rename_limit("diff.renameLimit",
Junio C Hamanof31027c2011-01-06 13:50:06 -08006948 opt->needed_rename_limit,
6949 opt->degraded_cc_to_c);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006950 if (!opt->flags.exit_with_status &&
Junio C Hamanoda31b352007-12-13 23:40:27 -08006951 !(opt->output_format & DIFF_FORMAT_CHECKDIFF))
6952 return status;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006953 if (opt->flags.exit_with_status &&
6954 opt->flags.has_changes)
Junio C Hamanoda31b352007-12-13 23:40:27 -08006955 result |= 01;
6956 if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006957 opt->flags.check_failed)
Junio C Hamanoda31b352007-12-13 23:40:27 -08006958 result |= 02;
6959 return result;
6960}
Junio C Hamano6973dca2006-04-21 23:57:45 -07006961
Junio C Hamano28b92642011-05-31 09:14:17 -07006962int diff_can_quit_early(struct diff_options *opt)
6963{
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006964 return (opt->flags.quick &&
Junio C Hamano28b92642011-05-31 09:14:17 -07006965 !opt->filter &&
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006966 opt->flags.has_changes);
Junio C Hamano28b92642011-05-31 09:14:17 -07006967}
6968
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006969/*
6970 * Shall changes to this submodule be ignored?
6971 *
6972 * Submodule changes can be configured to be ignored separately for each path,
6973 * but that configuration can be overridden from the command line.
6974 */
6975static int is_submodule_ignored(const char *path, struct diff_options *options)
6976{
6977 int ignored = 0;
Brandon Williams02f2f562017-10-31 11:19:05 -07006978 struct diff_flags orig_flags = options->flags;
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006979 if (!options->flags.override_submodule_config)
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006980 set_diffopt_flags_from_submodule_config(options, path);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07006981 if (options->flags.ignore_submodules)
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02006982 ignored = 1;
6983 options->flags = orig_flags;
6984 return ignored;
6985}
6986
Daniel Ferreirae4cb6592019-11-13 12:40:58 +00006987void compute_diffstat(struct diff_options *options,
6988 struct diffstat_t *diffstat,
6989 struct diff_queue_struct *q)
6990{
6991 int i;
6992
6993 memset(diffstat, 0, sizeof(struct diffstat_t));
6994 for (i = 0; i < q->nr; i++) {
6995 struct diff_filepair *p = q->queue[i];
6996 if (check_pair_status(p))
6997 diff_flush_stat(p, options, diffstat);
6998 }
6999}
7000
Junio C Hamano6973dca2006-04-21 23:57:45 -07007001void diff_addremove(struct diff_options *options,
7002 int addremove, unsigned mode,
Brandon Williamsc26022e2017-05-30 10:30:47 -07007003 const struct object_id *oid,
7004 int oid_valid,
Jens Lehmanne3d42c42010-01-18 21:26:18 +01007005 const char *concatpath, unsigned dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07007006{
Junio C Hamano6973dca2006-04-21 23:57:45 -07007007 struct diff_filespec *one, *two;
7008
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02007009 if (S_ISGITLINK(mode) && is_submodule_ignored(concatpath, options))
Johannes Schindelin50fd9bd2008-05-14 18:03:31 +01007010 return;
7011
Junio C Hamano6973dca2006-04-21 23:57:45 -07007012 /* This may look odd, but it is a preparation for
7013 * feeding "there are unchanged files which should
7014 * not produce diffs, but when you are doing copy
7015 * detection you would need them, so here they are"
7016 * entries to the diff-core. They will be prefixed
7017 * with something like '=' or '*' (I haven't decided
7018 * which but should not make any difference).
Junio C Hamanoa6080a02007-06-07 00:04:01 -07007019 * Feeding the same new and old to diff_change()
Junio C Hamano6973dca2006-04-21 23:57:45 -07007020 * also has the same effect.
7021 * Before the final output happens, they are pruned after
7022 * merged into rename/copy pairs as appropriate.
7023 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07007024 if (options->flags.reverse_diff)
Junio C Hamano6973dca2006-04-21 23:57:45 -07007025 addremove = (addremove == '+' ? '-' :
7026 addremove == '-' ? '+' : addremove);
7027
Junio C Hamanocd676a52008-02-12 14:26:02 -08007028 if (options->prefix &&
7029 strncmp(concatpath, options->prefix, options->prefix_length))
7030 return;
7031
Junio C Hamano6973dca2006-04-21 23:57:45 -07007032 one = alloc_filespec(concatpath);
7033 two = alloc_filespec(concatpath);
7034
7035 if (addremove != '+')
Brandon Williamsf9704c22017-05-30 10:30:50 -07007036 fill_filespec(one, oid, oid_valid, mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01007037 if (addremove != '-') {
Brandon Williamsf9704c22017-05-30 10:30:50 -07007038 fill_filespec(two, oid, oid_valid, mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01007039 two->dirty_submodule = dirty_submodule;
7040 }
Junio C Hamano6973dca2006-04-21 23:57:45 -07007041
7042 diff_queue(&diff_queued_diff, one, two);
Brandon Williams0d1e0e72017-10-31 11:19:11 -07007043 if (!options->flags.diff_from_contents)
7044 options->flags.has_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07007045}
7046
7047void diff_change(struct diff_options *options,
7048 unsigned old_mode, unsigned new_mode,
Brandon Williams94a00972017-05-30 10:30:49 -07007049 const struct object_id *old_oid,
7050 const struct object_id *new_oid,
7051 int old_oid_valid, int new_oid_valid,
Jens Lehmanne3d42c42010-01-18 21:26:18 +01007052 const char *concatpath,
7053 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
Junio C Hamano6973dca2006-04-21 23:57:45 -07007054{
Junio C Hamano6973dca2006-04-21 23:57:45 -07007055 struct diff_filespec *one, *two;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07007056 struct diff_filepair *p;
Junio C Hamano6973dca2006-04-21 23:57:45 -07007057
Jens Lehmannaee9c7d2010-08-06 00:39:25 +02007058 if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) &&
7059 is_submodule_ignored(concatpath, options))
Johannes Schindelin50fd9bd2008-05-14 18:03:31 +01007060 return;
7061
Brandon Williams0d1e0e72017-10-31 11:19:11 -07007062 if (options->flags.reverse_diff) {
René Scharfe35d803b2017-01-28 22:40:58 +01007063 SWAP(old_mode, new_mode);
Brandon Williams94a00972017-05-30 10:30:49 -07007064 SWAP(old_oid, new_oid);
7065 SWAP(old_oid_valid, new_oid_valid);
René Scharfe35d803b2017-01-28 22:40:58 +01007066 SWAP(old_dirty_submodule, new_dirty_submodule);
Junio C Hamano6973dca2006-04-21 23:57:45 -07007067 }
Junio C Hamanocd676a52008-02-12 14:26:02 -08007068
7069 if (options->prefix &&
7070 strncmp(concatpath, options->prefix, options->prefix_length))
7071 return;
7072
Junio C Hamano6973dca2006-04-21 23:57:45 -07007073 one = alloc_filespec(concatpath);
7074 two = alloc_filespec(concatpath);
Brandon Williamsf9704c22017-05-30 10:30:50 -07007075 fill_filespec(one, old_oid, old_oid_valid, old_mode);
7076 fill_filespec(two, new_oid, new_oid_valid, new_mode);
Jens Lehmanne3d42c42010-01-18 21:26:18 +01007077 one->dirty_submodule = old_dirty_submodule;
7078 two->dirty_submodule = new_dirty_submodule;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07007079 p = diff_queue(&diff_queued_diff, one, two);
Junio C Hamano6973dca2006-04-21 23:57:45 -07007080
Brandon Williams0d1e0e72017-10-31 11:19:11 -07007081 if (options->flags.diff_from_contents)
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07007082 return;
7083
Brandon Williams0d1e0e72017-10-31 11:19:11 -07007084 if (options->flags.quick && options->skip_stat_unmatch &&
Jeff Kingd2d7fbe2020-06-01 16:22:18 -04007085 !diff_filespec_check_stat_unmatch(options->repo, p)) {
7086 diff_free_filespec_data(p->one);
7087 diff_free_filespec_data(p->two);
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07007088 return;
Jeff Kingd2d7fbe2020-06-01 16:22:18 -04007089 }
Nguyễn Thái Ngọc Duyf34b2052014-01-25 13:46:50 +07007090
Brandon Williams0d1e0e72017-10-31 11:19:11 -07007091 options->flags.has_changes = 1;
Junio C Hamano6973dca2006-04-21 23:57:45 -07007092}
7093
Junio C Hamanofa7b2902011-04-22 16:05:58 -07007094struct diff_filepair *diff_unmerge(struct diff_options *options, const char *path)
Junio C Hamano6973dca2006-04-21 23:57:45 -07007095{
Junio C Hamano76399c02011-04-22 15:55:55 -07007096 struct diff_filepair *pair;
Junio C Hamano6973dca2006-04-21 23:57:45 -07007097 struct diff_filespec *one, *two;
Junio C Hamanocd676a52008-02-12 14:26:02 -08007098
7099 if (options->prefix &&
7100 strncmp(path, options->prefix, options->prefix_length))
Junio C Hamano76399c02011-04-22 15:55:55 -07007101 return NULL;
Junio C Hamanocd676a52008-02-12 14:26:02 -08007102
Junio C Hamano6973dca2006-04-21 23:57:45 -07007103 one = alloc_filespec(path);
7104 two = alloc_filespec(path);
Junio C Hamano76399c02011-04-22 15:55:55 -07007105 pair = diff_queue(&diff_queued_diff, one, two);
7106 pair->is_unmerged = 1;
7107 return pair;
Junio C Hamano6973dca2006-04-21 23:57:45 -07007108}
Jeff King9cb92c32008-10-05 17:43:45 -04007109
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02007110static char *run_textconv(struct repository *r,
7111 const char *pgm,
7112 struct diff_filespec *spec,
7113 size_t *outsize)
Jeff King9cb92c32008-10-05 17:43:45 -04007114{
Jeff King479b0ae2009-01-22 00:59:56 -05007115 struct diff_tempfile *temp;
René Scharfed3180272014-08-19 21:09:35 +02007116 struct child_process child = CHILD_PROCESS_INIT;
Jeff King9cb92c32008-10-05 17:43:45 -04007117 struct strbuf buf = STRBUF_INIT;
Johannes Sixtda1fbed2010-03-30 19:36:03 +02007118 int err = 0;
Jeff King9cb92c32008-10-05 17:43:45 -04007119
Jeff Kingf034bb12023-01-06 06:05:00 -05007120 temp = prepare_temp_file(r, spec);
Ævar Arnfjörð Bjarmason7f146092021-11-25 23:52:21 +01007121 strvec_push(&child.args, pgm);
7122 strvec_push(&child.args, temp->name);
Jeff King9cb92c32008-10-05 17:43:45 -04007123
Jeff King41a457e2009-12-30 06:01:09 -05007124 child.use_shell = 1;
Jeff King9cb92c32008-10-05 17:43:45 -04007125 child.out = -1;
Johannes Sixtda1fbed2010-03-30 19:36:03 +02007126 if (start_command(&child)) {
Jeff King479b0ae2009-01-22 00:59:56 -05007127 remove_tempfile();
Jeff King9cb92c32008-10-05 17:43:45 -04007128 return NULL;
7129 }
Johannes Sixtda1fbed2010-03-30 19:36:03 +02007130
7131 if (strbuf_read(&buf, child.out, 0) < 0)
7132 err = error("error reading from textconv command '%s'", pgm);
Jeff King70d70992009-12-30 04:02:53 -05007133 close(child.out);
Johannes Sixtda1fbed2010-03-30 19:36:03 +02007134
7135 if (finish_command(&child) || err) {
7136 strbuf_release(&buf);
7137 remove_tempfile();
7138 return NULL;
7139 }
Jeff King479b0ae2009-01-22 00:59:56 -05007140 remove_tempfile();
Jeff King9cb92c32008-10-05 17:43:45 -04007141
7142 return strbuf_detach(&buf, outsize);
7143}
Jeff King840383b2010-04-01 20:09:26 -04007144
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02007145size_t fill_textconv(struct repository *r,
7146 struct userdiff_driver *driver,
Axel Bonneta788d7d2010-06-07 17:23:36 +02007147 struct diff_filespec *df,
7148 char **outbuf)
Jeff King840383b2010-04-01 20:09:26 -04007149{
7150 size_t size;
7151
Jeff Kinga64e6a42016-02-22 13:28:54 -05007152 if (!driver) {
Jeff King840383b2010-04-01 20:09:26 -04007153 if (!DIFF_FILE_VALID(df)) {
7154 *outbuf = "";
7155 return 0;
7156 }
Jonathan Tan1c37e862020-04-07 15:11:41 -07007157 if (diff_populate_filespec(r, df, NULL))
Jeff King840383b2010-04-01 20:09:26 -04007158 die("unable to read files to diff");
7159 *outbuf = df->data;
7160 return df->size;
7161 }
7162
Jeff Kinga64e6a42016-02-22 13:28:54 -05007163 if (!driver->textconv)
Johannes Schindelin033abf92018-05-02 11:38:39 +02007164 BUG("fill_textconv called with non-textconv driver");
Jeff Kinga64e6a42016-02-22 13:28:54 -05007165
brian m. carlson41c95602016-06-24 23:09:24 +00007166 if (driver->textconv_cache && df->oid_valid) {
brian m. carlsona0d12c42016-06-24 23:09:23 +00007167 *outbuf = notes_cache_get(driver->textconv_cache,
brian m. carlson569aa372017-05-06 22:09:58 +00007168 &df->oid,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04007169 &size);
7170 if (*outbuf)
7171 return size;
7172 }
7173
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 17:57:19 +02007174 *outbuf = run_textconv(r, driver->textconv, df, &size);
Jeff King840383b2010-04-01 20:09:26 -04007175 if (!*outbuf)
7176 die("unable to read files to diff");
Jeff Kingd9bae1a2010-04-01 20:12:15 -04007177
brian m. carlson41c95602016-06-24 23:09:24 +00007178 if (driver->textconv_cache && df->oid_valid) {
Jeff Kingd9bae1a2010-04-01 20:12:15 -04007179 /* ignore errors, as we might be in a readonly repository */
brian m. carlson569aa372017-05-06 22:09:58 +00007180 notes_cache_put(driver->textconv_cache, &df->oid, *outbuf,
Jeff Kingd9bae1a2010-04-01 20:12:15 -04007181 size);
7182 /*
7183 * we could save up changes and flush them all at the end,
7184 * but we would need an extra call after all diffing is done.
7185 * Since generating a cache entry is the slow path anyway,
7186 * this extra overhead probably isn't a big deal.
7187 */
7188 notes_cache_write(driver->textconv_cache);
7189 }
7190
Jeff King840383b2010-04-01 20:09:26 -04007191 return size;
7192}
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07007193
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02007194int textconv_object(struct repository *r,
7195 const char *path,
Jeff Smith3a35cb22017-05-24 00:15:10 -05007196 unsigned mode,
7197 const struct object_id *oid,
7198 int oid_valid,
7199 char **buf,
7200 unsigned long *buf_size)
7201{
7202 struct diff_filespec *df;
7203 struct userdiff_driver *textconv;
7204
7205 df = alloc_filespec(path);
Junio C Hamanoa6f38c12017-06-19 12:38:44 -07007206 fill_filespec(df, oid, oid_valid, mode);
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +01007207 textconv = get_textconv(r, df);
Jeff Smith3a35cb22017-05-24 00:15:10 -05007208 if (!textconv) {
7209 free_filespec(df);
7210 return 0;
7211 }
7212
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +02007213 *buf_size = fill_textconv(r, textconv, df, buf);
Jeff Smith3a35cb22017-05-24 00:15:10 -05007214 free_filespec(df);
7215 return 1;
7216}
7217
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07007218void setup_diff_pager(struct diff_options *opt)
7219{
7220 /*
7221 * If the user asked for our exit code, then either they want --quiet
7222 * or --exit-code. We should definitely not bother with a pager in the
7223 * former case, as we will generate no output. Since we still properly
7224 * report our exit code even when a pager is run, we _could_ run a
7225 * pager with --exit-code. But since we have not done so historically,
7226 * and because it is easy to find people oneline advising "git diff
7227 * --exit-code" in hooks and other scripts, we do not do so.
7228 */
Brandon Williams0d1e0e72017-10-31 11:19:11 -07007229 if (!opt->flags.exit_with_status &&
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +07007230 check_pager_config("diff") != 0)
7231 setup_pager();
7232}