Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Junio C Hamano |
| 3 | */ |
Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 4 | #include "git-compat-util.h" |
Elijah Newren | 0b027f6 | 2023-03-21 06:25:58 +0000 | [diff] [blame] | 5 | #include "abspath.h" |
Elijah Newren | 9b5041f | 2023-04-22 20:17:13 +0000 | [diff] [blame] | 6 | #include "base85.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 7 | #include "config.h" |
Elijah Newren | 73359a9 | 2023-04-11 03:00:40 +0000 | [diff] [blame] | 8 | #include "convert.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 9 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 10 | #include "gettext.h" |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 11 | #include "tempfile.h" |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 12 | #include "quote.h" |
| 13 | #include "diff.h" |
| 14 | #include "diffcore.h" |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 15 | #include "delta.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 16 | #include "hex.h" |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 17 | #include "xdiff-interface.h" |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 18 | #include "color.h" |
Johannes Sixt | d5535ec | 2007-10-19 21:47:56 +0200 | [diff] [blame] | 19 | #include "run-command.h" |
Junio C Hamano | 2370781 | 2008-01-02 01:50:11 -0800 | [diff] [blame] | 20 | #include "utf8.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 21 | #include "object-store-ll.h" |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 22 | #include "userdiff.h" |
Johannes Schindelin | 752c0c2 | 2009-10-19 14:38:32 +0200 | [diff] [blame] | 23 | #include "submodule.h" |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 24 | #include "hashmap.h" |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 25 | #include "mem-pool.h" |
Elijah Newren | 6723899 | 2023-05-16 06:34:04 +0000 | [diff] [blame] | 26 | #include "merge-ll.h" |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 27 | #include "string-list.h" |
Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 28 | #include "strvec.h" |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 29 | #include "graph.h" |
Elijah Newren | 6f2d743 | 2023-04-11 03:00:42 +0000 | [diff] [blame] | 30 | #include "oid-array.h" |
Jonathan Tan | 150e300 | 2017-08-18 15:20:36 -0700 | [diff] [blame] | 31 | #include "packfile.h" |
Elijah Newren | ca4eed7 | 2023-04-11 00:41:59 -0700 | [diff] [blame] | 32 | #include "pager.h" |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 33 | #include "parse-options.h" |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 34 | #include "help.h" |
Christian Couder | b14ed5a | 2019-06-25 15:40:31 +0200 | [diff] [blame] | 35 | #include "promisor-remote.h" |
Derrick Stolee | ad90da7 | 2021-09-08 11:23:56 +0000 | [diff] [blame] | 36 | #include "dir.h" |
Elijah Newren | 87bed17 | 2023-04-11 00:41:53 -0700 | [diff] [blame] | 37 | #include "object-file.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 38 | #include "object-name.h" |
Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 +0000 | [diff] [blame] | 39 | #include "read-cache-ll.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 40 | #include "setup.h" |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 41 | #include "strmap.h" |
Elijah Newren | 6412231 | 2023-04-22 20:17:16 +0000 | [diff] [blame] | 42 | #include "ws.h" |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 43 | |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 44 | #ifdef NO_FAST_WORKING_DIRECTORY |
| 45 | #define FAST_WORKING_DIRECTORY 0 |
| 46 | #else |
| 47 | #define FAST_WORKING_DIRECTORY 1 |
| 48 | #endif |
| 49 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 50 | static int diff_detect_rename_default; |
Stefan Beller | 33de716 | 2017-05-08 12:03:38 -0400 | [diff] [blame] | 51 | static int diff_indent_heuristic = 1; |
Elijah Newren | 94b82d5 | 2021-07-15 00:45:24 +0000 | [diff] [blame] | 52 | static int diff_rename_limit_default = 1000; |
Jim Meyering | a624eaa | 2008-08-15 13:39:26 +0200 | [diff] [blame] | 53 | static int diff_suppress_blank_empty; |
Junio C Hamano | d2aea13 | 2012-09-15 13:59:59 -0700 | [diff] [blame] | 54 | static int diff_use_color_default = -1; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 55 | static int diff_color_moved_default; |
Stefan Beller | 626c0b5 | 2018-07-18 12:31:56 -0700 | [diff] [blame] | 56 | static int diff_color_moved_ws_default; |
Jeff Muizelaar | 6468a4e | 2012-09-27 15:12:52 -0400 | [diff] [blame] | 57 | static int diff_context_default = 3; |
Vegard Nossum | c488867 | 2017-01-12 13:21:11 +0100 | [diff] [blame] | 58 | static int diff_interhunk_context_default; |
Boyd Stephen Smith Jr | 98a4d87 | 2009-01-20 21:46:57 -0600 | [diff] [blame] | 59 | static const char *diff_word_regex_cfg; |
Johannes Schindelin | cbe0210 | 2007-12-17 13:42:20 +0000 | [diff] [blame] | 60 | static const char *external_diff_cmd_cfg; |
Samuel Bronson | 6d8940b | 2013-12-18 19:08:12 -0500 | [diff] [blame] | 61 | static const char *diff_order_file_cfg; |
Junio C Hamano | aecbf91 | 2007-08-31 13:13:42 -0700 | [diff] [blame] | 62 | int diff_auto_refresh_index = 1; |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 63 | static int diff_mnemonic_prefix; |
Eli Collins | f89504d | 2010-05-02 19:03:41 -0700 | [diff] [blame] | 64 | static int diff_no_prefix; |
Peter Hutterer | 7fdc265 | 2024-03-15 11:03:10 +1000 | [diff] [blame] | 65 | static const char *diff_src_prefix = "a/"; |
| 66 | static const char *diff_dst_prefix = "b/"; |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 67 | static int diff_relative; |
Dragan Simic | bd48adc | 2023-09-11 17:39:44 +0200 | [diff] [blame] | 68 | static int diff_stat_name_width; |
Zbigniew Jędrzejewski-Szmek | df44483 | 2012-03-01 13:26:46 +0100 | [diff] [blame] | 69 | static int diff_stat_graph_width; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 70 | static int diff_dirstat_permille_default = 30; |
Johannes Schindelin | be4f2b4 | 2010-08-05 10:49:55 +0200 | [diff] [blame] | 71 | static struct diff_options default_diff_options; |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 72 | static long diff_algorithm; |
Junio C Hamano | a17505f | 2016-10-04 15:26:27 -0700 | [diff] [blame] | 73 | static unsigned ws_error_highlight_default = WSEH_NEW; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 74 | |
Jeff King | 7c92fe0 | 2006-09-08 04:03:18 -0400 | [diff] [blame] | 75 | static char diff_colors[][COLOR_MAXLEN] = { |
Arjen Laarhoven | dc6ebd4 | 2009-02-13 22:53:40 +0100 | [diff] [blame] | 76 | GIT_COLOR_RESET, |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 77 | GIT_COLOR_NORMAL, /* CONTEXT */ |
Arjen Laarhoven | dc6ebd4 | 2009-02-13 22:53:40 +0100 | [diff] [blame] | 78 | GIT_COLOR_BOLD, /* METAINFO */ |
| 79 | GIT_COLOR_CYAN, /* FRAGINFO */ |
| 80 | GIT_COLOR_RED, /* OLD */ |
| 81 | GIT_COLOR_GREEN, /* NEW */ |
| 82 | GIT_COLOR_YELLOW, /* COMMIT */ |
| 83 | GIT_COLOR_BG_RED, /* WHITESPACE */ |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 84 | GIT_COLOR_NORMAL, /* FUNCINFO */ |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 85 | GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */ |
| 86 | GIT_COLOR_BOLD_BLUE, /* OLD_MOVED ALTERNATIVE */ |
| 87 | GIT_COLOR_FAINT, /* OLD_MOVED_DIM */ |
| 88 | GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */ |
| 89 | GIT_COLOR_BOLD_CYAN, /* NEW_MOVED */ |
| 90 | GIT_COLOR_BOLD_YELLOW, /* NEW_MOVED ALTERNATIVE */ |
| 91 | GIT_COLOR_FAINT, /* NEW_MOVED_DIM */ |
| 92 | GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */ |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 93 | GIT_COLOR_FAINT, /* CONTEXT_DIM */ |
| 94 | GIT_COLOR_FAINT_RED, /* OLD_DIM */ |
| 95 | GIT_COLOR_FAINT_GREEN, /* NEW_DIM */ |
| 96 | GIT_COLOR_BOLD, /* CONTEXT_BOLD */ |
| 97 | GIT_COLOR_BOLD_RED, /* OLD_BOLD */ |
| 98 | GIT_COLOR_BOLD_GREEN, /* NEW_BOLD */ |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 101 | static const char *color_diff_slots[] = { |
| 102 | [DIFF_CONTEXT] = "context", |
| 103 | [DIFF_METAINFO] = "meta", |
| 104 | [DIFF_FRAGINFO] = "frag", |
| 105 | [DIFF_FILE_OLD] = "old", |
| 106 | [DIFF_FILE_NEW] = "new", |
| 107 | [DIFF_COMMIT] = "commit", |
| 108 | [DIFF_WHITESPACE] = "whitespace", |
| 109 | [DIFF_FUNCINFO] = "func", |
| 110 | [DIFF_FILE_OLD_MOVED] = "oldMoved", |
| 111 | [DIFF_FILE_OLD_MOVED_ALT] = "oldMovedAlternative", |
| 112 | [DIFF_FILE_OLD_MOVED_DIM] = "oldMovedDimmed", |
| 113 | [DIFF_FILE_OLD_MOVED_ALT_DIM] = "oldMovedAlternativeDimmed", |
| 114 | [DIFF_FILE_NEW_MOVED] = "newMoved", |
| 115 | [DIFF_FILE_NEW_MOVED_ALT] = "newMovedAlternative", |
| 116 | [DIFF_FILE_NEW_MOVED_DIM] = "newMovedDimmed", |
| 117 | [DIFF_FILE_NEW_MOVED_ALT_DIM] = "newMovedAlternativeDimmed", |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 118 | [DIFF_CONTEXT_DIM] = "contextDimmed", |
| 119 | [DIFF_FILE_OLD_DIM] = "oldDimmed", |
| 120 | [DIFF_FILE_NEW_DIM] = "newDimmed", |
| 121 | [DIFF_CONTEXT_BOLD] = "contextBold", |
| 122 | [DIFF_FILE_OLD_BOLD] = "oldBold", |
| 123 | [DIFF_FILE_NEW_BOLD] = "newBold", |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 124 | }; |
| 125 | |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 126 | define_list_config_array_extra(color_diff_slots, {"plain"}); |
| 127 | |
Jeff King | 9e1a5eb | 2014-06-18 15:41:50 -0400 | [diff] [blame] | 128 | static int parse_diff_color_slot(const char *var) |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 129 | { |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 130 | if (!strcasecmp(var, "plain")) |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 131 | return DIFF_CONTEXT; |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 132 | return LOOKUP_CONFIG(color_diff_slots, var); |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 135 | static int parse_dirstat_params(struct diff_options *options, const char *params_string, |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 136 | struct strbuf *errmsg) |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 137 | { |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 138 | char *params_copy = xstrdup(params_string); |
| 139 | struct string_list params = STRING_LIST_INIT_NODUP; |
| 140 | int ret = 0; |
| 141 | int i; |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 142 | |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 143 | if (*params_copy) |
Taylor Blau | 52acddf | 2023-04-24 18:20:10 -0400 | [diff] [blame] | 144 | string_list_split_in_place(¶ms, params_copy, ",", -1); |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 145 | for (i = 0; i < params.nr; i++) { |
| 146 | const char *p = params.items[i].string; |
| 147 | if (!strcmp(p, "changes")) { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 148 | options->flags.dirstat_by_line = 0; |
| 149 | options->flags.dirstat_by_file = 0; |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 150 | } else if (!strcmp(p, "lines")) { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 151 | options->flags.dirstat_by_line = 1; |
| 152 | options->flags.dirstat_by_file = 0; |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 153 | } else if (!strcmp(p, "files")) { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 154 | options->flags.dirstat_by_line = 0; |
| 155 | options->flags.dirstat_by_file = 1; |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 156 | } else if (!strcmp(p, "noncumulative")) { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 157 | options->flags.dirstat_cumulative = 0; |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 158 | } else if (!strcmp(p, "cumulative")) { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 159 | options->flags.dirstat_cumulative = 1; |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 160 | } else if (isdigit(*p)) { |
| 161 | char *end; |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 162 | int permille = strtoul(p, &end, 10) * 10; |
| 163 | if (*end == '.' && isdigit(*++end)) { |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 164 | /* only use first digit */ |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 165 | permille += *end - '0'; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 166 | /* .. and ignore any further digits */ |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 167 | while (isdigit(*++end)) |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 168 | ; /* nothing */ |
| 169 | } |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 170 | if (!*end) |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 171 | options->dirstat_permille = permille; |
| 172 | else { |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 173 | strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"), |
| 174 | p); |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 175 | ret++; |
| 176 | } |
| 177 | } else { |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 178 | strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p); |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 179 | ret++; |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 180 | } |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 181 | |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 182 | } |
Michael Haggerty | 02e8ca0 | 2012-10-28 17:50:54 +0100 | [diff] [blame] | 183 | string_list_clear(¶ms, 0); |
| 184 | free(params_copy); |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 185 | return ret; |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 188 | static int parse_submodule_params(struct diff_options *options, const char *value) |
| 189 | { |
| 190 | if (!strcmp(value, "log")) |
Jacob Keller | 61cfbc0 | 2016-08-31 16:27:21 -0700 | [diff] [blame] | 191 | options->submodule_format = DIFF_SUBMODULE_LOG; |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 192 | else if (!strcmp(value, "short")) |
Jacob Keller | 61cfbc0 | 2016-08-31 16:27:21 -0700 | [diff] [blame] | 193 | options->submodule_format = DIFF_SUBMODULE_SHORT; |
Jacob Keller | fd47ae6 | 2016-08-31 16:27:25 -0700 | [diff] [blame] | 194 | else if (!strcmp(value, "diff")) |
| 195 | options->submodule_format = DIFF_SUBMODULE_INLINE_DIFF; |
Nguyễn Thái Ngọc Duy | 5a59a23 | 2019-02-16 18:24:41 +0700 | [diff] [blame] | 196 | /* |
| 197 | * Please update $__git_diff_submodule_formats in |
| 198 | * git-completion.bash when you add new formats. |
| 199 | */ |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 200 | else |
| 201 | return -1; |
| 202 | return 0; |
| 203 | } |
| 204 | |
Ben Peart | 85b4603 | 2018-05-02 16:01:14 +0000 | [diff] [blame] | 205 | int git_config_rename(const char *var, const char *value) |
Linus Torvalds | cced5fb | 2009-04-09 11:46:15 -0700 | [diff] [blame] | 206 | { |
| 207 | if (!value) |
| 208 | return DIFF_DETECT_RENAME; |
| 209 | if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy")) |
| 210 | return DIFF_DETECT_COPY; |
| 211 | return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0; |
| 212 | } |
| 213 | |
Michal Privoznik | 07924d4 | 2013-01-16 08:51:58 +0100 | [diff] [blame] | 214 | long parse_algorithm_value(const char *value) |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 215 | { |
| 216 | if (!value) |
| 217 | return -1; |
| 218 | else if (!strcasecmp(value, "myers") || !strcasecmp(value, "default")) |
| 219 | return 0; |
| 220 | else if (!strcasecmp(value, "minimal")) |
| 221 | return XDF_NEED_MINIMAL; |
| 222 | else if (!strcasecmp(value, "patience")) |
| 223 | return XDF_PATIENCE_DIFF; |
| 224 | else if (!strcasecmp(value, "histogram")) |
| 225 | return XDF_HISTOGRAM_DIFF; |
Nguyễn Thái Ngọc Duy | 5a59a23 | 2019-02-16 18:24:41 +0700 | [diff] [blame] | 226 | /* |
| 227 | * Please update $__git_diff_algorithms in git-completion.bash |
| 228 | * when you add new algorithms. |
| 229 | */ |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 230 | return -1; |
| 231 | } |
| 232 | |
Junio C Hamano | 0b4b42e | 2016-10-04 15:09:18 -0700 | [diff] [blame] | 233 | static int parse_one_token(const char **arg, const char *token) |
| 234 | { |
| 235 | const char *rest; |
| 236 | if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) { |
| 237 | *arg = rest; |
| 238 | return 1; |
| 239 | } |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static int parse_ws_error_highlight(const char *arg) |
| 244 | { |
| 245 | const char *orig_arg = arg; |
| 246 | unsigned val = 0; |
| 247 | |
| 248 | while (*arg) { |
| 249 | if (parse_one_token(&arg, "none")) |
| 250 | val = 0; |
| 251 | else if (parse_one_token(&arg, "default")) |
| 252 | val = WSEH_NEW; |
| 253 | else if (parse_one_token(&arg, "all")) |
| 254 | val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT; |
| 255 | else if (parse_one_token(&arg, "new")) |
| 256 | val |= WSEH_NEW; |
| 257 | else if (parse_one_token(&arg, "old")) |
| 258 | val |= WSEH_OLD; |
| 259 | else if (parse_one_token(&arg, "context")) |
| 260 | val |= WSEH_CONTEXT; |
| 261 | else { |
| 262 | return -1 - (int)(arg - orig_arg); |
| 263 | } |
| 264 | if (*arg) |
| 265 | arg++; |
| 266 | } |
| 267 | return val; |
| 268 | } |
| 269 | |
Junio C Hamano | e0e324a | 2007-07-07 01:49:58 -0700 | [diff] [blame] | 270 | /* |
Junio C Hamano | 83ad63c | 2006-07-08 01:05:16 -0700 | [diff] [blame] | 271 | * These are to give UI layer defaults. |
| 272 | * The core-level commands such as git-diff-files should |
| 273 | * never be affected by the setting of diff.renames |
| 274 | * the user happens to have in the configuration file. |
| 275 | */ |
Matthieu Moy | 5404c11 | 2016-02-25 09:59:21 +0100 | [diff] [blame] | 276 | void init_diff_ui_defaults(void) |
| 277 | { |
Nguyễn Thái Ngọc Duy | 06dba2b | 2017-12-27 17:18:35 +0700 | [diff] [blame] | 278 | diff_detect_rename_default = DIFF_DETECT_RENAME; |
Matthieu Moy | 5404c11 | 2016-02-25 09:59:21 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Jeff King | 783a86c | 2022-08-19 06:08:44 -0400 | [diff] [blame] | 281 | int git_diff_heuristic_config(const char *var, const char *value, |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 282 | void *cb UNUSED) |
Michael Haggerty | 5b16287 | 2016-09-05 11:44:53 +0200 | [diff] [blame] | 283 | { |
Junio C Hamano | 3cde4e0 | 2016-12-23 12:32:22 -0800 | [diff] [blame] | 284 | if (!strcmp(var, "diff.indentheuristic")) |
Michael Haggerty | 5b16287 | 2016-09-05 11:44:53 +0200 | [diff] [blame] | 285 | diff_indent_heuristic = git_config_bool(var, value); |
Michael Haggerty | 5b16287 | 2016-09-05 11:44:53 +0200 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 289 | static int parse_color_moved(const char *arg) |
| 290 | { |
| 291 | switch (git_parse_maybe_bool(arg)) { |
| 292 | case 0: |
| 293 | return COLOR_MOVED_NO; |
| 294 | case 1: |
| 295 | return COLOR_MOVED_DEFAULT; |
| 296 | default: |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | if (!strcmp(arg, "no")) |
| 301 | return COLOR_MOVED_NO; |
Stefan Beller | 176841f | 2017-06-30 13:53:08 -0700 | [diff] [blame] | 302 | else if (!strcmp(arg, "plain")) |
| 303 | return COLOR_MOVED_PLAIN; |
Stefan Beller | 51da15e | 2018-07-16 16:05:39 -0700 | [diff] [blame] | 304 | else if (!strcmp(arg, "blocks")) |
| 305 | return COLOR_MOVED_BLOCKS; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 306 | else if (!strcmp(arg, "zebra")) |
| 307 | return COLOR_MOVED_ZEBRA; |
| 308 | else if (!strcmp(arg, "default")) |
| 309 | return COLOR_MOVED_DEFAULT; |
Eric Sunshine | e3f2f5f | 2018-07-24 17:58:45 -0400 | [diff] [blame] | 310 | else if (!strcmp(arg, "dimmed-zebra")) |
| 311 | return COLOR_MOVED_ZEBRA_DIM; |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 312 | else if (!strcmp(arg, "dimmed_zebra")) |
| 313 | return COLOR_MOVED_ZEBRA_DIM; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 314 | else |
Junio C Hamano | 706b0b5 | 2018-08-15 15:08:22 -0700 | [diff] [blame] | 315 | return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'")); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Stefan Beller | d173e79 | 2018-11-13 13:33:57 -0800 | [diff] [blame] | 318 | static unsigned parse_color_moved_ws(const char *arg) |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 319 | { |
| 320 | int ret = 0; |
| 321 | struct string_list l = STRING_LIST_INIT_DUP; |
| 322 | struct string_list_item *i; |
| 323 | |
| 324 | string_list_split(&l, arg, ',', -1); |
| 325 | |
| 326 | for_each_string_list_item(i, &l) { |
| 327 | struct strbuf sb = STRBUF_INIT; |
| 328 | strbuf_addstr(&sb, i->string); |
| 329 | strbuf_trim(&sb); |
| 330 | |
Phillip Wood | b73bcba | 2018-11-23 11:16:52 +0000 | [diff] [blame] | 331 | if (!strcmp(sb.buf, "no")) |
| 332 | ret = 0; |
| 333 | else if (!strcmp(sb.buf, "ignore-space-change")) |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 334 | ret |= XDF_IGNORE_WHITESPACE_CHANGE; |
| 335 | else if (!strcmp(sb.buf, "ignore-space-at-eol")) |
| 336 | ret |= XDF_IGNORE_WHITESPACE_AT_EOL; |
| 337 | else if (!strcmp(sb.buf, "ignore-all-space")) |
| 338 | ret |= XDF_IGNORE_WHITESPACE; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 339 | else if (!strcmp(sb.buf, "allow-indentation-change")) |
| 340 | ret |= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE; |
Stefan Beller | d173e79 | 2018-11-13 13:33:57 -0800 | [diff] [blame] | 341 | else { |
| 342 | ret |= COLOR_MOVED_WS_ERROR; |
| 343 | error(_("unknown color-moved-ws mode '%s', possible values are 'ignore-space-change', 'ignore-space-at-eol', 'ignore-all-space', 'allow-indentation-change'"), sb.buf); |
| 344 | } |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 345 | |
| 346 | strbuf_release(&sb); |
| 347 | } |
| 348 | |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 349 | if ((ret & COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) && |
Stefan Beller | d173e79 | 2018-11-13 13:33:57 -0800 | [diff] [blame] | 350 | (ret & XDF_WHITESPACE_FLAGS)) { |
Junio C Hamano | 15b07cb | 2019-01-29 12:47:53 -0800 | [diff] [blame] | 351 | error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes")); |
Stefan Beller | d173e79 | 2018-11-13 13:33:57 -0800 | [diff] [blame] | 352 | ret |= COLOR_MOVED_WS_ERROR; |
| 353 | } |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 354 | |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 355 | string_list_clear(&l, 0); |
| 356 | |
| 357 | return ret; |
Jeff King | e269eb7 | 2011-08-17 22:03:48 -0700 | [diff] [blame] | 358 | } |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 359 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 360 | int git_diff_ui_config(const char *var, const char *value, |
| 361 | const struct config_context *ctx, void *cb) |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 362 | { |
| 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 Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 367 | 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 Beller | 626c0b5 | 2018-07-18 12:31:56 -0700 | [diff] [blame] | 374 | if (!strcmp(var, "diff.colormovedws")) { |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 375 | unsigned cm; |
| 376 | if (!value) |
| 377 | return config_error_nonbool(var); |
| 378 | cm = parse_color_moved_ws(value); |
Stefan Beller | d173e79 | 2018-11-13 13:33:57 -0800 | [diff] [blame] | 379 | if (cm & COLOR_MOVED_WS_ERROR) |
Stefan Beller | 626c0b5 | 2018-07-18 12:31:56 -0700 | [diff] [blame] | 380 | return -1; |
| 381 | diff_color_moved_ws_default = cm; |
| 382 | return 0; |
| 383 | } |
Jeff Muizelaar | 6468a4e | 2012-09-27 15:12:52 -0400 | [diff] [blame] | 384 | if (!strcmp(var, "diff.context")) { |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 385 | diff_context_default = git_config_int(var, value, ctx->kvi); |
Jeff Muizelaar | 6468a4e | 2012-09-27 15:12:52 -0400 | [diff] [blame] | 386 | if (diff_context_default < 0) |
| 387 | return -1; |
| 388 | return 0; |
| 389 | } |
Vegard Nossum | c488867 | 2017-01-12 13:21:11 +0100 | [diff] [blame] | 390 | if (!strcmp(var, "diff.interhunkcontext")) { |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 391 | diff_interhunk_context_default = git_config_int(var, value, |
| 392 | ctx->kvi); |
Vegard Nossum | c488867 | 2017-01-12 13:21:11 +0100 | [diff] [blame] | 393 | if (diff_interhunk_context_default < 0) |
| 394 | return -1; |
| 395 | return 0; |
| 396 | } |
Eric Wong | b68ea12 | 2006-07-07 04:01:23 -0700 | [diff] [blame] | 397 | if (!strcmp(var, "diff.renames")) { |
Linus Torvalds | cced5fb | 2009-04-09 11:46:15 -0700 | [diff] [blame] | 398 | diff_detect_rename_default = git_config_rename(var, value); |
Eric Wong | b68ea12 | 2006-07-07 04:01:23 -0700 | [diff] [blame] | 399 | return 0; |
| 400 | } |
Junio C Hamano | aecbf91 | 2007-08-31 13:13:42 -0700 | [diff] [blame] | 401 | if (!strcmp(var, "diff.autorefreshindex")) { |
| 402 | diff_auto_refresh_index = git_config_bool(var, value); |
| 403 | return 0; |
| 404 | } |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 405 | if (!strcmp(var, "diff.mnemonicprefix")) { |
| 406 | diff_mnemonic_prefix = git_config_bool(var, value); |
| 407 | return 0; |
| 408 | } |
Eli Collins | f89504d | 2010-05-02 19:03:41 -0700 | [diff] [blame] | 409 | if (!strcmp(var, "diff.noprefix")) { |
| 410 | diff_no_prefix = git_config_bool(var, value); |
| 411 | return 0; |
| 412 | } |
Peter Hutterer | 7fdc265 | 2024-03-15 11:03:10 +1000 | [diff] [blame] | 413 | if (!strcmp(var, "diff.srcprefix")) { |
| 414 | return git_config_string(&diff_src_prefix, var, value); |
| 415 | } |
| 416 | if (!strcmp(var, "diff.dstprefix")) { |
| 417 | return git_config_string(&diff_dst_prefix, var, value); |
| 418 | } |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 419 | if (!strcmp(var, "diff.relative")) { |
| 420 | diff_relative = git_config_bool(var, value); |
| 421 | return 0; |
| 422 | } |
Dragan Simic | bd48adc | 2023-09-11 17:39:44 +0200 | [diff] [blame] | 423 | if (!strcmp(var, "diff.statnamewidth")) { |
| 424 | diff_stat_name_width = git_config_int(var, value, ctx->kvi); |
| 425 | return 0; |
| 426 | } |
Zbigniew Jędrzejewski-Szmek | df44483 | 2012-03-01 13:26:46 +0100 | [diff] [blame] | 427 | if (!strcmp(var, "diff.statgraphwidth")) { |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 428 | diff_stat_graph_width = git_config_int(var, value, ctx->kvi); |
Zbigniew Jędrzejewski-Szmek | df44483 | 2012-03-01 13:26:46 +0100 | [diff] [blame] | 429 | return 0; |
| 430 | } |
Brian Hetro | daec808 | 2008-07-05 01:24:43 -0400 | [diff] [blame] | 431 | if (!strcmp(var, "diff.external")) |
| 432 | return git_config_string(&external_diff_cmd_cfg, var, value); |
Boyd Stephen Smith Jr | 98a4d87 | 2009-01-20 21:46:57 -0600 | [diff] [blame] | 433 | if (!strcmp(var, "diff.wordregex")) |
| 434 | return git_config_string(&diff_word_regex_cfg, var, value); |
Samuel Bronson | 6d8940b | 2013-12-18 19:08:12 -0500 | [diff] [blame] | 435 | if (!strcmp(var, "diff.orderfile")) |
| 436 | return git_config_pathname(&diff_order_file_cfg, var, value); |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 437 | |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 438 | if (!strcmp(var, "diff.ignoresubmodules")) { |
| 439 | if (!value) |
| 440 | return config_error_nonbool(var); |
Johannes Schindelin | be4f2b4 | 2010-08-05 10:49:55 +0200 | [diff] [blame] | 441 | handle_ignore_submodules_arg(&default_diff_options, value); |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 442 | } |
Johannes Schindelin | be4f2b4 | 2010-08-05 10:49:55 +0200 | [diff] [blame] | 443 | |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 444 | if (!strcmp(var, "diff.submodule")) { |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 445 | if (!value) |
| 446 | return config_error_nonbool(var); |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 447 | if (parse_submodule_params(&default_diff_options, value)) |
| 448 | warning(_("Unknown value for 'diff.submodule' config variable: '%s'"), |
| 449 | value); |
| 450 | return 0; |
| 451 | } |
| 452 | |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 453 | if (!strcmp(var, "diff.algorithm")) { |
Jeff King | 0824879 | 2023-12-07 02:25:23 -0500 | [diff] [blame] | 454 | if (!value) |
| 455 | return config_error_nonbool(var); |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 456 | diff_algorithm = parse_algorithm_value(value); |
| 457 | if (diff_algorithm < 0) |
Jeff King | 0824879 | 2023-12-07 02:25:23 -0500 | [diff] [blame] | 458 | return error(_("unknown value for config '%s': %s"), |
| 459 | var, value); |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 460 | return 0; |
| 461 | } |
| 462 | |
Jeff King | 33c643b | 2017-10-13 13:24:31 -0400 | [diff] [blame] | 463 | if (git_color_config(var, value, cb) < 0) |
| 464 | return -1; |
| 465 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 466 | return git_diff_basic_config(var, value, ctx, cb); |
Jeff King | 9a1805a | 2008-01-04 03:59:34 -0500 | [diff] [blame] | 467 | } |
| 468 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 469 | int git_diff_basic_config(const char *var, const char *value, |
| 470 | const struct config_context *ctx, void *cb) |
Jeff King | 9a1805a | 2008-01-04 03:59:34 -0500 | [diff] [blame] | 471 | { |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 472 | const char *name; |
| 473 | |
Linus Torvalds | 2b6ca6d | 2008-08-05 11:27:30 -0700 | [diff] [blame] | 474 | if (!strcmp(var, "diff.renamelimit")) { |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 475 | diff_rename_limit_default = git_config_int(var, value, ctx->kvi); |
Linus Torvalds | 2b6ca6d | 2008-08-05 11:27:30 -0700 | [diff] [blame] | 476 | return 0; |
| 477 | } |
| 478 | |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 479 | if (userdiff_config(var, value) < 0) |
| 480 | return -1; |
Jeff King | c7534ef | 2008-10-26 00:45:55 -0400 | [diff] [blame] | 481 | |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 482 | if (skip_prefix(var, "diff.color.", &name) || |
| 483 | skip_prefix(var, "color.diff.", &name)) { |
| 484 | int slot = parse_diff_color_slot(name); |
Jeff King | 8b8e862 | 2009-12-12 07:25:24 -0500 | [diff] [blame] | 485 | if (slot < 0) |
| 486 | return 0; |
Junio C Hamano | 64f30e9 | 2008-02-11 10:53:56 -0800 | [diff] [blame] | 487 | if (!value) |
| 488 | return config_error_nonbool(var); |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 489 | return color_parse(value, diff_colors[slot]); |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 490 | } |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 491 | |
Jeff King | da80635 | 2020-01-31 04:57:49 -0500 | [diff] [blame] | 492 | if (!strcmp(var, "diff.wserrorhighlight")) { |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 493 | int val; |
| 494 | if (!value) |
| 495 | return config_error_nonbool(var); |
| 496 | val = parse_ws_error_highlight(value); |
Jeff King | da80635 | 2020-01-31 04:57:49 -0500 | [diff] [blame] | 497 | if (val < 0) |
Jeff King | 0824879 | 2023-12-07 02:25:23 -0500 | [diff] [blame] | 498 | return error(_("unknown value for config '%s': %s"), |
| 499 | var, value); |
Jeff King | da80635 | 2020-01-31 04:57:49 -0500 | [diff] [blame] | 500 | ws_error_highlight_default = val; |
| 501 | return 0; |
| 502 | } |
| 503 | |
Jim Meyering | a624eaa | 2008-08-15 13:39:26 +0200 | [diff] [blame] | 504 | /* like GNU diff's --suppress-blank-empty option */ |
Johannes Schindelin | 950db87 | 2009-01-20 22:08:33 +0100 | [diff] [blame] | 505 | if (!strcmp(var, "diff.suppressblankempty") || |
| 506 | /* for backwards compatibility */ |
| 507 | !strcmp(var, "diff.suppress-blank-empty")) { |
Jim Meyering | a624eaa | 2008-08-15 13:39:26 +0200 | [diff] [blame] | 508 | diff_suppress_blank_empty = git_config_bool(var, value); |
| 509 | return 0; |
| 510 | } |
| 511 | |
Johan Herland | 2d17495 | 2011-04-29 11:36:19 +0200 | [diff] [blame] | 512 | if (!strcmp(var, "diff.dirstat")) { |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 513 | struct strbuf errmsg = STRBUF_INIT; |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 514 | if (!value) |
| 515 | return config_error_nonbool(var); |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 516 | default_diff_options.dirstat_permille = diff_dirstat_permille_default; |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 517 | if (parse_dirstat_params(&default_diff_options, value, &errmsg)) |
Johan Herland | 7478ac5 | 2011-04-29 11:36:23 +0200 | [diff] [blame] | 518 | warning(_("Found errors in 'diff.dirstat' config variable:\n%s"), |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 519 | errmsg.buf); |
| 520 | strbuf_release(&errmsg); |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 521 | diff_dirstat_permille_default = default_diff_options.dirstat_permille; |
Johan Herland | 2d17495 | 2011-04-29 11:36:19 +0200 | [diff] [blame] | 522 | return 0; |
| 523 | } |
| 524 | |
Marc Branchaud | cf5e772 | 2017-05-08 12:03:36 -0400 | [diff] [blame] | 525 | if (git_diff_heuristic_config(var, value, cb) < 0) |
| 526 | return -1; |
| 527 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 528 | return git_default_config(var, value, ctx, cb); |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 531 | static char *quote_two(const char *one, const char *two) |
| 532 | { |
Junio C Hamano | 7c37c97 | 2020-09-10 10:01:59 -0700 | [diff] [blame] | 533 | int need_one = quote_c_style(one, NULL, NULL, CQUOTE_NODQ); |
| 534 | int need_two = quote_c_style(two, NULL, NULL, CQUOTE_NODQ); |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 535 | struct strbuf res = STRBUF_INIT; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 536 | |
| 537 | if (need_one + need_two) { |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 538 | strbuf_addch(&res, '"'); |
Junio C Hamano | 7c37c97 | 2020-09-10 10:01:59 -0700 | [diff] [blame] | 539 | quote_c_style(one, &res, NULL, CQUOTE_NODQ); |
| 540 | quote_c_style(two, &res, NULL, CQUOTE_NODQ); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 541 | strbuf_addch(&res, '"'); |
| 542 | } else { |
| 543 | strbuf_addstr(&res, one); |
| 544 | strbuf_addstr(&res, two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 545 | } |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 546 | return strbuf_detach(&res, NULL); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | static const char *external_diff(void) |
| 550 | { |
| 551 | static const char *external_diff_cmd = NULL; |
| 552 | static int done_preparing = 0; |
| 553 | |
| 554 | if (done_preparing) |
| 555 | return external_diff_cmd; |
Kim Gybels | 6776a84 | 2019-01-11 21:26:08 +0100 | [diff] [blame] | 556 | external_diff_cmd = xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF")); |
Johannes Schindelin | cbe0210 | 2007-12-17 13:42:20 +0000 | [diff] [blame] | 557 | if (!external_diff_cmd) |
| 558 | external_diff_cmd = external_diff_cmd_cfg; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 559 | done_preparing = 1; |
| 560 | return external_diff_cmd; |
| 561 | } |
| 562 | |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 563 | /* |
| 564 | * Keep track of files used for diffing. Sometimes such an entry |
| 565 | * refers to a temporary file, sometimes to an existing file, and |
| 566 | * sometimes to "/dev/null". |
| 567 | */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 568 | static struct diff_tempfile { |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 569 | /* |
| 570 | * filename external diff should read from, or NULL if this |
| 571 | * entry is currently not in use: |
| 572 | */ |
| 573 | const char *name; |
| 574 | |
brian m. carlson | dc01505 | 2017-03-26 16:01:24 +0000 | [diff] [blame] | 575 | char hex[GIT_MAX_HEXSZ + 1]; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 576 | char mode[10]; |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 577 | |
| 578 | /* |
| 579 | * If this diff_tempfile instance refers to a temporary file, |
| 580 | * this tempfile object is used to manage its lifetime. |
| 581 | */ |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 582 | struct tempfile *tempfile; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 583 | } diff_temp[2]; |
| 584 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 585 | struct emit_callback { |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 586 | int color_diff; |
| 587 | unsigned ws_rule; |
| 588 | int blank_at_eof_in_preimage; |
| 589 | int blank_at_eof_in_postimage; |
| 590 | int lno_in_preimage; |
| 591 | int lno_in_postimage; |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 592 | const char **label_path; |
| 593 | struct diff_words_data *diff_words; |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 594 | struct diff_options *opt; |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 595 | struct strbuf *header; |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 596 | }; |
| 597 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 598 | static int count_lines(const char *data, int size) |
| 599 | { |
| 600 | int count, ch, completely_empty = 1, nl_just_seen = 0; |
| 601 | count = 0; |
| 602 | while (0 < size--) { |
| 603 | ch = *data++; |
| 604 | if (ch == '\n') { |
| 605 | count++; |
| 606 | nl_just_seen = 1; |
| 607 | completely_empty = 0; |
| 608 | } |
| 609 | else { |
| 610 | nl_just_seen = 0; |
| 611 | completely_empty = 0; |
| 612 | } |
| 613 | } |
| 614 | if (completely_empty) |
| 615 | return 0; |
| 616 | if (!nl_just_seen) |
| 617 | count++; /* no trailing newline */ |
| 618 | return count; |
| 619 | } |
| 620 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 621 | static int fill_mmfile(struct repository *r, mmfile_t *mf, |
| 622 | struct diff_filespec *one) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 623 | { |
| 624 | if (!DIFF_FILE_VALID(one)) { |
| 625 | mf->ptr = (char *)""; /* does not matter */ |
| 626 | mf->size = 0; |
| 627 | return 0; |
| 628 | } |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 629 | else if (diff_populate_filespec(r, one, NULL)) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 630 | return -1; |
Junio C Hamano | bb35fef | 2009-09-15 03:38:30 -0700 | [diff] [blame] | 631 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 632 | mf->ptr = one->data; |
| 633 | mf->size = one->size; |
| 634 | return 0; |
| 635 | } |
| 636 | |
Jeff King | abb371a | 2011-02-19 03:16:32 -0500 | [diff] [blame] | 637 | /* like fill_mmfile, but only for size, so we can avoid retrieving blob */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 638 | static unsigned long diff_filespec_size(struct repository *r, |
| 639 | struct diff_filespec *one) |
Jeff King | abb371a | 2011-02-19 03:16:32 -0500 | [diff] [blame] | 640 | { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 641 | struct diff_populate_filespec_options dpf_options = { |
| 642 | .check_size_only = 1, |
| 643 | }; |
| 644 | |
Jeff King | abb371a | 2011-02-19 03:16:32 -0500 | [diff] [blame] | 645 | if (!DIFF_FILE_VALID(one)) |
| 646 | return 0; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 647 | diff_populate_filespec(r, one, &dpf_options); |
Jeff King | abb371a | 2011-02-19 03:16:32 -0500 | [diff] [blame] | 648 | return one->size; |
| 649 | } |
| 650 | |
Jeff King | c5224f0 | 2022-12-13 06:12:58 -0500 | [diff] [blame] | 651 | static int count_trailing_blank(mmfile_t *mf) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 652 | { |
| 653 | char *ptr = mf->ptr; |
| 654 | long size = mf->size; |
| 655 | int cnt = 0; |
| 656 | |
| 657 | if (!size) |
| 658 | return cnt; |
| 659 | ptr += size - 1; /* pointing at the very end */ |
| 660 | if (*ptr != '\n') |
| 661 | ; /* incomplete line */ |
| 662 | else |
| 663 | ptr--; /* skip the last LF */ |
| 664 | while (mf->ptr < ptr) { |
| 665 | char *prev_eol; |
| 666 | for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--) |
| 667 | if (*prev_eol == '\n') |
| 668 | break; |
Jeff King | c5224f0 | 2022-12-13 06:12:58 -0500 | [diff] [blame] | 669 | if (!ws_blank_line(prev_eol + 1, ptr - prev_eol)) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 670 | break; |
| 671 | cnt++; |
| 672 | ptr = prev_eol - 1; |
| 673 | } |
| 674 | return cnt; |
| 675 | } |
| 676 | |
| 677 | static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2, |
| 678 | struct emit_callback *ecbdata) |
| 679 | { |
| 680 | int l1, l2, at; |
Jeff King | c5224f0 | 2022-12-13 06:12:58 -0500 | [diff] [blame] | 681 | l1 = count_trailing_blank(mf1); |
| 682 | l2 = count_trailing_blank(mf2); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 683 | if (l2 <= l1) { |
| 684 | ecbdata->blank_at_eof_in_preimage = 0; |
| 685 | ecbdata->blank_at_eof_in_postimage = 0; |
| 686 | return; |
| 687 | } |
| 688 | at = count_lines(mf1->ptr, mf1->size); |
| 689 | ecbdata->blank_at_eof_in_preimage = (at - l1) + 1; |
| 690 | |
| 691 | at = count_lines(mf2->ptr, mf2->size); |
| 692 | ecbdata->blank_at_eof_in_postimage = (at - l2) + 1; |
| 693 | } |
| 694 | |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 695 | static void emit_line_0(struct diff_options *o, |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 696 | const char *set_sign, const char *set, unsigned reverse, const char *reset, |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 697 | int first, const char *line, int len) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 698 | { |
| 699 | int has_trailing_newline, has_trailing_carriage_return; |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 700 | int needs_reset = 0; /* at the end of the line */ |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 701 | FILE *file = o->file; |
| 702 | |
Stefan Beller | f103a6f | 2018-08-13 18:41:21 -0700 | [diff] [blame] | 703 | fputs(diff_line_prefix(o), file); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 704 | |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 705 | has_trailing_newline = (len > 0 && line[len-1] == '\n'); |
| 706 | if (has_trailing_newline) |
| 707 | len--; |
| 708 | |
| 709 | has_trailing_carriage_return = (len > 0 && line[len-1] == '\r'); |
| 710 | if (has_trailing_carriage_return) |
| 711 | len--; |
| 712 | |
| 713 | if (!len && !first) |
| 714 | goto end_of_line; |
| 715 | |
| 716 | if (reverse && want_color(o->use_color)) { |
| 717 | fputs(GIT_COLOR_REVERSE, file); |
| 718 | needs_reset = 1; |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 719 | } |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 720 | |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 721 | if (set_sign) { |
| 722 | fputs(set_sign, file); |
| 723 | needs_reset = 1; |
Junio C Hamano | 06a4755 | 2009-11-27 22:04:10 -0800 | [diff] [blame] | 724 | } |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 725 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 726 | if (first) |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 727 | fputc(first, file); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 728 | |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 729 | if (!len) |
| 730 | goto end_of_line; |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 731 | |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 732 | if (set) { |
| 733 | if (set_sign && set != set_sign) |
| 734 | fputs(reset, file); |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 735 | fputs(set, file); |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 736 | needs_reset = 1; |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 737 | } |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 738 | fwrite(line, len, 1, file); |
| 739 | needs_reset = 1; /* 'line' may contain color codes. */ |
| 740 | |
| 741 | end_of_line: |
| 742 | if (needs_reset) |
| 743 | fputs(reset, file); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 744 | if (has_trailing_carriage_return) |
| 745 | fputc('\r', file); |
| 746 | if (has_trailing_newline) |
| 747 | fputc('\n', file); |
| 748 | } |
| 749 | |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 750 | static void emit_line(struct diff_options *o, const char *set, const char *reset, |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 751 | const char *line, int len) |
| 752 | { |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 753 | emit_line_0(o, set, NULL, 0, reset, 0, line, len); |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 756 | enum diff_symbol { |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 757 | DIFF_SYMBOL_BINARY_DIFF_HEADER, |
| 758 | DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA, |
| 759 | DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL, |
| 760 | DIFF_SYMBOL_BINARY_DIFF_BODY, |
| 761 | DIFF_SYMBOL_BINARY_DIFF_FOOTER, |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 762 | DIFF_SYMBOL_STATS_SUMMARY_NO_FILES, |
| 763 | DIFF_SYMBOL_STATS_SUMMARY_ABBREV, |
| 764 | DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES, |
| 765 | DIFF_SYMBOL_STATS_LINE, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 766 | DIFF_SYMBOL_WORD_DIFF, |
Stefan Beller | 30b7e1e | 2017-06-29 17:07:04 -0700 | [diff] [blame] | 767 | DIFF_SYMBOL_STAT_SEP, |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 768 | DIFF_SYMBOL_SUMMARY, |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 769 | DIFF_SYMBOL_SUBMODULE_ADD, |
| 770 | DIFF_SYMBOL_SUBMODULE_DEL, |
| 771 | DIFF_SYMBOL_SUBMODULE_UNTRACKED, |
| 772 | DIFF_SYMBOL_SUBMODULE_MODIFIED, |
| 773 | DIFF_SYMBOL_SUBMODULE_HEADER, |
| 774 | DIFF_SYMBOL_SUBMODULE_ERROR, |
| 775 | DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 776 | DIFF_SYMBOL_REWRITE_DIFF, |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 777 | DIFF_SYMBOL_BINARY_FILES, |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 778 | DIFF_SYMBOL_HEADER, |
Stefan Beller | 3ee8b7b | 2017-06-29 17:06:56 -0700 | [diff] [blame] | 779 | DIFF_SYMBOL_FILEPAIR_PLUS, |
| 780 | DIFF_SYMBOL_FILEPAIR_MINUS, |
Stefan Beller | ff95867 | 2017-06-29 17:06:54 -0700 | [diff] [blame] | 781 | DIFF_SYMBOL_WORDS_PORCELAIN, |
| 782 | DIFF_SYMBOL_WORDS, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 783 | DIFF_SYMBOL_CONTEXT, |
Stefan Beller | f2bb121 | 2017-06-29 17:06:55 -0700 | [diff] [blame] | 784 | DIFF_SYMBOL_CONTEXT_INCOMPLETE, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 785 | DIFF_SYMBOL_PLUS, |
| 786 | DIFF_SYMBOL_MINUS, |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 787 | DIFF_SYMBOL_NO_LF_EOF, |
Stefan Beller | 68abc6f | 2017-06-29 17:06:51 -0700 | [diff] [blame] | 788 | DIFF_SYMBOL_CONTEXT_FRAGINFO, |
Stefan Beller | c64b420 | 2017-06-29 17:06:50 -0700 | [diff] [blame] | 789 | DIFF_SYMBOL_CONTEXT_MARKER, |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 790 | DIFF_SYMBOL_SEPARATOR |
| 791 | }; |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 792 | /* |
| 793 | * Flags for content lines: |
| 794 | * 0..12 are whitespace rules |
| 795 | * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT |
| 796 | * 16 is marking if the line is blank at EOF |
| 797 | */ |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 798 | #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16) |
| 799 | #define DIFF_SYMBOL_MOVED_LINE (1<<17) |
| 800 | #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18) |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 801 | #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19) |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 802 | #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK) |
| 803 | |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 804 | /* |
| 805 | * This struct is used when we need to buffer the output of the diff output. |
| 806 | * |
| 807 | * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer |
| 808 | * into the pre/post image file. This pointer could be a union with the |
| 809 | * line pointer. By storing an offset into the file instead of the literal line, |
| 810 | * we can decrease the memory footprint for the buffered output. At first we |
| 811 | * may want to only have indirection for the content lines, but we could also |
| 812 | * enhance the state for emitting prefabricated lines, e.g. the similarity |
| 813 | * score line or hunk/file headers would only need to store a number or path |
| 814 | * and then the output can be constructed later on depending on state. |
| 815 | */ |
| 816 | struct emitted_diff_symbol { |
| 817 | const char *line; |
| 818 | int len; |
| 819 | int flags; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 820 | int indent_off; /* Offset to first non-whitespace character */ |
| 821 | int indent_width; /* The visual width of the indentation */ |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 822 | unsigned id; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 823 | enum diff_symbol s; |
| 824 | }; |
Ævar Arnfjörð Bjarmason | 9865b6e | 2021-09-27 14:54:25 +0200 | [diff] [blame] | 825 | #define EMITTED_DIFF_SYMBOL_INIT { 0 } |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 826 | |
| 827 | struct emitted_diff_symbols { |
| 828 | struct emitted_diff_symbol *buf; |
| 829 | int nr, alloc; |
| 830 | }; |
Ævar Arnfjörð Bjarmason | 9865b6e | 2021-09-27 14:54:25 +0200 | [diff] [blame] | 831 | #define EMITTED_DIFF_SYMBOLS_INIT { 0 } |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 832 | |
| 833 | static void append_emitted_diff_symbol(struct diff_options *o, |
| 834 | struct emitted_diff_symbol *e) |
| 835 | { |
| 836 | struct emitted_diff_symbol *f; |
| 837 | |
| 838 | ALLOC_GROW(o->emitted_symbols->buf, |
| 839 | o->emitted_symbols->nr + 1, |
| 840 | o->emitted_symbols->alloc); |
| 841 | f = &o->emitted_symbols->buf[o->emitted_symbols->nr++]; |
| 842 | |
| 843 | memcpy(f, e, sizeof(struct emitted_diff_symbol)); |
| 844 | f->line = e->line ? xmemdupz(e->line, e->len) : NULL; |
| 845 | } |
| 846 | |
Ævar Arnfjörð Bjarmason | 77e56d5 | 2022-03-17 15:55:35 +0100 | [diff] [blame] | 847 | static void free_emitted_diff_symbols(struct emitted_diff_symbols *e) |
| 848 | { |
| 849 | if (!e) |
| 850 | return; |
| 851 | free(e->buf); |
| 852 | free(e); |
| 853 | } |
| 854 | |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 855 | struct moved_entry { |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 856 | const struct emitted_diff_symbol *es; |
| 857 | struct moved_entry *next_line; |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 858 | struct moved_entry *next_match; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 859 | }; |
| 860 | |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 861 | struct moved_block { |
| 862 | struct moved_entry *match; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 863 | int wsd; /* The whitespace delta of this block */ |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 864 | }; |
| 865 | |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 866 | #define INDENT_BLANKLINE INT_MIN |
| 867 | |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 868 | static void fill_es_indent_data(struct emitted_diff_symbol *es) |
| 869 | { |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 870 | unsigned int off = 0, i; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 871 | int width = 0, tab_width = es->flags & WS_TAB_WIDTH_MASK; |
| 872 | const char *s = es->line; |
| 873 | const int len = es->len; |
| 874 | |
| 875 | /* skip any \v \f \r at start of indentation */ |
| 876 | while (s[off] == '\f' || s[off] == '\v' || |
| 877 | (s[off] == '\r' && off < len - 1)) |
| 878 | off++; |
| 879 | |
| 880 | /* calculate the visual width of indentation */ |
| 881 | while(1) { |
| 882 | if (s[off] == ' ') { |
| 883 | width++; |
| 884 | off++; |
| 885 | } else if (s[off] == '\t') { |
| 886 | width += tab_width - (width % tab_width); |
| 887 | while (s[++off] == '\t') |
| 888 | width += tab_width; |
| 889 | } else { |
| 890 | break; |
| 891 | } |
| 892 | } |
| 893 | |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 894 | /* check if this line is blank */ |
| 895 | for (i = off; i < len; i++) |
| 896 | if (!isspace(s[i])) |
| 897 | break; |
| 898 | |
| 899 | if (i == len) { |
| 900 | es->indent_width = INDENT_BLANKLINE; |
| 901 | es->indent_off = len; |
| 902 | } else { |
| 903 | es->indent_off = off; |
| 904 | es->indent_width = width; |
| 905 | } |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 906 | } |
| 907 | |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 908 | static int compute_ws_delta(const struct emitted_diff_symbol *a, |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 909 | const struct emitted_diff_symbol *b) |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 910 | { |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 911 | int a_width = a->indent_width, |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 912 | b_width = b->indent_width; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 913 | |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 914 | if (a_width == INDENT_BLANKLINE && b_width == INDENT_BLANKLINE) |
| 915 | return INDENT_BLANKLINE; |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 916 | |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 917 | return a_width - b_width; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 918 | } |
| 919 | |
Phillip Wood | 52d14e1 | 2021-12-09 10:30:02 +0000 | [diff] [blame] | 920 | static int cmp_in_block_with_wsd(const struct moved_entry *cur, |
| 921 | const struct emitted_diff_symbol *l, |
| 922 | struct moved_block *pmb) |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 923 | { |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 924 | int a_width = cur->es->indent_width, b_width = l->indent_width; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 925 | int delta; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 926 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 927 | /* The text of each line must match */ |
| 928 | if (cur->es->id != l->id) |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 929 | return 1; |
| 930 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 931 | /* |
| 932 | * If 'l' and 'cur' are both blank then we don't need to check the |
| 933 | * indent. We only need to check cur as we know the strings match. |
| 934 | * */ |
| 935 | if (a_width == INDENT_BLANKLINE) |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 936 | return 0; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 937 | |
| 938 | /* |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 939 | * The indent changes of the block are known and stored in pmb->wsd; |
| 940 | * however we need to check if the indent changes of the current line |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 941 | * match those of the current block. |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 942 | */ |
Phillip Wood | 52d14e1 | 2021-12-09 10:30:02 +0000 | [diff] [blame] | 943 | delta = b_width - a_width; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 944 | |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 945 | /* |
| 946 | * If the previous lines of this block were all blank then set its |
| 947 | * whitespace delta. |
| 948 | */ |
| 949 | if (pmb->wsd == INDENT_BLANKLINE) |
| 950 | pmb->wsd = delta; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 951 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 952 | return delta != pmb->wsd; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 955 | struct interned_diff_symbol { |
| 956 | struct hashmap_entry ent; |
| 957 | struct emitted_diff_symbol *es; |
| 958 | }; |
| 959 | |
| 960 | static int interned_diff_symbol_cmp(const void *hashmap_cmp_fn_data, |
| 961 | const struct hashmap_entry *eptr, |
| 962 | const struct hashmap_entry *entry_or_key, |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 963 | const void *keydata UNUSED) |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 964 | { |
Stefan Beller | ee1df66 | 2018-07-16 16:05:38 -0700 | [diff] [blame] | 965 | const struct diff_options *diffopt = hashmap_cmp_fn_data; |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 966 | const struct emitted_diff_symbol *a, *b; |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 967 | unsigned flags = diffopt->color_moved_ws_handling |
| 968 | & XDF_WHITESPACE_FLAGS; |
Stefan Beller | ee1df66 | 2018-07-16 16:05:38 -0700 | [diff] [blame] | 969 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 970 | a = container_of(eptr, const struct interned_diff_symbol, ent)->es; |
| 971 | b = container_of(entry_or_key, const struct interned_diff_symbol, ent)->es; |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 972 | |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 973 | return !xdiff_compare_lines(a->line + a->indent_off, |
| 974 | a->len - a->indent_off, |
| 975 | b->line + b->indent_off, |
| 976 | b->len - b->indent_off, flags); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 979 | static void prepare_entry(struct diff_options *o, struct emitted_diff_symbol *l, |
| 980 | struct interned_diff_symbol *s) |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 981 | { |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 982 | unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS; |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 983 | unsigned int hash = xdiff_hash_string(l->line + l->indent_off, |
| 984 | l->len - l->indent_off, flags); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 985 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 986 | hashmap_entry_init(&s->ent, hash); |
| 987 | s->es = l; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 990 | struct moved_entry_list { |
| 991 | struct moved_entry *add, *del; |
| 992 | }; |
| 993 | |
| 994 | static struct moved_entry_list *add_lines_to_move_detection(struct diff_options *o, |
| 995 | struct mem_pool *entry_mem_pool) |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 996 | { |
| 997 | struct moved_entry *prev_line = NULL; |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 998 | struct mem_pool interned_pool; |
| 999 | struct hashmap interned_map; |
| 1000 | struct moved_entry_list *entry_list = NULL; |
| 1001 | size_t entry_list_alloc = 0; |
| 1002 | unsigned id = 0; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1003 | int n; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1004 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1005 | hashmap_init(&interned_map, interned_diff_symbol_cmp, o, 8096); |
| 1006 | mem_pool_init(&interned_pool, 1024 * 1024); |
| 1007 | |
| 1008 | for (n = 0; n < o->emitted_symbols->nr; n++) { |
| 1009 | struct interned_diff_symbol key; |
| 1010 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; |
| 1011 | struct interned_diff_symbol *s; |
| 1012 | struct moved_entry *entry; |
| 1013 | |
| 1014 | if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS) { |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1015 | prev_line = NULL; |
| 1016 | continue; |
| 1017 | } |
| 1018 | |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 1019 | if (o->color_moved_ws_handling & |
| 1020 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1021 | fill_es_indent_data(l); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1022 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1023 | prepare_entry(o, l, &key); |
| 1024 | s = hashmap_get_entry(&interned_map, &key, ent, &key.ent); |
| 1025 | if (s) { |
| 1026 | l->id = s->es->id; |
| 1027 | } else { |
| 1028 | l->id = id; |
| 1029 | ALLOC_GROW_BY(entry_list, id, 1, entry_list_alloc); |
| 1030 | hashmap_add(&interned_map, |
| 1031 | memcpy(mem_pool_alloc(&interned_pool, |
| 1032 | sizeof(key)), |
| 1033 | &key, sizeof(key))); |
| 1034 | } |
| 1035 | entry = mem_pool_alloc(entry_mem_pool, sizeof(*entry)); |
| 1036 | entry->es = l; |
| 1037 | entry->next_line = NULL; |
| 1038 | if (prev_line && prev_line->es->s == l->s) |
| 1039 | prev_line->next_line = entry; |
| 1040 | prev_line = entry; |
| 1041 | if (l->s == DIFF_SYMBOL_PLUS) { |
| 1042 | entry->next_match = entry_list[l->id].add; |
| 1043 | entry_list[l->id].add = entry; |
| 1044 | } else { |
| 1045 | entry->next_match = entry_list[l->id].del; |
| 1046 | entry_list[l->id].del = entry; |
| 1047 | } |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1048 | } |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1049 | |
| 1050 | hashmap_clear(&interned_map); |
| 1051 | mem_pool_discard(&interned_pool, 0); |
| 1052 | |
| 1053 | return entry_list; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1056 | static void pmb_advance_or_null(struct diff_options *o, |
Phillip Wood | 08fba10 | 2021-12-09 10:30:03 +0000 | [diff] [blame] | 1057 | struct emitted_diff_symbol *l, |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1058 | struct moved_block *pmb, |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1059 | int *pmb_nr) |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1060 | { |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1061 | int i, j; |
Phillip Wood | 08fba10 | 2021-12-09 10:30:03 +0000 | [diff] [blame] | 1062 | |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1063 | for (i = 0, j = 0; i < *pmb_nr; i++) { |
Phillip Wood | ff046a0 | 2021-12-09 10:30:04 +0000 | [diff] [blame] | 1064 | int match; |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1065 | struct moved_entry *prev = pmb[i].match; |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1066 | struct moved_entry *cur = (prev && prev->next_line) ? |
| 1067 | prev->next_line : NULL; |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1068 | |
Phillip Wood | ff046a0 | 2021-12-09 10:30:04 +0000 | [diff] [blame] | 1069 | if (o->color_moved_ws_handling & |
| 1070 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) |
| 1071 | match = cur && |
| 1072 | !cmp_in_block_with_wsd(cur, l, &pmb[i]); |
| 1073 | else |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1074 | match = cur && cur->es->id == l->id; |
| 1075 | |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1076 | if (match) { |
| 1077 | pmb[j] = pmb[i]; |
| 1078 | pmb[j++].match = cur; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1079 | } |
| 1080 | } |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1081 | *pmb_nr = j; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1084 | static void fill_potential_moved_blocks(struct diff_options *o, |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1085 | struct moved_entry *match, |
| 1086 | struct emitted_diff_symbol *l, |
| 1087 | struct moved_block **pmb_p, |
| 1088 | int *pmb_alloc_p, int *pmb_nr_p) |
| 1089 | |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1090 | { |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1091 | struct moved_block *pmb = *pmb_p; |
| 1092 | int pmb_alloc = *pmb_alloc_p, pmb_nr = *pmb_nr_p; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1093 | |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1094 | /* |
| 1095 | * The current line is the start of a new block. |
| 1096 | * Setup the set of potential blocks. |
| 1097 | */ |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1098 | for (; match; match = match->next_match) { |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1099 | ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc); |
| 1100 | if (o->color_moved_ws_handling & |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 1101 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) |
| 1102 | pmb[pmb_nr].wsd = compute_ws_delta(l, match->es); |
| 1103 | else |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1104 | pmb[pmb_nr].wsd = 0; |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 1105 | pmb[pmb_nr++].match = match; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1108 | *pmb_p = pmb; |
| 1109 | *pmb_alloc_p = pmb_alloc; |
| 1110 | *pmb_nr_p = pmb_nr; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1113 | /* |
| 1114 | * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing. |
| 1115 | * |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1116 | * Otherwise, if the last block has fewer alphanumeric characters than |
| 1117 | * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1118 | * that block. |
| 1119 | * |
| 1120 | * The last block consists of the (n - block_length)'th line up to but not |
| 1121 | * including the nth line. |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1122 | * |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1123 | * Returns 0 if the last block is empty or is unset by this function, non zero |
| 1124 | * otherwise. |
| 1125 | * |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1126 | * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c. |
| 1127 | * Think of a way to unify them. |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1128 | */ |
Phillip Wood | bea084b | 2021-12-09 10:29:56 +0000 | [diff] [blame] | 1129 | #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \ |
| 1130 | (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT) |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1131 | static int adjust_last_block(struct diff_options *o, int n, int block_length) |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1132 | { |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1133 | int i, alnum_count = 0; |
| 1134 | if (o->color_moved == COLOR_MOVED_PLAIN) |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1135 | return block_length; |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1136 | for (i = 1; i < block_length + 1; i++) { |
| 1137 | const char *c = o->emitted_symbols->buf[n - i].line; |
| 1138 | for (; *c; c++) { |
| 1139 | if (!isalnum(*c)) |
| 1140 | continue; |
| 1141 | alnum_count++; |
| 1142 | if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT) |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1143 | return 1; |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1144 | } |
| 1145 | } |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1146 | for (i = 1; i < block_length + 1; i++) |
Phillip Wood | bea084b | 2021-12-09 10:29:56 +0000 | [diff] [blame] | 1147 | o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK; |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1148 | return 0; |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1151 | /* Find blocks of moved code, delegate actual coloring decision to helper */ |
| 1152 | static void mark_color_as_moved(struct diff_options *o, |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1153 | struct moved_entry_list *entry_list) |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1154 | { |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1155 | struct moved_block *pmb = NULL; /* potentially moved blocks */ |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1156 | int pmb_nr = 0, pmb_alloc = 0; |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1157 | int n, flipped_block = 0, block_length = 0; |
Phillip Wood | eb89352 | 2021-12-09 10:30:00 +0000 | [diff] [blame] | 1158 | enum diff_symbol moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1159 | |
| 1160 | |
| 1161 | for (n = 0; n < o->emitted_symbols->nr; n++) { |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1162 | struct moved_entry *match = NULL; |
| 1163 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1164 | |
| 1165 | switch (l->s) { |
| 1166 | case DIFF_SYMBOL_PLUS: |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1167 | match = entry_list[l->id].del; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1168 | break; |
| 1169 | case DIFF_SYMBOL_MINUS: |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1170 | match = entry_list[l->id].add; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1171 | break; |
| 1172 | default: |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1173 | flipped_block = 0; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Phillip Wood | eb89352 | 2021-12-09 10:30:00 +0000 | [diff] [blame] | 1176 | if (pmb_nr && (!match || l->s != moved_symbol)) { |
Phillip Wood | 0990658 | 2021-12-09 10:29:58 +0000 | [diff] [blame] | 1177 | if (!adjust_last_block(o, n, block_length) && |
| 1178 | block_length > 1) { |
| 1179 | /* |
| 1180 | * Rewind in case there is another match |
| 1181 | * starting at the second line of the block |
| 1182 | */ |
| 1183 | match = NULL; |
| 1184 | n -= block_length; |
| 1185 | } |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1186 | pmb_nr = 0; |
| 1187 | block_length = 0; |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1188 | flipped_block = 0; |
Phillip Wood | eb89352 | 2021-12-09 10:30:00 +0000 | [diff] [blame] | 1189 | } |
| 1190 | if (!match) { |
| 1191 | moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1192 | continue; |
| 1193 | } |
| 1194 | |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1195 | if (o->color_moved == COLOR_MOVED_PLAIN) { |
| 1196 | l->flags |= DIFF_SYMBOL_MOVED_LINE; |
Stefan Beller | 176841f | 2017-06-30 13:53:08 -0700 | [diff] [blame] | 1197 | continue; |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1198 | } |
Stefan Beller | 176841f | 2017-06-30 13:53:08 -0700 | [diff] [blame] | 1199 | |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1200 | pmb_advance_or_null(o, l, pmb, &pmb_nr); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1201 | |
| 1202 | if (pmb_nr == 0) { |
Phillip Wood | 0990658 | 2021-12-09 10:29:58 +0000 | [diff] [blame] | 1203 | int contiguous = adjust_last_block(o, n, block_length); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1204 | |
Phillip Wood | 0990658 | 2021-12-09 10:29:58 +0000 | [diff] [blame] | 1205 | if (!contiguous && block_length > 1) |
| 1206 | /* |
| 1207 | * Rewind in case there is another match |
| 1208 | * starting at the second line of the block |
| 1209 | */ |
| 1210 | n -= block_length; |
| 1211 | else |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1212 | fill_potential_moved_blocks(o, match, l, |
Phillip Wood | 0990658 | 2021-12-09 10:29:58 +0000 | [diff] [blame] | 1213 | &pmb, &pmb_alloc, |
| 1214 | &pmb_nr); |
| 1215 | |
Phillip Wood | eb89352 | 2021-12-09 10:30:00 +0000 | [diff] [blame] | 1216 | if (contiguous && pmb_nr && moved_symbol == l->s) |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1217 | flipped_block = (flipped_block + 1) % 2; |
| 1218 | else |
| 1219 | flipped_block = 0; |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1220 | |
Phillip Wood | eb89352 | 2021-12-09 10:30:00 +0000 | [diff] [blame] | 1221 | if (pmb_nr) |
| 1222 | moved_symbol = l->s; |
| 1223 | else |
| 1224 | moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER; |
| 1225 | |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1226 | block_length = 0; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1229 | if (pmb_nr) { |
| 1230 | block_length++; |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1231 | l->flags |= DIFF_SYMBOL_MOVED_LINE; |
| 1232 | if (flipped_block && o->color_moved != COLOR_MOVED_BLOCKS) |
| 1233 | l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT; |
| 1234 | } |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1235 | } |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1236 | adjust_last_block(o, n, block_length); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1237 | |
| 1238 | free(pmb); |
| 1239 | } |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1240 | |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1241 | static void dim_moved_lines(struct diff_options *o) |
| 1242 | { |
| 1243 | int n; |
| 1244 | for (n = 0; n < o->emitted_symbols->nr; n++) { |
| 1245 | struct emitted_diff_symbol *prev = (n != 0) ? |
| 1246 | &o->emitted_symbols->buf[n - 1] : NULL; |
| 1247 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; |
| 1248 | struct emitted_diff_symbol *next = |
| 1249 | (n < o->emitted_symbols->nr - 1) ? |
| 1250 | &o->emitted_symbols->buf[n + 1] : NULL; |
| 1251 | |
| 1252 | /* Not a plus or minus line? */ |
| 1253 | if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS) |
| 1254 | continue; |
| 1255 | |
| 1256 | /* Not a moved line? */ |
| 1257 | if (!(l->flags & DIFF_SYMBOL_MOVED_LINE)) |
| 1258 | continue; |
| 1259 | |
| 1260 | /* |
| 1261 | * If prev or next are not a plus or minus line, |
| 1262 | * pretend they don't exist |
| 1263 | */ |
| 1264 | if (prev && prev->s != DIFF_SYMBOL_PLUS && |
| 1265 | prev->s != DIFF_SYMBOL_MINUS) |
| 1266 | prev = NULL; |
| 1267 | if (next && next->s != DIFF_SYMBOL_PLUS && |
| 1268 | next->s != DIFF_SYMBOL_MINUS) |
| 1269 | next = NULL; |
| 1270 | |
| 1271 | /* Inside a block? */ |
| 1272 | if ((prev && |
| 1273 | (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) == |
| 1274 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) && |
| 1275 | (next && |
| 1276 | (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) == |
| 1277 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) { |
| 1278 | l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING; |
| 1279 | continue; |
| 1280 | } |
| 1281 | |
| 1282 | /* Check if we are at an interesting bound: */ |
| 1283 | if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) && |
| 1284 | (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) != |
| 1285 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT)) |
| 1286 | continue; |
| 1287 | if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) && |
| 1288 | (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) != |
| 1289 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT)) |
| 1290 | continue; |
| 1291 | |
| 1292 | /* |
| 1293 | * The boundary to prev and next are not interesting, |
| 1294 | * so this line is not interesting as a whole |
| 1295 | */ |
| 1296 | l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING; |
| 1297 | } |
| 1298 | } |
| 1299 | |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1300 | static void emit_line_ws_markup(struct diff_options *o, |
Stefan Beller | 9d1e16b | 2018-08-13 18:41:18 -0700 | [diff] [blame] | 1301 | const char *set_sign, const char *set, |
| 1302 | const char *reset, |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1303 | int sign_index, const char *line, int len, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1304 | unsigned ws_rule, int blank_at_eof) |
| 1305 | { |
| 1306 | const char *ws = NULL; |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1307 | int sign = o->output_indicators[sign_index]; |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1308 | |
| 1309 | if (o->ws_error_highlight & ws_rule) { |
| 1310 | ws = diff_get_color_opt(o, DIFF_WHITESPACE); |
| 1311 | if (!*ws) |
| 1312 | ws = NULL; |
| 1313 | } |
| 1314 | |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1315 | if (!ws && !set_sign) |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 1316 | emit_line_0(o, set, NULL, 0, reset, sign, line, len); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1317 | else if (!ws) { |
Stefan Beller | 29ef759 | 2018-08-13 18:41:20 -0700 | [diff] [blame] | 1318 | emit_line_0(o, set_sign, set, !!set_sign, reset, sign, line, len); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1319 | } else if (blank_at_eof) |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1320 | /* Blank line at EOF - paint '+' as well */ |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 1321 | emit_line_0(o, ws, NULL, 0, reset, sign, line, len); |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1322 | else { |
| 1323 | /* Emit just the prefix, then the rest. */ |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 1324 | emit_line_0(o, set_sign ? set_sign : set, NULL, !!set_sign, reset, |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1325 | sign, "", 0); |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1326 | ws_check_emit(line, len, ws_rule, |
| 1327 | o->file, set, reset, ws); |
| 1328 | } |
| 1329 | } |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1330 | |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1331 | static void emit_diff_symbol_from_struct(struct diff_options *o, |
| 1332 | struct emitted_diff_symbol *eds) |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1333 | { |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 1334 | static const char *nneof = " No newline at end of file\n"; |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1335 | const char *context, *reset, *set, *set_sign, *meta, *fraginfo; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1336 | |
| 1337 | enum diff_symbol s = eds->s; |
| 1338 | const char *line = eds->line; |
| 1339 | int len = eds->len; |
| 1340 | unsigned flags = eds->flags; |
| 1341 | |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1342 | switch (s) { |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 1343 | case DIFF_SYMBOL_NO_LF_EOF: |
| 1344 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1345 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1346 | putc('\n', o->file); |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 1347 | emit_line_0(o, context, NULL, 0, reset, '\\', |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 1348 | nneof, strlen(nneof)); |
| 1349 | break; |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 1350 | case DIFF_SYMBOL_SUBMODULE_HEADER: |
| 1351 | case DIFF_SYMBOL_SUBMODULE_ERROR: |
| 1352 | case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH: |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 1353 | case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 1354 | case DIFF_SYMBOL_SUMMARY: |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 1355 | case DIFF_SYMBOL_STATS_LINE: |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 1356 | case DIFF_SYMBOL_BINARY_DIFF_BODY: |
Stefan Beller | 68abc6f | 2017-06-29 17:06:51 -0700 | [diff] [blame] | 1357 | case DIFF_SYMBOL_CONTEXT_FRAGINFO: |
| 1358 | emit_line(o, "", "", line, len); |
| 1359 | break; |
Stefan Beller | f2bb121 | 2017-06-29 17:06:55 -0700 | [diff] [blame] | 1360 | case DIFF_SYMBOL_CONTEXT_INCOMPLETE: |
Stefan Beller | c64b420 | 2017-06-29 17:06:50 -0700 | [diff] [blame] | 1361 | case DIFF_SYMBOL_CONTEXT_MARKER: |
| 1362 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1363 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1364 | emit_line(o, context, reset, line, len); |
| 1365 | break; |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1366 | case DIFF_SYMBOL_SEPARATOR: |
| 1367 | fprintf(o->file, "%s%c", |
| 1368 | diff_line_prefix(o), |
| 1369 | o->line_termination); |
| 1370 | break; |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1371 | case DIFF_SYMBOL_CONTEXT: |
| 1372 | set = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1373 | reset = diff_get_color_opt(o, DIFF_RESET); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1374 | set_sign = NULL; |
| 1375 | if (o->flags.dual_color_diffed_diffs) { |
| 1376 | char c = !len ? 0 : line[0]; |
| 1377 | |
| 1378 | if (c == '+') |
| 1379 | set = diff_get_color_opt(o, DIFF_FILE_NEW); |
| 1380 | else if (c == '@') |
| 1381 | set = diff_get_color_opt(o, DIFF_FRAGINFO); |
| 1382 | else if (c == '-') |
| 1383 | set = diff_get_color_opt(o, DIFF_FILE_OLD); |
| 1384 | } |
Stefan Beller | 7648b79 | 2018-08-17 13:43:52 -0700 | [diff] [blame] | 1385 | emit_line_ws_markup(o, set_sign, set, reset, |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1386 | OUTPUT_INDICATOR_CONTEXT, line, len, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1387 | flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0); |
| 1388 | break; |
| 1389 | case DIFF_SYMBOL_PLUS: |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1390 | switch (flags & (DIFF_SYMBOL_MOVED_LINE | |
| 1391 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1392 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) { |
| 1393 | case DIFF_SYMBOL_MOVED_LINE | |
| 1394 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1395 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1396 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM); |
| 1397 | break; |
| 1398 | case DIFF_SYMBOL_MOVED_LINE | |
| 1399 | DIFF_SYMBOL_MOVED_LINE_ALT: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1400 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1401 | break; |
| 1402 | case DIFF_SYMBOL_MOVED_LINE | |
| 1403 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1404 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM); |
| 1405 | break; |
| 1406 | case DIFF_SYMBOL_MOVED_LINE: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1407 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1408 | break; |
| 1409 | default: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1410 | set = diff_get_color_opt(o, DIFF_FILE_NEW); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1411 | } |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1412 | reset = diff_get_color_opt(o, DIFF_RESET); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1413 | if (!o->flags.dual_color_diffed_diffs) |
| 1414 | set_sign = NULL; |
| 1415 | else { |
| 1416 | char c = !len ? 0 : line[0]; |
| 1417 | |
| 1418 | set_sign = set; |
| 1419 | if (c == '-') |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 1420 | set = diff_get_color_opt(o, DIFF_FILE_OLD_BOLD); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1421 | else if (c == '@') |
| 1422 | set = diff_get_color_opt(o, DIFF_FRAGINFO); |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 1423 | else if (c == '+') |
| 1424 | set = diff_get_color_opt(o, DIFF_FILE_NEW_BOLD); |
| 1425 | else |
| 1426 | set = diff_get_color_opt(o, DIFF_CONTEXT_BOLD); |
Johannes Schindelin | 0b91faa | 2018-08-13 04:33:24 -0700 | [diff] [blame] | 1427 | flags &= ~DIFF_SYMBOL_CONTENT_WS_MASK; |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1428 | } |
Stefan Beller | 7648b79 | 2018-08-17 13:43:52 -0700 | [diff] [blame] | 1429 | emit_line_ws_markup(o, set_sign, set, reset, |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1430 | OUTPUT_INDICATOR_NEW, line, len, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1431 | flags & DIFF_SYMBOL_CONTENT_WS_MASK, |
| 1432 | flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF); |
| 1433 | break; |
| 1434 | case DIFF_SYMBOL_MINUS: |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1435 | switch (flags & (DIFF_SYMBOL_MOVED_LINE | |
| 1436 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1437 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) { |
| 1438 | case DIFF_SYMBOL_MOVED_LINE | |
| 1439 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1440 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1441 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM); |
| 1442 | break; |
| 1443 | case DIFF_SYMBOL_MOVED_LINE | |
| 1444 | DIFF_SYMBOL_MOVED_LINE_ALT: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1445 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1446 | break; |
| 1447 | case DIFF_SYMBOL_MOVED_LINE | |
| 1448 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1449 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM); |
| 1450 | break; |
| 1451 | case DIFF_SYMBOL_MOVED_LINE: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1452 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1453 | break; |
| 1454 | default: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1455 | set = diff_get_color_opt(o, DIFF_FILE_OLD); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1456 | } |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1457 | reset = diff_get_color_opt(o, DIFF_RESET); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1458 | if (!o->flags.dual_color_diffed_diffs) |
| 1459 | set_sign = NULL; |
| 1460 | else { |
| 1461 | char c = !len ? 0 : line[0]; |
| 1462 | |
| 1463 | set_sign = set; |
| 1464 | if (c == '+') |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 1465 | set = diff_get_color_opt(o, DIFF_FILE_NEW_DIM); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1466 | else if (c == '@') |
| 1467 | set = diff_get_color_opt(o, DIFF_FRAGINFO); |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 1468 | else if (c == '-') |
| 1469 | set = diff_get_color_opt(o, DIFF_FILE_OLD_DIM); |
| 1470 | else |
| 1471 | set = diff_get_color_opt(o, DIFF_CONTEXT_DIM); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1472 | } |
Stefan Beller | 7648b79 | 2018-08-17 13:43:52 -0700 | [diff] [blame] | 1473 | emit_line_ws_markup(o, set_sign, set, reset, |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1474 | OUTPUT_INDICATOR_OLD, line, len, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1475 | flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0); |
| 1476 | break; |
Stefan Beller | ff95867 | 2017-06-29 17:06:54 -0700 | [diff] [blame] | 1477 | case DIFF_SYMBOL_WORDS_PORCELAIN: |
| 1478 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1479 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1480 | emit_line(o, context, reset, line, len); |
| 1481 | fputs("~\n", o->file); |
| 1482 | break; |
| 1483 | case DIFF_SYMBOL_WORDS: |
| 1484 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1485 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1486 | /* |
| 1487 | * Skip the prefix character, if any. With |
| 1488 | * diff_suppress_blank_empty, there may be |
| 1489 | * none. |
| 1490 | */ |
| 1491 | if (line[0] != '\n') { |
| 1492 | line++; |
| 1493 | len--; |
| 1494 | } |
| 1495 | emit_line(o, context, reset, line, len); |
| 1496 | break; |
Stefan Beller | 3ee8b7b | 2017-06-29 17:06:56 -0700 | [diff] [blame] | 1497 | case DIFF_SYMBOL_FILEPAIR_PLUS: |
| 1498 | meta = diff_get_color_opt(o, DIFF_METAINFO); |
| 1499 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1500 | fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta, |
| 1501 | line, reset, |
| 1502 | strchr(line, ' ') ? "\t" : ""); |
| 1503 | break; |
| 1504 | case DIFF_SYMBOL_FILEPAIR_MINUS: |
| 1505 | meta = diff_get_color_opt(o, DIFF_METAINFO); |
| 1506 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1507 | fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta, |
| 1508 | line, reset, |
| 1509 | strchr(line, ' ') ? "\t" : ""); |
| 1510 | break; |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 1511 | case DIFF_SYMBOL_BINARY_FILES: |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 1512 | case DIFF_SYMBOL_HEADER: |
| 1513 | fprintf(o->file, "%s", line); |
| 1514 | break; |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 1515 | case DIFF_SYMBOL_BINARY_DIFF_HEADER: |
| 1516 | fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o)); |
| 1517 | break; |
| 1518 | case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA: |
| 1519 | fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line); |
| 1520 | break; |
| 1521 | case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL: |
| 1522 | fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line); |
| 1523 | break; |
| 1524 | case DIFF_SYMBOL_BINARY_DIFF_FOOTER: |
| 1525 | fputs(diff_line_prefix(o), o->file); |
| 1526 | fputc('\n', o->file); |
| 1527 | break; |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1528 | case DIFF_SYMBOL_REWRITE_DIFF: |
| 1529 | fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO); |
| 1530 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1531 | emit_line(o, fraginfo, reset, line, len); |
| 1532 | break; |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 1533 | case DIFF_SYMBOL_SUBMODULE_ADD: |
| 1534 | set = diff_get_color_opt(o, DIFF_FILE_NEW); |
| 1535 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1536 | emit_line(o, set, reset, line, len); |
| 1537 | break; |
| 1538 | case DIFF_SYMBOL_SUBMODULE_DEL: |
| 1539 | set = diff_get_color_opt(o, DIFF_FILE_OLD); |
| 1540 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1541 | emit_line(o, set, reset, line, len); |
| 1542 | break; |
| 1543 | case DIFF_SYMBOL_SUBMODULE_UNTRACKED: |
| 1544 | fprintf(o->file, "%sSubmodule %s contains untracked content\n", |
| 1545 | diff_line_prefix(o), line); |
| 1546 | break; |
| 1547 | case DIFF_SYMBOL_SUBMODULE_MODIFIED: |
| 1548 | fprintf(o->file, "%sSubmodule %s contains modified content\n", |
| 1549 | diff_line_prefix(o), line); |
| 1550 | break; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 1551 | case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES: |
| 1552 | emit_line(o, "", "", " 0 files changed\n", |
| 1553 | strlen(" 0 files changed\n")); |
| 1554 | break; |
| 1555 | case DIFF_SYMBOL_STATS_SUMMARY_ABBREV: |
| 1556 | emit_line(o, "", "", " ...\n", strlen(" ...\n")); |
| 1557 | break; |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1558 | case DIFF_SYMBOL_WORD_DIFF: |
| 1559 | fprintf(o->file, "%.*s", len, line); |
| 1560 | break; |
Stefan Beller | 30b7e1e | 2017-06-29 17:07:04 -0700 | [diff] [blame] | 1561 | case DIFF_SYMBOL_STAT_SEP: |
| 1562 | fputs(o->stat_sep, o->file); |
| 1563 | break; |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1564 | default: |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 1565 | BUG("unknown diff symbol"); |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1566 | } |
| 1567 | } |
| 1568 | |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1569 | static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, |
| 1570 | const char *line, int len, unsigned flags) |
| 1571 | { |
Phillip Wood | b4a5c5c | 2021-12-09 10:30:08 +0000 | [diff] [blame] | 1572 | struct emitted_diff_symbol e = { |
| 1573 | .line = line, .len = len, .flags = flags, .s = s |
| 1574 | }; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1575 | |
| 1576 | if (o->emitted_symbols) |
| 1577 | append_emitted_diff_symbol(o, &e); |
| 1578 | else |
| 1579 | emit_diff_symbol_from_struct(o, &e); |
| 1580 | } |
| 1581 | |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 1582 | void diff_emit_submodule_del(struct diff_options *o, const char *line) |
| 1583 | { |
| 1584 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0); |
| 1585 | } |
| 1586 | |
| 1587 | void diff_emit_submodule_add(struct diff_options *o, const char *line) |
| 1588 | { |
| 1589 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0); |
| 1590 | } |
| 1591 | |
| 1592 | void diff_emit_submodule_untracked(struct diff_options *o, const char *path) |
| 1593 | { |
| 1594 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED, |
| 1595 | path, strlen(path), 0); |
| 1596 | } |
| 1597 | |
| 1598 | void diff_emit_submodule_modified(struct diff_options *o, const char *path) |
| 1599 | { |
| 1600 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED, |
| 1601 | path, strlen(path), 0); |
| 1602 | } |
| 1603 | |
| 1604 | void diff_emit_submodule_header(struct diff_options *o, const char *header) |
| 1605 | { |
| 1606 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER, |
| 1607 | header, strlen(header), 0); |
| 1608 | } |
| 1609 | |
| 1610 | void diff_emit_submodule_error(struct diff_options *o, const char *err) |
| 1611 | { |
| 1612 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0); |
| 1613 | } |
| 1614 | |
| 1615 | void diff_emit_submodule_pipethrough(struct diff_options *o, |
| 1616 | const char *line, int len) |
| 1617 | { |
| 1618 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0); |
| 1619 | } |
| 1620 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1621 | static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len) |
| 1622 | { |
| 1623 | if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) && |
| 1624 | ecbdata->blank_at_eof_in_preimage && |
| 1625 | ecbdata->blank_at_eof_in_postimage && |
| 1626 | ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage && |
| 1627 | ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage)) |
| 1628 | return 0; |
Jeff King | c5224f0 | 2022-12-13 06:12:58 -0500 | [diff] [blame] | 1629 | return ws_blank_line(line, len); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1632 | static void emit_add_line(struct emit_callback *ecbdata, |
Junio C Hamano | 018cff7 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1633 | const char *line, int len) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1634 | { |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1635 | unsigned flags = WSEH_NEW | ecbdata->ws_rule; |
| 1636 | if (new_blank_line_at_eof(ecbdata, line, len)) |
| 1637 | flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF; |
| 1638 | |
| 1639 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1640 | } |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1641 | |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1642 | static void emit_del_line(struct emit_callback *ecbdata, |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 1643 | const char *line, int len) |
| 1644 | { |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1645 | unsigned flags = WSEH_OLD | ecbdata->ws_rule; |
| 1646 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1649 | static void emit_context_line(struct emit_callback *ecbdata, |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 1650 | const char *line, int len) |
| 1651 | { |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1652 | unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule; |
| 1653 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1654 | } |
| 1655 | |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1656 | static void emit_hunk_header(struct emit_callback *ecbdata, |
| 1657 | const char *line, int len) |
| 1658 | { |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 1659 | const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT); |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1660 | const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO); |
| 1661 | const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO); |
| 1662 | const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1663 | const char *reverse = ecbdata->color_diff ? GIT_COLOR_REVERSE : ""; |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1664 | static const char atat[2] = { '@', '@' }; |
| 1665 | const char *cp, *ep; |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1666 | struct strbuf msgbuf = STRBUF_INIT; |
| 1667 | int org_len = len; |
| 1668 | int i = 1; |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1669 | |
| 1670 | /* |
| 1671 | * As a hunk header must begin with "@@ -<old>, +<new> @@", |
| 1672 | * it always is at least 10 bytes long. |
| 1673 | */ |
| 1674 | if (len < 10 || |
| 1675 | memcmp(line, atat, 2) || |
| 1676 | !(ep = memmem(line + 2, len - 2, atat, 2))) { |
Stefan Beller | c64b420 | 2017-06-29 17:06:50 -0700 | [diff] [blame] | 1677 | emit_diff_symbol(ecbdata->opt, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1678 | DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0); |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1679 | return; |
| 1680 | } |
| 1681 | ep += 2; /* skip over @@ */ |
| 1682 | |
| 1683 | /* The hunk header in fraginfo color */ |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1684 | if (ecbdata->opt->flags.dual_color_diffed_diffs) |
| 1685 | strbuf_addstr(&msgbuf, reverse); |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1686 | strbuf_addstr(&msgbuf, frag); |
Thomas Gummerer | 430be36 | 2019-07-11 17:08:48 +0100 | [diff] [blame] | 1687 | if (ecbdata->opt->flags.suppress_hunk_header_line_count) |
| 1688 | strbuf_add(&msgbuf, atat, sizeof(atat)); |
| 1689 | else |
| 1690 | strbuf_add(&msgbuf, line, ep - line); |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1691 | strbuf_addstr(&msgbuf, reset); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1692 | |
| 1693 | /* |
| 1694 | * trailing "\r\n" |
| 1695 | */ |
| 1696 | for ( ; i < 3; i++) |
| 1697 | if (line[len - i] == '\r' || line[len - i] == '\n') |
| 1698 | len--; |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1699 | |
| 1700 | /* blank before the func header */ |
| 1701 | for (cp = ep; ep - line < len; ep++) |
| 1702 | if (*ep != ' ' && *ep != '\t') |
| 1703 | break; |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1704 | if (ep != cp) { |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 1705 | strbuf_addstr(&msgbuf, context); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1706 | strbuf_add(&msgbuf, cp, ep - cp); |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1707 | strbuf_addstr(&msgbuf, reset); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1708 | } |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1709 | |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1710 | if (ep < line + len) { |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1711 | strbuf_addstr(&msgbuf, func); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1712 | strbuf_add(&msgbuf, ep, line + len - ep); |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1713 | strbuf_addstr(&msgbuf, reset); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | strbuf_add(&msgbuf, line + len, org_len - len); |
Stefan Beller | dfb7728 | 2017-06-29 17:06:47 -0700 | [diff] [blame] | 1717 | strbuf_complete_line(&msgbuf); |
Stefan Beller | 68abc6f | 2017-06-29 17:06:51 -0700 | [diff] [blame] | 1718 | emit_diff_symbol(ecbdata->opt, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1719 | DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1720 | strbuf_release(&msgbuf); |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1721 | } |
| 1722 | |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 1723 | static struct diff_tempfile *claim_diff_tempfile(void) |
| 1724 | { |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 1725 | int i; |
| 1726 | for (i = 0; i < ARRAY_SIZE(diff_temp); i++) |
| 1727 | if (!diff_temp[i].name) |
| 1728 | return diff_temp + i; |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 1729 | BUG("diff is failing to clean up its tempfiles"); |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 1730 | } |
| 1731 | |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 1732 | static void remove_tempfile(void) |
| 1733 | { |
| 1734 | int i; |
Nazri Ramliy | a8344ab | 2009-02-12 21:36:14 +0800 | [diff] [blame] | 1735 | for (i = 0; i < ARRAY_SIZE(diff_temp); i++) { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 1736 | if (is_tempfile_active(diff_temp[i].tempfile)) |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 1737 | delete_tempfile(&diff_temp[i].tempfile); |
Nazri Ramliy | a8344ab | 2009-02-12 21:36:14 +0800 | [diff] [blame] | 1738 | diff_temp[i].name = NULL; |
| 1739 | } |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 1740 | } |
| 1741 | |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1742 | static void add_line_count(struct strbuf *out, int count) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1743 | { |
| 1744 | switch (count) { |
| 1745 | case 0: |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1746 | strbuf_addstr(out, "0,0"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1747 | break; |
| 1748 | case 1: |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1749 | strbuf_addstr(out, "1"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1750 | break; |
| 1751 | default: |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1752 | strbuf_addf(out, "1,%d", count); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1753 | break; |
| 1754 | } |
| 1755 | } |
| 1756 | |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1757 | static void emit_rewrite_lines(struct emit_callback *ecb, |
| 1758 | int prefix, const char *data, int size) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1759 | { |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1760 | const char *endp = NULL; |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1761 | |
| 1762 | while (0 < size) { |
| 1763 | int len; |
| 1764 | |
| 1765 | endp = memchr(data, '\n', size); |
| 1766 | len = endp ? (endp - data + 1) : size; |
| 1767 | if (prefix != '+') { |
| 1768 | ecb->lno_in_preimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1769 | emit_del_line(ecb, data, len); |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1770 | } else { |
| 1771 | ecb->lno_in_postimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1772 | emit_add_line(ecb, data, len); |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 1773 | } |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1774 | size -= len; |
| 1775 | data += len; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1776 | } |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 1777 | if (!endp) |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1778 | emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | static void emit_rewrite_diff(const char *name_a, |
| 1782 | const char *name_b, |
| 1783 | struct diff_filespec *one, |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 1784 | struct diff_filespec *two, |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 1785 | struct userdiff_driver *textconv_one, |
| 1786 | struct userdiff_driver *textconv_two, |
Johannes Schindelin | eab9a40 | 2007-12-18 19:32:14 +0000 | [diff] [blame] | 1787 | struct diff_options *o) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1788 | { |
| 1789 | int lc_a, lc_b; |
Junio C Hamano | d562509 | 2007-12-26 17:13:36 -0800 | [diff] [blame] | 1790 | static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT; |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 1791 | const char *a_prefix, *b_prefix; |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 1792 | char *data_one, *data_two; |
Jeff King | 3aa1f7c | 2008-12-09 03:13:21 -0500 | [diff] [blame] | 1793 | size_t size_one, size_two; |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1794 | struct emit_callback ecbdata; |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1795 | struct strbuf out = STRBUF_INIT; |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 1796 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 1797 | if (diff_mnemonic_prefix && o->flags.reverse_diff) { |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 1798 | a_prefix = o->b_prefix; |
| 1799 | b_prefix = o->a_prefix; |
| 1800 | } else { |
| 1801 | a_prefix = o->a_prefix; |
| 1802 | b_prefix = o->b_prefix; |
| 1803 | } |
Junio C Hamano | 1a9eb3b | 2006-09-22 16:17:58 -0700 | [diff] [blame] | 1804 | |
Junio C Hamano | 8a13bec | 2007-02-24 01:42:06 -0800 | [diff] [blame] | 1805 | name_a += (*name_a == '/'); |
| 1806 | name_b += (*name_b == '/'); |
Junio C Hamano | 1a9eb3b | 2006-09-22 16:17:58 -0700 | [diff] [blame] | 1807 | |
Junio C Hamano | d562509 | 2007-12-26 17:13:36 -0800 | [diff] [blame] | 1808 | strbuf_reset(&a_name); |
| 1809 | strbuf_reset(&b_name); |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 1810 | quote_two_c_style(&a_name, a_prefix, name_a, 0); |
| 1811 | quote_two_c_style(&b_name, b_prefix, name_b, 0); |
Junio C Hamano | d562509 | 2007-12-26 17:13:36 -0800 | [diff] [blame] | 1812 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 1813 | size_one = fill_textconv(o->repo, textconv_one, one, &data_one); |
| 1814 | size_two = fill_textconv(o->repo, textconv_two, two, &data_two); |
Jeff King | 3aa1f7c | 2008-12-09 03:13:21 -0500 | [diff] [blame] | 1815 | |
Junio C Hamano | d91ba8f | 2009-09-15 11:21:10 -0700 | [diff] [blame] | 1816 | memset(&ecbdata, 0, sizeof(ecbdata)); |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 1817 | ecbdata.color_diff = want_color(o->use_color); |
Nguyễn Thái Ngọc Duy | 26d024e | 2018-09-21 17:57:37 +0200 | [diff] [blame] | 1818 | ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b); |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 1819 | ecbdata.opt = o; |
Junio C Hamano | d91ba8f | 2009-09-15 11:21:10 -0700 | [diff] [blame] | 1820 | if (ecbdata.ws_rule & WS_BLANK_AT_EOF) { |
| 1821 | mmfile_t mf1, mf2; |
| 1822 | mf1.ptr = (char *)data_one; |
| 1823 | mf2.ptr = (char *)data_two; |
| 1824 | mf1.size = size_one; |
| 1825 | mf2.size = size_two; |
| 1826 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
| 1827 | } |
| 1828 | ecbdata.lno_in_preimage = 1; |
| 1829 | ecbdata.lno_in_postimage = 1; |
| 1830 | |
Jeff King | 3aa1f7c | 2008-12-09 03:13:21 -0500 | [diff] [blame] | 1831 | lc_a = count_lines(data_one, size_one); |
| 1832 | lc_b = count_lines(data_two, size_two); |
Stefan Beller | 3ee8b7b | 2017-06-29 17:06:56 -0700 | [diff] [blame] | 1833 | |
| 1834 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS, |
| 1835 | a_name.buf, a_name.len, 0); |
| 1836 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS, |
| 1837 | b_name.buf, b_name.len, 0); |
| 1838 | |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1839 | strbuf_addstr(&out, "@@ -"); |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 1840 | if (!o->irreversible_delete) |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1841 | add_line_count(&out, lc_a); |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 1842 | else |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1843 | strbuf_addstr(&out, "?,?"); |
| 1844 | strbuf_addstr(&out, " +"); |
| 1845 | add_line_count(&out, lc_b); |
| 1846 | strbuf_addstr(&out, " @@\n"); |
| 1847 | emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0); |
| 1848 | strbuf_release(&out); |
| 1849 | |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 1850 | if (lc_a && !o->irreversible_delete) |
Junio C Hamano | d91ba8f | 2009-09-15 11:21:10 -0700 | [diff] [blame] | 1851 | emit_rewrite_lines(&ecbdata, '-', data_one, size_one); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1852 | if (lc_b) |
Junio C Hamano | d91ba8f | 2009-09-15 11:21:10 -0700 | [diff] [blame] | 1853 | emit_rewrite_lines(&ecbdata, '+', data_two, size_two); |
Jeff King | b76c056 | 2010-04-01 20:04:14 -0400 | [diff] [blame] | 1854 | if (textconv_one) |
Junio C Hamano | aed6ca5 | 2010-04-08 23:30:49 -0700 | [diff] [blame] | 1855 | free((char *)data_one); |
Jeff King | b76c056 | 2010-04-01 20:04:14 -0400 | [diff] [blame] | 1856 | if (textconv_two) |
Junio C Hamano | aed6ca5 | 2010-04-08 23:30:49 -0700 | [diff] [blame] | 1857 | free((char *)data_two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1860 | struct diff_words_buffer { |
| 1861 | mmfile_t text; |
Ramsay Jones | 071bcaa | 2017-09-21 17:49:38 +0100 | [diff] [blame] | 1862 | unsigned long alloc; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 1863 | struct diff_words_orig { |
| 1864 | const char *begin, *end; |
| 1865 | } *orig; |
| 1866 | int orig_nr, orig_alloc; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1867 | }; |
| 1868 | |
| 1869 | static void diff_words_append(char *line, unsigned long len, |
| 1870 | struct diff_words_buffer *buffer) |
| 1871 | { |
Johannes Schindelin | 23c1575 | 2009-01-17 17:29:43 +0100 | [diff] [blame] | 1872 | ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc); |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1873 | line++; |
| 1874 | len--; |
| 1875 | memcpy(buffer->text.ptr + buffer->text.size, line, len); |
| 1876 | buffer->text.size += len; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 1877 | buffer->text.ptr[buffer->text.size] = '\0'; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1878 | } |
| 1879 | |
Jonathan Nieder | 9cba13c | 2011-03-16 02:08:34 -0500 | [diff] [blame] | 1880 | struct diff_words_style_elem { |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1881 | const char *prefix; |
| 1882 | const char *suffix; |
| 1883 | const char *color; /* NULL; filled in by the setup code if |
| 1884 | * color is enabled */ |
| 1885 | }; |
| 1886 | |
Jonathan Nieder | 9cba13c | 2011-03-16 02:08:34 -0500 | [diff] [blame] | 1887 | struct diff_words_style { |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1888 | enum diff_words_type type; |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 1889 | struct diff_words_style_elem new_word, old_word, ctx; |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1890 | const char *newline; |
| 1891 | }; |
| 1892 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 1893 | static struct diff_words_style diff_words_styles[] = { |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1894 | { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" }, |
| 1895 | { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" }, |
| 1896 | { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" } |
| 1897 | }; |
| 1898 | |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1899 | struct diff_words_data { |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1900 | struct diff_words_buffer minus, plus; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 1901 | const char *current_plus; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1902 | int last_minus; |
| 1903 | struct diff_options *opt; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 1904 | regex_t *word_regex; |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1905 | enum diff_words_type type; |
| 1906 | struct diff_words_style *style; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1907 | }; |
| 1908 | |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1909 | static int fn_out_diff_words_write_helper(struct diff_options *o, |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1910 | struct diff_words_style_elem *st_el, |
| 1911 | const char *newline, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1912 | size_t count, const char *buf) |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1913 | { |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1914 | int print = 0; |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1915 | struct strbuf sb = STRBUF_INIT; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1916 | |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1917 | while (count) { |
| 1918 | char *p = memchr(buf, '\n', count); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1919 | if (print) |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1920 | strbuf_addstr(&sb, diff_line_prefix(o)); |
| 1921 | |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1922 | if (p != buf) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1923 | const char *reset = st_el->color && *st_el->color ? |
| 1924 | GIT_COLOR_RESET : NULL; |
| 1925 | if (st_el->color && *st_el->color) |
| 1926 | strbuf_addstr(&sb, st_el->color); |
| 1927 | strbuf_addstr(&sb, st_el->prefix); |
| 1928 | strbuf_add(&sb, buf, p ? p - buf : count); |
| 1929 | strbuf_addstr(&sb, st_el->suffix); |
| 1930 | if (reset) |
| 1931 | strbuf_addstr(&sb, reset); |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1932 | } |
| 1933 | if (!p) |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1934 | goto out; |
| 1935 | |
| 1936 | strbuf_addstr(&sb, newline); |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1937 | count -= p + 1 - buf; |
| 1938 | buf = p + 1; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1939 | print = 1; |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1940 | if (count) { |
| 1941 | emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF, |
| 1942 | sb.buf, sb.len, 0); |
| 1943 | strbuf_reset(&sb); |
| 1944 | } |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1945 | } |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1946 | |
| 1947 | out: |
| 1948 | if (sb.len) |
| 1949 | emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF, |
| 1950 | sb.buf, sb.len, 0); |
| 1951 | strbuf_release(&sb); |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1952 | return 0; |
| 1953 | } |
| 1954 | |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1955 | /* |
| 1956 | * '--color-words' algorithm can be described as: |
| 1957 | * |
René Genz | 5621760 | 2017-04-30 17:48:27 +0200 | [diff] [blame] | 1958 | * 1. collect the minus/plus lines of a diff hunk, divided into |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1959 | * minus-lines and plus-lines; |
| 1960 | * |
| 1961 | * 2. break both minus-lines and plus-lines into words and |
| 1962 | * place them into two mmfile_t with one word for each line; |
| 1963 | * |
| 1964 | * 3. use xdiff to run diff on the two mmfile_t to get the words level diff; |
| 1965 | * |
| 1966 | * And for the common parts of the both file, we output the plus side text. |
| 1967 | * diff_words->current_plus is used to trace the current position of the plus file |
| 1968 | * which printed. diff_words->last_minus is used to trace the last minus word |
| 1969 | * printed. |
| 1970 | * |
| 1971 | * For '--graph' to work with '--color-words', we need to output the graph prefix |
| 1972 | * on each line of color words output. Generally, there are two conditions on |
| 1973 | * which we should output the prefix. |
| 1974 | * |
| 1975 | * 1. diff_words->last_minus == 0 && |
| 1976 | * diff_words->current_plus == diff_words->plus.text.ptr |
| 1977 | * |
| 1978 | * that is: the plus text must start as a new line, and if there is no minus |
| 1979 | * word printed, a graph prefix must be printed. |
| 1980 | * |
| 1981 | * 2. diff_words->current_plus > diff_words->plus.text.ptr && |
| 1982 | * *(diff_words->current_plus - 1) == '\n' |
| 1983 | * |
| 1984 | * that is: a graph prefix must be printed following a '\n' |
| 1985 | */ |
| 1986 | static int color_words_output_graph_prefix(struct diff_words_data *diff_words) |
| 1987 | { |
| 1988 | if ((diff_words->last_minus == 0 && |
| 1989 | diff_words->current_plus == diff_words->plus.text.ptr) || |
| 1990 | (diff_words->current_plus > diff_words->plus.text.ptr && |
| 1991 | *(diff_words->current_plus - 1) == '\n')) { |
| 1992 | return 1; |
| 1993 | } else { |
| 1994 | return 0; |
| 1995 | } |
| 1996 | } |
| 1997 | |
Jeff King | 7c61e25 | 2018-11-02 02:37:18 -0400 | [diff] [blame] | 1998 | static void fn_out_diff_words_aux(void *priv, |
| 1999 | long minus_first, long minus_len, |
| 2000 | long plus_first, long plus_len, |
Jeff King | 61bdc7c | 2022-12-13 06:13:48 -0500 | [diff] [blame] | 2001 | const char *func UNUSED, long funclen UNUSED) |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2002 | { |
| 2003 | struct diff_words_data *diff_words = priv; |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 2004 | struct diff_words_style *style = diff_words->style; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2005 | const char *minus_begin, *minus_end, *plus_begin, *plus_end; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2006 | struct diff_options *opt = diff_words->opt; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2007 | const char *line_prefix; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2008 | |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2009 | assert(opt); |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2010 | line_prefix = diff_line_prefix(opt); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2011 | |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2012 | /* POSIX requires that first be decremented by one if len == 0... */ |
| 2013 | if (minus_len) { |
| 2014 | minus_begin = diff_words->minus.orig[minus_first].begin; |
| 2015 | minus_end = |
| 2016 | diff_words->minus.orig[minus_first + minus_len - 1].end; |
| 2017 | } else |
| 2018 | minus_begin = minus_end = |
| 2019 | diff_words->minus.orig[minus_first].end; |
| 2020 | |
| 2021 | if (plus_len) { |
| 2022 | plus_begin = diff_words->plus.orig[plus_first].begin; |
| 2023 | plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end; |
| 2024 | } else |
| 2025 | plus_begin = plus_end = diff_words->plus.orig[plus_first].end; |
| 2026 | |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2027 | if (color_words_output_graph_prefix(diff_words)) { |
| 2028 | fputs(line_prefix, diff_words->opt->file); |
| 2029 | } |
| 2030 | if (diff_words->current_plus != plus_begin) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2031 | fn_out_diff_words_write_helper(diff_words->opt, |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 2032 | &style->ctx, style->newline, |
| 2033 | plus_begin - diff_words->current_plus, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2034 | diff_words->current_plus); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2035 | } |
| 2036 | if (minus_begin != minus_end) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2037 | fn_out_diff_words_write_helper(diff_words->opt, |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2038 | &style->old_word, style->newline, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2039 | minus_end - minus_begin, minus_begin); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2040 | } |
| 2041 | if (plus_begin != plus_end) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2042 | fn_out_diff_words_write_helper(diff_words->opt, |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2043 | &style->new_word, style->newline, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2044 | plus_end - plus_begin, plus_begin); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2045 | } |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2046 | |
| 2047 | diff_words->current_plus = plus_end; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2048 | diff_words->last_minus = minus_first; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2049 | } |
| 2050 | |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2051 | /* This function starts looking at *begin, and returns 0 iff a word was found. */ |
| 2052 | static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex, |
| 2053 | int *begin, int *end) |
| 2054 | { |
Phillip Wood | 0324e8f | 2021-05-04 09:27:34 +0000 | [diff] [blame] | 2055 | while (word_regex && *begin < buffer->size) { |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2056 | regmatch_t match[1]; |
Johannes Schindelin | b7d36ff | 2016-09-21 20:24:14 +0200 | [diff] [blame] | 2057 | if (!regexec_buf(word_regex, buffer->ptr + *begin, |
| 2058 | buffer->size - *begin, 1, match, 0)) { |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2059 | char *p = memchr(buffer->ptr + *begin + match[0].rm_so, |
| 2060 | '\n', match[0].rm_eo - match[0].rm_so); |
| 2061 | *end = p ? p - buffer->ptr : match[0].rm_eo + *begin; |
| 2062 | *begin += match[0].rm_so; |
Phillip Wood | 0324e8f | 2021-05-04 09:27:34 +0000 | [diff] [blame] | 2063 | if (*begin == *end) |
| 2064 | (*begin)++; |
| 2065 | else |
| 2066 | return *begin > *end; |
| 2067 | } else { |
| 2068 | return -1; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2069 | } |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2070 | } |
| 2071 | |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2072 | /* find the next word */ |
| 2073 | while (*begin < buffer->size && isspace(buffer->ptr[*begin])) |
| 2074 | (*begin)++; |
| 2075 | if (*begin >= buffer->size) |
| 2076 | return -1; |
| 2077 | |
| 2078 | /* find the end of the word */ |
| 2079 | *end = *begin + 1; |
| 2080 | while (*end < buffer->size && !isspace(buffer->ptr[*end])) |
| 2081 | (*end)++; |
| 2082 | |
| 2083 | return 0; |
| 2084 | } |
| 2085 | |
Johannes Schindelin | 23c1575 | 2009-01-17 17:29:43 +0100 | [diff] [blame] | 2086 | /* |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2087 | * This function splits the words in buffer->text, stores the list with |
| 2088 | * newline separator into out, and saves the offsets of the original words |
| 2089 | * in buffer->orig. |
Johannes Schindelin | 23c1575 | 2009-01-17 17:29:43 +0100 | [diff] [blame] | 2090 | */ |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2091 | static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out, |
| 2092 | regex_t *word_regex) |
Johannes Schindelin | 23c1575 | 2009-01-17 17:29:43 +0100 | [diff] [blame] | 2093 | { |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2094 | int i, j; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2095 | long alloc = 0; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2096 | |
| 2097 | out->size = 0; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2098 | out->ptr = NULL; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2099 | |
| 2100 | /* fake an empty "0th" word */ |
| 2101 | ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc); |
| 2102 | buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr; |
| 2103 | buffer->orig_nr = 1; |
| 2104 | |
| 2105 | for (i = 0; i < buffer->text.size; i++) { |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2106 | if (find_word_boundaries(&buffer->text, word_regex, &i, &j)) |
| 2107 | return; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2108 | |
| 2109 | /* store original boundaries */ |
| 2110 | ALLOC_GROW(buffer->orig, buffer->orig_nr + 1, |
| 2111 | buffer->orig_alloc); |
| 2112 | buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i; |
| 2113 | buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j; |
| 2114 | buffer->orig_nr++; |
| 2115 | |
| 2116 | /* store one word */ |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2117 | ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2118 | memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i); |
| 2119 | out->ptr[out->size + j - i] = '\n'; |
| 2120 | out->size += j - i + 1; |
| 2121 | |
| 2122 | i = j - 1; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | /* this executes the word diff on the accumulated buffers */ |
| 2127 | static void diff_words_show(struct diff_words_data *diff_words) |
| 2128 | { |
| 2129 | xpparam_t xpp; |
| 2130 | xdemitconf_t xecfg; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2131 | mmfile_t minus, plus; |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 2132 | struct diff_words_style *style = diff_words->style; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2133 | |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2134 | struct diff_options *opt = diff_words->opt; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2135 | const char *line_prefix; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2136 | |
| 2137 | assert(opt); |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2138 | line_prefix = diff_line_prefix(opt); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2139 | |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2140 | /* special case: only removal */ |
| 2141 | if (!diff_words->plus.text.size) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2142 | emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF, |
| 2143 | line_prefix, strlen(line_prefix), 0); |
| 2144 | fn_out_diff_words_write_helper(diff_words->opt, |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2145 | &style->old_word, style->newline, |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2146 | diff_words->minus.text.size, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2147 | diff_words->minus.text.ptr); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2148 | diff_words->minus.text.size = 0; |
| 2149 | return; |
| 2150 | } |
| 2151 | |
| 2152 | diff_words->current_plus = diff_words->plus.text.ptr; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2153 | diff_words->last_minus = 0; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2154 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 2155 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 2156 | memset(&xecfg, 0, sizeof(xecfg)); |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2157 | diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex); |
| 2158 | diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex); |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 2159 | xpp.flags = 0; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2160 | /* as only the hunk header will be parsed, we need a 0-context */ |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2161 | xecfg.ctxlen = 0; |
Jeff King | 7c61e25 | 2018-11-02 02:37:18 -0400 | [diff] [blame] | 2162 | if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, NULL, |
Jeff King | 9346d6d | 2018-11-02 02:35:45 -0400 | [diff] [blame] | 2163 | diff_words, &xpp, &xecfg)) |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 2164 | die("unable to generate word diff"); |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2165 | free(minus.ptr); |
| 2166 | free(plus.ptr); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2167 | if (diff_words->current_plus != diff_words->plus.text.ptr + |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2168 | diff_words->plus.text.size) { |
| 2169 | if (color_words_output_graph_prefix(diff_words)) |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2170 | emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF, |
| 2171 | line_prefix, strlen(line_prefix), 0); |
| 2172 | fn_out_diff_words_write_helper(diff_words->opt, |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 2173 | &style->ctx, style->newline, |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2174 | diff_words->plus.text.ptr + diff_words->plus.text.size |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2175 | - diff_words->current_plus, diff_words->current_plus); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2176 | } |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2177 | diff_words->minus.text.size = diff_words->plus.text.size = 0; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2178 | } |
| 2179 | |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2180 | /* In "color-words" mode, show word-diff of words accumulated in the buffer */ |
| 2181 | static void diff_words_flush(struct emit_callback *ecbdata) |
| 2182 | { |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 2183 | struct diff_options *wo = ecbdata->diff_words->opt; |
| 2184 | |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2185 | if (ecbdata->diff_words->minus.text.size || |
| 2186 | ecbdata->diff_words->plus.text.size) |
| 2187 | diff_words_show(ecbdata->diff_words); |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 2188 | |
| 2189 | if (wo->emitted_symbols) { |
| 2190 | struct diff_options *o = ecbdata->opt; |
| 2191 | struct emitted_diff_symbols *wol = wo->emitted_symbols; |
| 2192 | int i; |
| 2193 | |
| 2194 | /* |
| 2195 | * NEEDSWORK: |
| 2196 | * Instead of appending each, concat all words to a line? |
| 2197 | */ |
| 2198 | for (i = 0; i < wol->nr; i++) |
| 2199 | append_emitted_diff_symbol(o, &wol->buf[i]); |
| 2200 | |
| 2201 | for (i = 0; i < wol->nr; i++) |
| 2202 | free((void *)wol->buf[i].line); |
| 2203 | |
| 2204 | wol->nr = 0; |
| 2205 | } |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2206 | } |
| 2207 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2208 | static void diff_filespec_load_driver(struct diff_filespec *one, |
| 2209 | struct index_state *istate) |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2210 | { |
| 2211 | /* Use already-loaded driver */ |
| 2212 | if (one->driver) |
| 2213 | return; |
| 2214 | |
| 2215 | if (S_ISREG(one->mode)) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2216 | one->driver = userdiff_find_by_path(istate, one->path); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2217 | |
| 2218 | /* Fallback to default settings */ |
| 2219 | if (!one->driver) |
| 2220 | one->driver = userdiff_find_by_name("default"); |
| 2221 | } |
| 2222 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2223 | static const char *userdiff_word_regex(struct diff_filespec *one, |
| 2224 | struct index_state *istate) |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2225 | { |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2226 | diff_filespec_load_driver(one, istate); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2227 | return one->driver->word_regex; |
| 2228 | } |
| 2229 | |
| 2230 | static void init_diff_words_data(struct emit_callback *ecbdata, |
Thomas Rast | 6440d34 | 2012-03-14 19:24:09 +0100 | [diff] [blame] | 2231 | struct diff_options *orig_opts, |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2232 | struct diff_filespec *one, |
| 2233 | struct diff_filespec *two) |
| 2234 | { |
| 2235 | int i; |
Thomas Rast | 6440d34 | 2012-03-14 19:24:09 +0100 | [diff] [blame] | 2236 | struct diff_options *o = xmalloc(sizeof(struct diff_options)); |
| 2237 | memcpy(o, orig_opts, sizeof(struct diff_options)); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2238 | |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 2239 | CALLOC_ARRAY(ecbdata->diff_words, 1); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2240 | ecbdata->diff_words->type = o->word_diff; |
| 2241 | ecbdata->diff_words->opt = o; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 2242 | |
| 2243 | if (orig_opts->emitted_symbols) |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 2244 | CALLOC_ARRAY(o->emitted_symbols, 1); |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 2245 | |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2246 | if (!o->word_regex) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2247 | o->word_regex = userdiff_word_regex(one, o->repo->index); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2248 | if (!o->word_regex) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2249 | o->word_regex = userdiff_word_regex(two, o->repo->index); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2250 | if (!o->word_regex) |
| 2251 | o->word_regex = diff_word_regex_cfg; |
| 2252 | if (o->word_regex) { |
| 2253 | ecbdata->diff_words->word_regex = (regex_t *) |
| 2254 | xmalloc(sizeof(regex_t)); |
| 2255 | if (regcomp(ecbdata->diff_words->word_regex, |
| 2256 | o->word_regex, |
| 2257 | REG_EXTENDED | REG_NEWLINE)) |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 2258 | die("invalid regular expression: %s", |
| 2259 | o->word_regex); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2260 | } |
| 2261 | for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) { |
| 2262 | if (o->word_diff == diff_words_styles[i].type) { |
| 2263 | ecbdata->diff_words->style = |
| 2264 | &diff_words_styles[i]; |
| 2265 | break; |
| 2266 | } |
| 2267 | } |
| 2268 | if (want_color(o->use_color)) { |
| 2269 | struct diff_words_style *st = ecbdata->diff_words->style; |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2270 | st->old_word.color = diff_get_color_opt(o, DIFF_FILE_OLD); |
| 2271 | st->new_word.color = diff_get_color_opt(o, DIFF_FILE_NEW); |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 2272 | st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2273 | } |
| 2274 | } |
| 2275 | |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2276 | static void free_diff_words_data(struct emit_callback *ecbdata) |
| 2277 | { |
| 2278 | if (ecbdata->diff_words) { |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2279 | diff_words_flush(ecbdata); |
Ævar Arnfjörð Bjarmason | 77e56d5 | 2022-03-17 15:55:35 +0100 | [diff] [blame] | 2280 | free_emitted_diff_symbols(ecbdata->diff_words->opt->emitted_symbols); |
Thomas Rast | 6440d34 | 2012-03-14 19:24:09 +0100 | [diff] [blame] | 2281 | free (ecbdata->diff_words->opt); |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 2282 | free (ecbdata->diff_words->minus.text.ptr); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2283 | free (ecbdata->diff_words->minus.orig); |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 2284 | free (ecbdata->diff_words->plus.text.ptr); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2285 | free (ecbdata->diff_words->plus.orig); |
Brandon Casey | ef5644e | 2010-09-09 14:02:45 -0500 | [diff] [blame] | 2286 | if (ecbdata->diff_words->word_regex) { |
| 2287 | regfree(ecbdata->diff_words->word_regex); |
| 2288 | free(ecbdata->diff_words->word_regex); |
| 2289 | } |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 2290 | FREE_AND_NULL(ecbdata->diff_words); |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2291 | } |
| 2292 | } |
| 2293 | |
Jeff King | ce43697 | 2006-07-23 05:24:18 -0400 | [diff] [blame] | 2294 | const char *diff_get_color(int diff_use_color, enum color_diff ix) |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2295 | { |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 2296 | if (want_color(diff_use_color)) |
Linus Torvalds | 50f575f | 2006-06-22 13:53:31 -0700 | [diff] [blame] | 2297 | return diff_colors[ix]; |
| 2298 | return ""; |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2299 | } |
| 2300 | |
John Keeping | f192223 | 2013-02-07 20:15:26 +0000 | [diff] [blame] | 2301 | const char *diff_line_prefix(struct diff_options *opt) |
| 2302 | { |
| 2303 | struct strbuf *msgbuf; |
| 2304 | if (!opt->output_prefix) |
| 2305 | return ""; |
| 2306 | |
| 2307 | msgbuf = opt->output_prefix(opt, opt->output_prefix_data); |
| 2308 | return msgbuf->buf; |
| 2309 | } |
| 2310 | |
Jeff King | 19b9046 | 2019-02-14 00:48:27 -0500 | [diff] [blame] | 2311 | static unsigned long sane_truncate_line(char *line, unsigned long len) |
Junio C Hamano | 2370781 | 2008-01-02 01:50:11 -0800 | [diff] [blame] | 2312 | { |
| 2313 | const char *cp; |
| 2314 | unsigned long allot; |
| 2315 | size_t l = len; |
| 2316 | |
Junio C Hamano | 2370781 | 2008-01-02 01:50:11 -0800 | [diff] [blame] | 2317 | cp = line; |
| 2318 | allot = l; |
| 2319 | while (0 < l) { |
| 2320 | (void) utf8_width(&cp, &l); |
| 2321 | if (!cp) |
| 2322 | break; /* truncated in the middle? */ |
| 2323 | } |
| 2324 | return allot - l; |
| 2325 | } |
| 2326 | |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2327 | static void find_lno(const char *line, struct emit_callback *ecbdata) |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 2328 | { |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2329 | const char *p; |
| 2330 | ecbdata->lno_in_preimage = 0; |
| 2331 | ecbdata->lno_in_postimage = 0; |
| 2332 | p = strchr(line, '-'); |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 2333 | if (!p) |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2334 | return; /* cannot happen */ |
| 2335 | ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10); |
| 2336 | p = strchr(p, '+'); |
| 2337 | if (!p) |
| 2338 | return; /* cannot happen */ |
| 2339 | ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10); |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 2340 | } |
| 2341 | |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2342 | static int fn_out_consume(void *priv, char *line, unsigned long len) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2343 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2344 | struct emit_callback *ecbdata = priv; |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 2345 | struct diff_options *o = ecbdata->opt; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2346 | |
Stefan Beller | ba16233 | 2016-09-07 16:36:46 -0700 | [diff] [blame] | 2347 | o->found_changes = 1; |
| 2348 | |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 2349 | if (ecbdata->header) { |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 2350 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 2351 | ecbdata->header->buf, ecbdata->header->len, 0); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 2352 | strbuf_reset(ecbdata->header); |
| 2353 | ecbdata->header = NULL; |
| 2354 | } |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 2355 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2356 | if (ecbdata->label_path[0]) { |
Stefan Beller | 3ee8b7b | 2017-06-29 17:06:56 -0700 | [diff] [blame] | 2357 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS, |
| 2358 | ecbdata->label_path[0], |
| 2359 | strlen(ecbdata->label_path[0]), 0); |
| 2360 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS, |
| 2361 | ecbdata->label_path[1], |
| 2362 | strlen(ecbdata->label_path[1]), 0); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2363 | ecbdata->label_path[0] = ecbdata->label_path[1] = NULL; |
| 2364 | } |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2365 | |
Jim Meyering | a624eaa | 2008-08-15 13:39:26 +0200 | [diff] [blame] | 2366 | if (diff_suppress_blank_empty |
| 2367 | && len == 2 && line[0] == ' ' && line[1] == '\n') { |
| 2368 | line[0] = '\n'; |
| 2369 | len = 1; |
| 2370 | } |
| 2371 | |
Junio C Hamano | b8d9c1a | 2009-09-03 23:59:25 -0700 | [diff] [blame] | 2372 | if (line[0] == '@') { |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2373 | if (ecbdata->diff_words) |
| 2374 | diff_words_flush(ecbdata); |
Jeff King | 19b9046 | 2019-02-14 00:48:27 -0500 | [diff] [blame] | 2375 | len = sane_truncate_line(line, len); |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2376 | find_lno(line, ecbdata); |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 2377 | emit_hunk_header(ecbdata, line, len); |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2378 | return 0; |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2379 | } |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2380 | |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2381 | if (ecbdata->diff_words) { |
Stefan Beller | ff95867 | 2017-06-29 17:06:54 -0700 | [diff] [blame] | 2382 | enum diff_symbol s = |
| 2383 | ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ? |
| 2384 | DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2385 | if (line[0] == '-') { |
| 2386 | diff_words_append(line, len, |
| 2387 | &ecbdata->diff_words->minus); |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2388 | return 0; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2389 | } else if (line[0] == '+') { |
| 2390 | diff_words_append(line, len, |
| 2391 | &ecbdata->diff_words->plus); |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2392 | return 0; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 2393 | } else if (starts_with(line, "\\ ")) { |
Thomas Rast | c7c2bc0 | 2012-01-12 12:15:33 +0100 | [diff] [blame] | 2394 | /* |
| 2395 | * Eat the "no newline at eof" marker as if we |
| 2396 | * saw a "+" or "-" line with nothing on it, |
| 2397 | * and return without diff_words_flush() to |
| 2398 | * defer processing. If this is the end of |
| 2399 | * preimage, more "+" lines may come after it. |
| 2400 | */ |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2401 | return 0; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2402 | } |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2403 | diff_words_flush(ecbdata); |
Stefan Beller | ff95867 | 2017-06-29 17:06:54 -0700 | [diff] [blame] | 2404 | emit_diff_symbol(o, s, line, len, 0); |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2405 | return 0; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2406 | } |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2407 | |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2408 | switch (line[0]) { |
| 2409 | case '+': |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2410 | ecbdata->lno_in_postimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 2411 | emit_add_line(ecbdata, line + 1, len - 1); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2412 | break; |
| 2413 | case '-': |
| 2414 | ecbdata->lno_in_preimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 2415 | emit_del_line(ecbdata, line + 1, len - 1); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2416 | break; |
| 2417 | case ' ': |
| 2418 | ecbdata->lno_in_postimage++; |
| 2419 | ecbdata->lno_in_preimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 2420 | emit_context_line(ecbdata, line + 1, len - 1); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2421 | break; |
| 2422 | default: |
| 2423 | /* incomplete line at the end */ |
| 2424 | ecbdata->lno_in_preimage++; |
Stefan Beller | f2bb121 | 2017-06-29 17:06:55 -0700 | [diff] [blame] | 2425 | emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE, |
| 2426 | line, len, 0); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2427 | break; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2428 | } |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2429 | return 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2430 | } |
| 2431 | |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2432 | static void pprint_rename(struct strbuf *name, const char *a, const char *b) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2433 | { |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2434 | const char *old_name = a; |
| 2435 | const char *new_name = b; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2436 | int pfx_length, sfx_length; |
Thomas Rast | dd281f0 | 2013-02-26 21:47:01 +0100 | [diff] [blame] | 2437 | int pfx_adjust_for_slash; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2438 | int len_a = strlen(a); |
| 2439 | int len_b = strlen(b); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2440 | int a_midlen, b_midlen; |
Alexandre Julliard | e5bfbf9 | 2007-02-10 15:39:00 +0100 | [diff] [blame] | 2441 | int qlen_a = quote_c_style(a, NULL, NULL, 0); |
| 2442 | int qlen_b = quote_c_style(b, NULL, NULL, 0); |
| 2443 | |
| 2444 | if (qlen_a || qlen_b) { |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2445 | quote_c_style(a, name, NULL, 0); |
| 2446 | strbuf_addstr(name, " => "); |
| 2447 | quote_c_style(b, name, NULL, 0); |
| 2448 | return; |
Alexandre Julliard | e5bfbf9 | 2007-02-10 15:39:00 +0100 | [diff] [blame] | 2449 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2450 | |
| 2451 | /* Find common prefix */ |
| 2452 | pfx_length = 0; |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2453 | while (*old_name && *new_name && *old_name == *new_name) { |
| 2454 | if (*old_name == '/') |
| 2455 | pfx_length = old_name - a + 1; |
| 2456 | old_name++; |
| 2457 | new_name++; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | /* Find common suffix */ |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2461 | old_name = a + len_a; |
| 2462 | new_name = b + len_b; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2463 | sfx_length = 0; |
Antoine Pelisse | d020e27 | 2013-02-23 17:48:45 +0100 | [diff] [blame] | 2464 | /* |
Thomas Rast | dd281f0 | 2013-02-26 21:47:01 +0100 | [diff] [blame] | 2465 | * If there is a common prefix, it must end in a slash. In |
| 2466 | * that case we let this loop run 1 into the prefix to see the |
| 2467 | * same slash. |
| 2468 | * |
| 2469 | * If there is no common prefix, we cannot do this as it would |
| 2470 | * underrun the input strings. |
Antoine Pelisse | d020e27 | 2013-02-23 17:48:45 +0100 | [diff] [blame] | 2471 | */ |
Thomas Rast | dd281f0 | 2013-02-26 21:47:01 +0100 | [diff] [blame] | 2472 | pfx_adjust_for_slash = (pfx_length ? 1 : 0); |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2473 | while (a + pfx_length - pfx_adjust_for_slash <= old_name && |
| 2474 | b + pfx_length - pfx_adjust_for_slash <= new_name && |
| 2475 | *old_name == *new_name) { |
| 2476 | if (*old_name == '/') |
| 2477 | sfx_length = len_a - (old_name - a); |
| 2478 | old_name--; |
| 2479 | new_name--; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | /* |
| 2483 | * pfx{mid-a => mid-b}sfx |
| 2484 | * {pfx-a => pfx-b}sfx |
| 2485 | * pfx{sfx-a => sfx-b} |
| 2486 | * name-a => name-b |
| 2487 | */ |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2488 | a_midlen = len_a - pfx_length - sfx_length; |
| 2489 | b_midlen = len_b - pfx_length - sfx_length; |
| 2490 | if (a_midlen < 0) |
| 2491 | a_midlen = 0; |
| 2492 | if (b_midlen < 0) |
| 2493 | b_midlen = 0; |
Junio C Hamano | cc908b8 | 2006-05-14 22:07:28 -0700 | [diff] [blame] | 2494 | |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2495 | strbuf_grow(name, pfx_length + a_midlen + b_midlen + sfx_length + 7); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2496 | if (pfx_length + sfx_length) { |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2497 | strbuf_add(name, a, pfx_length); |
| 2498 | strbuf_addch(name, '{'); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2499 | } |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2500 | strbuf_add(name, a + pfx_length, a_midlen); |
| 2501 | strbuf_addstr(name, " => "); |
| 2502 | strbuf_add(name, b + pfx_length, b_midlen); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2503 | if (pfx_length + sfx_length) { |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2504 | strbuf_addch(name, '}'); |
| 2505 | strbuf_add(name, a + len_a - sfx_length, sfx_length); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2506 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2507 | } |
| 2508 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2509 | static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat, |
| 2510 | const char *name_a, |
| 2511 | const char *name_b) |
| 2512 | { |
| 2513 | struct diffstat_file *x; |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 2514 | CALLOC_ARRAY(x, 1); |
Dmitry S. Dolzhenko | 4c960a4 | 2014-03-04 02:31:53 +0400 | [diff] [blame] | 2515 | ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2516 | diffstat->files[diffstat->nr++] = x; |
| 2517 | if (name_b) { |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2518 | x->from_name = xstrdup(name_a); |
| 2519 | x->name = xstrdup(name_b); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2520 | x->is_renamed = 1; |
| 2521 | } |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2522 | else { |
| 2523 | x->from_name = NULL; |
Shawn Pearce | 9befac4 | 2006-09-02 00:16:31 -0400 | [diff] [blame] | 2524 | x->name = xstrdup(name_a); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2525 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2526 | return x; |
| 2527 | } |
| 2528 | |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2529 | static int diffstat_consume(void *priv, char *line, unsigned long len) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2530 | { |
| 2531 | struct diffstat_t *diffstat = priv; |
| 2532 | struct diffstat_file *x = diffstat->files[diffstat->nr - 1]; |
| 2533 | |
Jeff King | 0e5a87e | 2022-10-17 21:01:17 -0400 | [diff] [blame] | 2534 | if (!len) |
| 2535 | BUG("xdiff fed us an empty line"); |
| 2536 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2537 | if (line[0] == '+') |
| 2538 | x->added++; |
| 2539 | else if (line[0] == '-') |
| 2540 | x->deleted++; |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2541 | return 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2542 | } |
| 2543 | |
Johannes Schindelin | 698ce6f | 2006-05-20 15:40:29 +0200 | [diff] [blame] | 2544 | const char mime_boundary_leader[] = "------------"; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2545 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2546 | static int scale_linear(int it, int width, int max_change) |
| 2547 | { |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2548 | if (!it) |
| 2549 | return 0; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2550 | /* |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2551 | * make sure that at least one '-' or '+' is printed if |
| 2552 | * there is any change to this path. The easiest way is to |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 2553 | * scale linearly as if the allotted width is one column shorter |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2554 | * than it is, and then add 1 to the result. |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2555 | */ |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2556 | return 1 + (it * (width - 1) / max_change); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2557 | } |
| 2558 | |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2559 | static void show_graph(struct strbuf *out, char ch, int cnt, |
| 2560 | const char *set, const char *reset) |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2561 | { |
| 2562 | if (cnt <= 0) |
| 2563 | return; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2564 | strbuf_addstr(out, set); |
| 2565 | strbuf_addchars(out, ch, cnt); |
| 2566 | strbuf_addstr(out, reset); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2567 | } |
| 2568 | |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2569 | static void fill_print_name(struct diffstat_file *file) |
| 2570 | { |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2571 | struct strbuf pname = STRBUF_INIT; |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2572 | |
| 2573 | if (file->print_name) |
| 2574 | return; |
| 2575 | |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2576 | if (file->is_renamed) |
| 2577 | pprint_rename(&pname, file->from_name, file->name); |
| 2578 | else |
| 2579 | quote_c_style(file->name, &pname, NULL, 0); |
| 2580 | |
Nguyễn Thái Ngọc Duy | ddf88fa | 2018-02-24 21:09:59 +0700 | [diff] [blame] | 2581 | if (file->comments) |
| 2582 | strbuf_addf(&pname, " (%s)", file->comments); |
| 2583 | |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2584 | file->print_name = strbuf_detach(&pname, NULL); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2585 | } |
| 2586 | |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2587 | static void print_stat_summary_inserts_deletes(struct diff_options *options, |
| 2588 | int files, int insertions, int deletions) |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2589 | { |
| 2590 | struct strbuf sb = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2591 | |
| 2592 | if (!files) { |
| 2593 | assert(insertions == 0 && deletions == 0); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2594 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES, |
| 2595 | NULL, 0, 0); |
| 2596 | return; |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | strbuf_addf(&sb, |
Nguyễn Thái Ngọc Duy | 218adaa | 2012-09-13 21:16:26 +0700 | [diff] [blame] | 2600 | (files == 1) ? " %d file changed" : " %d files changed", |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2601 | files); |
| 2602 | |
| 2603 | /* |
| 2604 | * For binary diff, the caller may want to print "x files |
| 2605 | * changed" with insertions == 0 && deletions == 0. |
| 2606 | * |
| 2607 | * Not omitting "0 insertions(+), 0 deletions(-)" in this case |
| 2608 | * is probably less confusing (i.e skip over "2 files changed |
| 2609 | * but nothing about added/removed lines? Is this a bug in Git?"). |
| 2610 | */ |
| 2611 | if (insertions || deletions == 0) { |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2612 | strbuf_addf(&sb, |
Nguyễn Thái Ngọc Duy | 218adaa | 2012-09-13 21:16:26 +0700 | [diff] [blame] | 2613 | (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)", |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2614 | insertions); |
| 2615 | } |
| 2616 | |
| 2617 | if (deletions || insertions == 0) { |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2618 | strbuf_addf(&sb, |
Nguyễn Thái Ngọc Duy | 218adaa | 2012-09-13 21:16:26 +0700 | [diff] [blame] | 2619 | (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)", |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2620 | deletions); |
| 2621 | } |
| 2622 | strbuf_addch(&sb, '\n'); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2623 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES, |
| 2624 | sb.buf, sb.len, 0); |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2625 | strbuf_release(&sb); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | void print_stat_summary(FILE *fp, int files, |
| 2629 | int insertions, int deletions) |
| 2630 | { |
| 2631 | struct diff_options o; |
| 2632 | memset(&o, 0, sizeof(o)); |
| 2633 | o.file = fp; |
| 2634 | |
| 2635 | print_stat_summary_inserts_deletes(&o, files, insertions, deletions); |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2636 | } |
| 2637 | |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 2638 | static void show_stats(struct diffstat_t *data, struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2639 | { |
Benjamin Kramer | eb3a9dd | 2009-03-07 21:02:10 +0100 | [diff] [blame] | 2640 | int i, len, add, del, adds = 0, dels = 0; |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2641 | uintmax_t max_change = 0, max_len = 0; |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2642 | int total_files = data->nr, count; |
| 2643 | int width, name_width, graph_width, number_width = 0, bin_width = 0; |
Johannes Schindelin | c0aa335 | 2011-03-22 13:50:08 +0100 | [diff] [blame] | 2644 | const char *reset, *add_c, *del_c; |
Junio C Hamano | e5f85df | 2011-05-27 21:50:39 -0700 | [diff] [blame] | 2645 | int extra_shown = 0; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2646 | const char *line_prefix = diff_line_prefix(options); |
| 2647 | struct strbuf out = STRBUF_INIT; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2648 | |
| 2649 | if (data->nr == 0) |
| 2650 | return; |
| 2651 | |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2652 | count = options->stat_count ? options->stat_count : data->nr; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2653 | |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 2654 | reset = diff_get_color_opt(options, DIFF_RESET); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 2655 | add_c = diff_get_color_opt(options, DIFF_FILE_NEW); |
| 2656 | del_c = diff_get_color_opt(options, DIFF_FILE_OLD); |
Junio C Hamano | 785f743 | 2006-09-26 18:59:41 -0700 | [diff] [blame] | 2657 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2658 | /* |
| 2659 | * Find the longest filename and max number of changes |
| 2660 | */ |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2661 | for (i = 0; (i < count) && (i < data->nr); i++) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2662 | struct diffstat_file *file = data->files[i]; |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2663 | uintmax_t change = file->added + file->deleted; |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2664 | |
| 2665 | if (!file->is_interesting && (change == 0)) { |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2666 | count++; /* not shown == room for one more */ |
Michael J Gruber | 358e460 | 2011-05-27 14:36:40 +0200 | [diff] [blame] | 2667 | continue; |
| 2668 | } |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2669 | fill_print_name(file); |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2670 | len = utf8_strwidth(file->print_name); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2671 | if (max_len < len) |
| 2672 | max_len = len; |
| 2673 | |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2674 | if (file->is_unmerged) { |
| 2675 | /* "Unmerged" is 8 characters */ |
| 2676 | bin_width = bin_width < 8 ? 8 : bin_width; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2677 | continue; |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2678 | } |
| 2679 | if (file->is_binary) { |
| 2680 | /* "Bin XXX -> YYY bytes" */ |
| 2681 | int w = 14 + decimal_width(file->added) |
| 2682 | + decimal_width(file->deleted); |
| 2683 | bin_width = bin_width < w ? w : bin_width; |
| 2684 | /* Display change counts aligned with "Bin" */ |
| 2685 | number_width = 3; |
| 2686 | continue; |
| 2687 | } |
| 2688 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2689 | if (max_change < change) |
| 2690 | max_change = change; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2691 | } |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2692 | count = i; /* where we can stop scanning in data->files[] */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2693 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2694 | /* |
| 2695 | * We have width = stat_width or term_columns() columns total. |
| 2696 | * We want a maximum of min(max_len, stat_name_width) for the name part. |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 2697 | * We want a maximum of min(max_change, stat_graph_width) for the +- part. |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2698 | * We also need 1 for " " and 4 + decimal_width(max_change) |
| 2699 | * for " | NNNN " and one the empty column at the end, altogether |
| 2700 | * 6 + decimal_width(max_change). |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2701 | * |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2702 | * If there's not enough space, we will use the smaller of |
| 2703 | * stat_name_width (if set) and 5/8*width for the filename, |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 2704 | * and the rest for constant elements + graph part, but no more |
| 2705 | * than stat_graph_width for the graph part. |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2706 | * (5/8 gives 50 for filename and 30 for the constant parts + graph |
| 2707 | * for the standard terminal size). |
| 2708 | * |
| 2709 | * In other words: stat_width limits the maximum width, and |
| 2710 | * stat_name_width fixes the maximum width of the filename, |
| 2711 | * and is also used to divide available columns if there |
| 2712 | * aren't enough. |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2713 | * |
| 2714 | * Binary files are displayed with "Bin XXX -> YYY bytes" |
| 2715 | * instead of the change count and graph. This part is treated |
| 2716 | * similarly to the graph part, except that it is not |
Stefano Lattarini | 41ccfdd | 2013-04-12 00:36:10 +0200 | [diff] [blame] | 2717 | * "scaled". If total width is too small to accommodate the |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2718 | * guaranteed minimum width of the filename part and the |
| 2719 | * separators and this message, this message will "overflow" |
| 2720 | * making the line longer than the maximum width. |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2721 | */ |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2722 | |
Junio C Hamano | ce8529b | 2022-10-21 14:53:25 -0700 | [diff] [blame] | 2723 | /* |
| 2724 | * NEEDSWORK: line_prefix is often used for "log --graph" output |
| 2725 | * and contains ANSI-colored string. utf8_strnwidth() should be |
| 2726 | * used to correctly count the display width instead of strlen(). |
| 2727 | */ |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2728 | if (options->stat_width == -1) |
Junio C Hamano | cd48dad | 2016-08-31 16:27:19 -0700 | [diff] [blame] | 2729 | width = term_columns() - strlen(line_prefix); |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2730 | else |
| 2731 | width = options->stat_width ? options->stat_width : 80; |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2732 | number_width = decimal_width(max_change) > number_width ? |
| 2733 | decimal_width(max_change) : number_width; |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2734 | |
Dragan Simic | bd48adc | 2023-09-11 17:39:44 +0200 | [diff] [blame] | 2735 | if (options->stat_name_width == -1) |
| 2736 | options->stat_name_width = diff_stat_name_width; |
Zbigniew Jędrzejewski-Szmek | df44483 | 2012-03-01 13:26:46 +0100 | [diff] [blame] | 2737 | if (options->stat_graph_width == -1) |
| 2738 | options->stat_graph_width = diff_stat_graph_width; |
| 2739 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2740 | /* |
Dragan Simic | bd48adc | 2023-09-11 17:39:44 +0200 | [diff] [blame] | 2741 | * Guarantee 3/8*16 == 6 for the graph part |
| 2742 | * and 5/8*16 == 10 for the filename part |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2743 | */ |
| 2744 | if (width < 16 + 6 + number_width) |
| 2745 | width = 16 + 6 + number_width; |
| 2746 | |
| 2747 | /* |
| 2748 | * First assign sizes that are wanted, ignoring available width. |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2749 | * strlen("Bin XXX -> YYY bytes") == bin_width, and the part |
| 2750 | * starting from "XXX" should fit in graph_width. |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2751 | */ |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2752 | graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4; |
| 2753 | if (options->stat_graph_width && |
| 2754 | options->stat_graph_width < graph_width) |
| 2755 | graph_width = options->stat_graph_width; |
| 2756 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2757 | name_width = (options->stat_name_width > 0 && |
| 2758 | options->stat_name_width < max_len) ? |
| 2759 | options->stat_name_width : max_len; |
| 2760 | |
| 2761 | /* |
| 2762 | * Adjust adjustable widths not to exceed maximum width |
| 2763 | */ |
| 2764 | if (name_width + number_width + 6 + graph_width > width) { |
Lucian Poston | 678c574 | 2012-04-18 14:12:18 -0700 | [diff] [blame] | 2765 | if (graph_width > width * 3/8 - number_width - 6) { |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2766 | graph_width = width * 3/8 - number_width - 6; |
Lucian Poston | 678c574 | 2012-04-18 14:12:18 -0700 | [diff] [blame] | 2767 | if (graph_width < 6) |
| 2768 | graph_width = 6; |
| 2769 | } |
| 2770 | |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 2771 | if (options->stat_graph_width && |
| 2772 | graph_width > options->stat_graph_width) |
| 2773 | graph_width = options->stat_graph_width; |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2774 | if (name_width > width - number_width - 6 - graph_width) |
| 2775 | name_width = width - number_width - 6 - graph_width; |
| 2776 | else |
| 2777 | graph_width = width - number_width - 6 - name_width; |
| 2778 | } |
| 2779 | |
| 2780 | /* |
| 2781 | * From here name_width is the width of the name area, |
| 2782 | * and graph_width is the width of the graph area. |
| 2783 | * max_change is used to scale graph properly. |
| 2784 | */ |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2785 | for (i = 0; i < count; i++) { |
Timo Hirvonen | d2543b8 | 2006-06-24 20:20:32 +0300 | [diff] [blame] | 2786 | const char *prefix = ""; |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2787 | struct diffstat_file *file = data->files[i]; |
| 2788 | char *name = file->print_name; |
| 2789 | uintmax_t added = file->added; |
| 2790 | uintmax_t deleted = file->deleted; |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2791 | int name_len, padding; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2792 | |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2793 | if (!file->is_interesting && (added + deleted == 0)) |
Michael J Gruber | 358e460 | 2011-05-27 14:36:40 +0200 | [diff] [blame] | 2794 | continue; |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2795 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2796 | /* |
| 2797 | * "scale" the filename |
| 2798 | */ |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2799 | len = name_width; |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2800 | name_len = utf8_strwidth(name); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2801 | if (name_width < name_len) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2802 | char *slash; |
| 2803 | prefix = "..."; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2804 | len -= 3; |
Junio C Hamano | ce8529b | 2022-10-21 14:53:25 -0700 | [diff] [blame] | 2805 | /* |
| 2806 | * NEEDSWORK: (name_len - len) counts the display |
| 2807 | * width, which would be shorter than the byte |
| 2808 | * length of the corresponding substring. |
| 2809 | * Advancing "name" by that number of bytes does |
| 2810 | * *NOT* skip over that many columns, so it is |
| 2811 | * very likely that chomping the pathname at the |
| 2812 | * slash we will find starting from "name" will |
| 2813 | * leave the resulting string still too long. |
| 2814 | */ |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2815 | name += name_len - len; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2816 | slash = strchr(name, '/'); |
| 2817 | if (slash) |
| 2818 | name = slash; |
| 2819 | } |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2820 | padding = len - utf8_strwidth(name); |
| 2821 | if (padding < 0) |
| 2822 | padding = 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2823 | |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2824 | if (file->is_binary) { |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2825 | strbuf_addf(&out, " %s%s%*s | %*s", |
| 2826 | prefix, name, padding, "", |
| 2827 | number_width, "Bin"); |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2828 | if (!added && !deleted) { |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2829 | strbuf_addch(&out, '\n'); |
| 2830 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 2831 | out.buf, out.len, 0); |
| 2832 | strbuf_reset(&out); |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2833 | continue; |
| 2834 | } |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2835 | strbuf_addf(&out, " %s%"PRIuMAX"%s", |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2836 | del_c, deleted, reset); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2837 | strbuf_addstr(&out, " -> "); |
| 2838 | strbuf_addf(&out, "%s%"PRIuMAX"%s", |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2839 | add_c, added, reset); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2840 | strbuf_addstr(&out, " bytes\n"); |
| 2841 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 2842 | out.buf, out.len, 0); |
| 2843 | strbuf_reset(&out); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2844 | continue; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2845 | } |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2846 | else if (file->is_unmerged) { |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2847 | strbuf_addf(&out, " %s%s%*s | %*s", |
| 2848 | prefix, name, padding, "", |
Peter Grayson | 209d9cb | 2022-12-14 12:41:51 -0500 | [diff] [blame] | 2849 | number_width, "Unmerged\n"); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2850 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 2851 | out.buf, out.len, 0); |
| 2852 | strbuf_reset(&out); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2853 | continue; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2854 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2855 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2856 | /* |
| 2857 | * scale the add/delete |
| 2858 | */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2859 | add = added; |
| 2860 | del = deleted; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2861 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2862 | if (graph_width <= max_change) { |
Stefan Beller | d3c9cf3 | 2013-07-14 23:35:49 +0200 | [diff] [blame] | 2863 | int total = scale_linear(add + del, graph_width, max_change); |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2864 | if (total < 2 && add && del) |
| 2865 | /* width >= 2 due to the sanity check */ |
| 2866 | total = 2; |
| 2867 | if (add < del) { |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2868 | add = scale_linear(add, graph_width, max_change); |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2869 | del = total - add; |
| 2870 | } else { |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2871 | del = scale_linear(del, graph_width, max_change); |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2872 | add = total - del; |
| 2873 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2874 | } |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2875 | strbuf_addf(&out, " %s%s%*s | %*"PRIuMAX"%s", |
| 2876 | prefix, name, padding, "", |
| 2877 | number_width, added + deleted, |
| 2878 | added + deleted ? " " : ""); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2879 | show_graph(&out, '+', add, add_c, reset); |
| 2880 | show_graph(&out, '-', del, del_c, reset); |
| 2881 | strbuf_addch(&out, '\n'); |
| 2882 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 2883 | out.buf, out.len, 0); |
| 2884 | strbuf_reset(&out); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2885 | } |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2886 | |
| 2887 | for (i = 0; i < data->nr; i++) { |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2888 | struct diffstat_file *file = data->files[i]; |
| 2889 | uintmax_t added = file->added; |
| 2890 | uintmax_t deleted = file->deleted; |
Junio C Hamano | 82dfc2c | 2012-11-27 12:05:10 -0800 | [diff] [blame] | 2891 | |
| 2892 | if (file->is_unmerged || |
| 2893 | (!file->is_interesting && (added + deleted == 0))) { |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2894 | total_files--; |
| 2895 | continue; |
| 2896 | } |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2897 | |
Junio C Hamano | 82dfc2c | 2012-11-27 12:05:10 -0800 | [diff] [blame] | 2898 | if (!file->is_binary) { |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2899 | adds += added; |
| 2900 | dels += deleted; |
| 2901 | } |
| 2902 | if (i < count) |
| 2903 | continue; |
Junio C Hamano | e5f85df | 2011-05-27 21:50:39 -0700 | [diff] [blame] | 2904 | if (!extra_shown) |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2905 | emit_diff_symbol(options, |
| 2906 | DIFF_SYMBOL_STATS_SUMMARY_ABBREV, |
| 2907 | NULL, 0, 0); |
Junio C Hamano | e5f85df | 2011-05-27 21:50:39 -0700 | [diff] [blame] | 2908 | extra_shown = 1; |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2909 | } |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2910 | |
| 2911 | print_stat_summary_inserts_deletes(options, total_files, adds, dels); |
Rene Scharfe | 5a61201 | 2017-08-30 19:49:43 +0200 | [diff] [blame] | 2912 | strbuf_release(&out); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2913 | } |
| 2914 | |
Felipe Contreras | 2775d92 | 2009-10-11 23:46:11 +0300 | [diff] [blame] | 2915 | static void show_shortstats(struct diffstat_t *data, struct diff_options *options) |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2916 | { |
| 2917 | int i, adds = 0, dels = 0, total_files = data->nr; |
| 2918 | |
| 2919 | if (data->nr == 0) |
| 2920 | return; |
| 2921 | |
| 2922 | for (i = 0; i < data->nr; i++) { |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2923 | int added = data->files[i]->added; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2924 | int deleted = data->files[i]->deleted; |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2925 | |
Junio C Hamano | 20c8cde | 2012-11-27 14:19:36 -0800 | [diff] [blame] | 2926 | if (data->files[i]->is_unmerged || |
| 2927 | (!data->files[i]->is_interesting && (added + deleted == 0))) { |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2928 | total_files--; |
Alexander Strasser | de9658b | 2012-06-15 23:50:30 +0200 | [diff] [blame] | 2929 | } else if (!data->files[i]->is_binary) { /* don't count bytes */ |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2930 | adds += added; |
| 2931 | dels += deleted; |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2932 | } |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2933 | } |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2934 | print_stat_summary_inserts_deletes(options, total_files, adds, dels); |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2935 | } |
| 2936 | |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 2937 | static void show_numstat(struct diffstat_t *data, struct diff_options *options) |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 2938 | { |
| 2939 | int i; |
| 2940 | |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2941 | if (data->nr == 0) |
| 2942 | return; |
| 2943 | |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 2944 | for (i = 0; i < data->nr; i++) { |
| 2945 | struct diffstat_file *file = data->files[i]; |
| 2946 | |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2947 | fprintf(options->file, "%s", diff_line_prefix(options)); |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 2948 | |
Junio C Hamano | bfddbc5 | 2006-12-10 13:50:59 -0800 | [diff] [blame] | 2949 | if (file->is_binary) |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2950 | fprintf(options->file, "-\t-\t"); |
Junio C Hamano | bfddbc5 | 2006-12-10 13:50:59 -0800 | [diff] [blame] | 2951 | else |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2952 | fprintf(options->file, |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2953 | "%"PRIuMAX"\t%"PRIuMAX"\t", |
| 2954 | file->added, file->deleted); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2955 | if (options->line_termination) { |
| 2956 | fill_print_name(file); |
| 2957 | if (!file->is_renamed) |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2958 | write_name_quoted(file->name, options->file, |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2959 | options->line_termination); |
| 2960 | else { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2961 | fputs(file->print_name, options->file); |
| 2962 | putc(options->line_termination, options->file); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2963 | } |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2964 | } else { |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2965 | if (file->is_renamed) { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2966 | putc('\0', options->file); |
| 2967 | write_name_quoted(file->from_name, options->file, '\0'); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2968 | } |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2969 | write_name_quoted(file->name, options->file, '\0'); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2970 | } |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 2971 | } |
| 2972 | } |
| 2973 | |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 2974 | struct dirstat_file { |
| 2975 | const char *name; |
| 2976 | unsigned long changed; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2977 | }; |
| 2978 | |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 2979 | struct dirstat_dir { |
| 2980 | struct dirstat_file *files; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 2981 | int alloc, nr, permille, cumulative; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 2982 | }; |
| 2983 | |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 2984 | static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir, |
| 2985 | unsigned long changed, const char *base, int baselen) |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2986 | { |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 2987 | unsigned long sum_changes = 0; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2988 | unsigned int sources = 0; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2989 | const char *line_prefix = diff_line_prefix(opt); |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2990 | |
| 2991 | while (dir->nr) { |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 2992 | struct dirstat_file *f = dir->files; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2993 | int namelen = strlen(f->name); |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 2994 | unsigned long changes; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2995 | char *slash; |
| 2996 | |
| 2997 | if (namelen < baselen) |
| 2998 | break; |
| 2999 | if (memcmp(f->name, base, baselen)) |
| 3000 | break; |
| 3001 | slash = strchr(f->name + baselen, '/'); |
| 3002 | if (slash) { |
| 3003 | int newbaselen = slash + 1 - f->name; |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 3004 | changes = gather_dirstat(opt, dir, changed, f->name, newbaselen); |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3005 | sources++; |
| 3006 | } else { |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 3007 | changes = f->changed; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3008 | dir->files++; |
| 3009 | dir->nr--; |
| 3010 | sources += 2; |
| 3011 | } |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 3012 | sum_changes += changes; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | /* |
| 3016 | * We don't report dirstat's for |
| 3017 | * - the top level |
| 3018 | * - or cases where everything came from a single directory |
| 3019 | * under this directory (sources == 1). |
| 3020 | */ |
| 3021 | if (baselen && sources != 1) { |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 3022 | if (sum_changes) { |
| 3023 | int permille = sum_changes * 1000 / changed; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 3024 | if (permille >= dir->permille) { |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 3025 | fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix, |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 3026 | permille / 10, permille % 10, baselen, base); |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3027 | if (!dir->cumulative) |
| 3028 | return 0; |
| 3029 | } |
| 3030 | } |
| 3031 | } |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 3032 | return sum_changes; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3033 | } |
| 3034 | |
Linus Torvalds | 441bca0 | 2008-08-28 16:19:08 -0700 | [diff] [blame] | 3035 | static int dirstat_compare(const void *_a, const void *_b) |
| 3036 | { |
| 3037 | const struct dirstat_file *a = _a; |
| 3038 | const struct dirstat_file *b = _b; |
| 3039 | return strcmp(a->name, b->name); |
| 3040 | } |
| 3041 | |
Junio C Hamano | 34a9489 | 2023-05-05 14:19:16 -0700 | [diff] [blame] | 3042 | static void conclude_dirstat(struct diff_options *options, |
| 3043 | struct dirstat_dir *dir, |
| 3044 | unsigned long changed) |
| 3045 | { |
Junio C Hamano | 8397398 | 2023-05-05 14:19:17 -0700 | [diff] [blame] | 3046 | struct dirstat_file *to_free = dir->files; |
Junio C Hamano | 34a9489 | 2023-05-05 14:19:16 -0700 | [diff] [blame] | 3047 | |
Junio C Hamano | 8397398 | 2023-05-05 14:19:17 -0700 | [diff] [blame] | 3048 | if (!changed) { |
| 3049 | /* This can happen even with many files, if everything was renames */ |
| 3050 | ; |
| 3051 | } else { |
| 3052 | /* Show all directories with more than x% of the changes */ |
| 3053 | QSORT(dir->files, dir->nr, dirstat_compare); |
| 3054 | gather_dirstat(options, dir, changed, "", 0); |
| 3055 | } |
| 3056 | |
| 3057 | free(to_free); |
Junio C Hamano | 34a9489 | 2023-05-05 14:19:16 -0700 | [diff] [blame] | 3058 | } |
| 3059 | |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3060 | static void show_dirstat(struct diff_options *options) |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3061 | { |
| 3062 | int i; |
| 3063 | unsigned long changed; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3064 | struct dirstat_dir dir; |
| 3065 | struct diff_queue_struct *q = &diff_queued_diff; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3066 | |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3067 | dir.files = NULL; |
| 3068 | dir.alloc = 0; |
| 3069 | dir.nr = 0; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 3070 | dir.permille = options->dirstat_permille; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3071 | dir.cumulative = options->flags.dirstat_cumulative; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3072 | |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3073 | changed = 0; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3074 | for (i = 0; i < q->nr; i++) { |
| 3075 | struct diff_filepair *p = q->queue[i]; |
| 3076 | const char *name; |
| 3077 | unsigned long copied, added, damage; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3078 | struct diff_populate_filespec_options dpf_options = { |
| 3079 | .check_size_only = 1, |
| 3080 | }; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3081 | |
Johan Herland | 2ca8671 | 2011-04-12 11:24:34 +0200 | [diff] [blame] | 3082 | name = p->two->path ? p->two->path : p->one->path; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3083 | |
Jeff King | d9f62df | 2018-08-28 17:23:03 -0400 | [diff] [blame] | 3084 | if (p->one->oid_valid && p->two->oid_valid && |
| 3085 | oideq(&p->one->oid, &p->two->oid)) { |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3086 | /* |
| 3087 | * The SHA1 has not changed, so pre-/post-content is |
| 3088 | * identical. We can therefore skip looking at the |
| 3089 | * file contents altogether. |
| 3090 | */ |
| 3091 | damage = 0; |
| 3092 | goto found_damage; |
| 3093 | } |
| 3094 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3095 | if (options->flags.dirstat_by_file) { |
Johan Herland | 0133dab | 2011-04-11 00:48:51 +0200 | [diff] [blame] | 3096 | /* |
| 3097 | * In --dirstat-by-file mode, we don't really need to |
| 3098 | * look at the actual file contents at all. |
| 3099 | * The fact that the SHA1 changed is enough for us to |
| 3100 | * add this file to the list of results |
| 3101 | * (with each file contributing equal damage). |
| 3102 | */ |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3103 | damage = 1; |
Johan Herland | 0133dab | 2011-04-11 00:48:51 +0200 | [diff] [blame] | 3104 | goto found_damage; |
| 3105 | } |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3106 | |
| 3107 | if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3108 | diff_populate_filespec(options->repo, p->one, NULL); |
| 3109 | diff_populate_filespec(options->repo, p->two, NULL); |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3110 | diffcore_count_changes(options->repo, |
| 3111 | p->one, p->two, NULL, NULL, |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3112 | &copied, &added); |
| 3113 | diff_free_filespec_data(p->one); |
| 3114 | diff_free_filespec_data(p->two); |
| 3115 | } else if (DIFF_FILE_VALID(p->one)) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3116 | diff_populate_filespec(options->repo, p->one, &dpf_options); |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3117 | copied = added = 0; |
| 3118 | diff_free_filespec_data(p->one); |
| 3119 | } else if (DIFF_FILE_VALID(p->two)) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3120 | diff_populate_filespec(options->repo, p->two, &dpf_options); |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3121 | copied = 0; |
| 3122 | added = p->two->size; |
| 3123 | diff_free_filespec_data(p->two); |
| 3124 | } else |
Junio C Hamano | 2b0b551 | 2008-02-24 17:37:15 -0800 | [diff] [blame] | 3125 | continue; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3126 | |
| 3127 | /* |
| 3128 | * Original minus copied is the removed material, |
| 3129 | * added is the new material. They are both damages |
Johan Herland | 0133dab | 2011-04-11 00:48:51 +0200 | [diff] [blame] | 3130 | * made to the preimage. |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3131 | * If the resulting damage is zero, we know that |
| 3132 | * diffcore_count_changes() considers the two entries to |
Jeff King | d9f62df | 2018-08-28 17:23:03 -0400 | [diff] [blame] | 3133 | * be identical, but since the oid changed, we |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3134 | * know that there must have been _some_ kind of change, |
| 3135 | * so we force all entries to have damage > 0. |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3136 | */ |
| 3137 | damage = (p->one->size - copied) + added; |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3138 | if (!damage) |
Heikki Orsila | fd33777 | 2008-09-05 22:27:35 +0300 | [diff] [blame] | 3139 | damage = 1; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3140 | |
Johan Herland | 0133dab | 2011-04-11 00:48:51 +0200 | [diff] [blame] | 3141 | found_damage: |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3142 | ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc); |
| 3143 | dir.files[dir.nr].name = name; |
| 3144 | dir.files[dir.nr].changed = damage; |
| 3145 | changed += damage; |
| 3146 | dir.nr++; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3147 | } |
| 3148 | |
Junio C Hamano | 34a9489 | 2023-05-05 14:19:16 -0700 | [diff] [blame] | 3149 | conclude_dirstat(options, &dir, changed); |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3150 | } |
| 3151 | |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 3152 | static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options) |
| 3153 | { |
| 3154 | int i; |
| 3155 | unsigned long changed; |
| 3156 | struct dirstat_dir dir; |
| 3157 | |
| 3158 | if (data->nr == 0) |
| 3159 | return; |
| 3160 | |
| 3161 | dir.files = NULL; |
| 3162 | dir.alloc = 0; |
| 3163 | dir.nr = 0; |
| 3164 | dir.permille = options->dirstat_permille; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3165 | dir.cumulative = options->flags.dirstat_cumulative; |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 3166 | |
| 3167 | changed = 0; |
| 3168 | for (i = 0; i < data->nr; i++) { |
| 3169 | struct diffstat_file *file = data->files[i]; |
| 3170 | unsigned long damage = file->added + file->deleted; |
| 3171 | if (file->is_binary) |
| 3172 | /* |
| 3173 | * binary files counts bytes, not lines. Must find some |
| 3174 | * way to normalize binary bytes vs. textual lines. |
| 3175 | * The following heuristic assumes that there are 64 |
| 3176 | * bytes per "line". |
| 3177 | * This is stupid and ugly, but very cheap... |
| 3178 | */ |
René Scharfe | 42c78a2 | 2017-07-08 12:35:35 +0200 | [diff] [blame] | 3179 | damage = DIV_ROUND_UP(damage, 64); |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 3180 | ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc); |
| 3181 | dir.files[dir.nr].name = file->name; |
| 3182 | dir.files[dir.nr].changed = damage; |
| 3183 | changed += damage; |
| 3184 | dir.nr++; |
| 3185 | } |
| 3186 | |
Junio C Hamano | 34a9489 | 2023-05-05 14:19:16 -0700 | [diff] [blame] | 3187 | conclude_dirstat(options, &dir, changed); |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 3188 | } |
| 3189 | |
Matthew Rogers | 1cf3d5d | 2020-08-20 00:41:32 +0000 | [diff] [blame] | 3190 | static void free_diffstat_file(struct diffstat_file *f) |
| 3191 | { |
| 3192 | free(f->print_name); |
| 3193 | free(f->name); |
| 3194 | free(f->from_name); |
| 3195 | free(f); |
| 3196 | } |
| 3197 | |
Daniel Ferreira | e4cb659 | 2019-11-13 12:40:58 +0000 | [diff] [blame] | 3198 | void free_diffstat_info(struct diffstat_t *diffstat) |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 3199 | { |
| 3200 | int i; |
Matthew Rogers | 1cf3d5d | 2020-08-20 00:41:32 +0000 | [diff] [blame] | 3201 | for (i = 0; i < diffstat->nr; i++) |
| 3202 | free_diffstat_file(diffstat->files[i]); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 3203 | free(diffstat->files); |
| 3204 | } |
| 3205 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3206 | struct checkdiff_t { |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3207 | const char *filename; |
Junio C Hamano | 1ba111d | 2008-06-26 15:36:34 -0700 | [diff] [blame] | 3208 | int lineno; |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3209 | int conflict_marker_size; |
Junio C Hamano | 1ba111d | 2008-06-26 15:36:34 -0700 | [diff] [blame] | 3210 | struct diff_options *o; |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 3211 | unsigned ws_rule; |
Wincent Colaiuta | 62c6489 | 2007-12-13 21:24:52 +0100 | [diff] [blame] | 3212 | unsigned status; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3213 | }; |
| 3214 | |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3215 | static int is_conflict_marker(const char *line, int marker_size, unsigned long len) |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3216 | { |
| 3217 | char firstchar; |
| 3218 | int cnt; |
| 3219 | |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3220 | if (len < marker_size + 1) |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3221 | return 0; |
| 3222 | firstchar = line[0]; |
| 3223 | switch (firstchar) { |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3224 | case '=': case '>': case '<': case '|': |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3225 | break; |
| 3226 | default: |
| 3227 | return 0; |
| 3228 | } |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3229 | for (cnt = 1; cnt < marker_size; cnt++) |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3230 | if (line[cnt] != firstchar) |
| 3231 | return 0; |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 3232 | /* line[1] through line[marker_size-1] are same as firstchar */ |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3233 | if (len < marker_size + 1 || !isspace(line[marker_size])) |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3234 | return 0; |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3235 | return 1; |
| 3236 | } |
| 3237 | |
Jeff King | 75ab763 | 2018-11-02 02:39:03 -0400 | [diff] [blame] | 3238 | static void checkdiff_consume_hunk(void *priv, |
Jeff King | 61bdc7c | 2022-12-13 06:13:48 -0500 | [diff] [blame] | 3239 | long ob UNUSED, long on UNUSED, |
| 3240 | long nb, long nn UNUSED, |
| 3241 | const char *func UNUSED, long funclen UNUSED) |
Jeff King | 75ab763 | 2018-11-02 02:39:03 -0400 | [diff] [blame] | 3242 | |
| 3243 | { |
| 3244 | struct checkdiff_t *data = priv; |
| 3245 | data->lineno = nb - 1; |
| 3246 | } |
| 3247 | |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 3248 | static int checkdiff_consume(void *priv, char *line, unsigned long len) |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3249 | { |
| 3250 | struct checkdiff_t *data = priv; |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3251 | int marker_size = data->conflict_marker_size; |
Jeff King | f1c9626 | 2011-08-17 22:03:12 -0700 | [diff] [blame] | 3252 | const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE); |
| 3253 | const char *reset = diff_get_color(data->o->use_color, DIFF_RESET); |
| 3254 | const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 3255 | char *err; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 3256 | const char *line_prefix; |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 3257 | |
| 3258 | assert(data->o); |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 3259 | line_prefix = diff_line_prefix(data->o); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3260 | |
| 3261 | if (line[0] == '+') { |
Junio C Hamano | 18374e5 | 2008-06-26 13:16:33 -0700 | [diff] [blame] | 3262 | unsigned bad; |
Junio C Hamano | 0ef617f | 2008-02-15 20:30:05 -0800 | [diff] [blame] | 3263 | data->lineno++; |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3264 | if (is_conflict_marker(line + 1, marker_size, len - 1)) { |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3265 | data->status |= 1; |
| 3266 | fprintf(data->o->file, |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 3267 | "%s%s:%d: leftover conflict marker\n", |
| 3268 | line_prefix, data->filename, data->lineno); |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3269 | } |
Junio C Hamano | 8f8841e | 2008-06-26 15:35:21 -0700 | [diff] [blame] | 3270 | bad = ws_check(line + 1, len - 1, data->ws_rule); |
Junio C Hamano | 18374e5 | 2008-06-26 13:16:33 -0700 | [diff] [blame] | 3271 | if (!bad) |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 3272 | return 0; |
Junio C Hamano | 18374e5 | 2008-06-26 13:16:33 -0700 | [diff] [blame] | 3273 | data->status |= bad; |
| 3274 | err = whitespace_error_string(bad); |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 3275 | fprintf(data->o->file, "%s%s:%d: %s.\n", |
| 3276 | line_prefix, data->filename, data->lineno, err); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 3277 | free(err); |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 3278 | emit_line(data->o, set, reset, line, 1); |
Junio C Hamano | 8f8841e | 2008-06-26 15:35:21 -0700 | [diff] [blame] | 3279 | ws_check_emit(line + 1, len - 1, data->ws_rule, |
Junio C Hamano | 1ba111d | 2008-06-26 15:36:34 -0700 | [diff] [blame] | 3280 | data->o->file, set, reset, ws); |
Junio C Hamano | 877f23c | 2008-06-26 15:36:59 -0700 | [diff] [blame] | 3281 | } else if (line[0] == ' ') { |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3282 | data->lineno++; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3283 | } |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 3284 | return 0; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3285 | } |
| 3286 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3287 | static unsigned char *deflate_it(char *data, |
| 3288 | unsigned long size, |
| 3289 | unsigned long *result_size) |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3290 | { |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3291 | int bound; |
| 3292 | unsigned char *deflated; |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 3293 | git_zstream stream; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3294 | |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 3295 | git_deflate_init(&stream, zlib_compression_level); |
Junio C Hamano | 225a6f1 | 2011-06-10 11:18:17 -0700 | [diff] [blame] | 3296 | bound = git_deflate_bound(&stream, size); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3297 | deflated = xmalloc(bound); |
| 3298 | stream.next_out = deflated; |
| 3299 | stream.avail_out = bound; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3300 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3301 | stream.next_in = (unsigned char *)data; |
| 3302 | stream.avail_in = size; |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 3303 | while (git_deflate(&stream, Z_FINISH) == Z_OK) |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3304 | ; /* nothing */ |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 3305 | git_deflate_end(&stream); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3306 | *result_size = stream.total_out; |
| 3307 | return deflated; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3308 | } |
| 3309 | |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3310 | static void emit_binary_diff_body(struct diff_options *o, |
| 3311 | mmfile_t *one, mmfile_t *two) |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3312 | { |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3313 | void *cp; |
| 3314 | void *delta; |
| 3315 | void *deflated; |
| 3316 | void *data; |
| 3317 | unsigned long orig_size; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3318 | unsigned long delta_size; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3319 | unsigned long deflate_size; |
| 3320 | unsigned long data_size; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3321 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3322 | /* We could do deflated delta, or we could do just deflated two, |
| 3323 | * whichever is smaller. |
| 3324 | */ |
| 3325 | delta = NULL; |
| 3326 | deflated = deflate_it(two->ptr, two->size, &deflate_size); |
| 3327 | if (one->size && two->size) { |
| 3328 | delta = diff_delta(one->ptr, one->size, |
| 3329 | two->ptr, two->size, |
| 3330 | &delta_size, deflate_size); |
| 3331 | if (delta) { |
| 3332 | void *to_free = delta; |
| 3333 | orig_size = delta_size; |
| 3334 | delta = deflate_it(delta, delta_size, &delta_size); |
| 3335 | free(to_free); |
| 3336 | } |
| 3337 | } |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3338 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3339 | if (delta && delta_size < deflate_size) { |
Torsten Bögershausen | ca473ce | 2018-11-11 08:05:04 +0100 | [diff] [blame] | 3340 | char *s = xstrfmt("%"PRIuMAX , (uintmax_t)orig_size); |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3341 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA, |
| 3342 | s, strlen(s), 0); |
| 3343 | free(s); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3344 | free(deflated); |
| 3345 | data = delta; |
| 3346 | data_size = delta_size; |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3347 | } else { |
| 3348 | char *s = xstrfmt("%lu", two->size); |
| 3349 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL, |
| 3350 | s, strlen(s), 0); |
| 3351 | free(s); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3352 | free(delta); |
| 3353 | data = deflated; |
| 3354 | data_size = deflate_size; |
| 3355 | } |
| 3356 | |
| 3357 | /* emit data encoded in base85 */ |
| 3358 | cp = data; |
| 3359 | while (data_size) { |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3360 | int len; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3361 | int bytes = (52 < data_size) ? 52 : data_size; |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3362 | char line[71]; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3363 | data_size -= bytes; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3364 | if (bytes <= 26) |
| 3365 | line[0] = bytes + 'A' - 1; |
| 3366 | else |
| 3367 | line[0] = bytes - 26 + 'a' - 1; |
| 3368 | encode_85(line + 1, cp, bytes); |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 3369 | cp = (char *) cp + bytes; |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3370 | |
| 3371 | len = strlen(line); |
| 3372 | line[len++] = '\n'; |
| 3373 | line[len] = '\0'; |
| 3374 | |
| 3375 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY, |
| 3376 | line, len, 0); |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3377 | } |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3378 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3379 | free(data); |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3380 | } |
| 3381 | |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3382 | static void emit_binary_diff(struct diff_options *o, |
| 3383 | mmfile_t *one, mmfile_t *two) |
Junio C Hamano | d4c452f | 2006-08-16 16:08:14 -0700 | [diff] [blame] | 3384 | { |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3385 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0); |
| 3386 | emit_binary_diff_body(o, one, two); |
| 3387 | emit_binary_diff_body(o, two, one); |
Junio C Hamano | d4c452f | 2006-08-16 16:08:14 -0700 | [diff] [blame] | 3388 | } |
| 3389 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3390 | int diff_filespec_is_binary(struct repository *r, |
| 3391 | struct diff_filespec *one) |
Junio C Hamano | 29a3eef | 2007-07-06 00:18:54 -0700 | [diff] [blame] | 3392 | { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3393 | struct diff_populate_filespec_options dpf_options = { |
| 3394 | .check_binary = 1, |
| 3395 | }; |
| 3396 | |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3397 | if (one->is_binary == -1) { |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3398 | diff_filespec_load_driver(one, r->index); |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3399 | if (one->driver->binary != -1) |
| 3400 | one->is_binary = one->driver->binary; |
| 3401 | else { |
| 3402 | if (!one->data && DIFF_FILE_VALID(one)) |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3403 | diff_populate_filespec(r, one, &dpf_options); |
Nguyễn Thái Ngọc Duy | 6bf3b81 | 2014-08-16 10:08:05 +0700 | [diff] [blame] | 3404 | if (one->is_binary == -1 && one->data) |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3405 | one->is_binary = buffer_is_binary(one->data, |
| 3406 | one->size); |
| 3407 | if (one->is_binary == -1) |
| 3408 | one->is_binary = 0; |
| 3409 | } |
| 3410 | } |
Junio C Hamano | 29a3eef | 2007-07-06 00:18:54 -0700 | [diff] [blame] | 3411 | return one->is_binary; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3412 | } |
| 3413 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3414 | static const struct userdiff_funcname * |
| 3415 | diff_funcname_pattern(struct diff_options *o, struct diff_filespec *one) |
Junio C Hamano | f258475 | 2007-07-06 00:45:10 -0700 | [diff] [blame] | 3416 | { |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3417 | diff_filespec_load_driver(one, o->repo->index); |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3418 | return one->driver->funcname.pattern ? &one->driver->funcname : NULL; |
Junio C Hamano | f258475 | 2007-07-06 00:45:10 -0700 | [diff] [blame] | 3419 | } |
| 3420 | |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3421 | void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b) |
| 3422 | { |
| 3423 | if (!options->a_prefix) |
| 3424 | options->a_prefix = a; |
| 3425 | if (!options->b_prefix) |
| 3426 | options->b_prefix = b; |
| 3427 | } |
| 3428 | |
Jeff King | 6799aad | 2023-03-09 01:07:06 -0500 | [diff] [blame] | 3429 | void diff_set_noprefix(struct diff_options *options) |
| 3430 | { |
| 3431 | options->a_prefix = options->b_prefix = ""; |
| 3432 | } |
| 3433 | |
| 3434 | void diff_set_default_prefix(struct diff_options *options) |
| 3435 | { |
Peter Hutterer | 7fdc265 | 2024-03-15 11:03:10 +1000 | [diff] [blame] | 3436 | options->a_prefix = diff_src_prefix; |
| 3437 | options->b_prefix = diff_dst_prefix; |
Jeff King | 6799aad | 2023-03-09 01:07:06 -0500 | [diff] [blame] | 3438 | } |
| 3439 | |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 3440 | struct userdiff_driver *get_textconv(struct repository *r, |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3441 | struct diff_filespec *one) |
Jeff King | 04427ac | 2008-10-26 00:44:53 -0400 | [diff] [blame] | 3442 | { |
| 3443 | if (!DIFF_FILE_VALID(one)) |
| 3444 | return NULL; |
Jeff King | d391c0f | 2010-09-21 17:01:24 -0400 | [diff] [blame] | 3445 | |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 3446 | diff_filespec_load_driver(one, r->index); |
| 3447 | return userdiff_get_textconv(r, one->driver); |
Jeff King | 04427ac | 2008-10-26 00:44:53 -0400 | [diff] [blame] | 3448 | } |
| 3449 | |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 3450 | static struct string_list *additional_headers(struct diff_options *o, |
| 3451 | const char *path) |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3452 | { |
| 3453 | if (!o->additional_path_headers) |
| 3454 | return NULL; |
| 3455 | return strmap_get(o->additional_path_headers, path); |
| 3456 | } |
| 3457 | |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 3458 | static void add_formatted_header(struct strbuf *msg, |
| 3459 | const char *header, |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3460 | const char *line_prefix, |
| 3461 | const char *meta, |
| 3462 | const char *reset) |
| 3463 | { |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 3464 | const char *next, *newline; |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3465 | |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 3466 | for (next = header; *next; next = newline) { |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3467 | newline = strchrnul(next, '\n'); |
| 3468 | strbuf_addf(msg, "%s%s%.*s%s\n", line_prefix, meta, |
| 3469 | (int)(newline - next), next, reset); |
| 3470 | if (*newline) |
| 3471 | newline++; |
| 3472 | } |
| 3473 | } |
| 3474 | |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 3475 | static void add_formatted_headers(struct strbuf *msg, |
| 3476 | struct string_list *more_headers, |
| 3477 | const char *line_prefix, |
| 3478 | const char *meta, |
| 3479 | const char *reset) |
| 3480 | { |
| 3481 | int i; |
| 3482 | |
| 3483 | for (i = 0; i < more_headers->nr; i++) |
| 3484 | add_formatted_header(msg, more_headers->items[i].string, |
| 3485 | line_prefix, meta, reset); |
| 3486 | } |
| 3487 | |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3488 | static int diff_filepair_is_phoney(struct diff_filespec *one, |
| 3489 | struct diff_filespec *two) |
| 3490 | { |
| 3491 | /* |
| 3492 | * This function specifically looks for pairs injected by |
| 3493 | * create_filepairs_for_header_only_notifications(). Such |
| 3494 | * pairs are "phoney" in that they do not represent any |
| 3495 | * content or even mode difference, but were inserted because |
| 3496 | * diff_queued_diff previously had no pair associated with |
| 3497 | * that path but we needed some pair to avoid losing the |
| 3498 | * "remerge CONFLICT" header associated with the path. |
| 3499 | */ |
| 3500 | return !DIFF_FILE_VALID(one) && !DIFF_FILE_VALID(two); |
| 3501 | } |
| 3502 | |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 3503 | static int set_diff_algorithm(struct diff_options *opts, |
| 3504 | const char *alg) |
| 3505 | { |
| 3506 | long value = parse_algorithm_value(alg); |
| 3507 | |
| 3508 | if (value < 0) |
| 3509 | return -1; |
| 3510 | |
| 3511 | /* clear out previous settings */ |
| 3512 | DIFF_XDL_CLR(opts, NEED_MINIMAL); |
| 3513 | opts->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK; |
| 3514 | opts->xdl_opts |= value; |
| 3515 | |
| 3516 | return 0; |
| 3517 | } |
| 3518 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3519 | static void builtin_diff(const char *name_a, |
| 3520 | const char *name_b, |
| 3521 | struct diff_filespec *one, |
| 3522 | struct diff_filespec *two, |
| 3523 | const char *xfrm_msg, |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3524 | int must_show_header, |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3525 | struct diff_options *o, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3526 | int complete_rewrite) |
| 3527 | { |
| 3528 | mmfile_t mf1, mf2; |
| 3529 | const char *lbl[2]; |
| 3530 | char *a_one, *b_two; |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3531 | const char *meta = diff_get_color_opt(o, DIFF_METAINFO); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 3532 | const char *reset = diff_get_color_opt(o, DIFF_RESET); |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3533 | const char *a_prefix, *b_prefix; |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 3534 | struct userdiff_driver *textconv_one = NULL; |
| 3535 | struct userdiff_driver *textconv_two = NULL; |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3536 | struct strbuf header = STRBUF_INIT; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 3537 | const char *line_prefix = diff_line_prefix(o); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3538 | |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3539 | diff_set_mnemonic_prefix(o, "a/", "b/"); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3540 | if (o->flags.reverse_diff) { |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3541 | a_prefix = o->b_prefix; |
| 3542 | b_prefix = o->a_prefix; |
| 3543 | } else { |
| 3544 | a_prefix = o->a_prefix; |
| 3545 | b_prefix = o->b_prefix; |
| 3546 | } |
| 3547 | |
Jacob Keller | 61cfbc0 | 2016-08-31 16:27:21 -0700 | [diff] [blame] | 3548 | if (o->submodule_format == DIFF_SUBMODULE_LOG && |
| 3549 | (!one->mode || S_ISGITLINK(one->mode)) && |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3550 | (!two->mode || S_ISGITLINK(two->mode)) && |
| 3551 | (!diff_filepair_is_phoney(one, two))) { |
Shourya Shukla | 180b154 | 2020-08-13 01:14:02 +0530 | [diff] [blame] | 3552 | show_submodule_diff_summary(o, one->path ? one->path : two->path, |
Jacob Keller | 602a283 | 2016-08-31 16:27:23 -0700 | [diff] [blame] | 3553 | &one->oid, &two->oid, |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 3554 | two->dirty_submodule); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3555 | return; |
Jacob Keller | fd47ae6 | 2016-08-31 16:27:25 -0700 | [diff] [blame] | 3556 | } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF && |
| 3557 | (!one->mode || S_ISGITLINK(one->mode)) && |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3558 | (!two->mode || S_ISGITLINK(two->mode)) && |
| 3559 | (!diff_filepair_is_phoney(one, two))) { |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 3560 | show_submodule_inline_diff(o, one->path ? one->path : two->path, |
Jacob Keller | fd47ae6 | 2016-08-31 16:27:25 -0700 | [diff] [blame] | 3561 | &one->oid, &two->oid, |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 3562 | two->dirty_submodule); |
Jacob Keller | fd47ae6 | 2016-08-31 16:27:25 -0700 | [diff] [blame] | 3563 | return; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3564 | } |
| 3565 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3566 | if (o->flags.allow_textconv) { |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 3567 | textconv_one = get_textconv(o->repo, one); |
| 3568 | textconv_two = get_textconv(o->repo, two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3569 | } |
| 3570 | |
Linus Torvalds | 71b989e | 2008-10-05 15:35:15 -0400 | [diff] [blame] | 3571 | /* Never use a non-valid filename anywhere if at all possible */ |
| 3572 | name_a = DIFF_FILE_VALID(one) ? name_a : name_b; |
| 3573 | name_b = DIFF_FILE_VALID(two) ? name_b : name_a; |
| 3574 | |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3575 | a_one = quote_two(a_prefix, name_a + (*name_a == '/')); |
| 3576 | b_two = quote_two(b_prefix, name_b + (*name_b == '/')); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3577 | lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null"; |
| 3578 | lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null"; |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3579 | if (diff_filepair_is_phoney(one, two)) { |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3580 | /* |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3581 | * We should only reach this point for pairs generated from |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3582 | * create_filepairs_for_header_only_notifications(). For |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3583 | * these, we want to avoid the "/dev/null" special casing |
| 3584 | * above, because we do not want such pairs shown as either |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3585 | * "new file" or "deleted file" below. |
| 3586 | */ |
| 3587 | lbl[0] = a_one; |
| 3588 | lbl[1] = b_two; |
| 3589 | } |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3590 | strbuf_addf(&header, "%s%sdiff --git %s %s%s\n", line_prefix, meta, a_one, b_two, reset); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3591 | if (lbl[0][0] == '/') { |
| 3592 | /* /dev/null */ |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3593 | strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 3594 | if (xfrm_msg) |
| 3595 | strbuf_addstr(&header, xfrm_msg); |
Junio C Hamano | c9a3e72 | 2023-08-18 16:59:29 -0700 | [diff] [blame] | 3596 | o->found_changes = 1; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3597 | must_show_header = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3598 | } |
| 3599 | else if (lbl[1][0] == '/') { |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3600 | strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 3601 | if (xfrm_msg) |
| 3602 | strbuf_addstr(&header, xfrm_msg); |
Junio C Hamano | c9a3e72 | 2023-08-18 16:59:29 -0700 | [diff] [blame] | 3603 | o->found_changes = 1; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3604 | must_show_header = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3605 | } |
| 3606 | else { |
| 3607 | if (one->mode != two->mode) { |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3608 | strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset); |
| 3609 | strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset); |
Junio C Hamano | c9a3e72 | 2023-08-18 16:59:29 -0700 | [diff] [blame] | 3610 | o->found_changes = 1; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3611 | must_show_header = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3612 | } |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 3613 | if (xfrm_msg) |
| 3614 | strbuf_addstr(&header, xfrm_msg); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3615 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3616 | /* |
| 3617 | * we do not run diff between different kind |
| 3618 | * of objects. |
| 3619 | */ |
| 3620 | if ((one->mode ^ two->mode) & S_IFMT) |
| 3621 | goto free_ab_and_return; |
Jeff King | 0c01857 | 2008-12-09 03:12:28 -0500 | [diff] [blame] | 3622 | if (complete_rewrite && |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3623 | (textconv_one || !diff_filespec_is_binary(o->repo, one)) && |
| 3624 | (textconv_two || !diff_filespec_is_binary(o->repo, two))) { |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3625 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3626 | header.buf, header.len, 0); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3627 | strbuf_reset(&header); |
Jeff King | 3aa1f7c | 2008-12-09 03:13:21 -0500 | [diff] [blame] | 3628 | emit_rewrite_diff(name_a, name_b, one, two, |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3629 | textconv_one, textconv_two, o); |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 3630 | o->found_changes = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3631 | goto free_ab_and_return; |
| 3632 | } |
| 3633 | } |
| 3634 | |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 3635 | if (o->irreversible_delete && lbl[1][0] == '/') { |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3636 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, |
| 3637 | header.len, 0); |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 3638 | strbuf_reset(&header); |
| 3639 | goto free_ab_and_return; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3640 | } else if (!o->flags.text && |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3641 | ( (!textconv_one && diff_filespec_is_binary(o->repo, one)) || |
| 3642 | (!textconv_two && diff_filespec_is_binary(o->repo, two)) )) { |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 3643 | struct strbuf sb = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | 1aaf69e | 2014-08-16 10:08:06 +0700 | [diff] [blame] | 3644 | if (!one->data && !two->data && |
| 3645 | S_ISREG(one->mode) && S_ISREG(two->mode) && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3646 | !o->flags.binary) { |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 3647 | if (oideq(&one->oid, &two->oid)) { |
Nguyễn Thái Ngọc Duy | 1aaf69e | 2014-08-16 10:08:06 +0700 | [diff] [blame] | 3648 | if (must_show_header) |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3649 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3650 | header.buf, header.len, |
| 3651 | 0); |
Nguyễn Thái Ngọc Duy | 1aaf69e | 2014-08-16 10:08:06 +0700 | [diff] [blame] | 3652 | goto free_ab_and_return; |
| 3653 | } |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3654 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3655 | header.buf, header.len, 0); |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 3656 | strbuf_addf(&sb, "%sBinary files %s and %s differ\n", |
| 3657 | diff_line_prefix(o), lbl[0], lbl[1]); |
| 3658 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES, |
| 3659 | sb.buf, sb.len, 0); |
| 3660 | strbuf_release(&sb); |
Nguyễn Thái Ngọc Duy | 1aaf69e | 2014-08-16 10:08:06 +0700 | [diff] [blame] | 3661 | goto free_ab_and_return; |
| 3662 | } |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3663 | if (fill_mmfile(o->repo, &mf1, one) < 0 || |
| 3664 | fill_mmfile(o->repo, &mf2, two) < 0) |
Jeff King | b337398 | 2010-04-01 20:14:24 -0400 | [diff] [blame] | 3665 | die("unable to read files to diff"); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3666 | /* Quite common confusing case */ |
| 3667 | if (mf1.size == mf2.size && |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3668 | !memcmp(mf1.ptr, mf2.ptr, mf1.size)) { |
| 3669 | if (must_show_header) |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3670 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3671 | header.buf, header.len, 0); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3672 | goto free_ab_and_return; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3673 | } |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3674 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3675 | strbuf_reset(&header); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3676 | if (o->flags.binary) |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3677 | emit_binary_diff(o, &mf1, &mf2); |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 3678 | else { |
| 3679 | 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); |
| 3684 | } |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 3685 | o->found_changes = 1; |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 3686 | } else { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3687 | /* Crazy xdl interfaces.. */ |
Jeff King | 0da0e92 | 2019-01-11 17:17:22 -0500 | [diff] [blame] | 3688 | const char *diffopts; |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 3689 | const char *v; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3690 | xpparam_t xpp; |
| 3691 | xdemitconf_t xecfg; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3692 | struct emit_callback ecbdata; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 3693 | const struct userdiff_funcname *pe; |
Junio C Hamano | f258475 | 2007-07-06 00:45:10 -0700 | [diff] [blame] | 3694 | |
Junio C Hamano | b3f01ff | 2012-02-29 18:14:16 -0800 | [diff] [blame] | 3695 | if (must_show_header) { |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3696 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3697 | header.buf, header.len, 0); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3698 | strbuf_reset(&header); |
| 3699 | } |
| 3700 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 3701 | mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr); |
| 3702 | mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr); |
Jeff King | 04427ac | 2008-10-26 00:44:53 -0400 | [diff] [blame] | 3703 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3704 | pe = diff_funcname_pattern(o, one); |
Brandon Casey | 45e7ca0 | 2008-09-18 17:40:48 -0500 | [diff] [blame] | 3705 | if (!pe) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3706 | pe = diff_funcname_pattern(o, two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3707 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 3708 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 3709 | memset(&xecfg, 0, sizeof(xecfg)); |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 3710 | memset(&ecbdata, 0, sizeof(ecbdata)); |
Johannes Schindelin | 1cdde29 | 2018-08-13 04:33:11 -0700 | [diff] [blame] | 3711 | if (o->flags.suppress_diff_headers) |
| 3712 | lbl[0] = NULL; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3713 | ecbdata.label_path = lbl; |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 3714 | ecbdata.color_diff = want_color(o->use_color); |
Nguyễn Thái Ngọc Duy | 26d024e | 2018-09-21 17:57:37 +0200 | [diff] [blame] | 3715 | ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b); |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 3716 | if (ecbdata.ws_rule & WS_BLANK_AT_EOF) |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 3717 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 3718 | ecbdata.opt = o; |
Johannes Schindelin | 1cdde29 | 2018-08-13 04:33:11 -0700 | [diff] [blame] | 3719 | if (header.len && !o->flags.suppress_diff_headers) |
| 3720 | ecbdata.header = &header; |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 3721 | xpp.flags = o->xdl_opts; |
Michał Kępień | 296d4a9 | 2020-10-20 08:48:09 +0200 | [diff] [blame] | 3722 | xpp.ignore_regex = o->ignore_regex; |
| 3723 | xpp.ignore_regex_nr = o->ignore_regex_nr; |
Jonathan Tan | 2477ab2 | 2017-11-27 11:47:47 -0800 | [diff] [blame] | 3724 | xpp.anchors = o->anchors; |
| 3725 | xpp.anchors_nr = o->anchors_nr; |
Linus Torvalds | ee1e541 | 2006-05-13 13:23:48 -0700 | [diff] [blame] | 3726 | xecfg.ctxlen = o->context; |
René Scharfe | 6d0e674 | 2008-12-28 19:45:32 +0100 | [diff] [blame] | 3727 | xecfg.interhunkctxlen = o->interhunkcontext; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3728 | xecfg.flags = XDL_EMIT_FUNCNAMES; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3729 | if (o->flags.funccontext) |
René Scharfe | 14937c2 | 2011-10-09 13:36:57 +0200 | [diff] [blame] | 3730 | xecfg.flags |= XDL_EMIT_FUNCCONTEXT; |
Brandon Casey | 45e7ca0 | 2008-09-18 17:40:48 -0500 | [diff] [blame] | 3731 | if (pe) |
Brandon Casey | a013585 | 2008-09-18 17:42:48 -0500 | [diff] [blame] | 3732 | xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags); |
Jeff King | 0da0e92 | 2019-01-11 17:17:22 -0500 | [diff] [blame] | 3733 | |
| 3734 | diffopts = getenv("GIT_DIFF_OPTS"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3735 | if (!diffopts) |
| 3736 | ; |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 3737 | else if (skip_prefix(diffopts, "--unified=", &v)) |
| 3738 | xecfg.ctxlen = strtoul(v, NULL, 10); |
| 3739 | else if (skip_prefix(diffopts, "-u", &v)) |
| 3740 | xecfg.ctxlen = strtoul(v, NULL, 10); |
Jeff King | 0da0e92 | 2019-01-11 17:17:22 -0500 | [diff] [blame] | 3741 | |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 3742 | if (o->word_diff) |
| 3743 | init_diff_words_data(&ecbdata, o, one, two); |
Jeff King | 9346d6d | 2018-11-02 02:35:45 -0400 | [diff] [blame] | 3744 | if (xdi_diff_outf(&mf1, &mf2, NULL, fn_out_consume, |
| 3745 | &ecbdata, &xpp, &xecfg)) |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 3746 | die("unable to generate diff for %s", one->path); |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 3747 | if (o->word_diff) |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 3748 | free_diff_words_data(&ecbdata); |
Jeff King | 04427ac | 2008-10-26 00:44:53 -0400 | [diff] [blame] | 3749 | if (textconv_one) |
| 3750 | free(mf1.ptr); |
| 3751 | if (textconv_two) |
| 3752 | free(mf2.ptr); |
René Scharfe | 8cfe5f1 | 2009-07-02 00:01:43 +0200 | [diff] [blame] | 3753 | xdiff_clear_find_func(&xecfg); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3754 | } |
| 3755 | |
| 3756 | free_ab_and_return: |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3757 | strbuf_release(&header); |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3758 | diff_free_filespec_data(one); |
| 3759 | diff_free_filespec_data(two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3760 | free(a_one); |
| 3761 | free(b_two); |
| 3762 | return; |
| 3763 | } |
| 3764 | |
Nguyễn Thái Ngọc Duy | ddf88fa | 2018-02-24 21:09:59 +0700 | [diff] [blame] | 3765 | static char *get_compact_summary(const struct diff_filepair *p, int is_renamed) |
| 3766 | { |
| 3767 | if (!is_renamed) { |
| 3768 | if (p->status == DIFF_STATUS_ADDED) { |
| 3769 | if (S_ISLNK(p->two->mode)) |
| 3770 | return "new +l"; |
| 3771 | else if ((p->two->mode & 0777) == 0755) |
| 3772 | return "new +x"; |
| 3773 | else |
| 3774 | return "new"; |
| 3775 | } else if (p->status == DIFF_STATUS_DELETED) |
| 3776 | return "gone"; |
| 3777 | } |
| 3778 | if (S_ISLNK(p->one->mode) && !S_ISLNK(p->two->mode)) |
| 3779 | return "mode -l"; |
| 3780 | else if (!S_ISLNK(p->one->mode) && S_ISLNK(p->two->mode)) |
| 3781 | return "mode +l"; |
| 3782 | else if ((p->one->mode & 0777) == 0644 && |
| 3783 | (p->two->mode & 0777) == 0755) |
| 3784 | return "mode +x"; |
| 3785 | else if ((p->one->mode & 0777) == 0755 && |
| 3786 | (p->two->mode & 0777) == 0644) |
| 3787 | return "mode -x"; |
| 3788 | return NULL; |
| 3789 | } |
| 3790 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3791 | static void builtin_diffstat(const char *name_a, const char *name_b, |
| 3792 | struct diff_filespec *one, |
| 3793 | struct diff_filespec *two, |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 3794 | struct diffstat_t *diffstat, |
Johannes Schindelin | 0d21efa | 2006-06-14 17:40:23 +0200 | [diff] [blame] | 3795 | struct diff_options *o, |
Linus Torvalds | 74faaa1 | 2012-10-17 10:00:37 -0700 | [diff] [blame] | 3796 | struct diff_filepair *p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3797 | { |
| 3798 | mmfile_t mf1, mf2; |
| 3799 | struct diffstat_file *data; |
Thomas Guyot-Sionnest | ff0c7fa | 2020-09-24 03:41:41 -0400 | [diff] [blame] | 3800 | int may_differ; |
Linus Torvalds | 74faaa1 | 2012-10-17 10:00:37 -0700 | [diff] [blame] | 3801 | int complete_rewrite = 0; |
| 3802 | |
| 3803 | if (!DIFF_PAIR_UNMERGED(p)) { |
| 3804 | if (p->status == DIFF_STATUS_MODIFIED && p->score) |
| 3805 | complete_rewrite = 1; |
| 3806 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3807 | |
| 3808 | data = diffstat_add(diffstat, name_a, name_b); |
Junio C Hamano | 99bfd40 | 2012-11-27 11:17:14 -0800 | [diff] [blame] | 3809 | data->is_interesting = p->status != DIFF_STATUS_UNKNOWN; |
Nguyễn Thái Ngọc Duy | ddf88fa | 2018-02-24 21:09:59 +0700 | [diff] [blame] | 3810 | if (o->flags.stat_with_summary) |
| 3811 | data->comments = get_compact_summary(p, data->is_renamed); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3812 | |
| 3813 | if (!one || !two) { |
| 3814 | data->is_unmerged = 1; |
| 3815 | return; |
| 3816 | } |
Jeff King | ded0abc | 2011-02-19 03:04:56 -0500 | [diff] [blame] | 3817 | |
Thomas Guyot-Sionnest | ff0c7fa | 2020-09-24 03:41:41 -0400 | [diff] [blame] | 3818 | /* saves some reads if true, not a guarantee of diff outcome */ |
| 3819 | may_differ = !(one->oid_valid && two->oid_valid && |
| 3820 | oideq(&one->oid, &two->oid)); |
Zbigniew Jędrzejewski-Szmek | 352ca4e | 2012-05-01 19:10:15 +0200 | [diff] [blame] | 3821 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3822 | if (diff_filespec_is_binary(o->repo, one) || |
| 3823 | diff_filespec_is_binary(o->repo, two)) { |
Jeff King | ded0abc | 2011-02-19 03:04:56 -0500 | [diff] [blame] | 3824 | data->is_binary = 1; |
Thomas Guyot-Sionnest | ff0c7fa | 2020-09-24 03:41:41 -0400 | [diff] [blame] | 3825 | if (!may_differ) { |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 3826 | data->added = 0; |
| 3827 | data->deleted = 0; |
| 3828 | } else { |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3829 | data->added = diff_filespec_size(o->repo, two); |
| 3830 | data->deleted = diff_filespec_size(o->repo, one); |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 3831 | } |
Jeff King | ded0abc | 2011-02-19 03:04:56 -0500 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | else if (complete_rewrite) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3835 | diff_populate_filespec(o->repo, one, NULL); |
| 3836 | diff_populate_filespec(o->repo, two, NULL); |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 3837 | data->deleted = count_lines(one->data, one->size); |
| 3838 | data->added = count_lines(two->data, two->size); |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 3839 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3840 | |
Thomas Guyot-Sionnest | ff0c7fa | 2020-09-24 03:41:41 -0400 | [diff] [blame] | 3841 | else if (may_differ) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3842 | /* Crazy xdl interfaces.. */ |
| 3843 | xpparam_t xpp; |
| 3844 | xdemitconf_t xecfg; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3845 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3846 | if (fill_mmfile(o->repo, &mf1, one) < 0 || |
| 3847 | fill_mmfile(o->repo, &mf2, two) < 0) |
Jeff King | ded0abc | 2011-02-19 03:04:56 -0500 | [diff] [blame] | 3848 | die("unable to read files to diff"); |
| 3849 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 3850 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 3851 | memset(&xecfg, 0, sizeof(xecfg)); |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 3852 | xpp.flags = o->xdl_opts; |
Michał Kępień | 296d4a9 | 2020-10-20 08:48:09 +0200 | [diff] [blame] | 3853 | xpp.ignore_regex = o->ignore_regex; |
| 3854 | xpp.ignore_regex_nr = o->ignore_regex_nr; |
Jonathan Tan | 2477ab2 | 2017-11-27 11:47:47 -0800 | [diff] [blame] | 3855 | xpp.anchors = o->anchors; |
| 3856 | xpp.anchors_nr = o->anchors_nr; |
Junio C Hamano | f01cae9 | 2011-09-22 10:54:47 -0700 | [diff] [blame] | 3857 | xecfg.ctxlen = o->context; |
| 3858 | xecfg.interhunkctxlen = o->interhunkcontext; |
Ævar Arnfjörð Bjarmason | 5d93460 | 2021-04-12 19:15:29 +0200 | [diff] [blame] | 3859 | xecfg.flags = XDL_EMIT_NO_HUNK_HDR; |
| 3860 | if (xdi_diff_outf(&mf1, &mf2, NULL, |
Jeff King | 3b40a09 | 2018-11-02 02:36:06 -0400 | [diff] [blame] | 3861 | diffstat_consume, diffstat, &xpp, &xecfg)) |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 3862 | die("unable to generate diffstat for %s", one->path); |
Matthew Rogers | 1cf3d5d | 2020-08-20 00:41:32 +0000 | [diff] [blame] | 3863 | |
| 3864 | if (DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two)) { |
| 3865 | struct diffstat_file *file = |
| 3866 | diffstat->files[diffstat->nr - 1]; |
| 3867 | /* |
| 3868 | * Omit diffstats of modified files where nothing changed. |
Thomas Guyot-Sionnest | ff0c7fa | 2020-09-24 03:41:41 -0400 | [diff] [blame] | 3869 | * Even if may_differ, this might be the case due to |
Matthew Rogers | 1cf3d5d | 2020-08-20 00:41:32 +0000 | [diff] [blame] | 3870 | * ignoring whitespace changes, etc. |
| 3871 | * |
| 3872 | * But note that we special-case additions, deletions, |
| 3873 | * renames, and mode changes as adding an empty file, |
| 3874 | * for example is still of interest. |
| 3875 | */ |
| 3876 | if ((p->status == DIFF_STATUS_MODIFIED) |
| 3877 | && !file->added |
| 3878 | && !file->deleted |
| 3879 | && one->mode == two->mode) { |
| 3880 | free_diffstat_file(file); |
| 3881 | diffstat->nr--; |
| 3882 | } |
| 3883 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3884 | } |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3885 | |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3886 | diff_free_filespec_data(one); |
| 3887 | diff_free_filespec_data(two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3888 | } |
| 3889 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3890 | static void builtin_checkdiff(const char *name_a, const char *name_b, |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 3891 | const char *attr_path, |
Junio C Hamano | 5ff10dd | 2008-06-26 15:34:54 -0700 | [diff] [blame] | 3892 | struct diff_filespec *one, |
| 3893 | struct diff_filespec *two, |
| 3894 | struct diff_options *o) |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3895 | { |
| 3896 | mmfile_t mf1, mf2; |
| 3897 | struct checkdiff_t data; |
| 3898 | |
| 3899 | if (!two) |
| 3900 | return; |
| 3901 | |
| 3902 | memset(&data, 0, sizeof(data)); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3903 | data.filename = name_b ? name_b : name_a; |
| 3904 | data.lineno = 0; |
Junio C Hamano | 1ba111d | 2008-06-26 15:36:34 -0700 | [diff] [blame] | 3905 | data.o = o; |
Nguyễn Thái Ngọc Duy | 26d024e | 2018-09-21 17:57:37 +0200 | [diff] [blame] | 3906 | data.ws_rule = whitespace_rule(o->repo->index, attr_path); |
Nguyễn Thái Ngọc Duy | 32eaa46 | 2018-09-21 17:57:27 +0200 | [diff] [blame] | 3907 | data.conflict_marker_size = ll_merge_marker_size(o->repo->index, attr_path); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3908 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3909 | if (fill_mmfile(o->repo, &mf1, one) < 0 || |
| 3910 | fill_mmfile(o->repo, &mf2, two) < 0) |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3911 | die("unable to read files to diff"); |
| 3912 | |
Junio C Hamano | 5ff10dd | 2008-06-26 15:34:54 -0700 | [diff] [blame] | 3913 | /* |
| 3914 | * All the other codepaths check both sides, but not checking |
| 3915 | * the "old" side here is deliberate. We are checking the newly |
| 3916 | * introduced changes, and as long as the "new" side is text, we |
| 3917 | * can and should check what it introduces. |
| 3918 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3919 | if (diff_filespec_is_binary(o->repo, two)) |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3920 | goto free_and_return; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3921 | else { |
| 3922 | /* Crazy xdl interfaces.. */ |
| 3923 | xpparam_t xpp; |
| 3924 | xdemitconf_t xecfg; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3925 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 3926 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 3927 | memset(&xecfg, 0, sizeof(xecfg)); |
Junio C Hamano | c35539e | 2008-08-20 11:47:55 -0700 | [diff] [blame] | 3928 | xecfg.ctxlen = 1; /* at least one context line */ |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 3929 | xpp.flags = 0; |
Jeff King | 75ab763 | 2018-11-02 02:39:03 -0400 | [diff] [blame] | 3930 | if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume_hunk, |
| 3931 | checkdiff_consume, &data, |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 3932 | &xpp, &xecfg)) |
| 3933 | die("unable to generate checkdiff for %s", one->path); |
Junio C Hamano | 877f23c | 2008-06-26 15:36:59 -0700 | [diff] [blame] | 3934 | |
Junio C Hamano | 467babf | 2009-09-03 23:39:43 -0700 | [diff] [blame] | 3935 | if (data.ws_rule & WS_BLANK_AT_EOF) { |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 3936 | struct emit_callback ecbdata; |
| 3937 | int blank_at_eof; |
| 3938 | |
| 3939 | ecbdata.ws_rule = data.ws_rule; |
| 3940 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
Christoph Mallon | 8837d33 | 2010-10-10 19:24:06 +0200 | [diff] [blame] | 3941 | blank_at_eof = ecbdata.blank_at_eof_in_postimage; |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 3942 | |
Junio C Hamano | 467babf | 2009-09-03 23:39:43 -0700 | [diff] [blame] | 3943 | if (blank_at_eof) { |
| 3944 | static char *err; |
| 3945 | if (!err) |
| 3946 | err = whitespace_error_string(WS_BLANK_AT_EOF); |
| 3947 | fprintf(o->file, "%s:%d: %s.\n", |
| 3948 | data.filename, blank_at_eof, err); |
| 3949 | data.status = 1; /* report errors */ |
| 3950 | } |
Junio C Hamano | 877f23c | 2008-06-26 15:36:59 -0700 | [diff] [blame] | 3951 | } |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3952 | } |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3953 | free_and_return: |
| 3954 | diff_free_filespec_data(one); |
| 3955 | diff_free_filespec_data(two); |
Wincent Colaiuta | 62c6489 | 2007-12-13 21:24:52 +0100 | [diff] [blame] | 3956 | if (data.status) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3957 | o->flags.check_failed = 1; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3958 | } |
| 3959 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3960 | struct diff_filespec *alloc_filespec(const char *path) |
| 3961 | { |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 3962 | struct diff_filespec *spec; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3963 | |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 3964 | FLEXPTR_ALLOC_STR(spec, path, path); |
Linus Torvalds | 9fb8841 | 2007-10-25 11:19:10 -0700 | [diff] [blame] | 3965 | spec->count = 1; |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3966 | spec->is_binary = -1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3967 | return spec; |
| 3968 | } |
| 3969 | |
Linus Torvalds | 9fb8841 | 2007-10-25 11:19:10 -0700 | [diff] [blame] | 3970 | void free_filespec(struct diff_filespec *spec) |
| 3971 | { |
| 3972 | if (!--spec->count) { |
| 3973 | diff_free_filespec_data(spec); |
| 3974 | free(spec); |
| 3975 | } |
| 3976 | } |
| 3977 | |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 3978 | void fill_filespec(struct diff_filespec *spec, const struct object_id *oid, |
| 3979 | int oid_valid, unsigned short mode) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3980 | { |
| 3981 | if (mode) { |
| 3982 | spec->mode = canon_mode(mode); |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 3983 | oidcpy(&spec->oid, oid); |
| 3984 | spec->oid_valid = oid_valid; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3985 | } |
| 3986 | } |
| 3987 | |
| 3988 | /* |
Jakub Narebski | 5adf317 | 2007-05-26 00:37:40 +0200 | [diff] [blame] | 3989 | * Given a name and sha1 pair, if the index tells us the file in |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3990 | * the work tree has that object contents, return true, so that |
| 3991 | * prepare_temp_file() does not have to inflate and extract. |
| 3992 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3993 | static int reuse_worktree_file(struct index_state *istate, |
| 3994 | const char *name, |
| 3995 | const struct object_id *oid, |
| 3996 | int want_file) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3997 | { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 3998 | const struct cache_entry *ce; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3999 | struct stat st; |
| 4000 | int pos, len; |
| 4001 | |
Junio C Hamano | 150115a | 2009-03-22 15:26:07 -0700 | [diff] [blame] | 4002 | /* |
| 4003 | * We do not read the cache ourselves here, because the |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4004 | * benchmark with my previous version that always reads cache |
| 4005 | * shows that it makes things worse for diff-tree comparing |
| 4006 | * two linux-2.6 kernel trees in an already checked out work |
| 4007 | * tree. This is because most diff-tree comparisons deal with |
| 4008 | * only a small number of files, while reading the cache is |
| 4009 | * expensive for a large project, and its cost outweighs the |
| 4010 | * savings we get by not inflating the object to a temporary |
| 4011 | * file. Practically, this code only helps when we are used |
| 4012 | * by diff-cache --cached, which does read the cache before |
| 4013 | * calling us. |
| 4014 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4015 | if (!istate->cache) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4016 | return 0; |
| 4017 | |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 4018 | /* We want to avoid the working directory if our caller |
| 4019 | * doesn't need the data in a normal file, this system |
| 4020 | * is rather slow with its stat/open/mmap/close syscalls, |
| 4021 | * and the object is contained in a pack file. The pack |
| 4022 | * is probably already open and will be faster to obtain |
| 4023 | * the data through than the working directory. Loose |
| 4024 | * objects however would tend to be slower as they need |
| 4025 | * to be individually opened and inflated. |
| 4026 | */ |
brian m. carlson | 14c3c80 | 2018-05-02 00:25:33 +0000 | [diff] [blame] | 4027 | if (!FAST_WORKING_DIRECTORY && !want_file && has_object_pack(oid)) |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 4028 | return 0; |
| 4029 | |
Jeff King | 06dec43 | 2016-07-22 11:27:53 -0400 | [diff] [blame] | 4030 | /* |
| 4031 | * Similarly, if we'd have to convert the file contents anyway, that |
| 4032 | * makes the optimization not worthwhile. |
| 4033 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4034 | if (!want_file && would_convert_to_git(istate, name)) |
Jeff King | 06dec43 | 2016-07-22 11:27:53 -0400 | [diff] [blame] | 4035 | return 0; |
| 4036 | |
Derrick Stolee | ad90da7 | 2021-09-08 11:23:56 +0000 | [diff] [blame] | 4037 | /* |
| 4038 | * If this path does not match our sparse-checkout definition, |
| 4039 | * then the file will not be in the working directory. |
| 4040 | */ |
| 4041 | if (!path_in_sparse_checkout(name, istate)) |
| 4042 | return 0; |
| 4043 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4044 | len = strlen(name); |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4045 | pos = index_name_pos(istate, name, len); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4046 | if (pos < 0) |
| 4047 | return 0; |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4048 | ce = istate->cache[pos]; |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 4049 | |
| 4050 | /* |
| 4051 | * This is not the sha1 we are looking for, or |
| 4052 | * unreusable because it is not a regular file. |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4053 | */ |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 4054 | if (!oideq(oid, &ce->oid) || !S_ISREG(ce->ce_mode)) |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 4055 | return 0; |
| 4056 | |
| 4057 | /* |
Junio C Hamano | 150115a | 2009-03-22 15:26:07 -0700 | [diff] [blame] | 4058 | * If ce is marked as "assume unchanged", there is no |
| 4059 | * guarantee that work tree matches what we are looking for. |
| 4060 | */ |
Nguyễn Thái Ngọc Duy | b4d1690 | 2009-08-20 20:46:58 +0700 | [diff] [blame] | 4061 | if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) |
Junio C Hamano | 150115a | 2009-03-22 15:26:07 -0700 | [diff] [blame] | 4062 | return 0; |
| 4063 | |
| 4064 | /* |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 4065 | * If ce matches the file in the work tree, we can reuse it. |
| 4066 | */ |
| 4067 | if (ce_uptodate(ce) || |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4068 | (!lstat(name, &st) && !ie_match_stat(istate, ce, &st, 0))) |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 4069 | return 1; |
| 4070 | |
| 4071 | return 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4072 | } |
| 4073 | |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 4074 | static int diff_populate_gitlink(struct diff_filespec *s, int size_only) |
| 4075 | { |
Jeff King | b1ddfb9 | 2016-02-22 17:45:08 -0500 | [diff] [blame] | 4076 | struct strbuf buf = STRBUF_INIT; |
| 4077 | char *dirty = ""; |
Junio C Hamano | 8e08b41 | 2010-01-16 18:42:53 +0100 | [diff] [blame] | 4078 | |
| 4079 | /* Are we looking at the work tree? */ |
Jens Lehmann | 85adbf2 | 2010-03-12 22:23:52 +0100 | [diff] [blame] | 4080 | if (s->dirty_submodule) |
Junio C Hamano | 8e08b41 | 2010-01-16 18:42:53 +0100 | [diff] [blame] | 4081 | dirty = "-dirty"; |
| 4082 | |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 4083 | strbuf_addf(&buf, "Subproject commit %s%s\n", |
| 4084 | oid_to_hex(&s->oid), dirty); |
Jeff King | b1ddfb9 | 2016-02-22 17:45:08 -0500 | [diff] [blame] | 4085 | s->size = buf.len; |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 4086 | if (size_only) { |
| 4087 | s->data = NULL; |
Jeff King | b1ddfb9 | 2016-02-22 17:45:08 -0500 | [diff] [blame] | 4088 | strbuf_release(&buf); |
| 4089 | } else { |
| 4090 | s->data = strbuf_detach(&buf, NULL); |
| 4091 | s->should_free = 1; |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 4092 | } |
| 4093 | return 0; |
| 4094 | } |
| 4095 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4096 | /* |
| 4097 | * While doing rename detection and pickaxe operation, we may need to |
| 4098 | * grab the data for the blob (or file) for our own in-core comparison. |
| 4099 | * diff_filespec has data and size fields for this purpose. |
| 4100 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4101 | int diff_populate_filespec(struct repository *r, |
| 4102 | struct diff_filespec *s, |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4103 | const struct diff_populate_filespec_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4104 | { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4105 | int size_only = options ? options->check_size_only : 0; |
| 4106 | int check_binary = options ? options->check_binary : 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4107 | int err = 0; |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 4108 | int conv_flags = global_conv_flags_eol; |
Junio C Hamano | 5430bb2 | 2013-06-24 14:35:04 -0700 | [diff] [blame] | 4109 | /* |
| 4110 | * demote FAIL to WARN to allow inspecting the situation |
| 4111 | * instead of refusing. |
| 4112 | */ |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 4113 | if (conv_flags & CONV_EOL_RNDTRP_DIE) |
| 4114 | conv_flags = CONV_EOL_RNDTRP_WARN; |
Junio C Hamano | 5430bb2 | 2013-06-24 14:35:04 -0700 | [diff] [blame] | 4115 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4116 | if (!DIFF_FILE_VALID(s)) |
| 4117 | die("internal error: asking to populate invalid file."); |
| 4118 | if (S_ISDIR(s->mode)) |
| 4119 | return -1; |
| 4120 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4121 | if (s->data) |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 4122 | return 0; |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 4123 | |
Junio C Hamano | 6e0b8ed | 2007-05-07 01:14:21 -0700 | [diff] [blame] | 4124 | if (size_only && 0 < s->size) |
| 4125 | return 0; |
| 4126 | |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 4127 | if (S_ISGITLINK(s->mode)) |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 4128 | return diff_populate_gitlink(s, size_only); |
| 4129 | |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4130 | if (!s->oid_valid || |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4131 | reuse_worktree_file(r->index, s->path, &s->oid, 0)) { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 4132 | struct strbuf buf = STRBUF_INIT; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4133 | struct stat st; |
| 4134 | int fd; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 4135 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4136 | if (lstat(s->path, &st) < 0) { |
Andrey Okoshkin | 10e0ca8 | 2017-10-27 12:33:25 +0300 | [diff] [blame] | 4137 | err_empty: |
| 4138 | err = -1; |
| 4139 | empty: |
| 4140 | s->data = (char *)""; |
| 4141 | s->size = 0; |
| 4142 | return err; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4143 | } |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 4144 | s->size = xsize_t(st.st_size); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4145 | if (!s->size) |
| 4146 | goto empty; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4147 | if (S_ISLNK(st.st_mode)) { |
Linus Torvalds | cf219d8 | 2008-12-17 10:26:13 -0800 | [diff] [blame] | 4148 | struct strbuf sb = STRBUF_INIT; |
| 4149 | |
| 4150 | if (strbuf_readlink(&sb, s->path, s->size)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4151 | goto err_empty; |
René Scharfe | 0956a6d | 2008-12-18 17:56:51 +0100 | [diff] [blame] | 4152 | s->size = sb.len; |
| 4153 | s->data = strbuf_detach(&sb, NULL); |
Linus Torvalds | cf219d8 | 2008-12-17 10:26:13 -0800 | [diff] [blame] | 4154 | s->should_free = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4155 | return 0; |
| 4156 | } |
Junio C Hamano | 12426e1 | 2017-03-01 18:04:44 +0100 | [diff] [blame] | 4157 | |
| 4158 | /* |
| 4159 | * Even if the caller would be happy with getting |
| 4160 | * only the size, we cannot return early at this |
| 4161 | * point if the path requires us to run the content |
| 4162 | * conversion. |
| 4163 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4164 | if (size_only && !would_convert_to_git(r->index, s->path)) |
Linus Torvalds | cf219d8 | 2008-12-17 10:26:13 -0800 | [diff] [blame] | 4165 | return 0; |
Junio C Hamano | 12426e1 | 2017-03-01 18:04:44 +0100 | [diff] [blame] | 4166 | |
| 4167 | /* |
| 4168 | * Note: this check uses xsize_t(st.st_size) that may |
| 4169 | * not be the true size of the blob after it goes |
| 4170 | * through convert_to_git(). This may not strictly be |
| 4171 | * correct, but the whole point of big_file_threshold |
| 4172 | * and is_binary check being that we want to avoid |
| 4173 | * opening the file and inspecting the contents, this |
| 4174 | * is probably fine. |
| 4175 | */ |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4176 | if (check_binary && |
Nguyễn Thái Ngọc Duy | 6bf3b81 | 2014-08-16 10:08:05 +0700 | [diff] [blame] | 4177 | s->size > big_file_threshold && s->is_binary == -1) { |
| 4178 | s->is_binary = 1; |
| 4179 | return 0; |
| 4180 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4181 | fd = open(s->path, O_RDONLY); |
| 4182 | if (fd < 0) |
| 4183 | goto err_empty; |
Shawn O. Pearce | c4712e4 | 2006-12-24 00:47:23 -0500 | [diff] [blame] | 4184 | s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4185 | close(fd); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4186 | s->should_munmap = 1; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 4187 | |
| 4188 | /* |
| 4189 | * Convert from working tree format to canonical git format |
| 4190 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4191 | if (convert_to_git(r->index, s->path, s->data, s->size, &buf, conv_flags)) { |
René Scharfe | c32f749 | 2007-10-21 11:23:49 +0200 | [diff] [blame] | 4192 | size_t size = 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 4193 | munmap(s->data, s->size); |
| 4194 | s->should_munmap = 0; |
René Scharfe | c32f749 | 2007-10-21 11:23:49 +0200 | [diff] [blame] | 4195 | s->data = strbuf_detach(&buf, &size); |
| 4196 | s->size = size; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 4197 | s->should_free = 1; |
| 4198 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4199 | } |
| 4200 | else { |
Jonathan Tan | c14b6f8 | 2020-04-07 15:11:42 -0700 | [diff] [blame] | 4201 | struct object_info info = { |
| 4202 | .sizep = &s->size |
| 4203 | }; |
| 4204 | |
| 4205 | if (!(size_only || check_binary)) |
| 4206 | /* |
| 4207 | * Set contentp, since there is no chance that merely |
| 4208 | * the size is sufficient. |
| 4209 | */ |
| 4210 | info.contentp = &s->data; |
| 4211 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 4212 | if (options && options->missing_object_cb) { |
| 4213 | if (!oid_object_info_extended(r, &s->oid, &info, |
| 4214 | OBJECT_INFO_LOOKUP_REPLACE | |
| 4215 | OBJECT_INFO_SKIP_FETCH_OBJECT)) |
| 4216 | goto object_read; |
| 4217 | options->missing_object_cb(options->missing_object_data); |
| 4218 | } |
Jonathan Tan | c14b6f8 | 2020-04-07 15:11:42 -0700 | [diff] [blame] | 4219 | if (oid_object_info_extended(r, &s->oid, &info, |
| 4220 | OBJECT_INFO_LOOKUP_REPLACE)) |
| 4221 | die("unable to read %s", oid_to_hex(&s->oid)); |
| 4222 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 4223 | object_read: |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4224 | if (size_only || check_binary) { |
Nguyễn Thái Ngọc Duy | 6bf3b81 | 2014-08-16 10:08:05 +0700 | [diff] [blame] | 4225 | if (size_only) |
| 4226 | return 0; |
| 4227 | if (s->size > big_file_threshold && s->is_binary == -1) { |
| 4228 | s->is_binary = 1; |
| 4229 | return 0; |
| 4230 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4231 | } |
Jonathan Tan | c14b6f8 | 2020-04-07 15:11:42 -0700 | [diff] [blame] | 4232 | if (!info.contentp) { |
| 4233 | info.contentp = &s->data; |
| 4234 | if (oid_object_info_extended(r, &s->oid, &info, |
| 4235 | OBJECT_INFO_LOOKUP_REPLACE)) |
| 4236 | die("unable to read %s", oid_to_hex(&s->oid)); |
| 4237 | } |
Nguyễn Thái Ngọc Duy | 6bf3b81 | 2014-08-16 10:08:05 +0700 | [diff] [blame] | 4238 | s->should_free = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4239 | } |
| 4240 | return 0; |
| 4241 | } |
| 4242 | |
Junio C Hamano | 8ae92e6 | 2007-10-02 21:01:03 -0700 | [diff] [blame] | 4243 | void diff_free_filespec_blob(struct diff_filespec *s) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4244 | { |
| 4245 | if (s->should_free) |
| 4246 | free(s->data); |
| 4247 | else if (s->should_munmap) |
| 4248 | munmap(s->data, s->size); |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 4249 | |
| 4250 | if (s->should_free || s->should_munmap) { |
| 4251 | s->should_free = s->should_munmap = 0; |
| 4252 | s->data = NULL; |
| 4253 | } |
Jeff King | eede7b7 | 2007-09-25 15:29:42 -0400 | [diff] [blame] | 4254 | } |
| 4255 | |
| 4256 | void diff_free_filespec_data(struct diff_filespec *s) |
| 4257 | { |
Jinoh Kang | 2469593 | 2020-11-06 17:14:52 +0000 | [diff] [blame] | 4258 | if (!s) |
| 4259 | return; |
| 4260 | |
Junio C Hamano | 8ae92e6 | 2007-10-02 21:01:03 -0700 | [diff] [blame] | 4261 | diff_free_filespec_blob(s); |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 4262 | FREE_AND_NULL(s->cnt_data); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4263 | } |
| 4264 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4265 | static void prep_temp_blob(struct index_state *istate, |
| 4266 | const char *path, struct diff_tempfile *temp, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4267 | void *blob, |
| 4268 | unsigned long size, |
brian m. carlson | 09bdff2 | 2016-06-24 23:09:29 +0000 | [diff] [blame] | 4269 | const struct object_id *oid, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4270 | int mode) |
| 4271 | { |
Johannes Schindelin | 4e218f5 | 2009-03-21 12:42:52 +0100 | [diff] [blame] | 4272 | struct strbuf buf = STRBUF_INIT; |
David Aguilar | 003b33a | 2009-05-31 01:35:52 -0700 | [diff] [blame] | 4273 | char *path_dup = xstrdup(path); |
| 4274 | const char *base = basename(path_dup); |
brian m. carlson | c397aac | 2020-03-16 18:05:03 +0000 | [diff] [blame] | 4275 | struct checkout_metadata meta; |
| 4276 | |
| 4277 | init_checkout_metadata(&meta, NULL, NULL, oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4278 | |
René Scharfe | 8af7593 | 2022-04-20 22:30:10 +0200 | [diff] [blame] | 4279 | temp->tempfile = mks_tempfile_dt("git-blob-XXXXXX", base); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 4280 | if (!temp->tempfile) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 4281 | die_errno("unable to create temp-file"); |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4282 | if (convert_to_working_tree(istate, path, |
brian m. carlson | c397aac | 2020-03-16 18:05:03 +0000 | [diff] [blame] | 4283 | (const char *)blob, (size_t)size, &buf, &meta)) { |
Johannes Schindelin | 4e218f5 | 2009-03-21 12:42:52 +0100 | [diff] [blame] | 4284 | blob = buf.buf; |
| 4285 | size = buf.len; |
| 4286 | } |
Junio C Hamano | c50424a | 2017-09-25 15:24:06 +0900 | [diff] [blame] | 4287 | if (write_in_full(temp->tempfile->fd, blob, size) < 0 || |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 4288 | close_tempfile_gently(temp->tempfile)) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 4289 | die_errno("unable to write temp-file"); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 4290 | temp->name = get_tempfile_path(temp->tempfile); |
brian m. carlson | 09bdff2 | 2016-06-24 23:09:29 +0000 | [diff] [blame] | 4291 | oid_to_hex_r(temp->hex, oid); |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 4292 | xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode); |
Johannes Schindelin | 4e218f5 | 2009-03-21 12:42:52 +0100 | [diff] [blame] | 4293 | strbuf_release(&buf); |
David Aguilar | 003b33a | 2009-05-31 01:35:52 -0700 | [diff] [blame] | 4294 | free(path_dup); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4295 | } |
| 4296 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4297 | static struct diff_tempfile *prepare_temp_file(struct repository *r, |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4298 | struct diff_filespec *one) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4299 | { |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 4300 | struct diff_tempfile *temp = claim_diff_tempfile(); |
| 4301 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4302 | if (!DIFF_FILE_VALID(one)) { |
| 4303 | not_a_valid_file: |
| 4304 | /* A '-' entry produces this for file-2, and |
| 4305 | * a '+' entry produces this for file-1. |
| 4306 | */ |
| 4307 | temp->name = "/dev/null"; |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 4308 | xsnprintf(temp->hex, sizeof(temp->hex), "."); |
| 4309 | xsnprintf(temp->mode, sizeof(temp->mode), "."); |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 4310 | return temp; |
| 4311 | } |
| 4312 | |
Thomas Rast | aba4727 | 2014-02-16 17:52:34 +0100 | [diff] [blame] | 4313 | if (!S_ISGITLINK(one->mode) && |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4314 | (!one->oid_valid || |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4315 | reuse_worktree_file(r->index, one->path, &one->oid, 1))) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4316 | struct stat st; |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4317 | if (lstat(one->path, &st) < 0) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4318 | if (errno == ENOENT) |
| 4319 | goto not_a_valid_file; |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4320 | die_errno("stat(%s)", one->path); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4321 | } |
| 4322 | if (S_ISLNK(st.st_mode)) { |
Jeff King | 3cd7388 | 2009-05-25 06:46:09 -0400 | [diff] [blame] | 4323 | struct strbuf sb = STRBUF_INIT; |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4324 | if (strbuf_readlink(&sb, one->path, st.st_size) < 0) |
| 4325 | die_errno("readlink(%s)", one->path); |
| 4326 | prep_temp_blob(r->index, one->path, temp, sb.buf, sb.len, |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4327 | (one->oid_valid ? |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 4328 | &one->oid : null_oid()), |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4329 | (one->oid_valid ? |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4330 | one->mode : S_IFLNK)); |
Jeff King | 3cd7388 | 2009-05-25 06:46:09 -0400 | [diff] [blame] | 4331 | strbuf_release(&sb); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4332 | } |
| 4333 | else { |
| 4334 | /* we can borrow from the file in the work tree */ |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4335 | temp->name = one->path; |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4336 | if (!one->oid_valid) |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 4337 | oid_to_hex_r(temp->hex, null_oid()); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4338 | else |
René Scharfe | 2490574 | 2017-01-28 23:03:03 +0100 | [diff] [blame] | 4339 | oid_to_hex_r(temp->hex, &one->oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4340 | /* Even though we may sometimes borrow the |
| 4341 | * contents from the work tree, we always want |
| 4342 | * one->mode. mode is trustworthy even when |
Brandon Williams | 7401415 | 2017-05-30 10:30:52 -0700 | [diff] [blame] | 4343 | * !(one->oid_valid), as long as |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4344 | * DIFF_FILE_VALID(one). |
| 4345 | */ |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 4346 | xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4347 | } |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 4348 | return temp; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4349 | } |
| 4350 | else { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4351 | if (diff_populate_filespec(r, one, NULL)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4352 | die("cannot read data blob for %s", one->path); |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4353 | prep_temp_blob(r->index, one->path, temp, |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4354 | one->data, one->size, |
brian m. carlson | 09bdff2 | 2016-06-24 23:09:29 +0000 | [diff] [blame] | 4355 | &one->oid, one->mode); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4356 | } |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 4357 | return temp; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4358 | } |
| 4359 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4360 | static void add_external_diff_name(struct repository *r, |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 4361 | struct strvec *argv, |
Jeff King | f3efe78 | 2014-04-19 15:22:25 -0400 | [diff] [blame] | 4362 | struct diff_filespec *df) |
| 4363 | { |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4364 | struct diff_tempfile *temp = prepare_temp_file(r, df); |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 4365 | strvec_push(argv, temp->name); |
| 4366 | strvec_push(argv, temp->hex); |
| 4367 | strvec_push(argv, temp->mode); |
Jeff King | f3efe78 | 2014-04-19 15:22:25 -0400 | [diff] [blame] | 4368 | } |
| 4369 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4370 | /* An external diff command takes: |
| 4371 | * |
| 4372 | * diff-cmd name infile1 infile1-sha1 infile1-mode \ |
| 4373 | * infile2 infile2-sha1 infile2-mode [ rename-to ] |
| 4374 | * |
| 4375 | */ |
| 4376 | static void run_external_diff(const char *pgm, |
| 4377 | const char *name, |
| 4378 | const char *other, |
| 4379 | struct diff_filespec *one, |
| 4380 | struct diff_filespec *two, |
| 4381 | const char *xfrm_msg, |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4382 | struct diff_options *o) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4383 | { |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 4384 | struct child_process cmd = CHILD_PROCESS_INIT; |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4385 | struct diff_queue_struct *q = &diff_queued_diff; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4386 | |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 4387 | strvec_push(&cmd.args, pgm); |
| 4388 | strvec_push(&cmd.args, name); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4389 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4390 | if (one && two) { |
Jeff King | de8f14e | 2023-01-06 06:04:18 -0500 | [diff] [blame] | 4391 | add_external_diff_name(o->repo, &cmd.args, one); |
| 4392 | add_external_diff_name(o->repo, &cmd.args, two); |
| 4393 | if (other) { |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 4394 | strvec_push(&cmd.args, other); |
Jeff King | 85a9a63 | 2024-01-28 20:57:08 -0500 | [diff] [blame] | 4395 | if (xfrm_msg) |
| 4396 | strvec_push(&cmd.args, xfrm_msg); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4397 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4398 | } |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4399 | |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 4400 | strvec_pushf(&cmd.env, "GIT_DIFF_PATH_COUNTER=%d", |
| 4401 | ++o->diff_path_counter); |
| 4402 | strvec_pushf(&cmd.env, "GIT_DIFF_PATH_TOTAL=%d", q->nr); |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4403 | |
Johannes Schindelin | 3aef54e | 2019-07-11 01:23:41 -0700 | [diff] [blame] | 4404 | diff_free_filespec_data(one); |
| 4405 | diff_free_filespec_data(two); |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 4406 | cmd.use_shell = 1; |
| 4407 | if (run_command(&cmd)) |
Jeff King | 89294d1 | 2014-04-19 15:19:19 -0400 | [diff] [blame] | 4408 | die(_("external diff died, stopping at %s"), name); |
| 4409 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4410 | remove_tempfile(); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4411 | } |
| 4412 | |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4413 | static int similarity_index(struct diff_filepair *p) |
| 4414 | { |
| 4415 | return p->score * 100 / MAX_SCORE; |
| 4416 | } |
| 4417 | |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 4418 | static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev) |
| 4419 | { |
| 4420 | if (startup_info->have_repository) |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 4421 | return repo_find_unique_abbrev(the_repository, oid, abbrev); |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 4422 | else { |
| 4423 | char *hex = oid_to_hex(oid); |
Junio C Hamano | 0d9c527 | 2016-10-27 14:58:48 -0700 | [diff] [blame] | 4424 | if (abbrev < 0) |
| 4425 | abbrev = FALLBACK_DEFAULT_ABBREV; |
brian m. carlson | 02afca1 | 2018-07-16 01:28:05 +0000 | [diff] [blame] | 4426 | if (abbrev > the_hash_algo->hexsz) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 4427 | BUG("oid abbreviation out of range: %d", abbrev); |
Jack Bates | 43d1948 | 2016-12-06 09:56:14 -0700 | [diff] [blame] | 4428 | if (abbrev) |
| 4429 | hex[abbrev] = '\0'; |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 4430 | return hex; |
| 4431 | } |
| 4432 | } |
| 4433 | |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4434 | static void fill_metainfo(struct strbuf *msg, |
| 4435 | const char *name, |
| 4436 | const char *other, |
| 4437 | struct diff_filespec *one, |
| 4438 | struct diff_filespec *two, |
| 4439 | struct diff_options *o, |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4440 | struct diff_filepair *p, |
Junio C Hamano | 5977744 | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4441 | int *must_show_header, |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4442 | int use_color) |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4443 | { |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4444 | const char *set = diff_get_color(use_color, DIFF_METAINFO); |
| 4445 | const char *reset = diff_get_color(use_color, DIFF_RESET); |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 4446 | const char *line_prefix = diff_line_prefix(o); |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 4447 | struct string_list *more_headers = NULL; |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 4448 | |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 4449 | *must_show_header = 1; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4450 | strbuf_init(msg, PATH_MAX * 2 + 300); |
| 4451 | switch (p->status) { |
| 4452 | case DIFF_STATUS_COPIED: |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4453 | strbuf_addf(msg, "%s%ssimilarity index %d%%", |
| 4454 | line_prefix, set, similarity_index(p)); |
| 4455 | strbuf_addf(msg, "%s\n%s%scopy from ", |
| 4456 | reset, line_prefix, set); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4457 | quote_c_style(name, msg, NULL, 0); |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4458 | strbuf_addf(msg, "%s\n%s%scopy to ", reset, line_prefix, set); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4459 | quote_c_style(other, msg, NULL, 0); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4460 | strbuf_addf(msg, "%s\n", reset); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4461 | break; |
| 4462 | case DIFF_STATUS_RENAMED: |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4463 | strbuf_addf(msg, "%s%ssimilarity index %d%%", |
| 4464 | line_prefix, set, similarity_index(p)); |
| 4465 | strbuf_addf(msg, "%s\n%s%srename from ", |
| 4466 | reset, line_prefix, set); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4467 | quote_c_style(name, msg, NULL, 0); |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4468 | strbuf_addf(msg, "%s\n%s%srename to ", |
| 4469 | reset, line_prefix, set); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4470 | quote_c_style(other, msg, NULL, 0); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4471 | strbuf_addf(msg, "%s\n", reset); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4472 | break; |
| 4473 | case DIFF_STATUS_MODIFIED: |
| 4474 | if (p->score) { |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4475 | strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n", |
| 4476 | line_prefix, |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4477 | set, similarity_index(p), reset); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4478 | break; |
| 4479 | } |
| 4480 | /* fallthru */ |
| 4481 | default: |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 4482 | *must_show_header = 0; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4483 | } |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 4484 | if ((more_headers = additional_headers(o, name))) { |
| 4485 | add_formatted_headers(msg, more_headers, |
| 4486 | line_prefix, set, reset); |
| 4487 | *must_show_header = 1; |
| 4488 | } |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 4489 | if (one && two && !oideq(&one->oid, &two->oid)) { |
brian m. carlson | de1d81d | 2018-05-02 00:25:52 +0000 | [diff] [blame] | 4490 | const unsigned hexsz = the_hash_algo->hexsz; |
Đoàn Trần Công Danh | 3046c7f | 2020-08-21 18:51:47 +0700 | [diff] [blame] | 4491 | int abbrev = o->abbrev ? o->abbrev : DEFAULT_ABBREV; |
| 4492 | |
| 4493 | if (o->flags.full_index) |
| 4494 | abbrev = hexsz; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4495 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4496 | if (o->flags.binary) { |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4497 | mmfile_t mf; |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4498 | if ((!fill_mmfile(o->repo, &mf, one) && |
| 4499 | diff_filespec_is_binary(o->repo, one)) || |
| 4500 | (!fill_mmfile(o->repo, &mf, two) && |
| 4501 | diff_filespec_is_binary(o->repo, two))) |
brian m. carlson | de1d81d | 2018-05-02 00:25:52 +0000 | [diff] [blame] | 4502 | abbrev = hexsz; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4503 | } |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 4504 | strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set, |
| 4505 | diff_abbrev_oid(&one->oid, abbrev), |
| 4506 | diff_abbrev_oid(&two->oid, abbrev)); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4507 | if (one->mode == two->mode) |
| 4508 | strbuf_addf(msg, " %06o", one->mode); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4509 | strbuf_addf(msg, "%s\n", reset); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4510 | } |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4511 | } |
| 4512 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4513 | static void run_diff_cmd(const char *pgm, |
| 4514 | const char *name, |
| 4515 | const char *other, |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4516 | const char *attr_path, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4517 | struct diff_filespec *one, |
| 4518 | struct diff_filespec *two, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4519 | struct strbuf *msg, |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 4520 | struct diff_options *o, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4521 | struct diff_filepair *p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4522 | { |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4523 | const char *xfrm_msg = NULL; |
| 4524 | int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 4525 | int must_show_header = 0; |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4526 | struct userdiff_driver *drv = NULL; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4527 | |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4528 | if (o->flags.allow_external || !o->ignore_driver_algorithm) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 4529 | drv = userdiff_find_by_path(o->repo->index, attr_path); |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4530 | |
| 4531 | if (o->flags.allow_external && drv && drv->external) |
| 4532 | pgm = drv->external; |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 4533 | |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4534 | if (msg) { |
| 4535 | /* |
| 4536 | * don't use colors when the header is intended for an |
| 4537 | * external diff driver |
| 4538 | */ |
| 4539 | fill_metainfo(msg, name, other, one, two, o, p, |
Junio C Hamano | 5977744 | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4540 | &must_show_header, |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 4541 | want_color(o->use_color) && !pgm); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4542 | xfrm_msg = msg->len ? msg->buf : NULL; |
| 4543 | } |
| 4544 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4545 | if (pgm) { |
Jeff King | 4bc1792 | 2019-02-14 00:49:42 -0500 | [diff] [blame] | 4546 | run_external_diff(pgm, name, other, one, two, xfrm_msg, o); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4547 | return; |
| 4548 | } |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4549 | if (one && two) { |
| 4550 | if (!o->ignore_driver_algorithm && drv && drv->algorithm) |
| 4551 | set_diff_algorithm(o, drv->algorithm); |
| 4552 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4553 | builtin_diff(name, other ? other : name, |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 4554 | one, two, xfrm_msg, must_show_header, |
| 4555 | o, complete_rewrite); |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4556 | } else { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 4557 | fprintf(o->file, "* Unmerged path %s\n", name); |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4558 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4559 | } |
| 4560 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4561 | static void diff_fill_oid_info(struct diff_filespec *one, struct index_state *istate) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4562 | { |
| 4563 | if (DIFF_FILE_VALID(one)) { |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4564 | if (!one->oid_valid) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4565 | struct stat st; |
Junio C Hamano | 4682d85 | 2012-06-27 20:14:47 -0700 | [diff] [blame] | 4566 | if (one->is_stdin) { |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 4567 | oidclr(&one->oid); |
Johannes Schindelin | 5332b2a | 2007-02-25 23:36:10 +0100 | [diff] [blame] | 4568 | return; |
| 4569 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4570 | if (lstat(one->path, &st) < 0) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 4571 | die_errno("stat '%s'", one->path); |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4572 | if (index_path(istate, &one->oid, one->path, &st, 0)) |
Alexander Potashev | d753070 | 2009-01-04 21:38:41 +0300 | [diff] [blame] | 4573 | die("cannot hash %s", one->path); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4574 | } |
| 4575 | } |
| 4576 | else |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 4577 | oidclr(&one->oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4578 | } |
| 4579 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4580 | static void strip_prefix(int prefix_length, const char **namep, const char **otherp) |
| 4581 | { |
| 4582 | /* Strip the prefix but do not molest /dev/null and absolute paths */ |
Johannes Sixt | ffd04e9 | 2018-10-19 18:58:07 +0200 | [diff] [blame] | 4583 | if (*namep && !is_absolute_path(*namep)) { |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4584 | *namep += prefix_length; |
Jakub Narebski | d8faea9 | 2010-08-09 10:50:53 -0400 | [diff] [blame] | 4585 | if (**namep == '/') |
| 4586 | ++*namep; |
| 4587 | } |
Johannes Sixt | ffd04e9 | 2018-10-19 18:58:07 +0200 | [diff] [blame] | 4588 | if (*otherp && !is_absolute_path(*otherp)) { |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4589 | *otherp += prefix_length; |
Jakub Narebski | d8faea9 | 2010-08-09 10:50:53 -0400 | [diff] [blame] | 4590 | if (**otherp == '/') |
| 4591 | ++*otherp; |
| 4592 | } |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4593 | } |
| 4594 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4595 | static void run_diff(struct diff_filepair *p, struct diff_options *o) |
| 4596 | { |
| 4597 | const char *pgm = external_diff(); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 4598 | struct strbuf msg; |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 4599 | struct diff_filespec *one = p->one; |
| 4600 | struct diff_filespec *two = p->two; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4601 | const char *name; |
| 4602 | const char *other; |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4603 | const char *attr_path; |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 4604 | |
Stefan Beller | f2d2a5d | 2017-06-29 17:06:46 -0700 | [diff] [blame] | 4605 | name = one->path; |
| 4606 | other = (strcmp(name, two->path) ? two->path : NULL); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4607 | attr_path = name; |
| 4608 | if (o->prefix_length) |
| 4609 | strip_prefix(o->prefix_length, &name, &other); |
| 4610 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4611 | if (!o->flags.allow_external) |
Junio C Hamano | bd8c1a9 | 2012-07-17 22:08:59 -0700 | [diff] [blame] | 4612 | pgm = NULL; |
| 4613 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4614 | if (DIFF_PAIR_UNMERGED(p)) { |
| 4615 | run_diff_cmd(pgm, name, NULL, attr_path, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4616 | NULL, NULL, NULL, o, p); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4617 | return; |
| 4618 | } |
| 4619 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4620 | diff_fill_oid_info(one, o->repo->index); |
| 4621 | diff_fill_oid_info(two, o->repo->index); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4622 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4623 | if (!pgm && |
| 4624 | DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) && |
| 4625 | (S_IFMT & one->mode) != (S_IFMT & two->mode)) { |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4626 | /* |
| 4627 | * a filepair that changes between file and symlink |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4628 | * needs to be split into deletion and creation. |
| 4629 | */ |
| 4630 | struct diff_filespec *null = alloc_filespec(two->path); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4631 | run_diff_cmd(NULL, name, other, attr_path, |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4632 | one, null, &msg, |
| 4633 | o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4634 | free(null); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4635 | strbuf_release(&msg); |
| 4636 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4637 | null = alloc_filespec(one->path); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4638 | run_diff_cmd(NULL, name, other, attr_path, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4639 | null, two, &msg, o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4640 | free(null); |
| 4641 | } |
| 4642 | else |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4643 | run_diff_cmd(pgm, name, other, attr_path, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4644 | one, two, &msg, o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4645 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 4646 | strbuf_release(&msg); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4647 | } |
| 4648 | |
| 4649 | static void run_diffstat(struct diff_filepair *p, struct diff_options *o, |
| 4650 | struct diffstat_t *diffstat) |
| 4651 | { |
| 4652 | const char *name; |
| 4653 | const char *other; |
| 4654 | |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4655 | if (!o->ignore_driver_algorithm) { |
| 4656 | struct userdiff_driver *drv = userdiff_find_by_path(o->repo->index, |
| 4657 | p->one->path); |
| 4658 | |
| 4659 | if (drv && drv->algorithm) |
| 4660 | set_diff_algorithm(o, drv->algorithm); |
| 4661 | } |
| 4662 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4663 | if (DIFF_PAIR_UNMERGED(p)) { |
| 4664 | /* unmerged */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4665 | builtin_diffstat(p->one->path, NULL, NULL, NULL, |
| 4666 | diffstat, o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4667 | return; |
| 4668 | } |
| 4669 | |
| 4670 | name = p->one->path; |
| 4671 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); |
| 4672 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4673 | if (o->prefix_length) |
| 4674 | strip_prefix(o->prefix_length, &name, &other); |
| 4675 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4676 | diff_fill_oid_info(p->one, o->repo->index); |
| 4677 | diff_fill_oid_info(p->two, o->repo->index); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4678 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4679 | builtin_diffstat(name, other, p->one, p->two, |
| 4680 | diffstat, o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4681 | } |
| 4682 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4683 | static void run_checkdiff(struct diff_filepair *p, struct diff_options *o) |
| 4684 | { |
| 4685 | const char *name; |
| 4686 | const char *other; |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4687 | const char *attr_path; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4688 | |
| 4689 | if (DIFF_PAIR_UNMERGED(p)) { |
| 4690 | /* unmerged */ |
| 4691 | return; |
| 4692 | } |
| 4693 | |
| 4694 | name = p->one->path; |
| 4695 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4696 | attr_path = other ? other : name; |
| 4697 | |
| 4698 | if (o->prefix_length) |
| 4699 | strip_prefix(o->prefix_length, &name, &other); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4700 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4701 | diff_fill_oid_info(p->one, o->repo->index); |
| 4702 | diff_fill_oid_info(p->two, o->repo->index); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4703 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4704 | builtin_checkdiff(name, other, attr_path, p->one, p->two, o); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4705 | } |
| 4706 | |
Nguyễn Thái Ngọc Duy | e675765 | 2018-09-21 17:57:24 +0200 | [diff] [blame] | 4707 | void repo_diff_setup(struct repository *r, struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4708 | { |
Johannes Schindelin | be4f2b4 | 2010-08-05 10:49:55 +0200 | [diff] [blame] | 4709 | memcpy(options, &default_diff_options, sizeof(*options)); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 4710 | |
| 4711 | options->file = stdout; |
Nguyễn Thái Ngọc Duy | e675765 | 2018-09-21 17:57:24 +0200 | [diff] [blame] | 4712 | options->repo = r; |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 4713 | |
Stefan Beller | 7648b79 | 2018-08-17 13:43:52 -0700 | [diff] [blame] | 4714 | options->output_indicators[OUTPUT_INDICATOR_NEW] = '+'; |
| 4715 | options->output_indicators[OUTPUT_INDICATOR_OLD] = '-'; |
| 4716 | options->output_indicators[OUTPUT_INDICATOR_CONTEXT] = ' '; |
Jack Bates | 43d1948 | 2016-12-06 09:56:14 -0700 | [diff] [blame] | 4717 | options->abbrev = DEFAULT_ABBREV; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4718 | options->line_termination = '\n'; |
| 4719 | options->break_opt = -1; |
| 4720 | options->rename_limit = -1; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 4721 | options->dirstat_permille = diff_dirstat_permille_default; |
Jeff Muizelaar | 6468a4e | 2012-09-27 15:12:52 -0400 | [diff] [blame] | 4722 | options->context = diff_context_default; |
Vegard Nossum | c488867 | 2017-01-12 13:21:11 +0100 | [diff] [blame] | 4723 | options->interhunkcontext = diff_interhunk_context_default; |
Junio C Hamano | a17505f | 2016-10-04 15:26:27 -0700 | [diff] [blame] | 4724 | options->ws_error_highlight = ws_error_highlight_default; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4725 | options->flags.rename_empty = 1; |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 4726 | options->flags.relative_name = diff_relative; |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 4727 | options->objfind = NULL; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4728 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 4729 | /* pathchange left =NULL by default */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4730 | options->change = diff_change; |
| 4731 | options->add_remove = diff_addremove; |
Jeff King | f1c9626 | 2011-08-17 22:03:12 -0700 | [diff] [blame] | 4732 | options->use_color = diff_use_color_default; |
Eric Wong | b68ea12 | 2006-07-07 04:01:23 -0700 | [diff] [blame] | 4733 | options->detect_rename = diff_detect_rename_default; |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 4734 | options->xdl_opts |= diff_algorithm; |
Michael Haggerty | 433860f | 2016-09-05 11:44:51 +0200 | [diff] [blame] | 4735 | if (diff_indent_heuristic) |
| 4736 | DIFF_XDL_SET(options, INDENT_HEURISTIC); |
Johannes Schindelin | eab9a40 | 2007-12-18 19:32:14 +0000 | [diff] [blame] | 4737 | |
Samuel Bronson | 6d8940b | 2013-12-18 19:08:12 -0500 | [diff] [blame] | 4738 | options->orderfile = diff_order_file_cfg; |
| 4739 | |
Sangeeta Jain | 8ef9312 | 2020-11-10 14:09:00 +0530 | [diff] [blame] | 4740 | if (!options->flags.ignore_submodule_set) |
| 4741 | options->flags.ignore_untracked_in_submodules = 1; |
| 4742 | |
Eli Collins | f89504d | 2010-05-02 19:03:41 -0700 | [diff] [blame] | 4743 | if (diff_no_prefix) { |
Jeff King | 6799aad | 2023-03-09 01:07:06 -0500 | [diff] [blame] | 4744 | diff_set_noprefix(options); |
Eli Collins | f89504d | 2010-05-02 19:03:41 -0700 | [diff] [blame] | 4745 | } else if (!diff_mnemonic_prefix) { |
Jeff King | 6799aad | 2023-03-09 01:07:06 -0500 | [diff] [blame] | 4746 | diff_set_default_prefix(options); |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 4747 | } |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 4748 | |
| 4749 | options->color_moved = diff_color_moved_default; |
Stefan Beller | 626c0b5 | 2018-07-18 12:31:56 -0700 | [diff] [blame] | 4750 | options->color_moved_ws_handling = diff_color_moved_ws_default; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4751 | } |
| 4752 | |
Johannes Schindelin | 4d4d4ea | 2022-01-28 12:02:49 +0000 | [diff] [blame] | 4753 | static const char diff_status_letters[] = { |
| 4754 | DIFF_STATUS_ADDED, |
| 4755 | DIFF_STATUS_COPIED, |
| 4756 | DIFF_STATUS_DELETED, |
| 4757 | DIFF_STATUS_MODIFIED, |
| 4758 | DIFF_STATUS_RENAMED, |
| 4759 | DIFF_STATUS_TYPE_CHANGED, |
| 4760 | DIFF_STATUS_UNKNOWN, |
| 4761 | DIFF_STATUS_UNMERGED, |
| 4762 | DIFF_STATUS_FILTER_AON, |
| 4763 | DIFF_STATUS_FILTER_BROKEN, |
| 4764 | '\0', |
| 4765 | }; |
| 4766 | |
| 4767 | static unsigned int filter_bit['Z' + 1]; |
| 4768 | |
| 4769 | static void prepare_filter_bits(void) |
| 4770 | { |
| 4771 | int i; |
| 4772 | |
| 4773 | if (!filter_bit[DIFF_STATUS_ADDED]) { |
| 4774 | for (i = 0; diff_status_letters[i]; i++) |
| 4775 | filter_bit[(int) diff_status_letters[i]] = (1 << i); |
| 4776 | } |
| 4777 | } |
| 4778 | |
| 4779 | static unsigned filter_bit_tst(char status, const struct diff_options *opt) |
| 4780 | { |
| 4781 | return opt->filter & filter_bit[(int) status]; |
| 4782 | } |
| 4783 | |
| 4784 | unsigned diff_filter_bit(char status) |
| 4785 | { |
| 4786 | prepare_filter_bits(); |
| 4787 | return filter_bit[(int) status]; |
| 4788 | } |
| 4789 | |
Jeff King | 9eac595 | 2023-06-01 13:41:06 -0400 | [diff] [blame] | 4790 | int diff_check_follow_pathspec(struct pathspec *ps, int die_on_error) |
| 4791 | { |
Jeff King | 8260bc5 | 2023-06-01 13:43:51 -0400 | [diff] [blame] | 4792 | unsigned forbidden_magic; |
| 4793 | |
Jeff King | 9eac595 | 2023-06-01 13:41:06 -0400 | [diff] [blame] | 4794 | if (ps->nr != 1) { |
| 4795 | if (die_on_error) |
| 4796 | die(_("--follow requires exactly one pathspec")); |
| 4797 | return 0; |
| 4798 | } |
Jeff King | 8260bc5 | 2023-06-01 13:43:51 -0400 | [diff] [blame] | 4799 | |
| 4800 | forbidden_magic = ps->items[0].magic & ~(PATHSPEC_FROMTOP | |
| 4801 | PATHSPEC_LITERAL); |
| 4802 | if (forbidden_magic) { |
| 4803 | if (die_on_error) { |
| 4804 | struct strbuf sb = STRBUF_INIT; |
| 4805 | pathspec_magic_names(forbidden_magic, &sb); |
| 4806 | die(_("pathspec magic not supported by --follow: %s"), |
| 4807 | sb.buf); |
| 4808 | } |
| 4809 | return 0; |
| 4810 | } |
| 4811 | |
Jeff King | 9eac595 | 2023-06-01 13:41:06 -0400 | [diff] [blame] | 4812 | return 1; |
| 4813 | } |
| 4814 | |
Thomas Rast | 2845265 | 2012-08-03 14:16:24 +0200 | [diff] [blame] | 4815 | void diff_setup_done(struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4816 | { |
Stefan Beller | 4d8c51a | 2018-01-04 14:50:44 -0800 | [diff] [blame] | 4817 | unsigned check_mask = DIFF_FORMAT_NAME | |
| 4818 | DIFF_FORMAT_NAME_STATUS | |
| 4819 | DIFF_FORMAT_CHECKDIFF | |
| 4820 | DIFF_FORMAT_NO_OUTPUT; |
brian m. carlson | de1d81d | 2018-05-02 00:25:52 +0000 | [diff] [blame] | 4821 | /* |
| 4822 | * This must be signed because we're comparing against a potentially |
| 4823 | * negative value. |
| 4824 | */ |
| 4825 | const int hexsz = the_hash_algo->hexsz; |
Timo Hirvonen | d7de00f | 2006-06-24 20:26:49 +0300 | [diff] [blame] | 4826 | |
Junio C Hamano | 6c37400 | 2013-05-10 17:10:11 +0200 | [diff] [blame] | 4827 | if (options->set_default) |
| 4828 | options->set_default(options); |
| 4829 | |
Stefan Beller | 4d8c51a | 2018-01-04 14:50:44 -0800 | [diff] [blame] | 4830 | if (HAS_MULTI_BITS(options->output_format & check_mask)) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 4831 | die(_("options '%s', '%s', '%s', and '%s' cannot be used together"), |
| 4832 | "--name-only", "--name-status", "--check", "-s"); |
Timo Hirvonen | d7de00f | 2006-06-24 20:26:49 +0300 | [diff] [blame] | 4833 | |
Stefan Beller | 5e50525 | 2018-01-04 14:50:43 -0800 | [diff] [blame] | 4834 | if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 4835 | die(_("options '%s', '%s', and '%s' cannot be used together"), |
| 4836 | "-G", "-S", "--find-object"); |
Stefan Beller | 5e50525 | 2018-01-04 14:50:43 -0800 | [diff] [blame] | 4837 | |
Ævar Arnfjörð Bjarmason | 188e9e2 | 2021-04-12 19:15:14 +0200 | [diff] [blame] | 4838 | if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_G_REGEX_MASK)) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 4839 | die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s'"), |
| 4840 | "-G", "--pickaxe-regex", "--pickaxe-regex", "-S"); |
Ævar Arnfjörð Bjarmason | 188e9e2 | 2021-04-12 19:15:14 +0200 | [diff] [blame] | 4841 | |
Ævar Arnfjörð Bjarmason | d26ec88 | 2021-04-12 19:15:15 +0200 | [diff] [blame] | 4842 | if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK)) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 4843 | die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s' and '%s'"), |
| 4844 | "--pickaxe-all", "--find-object", "--pickaxe-all", "-G", "-S"); |
Ævar Arnfjörð Bjarmason | d26ec88 | 2021-04-12 19:15:15 +0200 | [diff] [blame] | 4845 | |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 4846 | /* |
| 4847 | * Most of the time we can say "there are changes" |
| 4848 | * only by checking if there are changed paths, but |
| 4849 | * --ignore-whitespace* options force us to look |
Jim Meyering | 97bf2a0 | 2009-08-30 22:27:02 +0200 | [diff] [blame] | 4850 | * inside contents. |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 4851 | */ |
| 4852 | |
Junio C Hamano | 50f0439 | 2020-12-16 17:27:13 -0800 | [diff] [blame] | 4853 | if ((options->xdl_opts & XDF_WHITESPACE_FLAGS) || |
| 4854 | options->ignore_regex_nr) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4855 | options->flags.diff_from_contents = 1; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 4856 | else |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4857 | options->flags.diff_from_contents = 0; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 4858 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4859 | if (options->flags.find_copies_harder) |
Junio C Hamano | 03b9d56 | 2006-08-09 13:17:19 -0700 | [diff] [blame] | 4860 | options->detect_rename = DIFF_DETECT_COPY; |
| 4861 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4862 | if (!options->flags.relative_name) |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4863 | options->prefix = NULL; |
| 4864 | if (options->prefix) |
| 4865 | options->prefix_length = strlen(options->prefix); |
| 4866 | else |
| 4867 | options->prefix_length = 0; |
| 4868 | |
Junio C Hamano | a64f8b2 | 2023-08-18 16:59:32 -0700 | [diff] [blame] | 4869 | /* |
| 4870 | * --name-only, --name-status, --checkdiff, and -s |
| 4871 | * turn other output format off. |
| 4872 | */ |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4873 | if (options->output_format & (DIFF_FORMAT_NAME | |
| 4874 | DIFF_FORMAT_NAME_STATUS | |
| 4875 | DIFF_FORMAT_CHECKDIFF | |
| 4876 | DIFF_FORMAT_NO_OUTPUT)) |
| 4877 | options->output_format &= ~(DIFF_FORMAT_RAW | |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 4878 | DIFF_FORMAT_NUMSTAT | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4879 | DIFF_FORMAT_DIFFSTAT | |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 4880 | DIFF_FORMAT_SHORTSTAT | |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 4881 | DIFF_FORMAT_DIRSTAT | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4882 | DIFF_FORMAT_SUMMARY | |
| 4883 | DIFF_FORMAT_PATCH); |
| 4884 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4885 | /* |
| 4886 | * These cases always need recursive; we do not drop caller-supplied |
| 4887 | * recursive bits for other formats here. |
| 4888 | */ |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4889 | if (options->output_format & (DIFF_FORMAT_PATCH | |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 4890 | DIFF_FORMAT_NUMSTAT | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4891 | DIFF_FORMAT_DIFFSTAT | |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 4892 | DIFF_FORMAT_SHORTSTAT | |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 4893 | DIFF_FORMAT_DIRSTAT | |
Johannes Schindelin | d7014dc | 2006-10-03 23:09:56 +0200 | [diff] [blame] | 4894 | DIFF_FORMAT_SUMMARY | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4895 | DIFF_FORMAT_CHECKDIFF)) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4896 | options->flags.recursive = 1; |
Junio C Hamano | 5e36354 | 2006-05-22 00:31:02 -0700 | [diff] [blame] | 4897 | /* |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 4898 | * Also pickaxe would not work very well if you do not say recursive |
Junio C Hamano | 5e36354 | 2006-05-22 00:31:02 -0700 | [diff] [blame] | 4899 | */ |
Stefan Beller | cf63051 | 2018-01-04 14:50:41 -0800 | [diff] [blame] | 4900 | if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4901 | options->flags.recursive = 1; |
Jens Lehmann | ae6d5c1 | 2010-03-11 22:50:25 +0100 | [diff] [blame] | 4902 | /* |
| 4903 | * When patches are generated, submodules diffed against the work tree |
| 4904 | * must be checked for dirtiness too so it can be shown in the output |
| 4905 | */ |
| 4906 | if (options->output_format & DIFF_FORMAT_PATCH) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4907 | options->flags.dirty_submodules = 1; |
Junio C Hamano | 5e36354 | 2006-05-22 00:31:02 -0700 | [diff] [blame] | 4908 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4909 | if (options->detect_rename && options->rename_limit < 0) |
| 4910 | options->rename_limit = diff_rename_limit_default; |
brian m. carlson | de1d81d | 2018-05-02 00:25:52 +0000 | [diff] [blame] | 4911 | if (hexsz < options->abbrev) |
| 4912 | options->abbrev = hexsz; /* full */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4913 | |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 4914 | /* |
| 4915 | * It does not make sense to show the first hit we happened |
| 4916 | * to have found. It does not make sense not to return with |
| 4917 | * exit code in such a case either. |
| 4918 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4919 | if (options->flags.quick) { |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 4920 | options->output_format = DIFF_FORMAT_NO_OUTPUT; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4921 | options->flags.exit_with_status = 1; |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 4922 | } |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4923 | |
Jeff King | 0ea7d5b | 2013-12-16 15:02:21 -0500 | [diff] [blame] | 4924 | options->diff_path_counter = 0; |
Junio C Hamano | b0e2c99 | 2014-06-16 10:07:09 -0700 | [diff] [blame] | 4925 | |
Jeff King | 9eac595 | 2023-06-01 13:41:06 -0400 | [diff] [blame] | 4926 | if (options->flags.follow_renames) |
| 4927 | diff_check_follow_pathspec(&options->pathspec, 1); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 4928 | |
| 4929 | if (!options->use_color || external_diff()) |
| 4930 | options->color_moved = 0; |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 4931 | |
Johannes Schindelin | 75408ca | 2022-01-28 12:02:50 +0000 | [diff] [blame] | 4932 | if (options->filter_not) { |
| 4933 | if (!options->filter) |
| 4934 | options->filter = ~filter_bit[DIFF_STATUS_FILTER_AON]; |
| 4935 | options->filter &= ~options->filter_not; |
| 4936 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4937 | } |
| 4938 | |
Matthieu Moy | dea007f | 2010-08-05 10:22:52 +0200 | [diff] [blame] | 4939 | int parse_long_opt(const char *opt, const char **argv, |
| 4940 | const char **optarg) |
| 4941 | { |
| 4942 | const char *arg = argv[0]; |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 4943 | if (!skip_prefix(arg, "--", &arg)) |
Matthieu Moy | dea007f | 2010-08-05 10:22:52 +0200 | [diff] [blame] | 4944 | return 0; |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 4945 | if (!skip_prefix(arg, opt, &arg)) |
Matthieu Moy | dea007f | 2010-08-05 10:22:52 +0200 | [diff] [blame] | 4946 | return 0; |
Nicolas Vigier | b0d12fc | 2013-10-31 12:08:28 +0100 | [diff] [blame] | 4947 | if (*arg == '=') { /* stuck form: --option=value */ |
Matthieu Moy | dea007f | 2010-08-05 10:22:52 +0200 | [diff] [blame] | 4948 | *optarg = arg + 1; |
| 4949 | return 1; |
| 4950 | } |
| 4951 | if (*arg != '\0') |
| 4952 | return 0; |
| 4953 | /* separate form: --option value */ |
| 4954 | if (!argv[1]) |
| 4955 | die("Option '--%s' requires a value", opt); |
| 4956 | *optarg = argv[1]; |
| 4957 | return 2; |
| 4958 | } |
| 4959 | |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4960 | static int diff_opt_stat(const struct option *opt, const char *value, int unset) |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4961 | { |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4962 | struct diff_options *options = opt->value; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4963 | int width = options->stat_width; |
| 4964 | int name_width = options->stat_name_width; |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 4965 | int graph_width = options->stat_graph_width; |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 4966 | int count = options->stat_count; |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4967 | char *end; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4968 | |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4969 | BUG_ON_OPT_NEG(unset); |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 4970 | |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4971 | if (!strcmp(opt->long_name, "stat")) { |
| 4972 | if (value) { |
| 4973 | width = strtoul(value, &end, 10); |
| 4974 | if (*end == ',') |
| 4975 | name_width = strtoul(end+1, &end, 10); |
| 4976 | if (*end == ',') |
| 4977 | count = strtoul(end+1, &end, 10); |
| 4978 | if (*end) |
| 4979 | return error(_("invalid --stat value: %s"), value); |
Matthieu Moy | 1e57208 | 2010-08-05 10:22:54 +0200 | [diff] [blame] | 4980 | } |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4981 | } else if (!strcmp(opt->long_name, "stat-width")) { |
| 4982 | width = strtoul(value, &end, 10); |
| 4983 | if (*end) |
| 4984 | return error(_("%s expects a numerical value"), |
| 4985 | opt->long_name); |
| 4986 | } else if (!strcmp(opt->long_name, "stat-name-width")) { |
| 4987 | name_width = strtoul(value, &end, 10); |
| 4988 | if (*end) |
| 4989 | return error(_("%s expects a numerical value"), |
| 4990 | opt->long_name); |
| 4991 | } else if (!strcmp(opt->long_name, "stat-graph-width")) { |
| 4992 | graph_width = strtoul(value, &end, 10); |
| 4993 | if (*end) |
| 4994 | return error(_("%s expects a numerical value"), |
| 4995 | opt->long_name); |
| 4996 | } else if (!strcmp(opt->long_name, "stat-count")) { |
| 4997 | count = strtoul(value, &end, 10); |
| 4998 | if (*end) |
| 4999 | return error(_("%s expects a numerical value"), |
| 5000 | opt->long_name); |
| 5001 | } else |
| 5002 | BUG("%s should not get here", opt->long_name); |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 5003 | |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5004 | options->output_format &= ~DIFF_FORMAT_NO_OUTPUT; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 5005 | options->output_format |= DIFF_FORMAT_DIFFSTAT; |
| 5006 | options->stat_name_width = name_width; |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 5007 | options->stat_graph_width = graph_width; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 5008 | options->stat_width = width; |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 5009 | options->stat_count = count; |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 5010 | return 0; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 5011 | } |
| 5012 | |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 5013 | static int parse_dirstat_opt(struct diff_options *options, const char *params) |
| 5014 | { |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 5015 | struct strbuf errmsg = STRBUF_INIT; |
| 5016 | if (parse_dirstat_params(options, params, &errmsg)) |
Johan Herland | 7478ac5 | 2011-04-29 11:36:23 +0200 | [diff] [blame] | 5017 | die(_("Failed to parse --dirstat/-X option parameter:\n%s"), |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 5018 | errmsg.buf); |
| 5019 | strbuf_release(&errmsg); |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 5020 | /* |
| 5021 | * The caller knows a dirstat-related option is given from the command |
| 5022 | * line; allow it to say "return this_function();" |
| 5023 | */ |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5024 | options->output_format &= ~DIFF_FORMAT_NO_OUTPUT; |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 5025 | options->output_format |= DIFF_FORMAT_DIRSTAT; |
| 5026 | return 1; |
| 5027 | } |
| 5028 | |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 5029 | static int diff_opt_diff_filter(const struct option *option, |
| 5030 | const char *optarg, int unset) |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5031 | { |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 5032 | struct diff_options *opt = option->value; |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5033 | int i, optch; |
| 5034 | |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 5035 | BUG_ON_OPT_NEG(unset); |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5036 | prepare_filter_bits(); |
Junio C Hamano | 7f2ea5f | 2013-07-17 16:19:19 -0700 | [diff] [blame] | 5037 | |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5038 | for (i = 0; (optch = optarg[i]) != '\0'; i++) { |
| 5039 | unsigned int bit; |
Junio C Hamano | 7f2ea5f | 2013-07-17 16:19:19 -0700 | [diff] [blame] | 5040 | int negate; |
| 5041 | |
| 5042 | if ('a' <= optch && optch <= 'z') { |
| 5043 | negate = 1; |
| 5044 | optch = toupper(optch); |
| 5045 | } else { |
| 5046 | negate = 0; |
| 5047 | } |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5048 | |
| 5049 | bit = (0 <= optch && optch <= 'Z') ? filter_bit[optch] : 0; |
| 5050 | if (!bit) |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 5051 | return error(_("unknown change class '%c' in --diff-filter=%s"), |
| 5052 | optarg[i], optarg); |
Junio C Hamano | 7f2ea5f | 2013-07-17 16:19:19 -0700 | [diff] [blame] | 5053 | if (negate) |
Johannes Schindelin | 75408ca | 2022-01-28 12:02:50 +0000 | [diff] [blame] | 5054 | opt->filter_not |= bit; |
Junio C Hamano | 7f2ea5f | 2013-07-17 16:19:19 -0700 | [diff] [blame] | 5055 | else |
| 5056 | opt->filter |= bit; |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5057 | } |
| 5058 | return 0; |
| 5059 | } |
| 5060 | |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 5061 | static void enable_patch_output(int *fmt) |
| 5062 | { |
Matthieu Moy | 71482d3 | 2013-07-16 10:05:37 +0200 | [diff] [blame] | 5063 | *fmt &= ~DIFF_FORMAT_NO_OUTPUT; |
| 5064 | *fmt |= DIFF_FORMAT_PATCH; |
| 5065 | } |
| 5066 | |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 5067 | static int diff_opt_ws_error_highlight(const struct option *option, |
| 5068 | const char *arg, int unset) |
Junio C Hamano | b8767f7 | 2015-05-26 10:11:28 -0700 | [diff] [blame] | 5069 | { |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 5070 | struct diff_options *opt = option->value; |
Junio C Hamano | 077965f | 2016-10-04 15:03:33 -0700 | [diff] [blame] | 5071 | int val = parse_ws_error_highlight(arg); |
Junio C Hamano | b8767f7 | 2015-05-26 10:11:28 -0700 | [diff] [blame] | 5072 | |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 5073 | BUG_ON_OPT_NEG(unset); |
| 5074 | if (val < 0) |
| 5075 | return error(_("unknown value after ws-error-highlight=%.*s"), |
| 5076 | -1 - val, arg); |
Junio C Hamano | b8767f7 | 2015-05-26 10:11:28 -0700 | [diff] [blame] | 5077 | opt->ws_error_highlight = val; |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 5078 | return 0; |
Junio C Hamano | b8767f7 | 2015-05-26 10:11:28 -0700 | [diff] [blame] | 5079 | } |
| 5080 | |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5081 | static int diff_opt_find_object(const struct option *option, |
| 5082 | const char *arg, int unset) |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 5083 | { |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5084 | struct diff_options *opt = option->value; |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 5085 | struct object_id oid; |
| 5086 | |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5087 | BUG_ON_OPT_NEG(unset); |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 5088 | if (repo_get_oid(the_repository, arg, &oid)) |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5089 | return error(_("unable to resolve '%s'"), arg); |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 5090 | |
| 5091 | if (!opt->objfind) |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 5092 | CALLOC_ARRAY(opt->objfind, 1); |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 5093 | |
| 5094 | opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND; |
| 5095 | opt->flags.recursive = 1; |
| 5096 | opt->flags.tree_in_recursive = 1; |
| 5097 | oidset_insert(opt->objfind, &oid); |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5098 | return 0; |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 5099 | } |
| 5100 | |
Nguyễn Thái Ngọc Duy | df84a43 | 2019-03-05 19:30:11 +0700 | [diff] [blame] | 5101 | static int diff_opt_anchored(const struct option *opt, |
| 5102 | const char *arg, int unset) |
| 5103 | { |
| 5104 | struct diff_options *options = opt->value; |
| 5105 | |
| 5106 | BUG_ON_OPT_NEG(unset); |
| 5107 | options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF); |
| 5108 | ALLOC_GROW(options->anchors, options->anchors_nr + 1, |
| 5109 | options->anchors_alloc); |
| 5110 | options->anchors[options->anchors_nr++] = xstrdup(arg); |
| 5111 | return 0; |
| 5112 | } |
| 5113 | |
Nguyễn Thái Ngọc Duy | 6d9af6f | 2019-03-05 19:30:12 +0700 | [diff] [blame] | 5114 | static int diff_opt_binary(const struct option *opt, |
| 5115 | const char *arg, int unset) |
| 5116 | { |
| 5117 | struct diff_options *options = opt->value; |
| 5118 | |
| 5119 | BUG_ON_OPT_NEG(unset); |
| 5120 | BUG_ON_OPT_ARG(arg); |
| 5121 | enable_patch_output(&options->output_format); |
| 5122 | options->flags.binary = 1; |
| 5123 | return 0; |
| 5124 | } |
| 5125 | |
Nguyễn Thái Ngọc Duy | ced4e17 | 2019-02-21 18:16:13 +0700 | [diff] [blame] | 5126 | static int diff_opt_break_rewrites(const struct option *opt, |
| 5127 | const char *arg, int unset) |
| 5128 | { |
| 5129 | int *break_opt = opt->value; |
| 5130 | int opt1, opt2; |
| 5131 | |
| 5132 | BUG_ON_OPT_NEG(unset); |
| 5133 | if (!arg) |
| 5134 | arg = ""; |
| 5135 | opt1 = parse_rename_score(&arg); |
| 5136 | if (*arg == 0) |
| 5137 | opt2 = 0; |
| 5138 | else if (*arg != '/') |
| 5139 | return error(_("%s expects <n>/<m> form"), opt->long_name); |
| 5140 | else { |
| 5141 | arg++; |
| 5142 | opt2 = parse_rename_score(&arg); |
| 5143 | } |
| 5144 | if (*arg != 0) |
| 5145 | return error(_("%s expects <n>/<m> form"), opt->long_name); |
| 5146 | *break_opt = opt1 | (opt2 << 16); |
| 5147 | return 0; |
| 5148 | } |
| 5149 | |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5150 | static int diff_opt_char(const struct option *opt, |
| 5151 | const char *arg, int unset) |
| 5152 | { |
| 5153 | char *value = opt->value; |
| 5154 | |
| 5155 | BUG_ON_OPT_NEG(unset); |
| 5156 | if (arg[1]) |
| 5157 | return error(_("%s expects a character, got '%s'"), |
| 5158 | opt->long_name, arg); |
| 5159 | *value = arg[0]; |
| 5160 | return 0; |
| 5161 | } |
| 5162 | |
Nguyễn Thái Ngọc Duy | 59311a9 | 2019-03-24 15:20:09 +0700 | [diff] [blame] | 5163 | static int diff_opt_color_moved(const struct option *opt, |
| 5164 | const char *arg, int unset) |
| 5165 | { |
| 5166 | struct diff_options *options = opt->value; |
| 5167 | |
| 5168 | if (unset) { |
| 5169 | options->color_moved = COLOR_MOVED_NO; |
| 5170 | } else if (!arg) { |
| 5171 | if (diff_color_moved_default) |
| 5172 | options->color_moved = diff_color_moved_default; |
| 5173 | if (options->color_moved == COLOR_MOVED_NO) |
| 5174 | options->color_moved = COLOR_MOVED_DEFAULT; |
| 5175 | } else { |
| 5176 | int cm = parse_color_moved(arg); |
| 5177 | if (cm < 0) |
| 5178 | return error(_("bad --color-moved argument: %s"), arg); |
| 5179 | options->color_moved = cm; |
| 5180 | } |
| 5181 | return 0; |
| 5182 | } |
| 5183 | |
Nguyễn Thái Ngọc Duy | 8ce2020 | 2019-03-24 15:20:10 +0700 | [diff] [blame] | 5184 | static int diff_opt_color_moved_ws(const struct option *opt, |
| 5185 | const char *arg, int unset) |
| 5186 | { |
| 5187 | struct diff_options *options = opt->value; |
| 5188 | unsigned cm; |
| 5189 | |
Nguyễn Thái Ngọc Duy | bb98729 | 2019-03-24 15:20:11 +0700 | [diff] [blame] | 5190 | if (unset) { |
| 5191 | options->color_moved_ws_handling = 0; |
| 5192 | return 0; |
| 5193 | } |
| 5194 | |
Nguyễn Thái Ngọc Duy | 8ce2020 | 2019-03-24 15:20:10 +0700 | [diff] [blame] | 5195 | cm = parse_color_moved_ws(arg); |
| 5196 | if (cm & COLOR_MOVED_WS_ERROR) |
| 5197 | return error(_("invalid mode '%s' in --color-moved-ws"), arg); |
| 5198 | options->color_moved_ws_handling = cm; |
| 5199 | return 0; |
| 5200 | } |
| 5201 | |
Nguyễn Thái Ngọc Duy | 212db69 | 2019-03-05 19:30:20 +0700 | [diff] [blame] | 5202 | static int diff_opt_color_words(const struct option *opt, |
| 5203 | const char *arg, int unset) |
| 5204 | { |
| 5205 | struct diff_options *options = opt->value; |
| 5206 | |
| 5207 | BUG_ON_OPT_NEG(unset); |
| 5208 | options->use_color = 1; |
| 5209 | options->word_diff = DIFF_WORDS_COLOR; |
| 5210 | options->word_regex = arg; |
| 5211 | return 0; |
| 5212 | } |
| 5213 | |
Nguyễn Thái Ngọc Duy | 7d7942b | 2019-02-21 18:16:11 +0700 | [diff] [blame] | 5214 | static int diff_opt_compact_summary(const struct option *opt, |
| 5215 | const char *arg, int unset) |
| 5216 | { |
| 5217 | struct diff_options *options = opt->value; |
| 5218 | |
| 5219 | BUG_ON_OPT_ARG(arg); |
| 5220 | if (unset) { |
| 5221 | options->flags.stat_with_summary = 0; |
| 5222 | } else { |
| 5223 | options->flags.stat_with_summary = 1; |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5224 | options->output_format &= ~DIFF_FORMAT_NO_OUTPUT; |
Nguyễn Thái Ngọc Duy | 7d7942b | 2019-02-21 18:16:11 +0700 | [diff] [blame] | 5225 | options->output_format |= DIFF_FORMAT_DIFFSTAT; |
| 5226 | } |
| 5227 | return 0; |
| 5228 | } |
| 5229 | |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5230 | static int diff_opt_diff_algorithm(const struct option *opt, |
| 5231 | const char *arg, int unset) |
| 5232 | { |
| 5233 | struct diff_options *options = opt->value; |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5234 | |
| 5235 | BUG_ON_OPT_NEG(unset); |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 5236 | |
| 5237 | if (set_diff_algorithm(options, arg)) |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5238 | return error(_("option diff-algorithm accepts \"myers\", " |
| 5239 | "\"minimal\", \"patience\" and \"histogram\"")); |
| 5240 | |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 5241 | options->ignore_driver_algorithm = 1; |
| 5242 | |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 5243 | return 0; |
| 5244 | } |
| 5245 | |
| 5246 | static int diff_opt_diff_algorithm_no_arg(const struct option *opt, |
| 5247 | const char *arg, int unset) |
| 5248 | { |
| 5249 | struct diff_options *options = opt->value; |
| 5250 | |
| 5251 | BUG_ON_OPT_NEG(unset); |
| 5252 | BUG_ON_OPT_ARG(arg); |
| 5253 | |
| 5254 | if (set_diff_algorithm(options, opt->long_name)) |
| 5255 | BUG("available diff algorithms include \"myers\", " |
| 5256 | "\"minimal\", \"patience\" and \"histogram\""); |
| 5257 | |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 5258 | options->ignore_driver_algorithm = 1; |
| 5259 | |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5260 | return 0; |
| 5261 | } |
| 5262 | |
Nguyễn Thái Ngọc Duy | 4ce7aab | 2019-02-21 18:16:03 +0700 | [diff] [blame] | 5263 | static int diff_opt_dirstat(const struct option *opt, |
| 5264 | const char *arg, int unset) |
| 5265 | { |
| 5266 | struct diff_options *options = opt->value; |
| 5267 | |
| 5268 | BUG_ON_OPT_NEG(unset); |
| 5269 | if (!strcmp(opt->long_name, "cumulative")) { |
| 5270 | if (arg) |
| 5271 | BUG("how come --cumulative take a value?"); |
| 5272 | arg = "cumulative"; |
| 5273 | } else if (!strcmp(opt->long_name, "dirstat-by-file")) |
| 5274 | parse_dirstat_opt(options, "files"); |
| 5275 | parse_dirstat_opt(options, arg ? arg : ""); |
| 5276 | return 0; |
| 5277 | } |
| 5278 | |
Nguyễn Thái Ngọc Duy | 7f64850 | 2019-02-21 18:16:16 +0700 | [diff] [blame] | 5279 | static int diff_opt_find_copies(const struct option *opt, |
| 5280 | const char *arg, int unset) |
| 5281 | { |
| 5282 | struct diff_options *options = opt->value; |
| 5283 | |
| 5284 | BUG_ON_OPT_NEG(unset); |
| 5285 | if (!arg) |
| 5286 | arg = ""; |
| 5287 | options->rename_score = parse_rename_score(&arg); |
| 5288 | if (*arg != 0) |
| 5289 | return error(_("invalid argument to %s"), opt->long_name); |
| 5290 | |
| 5291 | if (options->detect_rename == DIFF_DETECT_COPY) |
| 5292 | options->flags.find_copies_harder = 1; |
| 5293 | else |
| 5294 | options->detect_rename = DIFF_DETECT_COPY; |
| 5295 | |
| 5296 | return 0; |
| 5297 | } |
| 5298 | |
Nguyễn Thái Ngọc Duy | f476308 | 2019-02-21 18:16:14 +0700 | [diff] [blame] | 5299 | static int diff_opt_find_renames(const struct option *opt, |
| 5300 | const char *arg, int unset) |
| 5301 | { |
| 5302 | struct diff_options *options = opt->value; |
| 5303 | |
| 5304 | BUG_ON_OPT_NEG(unset); |
| 5305 | if (!arg) |
| 5306 | arg = ""; |
| 5307 | options->rename_score = parse_rename_score(&arg); |
| 5308 | if (*arg != 0) |
| 5309 | return error(_("invalid argument to %s"), opt->long_name); |
| 5310 | |
| 5311 | options->detect_rename = DIFF_DETECT_RENAME; |
| 5312 | return 0; |
| 5313 | } |
| 5314 | |
Nguyễn Thái Ngọc Duy | 1e9250b | 2019-03-05 19:30:16 +0700 | [diff] [blame] | 5315 | static int diff_opt_follow(const struct option *opt, |
| 5316 | const char *arg, int unset) |
| 5317 | { |
| 5318 | struct diff_options *options = opt->value; |
| 5319 | |
| 5320 | BUG_ON_OPT_ARG(arg); |
| 5321 | if (unset) { |
| 5322 | options->flags.follow_renames = 0; |
| 5323 | options->flags.default_follow_renames = 0; |
| 5324 | } else { |
| 5325 | options->flags.follow_renames = 1; |
| 5326 | } |
| 5327 | return 0; |
| 5328 | } |
| 5329 | |
Nguyễn Thái Ngọc Duy | b680ee1 | 2019-03-05 19:30:25 +0700 | [diff] [blame] | 5330 | static int diff_opt_ignore_submodules(const struct option *opt, |
| 5331 | const char *arg, int unset) |
| 5332 | { |
| 5333 | struct diff_options *options = opt->value; |
| 5334 | |
| 5335 | BUG_ON_OPT_NEG(unset); |
| 5336 | if (!arg) |
| 5337 | arg = "all"; |
| 5338 | options->flags.override_submodule_config = 1; |
| 5339 | handle_ignore_submodules_arg(options, arg); |
| 5340 | return 0; |
| 5341 | } |
| 5342 | |
Nguyễn Thái Ngọc Duy | 2f81cf9 | 2019-03-24 15:20:06 +0700 | [diff] [blame] | 5343 | static int diff_opt_line_prefix(const struct option *opt, |
| 5344 | const char *optarg, int unset) |
| 5345 | { |
| 5346 | struct diff_options *options = opt->value; |
| 5347 | |
| 5348 | BUG_ON_OPT_NEG(unset); |
| 5349 | options->line_prefix = optarg; |
| 5350 | options->line_prefix_length = strlen(options->line_prefix); |
| 5351 | graph_setup_line_prefix(options); |
| 5352 | return 0; |
| 5353 | } |
| 5354 | |
Nguyễn Thái Ngọc Duy | 11c659d | 2019-03-24 15:20:07 +0700 | [diff] [blame] | 5355 | static int diff_opt_no_prefix(const struct option *opt, |
| 5356 | const char *optarg, int unset) |
| 5357 | { |
| 5358 | struct diff_options *options = opt->value; |
| 5359 | |
| 5360 | BUG_ON_OPT_NEG(unset); |
| 5361 | BUG_ON_OPT_ARG(optarg); |
Jeff King | 6799aad | 2023-03-09 01:07:06 -0500 | [diff] [blame] | 5362 | diff_set_noprefix(options); |
Nguyễn Thái Ngọc Duy | 11c659d | 2019-03-24 15:20:07 +0700 | [diff] [blame] | 5363 | return 0; |
| 5364 | } |
| 5365 | |
Jeff King | b39a569 | 2023-03-09 01:09:54 -0500 | [diff] [blame] | 5366 | static int diff_opt_default_prefix(const struct option *opt, |
| 5367 | const char *optarg, int unset) |
| 5368 | { |
| 5369 | struct diff_options *options = opt->value; |
| 5370 | |
| 5371 | BUG_ON_OPT_NEG(unset); |
| 5372 | BUG_ON_OPT_ARG(optarg); |
Peter Hutterer | 7fdc265 | 2024-03-15 11:03:10 +1000 | [diff] [blame] | 5373 | diff_src_prefix = "a/"; |
| 5374 | diff_dst_prefix = "b/"; |
Jeff King | b39a569 | 2023-03-09 01:09:54 -0500 | [diff] [blame] | 5375 | diff_set_default_prefix(options); |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 5376 | return 0; |
| 5377 | } |
| 5378 | |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5379 | static enum parse_opt_result diff_opt_output(struct parse_opt_ctx_t *ctx, |
| 5380 | const struct option *opt, |
| 5381 | const char *arg, int unset) |
| 5382 | { |
| 5383 | struct diff_options *options = opt->value; |
| 5384 | char *path; |
| 5385 | |
| 5386 | BUG_ON_OPT_NEG(unset); |
| 5387 | path = prefix_filename(ctx->prefix, arg); |
| 5388 | options->file = xfopen(path, "w"); |
| 5389 | options->close_file = 1; |
| 5390 | if (options->use_color != GIT_COLOR_ALWAYS) |
| 5391 | options->use_color = GIT_COLOR_NEVER; |
| 5392 | free(path); |
| 5393 | return 0; |
| 5394 | } |
| 5395 | |
Nguyễn Thái Ngọc Duy | 31fd640 | 2019-03-05 19:30:08 +0700 | [diff] [blame] | 5396 | static int diff_opt_patience(const struct option *opt, |
| 5397 | const char *arg, int unset) |
| 5398 | { |
| 5399 | struct diff_options *options = opt->value; |
| 5400 | int i; |
| 5401 | |
| 5402 | BUG_ON_OPT_NEG(unset); |
| 5403 | BUG_ON_OPT_ARG(arg); |
Nguyễn Thái Ngọc Duy | 31fd640 | 2019-03-05 19:30:08 +0700 | [diff] [blame] | 5404 | /* |
| 5405 | * Both --patience and --anchored use PATIENCE_DIFF |
| 5406 | * internally, so remove any anchors previously |
| 5407 | * specified. |
| 5408 | */ |
| 5409 | for (i = 0; i < options->anchors_nr; i++) |
| 5410 | free(options->anchors[i]); |
| 5411 | options->anchors_nr = 0; |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 5412 | options->ignore_driver_algorithm = 1; |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 5413 | |
| 5414 | return set_diff_algorithm(options, "patience"); |
Nguyễn Thái Ngọc Duy | 31fd640 | 2019-03-05 19:30:08 +0700 | [diff] [blame] | 5415 | } |
| 5416 | |
Michał Kępień | 296d4a9 | 2020-10-20 08:48:09 +0200 | [diff] [blame] | 5417 | static int diff_opt_ignore_regex(const struct option *opt, |
| 5418 | const char *arg, int unset) |
| 5419 | { |
| 5420 | struct diff_options *options = opt->value; |
| 5421 | regex_t *regex; |
| 5422 | |
| 5423 | BUG_ON_OPT_NEG(unset); |
| 5424 | regex = xmalloc(sizeof(*regex)); |
| 5425 | if (regcomp(regex, arg, REG_EXTENDED | REG_NEWLINE)) |
| 5426 | return error(_("invalid regex given to -I: '%s'"), arg); |
| 5427 | ALLOC_GROW(options->ignore_regex, options->ignore_regex_nr + 1, |
| 5428 | options->ignore_regex_alloc); |
| 5429 | options->ignore_regex[options->ignore_regex_nr++] = regex; |
| 5430 | return 0; |
| 5431 | } |
| 5432 | |
Nguyễn Thái Ngọc Duy | a41cfb3 | 2019-03-24 15:19:59 +0700 | [diff] [blame] | 5433 | static int diff_opt_pickaxe_regex(const struct option *opt, |
| 5434 | const char *arg, int unset) |
| 5435 | { |
| 5436 | struct diff_options *options = opt->value; |
| 5437 | |
| 5438 | BUG_ON_OPT_NEG(unset); |
| 5439 | options->pickaxe = arg; |
| 5440 | options->pickaxe_opts |= DIFF_PICKAXE_KIND_G; |
| 5441 | return 0; |
| 5442 | } |
| 5443 | |
| 5444 | static int diff_opt_pickaxe_string(const struct option *opt, |
| 5445 | const char *arg, int unset) |
| 5446 | { |
| 5447 | struct diff_options *options = opt->value; |
| 5448 | |
| 5449 | BUG_ON_OPT_NEG(unset); |
| 5450 | options->pickaxe = arg; |
| 5451 | options->pickaxe_opts |= DIFF_PICKAXE_KIND_S; |
| 5452 | return 0; |
| 5453 | } |
| 5454 | |
Nguyễn Thái Ngọc Duy | 0b1c5b5 | 2019-02-21 18:16:19 +0700 | [diff] [blame] | 5455 | static int diff_opt_relative(const struct option *opt, |
| 5456 | const char *arg, int unset) |
| 5457 | { |
| 5458 | struct diff_options *options = opt->value; |
| 5459 | |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 5460 | options->flags.relative_name = !unset; |
Nguyễn Thái Ngọc Duy | 0b1c5b5 | 2019-02-21 18:16:19 +0700 | [diff] [blame] | 5461 | if (arg) |
| 5462 | options->prefix = arg; |
| 5463 | return 0; |
| 5464 | } |
| 5465 | |
Nguyễn Thái Ngọc Duy | 125dcea | 2019-03-05 19:30:26 +0700 | [diff] [blame] | 5466 | static int diff_opt_submodule(const struct option *opt, |
| 5467 | const char *arg, int unset) |
| 5468 | { |
| 5469 | struct diff_options *options = opt->value; |
| 5470 | |
| 5471 | BUG_ON_OPT_NEG(unset); |
| 5472 | if (!arg) |
| 5473 | arg = "log"; |
| 5474 | if (parse_submodule_params(options, arg)) |
| 5475 | return error(_("failed to parse --submodule option parameter: '%s'"), |
| 5476 | arg); |
| 5477 | return 0; |
| 5478 | } |
| 5479 | |
Nguyễn Thái Ngọc Duy | 8ab7697 | 2019-03-05 19:30:24 +0700 | [diff] [blame] | 5480 | static int diff_opt_textconv(const struct option *opt, |
| 5481 | const char *arg, int unset) |
| 5482 | { |
| 5483 | struct diff_options *options = opt->value; |
| 5484 | |
| 5485 | BUG_ON_OPT_ARG(arg); |
| 5486 | if (unset) { |
| 5487 | options->flags.allow_textconv = 0; |
| 5488 | } else { |
| 5489 | options->flags.allow_textconv = 1; |
| 5490 | options->flags.textconv_set_via_cmdline = 1; |
| 5491 | } |
| 5492 | return 0; |
| 5493 | } |
| 5494 | |
Nguyễn Thái Ngọc Duy | d473e2e | 2019-01-27 07:35:33 +0700 | [diff] [blame] | 5495 | static int diff_opt_unified(const struct option *opt, |
| 5496 | const char *arg, int unset) |
| 5497 | { |
| 5498 | struct diff_options *options = opt->value; |
| 5499 | char *s; |
| 5500 | |
| 5501 | BUG_ON_OPT_NEG(unset); |
| 5502 | |
Nguyễn Thái Ngọc Duy | 8ef0519 | 2019-05-29 16:11:15 +0700 | [diff] [blame] | 5503 | if (arg) { |
| 5504 | options->context = strtol(arg, &s, 10); |
| 5505 | if (*s) |
| 5506 | return error(_("%s expects a numerical value"), "--unified"); |
| 5507 | } |
Nguyễn Thái Ngọc Duy | d473e2e | 2019-01-27 07:35:33 +0700 | [diff] [blame] | 5508 | enable_patch_output(&options->output_format); |
| 5509 | |
| 5510 | return 0; |
| 5511 | } |
| 5512 | |
Nguyễn Thái Ngọc Duy | e9fb39b | 2019-03-05 19:30:18 +0700 | [diff] [blame] | 5513 | static int diff_opt_word_diff(const struct option *opt, |
| 5514 | const char *arg, int unset) |
| 5515 | { |
| 5516 | struct diff_options *options = opt->value; |
| 5517 | |
| 5518 | BUG_ON_OPT_NEG(unset); |
| 5519 | if (arg) { |
| 5520 | if (!strcmp(arg, "plain")) |
| 5521 | options->word_diff = DIFF_WORDS_PLAIN; |
| 5522 | else if (!strcmp(arg, "color")) { |
| 5523 | options->use_color = 1; |
| 5524 | options->word_diff = DIFF_WORDS_COLOR; |
| 5525 | } |
| 5526 | else if (!strcmp(arg, "porcelain")) |
| 5527 | options->word_diff = DIFF_WORDS_PORCELAIN; |
| 5528 | else if (!strcmp(arg, "none")) |
| 5529 | options->word_diff = DIFF_WORDS_NONE; |
| 5530 | else |
| 5531 | return error(_("bad --word-diff argument: %s"), arg); |
| 5532 | } else { |
| 5533 | if (options->word_diff == DIFF_WORDS_NONE) |
| 5534 | options->word_diff = DIFF_WORDS_PLAIN; |
| 5535 | } |
| 5536 | return 0; |
| 5537 | } |
| 5538 | |
Nguyễn Thái Ngọc Duy | 797df11 | 2019-03-05 19:30:19 +0700 | [diff] [blame] | 5539 | static int diff_opt_word_diff_regex(const struct option *opt, |
| 5540 | const char *arg, int unset) |
| 5541 | { |
| 5542 | struct diff_options *options = opt->value; |
| 5543 | |
| 5544 | BUG_ON_OPT_NEG(unset); |
| 5545 | if (options->word_diff == DIFF_WORDS_NONE) |
| 5546 | options->word_diff = DIFF_WORDS_PLAIN; |
| 5547 | options->word_regex = arg; |
| 5548 | return 0; |
| 5549 | } |
| 5550 | |
Junio C Hamano | 1eb4136 | 2021-02-11 11:57:50 -0800 | [diff] [blame] | 5551 | static int diff_opt_rotate_to(const struct option *opt, const char *arg, int unset) |
| 5552 | { |
| 5553 | struct diff_options *options = opt->value; |
| 5554 | |
| 5555 | BUG_ON_OPT_NEG(unset); |
| 5556 | if (!strcmp(opt->long_name, "skip-to")) |
| 5557 | options->skip_instead_of_rotate = 1; |
| 5558 | else |
| 5559 | options->skip_instead_of_rotate = 0; |
| 5560 | options->rotate_to = arg; |
| 5561 | return 0; |
| 5562 | } |
| 5563 | |
Philippe Blain | 0a86803 | 2023-06-26 16:24:48 +0000 | [diff] [blame] | 5564 | /* |
| 5565 | * Consider adding new flags to __git_diff_common_options |
| 5566 | * in contrib/completion/git-completion.bash |
| 5567 | */ |
René Scharfe | c5630c4 | 2022-12-01 23:49:11 +0100 | [diff] [blame] | 5568 | struct option *add_diff_options(const struct option *opts, |
| 5569 | struct diff_options *options) |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5570 | { |
| 5571 | struct option parseopts[] = { |
Nguyễn Thái Ngọc Duy | cc013c2 | 2019-01-27 07:35:32 +0700 | [diff] [blame] | 5572 | OPT_GROUP(N_("Diff output format options")), |
| 5573 | OPT_BITOP('p', "patch", &options->output_format, |
| 5574 | N_("generate patch"), |
| 5575 | DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5576 | OPT_SET_INT('s', "no-patch", &options->output_format, |
| 5577 | N_("suppress diff output"), DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | cc013c2 | 2019-01-27 07:35:32 +0700 | [diff] [blame] | 5578 | OPT_BITOP('u', NULL, &options->output_format, |
| 5579 | N_("generate patch"), |
| 5580 | DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | d473e2e | 2019-01-27 07:35:33 +0700 | [diff] [blame] | 5581 | OPT_CALLBACK_F('U', "unified", options, N_("<n>"), |
| 5582 | N_("generate diffs with <n> lines context"), |
Junio C Hamano | 20aa7c5 | 2019-05-30 10:50:44 -0700 | [diff] [blame] | 5583 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_unified), |
Nguyễn Thái Ngọc Duy | 7fd9a1b | 2019-01-27 07:35:34 +0700 | [diff] [blame] | 5584 | OPT_BOOL('W', "function-context", &options->flags.funccontext, |
| 5585 | N_("generate diffs with <n> lines context")), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5586 | OPT_BITOP(0, "raw", &options->output_format, |
Nguyễn Thái Ngọc Duy | ed88148 | 2019-01-27 07:35:35 +0700 | [diff] [blame] | 5587 | N_("generate the diff in raw format"), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5588 | DIFF_FORMAT_RAW, DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | c659f30 | 2019-02-16 18:36:35 +0700 | [diff] [blame] | 5589 | OPT_BITOP(0, "patch-with-raw", &options->output_format, |
| 5590 | N_("synonym for '-p --raw'"), |
| 5591 | DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW, |
| 5592 | DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | e550f58 | 2019-02-21 18:16:06 +0700 | [diff] [blame] | 5593 | OPT_BITOP(0, "patch-with-stat", &options->output_format, |
| 5594 | N_("synonym for '-p --stat'"), |
| 5595 | DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT, |
| 5596 | DIFF_FORMAT_NO_OUTPUT), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5597 | OPT_BITOP(0, "numstat", &options->output_format, |
Nguyễn Thái Ngọc Duy | e567362 | 2019-02-16 18:36:36 +0700 | [diff] [blame] | 5598 | N_("machine friendly --stat"), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5599 | DIFF_FORMAT_NUMSTAT, DIFF_FORMAT_NO_OUTPUT), |
| 5600 | OPT_BITOP(0, "shortstat", &options->output_format, |
Nguyễn Thái Ngọc Duy | e567362 | 2019-02-16 18:36:36 +0700 | [diff] [blame] | 5601 | N_("output only the last line of --stat"), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5602 | DIFF_FORMAT_SHORTSTAT, DIFF_FORMAT_NO_OUTPUT), |
Jiang Xin | 5e7013a | 2024-02-14 16:46:41 +0800 | [diff] [blame] | 5603 | OPT_CALLBACK_F('X', "dirstat", options, N_("<param1>,<param2>..."), |
Nguyễn Thái Ngọc Duy | 4ce7aab | 2019-02-21 18:16:03 +0700 | [diff] [blame] | 5604 | N_("output the distribution of relative amount of changes for each sub-directory"), |
| 5605 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5606 | diff_opt_dirstat), |
| 5607 | OPT_CALLBACK_F(0, "cumulative", options, NULL, |
| 5608 | N_("synonym for --dirstat=cumulative"), |
| 5609 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, |
| 5610 | diff_opt_dirstat), |
Jiang Xin | 5e7013a | 2024-02-14 16:46:41 +0800 | [diff] [blame] | 5611 | OPT_CALLBACK_F(0, "dirstat-by-file", options, N_("<param1>,<param2>..."), |
| 5612 | N_("synonym for --dirstat=files,<param1>,<param2>..."), |
Nguyễn Thái Ngọc Duy | 4ce7aab | 2019-02-21 18:16:03 +0700 | [diff] [blame] | 5613 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5614 | diff_opt_dirstat), |
Nguyễn Thái Ngọc Duy | fc6af3e | 2019-02-21 18:16:04 +0700 | [diff] [blame] | 5615 | OPT_BIT_F(0, "check", &options->output_format, |
| 5616 | N_("warn if changes introduce conflict markers or whitespace errors"), |
| 5617 | DIFF_FORMAT_CHECKDIFF, PARSE_OPT_NONEG), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5618 | OPT_BITOP(0, "summary", &options->output_format, |
Nguyễn Thái Ngọc Duy | 70a3041 | 2019-02-21 18:16:05 +0700 | [diff] [blame] | 5619 | N_("condensed summary such as creations, renames and mode changes"), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5620 | DIFF_FORMAT_SUMMARY, DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | 0e840e2 | 2019-02-21 18:16:07 +0700 | [diff] [blame] | 5621 | OPT_BIT_F(0, "name-only", &options->output_format, |
| 5622 | N_("show only names of changed files"), |
| 5623 | DIFF_FORMAT_NAME, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | a238747 | 2019-02-21 18:16:08 +0700 | [diff] [blame] | 5624 | OPT_BIT_F(0, "name-status", &options->output_format, |
| 5625 | N_("show only names and status of changed files"), |
| 5626 | DIFF_FORMAT_NAME_STATUS, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 5627 | OPT_CALLBACK_F(0, "stat", options, N_("<width>[,<name-width>[,<count>]]"), |
| 5628 | N_("generate diffstat"), |
| 5629 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_stat), |
| 5630 | OPT_CALLBACK_F(0, "stat-width", options, N_("<width>"), |
| 5631 | N_("generate diffstat with a given width"), |
| 5632 | PARSE_OPT_NONEG, diff_opt_stat), |
| 5633 | OPT_CALLBACK_F(0, "stat-name-width", options, N_("<width>"), |
| 5634 | N_("generate diffstat with a given name width"), |
| 5635 | PARSE_OPT_NONEG, diff_opt_stat), |
| 5636 | OPT_CALLBACK_F(0, "stat-graph-width", options, N_("<width>"), |
| 5637 | N_("generate diffstat with a given graph width"), |
| 5638 | PARSE_OPT_NONEG, diff_opt_stat), |
| 5639 | OPT_CALLBACK_F(0, "stat-count", options, N_("<count>"), |
| 5640 | N_("generate diffstat with limited lines"), |
| 5641 | PARSE_OPT_NONEG, diff_opt_stat), |
Nguyễn Thái Ngọc Duy | 7d7942b | 2019-02-21 18:16:11 +0700 | [diff] [blame] | 5642 | OPT_CALLBACK_F(0, "compact-summary", options, NULL, |
| 5643 | N_("generate compact summary in diffstat"), |
| 5644 | PARSE_OPT_NOARG, diff_opt_compact_summary), |
Nguyễn Thái Ngọc Duy | 6d9af6f | 2019-03-05 19:30:12 +0700 | [diff] [blame] | 5645 | OPT_CALLBACK_F(0, "binary", options, NULL, |
| 5646 | N_("output a binary diff that can be applied"), |
| 5647 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_binary), |
Nguyễn Thái Ngọc Duy | 4fe0167 | 2019-03-05 19:30:13 +0700 | [diff] [blame] | 5648 | OPT_BOOL(0, "full-index", &options->flags.full_index, |
| 5649 | N_("show full pre- and post-image object names on the \"index\" lines")), |
Nguyễn Thái Ngọc Duy | 8b81c26 | 2019-03-05 19:30:17 +0700 | [diff] [blame] | 5650 | OPT_COLOR_FLAG(0, "color", &options->use_color, |
| 5651 | N_("show colored diff")), |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 5652 | OPT_CALLBACK_F(0, "ws-error-highlight", options, N_("<kind>"), |
| 5653 | N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"), |
| 5654 | PARSE_OPT_NONEG, diff_opt_ws_error_highlight), |
Nguyễn Thái Ngọc Duy | 1a1eb16 | 2019-03-24 15:19:57 +0700 | [diff] [blame] | 5655 | OPT_SET_INT('z', NULL, &options->line_termination, |
| 5656 | N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"), |
| 5657 | 0), |
Nguyễn Thái Ngọc Duy | d877418 | 2019-03-24 15:20:04 +0700 | [diff] [blame] | 5658 | OPT__ABBREV(&options->abbrev), |
Nguyễn Thái Ngọc Duy | 31fba9d | 2019-03-24 15:20:05 +0700 | [diff] [blame] | 5659 | OPT_STRING_F(0, "src-prefix", &options->a_prefix, N_("<prefix>"), |
| 5660 | N_("show the given source prefix instead of \"a/\""), |
| 5661 | PARSE_OPT_NONEG), |
| 5662 | OPT_STRING_F(0, "dst-prefix", &options->b_prefix, N_("<prefix>"), |
Jiang Xin | 8a1569d | 2019-06-02 23:11:22 +0800 | [diff] [blame] | 5663 | N_("show the given destination prefix instead of \"b/\""), |
Nguyễn Thái Ngọc Duy | 31fba9d | 2019-03-24 15:20:05 +0700 | [diff] [blame] | 5664 | PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | 2f81cf9 | 2019-03-24 15:20:06 +0700 | [diff] [blame] | 5665 | OPT_CALLBACK_F(0, "line-prefix", options, N_("<prefix>"), |
| 5666 | N_("prepend an additional prefix to every line of output"), |
| 5667 | PARSE_OPT_NONEG, diff_opt_line_prefix), |
Nguyễn Thái Ngọc Duy | 11c659d | 2019-03-24 15:20:07 +0700 | [diff] [blame] | 5668 | OPT_CALLBACK_F(0, "no-prefix", options, NULL, |
| 5669 | N_("do not show any source or destination prefix"), |
| 5670 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_no_prefix), |
Jeff King | b39a569 | 2023-03-09 01:09:54 -0500 | [diff] [blame] | 5671 | OPT_CALLBACK_F(0, "default-prefix", options, NULL, |
| 5672 | N_("use default prefixes a/ and b/"), |
| 5673 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_default_prefix), |
Nguyễn Thái Ngọc Duy | 16ed6c9 | 2019-03-24 15:20:08 +0700 | [diff] [blame] | 5674 | OPT_INTEGER_F(0, "inter-hunk-context", &options->interhunkcontext, |
| 5675 | N_("show context between diff hunks up to the specified number of lines"), |
| 5676 | PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5677 | OPT_CALLBACK_F(0, "output-indicator-new", |
| 5678 | &options->output_indicators[OUTPUT_INDICATOR_NEW], |
| 5679 | N_("<char>"), |
| 5680 | N_("specify the character to indicate a new line instead of '+'"), |
| 5681 | PARSE_OPT_NONEG, diff_opt_char), |
| 5682 | OPT_CALLBACK_F(0, "output-indicator-old", |
| 5683 | &options->output_indicators[OUTPUT_INDICATOR_OLD], |
| 5684 | N_("<char>"), |
| 5685 | N_("specify the character to indicate an old line instead of '-'"), |
| 5686 | PARSE_OPT_NONEG, diff_opt_char), |
| 5687 | OPT_CALLBACK_F(0, "output-indicator-context", |
| 5688 | &options->output_indicators[OUTPUT_INDICATOR_CONTEXT], |
| 5689 | N_("<char>"), |
| 5690 | N_("specify the character to indicate a context instead of ' '"), |
| 5691 | PARSE_OPT_NONEG, diff_opt_char), |
| 5692 | |
Nguyễn Thái Ngọc Duy | ced4e17 | 2019-02-21 18:16:13 +0700 | [diff] [blame] | 5693 | OPT_GROUP(N_("Diff rename options")), |
| 5694 | OPT_CALLBACK_F('B', "break-rewrites", &options->break_opt, N_("<n>[/<m>]"), |
| 5695 | N_("break complete rewrite changes into pairs of delete and create"), |
| 5696 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5697 | diff_opt_break_rewrites), |
Nguyễn Thái Ngọc Duy | f476308 | 2019-02-21 18:16:14 +0700 | [diff] [blame] | 5698 | OPT_CALLBACK_F('M', "find-renames", options, N_("<n>"), |
| 5699 | N_("detect renames"), |
| 5700 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5701 | diff_opt_find_renames), |
Nguyễn Thái Ngọc Duy | 1e53329 | 2019-02-21 18:16:15 +0700 | [diff] [blame] | 5702 | OPT_SET_INT_F('D', "irreversible-delete", &options->irreversible_delete, |
| 5703 | N_("omit the preimage for deletes"), |
| 5704 | 1, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | 7f64850 | 2019-02-21 18:16:16 +0700 | [diff] [blame] | 5705 | OPT_CALLBACK_F('C', "find-copies", options, N_("<n>"), |
| 5706 | N_("detect copies"), |
| 5707 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5708 | diff_opt_find_copies), |
Nguyễn Thái Ngọc Duy | bdd4741 | 2019-02-21 18:16:17 +0700 | [diff] [blame] | 5709 | OPT_BOOL(0, "find-copies-harder", &options->flags.find_copies_harder, |
| 5710 | N_("use unmodified files as source to find copies")), |
Nguyễn Thái Ngọc Duy | cdc43eb | 2019-02-21 18:16:18 +0700 | [diff] [blame] | 5711 | OPT_SET_INT_F(0, "no-renames", &options->detect_rename, |
| 5712 | N_("disable rename detection"), |
| 5713 | 0, PARSE_OPT_NONEG), |
| 5714 | OPT_BOOL(0, "rename-empty", &options->flags.rename_empty, |
| 5715 | N_("use empty blobs as rename source")), |
Nguyễn Thái Ngọc Duy | 1e9250b | 2019-03-05 19:30:16 +0700 | [diff] [blame] | 5716 | OPT_CALLBACK_F(0, "follow", options, NULL, |
| 5717 | N_("continue listing the history of a file beyond renames"), |
| 5718 | PARSE_OPT_NOARG, diff_opt_follow), |
Nguyễn Thái Ngọc Duy | bffee74 | 2019-03-24 15:19:58 +0700 | [diff] [blame] | 5719 | OPT_INTEGER('l', NULL, &options->rename_limit, |
| 5720 | N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")), |
Nguyễn Thái Ngọc Duy | ced4e17 | 2019-02-21 18:16:13 +0700 | [diff] [blame] | 5721 | |
Nguyễn Thái Ngọc Duy | 2e75f92 | 2019-02-21 18:16:20 +0700 | [diff] [blame] | 5722 | OPT_GROUP(N_("Diff algorithm options")), |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 5723 | OPT_CALLBACK_F(0, "minimal", options, NULL, |
| 5724 | N_("produce the smallest possible diff"), |
| 5725 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, |
| 5726 | diff_opt_diff_algorithm_no_arg), |
Nguyễn Thái Ngọc Duy | 87649a1 | 2019-02-21 18:16:21 +0700 | [diff] [blame] | 5727 | OPT_BIT_F('w', "ignore-all-space", &options->xdl_opts, |
| 5728 | N_("ignore whitespace when comparing lines"), |
| 5729 | XDF_IGNORE_WHITESPACE, PARSE_OPT_NONEG), |
| 5730 | OPT_BIT_F('b', "ignore-space-change", &options->xdl_opts, |
| 5731 | N_("ignore changes in amount of whitespace"), |
| 5732 | XDF_IGNORE_WHITESPACE_CHANGE, PARSE_OPT_NONEG), |
| 5733 | OPT_BIT_F(0, "ignore-space-at-eol", &options->xdl_opts, |
| 5734 | N_("ignore changes in whitespace at EOL"), |
| 5735 | XDF_IGNORE_WHITESPACE_AT_EOL, PARSE_OPT_NONEG), |
| 5736 | OPT_BIT_F(0, "ignore-cr-at-eol", &options->xdl_opts, |
| 5737 | N_("ignore carrier-return at the end of line"), |
| 5738 | XDF_IGNORE_CR_AT_EOL, PARSE_OPT_NONEG), |
| 5739 | OPT_BIT_F(0, "ignore-blank-lines", &options->xdl_opts, |
| 5740 | N_("ignore changes whose lines are all blank"), |
| 5741 | XDF_IGNORE_BLANK_LINES, PARSE_OPT_NONEG), |
Michał Kępień | 296d4a9 | 2020-10-20 08:48:09 +0200 | [diff] [blame] | 5742 | OPT_CALLBACK_F('I', "ignore-matching-lines", options, N_("<regex>"), |
| 5743 | N_("ignore changes whose all lines match <regex>"), |
| 5744 | 0, diff_opt_ignore_regex), |
Nguyễn Thái Ngọc Duy | 06f7751 | 2019-03-05 19:30:07 +0700 | [diff] [blame] | 5745 | OPT_BIT(0, "indent-heuristic", &options->xdl_opts, |
| 5746 | N_("heuristic to shift diff hunk boundaries for easy reading"), |
| 5747 | XDF_INDENT_HEURISTIC), |
Nguyễn Thái Ngọc Duy | 31fd640 | 2019-03-05 19:30:08 +0700 | [diff] [blame] | 5748 | OPT_CALLBACK_F(0, "patience", options, NULL, |
| 5749 | N_("generate diff using the \"patience diff\" algorithm"), |
| 5750 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, |
| 5751 | diff_opt_patience), |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 5752 | OPT_CALLBACK_F(0, "histogram", options, NULL, |
| 5753 | N_("generate diff using the \"histogram diff\" algorithm"), |
| 5754 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, |
| 5755 | diff_opt_diff_algorithm_no_arg), |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5756 | OPT_CALLBACK_F(0, "diff-algorithm", options, N_("<algorithm>"), |
| 5757 | N_("choose a diff algorithm"), |
| 5758 | PARSE_OPT_NONEG, diff_opt_diff_algorithm), |
Nguyễn Thái Ngọc Duy | df84a43 | 2019-03-05 19:30:11 +0700 | [diff] [blame] | 5759 | OPT_CALLBACK_F(0, "anchored", options, N_("<text>"), |
| 5760 | N_("generate diff using the \"anchored diff\" algorithm"), |
| 5761 | PARSE_OPT_NONEG, diff_opt_anchored), |
Nguyễn Thái Ngọc Duy | e9fb39b | 2019-03-05 19:30:18 +0700 | [diff] [blame] | 5762 | OPT_CALLBACK_F(0, "word-diff", options, N_("<mode>"), |
| 5763 | N_("show word diff, using <mode> to delimit changed words"), |
| 5764 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_word_diff), |
Nguyễn Thái Ngọc Duy | 797df11 | 2019-03-05 19:30:19 +0700 | [diff] [blame] | 5765 | OPT_CALLBACK_F(0, "word-diff-regex", options, N_("<regex>"), |
| 5766 | N_("use <regex> to decide what a word is"), |
| 5767 | PARSE_OPT_NONEG, diff_opt_word_diff_regex), |
Nguyễn Thái Ngọc Duy | 212db69 | 2019-03-05 19:30:20 +0700 | [diff] [blame] | 5768 | OPT_CALLBACK_F(0, "color-words", options, N_("<regex>"), |
| 5769 | N_("equivalent to --word-diff=color --word-diff-regex=<regex>"), |
| 5770 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_color_words), |
Nguyễn Thái Ngọc Duy | 59311a9 | 2019-03-24 15:20:09 +0700 | [diff] [blame] | 5771 | OPT_CALLBACK_F(0, "color-moved", options, N_("<mode>"), |
Jean-Noël Avila | 8bcd8f4 | 2019-05-17 21:26:19 +0200 | [diff] [blame] | 5772 | N_("moved lines of code are colored differently"), |
Nguyễn Thái Ngọc Duy | 59311a9 | 2019-03-24 15:20:09 +0700 | [diff] [blame] | 5773 | PARSE_OPT_OPTARG, diff_opt_color_moved), |
Nguyễn Thái Ngọc Duy | 8ce2020 | 2019-03-24 15:20:10 +0700 | [diff] [blame] | 5774 | OPT_CALLBACK_F(0, "color-moved-ws", options, N_("<mode>"), |
| 5775 | N_("how white spaces are ignored in --color-moved"), |
Nguyễn Thái Ngọc Duy | bb98729 | 2019-03-24 15:20:11 +0700 | [diff] [blame] | 5776 | 0, diff_opt_color_moved_ws), |
Nguyễn Thái Ngọc Duy | 2e75f92 | 2019-02-21 18:16:20 +0700 | [diff] [blame] | 5777 | |
Jean-Noël Avila | 8bcd8f4 | 2019-05-17 21:26:19 +0200 | [diff] [blame] | 5778 | OPT_GROUP(N_("Other diff options")), |
Nguyễn Thái Ngọc Duy | 0b1c5b5 | 2019-02-21 18:16:19 +0700 | [diff] [blame] | 5779 | OPT_CALLBACK_F(0, "relative", options, N_("<prefix>"), |
| 5780 | N_("when run from subdir, exclude changes outside and show relative paths"), |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 5781 | PARSE_OPT_OPTARG, |
Nguyễn Thái Ngọc Duy | 0b1c5b5 | 2019-02-21 18:16:19 +0700 | [diff] [blame] | 5782 | diff_opt_relative), |
Nguyễn Thái Ngọc Duy | c37d4c0 | 2019-03-05 19:30:14 +0700 | [diff] [blame] | 5783 | OPT_BOOL('a', "text", &options->flags.text, |
| 5784 | N_("treat all files as text")), |
Nguyễn Thái Ngọc Duy | 4f99f29 | 2019-03-05 19:30:15 +0700 | [diff] [blame] | 5785 | OPT_BOOL('R', NULL, &options->flags.reverse_diff, |
| 5786 | N_("swap two inputs, reverse the diff")), |
Nguyễn Thái Ngọc Duy | 1086ea0 | 2019-03-05 19:30:21 +0700 | [diff] [blame] | 5787 | OPT_BOOL(0, "exit-code", &options->flags.exit_with_status, |
| 5788 | N_("exit with 1 if there were differences, 0 otherwise")), |
Nguyễn Thái Ngọc Duy | 9bbaf1c | 2019-03-05 19:30:22 +0700 | [diff] [blame] | 5789 | OPT_BOOL(0, "quiet", &options->flags.quick, |
| 5790 | N_("disable all output of the program")), |
Nguyễn Thái Ngọc Duy | 0cda100 | 2019-03-05 19:30:23 +0700 | [diff] [blame] | 5791 | OPT_BOOL(0, "ext-diff", &options->flags.allow_external, |
| 5792 | N_("allow an external diff helper to be executed")), |
Nguyễn Thái Ngọc Duy | 8ab7697 | 2019-03-05 19:30:24 +0700 | [diff] [blame] | 5793 | OPT_CALLBACK_F(0, "textconv", options, NULL, |
| 5794 | N_("run external text conversion filters when comparing binary files"), |
| 5795 | PARSE_OPT_NOARG, diff_opt_textconv), |
Nguyễn Thái Ngọc Duy | b680ee1 | 2019-03-05 19:30:25 +0700 | [diff] [blame] | 5796 | OPT_CALLBACK_F(0, "ignore-submodules", options, N_("<when>"), |
| 5797 | N_("ignore changes to submodules in the diff generation"), |
| 5798 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5799 | diff_opt_ignore_submodules), |
Nguyễn Thái Ngọc Duy | 125dcea | 2019-03-05 19:30:26 +0700 | [diff] [blame] | 5800 | OPT_CALLBACK_F(0, "submodule", options, N_("<format>"), |
| 5801 | N_("specify how differences in submodules are shown"), |
| 5802 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5803 | diff_opt_submodule), |
Nguyễn Thái Ngọc Duy | 96860e4 | 2019-03-24 15:19:56 +0700 | [diff] [blame] | 5804 | OPT_SET_INT_F(0, "ita-invisible-in-index", &options->ita_invisible_in_index, |
| 5805 | N_("hide 'git add -N' entries from the index"), |
| 5806 | 1, PARSE_OPT_NONEG), |
| 5807 | OPT_SET_INT_F(0, "ita-visible-in-index", &options->ita_invisible_in_index, |
| 5808 | N_("treat 'git add -N' entries as real in the index"), |
| 5809 | 0, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | a41cfb3 | 2019-03-24 15:19:59 +0700 | [diff] [blame] | 5810 | OPT_CALLBACK_F('S', NULL, options, N_("<string>"), |
| 5811 | N_("look for differences that change the number of occurrences of the specified string"), |
| 5812 | 0, diff_opt_pickaxe_string), |
| 5813 | OPT_CALLBACK_F('G', NULL, options, N_("<regex>"), |
| 5814 | N_("look for differences that change the number of occurrences of the specified regex"), |
| 5815 | 0, diff_opt_pickaxe_regex), |
Nguyễn Thái Ngọc Duy | 85f8e88 | 2019-03-24 15:20:00 +0700 | [diff] [blame] | 5816 | OPT_BIT_F(0, "pickaxe-all", &options->pickaxe_opts, |
| 5817 | N_("show all changes in the changeset with -S or -G"), |
| 5818 | DIFF_PICKAXE_ALL, PARSE_OPT_NONEG), |
| 5819 | OPT_BIT_F(0, "pickaxe-regex", &options->pickaxe_opts, |
| 5820 | N_("treat <string> in -S as extended POSIX regular expression"), |
| 5821 | DIFF_PICKAXE_REGEX, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | f731814 | 2019-03-24 15:20:01 +0700 | [diff] [blame] | 5822 | OPT_FILENAME('O', NULL, &options->orderfile, |
| 5823 | N_("control the order in which files appear in the output")), |
Junio C Hamano | 1eb4136 | 2021-02-11 11:57:50 -0800 | [diff] [blame] | 5824 | OPT_CALLBACK_F(0, "rotate-to", options, N_("<path>"), |
| 5825 | N_("show the change in the specified path first"), |
| 5826 | PARSE_OPT_NONEG, diff_opt_rotate_to), |
| 5827 | OPT_CALLBACK_F(0, "skip-to", options, N_("<path>"), |
| 5828 | N_("skip the output to the specified path"), |
| 5829 | PARSE_OPT_NONEG, diff_opt_rotate_to), |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5830 | OPT_CALLBACK_F(0, "find-object", options, N_("<object-id>"), |
| 5831 | N_("look for differences that change the number of occurrences of the specified object"), |
| 5832 | PARSE_OPT_NONEG, diff_opt_find_object), |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 5833 | OPT_CALLBACK_F(0, "diff-filter", options, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"), |
| 5834 | N_("select files by diff type"), |
| 5835 | PARSE_OPT_NONEG, diff_opt_diff_filter), |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5836 | { OPTION_CALLBACK, 0, "output", options, N_("<file>"), |
Abhradeep Chakraborty | 9e1f22c | 2022-02-23 14:27:34 +0000 | [diff] [blame] | 5837 | N_("output to a specific file"), |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5838 | PARSE_OPT_NONEG, NULL, 0, diff_opt_output }, |
| 5839 | |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5840 | OPT_END() |
| 5841 | }; |
| 5842 | |
René Scharfe | 189e97b | 2022-12-01 23:53:17 +0100 | [diff] [blame] | 5843 | return parse_options_concat(opts, parseopts); |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5844 | } |
| 5845 | |
Duy Nguyen | a97262c | 2016-01-21 18:48:44 +0700 | [diff] [blame] | 5846 | int diff_opt_parse(struct diff_options *options, |
| 5847 | const char **av, int ac, const char *prefix) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5848 | { |
René Scharfe | 6c6048f | 2022-12-01 23:51:21 +0100 | [diff] [blame] | 5849 | struct option no_options[] = { OPT_END() }; |
| 5850 | struct option *parseopts = add_diff_options(no_options, options); |
| 5851 | |
Duy Nguyen | a97262c | 2016-01-21 18:48:44 +0700 | [diff] [blame] | 5852 | if (!prefix) |
| 5853 | prefix = ""; |
| 5854 | |
René Scharfe | 6c6048f | 2022-12-01 23:51:21 +0100 | [diff] [blame] | 5855 | ac = parse_options(ac, av, prefix, parseopts, NULL, |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5856 | PARSE_OPT_KEEP_DASHDASH | |
SZEDER Gábor | 99d86d6 | 2022-08-19 18:03:57 +0200 | [diff] [blame] | 5857 | PARSE_OPT_KEEP_UNKNOWN_OPT | |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5858 | PARSE_OPT_NO_INTERNAL_HELP | |
| 5859 | PARSE_OPT_ONE_SHOT | |
| 5860 | PARSE_OPT_STOP_AT_NON_OPTION); |
René Scharfe | 6c6048f | 2022-12-01 23:51:21 +0100 | [diff] [blame] | 5861 | free(parseopts); |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5862 | |
Nguyễn Thái Ngọc Duy | 8ce2020 | 2019-03-24 15:20:10 +0700 | [diff] [blame] | 5863 | return ac; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5864 | } |
| 5865 | |
Kevin Ballard | 10ae752 | 2010-09-27 16:58:25 -0700 | [diff] [blame] | 5866 | int parse_rename_score(const char **cp_p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5867 | { |
| 5868 | unsigned long num, scale; |
| 5869 | int ch, dot; |
| 5870 | const char *cp = *cp_p; |
| 5871 | |
| 5872 | num = 0; |
| 5873 | scale = 1; |
| 5874 | dot = 0; |
Brian Gianforcaro | eeefa7c | 2009-09-01 01:35:10 -0400 | [diff] [blame] | 5875 | for (;;) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5876 | ch = *cp; |
| 5877 | if ( !dot && ch == '.' ) { |
| 5878 | scale = 1; |
| 5879 | dot = 1; |
| 5880 | } else if ( ch == '%' ) { |
| 5881 | scale = dot ? scale*100 : 100; |
| 5882 | cp++; /* % is always at the end */ |
| 5883 | break; |
| 5884 | } else if ( ch >= '0' && ch <= '9' ) { |
| 5885 | if ( scale < 100000 ) { |
| 5886 | scale *= 10; |
| 5887 | num = (num*10) + (ch-'0'); |
| 5888 | } |
| 5889 | } else { |
| 5890 | break; |
| 5891 | } |
| 5892 | cp++; |
| 5893 | } |
| 5894 | *cp_p = cp; |
| 5895 | |
| 5896 | /* user says num divided by scale and we say internally that |
| 5897 | * is MAX_SCORE * num / scale. |
| 5898 | */ |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 5899 | return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale)); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5900 | } |
| 5901 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5902 | struct diff_queue_struct diff_queued_diff; |
| 5903 | |
| 5904 | void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp) |
| 5905 | { |
Dmitry S. Dolzhenko | 4c960a4 | 2014-03-04 02:31:53 +0400 | [diff] [blame] | 5906 | ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5907 | queue->queue[queue->nr++] = dp; |
| 5908 | } |
| 5909 | |
| 5910 | struct diff_filepair *diff_queue(struct diff_queue_struct *queue, |
| 5911 | struct diff_filespec *one, |
| 5912 | struct diff_filespec *two) |
| 5913 | { |
Junio C Hamano | ef67768 | 2006-08-03 12:01:01 -0700 | [diff] [blame] | 5914 | struct diff_filepair *dp = xcalloc(1, sizeof(*dp)); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5915 | dp->one = one; |
| 5916 | dp->two = two; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5917 | if (queue) |
| 5918 | diff_q(queue, dp); |
| 5919 | return dp; |
| 5920 | } |
| 5921 | |
| 5922 | void diff_free_filepair(struct diff_filepair *p) |
| 5923 | { |
Linus Torvalds | 9fb8841 | 2007-10-25 11:19:10 -0700 | [diff] [blame] | 5924 | free_filespec(p->one); |
| 5925 | free_filespec(p->two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5926 | free(p); |
| 5927 | } |
| 5928 | |
SZEDER Gábor | 04ae000 | 2022-11-02 23:01:40 +0100 | [diff] [blame] | 5929 | void diff_free_queue(struct diff_queue_struct *q) |
| 5930 | { |
| 5931 | for (int i = 0; i < q->nr; i++) |
| 5932 | diff_free_filepair(q->queue[i]); |
| 5933 | free(q->queue); |
| 5934 | } |
| 5935 | |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5936 | const char *diff_aligned_abbrev(const struct object_id *oid, int len) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5937 | { |
| 5938 | int abblen; |
| 5939 | const char *abbrev; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5940 | |
Ann T Ropea | 7cb6ac1 | 2017-12-03 22:27:42 +0100 | [diff] [blame] | 5941 | /* Do we want all 40 hex characters? */ |
brian m. carlson | 02afca1 | 2018-07-16 01:28:05 +0000 | [diff] [blame] | 5942 | if (len == the_hash_algo->hexsz) |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5943 | return oid_to_hex(oid); |
| 5944 | |
Ann T Ropea | 7cb6ac1 | 2017-12-03 22:27:42 +0100 | [diff] [blame] | 5945 | /* An abbreviated value is fine, possibly followed by an ellipsis. */ |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 5946 | abbrev = diff_abbrev_oid(oid, len); |
Ann T Ropea | 7cb6ac1 | 2017-12-03 22:27:42 +0100 | [diff] [blame] | 5947 | |
| 5948 | if (!print_sha1_ellipsis()) |
| 5949 | return abbrev; |
| 5950 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5951 | abblen = strlen(abbrev); |
Junio C Hamano | d709f1f | 2016-09-30 10:42:05 -0700 | [diff] [blame] | 5952 | |
| 5953 | /* |
Ann T Ropea | 7cb6ac1 | 2017-12-03 22:27:42 +0100 | [diff] [blame] | 5954 | * In well-behaved cases, where the abbreviated result is the |
Junio C Hamano | d709f1f | 2016-09-30 10:42:05 -0700 | [diff] [blame] | 5955 | * same as the requested length, append three dots after the |
| 5956 | * abbreviation (hence the whole logic is limited to the case |
| 5957 | * where abblen < 37); when the actual abbreviated result is a |
| 5958 | * bit longer than the requested length, we reduce the number |
| 5959 | * of dots so that they match the well-behaved ones. However, |
| 5960 | * if the actual abbreviation is longer than the requested |
| 5961 | * length by more than three, we give up on aligning, and add |
| 5962 | * three dots anyway, to indicate that the output is not the |
| 5963 | * full object name. Yes, this may be suboptimal, but this |
| 5964 | * appears only in "diff --raw --abbrev" output and it is not |
| 5965 | * worth the effort to change it now. Note that this would |
| 5966 | * likely to work fine when the automatic sizing of default |
| 5967 | * abbreviation length is used--we would be fed -1 in "len" in |
| 5968 | * that case, and will end up always appending three-dots, but |
| 5969 | * the automatic sizing is supposed to give abblen that ensures |
| 5970 | * uniqueness across all objects (statistically speaking). |
| 5971 | */ |
brian m. carlson | 02afca1 | 2018-07-16 01:28:05 +0000 | [diff] [blame] | 5972 | if (abblen < the_hash_algo->hexsz - 3) { |
brian m. carlson | dc01505 | 2017-03-26 16:01:24 +0000 | [diff] [blame] | 5973 | static char hex[GIT_MAX_HEXSZ + 1]; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5974 | if (len < abblen && abblen <= len + 2) |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 5975 | xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, ".."); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5976 | else |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 5977 | xsnprintf(hex, sizeof(hex), "%s...", abbrev); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5978 | return hex; |
| 5979 | } |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5980 | |
| 5981 | return oid_to_hex(oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5982 | } |
| 5983 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5984 | static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5985 | { |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5986 | int line_termination = opt->line_termination; |
| 5987 | int inter_name_termination = line_termination ? '\t' : '\0'; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 5988 | |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 5989 | fprintf(opt->file, "%s", diff_line_prefix(opt)); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5990 | if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 5991 | fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode, |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5992 | diff_aligned_abbrev(&p->one->oid, opt->abbrev)); |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 5993 | fprintf(opt->file, "%s ", |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5994 | diff_aligned_abbrev(&p->two->oid, opt->abbrev)); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5995 | } |
| 5996 | if (p->score) { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 5997 | fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p), |
| 5998 | inter_name_termination); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 5999 | } else { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6000 | fprintf(opt->file, "%c%c", p->status, inter_name_termination); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6001 | } |
| 6002 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 6003 | if (p->status == DIFF_STATUS_COPIED || |
| 6004 | p->status == DIFF_STATUS_RENAMED) { |
| 6005 | const char *name_a, *name_b; |
| 6006 | name_a = p->one->path; |
| 6007 | name_b = p->two->path; |
| 6008 | strip_prefix(opt->prefix_length, &name_a, &name_b); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6009 | write_name_quoted(name_a, opt->file, inter_name_termination); |
| 6010 | write_name_quoted(name_b, opt->file, line_termination); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6011 | } else { |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 6012 | const char *name_a, *name_b; |
| 6013 | name_a = p->one->mode ? p->one->path : p->two->path; |
| 6014 | name_b = NULL; |
| 6015 | strip_prefix(opt->prefix_length, &name_a, &name_b); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6016 | write_name_quoted(name_a, opt->file, line_termination); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6017 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6018 | } |
| 6019 | |
| 6020 | int diff_unmodified_pair(struct diff_filepair *p) |
| 6021 | { |
| 6022 | /* This function is written stricter than necessary to support |
| 6023 | * the currently implemented transformers, but the idea is to |
| 6024 | * let transformers to produce diff_filepairs any way they want, |
| 6025 | * and filter and clean them up here before producing the output. |
| 6026 | */ |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6027 | struct diff_filespec *one = p->one, *two = p->two; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6028 | |
| 6029 | if (DIFF_PAIR_UNMERGED(p)) |
| 6030 | return 0; /* unmerged is interesting */ |
| 6031 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6032 | /* deletion, addition, mode or type change |
| 6033 | * and rename are all interesting. |
| 6034 | */ |
| 6035 | if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) || |
| 6036 | DIFF_PAIR_MODE_CHANGED(p) || |
| 6037 | strcmp(one->path, two->path)) |
| 6038 | return 0; |
| 6039 | |
| 6040 | /* both are valid and point at the same path. that is, we are |
| 6041 | * dealing with a change. |
| 6042 | */ |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 6043 | if (one->oid_valid && two->oid_valid && |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 6044 | oideq(&one->oid, &two->oid) && |
Jens Lehmann | 85adbf2 | 2010-03-12 22:23:52 +0100 | [diff] [blame] | 6045 | !one->dirty_submodule && !two->dirty_submodule) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6046 | return 1; /* no change */ |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 6047 | if (!one->oid_valid && !two->oid_valid) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6048 | return 1; /* both look at the same file on the filesystem. */ |
| 6049 | return 0; |
| 6050 | } |
| 6051 | |
| 6052 | static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o) |
| 6053 | { |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6054 | int include_conflict_headers = |
| 6055 | (additional_headers(o, p->one->path) && |
Elijah Newren | 71a146d | 2022-09-02 03:53:29 +0000 | [diff] [blame] | 6056 | !o->pickaxe_opts && |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6057 | (!o->filter || filter_bit_tst(DIFF_STATUS_UNMERGED, o))); |
| 6058 | |
| 6059 | /* |
| 6060 | * Check if we can return early without showing a diff. Note that |
| 6061 | * diff_filepair only stores {oid, path, mode, is_valid} |
| 6062 | * information for each path, and thus diff_unmodified_pair() only |
| 6063 | * considers those bits of info. However, we do not want pairs |
| 6064 | * created by create_filepairs_for_header_only_notifications() |
| 6065 | * (which always look like unmodified pairs) to be ignored, so |
| 6066 | * return early if both p is unmodified AND we don't want to |
| 6067 | * include_conflict_headers. |
| 6068 | */ |
| 6069 | if (diff_unmodified_pair(p) && !include_conflict_headers) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6070 | return; |
| 6071 | |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6072 | /* Actually, we can also return early to avoid showing tree diffs */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6073 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 6074 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6075 | return; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6076 | |
| 6077 | run_diff(p, o); |
| 6078 | } |
| 6079 | |
| 6080 | static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o, |
| 6081 | struct diffstat_t *diffstat) |
| 6082 | { |
| 6083 | if (diff_unmodified_pair(p)) |
| 6084 | return; |
| 6085 | |
| 6086 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 6087 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
Yann Dirson | c3fced6 | 2010-10-24 23:03:10 +0200 | [diff] [blame] | 6088 | return; /* no useful stat for tree diffs */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6089 | |
| 6090 | run_diffstat(p, o, diffstat); |
| 6091 | } |
| 6092 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 6093 | static void diff_flush_checkdiff(struct diff_filepair *p, |
| 6094 | struct diff_options *o) |
| 6095 | { |
| 6096 | if (diff_unmodified_pair(p)) |
| 6097 | return; |
| 6098 | |
| 6099 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 6100 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
Yann Dirson | c3fced6 | 2010-10-24 23:03:10 +0200 | [diff] [blame] | 6101 | return; /* nothing to check in tree diffs */ |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 6102 | |
| 6103 | run_checkdiff(p, o); |
| 6104 | } |
| 6105 | |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6106 | int diff_queue_is_empty(struct diff_options *o) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6107 | { |
| 6108 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6109 | int i; |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6110 | int include_conflict_headers = |
| 6111 | (o->additional_path_headers && |
Elijah Newren | 67360b7 | 2022-09-02 03:53:30 +0000 | [diff] [blame] | 6112 | strmap_get_size(o->additional_path_headers) && |
Elijah Newren | 71a146d | 2022-09-02 03:53:29 +0000 | [diff] [blame] | 6113 | !o->pickaxe_opts && |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6114 | (!o->filter || filter_bit_tst(DIFF_STATUS_UNMERGED, o))); |
| 6115 | |
| 6116 | if (include_conflict_headers) |
| 6117 | return 0; |
| 6118 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6119 | for (i = 0; i < q->nr; i++) |
| 6120 | if (!diff_unmodified_pair(q->queue[i])) |
| 6121 | return 0; |
| 6122 | return 1; |
| 6123 | } |
| 6124 | |
| 6125 | #if DIFF_DEBUG |
| 6126 | void diff_debug_filespec(struct diff_filespec *s, int x, const char *one) |
| 6127 | { |
| 6128 | fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n", |
| 6129 | x, one ? one : "", |
| 6130 | s->path, |
| 6131 | DIFF_FILE_VALID(s) ? "valid" : "invalid", |
| 6132 | s->mode, |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 6133 | s->oid_valid ? oid_to_hex(&s->oid) : ""); |
Jeff King | 428d52a | 2014-01-16 20:21:59 -0500 | [diff] [blame] | 6134 | fprintf(stderr, "queue[%d] %s size %lu\n", |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6135 | x, one ? one : "", |
Jeff King | 428d52a | 2014-01-16 20:21:59 -0500 | [diff] [blame] | 6136 | s->size); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6137 | } |
| 6138 | |
| 6139 | void diff_debug_filepair(const struct diff_filepair *p, int i) |
| 6140 | { |
| 6141 | diff_debug_filespec(p->one, i, "one"); |
| 6142 | diff_debug_filespec(p->two, i, "two"); |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6143 | fprintf(stderr, "score %d, status %c rename_used %d broken %d\n", |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6144 | p->score, p->status ? p->status : '?', |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6145 | p->one->rename_used, p->broken_pair); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6146 | } |
| 6147 | |
| 6148 | void diff_debug_queue(const char *msg, struct diff_queue_struct *q) |
| 6149 | { |
| 6150 | int i; |
| 6151 | if (msg) |
| 6152 | fprintf(stderr, "%s\n", msg); |
| 6153 | fprintf(stderr, "q->nr = %d\n", q->nr); |
| 6154 | for (i = 0; i < q->nr; i++) { |
| 6155 | struct diff_filepair *p = q->queue[i]; |
| 6156 | diff_debug_filepair(p, i); |
| 6157 | } |
| 6158 | } |
| 6159 | #endif |
| 6160 | |
| 6161 | static void diff_resolve_rename_copy(void) |
| 6162 | { |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6163 | int i; |
| 6164 | struct diff_filepair *p; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6165 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6166 | |
| 6167 | diff_debug_queue("resolve-rename-copy", q); |
| 6168 | |
| 6169 | for (i = 0; i < q->nr; i++) { |
| 6170 | p = q->queue[i]; |
| 6171 | p->status = 0; /* undecided */ |
| 6172 | if (DIFF_PAIR_UNMERGED(p)) |
| 6173 | p->status = DIFF_STATUS_UNMERGED; |
| 6174 | else if (!DIFF_FILE_VALID(p->one)) |
| 6175 | p->status = DIFF_STATUS_ADDED; |
| 6176 | else if (!DIFF_FILE_VALID(p->two)) |
| 6177 | p->status = DIFF_STATUS_DELETED; |
| 6178 | else if (DIFF_PAIR_TYPE_CHANGED(p)) |
| 6179 | p->status = DIFF_STATUS_TYPE_CHANGED; |
| 6180 | |
| 6181 | /* from this point on, we are dealing with a pair |
| 6182 | * whose both sides are valid and of the same type, i.e. |
| 6183 | * either in-place edit or rename/copy edit. |
| 6184 | */ |
| 6185 | else if (DIFF_PAIR_RENAME(p)) { |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6186 | /* |
| 6187 | * A rename might have re-connected a broken |
| 6188 | * pair up, causing the pathnames to be the |
| 6189 | * same again. If so, that's not a rename at |
| 6190 | * all, just a modification.. |
| 6191 | * |
| 6192 | * Otherwise, see if this source was used for |
| 6193 | * multiple renames, in which case we decrement |
| 6194 | * the count, and call it a copy. |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6195 | */ |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6196 | if (!strcmp(p->one->path, p->two->path)) |
| 6197 | p->status = DIFF_STATUS_MODIFIED; |
| 6198 | else if (--p->one->rename_used > 0) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6199 | p->status = DIFF_STATUS_COPIED; |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6200 | else |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6201 | p->status = DIFF_STATUS_RENAMED; |
| 6202 | } |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 6203 | else if (!oideq(&p->one->oid, &p->two->oid) || |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 6204 | p->one->mode != p->two->mode || |
Jens Lehmann | 85adbf2 | 2010-03-12 22:23:52 +0100 | [diff] [blame] | 6205 | p->one->dirty_submodule || |
| 6206 | p->two->dirty_submodule || |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 6207 | is_null_oid(&p->one->oid)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6208 | p->status = DIFF_STATUS_MODIFIED; |
| 6209 | else { |
| 6210 | /* This is a "no-change" entry and should not |
| 6211 | * happen anymore, but prepare for broken callers. |
| 6212 | */ |
| 6213 | error("feeding unmodified %s to diffcore", |
| 6214 | p->one->path); |
| 6215 | p->status = DIFF_STATUS_UNKNOWN; |
| 6216 | } |
| 6217 | } |
| 6218 | diff_debug_queue("resolve-rename-copy done", q); |
| 6219 | } |
| 6220 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6221 | static int check_pair_status(struct diff_filepair *p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6222 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6223 | switch (p->status) { |
| 6224 | case DIFF_STATUS_UNKNOWN: |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6225 | return 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6226 | case 0: |
| 6227 | die("internal error in diff-resolve-rename-copy"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6228 | default: |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6229 | return 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6230 | } |
| 6231 | } |
| 6232 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6233 | static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt) |
| 6234 | { |
| 6235 | int fmt = opt->output_format; |
| 6236 | |
| 6237 | if (fmt & DIFF_FORMAT_CHECKDIFF) |
| 6238 | diff_flush_checkdiff(p, opt); |
| 6239 | else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) |
| 6240 | diff_flush_raw(p, opt); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 6241 | else if (fmt & DIFF_FORMAT_NAME) { |
| 6242 | const char *name_a, *name_b; |
| 6243 | name_a = p->two->path; |
| 6244 | name_b = NULL; |
| 6245 | strip_prefix(opt->prefix_length, &name_a, &name_b); |
Jeff King | f5022b5 | 2017-02-08 15:31:15 -0500 | [diff] [blame] | 6246 | fprintf(opt->file, "%s", diff_line_prefix(opt)); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6247 | write_name_quoted(name_a, opt->file, opt->line_termination); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 6248 | } |
Junio C Hamano | a64f8b2 | 2023-08-18 16:59:32 -0700 | [diff] [blame] | 6249 | |
| 6250 | opt->found_changes = 1; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6251 | } |
| 6252 | |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6253 | static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs) |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6254 | { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6255 | struct strbuf sb = STRBUF_INIT; |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6256 | if (fs->mode) |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6257 | strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6258 | else |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6259 | strbuf_addf(&sb, " %s ", newdelete); |
| 6260 | |
| 6261 | quote_c_style(fs->path, &sb, NULL, 0); |
| 6262 | strbuf_addch(&sb, '\n'); |
| 6263 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
| 6264 | sb.buf, sb.len, 0); |
| 6265 | strbuf_release(&sb); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6266 | } |
| 6267 | |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6268 | static void show_mode_change(struct diff_options *opt, struct diff_filepair *p, |
| 6269 | int show_name) |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6270 | { |
| 6271 | if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6272 | struct strbuf sb = STRBUF_INIT; |
| 6273 | strbuf_addf(&sb, " mode change %06o => %06o", |
| 6274 | p->one->mode, p->two->mode); |
Alexandre Julliard | 0d26a64 | 2007-02-10 15:37:48 +0100 | [diff] [blame] | 6275 | if (show_name) { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6276 | strbuf_addch(&sb, ' '); |
| 6277 | quote_c_style(p->two->path, &sb, NULL, 0); |
Alexandre Julliard | 0d26a64 | 2007-02-10 15:37:48 +0100 | [diff] [blame] | 6278 | } |
Stefan Beller | 58aaced | 2017-09-27 15:51:26 -0700 | [diff] [blame] | 6279 | strbuf_addch(&sb, '\n'); |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6280 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
| 6281 | sb.buf, sb.len, 0); |
| 6282 | strbuf_release(&sb); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6283 | } |
| 6284 | } |
| 6285 | |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6286 | static void show_rename_copy(struct diff_options *opt, const char *renamecopy, |
| 6287 | struct diff_filepair *p) |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6288 | { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6289 | struct strbuf sb = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 6290 | struct strbuf names = STRBUF_INIT; |
| 6291 | |
| 6292 | pprint_rename(&names, p->one->path, p->two->path); |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6293 | strbuf_addf(&sb, " %s %s (%d%%)\n", |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 6294 | renamecopy, names.buf, similarity_index(p)); |
| 6295 | strbuf_release(&names); |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6296 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
| 6297 | sb.buf, sb.len, 0); |
| 6298 | show_mode_change(opt, p, 0); |
Rene Scharfe | 348eda2 | 2017-08-30 19:49:42 +0200 | [diff] [blame] | 6299 | strbuf_release(&sb); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6300 | } |
| 6301 | |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 6302 | static void diff_summary(struct diff_options *opt, struct diff_filepair *p) |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6303 | { |
| 6304 | switch(p->status) { |
| 6305 | case DIFF_STATUS_DELETED: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6306 | show_file_mode_name(opt, "delete", p->one); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6307 | break; |
| 6308 | case DIFF_STATUS_ADDED: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6309 | show_file_mode_name(opt, "create", p->two); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6310 | break; |
| 6311 | case DIFF_STATUS_COPIED: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6312 | show_rename_copy(opt, "copy", p); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6313 | break; |
| 6314 | case DIFF_STATUS_RENAMED: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6315 | show_rename_copy(opt, "rename", p); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6316 | break; |
| 6317 | default: |
| 6318 | if (p->score) { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6319 | struct strbuf sb = STRBUF_INIT; |
| 6320 | strbuf_addstr(&sb, " rewrite "); |
| 6321 | quote_c_style(p->two->path, &sb, NULL, 0); |
| 6322 | strbuf_addf(&sb, " (%d%%)\n", similarity_index(p)); |
| 6323 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
| 6324 | sb.buf, sb.len, 0); |
Rene Scharfe | fa842d8 | 2017-08-30 19:49:41 +0200 | [diff] [blame] | 6325 | strbuf_release(&sb); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6326 | } |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6327 | show_mode_change(opt, p, !p->score); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6328 | break; |
| 6329 | } |
| 6330 | } |
| 6331 | |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6332 | struct patch_id_t { |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6333 | git_hash_ctx *ctx; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6334 | int patchlen; |
| 6335 | }; |
| 6336 | |
| 6337 | static int remove_space(char *line, int len) |
| 6338 | { |
| 6339 | int i; |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6340 | char *dst = line; |
| 6341 | unsigned char c; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6342 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6343 | for (i = 0; i < len; i++) |
| 6344 | if (!isspace((c = line[i]))) |
| 6345 | *dst++ = c; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6346 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6347 | return dst - line; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6348 | } |
| 6349 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6350 | void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx) |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6351 | { |
| 6352 | unsigned char hash[GIT_MAX_RAWSZ]; |
| 6353 | unsigned short carry = 0; |
| 6354 | int i; |
| 6355 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6356 | the_hash_algo->final_fn(hash, ctx); |
| 6357 | the_hash_algo->init_fn(ctx); |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6358 | /* 20-byte sum, with carry */ |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6359 | for (i = 0; i < the_hash_algo->rawsz; ++i) { |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6360 | carry += result->hash[i] + hash[i]; |
| 6361 | result->hash[i] = carry; |
| 6362 | carry >>= 8; |
| 6363 | } |
| 6364 | } |
| 6365 | |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 6366 | static int patch_id_consume(void *priv, char *line, unsigned long len) |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6367 | { |
| 6368 | struct patch_id_t *data = priv; |
| 6369 | int new_len; |
| 6370 | |
René Scharfe | 82a6201 | 2020-08-19 00:08:54 +0200 | [diff] [blame] | 6371 | if (len > 12 && starts_with(line, "\\ ")) |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 6372 | return 0; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6373 | new_len = remove_space(line, len); |
| 6374 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6375 | the_hash_algo->update_fn(data->ctx, line, new_len); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6376 | data->patchlen += new_len; |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 6377 | return 0; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6378 | } |
| 6379 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6380 | static void patch_id_add_string(git_hash_ctx *ctx, const char *str) |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6381 | { |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6382 | the_hash_algo->update_fn(ctx, str, strlen(str)); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6383 | } |
| 6384 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6385 | static void patch_id_add_mode(git_hash_ctx *ctx, unsigned mode) |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6386 | { |
| 6387 | /* large enough for 2^32 in octal */ |
| 6388 | char buf[12]; |
| 6389 | int len = xsnprintf(buf, sizeof(buf), "%06o", mode); |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6390 | the_hash_algo->update_fn(ctx, buf, len); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6391 | } |
| 6392 | |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6393 | /* returns 0 upon success, and writes result into oid */ |
Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 +0000 | [diff] [blame] | 6394 | static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only) |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6395 | { |
| 6396 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6397 | int i; |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6398 | git_hash_ctx ctx; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6399 | struct patch_id_t data; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6400 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6401 | the_hash_algo->init_fn(&ctx); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6402 | memset(&data, 0, sizeof(struct patch_id_t)); |
| 6403 | data.ctx = &ctx; |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6404 | oidclr(oid); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6405 | |
| 6406 | for (i = 0; i < q->nr; i++) { |
| 6407 | xpparam_t xpp; |
| 6408 | xdemitconf_t xecfg; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6409 | mmfile_t mf1, mf2; |
| 6410 | struct diff_filepair *p = q->queue[i]; |
| 6411 | int len1, len2; |
| 6412 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 6413 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 6414 | memset(&xecfg, 0, sizeof(xecfg)); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6415 | if (p->status == 0) |
| 6416 | return error("internal diff status error"); |
| 6417 | if (p->status == DIFF_STATUS_UNKNOWN) |
| 6418 | continue; |
| 6419 | if (diff_unmodified_pair(p)) |
| 6420 | continue; |
| 6421 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 6422 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
| 6423 | continue; |
| 6424 | if (DIFF_PAIR_UNMERGED(p)) |
| 6425 | continue; |
| 6426 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 6427 | diff_fill_oid_info(p->one, options->repo->index); |
| 6428 | diff_fill_oid_info(p->two, options->repo->index); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6429 | |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6430 | len1 = remove_space(p->one->path, strlen(p->one->path)); |
| 6431 | len2 = remove_space(p->two->path, strlen(p->two->path)); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6432 | patch_id_add_string(&ctx, "diff--git"); |
| 6433 | patch_id_add_string(&ctx, "a/"); |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6434 | the_hash_algo->update_fn(&ctx, p->one->path, len1); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6435 | patch_id_add_string(&ctx, "b/"); |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6436 | the_hash_algo->update_fn(&ctx, p->two->path, len2); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6437 | |
| 6438 | if (p->one->mode == 0) { |
| 6439 | patch_id_add_string(&ctx, "newfilemode"); |
| 6440 | patch_id_add_mode(&ctx, p->two->mode); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6441 | } else if (p->two->mode == 0) { |
| 6442 | patch_id_add_string(&ctx, "deletedfilemode"); |
| 6443 | patch_id_add_mode(&ctx, p->one->mode); |
Jerry Zhang | 93105ab | 2022-10-24 20:07:42 +0000 | [diff] [blame] | 6444 | } else if (p->one->mode != p->two->mode) { |
| 6445 | patch_id_add_string(&ctx, "oldmode"); |
| 6446 | patch_id_add_mode(&ctx, p->one->mode); |
| 6447 | patch_id_add_string(&ctx, "newmode"); |
| 6448 | patch_id_add_mode(&ctx, p->two->mode); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6449 | } |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6450 | |
Jerry Zhang | 0570be7 | 2022-10-24 20:07:39 +0000 | [diff] [blame] | 6451 | if (diff_header_only) { |
| 6452 | /* don't do anything since we're only populating header info */ |
| 6453 | } else if (diff_filespec_is_binary(options->repo, p->one) || |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6454 | diff_filespec_is_binary(options->repo, p->two)) { |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6455 | the_hash_algo->update_fn(&ctx, oid_to_hex(&p->one->oid), |
| 6456 | the_hash_algo->hexsz); |
| 6457 | the_hash_algo->update_fn(&ctx, oid_to_hex(&p->two->oid), |
| 6458 | the_hash_algo->hexsz); |
Jerry Zhang | 0570be7 | 2022-10-24 20:07:39 +0000 | [diff] [blame] | 6459 | } else { |
| 6460 | if (p->one->mode == 0) { |
| 6461 | patch_id_add_string(&ctx, "---/dev/null"); |
| 6462 | patch_id_add_string(&ctx, "+++b/"); |
| 6463 | the_hash_algo->update_fn(&ctx, p->two->path, len2); |
| 6464 | } else if (p->two->mode == 0) { |
| 6465 | patch_id_add_string(&ctx, "---a/"); |
| 6466 | the_hash_algo->update_fn(&ctx, p->one->path, len1); |
| 6467 | patch_id_add_string(&ctx, "+++/dev/null"); |
| 6468 | } else { |
| 6469 | patch_id_add_string(&ctx, "---a/"); |
| 6470 | the_hash_algo->update_fn(&ctx, p->one->path, len1); |
| 6471 | patch_id_add_string(&ctx, "+++b/"); |
| 6472 | the_hash_algo->update_fn(&ctx, p->two->path, len2); |
| 6473 | } |
| 6474 | |
| 6475 | if (fill_mmfile(options->repo, &mf1, p->one) < 0 || |
| 6476 | fill_mmfile(options->repo, &mf2, p->two) < 0) |
| 6477 | return error("unable to read files to diff"); |
| 6478 | xpp.flags = 0; |
| 6479 | xecfg.ctxlen = 3; |
| 6480 | xecfg.flags = XDL_EMIT_NO_HUNK_HDR; |
| 6481 | if (xdi_diff_outf(&mf1, &mf2, NULL, |
| 6482 | patch_id_consume, &data, &xpp, &xecfg)) |
| 6483 | return error("unable to generate patch-id diff for %s", |
| 6484 | p->one->path); |
Clemens Buchacher | 34597c1 | 2010-08-15 09:20:43 +0200 | [diff] [blame] | 6485 | } |
Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 +0000 | [diff] [blame] | 6486 | flush_one_hunk(oid, &ctx); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6487 | } |
| 6488 | |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6489 | return 0; |
| 6490 | } |
| 6491 | |
Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 +0000 | [diff] [blame] | 6492 | int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only) |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6493 | { |
| 6494 | struct diff_queue_struct *q = &diff_queued_diff; |
Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 +0000 | [diff] [blame] | 6495 | int result = diff_get_patch_id(options, oid, diff_header_only); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6496 | |
SZEDER Gábor | 586d8b5 | 2022-11-02 23:01:42 +0100 | [diff] [blame] | 6497 | diff_free_queue(q); |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 6498 | DIFF_QUEUE_CLEAR(q); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6499 | |
| 6500 | return result; |
| 6501 | } |
| 6502 | |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6503 | static int is_summary_empty(const struct diff_queue_struct *q) |
| 6504 | { |
| 6505 | int i; |
| 6506 | |
| 6507 | for (i = 0; i < q->nr; i++) { |
| 6508 | const struct diff_filepair *p = q->queue[i]; |
| 6509 | |
| 6510 | switch (p->status) { |
| 6511 | case DIFF_STATUS_DELETED: |
| 6512 | case DIFF_STATUS_ADDED: |
| 6513 | case DIFF_STATUS_COPIED: |
| 6514 | case DIFF_STATUS_RENAMED: |
| 6515 | return 0; |
| 6516 | default: |
| 6517 | if (p->score) |
| 6518 | return 0; |
| 6519 | if (p->one->mode && p->two->mode && |
| 6520 | p->one->mode != p->two->mode) |
| 6521 | return 0; |
| 6522 | break; |
| 6523 | } |
| 6524 | } |
| 6525 | return 1; |
| 6526 | } |
| 6527 | |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6528 | static const char rename_limit_warning[] = |
Elijah Newren | 05d2c61 | 2021-07-15 00:45:21 +0000 | [diff] [blame] | 6529 | N_("exhaustive rename detection was skipped due to too many files."); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6530 | |
| 6531 | static const char degrade_cc_to_c_warning[] = |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6532 | N_("only found copies from modified paths due to too many files."); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6533 | |
| 6534 | static const char rename_limit_advice[] = |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6535 | N_("you may want to set your %s variable to at least " |
| 6536 | "%d and retry the command."); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6537 | |
| 6538 | void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc) |
| 6539 | { |
Nguyễn Thái Ngọc Duy | 4e056c9 | 2018-01-16 16:23:49 +0700 | [diff] [blame] | 6540 | fflush(stdout); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6541 | if (degraded_cc) |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6542 | warning(_(degrade_cc_to_c_warning)); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6543 | else if (needed) |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6544 | warning(_(rename_limit_warning)); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6545 | else |
| 6546 | return; |
Elijah Newren | 9f7e4bf | 2017-11-13 12:15:59 -0800 | [diff] [blame] | 6547 | if (0 < needed) |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6548 | warning(_(rename_limit_advice), varname, needed); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6549 | } |
| 6550 | |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6551 | static void create_filepairs_for_header_only_notifications(struct diff_options *o) |
| 6552 | { |
| 6553 | struct strset present; |
| 6554 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6555 | struct hashmap_iter iter; |
| 6556 | struct strmap_entry *e; |
| 6557 | int i; |
| 6558 | |
| 6559 | strset_init_with_options(&present, /*pool*/ NULL, /*strdup*/ 0); |
| 6560 | |
| 6561 | /* |
| 6562 | * Find out which paths exist in diff_queued_diff, preferring |
| 6563 | * one->path for any pair that has multiple paths. |
| 6564 | */ |
| 6565 | for (i = 0; i < q->nr; i++) { |
| 6566 | struct diff_filepair *p = q->queue[i]; |
| 6567 | char *path = p->one->path ? p->one->path : p->two->path; |
| 6568 | |
| 6569 | if (strmap_contains(o->additional_path_headers, path)) |
| 6570 | strset_add(&present, path); |
| 6571 | } |
| 6572 | |
| 6573 | /* |
| 6574 | * Loop over paths in additional_path_headers; for each NOT already |
| 6575 | * in diff_queued_diff, create a synthetic filepair and insert that |
| 6576 | * into diff_queued_diff. |
| 6577 | */ |
| 6578 | strmap_for_each_entry(o->additional_path_headers, &iter, e) { |
| 6579 | if (!strset_contains(&present, e->key)) { |
| 6580 | struct diff_filespec *one, *two; |
| 6581 | struct diff_filepair *p; |
| 6582 | |
| 6583 | one = alloc_filespec(e->key); |
| 6584 | two = alloc_filespec(e->key); |
| 6585 | fill_filespec(one, null_oid(), 0, 0); |
| 6586 | fill_filespec(two, null_oid(), 0, 0); |
| 6587 | p = diff_queue(q, one, two); |
| 6588 | p->status = DIFF_STATUS_MODIFIED; |
| 6589 | } |
| 6590 | } |
| 6591 | |
| 6592 | /* Re-sort the filepairs */ |
| 6593 | diffcore_fix_diff_index(); |
| 6594 | |
| 6595 | /* Cleanup */ |
| 6596 | strset_clear(&present); |
| 6597 | } |
| 6598 | |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6599 | static void diff_flush_patch_all_file_pairs(struct diff_options *o) |
| 6600 | { |
| 6601 | int i; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6602 | static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT; |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6603 | struct diff_queue_struct *q = &diff_queued_diff; |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 6604 | |
| 6605 | if (WSEH_NEW & WS_RULE_MASK) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 6606 | BUG("WS rules bit mask overlaps with diff symbol flags"); |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 6607 | |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6608 | if (o->color_moved) |
| 6609 | o->emitted_symbols = &esm; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6610 | |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6611 | if (o->additional_path_headers) |
| 6612 | create_filepairs_for_header_only_notifications(o); |
| 6613 | |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6614 | for (i = 0; i < q->nr; i++) { |
| 6615 | struct diff_filepair *p = q->queue[i]; |
| 6616 | if (check_pair_status(p)) |
| 6617 | diff_flush_patch(p, o); |
| 6618 | } |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6619 | |
| 6620 | if (o->emitted_symbols) { |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6621 | if (o->color_moved) { |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 6622 | struct mem_pool entry_pool; |
| 6623 | struct moved_entry_list *entry_list; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6624 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 6625 | mem_pool_init(&entry_pool, 1024 * 1024); |
| 6626 | entry_list = add_lines_to_move_detection(o, |
| 6627 | &entry_pool); |
| 6628 | mark_color_as_moved(o, entry_list); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 6629 | if (o->color_moved == COLOR_MOVED_ZEBRA_DIM) |
| 6630 | dim_moved_lines(o); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6631 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 6632 | mem_pool_discard(&entry_pool, 0); |
| 6633 | free(entry_list); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6634 | } |
| 6635 | |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6636 | for (i = 0; i < esm.nr; i++) |
| 6637 | emit_diff_symbol_from_struct(o, &esm.buf[i]); |
| 6638 | |
| 6639 | for (i = 0; i < esm.nr; i++) |
| 6640 | free((void *)esm.buf[i].line); |
Jeff King | 48edf3a | 2019-01-24 07:32:41 -0500 | [diff] [blame] | 6641 | esm.nr = 0; |
| 6642 | |
| 6643 | o->emitted_symbols = NULL; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6644 | } |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6645 | } |
| 6646 | |
Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 11:45:34 +0100 | [diff] [blame] | 6647 | static void diff_free_file(struct diff_options *options) |
| 6648 | { |
| 6649 | if (options->close_file) |
| 6650 | fclose(options->file); |
| 6651 | } |
| 6652 | |
Ævar Arnfjörð Bjarmason | c45dc9c | 2021-02-11 11:45:35 +0100 | [diff] [blame] | 6653 | static void diff_free_ignore_regex(struct diff_options *options) |
| 6654 | { |
| 6655 | int i; |
| 6656 | |
| 6657 | for (i = 0; i < options->ignore_regex_nr; i++) { |
| 6658 | regfree(options->ignore_regex[i]); |
| 6659 | free(options->ignore_regex[i]); |
| 6660 | } |
| 6661 | free(options->ignore_regex); |
| 6662 | } |
| 6663 | |
Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 11:45:34 +0100 | [diff] [blame] | 6664 | void diff_free(struct diff_options *options) |
| 6665 | { |
| 6666 | if (options->no_free) |
| 6667 | return; |
| 6668 | |
| 6669 | diff_free_file(options); |
Ævar Arnfjörð Bjarmason | c45dc9c | 2021-02-11 11:45:35 +0100 | [diff] [blame] | 6670 | diff_free_ignore_regex(options); |
Ævar Arnfjörð Bjarmason | 244c272 | 2022-02-16 11:56:28 +0100 | [diff] [blame] | 6671 | clear_pathspec(&options->pathspec); |
Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 11:45:34 +0100 | [diff] [blame] | 6672 | } |
| 6673 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6674 | void diff_flush(struct diff_options *options) |
| 6675 | { |
| 6676 | struct diff_queue_struct *q = &diff_queued_diff; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6677 | int i, output_format = options->output_format; |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6678 | int separator = 0; |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 6679 | int dirstat_by_line = 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6680 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6681 | /* |
| 6682 | * Order: raw, stat, summary, patch |
| 6683 | * or: name/name-status/checkdiff (other bits clear) |
| 6684 | */ |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6685 | if (!q->nr && !options->additional_path_headers) |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6686 | goto free_queue; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6687 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6688 | if (output_format & (DIFF_FORMAT_RAW | |
| 6689 | DIFF_FORMAT_NAME | |
| 6690 | DIFF_FORMAT_NAME_STATUS | |
| 6691 | DIFF_FORMAT_CHECKDIFF)) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6692 | for (i = 0; i < q->nr; i++) { |
| 6693 | struct diff_filepair *p = q->queue[i]; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6694 | if (check_pair_status(p)) |
| 6695 | flush_one_pair(p, options); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6696 | } |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6697 | separator++; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6698 | } |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6699 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6700 | if (output_format & DIFF_FORMAT_DIRSTAT && options->flags.dirstat_by_line) |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 6701 | dirstat_by_line = 1; |
| 6702 | |
| 6703 | if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT) || |
| 6704 | dirstat_by_line) { |
Timo Hirvonen | 5e2b063 | 2006-06-25 14:28:19 +0300 | [diff] [blame] | 6705 | struct diffstat_t diffstat; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6706 | |
Daniel Ferreira | e4cb659 | 2019-11-13 12:40:58 +0000 | [diff] [blame] | 6707 | compute_diffstat(options, &diffstat, q); |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 6708 | if (output_format & DIFF_FORMAT_NUMSTAT) |
| 6709 | show_numstat(&diffstat, options); |
| 6710 | if (output_format & DIFF_FORMAT_DIFFSTAT) |
| 6711 | show_stats(&diffstat, options); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 6712 | if (output_format & DIFF_FORMAT_SHORTSTAT) |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6713 | show_shortstats(&diffstat, options); |
Mårten Kongstad | ab27389 | 2015-03-02 16:05:39 +0100 | [diff] [blame] | 6714 | if (output_format & DIFF_FORMAT_DIRSTAT && dirstat_by_line) |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 6715 | show_dirstat_by_line(&diffstat, options); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 6716 | free_diffstat_info(&diffstat); |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 6717 | separator++; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6718 | } |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 6719 | if ((output_format & DIFF_FORMAT_DIRSTAT) && !dirstat_by_line) |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 6720 | show_dirstat(options); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6721 | |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6722 | if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) { |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 6723 | for (i = 0; i < q->nr; i++) { |
| 6724 | diff_summary(options, q->queue[i]); |
| 6725 | } |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 6726 | separator++; |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6727 | } |
| 6728 | |
Junio C Hamano | 5f107ca | 2023-08-18 16:59:28 -0700 | [diff] [blame] | 6729 | if (output_format & DIFF_FORMAT_PATCH) { |
| 6730 | if (separator) { |
| 6731 | emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0); |
| 6732 | if (options->stat_sep) |
| 6733 | /* attach patch instead of inline */ |
| 6734 | emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP, |
| 6735 | NULL, 0, 0); |
| 6736 | } |
| 6737 | |
| 6738 | diff_flush_patch_all_file_pairs(options); |
| 6739 | } |
| 6740 | |
| 6741 | if (output_format & DIFF_FORMAT_CALLBACK) |
| 6742 | options->format_callback(q, options, options->format_callback_data); |
| 6743 | |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6744 | if (output_format & DIFF_FORMAT_NO_OUTPUT && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6745 | options->flags.exit_with_status && |
| 6746 | options->flags.diff_from_contents) { |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6747 | /* |
| 6748 | * run diff_flush_patch for the exit status. setting |
Ondřej Bílka | 749f763 | 2013-07-22 23:02:23 +0200 | [diff] [blame] | 6749 | * options->file to /dev/null should be safe, because we |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6750 | * aren't supposed to produce any output anyway. |
| 6751 | */ |
Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 11:45:34 +0100 | [diff] [blame] | 6752 | diff_free_file(options); |
Nguyễn Thái Ngọc Duy | 23a9e07 | 2017-05-03 17:16:46 +0700 | [diff] [blame] | 6753 | options->file = xfopen("/dev/null", "w"); |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6754 | options->close_file = 1; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6755 | options->color_moved = 0; |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6756 | for (i = 0; i < q->nr; i++) { |
| 6757 | struct diff_filepair *p = q->queue[i]; |
| 6758 | if (check_pair_status(p)) |
| 6759 | diff_flush_patch(p, options); |
| 6760 | if (options->found_changes) |
| 6761 | break; |
| 6762 | } |
| 6763 | } |
| 6764 | |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6765 | free_queue: |
SZEDER Gábor | 586d8b5 | 2022-11-02 23:01:42 +0100 | [diff] [blame] | 6766 | diff_free_queue(q); |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 6767 | DIFF_QUEUE_CLEAR(q); |
Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 11:45:34 +0100 | [diff] [blame] | 6768 | diff_free(options); |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6769 | |
| 6770 | /* |
Jim Meyering | 97bf2a0 | 2009-08-30 22:27:02 +0200 | [diff] [blame] | 6771 | * Report the content-level differences with HAS_CHANGES; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6772 | * diff_addremove/diff_change does not set the bit when |
| 6773 | * DIFF_FROM_CONTENTS is in effect (e.g. with -w). |
| 6774 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6775 | if (options->flags.diff_from_contents) { |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6776 | if (options->found_changes) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6777 | options->flags.has_changes = 1; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6778 | else |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6779 | options->flags.has_changes = 0; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6780 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6781 | } |
| 6782 | |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6783 | static int match_filter(const struct diff_options *options, const struct diff_filepair *p) |
| 6784 | { |
| 6785 | return (((p->status == DIFF_STATUS_MODIFIED) && |
| 6786 | ((p->score && |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6787 | filter_bit_tst(DIFF_STATUS_FILTER_BROKEN, options)) || |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6788 | (!p->score && |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6789 | filter_bit_tst(DIFF_STATUS_MODIFIED, options)))) || |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6790 | ((p->status != DIFF_STATUS_MODIFIED) && |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6791 | filter_bit_tst(p->status, options))); |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6792 | } |
| 6793 | |
Junio C Hamano | 949226f | 2013-07-17 14:19:24 -0700 | [diff] [blame] | 6794 | static void diffcore_apply_filter(struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6795 | { |
| 6796 | int i; |
| 6797 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6798 | struct diff_queue_struct outq; |
Junio C Hamano | 949226f | 2013-07-17 14:19:24 -0700 | [diff] [blame] | 6799 | |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 6800 | DIFF_QUEUE_CLEAR(&outq); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6801 | |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6802 | if (!options->filter) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6803 | return; |
| 6804 | |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6805 | if (filter_bit_tst(DIFF_STATUS_FILTER_AON, options)) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6806 | int found; |
| 6807 | for (i = found = 0; !found && i < q->nr; i++) { |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6808 | if (match_filter(options, q->queue[i])) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6809 | found++; |
| 6810 | } |
| 6811 | if (found) |
| 6812 | return; |
| 6813 | |
| 6814 | /* otherwise we will clear the whole queue |
| 6815 | * by copying the empty outq at the end of this |
| 6816 | * function, but first clear the current entries |
| 6817 | * in the queue. |
| 6818 | */ |
| 6819 | for (i = 0; i < q->nr; i++) |
| 6820 | diff_free_filepair(q->queue[i]); |
| 6821 | } |
| 6822 | else { |
| 6823 | /* Only the matching ones */ |
| 6824 | for (i = 0; i < q->nr; i++) { |
| 6825 | struct diff_filepair *p = q->queue[i]; |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6826 | if (match_filter(options, p)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6827 | diff_q(&outq, p); |
| 6828 | else |
| 6829 | diff_free_filepair(p); |
| 6830 | } |
| 6831 | } |
| 6832 | free(q->queue); |
| 6833 | *q = outq; |
| 6834 | } |
| 6835 | |
Sven Verdoolaege | 5701115 | 2007-09-08 12:30:22 +0200 | [diff] [blame] | 6836 | /* Check whether two filespecs with the same mode and size are identical */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6837 | static int diff_filespec_is_identical(struct repository *r, |
| 6838 | struct diff_filespec *one, |
Sven Verdoolaege | 5701115 | 2007-09-08 12:30:22 +0200 | [diff] [blame] | 6839 | struct diff_filespec *two) |
| 6840 | { |
Junio C Hamano | 2b459b4 | 2008-03-02 00:07:59 -0800 | [diff] [blame] | 6841 | if (S_ISGITLINK(one->mode)) |
| 6842 | return 0; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6843 | if (diff_populate_filespec(r, one, NULL)) |
Sven Verdoolaege | 5701115 | 2007-09-08 12:30:22 +0200 | [diff] [blame] | 6844 | return 0; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6845 | if (diff_populate_filespec(r, two, NULL)) |
Sven Verdoolaege | 5701115 | 2007-09-08 12:30:22 +0200 | [diff] [blame] | 6846 | return 0; |
| 6847 | return !memcmp(one->data, two->data, one->size); |
| 6848 | } |
| 6849 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6850 | static int diff_filespec_check_stat_unmatch(struct repository *r, |
| 6851 | struct diff_filepair *p) |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6852 | { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6853 | struct diff_populate_filespec_options dpf_options = { |
| 6854 | .check_size_only = 1, |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6855 | .missing_object_cb = diff_queued_diff_prefetch, |
| 6856 | .missing_object_data = r, |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6857 | }; |
| 6858 | |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 6859 | if (p->done_skip_stat_unmatch) |
| 6860 | return p->skip_stat_unmatch_result; |
| 6861 | |
| 6862 | p->done_skip_stat_unmatch = 1; |
| 6863 | p->skip_stat_unmatch_result = 0; |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6864 | /* |
| 6865 | * 1. Entries that come from stat info dirtiness |
| 6866 | * always have both sides (iow, not create/delete), |
| 6867 | * one side of the object name is unknown, with |
| 6868 | * the same mode and size. Keep the ones that |
| 6869 | * do not match these criteria. They have real |
| 6870 | * differences. |
| 6871 | * |
| 6872 | * 2. At this point, the file is known to be modified, |
| 6873 | * with the same mode and size, and the object |
| 6874 | * name of one side is unknown. Need to inspect |
| 6875 | * the identical contents. |
| 6876 | */ |
| 6877 | if (!DIFF_FILE_VALID(p->one) || /* (1) */ |
| 6878 | !DIFF_FILE_VALID(p->two) || |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 6879 | (p->one->oid_valid && p->two->oid_valid) || |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6880 | (p->one->mode != p->two->mode) || |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6881 | diff_populate_filespec(r, p->one, &dpf_options) || |
| 6882 | diff_populate_filespec(r, p->two, &dpf_options) || |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6883 | (p->one->size != p->two->size) || |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6884 | !diff_filespec_is_identical(r, p->one, p->two)) /* (2) */ |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 6885 | p->skip_stat_unmatch_result = 1; |
| 6886 | return p->skip_stat_unmatch_result; |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6887 | } |
| 6888 | |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6889 | static void diffcore_skip_stat_unmatch(struct diff_options *diffopt) |
| 6890 | { |
| 6891 | int i; |
| 6892 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6893 | struct diff_queue_struct outq; |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 6894 | DIFF_QUEUE_CLEAR(&outq); |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6895 | |
| 6896 | for (i = 0; i < q->nr; i++) { |
| 6897 | struct diff_filepair *p = q->queue[i]; |
| 6898 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6899 | if (diff_filespec_check_stat_unmatch(diffopt->repo, p)) |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6900 | diff_q(&outq, p); |
| 6901 | else { |
| 6902 | /* |
| 6903 | * The caller can subtract 1 from skip_stat_unmatch |
| 6904 | * to determine how many paths were dirty only |
| 6905 | * due to stat info mismatch. |
| 6906 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6907 | if (!diffopt->flags.no_index) |
René Scharfe | 6d2d9e8 | 2007-08-15 00:41:00 +0200 | [diff] [blame] | 6908 | diffopt->skip_stat_unmatch++; |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6909 | diff_free_filepair(p); |
| 6910 | } |
| 6911 | } |
| 6912 | free(q->queue); |
| 6913 | *q = outq; |
| 6914 | } |
| 6915 | |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 6916 | static int diffnamecmp(const void *a_, const void *b_) |
| 6917 | { |
| 6918 | const struct diff_filepair *a = *((const struct diff_filepair **)a_); |
| 6919 | const struct diff_filepair *b = *((const struct diff_filepair **)b_); |
| 6920 | const char *name_a, *name_b; |
| 6921 | |
| 6922 | name_a = a->one ? a->one->path : a->two->path; |
| 6923 | name_b = b->one ? b->one->path : b->two->path; |
| 6924 | return strcmp(name_a, name_b); |
| 6925 | } |
| 6926 | |
Jeff King | 784c0da | 2019-02-14 00:48:03 -0500 | [diff] [blame] | 6927 | void diffcore_fix_diff_index(void) |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 6928 | { |
| 6929 | struct diff_queue_struct *q = &diff_queued_diff; |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 6930 | QSORT(q->queue, q->nr, diffnamecmp); |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 6931 | } |
| 6932 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6933 | void diff_add_if_missing(struct repository *r, |
| 6934 | struct oid_array *to_fetch, |
| 6935 | const struct diff_filespec *filespec) |
Jonathan Tan | 7fbbcb2 | 2019-04-05 10:09:34 -0700 | [diff] [blame] | 6936 | { |
| 6937 | if (filespec && filespec->oid_valid && |
Jonathan Tan | a63694f | 2019-08-20 13:53:20 -0700 | [diff] [blame] | 6938 | !S_ISGITLINK(filespec->mode) && |
Jonathan Tan | 7fbbcb2 | 2019-04-05 10:09:34 -0700 | [diff] [blame] | 6939 | oid_object_info_extended(r, &filespec->oid, NULL, |
| 6940 | OBJECT_INFO_FOR_PREFETCH)) |
| 6941 | oid_array_append(to_fetch, &filespec->oid); |
| 6942 | } |
| 6943 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6944 | void diff_queued_diff_prefetch(void *repository) |
| 6945 | { |
| 6946 | struct repository *repo = repository; |
| 6947 | int i; |
| 6948 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6949 | struct oid_array to_fetch = OID_ARRAY_INIT; |
| 6950 | |
| 6951 | for (i = 0; i < q->nr; i++) { |
| 6952 | struct diff_filepair *p = q->queue[i]; |
| 6953 | diff_add_if_missing(repo, &to_fetch, p->one); |
| 6954 | diff_add_if_missing(repo, &to_fetch, p->two); |
| 6955 | } |
| 6956 | |
| 6957 | /* |
| 6958 | * NEEDSWORK: Consider deduplicating the OIDs sent. |
| 6959 | */ |
| 6960 | promisor_remote_get_direct(repo, to_fetch.oid, to_fetch.nr); |
| 6961 | |
| 6962 | oid_array_clear(&to_fetch); |
| 6963 | } |
| 6964 | |
Dragan Simic | 4ca7a3f | 2023-09-23 06:01:14 +0200 | [diff] [blame] | 6965 | void init_diffstat_widths(struct diff_options *options) |
| 6966 | { |
| 6967 | options->stat_width = -1; /* use full terminal width */ |
| 6968 | options->stat_name_width = -1; /* respect diff.statNameWidth config */ |
| 6969 | options->stat_graph_width = -1; /* respect diff.statGraphWidth config */ |
| 6970 | } |
| 6971 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6972 | void diffcore_std(struct diff_options *options) |
| 6973 | { |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6974 | int output_formats_to_prefetch = DIFF_FORMAT_DIFFSTAT | |
| 6975 | DIFF_FORMAT_NUMSTAT | |
| 6976 | DIFF_FORMAT_PATCH | |
| 6977 | DIFF_FORMAT_SHORTSTAT | |
| 6978 | DIFF_FORMAT_DIRSTAT; |
Jonathan Tan | 7fbbcb2 | 2019-04-05 10:09:34 -0700 | [diff] [blame] | 6979 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6980 | /* |
| 6981 | * Check if the user requested a blob-data-requiring diff output and/or |
| 6982 | * break-rewrite detection (which requires blob data). If yes, prefetch |
| 6983 | * the diff pairs. |
| 6984 | * |
| 6985 | * If no prefetching occurs, diffcore_rename() will prefetch if it |
| 6986 | * decides that it needs inexact rename detection. |
| 6987 | */ |
Ævar Arnfjörð Bjarmason | a5183d7 | 2023-03-28 15:58:53 +0200 | [diff] [blame] | 6988 | if (options->repo == the_repository && repo_has_promisor_remote(the_repository) && |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6989 | (options->output_format & output_formats_to_prefetch || |
| 6990 | options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)) |
| 6991 | diff_queued_diff_prefetch(options->repo); |
Jonathan Tan | 7fbbcb2 | 2019-04-05 10:09:34 -0700 | [diff] [blame] | 6992 | |
Kirill Smelkov | 7195fbf | 2014-02-24 20:21:51 +0400 | [diff] [blame] | 6993 | /* NOTE please keep the following in sync with diff_tree_combined() */ |
Junio C Hamano | 9d86535 | 2008-09-06 19:09:16 -0700 | [diff] [blame] | 6994 | if (options->skip_stat_unmatch) |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6995 | diffcore_skip_stat_unmatch(options); |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 6996 | if (!options->found_follow) { |
| 6997 | /* See try_to_follow_renames() in tree-diff.c */ |
| 6998 | if (options->break_opt != -1) |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6999 | diffcore_break(options->repo, |
| 7000 | options->break_opt); |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 7001 | if (options->detect_rename) |
| 7002 | diffcore_rename(options); |
| 7003 | if (options->break_opt != -1) |
| 7004 | diffcore_merge_broken(); |
| 7005 | } |
Stefan Beller | cf63051 | 2018-01-04 14:50:41 -0800 | [diff] [blame] | 7006 | if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) |
Junio C Hamano | 382f013 | 2010-08-31 13:44:39 -0700 | [diff] [blame] | 7007 | diffcore_pickaxe(options); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7008 | if (options->orderfile) |
| 7009 | diffcore_order(options->orderfile); |
Junio C Hamano | 1eb4136 | 2021-02-11 11:57:50 -0800 | [diff] [blame] | 7010 | if (options->rotate_to) |
| 7011 | diffcore_rotate(options); |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 7012 | if (!options->found_follow) |
| 7013 | /* See try_to_follow_renames() in tree-diff.c */ |
| 7014 | diff_resolve_rename_copy(); |
Junio C Hamano | 949226f | 2013-07-17 14:19:24 -0700 | [diff] [blame] | 7015 | diffcore_apply_filter(options); |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 7016 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7017 | if (diff_queued_diff.nr && !options->flags.diff_from_contents) |
| 7018 | options->flags.has_changes = 1; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 7019 | else |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7020 | options->flags.has_changes = 0; |
Bo Yang | 1da6175 | 2010-05-06 21:52:28 -0700 | [diff] [blame] | 7021 | |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 7022 | options->found_follow = 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7023 | } |
| 7024 | |
Jeff King | 5cc6b2d | 2023-08-21 16:20:46 -0400 | [diff] [blame] | 7025 | int diff_result_code(struct diff_options *opt) |
Junio C Hamano | da31b35 | 2007-12-13 23:40:27 -0800 | [diff] [blame] | 7026 | { |
| 7027 | int result = 0; |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 7028 | |
Max Nanasy | c9fc441 | 2013-03-21 12:53:38 -0700 | [diff] [blame] | 7029 | diff_warn_rename_limit("diff.renameLimit", |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 7030 | opt->needed_rename_limit, |
| 7031 | opt->degraded_cc_to_c); |
Jeff King | 5cc6b2d | 2023-08-21 16:20:46 -0400 | [diff] [blame] | 7032 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7033 | if (opt->flags.exit_with_status && |
| 7034 | opt->flags.has_changes) |
Junio C Hamano | da31b35 | 2007-12-13 23:40:27 -0800 | [diff] [blame] | 7035 | result |= 01; |
| 7036 | if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7037 | opt->flags.check_failed) |
Junio C Hamano | da31b35 | 2007-12-13 23:40:27 -0800 | [diff] [blame] | 7038 | result |= 02; |
| 7039 | return result; |
| 7040 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7041 | |
Junio C Hamano | 28b9264 | 2011-05-31 09:14:17 -0700 | [diff] [blame] | 7042 | int diff_can_quit_early(struct diff_options *opt) |
| 7043 | { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7044 | return (opt->flags.quick && |
Junio C Hamano | 28b9264 | 2011-05-31 09:14:17 -0700 | [diff] [blame] | 7045 | !opt->filter && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7046 | opt->flags.has_changes); |
Junio C Hamano | 28b9264 | 2011-05-31 09:14:17 -0700 | [diff] [blame] | 7047 | } |
| 7048 | |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 7049 | /* |
| 7050 | * Shall changes to this submodule be ignored? |
| 7051 | * |
| 7052 | * Submodule changes can be configured to be ignored separately for each path, |
| 7053 | * but that configuration can be overridden from the command line. |
| 7054 | */ |
| 7055 | static int is_submodule_ignored(const char *path, struct diff_options *options) |
| 7056 | { |
| 7057 | int ignored = 0; |
Brandon Williams | 02f2f56 | 2017-10-31 11:19:05 -0700 | [diff] [blame] | 7058 | struct diff_flags orig_flags = options->flags; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7059 | if (!options->flags.override_submodule_config) |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 7060 | set_diffopt_flags_from_submodule_config(options, path); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7061 | if (options->flags.ignore_submodules) |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 7062 | ignored = 1; |
| 7063 | options->flags = orig_flags; |
| 7064 | return ignored; |
| 7065 | } |
| 7066 | |
Daniel Ferreira | e4cb659 | 2019-11-13 12:40:58 +0000 | [diff] [blame] | 7067 | void compute_diffstat(struct diff_options *options, |
| 7068 | struct diffstat_t *diffstat, |
| 7069 | struct diff_queue_struct *q) |
| 7070 | { |
| 7071 | int i; |
| 7072 | |
| 7073 | memset(diffstat, 0, sizeof(struct diffstat_t)); |
| 7074 | for (i = 0; i < q->nr; i++) { |
| 7075 | struct diff_filepair *p = q->queue[i]; |
| 7076 | if (check_pair_status(p)) |
| 7077 | diff_flush_stat(p, options, diffstat); |
| 7078 | } |
Junio C Hamano | e8efd86 | 2023-08-18 16:59:30 -0700 | [diff] [blame] | 7079 | options->found_changes = !!diffstat->nr; |
Daniel Ferreira | e4cb659 | 2019-11-13 12:40:58 +0000 | [diff] [blame] | 7080 | } |
| 7081 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7082 | void diff_addremove(struct diff_options *options, |
| 7083 | int addremove, unsigned mode, |
Brandon Williams | c26022e | 2017-05-30 10:30:47 -0700 | [diff] [blame] | 7084 | const struct object_id *oid, |
| 7085 | int oid_valid, |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 7086 | const char *concatpath, unsigned dirty_submodule) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7087 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7088 | struct diff_filespec *one, *two; |
| 7089 | |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 7090 | if (S_ISGITLINK(mode) && is_submodule_ignored(concatpath, options)) |
Johannes Schindelin | 50fd9bd | 2008-05-14 18:03:31 +0100 | [diff] [blame] | 7091 | return; |
| 7092 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7093 | /* This may look odd, but it is a preparation for |
| 7094 | * feeding "there are unchanged files which should |
| 7095 | * not produce diffs, but when you are doing copy |
| 7096 | * detection you would need them, so here they are" |
| 7097 | * entries to the diff-core. They will be prefixed |
| 7098 | * with something like '=' or '*' (I haven't decided |
| 7099 | * which but should not make any difference). |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 7100 | * Feeding the same new and old to diff_change() |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7101 | * also has the same effect. |
| 7102 | * Before the final output happens, they are pruned after |
| 7103 | * merged into rename/copy pairs as appropriate. |
| 7104 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7105 | if (options->flags.reverse_diff) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7106 | addremove = (addremove == '+' ? '-' : |
| 7107 | addremove == '-' ? '+' : addremove); |
| 7108 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 7109 | if (options->prefix && |
| 7110 | strncmp(concatpath, options->prefix, options->prefix_length)) |
| 7111 | return; |
| 7112 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7113 | one = alloc_filespec(concatpath); |
| 7114 | two = alloc_filespec(concatpath); |
| 7115 | |
| 7116 | if (addremove != '+') |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 7117 | fill_filespec(one, oid, oid_valid, mode); |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 7118 | if (addremove != '-') { |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 7119 | fill_filespec(two, oid, oid_valid, mode); |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 7120 | two->dirty_submodule = dirty_submodule; |
| 7121 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7122 | |
| 7123 | diff_queue(&diff_queued_diff, one, two); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7124 | if (!options->flags.diff_from_contents) |
| 7125 | options->flags.has_changes = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7126 | } |
| 7127 | |
| 7128 | void diff_change(struct diff_options *options, |
| 7129 | unsigned old_mode, unsigned new_mode, |
Brandon Williams | 94a0097 | 2017-05-30 10:30:49 -0700 | [diff] [blame] | 7130 | const struct object_id *old_oid, |
| 7131 | const struct object_id *new_oid, |
| 7132 | int old_oid_valid, int new_oid_valid, |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 7133 | const char *concatpath, |
| 7134 | unsigned old_dirty_submodule, unsigned new_dirty_submodule) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7135 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7136 | struct diff_filespec *one, *two; |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 7137 | struct diff_filepair *p; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7138 | |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 7139 | if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) && |
| 7140 | is_submodule_ignored(concatpath, options)) |
Johannes Schindelin | 50fd9bd | 2008-05-14 18:03:31 +0100 | [diff] [blame] | 7141 | return; |
| 7142 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7143 | if (options->flags.reverse_diff) { |
René Scharfe | 35d803b | 2017-01-28 22:40:58 +0100 | [diff] [blame] | 7144 | SWAP(old_mode, new_mode); |
Brandon Williams | 94a0097 | 2017-05-30 10:30:49 -0700 | [diff] [blame] | 7145 | SWAP(old_oid, new_oid); |
| 7146 | SWAP(old_oid_valid, new_oid_valid); |
René Scharfe | 35d803b | 2017-01-28 22:40:58 +0100 | [diff] [blame] | 7147 | SWAP(old_dirty_submodule, new_dirty_submodule); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7148 | } |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 7149 | |
| 7150 | if (options->prefix && |
| 7151 | strncmp(concatpath, options->prefix, options->prefix_length)) |
| 7152 | return; |
| 7153 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7154 | one = alloc_filespec(concatpath); |
| 7155 | two = alloc_filespec(concatpath); |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 7156 | fill_filespec(one, old_oid, old_oid_valid, old_mode); |
| 7157 | fill_filespec(two, new_oid, new_oid_valid, new_mode); |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 7158 | one->dirty_submodule = old_dirty_submodule; |
| 7159 | two->dirty_submodule = new_dirty_submodule; |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 7160 | p = diff_queue(&diff_queued_diff, one, two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7161 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7162 | if (options->flags.diff_from_contents) |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 7163 | return; |
| 7164 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7165 | if (options->flags.quick && options->skip_stat_unmatch && |
Jeff King | d2d7fbe | 2020-06-01 16:22:18 -0400 | [diff] [blame] | 7166 | !diff_filespec_check_stat_unmatch(options->repo, p)) { |
| 7167 | diff_free_filespec_data(p->one); |
| 7168 | diff_free_filespec_data(p->two); |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 7169 | return; |
Jeff King | d2d7fbe | 2020-06-01 16:22:18 -0400 | [diff] [blame] | 7170 | } |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 7171 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7172 | options->flags.has_changes = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7173 | } |
| 7174 | |
Junio C Hamano | fa7b290 | 2011-04-22 16:05:58 -0700 | [diff] [blame] | 7175 | struct diff_filepair *diff_unmerge(struct diff_options *options, const char *path) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7176 | { |
Junio C Hamano | 76399c0 | 2011-04-22 15:55:55 -0700 | [diff] [blame] | 7177 | struct diff_filepair *pair; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7178 | struct diff_filespec *one, *two; |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 7179 | |
| 7180 | if (options->prefix && |
| 7181 | strncmp(path, options->prefix, options->prefix_length)) |
Junio C Hamano | 76399c0 | 2011-04-22 15:55:55 -0700 | [diff] [blame] | 7182 | return NULL; |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 7183 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7184 | one = alloc_filespec(path); |
| 7185 | two = alloc_filespec(path); |
Junio C Hamano | 76399c0 | 2011-04-22 15:55:55 -0700 | [diff] [blame] | 7186 | pair = diff_queue(&diff_queued_diff, one, two); |
| 7187 | pair->is_unmerged = 1; |
| 7188 | return pair; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7189 | } |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7190 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 7191 | static char *run_textconv(struct repository *r, |
| 7192 | const char *pgm, |
| 7193 | struct diff_filespec *spec, |
| 7194 | size_t *outsize) |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7195 | { |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 7196 | struct diff_tempfile *temp; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 7197 | struct child_process child = CHILD_PROCESS_INIT; |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7198 | struct strbuf buf = STRBUF_INIT; |
Johannes Sixt | da1fbed | 2010-03-30 19:36:03 +0200 | [diff] [blame] | 7199 | int err = 0; |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7200 | |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 7201 | temp = prepare_temp_file(r, spec); |
Ævar Arnfjörð Bjarmason | 7f14609 | 2021-11-25 23:52:21 +0100 | [diff] [blame] | 7202 | strvec_push(&child.args, pgm); |
| 7203 | strvec_push(&child.args, temp->name); |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7204 | |
Jeff King | 41a457e | 2009-12-30 06:01:09 -0500 | [diff] [blame] | 7205 | child.use_shell = 1; |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7206 | child.out = -1; |
Johannes Sixt | da1fbed | 2010-03-30 19:36:03 +0200 | [diff] [blame] | 7207 | if (start_command(&child)) { |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 7208 | remove_tempfile(); |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7209 | return NULL; |
| 7210 | } |
Johannes Sixt | da1fbed | 2010-03-30 19:36:03 +0200 | [diff] [blame] | 7211 | |
| 7212 | if (strbuf_read(&buf, child.out, 0) < 0) |
| 7213 | err = error("error reading from textconv command '%s'", pgm); |
Jeff King | 70d7099 | 2009-12-30 04:02:53 -0500 | [diff] [blame] | 7214 | close(child.out); |
Johannes Sixt | da1fbed | 2010-03-30 19:36:03 +0200 | [diff] [blame] | 7215 | |
| 7216 | if (finish_command(&child) || err) { |
| 7217 | strbuf_release(&buf); |
| 7218 | remove_tempfile(); |
| 7219 | return NULL; |
| 7220 | } |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 7221 | remove_tempfile(); |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7222 | |
| 7223 | return strbuf_detach(&buf, outsize); |
| 7224 | } |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7225 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 7226 | size_t fill_textconv(struct repository *r, |
| 7227 | struct userdiff_driver *driver, |
Axel Bonnet | a788d7d | 2010-06-07 17:23:36 +0200 | [diff] [blame] | 7228 | struct diff_filespec *df, |
| 7229 | char **outbuf) |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7230 | { |
| 7231 | size_t size; |
| 7232 | |
Jeff King | a64e6a4 | 2016-02-22 13:28:54 -0500 | [diff] [blame] | 7233 | if (!driver) { |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7234 | if (!DIFF_FILE_VALID(df)) { |
| 7235 | *outbuf = ""; |
| 7236 | return 0; |
| 7237 | } |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 7238 | if (diff_populate_filespec(r, df, NULL)) |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7239 | die("unable to read files to diff"); |
| 7240 | *outbuf = df->data; |
| 7241 | return df->size; |
| 7242 | } |
| 7243 | |
Jeff King | a64e6a4 | 2016-02-22 13:28:54 -0500 | [diff] [blame] | 7244 | if (!driver->textconv) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 7245 | BUG("fill_textconv called with non-textconv driver"); |
Jeff King | a64e6a4 | 2016-02-22 13:28:54 -0500 | [diff] [blame] | 7246 | |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 7247 | if (driver->textconv_cache && df->oid_valid) { |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 7248 | *outbuf = notes_cache_get(driver->textconv_cache, |
brian m. carlson | 569aa37 | 2017-05-06 22:09:58 +0000 | [diff] [blame] | 7249 | &df->oid, |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 7250 | &size); |
| 7251 | if (*outbuf) |
| 7252 | return size; |
| 7253 | } |
| 7254 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 7255 | *outbuf = run_textconv(r, driver->textconv, df, &size); |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7256 | if (!*outbuf) |
| 7257 | die("unable to read files to diff"); |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 7258 | |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 7259 | if (driver->textconv_cache && df->oid_valid) { |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 7260 | /* ignore errors, as we might be in a readonly repository */ |
brian m. carlson | 569aa37 | 2017-05-06 22:09:58 +0000 | [diff] [blame] | 7261 | notes_cache_put(driver->textconv_cache, &df->oid, *outbuf, |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 7262 | size); |
| 7263 | /* |
| 7264 | * we could save up changes and flush them all at the end, |
| 7265 | * but we would need an extra call after all diffing is done. |
| 7266 | * Since generating a cache entry is the slow path anyway, |
| 7267 | * this extra overhead probably isn't a big deal. |
| 7268 | */ |
| 7269 | notes_cache_write(driver->textconv_cache); |
| 7270 | } |
| 7271 | |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7272 | return size; |
| 7273 | } |
Nguyễn Thái Ngọc Duy | 4914c96 | 2012-10-26 22:53:52 +0700 | [diff] [blame] | 7274 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 7275 | int textconv_object(struct repository *r, |
| 7276 | const char *path, |
Jeff Smith | 3a35cb2 | 2017-05-24 00:15:10 -0500 | [diff] [blame] | 7277 | unsigned mode, |
| 7278 | const struct object_id *oid, |
| 7279 | int oid_valid, |
| 7280 | char **buf, |
| 7281 | unsigned long *buf_size) |
| 7282 | { |
| 7283 | struct diff_filespec *df; |
| 7284 | struct userdiff_driver *textconv; |
| 7285 | |
| 7286 | df = alloc_filespec(path); |
Junio C Hamano | a6f38c1 | 2017-06-19 12:38:44 -0700 | [diff] [blame] | 7287 | fill_filespec(df, oid, oid_valid, mode); |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 7288 | textconv = get_textconv(r, df); |
Jeff Smith | 3a35cb2 | 2017-05-24 00:15:10 -0500 | [diff] [blame] | 7289 | if (!textconv) { |
| 7290 | free_filespec(df); |
| 7291 | return 0; |
| 7292 | } |
| 7293 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 7294 | *buf_size = fill_textconv(r, textconv, df, buf); |
Jeff Smith | 3a35cb2 | 2017-05-24 00:15:10 -0500 | [diff] [blame] | 7295 | free_filespec(df); |
| 7296 | return 1; |
| 7297 | } |
| 7298 | |
Nguyễn Thái Ngọc Duy | 4914c96 | 2012-10-26 22:53:52 +0700 | [diff] [blame] | 7299 | void setup_diff_pager(struct diff_options *opt) |
| 7300 | { |
| 7301 | /* |
| 7302 | * If the user asked for our exit code, then either they want --quiet |
| 7303 | * or --exit-code. We should definitely not bother with a pager in the |
| 7304 | * former case, as we will generate no output. Since we still properly |
| 7305 | * report our exit code even when a pager is run, we _could_ run a |
| 7306 | * pager with --exit-code. But since we have not done so historically, |
| 7307 | * and because it is easy to find people oneline advising "git diff |
| 7308 | * --exit-code" in hooks and other scripts, we do not do so. |
| 7309 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7310 | if (!opt->flags.exit_with_status && |
Nguyễn Thái Ngọc Duy | 4914c96 | 2012-10-26 22:53:52 +0700 | [diff] [blame] | 7311 | check_pager_config("diff") != 0) |
| 7312 | setup_pager(); |
| 7313 | } |