blob: a1961526c0dab1af182d4f400468bf5617f5175c [file] [log] [blame]
Junio C Hamano6973dca2006-04-22 06:57:451/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
Patrick Steinhardte7da9382024-06-14 06:50:234
5#define USE_THE_REPOSITORY_VARIABLE
Patrick Steinhardt41f43b82024-12-06 10:27:196#define DISABLE_SIGN_COMPARE_WARNINGS
Patrick Steinhardte7da9382024-06-14 06:50:237
Elijah Newrenbc5c5ec2023-05-16 06:33:578#include "git-compat-util.h"
Elijah Newren0b027f62023-03-21 06:25:589#include "abspath.h"
Elijah Newren9b5041f2023-04-22 20:17:1310#include "base85.h"
Brandon Williamsb2141fc2017-06-14 18:07:3611#include "config.h"
Elijah Newren73359a92023-04-11 03:00:4012#include "convert.h"
Elijah Newren32a8f512023-03-21 06:26:0313#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:5414#include "gettext.h"
Michael Haggerty284098f2015-08-12 17:12:0115#include "tempfile.h"
Junio C Hamano4460e052024-08-09 22:31:3516#include "revision.h"
Junio C Hamano6973dca2006-04-22 06:57:4517#include "quote.h"
18#include "diff.h"
19#include "diffcore.h"
Junio C Hamano051308f2006-05-04 23:51:4420#include "delta.h"
Elijah Newren41771fa2023-02-24 00:09:2721#include "hex.h"
Junio C Hamano6973dca2006-04-22 06:57:4522#include "xdiff-interface.h"
Jeff King7c92fe02006-09-08 08:03:1823#include "color.h"
Johannes Sixtd5535ec2007-10-19 19:47:5624#include "run-command.h"
Junio C Hamano23707812008-01-02 09:50:1125#include "utf8.h"
Patrick Steinhardt8f491512025-07-01 12:22:1526#include "odb.h"
Jeff Kingbe58e702008-10-05 21:43:2127#include "userdiff.h"
Johannes Schindelin752c0c22009-10-19 12:38:3228#include "submodule.h"
Stefan Beller2e2d5ac2017-06-30 20:53:0729#include "hashmap.h"
Phillip Wood72962e82021-12-09 10:30:0930#include "mem-pool.h"
Elijah Newren67238992023-05-16 06:34:0431#include "merge-ll.h"
Michael Haggerty02e8ca02012-10-28 16:50:5432#include "string-list.h"
Jeff Kingdbbcd442020-07-28 20:23:3933#include "strvec.h"
Junio C Hamano4460e052024-08-09 22:31:3534#include "tmp-objdir.h"
Jacob Keller660e1132016-08-31 23:27:2035#include "graph.h"
Elijah Newren6f2d7432023-04-11 03:00:4236#include "oid-array.h"
Jonathan Tan150e3002017-08-18 22:20:3637#include "packfile.h"
Elijah Newrenca4eed72023-04-11 07:41:5938#include "pager.h"
Nguyễn Thái Ngọc Duy4a288472019-01-27 00:35:3139#include "parse-options.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 13:55:2440#include "help.h"
Christian Couderb14ed5a2019-06-25 13:40:3141#include "promisor-remote.h"
Derrick Stoleead90da72021-09-08 11:23:5642#include "dir.h"
Elijah Newren87bed172023-04-11 07:41:5343#include "object-file.h"
Elijah Newrendabab1d2023-04-11 07:41:4944#include "object-name.h"
Elijah Newren08c46a42023-05-16 06:33:5645#include "read-cache-ll.h"
Elijah Newrene38da482023-03-21 06:26:0546#include "setup.h"
Elijah Newren95433ee2022-02-02 02:37:3447#include "strmap.h"
Elijah Newren64122312023-04-22 20:17:1648#include "ws.h"
Junio C Hamano6973dca2006-04-22 06:57:4549
Shawn O. Pearce1510fea2006-12-14 11:15:5750#ifdef NO_FAST_WORKING_DIRECTORY
51#define FAST_WORKING_DIRECTORY 0
52#else
53#define FAST_WORKING_DIRECTORY 1
54#endif
55
David Rientjes96f1e582006-08-15 17:23:4856static int diff_detect_rename_default;
Stefan Beller33de7162017-05-08 16:03:3857static int diff_indent_heuristic = 1;
Elijah Newren94b82d52021-07-15 00:45:2458static int diff_rename_limit_default = 1000;
Jim Meyeringa624eaa2008-08-15 11:39:2659static int diff_suppress_blank_empty;
Jeff Kinge9330ae2025-09-16 23:13:5960static enum git_colorbool diff_use_color_default = GIT_COLOR_UNKNOWN;
Stefan Beller2e2d5ac2017-06-30 20:53:0761static int diff_color_moved_default;
Stefan Beller626c0b52018-07-18 19:31:5662static int diff_color_moved_ws_default;
Jeff Muizelaar6468a4e2012-09-27 19:12:5263static int diff_context_default = 3;
Vegard Nossumc4888672017-01-12 12:21:1164static int diff_interhunk_context_default;
Patrick Steinhardt1b261c22024-05-27 11:46:3965static char *diff_word_regex_cfg;
René Scharfe54443bb2024-06-09 07:39:0866static struct external_diff external_diff_cfg;
Patrick Steinhardt6073b3b2024-05-27 11:46:1567static char *diff_order_file_cfg;
Junio C Hamanoaecbf912007-08-31 20:13:4268int diff_auto_refresh_index = 1;
Junio C Hamanoa5a818e2008-08-19 03:08:0969static int diff_mnemonic_prefix;
Eli Collinsf89504d2010-05-03 02:03:4170static int diff_no_prefix;
Patrick Steinhardtf9c19892024-05-27 11:46:2071static char *diff_src_prefix;
72static char *diff_dst_prefix;
Laurent Arnoudc28ded82020-05-22 10:46:1873static int diff_relative;
Dragan Simicbd48adc2023-09-11 15:39:4474static int diff_stat_name_width;
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 12:26:4675static int diff_stat_graph_width;
Johan Herland712d2c72011-04-29 09:36:2076static int diff_dirstat_permille_default = 30;
Johannes Schindelinbe4f2b42010-08-05 08:49:5577static struct diff_options default_diff_options;
Michal Privoznik07ab4de2013-01-16 07:51:5778static long diff_algorithm;
Junio C Hamanoa17505f2016-10-04 22:26:2779static unsigned ws_error_highlight_default = WSEH_NEW;
Junio C Hamano6973dca2006-04-22 06:57:4580
Jeff King7c92fe02006-09-08 08:03:1881static char diff_colors[][COLOR_MAXLEN] = {
Arjen Laarhovendc6ebd42009-02-13 21:53:4082 GIT_COLOR_RESET,
Jeff King8dbf3eb2015-05-27 20:48:4683 GIT_COLOR_NORMAL, /* CONTEXT */
Arjen Laarhovendc6ebd42009-02-13 21:53:4084 GIT_COLOR_BOLD, /* METAINFO */
85 GIT_COLOR_CYAN, /* FRAGINFO */
86 GIT_COLOR_RED, /* OLD */
87 GIT_COLOR_GREEN, /* NEW */
88 GIT_COLOR_YELLOW, /* COMMIT */
89 GIT_COLOR_BG_RED, /* WHITESPACE */
Bert Wesarg89cb73a2009-11-27 06:55:1890 GIT_COLOR_NORMAL, /* FUNCINFO */
Stefan Beller86b452e2017-06-30 20:53:0991 GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */
92 GIT_COLOR_BOLD_BLUE, /* OLD_MOVED ALTERNATIVE */
93 GIT_COLOR_FAINT, /* OLD_MOVED_DIM */
94 GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */
95 GIT_COLOR_BOLD_CYAN, /* NEW_MOVED */
96 GIT_COLOR_BOLD_YELLOW, /* NEW_MOVED ALTERNATIVE */
97 GIT_COLOR_FAINT, /* NEW_MOVED_DIM */
98 GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */
Johannes Schindelina7be92a2018-08-13 11:33:3299 GIT_COLOR_FAINT, /* CONTEXT_DIM */
100 GIT_COLOR_FAINT_RED, /* OLD_DIM */
101 GIT_COLOR_FAINT_GREEN, /* NEW_DIM */
102 GIT_COLOR_BOLD, /* CONTEXT_BOLD */
103 GIT_COLOR_BOLD_RED, /* OLD_BOLD */
104 GIT_COLOR_BOLD_GREEN, /* NEW_BOLD */
Johannes Schindelincd112ce2006-06-13 16:45:44105};
106
Nguyễn Thái Ngọc Duya73b3682018-05-26 13:55:21107static const char *color_diff_slots[] = {
108 [DIFF_CONTEXT] = "context",
109 [DIFF_METAINFO] = "meta",
110 [DIFF_FRAGINFO] = "frag",
111 [DIFF_FILE_OLD] = "old",
112 [DIFF_FILE_NEW] = "new",
113 [DIFF_COMMIT] = "commit",
114 [DIFF_WHITESPACE] = "whitespace",
115 [DIFF_FUNCINFO] = "func",
116 [DIFF_FILE_OLD_MOVED] = "oldMoved",
117 [DIFF_FILE_OLD_MOVED_ALT] = "oldMovedAlternative",
118 [DIFF_FILE_OLD_MOVED_DIM] = "oldMovedDimmed",
119 [DIFF_FILE_OLD_MOVED_ALT_DIM] = "oldMovedAlternativeDimmed",
120 [DIFF_FILE_NEW_MOVED] = "newMoved",
121 [DIFF_FILE_NEW_MOVED_ALT] = "newMovedAlternative",
122 [DIFF_FILE_NEW_MOVED_DIM] = "newMovedDimmed",
123 [DIFF_FILE_NEW_MOVED_ALT_DIM] = "newMovedAlternativeDimmed",
Johannes Schindelina7be92a2018-08-13 11:33:32124 [DIFF_CONTEXT_DIM] = "contextDimmed",
125 [DIFF_FILE_OLD_DIM] = "oldDimmed",
126 [DIFF_FILE_NEW_DIM] = "newDimmed",
127 [DIFF_CONTEXT_BOLD] = "contextBold",
128 [DIFF_FILE_OLD_BOLD] = "oldBold",
129 [DIFF_FILE_NEW_BOLD] = "newBold",
Nguyễn Thái Ngọc Duya73b3682018-05-26 13:55:21130};
131
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 13:55:24132define_list_config_array_extra(color_diff_slots, {"plain"});
133
Jeff King9e1a5eb2014-06-18 19:41:50134static int parse_diff_color_slot(const char *var)
Junio C Hamano801235c2006-06-24 11:06:23135{
Nguyễn Thái Ngọc Duya73b3682018-05-26 13:55:21136 if (!strcasecmp(var, "plain"))
Jeff King8dbf3eb2015-05-27 20:48:46137 return DIFF_CONTEXT;
Nguyễn Thái Ngọc Duya73b3682018-05-26 13:55:21138 return LOOKUP_CONFIG(color_diff_slots, var);
Junio C Hamano801235c2006-06-24 11:06:23139}
140
Michael Haggerty02e8ca02012-10-28 16:50:54141static int parse_dirstat_params(struct diff_options *options, const char *params_string,
Johan Herland51670fc2011-04-29 09:36:22142 struct strbuf *errmsg)
Johan Herland333f3fb2011-04-29 09:36:18143{
Michael Haggerty02e8ca02012-10-28 16:50:54144 char *params_copy = xstrdup(params_string);
145 struct string_list params = STRING_LIST_INIT_NODUP;
146 int ret = 0;
147 int i;
Johan Herland51670fc2011-04-29 09:36:22148
Michael Haggerty02e8ca02012-10-28 16:50:54149 if (*params_copy)
Taylor Blau52acddf2023-04-24 22:20:10150 string_list_split_in_place(&params, params_copy, ",", -1);
Michael Haggerty02e8ca02012-10-28 16:50:54151 for (i = 0; i < params.nr; i++) {
152 const char *p = params.items[i].string;
153 if (!strcmp(p, "changes")) {
Brandon Williams0d1e0e72017-10-31 18:19:11154 options->flags.dirstat_by_line = 0;
155 options->flags.dirstat_by_file = 0;
Michael Haggerty02e8ca02012-10-28 16:50:54156 } else if (!strcmp(p, "lines")) {
Brandon Williams0d1e0e72017-10-31 18:19:11157 options->flags.dirstat_by_line = 1;
158 options->flags.dirstat_by_file = 0;
Michael Haggerty02e8ca02012-10-28 16:50:54159 } else if (!strcmp(p, "files")) {
Brandon Williams0d1e0e72017-10-31 18:19:11160 options->flags.dirstat_by_line = 0;
161 options->flags.dirstat_by_file = 1;
Michael Haggerty02e8ca02012-10-28 16:50:54162 } else if (!strcmp(p, "noncumulative")) {
Brandon Williams0d1e0e72017-10-31 18:19:11163 options->flags.dirstat_cumulative = 0;
Michael Haggerty02e8ca02012-10-28 16:50:54164 } else if (!strcmp(p, "cumulative")) {
Brandon Williams0d1e0e72017-10-31 18:19:11165 options->flags.dirstat_cumulative = 1;
Johan Herland333f3fb2011-04-29 09:36:18166 } else if (isdigit(*p)) {
167 char *end;
Johan Herland51670fc2011-04-29 09:36:22168 int permille = strtoul(p, &end, 10) * 10;
169 if (*end == '.' && isdigit(*++end)) {
Johan Herland712d2c72011-04-29 09:36:20170 /* only use first digit */
Johan Herland51670fc2011-04-29 09:36:22171 permille += *end - '0';
Johan Herland712d2c72011-04-29 09:36:20172 /* .. and ignore any further digits */
Johan Herland51670fc2011-04-29 09:36:22173 while (isdigit(*++end))
Johan Herland712d2c72011-04-29 09:36:20174 ; /* nothing */
175 }
Michael Haggerty02e8ca02012-10-28 16:50:54176 if (!*end)
Johan Herland51670fc2011-04-29 09:36:22177 options->dirstat_permille = permille;
178 else {
Michael Haggerty02e8ca02012-10-28 16:50:54179 strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
180 p);
Johan Herland51670fc2011-04-29 09:36:22181 ret++;
182 }
183 } else {
Michael Haggerty02e8ca02012-10-28 16:50:54184 strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p);
Johan Herland51670fc2011-04-29 09:36:22185 ret++;
Johan Herland333f3fb2011-04-29 09:36:18186 }
Johan Herland51670fc2011-04-29 09:36:22187
Johan Herland333f3fb2011-04-29 09:36:18188 }
Michael Haggerty02e8ca02012-10-28 16:50:54189 string_list_clear(&params, 0);
190 free(params_copy);
Johan Herland51670fc2011-04-29 09:36:22191 return ret;
Johan Herland333f3fb2011-04-29 09:36:18192}
193
Ramkumar Ramachandrac47ef572012-11-13 15:42:45194static int parse_submodule_params(struct diff_options *options, const char *value)
195{
196 if (!strcmp(value, "log"))
Jacob Keller61cfbc02016-08-31 23:27:21197 options->submodule_format = DIFF_SUBMODULE_LOG;
Ramkumar Ramachandrac47ef572012-11-13 15:42:45198 else if (!strcmp(value, "short"))
Jacob Keller61cfbc02016-08-31 23:27:21199 options->submodule_format = DIFF_SUBMODULE_SHORT;
Jacob Kellerfd47ae62016-08-31 23:27:25200 else if (!strcmp(value, "diff"))
201 options->submodule_format = DIFF_SUBMODULE_INLINE_DIFF;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 11:24:41202 /*
203 * Please update $__git_diff_submodule_formats in
204 * git-completion.bash when you add new formats.
205 */
Ramkumar Ramachandrac47ef572012-11-13 15:42:45206 else
207 return -1;
208 return 0;
209}
210
Ben Peart85b46032018-05-02 16:01:14211int git_config_rename(const char *var, const char *value)
Linus Torvaldscced5fb2009-04-09 18:46:15212{
213 if (!value)
214 return DIFF_DETECT_RENAME;
215 if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy"))
216 return DIFF_DETECT_COPY;
217 return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0;
218}
219
Michal Privoznik07924d42013-01-16 07:51:58220long parse_algorithm_value(const char *value)
Michal Privoznik07ab4de2013-01-16 07:51:57221{
222 if (!value)
223 return -1;
224 else if (!strcasecmp(value, "myers") || !strcasecmp(value, "default"))
225 return 0;
226 else if (!strcasecmp(value, "minimal"))
227 return XDF_NEED_MINIMAL;
228 else if (!strcasecmp(value, "patience"))
229 return XDF_PATIENCE_DIFF;
230 else if (!strcasecmp(value, "histogram"))
231 return XDF_HISTOGRAM_DIFF;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 11:24:41232 /*
233 * Please update $__git_diff_algorithms in git-completion.bash
234 * when you add new algorithms.
235 */
Michal Privoznik07ab4de2013-01-16 07:51:57236 return -1;
237}
238
Junio C Hamano0b4b42e2016-10-04 22:09:18239static int parse_one_token(const char **arg, const char *token)
240{
241 const char *rest;
242 if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) {
243 *arg = rest;
244 return 1;
245 }
246 return 0;
247}
248
249static int parse_ws_error_highlight(const char *arg)
250{
251 const char *orig_arg = arg;
252 unsigned val = 0;
253
254 while (*arg) {
255 if (parse_one_token(&arg, "none"))
256 val = 0;
257 else if (parse_one_token(&arg, "default"))
258 val = WSEH_NEW;
259 else if (parse_one_token(&arg, "all"))
260 val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT;
261 else if (parse_one_token(&arg, "new"))
262 val |= WSEH_NEW;
263 else if (parse_one_token(&arg, "old"))
264 val |= WSEH_OLD;
265 else if (parse_one_token(&arg, "context"))
266 val |= WSEH_CONTEXT;
267 else {
268 return -1 - (int)(arg - orig_arg);
269 }
270 if (*arg)
271 arg++;
272 }
273 return val;
274}
275
Junio C Hamanoe0e324a2007-07-07 08:49:58276/*
Junio C Hamano83ad63c2006-07-08 08:05:16277 * These are to give UI layer defaults.
278 * The core-level commands such as git-diff-files should
279 * never be affected by the setting of diff.renames
280 * the user happens to have in the configuration file.
281 */
Matthieu Moy5404c112016-02-25 08:59:21282void init_diff_ui_defaults(void)
283{
Nguyễn Thái Ngọc Duy06dba2b2017-12-27 10:18:35284 diff_detect_rename_default = DIFF_DETECT_RENAME;
Matthieu Moy5404c112016-02-25 08:59:21285}
286
Jeff King783a86c2022-08-19 10:08:44287int git_diff_heuristic_config(const char *var, const char *value,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 17:09:48288 void *cb UNUSED)
Michael Haggerty5b162872016-09-05 09:44:53289{
Junio C Hamano3cde4e02016-12-23 20:32:22290 if (!strcmp(var, "diff.indentheuristic"))
Michael Haggerty5b162872016-09-05 09:44:53291 diff_indent_heuristic = git_config_bool(var, value);
Michael Haggerty5b162872016-09-05 09:44:53292 return 0;
293}
294
Stefan Beller2e2d5ac2017-06-30 20:53:07295static int parse_color_moved(const char *arg)
296{
297 switch (git_parse_maybe_bool(arg)) {
298 case 0:
299 return COLOR_MOVED_NO;
300 case 1:
301 return COLOR_MOVED_DEFAULT;
302 default:
303 break;
304 }
305
306 if (!strcmp(arg, "no"))
307 return COLOR_MOVED_NO;
Stefan Beller176841f2017-06-30 20:53:08308 else if (!strcmp(arg, "plain"))
309 return COLOR_MOVED_PLAIN;
Stefan Beller51da15e2018-07-16 23:05:39310 else if (!strcmp(arg, "blocks"))
311 return COLOR_MOVED_BLOCKS;
Stefan Beller2e2d5ac2017-06-30 20:53:07312 else if (!strcmp(arg, "zebra"))
313 return COLOR_MOVED_ZEBRA;
314 else if (!strcmp(arg, "default"))
315 return COLOR_MOVED_DEFAULT;
Eric Sunshinee3f2f5f2018-07-24 21:58:45316 else if (!strcmp(arg, "dimmed-zebra"))
317 return COLOR_MOVED_ZEBRA_DIM;
Stefan Beller86b452e2017-06-30 20:53:09318 else if (!strcmp(arg, "dimmed_zebra"))
319 return COLOR_MOVED_ZEBRA_DIM;
Stefan Beller2e2d5ac2017-06-30 20:53:07320 else
Junio C Hamano706b0b52018-08-15 22:08:22321 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
Stefan Beller2e2d5ac2017-06-30 20:53:07322}
323
Stefan Bellerd173e792018-11-13 21:33:57324static unsigned parse_color_moved_ws(const char *arg)
Stefan Bellerb3095712018-07-16 23:05:40325{
326 int ret = 0;
327 struct string_list l = STRING_LIST_INIT_DUP;
328 struct string_list_item *i;
329
Junio C Hamanof3a303a2025-08-01 22:04:21330 string_list_split_f(&l, arg, ",", -1, STRING_LIST_SPLIT_TRIM);
Stefan Bellerb3095712018-07-16 23:05:40331
332 for_each_string_list_item(i, &l) {
Junio C Hamanof3a303a2025-08-01 22:04:21333 if (!strcmp(i->string, "no"))
Phillip Woodb73bcba2018-11-23 11:16:52334 ret = 0;
Junio C Hamanof3a303a2025-08-01 22:04:21335 else if (!strcmp(i->string, "ignore-space-change"))
Stefan Bellerb3095712018-07-16 23:05:40336 ret |= XDF_IGNORE_WHITESPACE_CHANGE;
Junio C Hamanof3a303a2025-08-01 22:04:21337 else if (!strcmp(i->string, "ignore-space-at-eol"))
Stefan Bellerb3095712018-07-16 23:05:40338 ret |= XDF_IGNORE_WHITESPACE_AT_EOL;
Junio C Hamanof3a303a2025-08-01 22:04:21339 else if (!strcmp(i->string, "ignore-all-space"))
Stefan Bellerb3095712018-07-16 23:05:40340 ret |= XDF_IGNORE_WHITESPACE;
Junio C Hamanof3a303a2025-08-01 22:04:21341 else if (!strcmp(i->string, "allow-indentation-change"))
Stefan Bellerca1f4ae2018-07-18 19:31:55342 ret |= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE;
Stefan Bellerd173e792018-11-13 21:33:57343 else {
344 ret |= COLOR_MOVED_WS_ERROR;
Junio C Hamanof3a303a2025-08-01 22:04:21345 error(_("unknown color-moved-ws mode '%s', possible values are 'ignore-space-change', 'ignore-space-at-eol', 'ignore-all-space', 'allow-indentation-change'"), i->string);
Stefan Bellerd173e792018-11-13 21:33:57346 }
Stefan Bellerb3095712018-07-16 23:05:40347 }
348
Stefan Bellerca1f4ae2018-07-18 19:31:55349 if ((ret & COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) &&
Stefan Bellerd173e792018-11-13 21:33:57350 (ret & XDF_WHITESPACE_FLAGS)) {
Junio C Hamano15b07cb2019-01-29 20:47:53351 error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes"));
Stefan Bellerd173e792018-11-13 21:33:57352 ret |= COLOR_MOVED_WS_ERROR;
353 }
Stefan Bellerca1f4ae2018-07-18 19:31:55354
Stefan Bellerb3095712018-07-16 23:05:40355 string_list_clear(&l, 0);
356
357 return ret;
Jeff Kinge269eb72011-08-18 05:03:48358}
Junio C Hamano801235c2006-06-24 11:06:23359
Glen Chooa4e7e312023-06-28 19:26:22360int git_diff_ui_config(const char *var, const char *value,
361 const struct config_context *ctx, void *cb)
Junio C Hamano801235c2006-06-24 11:06:23362{
363 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
364 diff_use_color_default = git_config_colorbool(var, value);
365 return 0;
366 }
Stefan Beller2e2d5ac2017-06-30 20:53:07367 if (!strcmp(var, "diff.colormoved")) {
368 int cm = parse_color_moved(value);
369 if (cm < 0)
370 return -1;
371 diff_color_moved_default = cm;
372 return 0;
373 }
Stefan Beller626c0b52018-07-18 19:31:56374 if (!strcmp(var, "diff.colormovedws")) {
Jeff Kingba176db2023-12-07 07:11:14375 unsigned cm;
376 if (!value)
377 return config_error_nonbool(var);
378 cm = parse_color_moved_ws(value);
Stefan Bellerd173e792018-11-13 21:33:57379 if (cm & COLOR_MOVED_WS_ERROR)
Stefan Beller626c0b52018-07-18 19:31:56380 return -1;
381 diff_color_moved_ws_default = cm;
382 return 0;
383 }
Jeff Muizelaar6468a4e2012-09-27 19:12:52384 if (!strcmp(var, "diff.context")) {
Glen Choo8868b1e2023-06-28 19:26:27385 diff_context_default = git_config_int(var, value, ctx->kvi);
Jeff Muizelaar6468a4e2012-09-27 19:12:52386 if (diff_context_default < 0)
387 return -1;
388 return 0;
389 }
Vegard Nossumc4888672017-01-12 12:21:11390 if (!strcmp(var, "diff.interhunkcontext")) {
Glen Choo8868b1e2023-06-28 19:26:27391 diff_interhunk_context_default = git_config_int(var, value,
392 ctx->kvi);
Vegard Nossumc4888672017-01-12 12:21:11393 if (diff_interhunk_context_default < 0)
394 return -1;
395 return 0;
396 }
Eric Wongb68ea122006-07-07 11:01:23397 if (!strcmp(var, "diff.renames")) {
Linus Torvaldscced5fb2009-04-09 18:46:15398 diff_detect_rename_default = git_config_rename(var, value);
Eric Wongb68ea122006-07-07 11:01:23399 return 0;
400 }
Junio C Hamanoaecbf912007-08-31 20:13:42401 if (!strcmp(var, "diff.autorefreshindex")) {
402 diff_auto_refresh_index = git_config_bool(var, value);
403 return 0;
404 }
Junio C Hamanoa5a818e2008-08-19 03:08:09405 if (!strcmp(var, "diff.mnemonicprefix")) {
406 diff_mnemonic_prefix = git_config_bool(var, value);
407 return 0;
408 }
Eli Collinsf89504d2010-05-03 02:03:41409 if (!strcmp(var, "diff.noprefix")) {
410 diff_no_prefix = git_config_bool(var, value);
411 return 0;
412 }
Peter Hutterer7fdc2652024-03-15 01:03:10413 if (!strcmp(var, "diff.srcprefix")) {
Patrick Steinhardtf9c19892024-05-27 11:46:20414 FREE_AND_NULL(diff_src_prefix);
Patrick Steinhardt1b261c22024-05-27 11:46:39415 return git_config_string(&diff_src_prefix, var, value);
Peter Hutterer7fdc2652024-03-15 01:03:10416 }
417 if (!strcmp(var, "diff.dstprefix")) {
Patrick Steinhardtf9c19892024-05-27 11:46:20418 FREE_AND_NULL(diff_dst_prefix);
Patrick Steinhardt1b261c22024-05-27 11:46:39419 return git_config_string(&diff_dst_prefix, var, value);
Peter Hutterer7fdc2652024-03-15 01:03:10420 }
Laurent Arnoudc28ded82020-05-22 10:46:18421 if (!strcmp(var, "diff.relative")) {
422 diff_relative = git_config_bool(var, value);
423 return 0;
424 }
Dragan Simicbd48adc2023-09-11 15:39:44425 if (!strcmp(var, "diff.statnamewidth")) {
426 diff_stat_name_width = git_config_int(var, value, ctx->kvi);
427 return 0;
428 }
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 12:26:46429 if (!strcmp(var, "diff.statgraphwidth")) {
Glen Choo8868b1e2023-06-28 19:26:27430 diff_stat_graph_width = git_config_int(var, value, ctx->kvi);
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 12:26:46431 return 0;
432 }
Brian Hetrodaec8082008-07-05 05:24:43433 if (!strcmp(var, "diff.external"))
René Scharfe54443bb2024-06-09 07:39:08434 return git_config_string(&external_diff_cfg.cmd, var, value);
René Scharfed7b97b72024-06-09 07:41:44435 if (!strcmp(var, "diff.trustexitcode")) {
436 external_diff_cfg.trust_exit_code = git_config_bool(var, value);
437 return 0;
438 }
Boyd Stephen Smith Jr98a4d872009-01-21 03:46:57439 if (!strcmp(var, "diff.wordregex"))
440 return git_config_string(&diff_word_regex_cfg, var, value);
Patrick Steinhardt76c7e702024-09-26 11:46:29441 if (!strcmp(var, "diff.orderfile")) {
442 FREE_AND_NULL(diff_order_file_cfg);
Samuel Bronson6d8940b2013-12-19 00:08:12443 return git_config_pathname(&diff_order_file_cfg, var, value);
Patrick Steinhardt76c7e702024-09-26 11:46:29444 }
Junio C Hamanof1af60b2007-04-23 00:52:55445
Jeff Kingba176db2023-12-07 07:11:14446 if (!strcmp(var, "diff.ignoresubmodules")) {
447 if (!value)
448 return config_error_nonbool(var);
Johannes Schindelinbe4f2b42010-08-05 08:49:55449 handle_ignore_submodules_arg(&default_diff_options, value);
Jeff Kingba176db2023-12-07 07:11:14450 }
Johannes Schindelinbe4f2b42010-08-05 08:49:55451
Ramkumar Ramachandrac47ef572012-11-13 15:42:45452 if (!strcmp(var, "diff.submodule")) {
Jeff Kingba176db2023-12-07 07:11:14453 if (!value)
454 return config_error_nonbool(var);
Ramkumar Ramachandrac47ef572012-11-13 15:42:45455 if (parse_submodule_params(&default_diff_options, value))
456 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
457 value);
458 return 0;
459 }
460
Michal Privoznik07ab4de2013-01-16 07:51:57461 if (!strcmp(var, "diff.algorithm")) {
Jeff King08248792023-12-07 07:25:23462 if (!value)
463 return config_error_nonbool(var);
Michal Privoznik07ab4de2013-01-16 07:51:57464 diff_algorithm = parse_algorithm_value(value);
465 if (diff_algorithm < 0)
Jeff King08248792023-12-07 07:25:23466 return error(_("unknown value for config '%s': %s"),
467 var, value);
Michal Privoznik07ab4de2013-01-16 07:51:57468 return 0;
469 }
470
Jeff King33c643b2017-10-13 17:24:31471 if (git_color_config(var, value, cb) < 0)
472 return -1;
473
Glen Chooa4e7e312023-06-28 19:26:22474 return git_diff_basic_config(var, value, ctx, cb);
Jeff King9a1805a2008-01-04 08:59:34475}
476
Glen Chooa4e7e312023-06-28 19:26:22477int git_diff_basic_config(const char *var, const char *value,
478 const struct config_context *ctx, void *cb)
Jeff King9a1805a2008-01-04 08:59:34479{
Jeff Kingae021d82014-06-18 19:47:50480 const char *name;
481
Linus Torvalds2b6ca6d2008-08-05 18:27:30482 if (!strcmp(var, "diff.renamelimit")) {
Glen Choo8868b1e2023-06-28 19:26:27483 diff_rename_limit_default = git_config_int(var, value, ctx->kvi);
Linus Torvalds2b6ca6d2008-08-05 18:27:30484 return 0;
485 }
486
Jeff King6680a082012-02-07 18:23:02487 if (userdiff_config(var, value) < 0)
488 return -1;
Jeff Kingc7534ef2008-10-26 04:45:55489
Jeff Kingae021d82014-06-18 19:47:50490 if (skip_prefix(var, "diff.color.", &name) ||
491 skip_prefix(var, "color.diff.", &name)) {
492 int slot = parse_diff_color_slot(name);
Jeff King8b8e8622009-12-12 12:25:24493 if (slot < 0)
494 return 0;
Junio C Hamano64f30e92008-02-11 18:53:56495 if (!value)
496 return config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 19:33:09497 return color_parse(value, diff_colors[slot]);
Junio C Hamano801235c2006-06-24 11:06:23498 }
Junio C Hamanof1af60b2007-04-23 00:52:55499
Jeff Kingda806352020-01-31 09:57:49500 if (!strcmp(var, "diff.wserrorhighlight")) {
Jeff Kingba176db2023-12-07 07:11:14501 int val;
502 if (!value)
503 return config_error_nonbool(var);
504 val = parse_ws_error_highlight(value);
Jeff Kingda806352020-01-31 09:57:49505 if (val < 0)
Jeff King08248792023-12-07 07:25:23506 return error(_("unknown value for config '%s': %s"),
507 var, value);
Jeff Kingda806352020-01-31 09:57:49508 ws_error_highlight_default = val;
509 return 0;
510 }
511
Jim Meyeringa624eaa2008-08-15 11:39:26512 /* like GNU diff's --suppress-blank-empty option */
Johannes Schindelin950db872009-01-20 21:08:33513 if (!strcmp(var, "diff.suppressblankempty") ||
514 /* for backwards compatibility */
515 !strcmp(var, "diff.suppress-blank-empty")) {
Jim Meyeringa624eaa2008-08-15 11:39:26516 diff_suppress_blank_empty = git_config_bool(var, value);
517 return 0;
518 }
519
Johan Herland2d174952011-04-29 09:36:19520 if (!strcmp(var, "diff.dirstat")) {
Johan Herland51670fc2011-04-29 09:36:22521 struct strbuf errmsg = STRBUF_INIT;
Jeff Kingba176db2023-12-07 07:11:14522 if (!value)
523 return config_error_nonbool(var);
Johan Herland712d2c72011-04-29 09:36:20524 default_diff_options.dirstat_permille = diff_dirstat_permille_default;
Johan Herland51670fc2011-04-29 09:36:22525 if (parse_dirstat_params(&default_diff_options, value, &errmsg))
Johan Herland7478ac52011-04-29 09:36:23526 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
Johan Herland51670fc2011-04-29 09:36:22527 errmsg.buf);
528 strbuf_release(&errmsg);
Johan Herland712d2c72011-04-29 09:36:20529 diff_dirstat_permille_default = default_diff_options.dirstat_permille;
Johan Herland2d174952011-04-29 09:36:19530 return 0;
531 }
532
Marc Branchaudcf5e7722017-05-08 16:03:36533 if (git_diff_heuristic_config(var, value, cb) < 0)
534 return -1;
535
Glen Chooa4e7e312023-06-28 19:26:22536 return git_default_config(var, value, ctx, cb);
Junio C Hamano801235c2006-06-24 11:06:23537}
538
Junio C Hamano6973dca2006-04-22 06:57:45539static char *quote_two(const char *one, const char *two)
540{
Junio C Hamano7c37c972020-09-10 17:01:59541 int need_one = quote_c_style(one, NULL, NULL, CQUOTE_NODQ);
542 int need_two = quote_c_style(two, NULL, NULL, CQUOTE_NODQ);
Brandon Caseyf285a2d2008-10-09 19:12:12543 struct strbuf res = STRBUF_INIT;
Junio C Hamano6973dca2006-04-22 06:57:45544
545 if (need_one + need_two) {
Pierre Habouzit663af342007-09-19 22:42:15546 strbuf_addch(&res, '"');
Junio C Hamano7c37c972020-09-10 17:01:59547 quote_c_style(one, &res, NULL, CQUOTE_NODQ);
548 quote_c_style(two, &res, NULL, CQUOTE_NODQ);
Pierre Habouzit663af342007-09-19 22:42:15549 strbuf_addch(&res, '"');
550 } else {
551 strbuf_addstr(&res, one);
552 strbuf_addstr(&res, two);
Junio C Hamano6973dca2006-04-22 06:57:45553 }
Pierre Habouzitb315c5c2007-09-27 10:58:23554 return strbuf_detach(&res, NULL);
Junio C Hamano6973dca2006-04-22 06:57:45555}
556
René Scharfe54443bb2024-06-09 07:39:08557static const struct external_diff *external_diff(void)
Junio C Hamano6973dca2006-04-22 06:57:45558{
René Scharfe54443bb2024-06-09 07:39:08559 static struct external_diff external_diff_env, *external_diff_ptr;
Junio C Hamano6973dca2006-04-22 06:57:45560 static int done_preparing = 0;
561
562 if (done_preparing)
René Scharfe54443bb2024-06-09 07:39:08563 return external_diff_ptr;
564 external_diff_env.cmd = xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
René Scharfed7b97b72024-06-09 07:41:44565 if (git_env_bool("GIT_EXTERNAL_DIFF_TRUST_EXIT_CODE", 0))
566 external_diff_env.trust_exit_code = 1;
René Scharfe54443bb2024-06-09 07:39:08567 if (external_diff_env.cmd)
568 external_diff_ptr = &external_diff_env;
569 else if (external_diff_cfg.cmd)
570 external_diff_ptr = &external_diff_cfg;
Junio C Hamano6973dca2006-04-22 06:57:45571 done_preparing = 1;
René Scharfe54443bb2024-06-09 07:39:08572 return external_diff_ptr;
Junio C Hamano6973dca2006-04-22 06:57:45573}
574
Michael Haggerty284098f2015-08-12 17:12:01575/*
576 * Keep track of files used for diffing. Sometimes such an entry
577 * refers to a temporary file, sometimes to an existing file, and
578 * sometimes to "/dev/null".
579 */
Junio C Hamano6973dca2006-04-22 06:57:45580static struct diff_tempfile {
Michael Haggerty284098f2015-08-12 17:12:01581 /*
582 * filename external diff should read from, or NULL if this
583 * entry is currently not in use:
584 */
585 const char *name;
586
brian m. carlsondc015052017-03-26 16:01:24587 char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano6973dca2006-04-22 06:57:45588 char mode[10];
Michael Haggerty284098f2015-08-12 17:12:01589
590 /*
591 * If this diff_tempfile instance refers to a temporary file,
592 * this tempfile object is used to manage its lifetime.
593 */
Jeff King076aa2c2017-09-05 12:15:08594 struct tempfile *tempfile;
Junio C Hamano6973dca2006-04-22 06:57:45595} diff_temp[2];
596
Junio C Hamano6957eb92009-09-15 01:44:01597struct emit_callback {
Junio C Hamano6957eb92009-09-15 01:44:01598 int color_diff;
599 unsigned ws_rule;
600 int blank_at_eof_in_preimage;
601 int blank_at_eof_in_postimage;
602 int lno_in_preimage;
603 int lno_in_postimage;
Junio C Hamano6957eb92009-09-15 01:44:01604 const char **label_path;
605 struct diff_words_data *diff_words;
Bo Yanga3c158d2010-05-26 07:08:02606 struct diff_options *opt;
Greg Bacon3e97c7c2009-11-19 21:12:24607 struct strbuf *header;
Junio C Hamano6957eb92009-09-15 01:44:01608};
609
Junio C Hamano6973dca2006-04-22 06:57:45610static int count_lines(const char *data, int size)
611{
612 int count, ch, completely_empty = 1, nl_just_seen = 0;
613 count = 0;
614 while (0 < size--) {
615 ch = *data++;
616 if (ch == '\n') {
617 count++;
618 nl_just_seen = 1;
619 completely_empty = 0;
620 }
621 else {
622 nl_just_seen = 0;
623 completely_empty = 0;
624 }
625 }
626 if (completely_empty)
627 return 0;
628 if (!nl_just_seen)
629 count++; /* no trailing newline */
630 return count;
631}
632
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:19633static int fill_mmfile(struct repository *r, mmfile_t *mf,
634 struct diff_filespec *one)
Junio C Hamano6957eb92009-09-15 01:44:01635{
636 if (!DIFF_FILE_VALID(one)) {
637 mf->ptr = (char *)""; /* does not matter */
638 mf->size = 0;
639 return 0;
640 }
Jonathan Tan1c37e862020-04-07 22:11:41641 else if (diff_populate_filespec(r, one, NULL))
Junio C Hamano6957eb92009-09-15 01:44:01642 return -1;
Junio C Hamanobb35fef2009-09-15 10:38:30643
Junio C Hamano6957eb92009-09-15 01:44:01644 mf->ptr = one->data;
645 mf->size = one->size;
646 return 0;
647}
648
Jeff Kingabb371a2011-02-19 08:16:32649/* like fill_mmfile, but only for size, so we can avoid retrieving blob */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:19650static unsigned long diff_filespec_size(struct repository *r,
651 struct diff_filespec *one)
Jeff Kingabb371a2011-02-19 08:16:32652{
Jonathan Tan1c37e862020-04-07 22:11:41653 struct diff_populate_filespec_options dpf_options = {
654 .check_size_only = 1,
655 };
656
Jeff Kingabb371a2011-02-19 08:16:32657 if (!DIFF_FILE_VALID(one))
658 return 0;
Jonathan Tan1c37e862020-04-07 22:11:41659 diff_populate_filespec(r, one, &dpf_options);
Jeff Kingabb371a2011-02-19 08:16:32660 return one->size;
661}
662
Jeff Kingc5224f02022-12-13 11:12:58663static int count_trailing_blank(mmfile_t *mf)
Junio C Hamano6957eb92009-09-15 01:44:01664{
665 char *ptr = mf->ptr;
666 long size = mf->size;
667 int cnt = 0;
668
669 if (!size)
670 return cnt;
671 ptr += size - 1; /* pointing at the very end */
672 if (*ptr != '\n')
673 ; /* incomplete line */
674 else
675 ptr--; /* skip the last LF */
676 while (mf->ptr < ptr) {
677 char *prev_eol;
678 for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--)
679 if (*prev_eol == '\n')
680 break;
Jeff Kingc5224f02022-12-13 11:12:58681 if (!ws_blank_line(prev_eol + 1, ptr - prev_eol))
Junio C Hamano6957eb92009-09-15 01:44:01682 break;
683 cnt++;
684 ptr = prev_eol - 1;
685 }
686 return cnt;
687}
688
689static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
690 struct emit_callback *ecbdata)
691{
692 int l1, l2, at;
Jeff Kingc5224f02022-12-13 11:12:58693 l1 = count_trailing_blank(mf1);
694 l2 = count_trailing_blank(mf2);
Junio C Hamano6957eb92009-09-15 01:44:01695 if (l2 <= l1) {
696 ecbdata->blank_at_eof_in_preimage = 0;
697 ecbdata->blank_at_eof_in_postimage = 0;
698 return;
699 }
700 at = count_lines(mf1->ptr, mf1->size);
701 ecbdata->blank_at_eof_in_preimage = (at - l1) + 1;
702
703 at = count_lines(mf2->ptr, mf2->size);
704 ecbdata->blank_at_eof_in_postimage = (at - l2) + 1;
705}
706
Johannes Schindelinf7c3b4e2018-08-13 11:33:20707static void emit_line_0(struct diff_options *o,
Stefan Beller017ac452018-08-14 01:41:19708 const char *set_sign, const char *set, unsigned reverse, const char *reset,
Junio C Hamano250f7992009-09-15 01:44:01709 int first, const char *line, int len)
Junio C Hamano6957eb92009-09-15 01:44:01710{
711 int has_trailing_newline, has_trailing_carriage_return;
Stefan Beller44410672018-08-14 01:41:22712 int needs_reset = 0; /* at the end of the line */
Bo Yanga3c158d2010-05-26 07:08:02713 FILE *file = o->file;
714
Stefan Bellerf103a6f2018-08-14 01:41:21715 fputs(diff_line_prefix(o), file);
Junio C Hamano6957eb92009-09-15 01:44:01716
Stefan Beller44410672018-08-14 01:41:22717 has_trailing_newline = (len > 0 && line[len-1] == '\n');
718 if (has_trailing_newline)
719 len--;
720
721 has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
722 if (has_trailing_carriage_return)
723 len--;
724
725 if (!len && !first)
726 goto end_of_line;
727
728 if (reverse && want_color(o->use_color)) {
729 fputs(GIT_COLOR_REVERSE, file);
730 needs_reset = 1;
Junio C Hamano250f7992009-09-15 01:44:01731 }
Junio C Hamano6957eb92009-09-15 01:44:01732
Stefan Beller44410672018-08-14 01:41:22733 if (set_sign) {
734 fputs(set_sign, file);
735 needs_reset = 1;
Junio C Hamano06a47552009-11-28 06:04:10736 }
Stefan Beller44410672018-08-14 01:41:22737
Junio C Hamano6957eb92009-09-15 01:44:01738 if (first)
Stefan Beller44410672018-08-14 01:41:22739 fputc(first, file);
Junio C Hamano6957eb92009-09-15 01:44:01740
Stefan Beller44410672018-08-14 01:41:22741 if (!len)
742 goto end_of_line;
Junio C Hamano6957eb92009-09-15 01:44:01743
Stefan Beller44410672018-08-14 01:41:22744 if (set) {
745 if (set_sign && set != set_sign)
746 fputs(reset, file);
Junio C Hamano250f7992009-09-15 01:44:01747 fputs(set, file);
Stefan Beller44410672018-08-14 01:41:22748 needs_reset = 1;
Junio C Hamano6957eb92009-09-15 01:44:01749 }
Stefan Beller44410672018-08-14 01:41:22750 fwrite(line, len, 1, file);
751 needs_reset = 1; /* 'line' may contain color codes. */
752
753end_of_line:
754 if (needs_reset)
755 fputs(reset, file);
Junio C Hamano6957eb92009-09-15 01:44:01756 if (has_trailing_carriage_return)
757 fputc('\r', file);
758 if (has_trailing_newline)
759 fputc('\n', file);
760}
761
Bo Yanga3c158d2010-05-26 07:08:02762static void emit_line(struct diff_options *o, const char *set, const char *reset,
Junio C Hamano250f7992009-09-15 01:44:01763 const char *line, int len)
764{
Stefan Beller44410672018-08-14 01:41:22765 emit_line_0(o, set, NULL, 0, reset, 0, line, len);
Junio C Hamano250f7992009-09-15 01:44:01766}
767
Stefan Beller36a4cef2017-06-30 00:06:49768enum diff_symbol {
Stefan Beller4eed0eb2017-06-30 00:07:01769 DIFF_SYMBOL_BINARY_DIFF_HEADER,
770 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
771 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
772 DIFF_SYMBOL_BINARY_DIFF_BODY,
773 DIFF_SYMBOL_BINARY_DIFF_FOOTER,
Stefan Beller0911c472017-06-30 00:07:02774 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
775 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
776 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
777 DIFF_SYMBOL_STATS_LINE,
Stefan Bellerbd033292017-06-30 00:07:03778 DIFF_SYMBOL_WORD_DIFF,
Stefan Beller30b7e1e2017-06-30 00:07:04779 DIFF_SYMBOL_STAT_SEP,
Stefan Beller146fdb02017-06-30 00:07:05780 DIFF_SYMBOL_SUMMARY,
Stefan Bellerf3597132017-06-30 00:07:00781 DIFF_SYMBOL_SUBMODULE_ADD,
782 DIFF_SYMBOL_SUBMODULE_DEL,
783 DIFF_SYMBOL_SUBMODULE_UNTRACKED,
784 DIFF_SYMBOL_SUBMODULE_MODIFIED,
785 DIFF_SYMBOL_SUBMODULE_HEADER,
786 DIFF_SYMBOL_SUBMODULE_ERROR,
787 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH,
Stefan Beller5af6ea92017-06-30 00:06:59788 DIFF_SYMBOL_REWRITE_DIFF,
Stefan Beller4acaaa72017-06-30 00:06:58789 DIFF_SYMBOL_BINARY_FILES,
Stefan Bellera29b0a12017-06-30 00:06:57790 DIFF_SYMBOL_HEADER,
Stefan Beller3ee8b7b2017-06-30 00:06:56791 DIFF_SYMBOL_FILEPAIR_PLUS,
792 DIFF_SYMBOL_FILEPAIR_MINUS,
Stefan Bellerff958672017-06-30 00:06:54793 DIFF_SYMBOL_WORDS_PORCELAIN,
794 DIFF_SYMBOL_WORDS,
Stefan Beller091f8e22017-06-30 00:06:53795 DIFF_SYMBOL_CONTEXT,
Stefan Bellerf2bb1212017-06-30 00:06:55796 DIFF_SYMBOL_CONTEXT_INCOMPLETE,
Stefan Beller091f8e22017-06-30 00:06:53797 DIFF_SYMBOL_PLUS,
798 DIFF_SYMBOL_MINUS,
Stefan Bellerb9cbfde2017-06-30 00:06:52799 DIFF_SYMBOL_NO_LF_EOF,
Stefan Beller68abc6f2017-06-30 00:06:51800 DIFF_SYMBOL_CONTEXT_FRAGINFO,
Stefan Bellerc64b4202017-06-30 00:06:50801 DIFF_SYMBOL_CONTEXT_MARKER,
Stefan Beller36a4cef2017-06-30 00:06:49802 DIFF_SYMBOL_SEPARATOR
803};
Stefan Beller091f8e22017-06-30 00:06:53804/*
805 * Flags for content lines:
806 * 0..12 are whitespace rules
807 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
808 * 16 is marking if the line is blank at EOF
809 */
Stefan Beller2e2d5ac2017-06-30 20:53:07810#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
811#define DIFF_SYMBOL_MOVED_LINE (1<<17)
812#define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
Stefan Beller86b452e2017-06-30 20:53:09813#define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
Stefan Beller091f8e22017-06-30 00:06:53814#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
815
Stefan Bellere6e045f2017-06-30 00:07:06816/*
817 * This struct is used when we need to buffer the output of the diff output.
818 *
819 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
820 * into the pre/post image file. This pointer could be a union with the
821 * line pointer. By storing an offset into the file instead of the literal line,
822 * we can decrease the memory footprint for the buffered output. At first we
823 * may want to only have indirection for the content lines, but we could also
824 * enhance the state for emitting prefabricated lines, e.g. the similarity
825 * score line or hunk/file headers would only need to store a number or path
826 * and then the output can be constructed later on depending on state.
827 */
828struct emitted_diff_symbol {
829 const char *line;
830 int len;
831 int flags;
Phillip Wood21536d02018-11-23 11:16:57832 int indent_off; /* Offset to first non-whitespace character */
833 int indent_width; /* The visual width of the indentation */
Phillip Wood72962e82021-12-09 10:30:09834 unsigned id;
Stefan Bellere6e045f2017-06-30 00:07:06835 enum diff_symbol s;
836};
Ævar Arnfjörð Bjarmason9865b6e2021-09-27 12:54:25837#define EMITTED_DIFF_SYMBOL_INIT { 0 }
Stefan Bellere6e045f2017-06-30 00:07:06838
839struct emitted_diff_symbols {
840 struct emitted_diff_symbol *buf;
841 int nr, alloc;
842};
Ævar Arnfjörð Bjarmason9865b6e2021-09-27 12:54:25843#define EMITTED_DIFF_SYMBOLS_INIT { 0 }
Stefan Bellere6e045f2017-06-30 00:07:06844
845static void append_emitted_diff_symbol(struct diff_options *o,
846 struct emitted_diff_symbol *e)
847{
848 struct emitted_diff_symbol *f;
849
850 ALLOC_GROW(o->emitted_symbols->buf,
851 o->emitted_symbols->nr + 1,
852 o->emitted_symbols->alloc);
853 f = &o->emitted_symbols->buf[o->emitted_symbols->nr++];
854
855 memcpy(f, e, sizeof(struct emitted_diff_symbol));
856 f->line = e->line ? xmemdupz(e->line, e->len) : NULL;
857}
858
Ævar Arnfjörð Bjarmason77e56d52022-03-17 14:55:35859static void free_emitted_diff_symbols(struct emitted_diff_symbols *e)
860{
861 if (!e)
862 return;
863 free(e->buf);
864 free(e);
865}
866
Stefan Beller2e2d5ac2017-06-30 20:53:07867struct moved_entry {
Stefan Beller2e2d5ac2017-06-30 20:53:07868 const struct emitted_diff_symbol *es;
869 struct moved_entry *next_line;
Phillip Wood72962e82021-12-09 10:30:09870 struct moved_entry *next_match;
Stefan Beller2e2d5ac2017-06-30 20:53:07871};
872
Phillip Wood74d156f2018-10-04 10:07:41873struct moved_block {
874 struct moved_entry *match;
Phillip Wood21536d02018-11-23 11:16:57875 int wsd; /* The whitespace delta of this block */
Phillip Wood74d156f2018-10-04 10:07:41876};
877
Phillip Wood0cd51e92018-11-23 11:16:58878#define INDENT_BLANKLINE INT_MIN
879
Phillip Wood21536d02018-11-23 11:16:57880static void fill_es_indent_data(struct emitted_diff_symbol *es)
881{
Phillip Wood0cd51e92018-11-23 11:16:58882 unsigned int off = 0, i;
Phillip Wood21536d02018-11-23 11:16:57883 int width = 0, tab_width = es->flags & WS_TAB_WIDTH_MASK;
884 const char *s = es->line;
885 const int len = es->len;
886
887 /* skip any \v \f \r at start of indentation */
888 while (s[off] == '\f' || s[off] == '\v' ||
Johannes Schindelin104add82025-04-29 11:37:58889 (off < len - 1 && s[off] == '\r'))
Phillip Wood21536d02018-11-23 11:16:57890 off++;
891
892 /* calculate the visual width of indentation */
893 while(1) {
894 if (s[off] == ' ') {
895 width++;
896 off++;
897 } else if (s[off] == '\t') {
898 width += tab_width - (width % tab_width);
899 while (s[++off] == '\t')
900 width += tab_width;
901 } else {
902 break;
903 }
904 }
905
Phillip Wood0cd51e92018-11-23 11:16:58906 /* check if this line is blank */
907 for (i = off; i < len; i++)
908 if (!isspace(s[i]))
909 break;
910
911 if (i == len) {
912 es->indent_width = INDENT_BLANKLINE;
913 es->indent_off = len;
914 } else {
915 es->indent_off = off;
916 es->indent_width = width;
917 }
Phillip Wood74d156f2018-10-04 10:07:41918}
919
Stefan Bellerca1f4ae2018-07-18 19:31:55920static int compute_ws_delta(const struct emitted_diff_symbol *a,
Phillip Wood25e61902021-12-09 10:30:07921 const struct emitted_diff_symbol *b)
Stefan Bellerca1f4ae2018-07-18 19:31:55922{
Phillip Wood25e61902021-12-09 10:30:07923 int a_width = a->indent_width,
Phillip Wood21536d02018-11-23 11:16:57924 b_width = b->indent_width;
Stefan Bellerca1f4ae2018-07-18 19:31:55925
Phillip Wood25e61902021-12-09 10:30:07926 if (a_width == INDENT_BLANKLINE && b_width == INDENT_BLANKLINE)
927 return INDENT_BLANKLINE;
Phillip Wood0cd51e92018-11-23 11:16:58928
Phillip Wood25e61902021-12-09 10:30:07929 return a_width - b_width;
Stefan Bellerca1f4ae2018-07-18 19:31:55930}
931
Phillip Wood52d14e12021-12-09 10:30:02932static int cmp_in_block_with_wsd(const struct moved_entry *cur,
933 const struct emitted_diff_symbol *l,
934 struct moved_block *pmb)
Stefan Bellerca1f4ae2018-07-18 19:31:55935{
Phillip Wood72962e82021-12-09 10:30:09936 int a_width = cur->es->indent_width, b_width = l->indent_width;
Phillip Wood21536d02018-11-23 11:16:57937 int delta;
Stefan Bellerca1f4ae2018-07-18 19:31:55938
Phillip Wood72962e82021-12-09 10:30:09939 /* The text of each line must match */
940 if (cur->es->id != l->id)
Stefan Bellerca1f4ae2018-07-18 19:31:55941 return 1;
942
Phillip Wood72962e82021-12-09 10:30:09943 /*
944 * If 'l' and 'cur' are both blank then we don't need to check the
945 * indent. We only need to check cur as we know the strings match.
946 * */
947 if (a_width == INDENT_BLANKLINE)
Phillip Wood0cd51e92018-11-23 11:16:58948 return 0;
Stefan Bellerca1f4ae2018-07-18 19:31:55949
950 /*
Phillip Wood21536d02018-11-23 11:16:57951 * The indent changes of the block are known and stored in pmb->wsd;
952 * however we need to check if the indent changes of the current line
Phillip Wood72962e82021-12-09 10:30:09953 * match those of the current block.
Stefan Bellerca1f4ae2018-07-18 19:31:55954 */
Phillip Wood52d14e12021-12-09 10:30:02955 delta = b_width - a_width;
Stefan Bellerca1f4ae2018-07-18 19:31:55956
Phillip Wood0cd51e92018-11-23 11:16:58957 /*
958 * If the previous lines of this block were all blank then set its
959 * whitespace delta.
960 */
961 if (pmb->wsd == INDENT_BLANKLINE)
962 pmb->wsd = delta;
Stefan Bellerca1f4ae2018-07-18 19:31:55963
Phillip Wood72962e82021-12-09 10:30:09964 return delta != pmb->wsd;
Stefan Bellerca1f4ae2018-07-18 19:31:55965}
966
Phillip Wood72962e82021-12-09 10:30:09967struct interned_diff_symbol {
968 struct hashmap_entry ent;
969 struct emitted_diff_symbol *es;
970};
971
972static int interned_diff_symbol_cmp(const void *hashmap_cmp_fn_data,
973 const struct hashmap_entry *eptr,
974 const struct hashmap_entry *entry_or_key,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 17:09:48975 const void *keydata UNUSED)
Stefan Beller2e2d5ac2017-06-30 20:53:07976{
Stefan Belleree1df662018-07-16 23:05:38977 const struct diff_options *diffopt = hashmap_cmp_fn_data;
Phillip Wood25e61902021-12-09 10:30:07978 const struct emitted_diff_symbol *a, *b;
Stefan Bellerb3095712018-07-16 23:05:40979 unsigned flags = diffopt->color_moved_ws_handling
980 & XDF_WHITESPACE_FLAGS;
Stefan Belleree1df662018-07-16 23:05:38981
Phillip Wood72962e82021-12-09 10:30:09982 a = container_of(eptr, const struct interned_diff_symbol, ent)->es;
983 b = container_of(entry_or_key, const struct interned_diff_symbol, ent)->es;
Eric Wong939af162019-10-06 23:30:37984
Phillip Wood25e61902021-12-09 10:30:07985 return !xdiff_compare_lines(a->line + a->indent_off,
986 a->len - a->indent_off,
987 b->line + b->indent_off,
988 b->len - b->indent_off, flags);
Stefan Beller2e2d5ac2017-06-30 20:53:07989}
990
Phillip Wood72962e82021-12-09 10:30:09991static void prepare_entry(struct diff_options *o, struct emitted_diff_symbol *l,
992 struct interned_diff_symbol *s)
Stefan Beller2e2d5ac2017-06-30 20:53:07993{
Stefan Bellerb3095712018-07-16 23:05:40994 unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS;
Phillip Wood25e61902021-12-09 10:30:07995 unsigned int hash = xdiff_hash_string(l->line + l->indent_off,
996 l->len - l->indent_off, flags);
Stefan Beller2e2d5ac2017-06-30 20:53:07997
Phillip Wood72962e82021-12-09 10:30:09998 hashmap_entry_init(&s->ent, hash);
999 s->es = l;
Stefan Beller2e2d5ac2017-06-30 20:53:071000}
1001
Phillip Wood72962e82021-12-09 10:30:091002struct moved_entry_list {
1003 struct moved_entry *add, *del;
1004};
1005
1006static struct moved_entry_list *add_lines_to_move_detection(struct diff_options *o,
1007 struct mem_pool *entry_mem_pool)
Stefan Beller2e2d5ac2017-06-30 20:53:071008{
1009 struct moved_entry *prev_line = NULL;
Phillip Wood72962e82021-12-09 10:30:091010 struct mem_pool interned_pool;
1011 struct hashmap interned_map;
1012 struct moved_entry_list *entry_list = NULL;
1013 size_t entry_list_alloc = 0;
1014 unsigned id = 0;
Stefan Beller2e2d5ac2017-06-30 20:53:071015 int n;
Stefan Beller2e2d5ac2017-06-30 20:53:071016
Phillip Wood72962e82021-12-09 10:30:091017 hashmap_init(&interned_map, interned_diff_symbol_cmp, o, 8096);
1018 mem_pool_init(&interned_pool, 1024 * 1024);
1019
1020 for (n = 0; n < o->emitted_symbols->nr; n++) {
1021 struct interned_diff_symbol key;
1022 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
1023 struct interned_diff_symbol *s;
1024 struct moved_entry *entry;
1025
1026 if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS) {
Stefan Beller2e2d5ac2017-06-30 20:53:071027 prev_line = NULL;
1028 continue;
1029 }
1030
Phillip Wood21536d02018-11-23 11:16:571031 if (o->color_moved_ws_handling &
1032 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
Phillip Wood72962e82021-12-09 10:30:091033 fill_es_indent_data(l);
Stefan Beller2e2d5ac2017-06-30 20:53:071034
Phillip Wood72962e82021-12-09 10:30:091035 prepare_entry(o, l, &key);
1036 s = hashmap_get_entry(&interned_map, &key, ent, &key.ent);
1037 if (s) {
1038 l->id = s->es->id;
1039 } else {
1040 l->id = id;
1041 ALLOC_GROW_BY(entry_list, id, 1, entry_list_alloc);
1042 hashmap_add(&interned_map,
1043 memcpy(mem_pool_alloc(&interned_pool,
1044 sizeof(key)),
1045 &key, sizeof(key)));
1046 }
1047 entry = mem_pool_alloc(entry_mem_pool, sizeof(*entry));
1048 entry->es = l;
1049 entry->next_line = NULL;
1050 if (prev_line && prev_line->es->s == l->s)
1051 prev_line->next_line = entry;
1052 prev_line = entry;
1053 if (l->s == DIFF_SYMBOL_PLUS) {
1054 entry->next_match = entry_list[l->id].add;
1055 entry_list[l->id].add = entry;
1056 } else {
1057 entry->next_match = entry_list[l->id].del;
1058 entry_list[l->id].del = entry;
1059 }
Stefan Beller2e2d5ac2017-06-30 20:53:071060 }
Phillip Wood72962e82021-12-09 10:30:091061
1062 hashmap_clear(&interned_map);
1063 mem_pool_discard(&interned_pool, 0);
1064
1065 return entry_list;
Stefan Beller2e2d5ac2017-06-30 20:53:071066}
1067
Stefan Bellere2fe6ab2018-07-16 23:05:411068static void pmb_advance_or_null(struct diff_options *o,
Phillip Wood08fba102021-12-09 10:30:031069 struct emitted_diff_symbol *l,
Phillip Wood74d156f2018-10-04 10:07:411070 struct moved_block *pmb,
Phillip Wood0e488f12021-12-09 10:30:051071 int *pmb_nr)
Stefan Bellere2fe6ab2018-07-16 23:05:411072{
Phillip Wood0e488f12021-12-09 10:30:051073 int i, j;
Phillip Wood08fba102021-12-09 10:30:031074
Phillip Wood0e488f12021-12-09 10:30:051075 for (i = 0, j = 0; i < *pmb_nr; i++) {
Phillip Woodff046a02021-12-09 10:30:041076 int match;
Phillip Wood74d156f2018-10-04 10:07:411077 struct moved_entry *prev = pmb[i].match;
Stefan Bellere2fe6ab2018-07-16 23:05:411078 struct moved_entry *cur = (prev && prev->next_line) ?
1079 prev->next_line : NULL;
Stefan Bellere2fe6ab2018-07-16 23:05:411080
Phillip Woodff046a02021-12-09 10:30:041081 if (o->color_moved_ws_handling &
1082 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
1083 match = cur &&
1084 !cmp_in_block_with_wsd(cur, l, &pmb[i]);
1085 else
Phillip Wood72962e82021-12-09 10:30:091086 match = cur && cur->es->id == l->id;
1087
Phillip Wood0e488f12021-12-09 10:30:051088 if (match) {
1089 pmb[j] = pmb[i];
1090 pmb[j++].match = cur;
Stefan Beller2e2d5ac2017-06-30 20:53:071091 }
1092 }
Phillip Wood0e488f12021-12-09 10:30:051093 *pmb_nr = j;
Stefan Beller2e2d5ac2017-06-30 20:53:071094}
1095
Phillip Wood7dfe4272021-12-09 10:29:571096static void fill_potential_moved_blocks(struct diff_options *o,
Phillip Wood7dfe4272021-12-09 10:29:571097 struct moved_entry *match,
1098 struct emitted_diff_symbol *l,
1099 struct moved_block **pmb_p,
1100 int *pmb_alloc_p, int *pmb_nr_p)
1101
Stefan Bellerca1f4ae2018-07-18 19:31:551102{
Phillip Wood7dfe4272021-12-09 10:29:571103 struct moved_block *pmb = *pmb_p;
1104 int pmb_alloc = *pmb_alloc_p, pmb_nr = *pmb_nr_p;
Stefan Bellerca1f4ae2018-07-18 19:31:551105
Phillip Wood7dfe4272021-12-09 10:29:571106 /*
1107 * The current line is the start of a new block.
1108 * Setup the set of potential blocks.
1109 */
Phillip Wood72962e82021-12-09 10:30:091110 for (; match; match = match->next_match) {
Phillip Wood7dfe4272021-12-09 10:29:571111 ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
1112 if (o->color_moved_ws_handling &
Phillip Wood25e61902021-12-09 10:30:071113 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
1114 pmb[pmb_nr].wsd = compute_ws_delta(l, match->es);
1115 else
Phillip Wood7dfe4272021-12-09 10:29:571116 pmb[pmb_nr].wsd = 0;
Phillip Wood25e61902021-12-09 10:30:071117 pmb[pmb_nr++].match = match;
Stefan Bellerca1f4ae2018-07-18 19:31:551118 }
1119
Phillip Wood7dfe4272021-12-09 10:29:571120 *pmb_p = pmb;
1121 *pmb_alloc_p = pmb_alloc;
1122 *pmb_nr_p = pmb_nr;
Stefan Beller2e2d5ac2017-06-30 20:53:071123}
1124
Jonathan Tan09153272017-08-16 01:27:381125/*
1126 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
1127 *
Jonathan Tanf0b8fb62017-08-16 01:27:391128 * Otherwise, if the last block has fewer alphanumeric characters than
1129 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
Jonathan Tan09153272017-08-16 01:27:381130 * that block.
1131 *
1132 * The last block consists of the (n - block_length)'th line up to but not
1133 * including the nth line.
Jonathan Tanf0b8fb62017-08-16 01:27:391134 *
Phillip Woodb0a2ba42018-11-23 11:16:551135 * Returns 0 if the last block is empty or is unset by this function, non zero
1136 * otherwise.
1137 *
Jonathan Tanf0b8fb62017-08-16 01:27:391138 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
1139 * Think of a way to unify them.
Jonathan Tan09153272017-08-16 01:27:381140 */
Phillip Woodbea084b2021-12-09 10:29:561141#define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1142 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
Phillip Woodb0a2ba42018-11-23 11:16:551143static int adjust_last_block(struct diff_options *o, int n, int block_length)
Jonathan Tan09153272017-08-16 01:27:381144{
Jonathan Tanf0b8fb62017-08-16 01:27:391145 int i, alnum_count = 0;
1146 if (o->color_moved == COLOR_MOVED_PLAIN)
Phillip Woodb0a2ba42018-11-23 11:16:551147 return block_length;
Jonathan Tanf0b8fb62017-08-16 01:27:391148 for (i = 1; i < block_length + 1; i++) {
1149 const char *c = o->emitted_symbols->buf[n - i].line;
1150 for (; *c; c++) {
1151 if (!isalnum(*c))
1152 continue;
1153 alnum_count++;
1154 if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT)
Phillip Woodb0a2ba42018-11-23 11:16:551155 return 1;
Jonathan Tanf0b8fb62017-08-16 01:27:391156 }
1157 }
Jonathan Tan09153272017-08-16 01:27:381158 for (i = 1; i < block_length + 1; i++)
Phillip Woodbea084b2021-12-09 10:29:561159 o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK;
Phillip Woodb0a2ba42018-11-23 11:16:551160 return 0;
Jonathan Tan09153272017-08-16 01:27:381161}
1162
Stefan Beller2e2d5ac2017-06-30 20:53:071163/* Find blocks of moved code, delegate actual coloring decision to helper */
1164static void mark_color_as_moved(struct diff_options *o,
Phillip Wood72962e82021-12-09 10:30:091165 struct moved_entry_list *entry_list)
Stefan Beller2e2d5ac2017-06-30 20:53:071166{
Phillip Wood74d156f2018-10-04 10:07:411167 struct moved_block *pmb = NULL; /* potentially moved blocks */
Stefan Beller2e2d5ac2017-06-30 20:53:071168 int pmb_nr = 0, pmb_alloc = 0;
Phillip Woodb0a2ba42018-11-23 11:16:551169 int n, flipped_block = 0, block_length = 0;
Phillip Woodeb893522021-12-09 10:30:001170 enum diff_symbol moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER;
Stefan Beller2e2d5ac2017-06-30 20:53:071171
1172
1173 for (n = 0; n < o->emitted_symbols->nr; n++) {
Stefan Beller2e2d5ac2017-06-30 20:53:071174 struct moved_entry *match = NULL;
1175 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
Stefan Beller2e2d5ac2017-06-30 20:53:071176
1177 switch (l->s) {
1178 case DIFF_SYMBOL_PLUS:
Phillip Wood72962e82021-12-09 10:30:091179 match = entry_list[l->id].del;
Stefan Beller2e2d5ac2017-06-30 20:53:071180 break;
1181 case DIFF_SYMBOL_MINUS:
Phillip Wood72962e82021-12-09 10:30:091182 match = entry_list[l->id].add;
Stefan Beller2e2d5ac2017-06-30 20:53:071183 break;
1184 default:
Phillip Woodb0a2ba42018-11-23 11:16:551185 flipped_block = 0;
Stefan Beller2e2d5ac2017-06-30 20:53:071186 }
1187
Phillip Woodeb893522021-12-09 10:30:001188 if (pmb_nr && (!match || l->s != moved_symbol)) {
Phillip Wood09906582021-12-09 10:29:581189 if (!adjust_last_block(o, n, block_length) &&
1190 block_length > 1) {
1191 /*
1192 * Rewind in case there is another match
1193 * starting at the second line of the block
1194 */
1195 match = NULL;
1196 n -= block_length;
1197 }
Stefan Beller2e2d5ac2017-06-30 20:53:071198 pmb_nr = 0;
1199 block_length = 0;
Phillip Woodb0a2ba42018-11-23 11:16:551200 flipped_block = 0;
Phillip Woodeb893522021-12-09 10:30:001201 }
1202 if (!match) {
1203 moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER;
Stefan Beller2e2d5ac2017-06-30 20:53:071204 continue;
1205 }
1206
Phillip Wood2034b472018-11-23 11:16:541207 if (o->color_moved == COLOR_MOVED_PLAIN) {
1208 l->flags |= DIFF_SYMBOL_MOVED_LINE;
Stefan Beller176841f2017-06-30 20:53:081209 continue;
Phillip Wood2034b472018-11-23 11:16:541210 }
Stefan Beller176841f2017-06-30 20:53:081211
Phillip Wood0e488f12021-12-09 10:30:051212 pmb_advance_or_null(o, l, pmb, &pmb_nr);
Stefan Beller2e2d5ac2017-06-30 20:53:071213
1214 if (pmb_nr == 0) {
Phillip Wood09906582021-12-09 10:29:581215 int contiguous = adjust_last_block(o, n, block_length);
Stefan Beller2e2d5ac2017-06-30 20:53:071216
Phillip Wood09906582021-12-09 10:29:581217 if (!contiguous && block_length > 1)
1218 /*
1219 * Rewind in case there is another match
1220 * starting at the second line of the block
1221 */
1222 n -= block_length;
1223 else
Phillip Wood72962e82021-12-09 10:30:091224 fill_potential_moved_blocks(o, match, l,
Phillip Wood09906582021-12-09 10:29:581225 &pmb, &pmb_alloc,
1226 &pmb_nr);
1227
Phillip Woodeb893522021-12-09 10:30:001228 if (contiguous && pmb_nr && moved_symbol == l->s)
Phillip Woodb0a2ba42018-11-23 11:16:551229 flipped_block = (flipped_block + 1) % 2;
1230 else
1231 flipped_block = 0;
Jonathan Tanf0b8fb62017-08-16 01:27:391232
Phillip Woodeb893522021-12-09 10:30:001233 if (pmb_nr)
1234 moved_symbol = l->s;
1235 else
1236 moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER;
1237
Jonathan Tanf0b8fb62017-08-16 01:27:391238 block_length = 0;
Stefan Beller2e2d5ac2017-06-30 20:53:071239 }
1240
Phillip Wood2034b472018-11-23 11:16:541241 if (pmb_nr) {
1242 block_length++;
Phillip Wood2034b472018-11-23 11:16:541243 l->flags |= DIFF_SYMBOL_MOVED_LINE;
1244 if (flipped_block && o->color_moved != COLOR_MOVED_BLOCKS)
1245 l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
1246 }
Stefan Beller2e2d5ac2017-06-30 20:53:071247 }
Jonathan Tan09153272017-08-16 01:27:381248 adjust_last_block(o, n, block_length);
Stefan Beller2e2d5ac2017-06-30 20:53:071249
1250 free(pmb);
1251}
Stefan Bellere6e045f2017-06-30 00:07:061252
Stefan Beller86b452e2017-06-30 20:53:091253static void dim_moved_lines(struct diff_options *o)
1254{
1255 int n;
1256 for (n = 0; n < o->emitted_symbols->nr; n++) {
1257 struct emitted_diff_symbol *prev = (n != 0) ?
1258 &o->emitted_symbols->buf[n - 1] : NULL;
1259 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
1260 struct emitted_diff_symbol *next =
1261 (n < o->emitted_symbols->nr - 1) ?
1262 &o->emitted_symbols->buf[n + 1] : NULL;
1263
1264 /* Not a plus or minus line? */
1265 if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS)
1266 continue;
1267
1268 /* Not a moved line? */
1269 if (!(l->flags & DIFF_SYMBOL_MOVED_LINE))
1270 continue;
1271
1272 /*
1273 * If prev or next are not a plus or minus line,
1274 * pretend they don't exist
1275 */
1276 if (prev && prev->s != DIFF_SYMBOL_PLUS &&
1277 prev->s != DIFF_SYMBOL_MINUS)
1278 prev = NULL;
1279 if (next && next->s != DIFF_SYMBOL_PLUS &&
1280 next->s != DIFF_SYMBOL_MINUS)
1281 next = NULL;
1282
1283 /* Inside a block? */
1284 if ((prev &&
1285 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1286 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) &&
1287 (next &&
1288 (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1289 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) {
1290 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1291 continue;
1292 }
1293
1294 /* Check if we are at an interesting bound: */
1295 if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) &&
1296 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1297 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1298 continue;
1299 if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) &&
1300 (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1301 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1302 continue;
1303
1304 /*
1305 * The boundary to prev and next are not interesting,
1306 * so this line is not interesting as a whole
1307 */
1308 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1309 }
1310}
1311
Stefan Beller091f8e22017-06-30 00:06:531312static void emit_line_ws_markup(struct diff_options *o,
Stefan Beller9d1e16b2018-08-14 01:41:181313 const char *set_sign, const char *set,
1314 const char *reset,
Stefan Bellerbc9feb02018-10-10 23:24:591315 int sign_index, const char *line, int len,
Stefan Beller091f8e22017-06-30 00:06:531316 unsigned ws_rule, int blank_at_eof)
1317{
1318 const char *ws = NULL;
Stefan Bellerbc9feb02018-10-10 23:24:591319 int sign = o->output_indicators[sign_index];
Stefan Beller091f8e22017-06-30 00:06:531320
1321 if (o->ws_error_highlight & ws_rule) {
1322 ws = diff_get_color_opt(o, DIFF_WHITESPACE);
1323 if (!*ws)
1324 ws = NULL;
1325 }
1326
Johannes Schindelinf7c3b4e2018-08-13 11:33:201327 if (!ws && !set_sign)
Stefan Beller017ac452018-08-14 01:41:191328 emit_line_0(o, set, NULL, 0, reset, sign, line, len);
Johannes Schindelinf7c3b4e2018-08-13 11:33:201329 else if (!ws) {
Stefan Beller29ef7592018-08-14 01:41:201330 emit_line_0(o, set_sign, set, !!set_sign, reset, sign, line, len);
Johannes Schindelinf7c3b4e2018-08-13 11:33:201331 } else if (blank_at_eof)
Stefan Beller091f8e22017-06-30 00:06:531332 /* Blank line at EOF - paint '+' as well */
Stefan Beller017ac452018-08-14 01:41:191333 emit_line_0(o, ws, NULL, 0, reset, sign, line, len);
Stefan Beller091f8e22017-06-30 00:06:531334 else {
1335 /* Emit just the prefix, then the rest. */
Stefan Beller017ac452018-08-14 01:41:191336 emit_line_0(o, set_sign ? set_sign : set, NULL, !!set_sign, reset,
Johannes Schindelinf7c3b4e2018-08-13 11:33:201337 sign, "", 0);
Stefan Beller091f8e22017-06-30 00:06:531338 ws_check_emit(line, len, ws_rule,
1339 o->file, set, reset, ws);
1340 }
1341}
Stefan Beller36a4cef2017-06-30 00:06:491342
Stefan Bellere6e045f2017-06-30 00:07:061343static void emit_diff_symbol_from_struct(struct diff_options *o,
1344 struct emitted_diff_symbol *eds)
Stefan Beller36a4cef2017-06-30 00:06:491345{
Stefan Bellerb9cbfde2017-06-30 00:06:521346 static const char *nneof = " No newline at end of file\n";
Johannes Schindelinf7c3b4e2018-08-13 11:33:201347 const char *context, *reset, *set, *set_sign, *meta, *fraginfo;
Stefan Bellere6e045f2017-06-30 00:07:061348
1349 enum diff_symbol s = eds->s;
1350 const char *line = eds->line;
1351 int len = eds->len;
1352 unsigned flags = eds->flags;
1353
Jeff Kingb2b5ad52025-10-24 17:08:531354 if (!o->file)
Lidong Yan3ed5d8b2025-10-19 16:30:241355 return;
1356
Stefan Beller36a4cef2017-06-30 00:06:491357 switch (s) {
Stefan Bellerb9cbfde2017-06-30 00:06:521358 case DIFF_SYMBOL_NO_LF_EOF:
1359 context = diff_get_color_opt(o, DIFF_CONTEXT);
1360 reset = diff_get_color_opt(o, DIFF_RESET);
1361 putc('\n', o->file);
Stefan Beller017ac452018-08-14 01:41:191362 emit_line_0(o, context, NULL, 0, reset, '\\',
Stefan Bellerb9cbfde2017-06-30 00:06:521363 nneof, strlen(nneof));
1364 break;
Stefan Bellerf3597132017-06-30 00:07:001365 case DIFF_SYMBOL_SUBMODULE_HEADER:
1366 case DIFF_SYMBOL_SUBMODULE_ERROR:
1367 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
Stefan Beller0911c472017-06-30 00:07:021368 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
Stefan Beller146fdb02017-06-30 00:07:051369 case DIFF_SYMBOL_SUMMARY:
Stefan Beller0911c472017-06-30 00:07:021370 case DIFF_SYMBOL_STATS_LINE:
Stefan Beller4eed0eb2017-06-30 00:07:011371 case DIFF_SYMBOL_BINARY_DIFF_BODY:
Stefan Beller68abc6f2017-06-30 00:06:511372 case DIFF_SYMBOL_CONTEXT_FRAGINFO:
1373 emit_line(o, "", "", line, len);
1374 break;
Stefan Bellerf2bb1212017-06-30 00:06:551375 case DIFF_SYMBOL_CONTEXT_INCOMPLETE:
Stefan Bellerc64b4202017-06-30 00:06:501376 case DIFF_SYMBOL_CONTEXT_MARKER:
1377 context = diff_get_color_opt(o, DIFF_CONTEXT);
1378 reset = diff_get_color_opt(o, DIFF_RESET);
1379 emit_line(o, context, reset, line, len);
1380 break;
Stefan Beller36a4cef2017-06-30 00:06:491381 case DIFF_SYMBOL_SEPARATOR:
1382 fprintf(o->file, "%s%c",
1383 diff_line_prefix(o),
1384 o->line_termination);
1385 break;
Stefan Beller091f8e22017-06-30 00:06:531386 case DIFF_SYMBOL_CONTEXT:
1387 set = diff_get_color_opt(o, DIFF_CONTEXT);
1388 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 11:33:201389 set_sign = NULL;
1390 if (o->flags.dual_color_diffed_diffs) {
1391 char c = !len ? 0 : line[0];
1392
1393 if (c == '+')
1394 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1395 else if (c == '@')
1396 set = diff_get_color_opt(o, DIFF_FRAGINFO);
1397 else if (c == '-')
1398 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1399 }
Stefan Beller7648b792018-08-17 20:43:521400 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 23:24:591401 OUTPUT_INDICATOR_CONTEXT, line, len,
Stefan Beller091f8e22017-06-30 00:06:531402 flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
1403 break;
1404 case DIFF_SYMBOL_PLUS:
Stefan Beller86b452e2017-06-30 20:53:091405 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_NEW_MOVED_ALT_DIM);
1412 break;
1413 case DIFF_SYMBOL_MOVED_LINE |
1414 DIFF_SYMBOL_MOVED_LINE_ALT:
Stefan Beller2e2d5ac2017-06-30 20:53:071415 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT);
Stefan Beller86b452e2017-06-30 20:53:091416 break;
1417 case DIFF_SYMBOL_MOVED_LINE |
1418 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1419 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM);
1420 break;
1421 case DIFF_SYMBOL_MOVED_LINE:
Stefan Beller2e2d5ac2017-06-30 20:53:071422 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED);
Stefan Beller86b452e2017-06-30 20:53:091423 break;
1424 default:
Stefan Beller2e2d5ac2017-06-30 20:53:071425 set = diff_get_color_opt(o, DIFF_FILE_NEW);
Stefan Beller86b452e2017-06-30 20:53:091426 }
Stefan Beller091f8e22017-06-30 00:06:531427 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 11:33:201428 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 11:33:321435 set = diff_get_color_opt(o, DIFF_FILE_OLD_BOLD);
Johannes Schindelinf7c3b4e2018-08-13 11:33:201436 else if (c == '@')
1437 set = diff_get_color_opt(o, DIFF_FRAGINFO);
Johannes Schindelina7be92a2018-08-13 11:33:321438 else if (c == '+')
1439 set = diff_get_color_opt(o, DIFF_FILE_NEW_BOLD);
1440 else
1441 set = diff_get_color_opt(o, DIFF_CONTEXT_BOLD);
Johannes Schindelin0b91faa2018-08-13 11:33:241442 flags &= ~DIFF_SYMBOL_CONTENT_WS_MASK;
Johannes Schindelinf7c3b4e2018-08-13 11:33:201443 }
Stefan Beller7648b792018-08-17 20:43:521444 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 23:24:591445 OUTPUT_INDICATOR_NEW, line, len,
Stefan Beller091f8e22017-06-30 00:06:531446 flags & DIFF_SYMBOL_CONTENT_WS_MASK,
1447 flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
1448 break;
1449 case DIFF_SYMBOL_MINUS:
Stefan Beller86b452e2017-06-30 20:53:091450 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1451 DIFF_SYMBOL_MOVED_LINE_ALT |
1452 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1453 case DIFF_SYMBOL_MOVED_LINE |
1454 DIFF_SYMBOL_MOVED_LINE_ALT |
1455 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1456 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM);
1457 break;
1458 case DIFF_SYMBOL_MOVED_LINE |
1459 DIFF_SYMBOL_MOVED_LINE_ALT:
Stefan Beller2e2d5ac2017-06-30 20:53:071460 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT);
Stefan Beller86b452e2017-06-30 20:53:091461 break;
1462 case DIFF_SYMBOL_MOVED_LINE |
1463 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1464 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM);
1465 break;
1466 case DIFF_SYMBOL_MOVED_LINE:
Stefan Beller2e2d5ac2017-06-30 20:53:071467 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED);
Stefan Beller86b452e2017-06-30 20:53:091468 break;
1469 default:
Stefan Beller2e2d5ac2017-06-30 20:53:071470 set = diff_get_color_opt(o, DIFF_FILE_OLD);
Stefan Beller86b452e2017-06-30 20:53:091471 }
Stefan Beller091f8e22017-06-30 00:06:531472 reset = diff_get_color_opt(o, DIFF_RESET);
Johannes Schindelinf7c3b4e2018-08-13 11:33:201473 if (!o->flags.dual_color_diffed_diffs)
1474 set_sign = NULL;
1475 else {
1476 char c = !len ? 0 : line[0];
1477
1478 set_sign = set;
1479 if (c == '+')
Johannes Schindelina7be92a2018-08-13 11:33:321480 set = diff_get_color_opt(o, DIFF_FILE_NEW_DIM);
Johannes Schindelinf7c3b4e2018-08-13 11:33:201481 else if (c == '@')
1482 set = diff_get_color_opt(o, DIFF_FRAGINFO);
Johannes Schindelina7be92a2018-08-13 11:33:321483 else if (c == '-')
1484 set = diff_get_color_opt(o, DIFF_FILE_OLD_DIM);
1485 else
1486 set = diff_get_color_opt(o, DIFF_CONTEXT_DIM);
Johannes Schindelinf7c3b4e2018-08-13 11:33:201487 }
Stefan Beller7648b792018-08-17 20:43:521488 emit_line_ws_markup(o, set_sign, set, reset,
Stefan Bellerbc9feb02018-10-10 23:24:591489 OUTPUT_INDICATOR_OLD, line, len,
Stefan Beller091f8e22017-06-30 00:06:531490 flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
1491 break;
Stefan Bellerff958672017-06-30 00:06:541492 case DIFF_SYMBOL_WORDS_PORCELAIN:
1493 context = diff_get_color_opt(o, DIFF_CONTEXT);
1494 reset = diff_get_color_opt(o, DIFF_RESET);
1495 emit_line(o, context, reset, line, len);
1496 fputs("~\n", o->file);
1497 break;
1498 case DIFF_SYMBOL_WORDS:
1499 context = diff_get_color_opt(o, DIFF_CONTEXT);
1500 reset = diff_get_color_opt(o, DIFF_RESET);
1501 /*
1502 * Skip the prefix character, if any. With
1503 * diff_suppress_blank_empty, there may be
1504 * none.
1505 */
1506 if (line[0] != '\n') {
1507 line++;
1508 len--;
1509 }
1510 emit_line(o, context, reset, line, len);
1511 break;
Stefan Beller3ee8b7b2017-06-30 00:06:561512 case DIFF_SYMBOL_FILEPAIR_PLUS:
1513 meta = diff_get_color_opt(o, DIFF_METAINFO);
1514 reset = diff_get_color_opt(o, DIFF_RESET);
1515 fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
1516 line, reset,
1517 strchr(line, ' ') ? "\t" : "");
1518 break;
1519 case DIFF_SYMBOL_FILEPAIR_MINUS:
1520 meta = diff_get_color_opt(o, DIFF_METAINFO);
1521 reset = diff_get_color_opt(o, DIFF_RESET);
1522 fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta,
1523 line, reset,
1524 strchr(line, ' ') ? "\t" : "");
1525 break;
Stefan Beller4acaaa72017-06-30 00:06:581526 case DIFF_SYMBOL_BINARY_FILES:
Stefan Bellera29b0a12017-06-30 00:06:571527 case DIFF_SYMBOL_HEADER:
1528 fprintf(o->file, "%s", line);
1529 break;
Stefan Beller4eed0eb2017-06-30 00:07:011530 case DIFF_SYMBOL_BINARY_DIFF_HEADER:
1531 fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o));
1532 break;
1533 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA:
1534 fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line);
1535 break;
1536 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL:
1537 fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line);
1538 break;
1539 case DIFF_SYMBOL_BINARY_DIFF_FOOTER:
1540 fputs(diff_line_prefix(o), o->file);
1541 fputc('\n', o->file);
1542 break;
Stefan Beller5af6ea92017-06-30 00:06:591543 case DIFF_SYMBOL_REWRITE_DIFF:
1544 fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
1545 reset = diff_get_color_opt(o, DIFF_RESET);
1546 emit_line(o, fraginfo, reset, line, len);
1547 break;
Stefan Bellerf3597132017-06-30 00:07:001548 case DIFF_SYMBOL_SUBMODULE_ADD:
1549 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1550 reset = diff_get_color_opt(o, DIFF_RESET);
1551 emit_line(o, set, reset, line, len);
1552 break;
1553 case DIFF_SYMBOL_SUBMODULE_DEL:
1554 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1555 reset = diff_get_color_opt(o, DIFF_RESET);
1556 emit_line(o, set, reset, line, len);
1557 break;
1558 case DIFF_SYMBOL_SUBMODULE_UNTRACKED:
1559 fprintf(o->file, "%sSubmodule %s contains untracked content\n",
1560 diff_line_prefix(o), line);
1561 break;
1562 case DIFF_SYMBOL_SUBMODULE_MODIFIED:
1563 fprintf(o->file, "%sSubmodule %s contains modified content\n",
1564 diff_line_prefix(o), line);
1565 break;
Stefan Beller0911c472017-06-30 00:07:021566 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES:
1567 emit_line(o, "", "", " 0 files changed\n",
1568 strlen(" 0 files changed\n"));
1569 break;
1570 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV:
1571 emit_line(o, "", "", " ...\n", strlen(" ...\n"));
1572 break;
Stefan Bellerbd033292017-06-30 00:07:031573 case DIFF_SYMBOL_WORD_DIFF:
1574 fprintf(o->file, "%.*s", len, line);
1575 break;
Stefan Beller30b7e1e2017-06-30 00:07:041576 case DIFF_SYMBOL_STAT_SEP:
1577 fputs(o->stat_sep, o->file);
1578 break;
Stefan Beller36a4cef2017-06-30 00:06:491579 default:
Johannes Schindelin033abf92018-05-02 09:38:391580 BUG("unknown diff symbol");
Stefan Beller36a4cef2017-06-30 00:06:491581 }
1582}
1583
Stefan Bellere6e045f2017-06-30 00:07:061584static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
1585 const char *line, int len, unsigned flags)
1586{
Phillip Woodb4a5c5c2021-12-09 10:30:081587 struct emitted_diff_symbol e = {
1588 .line = line, .len = len, .flags = flags, .s = s
1589 };
Stefan Bellere6e045f2017-06-30 00:07:061590
1591 if (o->emitted_symbols)
1592 append_emitted_diff_symbol(o, &e);
1593 else
1594 emit_diff_symbol_from_struct(o, &e);
1595}
1596
Stefan Bellerf3597132017-06-30 00:07:001597void diff_emit_submodule_del(struct diff_options *o, const char *line)
1598{
1599 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0);
1600}
1601
1602void diff_emit_submodule_add(struct diff_options *o, const char *line)
1603{
1604 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0);
1605}
1606
1607void diff_emit_submodule_untracked(struct diff_options *o, const char *path)
1608{
1609 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED,
1610 path, strlen(path), 0);
1611}
1612
1613void diff_emit_submodule_modified(struct diff_options *o, const char *path)
1614{
1615 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED,
1616 path, strlen(path), 0);
1617}
1618
1619void diff_emit_submodule_header(struct diff_options *o, const char *header)
1620{
1621 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER,
1622 header, strlen(header), 0);
1623}
1624
1625void diff_emit_submodule_error(struct diff_options *o, const char *err)
1626{
1627 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0);
1628}
1629
1630void diff_emit_submodule_pipethrough(struct diff_options *o,
1631 const char *line, int len)
1632{
1633 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0);
1634}
1635
Junio C Hamano6957eb92009-09-15 01:44:011636static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
1637{
1638 if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
1639 ecbdata->blank_at_eof_in_preimage &&
1640 ecbdata->blank_at_eof_in_postimage &&
1641 ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
1642 ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
1643 return 0;
Jeff Kingc5224f02022-12-13 11:12:581644 return ws_blank_line(line, len);
Junio C Hamano6957eb92009-09-15 01:44:011645}
1646
Jeff Kinge04df612019-02-14 05:48:131647static void emit_add_line(struct emit_callback *ecbdata,
Junio C Hamano018cff72009-09-15 01:44:011648 const char *line, int len)
Junio C Hamano6957eb92009-09-15 01:44:011649{
Stefan Beller091f8e22017-06-30 00:06:531650 unsigned flags = WSEH_NEW | ecbdata->ws_rule;
1651 if (new_blank_line_at_eof(ecbdata, line, len))
1652 flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF;
1653
1654 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags);
Junio C Hamano6957eb92009-09-15 01:44:011655}
Junio C Hamano6957eb92009-09-15 01:44:011656
Jeff Kinge04df612019-02-14 05:48:131657static void emit_del_line(struct emit_callback *ecbdata,
Junio C Hamano0e383e12015-05-26 16:56:331658 const char *line, int len)
1659{
Stefan Beller091f8e22017-06-30 00:06:531660 unsigned flags = WSEH_OLD | ecbdata->ws_rule;
1661 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags);
Junio C Hamano0e383e12015-05-26 16:56:331662}
1663
Jeff Kinge04df612019-02-14 05:48:131664static void emit_context_line(struct emit_callback *ecbdata,
Junio C Hamano0e383e12015-05-26 16:56:331665 const char *line, int len)
1666{
Stefan Beller091f8e22017-06-30 00:06:531667 unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule;
1668 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags);
Junio C Hamano6957eb92009-09-15 01:44:011669}
1670
Bert Wesarg89cb73a2009-11-27 06:55:181671static void emit_hunk_header(struct emit_callback *ecbdata,
1672 const char *line, int len)
1673{
Jeff King8dbf3eb2015-05-27 20:48:461674 const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT);
Bert Wesarg89cb73a2009-11-27 06:55:181675 const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
1676 const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
1677 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
Jeff King955000d2025-09-16 20:21:201678 const char *reverse = want_color(ecbdata->color_diff) ? GIT_COLOR_REVERSE : "";
Bert Wesarg89cb73a2009-11-27 06:55:181679 static const char atat[2] = { '@', '@' };
1680 const char *cp, *ep;
Bo Yang2efcc972010-05-29 15:32:051681 struct strbuf msgbuf = STRBUF_INIT;
1682 int org_len = len;
1683 int i = 1;
Bert Wesarg89cb73a2009-11-27 06:55:181684
1685 /*
1686 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1687 * it always is at least 10 bytes long.
1688 */
1689 if (len < 10 ||
1690 memcmp(line, atat, 2) ||
1691 !(ep = memmem(line + 2, len - 2, atat, 2))) {
Stefan Bellerc64b4202017-06-30 00:06:501692 emit_diff_symbol(ecbdata->opt,
Stefan Beller091f8e22017-06-30 00:06:531693 DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
Bert Wesarg89cb73a2009-11-27 06:55:181694 return;
1695 }
1696 ep += 2; /* skip over @@ */
1697
1698 /* The hunk header in fraginfo color */
Johannes Schindelinf7c3b4e2018-08-13 11:33:201699 if (ecbdata->opt->flags.dual_color_diffed_diffs)
1700 strbuf_addstr(&msgbuf, reverse);
René Scharfecedc61a2014-07-16 23:38:181701 strbuf_addstr(&msgbuf, frag);
Thomas Gummerer430be362019-07-11 16:08:481702 if (ecbdata->opt->flags.suppress_hunk_header_line_count)
1703 strbuf_add(&msgbuf, atat, sizeof(atat));
1704 else
1705 strbuf_add(&msgbuf, line, ep - line);
René Scharfecedc61a2014-07-16 23:38:181706 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 15:32:051707
1708 /*
1709 * trailing "\r\n"
1710 */
1711 for ( ; i < 3; i++)
1712 if (line[len - i] == '\r' || line[len - i] == '\n')
1713 len--;
Bert Wesarg89cb73a2009-11-27 06:55:181714
1715 /* blank before the func header */
1716 for (cp = ep; ep - line < len; ep++)
1717 if (*ep != ' ' && *ep != '\t')
1718 break;
Bo Yang2efcc972010-05-29 15:32:051719 if (ep != cp) {
Jeff King8dbf3eb2015-05-27 20:48:461720 strbuf_addstr(&msgbuf, context);
Bo Yang2efcc972010-05-29 15:32:051721 strbuf_add(&msgbuf, cp, ep - cp);
René Scharfecedc61a2014-07-16 23:38:181722 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 15:32:051723 }
Bert Wesarg89cb73a2009-11-27 06:55:181724
Bo Yang2efcc972010-05-29 15:32:051725 if (ep < line + len) {
René Scharfecedc61a2014-07-16 23:38:181726 strbuf_addstr(&msgbuf, func);
Bo Yang2efcc972010-05-29 15:32:051727 strbuf_add(&msgbuf, ep, line + len - ep);
René Scharfecedc61a2014-07-16 23:38:181728 strbuf_addstr(&msgbuf, reset);
Bo Yang2efcc972010-05-29 15:32:051729 }
1730
1731 strbuf_add(&msgbuf, line + len, org_len - len);
Stefan Bellerdfb77282017-06-30 00:06:471732 strbuf_complete_line(&msgbuf);
Stefan Beller68abc6f2017-06-30 00:06:511733 emit_diff_symbol(ecbdata->opt,
Stefan Beller091f8e22017-06-30 00:06:531734 DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0);
Bo Yang2efcc972010-05-29 15:32:051735 strbuf_release(&msgbuf);
Bert Wesarg89cb73a2009-11-27 06:55:181736}
1737
Nguyễn Thái Ngọc Duy3b335762018-12-09 10:25:211738static struct diff_tempfile *claim_diff_tempfile(void)
1739{
Jeff King479b0ae2009-01-22 05:59:561740 int i;
1741 for (i = 0; i < ARRAY_SIZE(diff_temp); i++)
1742 if (!diff_temp[i].name)
1743 return diff_temp + i;
Johannes Schindelin033abf92018-05-02 09:38:391744 BUG("diff is failing to clean up its tempfiles");
Jeff King479b0ae2009-01-22 05:59:561745}
1746
Jeff King479b0ae2009-01-22 05:59:561747static void remove_tempfile(void)
1748{
1749 int i;
Nazri Ramliya8344ab2009-02-12 13:36:141750 for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
Jeff King076aa2c2017-09-05 12:15:081751 if (is_tempfile_active(diff_temp[i].tempfile))
Michael Haggerty284098f2015-08-12 17:12:011752 delete_tempfile(&diff_temp[i].tempfile);
Nazri Ramliya8344ab2009-02-12 13:36:141753 diff_temp[i].name = NULL;
1754 }
Jeff King479b0ae2009-01-22 05:59:561755}
1756
Stefan Beller5af6ea92017-06-30 00:06:591757static void add_line_count(struct strbuf *out, int count)
Junio C Hamano6973dca2006-04-22 06:57:451758{
1759 switch (count) {
1760 case 0:
Stefan Beller5af6ea92017-06-30 00:06:591761 strbuf_addstr(out, "0,0");
Junio C Hamano6973dca2006-04-22 06:57:451762 break;
1763 case 1:
Stefan Beller5af6ea92017-06-30 00:06:591764 strbuf_addstr(out, "1");
Junio C Hamano6973dca2006-04-22 06:57:451765 break;
1766 default:
Stefan Beller5af6ea92017-06-30 00:06:591767 strbuf_addf(out, "1,%d", count);
Junio C Hamano6973dca2006-04-22 06:57:451768 break;
1769 }
1770}
1771
Junio C Hamano7f7ee2f2009-09-15 01:44:011772static void emit_rewrite_lines(struct emit_callback *ecb,
1773 int prefix, const char *data, int size)
Junio C Hamano6973dca2006-04-22 06:57:451774{
Junio C Hamano7f7ee2f2009-09-15 01:44:011775 const char *endp = NULL;
Junio C Hamano7f7ee2f2009-09-15 01:44:011776
1777 while (0 < size) {
1778 int len;
1779
1780 endp = memchr(data, '\n', size);
1781 len = endp ? (endp - data + 1) : size;
1782 if (prefix != '+') {
1783 ecb->lno_in_preimage++;
Jeff Kinge04df612019-02-14 05:48:131784 emit_del_line(ecb, data, len);
Junio C Hamano7f7ee2f2009-09-15 01:44:011785 } else {
1786 ecb->lno_in_postimage++;
Jeff Kinge04df612019-02-14 05:48:131787 emit_add_line(ecb, data, len);
Johannes Schindelin13e36ec2007-02-20 14:08:461788 }
Junio C Hamano7f7ee2f2009-09-15 01:44:011789 size -= len;
1790 data += len;
Junio C Hamano6973dca2006-04-22 06:57:451791 }
Stefan Bellerb9cbfde2017-06-30 00:06:521792 if (!endp)
Stefan Beller091f8e22017-06-30 00:06:531793 emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
Junio C Hamano6973dca2006-04-22 06:57:451794}
1795
1796static void emit_rewrite_diff(const char *name_a,
1797 const char *name_b,
1798 struct diff_filespec *one,
Johannes Schindelin13e36ec2007-02-20 14:08:461799 struct diff_filespec *two,
Jeff Kingd9bae1a2010-04-02 00:12:151800 struct userdiff_driver *textconv_one,
1801 struct userdiff_driver *textconv_two,
Johannes Schindelineab9a402007-12-18 19:32:141802 struct diff_options *o)
Junio C Hamano6973dca2006-04-22 06:57:451803{
1804 int lc_a, lc_b;
Junio C Hamanod5625092007-12-27 01:13:361805 static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
Junio C Hamanoa5a818e2008-08-19 03:08:091806 const char *a_prefix, *b_prefix;
Jeff King840383b2010-04-02 00:09:261807 char *data_one, *data_two;
Jeff King3aa1f7c2008-12-09 08:13:211808 size_t size_one, size_two;
Junio C Hamano7f7ee2f2009-09-15 01:44:011809 struct emit_callback ecbdata;
Stefan Beller5af6ea92017-06-30 00:06:591810 struct strbuf out = STRBUF_INIT;
Junio C Hamanoa5a818e2008-08-19 03:08:091811
Brandon Williams0d1e0e72017-10-31 18:19:111812 if (diff_mnemonic_prefix && o->flags.reverse_diff) {
Junio C Hamanoa5a818e2008-08-19 03:08:091813 a_prefix = o->b_prefix;
1814 b_prefix = o->a_prefix;
1815 } else {
1816 a_prefix = o->a_prefix;
1817 b_prefix = o->b_prefix;
1818 }
Junio C Hamano1a9eb3b2006-09-22 23:17:581819
Junio C Hamano8a13bec2007-02-24 09:42:061820 name_a += (*name_a == '/');
1821 name_b += (*name_b == '/');
Junio C Hamano1a9eb3b2006-09-22 23:17:581822
Junio C Hamanod5625092007-12-27 01:13:361823 strbuf_reset(&a_name);
1824 strbuf_reset(&b_name);
Junio C Hamanoa5a818e2008-08-19 03:08:091825 quote_two_c_style(&a_name, a_prefix, name_a, 0);
1826 quote_two_c_style(&b_name, b_prefix, name_b, 0);
Junio C Hamanod5625092007-12-27 01:13:361827
Nguyễn Thái Ngọc Duy6afaf802018-09-21 15:57:221828 size_one = fill_textconv(o->repo, textconv_one, one, &data_one);
1829 size_two = fill_textconv(o->repo, textconv_two, two, &data_two);
Jeff King3aa1f7c2008-12-09 08:13:211830
Junio C Hamanod91ba8f2009-09-15 18:21:101831 memset(&ecbdata, 0, sizeof(ecbdata));
Jeff King955000d2025-09-16 20:21:201832 ecbdata.color_diff = o->use_color;
Nguyễn Thái Ngọc Duy26d024e2018-09-21 15:57:371833 ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
Bo Yanga3c158d2010-05-26 07:08:021834 ecbdata.opt = o;
Junio C Hamanod91ba8f2009-09-15 18:21:101835 if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
1836 mmfile_t mf1, mf2;
1837 mf1.ptr = (char *)data_one;
1838 mf2.ptr = (char *)data_two;
1839 mf1.size = size_one;
1840 mf2.size = size_two;
1841 check_blank_at_eof(&mf1, &mf2, &ecbdata);
1842 }
1843 ecbdata.lno_in_preimage = 1;
1844 ecbdata.lno_in_postimage = 1;
1845
Jeff King3aa1f7c2008-12-09 08:13:211846 lc_a = count_lines(data_one, size_one);
1847 lc_b = count_lines(data_two, size_two);
Stefan Beller3ee8b7b2017-06-30 00:06:561848
1849 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
1850 a_name.buf, a_name.len, 0);
1851 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
1852 b_name.buf, b_name.len, 0);
1853
Stefan Beller5af6ea92017-06-30 00:06:591854 strbuf_addstr(&out, "@@ -");
Junio C Hamano467ddc12011-03-01 00:11:551855 if (!o->irreversible_delete)
Stefan Beller5af6ea92017-06-30 00:06:591856 add_line_count(&out, lc_a);
Junio C Hamano467ddc12011-03-01 00:11:551857 else
Stefan Beller5af6ea92017-06-30 00:06:591858 strbuf_addstr(&out, "?,?");
1859 strbuf_addstr(&out, " +");
1860 add_line_count(&out, lc_b);
1861 strbuf_addstr(&out, " @@\n");
1862 emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
1863 strbuf_release(&out);
1864
Junio C Hamano467ddc12011-03-01 00:11:551865 if (lc_a && !o->irreversible_delete)
Junio C Hamanod91ba8f2009-09-15 18:21:101866 emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
Junio C Hamano6973dca2006-04-22 06:57:451867 if (lc_b)
Junio C Hamanod91ba8f2009-09-15 18:21:101868 emit_rewrite_lines(&ecbdata, '+', data_two, size_two);
Jeff Kingb76c0562010-04-02 00:04:141869 if (textconv_one)
Junio C Hamanoaed6ca52010-04-09 06:30:491870 free((char *)data_one);
Jeff Kingb76c0562010-04-02 00:04:141871 if (textconv_two)
Junio C Hamanoaed6ca52010-04-09 06:30:491872 free((char *)data_two);
Junio C Hamano6973dca2006-04-22 06:57:451873}
1874
Johannes Schindelinf59a59e2006-07-28 21:56:151875struct diff_words_buffer {
1876 mmfile_t text;
Ramsay Jones071bcaa2017-09-21 16:49:381877 unsigned long alloc;
Johannes Schindelin2e5d2002009-01-17 16:29:441878 struct diff_words_orig {
1879 const char *begin, *end;
1880 } *orig;
1881 int orig_nr, orig_alloc;
Johannes Schindelinf59a59e2006-07-28 21:56:151882};
1883
1884static void diff_words_append(char *line, unsigned long len,
1885 struct diff_words_buffer *buffer)
1886{
Johannes Schindelin23c15752009-01-17 16:29:431887 ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc);
Johannes Schindelinf59a59e2006-07-28 21:56:151888 line++;
1889 len--;
1890 memcpy(buffer->text.ptr + buffer->text.size, line, len);
1891 buffer->text.size += len;
Johannes Schindelin2b6a5412009-01-17 16:29:451892 buffer->text.ptr[buffer->text.size] = '\0';
Johannes Schindelinf59a59e2006-07-28 21:56:151893}
1894
Jonathan Nieder9cba13c2011-03-16 07:08:341895struct diff_words_style_elem {
Thomas Rast882749a2010-04-14 15:59:061896 const char *prefix;
1897 const char *suffix;
1898 const char *color; /* NULL; filled in by the setup code if
1899 * color is enabled */
1900};
1901
Jonathan Nieder9cba13c2011-03-16 07:08:341902struct diff_words_style {
Thomas Rast882749a2010-04-14 15:59:061903 enum diff_words_type type;
Brandon Williams63a01c32018-02-14 18:59:391904 struct diff_words_style_elem new_word, old_word, ctx;
Thomas Rast882749a2010-04-14 15:59:061905 const char *newline;
1906};
1907
Stephen Boydc2e86ad2011-03-22 07:51:051908static struct diff_words_style diff_words_styles[] = {
Thomas Rast882749a2010-04-14 15:59:061909 { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1910 { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1911 { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" }
1912};
1913
Johannes Schindelinf59a59e2006-07-28 21:56:151914struct diff_words_data {
Johannes Schindelinf59a59e2006-07-28 21:56:151915 struct diff_words_buffer minus, plus;
Johannes Schindelin2e5d2002009-01-17 16:29:441916 const char *current_plus;
Bo Yang4297c0a2010-05-29 15:32:061917 int last_minus;
1918 struct diff_options *opt;
Johannes Schindelin2b6a5412009-01-17 16:29:451919 regex_t *word_regex;
Thomas Rast882749a2010-04-14 15:59:061920 enum diff_words_type type;
1921 struct diff_words_style *style;
Johannes Schindelinf59a59e2006-07-28 21:56:151922};
1923
Stefan Bellerbd033292017-06-30 00:07:031924static int fn_out_diff_words_write_helper(struct diff_options *o,
Thomas Rast882749a2010-04-14 15:59:061925 struct diff_words_style_elem *st_el,
1926 const char *newline,
Stefan Bellerbd033292017-06-30 00:07:031927 size_t count, const char *buf)
Thomas Rast882749a2010-04-14 15:59:061928{
Bo Yang4297c0a2010-05-29 15:32:061929 int print = 0;
Stefan Bellerbd033292017-06-30 00:07:031930 struct strbuf sb = STRBUF_INIT;
Bo Yang4297c0a2010-05-29 15:32:061931
Thomas Rast882749a2010-04-14 15:59:061932 while (count) {
1933 char *p = memchr(buf, '\n', count);
Bo Yang4297c0a2010-05-29 15:32:061934 if (print)
Stefan Bellerbd033292017-06-30 00:07:031935 strbuf_addstr(&sb, diff_line_prefix(o));
1936
Thomas Rast882749a2010-04-14 15:59:061937 if (p != buf) {
Stefan Bellerbd033292017-06-30 00:07:031938 const char *reset = st_el->color && *st_el->color ?
1939 GIT_COLOR_RESET : NULL;
1940 if (st_el->color && *st_el->color)
1941 strbuf_addstr(&sb, st_el->color);
1942 strbuf_addstr(&sb, st_el->prefix);
1943 strbuf_add(&sb, buf, p ? p - buf : count);
1944 strbuf_addstr(&sb, st_el->suffix);
1945 if (reset)
1946 strbuf_addstr(&sb, reset);
Thomas Rast882749a2010-04-14 15:59:061947 }
1948 if (!p)
Stefan Bellerbd033292017-06-30 00:07:031949 goto out;
1950
1951 strbuf_addstr(&sb, newline);
Thomas Rast882749a2010-04-14 15:59:061952 count -= p + 1 - buf;
1953 buf = p + 1;
Bo Yang4297c0a2010-05-29 15:32:061954 print = 1;
Stefan Bellerbd033292017-06-30 00:07:031955 if (count) {
1956 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1957 sb.buf, sb.len, 0);
1958 strbuf_reset(&sb);
1959 }
Thomas Rast882749a2010-04-14 15:59:061960 }
Stefan Bellerbd033292017-06-30 00:07:031961
1962out:
1963 if (sb.len)
1964 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1965 sb.buf, sb.len, 0);
1966 strbuf_release(&sb);
Thomas Rast882749a2010-04-14 15:59:061967 return 0;
1968}
1969
Bo Yang4297c0a2010-05-29 15:32:061970/*
1971 * '--color-words' algorithm can be described as:
1972 *
René Genz56217602017-04-30 15:48:271973 * 1. collect the minus/plus lines of a diff hunk, divided into
Bo Yang4297c0a2010-05-29 15:32:061974 * minus-lines and plus-lines;
1975 *
1976 * 2. break both minus-lines and plus-lines into words and
1977 * place them into two mmfile_t with one word for each line;
1978 *
1979 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1980 *
1981 * And for the common parts of the both file, we output the plus side text.
1982 * diff_words->current_plus is used to trace the current position of the plus file
1983 * which printed. diff_words->last_minus is used to trace the last minus word
1984 * printed.
1985 *
1986 * For '--graph' to work with '--color-words', we need to output the graph prefix
1987 * on each line of color words output. Generally, there are two conditions on
1988 * which we should output the prefix.
1989 *
1990 * 1. diff_words->last_minus == 0 &&
1991 * diff_words->current_plus == diff_words->plus.text.ptr
1992 *
1993 * that is: the plus text must start as a new line, and if there is no minus
1994 * word printed, a graph prefix must be printed.
1995 *
1996 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1997 * *(diff_words->current_plus - 1) == '\n'
1998 *
1999 * that is: a graph prefix must be printed following a '\n'
2000 */
2001static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
2002{
2003 if ((diff_words->last_minus == 0 &&
2004 diff_words->current_plus == diff_words->plus.text.ptr) ||
2005 (diff_words->current_plus > diff_words->plus.text.ptr &&
2006 *(diff_words->current_plus - 1) == '\n')) {
2007 return 1;
2008 } else {
2009 return 0;
2010 }
2011}
2012
Jeff King7c61e252018-11-02 06:37:182013static void fn_out_diff_words_aux(void *priv,
2014 long minus_first, long minus_len,
2015 long plus_first, long plus_len,
Jeff King61bdc7c2022-12-13 11:13:482016 const char *func UNUSED, long funclen UNUSED)
Johannes Schindelinf59a59e2006-07-28 21:56:152017{
2018 struct diff_words_data *diff_words = priv;
Thomas Rast882749a2010-04-14 15:59:062019 struct diff_words_style *style = diff_words->style;
Johannes Schindelin2e5d2002009-01-17 16:29:442020 const char *minus_begin, *minus_end, *plus_begin, *plus_end;
Bo Yang4297c0a2010-05-29 15:32:062021 struct diff_options *opt = diff_words->opt;
John Keeping30997bb2013-02-07 20:15:272022 const char *line_prefix;
Johannes Schindelinf59a59e2006-07-28 21:56:152023
Bo Yang4297c0a2010-05-29 15:32:062024 assert(opt);
John Keeping30997bb2013-02-07 20:15:272025 line_prefix = diff_line_prefix(opt);
Bo Yang4297c0a2010-05-29 15:32:062026
Johannes Schindelin2e5d2002009-01-17 16:29:442027 /* POSIX requires that first be decremented by one if len == 0... */
2028 if (minus_len) {
2029 minus_begin = diff_words->minus.orig[minus_first].begin;
2030 minus_end =
2031 diff_words->minus.orig[minus_first + minus_len - 1].end;
2032 } else
2033 minus_begin = minus_end =
2034 diff_words->minus.orig[minus_first].end;
2035
2036 if (plus_len) {
2037 plus_begin = diff_words->plus.orig[plus_first].begin;
2038 plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end;
2039 } else
2040 plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
2041
Bo Yang4297c0a2010-05-29 15:32:062042 if (color_words_output_graph_prefix(diff_words)) {
2043 fputs(line_prefix, diff_words->opt->file);
2044 }
2045 if (diff_words->current_plus != plus_begin) {
Stefan Bellerbd033292017-06-30 00:07:032046 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 15:59:062047 &style->ctx, style->newline,
2048 plus_begin - diff_words->current_plus,
Stefan Bellerbd033292017-06-30 00:07:032049 diff_words->current_plus);
Bo Yang4297c0a2010-05-29 15:32:062050 }
2051 if (minus_begin != minus_end) {
Stefan Bellerbd033292017-06-30 00:07:032052 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 18:59:392053 &style->old_word, style->newline,
Stefan Bellerbd033292017-06-30 00:07:032054 minus_end - minus_begin, minus_begin);
Bo Yang4297c0a2010-05-29 15:32:062055 }
2056 if (plus_begin != plus_end) {
Stefan Bellerbd033292017-06-30 00:07:032057 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 18:59:392058 &style->new_word, style->newline,
Stefan Bellerbd033292017-06-30 00:07:032059 plus_end - plus_begin, plus_begin);
Bo Yang4297c0a2010-05-29 15:32:062060 }
Johannes Schindelin2e5d2002009-01-17 16:29:442061
2062 diff_words->current_plus = plus_end;
Bo Yang4297c0a2010-05-29 15:32:062063 diff_words->last_minus = minus_first;
Johannes Schindelinf59a59e2006-07-28 21:56:152064}
2065
Johannes Schindelin2b6a5412009-01-17 16:29:452066/* This function starts looking at *begin, and returns 0 iff a word was found. */
2067static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
2068 int *begin, int *end)
2069{
Phillip Wood0324e8f2021-05-04 09:27:342070 while (word_regex && *begin < buffer->size) {
Johannes Schindelin2b6a5412009-01-17 16:29:452071 regmatch_t match[1];
Johannes Schindelinb7d36ff2016-09-21 18:24:142072 if (!regexec_buf(word_regex, buffer->ptr + *begin,
2073 buffer->size - *begin, 1, match, 0)) {
Johannes Schindelin2b6a5412009-01-17 16:29:452074 char *p = memchr(buffer->ptr + *begin + match[0].rm_so,
2075 '\n', match[0].rm_eo - match[0].rm_so);
2076 *end = p ? p - buffer->ptr : match[0].rm_eo + *begin;
2077 *begin += match[0].rm_so;
Phillip Wood0324e8f2021-05-04 09:27:342078 if (*begin == *end)
2079 (*begin)++;
2080 else
2081 return *begin > *end;
2082 } else {
2083 return -1;
Johannes Schindelin2b6a5412009-01-17 16:29:452084 }
Johannes Schindelinf59a59e2006-07-28 21:56:152085 }
2086
Johannes Schindelin2b6a5412009-01-17 16:29:452087 /* find the next word */
2088 while (*begin < buffer->size && isspace(buffer->ptr[*begin]))
2089 (*begin)++;
2090 if (*begin >= buffer->size)
2091 return -1;
2092
2093 /* find the end of the word */
2094 *end = *begin + 1;
2095 while (*end < buffer->size && !isspace(buffer->ptr[*end]))
2096 (*end)++;
2097
2098 return 0;
2099}
2100
Johannes Schindelin23c15752009-01-17 16:29:432101/*
Johannes Schindelin2e5d2002009-01-17 16:29:442102 * This function splits the words in buffer->text, stores the list with
2103 * newline separator into out, and saves the offsets of the original words
2104 * in buffer->orig.
Johannes Schindelin23c15752009-01-17 16:29:432105 */
Johannes Schindelin2b6a5412009-01-17 16:29:452106static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out,
2107 regex_t *word_regex)
Johannes Schindelin23c15752009-01-17 16:29:432108{
Johannes Schindelin2e5d2002009-01-17 16:29:442109 int i, j;
Johannes Schindelin2b6a5412009-01-17 16:29:452110 long alloc = 0;
Johannes Schindelin2e5d2002009-01-17 16:29:442111
2112 out->size = 0;
Johannes Schindelin2b6a5412009-01-17 16:29:452113 out->ptr = NULL;
Johannes Schindelin2e5d2002009-01-17 16:29:442114
2115 /* fake an empty "0th" word */
2116 ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc);
2117 buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr;
2118 buffer->orig_nr = 1;
2119
2120 for (i = 0; i < buffer->text.size; i++) {
Johannes Schindelin2b6a5412009-01-17 16:29:452121 if (find_word_boundaries(&buffer->text, word_regex, &i, &j))
2122 return;
Johannes Schindelin2e5d2002009-01-17 16:29:442123
2124 /* store original boundaries */
2125 ALLOC_GROW(buffer->orig, buffer->orig_nr + 1,
2126 buffer->orig_alloc);
2127 buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i;
2128 buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j;
2129 buffer->orig_nr++;
2130
2131 /* store one word */
Johannes Schindelin2b6a5412009-01-17 16:29:452132 ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc);
Johannes Schindelin2e5d2002009-01-17 16:29:442133 memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i);
2134 out->ptr[out->size + j - i] = '\n';
2135 out->size += j - i + 1;
2136
2137 i = j - 1;
Johannes Schindelinf59a59e2006-07-28 21:56:152138 }
2139}
2140
2141/* this executes the word diff on the accumulated buffers */
2142static void diff_words_show(struct diff_words_data *diff_words)
2143{
2144 xpparam_t xpp;
2145 xdemitconf_t xecfg;
Johannes Schindelinf59a59e2006-07-28 21:56:152146 mmfile_t minus, plus;
Thomas Rast882749a2010-04-14 15:59:062147 struct diff_words_style *style = diff_words->style;
Johannes Schindelinf59a59e2006-07-28 21:56:152148
Bo Yang4297c0a2010-05-29 15:32:062149 struct diff_options *opt = diff_words->opt;
John Keeping30997bb2013-02-07 20:15:272150 const char *line_prefix;
Bo Yang4297c0a2010-05-29 15:32:062151
2152 assert(opt);
John Keeping30997bb2013-02-07 20:15:272153 line_prefix = diff_line_prefix(opt);
Bo Yang4297c0a2010-05-29 15:32:062154
Johannes Schindelin2e5d2002009-01-17 16:29:442155 /* special case: only removal */
2156 if (!diff_words->plus.text.size) {
Stefan Bellerbd033292017-06-30 00:07:032157 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
2158 line_prefix, strlen(line_prefix), 0);
2159 fn_out_diff_words_write_helper(diff_words->opt,
Brandon Williams63a01c32018-02-14 18:59:392160 &style->old_word, style->newline,
Bo Yang4297c0a2010-05-29 15:32:062161 diff_words->minus.text.size,
Stefan Bellerbd033292017-06-30 00:07:032162 diff_words->minus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 16:29:442163 diff_words->minus.text.size = 0;
2164 return;
2165 }
2166
2167 diff_words->current_plus = diff_words->plus.text.ptr;
Bo Yang4297c0a2010-05-29 15:32:062168 diff_words->last_minus = 0;
Johannes Schindelinf59a59e2006-07-28 21:56:152169
Brian Downing9ccd0a82008-10-25 13:30:372170 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 18:05:462171 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelin2b6a5412009-01-17 16:29:452172 diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex);
2173 diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex);
René Scharfe582aa002010-05-02 13:04:412174 xpp.flags = 0;
Johannes Schindelin2b6a5412009-01-17 16:29:452175 /* as only the hunk header will be parsed, we need a 0-context */
Johannes Schindelin2e5d2002009-01-17 16:29:442176 xecfg.ctxlen = 0;
Jeff King7c61e252018-11-02 06:37:182177 if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, NULL,
Jeff King9346d6d2018-11-02 06:35:452178 diff_words, &xpp, &xecfg))
Jeff King3efb9882015-09-24 23:12:232179 die("unable to generate word diff");
Johannes Schindelinf59a59e2006-07-28 21:56:152180 free(minus.ptr);
2181 free(plus.ptr);
Johannes Schindelin2e5d2002009-01-17 16:29:442182 if (diff_words->current_plus != diff_words->plus.text.ptr +
Bo Yang4297c0a2010-05-29 15:32:062183 diff_words->plus.text.size) {
2184 if (color_words_output_graph_prefix(diff_words))
Stefan Bellerbd033292017-06-30 00:07:032185 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
2186 line_prefix, strlen(line_prefix), 0);
2187 fn_out_diff_words_write_helper(diff_words->opt,
Thomas Rast882749a2010-04-14 15:59:062188 &style->ctx, style->newline,
Johannes Schindelin2e5d2002009-01-17 16:29:442189 diff_words->plus.text.ptr + diff_words->plus.text.size
Stefan Bellerbd033292017-06-30 00:07:032190 - diff_words->current_plus, diff_words->current_plus);
Bo Yang4297c0a2010-05-29 15:32:062191 }
Johannes Schindelinf59a59e2006-07-28 21:56:152192 diff_words->minus.text.size = diff_words->plus.text.size = 0;
Johannes Schindelinf59a59e2006-07-28 21:56:152193}
2194
Junio C Hamano76fd2822009-10-30 17:09:062195/* In "color-words" mode, show word-diff of words accumulated in the buffer */
2196static void diff_words_flush(struct emit_callback *ecbdata)
2197{
Stefan Bellere6e045f2017-06-30 00:07:062198 struct diff_options *wo = ecbdata->diff_words->opt;
2199
Junio C Hamano76fd2822009-10-30 17:09:062200 if (ecbdata->diff_words->minus.text.size ||
2201 ecbdata->diff_words->plus.text.size)
2202 diff_words_show(ecbdata->diff_words);
Stefan Bellere6e045f2017-06-30 00:07:062203
2204 if (wo->emitted_symbols) {
2205 struct diff_options *o = ecbdata->opt;
2206 struct emitted_diff_symbols *wol = wo->emitted_symbols;
2207 int i;
2208
2209 /*
2210 * NEEDSWORK:
2211 * Instead of appending each, concat all words to a line?
2212 */
2213 for (i = 0; i < wol->nr; i++)
2214 append_emitted_diff_symbol(o, &wol->buf[i]);
2215
2216 for (i = 0; i < wol->nr; i++)
2217 free((void *)wol->buf[i].line);
2218
2219 wol->nr = 0;
2220 }
Junio C Hamano76fd2822009-10-30 17:09:062221}
2222
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:332223static void diff_filespec_load_driver(struct diff_filespec *one,
2224 struct index_state *istate)
Thomas Rast77d1a522012-03-14 18:24:082225{
2226 /* Use already-loaded driver */
2227 if (one->driver)
2228 return;
2229
2230 if (S_ISREG(one->mode))
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:332231 one->driver = userdiff_find_by_path(istate, one->path);
Thomas Rast77d1a522012-03-14 18:24:082232
2233 /* Fallback to default settings */
2234 if (!one->driver)
2235 one->driver = userdiff_find_by_name("default");
2236}
2237
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:332238static const char *userdiff_word_regex(struct diff_filespec *one,
2239 struct index_state *istate)
Thomas Rast77d1a522012-03-14 18:24:082240{
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:332241 diff_filespec_load_driver(one, istate);
Thomas Rast77d1a522012-03-14 18:24:082242 return one->driver->word_regex;
2243}
2244
2245static void init_diff_words_data(struct emit_callback *ecbdata,
Thomas Rast6440d342012-03-14 18:24:092246 struct diff_options *orig_opts,
Thomas Rast77d1a522012-03-14 18:24:082247 struct diff_filespec *one,
2248 struct diff_filespec *two)
2249{
2250 int i;
Thomas Rast6440d342012-03-14 18:24:092251 struct diff_options *o = xmalloc(sizeof(struct diff_options));
2252 memcpy(o, orig_opts, sizeof(struct diff_options));
Thomas Rast77d1a522012-03-14 18:24:082253
René Scharfeca56dad2021-03-13 16:17:222254 CALLOC_ARRAY(ecbdata->diff_words, 1);
Thomas Rast77d1a522012-03-14 18:24:082255 ecbdata->diff_words->type = o->word_diff;
2256 ecbdata->diff_words->opt = o;
Stefan Bellere6e045f2017-06-30 00:07:062257
2258 if (orig_opts->emitted_symbols)
René Scharfeca56dad2021-03-13 16:17:222259 CALLOC_ARRAY(o->emitted_symbols, 1);
Stefan Bellere6e045f2017-06-30 00:07:062260
Thomas Rast77d1a522012-03-14 18:24:082261 if (!o->word_regex)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:332262 o->word_regex = userdiff_word_regex(one, o->repo->index);
Thomas Rast77d1a522012-03-14 18:24:082263 if (!o->word_regex)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:332264 o->word_regex = userdiff_word_regex(two, o->repo->index);
Thomas Rast77d1a522012-03-14 18:24:082265 if (!o->word_regex)
2266 o->word_regex = diff_word_regex_cfg;
2267 if (o->word_regex) {
2268 ecbdata->diff_words->word_regex = (regex_t *)
2269 xmalloc(sizeof(regex_t));
2270 if (regcomp(ecbdata->diff_words->word_regex,
2271 o->word_regex,
2272 REG_EXTENDED | REG_NEWLINE))
Nguyễn Thái Ngọc Duy1a07e592018-07-21 07:49:192273 die("invalid regular expression: %s",
2274 o->word_regex);
Thomas Rast77d1a522012-03-14 18:24:082275 }
2276 for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) {
2277 if (o->word_diff == diff_words_styles[i].type) {
2278 ecbdata->diff_words->style =
2279 &diff_words_styles[i];
2280 break;
2281 }
2282 }
2283 if (want_color(o->use_color)) {
2284 struct diff_words_style *st = ecbdata->diff_words->style;
Brandon Williams63a01c32018-02-14 18:59:392285 st->old_word.color = diff_get_color_opt(o, DIFF_FILE_OLD);
2286 st->new_word.color = diff_get_color_opt(o, DIFF_FILE_NEW);
Jeff King8dbf3eb2015-05-27 20:48:462287 st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT);
Thomas Rast77d1a522012-03-14 18:24:082288 }
2289}
2290
Johannes Schindelinf59a59e2006-07-28 21:56:152291static void free_diff_words_data(struct emit_callback *ecbdata)
2292{
2293 if (ecbdata->diff_words) {
Junio C Hamano76fd2822009-10-30 17:09:062294 diff_words_flush(ecbdata);
Ævar Arnfjörð Bjarmason77e56d52022-03-17 14:55:352295 free_emitted_diff_symbols(ecbdata->diff_words->opt->emitted_symbols);
Thomas Rast6440d342012-03-14 18:24:092296 free (ecbdata->diff_words->opt);
Jim Meyering8e0f7002008-01-31 17:26:322297 free (ecbdata->diff_words->minus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 16:29:442298 free (ecbdata->diff_words->minus.orig);
Jim Meyering8e0f7002008-01-31 17:26:322299 free (ecbdata->diff_words->plus.text.ptr);
Johannes Schindelin2e5d2002009-01-17 16:29:442300 free (ecbdata->diff_words->plus.orig);
Brandon Caseyef5644e2010-09-09 19:02:452301 if (ecbdata->diff_words->word_regex) {
2302 regfree(ecbdata->diff_words->word_regex);
2303 free(ecbdata->diff_words->word_regex);
2304 }
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:462305 FREE_AND_NULL(ecbdata->diff_words);
Johannes Schindelinf59a59e2006-07-28 21:56:152306 }
2307}
2308
Jeff Kinge9330ae2025-09-16 23:13:592309const char *diff_get_color(enum git_colorbool diff_use_color, enum color_diff ix)
Johannes Schindelincd112ce2006-06-13 16:45:442310{
Jeff Kingdaa0c3d2011-08-18 05:04:232311 if (want_color(diff_use_color))
Linus Torvalds50f575f2006-06-22 20:53:312312 return diff_colors[ix];
2313 return "";
Johannes Schindelincd112ce2006-06-13 16:45:442314}
2315
John Keepingf1922232013-02-07 20:15:262316const char *diff_line_prefix(struct diff_options *opt)
2317{
Jeff King436728f2024-10-03 21:09:242318 return opt->output_prefix ?
2319 opt->output_prefix(opt, opt->output_prefix_data) :
2320 "";
John Keepingf1922232013-02-07 20:15:262321}
2322
Jeff King19b90462019-02-14 05:48:272323static unsigned long sane_truncate_line(char *line, unsigned long len)
Junio C Hamano23707812008-01-02 09:50:112324{
2325 const char *cp;
2326 unsigned long allot;
2327 size_t l = len;
2328
Junio C Hamano23707812008-01-02 09:50:112329 cp = line;
2330 allot = l;
2331 while (0 < l) {
2332 (void) utf8_width(&cp, &l);
2333 if (!cp)
2334 break; /* truncated in the middle? */
2335 }
2336 return allot - l;
2337}
2338
Junio C Hamanod68fe262009-09-15 05:05:572339static void find_lno(const char *line, struct emit_callback *ecbdata)
Junio C Hamano690ed842009-09-04 07:41:152340{
Junio C Hamanod68fe262009-09-15 05:05:572341 const char *p;
2342 ecbdata->lno_in_preimage = 0;
2343 ecbdata->lno_in_postimage = 0;
2344 p = strchr(line, '-');
Junio C Hamano690ed842009-09-04 07:41:152345 if (!p)
Junio C Hamanod68fe262009-09-15 05:05:572346 return; /* cannot happen */
2347 ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10);
2348 p = strchr(p, '+');
2349 if (!p)
2350 return; /* cannot happen */
2351 ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10);
Junio C Hamano690ed842009-09-04 07:41:152352}
2353
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 17:15:242354static int fn_out_consume(void *priv, char *line, unsigned long len)
Junio C Hamano6973dca2006-04-22 06:57:452355{
Junio C Hamano6973dca2006-04-22 06:57:452356 struct emit_callback *ecbdata = priv;
Bo Yang7be57612010-05-26 07:23:542357 struct diff_options *o = ecbdata->opt;
Junio C Hamano6973dca2006-04-22 06:57:452358
Stefan Bellerba162332016-09-07 23:36:462359 o->found_changes = 1;
2360
Greg Bacon3e97c7c2009-11-19 21:12:242361 if (ecbdata->header) {
Stefan Bellera29b0a12017-06-30 00:06:572362 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
2363 ecbdata->header->buf, ecbdata->header->len, 0);
Greg Bacon3e97c7c2009-11-19 21:12:242364 strbuf_reset(ecbdata->header);
2365 ecbdata->header = NULL;
2366 }
Johannes Schindelin34a5e1a2007-02-25 22:34:542367
Junio C Hamano6973dca2006-04-22 06:57:452368 if (ecbdata->label_path[0]) {
Stefan Beller3ee8b7b2017-06-30 00:06:562369 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
2370 ecbdata->label_path[0],
2371 strlen(ecbdata->label_path[0]), 0);
2372 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
2373 ecbdata->label_path[1],
2374 strlen(ecbdata->label_path[1]), 0);
Junio C Hamano6973dca2006-04-22 06:57:452375 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
2376 }
Johannes Schindelincd112ce2006-06-13 16:45:442377
Jim Meyeringa624eaa2008-08-15 11:39:262378 if (diff_suppress_blank_empty
2379 && len == 2 && line[0] == ' ' && line[1] == '\n') {
2380 line[0] = '\n';
2381 len = 1;
2382 }
2383
Junio C Hamanob8d9c1a2009-09-04 06:59:252384 if (line[0] == '@') {
Junio C Hamano76fd2822009-10-30 17:09:062385 if (ecbdata->diff_words)
2386 diff_words_flush(ecbdata);
Jeff King19b90462019-02-14 05:48:272387 len = sane_truncate_line(line, len);
Junio C Hamanod68fe262009-09-15 05:05:572388 find_lno(line, ecbdata);
Bert Wesarg89cb73a2009-11-27 06:55:182389 emit_hunk_header(ecbdata, line, len);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 17:15:242390 return 0;
Johannes Schindelincd112ce2006-06-13 16:45:442391 }
Junio C Hamano448c3ef2006-09-23 05:48:392392
Junio C Hamano448c3ef2006-09-23 05:48:392393 if (ecbdata->diff_words) {
Stefan Bellerff958672017-06-30 00:06:542394 enum diff_symbol s =
2395 ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ?
2396 DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS;
Junio C Hamano448c3ef2006-09-23 05:48:392397 if (line[0] == '-') {
2398 diff_words_append(line, len,
2399 &ecbdata->diff_words->minus);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 17:15:242400 return 0;
Junio C Hamano448c3ef2006-09-23 05:48:392401 } else if (line[0] == '+') {
2402 diff_words_append(line, len,
2403 &ecbdata->diff_words->plus);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 17:15:242404 return 0;
Christian Couder59556542013-11-30 20:55:402405 } else if (starts_with(line, "\\ ")) {
Thomas Rastc7c2bc02012-01-12 11:15:332406 /*
2407 * Eat the "no newline at eof" marker as if we
2408 * saw a "+" or "-" line with nothing on it,
2409 * and return without diff_words_flush() to
2410 * defer processing. If this is the end of
2411 * preimage, more "+" lines may come after it.
2412 */
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 17:15:242413 return 0;
Junio C Hamano448c3ef2006-09-23 05:48:392414 }
Junio C Hamano76fd2822009-10-30 17:09:062415 diff_words_flush(ecbdata);
Stefan Bellerff958672017-06-30 00:06:542416 emit_diff_symbol(o, s, line, len, 0);
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 17:15:242417 return 0;
Junio C Hamano448c3ef2006-09-23 05:48:392418 }
Junio C Hamano448c3ef2006-09-23 05:48:392419
Junio C Hamano0e383e12015-05-26 16:56:332420 switch (line[0]) {
2421 case '+':
Junio C Hamanod68fe262009-09-15 05:05:572422 ecbdata->lno_in_postimage++;
Jeff Kinge04df612019-02-14 05:48:132423 emit_add_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 16:56:332424 break;
2425 case '-':
2426 ecbdata->lno_in_preimage++;
Jeff Kinge04df612019-02-14 05:48:132427 emit_del_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 16:56:332428 break;
2429 case ' ':
2430 ecbdata->lno_in_postimage++;
2431 ecbdata->lno_in_preimage++;
Jeff Kinge04df612019-02-14 05:48:132432 emit_context_line(ecbdata, line + 1, len - 1);
Junio C Hamano0e383e12015-05-26 16:56:332433 break;
2434 default:
2435 /* incomplete line at the end */
2436 ecbdata->lno_in_preimage++;
Stefan Bellerf2bb1212017-06-30 00:06:552437 emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE,
2438 line, len, 0);
Junio C Hamano0e383e12015-05-26 16:56:332439 break;
Junio C Hamano448c3ef2006-09-23 05:48:392440 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 17:15:242441 return 0;
Junio C Hamano6973dca2006-04-22 06:57:452442}
2443
Lidong Yanb55e6d32025-08-08 03:30:192444static int quick_consume(void *priv, char *line UNUSED, unsigned long len UNUSED)
2445{
2446 struct emit_callback *ecbdata = priv;
2447 struct diff_options *o = ecbdata->opt;
2448
2449 o->found_changes = 1;
2450 return 1;
2451}
2452
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 13:02:202453static void pprint_rename(struct strbuf *name, const char *a, const char *b)
Junio C Hamano6973dca2006-04-22 06:57:452454{
Brandon Williams63a01c32018-02-14 18:59:392455 const char *old_name = a;
2456 const char *new_name = b;
Junio C Hamano6973dca2006-04-22 06:57:452457 int pfx_length, sfx_length;
Thomas Rastdd281f02013-02-26 20:47:012458 int pfx_adjust_for_slash;
Junio C Hamano6973dca2006-04-22 06:57:452459 int len_a = strlen(a);
2460 int len_b = strlen(b);
Pierre Habouzit663af342007-09-19 22:42:152461 int a_midlen, b_midlen;
Alexandre Julliarde5bfbf92007-02-10 14:39:002462 int qlen_a = quote_c_style(a, NULL, NULL, 0);
2463 int qlen_b = quote_c_style(b, NULL, NULL, 0);
2464
2465 if (qlen_a || qlen_b) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 13:02:202466 quote_c_style(a, name, NULL, 0);
2467 strbuf_addstr(name, " => ");
2468 quote_c_style(b, name, NULL, 0);
2469 return;
Alexandre Julliarde5bfbf92007-02-10 14:39:002470 }
Junio C Hamano6973dca2006-04-22 06:57:452471
2472 /* Find common prefix */
2473 pfx_length = 0;
Brandon Williams63a01c32018-02-14 18:59:392474 while (*old_name && *new_name && *old_name == *new_name) {
2475 if (*old_name == '/')
2476 pfx_length = old_name - a + 1;
2477 old_name++;
2478 new_name++;
Junio C Hamano6973dca2006-04-22 06:57:452479 }
2480
2481 /* Find common suffix */
Brandon Williams63a01c32018-02-14 18:59:392482 old_name = a + len_a;
2483 new_name = b + len_b;
Junio C Hamano6973dca2006-04-22 06:57:452484 sfx_length = 0;
Antoine Pelissed020e272013-02-23 16:48:452485 /*
Thomas Rastdd281f02013-02-26 20:47:012486 * If there is a common prefix, it must end in a slash. In
2487 * that case we let this loop run 1 into the prefix to see the
2488 * same slash.
2489 *
2490 * If there is no common prefix, we cannot do this as it would
2491 * underrun the input strings.
Antoine Pelissed020e272013-02-23 16:48:452492 */
Thomas Rastdd281f02013-02-26 20:47:012493 pfx_adjust_for_slash = (pfx_length ? 1 : 0);
Brandon Williams63a01c32018-02-14 18:59:392494 while (a + pfx_length - pfx_adjust_for_slash <= old_name &&
2495 b + pfx_length - pfx_adjust_for_slash <= new_name &&
2496 *old_name == *new_name) {
2497 if (*old_name == '/')
2498 sfx_length = len_a - (old_name - a);
2499 old_name--;
2500 new_name--;
Junio C Hamano6973dca2006-04-22 06:57:452501 }
2502
2503 /*
2504 * pfx{mid-a => mid-b}sfx
2505 * {pfx-a => pfx-b}sfx
2506 * pfx{sfx-a => sfx-b}
2507 * name-a => name-b
2508 */
Pierre Habouzit663af342007-09-19 22:42:152509 a_midlen = len_a - pfx_length - sfx_length;
2510 b_midlen = len_b - pfx_length - sfx_length;
2511 if (a_midlen < 0)
2512 a_midlen = 0;
2513 if (b_midlen < 0)
2514 b_midlen = 0;
Junio C Hamanocc908b82006-05-15 05:07:282515
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 13:02:202516 strbuf_grow(name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
Pierre Habouzit663af342007-09-19 22:42:152517 if (pfx_length + sfx_length) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 13:02:202518 strbuf_add(name, a, pfx_length);
2519 strbuf_addch(name, '{');
Junio C Hamano6973dca2006-04-22 06:57:452520 }
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 13:02:202521 strbuf_add(name, a + pfx_length, a_midlen);
2522 strbuf_addstr(name, " => ");
2523 strbuf_add(name, b + pfx_length, b_midlen);
Pierre Habouzit663af342007-09-19 22:42:152524 if (pfx_length + sfx_length) {
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 13:02:202525 strbuf_addch(name, '}');
2526 strbuf_add(name, a + len_a - sfx_length, sfx_length);
Junio C Hamano6973dca2006-04-22 06:57:452527 }
Junio C Hamano6973dca2006-04-22 06:57:452528}
2529
Junio C Hamano6973dca2006-04-22 06:57:452530static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
2531 const char *name_a,
2532 const char *name_b)
2533{
2534 struct diffstat_file *x;
René Scharfeca56dad2021-03-13 16:17:222535 CALLOC_ARRAY(x, 1);
Dmitry S. Dolzhenko4c960a42014-03-03 22:31:532536 ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
Junio C Hamano6973dca2006-04-22 06:57:452537 diffstat->files[diffstat->nr++] = x;
2538 if (name_b) {
Junio C Hamanof6046522007-12-12 07:46:302539 x->from_name = xstrdup(name_a);
2540 x->name = xstrdup(name_b);
Junio C Hamano6973dca2006-04-22 06:57:452541 x->is_renamed = 1;
2542 }
Junio C Hamanof6046522007-12-12 07:46:302543 else {
2544 x->from_name = NULL;
Shawn Pearce9befac42006-09-02 04:16:312545 x->name = xstrdup(name_a);
Junio C Hamanof6046522007-12-12 07:46:302546 }
Junio C Hamano6973dca2006-04-22 06:57:452547 return x;
2548}
2549
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 17:15:242550static int diffstat_consume(void *priv, char *line, unsigned long len)
Junio C Hamano6973dca2006-04-22 06:57:452551{
2552 struct diffstat_t *diffstat = priv;
2553 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
2554
Jeff King0e5a87e2022-10-18 01:01:172555 if (!len)
2556 BUG("xdiff fed us an empty line");
2557
Junio C Hamano6973dca2006-04-22 06:57:452558 if (line[0] == '+')
2559 x->added++;
2560 else if (line[0] == '-')
2561 x->deleted++;
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 17:15:242562 return 0;
Junio C Hamano6973dca2006-04-22 06:57:452563}
2564
Johannes Schindelin698ce6f2006-05-20 13:40:292565const char mime_boundary_leader[] = "------------";
Junio C Hamano6973dca2006-04-22 06:57:452566
Junio C Hamanoa2540022006-09-27 01:53:022567static int scale_linear(int it, int width, int max_change)
2568{
Junio C Hamano2eeeef22012-02-14 21:49:112569 if (!it)
2570 return 0;
Junio C Hamanoa2540022006-09-27 01:53:022571 /*
Junio C Hamano2eeeef22012-02-14 21:49:112572 * make sure that at least one '-' or '+' is printed if
2573 * there is any change to this path. The easiest way is to
Elijah Newren15beaaa2019-11-05 17:07:232574 * scale linearly as if the allotted width is one column shorter
Junio C Hamano2eeeef22012-02-14 21:49:112575 * than it is, and then add 1 to the result.
Junio C Hamanoa2540022006-09-27 01:53:022576 */
Junio C Hamano2eeeef22012-02-14 21:49:112577 return 1 + (it * (width - 1) / max_change);
Junio C Hamanoa2540022006-09-27 01:53:022578}
2579
Stefan Beller0911c472017-06-30 00:07:022580static void show_graph(struct strbuf *out, char ch, int cnt,
2581 const char *set, const char *reset)
Junio C Hamanoa2540022006-09-27 01:53:022582{
2583 if (cnt <= 0)
2584 return;
Stefan Beller0911c472017-06-30 00:07:022585 strbuf_addstr(out, set);
2586 strbuf_addchars(out, ch, cnt);
2587 strbuf_addstr(out, reset);
Junio C Hamanoa2540022006-09-27 01:53:022588}
2589
Junio C Hamanof6046522007-12-12 07:46:302590static void fill_print_name(struct diffstat_file *file)
2591{
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 13:02:202592 struct strbuf pname = STRBUF_INIT;
Junio C Hamanof6046522007-12-12 07:46:302593
2594 if (file->print_name)
2595 return;
2596
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 13:02:202597 if (file->is_renamed)
2598 pprint_rename(&pname, file->from_name, file->name);
2599 else
2600 quote_c_style(file->name, &pname, NULL, 0);
2601
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 14:09:592602 if (file->comments)
2603 strbuf_addf(&pname, " (%s)", file->comments);
2604
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 13:02:202605 file->print_name = strbuf_detach(&pname, NULL);
Junio C Hamanof6046522007-12-12 07:46:302606}
2607
Stefan Beller0911c472017-06-30 00:07:022608static void print_stat_summary_inserts_deletes(struct diff_options *options,
2609 int files, int insertions, int deletions)
Nguyễn Thái Ngọc Duy7f814632012-02-01 12:55:072610{
2611 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy7f814632012-02-01 12:55:072612
2613 if (!files) {
2614 assert(insertions == 0 && deletions == 0);
Stefan Beller0911c472017-06-30 00:07:022615 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
2616 NULL, 0, 0);
2617 return;
Nguyễn Thái Ngọc Duy7f814632012-02-01 12:55:072618 }
2619
2620 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 14:16:262621 (files == 1) ? " %d file changed" : " %d files changed",
Nguyễn Thái Ngọc Duy7f814632012-02-01 12:55:072622 files);
2623
2624 /*
2625 * For binary diff, the caller may want to print "x files
2626 * changed" with insertions == 0 && deletions == 0.
2627 *
2628 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2629 * is probably less confusing (i.e skip over "2 files changed
2630 * but nothing about added/removed lines? Is this a bug in Git?").
2631 */
2632 if (insertions || deletions == 0) {
Nguyễn Thái Ngọc Duy7f814632012-02-01 12:55:072633 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 14:16:262634 (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)",
Nguyễn Thái Ngọc Duy7f814632012-02-01 12:55:072635 insertions);
2636 }
2637
2638 if (deletions || insertions == 0) {
Nguyễn Thái Ngọc Duy7f814632012-02-01 12:55:072639 strbuf_addf(&sb,
Nguyễn Thái Ngọc Duy218adaa2012-09-13 14:16:262640 (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)",
Nguyễn Thái Ngọc Duy7f814632012-02-01 12:55:072641 deletions);
2642 }
2643 strbuf_addch(&sb, '\n');
Stefan Beller0911c472017-06-30 00:07:022644 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
2645 sb.buf, sb.len, 0);
Nguyễn Thái Ngọc Duy7f814632012-02-01 12:55:072646 strbuf_release(&sb);
Stefan Beller0911c472017-06-30 00:07:022647}
2648
2649void print_stat_summary(FILE *fp, int files,
2650 int insertions, int deletions)
2651{
2652 struct diff_options o;
2653 memset(&o, 0, sizeof(o));
2654 o.file = fp;
2655
2656 print_stat_summary_inserts_deletes(&o, files, insertions, deletions);
Nguyễn Thái Ngọc Duy7f814632012-02-01 12:55:072657}
2658
Felipe Contreras4b25d092009-05-01 09:06:362659static void show_stats(struct diffstat_t *data, struct diff_options *options)
Junio C Hamano6973dca2006-04-22 06:57:452660{
Benjamin Kramereb3a9dd2009-03-07 20:02:102661 int i, len, add, del, adds = 0, dels = 0;
Jeff King0974c112010-04-17 17:41:082662 uintmax_t max_change = 0, max_len = 0;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 20:38:582663 int total_files = data->nr, count;
2664 int width, name_width, graph_width, number_width = 0, bin_width = 0;
Johannes Schindelinc0aa3352011-03-22 12:50:082665 const char *reset, *add_c, *del_c;
Junio C Hamanoe5f85df2011-05-28 04:50:392666 int extra_shown = 0;
Stefan Beller0911c472017-06-30 00:07:022667 const char *line_prefix = diff_line_prefix(options);
2668 struct strbuf out = STRBUF_INIT;
Junio C Hamano6973dca2006-04-22 06:57:452669
2670 if (data->nr == 0)
2671 return;
2672
Michael J Gruber808e1db2011-05-27 12:36:412673 count = options->stat_count ? options->stat_count : data->nr;
Junio C Hamanoa2540022006-09-27 01:53:022674
Pierre Habouzit8f67f8a2007-11-10 19:05:142675 reset = diff_get_color_opt(options, DIFF_RESET);
Pierre Habouzit8f67f8a2007-11-10 19:05:142676 add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
2677 del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
Junio C Hamano785f7432006-09-27 01:59:412678
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432679 /*
2680 * Find the longest filename and max number of changes
2681 */
Michael J Gruber808e1db2011-05-27 12:36:412682 for (i = 0; (i < count) && (i < data->nr); i++) {
Junio C Hamano6973dca2006-04-22 06:57:452683 struct diffstat_file *file = data->files[i];
Jeff King0974c112010-04-17 17:41:082684 uintmax_t change = file->added + file->deleted;
Junio C Hamanoaf0ed812012-11-27 19:24:542685
2686 if (!file->is_interesting && (change == 0)) {
Michael J Gruber808e1db2011-05-27 12:36:412687 count++; /* not shown == room for one more */
Michael J Gruber358e4602011-05-27 12:36:402688 continue;
2689 }
Junio C Hamanof6046522007-12-12 07:46:302690 fill_print_name(file);
Torsten Bögershausen12fc4ad2022-09-14 15:13:332691 len = utf8_strwidth(file->print_name);
Junio C Hamano6973dca2006-04-22 06:57:452692 if (max_len < len)
2693 max_len = len;
2694
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 20:38:582695 if (file->is_unmerged) {
2696 /* "Unmerged" is 8 characters */
2697 bin_width = bin_width < 8 ? 8 : bin_width;
Junio C Hamano6973dca2006-04-22 06:57:452698 continue;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 20:38:582699 }
2700 if (file->is_binary) {
2701 /* "Bin XXX -> YYY bytes" */
2702 int w = 14 + decimal_width(file->added)
2703 + decimal_width(file->deleted);
2704 bin_width = bin_width < w ? w : bin_width;
2705 /* Display change counts aligned with "Bin" */
2706 number_width = 3;
2707 continue;
2708 }
2709
Junio C Hamanoa2540022006-09-27 01:53:022710 if (max_change < change)
2711 max_change = change;
Junio C Hamano6973dca2006-04-22 06:57:452712 }
Junio C Hamanoa20d3c02012-11-27 19:47:462713 count = i; /* where we can stop scanning in data->files[] */
Junio C Hamano6973dca2006-04-22 06:57:452714
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432715 /*
2716 * We have width = stat_width or term_columns() columns total.
2717 * We want a maximum of min(max_len, stat_name_width) for the name part.
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 12:26:452718 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432719 * We also need 1 for " " and 4 + decimal_width(max_change)
2720 * for " | NNNN " and one the empty column at the end, altogether
2721 * 6 + decimal_width(max_change).
Junio C Hamanoa2540022006-09-27 01:53:022722 *
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432723 * If there's not enough space, we will use the smaller of
2724 * stat_name_width (if set) and 5/8*width for the filename,
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 12:26:452725 * and the rest for constant elements + graph part, but no more
2726 * than stat_graph_width for the graph part.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432727 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2728 * for the standard terminal size).
2729 *
2730 * In other words: stat_width limits the maximum width, and
2731 * stat_name_width fixes the maximum width of the filename,
2732 * and is also used to divide available columns if there
2733 * aren't enough.
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 20:38:582734 *
2735 * Binary files are displayed with "Bin XXX -> YYY bytes"
2736 * instead of the change count and graph. This part is treated
2737 * similarly to the graph part, except that it is not
Stefano Lattarini41ccfdd2013-04-11 22:36:102738 * "scaled". If total width is too small to accommodate the
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 20:38:582739 * guaranteed minimum width of the filename part and the
2740 * separators and this message, this message will "overflow"
2741 * making the line longer than the maximum width.
Junio C Hamanoa2540022006-09-27 01:53:022742 */
Junio C Hamanoa2540022006-09-27 01:53:022743
Junio C Hamanoce8529b2022-10-21 21:53:252744 /*
2745 * NEEDSWORK: line_prefix is often used for "log --graph" output
2746 * and contains ANSI-colored string. utf8_strnwidth() should be
2747 * used to correctly count the display width instead of strlen().
2748 */
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432749 if (options->stat_width == -1)
Junio C Hamanocd48dad2016-08-31 23:27:192750 width = term_columns() - strlen(line_prefix);
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432751 else
2752 width = options->stat_width ? options->stat_width : 80;
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 20:38:582753 number_width = decimal_width(max_change) > number_width ?
2754 decimal_width(max_change) : number_width;
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432755
Dragan Simicbd48adc2023-09-11 15:39:442756 if (options->stat_name_width == -1)
2757 options->stat_name_width = diff_stat_name_width;
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 12:26:462758 if (options->stat_graph_width == -1)
2759 options->stat_graph_width = diff_stat_graph_width;
2760
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432761 /*
Dragan Simicbd48adc2023-09-11 15:39:442762 * Guarantee 3/8*16 == 6 for the graph part
2763 * and 5/8*16 == 10 for the filename part
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432764 */
2765 if (width < 16 + 6 + number_width)
2766 width = 16 + 6 + number_width;
2767
2768 /*
2769 * First assign sizes that are wanted, ignoring available width.
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 20:38:582770 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2771 * starting from "XXX" should fit in graph_width.
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432772 */
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 20:38:582773 graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4;
2774 if (options->stat_graph_width &&
2775 options->stat_graph_width < graph_width)
2776 graph_width = options->stat_graph_width;
2777
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432778 name_width = (options->stat_name_width > 0 &&
2779 options->stat_name_width < max_len) ?
2780 options->stat_name_width : max_len;
2781
2782 /*
2783 * Adjust adjustable widths not to exceed maximum width
2784 */
2785 if (name_width + number_width + 6 + graph_width > width) {
Lucian Poston678c5742012-04-18 21:12:182786 if (graph_width > width * 3/8 - number_width - 6) {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432787 graph_width = width * 3/8 - number_width - 6;
Lucian Poston678c5742012-04-18 21:12:182788 if (graph_width < 6)
2789 graph_width = 6;
2790 }
2791
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 12:26:452792 if (options->stat_graph_width &&
2793 graph_width > options->stat_graph_width)
2794 graph_width = options->stat_graph_width;
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432795 if (name_width > width - number_width - 6 - graph_width)
2796 name_width = width - number_width - 6 - graph_width;
2797 else
2798 graph_width = width - number_width - 6 - name_width;
2799 }
2800
2801 /*
2802 * From here name_width is the width of the name area,
2803 * and graph_width is the width of the graph area.
2804 * max_change is used to scale graph properly.
2805 */
Michael J Gruber808e1db2011-05-27 12:36:412806 for (i = 0; i < count; i++) {
Timo Hirvonend2543b82006-06-24 17:20:322807 const char *prefix = "";
Junio C Hamanoaf0ed812012-11-27 19:24:542808 struct diffstat_file *file = data->files[i];
2809 char *name = file->print_name;
2810 uintmax_t added = file->added;
2811 uintmax_t deleted = file->deleted;
Torsten Bögershausen12fc4ad2022-09-14 15:13:332812 int name_len, padding;
Junio C Hamano6973dca2006-04-22 06:57:452813
Junio C Hamanoa20d3c02012-11-27 19:47:462814 if (!file->is_interesting && (added + deleted == 0))
Michael J Gruber358e4602011-05-27 12:36:402815 continue;
Junio C Hamanoa20d3c02012-11-27 19:47:462816
Junio C Hamano6973dca2006-04-22 06:57:452817 /*
2818 * "scale" the filename
2819 */
Junio C Hamanoa2540022006-09-27 01:53:022820 len = name_width;
Torsten Bögershausen12fc4ad2022-09-14 15:13:332821 name_len = utf8_strwidth(name);
Junio C Hamanoa2540022006-09-27 01:53:022822 if (name_width < name_len) {
Junio C Hamano6973dca2006-04-22 06:57:452823 char *slash;
2824 prefix = "...";
Junio C Hamanoa2540022006-09-27 01:53:022825 len -= 3;
Junio C Hamanoce8529b2022-10-21 21:53:252826 /*
2827 * NEEDSWORK: (name_len - len) counts the display
2828 * width, which would be shorter than the byte
2829 * length of the corresponding substring.
2830 * Advancing "name" by that number of bytes does
2831 * *NOT* skip over that many columns, so it is
2832 * very likely that chomping the pathname at the
2833 * slash we will find starting from "name" will
2834 * leave the resulting string still too long.
2835 */
Junio C Hamanoa2540022006-09-27 01:53:022836 name += name_len - len;
Junio C Hamano6973dca2006-04-22 06:57:452837 slash = strchr(name, '/');
2838 if (slash)
2839 name = slash;
2840 }
Torsten Bögershausen12fc4ad2022-09-14 15:13:332841 padding = len - utf8_strwidth(name);
2842 if (padding < 0)
2843 padding = 0;
Junio C Hamano6973dca2006-04-22 06:57:452844
Junio C Hamanoaf0ed812012-11-27 19:24:542845 if (file->is_binary) {
Torsten Bögershausen12fc4ad2022-09-14 15:13:332846 strbuf_addf(&out, " %s%s%*s | %*s",
2847 prefix, name, padding, "",
2848 number_width, "Bin");
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 17:10:142849 if (!added && !deleted) {
Stefan Beller0911c472017-06-30 00:07:022850 strbuf_addch(&out, '\n');
2851 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2852 out.buf, out.len, 0);
2853 strbuf_reset(&out);
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 17:10:142854 continue;
2855 }
Stefan Beller0911c472017-06-30 00:07:022856 strbuf_addf(&out, " %s%"PRIuMAX"%s",
Jeff King0974c112010-04-17 17:41:082857 del_c, deleted, reset);
Stefan Beller0911c472017-06-30 00:07:022858 strbuf_addstr(&out, " -> ");
2859 strbuf_addf(&out, "%s%"PRIuMAX"%s",
Jeff King0974c112010-04-17 17:41:082860 add_c, added, reset);
Stefan Beller0911c472017-06-30 00:07:022861 strbuf_addstr(&out, " bytes\n");
2862 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2863 out.buf, out.len, 0);
2864 strbuf_reset(&out);
Junio C Hamanof6046522007-12-12 07:46:302865 continue;
Junio C Hamano6973dca2006-04-22 06:57:452866 }
Junio C Hamanoaf0ed812012-11-27 19:24:542867 else if (file->is_unmerged) {
Torsten Bögershausen12fc4ad2022-09-14 15:13:332868 strbuf_addf(&out, " %s%s%*s | %*s",
2869 prefix, name, padding, "",
Peter Grayson209d9cb2022-12-14 17:41:512870 number_width, "Unmerged\n");
Stefan Beller0911c472017-06-30 00:07:022871 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2872 out.buf, out.len, 0);
2873 strbuf_reset(&out);
Junio C Hamanof6046522007-12-12 07:46:302874 continue;
Junio C Hamano6973dca2006-04-22 06:57:452875 }
Junio C Hamano6973dca2006-04-22 06:57:452876
Junio C Hamanoa2540022006-09-27 01:53:022877 /*
2878 * scale the add/delete
2879 */
Junio C Hamano6973dca2006-04-22 06:57:452880 add = added;
2881 del = deleted;
Junio C Hamano6973dca2006-04-22 06:57:452882
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432883 if (graph_width <= max_change) {
Stefan Bellerd3c9cf32013-07-14 21:35:492884 int total = scale_linear(add + del, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 21:49:112885 if (total < 2 && add && del)
2886 /* width >= 2 due to the sanity check */
2887 total = 2;
2888 if (add < del) {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432889 add = scale_linear(add, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 21:49:112890 del = total - add;
2891 } else {
Zbigniew Jędrzejewski-Szmek1b058bc2012-03-01 12:26:432892 del = scale_linear(del, graph_width, max_change);
Junio C Hamano2eeeef22012-02-14 21:49:112893 add = total - del;
2894 }
Junio C Hamano6973dca2006-04-22 06:57:452895 }
Torsten Bögershausen12fc4ad2022-09-14 15:13:332896 strbuf_addf(&out, " %s%s%*s | %*"PRIuMAX"%s",
2897 prefix, name, padding, "",
2898 number_width, added + deleted,
2899 added + deleted ? " " : "");
Stefan Beller0911c472017-06-30 00:07:022900 show_graph(&out, '+', add, add_c, reset);
2901 show_graph(&out, '-', del, del_c, reset);
2902 strbuf_addch(&out, '\n');
2903 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2904 out.buf, out.len, 0);
2905 strbuf_reset(&out);
Junio C Hamano6973dca2006-04-22 06:57:452906 }
Junio C Hamanoa20d3c02012-11-27 19:47:462907
2908 for (i = 0; i < data->nr; i++) {
Junio C Hamanoaf0ed812012-11-27 19:24:542909 struct diffstat_file *file = data->files[i];
2910 uintmax_t added = file->added;
2911 uintmax_t deleted = file->deleted;
Junio C Hamano82dfc2c2012-11-27 20:05:102912
2913 if (file->is_unmerged ||
2914 (!file->is_interesting && (added + deleted == 0))) {
Michael J Gruber808e1db2011-05-27 12:36:412915 total_files--;
2916 continue;
2917 }
Junio C Hamanoa20d3c02012-11-27 19:47:462918
Junio C Hamano82dfc2c2012-11-27 20:05:102919 if (!file->is_binary) {
Junio C Hamanoa20d3c02012-11-27 19:47:462920 adds += added;
2921 dels += deleted;
2922 }
2923 if (i < count)
2924 continue;
Junio C Hamanoe5f85df2011-05-28 04:50:392925 if (!extra_shown)
Stefan Beller0911c472017-06-30 00:07:022926 emit_diff_symbol(options,
2927 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
2928 NULL, 0, 0);
Junio C Hamanoe5f85df2011-05-28 04:50:392929 extra_shown = 1;
Michael J Gruber808e1db2011-05-27 12:36:412930 }
Stefan Beller0911c472017-06-30 00:07:022931
2932 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
Rene Scharfe5a612012017-08-30 17:49:432933 strbuf_release(&out);
Junio C Hamano6973dca2006-04-22 06:57:452934}
2935
Felipe Contreras2775d922009-10-11 20:46:112936static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
Nicolas Pitreebd124c2006-12-15 04:15:442937{
2938 int i, adds = 0, dels = 0, total_files = data->nr;
2939
2940 if (data->nr == 0)
2941 return;
2942
2943 for (i = 0; i < data->nr; i++) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 17:10:142944 int added = data->files[i]->added;
Stefan Beller0911c472017-06-30 00:07:022945 int deleted = data->files[i]->deleted;
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 17:10:142946
Junio C Hamano20c8cde2012-11-27 22:19:362947 if (data->files[i]->is_unmerged ||
2948 (!data->files[i]->is_interesting && (added + deleted == 0))) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 17:10:142949 total_files--;
Alexander Strasserde9658b2012-06-15 21:50:302950 } else if (!data->files[i]->is_binary) { /* don't count bytes */
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 17:10:142951 adds += added;
2952 dels += deleted;
Nicolas Pitreebd124c2006-12-15 04:15:442953 }
Nicolas Pitreebd124c2006-12-15 04:15:442954 }
Stefan Beller0911c472017-06-30 00:07:022955 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
Nicolas Pitreebd124c2006-12-15 04:15:442956}
2957
Felipe Contreras4b25d092009-05-01 09:06:362958static void show_numstat(struct diffstat_t *data, struct diff_options *options)
Junio C Hamano74e2abe2006-10-12 10:01:002959{
2960 int i;
2961
Junio C Hamanof6046522007-12-12 07:46:302962 if (data->nr == 0)
2963 return;
2964
Junio C Hamano74e2abe2006-10-12 10:01:002965 for (i = 0; i < data->nr; i++) {
2966 struct diffstat_file *file = data->files[i];
2967
John Keeping30997bb2013-02-07 20:15:272968 fprintf(options->file, "%s", diff_line_prefix(options));
Bo Yang7be57612010-05-26 07:23:542969
Junio C Hamanobfddbc52006-12-10 21:50:592970 if (file->is_binary)
Daniel Barkalowc0c77732008-03-10 02:43:392971 fprintf(options->file, "-\t-\t");
Junio C Hamanobfddbc52006-12-10 21:50:592972 else
Daniel Barkalowc0c77732008-03-10 02:43:392973 fprintf(options->file,
Jeff King0974c112010-04-17 17:41:082974 "%"PRIuMAX"\t%"PRIuMAX"\t",
2975 file->added, file->deleted);
Junio C Hamanof6046522007-12-12 07:46:302976 if (options->line_termination) {
2977 fill_print_name(file);
2978 if (!file->is_renamed)
Daniel Barkalowc0c77732008-03-10 02:43:392979 write_name_quoted(file->name, options->file,
Junio C Hamanof6046522007-12-12 07:46:302980 options->line_termination);
2981 else {
Daniel Barkalowc0c77732008-03-10 02:43:392982 fputs(file->print_name, options->file);
2983 putc(options->line_termination, options->file);
Junio C Hamanof6046522007-12-12 07:46:302984 }
Pierre Habouzit663af342007-09-19 22:42:152985 } else {
Junio C Hamanof6046522007-12-12 07:46:302986 if (file->is_renamed) {
Daniel Barkalowc0c77732008-03-10 02:43:392987 putc('\0', options->file);
2988 write_name_quoted(file->from_name, options->file, '\0');
Junio C Hamanof6046522007-12-12 07:46:302989 }
Daniel Barkalowc0c77732008-03-10 02:43:392990 write_name_quoted(file->name, options->file, '\0');
Pierre Habouzit663af342007-09-19 22:42:152991 }
Junio C Hamano74e2abe2006-10-12 10:01:002992 }
2993}
2994
Junio C Hamanoc04a7152008-02-13 01:06:582995struct dirstat_file {
2996 const char *name;
2997 unsigned long changed;
Linus Torvalds7df7c012008-02-12 21:26:312998};
2999
Junio C Hamanoc04a7152008-02-13 01:06:583000struct dirstat_dir {
3001 struct dirstat_file *files;
Johan Herland712d2c72011-04-29 09:36:203002 int alloc, nr, permille, cumulative;
Junio C Hamanoc04a7152008-02-13 01:06:583003};
3004
Bo Yang7be57612010-05-26 07:23:543005static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
3006 unsigned long changed, const char *base, int baselen)
Linus Torvalds7df7c012008-02-12 21:26:313007{
Brandon Williams585c0e22018-02-14 18:59:283008 unsigned long sum_changes = 0;
Linus Torvalds7df7c012008-02-12 21:26:313009 unsigned int sources = 0;
John Keeping30997bb2013-02-07 20:15:273010 const char *line_prefix = diff_line_prefix(opt);
Linus Torvalds7df7c012008-02-12 21:26:313011
3012 while (dir->nr) {
Junio C Hamanoc04a7152008-02-13 01:06:583013 struct dirstat_file *f = dir->files;
Linus Torvalds7df7c012008-02-12 21:26:313014 int namelen = strlen(f->name);
Brandon Williams585c0e22018-02-14 18:59:283015 unsigned long changes;
Linus Torvalds7df7c012008-02-12 21:26:313016 char *slash;
3017
3018 if (namelen < baselen)
3019 break;
3020 if (memcmp(f->name, base, baselen))
3021 break;
3022 slash = strchr(f->name + baselen, '/');
3023 if (slash) {
3024 int newbaselen = slash + 1 - f->name;
Brandon Williams585c0e22018-02-14 18:59:283025 changes = gather_dirstat(opt, dir, changed, f->name, newbaselen);
Linus Torvalds7df7c012008-02-12 21:26:313026 sources++;
3027 } else {
Brandon Williams585c0e22018-02-14 18:59:283028 changes = f->changed;
Linus Torvalds7df7c012008-02-12 21:26:313029 dir->files++;
3030 dir->nr--;
3031 sources += 2;
3032 }
Brandon Williams585c0e22018-02-14 18:59:283033 sum_changes += changes;
Linus Torvalds7df7c012008-02-12 21:26:313034 }
3035
3036 /*
3037 * We don't report dirstat's for
3038 * - the top level
3039 * - or cases where everything came from a single directory
3040 * under this directory (sources == 1).
3041 */
3042 if (baselen && sources != 1) {
Brandon Williams585c0e22018-02-14 18:59:283043 if (sum_changes) {
3044 int permille = sum_changes * 1000 / changed;
Johan Herland712d2c72011-04-29 09:36:203045 if (permille >= dir->permille) {
Bo Yang7be57612010-05-26 07:23:543046 fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix,
Johan Herland712d2c72011-04-29 09:36:203047 permille / 10, permille % 10, baselen, base);
Linus Torvalds7df7c012008-02-12 21:26:313048 if (!dir->cumulative)
3049 return 0;
3050 }
3051 }
3052 }
Brandon Williams585c0e22018-02-14 18:59:283053 return sum_changes;
Linus Torvalds7df7c012008-02-12 21:26:313054}
3055
Linus Torvalds441bca02008-08-28 23:19:083056static int dirstat_compare(const void *_a, const void *_b)
3057{
3058 const struct dirstat_file *a = _a;
3059 const struct dirstat_file *b = _b;
3060 return strcmp(a->name, b->name);
3061}
3062
Junio C Hamano34a94892023-05-05 21:19:163063static void conclude_dirstat(struct diff_options *options,
3064 struct dirstat_dir *dir,
3065 unsigned long changed)
3066{
Junio C Hamano83973982023-05-05 21:19:173067 struct dirstat_file *to_free = dir->files;
Junio C Hamano34a94892023-05-05 21:19:163068
Junio C Hamano83973982023-05-05 21:19:173069 if (!changed) {
3070 /* This can happen even with many files, if everything was renames */
3071 ;
3072 } else {
3073 /* Show all directories with more than x% of the changes */
3074 QSORT(dir->files, dir->nr, dirstat_compare);
3075 gather_dirstat(options, dir, changed, "", 0);
3076 }
3077
3078 free(to_free);
Junio C Hamano34a94892023-05-05 21:19:163079}
3080
Junio C Hamanoc04a7152008-02-13 01:06:583081static void show_dirstat(struct diff_options *options)
Linus Torvalds7df7c012008-02-12 21:26:313082{
3083 int i;
3084 unsigned long changed;
Junio C Hamanoc04a7152008-02-13 01:06:583085 struct dirstat_dir dir;
3086 struct diff_queue_struct *q = &diff_queued_diff;
Linus Torvalds7df7c012008-02-12 21:26:313087
Junio C Hamanoc04a7152008-02-13 01:06:583088 dir.files = NULL;
3089 dir.alloc = 0;
3090 dir.nr = 0;
Johan Herland712d2c72011-04-29 09:36:203091 dir.permille = options->dirstat_permille;
Brandon Williams0d1e0e72017-10-31 18:19:113092 dir.cumulative = options->flags.dirstat_cumulative;
Junio C Hamanoc04a7152008-02-13 01:06:583093
Linus Torvalds7df7c012008-02-12 21:26:313094 changed = 0;
Junio C Hamanoc04a7152008-02-13 01:06:583095 for (i = 0; i < q->nr; i++) {
3096 struct diff_filepair *p = q->queue[i];
3097 const char *name;
3098 unsigned long copied, added, damage;
Jonathan Tan1c37e862020-04-07 22:11:413099 struct diff_populate_filespec_options dpf_options = {
3100 .check_size_only = 1,
3101 };
Junio C Hamanoc04a7152008-02-13 01:06:583102
Johan Herland2ca86712011-04-12 09:24:343103 name = p->two->path ? p->two->path : p->one->path;
Junio C Hamanoc04a7152008-02-13 01:06:583104
Jeff Kingd9f62df2018-08-28 21:23:033105 if (p->one->oid_valid && p->two->oid_valid &&
3106 oideq(&p->one->oid, &p->two->oid)) {
Johan Herland2ff3a802011-04-10 22:48:523107 /*
3108 * The SHA1 has not changed, so pre-/post-content is
3109 * identical. We can therefore skip looking at the
3110 * file contents altogether.
3111 */
3112 damage = 0;
3113 goto found_damage;
3114 }
3115
Brandon Williams0d1e0e72017-10-31 18:19:113116 if (options->flags.dirstat_by_file) {
Johan Herland0133dab2011-04-10 22:48:513117 /*
3118 * In --dirstat-by-file mode, we don't really need to
3119 * look at the actual file contents at all.
3120 * The fact that the SHA1 changed is enough for us to
3121 * add this file to the list of results
3122 * (with each file contributing equal damage).
3123 */
Johan Herland2ff3a802011-04-10 22:48:523124 damage = 1;
Johan Herland0133dab2011-04-10 22:48:513125 goto found_damage;
3126 }
Junio C Hamanoc04a7152008-02-13 01:06:583127
3128 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
Jonathan Tan1c37e862020-04-07 22:11:413129 diff_populate_filespec(options->repo, p->one, NULL);
3130 diff_populate_filespec(options->repo, p->two, NULL);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:193131 diffcore_count_changes(options->repo,
3132 p->one, p->two, NULL, NULL,
Junio C Hamanoc04a7152008-02-13 01:06:583133 &copied, &added);
3134 diff_free_filespec_data(p->one);
3135 diff_free_filespec_data(p->two);
3136 } else if (DIFF_FILE_VALID(p->one)) {
Jonathan Tan1c37e862020-04-07 22:11:413137 diff_populate_filespec(options->repo, p->one, &dpf_options);
Junio C Hamanoc04a7152008-02-13 01:06:583138 copied = added = 0;
3139 diff_free_filespec_data(p->one);
3140 } else if (DIFF_FILE_VALID(p->two)) {
Jonathan Tan1c37e862020-04-07 22:11:413141 diff_populate_filespec(options->repo, p->two, &dpf_options);
Junio C Hamanoc04a7152008-02-13 01:06:583142 copied = 0;
3143 added = p->two->size;
3144 diff_free_filespec_data(p->two);
3145 } else
Junio C Hamano2b0b5512008-02-25 01:37:153146 continue;
Junio C Hamanoc04a7152008-02-13 01:06:583147
3148 /*
3149 * Original minus copied is the removed material,
3150 * added is the new material. They are both damages
Johan Herland0133dab2011-04-10 22:48:513151 * made to the preimage.
Johan Herland2ff3a802011-04-10 22:48:523152 * If the resulting damage is zero, we know that
3153 * diffcore_count_changes() considers the two entries to
Jeff Kingd9f62df2018-08-28 21:23:033154 * be identical, but since the oid changed, we
Johan Herland2ff3a802011-04-10 22:48:523155 * know that there must have been _some_ kind of change,
3156 * so we force all entries to have damage > 0.
Junio C Hamanoc04a7152008-02-13 01:06:583157 */
3158 damage = (p->one->size - copied) + added;
Johan Herland2ff3a802011-04-10 22:48:523159 if (!damage)
Heikki Orsilafd337772008-09-05 19:27:353160 damage = 1;
Junio C Hamanoc04a7152008-02-13 01:06:583161
Johan Herland0133dab2011-04-10 22:48:513162found_damage:
Junio C Hamanoc04a7152008-02-13 01:06:583163 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
3164 dir.files[dir.nr].name = name;
3165 dir.files[dir.nr].changed = damage;
3166 changed += damage;
3167 dir.nr++;
Linus Torvalds7df7c012008-02-12 21:26:313168 }
3169
Junio C Hamano34a94892023-05-05 21:19:163170 conclude_dirstat(options, &dir, changed);
Linus Torvalds7df7c012008-02-12 21:26:313171}
3172
Johan Herland1c57a622011-04-29 09:36:213173static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options)
3174{
3175 int i;
3176 unsigned long changed;
3177 struct dirstat_dir dir;
3178
3179 if (data->nr == 0)
3180 return;
3181
3182 dir.files = NULL;
3183 dir.alloc = 0;
3184 dir.nr = 0;
3185 dir.permille = options->dirstat_permille;
Brandon Williams0d1e0e72017-10-31 18:19:113186 dir.cumulative = options->flags.dirstat_cumulative;
Johan Herland1c57a622011-04-29 09:36:213187
3188 changed = 0;
3189 for (i = 0; i < data->nr; i++) {
3190 struct diffstat_file *file = data->files[i];
3191 unsigned long damage = file->added + file->deleted;
3192 if (file->is_binary)
3193 /*
3194 * binary files counts bytes, not lines. Must find some
3195 * way to normalize binary bytes vs. textual lines.
3196 * The following heuristic assumes that there are 64
3197 * bytes per "line".
3198 * This is stupid and ugly, but very cheap...
3199 */
René Scharfe42c78a22017-07-08 10:35:353200 damage = DIV_ROUND_UP(damage, 64);
Johan Herland1c57a622011-04-29 09:36:213201 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
3202 dir.files[dir.nr].name = file->name;
3203 dir.files[dir.nr].changed = damage;
3204 changed += damage;
3205 dir.nr++;
3206 }
3207
Junio C Hamano34a94892023-05-05 21:19:163208 conclude_dirstat(options, &dir, changed);
Johan Herland1c57a622011-04-29 09:36:213209}
3210
Matthew Rogers1cf3d5d2020-08-20 00:41:323211static void free_diffstat_file(struct diffstat_file *f)
3212{
3213 free(f->print_name);
3214 free(f->name);
3215 free(f->from_name);
3216 free(f);
3217}
3218
Daniel Ferreirae4cb6592019-11-13 12:40:583219void free_diffstat_info(struct diffstat_t *diffstat)
Junio C Hamanof6046522007-12-12 07:46:303220{
3221 int i;
Matthew Rogers1cf3d5d2020-08-20 00:41:323222 for (i = 0; i < diffstat->nr; i++)
3223 free_diffstat_file(diffstat->files[i]);
Junio C Hamanof6046522007-12-12 07:46:303224 free(diffstat->files);
3225}
3226
Johannes Schindelin88246892006-05-20 21:43:133227struct checkdiff_t {
Johannes Schindelin88246892006-05-20 21:43:133228 const char *filename;
Junio C Hamano1ba111d2008-06-26 22:36:343229 int lineno;
Junio C Hamanoa757c642010-03-25 02:21:323230 int conflict_marker_size;
Junio C Hamano1ba111d2008-06-26 22:36:343231 struct diff_options *o;
Junio C Hamanocf1b7862007-12-06 08:14:143232 unsigned ws_rule;
Wincent Colaiuta62c64892007-12-13 20:24:523233 unsigned status;
Johannes Schindelin88246892006-05-20 21:43:133234};
3235
Junio C Hamanoa757c642010-03-25 02:21:323236static int is_conflict_marker(const char *line, int marker_size, unsigned long len)
Junio C Hamano04954042008-06-26 22:37:213237{
3238 char firstchar;
3239 int cnt;
3240
Junio C Hamanoa757c642010-03-25 02:21:323241 if (len < marker_size + 1)
Junio C Hamano04954042008-06-26 22:37:213242 return 0;
3243 firstchar = line[0];
3244 switch (firstchar) {
Junio C Hamanoa757c642010-03-25 02:21:323245 case '=': case '>': case '<': case '|':
Junio C Hamano04954042008-06-26 22:37:213246 break;
3247 default:
3248 return 0;
3249 }
Junio C Hamanoa757c642010-03-25 02:21:323250 for (cnt = 1; cnt < marker_size; cnt++)
Junio C Hamano04954042008-06-26 22:37:213251 if (line[cnt] != firstchar)
3252 return 0;
Elijah Newren15beaaa2019-11-05 17:07:233253 /* line[1] through line[marker_size-1] are same as firstchar */
Junio C Hamanoa757c642010-03-25 02:21:323254 if (len < marker_size + 1 || !isspace(line[marker_size]))
Junio C Hamano04954042008-06-26 22:37:213255 return 0;
Junio C Hamano04954042008-06-26 22:37:213256 return 1;
3257}
3258
Jeff King75ab7632018-11-02 06:39:033259static void checkdiff_consume_hunk(void *priv,
Jeff King61bdc7c2022-12-13 11:13:483260 long ob UNUSED, long on UNUSED,
3261 long nb, long nn UNUSED,
3262 const char *func UNUSED, long funclen UNUSED)
Jeff King75ab7632018-11-02 06:39:033263
3264{
3265 struct checkdiff_t *data = priv;
3266 data->lineno = nb - 1;
3267}
3268
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 17:15:243269static int checkdiff_consume(void *priv, char *line, unsigned long len)
Johannes Schindelin88246892006-05-20 21:43:133270{
3271 struct checkdiff_t *data = priv;
Junio C Hamanoa757c642010-03-25 02:21:323272 int marker_size = data->conflict_marker_size;
Jeff Kingf1c96262011-08-18 05:03:123273 const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE);
3274 const char *reset = diff_get_color(data->o->use_color, DIFF_RESET);
3275 const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW);
Wincent Colaiutac1795bb2007-12-13 13:32:293276 char *err;
John Keeping30997bb2013-02-07 20:15:273277 const char *line_prefix;
Bo Yang7be57612010-05-26 07:23:543278
3279 assert(data->o);
John Keeping30997bb2013-02-07 20:15:273280 line_prefix = diff_line_prefix(data->o);
Johannes Schindelin88246892006-05-20 21:43:133281
3282 if (line[0] == '+') {
Junio C Hamano18374e52008-06-26 20:16:333283 unsigned bad;
Junio C Hamano0ef617f2008-02-16 04:30:053284 data->lineno++;
Junio C Hamanoa757c642010-03-25 02:21:323285 if (is_conflict_marker(line + 1, marker_size, len - 1)) {
Junio C Hamano04954042008-06-26 22:37:213286 data->status |= 1;
3287 fprintf(data->o->file,
Bo Yang7be57612010-05-26 07:23:543288 "%s%s:%d: leftover conflict marker\n",
3289 line_prefix, data->filename, data->lineno);
Junio C Hamano04954042008-06-26 22:37:213290 }
Junio C Hamano8f8841e2008-06-26 22:35:213291 bad = ws_check(line + 1, len - 1, data->ws_rule);
Junio C Hamano18374e52008-06-26 20:16:333292 if (!bad)
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 17:15:243293 return 0;
Junio C Hamano18374e52008-06-26 20:16:333294 data->status |= bad;
3295 err = whitespace_error_string(bad);
Bo Yang7be57612010-05-26 07:23:543296 fprintf(data->o->file, "%s%s:%d: %s.\n",
3297 line_prefix, data->filename, data->lineno, err);
Wincent Colaiutac1795bb2007-12-13 13:32:293298 free(err);
Bo Yanga3c158d2010-05-26 07:08:023299 emit_line(data->o, set, reset, line, 1);
Junio C Hamano8f8841e2008-06-26 22:35:213300 ws_check_emit(line + 1, len - 1, data->ws_rule,
Junio C Hamano1ba111d2008-06-26 22:36:343301 data->o->file, set, reset, ws);
Junio C Hamano877f23c2008-06-26 22:36:593302 } else if (line[0] == ' ') {
Johannes Schindelin88246892006-05-20 21:43:133303 data->lineno++;
Johannes Schindelin88246892006-05-20 21:43:133304 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 17:15:243305 return 0;
Johannes Schindelin88246892006-05-20 21:43:133306}
3307
Junio C Hamano06606262006-05-05 09:41:533308static unsigned char *deflate_it(char *data,
3309 unsigned long size,
3310 unsigned long *result_size)
Junio C Hamano051308f2006-05-04 23:51:443311{
Junio C Hamano06606262006-05-05 09:41:533312 int bound;
3313 unsigned char *deflated;
Junio C Hamanoef49a7a2011-06-10 18:52:153314 git_zstream stream;
Junio C Hamano051308f2006-05-04 23:51:443315
Junio C Hamano55bb5c92011-06-10 17:55:103316 git_deflate_init(&stream, zlib_compression_level);
Junio C Hamano225a6f12011-06-10 18:18:173317 bound = git_deflate_bound(&stream, size);
Junio C Hamano06606262006-05-05 09:41:533318 deflated = xmalloc(bound);
3319 stream.next_out = deflated;
3320 stream.avail_out = bound;
Junio C Hamano051308f2006-05-04 23:51:443321
Junio C Hamano06606262006-05-05 09:41:533322 stream.next_in = (unsigned char *)data;
3323 stream.avail_in = size;
Junio C Hamano55bb5c92011-06-10 17:55:103324 while (git_deflate(&stream, Z_FINISH) == Z_OK)
Junio C Hamano06606262006-05-05 09:41:533325 ; /* nothing */
Junio C Hamano55bb5c92011-06-10 17:55:103326 git_deflate_end(&stream);
Junio C Hamano06606262006-05-05 09:41:533327 *result_size = stream.total_out;
3328 return deflated;
Junio C Hamano051308f2006-05-04 23:51:443329}
3330
Stefan Beller4eed0eb2017-06-30 00:07:013331static void emit_binary_diff_body(struct diff_options *o,
3332 mmfile_t *one, mmfile_t *two)
Junio C Hamano051308f2006-05-04 23:51:443333{
Junio C Hamano06606262006-05-05 09:41:533334 void *cp;
3335 void *delta;
3336 void *deflated;
3337 void *data;
3338 unsigned long orig_size;
Junio C Hamano051308f2006-05-04 23:51:443339 unsigned long delta_size;
Junio C Hamano06606262006-05-05 09:41:533340 unsigned long deflate_size;
3341 unsigned long data_size;
Junio C Hamano051308f2006-05-04 23:51:443342
Junio C Hamano06606262006-05-05 09:41:533343 /* We could do deflated delta, or we could do just deflated two,
3344 * whichever is smaller.
3345 */
3346 delta = NULL;
3347 deflated = deflate_it(two->ptr, two->size, &deflate_size);
3348 if (one->size && two->size) {
3349 delta = diff_delta(one->ptr, one->size,
3350 two->ptr, two->size,
3351 &delta_size, deflate_size);
3352 if (delta) {
3353 void *to_free = delta;
3354 orig_size = delta_size;
3355 delta = deflate_it(delta, delta_size, &delta_size);
3356 free(to_free);
3357 }
3358 }
Junio C Hamano051308f2006-05-04 23:51:443359
Junio C Hamano06606262006-05-05 09:41:533360 if (delta && delta_size < deflate_size) {
Torsten Bögershausenca473ce2018-11-11 07:05:043361 char *s = xstrfmt("%"PRIuMAX , (uintmax_t)orig_size);
Stefan Beller4eed0eb2017-06-30 00:07:013362 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
3363 s, strlen(s), 0);
3364 free(s);
Junio C Hamano06606262006-05-05 09:41:533365 free(deflated);
3366 data = delta;
3367 data_size = delta_size;
Stefan Beller4eed0eb2017-06-30 00:07:013368 } else {
3369 char *s = xstrfmt("%lu", two->size);
3370 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
3371 s, strlen(s), 0);
3372 free(s);
Junio C Hamano06606262006-05-05 09:41:533373 free(delta);
3374 data = deflated;
3375 data_size = deflate_size;
3376 }
3377
3378 /* emit data encoded in base85 */
3379 cp = data;
3380 while (data_size) {
Stefan Beller4eed0eb2017-06-30 00:07:013381 int len;
Junio C Hamano06606262006-05-05 09:41:533382 int bytes = (52 < data_size) ? 52 : data_size;
Stefan Beller4eed0eb2017-06-30 00:07:013383 char line[71];
Junio C Hamano06606262006-05-05 09:41:533384 data_size -= bytes;
Junio C Hamano051308f2006-05-04 23:51:443385 if (bytes <= 26)
3386 line[0] = bytes + 'A' - 1;
3387 else
3388 line[0] = bytes - 26 + 'a' - 1;
3389 encode_85(line + 1, cp, bytes);
Florian Forster1d7f1712006-06-18 15:18:093390 cp = (char *) cp + bytes;
Stefan Beller4eed0eb2017-06-30 00:07:013391
3392 len = strlen(line);
3393 line[len++] = '\n';
3394 line[len] = '\0';
3395
3396 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY,
3397 line, len, 0);
Junio C Hamano051308f2006-05-04 23:51:443398 }
Stefan Beller4eed0eb2017-06-30 00:07:013399 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0);
Junio C Hamano06606262006-05-05 09:41:533400 free(data);
Junio C Hamano051308f2006-05-04 23:51:443401}
3402
Stefan Beller4eed0eb2017-06-30 00:07:013403static void emit_binary_diff(struct diff_options *o,
3404 mmfile_t *one, mmfile_t *two)
Junio C Hamanod4c452f2006-08-16 23:08:143405{
Stefan Beller4eed0eb2017-06-30 00:07:013406 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0);
3407 emit_binary_diff_body(o, one, two);
3408 emit_binary_diff_body(o, two, one);
Junio C Hamanod4c452f2006-08-16 23:08:143409}
3410
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:193411int diff_filespec_is_binary(struct repository *r,
3412 struct diff_filespec *one)
Junio C Hamano29a3eef2007-07-06 07:18:543413{
Jonathan Tan1c37e862020-04-07 22:11:413414 struct diff_populate_filespec_options dpf_options = {
3415 .check_binary = 1,
3416 };
3417
Jeff King122aa6f2008-10-05 21:43:363418 if (one->is_binary == -1) {
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:333419 diff_filespec_load_driver(one, r->index);
Jeff King122aa6f2008-10-05 21:43:363420 if (one->driver->binary != -1)
3421 one->is_binary = one->driver->binary;
3422 else {
3423 if (!one->data && DIFF_FILE_VALID(one))
Jonathan Tan1c37e862020-04-07 22:11:413424 diff_populate_filespec(r, one, &dpf_options);
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 03:08:053425 if (one->is_binary == -1 && one->data)
Jeff King122aa6f2008-10-05 21:43:363426 one->is_binary = buffer_is_binary(one->data,
3427 one->size);
3428 if (one->is_binary == -1)
3429 one->is_binary = 0;
3430 }
3431 }
Junio C Hamano29a3eef2007-07-06 07:18:543432 return one->is_binary;
Junio C Hamano6973dca2006-04-22 06:57:453433}
3434
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:333435static const struct userdiff_funcname *
3436diff_funcname_pattern(struct diff_options *o, struct diff_filespec *one)
Junio C Hamanof2584752007-07-06 07:45:103437{
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:333438 diff_filespec_load_driver(one, o->repo->index);
Jeff King122aa6f2008-10-05 21:43:363439 return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
Junio C Hamanof2584752007-07-06 07:45:103440}
3441
Junio C Hamanoa5a818e2008-08-19 03:08:093442void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
3443{
3444 if (!options->a_prefix)
3445 options->a_prefix = a;
3446 if (!options->b_prefix)
3447 options->b_prefix = b;
3448}
3449
Jeff King6799aad2023-03-09 06:07:063450void diff_set_noprefix(struct diff_options *options)
3451{
3452 options->a_prefix = options->b_prefix = "";
3453}
3454
3455void diff_set_default_prefix(struct diff_options *options)
3456{
Patrick Steinhardtf9c19892024-05-27 11:46:203457 options->a_prefix = diff_src_prefix ? diff_src_prefix : "a/";
3458 options->b_prefix = diff_dst_prefix ? diff_dst_prefix : "b/";
Jeff King6799aad2023-03-09 06:07:063459}
3460
Nguyễn Thái Ngọc Duybd7ad452018-11-10 05:49:063461struct userdiff_driver *get_textconv(struct repository *r,
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:333462 struct diff_filespec *one)
Jeff King04427ac2008-10-26 04:44:533463{
3464 if (!DIFF_FILE_VALID(one))
3465 return NULL;
Jeff Kingd391c0f2010-09-21 21:01:243466
Nguyễn Thái Ngọc Duybd7ad452018-11-10 05:49:063467 diff_filespec_load_driver(one, r->index);
3468 return userdiff_get_textconv(r, one->driver);
Jeff King04427ac2008-10-26 04:44:533469}
3470
Johannes Schindelin2715e8a2022-06-18 00:20:553471static struct string_list *additional_headers(struct diff_options *o,
3472 const char *path)
Elijah Newren95433ee2022-02-02 02:37:343473{
3474 if (!o->additional_path_headers)
3475 return NULL;
3476 return strmap_get(o->additional_path_headers, path);
3477}
3478
Johannes Schindelin2715e8a2022-06-18 00:20:553479static void add_formatted_header(struct strbuf *msg,
3480 const char *header,
Elijah Newren95433ee2022-02-02 02:37:343481 const char *line_prefix,
3482 const char *meta,
3483 const char *reset)
3484{
Johannes Schindelin2715e8a2022-06-18 00:20:553485 const char *next, *newline;
Elijah Newren95433ee2022-02-02 02:37:343486
Johannes Schindelin2715e8a2022-06-18 00:20:553487 for (next = header; *next; next = newline) {
Elijah Newren95433ee2022-02-02 02:37:343488 newline = strchrnul(next, '\n');
3489 strbuf_addf(msg, "%s%s%.*s%s\n", line_prefix, meta,
3490 (int)(newline - next), next, reset);
3491 if (*newline)
3492 newline++;
3493 }
3494}
3495
Johannes Schindelin2715e8a2022-06-18 00:20:553496static void add_formatted_headers(struct strbuf *msg,
3497 struct string_list *more_headers,
3498 const char *line_prefix,
3499 const char *meta,
3500 const char *reset)
3501{
3502 int i;
3503
3504 for (i = 0; i < more_headers->nr; i++)
3505 add_formatted_header(msg, more_headers->items[i].string,
3506 line_prefix, meta, reset);
3507}
3508
Elijah Newren9b080912022-09-02 03:53:283509static int diff_filepair_is_phoney(struct diff_filespec *one,
3510 struct diff_filespec *two)
3511{
3512 /*
3513 * This function specifically looks for pairs injected by
3514 * create_filepairs_for_header_only_notifications(). Such
3515 * pairs are "phoney" in that they do not represent any
3516 * content or even mode difference, but were inserted because
3517 * diff_queued_diff previously had no pair associated with
3518 * that path but we needed some pair to avoid losing the
3519 * "remerge CONFLICT" header associated with the path.
3520 */
3521 return !DIFF_FILE_VALID(one) && !DIFF_FILE_VALID(two);
3522}
3523
John Cai11e95e12023-02-20 21:04:413524static int set_diff_algorithm(struct diff_options *opts,
3525 const char *alg)
3526{
3527 long value = parse_algorithm_value(alg);
3528
3529 if (value < 0)
3530 return -1;
3531
3532 /* clear out previous settings */
3533 DIFF_XDL_CLR(opts, NEED_MINIMAL);
3534 opts->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
3535 opts->xdl_opts |= value;
3536
3537 return 0;
3538}
3539
Junio C Hamano6973dca2006-04-22 06:57:453540static void builtin_diff(const char *name_a,
3541 const char *name_b,
3542 struct diff_filespec *one,
3543 struct diff_filespec *two,
3544 const char *xfrm_msg,
Christian Couder296c6bb2010-05-26 02:50:123545 int must_show_header,
Junio C Hamano051308f2006-05-04 23:51:443546 struct diff_options *o,
Junio C Hamano6973dca2006-04-22 06:57:453547 int complete_rewrite)
3548{
3549 mmfile_t mf1, mf2;
3550 const char *lbl[2];
3551 char *a_one, *b_two;
Jeff Kingd9c552f2012-11-13 15:42:463552 const char *meta = diff_get_color_opt(o, DIFF_METAINFO);
Pierre Habouzit8f67f8a2007-11-10 19:05:143553 const char *reset = diff_get_color_opt(o, DIFF_RESET);
Junio C Hamanoa5a818e2008-08-19 03:08:093554 const char *a_prefix, *b_prefix;
Jeff Kingd9bae1a2010-04-02 00:12:153555 struct userdiff_driver *textconv_one = NULL;
3556 struct userdiff_driver *textconv_two = NULL;
Greg Bacon3e97c7c2009-11-19 21:12:243557 struct strbuf header = STRBUF_INIT;
John Keeping30997bb2013-02-07 20:15:273558 const char *line_prefix = diff_line_prefix(o);
Junio C Hamano6973dca2006-04-22 06:57:453559
Junio C Hamanoa5a818e2008-08-19 03:08:093560 diff_set_mnemonic_prefix(o, "a/", "b/");
Brandon Williams0d1e0e72017-10-31 18:19:113561 if (o->flags.reverse_diff) {
Junio C Hamanoa5a818e2008-08-19 03:08:093562 a_prefix = o->b_prefix;
3563 b_prefix = o->a_prefix;
3564 } else {
3565 a_prefix = o->a_prefix;
3566 b_prefix = o->b_prefix;
3567 }
3568
Jacob Keller61cfbc02016-08-31 23:27:213569 if (o->submodule_format == DIFF_SUBMODULE_LOG &&
3570 (!one->mode || S_ISGITLINK(one->mode)) &&
Elijah Newren9b080912022-09-02 03:53:283571 (!two->mode || S_ISGITLINK(two->mode)) &&
3572 (!diff_filepair_is_phoney(one, two))) {
Shourya Shukla180b1542020-08-12 19:44:023573 show_submodule_diff_summary(o, one->path ? one->path : two->path,
Jacob Keller602a2832016-08-31 23:27:233574 &one->oid, &two->oid,
Stefan Bellerf3597132017-06-30 00:07:003575 two->dirty_submodule);
René Scharfe11591852024-09-08 07:08:353576 o->found_changes = 1;
Junio C Hamano6973dca2006-04-22 06:57:453577 return;
Jacob Kellerfd47ae62016-08-31 23:27:253578 } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF &&
3579 (!one->mode || S_ISGITLINK(one->mode)) &&
Elijah Newren9b080912022-09-02 03:53:283580 (!two->mode || S_ISGITLINK(two->mode)) &&
3581 (!diff_filepair_is_phoney(one, two))) {
Stefan Bellerf3597132017-06-30 00:07:003582 show_submodule_inline_diff(o, one->path ? one->path : two->path,
Jacob Kellerfd47ae62016-08-31 23:27:253583 &one->oid, &two->oid,
Stefan Bellerf3597132017-06-30 00:07:003584 two->dirty_submodule);
René Scharfe11591852024-09-08 07:08:353585 o->found_changes = 1;
Jacob Kellerfd47ae62016-08-31 23:27:253586 return;
Junio C Hamano6973dca2006-04-22 06:57:453587 }
3588
Brandon Williams0d1e0e72017-10-31 18:19:113589 if (o->flags.allow_textconv) {
Nguyễn Thái Ngọc Duybd7ad452018-11-10 05:49:063590 textconv_one = get_textconv(o->repo, one);
3591 textconv_two = get_textconv(o->repo, two);
Junio C Hamano6973dca2006-04-22 06:57:453592 }
3593
Linus Torvalds71b989e2008-10-05 19:35:153594 /* Never use a non-valid filename anywhere if at all possible */
3595 name_a = DIFF_FILE_VALID(one) ? name_a : name_b;
3596 name_b = DIFF_FILE_VALID(two) ? name_b : name_a;
3597
Junio C Hamanoa5a818e2008-08-19 03:08:093598 a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
3599 b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
Junio C Hamano6973dca2006-04-22 06:57:453600 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
3601 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
Elijah Newren9b080912022-09-02 03:53:283602 if (diff_filepair_is_phoney(one, two)) {
Elijah Newren95433ee2022-02-02 02:37:343603 /*
Elijah Newren9b080912022-09-02 03:53:283604 * We should only reach this point for pairs generated from
Elijah Newren95433ee2022-02-02 02:37:343605 * create_filepairs_for_header_only_notifications(). For
Elijah Newren9b080912022-09-02 03:53:283606 * these, we want to avoid the "/dev/null" special casing
3607 * above, because we do not want such pairs shown as either
Elijah Newren95433ee2022-02-02 02:37:343608 * "new file" or "deleted file" below.
3609 */
3610 lbl[0] = a_one;
3611 lbl[1] = b_two;
3612 }
Jeff Kingd9c552f2012-11-13 15:42:463613 strbuf_addf(&header, "%s%sdiff --git %s %s%s\n", line_prefix, meta, a_one, b_two, reset);
Junio C Hamano6973dca2006-04-22 06:57:453614 if (lbl[0][0] == '/') {
3615 /* /dev/null */
Jeff Kingd9c552f2012-11-13 15:42:463616 strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset);
Bert Wesarg37466442010-05-03 22:38:073617 if (xfrm_msg)
3618 strbuf_addstr(&header, xfrm_msg);
Junio C Hamanoc9a3e722023-08-18 23:59:293619 o->found_changes = 1;
Christian Couder296c6bb2010-05-26 02:50:123620 must_show_header = 1;
Junio C Hamano6973dca2006-04-22 06:57:453621 }
3622 else if (lbl[1][0] == '/') {
Jeff Kingd9c552f2012-11-13 15:42:463623 strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset);
Bert Wesarg37466442010-05-03 22:38:073624 if (xfrm_msg)
3625 strbuf_addstr(&header, xfrm_msg);
Junio C Hamanoc9a3e722023-08-18 23:59:293626 o->found_changes = 1;
Christian Couder296c6bb2010-05-26 02:50:123627 must_show_header = 1;
Junio C Hamano6973dca2006-04-22 06:57:453628 }
3629 else {
3630 if (one->mode != two->mode) {
Jeff Kingd9c552f2012-11-13 15:42:463631 strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset);
3632 strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset);
Junio C Hamanoc9a3e722023-08-18 23:59:293633 o->found_changes = 1;
Christian Couder296c6bb2010-05-26 02:50:123634 must_show_header = 1;
Junio C Hamano6973dca2006-04-22 06:57:453635 }
Bert Wesarg37466442010-05-03 22:38:073636 if (xfrm_msg)
3637 strbuf_addstr(&header, xfrm_msg);
Greg Bacon3e97c7c2009-11-19 21:12:243638
Junio C Hamano6973dca2006-04-22 06:57:453639 /*
3640 * we do not run diff between different kind
3641 * of objects.
3642 */
3643 if ((one->mode ^ two->mode) & S_IFMT)
3644 goto free_ab_and_return;
Jeff King0c018572008-12-09 08:12:283645 if (complete_rewrite &&
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:193646 (textconv_one || !diff_filespec_is_binary(o->repo, one)) &&
3647 (textconv_two || !diff_filespec_is_binary(o->repo, two))) {
Stefan Bellera29b0a12017-06-30 00:06:573648 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3649 header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 21:12:243650 strbuf_reset(&header);
Jeff King3aa1f7c2008-12-09 08:13:213651 emit_rewrite_diff(name_a, name_b, one, two,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:193652 textconv_one, textconv_two, o);
Johannes Schindelin34a5e1a2007-02-25 22:34:543653 o->found_changes = 1;
Junio C Hamano6973dca2006-04-22 06:57:453654 goto free_ab_and_return;
3655 }
3656 }
3657
Junio C Hamano467ddc12011-03-01 00:11:553658 if (o->irreversible_delete && lbl[1][0] == '/') {
Stefan Bellera29b0a12017-06-30 00:06:573659 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf,
3660 header.len, 0);
Junio C Hamano467ddc12011-03-01 00:11:553661 strbuf_reset(&header);
3662 goto free_ab_and_return;
Brandon Williams0d1e0e72017-10-31 18:19:113663 } else if (!o->flags.text &&
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:193664 ( (!textconv_one && diff_filespec_is_binary(o->repo, one)) ||
3665 (!textconv_two && diff_filespec_is_binary(o->repo, two)) )) {
Stefan Beller4acaaa72017-06-30 00:06:583666 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 03:08:063667 if (!one->data && !two->data &&
3668 S_ISREG(one->mode) && S_ISREG(two->mode) &&
Brandon Williams0d1e0e72017-10-31 18:19:113669 !o->flags.binary) {
Jeff King4a7e27e2018-08-28 21:22:403670 if (oideq(&one->oid, &two->oid)) {
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 03:08:063671 if (must_show_header)
Stefan Bellera29b0a12017-06-30 00:06:573672 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3673 header.buf, header.len,
3674 0);
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 03:08:063675 goto free_ab_and_return;
3676 }
Stefan Bellera29b0a12017-06-30 00:06:573677 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3678 header.buf, header.len, 0);
Stefan Beller4acaaa72017-06-30 00:06:583679 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3680 diff_line_prefix(o), lbl[0], lbl[1]);
3681 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3682 sb.buf, sb.len, 0);
3683 strbuf_release(&sb);
René Scharfe9a417352024-09-21 15:09:543684 o->found_changes = 1;
Nguyễn Thái Ngọc Duy1aaf69e2014-08-16 03:08:063685 goto free_ab_and_return;
3686 }
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:193687 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3688 fill_mmfile(o->repo, &mf2, two) < 0)
Jeff Kingb3373982010-04-02 00:14:243689 die("unable to read files to diff");
Junio C Hamano06606262006-05-05 09:41:533690 /* Quite common confusing case */
3691 if (mf1.size == mf2.size &&
Christian Couder296c6bb2010-05-26 02:50:123692 !memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
3693 if (must_show_header)
Stefan Bellera29b0a12017-06-30 00:06:573694 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3695 header.buf, header.len, 0);
Junio C Hamano06606262006-05-05 09:41:533696 goto free_ab_and_return;
Christian Couder296c6bb2010-05-26 02:50:123697 }
Stefan Bellera29b0a12017-06-30 00:06:573698 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 21:12:243699 strbuf_reset(&header);
Brandon Williams0d1e0e72017-10-31 18:19:113700 if (o->flags.binary)
Stefan Beller4eed0eb2017-06-30 00:07:013701 emit_binary_diff(o, &mf1, &mf2);
Stefan Beller4acaaa72017-06-30 00:06:583702 else {
3703 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3704 diff_line_prefix(o), lbl[0], lbl[1]);
3705 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3706 sb.buf, sb.len, 0);
3707 strbuf_release(&sb);
3708 }
Johannes Schindelin34a5e1a2007-02-25 22:34:543709 o->found_changes = 1;
Junio C Hamano467ddc12011-03-01 00:11:553710 } else {
Junio C Hamano6973dca2006-04-22 06:57:453711 /* Crazy xdl interfaces.. */
Jeff King0da0e922019-01-11 22:17:223712 const char *diffopts;
Jeff Kingae021d82014-06-18 19:47:503713 const char *v;
Junio C Hamano6973dca2006-04-22 06:57:453714 xpparam_t xpp;
3715 xdemitconf_t xecfg;
Junio C Hamano6973dca2006-04-22 06:57:453716 struct emit_callback ecbdata;
Jeff Kingbe58e702008-10-05 21:43:213717 const struct userdiff_funcname *pe;
Junio C Hamanof2584752007-07-06 07:45:103718
Junio C Hamanob3f01ff2012-03-01 02:14:163719 if (must_show_header) {
Stefan Bellera29b0a12017-06-30 00:06:573720 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3721 header.buf, header.len, 0);
Greg Bacon3e97c7c2009-11-19 21:12:243722 strbuf_reset(&header);
3723 }
3724
Nguyễn Thái Ngọc Duy6afaf802018-09-21 15:57:223725 mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr);
3726 mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr);
Jeff King04427ac2008-10-26 04:44:533727
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:333728 pe = diff_funcname_pattern(o, one);
Brandon Casey45e7ca02008-09-18 22:40:483729 if (!pe)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:333730 pe = diff_funcname_pattern(o, two);
Junio C Hamano6973dca2006-04-22 06:57:453731
Brian Downing9ccd0a82008-10-25 13:30:373732 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 18:05:463733 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelincd112ce2006-06-13 16:45:443734 memset(&ecbdata, 0, sizeof(ecbdata));
Johannes Schindelin1cdde292018-08-13 11:33:113735 if (o->flags.suppress_diff_headers)
3736 lbl[0] = NULL;
Junio C Hamano6973dca2006-04-22 06:57:453737 ecbdata.label_path = lbl;
Jeff King955000d2025-09-16 20:21:203738 ecbdata.color_diff = o->use_color;
Nguyễn Thái Ngọc Duy26d024e2018-09-21 15:57:373739 ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b);
Junio C Hamano690ed842009-09-04 07:41:153740 if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
Junio C Hamanod68fe262009-09-15 05:05:573741 check_blank_at_eof(&mf1, &mf2, &ecbdata);
Bo Yanga3c158d2010-05-26 07:08:023742 ecbdata.opt = o;
Johannes Schindelin1cdde292018-08-13 11:33:113743 if (header.len && !o->flags.suppress_diff_headers)
3744 ecbdata.header = &header;
René Scharfe582aa002010-05-02 13:04:413745 xpp.flags = o->xdl_opts;
Michał Kępień296d4a92020-10-20 06:48:093746 xpp.ignore_regex = o->ignore_regex;
3747 xpp.ignore_regex_nr = o->ignore_regex_nr;
Jonathan Tan2477ab22017-11-27 19:47:473748 xpp.anchors = o->anchors;
3749 xpp.anchors_nr = o->anchors_nr;
Linus Torvaldsee1e5412006-05-13 20:23:483750 xecfg.ctxlen = o->context;
René Scharfe6d0e6742008-12-28 18:45:323751 xecfg.interhunkctxlen = o->interhunkcontext;
Junio C Hamano6973dca2006-04-22 06:57:453752 xecfg.flags = XDL_EMIT_FUNCNAMES;
Brandon Williams0d1e0e72017-10-31 18:19:113753 if (o->flags.funccontext)
René Scharfe14937c22011-10-09 11:36:573754 xecfg.flags |= XDL_EMIT_FUNCCONTEXT;
Brandon Casey45e7ca02008-09-18 22:40:483755 if (pe)
Brandon Caseya0135852008-09-18 22:42:483756 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
Jeff King0da0e922019-01-11 22:17:223757
3758 diffopts = getenv("GIT_DIFF_OPTS");
Junio C Hamano6973dca2006-04-22 06:57:453759 if (!diffopts)
3760 ;
Jeff Kingae021d82014-06-18 19:47:503761 else if (skip_prefix(diffopts, "--unified=", &v))
3762 xecfg.ctxlen = strtoul(v, NULL, 10);
3763 else if (skip_prefix(diffopts, "-u", &v))
3764 xecfg.ctxlen = strtoul(v, NULL, 10);
Jeff King0da0e922019-01-11 22:17:223765
Thomas Rast77d1a522012-03-14 18:24:083766 if (o->word_diff)
3767 init_diff_words_data(&ecbdata, o, one, two);
Jeff Kingb2b5ad52025-10-24 17:08:533768 if (!o->file) {
Lidong Yanb55e6d32025-08-08 03:30:193769 /*
Jeff Kingb2b5ad52025-10-24 17:08:533770 * Unlike the normal output case, we need to ignore the
Lidong Yanb55e6d32025-08-08 03:30:193771 * return value from xdi_diff_outf() here, because
3772 * xdi_diff_outf() takes non-zero return from its
3773 * callback function as a sign of error and returns
3774 * early (which is why we return non-zero from our
3775 * callback, quick_consume()). Unfortunately,
3776 * xdi_diff_outf() signals an error by returning
3777 * non-zero.
3778 */
3779 xdi_diff_outf(&mf1, &mf2, NULL, quick_consume,
3780 &ecbdata, &xpp, &xecfg);
3781 } else if (xdi_diff_outf(&mf1, &mf2, NULL, fn_out_consume,
3782 &ecbdata, &xpp, &xecfg))
Jeff King3efb9882015-09-24 23:12:233783 die("unable to generate diff for %s", one->path);
Thomas Rast882749a2010-04-14 15:59:063784 if (o->word_diff)
Johannes Schindelinf59a59e2006-07-28 21:56:153785 free_diff_words_data(&ecbdata);
Jeff King04427ac2008-10-26 04:44:533786 if (textconv_one)
3787 free(mf1.ptr);
3788 if (textconv_two)
3789 free(mf2.ptr);
René Scharfe8cfe5f12009-07-01 22:01:433790 xdiff_clear_find_func(&xecfg);
Junio C Hamano6973dca2006-04-22 06:57:453791 }
3792
3793 free_ab_and_return:
Greg Bacon3e97c7c2009-11-19 21:12:243794 strbuf_release(&header);
Junio C Hamanofc3abdf2007-05-03 20:05:483795 diff_free_filespec_data(one);
3796 diff_free_filespec_data(two);
Junio C Hamano6973dca2006-04-22 06:57:453797 free(a_one);
3798 free(b_two);
3799 return;
3800}
3801
Patrick Steinhardtb5670042024-06-07 06:37:393802static const char *get_compact_summary(const struct diff_filepair *p, int is_renamed)
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 14:09:593803{
3804 if (!is_renamed) {
3805 if (p->status == DIFF_STATUS_ADDED) {
3806 if (S_ISLNK(p->two->mode))
3807 return "new +l";
3808 else if ((p->two->mode & 0777) == 0755)
3809 return "new +x";
3810 else
3811 return "new";
3812 } else if (p->status == DIFF_STATUS_DELETED)
3813 return "gone";
3814 }
3815 if (S_ISLNK(p->one->mode) && !S_ISLNK(p->two->mode))
3816 return "mode -l";
3817 else if (!S_ISLNK(p->one->mode) && S_ISLNK(p->two->mode))
3818 return "mode +l";
3819 else if ((p->one->mode & 0777) == 0644 &&
3820 (p->two->mode & 0777) == 0755)
3821 return "mode +x";
3822 else if ((p->one->mode & 0777) == 0755 &&
3823 (p->two->mode & 0777) == 0644)
3824 return "mode -x";
3825 return NULL;
3826}
3827
Junio C Hamano6973dca2006-04-22 06:57:453828static void builtin_diffstat(const char *name_a, const char *name_b,
3829 struct diff_filespec *one,
3830 struct diff_filespec *two,
Junio C Hamano710158e2006-04-26 06:40:093831 struct diffstat_t *diffstat,
Johannes Schindelin0d21efa2006-06-14 15:40:233832 struct diff_options *o,
Linus Torvalds74faaa12012-10-17 17:00:373833 struct diff_filepair *p)
Junio C Hamano6973dca2006-04-22 06:57:453834{
3835 mmfile_t mf1, mf2;
3836 struct diffstat_file *data;
Thomas Guyot-Sionnestff0c7fa2020-09-24 07:41:413837 int may_differ;
Linus Torvalds74faaa12012-10-17 17:00:373838 int complete_rewrite = 0;
3839
3840 if (!DIFF_PAIR_UNMERGED(p)) {
3841 if (p->status == DIFF_STATUS_MODIFIED && p->score)
3842 complete_rewrite = 1;
3843 }
Junio C Hamano6973dca2006-04-22 06:57:453844
3845 data = diffstat_add(diffstat, name_a, name_b);
Junio C Hamano99bfd402012-11-27 19:17:143846 data->is_interesting = p->status != DIFF_STATUS_UNKNOWN;
Nguyễn Thái Ngọc Duyddf88fa2018-02-24 14:09:593847 if (o->flags.stat_with_summary)
3848 data->comments = get_compact_summary(p, data->is_renamed);
Junio C Hamano6973dca2006-04-22 06:57:453849
3850 if (!one || !two) {
3851 data->is_unmerged = 1;
3852 return;
3853 }
Jeff Kingded0abc2011-02-19 08:04:563854
Thomas Guyot-Sionnestff0c7fa2020-09-24 07:41:413855 /* saves some reads if true, not a guarantee of diff outcome */
3856 may_differ = !(one->oid_valid && two->oid_valid &&
3857 oideq(&one->oid, &two->oid));
Zbigniew Jędrzejewski-Szmek352ca4e2012-05-01 17:10:153858
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:193859 if (diff_filespec_is_binary(o->repo, one) ||
3860 diff_filespec_is_binary(o->repo, two)) {
Jeff Kingded0abc2011-02-19 08:04:563861 data->is_binary = 1;
Thomas Guyot-Sionnestff0c7fa2020-09-24 07:41:413862 if (!may_differ) {
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 17:10:143863 data->added = 0;
3864 data->deleted = 0;
3865 } else {
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:193866 data->added = diff_filespec_size(o->repo, two);
3867 data->deleted = diff_filespec_size(o->repo, one);
Zbigniew Jędrzejewski-Szmeke18872b2012-05-01 17:10:143868 }
Jeff Kingded0abc2011-02-19 08:04:563869 }
3870
3871 else if (complete_rewrite) {
Jonathan Tan1c37e862020-04-07 22:11:413872 diff_populate_filespec(o->repo, one, NULL);
3873 diff_populate_filespec(o->repo, two, NULL);
Junio C Hamano710158e2006-04-26 06:40:093874 data->deleted = count_lines(one->data, one->size);
3875 data->added = count_lines(two->data, two->size);
Junio C Hamano710158e2006-04-26 06:40:093876 }
Junio C Hamano6973dca2006-04-22 06:57:453877
Thomas Guyot-Sionnestff0c7fa2020-09-24 07:41:413878 else if (may_differ) {
Junio C Hamano6973dca2006-04-22 06:57:453879 /* Crazy xdl interfaces.. */
3880 xpparam_t xpp;
3881 xdemitconf_t xecfg;
Junio C Hamano6973dca2006-04-22 06:57:453882
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:193883 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3884 fill_mmfile(o->repo, &mf2, two) < 0)
Jeff Kingded0abc2011-02-19 08:04:563885 die("unable to read files to diff");
3886
Brian Downing9ccd0a82008-10-25 13:30:373887 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 18:05:463888 memset(&xecfg, 0, sizeof(xecfg));
René Scharfe582aa002010-05-02 13:04:413889 xpp.flags = o->xdl_opts;
Michał Kępień296d4a92020-10-20 06:48:093890 xpp.ignore_regex = o->ignore_regex;
3891 xpp.ignore_regex_nr = o->ignore_regex_nr;
Jonathan Tan2477ab22017-11-27 19:47:473892 xpp.anchors = o->anchors;
3893 xpp.anchors_nr = o->anchors_nr;
Junio C Hamanof01cae92011-09-22 17:54:473894 xecfg.ctxlen = o->context;
3895 xecfg.interhunkctxlen = o->interhunkcontext;
Ævar Arnfjörð Bjarmason5d934602021-04-12 17:15:293896 xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
3897 if (xdi_diff_outf(&mf1, &mf2, NULL,
Jeff King3b40a092018-11-02 06:36:063898 diffstat_consume, diffstat, &xpp, &xecfg))
Jeff King3efb9882015-09-24 23:12:233899 die("unable to generate diffstat for %s", one->path);
Matthew Rogers1cf3d5d2020-08-20 00:41:323900
3901 if (DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two)) {
3902 struct diffstat_file *file =
3903 diffstat->files[diffstat->nr - 1];
3904 /*
3905 * Omit diffstats of modified files where nothing changed.
Thomas Guyot-Sionnestff0c7fa2020-09-24 07:41:413906 * Even if may_differ, this might be the case due to
Matthew Rogers1cf3d5d2020-08-20 00:41:323907 * ignoring whitespace changes, etc.
3908 *
3909 * But note that we special-case additions, deletions,
3910 * renames, and mode changes as adding an empty file,
3911 * for example is still of interest.
3912 */
3913 if ((p->status == DIFF_STATUS_MODIFIED)
3914 && !file->added
3915 && !file->deleted
3916 && one->mode == two->mode) {
3917 free_diffstat_file(file);
3918 diffstat->nr--;
3919 }
3920 }
Junio C Hamano6973dca2006-04-22 06:57:453921 }
Junio C Hamanofc3abdf2007-05-03 20:05:483922
Junio C Hamanofc3abdf2007-05-03 20:05:483923 diff_free_filespec_data(one);
3924 diff_free_filespec_data(two);
Junio C Hamano6973dca2006-04-22 06:57:453925}
3926
Johannes Schindelin88246892006-05-20 21:43:133927static void builtin_checkdiff(const char *name_a, const char *name_b,
Junio C Hamanocd676a52008-02-12 22:26:023928 const char *attr_path,
Junio C Hamano5ff10dd2008-06-26 22:34:543929 struct diff_filespec *one,
3930 struct diff_filespec *two,
3931 struct diff_options *o)
Johannes Schindelin88246892006-05-20 21:43:133932{
3933 mmfile_t mf1, mf2;
3934 struct checkdiff_t data;
3935
3936 if (!two)
3937 return;
3938
3939 memset(&data, 0, sizeof(data));
Johannes Schindelin88246892006-05-20 21:43:133940 data.filename = name_b ? name_b : name_a;
3941 data.lineno = 0;
Junio C Hamano1ba111d2008-06-26 22:36:343942 data.o = o;
Nguyễn Thái Ngọc Duy26d024e2018-09-21 15:57:373943 data.ws_rule = whitespace_rule(o->repo->index, attr_path);
Nguyễn Thái Ngọc Duy32eaa462018-09-21 15:57:273944 data.conflict_marker_size = ll_merge_marker_size(o->repo->index, attr_path);
Johannes Schindelin88246892006-05-20 21:43:133945
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:193946 if (fill_mmfile(o->repo, &mf1, one) < 0 ||
3947 fill_mmfile(o->repo, &mf2, two) < 0)
Johannes Schindelin88246892006-05-20 21:43:133948 die("unable to read files to diff");
3949
Junio C Hamano5ff10dd2008-06-26 22:34:543950 /*
3951 * All the other codepaths check both sides, but not checking
3952 * the "old" side here is deliberate. We are checking the newly
3953 * introduced changes, and as long as the "new" side is text, we
3954 * can and should check what it introduces.
3955 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:193956 if (diff_filespec_is_binary(o->repo, two))
Junio C Hamanofc3abdf2007-05-03 20:05:483957 goto free_and_return;
Johannes Schindelin88246892006-05-20 21:43:133958 else {
3959 /* Crazy xdl interfaces.. */
3960 xpparam_t xpp;
3961 xdemitconf_t xecfg;
Johannes Schindelin88246892006-05-20 21:43:133962
Brian Downing9ccd0a82008-10-25 13:30:373963 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 18:05:463964 memset(&xecfg, 0, sizeof(xecfg));
Junio C Hamanoc35539e2008-08-20 18:47:553965 xecfg.ctxlen = 1; /* at least one context line */
René Scharfe582aa002010-05-02 13:04:413966 xpp.flags = 0;
Jeff King75ab7632018-11-02 06:39:033967 if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume_hunk,
3968 checkdiff_consume, &data,
Jeff King3efb9882015-09-24 23:12:233969 &xpp, &xecfg))
3970 die("unable to generate checkdiff for %s", one->path);
Junio C Hamano877f23c2008-06-26 22:36:593971
Junio C Hamano467babf2009-09-04 06:39:433972 if (data.ws_rule & WS_BLANK_AT_EOF) {
Junio C Hamanod68fe262009-09-15 05:05:573973 struct emit_callback ecbdata;
3974 int blank_at_eof;
3975
3976 ecbdata.ws_rule = data.ws_rule;
3977 check_blank_at_eof(&mf1, &mf2, &ecbdata);
Christoph Mallon8837d332010-10-10 17:24:063978 blank_at_eof = ecbdata.blank_at_eof_in_postimage;
Junio C Hamanod68fe262009-09-15 05:05:573979
Junio C Hamano467babf2009-09-04 06:39:433980 if (blank_at_eof) {
3981 static char *err;
3982 if (!err)
3983 err = whitespace_error_string(WS_BLANK_AT_EOF);
3984 fprintf(o->file, "%s:%d: %s.\n",
3985 data.filename, blank_at_eof, err);
3986 data.status = 1; /* report errors */
3987 }
Junio C Hamano877f23c2008-06-26 22:36:593988 }
Johannes Schindelin88246892006-05-20 21:43:133989 }
Junio C Hamanofc3abdf2007-05-03 20:05:483990 free_and_return:
3991 diff_free_filespec_data(one);
3992 diff_free_filespec_data(two);
Wincent Colaiuta62c64892007-12-13 20:24:523993 if (data.status)
Brandon Williams0d1e0e72017-10-31 18:19:113994 o->flags.check_failed = 1;
Johannes Schindelin88246892006-05-20 21:43:133995}
3996
Junio C Hamano6973dca2006-04-22 06:57:453997struct diff_filespec *alloc_filespec(const char *path)
3998{
Jeff King96ffc062016-02-22 22:44:323999 struct diff_filespec *spec;
Junio C Hamano6973dca2006-04-22 06:57:454000
Jeff King96ffc062016-02-22 22:44:324001 FLEXPTR_ALLOC_STR(spec, path, path);
Linus Torvalds9fb88412007-10-25 18:19:104002 spec->count = 1;
Jeff King122aa6f2008-10-05 21:43:364003 spec->is_binary = -1;
Junio C Hamano6973dca2006-04-22 06:57:454004 return spec;
4005}
4006
Linus Torvalds9fb88412007-10-25 18:19:104007void free_filespec(struct diff_filespec *spec)
4008{
4009 if (!--spec->count) {
4010 diff_free_filespec_data(spec);
4011 free(spec);
4012 }
4013}
4014
Brandon Williamsf9704c22017-05-30 17:30:504015void fill_filespec(struct diff_filespec *spec, const struct object_id *oid,
4016 int oid_valid, unsigned short mode)
Junio C Hamano6973dca2006-04-22 06:57:454017{
4018 if (mode) {
4019 spec->mode = canon_mode(mode);
Brandon Williamsf9704c22017-05-30 17:30:504020 oidcpy(&spec->oid, oid);
4021 spec->oid_valid = oid_valid;
Junio C Hamano6973dca2006-04-22 06:57:454022 }
4023}
4024
4025/*
Jakub Narebski5adf3172007-05-25 22:37:404026 * Given a name and sha1 pair, if the index tells us the file in
Junio C Hamano6973dca2006-04-22 06:57:454027 * the work tree has that object contents, return true, so that
4028 * prepare_temp_file() does not have to inflate and extract.
4029 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194030static int reuse_worktree_file(struct index_state *istate,
4031 const char *name,
4032 const struct object_id *oid,
4033 int want_file)
Junio C Hamano6973dca2006-04-22 06:57:454034{
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 15:29:004035 const struct cache_entry *ce;
Junio C Hamano6973dca2006-04-22 06:57:454036 struct stat st;
4037 int pos, len;
4038
Junio C Hamano150115a2009-03-22 22:26:074039 /*
4040 * We do not read the cache ourselves here, because the
Junio C Hamano6973dca2006-04-22 06:57:454041 * benchmark with my previous version that always reads cache
4042 * shows that it makes things worse for diff-tree comparing
4043 * two linux-2.6 kernel trees in an already checked out work
4044 * tree. This is because most diff-tree comparisons deal with
4045 * only a small number of files, while reading the cache is
4046 * expensive for a large project, and its cost outweighs the
4047 * savings we get by not inflating the object to a temporary
4048 * file. Practically, this code only helps when we are used
4049 * by diff-cache --cached, which does read the cache before
4050 * calling us.
4051 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194052 if (!istate->cache)
Junio C Hamano6973dca2006-04-22 06:57:454053 return 0;
4054
Shawn O. Pearce1510fea2006-12-14 11:15:574055 /* We want to avoid the working directory if our caller
4056 * doesn't need the data in a normal file, this system
4057 * is rather slow with its stat/open/mmap/close syscalls,
4058 * and the object is contained in a pack file. The pack
4059 * is probably already open and will be faster to obtain
4060 * the data through than the working directory. Loose
4061 * objects however would tend to be slower as they need
4062 * to be individually opened and inflated.
4063 */
Karthik Nayakcc656f42024-12-03 14:43:594064 if (!FAST_WORKING_DIRECTORY && !want_file &&
4065 has_object_pack(istate->repo, oid))
Shawn O. Pearce1510fea2006-12-14 11:15:574066 return 0;
4067
Jeff King06dec432016-07-22 15:27:534068 /*
4069 * Similarly, if we'd have to convert the file contents anyway, that
4070 * makes the optimization not worthwhile.
4071 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194072 if (!want_file && would_convert_to_git(istate, name))
Jeff King06dec432016-07-22 15:27:534073 return 0;
4074
Derrick Stoleead90da72021-09-08 11:23:564075 /*
4076 * If this path does not match our sparse-checkout definition,
4077 * then the file will not be in the working directory.
4078 */
4079 if (!path_in_sparse_checkout(name, istate))
4080 return 0;
4081
Junio C Hamano6973dca2006-04-22 06:57:454082 len = strlen(name);
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194083 pos = index_name_pos(istate, name, len);
Junio C Hamano6973dca2006-04-22 06:57:454084 if (pos < 0)
4085 return 0;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194086 ce = istate->cache[pos];
Junio C Hamanoeadb5832008-01-19 07:45:244087
4088 /*
4089 * This is not the sha1 we are looking for, or
4090 * unreusable because it is not a regular file.
Junio C Hamano6973dca2006-04-22 06:57:454091 */
Jeff King9001dc22018-08-28 21:22:484092 if (!oideq(oid, &ce->oid) || !S_ISREG(ce->ce_mode))
Junio C Hamanoeadb5832008-01-19 07:45:244093 return 0;
4094
4095 /*
Junio C Hamano150115a2009-03-22 22:26:074096 * If ce is marked as "assume unchanged", there is no
4097 * guarantee that work tree matches what we are looking for.
4098 */
Nguyễn Thái Ngọc Duyb4d16902009-08-20 13:46:584099 if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
Junio C Hamano150115a2009-03-22 22:26:074100 return 0;
4101
4102 /*
Junio C Hamanoeadb5832008-01-19 07:45:244103 * If ce matches the file in the work tree, we can reuse it.
4104 */
4105 if (ce_uptodate(ce) ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194106 (!lstat(name, &st) && !ie_match_stat(istate, ce, &st, 0)))
Junio C Hamanoeadb5832008-01-19 07:45:244107 return 1;
4108
4109 return 0;
Junio C Hamano6973dca2006-04-22 06:57:454110}
4111
Linus Torvalds04786752007-04-15 18:14:284112static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
4113{
Jeff Kingb1ddfb92016-02-22 22:45:084114 struct strbuf buf = STRBUF_INIT;
Patrick Steinhardtb5670042024-06-07 06:37:394115 const char *dirty = "";
Junio C Hamano8e08b412010-01-16 17:42:534116
4117 /* Are we looking at the work tree? */
Jens Lehmann85adbf22010-03-12 21:23:524118 if (s->dirty_submodule)
Junio C Hamano8e08b412010-01-16 17:42:534119 dirty = "-dirty";
4120
brian m. carlsona0d12c42016-06-24 23:09:234121 strbuf_addf(&buf, "Subproject commit %s%s\n",
4122 oid_to_hex(&s->oid), dirty);
Jeff Kingb1ddfb92016-02-22 22:45:084123 s->size = buf.len;
Linus Torvalds04786752007-04-15 18:14:284124 if (size_only) {
4125 s->data = NULL;
Jeff Kingb1ddfb92016-02-22 22:45:084126 strbuf_release(&buf);
4127 } else {
4128 s->data = strbuf_detach(&buf, NULL);
4129 s->should_free = 1;
Linus Torvalds04786752007-04-15 18:14:284130 }
4131 return 0;
4132}
4133
Junio C Hamano6973dca2006-04-22 06:57:454134/*
4135 * While doing rename detection and pickaxe operation, we may need to
4136 * grab the data for the blob (or file) for our own in-core comparison.
4137 * diff_filespec has data and size fields for this purpose.
4138 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194139int diff_populate_filespec(struct repository *r,
4140 struct diff_filespec *s,
Jonathan Tan1c37e862020-04-07 22:11:414141 const struct diff_populate_filespec_options *options)
Junio C Hamano6973dca2006-04-22 06:57:454142{
Jonathan Tan1c37e862020-04-07 22:11:414143 int size_only = options ? options->check_size_only : 0;
4144 int check_binary = options ? options->check_binary : 0;
Junio C Hamano6973dca2006-04-22 06:57:454145 int err = 0;
Torsten Bögershausen8462ff42018-01-13 22:49:314146 int conv_flags = global_conv_flags_eol;
Junio C Hamano5430bb22013-06-24 21:35:044147 /*
4148 * demote FAIL to WARN to allow inspecting the situation
4149 * instead of refusing.
4150 */
Torsten Bögershausen8462ff42018-01-13 22:49:314151 if (conv_flags & CONV_EOL_RNDTRP_DIE)
4152 conv_flags = CONV_EOL_RNDTRP_WARN;
Junio C Hamano5430bb22013-06-24 21:35:044153
Junio C Hamano6973dca2006-04-22 06:57:454154 if (!DIFF_FILE_VALID(s))
4155 die("internal error: asking to populate invalid file.");
4156 if (S_ISDIR(s->mode))
4157 return -1;
4158
Junio C Hamano6973dca2006-04-22 06:57:454159 if (s->data)
Junio C Hamanofc3abdf2007-05-03 20:05:484160 return 0;
Linus Torvalds04786752007-04-15 18:14:284161
Junio C Hamano6e0b8ed2007-05-07 08:14:214162 if (size_only && 0 < s->size)
4163 return 0;
4164
Martin Waitz302b9282007-05-21 20:08:284165 if (S_ISGITLINK(s->mode))
Linus Torvalds04786752007-04-15 18:14:284166 return diff_populate_gitlink(s, size_only);
4167
brian m. carlson41c95602016-06-24 23:09:244168 if (!s->oid_valid ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194169 reuse_worktree_file(r->index, s->path, &s->oid, 0)) {
Brandon Caseyf285a2d2008-10-09 19:12:124170 struct strbuf buf = STRBUF_INIT;
Junio C Hamano6973dca2006-04-22 06:57:454171 struct stat st;
4172 int fd;
Linus Torvalds6c510be2007-02-13 19:07:234173
Junio C Hamano6973dca2006-04-22 06:57:454174 if (lstat(s->path, &st) < 0) {
Andrey Okoshkin10e0ca82017-10-27 09:33:254175 err_empty:
4176 err = -1;
4177 empty:
4178 s->data = (char *)"";
4179 s->size = 0;
4180 return err;
Junio C Hamano6973dca2006-04-22 06:57:454181 }
Shawn O. Pearcedc49cd72007-03-07 01:44:374182 s->size = xsize_t(st.st_size);
Junio C Hamano6973dca2006-04-22 06:57:454183 if (!s->size)
4184 goto empty;
Junio C Hamano6973dca2006-04-22 06:57:454185 if (S_ISLNK(st.st_mode)) {
Linus Torvaldscf219d82008-12-17 18:26:134186 struct strbuf sb = STRBUF_INIT;
4187
4188 if (strbuf_readlink(&sb, s->path, s->size))
Junio C Hamano6973dca2006-04-22 06:57:454189 goto err_empty;
René Scharfe0956a6d2008-12-18 16:56:514190 s->size = sb.len;
4191 s->data = strbuf_detach(&sb, NULL);
Linus Torvaldscf219d82008-12-17 18:26:134192 s->should_free = 1;
Junio C Hamano6973dca2006-04-22 06:57:454193 return 0;
4194 }
Junio C Hamano12426e12017-03-01 17:04:444195
4196 /*
4197 * Even if the caller would be happy with getting
4198 * only the size, we cannot return early at this
4199 * point if the path requires us to run the content
4200 * conversion.
4201 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194202 if (size_only && !would_convert_to_git(r->index, s->path))
Linus Torvaldscf219d82008-12-17 18:26:134203 return 0;
Junio C Hamano12426e12017-03-01 17:04:444204
4205 /*
4206 * Note: this check uses xsize_t(st.st_size) that may
4207 * not be the true size of the blob after it goes
4208 * through convert_to_git(). This may not strictly be
4209 * correct, but the whole point of big_file_threshold
4210 * and is_binary check being that we want to avoid
4211 * opening the file and inspecting the contents, this
4212 * is probably fine.
4213 */
Jonathan Tan1c37e862020-04-07 22:11:414214 if (check_binary &&
Patrick Steinhardt7835ee72025-03-10 07:13:234215 s->size > repo_settings_get_big_file_threshold(the_repository) &&
4216 s->is_binary == -1) {
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 03:08:054217 s->is_binary = 1;
4218 return 0;
4219 }
Junio C Hamano6973dca2006-04-22 06:57:454220 fd = open(s->path, O_RDONLY);
4221 if (fd < 0)
4222 goto err_empty;
Shawn O. Pearcec4712e42006-12-24 05:47:234223 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
Junio C Hamano6973dca2006-04-22 06:57:454224 close(fd);
Junio C Hamano6973dca2006-04-22 06:57:454225 s->should_munmap = 1;
Linus Torvalds6c510be2007-02-13 19:07:234226
4227 /*
4228 * Convert from working tree format to canonical git format
4229 */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194230 if (convert_to_git(r->index, s->path, s->data, s->size, &buf, conv_flags)) {
René Scharfec32f7492007-10-21 09:23:494231 size_t size = 0;
Linus Torvalds6c510be2007-02-13 19:07:234232 munmap(s->data, s->size);
4233 s->should_munmap = 0;
René Scharfec32f7492007-10-21 09:23:494234 s->data = strbuf_detach(&buf, &size);
4235 s->size = size;
Linus Torvalds6c510be2007-02-13 19:07:234236 s->should_free = 1;
4237 }
Junio C Hamano6973dca2006-04-22 06:57:454238 }
4239 else {
Jonathan Tanc14b6f82020-04-07 22:11:424240 struct object_info info = {
4241 .sizep = &s->size
4242 };
4243
4244 if (!(size_only || check_binary))
4245 /*
4246 * Set contentp, since there is no chance that merely
4247 * the size is sufficient.
4248 */
4249 info.contentp = &s->data;
4250
Jonathan Tan95acf112020-04-07 22:11:434251 if (options && options->missing_object_cb) {
Patrick Steinhardte989dd92025-07-01 12:22:254252 if (!odb_read_object_info_extended(r->objects, &s->oid, &info,
4253 OBJECT_INFO_LOOKUP_REPLACE |
4254 OBJECT_INFO_SKIP_FETCH_OBJECT))
Jonathan Tan95acf112020-04-07 22:11:434255 goto object_read;
4256 options->missing_object_cb(options->missing_object_data);
4257 }
Patrick Steinhardte989dd92025-07-01 12:22:254258 if (odb_read_object_info_extended(r->objects, &s->oid, &info,
4259 OBJECT_INFO_LOOKUP_REPLACE))
Jonathan Tanc14b6f82020-04-07 22:11:424260 die("unable to read %s", oid_to_hex(&s->oid));
4261
Jonathan Tan95acf112020-04-07 22:11:434262object_read:
Jonathan Tan1c37e862020-04-07 22:11:414263 if (size_only || check_binary) {
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 03:08:054264 if (size_only)
4265 return 0;
Patrick Steinhardt7835ee72025-03-10 07:13:234266 if (s->size > repo_settings_get_big_file_threshold(the_repository) &&
4267 s->is_binary == -1) {
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 03:08:054268 s->is_binary = 1;
4269 return 0;
4270 }
Junio C Hamano6973dca2006-04-22 06:57:454271 }
Jonathan Tanc14b6f82020-04-07 22:11:424272 if (!info.contentp) {
4273 info.contentp = &s->data;
Patrick Steinhardte989dd92025-07-01 12:22:254274 if (odb_read_object_info_extended(r->objects, &s->oid, &info,
4275 OBJECT_INFO_LOOKUP_REPLACE))
Jonathan Tanc14b6f82020-04-07 22:11:424276 die("unable to read %s", oid_to_hex(&s->oid));
4277 }
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 03:08:054278 s->should_free = 1;
Junio C Hamano6973dca2006-04-22 06:57:454279 }
4280 return 0;
4281}
4282
Junio C Hamano8ae92e62007-10-03 04:01:034283void diff_free_filespec_blob(struct diff_filespec *s)
Junio C Hamano6973dca2006-04-22 06:57:454284{
4285 if (s->should_free)
4286 free(s->data);
4287 else if (s->should_munmap)
4288 munmap(s->data, s->size);
Junio C Hamanofc3abdf2007-05-03 20:05:484289
4290 if (s->should_free || s->should_munmap) {
4291 s->should_free = s->should_munmap = 0;
4292 s->data = NULL;
4293 }
Jeff Kingeede7b72007-09-25 19:29:424294}
4295
4296void diff_free_filespec_data(struct diff_filespec *s)
4297{
Jinoh Kang24695932020-11-06 17:14:524298 if (!s)
4299 return;
4300
Junio C Hamano8ae92e62007-10-03 04:01:034301 diff_free_filespec_blob(s);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:464302 FREE_AND_NULL(s->cnt_data);
Junio C Hamano6973dca2006-04-22 06:57:454303}
4304
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194305static void prep_temp_blob(struct index_state *istate,
4306 const char *path, struct diff_tempfile *temp,
Junio C Hamano6973dca2006-04-22 06:57:454307 void *blob,
4308 unsigned long size,
brian m. carlson09bdff22016-06-24 23:09:294309 const struct object_id *oid,
Junio C Hamano6973dca2006-04-22 06:57:454310 int mode)
4311{
Johannes Schindelin4e218f52009-03-21 11:42:524312 struct strbuf buf = STRBUF_INIT;
David Aguilar003b33a2009-05-31 08:35:524313 char *path_dup = xstrdup(path);
4314 const char *base = basename(path_dup);
brian m. carlsonc397aac2020-03-16 18:05:034315 struct checkout_metadata meta;
4316
4317 init_checkout_metadata(&meta, NULL, NULL, oid);
Junio C Hamano6973dca2006-04-22 06:57:454318
René Scharfe8af75932022-04-20 20:30:104319 temp->tempfile = mks_tempfile_dt("git-blob-XXXXXX", base);
Jeff King076aa2c2017-09-05 12:15:084320 if (!temp->tempfile)
Thomas Rastd824cbb2009-06-27 15:58:464321 die_errno("unable to create temp-file");
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194322 if (convert_to_working_tree(istate, path,
brian m. carlsonc397aac2020-03-16 18:05:034323 (const char *)blob, (size_t)size, &buf, &meta)) {
Johannes Schindelin4e218f52009-03-21 11:42:524324 blob = buf.buf;
4325 size = buf.len;
4326 }
Junio C Hamanoc50424a2017-09-25 06:24:064327 if (write_in_full(temp->tempfile->fd, blob, size) < 0 ||
Jeff King076aa2c2017-09-05 12:15:084328 close_tempfile_gently(temp->tempfile))
Thomas Rast0721c312009-06-27 15:58:474329 die_errno("unable to write temp-file");
Jeff King076aa2c2017-09-05 12:15:084330 temp->name = get_tempfile_path(temp->tempfile);
brian m. carlson09bdff22016-06-24 23:09:294331 oid_to_hex_r(temp->hex, oid);
Jeff King5096d492015-09-24 21:06:084332 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode);
Johannes Schindelin4e218f52009-03-21 11:42:524333 strbuf_release(&buf);
David Aguilar003b33a2009-05-31 08:35:524334 free(path_dup);
Junio C Hamano6973dca2006-04-22 06:57:454335}
4336
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194337static struct diff_tempfile *prepare_temp_file(struct repository *r,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194338 struct diff_filespec *one)
Junio C Hamano6973dca2006-04-22 06:57:454339{
Jeff King479b0ae2009-01-22 05:59:564340 struct diff_tempfile *temp = claim_diff_tempfile();
4341
Junio C Hamano6973dca2006-04-22 06:57:454342 if (!DIFF_FILE_VALID(one)) {
4343 not_a_valid_file:
4344 /* A '-' entry produces this for file-2, and
4345 * a '+' entry produces this for file-1.
4346 */
4347 temp->name = "/dev/null";
Jeff King5096d492015-09-24 21:06:084348 xsnprintf(temp->hex, sizeof(temp->hex), ".");
4349 xsnprintf(temp->mode, sizeof(temp->mode), ".");
Jeff King479b0ae2009-01-22 05:59:564350 return temp;
4351 }
4352
Thomas Rastaba47272014-02-16 16:52:344353 if (!S_ISGITLINK(one->mode) &&
brian m. carlson41c95602016-06-24 23:09:244354 (!one->oid_valid ||
Jeff Kingf034bb12023-01-06 11:05:004355 reuse_worktree_file(r->index, one->path, &one->oid, 1))) {
Junio C Hamano6973dca2006-04-22 06:57:454356 struct stat st;
Jeff Kingf034bb12023-01-06 11:05:004357 if (lstat(one->path, &st) < 0) {
Junio C Hamano6973dca2006-04-22 06:57:454358 if (errno == ENOENT)
4359 goto not_a_valid_file;
Jeff Kingf034bb12023-01-06 11:05:004360 die_errno("stat(%s)", one->path);
Junio C Hamano6973dca2006-04-22 06:57:454361 }
4362 if (S_ISLNK(st.st_mode)) {
Jeff King3cd73882009-05-25 10:46:094363 struct strbuf sb = STRBUF_INIT;
Jeff Kingf034bb12023-01-06 11:05:004364 if (strbuf_readlink(&sb, one->path, st.st_size) < 0)
4365 die_errno("readlink(%s)", one->path);
4366 prep_temp_blob(r->index, one->path, temp, sb.buf, sb.len,
brian m. carlson41c95602016-06-24 23:09:244367 (one->oid_valid ?
Patrick Steinhardt7d70b292025-03-10 07:13:314368 &one->oid : null_oid(the_hash_algo)),
brian m. carlson41c95602016-06-24 23:09:244369 (one->oid_valid ?
Junio C Hamano6973dca2006-04-22 06:57:454370 one->mode : S_IFLNK));
Jeff King3cd73882009-05-25 10:46:094371 strbuf_release(&sb);
Junio C Hamano6973dca2006-04-22 06:57:454372 }
4373 else {
4374 /* we can borrow from the file in the work tree */
Jeff Kingf034bb12023-01-06 11:05:004375 temp->name = one->path;
brian m. carlson41c95602016-06-24 23:09:244376 if (!one->oid_valid)
Patrick Steinhardt7d70b292025-03-10 07:13:314377 oid_to_hex_r(temp->hex, null_oid(the_hash_algo));
Junio C Hamano6973dca2006-04-22 06:57:454378 else
René Scharfe24905742017-01-28 22:03:034379 oid_to_hex_r(temp->hex, &one->oid);
Junio C Hamano6973dca2006-04-22 06:57:454380 /* Even though we may sometimes borrow the
4381 * contents from the work tree, we always want
4382 * one->mode. mode is trustworthy even when
Brandon Williams74014152017-05-30 17:30:524383 * !(one->oid_valid), as long as
Junio C Hamano6973dca2006-04-22 06:57:454384 * DIFF_FILE_VALID(one).
4385 */
Jeff King5096d492015-09-24 21:06:084386 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode);
Junio C Hamano6973dca2006-04-22 06:57:454387 }
Jeff King479b0ae2009-01-22 05:59:564388 return temp;
Junio C Hamano6973dca2006-04-22 06:57:454389 }
4390 else {
Jonathan Tan1c37e862020-04-07 22:11:414391 if (diff_populate_filespec(r, one, NULL))
Junio C Hamano6973dca2006-04-22 06:57:454392 die("cannot read data blob for %s", one->path);
Jeff Kingf034bb12023-01-06 11:05:004393 prep_temp_blob(r->index, one->path, temp,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194394 one->data, one->size,
brian m. carlson09bdff22016-06-24 23:09:294395 &one->oid, one->mode);
Junio C Hamano6973dca2006-04-22 06:57:454396 }
Jeff King479b0ae2009-01-22 05:59:564397 return temp;
Junio C Hamano6973dca2006-04-22 06:57:454398}
4399
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194400static void add_external_diff_name(struct repository *r,
Jeff Kingef8d7ac2020-07-28 20:24:534401 struct strvec *argv,
Jeff Kingf3efe782014-04-19 19:22:254402 struct diff_filespec *df)
4403{
Jeff Kingf034bb12023-01-06 11:05:004404 struct diff_tempfile *temp = prepare_temp_file(r, df);
Jeff Kingef8d7ac2020-07-28 20:24:534405 strvec_push(argv, temp->name);
4406 strvec_push(argv, temp->hex);
4407 strvec_push(argv, temp->mode);
Jeff Kingf3efe782014-04-19 19:22:254408}
4409
Junio C Hamano6973dca2006-04-22 06:57:454410/* An external diff command takes:
4411 *
4412 * diff-cmd name infile1 infile1-sha1 infile1-mode \
4413 * infile2 infile2-sha1 infile2-mode [ rename-to ]
4414 *
4415 */
René Scharfe54443bb2024-06-09 07:39:084416static void run_external_diff(const struct external_diff *pgm,
Junio C Hamano6973dca2006-04-22 06:57:454417 const char *name,
4418 const char *other,
4419 struct diff_filespec *one,
4420 struct diff_filespec *two,
4421 const char *xfrm_msg,
Zoltan Klingeree7fb0b2013-12-05 23:38:464422 struct diff_options *o)
Junio C Hamano6973dca2006-04-22 06:57:454423{
René Scharfe0e906732022-10-30 11:51:144424 struct child_process cmd = CHILD_PROCESS_INIT;
Zoltan Klingeree7fb0b2013-12-05 23:38:464425 struct diff_queue_struct *q = &diff_queued_diff;
René Scharfed7b97b72024-06-09 07:41:444426 int rc;
Junio C Hamano6973dca2006-04-22 06:57:454427
René Scharfed7b97b72024-06-09 07:41:444428 /*
4429 * Trivial equality is handled by diff_unmodified_pair() before
4430 * we get here. If we don't need to show the diff and the
4431 * external diff program lacks the ability to tell us whether
4432 * it's empty then we consider it non-empty without even asking.
4433 */
Jeff King2ecb8852025-10-24 17:25:074434 if (!pgm->trust_exit_code && !o->file) {
René Scharfed7b97b72024-06-09 07:41:444435 o->found_changes = 1;
4436 return;
4437 }
Junio C Hamano6973dca2006-04-22 06:57:454438
René Scharfe54443bb2024-06-09 07:39:084439 strvec_push(&cmd.args, pgm->cmd);
René Scharfe0e906732022-10-30 11:51:144440 strvec_push(&cmd.args, name);
Junio C Hamano6973dca2006-04-22 06:57:454441
Junio C Hamano6973dca2006-04-22 06:57:454442 if (one && two) {
Jeff Kingde8f14e2023-01-06 11:04:184443 add_external_diff_name(o->repo, &cmd.args, one);
4444 add_external_diff_name(o->repo, &cmd.args, two);
4445 if (other) {
René Scharfe0e906732022-10-30 11:51:144446 strvec_push(&cmd.args, other);
Jeff King85a9a632024-01-29 01:57:084447 if (xfrm_msg)
4448 strvec_push(&cmd.args, xfrm_msg);
Junio C Hamano6973dca2006-04-22 06:57:454449 }
Junio C Hamano6973dca2006-04-22 06:57:454450 }
Zoltan Klingeree7fb0b2013-12-05 23:38:464451
René Scharfe0e906732022-10-30 11:51:144452 strvec_pushf(&cmd.env, "GIT_DIFF_PATH_COUNTER=%d",
4453 ++o->diff_path_counter);
4454 strvec_pushf(&cmd.env, "GIT_DIFF_PATH_TOTAL=%d", q->nr);
Zoltan Klingeree7fb0b2013-12-05 23:38:464455
Johannes Schindelin3aef54e2019-07-11 08:23:414456 diff_free_filespec_data(one);
4457 diff_free_filespec_data(two);
René Scharfe0e906732022-10-30 11:51:144458 cmd.use_shell = 1;
Jeff King2ecb8852025-10-24 17:25:074459 if (!o->file)
Jeff King57c2b6c2025-10-24 17:06:494460 cmd.no_stdout = 1;
4461 else if (o->file != stdout)
4462 cmd.out = xdup(fileno(o->file));
René Scharfed7b97b72024-06-09 07:41:444463 rc = run_command(&cmd);
4464 if (!pgm->trust_exit_code && rc == 0)
4465 o->found_changes = 1;
4466 else if (pgm->trust_exit_code && rc == 0)
4467 ; /* nothing */
4468 else if (pgm->trust_exit_code && rc == 1)
4469 o->found_changes = 1;
4470 else
Junio C Hamanoe37423f2024-05-16 17:08:354471 die(_("external diff died, stopping at %s"), name);
Jeff King89294d12014-04-19 19:19:194472
Junio C Hamano6973dca2006-04-22 06:57:454473 remove_tempfile();
Junio C Hamano6973dca2006-04-22 06:57:454474}
4475
Junio C Hamanob67b9612009-01-26 08:08:244476static int similarity_index(struct diff_filepair *p)
4477{
4478 return p->score * 100 / MAX_SCORE;
4479}
4480
Jeff King4f036662016-10-20 06:21:254481static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev)
4482{
4483 if (startup_info->have_repository)
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 13:58:464484 return repo_find_unique_abbrev(the_repository, oid, abbrev);
Jeff King4f036662016-10-20 06:21:254485 else {
4486 char *hex = oid_to_hex(oid);
Junio C Hamano0d9c5272016-10-27 21:58:484487 if (abbrev < 0)
4488 abbrev = FALLBACK_DEFAULT_ABBREV;
brian m. carlson02afca12018-07-16 01:28:054489 if (abbrev > the_hash_algo->hexsz)
Johannes Schindelin033abf92018-05-02 09:38:394490 BUG("oid abbreviation out of range: %d", abbrev);
Jack Bates43d19482016-12-06 16:56:144491 if (abbrev)
4492 hex[abbrev] = '\0';
Jeff King4f036662016-10-20 06:21:254493 return hex;
4494 }
4495}
4496
Junio C Hamanob67b9612009-01-26 08:08:244497static void fill_metainfo(struct strbuf *msg,
4498 const char *name,
4499 const char *other,
4500 struct diff_filespec *one,
4501 struct diff_filespec *two,
4502 struct diff_options *o,
Bert Wesarg37466442010-05-03 22:38:074503 struct diff_filepair *p,
Junio C Hamano59777442010-06-18 18:16:574504 int *must_show_header,
Jeff Kinge9330ae2025-09-16 23:13:594505 enum git_colorbool use_color)
Junio C Hamanob67b9612009-01-26 08:08:244506{
Bert Wesarg37466442010-05-03 22:38:074507 const char *set = diff_get_color(use_color, DIFF_METAINFO);
4508 const char *reset = diff_get_color(use_color, DIFF_RESET);
John Keeping30997bb2013-02-07 20:15:274509 const char *line_prefix = diff_line_prefix(o);
Johannes Schindelin2715e8a2022-06-18 00:20:554510 struct string_list *more_headers = NULL;
Bo Yang7be57612010-05-26 07:23:544511
Christian Couder296c6bb2010-05-26 02:50:124512 *must_show_header = 1;
Junio C Hamanob67b9612009-01-26 08:08:244513 strbuf_init(msg, PATH_MAX * 2 + 300);
4514 switch (p->status) {
4515 case DIFF_STATUS_COPIED:
Junio C Hamano98ad90f2010-06-18 18:16:574516 strbuf_addf(msg, "%s%ssimilarity index %d%%",
4517 line_prefix, set, similarity_index(p));
4518 strbuf_addf(msg, "%s\n%s%scopy from ",
4519 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 08:08:244520 quote_c_style(name, msg, NULL, 0);
Junio C Hamano98ad90f2010-06-18 18:16:574521 strbuf_addf(msg, "%s\n%s%scopy to ", reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 08:08:244522 quote_c_style(other, msg, NULL, 0);
Bert Wesarg37466442010-05-03 22:38:074523 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 08:08:244524 break;
4525 case DIFF_STATUS_RENAMED:
Junio C Hamano98ad90f2010-06-18 18:16:574526 strbuf_addf(msg, "%s%ssimilarity index %d%%",
4527 line_prefix, set, similarity_index(p));
4528 strbuf_addf(msg, "%s\n%s%srename from ",
4529 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 08:08:244530 quote_c_style(name, msg, NULL, 0);
Junio C Hamano98ad90f2010-06-18 18:16:574531 strbuf_addf(msg, "%s\n%s%srename to ",
4532 reset, line_prefix, set);
Junio C Hamanob67b9612009-01-26 08:08:244533 quote_c_style(other, msg, NULL, 0);
Bert Wesarg37466442010-05-03 22:38:074534 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 08:08:244535 break;
4536 case DIFF_STATUS_MODIFIED:
4537 if (p->score) {
Junio C Hamano98ad90f2010-06-18 18:16:574538 strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n",
4539 line_prefix,
Bert Wesarg37466442010-05-03 22:38:074540 set, similarity_index(p), reset);
Junio C Hamanob67b9612009-01-26 08:08:244541 break;
4542 }
4543 /* fallthru */
4544 default:
Christian Couder296c6bb2010-05-26 02:50:124545 *must_show_header = 0;
Junio C Hamanob67b9612009-01-26 08:08:244546 }
Elijah Newren95433ee2022-02-02 02:37:344547 if ((more_headers = additional_headers(o, name))) {
4548 add_formatted_headers(msg, more_headers,
4549 line_prefix, set, reset);
4550 *must_show_header = 1;
4551 }
Jeff King9001dc22018-08-28 21:22:484552 if (one && two && !oideq(&one->oid, &two->oid)) {
brian m. carlsonde1d81d2018-05-02 00:25:524553 const unsigned hexsz = the_hash_algo->hexsz;
Đoàn Trần Công Danh3046c7f2020-08-21 11:51:474554 int abbrev = o->abbrev ? o->abbrev : DEFAULT_ABBREV;
4555
4556 if (o->flags.full_index)
4557 abbrev = hexsz;
Junio C Hamanob67b9612009-01-26 08:08:244558
Brandon Williams0d1e0e72017-10-31 18:19:114559 if (o->flags.binary) {
Junio C Hamanob67b9612009-01-26 08:08:244560 mmfile_t mf;
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194561 if ((!fill_mmfile(o->repo, &mf, one) &&
4562 diff_filespec_is_binary(o->repo, one)) ||
4563 (!fill_mmfile(o->repo, &mf, two) &&
4564 diff_filespec_is_binary(o->repo, two)))
brian m. carlsonde1d81d2018-05-02 00:25:524565 abbrev = hexsz;
Junio C Hamanob67b9612009-01-26 08:08:244566 }
Jeff King4f036662016-10-20 06:21:254567 strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set,
4568 diff_abbrev_oid(&one->oid, abbrev),
4569 diff_abbrev_oid(&two->oid, abbrev));
Junio C Hamanob67b9612009-01-26 08:08:244570 if (one->mode == two->mode)
4571 strbuf_addf(msg, " %06o", one->mode);
Bert Wesarg37466442010-05-03 22:38:074572 strbuf_addf(msg, "%s\n", reset);
Junio C Hamanob67b9612009-01-26 08:08:244573 }
Junio C Hamanob67b9612009-01-26 08:08:244574}
4575
René Scharfe54443bb2024-06-09 07:39:084576static void run_diff_cmd(const struct external_diff *pgm,
Junio C Hamano6973dca2006-04-22 06:57:454577 const char *name,
4578 const char *other,
Junio C Hamanocd676a52008-02-12 22:26:024579 const char *attr_path,
Junio C Hamano6973dca2006-04-22 06:57:454580 struct diff_filespec *one,
4581 struct diff_filespec *two,
Junio C Hamanob67b9612009-01-26 08:08:244582 struct strbuf *msg,
Junio C Hamano051308f2006-05-04 23:51:444583 struct diff_options *o,
Junio C Hamanob67b9612009-01-26 08:08:244584 struct diff_filepair *p)
Junio C Hamano6973dca2006-04-22 06:57:454585{
Junio C Hamanob67b9612009-01-26 08:08:244586 const char *xfrm_msg = NULL;
4587 int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
Christian Couder296c6bb2010-05-26 02:50:124588 int must_show_header = 0;
John Caia4cf9002023-02-20 21:04:424589 struct userdiff_driver *drv = NULL;
Junio C Hamanob67b9612009-01-26 08:08:244590
John Caia4cf9002023-02-20 21:04:424591 if (o->flags.allow_external || !o->ignore_driver_algorithm)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:334592 drv = userdiff_find_by_path(o->repo->index, attr_path);
John Caia4cf9002023-02-20 21:04:424593
René Scharfe54443bb2024-06-09 07:39:084594 if (o->flags.allow_external && drv && drv->external.cmd)
4595 pgm = &drv->external;
Junio C Hamanof1af60b2007-04-23 00:52:554596
Bert Wesarg37466442010-05-03 22:38:074597 if (msg) {
4598 /*
4599 * don't use colors when the header is intended for an
4600 * external diff driver
4601 */
4602 fill_metainfo(msg, name, other, one, two, o, p,
Junio C Hamano59777442010-06-18 18:16:574603 &must_show_header,
Jeff King12df3c22025-09-16 20:20:264604 pgm ? GIT_COLOR_NEVER : o->use_color);
Bert Wesarg37466442010-05-03 22:38:074605 xfrm_msg = msg->len ? msg->buf : NULL;
4606 }
4607
Junio C Hamano6973dca2006-04-22 06:57:454608 if (pgm) {
Jeff King4bc17922019-02-14 05:49:424609 run_external_diff(pgm, name, other, one, two, xfrm_msg, o);
Junio C Hamano6973dca2006-04-22 06:57:454610 return;
4611 }
John Caia4cf9002023-02-20 21:04:424612 if (one && two) {
4613 if (!o->ignore_driver_algorithm && drv && drv->algorithm)
4614 set_diff_algorithm(o, drv->algorithm);
4615
Junio C Hamano6973dca2006-04-22 06:57:454616 builtin_diff(name, other ? other : name,
Christian Couder296c6bb2010-05-26 02:50:124617 one, two, xfrm_msg, must_show_header,
4618 o, complete_rewrite);
René Scharfe87cf9602024-09-08 07:05:444619 if (p->status == DIFF_STATUS_COPIED ||
4620 p->status == DIFF_STATUS_RENAMED)
4621 o->found_changes = 1;
John Caia4cf9002023-02-20 21:04:424622 } else {
Jeff Kingb2b5ad52025-10-24 17:08:534623 if (o->file)
Lidong Yan3ed5d8b2025-10-19 16:30:244624 fprintf(o->file, "* Unmerged path %s\n", name);
René Scharfe7b30c3a2024-05-05 10:19:564625 o->found_changes = 1;
John Caia4cf9002023-02-20 21:04:424626 }
Junio C Hamano6973dca2006-04-22 06:57:454627}
4628
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 15:57:314629static void diff_fill_oid_info(struct diff_filespec *one, struct index_state *istate)
Junio C Hamano6973dca2006-04-22 06:57:454630{
4631 if (DIFF_FILE_VALID(one)) {
brian m. carlson41c95602016-06-24 23:09:244632 if (!one->oid_valid) {
Junio C Hamano6973dca2006-04-22 06:57:454633 struct stat st;
Junio C Hamano4682d852012-06-28 03:14:474634 if (one->is_stdin) {
Patrick Steinhardt9da95bd2024-06-14 06:49:544635 oidclr(&one->oid, the_repository->hash_algo);
Johannes Schindelin5332b2a2007-02-25 22:36:104636 return;
4637 }
Junio C Hamano6973dca2006-04-22 06:57:454638 if (lstat(one->path, &st) < 0)
Thomas Rast0721c312009-06-27 15:58:474639 die_errno("stat '%s'", one->path);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 15:57:314640 if (index_path(istate, &one->oid, one->path, &st, 0))
Alexander Potashevd7530702009-01-04 18:38:414641 die("cannot hash %s", one->path);
Junio C Hamano6973dca2006-04-22 06:57:454642 }
4643 }
4644 else
Patrick Steinhardt9da95bd2024-06-14 06:49:544645 oidclr(&one->oid, the_repository->hash_algo);
Junio C Hamano6973dca2006-04-22 06:57:454646}
4647
Junio C Hamanocd676a52008-02-12 22:26:024648static void strip_prefix(int prefix_length, const char **namep, const char **otherp)
4649{
4650 /* Strip the prefix but do not molest /dev/null and absolute paths */
Johannes Sixtffd04e92018-10-19 16:58:074651 if (*namep && !is_absolute_path(*namep)) {
Junio C Hamanocd676a52008-02-12 22:26:024652 *namep += prefix_length;
Jakub Narebskid8faea92010-08-09 14:50:534653 if (**namep == '/')
4654 ++*namep;
4655 }
Johannes Sixtffd04e92018-10-19 16:58:074656 if (*otherp && !is_absolute_path(*otherp)) {
Junio C Hamanocd676a52008-02-12 22:26:024657 *otherp += prefix_length;
Jakub Narebskid8faea92010-08-09 14:50:534658 if (**otherp == '/')
4659 ++*otherp;
4660 }
Junio C Hamanocd676a52008-02-12 22:26:024661}
4662
Junio C Hamano6973dca2006-04-22 06:57:454663static void run_diff(struct diff_filepair *p, struct diff_options *o)
4664{
René Scharfe54443bb2024-06-09 07:39:084665 const struct external_diff *pgm = external_diff();
Pierre Habouzit663af342007-09-19 22:42:154666 struct strbuf msg;
Pierre Habouzit663af342007-09-19 22:42:154667 struct diff_filespec *one = p->one;
4668 struct diff_filespec *two = p->two;
Junio C Hamano6973dca2006-04-22 06:57:454669 const char *name;
4670 const char *other;
Junio C Hamanocd676a52008-02-12 22:26:024671 const char *attr_path;
Pierre Habouzit663af342007-09-19 22:42:154672
Stefan Bellerf2d2a5d2017-06-30 00:06:464673 name = one->path;
4674 other = (strcmp(name, two->path) ? two->path : NULL);
Junio C Hamanocd676a52008-02-12 22:26:024675 attr_path = name;
4676 if (o->prefix_length)
4677 strip_prefix(o->prefix_length, &name, &other);
4678
Brandon Williams0d1e0e72017-10-31 18:19:114679 if (!o->flags.allow_external)
Junio C Hamanobd8c1a92012-07-18 05:08:594680 pgm = NULL;
4681
Junio C Hamanocd676a52008-02-12 22:26:024682 if (DIFF_PAIR_UNMERGED(p)) {
4683 run_diff_cmd(pgm, name, NULL, attr_path,
Junio C Hamanob67b9612009-01-26 08:08:244684 NULL, NULL, NULL, o, p);
Junio C Hamanocd676a52008-02-12 22:26:024685 return;
4686 }
4687
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 15:57:314688 diff_fill_oid_info(one, o->repo->index);
4689 diff_fill_oid_info(two, o->repo->index);
Junio C Hamano6973dca2006-04-22 06:57:454690
Junio C Hamano6973dca2006-04-22 06:57:454691 if (!pgm &&
4692 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
4693 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
Junio C Hamanob67b9612009-01-26 08:08:244694 /*
4695 * a filepair that changes between file and symlink
Junio C Hamano6973dca2006-04-22 06:57:454696 * needs to be split into deletion and creation.
4697 */
4698 struct diff_filespec *null = alloc_filespec(two->path);
Junio C Hamanocd676a52008-02-12 22:26:024699 run_diff_cmd(NULL, name, other, attr_path,
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194700 one, null, &msg,
4701 o, p);
Junio C Hamano6973dca2006-04-22 06:57:454702 free(null);
Junio C Hamanob67b9612009-01-26 08:08:244703 strbuf_release(&msg);
4704
Junio C Hamano6973dca2006-04-22 06:57:454705 null = alloc_filespec(one->path);
Junio C Hamanocd676a52008-02-12 22:26:024706 run_diff_cmd(NULL, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 08:08:244707 null, two, &msg, o, p);
Junio C Hamano6973dca2006-04-22 06:57:454708 free(null);
4709 }
4710 else
Junio C Hamanocd676a52008-02-12 22:26:024711 run_diff_cmd(pgm, name, other, attr_path,
Junio C Hamanob67b9612009-01-26 08:08:244712 one, two, &msg, o, p);
Junio C Hamano6973dca2006-04-22 06:57:454713
Pierre Habouzit663af342007-09-19 22:42:154714 strbuf_release(&msg);
Junio C Hamano6973dca2006-04-22 06:57:454715}
4716
4717static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
4718 struct diffstat_t *diffstat)
4719{
4720 const char *name;
4721 const char *other;
4722
John Caia4cf9002023-02-20 21:04:424723 if (!o->ignore_driver_algorithm) {
4724 struct userdiff_driver *drv = userdiff_find_by_path(o->repo->index,
4725 p->one->path);
4726
4727 if (drv && drv->algorithm)
4728 set_diff_algorithm(o, drv->algorithm);
4729 }
4730
Junio C Hamano6973dca2006-04-22 06:57:454731 if (DIFF_PAIR_UNMERGED(p)) {
4732 /* unmerged */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194733 builtin_diffstat(p->one->path, NULL, NULL, NULL,
4734 diffstat, o, p);
Junio C Hamano6973dca2006-04-22 06:57:454735 return;
4736 }
4737
4738 name = p->one->path;
4739 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
4740
Junio C Hamanocd676a52008-02-12 22:26:024741 if (o->prefix_length)
4742 strip_prefix(o->prefix_length, &name, &other);
4743
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 15:57:314744 diff_fill_oid_info(p->one, o->repo->index);
4745 diff_fill_oid_info(p->two, o->repo->index);
Junio C Hamano6973dca2006-04-22 06:57:454746
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:194747 builtin_diffstat(name, other, p->one, p->two,
4748 diffstat, o, p);
Junio C Hamano6973dca2006-04-22 06:57:454749}
4750
Johannes Schindelin88246892006-05-20 21:43:134751static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
4752{
4753 const char *name;
4754 const char *other;
Junio C Hamanocd676a52008-02-12 22:26:024755 const char *attr_path;
Johannes Schindelin88246892006-05-20 21:43:134756
4757 if (DIFF_PAIR_UNMERGED(p)) {
4758 /* unmerged */
4759 return;
4760 }
4761
4762 name = p->one->path;
4763 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
Junio C Hamanocd676a52008-02-12 22:26:024764 attr_path = other ? other : name;
4765
4766 if (o->prefix_length)
4767 strip_prefix(o->prefix_length, &name, &other);
Johannes Schindelin88246892006-05-20 21:43:134768
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 15:57:314769 diff_fill_oid_info(p->one, o->repo->index);
4770 diff_fill_oid_info(p->two, o->repo->index);
Johannes Schindelin88246892006-05-20 21:43:134771
Junio C Hamanocd676a52008-02-12 22:26:024772 builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
Johannes Schindelin88246892006-05-20 21:43:134773}
4774
Nguyễn Thái Ngọc Duye6757652018-09-21 15:57:244775void repo_diff_setup(struct repository *r, struct diff_options *options)
Junio C Hamano6973dca2006-04-22 06:57:454776{
Johannes Schindelinbe4f2b42010-08-05 08:49:554777 memcpy(options, &default_diff_options, sizeof(*options));
Daniel Barkalowc0c77732008-03-10 02:43:394778
4779 options->file = stdout;
Nguyễn Thái Ngọc Duye6757652018-09-21 15:57:244780 options->repo = r;
Daniel Barkalowc0c77732008-03-10 02:43:394781
Stefan Beller7648b792018-08-17 20:43:524782 options->output_indicators[OUTPUT_INDICATOR_NEW] = '+';
4783 options->output_indicators[OUTPUT_INDICATOR_OLD] = '-';
4784 options->output_indicators[OUTPUT_INDICATOR_CONTEXT] = ' ';
Jack Bates43d19482016-12-06 16:56:144785 options->abbrev = DEFAULT_ABBREV;
Junio C Hamano6973dca2006-04-22 06:57:454786 options->line_termination = '\n';
4787 options->break_opt = -1;
4788 options->rename_limit = -1;
Johan Herland712d2c72011-04-29 09:36:204789 options->dirstat_permille = diff_dirstat_permille_default;
Jeff Muizelaar6468a4e2012-09-27 19:12:524790 options->context = diff_context_default;
Vegard Nossumc4888672017-01-12 12:21:114791 options->interhunkcontext = diff_interhunk_context_default;
Junio C Hamanoa17505f2016-10-04 22:26:274792 options->ws_error_highlight = ws_error_highlight_default;
Brandon Williams0d1e0e72017-10-31 18:19:114793 options->flags.rename_empty = 1;
Laurent Arnoudc28ded82020-05-22 10:46:184794 options->flags.relative_name = diff_relative;
Stefan Beller15af58c2018-01-04 22:50:424795 options->objfind = NULL;
Junio C Hamano6973dca2006-04-22 06:57:454796
Kirill Smelkov72441af2014-04-06 21:46:264797 /* pathchange left =NULL by default */
Junio C Hamano6973dca2006-04-22 06:57:454798 options->change = diff_change;
4799 options->add_remove = diff_addremove;
Jeff Kingf1c96262011-08-18 05:03:124800 options->use_color = diff_use_color_default;
Eric Wongb68ea122006-07-07 11:01:234801 options->detect_rename = diff_detect_rename_default;
Michal Privoznik07ab4de2013-01-16 07:51:574802 options->xdl_opts |= diff_algorithm;
Michael Haggerty433860f2016-09-05 09:44:514803 if (diff_indent_heuristic)
4804 DIFF_XDL_SET(options, INDENT_HEURISTIC);
Johannes Schindelineab9a402007-12-18 19:32:144805
Patrick Steinhardt76c7e702024-09-26 11:46:294806 options->orderfile = xstrdup_or_null(diff_order_file_cfg);
Samuel Bronson6d8940b2013-12-19 00:08:124807
Sangeeta Jain8ef93122020-11-10 08:39:004808 if (!options->flags.ignore_submodule_set)
4809 options->flags.ignore_untracked_in_submodules = 1;
4810
Eli Collinsf89504d2010-05-03 02:03:414811 if (diff_no_prefix) {
Jeff King6799aad2023-03-09 06:07:064812 diff_set_noprefix(options);
Eli Collinsf89504d2010-05-03 02:03:414813 } else if (!diff_mnemonic_prefix) {
Jeff King6799aad2023-03-09 06:07:064814 diff_set_default_prefix(options);
Junio C Hamanoa5a818e2008-08-19 03:08:094815 }
Stefan Beller2e2d5ac2017-06-30 20:53:074816
4817 options->color_moved = diff_color_moved_default;
Stefan Beller626c0b52018-07-18 19:31:564818 options->color_moved_ws_handling = diff_color_moved_ws_default;
Junio C Hamano6973dca2006-04-22 06:57:454819}
4820
Johannes Schindelin4d4d4ea2022-01-28 12:02:494821static const char diff_status_letters[] = {
4822 DIFF_STATUS_ADDED,
4823 DIFF_STATUS_COPIED,
4824 DIFF_STATUS_DELETED,
4825 DIFF_STATUS_MODIFIED,
4826 DIFF_STATUS_RENAMED,
4827 DIFF_STATUS_TYPE_CHANGED,
4828 DIFF_STATUS_UNKNOWN,
4829 DIFF_STATUS_UNMERGED,
4830 DIFF_STATUS_FILTER_AON,
4831 DIFF_STATUS_FILTER_BROKEN,
4832 '\0',
4833};
4834
4835static unsigned int filter_bit['Z' + 1];
4836
4837static void prepare_filter_bits(void)
4838{
4839 int i;
4840
4841 if (!filter_bit[DIFF_STATUS_ADDED]) {
4842 for (i = 0; diff_status_letters[i]; i++)
4843 filter_bit[(int) diff_status_letters[i]] = (1 << i);
4844 }
4845}
4846
4847static unsigned filter_bit_tst(char status, const struct diff_options *opt)
4848{
4849 return opt->filter & filter_bit[(int) status];
4850}
4851
4852unsigned diff_filter_bit(char status)
4853{
4854 prepare_filter_bits();
4855 return filter_bit[(int) status];
4856}
4857
Jeff King9eac5952023-06-01 17:41:064858int diff_check_follow_pathspec(struct pathspec *ps, int die_on_error)
4859{
Jeff King8260bc52023-06-01 17:43:514860 unsigned forbidden_magic;
4861
Jeff King9eac5952023-06-01 17:41:064862 if (ps->nr != 1) {
4863 if (die_on_error)
4864 die(_("--follow requires exactly one pathspec"));
4865 return 0;
4866 }
Jeff King8260bc52023-06-01 17:43:514867
4868 forbidden_magic = ps->items[0].magic & ~(PATHSPEC_FROMTOP |
4869 PATHSPEC_LITERAL);
4870 if (forbidden_magic) {
4871 if (die_on_error) {
4872 struct strbuf sb = STRBUF_INIT;
4873 pathspec_magic_names(forbidden_magic, &sb);
4874 die(_("pathspec magic not supported by --follow: %s"),
4875 sb.buf);
4876 }
4877 return 0;
4878 }
4879
Jeff King9eac5952023-06-01 17:41:064880 return 1;
4881}
4882
Thomas Rast28452652012-08-03 12:16:244883void diff_setup_done(struct diff_options *options)
Junio C Hamano6973dca2006-04-22 06:57:454884{
Stefan Beller4d8c51a2018-01-04 22:50:444885 unsigned check_mask = DIFF_FORMAT_NAME |
4886 DIFF_FORMAT_NAME_STATUS |
4887 DIFF_FORMAT_CHECKDIFF |
4888 DIFF_FORMAT_NO_OUTPUT;
brian m. carlsonde1d81d2018-05-02 00:25:524889 /*
4890 * This must be signed because we're comparing against a potentially
4891 * negative value.
4892 */
4893 const int hexsz = the_hash_algo->hexsz;
Timo Hirvonend7de00f2006-06-24 17:26:494894
Junio C Hamano6c374002013-05-10 15:10:114895 if (options->set_default)
4896 options->set_default(options);
4897
Stefan Beller4d8c51a2018-01-04 22:50:444898 if (HAS_MULTI_BITS(options->output_format & check_mask))
Jean-Noël Avila246cac82022-01-05 20:02:244899 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
4900 "--name-only", "--name-status", "--check", "-s");
Timo Hirvonend7de00f2006-06-24 17:26:494901
Stefan Beller5e505252018-01-04 22:50:434902 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
Jean-Noël Avila246cac82022-01-05 20:02:244903 die(_("options '%s', '%s', and '%s' cannot be used together"),
4904 "-G", "-S", "--find-object");
Stefan Beller5e505252018-01-04 22:50:434905
Ævar Arnfjörð Bjarmason188e9e22021-04-12 17:15:144906 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_G_REGEX_MASK))
Jean-Noël Avila246cac82022-01-05 20:02:244907 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s'"),
4908 "-G", "--pickaxe-regex", "--pickaxe-regex", "-S");
Ævar Arnfjörð Bjarmason188e9e22021-04-12 17:15:144909
Ævar Arnfjörð Bjarmasond26ec882021-04-12 17:15:154910 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK))
Jean-Noël Avila246cac82022-01-05 20:02:244911 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s' and '%s'"),
4912 "--pickaxe-all", "--find-object", "--pickaxe-all", "-G", "-S");
Ævar Arnfjörð Bjarmasond26ec882021-04-12 17:15:154913
Junio C Hamanof2451942009-05-22 19:45:294914 /*
4915 * Most of the time we can say "there are changes"
4916 * only by checking if there are changed paths, but
4917 * --ignore-whitespace* options force us to look
Jim Meyering97bf2a02009-08-30 20:27:024918 * inside contents.
Junio C Hamanof2451942009-05-22 19:45:294919 */
4920
Junio C Hamano50f04392020-12-17 01:27:134921 if ((options->xdl_opts & XDF_WHITESPACE_FLAGS) ||
4922 options->ignore_regex_nr)
Brandon Williams0d1e0e72017-10-31 18:19:114923 options->flags.diff_from_contents = 1;
Junio C Hamanof2451942009-05-22 19:45:294924 else
Brandon Williams0d1e0e72017-10-31 18:19:114925 options->flags.diff_from_contents = 0;
Junio C Hamanof2451942009-05-22 19:45:294926
Brandon Williams0d1e0e72017-10-31 18:19:114927 if (options->flags.find_copies_harder)
Junio C Hamano03b9d562006-08-09 20:17:194928 options->detect_rename = DIFF_DETECT_COPY;
4929
Brandon Williams0d1e0e72017-10-31 18:19:114930 if (!options->flags.relative_name)
Junio C Hamanocd676a52008-02-12 22:26:024931 options->prefix = NULL;
4932 if (options->prefix)
4933 options->prefix_length = strlen(options->prefix);
4934 else
4935 options->prefix_length = 0;
4936
Junio C Hamanoa64f8b22023-08-18 23:59:324937 /*
4938 * --name-only, --name-status, --checkdiff, and -s
4939 * turn other output format off.
4940 */
Timo Hirvonenc6744342006-06-24 17:21:534941 if (options->output_format & (DIFF_FORMAT_NAME |
4942 DIFF_FORMAT_NAME_STATUS |
4943 DIFF_FORMAT_CHECKDIFF |
4944 DIFF_FORMAT_NO_OUTPUT))
4945 options->output_format &= ~(DIFF_FORMAT_RAW |
Junio C Hamano74e2abe2006-10-12 10:01:004946 DIFF_FORMAT_NUMSTAT |
Timo Hirvonenc6744342006-06-24 17:21:534947 DIFF_FORMAT_DIFFSTAT |
Nicolas Pitreebd124c2006-12-15 04:15:444948 DIFF_FORMAT_SHORTSTAT |
Linus Torvalds7df7c012008-02-12 21:26:314949 DIFF_FORMAT_DIRSTAT |
Timo Hirvonenc6744342006-06-24 17:21:534950 DIFF_FORMAT_SUMMARY |
4951 DIFF_FORMAT_PATCH);
4952
Junio C Hamano6973dca2006-04-22 06:57:454953 /*
4954 * These cases always need recursive; we do not drop caller-supplied
4955 * recursive bits for other formats here.
4956 */
Timo Hirvonenc6744342006-06-24 17:21:534957 if (options->output_format & (DIFF_FORMAT_PATCH |
Junio C Hamano74e2abe2006-10-12 10:01:004958 DIFF_FORMAT_NUMSTAT |
Timo Hirvonenc6744342006-06-24 17:21:534959 DIFF_FORMAT_DIFFSTAT |
Nicolas Pitreebd124c2006-12-15 04:15:444960 DIFF_FORMAT_SHORTSTAT |
Linus Torvalds7df7c012008-02-12 21:26:314961 DIFF_FORMAT_DIRSTAT |
Johannes Schindelind7014dc2006-10-03 21:09:564962 DIFF_FORMAT_SUMMARY |
Timo Hirvonenc6744342006-06-24 17:21:534963 DIFF_FORMAT_CHECKDIFF))
Brandon Williams0d1e0e72017-10-31 18:19:114964 options->flags.recursive = 1;
Junio C Hamano5e363542006-05-22 07:31:024965 /*
Junio C Hamano3969cf72006-06-27 22:08:194966 * Also pickaxe would not work very well if you do not say recursive
Junio C Hamano5e363542006-05-22 07:31:024967 */
Stefan Bellercf630512018-01-04 22:50:414968 if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
Brandon Williams0d1e0e72017-10-31 18:19:114969 options->flags.recursive = 1;
Jens Lehmannae6d5c12010-03-11 21:50:254970 /*
4971 * When patches are generated, submodules diffed against the work tree
4972 * must be checked for dirtiness too so it can be shown in the output
4973 */
4974 if (options->output_format & DIFF_FORMAT_PATCH)
Brandon Williams0d1e0e72017-10-31 18:19:114975 options->flags.dirty_submodules = 1;
Junio C Hamano5e363542006-05-22 07:31:024976
Junio C Hamano6973dca2006-04-22 06:57:454977 if (options->detect_rename && options->rename_limit < 0)
4978 options->rename_limit = diff_rename_limit_default;
brian m. carlsonde1d81d2018-05-02 00:25:524979 if (hexsz < options->abbrev)
4980 options->abbrev = hexsz; /* full */
Junio C Hamano6973dca2006-04-22 06:57:454981
Junio C Hamano68aacb22007-03-14 18:12:134982 /*
4983 * It does not make sense to show the first hit we happened
4984 * to have found. It does not make sense not to return with
4985 * exit code in such a case either.
4986 */
Brandon Williams0d1e0e72017-10-31 18:19:114987 if (options->flags.quick) {
Junio C Hamano68aacb22007-03-14 18:12:134988 options->output_format = DIFF_FORMAT_NO_OUTPUT;
Brandon Williams0d1e0e72017-10-31 18:19:114989 options->flags.exit_with_status = 1;
Junio C Hamano68aacb22007-03-14 18:12:134990 }
Zoltan Klingeree7fb0b2013-12-05 23:38:464991
René Scharfed7b97b72024-06-09 07:41:444992 /*
4993 * External diffs could declare non-identical contents equal
4994 * (think diff --ignore-space-change).
4995 */
4996 if (options->flags.allow_external && options->flags.exit_with_status)
4997 options->flags.diff_from_contents = 1;
4998
Jeff King0ea7d5b2013-12-16 20:02:214999 options->diff_path_counter = 0;
Junio C Hamanob0e2c992014-06-16 17:07:095000
Jeff King9eac5952023-06-01 17:41:065001 if (options->flags.follow_renames)
5002 diff_check_follow_pathspec(&options->pathspec, 1);
Stefan Beller2e2d5ac2017-06-30 20:53:075003
Jeff King4cfc9712025-09-16 20:19:335004 if (options->flags.allow_external && external_diff())
Stefan Beller2e2d5ac2017-06-30 20:53:075005 options->color_moved = 0;
Nguyễn Thái Ngọc Duy4a288472019-01-27 00:35:315006
Johannes Schindelin75408ca2022-01-28 12:02:505007 if (options->filter_not) {
5008 if (!options->filter)
5009 options->filter = ~filter_bit[DIFF_STATUS_FILTER_AON];
5010 options->filter &= ~options->filter_not;
5011 }
Jeff Kinga1dfa542025-08-07 20:52:585012
5013 if (options->pathspec.has_wildcard && options->max_depth_valid)
5014 die("max-depth cannot be used with wildcard pathspecs");
Junio C Hamano6973dca2006-04-22 06:57:455015}
5016
Matthieu Moydea007f2010-08-05 08:22:525017int parse_long_opt(const char *opt, const char **argv,
5018 const char **optarg)
5019{
5020 const char *arg = argv[0];
Jeff King95b567c2014-06-18 19:48:295021 if (!skip_prefix(arg, "--", &arg))
Matthieu Moydea007f2010-08-05 08:22:525022 return 0;
Jeff King95b567c2014-06-18 19:48:295023 if (!skip_prefix(arg, opt, &arg))
Matthieu Moydea007f2010-08-05 08:22:525024 return 0;
Nicolas Vigierb0d12fc2013-10-31 11:08:285025 if (*arg == '=') { /* stuck form: --option=value */
Matthieu Moydea007f2010-08-05 08:22:525026 *optarg = arg + 1;
5027 return 1;
5028 }
5029 if (*arg != '\0')
5030 return 0;
5031 /* separate form: --option value */
5032 if (!argv[1])
5033 die("Option '--%s' requires a value", opt);
5034 *optarg = argv[1];
5035 return 2;
5036}
5037
Nguyễn Thái Ngọc Duy84b50892019-02-21 11:16:105038static int diff_opt_stat(const struct option *opt, const char *value, int unset)
Jonathan Nieder4d7f7a42010-08-05 08:22:535039{
Nguyễn Thái Ngọc Duy84b50892019-02-21 11:16:105040 struct diff_options *options = opt->value;
Jonathan Nieder4d7f7a42010-08-05 08:22:535041 int width = options->stat_width;
5042 int name_width = options->stat_name_width;
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 12:26:455043 int graph_width = options->stat_graph_width;
Michael J Gruber808e1db2011-05-27 12:36:415044 int count = options->stat_count;
Nguyễn Thái Ngọc Duy84b50892019-02-21 11:16:105045 char *end;
Jonathan Nieder4d7f7a42010-08-05 08:22:535046
Nguyễn Thái Ngọc Duy84b50892019-02-21 11:16:105047 BUG_ON_OPT_NEG(unset);
Jonathan Nieder4d7f7a42010-08-05 08:22:535048
Nguyễn Thái Ngọc Duy84b50892019-02-21 11:16:105049 if (!strcmp(opt->long_name, "stat")) {
5050 if (value) {
5051 width = strtoul(value, &end, 10);
5052 if (*end == ',')
5053 name_width = strtoul(end+1, &end, 10);
5054 if (*end == ',')
5055 count = strtoul(end+1, &end, 10);
5056 if (*end)
5057 return error(_("invalid --stat value: %s"), value);
Matthieu Moy1e572082010-08-05 08:22:545058 }
Nguyễn Thái Ngọc Duy84b50892019-02-21 11:16:105059 } else if (!strcmp(opt->long_name, "stat-width")) {
5060 width = strtoul(value, &end, 10);
5061 if (*end)
5062 return error(_("%s expects a numerical value"),
5063 opt->long_name);
5064 } else if (!strcmp(opt->long_name, "stat-name-width")) {
5065 name_width = strtoul(value, &end, 10);
5066 if (*end)
5067 return error(_("%s expects a numerical value"),
5068 opt->long_name);
5069 } else if (!strcmp(opt->long_name, "stat-graph-width")) {
5070 graph_width = strtoul(value, &end, 10);
5071 if (*end)
5072 return error(_("%s expects a numerical value"),
5073 opt->long_name);
5074 } else if (!strcmp(opt->long_name, "stat-count")) {
5075 count = strtoul(value, &end, 10);
5076 if (*end)
5077 return error(_("%s expects a numerical value"),
5078 opt->long_name);
5079 } else
5080 BUG("%s should not get here", opt->long_name);
Jonathan Nieder4d7f7a42010-08-05 08:22:535081
Junio C Hamano9d484b92023-05-05 16:59:525082 options->output_format &= ~DIFF_FORMAT_NO_OUTPUT;
Jonathan Nieder4d7f7a42010-08-05 08:22:535083 options->output_format |= DIFF_FORMAT_DIFFSTAT;
5084 options->stat_name_width = name_width;
Zbigniew Jędrzejewski-Szmek969fe572012-03-01 12:26:455085 options->stat_graph_width = graph_width;
Jonathan Nieder4d7f7a42010-08-05 08:22:535086 options->stat_width = width;
Michael J Gruber808e1db2011-05-27 12:36:415087 options->stat_count = count;
Nguyễn Thái Ngọc Duy84b50892019-02-21 11:16:105088 return 0;
Jonathan Nieder4d7f7a42010-08-05 08:22:535089}
5090
Johan Herland333f3fb2011-04-29 09:36:185091static int parse_dirstat_opt(struct diff_options *options, const char *params)
5092{
Johan Herland51670fc2011-04-29 09:36:225093 struct strbuf errmsg = STRBUF_INIT;
5094 if (parse_dirstat_params(options, params, &errmsg))
Johan Herland7478ac52011-04-29 09:36:235095 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
Johan Herland51670fc2011-04-29 09:36:225096 errmsg.buf);
5097 strbuf_release(&errmsg);
Johan Herland333f3fb2011-04-29 09:36:185098 /*
5099 * The caller knows a dirstat-related option is given from the command
5100 * line; allow it to say "return this_function();"
5101 */
Junio C Hamano9d484b92023-05-05 16:59:525102 options->output_format &= ~DIFF_FORMAT_NO_OUTPUT;
Johan Herland333f3fb2011-04-29 09:36:185103 options->output_format |= DIFF_FORMAT_DIRSTAT;
5104 return 1;
5105}
5106
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 08:20:035107static int diff_opt_diff_filter(const struct option *option,
5108 const char *optarg, int unset)
Junio C Hamano1ecc1cb2013-07-17 22:05:465109{
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 08:20:035110 struct diff_options *opt = option->value;
Junio C Hamano1ecc1cb2013-07-17 22:05:465111 int i, optch;
5112
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 08:20:035113 BUG_ON_OPT_NEG(unset);
Junio C Hamano1ecc1cb2013-07-17 22:05:465114 prepare_filter_bits();
Junio C Hamano7f2ea5f2013-07-17 23:19:195115
Junio C Hamano1ecc1cb2013-07-17 22:05:465116 for (i = 0; (optch = optarg[i]) != '\0'; i++) {
5117 unsigned int bit;
Junio C Hamano7f2ea5f2013-07-17 23:19:195118 int negate;
5119
5120 if ('a' <= optch && optch <= 'z') {
5121 negate = 1;
5122 optch = toupper(optch);
5123 } else {
5124 negate = 0;
5125 }
Junio C Hamano1ecc1cb2013-07-17 22:05:465126
5127 bit = (0 <= optch && optch <= 'Z') ? filter_bit[optch] : 0;
5128 if (!bit)
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 08:20:035129 return error(_("unknown change class '%c' in --diff-filter=%s"),
5130 optarg[i], optarg);
Junio C Hamano7f2ea5f2013-07-17 23:19:195131 if (negate)
Johannes Schindelin75408ca2022-01-28 12:02:505132 opt->filter_not |= bit;
Junio C Hamano7f2ea5f2013-07-17 23:19:195133 else
5134 opt->filter |= bit;
Junio C Hamano1ecc1cb2013-07-17 22:05:465135 }
5136 return 0;
5137}
5138
Nguyễn Thái Ngọc Duy3b335762018-12-09 10:25:215139static void enable_patch_output(int *fmt)
5140{
Matthieu Moy71482d32013-07-16 08:05:375141 *fmt &= ~DIFF_FORMAT_NO_OUTPUT;
5142 *fmt |= DIFF_FORMAT_PATCH;
5143}
5144
Nguyễn Thái Ngọc Duy5a749d92019-03-24 08:19:555145static int diff_opt_ws_error_highlight(const struct option *option,
5146 const char *arg, int unset)
Junio C Hamanob8767f72015-05-26 17:11:285147{
Nguyễn Thái Ngọc Duy5a749d92019-03-24 08:19:555148 struct diff_options *opt = option->value;
Junio C Hamano077965f2016-10-04 22:03:335149 int val = parse_ws_error_highlight(arg);
Junio C Hamanob8767f72015-05-26 17:11:285150
Nguyễn Thái Ngọc Duy5a749d92019-03-24 08:19:555151 BUG_ON_OPT_NEG(unset);
5152 if (val < 0)
5153 return error(_("unknown value after ws-error-highlight=%.*s"),
5154 -1 - val, arg);
Junio C Hamanob8767f72015-05-26 17:11:285155 opt->ws_error_highlight = val;
Nguyễn Thái Ngọc Duy5a749d92019-03-24 08:19:555156 return 0;
Junio C Hamanob8767f72015-05-26 17:11:285157}
5158
Nguyễn Thái Ngọc Duya75f28c2019-03-24 08:20:025159static int diff_opt_find_object(const struct option *option,
5160 const char *arg, int unset)
Stefan Beller15af58c2018-01-04 22:50:425161{
Nguyễn Thái Ngọc Duya75f28c2019-03-24 08:20:025162 struct diff_options *opt = option->value;
Stefan Beller15af58c2018-01-04 22:50:425163 struct object_id oid;
5164
Nguyễn Thái Ngọc Duya75f28c2019-03-24 08:20:025165 BUG_ON_OPT_NEG(unset);
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 13:58:465166 if (repo_get_oid(the_repository, arg, &oid))
Nguyễn Thái Ngọc Duya75f28c2019-03-24 08:20:025167 return error(_("unable to resolve '%s'"), arg);
Stefan Beller15af58c2018-01-04 22:50:425168
5169 if (!opt->objfind)
René Scharfeca56dad2021-03-13 16:17:225170 CALLOC_ARRAY(opt->objfind, 1);
Stefan Beller15af58c2018-01-04 22:50:425171
5172 opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND;
5173 opt->flags.recursive = 1;
5174 opt->flags.tree_in_recursive = 1;
5175 oidset_insert(opt->objfind, &oid);
Nguyễn Thái Ngọc Duya75f28c2019-03-24 08:20:025176 return 0;
Stefan Beller15af58c2018-01-04 22:50:425177}
5178
Nguyễn Thái Ngọc Duydf84a432019-03-05 12:30:115179static int diff_opt_anchored(const struct option *opt,
5180 const char *arg, int unset)
5181{
5182 struct diff_options *options = opt->value;
5183
5184 BUG_ON_OPT_NEG(unset);
5185 options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
5186 ALLOC_GROW(options->anchors, options->anchors_nr + 1,
5187 options->anchors_alloc);
5188 options->anchors[options->anchors_nr++] = xstrdup(arg);
5189 return 0;
5190}
5191
Nguyễn Thái Ngọc Duy6d9af6f2019-03-05 12:30:125192static int diff_opt_binary(const struct option *opt,
5193 const char *arg, int unset)
5194{
5195 struct diff_options *options = opt->value;
5196
5197 BUG_ON_OPT_NEG(unset);
5198 BUG_ON_OPT_ARG(arg);
5199 enable_patch_output(&options->output_format);
5200 options->flags.binary = 1;
5201 return 0;
5202}
5203
Nguyễn Thái Ngọc Duyced4e172019-02-21 11:16:135204static int diff_opt_break_rewrites(const struct option *opt,
5205 const char *arg, int unset)
5206{
5207 int *break_opt = opt->value;
5208 int opt1, opt2;
5209
5210 BUG_ON_OPT_NEG(unset);
5211 if (!arg)
5212 arg = "";
5213 opt1 = parse_rename_score(&arg);
5214 if (*arg == 0)
5215 opt2 = 0;
5216 else if (*arg != '/')
5217 return error(_("%s expects <n>/<m> form"), opt->long_name);
5218 else {
5219 arg++;
5220 opt2 = parse_rename_score(&arg);
5221 }
5222 if (*arg != 0)
5223 return error(_("%s expects <n>/<m> form"), opt->long_name);
5224 *break_opt = opt1 | (opt2 << 16);
5225 return 0;
5226}
5227
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 11:16:125228static int diff_opt_char(const struct option *opt,
5229 const char *arg, int unset)
5230{
5231 char *value = opt->value;
5232
5233 BUG_ON_OPT_NEG(unset);
5234 if (arg[1])
5235 return error(_("%s expects a character, got '%s'"),
5236 opt->long_name, arg);
5237 *value = arg[0];
5238 return 0;
5239}
5240
Nguyễn Thái Ngọc Duy59311a92019-03-24 08:20:095241static int diff_opt_color_moved(const struct option *opt,
5242 const char *arg, int unset)
5243{
5244 struct diff_options *options = opt->value;
5245
5246 if (unset) {
5247 options->color_moved = COLOR_MOVED_NO;
5248 } else if (!arg) {
5249 if (diff_color_moved_default)
5250 options->color_moved = diff_color_moved_default;
5251 if (options->color_moved == COLOR_MOVED_NO)
5252 options->color_moved = COLOR_MOVED_DEFAULT;
5253 } else {
5254 int cm = parse_color_moved(arg);
5255 if (cm < 0)
5256 return error(_("bad --color-moved argument: %s"), arg);
5257 options->color_moved = cm;
5258 }
5259 return 0;
5260}
5261
Nguyễn Thái Ngọc Duy8ce20202019-03-24 08:20:105262static int diff_opt_color_moved_ws(const struct option *opt,
5263 const char *arg, int unset)
5264{
5265 struct diff_options *options = opt->value;
5266 unsigned cm;
5267
Nguyễn Thái Ngọc Duybb987292019-03-24 08:20:115268 if (unset) {
5269 options->color_moved_ws_handling = 0;
5270 return 0;
5271 }
5272
Nguyễn Thái Ngọc Duy8ce20202019-03-24 08:20:105273 cm = parse_color_moved_ws(arg);
5274 if (cm & COLOR_MOVED_WS_ERROR)
5275 return error(_("invalid mode '%s' in --color-moved-ws"), arg);
5276 options->color_moved_ws_handling = cm;
5277 return 0;
5278}
5279
Nguyễn Thái Ngọc Duy212db692019-03-05 12:30:205280static int diff_opt_color_words(const struct option *opt,
5281 const char *arg, int unset)
5282{
5283 struct diff_options *options = opt->value;
5284
5285 BUG_ON_OPT_NEG(unset);
Jeff King3c3e9b82025-09-16 20:13:285286 options->use_color = GIT_COLOR_ALWAYS;
Nguyễn Thái Ngọc Duy212db692019-03-05 12:30:205287 options->word_diff = DIFF_WORDS_COLOR;
5288 options->word_regex = arg;
5289 return 0;
5290}
5291
Nguyễn Thái Ngọc Duy7d7942b2019-02-21 11:16:115292static int diff_opt_compact_summary(const struct option *opt,
5293 const char *arg, int unset)
5294{
5295 struct diff_options *options = opt->value;
5296
5297 BUG_ON_OPT_ARG(arg);
5298 if (unset) {
5299 options->flags.stat_with_summary = 0;
5300 } else {
5301 options->flags.stat_with_summary = 1;
Junio C Hamano9d484b92023-05-05 16:59:525302 options->output_format &= ~DIFF_FORMAT_NO_OUTPUT;
Nguyễn Thái Ngọc Duy7d7942b2019-02-21 11:16:115303 options->output_format |= DIFF_FORMAT_DIFFSTAT;
5304 }
5305 return 0;
5306}
5307
Nguyễn Thái Ngọc Duy10f35b12019-03-05 12:30:105308static int diff_opt_diff_algorithm(const struct option *opt,
5309 const char *arg, int unset)
5310{
5311 struct diff_options *options = opt->value;
Nguyễn Thái Ngọc Duy10f35b12019-03-05 12:30:105312
5313 BUG_ON_OPT_NEG(unset);
John Cai11e95e12023-02-20 21:04:415314
5315 if (set_diff_algorithm(options, arg))
Nguyễn Thái Ngọc Duy10f35b12019-03-05 12:30:105316 return error(_("option diff-algorithm accepts \"myers\", "
5317 "\"minimal\", \"patience\" and \"histogram\""));
5318
John Caia4cf9002023-02-20 21:04:425319 options->ignore_driver_algorithm = 1;
5320
John Cai11e95e12023-02-20 21:04:415321 return 0;
5322}
5323
5324static int diff_opt_diff_algorithm_no_arg(const struct option *opt,
5325 const char *arg, int unset)
5326{
5327 struct diff_options *options = opt->value;
5328
5329 BUG_ON_OPT_NEG(unset);
5330 BUG_ON_OPT_ARG(arg);
5331
5332 if (set_diff_algorithm(options, opt->long_name))
5333 BUG("available diff algorithms include \"myers\", "
5334 "\"minimal\", \"patience\" and \"histogram\"");
5335
John Caia4cf9002023-02-20 21:04:425336 options->ignore_driver_algorithm = 1;
5337
Nguyễn Thái Ngọc Duy10f35b12019-03-05 12:30:105338 return 0;
5339}
5340
Nguyễn Thái Ngọc Duy4ce7aab2019-02-21 11:16:035341static int diff_opt_dirstat(const struct option *opt,
5342 const char *arg, int unset)
5343{
5344 struct diff_options *options = opt->value;
5345
5346 BUG_ON_OPT_NEG(unset);
5347 if (!strcmp(opt->long_name, "cumulative")) {
5348 if (arg)
5349 BUG("how come --cumulative take a value?");
5350 arg = "cumulative";
5351 } else if (!strcmp(opt->long_name, "dirstat-by-file"))
5352 parse_dirstat_opt(options, "files");
5353 parse_dirstat_opt(options, arg ? arg : "");
5354 return 0;
5355}
5356
Nguyễn Thái Ngọc Duy7f648502019-02-21 11:16:165357static int diff_opt_find_copies(const struct option *opt,
5358 const char *arg, int unset)
5359{
5360 struct diff_options *options = opt->value;
5361
5362 BUG_ON_OPT_NEG(unset);
5363 if (!arg)
5364 arg = "";
5365 options->rename_score = parse_rename_score(&arg);
5366 if (*arg != 0)
5367 return error(_("invalid argument to %s"), opt->long_name);
5368
5369 if (options->detect_rename == DIFF_DETECT_COPY)
5370 options->flags.find_copies_harder = 1;
5371 else
5372 options->detect_rename = DIFF_DETECT_COPY;
5373
5374 return 0;
5375}
5376
Nguyễn Thái Ngọc Duyf4763082019-02-21 11:16:145377static int diff_opt_find_renames(const struct option *opt,
5378 const char *arg, int unset)
5379{
5380 struct diff_options *options = opt->value;
5381
5382 BUG_ON_OPT_NEG(unset);
5383 if (!arg)
5384 arg = "";
5385 options->rename_score = parse_rename_score(&arg);
5386 if (*arg != 0)
5387 return error(_("invalid argument to %s"), opt->long_name);
5388
5389 options->detect_rename = DIFF_DETECT_RENAME;
5390 return 0;
5391}
5392
Nguyễn Thái Ngọc Duy1e9250b2019-03-05 12:30:165393static int diff_opt_follow(const struct option *opt,
5394 const char *arg, int unset)
5395{
5396 struct diff_options *options = opt->value;
5397
5398 BUG_ON_OPT_ARG(arg);
5399 if (unset) {
5400 options->flags.follow_renames = 0;
5401 options->flags.default_follow_renames = 0;
5402 } else {
5403 options->flags.follow_renames = 1;
5404 }
5405 return 0;
5406}
5407
Nguyễn Thái Ngọc Duyb680ee12019-03-05 12:30:255408static int diff_opt_ignore_submodules(const struct option *opt,
5409 const char *arg, int unset)
5410{
5411 struct diff_options *options = opt->value;
5412
5413 BUG_ON_OPT_NEG(unset);
5414 if (!arg)
5415 arg = "all";
5416 options->flags.override_submodule_config = 1;
5417 handle_ignore_submodules_arg(options, arg);
5418 return 0;
5419}
5420
Nguyễn Thái Ngọc Duy2f81cf92019-03-24 08:20:065421static int diff_opt_line_prefix(const struct option *opt,
5422 const char *optarg, int unset)
5423{
5424 struct diff_options *options = opt->value;
5425
5426 BUG_ON_OPT_NEG(unset);
5427 options->line_prefix = optarg;
Nguyễn Thái Ngọc Duy2f81cf92019-03-24 08:20:065428 graph_setup_line_prefix(options);
5429 return 0;
5430}
5431
Nguyễn Thái Ngọc Duy11c659d2019-03-24 08:20:075432static int diff_opt_no_prefix(const struct option *opt,
5433 const char *optarg, int unset)
5434{
5435 struct diff_options *options = opt->value;
5436
5437 BUG_ON_OPT_NEG(unset);
5438 BUG_ON_OPT_ARG(optarg);
Jeff King6799aad2023-03-09 06:07:065439 diff_set_noprefix(options);
Nguyễn Thái Ngọc Duy11c659d2019-03-24 08:20:075440 return 0;
5441}
5442
Jeff Kingb39a5692023-03-09 06:09:545443static int diff_opt_default_prefix(const struct option *opt,
5444 const char *optarg, int unset)
5445{
5446 struct diff_options *options = opt->value;
5447
5448 BUG_ON_OPT_NEG(unset);
5449 BUG_ON_OPT_ARG(optarg);
Patrick Steinhardtf9c19892024-05-27 11:46:205450 FREE_AND_NULL(diff_src_prefix);
5451 FREE_AND_NULL(diff_dst_prefix);
Jeff Kingb39a5692023-03-09 06:09:545452 diff_set_default_prefix(options);
Ramkumar Ramachandrac47ef572012-11-13 15:42:455453 return 0;
5454}
5455
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 11:16:125456static enum parse_opt_result diff_opt_output(struct parse_opt_ctx_t *ctx,
5457 const struct option *opt,
5458 const char *arg, int unset)
5459{
5460 struct diff_options *options = opt->value;
5461 char *path;
5462
5463 BUG_ON_OPT_NEG(unset);
5464 path = prefix_filename(ctx->prefix, arg);
5465 options->file = xfopen(path, "w");
5466 options->close_file = 1;
5467 if (options->use_color != GIT_COLOR_ALWAYS)
5468 options->use_color = GIT_COLOR_NEVER;
5469 free(path);
5470 return 0;
5471}
5472
Nguyễn Thái Ngọc Duy31fd6402019-03-05 12:30:085473static int diff_opt_patience(const struct option *opt,
5474 const char *arg, int unset)
5475{
5476 struct diff_options *options = opt->value;
5477 int i;
5478
5479 BUG_ON_OPT_NEG(unset);
5480 BUG_ON_OPT_ARG(arg);
Nguyễn Thái Ngọc Duy31fd6402019-03-05 12:30:085481 /*
5482 * Both --patience and --anchored use PATIENCE_DIFF
5483 * internally, so remove any anchors previously
5484 * specified.
5485 */
5486 for (i = 0; i < options->anchors_nr; i++)
5487 free(options->anchors[i]);
5488 options->anchors_nr = 0;
John Caia4cf9002023-02-20 21:04:425489 options->ignore_driver_algorithm = 1;
John Cai11e95e12023-02-20 21:04:415490
5491 return set_diff_algorithm(options, "patience");
Nguyễn Thái Ngọc Duy31fd6402019-03-05 12:30:085492}
5493
Michał Kępień296d4a92020-10-20 06:48:095494static int diff_opt_ignore_regex(const struct option *opt,
5495 const char *arg, int unset)
5496{
5497 struct diff_options *options = opt->value;
5498 regex_t *regex;
5499
5500 BUG_ON_OPT_NEG(unset);
Patrick Steinhardt6b15d9c2024-08-14 06:52:425501
Michał Kępień296d4a92020-10-20 06:48:095502 regex = xmalloc(sizeof(*regex));
Patrick Steinhardt6b15d9c2024-08-14 06:52:425503 if (regcomp(regex, arg, REG_EXTENDED | REG_NEWLINE)) {
5504 free(regex);
Michał Kępień296d4a92020-10-20 06:48:095505 return error(_("invalid regex given to -I: '%s'"), arg);
Patrick Steinhardt6b15d9c2024-08-14 06:52:425506 }
5507
Michał Kępień296d4a92020-10-20 06:48:095508 ALLOC_GROW(options->ignore_regex, options->ignore_regex_nr + 1,
5509 options->ignore_regex_alloc);
5510 options->ignore_regex[options->ignore_regex_nr++] = regex;
5511 return 0;
5512}
5513
Nguyễn Thái Ngọc Duya41cfb32019-03-24 08:19:595514static int diff_opt_pickaxe_regex(const struct option *opt,
5515 const char *arg, int unset)
5516{
5517 struct diff_options *options = opt->value;
5518
5519 BUG_ON_OPT_NEG(unset);
5520 options->pickaxe = arg;
5521 options->pickaxe_opts |= DIFF_PICKAXE_KIND_G;
brian m. carlsona6200462025-02-17 17:57:595522 if (arg && !*arg)
5523 return error(_("-G requires a non-empty argument"));
Nguyễn Thái Ngọc Duya41cfb32019-03-24 08:19:595524 return 0;
5525}
5526
5527static int diff_opt_pickaxe_string(const struct option *opt,
5528 const char *arg, int unset)
5529{
5530 struct diff_options *options = opt->value;
5531
5532 BUG_ON_OPT_NEG(unset);
5533 options->pickaxe = arg;
5534 options->pickaxe_opts |= DIFF_PICKAXE_KIND_S;
brian m. carlsona6200462025-02-17 17:57:595535 if (arg && !*arg)
5536 return error(_("-S requires a non-empty argument"));
Nguyễn Thái Ngọc Duya41cfb32019-03-24 08:19:595537 return 0;
5538}
5539
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 11:16:195540static int diff_opt_relative(const struct option *opt,
5541 const char *arg, int unset)
5542{
5543 struct diff_options *options = opt->value;
5544
Laurent Arnoudc28ded82020-05-22 10:46:185545 options->flags.relative_name = !unset;
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 11:16:195546 if (arg)
5547 options->prefix = arg;
5548 return 0;
5549}
5550
Nguyễn Thái Ngọc Duy125dcea2019-03-05 12:30:265551static int diff_opt_submodule(const struct option *opt,
5552 const char *arg, int unset)
5553{
5554 struct diff_options *options = opt->value;
5555
5556 BUG_ON_OPT_NEG(unset);
5557 if (!arg)
5558 arg = "log";
5559 if (parse_submodule_params(options, arg))
5560 return error(_("failed to parse --submodule option parameter: '%s'"),
5561 arg);
5562 return 0;
5563}
5564
Nguyễn Thái Ngọc Duy8ab76972019-03-05 12:30:245565static int diff_opt_textconv(const struct option *opt,
5566 const char *arg, int unset)
5567{
5568 struct diff_options *options = opt->value;
5569
5570 BUG_ON_OPT_ARG(arg);
5571 if (unset) {
5572 options->flags.allow_textconv = 0;
5573 } else {
5574 options->flags.allow_textconv = 1;
5575 options->flags.textconv_set_via_cmdline = 1;
5576 }
5577 return 0;
5578}
5579
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 00:35:335580static int diff_opt_unified(const struct option *opt,
5581 const char *arg, int unset)
5582{
5583 struct diff_options *options = opt->value;
5584 char *s;
5585
5586 BUG_ON_OPT_NEG(unset);
5587
Nguyễn Thái Ngọc Duy8ef05192019-05-29 09:11:155588 if (arg) {
5589 options->context = strtol(arg, &s, 10);
5590 if (*s)
5591 return error(_("%s expects a numerical value"), "--unified");
5592 }
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 00:35:335593 enable_patch_output(&options->output_format);
5594
5595 return 0;
5596}
5597
Nguyễn Thái Ngọc Duye9fb39b2019-03-05 12:30:185598static int diff_opt_word_diff(const struct option *opt,
5599 const char *arg, int unset)
5600{
5601 struct diff_options *options = opt->value;
5602
5603 BUG_ON_OPT_NEG(unset);
5604 if (arg) {
5605 if (!strcmp(arg, "plain"))
5606 options->word_diff = DIFF_WORDS_PLAIN;
5607 else if (!strcmp(arg, "color")) {
Jeff King3c3e9b82025-09-16 20:13:285608 options->use_color = GIT_COLOR_ALWAYS;
Nguyễn Thái Ngọc Duye9fb39b2019-03-05 12:30:185609 options->word_diff = DIFF_WORDS_COLOR;
5610 }
5611 else if (!strcmp(arg, "porcelain"))
5612 options->word_diff = DIFF_WORDS_PORCELAIN;
5613 else if (!strcmp(arg, "none"))
5614 options->word_diff = DIFF_WORDS_NONE;
5615 else
5616 return error(_("bad --word-diff argument: %s"), arg);
5617 } else {
5618 if (options->word_diff == DIFF_WORDS_NONE)
5619 options->word_diff = DIFF_WORDS_PLAIN;
5620 }
5621 return 0;
5622}
5623
Nguyễn Thái Ngọc Duy797df112019-03-05 12:30:195624static int diff_opt_word_diff_regex(const struct option *opt,
5625 const char *arg, int unset)
5626{
5627 struct diff_options *options = opt->value;
5628
5629 BUG_ON_OPT_NEG(unset);
5630 if (options->word_diff == DIFF_WORDS_NONE)
5631 options->word_diff = DIFF_WORDS_PLAIN;
5632 options->word_regex = arg;
5633 return 0;
5634}
5635
Junio C Hamano1eb41362021-02-11 19:57:505636static int diff_opt_rotate_to(const struct option *opt, const char *arg, int unset)
5637{
5638 struct diff_options *options = opt->value;
5639
5640 BUG_ON_OPT_NEG(unset);
5641 if (!strcmp(opt->long_name, "skip-to"))
5642 options->skip_instead_of_rotate = 1;
5643 else
5644 options->skip_instead_of_rotate = 0;
5645 options->rotate_to = arg;
5646 return 0;
5647}
5648
Jeff Kinga1dfa542025-08-07 20:52:585649static int diff_opt_max_depth(const struct option *opt,
5650 const char *arg, int unset)
5651{
5652 struct diff_options *options = opt->value;
5653
5654 BUG_ON_OPT_NEG(unset);
5655
5656 if (!git_parse_int(arg, &options->max_depth))
5657 return error(_("invalid value for '%s': '%s'"),
5658 "--max-depth", arg);
5659
5660 options->flags.recursive = 1;
5661 options->max_depth_valid = options->max_depth >= 0;
5662
5663 return 0;
5664}
5665
Philippe Blain0a868032023-06-26 16:24:485666/*
5667 * Consider adding new flags to __git_diff_common_options
5668 * in contrib/completion/git-completion.bash
5669 */
René Scharfec5630c42022-12-01 22:49:115670struct option *add_diff_options(const struct option *opts,
5671 struct diff_options *options)
Nguyễn Thái Ngọc Duy4a288472019-01-27 00:35:315672{
5673 struct option parseopts[] = {
Nguyễn Thái Ngọc Duycc013c22019-01-27 00:35:325674 OPT_GROUP(N_("Diff output format options")),
5675 OPT_BITOP('p', "patch", &options->output_format,
5676 N_("generate patch"),
5677 DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
Junio C Hamano9d484b92023-05-05 16:59:525678 OPT_SET_INT('s', "no-patch", &options->output_format,
5679 N_("suppress diff output"), DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duycc013c22019-01-27 00:35:325680 OPT_BITOP('u', NULL, &options->output_format,
5681 N_("generate patch"),
5682 DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duyd473e2e2019-01-27 00:35:335683 OPT_CALLBACK_F('U', "unified", options, N_("<n>"),
5684 N_("generate diffs with <n> lines context"),
Junio C Hamano20aa7c52019-05-30 17:50:445685 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_unified),
Nguyễn Thái Ngọc Duy7fd9a1b2019-01-27 00:35:345686 OPT_BOOL('W', "function-context", &options->flags.funccontext,
5687 N_("generate diffs with <n> lines context")),
Junio C Hamano9d484b92023-05-05 16:59:525688 OPT_BITOP(0, "raw", &options->output_format,
Nguyễn Thái Ngọc Duyed881482019-01-27 00:35:355689 N_("generate the diff in raw format"),
Junio C Hamano9d484b92023-05-05 16:59:525690 DIFF_FORMAT_RAW, DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duyc659f302019-02-16 11:36:355691 OPT_BITOP(0, "patch-with-raw", &options->output_format,
5692 N_("synonym for '-p --raw'"),
5693 DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW,
5694 DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duye550f582019-02-21 11:16:065695 OPT_BITOP(0, "patch-with-stat", &options->output_format,
5696 N_("synonym for '-p --stat'"),
5697 DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT,
5698 DIFF_FORMAT_NO_OUTPUT),
Junio C Hamano9d484b92023-05-05 16:59:525699 OPT_BITOP(0, "numstat", &options->output_format,
Nguyễn Thái Ngọc Duye5673622019-02-16 11:36:365700 N_("machine friendly --stat"),
Junio C Hamano9d484b92023-05-05 16:59:525701 DIFF_FORMAT_NUMSTAT, DIFF_FORMAT_NO_OUTPUT),
5702 OPT_BITOP(0, "shortstat", &options->output_format,
Nguyễn Thái Ngọc Duye5673622019-02-16 11:36:365703 N_("output only the last line of --stat"),
Junio C Hamano9d484b92023-05-05 16:59:525704 DIFF_FORMAT_SHORTSTAT, DIFF_FORMAT_NO_OUTPUT),
Jiang Xin5e7013a2024-02-14 08:46:415705 OPT_CALLBACK_F('X', "dirstat", options, N_("<param1>,<param2>..."),
Nguyễn Thái Ngọc Duy4ce7aab2019-02-21 11:16:035706 N_("output the distribution of relative amount of changes for each sub-directory"),
5707 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5708 diff_opt_dirstat),
5709 OPT_CALLBACK_F(0, "cumulative", options, NULL,
5710 N_("synonym for --dirstat=cumulative"),
5711 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
5712 diff_opt_dirstat),
Jiang Xin5e7013a2024-02-14 08:46:415713 OPT_CALLBACK_F(0, "dirstat-by-file", options, N_("<param1>,<param2>..."),
5714 N_("synonym for --dirstat=files,<param1>,<param2>..."),
Nguyễn Thái Ngọc Duy4ce7aab2019-02-21 11:16:035715 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5716 diff_opt_dirstat),
Nguyễn Thái Ngọc Duyfc6af3e2019-02-21 11:16:045717 OPT_BIT_F(0, "check", &options->output_format,
5718 N_("warn if changes introduce conflict markers or whitespace errors"),
5719 DIFF_FORMAT_CHECKDIFF, PARSE_OPT_NONEG),
Junio C Hamano9d484b92023-05-05 16:59:525720 OPT_BITOP(0, "summary", &options->output_format,
Nguyễn Thái Ngọc Duy70a30412019-02-21 11:16:055721 N_("condensed summary such as creations, renames and mode changes"),
Junio C Hamano9d484b92023-05-05 16:59:525722 DIFF_FORMAT_SUMMARY, DIFF_FORMAT_NO_OUTPUT),
Nguyễn Thái Ngọc Duy0e840e22019-02-21 11:16:075723 OPT_BIT_F(0, "name-only", &options->output_format,
5724 N_("show only names of changed files"),
5725 DIFF_FORMAT_NAME, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duya2387472019-02-21 11:16:085726 OPT_BIT_F(0, "name-status", &options->output_format,
5727 N_("show only names and status of changed files"),
5728 DIFF_FORMAT_NAME_STATUS, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy84b50892019-02-21 11:16:105729 OPT_CALLBACK_F(0, "stat", options, N_("<width>[,<name-width>[,<count>]]"),
5730 N_("generate diffstat"),
5731 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_stat),
5732 OPT_CALLBACK_F(0, "stat-width", options, N_("<width>"),
5733 N_("generate diffstat with a given width"),
5734 PARSE_OPT_NONEG, diff_opt_stat),
5735 OPT_CALLBACK_F(0, "stat-name-width", options, N_("<width>"),
5736 N_("generate diffstat with a given name width"),
5737 PARSE_OPT_NONEG, diff_opt_stat),
5738 OPT_CALLBACK_F(0, "stat-graph-width", options, N_("<width>"),
5739 N_("generate diffstat with a given graph width"),
5740 PARSE_OPT_NONEG, diff_opt_stat),
5741 OPT_CALLBACK_F(0, "stat-count", options, N_("<count>"),
5742 N_("generate diffstat with limited lines"),
5743 PARSE_OPT_NONEG, diff_opt_stat),
Nguyễn Thái Ngọc Duy7d7942b2019-02-21 11:16:115744 OPT_CALLBACK_F(0, "compact-summary", options, NULL,
5745 N_("generate compact summary in diffstat"),
5746 PARSE_OPT_NOARG, diff_opt_compact_summary),
Nguyễn Thái Ngọc Duy6d9af6f2019-03-05 12:30:125747 OPT_CALLBACK_F(0, "binary", options, NULL,
5748 N_("output a binary diff that can be applied"),
5749 PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_binary),
Nguyễn Thái Ngọc Duy4fe01672019-03-05 12:30:135750 OPT_BOOL(0, "full-index", &options->flags.full_index,
5751 N_("show full pre- and post-image object names on the \"index\" lines")),
Nguyễn Thái Ngọc Duy8b81c262019-03-05 12:30:175752 OPT_COLOR_FLAG(0, "color", &options->use_color,
5753 N_("show colored diff")),
Nguyễn Thái Ngọc Duy5a749d92019-03-24 08:19:555754 OPT_CALLBACK_F(0, "ws-error-highlight", options, N_("<kind>"),
5755 N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
5756 PARSE_OPT_NONEG, diff_opt_ws_error_highlight),
Nguyễn Thái Ngọc Duy1a1eb162019-03-24 08:19:575757 OPT_SET_INT('z', NULL, &options->line_termination,
5758 N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
5759 0),
Nguyễn Thái Ngọc Duyd8774182019-03-24 08:20:045760 OPT__ABBREV(&options->abbrev),
Nguyễn Thái Ngọc Duy31fba9d2019-03-24 08:20:055761 OPT_STRING_F(0, "src-prefix", &options->a_prefix, N_("<prefix>"),
5762 N_("show the given source prefix instead of \"a/\""),
5763 PARSE_OPT_NONEG),
5764 OPT_STRING_F(0, "dst-prefix", &options->b_prefix, N_("<prefix>"),
Jiang Xin8a1569d2019-06-02 15:11:225765 N_("show the given destination prefix instead of \"b/\""),
Nguyễn Thái Ngọc Duy31fba9d2019-03-24 08:20:055766 PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy2f81cf92019-03-24 08:20:065767 OPT_CALLBACK_F(0, "line-prefix", options, N_("<prefix>"),
5768 N_("prepend an additional prefix to every line of output"),
5769 PARSE_OPT_NONEG, diff_opt_line_prefix),
Nguyễn Thái Ngọc Duy11c659d2019-03-24 08:20:075770 OPT_CALLBACK_F(0, "no-prefix", options, NULL,
5771 N_("do not show any source or destination prefix"),
5772 PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_no_prefix),
Jeff Kingb39a5692023-03-09 06:09:545773 OPT_CALLBACK_F(0, "default-prefix", options, NULL,
5774 N_("use default prefixes a/ and b/"),
5775 PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_default_prefix),
Nguyễn Thái Ngọc Duy16ed6c92019-03-24 08:20:085776 OPT_INTEGER_F(0, "inter-hunk-context", &options->interhunkcontext,
5777 N_("show context between diff hunks up to the specified number of lines"),
5778 PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyaf2f3682019-02-21 11:16:125779 OPT_CALLBACK_F(0, "output-indicator-new",
5780 &options->output_indicators[OUTPUT_INDICATOR_NEW],
5781 N_("<char>"),
5782 N_("specify the character to indicate a new line instead of '+'"),
5783 PARSE_OPT_NONEG, diff_opt_char),
5784 OPT_CALLBACK_F(0, "output-indicator-old",
5785 &options->output_indicators[OUTPUT_INDICATOR_OLD],
5786 N_("<char>"),
5787 N_("specify the character to indicate an old line instead of '-'"),
5788 PARSE_OPT_NONEG, diff_opt_char),
5789 OPT_CALLBACK_F(0, "output-indicator-context",
5790 &options->output_indicators[OUTPUT_INDICATOR_CONTEXT],
5791 N_("<char>"),
5792 N_("specify the character to indicate a context instead of ' '"),
5793 PARSE_OPT_NONEG, diff_opt_char),
5794
Nguyễn Thái Ngọc Duyced4e172019-02-21 11:16:135795 OPT_GROUP(N_("Diff rename options")),
5796 OPT_CALLBACK_F('B', "break-rewrites", &options->break_opt, N_("<n>[/<m>]"),
5797 N_("break complete rewrite changes into pairs of delete and create"),
5798 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5799 diff_opt_break_rewrites),
Nguyễn Thái Ngọc Duyf4763082019-02-21 11:16:145800 OPT_CALLBACK_F('M', "find-renames", options, N_("<n>"),
5801 N_("detect renames"),
5802 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5803 diff_opt_find_renames),
Nguyễn Thái Ngọc Duy1e533292019-02-21 11:16:155804 OPT_SET_INT_F('D', "irreversible-delete", &options->irreversible_delete,
5805 N_("omit the preimage for deletes"),
5806 1, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy7f648502019-02-21 11:16:165807 OPT_CALLBACK_F('C', "find-copies", options, N_("<n>"),
5808 N_("detect copies"),
5809 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5810 diff_opt_find_copies),
Nguyễn Thái Ngọc Duybdd47412019-02-21 11:16:175811 OPT_BOOL(0, "find-copies-harder", &options->flags.find_copies_harder,
5812 N_("use unmodified files as source to find copies")),
Nguyễn Thái Ngọc Duycdc43eb2019-02-21 11:16:185813 OPT_SET_INT_F(0, "no-renames", &options->detect_rename,
5814 N_("disable rename detection"),
5815 0, PARSE_OPT_NONEG),
5816 OPT_BOOL(0, "rename-empty", &options->flags.rename_empty,
5817 N_("use empty blobs as rename source")),
Nguyễn Thái Ngọc Duy1e9250b2019-03-05 12:30:165818 OPT_CALLBACK_F(0, "follow", options, NULL,
5819 N_("continue listing the history of a file beyond renames"),
5820 PARSE_OPT_NOARG, diff_opt_follow),
Nguyễn Thái Ngọc Duybffee742019-03-24 08:19:585821 OPT_INTEGER('l', NULL, &options->rename_limit,
5822 N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")),
Nguyễn Thái Ngọc Duyced4e172019-02-21 11:16:135823
Nguyễn Thái Ngọc Duy2e75f922019-02-21 11:16:205824 OPT_GROUP(N_("Diff algorithm options")),
John Cai11e95e12023-02-20 21:04:415825 OPT_CALLBACK_F(0, "minimal", options, NULL,
5826 N_("produce the smallest possible diff"),
5827 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
5828 diff_opt_diff_algorithm_no_arg),
Nguyễn Thái Ngọc Duy87649a12019-02-21 11:16:215829 OPT_BIT_F('w', "ignore-all-space", &options->xdl_opts,
5830 N_("ignore whitespace when comparing lines"),
5831 XDF_IGNORE_WHITESPACE, PARSE_OPT_NONEG),
5832 OPT_BIT_F('b', "ignore-space-change", &options->xdl_opts,
5833 N_("ignore changes in amount of whitespace"),
5834 XDF_IGNORE_WHITESPACE_CHANGE, PARSE_OPT_NONEG),
5835 OPT_BIT_F(0, "ignore-space-at-eol", &options->xdl_opts,
5836 N_("ignore changes in whitespace at EOL"),
5837 XDF_IGNORE_WHITESPACE_AT_EOL, PARSE_OPT_NONEG),
5838 OPT_BIT_F(0, "ignore-cr-at-eol", &options->xdl_opts,
5839 N_("ignore carrier-return at the end of line"),
5840 XDF_IGNORE_CR_AT_EOL, PARSE_OPT_NONEG),
5841 OPT_BIT_F(0, "ignore-blank-lines", &options->xdl_opts,
5842 N_("ignore changes whose lines are all blank"),
5843 XDF_IGNORE_BLANK_LINES, PARSE_OPT_NONEG),
Michał Kępień296d4a92020-10-20 06:48:095844 OPT_CALLBACK_F('I', "ignore-matching-lines", options, N_("<regex>"),
5845 N_("ignore changes whose all lines match <regex>"),
5846 0, diff_opt_ignore_regex),
Nguyễn Thái Ngọc Duy06f77512019-03-05 12:30:075847 OPT_BIT(0, "indent-heuristic", &options->xdl_opts,
5848 N_("heuristic to shift diff hunk boundaries for easy reading"),
5849 XDF_INDENT_HEURISTIC),
Nguyễn Thái Ngọc Duy31fd6402019-03-05 12:30:085850 OPT_CALLBACK_F(0, "patience", options, NULL,
5851 N_("generate diff using the \"patience diff\" algorithm"),
5852 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
5853 diff_opt_patience),
John Cai11e95e12023-02-20 21:04:415854 OPT_CALLBACK_F(0, "histogram", options, NULL,
5855 N_("generate diff using the \"histogram diff\" algorithm"),
5856 PARSE_OPT_NONEG | PARSE_OPT_NOARG,
5857 diff_opt_diff_algorithm_no_arg),
Nguyễn Thái Ngọc Duy10f35b12019-03-05 12:30:105858 OPT_CALLBACK_F(0, "diff-algorithm", options, N_("<algorithm>"),
5859 N_("choose a diff algorithm"),
5860 PARSE_OPT_NONEG, diff_opt_diff_algorithm),
Nguyễn Thái Ngọc Duydf84a432019-03-05 12:30:115861 OPT_CALLBACK_F(0, "anchored", options, N_("<text>"),
5862 N_("generate diff using the \"anchored diff\" algorithm"),
5863 PARSE_OPT_NONEG, diff_opt_anchored),
Nguyễn Thái Ngọc Duye9fb39b2019-03-05 12:30:185864 OPT_CALLBACK_F(0, "word-diff", options, N_("<mode>"),
5865 N_("show word diff, using <mode> to delimit changed words"),
5866 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_word_diff),
Nguyễn Thái Ngọc Duy797df112019-03-05 12:30:195867 OPT_CALLBACK_F(0, "word-diff-regex", options, N_("<regex>"),
5868 N_("use <regex> to decide what a word is"),
5869 PARSE_OPT_NONEG, diff_opt_word_diff_regex),
Nguyễn Thái Ngọc Duy212db692019-03-05 12:30:205870 OPT_CALLBACK_F(0, "color-words", options, N_("<regex>"),
5871 N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
5872 PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_color_words),
Nguyễn Thái Ngọc Duy59311a92019-03-24 08:20:095873 OPT_CALLBACK_F(0, "color-moved", options, N_("<mode>"),
Jean-Noël Avila8bcd8f42019-05-17 19:26:195874 N_("moved lines of code are colored differently"),
Nguyễn Thái Ngọc Duy59311a92019-03-24 08:20:095875 PARSE_OPT_OPTARG, diff_opt_color_moved),
Nguyễn Thái Ngọc Duy8ce20202019-03-24 08:20:105876 OPT_CALLBACK_F(0, "color-moved-ws", options, N_("<mode>"),
5877 N_("how white spaces are ignored in --color-moved"),
Nguyễn Thái Ngọc Duybb987292019-03-24 08:20:115878 0, diff_opt_color_moved_ws),
Nguyễn Thái Ngọc Duy2e75f922019-02-21 11:16:205879
Jean-Noël Avila8bcd8f42019-05-17 19:26:195880 OPT_GROUP(N_("Other diff options")),
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 11:16:195881 OPT_CALLBACK_F(0, "relative", options, N_("<prefix>"),
5882 N_("when run from subdir, exclude changes outside and show relative paths"),
Laurent Arnoudc28ded82020-05-22 10:46:185883 PARSE_OPT_OPTARG,
Nguyễn Thái Ngọc Duy0b1c5b52019-02-21 11:16:195884 diff_opt_relative),
Nguyễn Thái Ngọc Duyc37d4c02019-03-05 12:30:145885 OPT_BOOL('a', "text", &options->flags.text,
5886 N_("treat all files as text")),
Nguyễn Thái Ngọc Duy4f99f292019-03-05 12:30:155887 OPT_BOOL('R', NULL, &options->flags.reverse_diff,
5888 N_("swap two inputs, reverse the diff")),
Nguyễn Thái Ngọc Duy1086ea02019-03-05 12:30:215889 OPT_BOOL(0, "exit-code", &options->flags.exit_with_status,
5890 N_("exit with 1 if there were differences, 0 otherwise")),
Nguyễn Thái Ngọc Duy9bbaf1c2019-03-05 12:30:225891 OPT_BOOL(0, "quiet", &options->flags.quick,
5892 N_("disable all output of the program")),
Nguyễn Thái Ngọc Duy0cda1002019-03-05 12:30:235893 OPT_BOOL(0, "ext-diff", &options->flags.allow_external,
5894 N_("allow an external diff helper to be executed")),
Nguyễn Thái Ngọc Duy8ab76972019-03-05 12:30:245895 OPT_CALLBACK_F(0, "textconv", options, NULL,
5896 N_("run external text conversion filters when comparing binary files"),
5897 PARSE_OPT_NOARG, diff_opt_textconv),
Nguyễn Thái Ngọc Duyb680ee12019-03-05 12:30:255898 OPT_CALLBACK_F(0, "ignore-submodules", options, N_("<when>"),
5899 N_("ignore changes to submodules in the diff generation"),
5900 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5901 diff_opt_ignore_submodules),
Nguyễn Thái Ngọc Duy125dcea2019-03-05 12:30:265902 OPT_CALLBACK_F(0, "submodule", options, N_("<format>"),
5903 N_("specify how differences in submodules are shown"),
5904 PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
5905 diff_opt_submodule),
Nguyễn Thái Ngọc Duy96860e42019-03-24 08:19:565906 OPT_SET_INT_F(0, "ita-invisible-in-index", &options->ita_invisible_in_index,
5907 N_("hide 'git add -N' entries from the index"),
5908 1, PARSE_OPT_NONEG),
5909 OPT_SET_INT_F(0, "ita-visible-in-index", &options->ita_invisible_in_index,
5910 N_("treat 'git add -N' entries as real in the index"),
5911 0, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duya41cfb32019-03-24 08:19:595912 OPT_CALLBACK_F('S', NULL, options, N_("<string>"),
5913 N_("look for differences that change the number of occurrences of the specified string"),
5914 0, diff_opt_pickaxe_string),
5915 OPT_CALLBACK_F('G', NULL, options, N_("<regex>"),
5916 N_("look for differences that change the number of occurrences of the specified regex"),
5917 0, diff_opt_pickaxe_regex),
Nguyễn Thái Ngọc Duy85f8e882019-03-24 08:20:005918 OPT_BIT_F(0, "pickaxe-all", &options->pickaxe_opts,
5919 N_("show all changes in the changeset with -S or -G"),
5920 DIFF_PICKAXE_ALL, PARSE_OPT_NONEG),
5921 OPT_BIT_F(0, "pickaxe-regex", &options->pickaxe_opts,
5922 N_("treat <string> in -S as extended POSIX regular expression"),
5923 DIFF_PICKAXE_REGEX, PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duyf7318142019-03-24 08:20:015924 OPT_FILENAME('O', NULL, &options->orderfile,
5925 N_("control the order in which files appear in the output")),
Junio C Hamano1eb41362021-02-11 19:57:505926 OPT_CALLBACK_F(0, "rotate-to", options, N_("<path>"),
5927 N_("show the change in the specified path first"),
5928 PARSE_OPT_NONEG, diff_opt_rotate_to),
5929 OPT_CALLBACK_F(0, "skip-to", options, N_("<path>"),
5930 N_("skip the output to the specified path"),
5931 PARSE_OPT_NONEG, diff_opt_rotate_to),
Nguyễn Thái Ngọc Duya75f28c2019-03-24 08:20:025932 OPT_CALLBACK_F(0, "find-object", options, N_("<object-id>"),
5933 N_("look for differences that change the number of occurrences of the specified object"),
5934 PARSE_OPT_NONEG, diff_opt_find_object),
Nguyễn Thái Ngọc Duyd2d3f272019-03-24 08:20:035935 OPT_CALLBACK_F(0, "diff-filter", options, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
5936 N_("select files by diff type"),
5937 PARSE_OPT_NONEG, diff_opt_diff_filter),
Jeff Kinga1dfa542025-08-07 20:52:585938 OPT_CALLBACK_F(0, "max-depth", options, N_("<depth>"),
5939 N_("maximum tree depth to recurse"),
5940 PARSE_OPT_NONEG, diff_opt_max_depth),
5941
Patrick Steinhardtd012ceb2025-04-17 10:49:375942 {
5943 .type = OPTION_CALLBACK,
5944 .long_name = "output",
5945 .value = options,
5946 .argh = N_("<file>"),
5947 .help = N_("output to a specific file"),
5948 .flags = PARSE_OPT_NONEG,
5949 .ll_callback = diff_opt_output,
5950 },
Nguyễn Thái Ngọc Duy4a288472019-01-27 00:35:315951 OPT_END()
5952 };
5953
René Scharfe189e97b2022-12-01 22:53:175954 return parse_options_concat(opts, parseopts);
Nguyễn Thái Ngọc Duy4a288472019-01-27 00:35:315955}
5956
Duy Nguyena97262c2016-01-21 11:48:445957int diff_opt_parse(struct diff_options *options,
5958 const char **av, int ac, const char *prefix)
Junio C Hamano6973dca2006-04-22 06:57:455959{
René Scharfe6c6048f2022-12-01 22:51:215960 struct option no_options[] = { OPT_END() };
5961 struct option *parseopts = add_diff_options(no_options, options);
5962
Duy Nguyena97262c2016-01-21 11:48:445963 if (!prefix)
5964 prefix = "";
5965
René Scharfe6c6048f2022-12-01 22:51:215966 ac = parse_options(ac, av, prefix, parseopts, NULL,
Nguyễn Thái Ngọc Duy4a288472019-01-27 00:35:315967 PARSE_OPT_KEEP_DASHDASH |
SZEDER Gábor99d86d62022-08-19 16:03:575968 PARSE_OPT_KEEP_UNKNOWN_OPT |
Nguyễn Thái Ngọc Duy4a288472019-01-27 00:35:315969 PARSE_OPT_NO_INTERNAL_HELP |
5970 PARSE_OPT_ONE_SHOT |
5971 PARSE_OPT_STOP_AT_NON_OPTION);
René Scharfe6c6048f2022-12-01 22:51:215972 free(parseopts);
Nguyễn Thái Ngọc Duy4a288472019-01-27 00:35:315973
Nguyễn Thái Ngọc Duy8ce20202019-03-24 08:20:105974 return ac;
Junio C Hamano6973dca2006-04-22 06:57:455975}
5976
Kevin Ballard10ae7522010-09-27 23:58:255977int parse_rename_score(const char **cp_p)
Junio C Hamano6973dca2006-04-22 06:57:455978{
5979 unsigned long num, scale;
5980 int ch, dot;
5981 const char *cp = *cp_p;
5982
5983 num = 0;
5984 scale = 1;
5985 dot = 0;
Brian Gianforcaroeeefa7c2009-09-01 05:35:105986 for (;;) {
Junio C Hamano6973dca2006-04-22 06:57:455987 ch = *cp;
5988 if ( !dot && ch == '.' ) {
5989 scale = 1;
5990 dot = 1;
5991 } else if ( ch == '%' ) {
5992 scale = dot ? scale*100 : 100;
5993 cp++; /* % is always at the end */
5994 break;
5995 } else if ( ch >= '0' && ch <= '9' ) {
5996 if ( scale < 100000 ) {
5997 scale *= 10;
5998 num = (num*10) + (ch-'0');
5999 }
6000 } else {
6001 break;
6002 }
6003 cp++;
6004 }
6005 *cp_p = cp;
6006
6007 /* user says num divided by scale and we say internally that
6008 * is MAX_SCORE * num / scale.
6009 */
Shawn O. Pearcedc49cd72007-03-07 01:44:376010 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
Junio C Hamano6973dca2006-04-22 06:57:456011}
6012
Junio C Hamano6973dca2006-04-22 06:57:456013struct diff_queue_struct diff_queued_diff;
6014
6015void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
6016{
Dmitry S. Dolzhenko4c960a42014-03-03 22:31:536017 ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc);
Junio C Hamano6973dca2006-04-22 06:57:456018 queue->queue[queue->nr++] = dp;
6019}
6020
6021struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
6022 struct diff_filespec *one,
6023 struct diff_filespec *two)
6024{
Junio C Hamanoef677682006-08-03 19:01:016025 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
Junio C Hamano6973dca2006-04-22 06:57:456026 dp->one = one;
6027 dp->two = two;
Junio C Hamano6973dca2006-04-22 06:57:456028 if (queue)
6029 diff_q(queue, dp);
6030 return dp;
6031}
6032
6033void diff_free_filepair(struct diff_filepair *p)
6034{
Linus Torvalds9fb88412007-10-25 18:19:106035 free_filespec(p->one);
6036 free_filespec(p->two);
Junio C Hamano6973dca2006-04-22 06:57:456037 free(p);
6038}
6039
Patrick Steinhardta5aecb22024-09-30 09:13:456040void diff_queue_init(struct diff_queue_struct *q)
6041{
6042 struct diff_queue_struct blank = DIFF_QUEUE_INIT;
6043 memcpy(q, &blank, sizeof(*q));
6044}
6045
6046void diff_queue_clear(struct diff_queue_struct *q)
SZEDER Gábor04ae0002022-11-02 22:01:406047{
6048 for (int i = 0; i < q->nr; i++)
6049 diff_free_filepair(q->queue[i]);
6050 free(q->queue);
Patrick Steinhardta5aecb22024-09-30 09:13:456051 diff_queue_init(q);
SZEDER Gábor04ae0002022-11-02 22:01:406052}
6053
Jeff Kingd6cece52016-10-20 06:20:076054const char *diff_aligned_abbrev(const struct object_id *oid, int len)
Junio C Hamano6973dca2006-04-22 06:57:456055{
6056 int abblen;
6057 const char *abbrev;
Junio C Hamano6973dca2006-04-22 06:57:456058
Ann T Ropea7cb6ac12017-12-03 21:27:426059 /* Do we want all 40 hex characters? */
brian m. carlson02afca12018-07-16 01:28:056060 if (len == the_hash_algo->hexsz)
Jeff Kingd6cece52016-10-20 06:20:076061 return oid_to_hex(oid);
6062
Ann T Ropea7cb6ac12017-12-03 21:27:426063 /* An abbreviated value is fine, possibly followed by an ellipsis. */
Jeff King4f036662016-10-20 06:21:256064 abbrev = diff_abbrev_oid(oid, len);
Ann T Ropea7cb6ac12017-12-03 21:27:426065
6066 if (!print_sha1_ellipsis())
6067 return abbrev;
6068
Junio C Hamano6973dca2006-04-22 06:57:456069 abblen = strlen(abbrev);
Junio C Hamanod709f1f2016-09-30 17:42:056070
6071 /*
Ann T Ropea7cb6ac12017-12-03 21:27:426072 * In well-behaved cases, where the abbreviated result is the
Junio C Hamanod709f1f2016-09-30 17:42:056073 * same as the requested length, append three dots after the
6074 * abbreviation (hence the whole logic is limited to the case
6075 * where abblen < 37); when the actual abbreviated result is a
6076 * bit longer than the requested length, we reduce the number
6077 * of dots so that they match the well-behaved ones. However,
6078 * if the actual abbreviation is longer than the requested
6079 * length by more than three, we give up on aligning, and add
6080 * three dots anyway, to indicate that the output is not the
6081 * full object name. Yes, this may be suboptimal, but this
6082 * appears only in "diff --raw --abbrev" output and it is not
6083 * worth the effort to change it now. Note that this would
6084 * likely to work fine when the automatic sizing of default
6085 * abbreviation length is used--we would be fed -1 in "len" in
6086 * that case, and will end up always appending three-dots, but
6087 * the automatic sizing is supposed to give abblen that ensures
6088 * uniqueness across all objects (statistically speaking).
6089 */
brian m. carlson02afca12018-07-16 01:28:056090 if (abblen < the_hash_algo->hexsz - 3) {
brian m. carlsondc015052017-03-26 16:01:246091 static char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano6973dca2006-04-22 06:57:456092 if (len < abblen && abblen <= len + 2)
Jeff King5096d492015-09-24 21:06:086093 xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");
Junio C Hamano6973dca2006-04-22 06:57:456094 else
Jeff King5096d492015-09-24 21:06:086095 xsnprintf(hex, sizeof(hex), "%s...", abbrev);
Junio C Hamano6973dca2006-04-22 06:57:456096 return hex;
6097 }
Jeff Kingd6cece52016-10-20 06:20:076098
6099 return oid_to_hex(oid);
Junio C Hamano6973dca2006-04-22 06:57:456100}
6101
Pierre Habouzit663af342007-09-19 22:42:156102static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
Junio C Hamano6973dca2006-04-22 06:57:456103{
Pierre Habouzit663af342007-09-19 22:42:156104 int line_termination = opt->line_termination;
6105 int inter_name_termination = line_termination ? '\t' : '\0';
Timo Hirvonenc6744342006-06-24 17:21:536106
John Keeping30997bb2013-02-07 20:15:276107 fprintf(opt->file, "%s", diff_line_prefix(opt));
Pierre Habouzit663af342007-09-19 22:42:156108 if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
Daniel Barkalowc0c77732008-03-10 02:43:396109 fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
Jeff Kingd6cece52016-10-20 06:20:076110 diff_aligned_abbrev(&p->one->oid, opt->abbrev));
brian m. carlsona0d12c42016-06-24 23:09:236111 fprintf(opt->file, "%s ",
Jeff Kingd6cece52016-10-20 06:20:076112 diff_aligned_abbrev(&p->two->oid, opt->abbrev));
Pierre Habouzit663af342007-09-19 22:42:156113 }
6114 if (p->score) {
Daniel Barkalowc0c77732008-03-10 02:43:396115 fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),
6116 inter_name_termination);
Pierre Habouzit663af342007-09-19 22:42:156117 } else {
Daniel Barkalowc0c77732008-03-10 02:43:396118 fprintf(opt->file, "%c%c", p->status, inter_name_termination);
Junio C Hamano6973dca2006-04-22 06:57:456119 }
6120
Junio C Hamanocd676a52008-02-12 22:26:026121 if (p->status == DIFF_STATUS_COPIED ||
6122 p->status == DIFF_STATUS_RENAMED) {
6123 const char *name_a, *name_b;
6124 name_a = p->one->path;
6125 name_b = p->two->path;
6126 strip_prefix(opt->prefix_length, &name_a, &name_b);
Daniel Barkalowc0c77732008-03-10 02:43:396127 write_name_quoted(name_a, opt->file, inter_name_termination);
6128 write_name_quoted(name_b, opt->file, line_termination);
Pierre Habouzit663af342007-09-19 22:42:156129 } else {
Junio C Hamanocd676a52008-02-12 22:26:026130 const char *name_a, *name_b;
6131 name_a = p->one->mode ? p->one->path : p->two->path;
6132 name_b = NULL;
6133 strip_prefix(opt->prefix_length, &name_a, &name_b);
Daniel Barkalowc0c77732008-03-10 02:43:396134 write_name_quoted(name_a, opt->file, line_termination);
Junio C Hamano6973dca2006-04-22 06:57:456135 }
Junio C Hamano6973dca2006-04-22 06:57:456136}
6137
6138int diff_unmodified_pair(struct diff_filepair *p)
6139{
6140 /* This function is written stricter than necessary to support
6141 * the currently implemented transformers, but the idea is to
6142 * let transformers to produce diff_filepairs any way they want,
6143 * and filter and clean them up here before producing the output.
6144 */
Pierre Habouzit663af342007-09-19 22:42:156145 struct diff_filespec *one = p->one, *two = p->two;
Junio C Hamano6973dca2006-04-22 06:57:456146
6147 if (DIFF_PAIR_UNMERGED(p))
6148 return 0; /* unmerged is interesting */
6149
Junio C Hamano6973dca2006-04-22 06:57:456150 /* deletion, addition, mode or type change
6151 * and rename are all interesting.
6152 */
6153 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
6154 DIFF_PAIR_MODE_CHANGED(p) ||
6155 strcmp(one->path, two->path))
6156 return 0;
6157
6158 /* both are valid and point at the same path. that is, we are
6159 * dealing with a change.
6160 */
brian m. carlson41c95602016-06-24 23:09:246161 if (one->oid_valid && two->oid_valid &&
Jeff King4a7e27e2018-08-28 21:22:406162 oideq(&one->oid, &two->oid) &&
Jens Lehmann85adbf22010-03-12 21:23:526163 !one->dirty_submodule && !two->dirty_submodule)
Junio C Hamano6973dca2006-04-22 06:57:456164 return 1; /* no change */
brian m. carlson41c95602016-06-24 23:09:246165 if (!one->oid_valid && !two->oid_valid)
Junio C Hamano6973dca2006-04-22 06:57:456166 return 1; /* both look at the same file on the filesystem. */
6167 return 0;
6168}
6169
6170static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
6171{
Elijah Newren95433ee2022-02-02 02:37:346172 int include_conflict_headers =
6173 (additional_headers(o, p->one->path) &&
Elijah Newren71a146d2022-09-02 03:53:296174 !o->pickaxe_opts &&
Elijah Newren95433ee2022-02-02 02:37:346175 (!o->filter || filter_bit_tst(DIFF_STATUS_UNMERGED, o)));
6176
6177 /*
6178 * Check if we can return early without showing a diff. Note that
6179 * diff_filepair only stores {oid, path, mode, is_valid}
6180 * information for each path, and thus diff_unmodified_pair() only
6181 * considers those bits of info. However, we do not want pairs
6182 * created by create_filepairs_for_header_only_notifications()
6183 * (which always look like unmodified pairs) to be ignored, so
6184 * return early if both p is unmodified AND we don't want to
6185 * include_conflict_headers.
6186 */
6187 if (diff_unmodified_pair(p) && !include_conflict_headers)
Junio C Hamano6973dca2006-04-22 06:57:456188 return;
6189
Elijah Newren95433ee2022-02-02 02:37:346190 /* Actually, we can also return early to avoid showing tree diffs */
Junio C Hamano6973dca2006-04-22 06:57:456191 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
6192 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Elijah Newren95433ee2022-02-02 02:37:346193 return;
Junio C Hamano6973dca2006-04-22 06:57:456194
6195 run_diff(p, o);
6196}
6197
Lidong Yanb55e6d32025-08-08 03:30:196198/* return 1 if any change is found; otherwise, return 0 */
6199static int diff_flush_patch_quietly(struct diff_filepair *p, struct diff_options *o)
6200{
Jeff Kingb2b5ad52025-10-24 17:08:536201 FILE *saved_file = o->file;
Lidong Yanb55e6d32025-08-08 03:30:196202 int saved_found_changes = o->found_changes;
6203 int ret;
6204
Jeff Kingb2b5ad52025-10-24 17:08:536205 o->file = NULL;
Lidong Yanb55e6d32025-08-08 03:30:196206 o->found_changes = 0;
6207 diff_flush_patch(p, o);
6208 ret = o->found_changes;
Jeff Kingb2b5ad52025-10-24 17:08:536209 o->file = saved_file;
Lidong Yanb55e6d32025-08-08 03:30:196210 o->found_changes |= saved_found_changes;
6211 return ret;
6212}
6213
Junio C Hamano6973dca2006-04-22 06:57:456214static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
6215 struct diffstat_t *diffstat)
6216{
6217 if (diff_unmodified_pair(p))
6218 return;
6219
6220 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
6221 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Yann Dirsonc3fced62010-10-24 21:03:106222 return; /* no useful stat for tree diffs */
Junio C Hamano6973dca2006-04-22 06:57:456223
6224 run_diffstat(p, o, diffstat);
6225}
6226
Johannes Schindelin88246892006-05-20 21:43:136227static void diff_flush_checkdiff(struct diff_filepair *p,
6228 struct diff_options *o)
6229{
6230 if (diff_unmodified_pair(p))
6231 return;
6232
6233 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
6234 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
Yann Dirsonc3fced62010-10-24 21:03:106235 return; /* nothing to check in tree diffs */
Johannes Schindelin88246892006-05-20 21:43:136236
6237 run_checkdiff(p, o);
6238}
6239
Elijah Newren95433ee2022-02-02 02:37:346240int diff_queue_is_empty(struct diff_options *o)
Junio C Hamano6973dca2006-04-22 06:57:456241{
6242 struct diff_queue_struct *q = &diff_queued_diff;
6243 int i;
Elijah Newren95433ee2022-02-02 02:37:346244 int include_conflict_headers =
6245 (o->additional_path_headers &&
Elijah Newren67360b72022-09-02 03:53:306246 strmap_get_size(o->additional_path_headers) &&
Elijah Newren71a146d2022-09-02 03:53:296247 !o->pickaxe_opts &&
Elijah Newren95433ee2022-02-02 02:37:346248 (!o->filter || filter_bit_tst(DIFF_STATUS_UNMERGED, o)));
6249
6250 if (include_conflict_headers)
6251 return 0;
6252
Junio C Hamano6973dca2006-04-22 06:57:456253 for (i = 0; i < q->nr; i++)
6254 if (!diff_unmodified_pair(q->queue[i]))
6255 return 0;
6256 return 1;
6257}
6258
6259#if DIFF_DEBUG
6260void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
6261{
6262 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
6263 x, one ? one : "",
6264 s->path,
6265 DIFF_FILE_VALID(s) ? "valid" : "invalid",
6266 s->mode,
brian m. carlson41c95602016-06-24 23:09:246267 s->oid_valid ? oid_to_hex(&s->oid) : "");
Jeff King428d52a2014-01-17 01:21:596268 fprintf(stderr, "queue[%d] %s size %lu\n",
Junio C Hamano6973dca2006-04-22 06:57:456269 x, one ? one : "",
Jeff King428d52a2014-01-17 01:21:596270 s->size);
Junio C Hamano6973dca2006-04-22 06:57:456271}
6272
6273void diff_debug_filepair(const struct diff_filepair *p, int i)
6274{
6275 diff_debug_filespec(p->one, i, "one");
6276 diff_debug_filespec(p->two, i, "two");
Linus Torvalds64479712007-10-25 18:20:566277 fprintf(stderr, "score %d, status %c rename_used %d broken %d\n",
Junio C Hamano6973dca2006-04-22 06:57:456278 p->score, p->status ? p->status : '?',
Linus Torvalds64479712007-10-25 18:20:566279 p->one->rename_used, p->broken_pair);
Junio C Hamano6973dca2006-04-22 06:57:456280}
6281
6282void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
6283{
6284 int i;
6285 if (msg)
6286 fprintf(stderr, "%s\n", msg);
6287 fprintf(stderr, "q->nr = %d\n", q->nr);
6288 for (i = 0; i < q->nr; i++) {
6289 struct diff_filepair *p = q->queue[i];
6290 diff_debug_filepair(p, i);
6291 }
6292}
6293#endif
6294
6295static void diff_resolve_rename_copy(void)
6296{
Linus Torvalds64479712007-10-25 18:20:566297 int i;
6298 struct diff_filepair *p;
Junio C Hamano6973dca2006-04-22 06:57:456299 struct diff_queue_struct *q = &diff_queued_diff;
6300
6301 diff_debug_queue("resolve-rename-copy", q);
6302
6303 for (i = 0; i < q->nr; i++) {
6304 p = q->queue[i];
6305 p->status = 0; /* undecided */
6306 if (DIFF_PAIR_UNMERGED(p))
6307 p->status = DIFF_STATUS_UNMERGED;
6308 else if (!DIFF_FILE_VALID(p->one))
6309 p->status = DIFF_STATUS_ADDED;
6310 else if (!DIFF_FILE_VALID(p->two))
6311 p->status = DIFF_STATUS_DELETED;
6312 else if (DIFF_PAIR_TYPE_CHANGED(p))
6313 p->status = DIFF_STATUS_TYPE_CHANGED;
6314
6315 /* from this point on, we are dealing with a pair
6316 * whose both sides are valid and of the same type, i.e.
6317 * either in-place edit or rename/copy edit.
6318 */
6319 else if (DIFF_PAIR_RENAME(p)) {
Linus Torvalds64479712007-10-25 18:20:566320 /*
6321 * A rename might have re-connected a broken
6322 * pair up, causing the pathnames to be the
6323 * same again. If so, that's not a rename at
6324 * all, just a modification..
6325 *
6326 * Otherwise, see if this source was used for
6327 * multiple renames, in which case we decrement
6328 * the count, and call it a copy.
Junio C Hamano6973dca2006-04-22 06:57:456329 */
Linus Torvalds64479712007-10-25 18:20:566330 if (!strcmp(p->one->path, p->two->path))
6331 p->status = DIFF_STATUS_MODIFIED;
6332 else if (--p->one->rename_used > 0)
Junio C Hamano6973dca2006-04-22 06:57:456333 p->status = DIFF_STATUS_COPIED;
Linus Torvalds64479712007-10-25 18:20:566334 else
Junio C Hamano6973dca2006-04-22 06:57:456335 p->status = DIFF_STATUS_RENAMED;
6336 }
Jeff King9001dc22018-08-28 21:22:486337 else if (!oideq(&p->one->oid, &p->two->oid) ||
Johannes Schindelind516c2d2007-02-22 20:50:106338 p->one->mode != p->two->mode ||
Jens Lehmann85adbf22010-03-12 21:23:526339 p->one->dirty_submodule ||
6340 p->two->dirty_submodule ||
brian m. carlsona0d12c42016-06-24 23:09:236341 is_null_oid(&p->one->oid))
Junio C Hamano6973dca2006-04-22 06:57:456342 p->status = DIFF_STATUS_MODIFIED;
6343 else {
6344 /* This is a "no-change" entry and should not
6345 * happen anymore, but prepare for broken callers.
6346 */
6347 error("feeding unmodified %s to diffcore",
6348 p->one->path);
6349 p->status = DIFF_STATUS_UNKNOWN;
6350 }
6351 }
6352 diff_debug_queue("resolve-rename-copy done", q);
6353}
6354
Timo Hirvonenc6744342006-06-24 17:21:536355static int check_pair_status(struct diff_filepair *p)
Junio C Hamano6973dca2006-04-22 06:57:456356{
Junio C Hamano6973dca2006-04-22 06:57:456357 switch (p->status) {
6358 case DIFF_STATUS_UNKNOWN:
Timo Hirvonenc6744342006-06-24 17:21:536359 return 0;
Junio C Hamano6973dca2006-04-22 06:57:456360 case 0:
6361 die("internal error in diff-resolve-rename-copy");
Junio C Hamano6973dca2006-04-22 06:57:456362 default:
Timo Hirvonenc6744342006-06-24 17:21:536363 return 1;
Junio C Hamano6973dca2006-04-22 06:57:456364 }
6365}
6366
Timo Hirvonenc6744342006-06-24 17:21:536367static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
6368{
6369 int fmt = opt->output_format;
6370
6371 if (fmt & DIFF_FORMAT_CHECKDIFF)
6372 diff_flush_checkdiff(p, opt);
6373 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
6374 diff_flush_raw(p, opt);
Junio C Hamanocd676a52008-02-12 22:26:026375 else if (fmt & DIFF_FORMAT_NAME) {
6376 const char *name_a, *name_b;
6377 name_a = p->two->path;
6378 name_b = NULL;
6379 strip_prefix(opt->prefix_length, &name_a, &name_b);
Jeff Kingf5022b52017-02-08 20:31:156380 fprintf(opt->file, "%s", diff_line_prefix(opt));
Daniel Barkalowc0c77732008-03-10 02:43:396381 write_name_quoted(name_a, opt->file, opt->line_termination);
Junio C Hamanocd676a52008-02-12 22:26:026382 }
Junio C Hamanoa64f8b22023-08-18 23:59:326383
6384 opt->found_changes = 1;
Timo Hirvonenc6744342006-06-24 17:21:536385}
6386
Stefan Beller146fdb02017-06-30 00:07:056387static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs)
Sean4bbd2612006-05-14 12:13:496388{
Stefan Beller146fdb02017-06-30 00:07:056389 struct strbuf sb = STRBUF_INIT;
Sean4bbd2612006-05-14 12:13:496390 if (fs->mode)
Stefan Beller146fdb02017-06-30 00:07:056391 strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode);
Sean4bbd2612006-05-14 12:13:496392 else
Stefan Beller146fdb02017-06-30 00:07:056393 strbuf_addf(&sb, " %s ", newdelete);
6394
6395 quote_c_style(fs->path, &sb, NULL, 0);
6396 strbuf_addch(&sb, '\n');
6397 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6398 sb.buf, sb.len, 0);
6399 strbuf_release(&sb);
Sean4bbd2612006-05-14 12:13:496400}
6401
Stefan Beller146fdb02017-06-30 00:07:056402static void show_mode_change(struct diff_options *opt, struct diff_filepair *p,
6403 int show_name)
Sean4bbd2612006-05-14 12:13:496404{
6405 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
Stefan Beller146fdb02017-06-30 00:07:056406 struct strbuf sb = STRBUF_INIT;
6407 strbuf_addf(&sb, " mode change %06o => %06o",
6408 p->one->mode, p->two->mode);
Alexandre Julliard0d26a642007-02-10 14:37:486409 if (show_name) {
Stefan Beller146fdb02017-06-30 00:07:056410 strbuf_addch(&sb, ' ');
6411 quote_c_style(p->two->path, &sb, NULL, 0);
Alexandre Julliard0d26a642007-02-10 14:37:486412 }
Stefan Beller58aaced2017-09-27 22:51:266413 strbuf_addch(&sb, '\n');
Stefan Beller146fdb02017-06-30 00:07:056414 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6415 sb.buf, sb.len, 0);
6416 strbuf_release(&sb);
Sean4bbd2612006-05-14 12:13:496417 }
6418}
6419
Stefan Beller146fdb02017-06-30 00:07:056420static void show_rename_copy(struct diff_options *opt, const char *renamecopy,
6421 struct diff_filepair *p)
Sean4bbd2612006-05-14 12:13:496422{
Stefan Beller146fdb02017-06-30 00:07:056423 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 13:02:206424 struct strbuf names = STRBUF_INIT;
6425
6426 pprint_rename(&names, p->one->path, p->two->path);
Stefan Beller146fdb02017-06-30 00:07:056427 strbuf_addf(&sb, " %s %s (%d%%)\n",
Nguyễn Thái Ngọc Duyc905cbc2018-02-01 13:02:206428 renamecopy, names.buf, similarity_index(p));
6429 strbuf_release(&names);
Stefan Beller146fdb02017-06-30 00:07:056430 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6431 sb.buf, sb.len, 0);
6432 show_mode_change(opt, p, 0);
Rene Scharfe348eda22017-08-30 17:49:426433 strbuf_release(&sb);
Sean4bbd2612006-05-14 12:13:496434}
6435
Bo Yang7be57612010-05-26 07:23:546436static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
Sean4bbd2612006-05-14 12:13:496437{
6438 switch(p->status) {
6439 case DIFF_STATUS_DELETED:
Stefan Beller146fdb02017-06-30 00:07:056440 show_file_mode_name(opt, "delete", p->one);
Sean4bbd2612006-05-14 12:13:496441 break;
6442 case DIFF_STATUS_ADDED:
Stefan Beller146fdb02017-06-30 00:07:056443 show_file_mode_name(opt, "create", p->two);
Sean4bbd2612006-05-14 12:13:496444 break;
6445 case DIFF_STATUS_COPIED:
Stefan Beller146fdb02017-06-30 00:07:056446 show_rename_copy(opt, "copy", p);
Sean4bbd2612006-05-14 12:13:496447 break;
6448 case DIFF_STATUS_RENAMED:
Stefan Beller146fdb02017-06-30 00:07:056449 show_rename_copy(opt, "rename", p);
Sean4bbd2612006-05-14 12:13:496450 break;
6451 default:
6452 if (p->score) {
Stefan Beller146fdb02017-06-30 00:07:056453 struct strbuf sb = STRBUF_INIT;
6454 strbuf_addstr(&sb, " rewrite ");
6455 quote_c_style(p->two->path, &sb, NULL, 0);
6456 strbuf_addf(&sb, " (%d%%)\n", similarity_index(p));
6457 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
6458 sb.buf, sb.len, 0);
Rene Scharfefa842d82017-08-30 17:49:416459 strbuf_release(&sb);
Pierre Habouzit663af342007-09-19 22:42:156460 }
Stefan Beller146fdb02017-06-30 00:07:056461 show_mode_change(opt, p, !p->score);
Sean4bbd2612006-05-14 12:13:496462 break;
6463 }
6464}
6465
Johannes Schindelinfcb3d0a2006-06-25 01:51:086466struct patch_id_t {
Patrick Steinhardt7346e342025-01-31 12:55:296467 struct git_hash_ctx *ctx;
Johannes Schindelinfcb3d0a2006-06-25 01:51:086468 int patchlen;
6469};
6470
6471static int remove_space(char *line, int len)
6472{
6473 int i;
Pierre Habouzit663af342007-09-19 22:42:156474 char *dst = line;
6475 unsigned char c;
Johannes Schindelinfcb3d0a2006-06-25 01:51:086476
Pierre Habouzit663af342007-09-19 22:42:156477 for (i = 0; i < len; i++)
6478 if (!isspace((c = line[i])))
6479 *dst++ = c;
Johannes Schindelinfcb3d0a2006-06-25 01:51:086480
Pierre Habouzit663af342007-09-19 22:42:156481 return dst - line;
Johannes Schindelinfcb3d0a2006-06-25 01:51:086482}
6483
Patrick Steinhardt7346e342025-01-31 12:55:296484void flush_one_hunk(struct object_id *result, struct git_hash_ctx *ctx)
Stephen Boyda8f68552019-04-26 23:51:576485{
6486 unsigned char hash[GIT_MAX_RAWSZ];
6487 unsigned short carry = 0;
6488 int i;
6489
Patrick Steinhardt0578f1e2025-01-31 12:55:316490 git_hash_final(hash, ctx);
brian m. carlson36261e42019-08-18 20:04:036491 the_hash_algo->init_fn(ctx);
Stephen Boyda8f68552019-04-26 23:51:576492 /* 20-byte sum, with carry */
brian m. carlson36261e42019-08-18 20:04:036493 for (i = 0; i < the_hash_algo->rawsz; ++i) {
Stephen Boyda8f68552019-04-26 23:51:576494 carry += result->hash[i] + hash[i];
6495 result->hash[i] = carry;
6496 carry >>= 8;
6497 }
6498}
6499
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 17:15:246500static int patch_id_consume(void *priv, char *line, unsigned long len)
Johannes Schindelinfcb3d0a2006-06-25 01:51:086501{
6502 struct patch_id_t *data = priv;
6503 int new_len;
6504
René Scharfe82a62012020-08-18 22:08:546505 if (len > 12 && starts_with(line, "\\ "))
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 17:15:246506 return 0;
Johannes Schindelinfcb3d0a2006-06-25 01:51:086507 new_len = remove_space(line, len);
6508
Patrick Steinhardt0578f1e2025-01-31 12:55:316509 git_hash_update(data->ctx, line, new_len);
Johannes Schindelinfcb3d0a2006-06-25 01:51:086510 data->patchlen += new_len;
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 17:15:246511 return 0;
Johannes Schindelinfcb3d0a2006-06-25 01:51:086512}
6513
Patrick Steinhardt7346e342025-01-31 12:55:296514static void patch_id_add_string(struct git_hash_ctx *ctx, const char *str)
Jeff King977db6b2017-03-30 18:26:056515{
Patrick Steinhardt0578f1e2025-01-31 12:55:316516 git_hash_update(ctx, str, strlen(str));
Jeff King977db6b2017-03-30 18:26:056517}
6518
Patrick Steinhardt7346e342025-01-31 12:55:296519static void patch_id_add_mode(struct git_hash_ctx *ctx, unsigned mode)
Jeff King977db6b2017-03-30 18:26:056520{
6521 /* large enough for 2^32 in octal */
6522 char buf[12];
6523 int len = xsnprintf(buf, sizeof(buf), "%06o", mode);
Patrick Steinhardt0578f1e2025-01-31 12:55:316524 git_hash_update(ctx, buf, len);
Jeff King977db6b2017-03-30 18:26:056525}
6526
Stephen Boyda8f68552019-04-26 23:51:576527/* returns 0 upon success, and writes result into oid */
Jerry Zhang51276c12022-10-24 20:07:406528static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only)
Johannes Schindelinfcb3d0a2006-06-25 01:51:086529{
6530 struct diff_queue_struct *q = &diff_queued_diff;
6531 int i;
Patrick Steinhardt7346e342025-01-31 12:55:296532 struct git_hash_ctx ctx;
Johannes Schindelinfcb3d0a2006-06-25 01:51:086533 struct patch_id_t data;
Johannes Schindelinfcb3d0a2006-06-25 01:51:086534
brian m. carlson36261e42019-08-18 20:04:036535 the_hash_algo->init_fn(&ctx);
Johannes Schindelinfcb3d0a2006-06-25 01:51:086536 memset(&data, 0, sizeof(struct patch_id_t));
6537 data.ctx = &ctx;
Patrick Steinhardt9da95bd2024-06-14 06:49:546538 oidclr(oid, the_repository->hash_algo);
Johannes Schindelinfcb3d0a2006-06-25 01:51:086539
6540 for (i = 0; i < q->nr; i++) {
6541 xpparam_t xpp;
6542 xdemitconf_t xecfg;
Johannes Schindelinfcb3d0a2006-06-25 01:51:086543 mmfile_t mf1, mf2;
6544 struct diff_filepair *p = q->queue[i];
6545 int len1, len2;
6546
Brian Downing9ccd0a82008-10-25 13:30:376547 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin30b25012007-07-04 18:05:466548 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelinfcb3d0a2006-06-25 01:51:086549 if (p->status == 0)
6550 return error("internal diff status error");
6551 if (p->status == DIFF_STATUS_UNKNOWN)
6552 continue;
6553 if (diff_unmodified_pair(p))
6554 continue;
6555 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
6556 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
6557 continue;
6558 if (DIFF_PAIR_UNMERGED(p))
6559 continue;
6560
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 15:57:316561 diff_fill_oid_info(p->one, options->repo->index);
6562 diff_fill_oid_info(p->two, options->repo->index);
Johannes Schindelinfcb3d0a2006-06-25 01:51:086563
Johannes Schindelinfcb3d0a2006-06-25 01:51:086564 len1 = remove_space(p->one->path, strlen(p->one->path));
6565 len2 = remove_space(p->two->path, strlen(p->two->path));
Jeff King977db6b2017-03-30 18:26:056566 patch_id_add_string(&ctx, "diff--git");
6567 patch_id_add_string(&ctx, "a/");
Patrick Steinhardt0578f1e2025-01-31 12:55:316568 git_hash_update(&ctx, p->one->path, len1);
Jeff King977db6b2017-03-30 18:26:056569 patch_id_add_string(&ctx, "b/");
Patrick Steinhardt0578f1e2025-01-31 12:55:316570 git_hash_update(&ctx, p->two->path, len2);
Jeff King977db6b2017-03-30 18:26:056571
6572 if (p->one->mode == 0) {
6573 patch_id_add_string(&ctx, "newfilemode");
6574 patch_id_add_mode(&ctx, p->two->mode);
Jeff King977db6b2017-03-30 18:26:056575 } else if (p->two->mode == 0) {
6576 patch_id_add_string(&ctx, "deletedfilemode");
6577 patch_id_add_mode(&ctx, p->one->mode);
Jerry Zhang93105ab2022-10-24 20:07:426578 } else if (p->one->mode != p->two->mode) {
6579 patch_id_add_string(&ctx, "oldmode");
6580 patch_id_add_mode(&ctx, p->one->mode);
6581 patch_id_add_string(&ctx, "newmode");
6582 patch_id_add_mode(&ctx, p->two->mode);
Jeff King977db6b2017-03-30 18:26:056583 }
Johannes Schindelinfcb3d0a2006-06-25 01:51:086584
Jerry Zhang0570be72022-10-24 20:07:396585 if (diff_header_only) {
6586 /* don't do anything since we're only populating header info */
6587 } else if (diff_filespec_is_binary(options->repo, p->one) ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:196588 diff_filespec_is_binary(options->repo, p->two)) {
Patrick Steinhardt0578f1e2025-01-31 12:55:316589 git_hash_update(&ctx, oid_to_hex(&p->one->oid),
brian m. carlson36261e42019-08-18 20:04:036590 the_hash_algo->hexsz);
Patrick Steinhardt0578f1e2025-01-31 12:55:316591 git_hash_update(&ctx, oid_to_hex(&p->two->oid),
brian m. carlson36261e42019-08-18 20:04:036592 the_hash_algo->hexsz);
Jerry Zhang0570be72022-10-24 20:07:396593 } else {
6594 if (p->one->mode == 0) {
6595 patch_id_add_string(&ctx, "---/dev/null");
6596 patch_id_add_string(&ctx, "+++b/");
Patrick Steinhardt0578f1e2025-01-31 12:55:316597 git_hash_update(&ctx, p->two->path, len2);
Jerry Zhang0570be72022-10-24 20:07:396598 } else if (p->two->mode == 0) {
6599 patch_id_add_string(&ctx, "---a/");
Patrick Steinhardt0578f1e2025-01-31 12:55:316600 git_hash_update(&ctx, p->one->path, len1);
Jerry Zhang0570be72022-10-24 20:07:396601 patch_id_add_string(&ctx, "+++/dev/null");
6602 } else {
6603 patch_id_add_string(&ctx, "---a/");
Patrick Steinhardt0578f1e2025-01-31 12:55:316604 git_hash_update(&ctx, p->one->path, len1);
Jerry Zhang0570be72022-10-24 20:07:396605 patch_id_add_string(&ctx, "+++b/");
Patrick Steinhardt0578f1e2025-01-31 12:55:316606 git_hash_update(&ctx, p->two->path, len2);
Jerry Zhang0570be72022-10-24 20:07:396607 }
6608
6609 if (fill_mmfile(options->repo, &mf1, p->one) < 0 ||
6610 fill_mmfile(options->repo, &mf2, p->two) < 0)
6611 return error("unable to read files to diff");
6612 xpp.flags = 0;
6613 xecfg.ctxlen = 3;
6614 xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
6615 if (xdi_diff_outf(&mf1, &mf2, NULL,
6616 patch_id_consume, &data, &xpp, &xecfg))
6617 return error("unable to generate patch-id diff for %s",
6618 p->one->path);
Clemens Buchacher34597c12010-08-15 07:20:436619 }
Jerry Zhang51276c12022-10-24 20:07:406620 flush_one_hunk(oid, &ctx);
Johannes Schindelinfcb3d0a2006-06-25 01:51:086621 }
6622
Johannes Schindelinfcb3d0a2006-06-25 01:51:086623 return 0;
6624}
6625
Jerry Zhang51276c12022-10-24 20:07:406626int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only)
Johannes Schindelinfcb3d0a2006-06-25 01:51:086627{
6628 struct diff_queue_struct *q = &diff_queued_diff;
Jerry Zhang51276c12022-10-24 20:07:406629 int result = diff_get_patch_id(options, oid, diff_header_only);
Johannes Schindelinfcb3d0a2006-06-25 01:51:086630
Patrick Steinhardta5aecb22024-09-30 09:13:456631 diff_queue_clear(q);
Johannes Schindelinfcb3d0a2006-06-25 01:51:086632
6633 return result;
6634}
6635
Timo Hirvonen946c3782006-06-27 12:09:176636static int is_summary_empty(const struct diff_queue_struct *q)
6637{
6638 int i;
6639
6640 for (i = 0; i < q->nr; i++) {
6641 const struct diff_filepair *p = q->queue[i];
6642
6643 switch (p->status) {
6644 case DIFF_STATUS_DELETED:
6645 case DIFF_STATUS_ADDED:
6646 case DIFF_STATUS_COPIED:
6647 case DIFF_STATUS_RENAMED:
6648 return 0;
6649 default:
6650 if (p->score)
6651 return 0;
6652 if (p->one->mode && p->two->mode &&
6653 p->one->mode != p->two->mode)
6654 return 0;
6655 break;
6656 }
6657 }
6658 return 1;
6659}
6660
Junio C Hamanof31027c2011-01-06 21:50:066661static const char rename_limit_warning[] =
Elijah Newren05d2c612021-07-15 00:45:216662N_("exhaustive rename detection was skipped due to too many files.");
Junio C Hamanof31027c2011-01-06 21:50:066663
6664static const char degrade_cc_to_c_warning[] =
Vasco Almeidadb424972016-10-17 13:15:296665N_("only found copies from modified paths due to too many files.");
Junio C Hamanof31027c2011-01-06 21:50:066666
6667static const char rename_limit_advice[] =
Vasco Almeidadb424972016-10-17 13:15:296668N_("you may want to set your %s variable to at least "
6669 "%d and retry the command.");
Junio C Hamanof31027c2011-01-06 21:50:066670
6671void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)
6672{
Nguyễn Thái Ngọc Duy4e056c92018-01-16 09:23:496673 fflush(stdout);
Junio C Hamanof31027c2011-01-06 21:50:066674 if (degraded_cc)
Vasco Almeidadb424972016-10-17 13:15:296675 warning(_(degrade_cc_to_c_warning));
Junio C Hamanof31027c2011-01-06 21:50:066676 else if (needed)
Vasco Almeidadb424972016-10-17 13:15:296677 warning(_(rename_limit_warning));
Junio C Hamanof31027c2011-01-06 21:50:066678 else
6679 return;
Elijah Newren9f7e4bf2017-11-13 20:15:596680 if (0 < needed)
Vasco Almeidadb424972016-10-17 13:15:296681 warning(_(rename_limit_advice), varname, needed);
Junio C Hamanof31027c2011-01-06 21:50:066682}
6683
Elijah Newren95433ee2022-02-02 02:37:346684static void create_filepairs_for_header_only_notifications(struct diff_options *o)
6685{
6686 struct strset present;
6687 struct diff_queue_struct *q = &diff_queued_diff;
6688 struct hashmap_iter iter;
6689 struct strmap_entry *e;
6690 int i;
6691
6692 strset_init_with_options(&present, /*pool*/ NULL, /*strdup*/ 0);
6693
6694 /*
6695 * Find out which paths exist in diff_queued_diff, preferring
6696 * one->path for any pair that has multiple paths.
6697 */
6698 for (i = 0; i < q->nr; i++) {
6699 struct diff_filepair *p = q->queue[i];
6700 char *path = p->one->path ? p->one->path : p->two->path;
6701
6702 if (strmap_contains(o->additional_path_headers, path))
6703 strset_add(&present, path);
6704 }
6705
6706 /*
6707 * Loop over paths in additional_path_headers; for each NOT already
6708 * in diff_queued_diff, create a synthetic filepair and insert that
6709 * into diff_queued_diff.
6710 */
6711 strmap_for_each_entry(o->additional_path_headers, &iter, e) {
6712 if (!strset_contains(&present, e->key)) {
6713 struct diff_filespec *one, *two;
6714 struct diff_filepair *p;
6715
6716 one = alloc_filespec(e->key);
6717 two = alloc_filespec(e->key);
Patrick Steinhardt7d70b292025-03-10 07:13:316718 fill_filespec(one, null_oid(the_hash_algo), 0, 0);
6719 fill_filespec(two, null_oid(the_hash_algo), 0, 0);
Elijah Newren95433ee2022-02-02 02:37:346720 p = diff_queue(q, one, two);
6721 p->status = DIFF_STATUS_MODIFIED;
6722 }
6723 }
6724
6725 /* Re-sort the filepairs */
6726 diffcore_fix_diff_index();
6727
6728 /* Cleanup */
6729 strset_clear(&present);
6730}
6731
Stefan Bellerec331502017-06-30 00:06:486732static void diff_flush_patch_all_file_pairs(struct diff_options *o)
6733{
6734 int i;
Stefan Bellere6e045f2017-06-30 00:07:066735 static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT;
Stefan Bellerec331502017-06-30 00:06:486736 struct diff_queue_struct *q = &diff_queued_diff;
Stefan Beller091f8e22017-06-30 00:06:536737
6738 if (WSEH_NEW & WS_RULE_MASK)
Johannes Schindelin033abf92018-05-02 09:38:396739 BUG("WS rules bit mask overlaps with diff symbol flags");
Stefan Beller091f8e22017-06-30 00:06:536740
Jeff King4cfc9712025-09-16 20:19:336741 if (o->color_moved && want_color(o->use_color))
Stefan Beller2e2d5ac2017-06-30 20:53:076742 o->emitted_symbols = &esm;
Stefan Bellere6e045f2017-06-30 00:07:066743
Elijah Newren95433ee2022-02-02 02:37:346744 if (o->additional_path_headers)
6745 create_filepairs_for_header_only_notifications(o);
6746
Stefan Bellerec331502017-06-30 00:06:486747 for (i = 0; i < q->nr; i++) {
6748 struct diff_filepair *p = q->queue[i];
6749 if (check_pair_status(p))
6750 diff_flush_patch(p, o);
6751 }
Stefan Bellere6e045f2017-06-30 00:07:066752
6753 if (o->emitted_symbols) {
Jeff King8efe6432025-09-16 20:17:196754 struct mem_pool entry_pool;
6755 struct moved_entry_list *entry_list;
Stefan Beller2e2d5ac2017-06-30 20:53:076756
Jeff King8efe6432025-09-16 20:17:196757 mem_pool_init(&entry_pool, 1024 * 1024);
6758 entry_list = add_lines_to_move_detection(o, &entry_pool);
6759 mark_color_as_moved(o, entry_list);
6760 if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
6761 dim_moved_lines(o);
Stefan Beller2e2d5ac2017-06-30 20:53:076762
Jeff King8efe6432025-09-16 20:17:196763 mem_pool_discard(&entry_pool, 0);
6764 free(entry_list);
Stefan Beller2e2d5ac2017-06-30 20:53:076765
Stefan Bellere6e045f2017-06-30 00:07:066766 for (i = 0; i < esm.nr; i++)
6767 emit_diff_symbol_from_struct(o, &esm.buf[i]);
6768
6769 for (i = 0; i < esm.nr; i++)
6770 free((void *)esm.buf[i].line);
Jeff King48edf3a2019-01-24 12:32:416771 esm.nr = 0;
6772
6773 o->emitted_symbols = NULL;
Stefan Bellere6e045f2017-06-30 00:07:066774 }
Stefan Bellerec331502017-06-30 00:06:486775}
6776
Ævar Arnfjörð Bjarmasone900d492021-02-11 10:45:346777static void diff_free_file(struct diff_options *options)
6778{
Patrick Steinhardta90a0892024-06-11 09:20:246779 if (options->close_file && options->file) {
Ævar Arnfjörð Bjarmasone900d492021-02-11 10:45:346780 fclose(options->file);
Patrick Steinhardta90a0892024-06-11 09:20:246781 options->file = NULL;
6782 }
Ævar Arnfjörð Bjarmasone900d492021-02-11 10:45:346783}
6784
Ævar Arnfjörð Bjarmasonc45dc9c2021-02-11 10:45:356785static void diff_free_ignore_regex(struct diff_options *options)
6786{
6787 int i;
6788
6789 for (i = 0; i < options->ignore_regex_nr; i++) {
6790 regfree(options->ignore_regex[i]);
6791 free(options->ignore_regex[i]);
6792 }
Patrick Steinhardta90a0892024-06-11 09:20:246793
6794 FREE_AND_NULL(options->ignore_regex);
6795 options->ignore_regex_nr = 0;
Ævar Arnfjörð Bjarmasonc45dc9c2021-02-11 10:45:356796}
6797
Ævar Arnfjörð Bjarmasone900d492021-02-11 10:45:346798void diff_free(struct diff_options *options)
6799{
6800 if (options->no_free)
6801 return;
6802
Patrick Steinhardt36f971f2024-08-14 06:52:556803 if (options->objfind) {
6804 oidset_clear(options->objfind);
6805 FREE_AND_NULL(options->objfind);
6806 }
6807
Patrick Steinhardt76c7e702024-09-26 11:46:296808 FREE_AND_NULL(options->orderfile);
Patrick Steinhardt36f971f2024-08-14 06:52:556809 for (size_t i = 0; i < options->anchors_nr; i++)
6810 free(options->anchors[i]);
6811 FREE_AND_NULL(options->anchors);
6812 options->anchors_nr = options->anchors_alloc = 0;
6813
Ævar Arnfjörð Bjarmasone900d492021-02-11 10:45:346814 diff_free_file(options);
Ævar Arnfjörð Bjarmasonc45dc9c2021-02-11 10:45:356815 diff_free_ignore_regex(options);
Ævar Arnfjörð Bjarmason244c2722022-02-16 10:56:286816 clear_pathspec(&options->pathspec);
Ævar Arnfjörð Bjarmasone900d492021-02-11 10:45:346817}
6818
Junio C Hamano6973dca2006-04-22 06:57:456819void diff_flush(struct diff_options *options)
6820{
6821 struct diff_queue_struct *q = &diff_queued_diff;
Timo Hirvonenc6744342006-06-24 17:21:536822 int i, output_format = options->output_format;
Timo Hirvonen946c3782006-06-27 12:09:176823 int separator = 0;
Johan Herland1c57a622011-04-29 09:36:216824 int dirstat_by_line = 0;
Junio C Hamano6973dca2006-04-22 06:57:456825
Timo Hirvonenc6744342006-06-24 17:21:536826 /*
6827 * Order: raw, stat, summary, patch
6828 * or: name/name-status/checkdiff (other bits clear)
6829 */
Elijah Newren95433ee2022-02-02 02:37:346830 if (!q->nr && !options->additional_path_headers)
Timo Hirvonen946c3782006-06-27 12:09:176831 goto free_queue;
Junio C Hamano6973dca2006-04-22 06:57:456832
Timo Hirvonenc6744342006-06-24 17:21:536833 if (output_format & (DIFF_FORMAT_RAW |
6834 DIFF_FORMAT_NAME |
6835 DIFF_FORMAT_NAME_STATUS |
6836 DIFF_FORMAT_CHECKDIFF)) {
Junio C Hamano6973dca2006-04-22 06:57:456837 for (i = 0; i < q->nr; i++) {
6838 struct diff_filepair *p = q->queue[i];
Lidong Yanb55e6d32025-08-08 03:30:196839
6840 if (!check_pair_status(p))
6841 continue;
6842
Jeff King1ad27602025-10-24 17:09:256843 if (options->flags.diff_from_contents &&
6844 !diff_flush_patch_quietly(p, options))
6845 continue;
Lidong Yanb55e6d32025-08-08 03:30:196846
6847 flush_one_pair(p, options);
Junio C Hamano6973dca2006-04-22 06:57:456848 }
Timo Hirvonen946c3782006-06-27 12:09:176849 separator++;
Junio C Hamano6973dca2006-04-22 06:57:456850 }
Timo Hirvonenc6744342006-06-24 17:21:536851
Brandon Williams0d1e0e72017-10-31 18:19:116852 if (output_format & DIFF_FORMAT_DIRSTAT && options->flags.dirstat_by_line)
Johan Herland1c57a622011-04-29 09:36:216853 dirstat_by_line = 1;
6854
6855 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT) ||
6856 dirstat_by_line) {
Timo Hirvonen5e2b0632006-06-25 11:28:196857 struct diffstat_t diffstat;
Timo Hirvonenc6744342006-06-24 17:21:536858
Daniel Ferreirae4cb6592019-11-13 12:40:586859 compute_diffstat(options, &diffstat, q);
Junio C Hamano74e2abe2006-10-12 10:01:006860 if (output_format & DIFF_FORMAT_NUMSTAT)
6861 show_numstat(&diffstat, options);
6862 if (output_format & DIFF_FORMAT_DIFFSTAT)
6863 show_stats(&diffstat, options);
Junio C Hamanof6046522007-12-12 07:46:306864 if (output_format & DIFF_FORMAT_SHORTSTAT)
Daniel Barkalowc0c77732008-03-10 02:43:396865 show_shortstats(&diffstat, options);
Mårten Kongstadab273892015-03-02 15:05:396866 if (output_format & DIFF_FORMAT_DIRSTAT && dirstat_by_line)
Johan Herland1c57a622011-04-29 09:36:216867 show_dirstat_by_line(&diffstat, options);
Junio C Hamanof6046522007-12-12 07:46:306868 free_diffstat_info(&diffstat);
Junio C Hamano3969cf72006-06-27 22:08:196869 separator++;
Junio C Hamano6973dca2006-04-22 06:57:456870 }
Johan Herland1c57a622011-04-29 09:36:216871 if ((output_format & DIFF_FORMAT_DIRSTAT) && !dirstat_by_line)
Junio C Hamanoc04a7152008-02-13 01:06:586872 show_dirstat(options);
Junio C Hamano6973dca2006-04-22 06:57:456873
Timo Hirvonen946c3782006-06-27 12:09:176874 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
Bo Yang7be57612010-05-26 07:23:546875 for (i = 0; i < q->nr; i++) {
6876 diff_summary(options, q->queue[i]);
6877 }
Junio C Hamano3969cf72006-06-27 22:08:196878 separator++;
Sean4bbd2612006-05-14 12:13:496879 }
6880
Junio C Hamano5f107ca2023-08-18 23:59:286881 if (output_format & DIFF_FORMAT_PATCH) {
6882 if (separator) {
6883 emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0);
6884 if (options->stat_sep)
6885 /* attach patch instead of inline */
6886 emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP,
6887 NULL, 0, 0);
6888 }
6889
6890 diff_flush_patch_all_file_pairs(options);
6891 }
6892
6893 if (output_format & DIFF_FORMAT_CALLBACK)
6894 options->format_callback(q, options, options->format_callback_data);
6895
Junio C Hamanoe37423f2024-05-16 17:08:356896 if (output_format & DIFF_FORMAT_NO_OUTPUT &&
Brandon Williams0d1e0e72017-10-31 18:19:116897 options->flags.exit_with_status &&
6898 options->flags.diff_from_contents) {
Larry D'Anna6977c252010-02-16 06:55:216899 for (i = 0; i < q->nr; i++) {
6900 struct diff_filepair *p = q->queue[i];
6901 if (check_pair_status(p))
Lidong Yanb55e6d32025-08-08 03:30:196902 diff_flush_patch_quietly(p, options);
Larry D'Anna6977c252010-02-16 06:55:216903 if (options->found_changes)
6904 break;
6905 }
6906 }
6907
Timo Hirvonen946c3782006-06-27 12:09:176908free_queue:
Patrick Steinhardta5aecb22024-09-30 09:13:456909 diff_queue_clear(q);
Ævar Arnfjörð Bjarmasone900d492021-02-11 10:45:346910 diff_free(options);
Junio C Hamanof2451942009-05-22 19:45:296911
6912 /*
Jim Meyering97bf2a02009-08-30 20:27:026913 * Report the content-level differences with HAS_CHANGES;
Junio C Hamanof2451942009-05-22 19:45:296914 * diff_addremove/diff_change does not set the bit when
6915 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
6916 */
Brandon Williams0d1e0e72017-10-31 18:19:116917 if (options->flags.diff_from_contents) {
Junio C Hamanof2451942009-05-22 19:45:296918 if (options->found_changes)
Brandon Williams0d1e0e72017-10-31 18:19:116919 options->flags.has_changes = 1;
Junio C Hamanof2451942009-05-22 19:45:296920 else
Brandon Williams0d1e0e72017-10-31 18:19:116921 options->flags.has_changes = 0;
Junio C Hamanof2451942009-05-22 19:45:296922 }
Junio C Hamano6973dca2006-04-22 06:57:456923}
6924
Junio C Hamano08578fa2013-07-17 22:09:346925static int match_filter(const struct diff_options *options, const struct diff_filepair *p)
6926{
6927 return (((p->status == DIFF_STATUS_MODIFIED) &&
6928 ((p->score &&
Junio C Hamano1ecc1cb2013-07-17 22:05:466929 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN, options)) ||
Junio C Hamano08578fa2013-07-17 22:09:346930 (!p->score &&
Junio C Hamano1ecc1cb2013-07-17 22:05:466931 filter_bit_tst(DIFF_STATUS_MODIFIED, options)))) ||
Junio C Hamano08578fa2013-07-17 22:09:346932 ((p->status != DIFF_STATUS_MODIFIED) &&
Junio C Hamano1ecc1cb2013-07-17 22:05:466933 filter_bit_tst(p->status, options)));
Junio C Hamano08578fa2013-07-17 22:09:346934}
6935
Junio C Hamano949226f2013-07-17 21:19:246936static void diffcore_apply_filter(struct diff_options *options)
Junio C Hamano6973dca2006-04-22 06:57:456937{
6938 int i;
6939 struct diff_queue_struct *q = &diff_queued_diff;
Patrick Steinhardta5aecb22024-09-30 09:13:456940 struct diff_queue_struct outq = DIFF_QUEUE_INIT;
Junio C Hamano6973dca2006-04-22 06:57:456941
Junio C Hamano1ecc1cb2013-07-17 22:05:466942 if (!options->filter)
Junio C Hamano6973dca2006-04-22 06:57:456943 return;
6944
Junio C Hamano1ecc1cb2013-07-17 22:05:466945 if (filter_bit_tst(DIFF_STATUS_FILTER_AON, options)) {
Junio C Hamano6973dca2006-04-22 06:57:456946 int found;
6947 for (i = found = 0; !found && i < q->nr; i++) {
Junio C Hamano08578fa2013-07-17 22:09:346948 if (match_filter(options, q->queue[i]))
Junio C Hamano6973dca2006-04-22 06:57:456949 found++;
6950 }
6951 if (found)
6952 return;
6953
6954 /* otherwise we will clear the whole queue
6955 * by copying the empty outq at the end of this
6956 * function, but first clear the current entries
6957 * in the queue.
6958 */
6959 for (i = 0; i < q->nr; i++)
6960 diff_free_filepair(q->queue[i]);
6961 }
6962 else {
6963 /* Only the matching ones */
6964 for (i = 0; i < q->nr; i++) {
6965 struct diff_filepair *p = q->queue[i];
Junio C Hamano08578fa2013-07-17 22:09:346966 if (match_filter(options, p))
Junio C Hamano6973dca2006-04-22 06:57:456967 diff_q(&outq, p);
6968 else
6969 diff_free_filepair(p);
6970 }
6971 }
6972 free(q->queue);
6973 *q = outq;
6974}
6975
Sven Verdoolaege57011152007-09-08 10:30:226976/* Check whether two filespecs with the same mode and size are identical */
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:196977static int diff_filespec_is_identical(struct repository *r,
6978 struct diff_filespec *one,
Sven Verdoolaege57011152007-09-08 10:30:226979 struct diff_filespec *two)
6980{
Junio C Hamano2b459b42008-03-02 08:07:596981 if (S_ISGITLINK(one->mode))
6982 return 0;
Jonathan Tan1c37e862020-04-07 22:11:416983 if (diff_populate_filespec(r, one, NULL))
Sven Verdoolaege57011152007-09-08 10:30:226984 return 0;
Jonathan Tan1c37e862020-04-07 22:11:416985 if (diff_populate_filespec(r, two, NULL))
Sven Verdoolaege57011152007-09-08 10:30:226986 return 0;
6987 return !memcmp(one->data, two->data, one->size);
6988}
6989
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:196990static int diff_filespec_check_stat_unmatch(struct repository *r,
6991 struct diff_filepair *p)
Nguyễn Thái Ngọc Duyfceb9072014-01-25 06:46:496992{
Jonathan Tan1c37e862020-04-07 22:11:416993 struct diff_populate_filespec_options dpf_options = {
6994 .check_size_only = 1,
Jonathan Tan95acf112020-04-07 22:11:436995 .missing_object_cb = diff_queued_diff_prefetch,
6996 .missing_object_data = r,
Jonathan Tan1c37e862020-04-07 22:11:416997 };
6998
Nguyễn Thái Ngọc Duyf34b2052014-01-25 06:46:506999 if (p->done_skip_stat_unmatch)
7000 return p->skip_stat_unmatch_result;
7001
7002 p->done_skip_stat_unmatch = 1;
7003 p->skip_stat_unmatch_result = 0;
Nguyễn Thái Ngọc Duyfceb9072014-01-25 06:46:497004 /*
7005 * 1. Entries that come from stat info dirtiness
7006 * always have both sides (iow, not create/delete),
7007 * one side of the object name is unknown, with
7008 * the same mode and size. Keep the ones that
7009 * do not match these criteria. They have real
7010 * differences.
7011 *
7012 * 2. At this point, the file is known to be modified,
7013 * with the same mode and size, and the object
7014 * name of one side is unknown. Need to inspect
7015 * the identical contents.
7016 */
7017 if (!DIFF_FILE_VALID(p->one) || /* (1) */
7018 !DIFF_FILE_VALID(p->two) ||
brian m. carlson41c95602016-06-24 23:09:247019 (p->one->oid_valid && p->two->oid_valid) ||
Nguyễn Thái Ngọc Duyfceb9072014-01-25 06:46:497020 (p->one->mode != p->two->mode) ||
Jonathan Tan1c37e862020-04-07 22:11:417021 diff_populate_filespec(r, p->one, &dpf_options) ||
7022 diff_populate_filespec(r, p->two, &dpf_options) ||
Nguyễn Thái Ngọc Duyfceb9072014-01-25 06:46:497023 (p->one->size != p->two->size) ||
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:197024 !diff_filespec_is_identical(r, p->one, p->two)) /* (2) */
Nguyễn Thái Ngọc Duyf34b2052014-01-25 06:46:507025 p->skip_stat_unmatch_result = 1;
7026 return p->skip_stat_unmatch_result;
Nguyễn Thái Ngọc Duyfceb9072014-01-25 06:46:497027}
7028
Junio C Hamanofb132272007-08-03 20:33:317029static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
7030{
7031 int i;
7032 struct diff_queue_struct *q = &diff_queued_diff;
Patrick Steinhardta5aecb22024-09-30 09:13:457033 struct diff_queue_struct outq = DIFF_QUEUE_INIT;
Junio C Hamanofb132272007-08-03 20:33:317034
7035 for (i = 0; i < q->nr; i++) {
7036 struct diff_filepair *p = q->queue[i];
7037
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:197038 if (diff_filespec_check_stat_unmatch(diffopt->repo, p))
Junio C Hamanofb132272007-08-03 20:33:317039 diff_q(&outq, p);
7040 else {
7041 /*
7042 * The caller can subtract 1 from skip_stat_unmatch
7043 * to determine how many paths were dirty only
7044 * due to stat info mismatch.
7045 */
Brandon Williams0d1e0e72017-10-31 18:19:117046 if (!diffopt->flags.no_index)
René Scharfe6d2d9e82007-08-14 22:41:007047 diffopt->skip_stat_unmatch++;
Junio C Hamanofb132272007-08-03 20:33:317048 diff_free_filepair(p);
7049 }
7050 }
7051 free(q->queue);
7052 *q = outq;
7053}
7054
Junio C Hamano730f7282009-09-20 07:03:397055static int diffnamecmp(const void *a_, const void *b_)
7056{
7057 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
7058 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
7059 const char *name_a, *name_b;
7060
7061 name_a = a->one ? a->one->path : a->two->path;
7062 name_b = b->one ? b->one->path : b->two->path;
7063 return strcmp(name_a, name_b);
7064}
7065
Jeff King784c0da2019-02-14 05:48:037066void diffcore_fix_diff_index(void)
Junio C Hamano730f7282009-09-20 07:03:397067{
7068 struct diff_queue_struct *q = &diff_queued_diff;
René Scharfe9ed0d8d2016-09-29 15:27:317069 QSORT(q->queue, q->nr, diffnamecmp);
Junio C Hamano730f7282009-09-20 07:03:397070}
7071
Jonathan Tan95acf112020-04-07 22:11:437072void diff_add_if_missing(struct repository *r,
7073 struct oid_array *to_fetch,
7074 const struct diff_filespec *filespec)
Jonathan Tan7fbbcb22019-04-05 17:09:347075{
7076 if (filespec && filespec->oid_valid &&
Jonathan Tana63694f2019-08-20 20:53:207077 !S_ISGITLINK(filespec->mode) &&
Patrick Steinhardte989dd92025-07-01 12:22:257078 odb_read_object_info_extended(r->objects, &filespec->oid, NULL,
7079 OBJECT_INFO_FOR_PREFETCH))
Jonathan Tan7fbbcb22019-04-05 17:09:347080 oid_array_append(to_fetch, &filespec->oid);
7081}
7082
Jonathan Tan95acf112020-04-07 22:11:437083void diff_queued_diff_prefetch(void *repository)
7084{
7085 struct repository *repo = repository;
7086 int i;
7087 struct diff_queue_struct *q = &diff_queued_diff;
7088 struct oid_array to_fetch = OID_ARRAY_INIT;
7089
7090 for (i = 0; i < q->nr; i++) {
7091 struct diff_filepair *p = q->queue[i];
7092 diff_add_if_missing(repo, &to_fetch, p->one);
7093 diff_add_if_missing(repo, &to_fetch, p->two);
7094 }
7095
7096 /*
7097 * NEEDSWORK: Consider deduplicating the OIDs sent.
7098 */
7099 promisor_remote_get_direct(repo, to_fetch.oid, to_fetch.nr);
7100
7101 oid_array_clear(&to_fetch);
7102}
7103
Dragan Simic4ca7a3f2023-09-23 04:01:147104void init_diffstat_widths(struct diff_options *options)
7105{
7106 options->stat_width = -1; /* use full terminal width */
7107 options->stat_name_width = -1; /* respect diff.statNameWidth config */
7108 options->stat_graph_width = -1; /* respect diff.statGraphWidth config */
7109}
7110
Junio C Hamano6973dca2006-04-22 06:57:457111void diffcore_std(struct diff_options *options)
7112{
Jonathan Tan95acf112020-04-07 22:11:437113 int output_formats_to_prefetch = DIFF_FORMAT_DIFFSTAT |
7114 DIFF_FORMAT_NUMSTAT |
7115 DIFF_FORMAT_PATCH |
7116 DIFF_FORMAT_SHORTSTAT |
7117 DIFF_FORMAT_DIRSTAT;
Jonathan Tan7fbbcb22019-04-05 17:09:347118
Jonathan Tan95acf112020-04-07 22:11:437119 /*
7120 * Check if the user requested a blob-data-requiring diff output and/or
7121 * break-rewrite detection (which requires blob data). If yes, prefetch
7122 * the diff pairs.
7123 *
7124 * If no prefetching occurs, diffcore_rename() will prefetch if it
7125 * decides that it needs inexact rename detection.
7126 */
Ævar Arnfjörð Bjarmasona5183d72023-03-28 13:58:537127 if (options->repo == the_repository && repo_has_promisor_remote(the_repository) &&
Jonathan Tan95acf112020-04-07 22:11:437128 (options->output_format & output_formats_to_prefetch ||
7129 options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
7130 diff_queued_diff_prefetch(options->repo);
Jonathan Tan7fbbcb22019-04-05 17:09:347131
Kirill Smelkov7195fbf2014-02-24 16:21:517132 /* NOTE please keep the following in sync with diff_tree_combined() */
Junio C Hamano9d865352008-09-07 02:09:167133 if (options->skip_stat_unmatch)
Junio C Hamanofb132272007-08-03 20:33:317134 diffcore_skip_stat_unmatch(options);
Junio C Hamano44c48a92010-08-13 19:17:457135 if (!options->found_follow) {
7136 /* See try_to_follow_renames() in tree-diff.c */
7137 if (options->break_opt != -1)
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:197138 diffcore_break(options->repo,
7139 options->break_opt);
Junio C Hamano44c48a92010-08-13 19:17:457140 if (options->detect_rename)
7141 diffcore_rename(options);
7142 if (options->break_opt != -1)
7143 diffcore_merge_broken();
7144 }
Stefan Bellercf630512018-01-04 22:50:417145 if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
Junio C Hamano382f0132010-08-31 20:44:397146 diffcore_pickaxe(options);
Junio C Hamano6973dca2006-04-22 06:57:457147 if (options->orderfile)
7148 diffcore_order(options->orderfile);
Junio C Hamano1eb41362021-02-11 19:57:507149 if (options->rotate_to)
7150 diffcore_rotate(options);
Justin Toblerc8a8e042025-02-28 21:33:447151 if (!options->found_follow && !options->skip_resolving_statuses)
Junio C Hamano44c48a92010-08-13 19:17:457152 /* See try_to_follow_renames() in tree-diff.c */
7153 diff_resolve_rename_copy();
Junio C Hamano949226f2013-07-17 21:19:247154 diffcore_apply_filter(options);
Junio C Hamano68aacb22007-03-14 18:12:137155
Brandon Williams0d1e0e72017-10-31 18:19:117156 if (diff_queued_diff.nr && !options->flags.diff_from_contents)
7157 options->flags.has_changes = 1;
Pierre Habouzit8f67f8a2007-11-10 19:05:147158 else
Brandon Williams0d1e0e72017-10-31 18:19:117159 options->flags.has_changes = 0;
Bo Yang1da61752010-05-07 04:52:287160
Junio C Hamano44c48a92010-08-13 19:17:457161 options->found_follow = 0;
Junio C Hamano6973dca2006-04-22 06:57:457162}
7163
Junio C Hamano4460e052024-08-09 22:31:357164int diff_result_code(struct rev_info *revs)
Junio C Hamanoda31b352007-12-14 07:40:277165{
Junio C Hamano4460e052024-08-09 22:31:357166 struct diff_options *opt = &revs->diffopt;
Junio C Hamanoda31b352007-12-14 07:40:277167 int result = 0;
Junio C Hamanof31027c2011-01-06 21:50:067168
Junio C Hamano4460e052024-08-09 22:31:357169 if (revs->remerge_diff) {
7170 tmp_objdir_destroy(revs->remerge_objdir);
7171 revs->remerge_objdir = NULL;
7172 }
7173
Max Nanasyc9fc4412013-03-21 19:53:387174 diff_warn_rename_limit("diff.renameLimit",
Junio C Hamanof31027c2011-01-06 21:50:067175 opt->needed_rename_limit,
7176 opt->degraded_cc_to_c);
Jeff King5cc6b2d2023-08-21 20:20:467177
Brandon Williams0d1e0e72017-10-31 18:19:117178 if (opt->flags.exit_with_status &&
7179 opt->flags.has_changes)
Junio C Hamanoda31b352007-12-14 07:40:277180 result |= 01;
7181 if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) &&
Brandon Williams0d1e0e72017-10-31 18:19:117182 opt->flags.check_failed)
Junio C Hamanoda31b352007-12-14 07:40:277183 result |= 02;
7184 return result;
7185}
Junio C Hamano6973dca2006-04-22 06:57:457186
Junio C Hamano28b92642011-05-31 16:14:177187int diff_can_quit_early(struct diff_options *opt)
7188{
Brandon Williams0d1e0e72017-10-31 18:19:117189 return (opt->flags.quick &&
Junio C Hamano28b92642011-05-31 16:14:177190 !opt->filter &&
Brandon Williams0d1e0e72017-10-31 18:19:117191 opt->flags.has_changes);
Junio C Hamano28b92642011-05-31 16:14:177192}
7193
Jens Lehmannaee9c7d2010-08-05 22:39:257194/*
7195 * Shall changes to this submodule be ignored?
7196 *
7197 * Submodule changes can be configured to be ignored separately for each path,
7198 * but that configuration can be overridden from the command line.
7199 */
7200static int is_submodule_ignored(const char *path, struct diff_options *options)
7201{
7202 int ignored = 0;
Brandon Williams02f2f562017-10-31 18:19:057203 struct diff_flags orig_flags = options->flags;
Brandon Williams0d1e0e72017-10-31 18:19:117204 if (!options->flags.override_submodule_config)
Jens Lehmannaee9c7d2010-08-05 22:39:257205 set_diffopt_flags_from_submodule_config(options, path);
Brandon Williams0d1e0e72017-10-31 18:19:117206 if (options->flags.ignore_submodules)
Jens Lehmannaee9c7d2010-08-05 22:39:257207 ignored = 1;
7208 options->flags = orig_flags;
7209 return ignored;
7210}
7211
Daniel Ferreirae4cb6592019-11-13 12:40:587212void compute_diffstat(struct diff_options *options,
7213 struct diffstat_t *diffstat,
7214 struct diff_queue_struct *q)
7215{
7216 int i;
7217
7218 memset(diffstat, 0, sizeof(struct diffstat_t));
7219 for (i = 0; i < q->nr; i++) {
7220 struct diff_filepair *p = q->queue[i];
7221 if (check_pair_status(p))
7222 diff_flush_stat(p, options, diffstat);
7223 }
Junio C Hamanoe8efd862023-08-18 23:59:307224 options->found_changes = !!diffstat->nr;
Daniel Ferreirae4cb6592019-11-13 12:40:587225}
7226
Justin Tobler7c67d2a2025-02-28 21:33:437227struct diff_filepair *diff_queue_addremove(struct diff_queue_struct *queue,
7228 struct diff_options *options,
7229 int addremove, unsigned mode,
7230 const struct object_id *oid,
7231 int oid_valid,
7232 const char *concatpath,
7233 unsigned dirty_submodule)
Junio C Hamano6973dca2006-04-22 06:57:457234{
Junio C Hamano6973dca2006-04-22 06:57:457235 struct diff_filespec *one, *two;
Justin Tobler7c67d2a2025-02-28 21:33:437236 struct diff_filepair *pair;
Junio C Hamano6973dca2006-04-22 06:57:457237
Jens Lehmannaee9c7d2010-08-05 22:39:257238 if (S_ISGITLINK(mode) && is_submodule_ignored(concatpath, options))
Justin Tobler7c67d2a2025-02-28 21:33:437239 return NULL;
Johannes Schindelin50fd9bd2008-05-14 17:03:317240
Junio C Hamano6973dca2006-04-22 06:57:457241 /* This may look odd, but it is a preparation for
7242 * feeding "there are unchanged files which should
7243 * not produce diffs, but when you are doing copy
7244 * detection you would need them, so here they are"
7245 * entries to the diff-core. They will be prefixed
7246 * with something like '=' or '*' (I haven't decided
7247 * which but should not make any difference).
Junio C Hamanoa6080a02007-06-07 07:04:017248 * Feeding the same new and old to diff_change()
Junio C Hamano6973dca2006-04-22 06:57:457249 * also has the same effect.
7250 * Before the final output happens, they are pruned after
7251 * merged into rename/copy pairs as appropriate.
7252 */
Brandon Williams0d1e0e72017-10-31 18:19:117253 if (options->flags.reverse_diff)
Junio C Hamano6973dca2006-04-22 06:57:457254 addremove = (addremove == '+' ? '-' :
7255 addremove == '-' ? '+' : addremove);
7256
Junio C Hamanocd676a52008-02-12 22:26:027257 if (options->prefix &&
7258 strncmp(concatpath, options->prefix, options->prefix_length))
Justin Tobler7c67d2a2025-02-28 21:33:437259 return NULL;
Junio C Hamanocd676a52008-02-12 22:26:027260
Junio C Hamano6973dca2006-04-22 06:57:457261 one = alloc_filespec(concatpath);
7262 two = alloc_filespec(concatpath);
7263
7264 if (addremove != '+')
Brandon Williamsf9704c22017-05-30 17:30:507265 fill_filespec(one, oid, oid_valid, mode);
Jens Lehmanne3d42c42010-01-18 20:26:187266 if (addremove != '-') {
Brandon Williamsf9704c22017-05-30 17:30:507267 fill_filespec(two, oid, oid_valid, mode);
Jens Lehmanne3d42c42010-01-18 20:26:187268 two->dirty_submodule = dirty_submodule;
7269 }
Junio C Hamano6973dca2006-04-22 06:57:457270
Justin Tobler7c67d2a2025-02-28 21:33:437271 pair = diff_queue(queue, one, two);
Brandon Williams0d1e0e72017-10-31 18:19:117272 if (!options->flags.diff_from_contents)
7273 options->flags.has_changes = 1;
Justin Tobler7c67d2a2025-02-28 21:33:437274
7275 return pair;
Junio C Hamano6973dca2006-04-22 06:57:457276}
7277
Justin Tobler7c67d2a2025-02-28 21:33:437278struct diff_filepair *diff_queue_change(struct diff_queue_struct *queue,
7279 struct diff_options *options,
7280 unsigned old_mode, unsigned new_mode,
7281 const struct object_id *old_oid,
7282 const struct object_id *new_oid,
7283 int old_oid_valid, int new_oid_valid,
7284 const char *concatpath,
7285 unsigned old_dirty_submodule,
7286 unsigned new_dirty_submodule)
Junio C Hamano6973dca2006-04-22 06:57:457287{
Junio C Hamano6973dca2006-04-22 06:57:457288 struct diff_filespec *one, *two;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 06:46:507289 struct diff_filepair *p;
Junio C Hamano6973dca2006-04-22 06:57:457290
Jens Lehmannaee9c7d2010-08-05 22:39:257291 if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) &&
7292 is_submodule_ignored(concatpath, options))
Justin Tobler7c67d2a2025-02-28 21:33:437293 return NULL;
Johannes Schindelin50fd9bd2008-05-14 17:03:317294
Brandon Williams0d1e0e72017-10-31 18:19:117295 if (options->flags.reverse_diff) {
René Scharfe35d803b2017-01-28 21:40:587296 SWAP(old_mode, new_mode);
Brandon Williams94a00972017-05-30 17:30:497297 SWAP(old_oid, new_oid);
7298 SWAP(old_oid_valid, new_oid_valid);
René Scharfe35d803b2017-01-28 21:40:587299 SWAP(old_dirty_submodule, new_dirty_submodule);
Junio C Hamano6973dca2006-04-22 06:57:457300 }
Junio C Hamanocd676a52008-02-12 22:26:027301
7302 if (options->prefix &&
7303 strncmp(concatpath, options->prefix, options->prefix_length))
Justin Tobler7c67d2a2025-02-28 21:33:437304 return NULL;
Junio C Hamanocd676a52008-02-12 22:26:027305
Junio C Hamano6973dca2006-04-22 06:57:457306 one = alloc_filespec(concatpath);
7307 two = alloc_filespec(concatpath);
Brandon Williamsf9704c22017-05-30 17:30:507308 fill_filespec(one, old_oid, old_oid_valid, old_mode);
7309 fill_filespec(two, new_oid, new_oid_valid, new_mode);
Jens Lehmanne3d42c42010-01-18 20:26:187310 one->dirty_submodule = old_dirty_submodule;
7311 two->dirty_submodule = new_dirty_submodule;
Justin Tobler7c67d2a2025-02-28 21:33:437312 p = diff_queue(queue, one, two);
Junio C Hamano6973dca2006-04-22 06:57:457313
Brandon Williams0d1e0e72017-10-31 18:19:117314 if (options->flags.diff_from_contents)
Justin Tobler7c67d2a2025-02-28 21:33:437315 return p;
Nguyễn Thái Ngọc Duyf34b2052014-01-25 06:46:507316
Brandon Williams0d1e0e72017-10-31 18:19:117317 if (options->flags.quick && options->skip_stat_unmatch &&
Jeff Kingd2d7fbe2020-06-01 20:22:187318 !diff_filespec_check_stat_unmatch(options->repo, p)) {
7319 diff_free_filespec_data(p->one);
7320 diff_free_filespec_data(p->two);
Justin Tobler7c67d2a2025-02-28 21:33:437321 return p;
Jeff Kingd2d7fbe2020-06-01 20:22:187322 }
Nguyễn Thái Ngọc Duyf34b2052014-01-25 06:46:507323
Brandon Williams0d1e0e72017-10-31 18:19:117324 options->flags.has_changes = 1;
Justin Tobler7c67d2a2025-02-28 21:33:437325
7326 return p;
7327}
7328
7329void diff_addremove(struct diff_options *options, int addremove, unsigned mode,
7330 const struct object_id *oid, int oid_valid,
7331 const char *concatpath, unsigned dirty_submodule)
7332{
7333 diff_queue_addremove(&diff_queued_diff, options, addremove, mode, oid,
7334 oid_valid, concatpath, dirty_submodule);
7335}
7336
7337void diff_change(struct diff_options *options,
7338 unsigned old_mode, unsigned new_mode,
7339 const struct object_id *old_oid,
7340 const struct object_id *new_oid,
7341 int old_oid_valid, int new_oid_valid,
7342 const char *concatpath,
7343 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
7344{
7345 diff_queue_change(&diff_queued_diff, options, old_mode, new_mode,
7346 old_oid, new_oid, old_oid_valid, new_oid_valid,
7347 concatpath, old_dirty_submodule, new_dirty_submodule);
Junio C Hamano6973dca2006-04-22 06:57:457348}
7349
Junio C Hamanofa7b2902011-04-22 23:05:587350struct diff_filepair *diff_unmerge(struct diff_options *options, const char *path)
Junio C Hamano6973dca2006-04-22 06:57:457351{
Junio C Hamano76399c02011-04-22 22:55:557352 struct diff_filepair *pair;
Junio C Hamano6973dca2006-04-22 06:57:457353 struct diff_filespec *one, *two;
Junio C Hamanocd676a52008-02-12 22:26:027354
7355 if (options->prefix &&
7356 strncmp(path, options->prefix, options->prefix_length))
Junio C Hamano76399c02011-04-22 22:55:557357 return NULL;
Junio C Hamanocd676a52008-02-12 22:26:027358
Junio C Hamano6973dca2006-04-22 06:57:457359 one = alloc_filespec(path);
7360 two = alloc_filespec(path);
Junio C Hamano76399c02011-04-22 22:55:557361 pair = diff_queue(&diff_queued_diff, one, two);
7362 pair->is_unmerged = 1;
7363 return pair;
Junio C Hamano6973dca2006-04-22 06:57:457364}
Jeff King9cb92c32008-10-05 21:43:457365
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:197366static char *run_textconv(struct repository *r,
7367 const char *pgm,
7368 struct diff_filespec *spec,
7369 size_t *outsize)
Jeff King9cb92c32008-10-05 21:43:457370{
Jeff King479b0ae2009-01-22 05:59:567371 struct diff_tempfile *temp;
René Scharfed3180272014-08-19 19:09:357372 struct child_process child = CHILD_PROCESS_INIT;
Jeff King9cb92c32008-10-05 21:43:457373 struct strbuf buf = STRBUF_INIT;
Johannes Sixtda1fbed2010-03-30 17:36:037374 int err = 0;
Jeff King9cb92c32008-10-05 21:43:457375
Jeff Kingf034bb12023-01-06 11:05:007376 temp = prepare_temp_file(r, spec);
Ævar Arnfjörð Bjarmason7f146092021-11-25 22:52:217377 strvec_push(&child.args, pgm);
7378 strvec_push(&child.args, temp->name);
Jeff King9cb92c32008-10-05 21:43:457379
Jeff King41a457e2009-12-30 11:01:097380 child.use_shell = 1;
Jeff King9cb92c32008-10-05 21:43:457381 child.out = -1;
Johannes Sixtda1fbed2010-03-30 17:36:037382 if (start_command(&child)) {
Jeff King479b0ae2009-01-22 05:59:567383 remove_tempfile();
Jeff King9cb92c32008-10-05 21:43:457384 return NULL;
7385 }
Johannes Sixtda1fbed2010-03-30 17:36:037386
7387 if (strbuf_read(&buf, child.out, 0) < 0)
7388 err = error("error reading from textconv command '%s'", pgm);
Jeff King70d70992009-12-30 09:02:537389 close(child.out);
Johannes Sixtda1fbed2010-03-30 17:36:037390
7391 if (finish_command(&child) || err) {
7392 strbuf_release(&buf);
7393 remove_tempfile();
7394 return NULL;
7395 }
Jeff King479b0ae2009-01-22 05:59:567396 remove_tempfile();
Jeff King9cb92c32008-10-05 21:43:457397
7398 return strbuf_detach(&buf, outsize);
7399}
Jeff King840383b2010-04-02 00:09:267400
Nguyễn Thái Ngọc Duy6afaf802018-09-21 15:57:227401size_t fill_textconv(struct repository *r,
7402 struct userdiff_driver *driver,
Axel Bonneta788d7d2010-06-07 15:23:367403 struct diff_filespec *df,
7404 char **outbuf)
Jeff King840383b2010-04-02 00:09:267405{
7406 size_t size;
7407
Jeff Kinga64e6a42016-02-22 18:28:547408 if (!driver) {
Jeff King840383b2010-04-02 00:09:267409 if (!DIFF_FILE_VALID(df)) {
Patrick Steinhardt86badd42024-06-07 06:38:067410 *outbuf = (char *) "";
Jeff King840383b2010-04-02 00:09:267411 return 0;
7412 }
Jonathan Tan1c37e862020-04-07 22:11:417413 if (diff_populate_filespec(r, df, NULL))
Jeff King840383b2010-04-02 00:09:267414 die("unable to read files to diff");
7415 *outbuf = df->data;
7416 return df->size;
7417 }
7418
Jeff Kinga64e6a42016-02-22 18:28:547419 if (!driver->textconv)
Johannes Schindelin033abf92018-05-02 09:38:397420 BUG("fill_textconv called with non-textconv driver");
Jeff Kinga64e6a42016-02-22 18:28:547421
brian m. carlson41c95602016-06-24 23:09:247422 if (driver->textconv_cache && df->oid_valid) {
brian m. carlsona0d12c42016-06-24 23:09:237423 *outbuf = notes_cache_get(driver->textconv_cache,
brian m. carlson569aa372017-05-06 22:09:587424 &df->oid,
Jeff Kingd9bae1a2010-04-02 00:12:157425 &size);
7426 if (*outbuf)
7427 return size;
7428 }
7429
Nguyễn Thái Ngọc Duyb78ea5f2018-09-21 15:57:197430 *outbuf = run_textconv(r, driver->textconv, df, &size);
Jeff King840383b2010-04-02 00:09:267431 if (!*outbuf)
7432 die("unable to read files to diff");
Jeff Kingd9bae1a2010-04-02 00:12:157433
brian m. carlson41c95602016-06-24 23:09:247434 if (driver->textconv_cache && df->oid_valid) {
Jeff Kingd9bae1a2010-04-02 00:12:157435 /* ignore errors, as we might be in a readonly repository */
brian m. carlson569aa372017-05-06 22:09:587436 notes_cache_put(driver->textconv_cache, &df->oid, *outbuf,
Jeff Kingd9bae1a2010-04-02 00:12:157437 size);
7438 /*
7439 * we could save up changes and flush them all at the end,
7440 * but we would need an extra call after all diffing is done.
7441 * Since generating a cache entry is the slow path anyway,
7442 * this extra overhead probably isn't a big deal.
7443 */
7444 notes_cache_write(driver->textconv_cache);
7445 }
7446
Jeff King840383b2010-04-02 00:09:267447 return size;
7448}
Nguyễn Thái Ngọc Duy4914c962012-10-26 15:53:527449
Nguyễn Thái Ngọc Duy6afaf802018-09-21 15:57:227450int textconv_object(struct repository *r,
7451 const char *path,
Jeff Smith3a35cb22017-05-24 05:15:107452 unsigned mode,
7453 const struct object_id *oid,
7454 int oid_valid,
7455 char **buf,
7456 unsigned long *buf_size)
7457{
7458 struct diff_filespec *df;
7459 struct userdiff_driver *textconv;
7460
7461 df = alloc_filespec(path);
Junio C Hamanoa6f38c12017-06-19 19:38:447462 fill_filespec(df, oid, oid_valid, mode);
Nguyễn Thái Ngọc Duybd7ad452018-11-10 05:49:067463 textconv = get_textconv(r, df);
Jeff Smith3a35cb22017-05-24 05:15:107464 if (!textconv) {
7465 free_filespec(df);
7466 return 0;
7467 }
7468
Nguyễn Thái Ngọc Duy6afaf802018-09-21 15:57:227469 *buf_size = fill_textconv(r, textconv, df, buf);
Jeff Smith3a35cb22017-05-24 05:15:107470 free_filespec(df);
7471 return 1;
7472}
7473
Nguyễn Thái Ngọc Duy4914c962012-10-26 15:53:527474void setup_diff_pager(struct diff_options *opt)
7475{
7476 /*
7477 * If the user asked for our exit code, then either they want --quiet
7478 * or --exit-code. We should definitely not bother with a pager in the
7479 * former case, as we will generate no output. Since we still properly
7480 * report our exit code even when a pager is run, we _could_ run a
7481 * pager with --exit-code. But since we have not done so historically,
7482 * and because it is easy to find people oneline advising "git diff
7483 * --exit-code" in hooks and other scripts, we do not do so.
7484 */
Brandon Williams0d1e0e72017-10-31 18:19:117485 if (!opt->flags.exit_with_status &&
Patrick Steinhardt59b61312024-12-17 06:43:497486 check_pager_config(the_repository, "diff") != 0)
7487 setup_pager(the_repository);
Nguyễn Thái Ngọc Duy4914c962012-10-26 15:53:527488}