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; |
Patrick Steinhardt | 1b261c2 | 2024-05-27 13:46:39 +0200 | [diff] [blame] | 59 | static char *diff_word_regex_cfg; |
René Scharfe | 54443bb | 2024-06-09 09:39:08 +0200 | [diff] [blame] | 60 | static struct external_diff external_diff_cfg; |
Patrick Steinhardt | 6073b3b | 2024-05-27 13:46:15 +0200 | [diff] [blame] | 61 | static 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; |
Patrick Steinhardt | f9c1989 | 2024-05-27 13:46:20 +0200 | [diff] [blame] | 65 | static char *diff_src_prefix; |
| 66 | static char *diff_dst_prefix; |
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")) { |
Patrick Steinhardt | f9c1989 | 2024-05-27 13:46:20 +0200 | [diff] [blame] | 414 | FREE_AND_NULL(diff_src_prefix); |
Patrick Steinhardt | 1b261c2 | 2024-05-27 13:46:39 +0200 | [diff] [blame] | 415 | return git_config_string(&diff_src_prefix, var, value); |
Peter Hutterer | 7fdc265 | 2024-03-15 11:03:10 +1000 | [diff] [blame] | 416 | } |
| 417 | if (!strcmp(var, "diff.dstprefix")) { |
Patrick Steinhardt | f9c1989 | 2024-05-27 13:46:20 +0200 | [diff] [blame] | 418 | FREE_AND_NULL(diff_dst_prefix); |
Patrick Steinhardt | 1b261c2 | 2024-05-27 13:46:39 +0200 | [diff] [blame] | 419 | return git_config_string(&diff_dst_prefix, var, value); |
Peter Hutterer | 7fdc265 | 2024-03-15 11:03:10 +1000 | [diff] [blame] | 420 | } |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 421 | if (!strcmp(var, "diff.relative")) { |
| 422 | diff_relative = git_config_bool(var, value); |
| 423 | return 0; |
| 424 | } |
Dragan Simic | bd48adc | 2023-09-11 17:39:44 +0200 | [diff] [blame] | 425 | if (!strcmp(var, "diff.statnamewidth")) { |
| 426 | diff_stat_name_width = git_config_int(var, value, ctx->kvi); |
| 427 | return 0; |
| 428 | } |
Zbigniew Jędrzejewski-Szmek | df44483 | 2012-03-01 13:26:46 +0100 | [diff] [blame] | 429 | if (!strcmp(var, "diff.statgraphwidth")) { |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 430 | 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] | 431 | return 0; |
| 432 | } |
Brian Hetro | daec808 | 2008-07-05 01:24:43 -0400 | [diff] [blame] | 433 | if (!strcmp(var, "diff.external")) |
René Scharfe | 54443bb | 2024-06-09 09:39:08 +0200 | [diff] [blame] | 434 | return git_config_string(&external_diff_cfg.cmd, var, value); |
René Scharfe | d7b97b7 | 2024-06-09 09:41:44 +0200 | [diff] [blame] | 435 | if (!strcmp(var, "diff.trustexitcode")) { |
| 436 | external_diff_cfg.trust_exit_code = git_config_bool(var, value); |
| 437 | return 0; |
| 438 | } |
Boyd Stephen Smith Jr | 98a4d87 | 2009-01-20 21:46:57 -0600 | [diff] [blame] | 439 | if (!strcmp(var, "diff.wordregex")) |
| 440 | return git_config_string(&diff_word_regex_cfg, var, value); |
Samuel Bronson | 6d8940b | 2013-12-18 19:08:12 -0500 | [diff] [blame] | 441 | if (!strcmp(var, "diff.orderfile")) |
| 442 | return git_config_pathname(&diff_order_file_cfg, var, value); |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 443 | |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 444 | if (!strcmp(var, "diff.ignoresubmodules")) { |
| 445 | if (!value) |
| 446 | return config_error_nonbool(var); |
Johannes Schindelin | be4f2b4 | 2010-08-05 10:49:55 +0200 | [diff] [blame] | 447 | handle_ignore_submodules_arg(&default_diff_options, value); |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 448 | } |
Johannes Schindelin | be4f2b4 | 2010-08-05 10:49:55 +0200 | [diff] [blame] | 449 | |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 450 | if (!strcmp(var, "diff.submodule")) { |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 451 | if (!value) |
| 452 | return config_error_nonbool(var); |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 453 | if (parse_submodule_params(&default_diff_options, value)) |
| 454 | warning(_("Unknown value for 'diff.submodule' config variable: '%s'"), |
| 455 | value); |
| 456 | return 0; |
| 457 | } |
| 458 | |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 459 | if (!strcmp(var, "diff.algorithm")) { |
Jeff King | 0824879 | 2023-12-07 02:25:23 -0500 | [diff] [blame] | 460 | if (!value) |
| 461 | return config_error_nonbool(var); |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 462 | diff_algorithm = parse_algorithm_value(value); |
| 463 | if (diff_algorithm < 0) |
Jeff King | 0824879 | 2023-12-07 02:25:23 -0500 | [diff] [blame] | 464 | return error(_("unknown value for config '%s': %s"), |
| 465 | var, value); |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 466 | return 0; |
| 467 | } |
| 468 | |
Jeff King | 33c643b | 2017-10-13 13:24:31 -0400 | [diff] [blame] | 469 | if (git_color_config(var, value, cb) < 0) |
| 470 | return -1; |
| 471 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 472 | return git_diff_basic_config(var, value, ctx, cb); |
Jeff King | 9a1805a | 2008-01-04 03:59:34 -0500 | [diff] [blame] | 473 | } |
| 474 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 475 | int git_diff_basic_config(const char *var, const char *value, |
| 476 | const struct config_context *ctx, void *cb) |
Jeff King | 9a1805a | 2008-01-04 03:59:34 -0500 | [diff] [blame] | 477 | { |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 478 | const char *name; |
| 479 | |
Linus Torvalds | 2b6ca6d | 2008-08-05 11:27:30 -0700 | [diff] [blame] | 480 | if (!strcmp(var, "diff.renamelimit")) { |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 481 | diff_rename_limit_default = git_config_int(var, value, ctx->kvi); |
Linus Torvalds | 2b6ca6d | 2008-08-05 11:27:30 -0700 | [diff] [blame] | 482 | return 0; |
| 483 | } |
| 484 | |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 485 | if (userdiff_config(var, value) < 0) |
| 486 | return -1; |
Jeff King | c7534ef | 2008-10-26 00:45:55 -0400 | [diff] [blame] | 487 | |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 488 | if (skip_prefix(var, "diff.color.", &name) || |
| 489 | skip_prefix(var, "color.diff.", &name)) { |
| 490 | int slot = parse_diff_color_slot(name); |
Jeff King | 8b8e862 | 2009-12-12 07:25:24 -0500 | [diff] [blame] | 491 | if (slot < 0) |
| 492 | return 0; |
Junio C Hamano | 64f30e9 | 2008-02-11 10:53:56 -0800 | [diff] [blame] | 493 | if (!value) |
| 494 | return config_error_nonbool(var); |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 495 | return color_parse(value, diff_colors[slot]); |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 496 | } |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 497 | |
Jeff King | da80635 | 2020-01-31 04:57:49 -0500 | [diff] [blame] | 498 | if (!strcmp(var, "diff.wserrorhighlight")) { |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 499 | int val; |
| 500 | if (!value) |
| 501 | return config_error_nonbool(var); |
| 502 | val = parse_ws_error_highlight(value); |
Jeff King | da80635 | 2020-01-31 04:57:49 -0500 | [diff] [blame] | 503 | if (val < 0) |
Jeff King | 0824879 | 2023-12-07 02:25:23 -0500 | [diff] [blame] | 504 | return error(_("unknown value for config '%s': %s"), |
| 505 | var, value); |
Jeff King | da80635 | 2020-01-31 04:57:49 -0500 | [diff] [blame] | 506 | ws_error_highlight_default = val; |
| 507 | return 0; |
| 508 | } |
| 509 | |
Jim Meyering | a624eaa | 2008-08-15 13:39:26 +0200 | [diff] [blame] | 510 | /* like GNU diff's --suppress-blank-empty option */ |
Johannes Schindelin | 950db87 | 2009-01-20 22:08:33 +0100 | [diff] [blame] | 511 | if (!strcmp(var, "diff.suppressblankempty") || |
| 512 | /* for backwards compatibility */ |
| 513 | !strcmp(var, "diff.suppress-blank-empty")) { |
Jim Meyering | a624eaa | 2008-08-15 13:39:26 +0200 | [diff] [blame] | 514 | diff_suppress_blank_empty = git_config_bool(var, value); |
| 515 | return 0; |
| 516 | } |
| 517 | |
Johan Herland | 2d17495 | 2011-04-29 11:36:19 +0200 | [diff] [blame] | 518 | if (!strcmp(var, "diff.dirstat")) { |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 519 | struct strbuf errmsg = STRBUF_INIT; |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 520 | if (!value) |
| 521 | return config_error_nonbool(var); |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 522 | default_diff_options.dirstat_permille = diff_dirstat_permille_default; |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 523 | if (parse_dirstat_params(&default_diff_options, value, &errmsg)) |
Johan Herland | 7478ac5 | 2011-04-29 11:36:23 +0200 | [diff] [blame] | 524 | warning(_("Found errors in 'diff.dirstat' config variable:\n%s"), |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 525 | errmsg.buf); |
| 526 | strbuf_release(&errmsg); |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 527 | diff_dirstat_permille_default = default_diff_options.dirstat_permille; |
Johan Herland | 2d17495 | 2011-04-29 11:36:19 +0200 | [diff] [blame] | 528 | return 0; |
| 529 | } |
| 530 | |
Marc Branchaud | cf5e772 | 2017-05-08 12:03:36 -0400 | [diff] [blame] | 531 | if (git_diff_heuristic_config(var, value, cb) < 0) |
| 532 | return -1; |
| 533 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 534 | return git_default_config(var, value, ctx, cb); |
Junio C Hamano | 801235c | 2006-06-24 04:06:23 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 537 | static char *quote_two(const char *one, const char *two) |
| 538 | { |
Junio C Hamano | 7c37c97 | 2020-09-10 10:01:59 -0700 | [diff] [blame] | 539 | int need_one = quote_c_style(one, NULL, NULL, CQUOTE_NODQ); |
| 540 | int need_two = quote_c_style(two, NULL, NULL, CQUOTE_NODQ); |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 541 | struct strbuf res = STRBUF_INIT; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 542 | |
| 543 | if (need_one + need_two) { |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 544 | strbuf_addch(&res, '"'); |
Junio C Hamano | 7c37c97 | 2020-09-10 10:01:59 -0700 | [diff] [blame] | 545 | quote_c_style(one, &res, NULL, CQUOTE_NODQ); |
| 546 | quote_c_style(two, &res, NULL, CQUOTE_NODQ); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 547 | strbuf_addch(&res, '"'); |
| 548 | } else { |
| 549 | strbuf_addstr(&res, one); |
| 550 | strbuf_addstr(&res, two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 551 | } |
Pierre Habouzit | b315c5c | 2007-09-27 12:58:23 +0200 | [diff] [blame] | 552 | return strbuf_detach(&res, NULL); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 553 | } |
| 554 | |
René Scharfe | 54443bb | 2024-06-09 09:39:08 +0200 | [diff] [blame] | 555 | static const struct external_diff *external_diff(void) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 556 | { |
René Scharfe | 54443bb | 2024-06-09 09:39:08 +0200 | [diff] [blame] | 557 | static struct external_diff external_diff_env, *external_diff_ptr; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 558 | static int done_preparing = 0; |
| 559 | |
| 560 | if (done_preparing) |
René Scharfe | 54443bb | 2024-06-09 09:39:08 +0200 | [diff] [blame] | 561 | return external_diff_ptr; |
| 562 | external_diff_env.cmd = xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF")); |
René Scharfe | d7b97b7 | 2024-06-09 09:41:44 +0200 | [diff] [blame] | 563 | if (git_env_bool("GIT_EXTERNAL_DIFF_TRUST_EXIT_CODE", 0)) |
| 564 | external_diff_env.trust_exit_code = 1; |
René Scharfe | 54443bb | 2024-06-09 09:39:08 +0200 | [diff] [blame] | 565 | if (external_diff_env.cmd) |
| 566 | external_diff_ptr = &external_diff_env; |
| 567 | else if (external_diff_cfg.cmd) |
| 568 | external_diff_ptr = &external_diff_cfg; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 569 | done_preparing = 1; |
René Scharfe | 54443bb | 2024-06-09 09:39:08 +0200 | [diff] [blame] | 570 | return external_diff_ptr; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 573 | /* |
| 574 | * Keep track of files used for diffing. Sometimes such an entry |
| 575 | * refers to a temporary file, sometimes to an existing file, and |
| 576 | * sometimes to "/dev/null". |
| 577 | */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 578 | static struct diff_tempfile { |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 579 | /* |
| 580 | * filename external diff should read from, or NULL if this |
| 581 | * entry is currently not in use: |
| 582 | */ |
| 583 | const char *name; |
| 584 | |
brian m. carlson | dc01505 | 2017-03-26 16:01:24 +0000 | [diff] [blame] | 585 | char hex[GIT_MAX_HEXSZ + 1]; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 586 | char mode[10]; |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 587 | |
| 588 | /* |
| 589 | * If this diff_tempfile instance refers to a temporary file, |
| 590 | * this tempfile object is used to manage its lifetime. |
| 591 | */ |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 592 | struct tempfile *tempfile; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 593 | } diff_temp[2]; |
| 594 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 595 | struct emit_callback { |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 596 | int color_diff; |
| 597 | unsigned ws_rule; |
| 598 | int blank_at_eof_in_preimage; |
| 599 | int blank_at_eof_in_postimage; |
| 600 | int lno_in_preimage; |
| 601 | int lno_in_postimage; |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 602 | const char **label_path; |
| 603 | struct diff_words_data *diff_words; |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 604 | struct diff_options *opt; |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 605 | struct strbuf *header; |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 606 | }; |
| 607 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 608 | static int count_lines(const char *data, int size) |
| 609 | { |
| 610 | int count, ch, completely_empty = 1, nl_just_seen = 0; |
| 611 | count = 0; |
| 612 | while (0 < size--) { |
| 613 | ch = *data++; |
| 614 | if (ch == '\n') { |
| 615 | count++; |
| 616 | nl_just_seen = 1; |
| 617 | completely_empty = 0; |
| 618 | } |
| 619 | else { |
| 620 | nl_just_seen = 0; |
| 621 | completely_empty = 0; |
| 622 | } |
| 623 | } |
| 624 | if (completely_empty) |
| 625 | return 0; |
| 626 | if (!nl_just_seen) |
| 627 | count++; /* no trailing newline */ |
| 628 | return count; |
| 629 | } |
| 630 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 631 | static int fill_mmfile(struct repository *r, mmfile_t *mf, |
| 632 | struct diff_filespec *one) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 633 | { |
| 634 | if (!DIFF_FILE_VALID(one)) { |
| 635 | mf->ptr = (char *)""; /* does not matter */ |
| 636 | mf->size = 0; |
| 637 | return 0; |
| 638 | } |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 639 | else if (diff_populate_filespec(r, one, NULL)) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 640 | return -1; |
Junio C Hamano | bb35fef | 2009-09-15 03:38:30 -0700 | [diff] [blame] | 641 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 642 | mf->ptr = one->data; |
| 643 | mf->size = one->size; |
| 644 | return 0; |
| 645 | } |
| 646 | |
Jeff King | abb371a | 2011-02-19 03:16:32 -0500 | [diff] [blame] | 647 | /* 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] | 648 | static unsigned long diff_filespec_size(struct repository *r, |
| 649 | struct diff_filespec *one) |
Jeff King | abb371a | 2011-02-19 03:16:32 -0500 | [diff] [blame] | 650 | { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 651 | struct diff_populate_filespec_options dpf_options = { |
| 652 | .check_size_only = 1, |
| 653 | }; |
| 654 | |
Jeff King | abb371a | 2011-02-19 03:16:32 -0500 | [diff] [blame] | 655 | if (!DIFF_FILE_VALID(one)) |
| 656 | return 0; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 657 | diff_populate_filespec(r, one, &dpf_options); |
Jeff King | abb371a | 2011-02-19 03:16:32 -0500 | [diff] [blame] | 658 | return one->size; |
| 659 | } |
| 660 | |
Jeff King | c5224f0 | 2022-12-13 06:12:58 -0500 | [diff] [blame] | 661 | static int count_trailing_blank(mmfile_t *mf) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 662 | { |
| 663 | char *ptr = mf->ptr; |
| 664 | long size = mf->size; |
| 665 | int cnt = 0; |
| 666 | |
| 667 | if (!size) |
| 668 | return cnt; |
| 669 | ptr += size - 1; /* pointing at the very end */ |
| 670 | if (*ptr != '\n') |
| 671 | ; /* incomplete line */ |
| 672 | else |
| 673 | ptr--; /* skip the last LF */ |
| 674 | while (mf->ptr < ptr) { |
| 675 | char *prev_eol; |
| 676 | for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--) |
| 677 | if (*prev_eol == '\n') |
| 678 | break; |
Jeff King | c5224f0 | 2022-12-13 06:12:58 -0500 | [diff] [blame] | 679 | if (!ws_blank_line(prev_eol + 1, ptr - prev_eol)) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 680 | break; |
| 681 | cnt++; |
| 682 | ptr = prev_eol - 1; |
| 683 | } |
| 684 | return cnt; |
| 685 | } |
| 686 | |
| 687 | static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2, |
| 688 | struct emit_callback *ecbdata) |
| 689 | { |
| 690 | int l1, l2, at; |
Jeff King | c5224f0 | 2022-12-13 06:12:58 -0500 | [diff] [blame] | 691 | l1 = count_trailing_blank(mf1); |
| 692 | l2 = count_trailing_blank(mf2); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 693 | if (l2 <= l1) { |
| 694 | ecbdata->blank_at_eof_in_preimage = 0; |
| 695 | ecbdata->blank_at_eof_in_postimage = 0; |
| 696 | return; |
| 697 | } |
| 698 | at = count_lines(mf1->ptr, mf1->size); |
| 699 | ecbdata->blank_at_eof_in_preimage = (at - l1) + 1; |
| 700 | |
| 701 | at = count_lines(mf2->ptr, mf2->size); |
| 702 | ecbdata->blank_at_eof_in_postimage = (at - l2) + 1; |
| 703 | } |
| 704 | |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 705 | static void emit_line_0(struct diff_options *o, |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 706 | 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] | 707 | int first, const char *line, int len) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 708 | { |
| 709 | int has_trailing_newline, has_trailing_carriage_return; |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 710 | int needs_reset = 0; /* at the end of the line */ |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 711 | FILE *file = o->file; |
| 712 | |
Stefan Beller | f103a6f | 2018-08-13 18:41:21 -0700 | [diff] [blame] | 713 | fputs(diff_line_prefix(o), file); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 714 | |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 715 | has_trailing_newline = (len > 0 && line[len-1] == '\n'); |
| 716 | if (has_trailing_newline) |
| 717 | len--; |
| 718 | |
| 719 | has_trailing_carriage_return = (len > 0 && line[len-1] == '\r'); |
| 720 | if (has_trailing_carriage_return) |
| 721 | len--; |
| 722 | |
| 723 | if (!len && !first) |
| 724 | goto end_of_line; |
| 725 | |
| 726 | if (reverse && want_color(o->use_color)) { |
| 727 | fputs(GIT_COLOR_REVERSE, file); |
| 728 | needs_reset = 1; |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 729 | } |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 730 | |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 731 | if (set_sign) { |
| 732 | fputs(set_sign, file); |
| 733 | needs_reset = 1; |
Junio C Hamano | 06a4755 | 2009-11-27 22:04:10 -0800 | [diff] [blame] | 734 | } |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 735 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 736 | if (first) |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 737 | fputc(first, file); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 738 | |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 739 | if (!len) |
| 740 | goto end_of_line; |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 741 | |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 742 | if (set) { |
| 743 | if (set_sign && set != set_sign) |
| 744 | fputs(reset, file); |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 745 | fputs(set, file); |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 746 | needs_reset = 1; |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 747 | } |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 748 | fwrite(line, len, 1, file); |
| 749 | needs_reset = 1; /* 'line' may contain color codes. */ |
| 750 | |
| 751 | end_of_line: |
| 752 | if (needs_reset) |
| 753 | fputs(reset, file); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 754 | if (has_trailing_carriage_return) |
| 755 | fputc('\r', file); |
| 756 | if (has_trailing_newline) |
| 757 | fputc('\n', file); |
| 758 | } |
| 759 | |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 760 | 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] | 761 | const char *line, int len) |
| 762 | { |
Stefan Beller | 4441067 | 2018-08-13 18:41:22 -0700 | [diff] [blame] | 763 | emit_line_0(o, set, NULL, 0, reset, 0, line, len); |
Junio C Hamano | 250f799 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 766 | enum diff_symbol { |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 767 | DIFF_SYMBOL_BINARY_DIFF_HEADER, |
| 768 | DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA, |
| 769 | DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL, |
| 770 | DIFF_SYMBOL_BINARY_DIFF_BODY, |
| 771 | DIFF_SYMBOL_BINARY_DIFF_FOOTER, |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 772 | DIFF_SYMBOL_STATS_SUMMARY_NO_FILES, |
| 773 | DIFF_SYMBOL_STATS_SUMMARY_ABBREV, |
| 774 | DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES, |
| 775 | DIFF_SYMBOL_STATS_LINE, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 776 | DIFF_SYMBOL_WORD_DIFF, |
Stefan Beller | 30b7e1e | 2017-06-29 17:07:04 -0700 | [diff] [blame] | 777 | DIFF_SYMBOL_STAT_SEP, |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 778 | DIFF_SYMBOL_SUMMARY, |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 779 | DIFF_SYMBOL_SUBMODULE_ADD, |
| 780 | DIFF_SYMBOL_SUBMODULE_DEL, |
| 781 | DIFF_SYMBOL_SUBMODULE_UNTRACKED, |
| 782 | DIFF_SYMBOL_SUBMODULE_MODIFIED, |
| 783 | DIFF_SYMBOL_SUBMODULE_HEADER, |
| 784 | DIFF_SYMBOL_SUBMODULE_ERROR, |
| 785 | DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 786 | DIFF_SYMBOL_REWRITE_DIFF, |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 787 | DIFF_SYMBOL_BINARY_FILES, |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 788 | DIFF_SYMBOL_HEADER, |
Stefan Beller | 3ee8b7b | 2017-06-29 17:06:56 -0700 | [diff] [blame] | 789 | DIFF_SYMBOL_FILEPAIR_PLUS, |
| 790 | DIFF_SYMBOL_FILEPAIR_MINUS, |
Stefan Beller | ff95867 | 2017-06-29 17:06:54 -0700 | [diff] [blame] | 791 | DIFF_SYMBOL_WORDS_PORCELAIN, |
| 792 | DIFF_SYMBOL_WORDS, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 793 | DIFF_SYMBOL_CONTEXT, |
Stefan Beller | f2bb121 | 2017-06-29 17:06:55 -0700 | [diff] [blame] | 794 | DIFF_SYMBOL_CONTEXT_INCOMPLETE, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 795 | DIFF_SYMBOL_PLUS, |
| 796 | DIFF_SYMBOL_MINUS, |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 797 | DIFF_SYMBOL_NO_LF_EOF, |
Stefan Beller | 68abc6f | 2017-06-29 17:06:51 -0700 | [diff] [blame] | 798 | DIFF_SYMBOL_CONTEXT_FRAGINFO, |
Stefan Beller | c64b420 | 2017-06-29 17:06:50 -0700 | [diff] [blame] | 799 | DIFF_SYMBOL_CONTEXT_MARKER, |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 800 | DIFF_SYMBOL_SEPARATOR |
| 801 | }; |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 802 | /* |
| 803 | * Flags for content lines: |
| 804 | * 0..12 are whitespace rules |
| 805 | * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT |
| 806 | * 16 is marking if the line is blank at EOF |
| 807 | */ |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 808 | #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16) |
| 809 | #define DIFF_SYMBOL_MOVED_LINE (1<<17) |
| 810 | #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18) |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 811 | #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19) |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 812 | #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK) |
| 813 | |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 814 | /* |
| 815 | * This struct is used when we need to buffer the output of the diff output. |
| 816 | * |
| 817 | * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer |
| 818 | * into the pre/post image file. This pointer could be a union with the |
| 819 | * line pointer. By storing an offset into the file instead of the literal line, |
| 820 | * we can decrease the memory footprint for the buffered output. At first we |
| 821 | * may want to only have indirection for the content lines, but we could also |
| 822 | * enhance the state for emitting prefabricated lines, e.g. the similarity |
| 823 | * score line or hunk/file headers would only need to store a number or path |
| 824 | * and then the output can be constructed later on depending on state. |
| 825 | */ |
| 826 | struct emitted_diff_symbol { |
| 827 | const char *line; |
| 828 | int len; |
| 829 | int flags; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 830 | int indent_off; /* Offset to first non-whitespace character */ |
| 831 | int indent_width; /* The visual width of the indentation */ |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 832 | unsigned id; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 833 | enum diff_symbol s; |
| 834 | }; |
Ævar Arnfjörð Bjarmason | 9865b6e | 2021-09-27 14:54:25 +0200 | [diff] [blame] | 835 | #define EMITTED_DIFF_SYMBOL_INIT { 0 } |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 836 | |
| 837 | struct emitted_diff_symbols { |
| 838 | struct emitted_diff_symbol *buf; |
| 839 | int nr, alloc; |
| 840 | }; |
Ævar Arnfjörð Bjarmason | 9865b6e | 2021-09-27 14:54:25 +0200 | [diff] [blame] | 841 | #define EMITTED_DIFF_SYMBOLS_INIT { 0 } |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 842 | |
| 843 | static void append_emitted_diff_symbol(struct diff_options *o, |
| 844 | struct emitted_diff_symbol *e) |
| 845 | { |
| 846 | struct emitted_diff_symbol *f; |
| 847 | |
| 848 | ALLOC_GROW(o->emitted_symbols->buf, |
| 849 | o->emitted_symbols->nr + 1, |
| 850 | o->emitted_symbols->alloc); |
| 851 | f = &o->emitted_symbols->buf[o->emitted_symbols->nr++]; |
| 852 | |
| 853 | memcpy(f, e, sizeof(struct emitted_diff_symbol)); |
| 854 | f->line = e->line ? xmemdupz(e->line, e->len) : NULL; |
| 855 | } |
| 856 | |
Ævar Arnfjörð Bjarmason | 77e56d5 | 2022-03-17 15:55:35 +0100 | [diff] [blame] | 857 | static void free_emitted_diff_symbols(struct emitted_diff_symbols *e) |
| 858 | { |
| 859 | if (!e) |
| 860 | return; |
| 861 | free(e->buf); |
| 862 | free(e); |
| 863 | } |
| 864 | |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 865 | struct moved_entry { |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 866 | const struct emitted_diff_symbol *es; |
| 867 | struct moved_entry *next_line; |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 868 | struct moved_entry *next_match; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 869 | }; |
| 870 | |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 871 | struct moved_block { |
| 872 | struct moved_entry *match; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 873 | int wsd; /* The whitespace delta of this block */ |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 874 | }; |
| 875 | |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 876 | #define INDENT_BLANKLINE INT_MIN |
| 877 | |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 878 | static void fill_es_indent_data(struct emitted_diff_symbol *es) |
| 879 | { |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 880 | unsigned int off = 0, i; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 881 | int width = 0, tab_width = es->flags & WS_TAB_WIDTH_MASK; |
| 882 | const char *s = es->line; |
| 883 | const int len = es->len; |
| 884 | |
| 885 | /* skip any \v \f \r at start of indentation */ |
| 886 | while (s[off] == '\f' || s[off] == '\v' || |
| 887 | (s[off] == '\r' && off < len - 1)) |
| 888 | off++; |
| 889 | |
| 890 | /* calculate the visual width of indentation */ |
| 891 | while(1) { |
| 892 | if (s[off] == ' ') { |
| 893 | width++; |
| 894 | off++; |
| 895 | } else if (s[off] == '\t') { |
| 896 | width += tab_width - (width % tab_width); |
| 897 | while (s[++off] == '\t') |
| 898 | width += tab_width; |
| 899 | } else { |
| 900 | break; |
| 901 | } |
| 902 | } |
| 903 | |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 904 | /* check if this line is blank */ |
| 905 | for (i = off; i < len; i++) |
| 906 | if (!isspace(s[i])) |
| 907 | break; |
| 908 | |
| 909 | if (i == len) { |
| 910 | es->indent_width = INDENT_BLANKLINE; |
| 911 | es->indent_off = len; |
| 912 | } else { |
| 913 | es->indent_off = off; |
| 914 | es->indent_width = width; |
| 915 | } |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 918 | static int compute_ws_delta(const struct emitted_diff_symbol *a, |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 919 | const struct emitted_diff_symbol *b) |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 920 | { |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 921 | int a_width = a->indent_width, |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 922 | b_width = b->indent_width; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 923 | |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 924 | if (a_width == INDENT_BLANKLINE && b_width == INDENT_BLANKLINE) |
| 925 | return INDENT_BLANKLINE; |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 926 | |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 927 | return a_width - b_width; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Phillip Wood | 52d14e1 | 2021-12-09 10:30:02 +0000 | [diff] [blame] | 930 | static int cmp_in_block_with_wsd(const struct moved_entry *cur, |
| 931 | const struct emitted_diff_symbol *l, |
| 932 | struct moved_block *pmb) |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 933 | { |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 934 | int a_width = cur->es->indent_width, b_width = l->indent_width; |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 935 | int delta; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 936 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 937 | /* The text of each line must match */ |
| 938 | if (cur->es->id != l->id) |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 939 | return 1; |
| 940 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 941 | /* |
| 942 | * If 'l' and 'cur' are both blank then we don't need to check the |
| 943 | * indent. We only need to check cur as we know the strings match. |
| 944 | * */ |
| 945 | if (a_width == INDENT_BLANKLINE) |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 946 | return 0; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 947 | |
| 948 | /* |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 949 | * The indent changes of the block are known and stored in pmb->wsd; |
| 950 | * 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] | 951 | * match those of the current block. |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 952 | */ |
Phillip Wood | 52d14e1 | 2021-12-09 10:30:02 +0000 | [diff] [blame] | 953 | delta = b_width - a_width; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 954 | |
Phillip Wood | 0cd51e9 | 2018-11-23 11:16:58 +0000 | [diff] [blame] | 955 | /* |
| 956 | * If the previous lines of this block were all blank then set its |
| 957 | * whitespace delta. |
| 958 | */ |
| 959 | if (pmb->wsd == INDENT_BLANKLINE) |
| 960 | pmb->wsd = delta; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 961 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 962 | return delta != pmb->wsd; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 965 | struct interned_diff_symbol { |
| 966 | struct hashmap_entry ent; |
| 967 | struct emitted_diff_symbol *es; |
| 968 | }; |
| 969 | |
| 970 | static int interned_diff_symbol_cmp(const void *hashmap_cmp_fn_data, |
| 971 | const struct hashmap_entry *eptr, |
| 972 | const struct hashmap_entry *entry_or_key, |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 973 | const void *keydata UNUSED) |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 974 | { |
Stefan Beller | ee1df66 | 2018-07-16 16:05:38 -0700 | [diff] [blame] | 975 | const struct diff_options *diffopt = hashmap_cmp_fn_data; |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 976 | const struct emitted_diff_symbol *a, *b; |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 977 | unsigned flags = diffopt->color_moved_ws_handling |
| 978 | & XDF_WHITESPACE_FLAGS; |
Stefan Beller | ee1df66 | 2018-07-16 16:05:38 -0700 | [diff] [blame] | 979 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 980 | a = container_of(eptr, const struct interned_diff_symbol, ent)->es; |
| 981 | 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] | 982 | |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 983 | return !xdiff_compare_lines(a->line + a->indent_off, |
| 984 | a->len - a->indent_off, |
| 985 | b->line + b->indent_off, |
| 986 | b->len - b->indent_off, flags); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 989 | static void prepare_entry(struct diff_options *o, struct emitted_diff_symbol *l, |
| 990 | struct interned_diff_symbol *s) |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 991 | { |
Stefan Beller | b309571 | 2018-07-16 16:05:40 -0700 | [diff] [blame] | 992 | unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS; |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 993 | unsigned int hash = xdiff_hash_string(l->line + l->indent_off, |
| 994 | l->len - l->indent_off, flags); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 995 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 996 | hashmap_entry_init(&s->ent, hash); |
| 997 | s->es = l; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1000 | struct moved_entry_list { |
| 1001 | struct moved_entry *add, *del; |
| 1002 | }; |
| 1003 | |
| 1004 | static struct moved_entry_list *add_lines_to_move_detection(struct diff_options *o, |
| 1005 | struct mem_pool *entry_mem_pool) |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1006 | { |
| 1007 | struct moved_entry *prev_line = NULL; |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1008 | struct mem_pool interned_pool; |
| 1009 | struct hashmap interned_map; |
| 1010 | struct moved_entry_list *entry_list = NULL; |
| 1011 | size_t entry_list_alloc = 0; |
| 1012 | unsigned id = 0; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1013 | int n; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1014 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1015 | hashmap_init(&interned_map, interned_diff_symbol_cmp, o, 8096); |
| 1016 | mem_pool_init(&interned_pool, 1024 * 1024); |
| 1017 | |
| 1018 | for (n = 0; n < o->emitted_symbols->nr; n++) { |
| 1019 | struct interned_diff_symbol key; |
| 1020 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; |
| 1021 | struct interned_diff_symbol *s; |
| 1022 | struct moved_entry *entry; |
| 1023 | |
| 1024 | if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS) { |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1025 | prev_line = NULL; |
| 1026 | continue; |
| 1027 | } |
| 1028 | |
Phillip Wood | 21536d0 | 2018-11-23 11:16:57 +0000 | [diff] [blame] | 1029 | if (o->color_moved_ws_handling & |
| 1030 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1031 | fill_es_indent_data(l); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1032 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1033 | prepare_entry(o, l, &key); |
| 1034 | s = hashmap_get_entry(&interned_map, &key, ent, &key.ent); |
| 1035 | if (s) { |
| 1036 | l->id = s->es->id; |
| 1037 | } else { |
| 1038 | l->id = id; |
| 1039 | ALLOC_GROW_BY(entry_list, id, 1, entry_list_alloc); |
| 1040 | hashmap_add(&interned_map, |
| 1041 | memcpy(mem_pool_alloc(&interned_pool, |
| 1042 | sizeof(key)), |
| 1043 | &key, sizeof(key))); |
| 1044 | } |
| 1045 | entry = mem_pool_alloc(entry_mem_pool, sizeof(*entry)); |
| 1046 | entry->es = l; |
| 1047 | entry->next_line = NULL; |
| 1048 | if (prev_line && prev_line->es->s == l->s) |
| 1049 | prev_line->next_line = entry; |
| 1050 | prev_line = entry; |
| 1051 | if (l->s == DIFF_SYMBOL_PLUS) { |
| 1052 | entry->next_match = entry_list[l->id].add; |
| 1053 | entry_list[l->id].add = entry; |
| 1054 | } else { |
| 1055 | entry->next_match = entry_list[l->id].del; |
| 1056 | entry_list[l->id].del = entry; |
| 1057 | } |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1058 | } |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1059 | |
| 1060 | hashmap_clear(&interned_map); |
| 1061 | mem_pool_discard(&interned_pool, 0); |
| 1062 | |
| 1063 | return entry_list; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1066 | static void pmb_advance_or_null(struct diff_options *o, |
Phillip Wood | 08fba10 | 2021-12-09 10:30:03 +0000 | [diff] [blame] | 1067 | struct emitted_diff_symbol *l, |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1068 | struct moved_block *pmb, |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1069 | int *pmb_nr) |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1070 | { |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1071 | int i, j; |
Phillip Wood | 08fba10 | 2021-12-09 10:30:03 +0000 | [diff] [blame] | 1072 | |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1073 | for (i = 0, j = 0; i < *pmb_nr; i++) { |
Phillip Wood | ff046a0 | 2021-12-09 10:30:04 +0000 | [diff] [blame] | 1074 | int match; |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1075 | struct moved_entry *prev = pmb[i].match; |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1076 | struct moved_entry *cur = (prev && prev->next_line) ? |
| 1077 | prev->next_line : NULL; |
Stefan Beller | e2fe6ab | 2018-07-16 16:05:41 -0700 | [diff] [blame] | 1078 | |
Phillip Wood | ff046a0 | 2021-12-09 10:30:04 +0000 | [diff] [blame] | 1079 | if (o->color_moved_ws_handling & |
| 1080 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) |
| 1081 | match = cur && |
| 1082 | !cmp_in_block_with_wsd(cur, l, &pmb[i]); |
| 1083 | else |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1084 | match = cur && cur->es->id == l->id; |
| 1085 | |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1086 | if (match) { |
| 1087 | pmb[j] = pmb[i]; |
| 1088 | pmb[j++].match = cur; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1089 | } |
| 1090 | } |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1091 | *pmb_nr = j; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1094 | static void fill_potential_moved_blocks(struct diff_options *o, |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1095 | struct moved_entry *match, |
| 1096 | struct emitted_diff_symbol *l, |
| 1097 | struct moved_block **pmb_p, |
| 1098 | int *pmb_alloc_p, int *pmb_nr_p) |
| 1099 | |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1100 | { |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1101 | struct moved_block *pmb = *pmb_p; |
| 1102 | int pmb_alloc = *pmb_alloc_p, pmb_nr = *pmb_nr_p; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1103 | |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1104 | /* |
| 1105 | * The current line is the start of a new block. |
| 1106 | * Setup the set of potential blocks. |
| 1107 | */ |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1108 | for (; match; match = match->next_match) { |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1109 | ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc); |
| 1110 | if (o->color_moved_ws_handling & |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 1111 | COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) |
| 1112 | pmb[pmb_nr].wsd = compute_ws_delta(l, match->es); |
| 1113 | else |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1114 | pmb[pmb_nr].wsd = 0; |
Phillip Wood | 25e6190 | 2021-12-09 10:30:07 +0000 | [diff] [blame] | 1115 | pmb[pmb_nr++].match = match; |
Stefan Beller | ca1f4ae | 2018-07-18 12:31:55 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Phillip Wood | 7dfe427 | 2021-12-09 10:29:57 +0000 | [diff] [blame] | 1118 | *pmb_p = pmb; |
| 1119 | *pmb_alloc_p = pmb_alloc; |
| 1120 | *pmb_nr_p = pmb_nr; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1123 | /* |
| 1124 | * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing. |
| 1125 | * |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1126 | * Otherwise, if the last block has fewer alphanumeric characters than |
| 1127 | * 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] | 1128 | * that block. |
| 1129 | * |
| 1130 | * The last block consists of the (n - block_length)'th line up to but not |
| 1131 | * including the nth line. |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1132 | * |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1133 | * Returns 0 if the last block is empty or is unset by this function, non zero |
| 1134 | * otherwise. |
| 1135 | * |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1136 | * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c. |
| 1137 | * Think of a way to unify them. |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1138 | */ |
Phillip Wood | bea084b | 2021-12-09 10:29:56 +0000 | [diff] [blame] | 1139 | #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \ |
| 1140 | (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT) |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1141 | 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] | 1142 | { |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1143 | int i, alnum_count = 0; |
| 1144 | if (o->color_moved == COLOR_MOVED_PLAIN) |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1145 | return block_length; |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1146 | for (i = 1; i < block_length + 1; i++) { |
| 1147 | const char *c = o->emitted_symbols->buf[n - i].line; |
| 1148 | for (; *c; c++) { |
| 1149 | if (!isalnum(*c)) |
| 1150 | continue; |
| 1151 | alnum_count++; |
| 1152 | if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT) |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1153 | return 1; |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1154 | } |
| 1155 | } |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1156 | for (i = 1; i < block_length + 1; i++) |
Phillip Wood | bea084b | 2021-12-09 10:29:56 +0000 | [diff] [blame] | 1157 | 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] | 1158 | return 0; |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1159 | } |
| 1160 | |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1161 | /* Find blocks of moved code, delegate actual coloring decision to helper */ |
| 1162 | static void mark_color_as_moved(struct diff_options *o, |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1163 | struct moved_entry_list *entry_list) |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1164 | { |
Phillip Wood | 74d156f | 2018-10-04 11:07:41 +0100 | [diff] [blame] | 1165 | struct moved_block *pmb = NULL; /* potentially moved blocks */ |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1166 | int pmb_nr = 0, pmb_alloc = 0; |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1167 | int n, flipped_block = 0, block_length = 0; |
Phillip Wood | eb89352 | 2021-12-09 10:30:00 +0000 | [diff] [blame] | 1168 | enum diff_symbol moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1169 | |
| 1170 | |
| 1171 | for (n = 0; n < o->emitted_symbols->nr; n++) { |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1172 | struct moved_entry *match = NULL; |
| 1173 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1174 | |
| 1175 | switch (l->s) { |
| 1176 | case DIFF_SYMBOL_PLUS: |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1177 | match = entry_list[l->id].del; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1178 | break; |
| 1179 | case DIFF_SYMBOL_MINUS: |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1180 | match = entry_list[l->id].add; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1181 | break; |
| 1182 | default: |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1183 | flipped_block = 0; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
Phillip Wood | eb89352 | 2021-12-09 10:30:00 +0000 | [diff] [blame] | 1186 | if (pmb_nr && (!match || l->s != moved_symbol)) { |
Phillip Wood | 0990658 | 2021-12-09 10:29:58 +0000 | [diff] [blame] | 1187 | if (!adjust_last_block(o, n, block_length) && |
| 1188 | block_length > 1) { |
| 1189 | /* |
| 1190 | * Rewind in case there is another match |
| 1191 | * starting at the second line of the block |
| 1192 | */ |
| 1193 | match = NULL; |
| 1194 | n -= block_length; |
| 1195 | } |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1196 | pmb_nr = 0; |
| 1197 | block_length = 0; |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1198 | flipped_block = 0; |
Phillip Wood | eb89352 | 2021-12-09 10:30:00 +0000 | [diff] [blame] | 1199 | } |
| 1200 | if (!match) { |
| 1201 | moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1202 | continue; |
| 1203 | } |
| 1204 | |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1205 | if (o->color_moved == COLOR_MOVED_PLAIN) { |
| 1206 | l->flags |= DIFF_SYMBOL_MOVED_LINE; |
Stefan Beller | 176841f | 2017-06-30 13:53:08 -0700 | [diff] [blame] | 1207 | continue; |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1208 | } |
Stefan Beller | 176841f | 2017-06-30 13:53:08 -0700 | [diff] [blame] | 1209 | |
Phillip Wood | 0e488f1 | 2021-12-09 10:30:05 +0000 | [diff] [blame] | 1210 | pmb_advance_or_null(o, l, pmb, &pmb_nr); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1211 | |
| 1212 | if (pmb_nr == 0) { |
Phillip Wood | 0990658 | 2021-12-09 10:29:58 +0000 | [diff] [blame] | 1213 | int contiguous = adjust_last_block(o, n, block_length); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1214 | |
Phillip Wood | 0990658 | 2021-12-09 10:29:58 +0000 | [diff] [blame] | 1215 | if (!contiguous && block_length > 1) |
| 1216 | /* |
| 1217 | * Rewind in case there is another match |
| 1218 | * starting at the second line of the block |
| 1219 | */ |
| 1220 | n -= block_length; |
| 1221 | else |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 1222 | fill_potential_moved_blocks(o, match, l, |
Phillip Wood | 0990658 | 2021-12-09 10:29:58 +0000 | [diff] [blame] | 1223 | &pmb, &pmb_alloc, |
| 1224 | &pmb_nr); |
| 1225 | |
Phillip Wood | eb89352 | 2021-12-09 10:30:00 +0000 | [diff] [blame] | 1226 | if (contiguous && pmb_nr && moved_symbol == l->s) |
Phillip Wood | b0a2ba4 | 2018-11-23 11:16:55 +0000 | [diff] [blame] | 1227 | flipped_block = (flipped_block + 1) % 2; |
| 1228 | else |
| 1229 | flipped_block = 0; |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1230 | |
Phillip Wood | eb89352 | 2021-12-09 10:30:00 +0000 | [diff] [blame] | 1231 | if (pmb_nr) |
| 1232 | moved_symbol = l->s; |
| 1233 | else |
| 1234 | moved_symbol = DIFF_SYMBOL_BINARY_DIFF_HEADER; |
| 1235 | |
Jonathan Tan | f0b8fb6 | 2017-08-15 18:27:39 -0700 | [diff] [blame] | 1236 | block_length = 0; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1239 | if (pmb_nr) { |
| 1240 | block_length++; |
Phillip Wood | 2034b47 | 2018-11-23 11:16:54 +0000 | [diff] [blame] | 1241 | l->flags |= DIFF_SYMBOL_MOVED_LINE; |
| 1242 | if (flipped_block && o->color_moved != COLOR_MOVED_BLOCKS) |
| 1243 | l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT; |
| 1244 | } |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1245 | } |
Jonathan Tan | 0915327 | 2017-08-15 18:27:38 -0700 | [diff] [blame] | 1246 | adjust_last_block(o, n, block_length); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1247 | |
| 1248 | free(pmb); |
| 1249 | } |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1250 | |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1251 | static void dim_moved_lines(struct diff_options *o) |
| 1252 | { |
| 1253 | int n; |
| 1254 | for (n = 0; n < o->emitted_symbols->nr; n++) { |
| 1255 | struct emitted_diff_symbol *prev = (n != 0) ? |
| 1256 | &o->emitted_symbols->buf[n - 1] : NULL; |
| 1257 | struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n]; |
| 1258 | struct emitted_diff_symbol *next = |
| 1259 | (n < o->emitted_symbols->nr - 1) ? |
| 1260 | &o->emitted_symbols->buf[n + 1] : NULL; |
| 1261 | |
| 1262 | /* Not a plus or minus line? */ |
| 1263 | if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS) |
| 1264 | continue; |
| 1265 | |
| 1266 | /* Not a moved line? */ |
| 1267 | if (!(l->flags & DIFF_SYMBOL_MOVED_LINE)) |
| 1268 | continue; |
| 1269 | |
| 1270 | /* |
| 1271 | * If prev or next are not a plus or minus line, |
| 1272 | * pretend they don't exist |
| 1273 | */ |
| 1274 | if (prev && prev->s != DIFF_SYMBOL_PLUS && |
| 1275 | prev->s != DIFF_SYMBOL_MINUS) |
| 1276 | prev = NULL; |
| 1277 | if (next && next->s != DIFF_SYMBOL_PLUS && |
| 1278 | next->s != DIFF_SYMBOL_MINUS) |
| 1279 | next = NULL; |
| 1280 | |
| 1281 | /* Inside a block? */ |
| 1282 | if ((prev && |
| 1283 | (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) == |
| 1284 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) && |
| 1285 | (next && |
| 1286 | (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) == |
| 1287 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) { |
| 1288 | l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING; |
| 1289 | continue; |
| 1290 | } |
| 1291 | |
| 1292 | /* Check if we are at an interesting bound: */ |
| 1293 | if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) && |
| 1294 | (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) != |
| 1295 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT)) |
| 1296 | continue; |
| 1297 | if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) && |
| 1298 | (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) != |
| 1299 | (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT)) |
| 1300 | continue; |
| 1301 | |
| 1302 | /* |
| 1303 | * The boundary to prev and next are not interesting, |
| 1304 | * so this line is not interesting as a whole |
| 1305 | */ |
| 1306 | l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING; |
| 1307 | } |
| 1308 | } |
| 1309 | |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1310 | static void emit_line_ws_markup(struct diff_options *o, |
Stefan Beller | 9d1e16b | 2018-08-13 18:41:18 -0700 | [diff] [blame] | 1311 | const char *set_sign, const char *set, |
| 1312 | const char *reset, |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1313 | int sign_index, const char *line, int len, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1314 | unsigned ws_rule, int blank_at_eof) |
| 1315 | { |
| 1316 | const char *ws = NULL; |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1317 | int sign = o->output_indicators[sign_index]; |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1318 | |
| 1319 | if (o->ws_error_highlight & ws_rule) { |
| 1320 | ws = diff_get_color_opt(o, DIFF_WHITESPACE); |
| 1321 | if (!*ws) |
| 1322 | ws = NULL; |
| 1323 | } |
| 1324 | |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1325 | if (!ws && !set_sign) |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 1326 | emit_line_0(o, set, NULL, 0, reset, sign, line, len); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1327 | else if (!ws) { |
Stefan Beller | 29ef759 | 2018-08-13 18:41:20 -0700 | [diff] [blame] | 1328 | 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] | 1329 | } else if (blank_at_eof) |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1330 | /* Blank line at EOF - paint '+' as well */ |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 1331 | emit_line_0(o, ws, NULL, 0, reset, sign, line, len); |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1332 | else { |
| 1333 | /* Emit just the prefix, then the rest. */ |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 1334 | 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] | 1335 | sign, "", 0); |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1336 | ws_check_emit(line, len, ws_rule, |
| 1337 | o->file, set, reset, ws); |
| 1338 | } |
| 1339 | } |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1340 | |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1341 | static void emit_diff_symbol_from_struct(struct diff_options *o, |
| 1342 | struct emitted_diff_symbol *eds) |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1343 | { |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 1344 | static const char *nneof = " No newline at end of file\n"; |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1345 | const char *context, *reset, *set, *set_sign, *meta, *fraginfo; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1346 | |
| 1347 | enum diff_symbol s = eds->s; |
| 1348 | const char *line = eds->line; |
| 1349 | int len = eds->len; |
| 1350 | unsigned flags = eds->flags; |
| 1351 | |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1352 | switch (s) { |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 1353 | case DIFF_SYMBOL_NO_LF_EOF: |
| 1354 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1355 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1356 | putc('\n', o->file); |
Stefan Beller | 017ac45 | 2018-08-13 18:41:19 -0700 | [diff] [blame] | 1357 | emit_line_0(o, context, NULL, 0, reset, '\\', |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 1358 | nneof, strlen(nneof)); |
| 1359 | break; |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 1360 | case DIFF_SYMBOL_SUBMODULE_HEADER: |
| 1361 | case DIFF_SYMBOL_SUBMODULE_ERROR: |
| 1362 | case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH: |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 1363 | case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 1364 | case DIFF_SYMBOL_SUMMARY: |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 1365 | case DIFF_SYMBOL_STATS_LINE: |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 1366 | case DIFF_SYMBOL_BINARY_DIFF_BODY: |
Stefan Beller | 68abc6f | 2017-06-29 17:06:51 -0700 | [diff] [blame] | 1367 | case DIFF_SYMBOL_CONTEXT_FRAGINFO: |
| 1368 | emit_line(o, "", "", line, len); |
| 1369 | break; |
Stefan Beller | f2bb121 | 2017-06-29 17:06:55 -0700 | [diff] [blame] | 1370 | case DIFF_SYMBOL_CONTEXT_INCOMPLETE: |
Stefan Beller | c64b420 | 2017-06-29 17:06:50 -0700 | [diff] [blame] | 1371 | case DIFF_SYMBOL_CONTEXT_MARKER: |
| 1372 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1373 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1374 | emit_line(o, context, reset, line, len); |
| 1375 | break; |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1376 | case DIFF_SYMBOL_SEPARATOR: |
| 1377 | fprintf(o->file, "%s%c", |
| 1378 | diff_line_prefix(o), |
| 1379 | o->line_termination); |
| 1380 | break; |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1381 | case DIFF_SYMBOL_CONTEXT: |
| 1382 | set = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1383 | reset = diff_get_color_opt(o, DIFF_RESET); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1384 | set_sign = NULL; |
| 1385 | if (o->flags.dual_color_diffed_diffs) { |
| 1386 | char c = !len ? 0 : line[0]; |
| 1387 | |
| 1388 | if (c == '+') |
| 1389 | set = diff_get_color_opt(o, DIFF_FILE_NEW); |
| 1390 | else if (c == '@') |
| 1391 | set = diff_get_color_opt(o, DIFF_FRAGINFO); |
| 1392 | else if (c == '-') |
| 1393 | set = diff_get_color_opt(o, DIFF_FILE_OLD); |
| 1394 | } |
Stefan Beller | 7648b79 | 2018-08-17 13:43:52 -0700 | [diff] [blame] | 1395 | emit_line_ws_markup(o, set_sign, set, reset, |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1396 | OUTPUT_INDICATOR_CONTEXT, line, len, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1397 | flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0); |
| 1398 | break; |
| 1399 | case DIFF_SYMBOL_PLUS: |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1400 | switch (flags & (DIFF_SYMBOL_MOVED_LINE | |
| 1401 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1402 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) { |
| 1403 | case DIFF_SYMBOL_MOVED_LINE | |
| 1404 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1405 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1406 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM); |
| 1407 | break; |
| 1408 | case DIFF_SYMBOL_MOVED_LINE | |
| 1409 | DIFF_SYMBOL_MOVED_LINE_ALT: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1410 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1411 | break; |
| 1412 | case DIFF_SYMBOL_MOVED_LINE | |
| 1413 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1414 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM); |
| 1415 | break; |
| 1416 | case DIFF_SYMBOL_MOVED_LINE: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1417 | set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1418 | break; |
| 1419 | default: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1420 | set = diff_get_color_opt(o, DIFF_FILE_NEW); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1421 | } |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1422 | reset = diff_get_color_opt(o, DIFF_RESET); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1423 | if (!o->flags.dual_color_diffed_diffs) |
| 1424 | set_sign = NULL; |
| 1425 | else { |
| 1426 | char c = !len ? 0 : line[0]; |
| 1427 | |
| 1428 | set_sign = set; |
| 1429 | if (c == '-') |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 1430 | set = diff_get_color_opt(o, DIFF_FILE_OLD_BOLD); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1431 | else if (c == '@') |
| 1432 | set = diff_get_color_opt(o, DIFF_FRAGINFO); |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 1433 | else if (c == '+') |
| 1434 | set = diff_get_color_opt(o, DIFF_FILE_NEW_BOLD); |
| 1435 | else |
| 1436 | set = diff_get_color_opt(o, DIFF_CONTEXT_BOLD); |
Johannes Schindelin | 0b91faa | 2018-08-13 04:33:24 -0700 | [diff] [blame] | 1437 | flags &= ~DIFF_SYMBOL_CONTENT_WS_MASK; |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1438 | } |
Stefan Beller | 7648b79 | 2018-08-17 13:43:52 -0700 | [diff] [blame] | 1439 | emit_line_ws_markup(o, set_sign, set, reset, |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1440 | OUTPUT_INDICATOR_NEW, line, len, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1441 | flags & DIFF_SYMBOL_CONTENT_WS_MASK, |
| 1442 | flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF); |
| 1443 | break; |
| 1444 | case DIFF_SYMBOL_MINUS: |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1445 | switch (flags & (DIFF_SYMBOL_MOVED_LINE | |
| 1446 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1447 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) { |
| 1448 | case DIFF_SYMBOL_MOVED_LINE | |
| 1449 | DIFF_SYMBOL_MOVED_LINE_ALT | |
| 1450 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1451 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM); |
| 1452 | break; |
| 1453 | case DIFF_SYMBOL_MOVED_LINE | |
| 1454 | DIFF_SYMBOL_MOVED_LINE_ALT: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1455 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1456 | break; |
| 1457 | case DIFF_SYMBOL_MOVED_LINE | |
| 1458 | DIFF_SYMBOL_MOVED_LINE_UNINTERESTING: |
| 1459 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM); |
| 1460 | break; |
| 1461 | case DIFF_SYMBOL_MOVED_LINE: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1462 | set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1463 | break; |
| 1464 | default: |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 1465 | set = diff_get_color_opt(o, DIFF_FILE_OLD); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 1466 | } |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1467 | reset = diff_get_color_opt(o, DIFF_RESET); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1468 | if (!o->flags.dual_color_diffed_diffs) |
| 1469 | set_sign = NULL; |
| 1470 | else { |
| 1471 | char c = !len ? 0 : line[0]; |
| 1472 | |
| 1473 | set_sign = set; |
| 1474 | if (c == '+') |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 1475 | set = diff_get_color_opt(o, DIFF_FILE_NEW_DIM); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1476 | else if (c == '@') |
| 1477 | set = diff_get_color_opt(o, DIFF_FRAGINFO); |
Johannes Schindelin | a7be92a | 2018-08-13 04:33:32 -0700 | [diff] [blame] | 1478 | else if (c == '-') |
| 1479 | set = diff_get_color_opt(o, DIFF_FILE_OLD_DIM); |
| 1480 | else |
| 1481 | set = diff_get_color_opt(o, DIFF_CONTEXT_DIM); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1482 | } |
Stefan Beller | 7648b79 | 2018-08-17 13:43:52 -0700 | [diff] [blame] | 1483 | emit_line_ws_markup(o, set_sign, set, reset, |
Stefan Beller | bc9feb0 | 2018-10-10 16:24:59 -0700 | [diff] [blame] | 1484 | OUTPUT_INDICATOR_OLD, line, len, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1485 | flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0); |
| 1486 | break; |
Stefan Beller | ff95867 | 2017-06-29 17:06:54 -0700 | [diff] [blame] | 1487 | case DIFF_SYMBOL_WORDS_PORCELAIN: |
| 1488 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1489 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1490 | emit_line(o, context, reset, line, len); |
| 1491 | fputs("~\n", o->file); |
| 1492 | break; |
| 1493 | case DIFF_SYMBOL_WORDS: |
| 1494 | context = diff_get_color_opt(o, DIFF_CONTEXT); |
| 1495 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1496 | /* |
| 1497 | * Skip the prefix character, if any. With |
| 1498 | * diff_suppress_blank_empty, there may be |
| 1499 | * none. |
| 1500 | */ |
| 1501 | if (line[0] != '\n') { |
| 1502 | line++; |
| 1503 | len--; |
| 1504 | } |
| 1505 | emit_line(o, context, reset, line, len); |
| 1506 | break; |
Stefan Beller | 3ee8b7b | 2017-06-29 17:06:56 -0700 | [diff] [blame] | 1507 | case DIFF_SYMBOL_FILEPAIR_PLUS: |
| 1508 | meta = diff_get_color_opt(o, DIFF_METAINFO); |
| 1509 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1510 | fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta, |
| 1511 | line, reset, |
| 1512 | strchr(line, ' ') ? "\t" : ""); |
| 1513 | break; |
| 1514 | case DIFF_SYMBOL_FILEPAIR_MINUS: |
| 1515 | meta = diff_get_color_opt(o, DIFF_METAINFO); |
| 1516 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1517 | fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta, |
| 1518 | line, reset, |
| 1519 | strchr(line, ' ') ? "\t" : ""); |
| 1520 | break; |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 1521 | case DIFF_SYMBOL_BINARY_FILES: |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 1522 | case DIFF_SYMBOL_HEADER: |
| 1523 | fprintf(o->file, "%s", line); |
| 1524 | break; |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 1525 | case DIFF_SYMBOL_BINARY_DIFF_HEADER: |
| 1526 | fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o)); |
| 1527 | break; |
| 1528 | case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA: |
| 1529 | fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line); |
| 1530 | break; |
| 1531 | case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL: |
| 1532 | fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line); |
| 1533 | break; |
| 1534 | case DIFF_SYMBOL_BINARY_DIFF_FOOTER: |
| 1535 | fputs(diff_line_prefix(o), o->file); |
| 1536 | fputc('\n', o->file); |
| 1537 | break; |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1538 | case DIFF_SYMBOL_REWRITE_DIFF: |
| 1539 | fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO); |
| 1540 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1541 | emit_line(o, fraginfo, reset, line, len); |
| 1542 | break; |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 1543 | case DIFF_SYMBOL_SUBMODULE_ADD: |
| 1544 | set = diff_get_color_opt(o, DIFF_FILE_NEW); |
| 1545 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1546 | emit_line(o, set, reset, line, len); |
| 1547 | break; |
| 1548 | case DIFF_SYMBOL_SUBMODULE_DEL: |
| 1549 | set = diff_get_color_opt(o, DIFF_FILE_OLD); |
| 1550 | reset = diff_get_color_opt(o, DIFF_RESET); |
| 1551 | emit_line(o, set, reset, line, len); |
| 1552 | break; |
| 1553 | case DIFF_SYMBOL_SUBMODULE_UNTRACKED: |
| 1554 | fprintf(o->file, "%sSubmodule %s contains untracked content\n", |
| 1555 | diff_line_prefix(o), line); |
| 1556 | break; |
| 1557 | case DIFF_SYMBOL_SUBMODULE_MODIFIED: |
| 1558 | fprintf(o->file, "%sSubmodule %s contains modified content\n", |
| 1559 | diff_line_prefix(o), line); |
| 1560 | break; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 1561 | case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES: |
| 1562 | emit_line(o, "", "", " 0 files changed\n", |
| 1563 | strlen(" 0 files changed\n")); |
| 1564 | break; |
| 1565 | case DIFF_SYMBOL_STATS_SUMMARY_ABBREV: |
| 1566 | emit_line(o, "", "", " ...\n", strlen(" ...\n")); |
| 1567 | break; |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1568 | case DIFF_SYMBOL_WORD_DIFF: |
| 1569 | fprintf(o->file, "%.*s", len, line); |
| 1570 | break; |
Stefan Beller | 30b7e1e | 2017-06-29 17:07:04 -0700 | [diff] [blame] | 1571 | case DIFF_SYMBOL_STAT_SEP: |
| 1572 | fputs(o->stat_sep, o->file); |
| 1573 | break; |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1574 | default: |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 1575 | BUG("unknown diff symbol"); |
Stefan Beller | 36a4cef | 2017-06-29 17:06:49 -0700 | [diff] [blame] | 1576 | } |
| 1577 | } |
| 1578 | |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1579 | static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s, |
| 1580 | const char *line, int len, unsigned flags) |
| 1581 | { |
Phillip Wood | b4a5c5c | 2021-12-09 10:30:08 +0000 | [diff] [blame] | 1582 | struct emitted_diff_symbol e = { |
| 1583 | .line = line, .len = len, .flags = flags, .s = s |
| 1584 | }; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 1585 | |
| 1586 | if (o->emitted_symbols) |
| 1587 | append_emitted_diff_symbol(o, &e); |
| 1588 | else |
| 1589 | emit_diff_symbol_from_struct(o, &e); |
| 1590 | } |
| 1591 | |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 1592 | void diff_emit_submodule_del(struct diff_options *o, const char *line) |
| 1593 | { |
| 1594 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0); |
| 1595 | } |
| 1596 | |
| 1597 | void diff_emit_submodule_add(struct diff_options *o, const char *line) |
| 1598 | { |
| 1599 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0); |
| 1600 | } |
| 1601 | |
| 1602 | void diff_emit_submodule_untracked(struct diff_options *o, const char *path) |
| 1603 | { |
| 1604 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED, |
| 1605 | path, strlen(path), 0); |
| 1606 | } |
| 1607 | |
| 1608 | void diff_emit_submodule_modified(struct diff_options *o, const char *path) |
| 1609 | { |
| 1610 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED, |
| 1611 | path, strlen(path), 0); |
| 1612 | } |
| 1613 | |
| 1614 | void diff_emit_submodule_header(struct diff_options *o, const char *header) |
| 1615 | { |
| 1616 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER, |
| 1617 | header, strlen(header), 0); |
| 1618 | } |
| 1619 | |
| 1620 | void diff_emit_submodule_error(struct diff_options *o, const char *err) |
| 1621 | { |
| 1622 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0); |
| 1623 | } |
| 1624 | |
| 1625 | void diff_emit_submodule_pipethrough(struct diff_options *o, |
| 1626 | const char *line, int len) |
| 1627 | { |
| 1628 | emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0); |
| 1629 | } |
| 1630 | |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1631 | static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len) |
| 1632 | { |
| 1633 | if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) && |
| 1634 | ecbdata->blank_at_eof_in_preimage && |
| 1635 | ecbdata->blank_at_eof_in_postimage && |
| 1636 | ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage && |
| 1637 | ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage)) |
| 1638 | return 0; |
Jeff King | c5224f0 | 2022-12-13 06:12:58 -0500 | [diff] [blame] | 1639 | return ws_blank_line(line, len); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1640 | } |
| 1641 | |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1642 | static void emit_add_line(struct emit_callback *ecbdata, |
Junio C Hamano | 018cff7 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1643 | const char *line, int len) |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1644 | { |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1645 | unsigned flags = WSEH_NEW | ecbdata->ws_rule; |
| 1646 | if (new_blank_line_at_eof(ecbdata, line, len)) |
| 1647 | flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF; |
| 1648 | |
| 1649 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1650 | } |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1651 | |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1652 | static void emit_del_line(struct emit_callback *ecbdata, |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 1653 | const char *line, int len) |
| 1654 | { |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1655 | unsigned flags = WSEH_OLD | ecbdata->ws_rule; |
| 1656 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1659 | static void emit_context_line(struct emit_callback *ecbdata, |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 1660 | const char *line, int len) |
| 1661 | { |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1662 | unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule; |
| 1663 | emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags); |
Junio C Hamano | 6957eb9 | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1666 | static void emit_hunk_header(struct emit_callback *ecbdata, |
| 1667 | const char *line, int len) |
| 1668 | { |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 1669 | const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT); |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1670 | const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO); |
| 1671 | const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO); |
| 1672 | const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET); |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1673 | const char *reverse = ecbdata->color_diff ? GIT_COLOR_REVERSE : ""; |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1674 | static const char atat[2] = { '@', '@' }; |
| 1675 | const char *cp, *ep; |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1676 | struct strbuf msgbuf = STRBUF_INIT; |
| 1677 | int org_len = len; |
| 1678 | int i = 1; |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1679 | |
| 1680 | /* |
| 1681 | * As a hunk header must begin with "@@ -<old>, +<new> @@", |
| 1682 | * it always is at least 10 bytes long. |
| 1683 | */ |
| 1684 | if (len < 10 || |
| 1685 | memcmp(line, atat, 2) || |
| 1686 | !(ep = memmem(line + 2, len - 2, atat, 2))) { |
Stefan Beller | c64b420 | 2017-06-29 17:06:50 -0700 | [diff] [blame] | 1687 | emit_diff_symbol(ecbdata->opt, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1688 | DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0); |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1689 | return; |
| 1690 | } |
| 1691 | ep += 2; /* skip over @@ */ |
| 1692 | |
| 1693 | /* The hunk header in fraginfo color */ |
Johannes Schindelin | f7c3b4e | 2018-08-13 04:33:20 -0700 | [diff] [blame] | 1694 | if (ecbdata->opt->flags.dual_color_diffed_diffs) |
| 1695 | strbuf_addstr(&msgbuf, reverse); |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1696 | strbuf_addstr(&msgbuf, frag); |
Thomas Gummerer | 430be36 | 2019-07-11 17:08:48 +0100 | [diff] [blame] | 1697 | if (ecbdata->opt->flags.suppress_hunk_header_line_count) |
| 1698 | strbuf_add(&msgbuf, atat, sizeof(atat)); |
| 1699 | else |
| 1700 | strbuf_add(&msgbuf, line, ep - line); |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1701 | strbuf_addstr(&msgbuf, reset); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1702 | |
| 1703 | /* |
| 1704 | * trailing "\r\n" |
| 1705 | */ |
| 1706 | for ( ; i < 3; i++) |
| 1707 | if (line[len - i] == '\r' || line[len - i] == '\n') |
| 1708 | len--; |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1709 | |
| 1710 | /* blank before the func header */ |
| 1711 | for (cp = ep; ep - line < len; ep++) |
| 1712 | if (*ep != ' ' && *ep != '\t') |
| 1713 | break; |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1714 | if (ep != cp) { |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 1715 | strbuf_addstr(&msgbuf, context); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1716 | strbuf_add(&msgbuf, cp, ep - cp); |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1717 | strbuf_addstr(&msgbuf, reset); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1718 | } |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1719 | |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1720 | if (ep < line + len) { |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1721 | strbuf_addstr(&msgbuf, func); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1722 | strbuf_add(&msgbuf, ep, line + len - ep); |
René Scharfe | cedc61a | 2014-07-17 01:38:18 +0200 | [diff] [blame] | 1723 | strbuf_addstr(&msgbuf, reset); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | strbuf_add(&msgbuf, line + len, org_len - len); |
Stefan Beller | dfb7728 | 2017-06-29 17:06:47 -0700 | [diff] [blame] | 1727 | strbuf_complete_line(&msgbuf); |
Stefan Beller | 68abc6f | 2017-06-29 17:06:51 -0700 | [diff] [blame] | 1728 | emit_diff_symbol(ecbdata->opt, |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1729 | DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0); |
Bo Yang | 2efcc97 | 2010-05-29 23:32:05 +0800 | [diff] [blame] | 1730 | strbuf_release(&msgbuf); |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 1731 | } |
| 1732 | |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 1733 | static struct diff_tempfile *claim_diff_tempfile(void) |
| 1734 | { |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 1735 | int i; |
| 1736 | for (i = 0; i < ARRAY_SIZE(diff_temp); i++) |
| 1737 | if (!diff_temp[i].name) |
| 1738 | return diff_temp + i; |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 1739 | BUG("diff is failing to clean up its tempfiles"); |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 1740 | } |
| 1741 | |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 1742 | static void remove_tempfile(void) |
| 1743 | { |
| 1744 | int i; |
Nazri Ramliy | a8344ab | 2009-02-12 21:36:14 +0800 | [diff] [blame] | 1745 | for (i = 0; i < ARRAY_SIZE(diff_temp); i++) { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 1746 | if (is_tempfile_active(diff_temp[i].tempfile)) |
Michael Haggerty | 284098f | 2015-08-12 19:12:01 +0200 | [diff] [blame] | 1747 | delete_tempfile(&diff_temp[i].tempfile); |
Nazri Ramliy | a8344ab | 2009-02-12 21:36:14 +0800 | [diff] [blame] | 1748 | diff_temp[i].name = NULL; |
| 1749 | } |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 1750 | } |
| 1751 | |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1752 | static void add_line_count(struct strbuf *out, int count) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1753 | { |
| 1754 | switch (count) { |
| 1755 | case 0: |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1756 | strbuf_addstr(out, "0,0"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1757 | break; |
| 1758 | case 1: |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1759 | strbuf_addstr(out, "1"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1760 | break; |
| 1761 | default: |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1762 | strbuf_addf(out, "1,%d", count); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1763 | break; |
| 1764 | } |
| 1765 | } |
| 1766 | |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1767 | static void emit_rewrite_lines(struct emit_callback *ecb, |
| 1768 | int prefix, const char *data, int size) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1769 | { |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1770 | const char *endp = NULL; |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1771 | |
| 1772 | while (0 < size) { |
| 1773 | int len; |
| 1774 | |
| 1775 | endp = memchr(data, '\n', size); |
| 1776 | len = endp ? (endp - data + 1) : size; |
| 1777 | if (prefix != '+') { |
| 1778 | ecb->lno_in_preimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1779 | emit_del_line(ecb, data, len); |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1780 | } else { |
| 1781 | ecb->lno_in_postimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 1782 | emit_add_line(ecb, data, len); |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 1783 | } |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1784 | size -= len; |
| 1785 | data += len; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1786 | } |
Stefan Beller | b9cbfde | 2017-06-29 17:06:52 -0700 | [diff] [blame] | 1787 | if (!endp) |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 1788 | 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] | 1789 | } |
| 1790 | |
| 1791 | static void emit_rewrite_diff(const char *name_a, |
| 1792 | const char *name_b, |
| 1793 | struct diff_filespec *one, |
Johannes Schindelin | 13e36ec | 2007-02-20 15:08:46 +0100 | [diff] [blame] | 1794 | struct diff_filespec *two, |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 1795 | struct userdiff_driver *textconv_one, |
| 1796 | struct userdiff_driver *textconv_two, |
Johannes Schindelin | eab9a40 | 2007-12-18 19:32:14 +0000 | [diff] [blame] | 1797 | struct diff_options *o) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1798 | { |
| 1799 | int lc_a, lc_b; |
Junio C Hamano | d562509 | 2007-12-26 17:13:36 -0800 | [diff] [blame] | 1800 | static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT; |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 1801 | const char *a_prefix, *b_prefix; |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 1802 | char *data_one, *data_two; |
Jeff King | 3aa1f7c | 2008-12-09 03:13:21 -0500 | [diff] [blame] | 1803 | size_t size_one, size_two; |
Junio C Hamano | 7f7ee2f | 2009-09-14 18:44:01 -0700 | [diff] [blame] | 1804 | struct emit_callback ecbdata; |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1805 | struct strbuf out = STRBUF_INIT; |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 1806 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 1807 | if (diff_mnemonic_prefix && o->flags.reverse_diff) { |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 1808 | a_prefix = o->b_prefix; |
| 1809 | b_prefix = o->a_prefix; |
| 1810 | } else { |
| 1811 | a_prefix = o->a_prefix; |
| 1812 | b_prefix = o->b_prefix; |
| 1813 | } |
Junio C Hamano | 1a9eb3b | 2006-09-22 16:17:58 -0700 | [diff] [blame] | 1814 | |
Junio C Hamano | 8a13bec | 2007-02-24 01:42:06 -0800 | [diff] [blame] | 1815 | name_a += (*name_a == '/'); |
| 1816 | name_b += (*name_b == '/'); |
Junio C Hamano | 1a9eb3b | 2006-09-22 16:17:58 -0700 | [diff] [blame] | 1817 | |
Junio C Hamano | d562509 | 2007-12-26 17:13:36 -0800 | [diff] [blame] | 1818 | strbuf_reset(&a_name); |
| 1819 | strbuf_reset(&b_name); |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 1820 | quote_two_c_style(&a_name, a_prefix, name_a, 0); |
| 1821 | quote_two_c_style(&b_name, b_prefix, name_b, 0); |
Junio C Hamano | d562509 | 2007-12-26 17:13:36 -0800 | [diff] [blame] | 1822 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 1823 | size_one = fill_textconv(o->repo, textconv_one, one, &data_one); |
| 1824 | size_two = fill_textconv(o->repo, textconv_two, two, &data_two); |
Jeff King | 3aa1f7c | 2008-12-09 03:13:21 -0500 | [diff] [blame] | 1825 | |
Junio C Hamano | d91ba8f | 2009-09-15 11:21:10 -0700 | [diff] [blame] | 1826 | memset(&ecbdata, 0, sizeof(ecbdata)); |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 1827 | 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] | 1828 | ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b); |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 1829 | ecbdata.opt = o; |
Junio C Hamano | d91ba8f | 2009-09-15 11:21:10 -0700 | [diff] [blame] | 1830 | if (ecbdata.ws_rule & WS_BLANK_AT_EOF) { |
| 1831 | mmfile_t mf1, mf2; |
| 1832 | mf1.ptr = (char *)data_one; |
| 1833 | mf2.ptr = (char *)data_two; |
| 1834 | mf1.size = size_one; |
| 1835 | mf2.size = size_two; |
| 1836 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
| 1837 | } |
| 1838 | ecbdata.lno_in_preimage = 1; |
| 1839 | ecbdata.lno_in_postimage = 1; |
| 1840 | |
Jeff King | 3aa1f7c | 2008-12-09 03:13:21 -0500 | [diff] [blame] | 1841 | lc_a = count_lines(data_one, size_one); |
| 1842 | lc_b = count_lines(data_two, size_two); |
Stefan Beller | 3ee8b7b | 2017-06-29 17:06:56 -0700 | [diff] [blame] | 1843 | |
| 1844 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS, |
| 1845 | a_name.buf, a_name.len, 0); |
| 1846 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS, |
| 1847 | b_name.buf, b_name.len, 0); |
| 1848 | |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1849 | strbuf_addstr(&out, "@@ -"); |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 1850 | if (!o->irreversible_delete) |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1851 | add_line_count(&out, lc_a); |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 1852 | else |
Stefan Beller | 5af6ea9 | 2017-06-29 17:06:59 -0700 | [diff] [blame] | 1853 | strbuf_addstr(&out, "?,?"); |
| 1854 | strbuf_addstr(&out, " +"); |
| 1855 | add_line_count(&out, lc_b); |
| 1856 | strbuf_addstr(&out, " @@\n"); |
| 1857 | emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0); |
| 1858 | strbuf_release(&out); |
| 1859 | |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 1860 | if (lc_a && !o->irreversible_delete) |
Junio C Hamano | d91ba8f | 2009-09-15 11:21:10 -0700 | [diff] [blame] | 1861 | emit_rewrite_lines(&ecbdata, '-', data_one, size_one); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1862 | if (lc_b) |
Junio C Hamano | d91ba8f | 2009-09-15 11:21:10 -0700 | [diff] [blame] | 1863 | emit_rewrite_lines(&ecbdata, '+', data_two, size_two); |
Jeff King | b76c056 | 2010-04-01 20:04:14 -0400 | [diff] [blame] | 1864 | if (textconv_one) |
Junio C Hamano | aed6ca5 | 2010-04-08 23:30:49 -0700 | [diff] [blame] | 1865 | free((char *)data_one); |
Jeff King | b76c056 | 2010-04-01 20:04:14 -0400 | [diff] [blame] | 1866 | if (textconv_two) |
Junio C Hamano | aed6ca5 | 2010-04-08 23:30:49 -0700 | [diff] [blame] | 1867 | free((char *)data_two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1870 | struct diff_words_buffer { |
| 1871 | mmfile_t text; |
Ramsay Jones | 071bcaa | 2017-09-21 17:49:38 +0100 | [diff] [blame] | 1872 | unsigned long alloc; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 1873 | struct diff_words_orig { |
| 1874 | const char *begin, *end; |
| 1875 | } *orig; |
| 1876 | int orig_nr, orig_alloc; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1877 | }; |
| 1878 | |
| 1879 | static void diff_words_append(char *line, unsigned long len, |
| 1880 | struct diff_words_buffer *buffer) |
| 1881 | { |
Johannes Schindelin | 23c1575 | 2009-01-17 17:29:43 +0100 | [diff] [blame] | 1882 | ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc); |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1883 | line++; |
| 1884 | len--; |
| 1885 | memcpy(buffer->text.ptr + buffer->text.size, line, len); |
| 1886 | buffer->text.size += len; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 1887 | buffer->text.ptr[buffer->text.size] = '\0'; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1888 | } |
| 1889 | |
Jonathan Nieder | 9cba13c | 2011-03-16 02:08:34 -0500 | [diff] [blame] | 1890 | struct diff_words_style_elem { |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1891 | const char *prefix; |
| 1892 | const char *suffix; |
| 1893 | const char *color; /* NULL; filled in by the setup code if |
| 1894 | * color is enabled */ |
| 1895 | }; |
| 1896 | |
Jonathan Nieder | 9cba13c | 2011-03-16 02:08:34 -0500 | [diff] [blame] | 1897 | struct diff_words_style { |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1898 | enum diff_words_type type; |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 1899 | struct diff_words_style_elem new_word, old_word, ctx; |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1900 | const char *newline; |
| 1901 | }; |
| 1902 | |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 1903 | static struct diff_words_style diff_words_styles[] = { |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1904 | { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" }, |
| 1905 | { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" }, |
| 1906 | { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" } |
| 1907 | }; |
| 1908 | |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1909 | struct diff_words_data { |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1910 | struct diff_words_buffer minus, plus; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 1911 | const char *current_plus; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1912 | int last_minus; |
| 1913 | struct diff_options *opt; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 1914 | regex_t *word_regex; |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1915 | enum diff_words_type type; |
| 1916 | struct diff_words_style *style; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 1917 | }; |
| 1918 | |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1919 | static int fn_out_diff_words_write_helper(struct diff_options *o, |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1920 | struct diff_words_style_elem *st_el, |
| 1921 | const char *newline, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1922 | size_t count, const char *buf) |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1923 | { |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1924 | int print = 0; |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1925 | struct strbuf sb = STRBUF_INIT; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1926 | |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1927 | while (count) { |
| 1928 | char *p = memchr(buf, '\n', count); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1929 | if (print) |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1930 | strbuf_addstr(&sb, diff_line_prefix(o)); |
| 1931 | |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1932 | if (p != buf) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1933 | const char *reset = st_el->color && *st_el->color ? |
| 1934 | GIT_COLOR_RESET : NULL; |
| 1935 | if (st_el->color && *st_el->color) |
| 1936 | strbuf_addstr(&sb, st_el->color); |
| 1937 | strbuf_addstr(&sb, st_el->prefix); |
| 1938 | strbuf_add(&sb, buf, p ? p - buf : count); |
| 1939 | strbuf_addstr(&sb, st_el->suffix); |
| 1940 | if (reset) |
| 1941 | strbuf_addstr(&sb, reset); |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1942 | } |
| 1943 | if (!p) |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1944 | goto out; |
| 1945 | |
| 1946 | strbuf_addstr(&sb, newline); |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1947 | count -= p + 1 - buf; |
| 1948 | buf = p + 1; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1949 | print = 1; |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1950 | if (count) { |
| 1951 | emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF, |
| 1952 | sb.buf, sb.len, 0); |
| 1953 | strbuf_reset(&sb); |
| 1954 | } |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1955 | } |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 1956 | |
| 1957 | out: |
| 1958 | if (sb.len) |
| 1959 | emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF, |
| 1960 | sb.buf, sb.len, 0); |
| 1961 | strbuf_release(&sb); |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 1962 | return 0; |
| 1963 | } |
| 1964 | |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1965 | /* |
| 1966 | * '--color-words' algorithm can be described as: |
| 1967 | * |
René Genz | 5621760 | 2017-04-30 17:48:27 +0200 | [diff] [blame] | 1968 | * 1. collect the minus/plus lines of a diff hunk, divided into |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 1969 | * minus-lines and plus-lines; |
| 1970 | * |
| 1971 | * 2. break both minus-lines and plus-lines into words and |
| 1972 | * place them into two mmfile_t with one word for each line; |
| 1973 | * |
| 1974 | * 3. use xdiff to run diff on the two mmfile_t to get the words level diff; |
| 1975 | * |
| 1976 | * And for the common parts of the both file, we output the plus side text. |
| 1977 | * diff_words->current_plus is used to trace the current position of the plus file |
| 1978 | * which printed. diff_words->last_minus is used to trace the last minus word |
| 1979 | * printed. |
| 1980 | * |
| 1981 | * For '--graph' to work with '--color-words', we need to output the graph prefix |
| 1982 | * on each line of color words output. Generally, there are two conditions on |
| 1983 | * which we should output the prefix. |
| 1984 | * |
| 1985 | * 1. diff_words->last_minus == 0 && |
| 1986 | * diff_words->current_plus == diff_words->plus.text.ptr |
| 1987 | * |
| 1988 | * that is: the plus text must start as a new line, and if there is no minus |
| 1989 | * word printed, a graph prefix must be printed. |
| 1990 | * |
| 1991 | * 2. diff_words->current_plus > diff_words->plus.text.ptr && |
| 1992 | * *(diff_words->current_plus - 1) == '\n' |
| 1993 | * |
| 1994 | * that is: a graph prefix must be printed following a '\n' |
| 1995 | */ |
| 1996 | static int color_words_output_graph_prefix(struct diff_words_data *diff_words) |
| 1997 | { |
| 1998 | if ((diff_words->last_minus == 0 && |
| 1999 | diff_words->current_plus == diff_words->plus.text.ptr) || |
| 2000 | (diff_words->current_plus > diff_words->plus.text.ptr && |
| 2001 | *(diff_words->current_plus - 1) == '\n')) { |
| 2002 | return 1; |
| 2003 | } else { |
| 2004 | return 0; |
| 2005 | } |
| 2006 | } |
| 2007 | |
Jeff King | 7c61e25 | 2018-11-02 02:37:18 -0400 | [diff] [blame] | 2008 | static void fn_out_diff_words_aux(void *priv, |
| 2009 | long minus_first, long minus_len, |
| 2010 | long plus_first, long plus_len, |
Jeff King | 61bdc7c | 2022-12-13 06:13:48 -0500 | [diff] [blame] | 2011 | const char *func UNUSED, long funclen UNUSED) |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2012 | { |
| 2013 | struct diff_words_data *diff_words = priv; |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 2014 | struct diff_words_style *style = diff_words->style; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2015 | const char *minus_begin, *minus_end, *plus_begin, *plus_end; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2016 | struct diff_options *opt = diff_words->opt; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2017 | const char *line_prefix; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2018 | |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2019 | assert(opt); |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2020 | line_prefix = diff_line_prefix(opt); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2021 | |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2022 | /* POSIX requires that first be decremented by one if len == 0... */ |
| 2023 | if (minus_len) { |
| 2024 | minus_begin = diff_words->minus.orig[minus_first].begin; |
| 2025 | minus_end = |
| 2026 | diff_words->minus.orig[minus_first + minus_len - 1].end; |
| 2027 | } else |
| 2028 | minus_begin = minus_end = |
| 2029 | diff_words->minus.orig[minus_first].end; |
| 2030 | |
| 2031 | if (plus_len) { |
| 2032 | plus_begin = diff_words->plus.orig[plus_first].begin; |
| 2033 | plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end; |
| 2034 | } else |
| 2035 | plus_begin = plus_end = diff_words->plus.orig[plus_first].end; |
| 2036 | |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2037 | if (color_words_output_graph_prefix(diff_words)) { |
| 2038 | fputs(line_prefix, diff_words->opt->file); |
| 2039 | } |
| 2040 | if (diff_words->current_plus != plus_begin) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2041 | fn_out_diff_words_write_helper(diff_words->opt, |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 2042 | &style->ctx, style->newline, |
| 2043 | plus_begin - diff_words->current_plus, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2044 | diff_words->current_plus); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2045 | } |
| 2046 | if (minus_begin != minus_end) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2047 | fn_out_diff_words_write_helper(diff_words->opt, |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2048 | &style->old_word, style->newline, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2049 | minus_end - minus_begin, minus_begin); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2050 | } |
| 2051 | if (plus_begin != plus_end) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2052 | fn_out_diff_words_write_helper(diff_words->opt, |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2053 | &style->new_word, style->newline, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2054 | plus_end - plus_begin, plus_begin); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2055 | } |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2056 | |
| 2057 | diff_words->current_plus = plus_end; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2058 | diff_words->last_minus = minus_first; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2059 | } |
| 2060 | |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2061 | /* This function starts looking at *begin, and returns 0 iff a word was found. */ |
| 2062 | static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex, |
| 2063 | int *begin, int *end) |
| 2064 | { |
Phillip Wood | 0324e8f | 2021-05-04 09:27:34 +0000 | [diff] [blame] | 2065 | while (word_regex && *begin < buffer->size) { |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2066 | regmatch_t match[1]; |
Johannes Schindelin | b7d36ff | 2016-09-21 20:24:14 +0200 | [diff] [blame] | 2067 | if (!regexec_buf(word_regex, buffer->ptr + *begin, |
| 2068 | buffer->size - *begin, 1, match, 0)) { |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2069 | char *p = memchr(buffer->ptr + *begin + match[0].rm_so, |
| 2070 | '\n', match[0].rm_eo - match[0].rm_so); |
| 2071 | *end = p ? p - buffer->ptr : match[0].rm_eo + *begin; |
| 2072 | *begin += match[0].rm_so; |
Phillip Wood | 0324e8f | 2021-05-04 09:27:34 +0000 | [diff] [blame] | 2073 | if (*begin == *end) |
| 2074 | (*begin)++; |
| 2075 | else |
| 2076 | return *begin > *end; |
| 2077 | } else { |
| 2078 | return -1; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2079 | } |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2080 | } |
| 2081 | |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2082 | /* find the next word */ |
| 2083 | while (*begin < buffer->size && isspace(buffer->ptr[*begin])) |
| 2084 | (*begin)++; |
| 2085 | if (*begin >= buffer->size) |
| 2086 | return -1; |
| 2087 | |
| 2088 | /* find the end of the word */ |
| 2089 | *end = *begin + 1; |
| 2090 | while (*end < buffer->size && !isspace(buffer->ptr[*end])) |
| 2091 | (*end)++; |
| 2092 | |
| 2093 | return 0; |
| 2094 | } |
| 2095 | |
Johannes Schindelin | 23c1575 | 2009-01-17 17:29:43 +0100 | [diff] [blame] | 2096 | /* |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2097 | * This function splits the words in buffer->text, stores the list with |
| 2098 | * newline separator into out, and saves the offsets of the original words |
| 2099 | * in buffer->orig. |
Johannes Schindelin | 23c1575 | 2009-01-17 17:29:43 +0100 | [diff] [blame] | 2100 | */ |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2101 | static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out, |
| 2102 | regex_t *word_regex) |
Johannes Schindelin | 23c1575 | 2009-01-17 17:29:43 +0100 | [diff] [blame] | 2103 | { |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2104 | int i, j; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2105 | long alloc = 0; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2106 | |
| 2107 | out->size = 0; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2108 | out->ptr = NULL; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2109 | |
| 2110 | /* fake an empty "0th" word */ |
| 2111 | ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc); |
| 2112 | buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr; |
| 2113 | buffer->orig_nr = 1; |
| 2114 | |
| 2115 | for (i = 0; i < buffer->text.size; i++) { |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2116 | if (find_word_boundaries(&buffer->text, word_regex, &i, &j)) |
| 2117 | return; |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2118 | |
| 2119 | /* store original boundaries */ |
| 2120 | ALLOC_GROW(buffer->orig, buffer->orig_nr + 1, |
| 2121 | buffer->orig_alloc); |
| 2122 | buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i; |
| 2123 | buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j; |
| 2124 | buffer->orig_nr++; |
| 2125 | |
| 2126 | /* store one word */ |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2127 | ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2128 | memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i); |
| 2129 | out->ptr[out->size + j - i] = '\n'; |
| 2130 | out->size += j - i + 1; |
| 2131 | |
| 2132 | i = j - 1; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | /* this executes the word diff on the accumulated buffers */ |
| 2137 | static void diff_words_show(struct diff_words_data *diff_words) |
| 2138 | { |
| 2139 | xpparam_t xpp; |
| 2140 | xdemitconf_t xecfg; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2141 | mmfile_t minus, plus; |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 2142 | struct diff_words_style *style = diff_words->style; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2143 | |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2144 | struct diff_options *opt = diff_words->opt; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2145 | const char *line_prefix; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2146 | |
| 2147 | assert(opt); |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2148 | line_prefix = diff_line_prefix(opt); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2149 | |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2150 | /* special case: only removal */ |
| 2151 | if (!diff_words->plus.text.size) { |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2152 | emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF, |
| 2153 | line_prefix, strlen(line_prefix), 0); |
| 2154 | fn_out_diff_words_write_helper(diff_words->opt, |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2155 | &style->old_word, style->newline, |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2156 | diff_words->minus.text.size, |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2157 | diff_words->minus.text.ptr); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2158 | diff_words->minus.text.size = 0; |
| 2159 | return; |
| 2160 | } |
| 2161 | |
| 2162 | diff_words->current_plus = diff_words->plus.text.ptr; |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2163 | diff_words->last_minus = 0; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2164 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 2165 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 2166 | memset(&xecfg, 0, sizeof(xecfg)); |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2167 | diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex); |
| 2168 | diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex); |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 2169 | xpp.flags = 0; |
Johannes Schindelin | 2b6a541 | 2009-01-17 17:29:45 +0100 | [diff] [blame] | 2170 | /* 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] | 2171 | xecfg.ctxlen = 0; |
Jeff King | 7c61e25 | 2018-11-02 02:37:18 -0400 | [diff] [blame] | 2172 | if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, NULL, |
Jeff King | 9346d6d | 2018-11-02 02:35:45 -0400 | [diff] [blame] | 2173 | diff_words, &xpp, &xecfg)) |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 2174 | die("unable to generate word diff"); |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2175 | free(minus.ptr); |
| 2176 | free(plus.ptr); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2177 | if (diff_words->current_plus != diff_words->plus.text.ptr + |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2178 | diff_words->plus.text.size) { |
| 2179 | if (color_words_output_graph_prefix(diff_words)) |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2180 | emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF, |
| 2181 | line_prefix, strlen(line_prefix), 0); |
| 2182 | fn_out_diff_words_write_helper(diff_words->opt, |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 2183 | &style->ctx, style->newline, |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2184 | diff_words->plus.text.ptr + diff_words->plus.text.size |
Stefan Beller | bd03329 | 2017-06-29 17:07:03 -0700 | [diff] [blame] | 2185 | - diff_words->current_plus, diff_words->current_plus); |
Bo Yang | 4297c0a | 2010-05-29 23:32:06 +0800 | [diff] [blame] | 2186 | } |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2187 | diff_words->minus.text.size = diff_words->plus.text.size = 0; |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2188 | } |
| 2189 | |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2190 | /* In "color-words" mode, show word-diff of words accumulated in the buffer */ |
| 2191 | static void diff_words_flush(struct emit_callback *ecbdata) |
| 2192 | { |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 2193 | struct diff_options *wo = ecbdata->diff_words->opt; |
| 2194 | |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2195 | if (ecbdata->diff_words->minus.text.size || |
| 2196 | ecbdata->diff_words->plus.text.size) |
| 2197 | diff_words_show(ecbdata->diff_words); |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 2198 | |
| 2199 | if (wo->emitted_symbols) { |
| 2200 | struct diff_options *o = ecbdata->opt; |
| 2201 | struct emitted_diff_symbols *wol = wo->emitted_symbols; |
| 2202 | int i; |
| 2203 | |
| 2204 | /* |
| 2205 | * NEEDSWORK: |
| 2206 | * Instead of appending each, concat all words to a line? |
| 2207 | */ |
| 2208 | for (i = 0; i < wol->nr; i++) |
| 2209 | append_emitted_diff_symbol(o, &wol->buf[i]); |
| 2210 | |
| 2211 | for (i = 0; i < wol->nr; i++) |
| 2212 | free((void *)wol->buf[i].line); |
| 2213 | |
| 2214 | wol->nr = 0; |
| 2215 | } |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2218 | static void diff_filespec_load_driver(struct diff_filespec *one, |
| 2219 | struct index_state *istate) |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2220 | { |
| 2221 | /* Use already-loaded driver */ |
| 2222 | if (one->driver) |
| 2223 | return; |
| 2224 | |
| 2225 | if (S_ISREG(one->mode)) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2226 | one->driver = userdiff_find_by_path(istate, one->path); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2227 | |
| 2228 | /* Fallback to default settings */ |
| 2229 | if (!one->driver) |
| 2230 | one->driver = userdiff_find_by_name("default"); |
| 2231 | } |
| 2232 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2233 | static const char *userdiff_word_regex(struct diff_filespec *one, |
| 2234 | struct index_state *istate) |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2235 | { |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2236 | diff_filespec_load_driver(one, istate); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2237 | return one->driver->word_regex; |
| 2238 | } |
| 2239 | |
| 2240 | static void init_diff_words_data(struct emit_callback *ecbdata, |
Thomas Rast | 6440d34 | 2012-03-14 19:24:09 +0100 | [diff] [blame] | 2241 | struct diff_options *orig_opts, |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2242 | struct diff_filespec *one, |
| 2243 | struct diff_filespec *two) |
| 2244 | { |
| 2245 | int i; |
Thomas Rast | 6440d34 | 2012-03-14 19:24:09 +0100 | [diff] [blame] | 2246 | struct diff_options *o = xmalloc(sizeof(struct diff_options)); |
| 2247 | memcpy(o, orig_opts, sizeof(struct diff_options)); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2248 | |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 2249 | CALLOC_ARRAY(ecbdata->diff_words, 1); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2250 | ecbdata->diff_words->type = o->word_diff; |
| 2251 | ecbdata->diff_words->opt = o; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 2252 | |
| 2253 | if (orig_opts->emitted_symbols) |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 2254 | CALLOC_ARRAY(o->emitted_symbols, 1); |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 2255 | |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2256 | if (!o->word_regex) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2257 | o->word_regex = userdiff_word_regex(one, o->repo->index); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2258 | if (!o->word_regex) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 2259 | o->word_regex = userdiff_word_regex(two, o->repo->index); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2260 | if (!o->word_regex) |
| 2261 | o->word_regex = diff_word_regex_cfg; |
| 2262 | if (o->word_regex) { |
| 2263 | ecbdata->diff_words->word_regex = (regex_t *) |
| 2264 | xmalloc(sizeof(regex_t)); |
| 2265 | if (regcomp(ecbdata->diff_words->word_regex, |
| 2266 | o->word_regex, |
| 2267 | REG_EXTENDED | REG_NEWLINE)) |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 2268 | die("invalid regular expression: %s", |
| 2269 | o->word_regex); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2270 | } |
| 2271 | for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) { |
| 2272 | if (o->word_diff == diff_words_styles[i].type) { |
| 2273 | ecbdata->diff_words->style = |
| 2274 | &diff_words_styles[i]; |
| 2275 | break; |
| 2276 | } |
| 2277 | } |
| 2278 | if (want_color(o->use_color)) { |
| 2279 | struct diff_words_style *st = ecbdata->diff_words->style; |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2280 | st->old_word.color = diff_get_color_opt(o, DIFF_FILE_OLD); |
| 2281 | st->new_word.color = diff_get_color_opt(o, DIFF_FILE_NEW); |
Jeff King | 8dbf3eb | 2015-05-27 16:48:46 -0400 | [diff] [blame] | 2282 | st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT); |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 2283 | } |
| 2284 | } |
| 2285 | |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2286 | static void free_diff_words_data(struct emit_callback *ecbdata) |
| 2287 | { |
| 2288 | if (ecbdata->diff_words) { |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2289 | diff_words_flush(ecbdata); |
Ævar Arnfjörð Bjarmason | 77e56d5 | 2022-03-17 15:55:35 +0100 | [diff] [blame] | 2290 | free_emitted_diff_symbols(ecbdata->diff_words->opt->emitted_symbols); |
Thomas Rast | 6440d34 | 2012-03-14 19:24:09 +0100 | [diff] [blame] | 2291 | free (ecbdata->diff_words->opt); |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 2292 | free (ecbdata->diff_words->minus.text.ptr); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2293 | free (ecbdata->diff_words->minus.orig); |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 2294 | free (ecbdata->diff_words->plus.text.ptr); |
Johannes Schindelin | 2e5d200 | 2009-01-17 17:29:44 +0100 | [diff] [blame] | 2295 | free (ecbdata->diff_words->plus.orig); |
Brandon Casey | ef5644e | 2010-09-09 14:02:45 -0500 | [diff] [blame] | 2296 | if (ecbdata->diff_words->word_regex) { |
| 2297 | regfree(ecbdata->diff_words->word_regex); |
| 2298 | free(ecbdata->diff_words->word_regex); |
| 2299 | } |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 2300 | FREE_AND_NULL(ecbdata->diff_words); |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 2301 | } |
| 2302 | } |
| 2303 | |
Jeff King | ce43697 | 2006-07-23 05:24:18 -0400 | [diff] [blame] | 2304 | 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] | 2305 | { |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 2306 | if (want_color(diff_use_color)) |
Linus Torvalds | 50f575f | 2006-06-22 13:53:31 -0700 | [diff] [blame] | 2307 | return diff_colors[ix]; |
| 2308 | return ""; |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2309 | } |
| 2310 | |
John Keeping | f192223 | 2013-02-07 20:15:26 +0000 | [diff] [blame] | 2311 | const char *diff_line_prefix(struct diff_options *opt) |
| 2312 | { |
| 2313 | struct strbuf *msgbuf; |
| 2314 | if (!opt->output_prefix) |
| 2315 | return ""; |
| 2316 | |
| 2317 | msgbuf = opt->output_prefix(opt, opt->output_prefix_data); |
| 2318 | return msgbuf->buf; |
| 2319 | } |
| 2320 | |
Jeff King | 19b9046 | 2019-02-14 00:48:27 -0500 | [diff] [blame] | 2321 | static unsigned long sane_truncate_line(char *line, unsigned long len) |
Junio C Hamano | 2370781 | 2008-01-02 01:50:11 -0800 | [diff] [blame] | 2322 | { |
| 2323 | const char *cp; |
| 2324 | unsigned long allot; |
| 2325 | size_t l = len; |
| 2326 | |
Junio C Hamano | 2370781 | 2008-01-02 01:50:11 -0800 | [diff] [blame] | 2327 | cp = line; |
| 2328 | allot = l; |
| 2329 | while (0 < l) { |
| 2330 | (void) utf8_width(&cp, &l); |
| 2331 | if (!cp) |
| 2332 | break; /* truncated in the middle? */ |
| 2333 | } |
| 2334 | return allot - l; |
| 2335 | } |
| 2336 | |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2337 | static void find_lno(const char *line, struct emit_callback *ecbdata) |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 2338 | { |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2339 | const char *p; |
| 2340 | ecbdata->lno_in_preimage = 0; |
| 2341 | ecbdata->lno_in_postimage = 0; |
| 2342 | p = strchr(line, '-'); |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 2343 | if (!p) |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2344 | return; /* cannot happen */ |
| 2345 | ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10); |
| 2346 | p = strchr(p, '+'); |
| 2347 | if (!p) |
| 2348 | return; /* cannot happen */ |
| 2349 | ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10); |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 2350 | } |
| 2351 | |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2352 | 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] | 2353 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2354 | struct emit_callback *ecbdata = priv; |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 2355 | struct diff_options *o = ecbdata->opt; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2356 | |
Stefan Beller | ba16233 | 2016-09-07 16:36:46 -0700 | [diff] [blame] | 2357 | o->found_changes = 1; |
| 2358 | |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 2359 | if (ecbdata->header) { |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 2360 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 2361 | ecbdata->header->buf, ecbdata->header->len, 0); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 2362 | strbuf_reset(ecbdata->header); |
| 2363 | ecbdata->header = NULL; |
| 2364 | } |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 2365 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2366 | if (ecbdata->label_path[0]) { |
Stefan Beller | 3ee8b7b | 2017-06-29 17:06:56 -0700 | [diff] [blame] | 2367 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS, |
| 2368 | ecbdata->label_path[0], |
| 2369 | strlen(ecbdata->label_path[0]), 0); |
| 2370 | emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS, |
| 2371 | ecbdata->label_path[1], |
| 2372 | strlen(ecbdata->label_path[1]), 0); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2373 | ecbdata->label_path[0] = ecbdata->label_path[1] = NULL; |
| 2374 | } |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2375 | |
Jim Meyering | a624eaa | 2008-08-15 13:39:26 +0200 | [diff] [blame] | 2376 | if (diff_suppress_blank_empty |
| 2377 | && len == 2 && line[0] == ' ' && line[1] == '\n') { |
| 2378 | line[0] = '\n'; |
| 2379 | len = 1; |
| 2380 | } |
| 2381 | |
Junio C Hamano | b8d9c1a | 2009-09-03 23:59:25 -0700 | [diff] [blame] | 2382 | if (line[0] == '@') { |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2383 | if (ecbdata->diff_words) |
| 2384 | diff_words_flush(ecbdata); |
Jeff King | 19b9046 | 2019-02-14 00:48:27 -0500 | [diff] [blame] | 2385 | len = sane_truncate_line(line, len); |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2386 | find_lno(line, ecbdata); |
Bert Wesarg | 89cb73a | 2009-11-27 07:55:18 +0100 | [diff] [blame] | 2387 | emit_hunk_header(ecbdata, line, len); |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2388 | return 0; |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 2389 | } |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2390 | |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2391 | if (ecbdata->diff_words) { |
Stefan Beller | ff95867 | 2017-06-29 17:06:54 -0700 | [diff] [blame] | 2392 | enum diff_symbol s = |
| 2393 | ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ? |
| 2394 | DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2395 | if (line[0] == '-') { |
| 2396 | diff_words_append(line, len, |
| 2397 | &ecbdata->diff_words->minus); |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2398 | return 0; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2399 | } else if (line[0] == '+') { |
| 2400 | diff_words_append(line, len, |
| 2401 | &ecbdata->diff_words->plus); |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2402 | return 0; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 2403 | } else if (starts_with(line, "\\ ")) { |
Thomas Rast | c7c2bc0 | 2012-01-12 12:15:33 +0100 | [diff] [blame] | 2404 | /* |
| 2405 | * Eat the "no newline at eof" marker as if we |
| 2406 | * saw a "+" or "-" line with nothing on it, |
| 2407 | * and return without diff_words_flush() to |
| 2408 | * defer processing. If this is the end of |
| 2409 | * preimage, more "+" lines may come after it. |
| 2410 | */ |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2411 | return 0; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2412 | } |
Junio C Hamano | 76fd282 | 2009-10-30 10:09:06 -0700 | [diff] [blame] | 2413 | diff_words_flush(ecbdata); |
Stefan Beller | ff95867 | 2017-06-29 17:06:54 -0700 | [diff] [blame] | 2414 | emit_diff_symbol(o, s, line, len, 0); |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2415 | return 0; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2416 | } |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2417 | |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2418 | switch (line[0]) { |
| 2419 | case '+': |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 2420 | ecbdata->lno_in_postimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 2421 | emit_add_line(ecbdata, line + 1, len - 1); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2422 | break; |
| 2423 | case '-': |
| 2424 | ecbdata->lno_in_preimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 2425 | emit_del_line(ecbdata, line + 1, len - 1); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2426 | break; |
| 2427 | case ' ': |
| 2428 | ecbdata->lno_in_postimage++; |
| 2429 | ecbdata->lno_in_preimage++; |
Jeff King | e04df61 | 2019-02-14 00:48:13 -0500 | [diff] [blame] | 2430 | emit_context_line(ecbdata, line + 1, len - 1); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2431 | break; |
| 2432 | default: |
| 2433 | /* incomplete line at the end */ |
| 2434 | ecbdata->lno_in_preimage++; |
Stefan Beller | f2bb121 | 2017-06-29 17:06:55 -0700 | [diff] [blame] | 2435 | emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE, |
| 2436 | line, len, 0); |
Junio C Hamano | 0e383e1 | 2015-05-26 09:56:33 -0700 | [diff] [blame] | 2437 | break; |
Junio C Hamano | 448c3ef | 2006-09-22 22:48:39 -0700 | [diff] [blame] | 2438 | } |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2439 | return 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2440 | } |
| 2441 | |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2442 | 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] | 2443 | { |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2444 | const char *old_name = a; |
| 2445 | const char *new_name = b; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2446 | int pfx_length, sfx_length; |
Thomas Rast | dd281f0 | 2013-02-26 21:47:01 +0100 | [diff] [blame] | 2447 | int pfx_adjust_for_slash; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2448 | int len_a = strlen(a); |
| 2449 | int len_b = strlen(b); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2450 | int a_midlen, b_midlen; |
Alexandre Julliard | e5bfbf9 | 2007-02-10 15:39:00 +0100 | [diff] [blame] | 2451 | int qlen_a = quote_c_style(a, NULL, NULL, 0); |
| 2452 | int qlen_b = quote_c_style(b, NULL, NULL, 0); |
| 2453 | |
| 2454 | if (qlen_a || qlen_b) { |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2455 | quote_c_style(a, name, NULL, 0); |
| 2456 | strbuf_addstr(name, " => "); |
| 2457 | quote_c_style(b, name, NULL, 0); |
| 2458 | return; |
Alexandre Julliard | e5bfbf9 | 2007-02-10 15:39:00 +0100 | [diff] [blame] | 2459 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2460 | |
| 2461 | /* Find common prefix */ |
| 2462 | pfx_length = 0; |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2463 | while (*old_name && *new_name && *old_name == *new_name) { |
| 2464 | if (*old_name == '/') |
| 2465 | pfx_length = old_name - a + 1; |
| 2466 | old_name++; |
| 2467 | new_name++; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | /* Find common suffix */ |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2471 | old_name = a + len_a; |
| 2472 | new_name = b + len_b; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2473 | sfx_length = 0; |
Antoine Pelisse | d020e27 | 2013-02-23 17:48:45 +0100 | [diff] [blame] | 2474 | /* |
Thomas Rast | dd281f0 | 2013-02-26 21:47:01 +0100 | [diff] [blame] | 2475 | * If there is a common prefix, it must end in a slash. In |
| 2476 | * that case we let this loop run 1 into the prefix to see the |
| 2477 | * same slash. |
| 2478 | * |
| 2479 | * If there is no common prefix, we cannot do this as it would |
| 2480 | * underrun the input strings. |
Antoine Pelisse | d020e27 | 2013-02-23 17:48:45 +0100 | [diff] [blame] | 2481 | */ |
Thomas Rast | dd281f0 | 2013-02-26 21:47:01 +0100 | [diff] [blame] | 2482 | pfx_adjust_for_slash = (pfx_length ? 1 : 0); |
Brandon Williams | 63a01c3 | 2018-02-14 10:59:39 -0800 | [diff] [blame] | 2483 | while (a + pfx_length - pfx_adjust_for_slash <= old_name && |
| 2484 | b + pfx_length - pfx_adjust_for_slash <= new_name && |
| 2485 | *old_name == *new_name) { |
| 2486 | if (*old_name == '/') |
| 2487 | sfx_length = len_a - (old_name - a); |
| 2488 | old_name--; |
| 2489 | new_name--; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | /* |
| 2493 | * pfx{mid-a => mid-b}sfx |
| 2494 | * {pfx-a => pfx-b}sfx |
| 2495 | * pfx{sfx-a => sfx-b} |
| 2496 | * name-a => name-b |
| 2497 | */ |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2498 | a_midlen = len_a - pfx_length - sfx_length; |
| 2499 | b_midlen = len_b - pfx_length - sfx_length; |
| 2500 | if (a_midlen < 0) |
| 2501 | a_midlen = 0; |
| 2502 | if (b_midlen < 0) |
| 2503 | b_midlen = 0; |
Junio C Hamano | cc908b8 | 2006-05-14 22:07:28 -0700 | [diff] [blame] | 2504 | |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2505 | strbuf_grow(name, pfx_length + a_midlen + b_midlen + sfx_length + 7); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2506 | if (pfx_length + sfx_length) { |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2507 | strbuf_add(name, a, pfx_length); |
| 2508 | strbuf_addch(name, '{'); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2509 | } |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2510 | strbuf_add(name, a + pfx_length, a_midlen); |
| 2511 | strbuf_addstr(name, " => "); |
| 2512 | strbuf_add(name, b + pfx_length, b_midlen); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2513 | if (pfx_length + sfx_length) { |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2514 | strbuf_addch(name, '}'); |
| 2515 | strbuf_add(name, a + len_a - sfx_length, sfx_length); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2516 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2517 | } |
| 2518 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2519 | static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat, |
| 2520 | const char *name_a, |
| 2521 | const char *name_b) |
| 2522 | { |
| 2523 | struct diffstat_file *x; |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 2524 | CALLOC_ARRAY(x, 1); |
Dmitry S. Dolzhenko | 4c960a4 | 2014-03-04 02:31:53 +0400 | [diff] [blame] | 2525 | ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2526 | diffstat->files[diffstat->nr++] = x; |
| 2527 | if (name_b) { |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2528 | x->from_name = xstrdup(name_a); |
| 2529 | x->name = xstrdup(name_b); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2530 | x->is_renamed = 1; |
| 2531 | } |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2532 | else { |
| 2533 | x->from_name = NULL; |
Shawn Pearce | 9befac4 | 2006-09-02 00:16:31 -0400 | [diff] [blame] | 2534 | x->name = xstrdup(name_a); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2535 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2536 | return x; |
| 2537 | } |
| 2538 | |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2539 | static int diffstat_consume(void *priv, char *line, unsigned long len) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2540 | { |
| 2541 | struct diffstat_t *diffstat = priv; |
| 2542 | struct diffstat_file *x = diffstat->files[diffstat->nr - 1]; |
| 2543 | |
Jeff King | 0e5a87e | 2022-10-17 21:01:17 -0400 | [diff] [blame] | 2544 | if (!len) |
| 2545 | BUG("xdiff fed us an empty line"); |
| 2546 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2547 | if (line[0] == '+') |
| 2548 | x->added++; |
| 2549 | else if (line[0] == '-') |
| 2550 | x->deleted++; |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 2551 | return 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2552 | } |
| 2553 | |
Johannes Schindelin | 698ce6f | 2006-05-20 15:40:29 +0200 | [diff] [blame] | 2554 | const char mime_boundary_leader[] = "------------"; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2555 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2556 | static int scale_linear(int it, int width, int max_change) |
| 2557 | { |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2558 | if (!it) |
| 2559 | return 0; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2560 | /* |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2561 | * make sure that at least one '-' or '+' is printed if |
| 2562 | * there is any change to this path. The easiest way is to |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 2563 | * scale linearly as if the allotted width is one column shorter |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2564 | * than it is, and then add 1 to the result. |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2565 | */ |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2566 | return 1 + (it * (width - 1) / max_change); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2567 | } |
| 2568 | |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2569 | static void show_graph(struct strbuf *out, char ch, int cnt, |
| 2570 | const char *set, const char *reset) |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2571 | { |
| 2572 | if (cnt <= 0) |
| 2573 | return; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2574 | strbuf_addstr(out, set); |
| 2575 | strbuf_addchars(out, ch, cnt); |
| 2576 | strbuf_addstr(out, reset); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2577 | } |
| 2578 | |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2579 | static void fill_print_name(struct diffstat_file *file) |
| 2580 | { |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2581 | struct strbuf pname = STRBUF_INIT; |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2582 | |
| 2583 | if (file->print_name) |
| 2584 | return; |
| 2585 | |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2586 | if (file->is_renamed) |
| 2587 | pprint_rename(&pname, file->from_name, file->name); |
| 2588 | else |
| 2589 | quote_c_style(file->name, &pname, NULL, 0); |
| 2590 | |
Nguyễn Thái Ngọc Duy | ddf88fa | 2018-02-24 21:09:59 +0700 | [diff] [blame] | 2591 | if (file->comments) |
| 2592 | strbuf_addf(&pname, " (%s)", file->comments); |
| 2593 | |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 2594 | file->print_name = strbuf_detach(&pname, NULL); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2595 | } |
| 2596 | |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2597 | static void print_stat_summary_inserts_deletes(struct diff_options *options, |
| 2598 | int files, int insertions, int deletions) |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2599 | { |
| 2600 | struct strbuf sb = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2601 | |
| 2602 | if (!files) { |
| 2603 | assert(insertions == 0 && deletions == 0); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2604 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES, |
| 2605 | NULL, 0, 0); |
| 2606 | return; |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | strbuf_addf(&sb, |
Nguyễn Thái Ngọc Duy | 218adaa | 2012-09-13 21:16:26 +0700 | [diff] [blame] | 2610 | (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] | 2611 | files); |
| 2612 | |
| 2613 | /* |
| 2614 | * For binary diff, the caller may want to print "x files |
| 2615 | * changed" with insertions == 0 && deletions == 0. |
| 2616 | * |
| 2617 | * Not omitting "0 insertions(+), 0 deletions(-)" in this case |
| 2618 | * is probably less confusing (i.e skip over "2 files changed |
| 2619 | * but nothing about added/removed lines? Is this a bug in Git?"). |
| 2620 | */ |
| 2621 | if (insertions || deletions == 0) { |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2622 | strbuf_addf(&sb, |
Nguyễn Thái Ngọc Duy | 218adaa | 2012-09-13 21:16:26 +0700 | [diff] [blame] | 2623 | (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)", |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2624 | insertions); |
| 2625 | } |
| 2626 | |
| 2627 | if (deletions || insertions == 0) { |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2628 | strbuf_addf(&sb, |
Nguyễn Thái Ngọc Duy | 218adaa | 2012-09-13 21:16:26 +0700 | [diff] [blame] | 2629 | (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)", |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2630 | deletions); |
| 2631 | } |
| 2632 | strbuf_addch(&sb, '\n'); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2633 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES, |
| 2634 | sb.buf, sb.len, 0); |
Nguyễn Thái Ngọc Duy | 7f81463 | 2012-02-01 19:55:07 +0700 | [diff] [blame] | 2635 | strbuf_release(&sb); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | void print_stat_summary(FILE *fp, int files, |
| 2639 | int insertions, int deletions) |
| 2640 | { |
| 2641 | struct diff_options o; |
| 2642 | memset(&o, 0, sizeof(o)); |
| 2643 | o.file = fp; |
| 2644 | |
| 2645 | 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] | 2646 | } |
| 2647 | |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 2648 | 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] | 2649 | { |
Benjamin Kramer | eb3a9dd | 2009-03-07 21:02:10 +0100 | [diff] [blame] | 2650 | int i, len, add, del, adds = 0, dels = 0; |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2651 | uintmax_t max_change = 0, max_len = 0; |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2652 | int total_files = data->nr, count; |
| 2653 | int width, name_width, graph_width, number_width = 0, bin_width = 0; |
Johannes Schindelin | c0aa335 | 2011-03-22 13:50:08 +0100 | [diff] [blame] | 2654 | const char *reset, *add_c, *del_c; |
Junio C Hamano | e5f85df | 2011-05-27 21:50:39 -0700 | [diff] [blame] | 2655 | int extra_shown = 0; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2656 | const char *line_prefix = diff_line_prefix(options); |
| 2657 | struct strbuf out = STRBUF_INIT; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2658 | |
| 2659 | if (data->nr == 0) |
| 2660 | return; |
| 2661 | |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2662 | count = options->stat_count ? options->stat_count : data->nr; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2663 | |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 2664 | reset = diff_get_color_opt(options, DIFF_RESET); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 2665 | add_c = diff_get_color_opt(options, DIFF_FILE_NEW); |
| 2666 | del_c = diff_get_color_opt(options, DIFF_FILE_OLD); |
Junio C Hamano | 785f743 | 2006-09-26 18:59:41 -0700 | [diff] [blame] | 2667 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2668 | /* |
| 2669 | * Find the longest filename and max number of changes |
| 2670 | */ |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2671 | for (i = 0; (i < count) && (i < data->nr); i++) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2672 | struct diffstat_file *file = data->files[i]; |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2673 | uintmax_t change = file->added + file->deleted; |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2674 | |
| 2675 | if (!file->is_interesting && (change == 0)) { |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2676 | count++; /* not shown == room for one more */ |
Michael J Gruber | 358e460 | 2011-05-27 14:36:40 +0200 | [diff] [blame] | 2677 | continue; |
| 2678 | } |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2679 | fill_print_name(file); |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2680 | len = utf8_strwidth(file->print_name); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2681 | if (max_len < len) |
| 2682 | max_len = len; |
| 2683 | |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2684 | if (file->is_unmerged) { |
| 2685 | /* "Unmerged" is 8 characters */ |
| 2686 | bin_width = bin_width < 8 ? 8 : bin_width; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2687 | continue; |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2688 | } |
| 2689 | if (file->is_binary) { |
| 2690 | /* "Bin XXX -> YYY bytes" */ |
| 2691 | int w = 14 + decimal_width(file->added) |
| 2692 | + decimal_width(file->deleted); |
| 2693 | bin_width = bin_width < w ? w : bin_width; |
| 2694 | /* Display change counts aligned with "Bin" */ |
| 2695 | number_width = 3; |
| 2696 | continue; |
| 2697 | } |
| 2698 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2699 | if (max_change < change) |
| 2700 | max_change = change; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2701 | } |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2702 | count = i; /* where we can stop scanning in data->files[] */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2703 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2704 | /* |
| 2705 | * We have width = stat_width or term_columns() columns total. |
| 2706 | * 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] | 2707 | * 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] | 2708 | * We also need 1 for " " and 4 + decimal_width(max_change) |
| 2709 | * for " | NNNN " and one the empty column at the end, altogether |
| 2710 | * 6 + decimal_width(max_change). |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2711 | * |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2712 | * If there's not enough space, we will use the smaller of |
| 2713 | * 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] | 2714 | * and the rest for constant elements + graph part, but no more |
| 2715 | * than stat_graph_width for the graph part. |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2716 | * (5/8 gives 50 for filename and 30 for the constant parts + graph |
| 2717 | * for the standard terminal size). |
| 2718 | * |
| 2719 | * In other words: stat_width limits the maximum width, and |
| 2720 | * stat_name_width fixes the maximum width of the filename, |
| 2721 | * and is also used to divide available columns if there |
| 2722 | * aren't enough. |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2723 | * |
| 2724 | * Binary files are displayed with "Bin XXX -> YYY bytes" |
| 2725 | * instead of the change count and graph. This part is treated |
| 2726 | * similarly to the graph part, except that it is not |
Stefano Lattarini | 41ccfdd | 2013-04-12 00:36:10 +0200 | [diff] [blame] | 2727 | * "scaled". If total width is too small to accommodate the |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2728 | * guaranteed minimum width of the filename part and the |
| 2729 | * separators and this message, this message will "overflow" |
| 2730 | * making the line longer than the maximum width. |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2731 | */ |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2732 | |
Junio C Hamano | ce8529b | 2022-10-21 14:53:25 -0700 | [diff] [blame] | 2733 | /* |
| 2734 | * NEEDSWORK: line_prefix is often used for "log --graph" output |
| 2735 | * and contains ANSI-colored string. utf8_strnwidth() should be |
| 2736 | * used to correctly count the display width instead of strlen(). |
| 2737 | */ |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2738 | if (options->stat_width == -1) |
Junio C Hamano | cd48dad | 2016-08-31 16:27:19 -0700 | [diff] [blame] | 2739 | width = term_columns() - strlen(line_prefix); |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2740 | else |
| 2741 | width = options->stat_width ? options->stat_width : 80; |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2742 | number_width = decimal_width(max_change) > number_width ? |
| 2743 | decimal_width(max_change) : number_width; |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2744 | |
Dragan Simic | bd48adc | 2023-09-11 17:39:44 +0200 | [diff] [blame] | 2745 | if (options->stat_name_width == -1) |
| 2746 | options->stat_name_width = diff_stat_name_width; |
Zbigniew Jędrzejewski-Szmek | df44483 | 2012-03-01 13:26:46 +0100 | [diff] [blame] | 2747 | if (options->stat_graph_width == -1) |
| 2748 | options->stat_graph_width = diff_stat_graph_width; |
| 2749 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2750 | /* |
Dragan Simic | bd48adc | 2023-09-11 17:39:44 +0200 | [diff] [blame] | 2751 | * Guarantee 3/8*16 == 6 for the graph part |
| 2752 | * and 5/8*16 == 10 for the filename part |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2753 | */ |
| 2754 | if (width < 16 + 6 + number_width) |
| 2755 | width = 16 + 6 + number_width; |
| 2756 | |
| 2757 | /* |
| 2758 | * First assign sizes that are wanted, ignoring available width. |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2759 | * strlen("Bin XXX -> YYY bytes") == bin_width, and the part |
| 2760 | * starting from "XXX" should fit in graph_width. |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2761 | */ |
Zbigniew Jędrzejewski-Szmek | dc801e7 | 2012-04-30 22:38:58 +0200 | [diff] [blame] | 2762 | graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4; |
| 2763 | if (options->stat_graph_width && |
| 2764 | options->stat_graph_width < graph_width) |
| 2765 | graph_width = options->stat_graph_width; |
| 2766 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2767 | name_width = (options->stat_name_width > 0 && |
| 2768 | options->stat_name_width < max_len) ? |
| 2769 | options->stat_name_width : max_len; |
| 2770 | |
| 2771 | /* |
| 2772 | * Adjust adjustable widths not to exceed maximum width |
| 2773 | */ |
| 2774 | if (name_width + number_width + 6 + graph_width > width) { |
Lucian Poston | 678c574 | 2012-04-18 14:12:18 -0700 | [diff] [blame] | 2775 | if (graph_width > width * 3/8 - number_width - 6) { |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2776 | graph_width = width * 3/8 - number_width - 6; |
Lucian Poston | 678c574 | 2012-04-18 14:12:18 -0700 | [diff] [blame] | 2777 | if (graph_width < 6) |
| 2778 | graph_width = 6; |
| 2779 | } |
| 2780 | |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 2781 | if (options->stat_graph_width && |
| 2782 | graph_width > options->stat_graph_width) |
| 2783 | graph_width = options->stat_graph_width; |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2784 | if (name_width > width - number_width - 6 - graph_width) |
| 2785 | name_width = width - number_width - 6 - graph_width; |
| 2786 | else |
| 2787 | graph_width = width - number_width - 6 - name_width; |
| 2788 | } |
| 2789 | |
| 2790 | /* |
| 2791 | * From here name_width is the width of the name area, |
| 2792 | * and graph_width is the width of the graph area. |
| 2793 | * max_change is used to scale graph properly. |
| 2794 | */ |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2795 | for (i = 0; i < count; i++) { |
Timo Hirvonen | d2543b8 | 2006-06-24 20:20:32 +0300 | [diff] [blame] | 2796 | const char *prefix = ""; |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2797 | struct diffstat_file *file = data->files[i]; |
| 2798 | char *name = file->print_name; |
| 2799 | uintmax_t added = file->added; |
| 2800 | uintmax_t deleted = file->deleted; |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2801 | int name_len, padding; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2802 | |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2803 | if (!file->is_interesting && (added + deleted == 0)) |
Michael J Gruber | 358e460 | 2011-05-27 14:36:40 +0200 | [diff] [blame] | 2804 | continue; |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2805 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2806 | /* |
| 2807 | * "scale" the filename |
| 2808 | */ |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2809 | len = name_width; |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2810 | name_len = utf8_strwidth(name); |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2811 | if (name_width < name_len) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2812 | char *slash; |
| 2813 | prefix = "..."; |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2814 | len -= 3; |
Junio C Hamano | ce8529b | 2022-10-21 14:53:25 -0700 | [diff] [blame] | 2815 | /* |
| 2816 | * NEEDSWORK: (name_len - len) counts the display |
| 2817 | * width, which would be shorter than the byte |
| 2818 | * length of the corresponding substring. |
| 2819 | * Advancing "name" by that number of bytes does |
| 2820 | * *NOT* skip over that many columns, so it is |
| 2821 | * very likely that chomping the pathname at the |
| 2822 | * slash we will find starting from "name" will |
| 2823 | * leave the resulting string still too long. |
| 2824 | */ |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2825 | name += name_len - len; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2826 | slash = strchr(name, '/'); |
| 2827 | if (slash) |
| 2828 | name = slash; |
| 2829 | } |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2830 | padding = len - utf8_strwidth(name); |
| 2831 | if (padding < 0) |
| 2832 | padding = 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2833 | |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2834 | if (file->is_binary) { |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2835 | strbuf_addf(&out, " %s%s%*s | %*s", |
| 2836 | prefix, name, padding, "", |
| 2837 | number_width, "Bin"); |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2838 | if (!added && !deleted) { |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2839 | strbuf_addch(&out, '\n'); |
| 2840 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 2841 | out.buf, out.len, 0); |
| 2842 | strbuf_reset(&out); |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2843 | continue; |
| 2844 | } |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2845 | strbuf_addf(&out, " %s%"PRIuMAX"%s", |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2846 | del_c, deleted, reset); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2847 | strbuf_addstr(&out, " -> "); |
| 2848 | strbuf_addf(&out, "%s%"PRIuMAX"%s", |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2849 | add_c, added, reset); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2850 | strbuf_addstr(&out, " bytes\n"); |
| 2851 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 2852 | out.buf, out.len, 0); |
| 2853 | strbuf_reset(&out); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2854 | continue; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2855 | } |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2856 | else if (file->is_unmerged) { |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2857 | strbuf_addf(&out, " %s%s%*s | %*s", |
| 2858 | prefix, name, padding, "", |
Peter Grayson | 209d9cb | 2022-12-14 12:41:51 -0500 | [diff] [blame] | 2859 | number_width, "Unmerged\n"); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2860 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 2861 | out.buf, out.len, 0); |
| 2862 | strbuf_reset(&out); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2863 | continue; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2864 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2865 | |
Junio C Hamano | a254002 | 2006-09-26 18:53:02 -0700 | [diff] [blame] | 2866 | /* |
| 2867 | * scale the add/delete |
| 2868 | */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2869 | add = added; |
| 2870 | del = deleted; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2871 | |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2872 | if (graph_width <= max_change) { |
Stefan Beller | d3c9cf3 | 2013-07-14 23:35:49 +0200 | [diff] [blame] | 2873 | int total = scale_linear(add + del, graph_width, max_change); |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2874 | if (total < 2 && add && del) |
| 2875 | /* width >= 2 due to the sanity check */ |
| 2876 | total = 2; |
| 2877 | if (add < del) { |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2878 | add = scale_linear(add, graph_width, max_change); |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2879 | del = total - add; |
| 2880 | } else { |
Zbigniew Jędrzejewski-Szmek | 1b058bc | 2012-03-01 13:26:43 +0100 | [diff] [blame] | 2881 | del = scale_linear(del, graph_width, max_change); |
Junio C Hamano | 2eeeef2 | 2012-02-14 13:49:11 -0800 | [diff] [blame] | 2882 | add = total - del; |
| 2883 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2884 | } |
Torsten Bögershausen | 12fc4ad | 2022-09-14 17:13:33 +0200 | [diff] [blame] | 2885 | strbuf_addf(&out, " %s%s%*s | %*"PRIuMAX"%s", |
| 2886 | prefix, name, padding, "", |
| 2887 | number_width, added + deleted, |
| 2888 | added + deleted ? " " : ""); |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2889 | show_graph(&out, '+', add, add_c, reset); |
| 2890 | show_graph(&out, '-', del, del_c, reset); |
| 2891 | strbuf_addch(&out, '\n'); |
| 2892 | emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE, |
| 2893 | out.buf, out.len, 0); |
| 2894 | strbuf_reset(&out); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2895 | } |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2896 | |
| 2897 | for (i = 0; i < data->nr; i++) { |
Junio C Hamano | af0ed81 | 2012-11-27 11:24:54 -0800 | [diff] [blame] | 2898 | struct diffstat_file *file = data->files[i]; |
| 2899 | uintmax_t added = file->added; |
| 2900 | uintmax_t deleted = file->deleted; |
Junio C Hamano | 82dfc2c | 2012-11-27 12:05:10 -0800 | [diff] [blame] | 2901 | |
| 2902 | if (file->is_unmerged || |
| 2903 | (!file->is_interesting && (added + deleted == 0))) { |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2904 | total_files--; |
| 2905 | continue; |
| 2906 | } |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2907 | |
Junio C Hamano | 82dfc2c | 2012-11-27 12:05:10 -0800 | [diff] [blame] | 2908 | if (!file->is_binary) { |
Junio C Hamano | a20d3c0 | 2012-11-27 11:47:46 -0800 | [diff] [blame] | 2909 | adds += added; |
| 2910 | dels += deleted; |
| 2911 | } |
| 2912 | if (i < count) |
| 2913 | continue; |
Junio C Hamano | e5f85df | 2011-05-27 21:50:39 -0700 | [diff] [blame] | 2914 | if (!extra_shown) |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2915 | emit_diff_symbol(options, |
| 2916 | DIFF_SYMBOL_STATS_SUMMARY_ABBREV, |
| 2917 | NULL, 0, 0); |
Junio C Hamano | e5f85df | 2011-05-27 21:50:39 -0700 | [diff] [blame] | 2918 | extra_shown = 1; |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 2919 | } |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2920 | |
| 2921 | print_stat_summary_inserts_deletes(options, total_files, adds, dels); |
Rene Scharfe | 5a61201 | 2017-08-30 19:49:43 +0200 | [diff] [blame] | 2922 | strbuf_release(&out); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 2923 | } |
| 2924 | |
Felipe Contreras | 2775d92 | 2009-10-11 23:46:11 +0300 | [diff] [blame] | 2925 | static void show_shortstats(struct diffstat_t *data, struct diff_options *options) |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2926 | { |
| 2927 | int i, adds = 0, dels = 0, total_files = data->nr; |
| 2928 | |
| 2929 | if (data->nr == 0) |
| 2930 | return; |
| 2931 | |
| 2932 | for (i = 0; i < data->nr; i++) { |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2933 | int added = data->files[i]->added; |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2934 | int deleted = data->files[i]->deleted; |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2935 | |
Junio C Hamano | 20c8cde | 2012-11-27 14:19:36 -0800 | [diff] [blame] | 2936 | if (data->files[i]->is_unmerged || |
| 2937 | (!data->files[i]->is_interesting && (added + deleted == 0))) { |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 2938 | total_files--; |
Alexander Strasser | de9658b | 2012-06-15 23:50:30 +0200 | [diff] [blame] | 2939 | } 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] | 2940 | adds += added; |
| 2941 | dels += deleted; |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2942 | } |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2943 | } |
Stefan Beller | 0911c47 | 2017-06-29 17:07:02 -0700 | [diff] [blame] | 2944 | print_stat_summary_inserts_deletes(options, total_files, adds, dels); |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 2945 | } |
| 2946 | |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 2947 | 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] | 2948 | { |
| 2949 | int i; |
| 2950 | |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2951 | if (data->nr == 0) |
| 2952 | return; |
| 2953 | |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 2954 | for (i = 0; i < data->nr; i++) { |
| 2955 | struct diffstat_file *file = data->files[i]; |
| 2956 | |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2957 | fprintf(options->file, "%s", diff_line_prefix(options)); |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 2958 | |
Junio C Hamano | bfddbc5 | 2006-12-10 13:50:59 -0800 | [diff] [blame] | 2959 | if (file->is_binary) |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2960 | fprintf(options->file, "-\t-\t"); |
Junio C Hamano | bfddbc5 | 2006-12-10 13:50:59 -0800 | [diff] [blame] | 2961 | else |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2962 | fprintf(options->file, |
Jeff King | 0974c11 | 2010-04-17 13:41:08 -0400 | [diff] [blame] | 2963 | "%"PRIuMAX"\t%"PRIuMAX"\t", |
| 2964 | file->added, file->deleted); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2965 | if (options->line_termination) { |
| 2966 | fill_print_name(file); |
| 2967 | if (!file->is_renamed) |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2968 | write_name_quoted(file->name, options->file, |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2969 | options->line_termination); |
| 2970 | else { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2971 | fputs(file->print_name, options->file); |
| 2972 | putc(options->line_termination, options->file); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2973 | } |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2974 | } else { |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2975 | if (file->is_renamed) { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2976 | putc('\0', options->file); |
| 2977 | write_name_quoted(file->from_name, options->file, '\0'); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 2978 | } |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 2979 | write_name_quoted(file->name, options->file, '\0'); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 2980 | } |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 2981 | } |
| 2982 | } |
| 2983 | |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 2984 | struct dirstat_file { |
| 2985 | const char *name; |
| 2986 | unsigned long changed; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2987 | }; |
| 2988 | |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 2989 | struct dirstat_dir { |
| 2990 | struct dirstat_file *files; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 2991 | int alloc, nr, permille, cumulative; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 2992 | }; |
| 2993 | |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 2994 | static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir, |
| 2995 | unsigned long changed, const char *base, int baselen) |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2996 | { |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 2997 | unsigned long sum_changes = 0; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 2998 | unsigned int sources = 0; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 2999 | const char *line_prefix = diff_line_prefix(opt); |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3000 | |
| 3001 | while (dir->nr) { |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3002 | struct dirstat_file *f = dir->files; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3003 | int namelen = strlen(f->name); |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 3004 | unsigned long changes; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3005 | char *slash; |
| 3006 | |
| 3007 | if (namelen < baselen) |
| 3008 | break; |
| 3009 | if (memcmp(f->name, base, baselen)) |
| 3010 | break; |
| 3011 | slash = strchr(f->name + baselen, '/'); |
| 3012 | if (slash) { |
| 3013 | int newbaselen = slash + 1 - f->name; |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 3014 | changes = gather_dirstat(opt, dir, changed, f->name, newbaselen); |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3015 | sources++; |
| 3016 | } else { |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 3017 | changes = f->changed; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3018 | dir->files++; |
| 3019 | dir->nr--; |
| 3020 | sources += 2; |
| 3021 | } |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 3022 | sum_changes += changes; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | /* |
| 3026 | * We don't report dirstat's for |
| 3027 | * - the top level |
| 3028 | * - or cases where everything came from a single directory |
| 3029 | * under this directory (sources == 1). |
| 3030 | */ |
| 3031 | if (baselen && sources != 1) { |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 3032 | if (sum_changes) { |
| 3033 | int permille = sum_changes * 1000 / changed; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 3034 | if (permille >= dir->permille) { |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 3035 | fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix, |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 3036 | permille / 10, permille % 10, baselen, base); |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3037 | if (!dir->cumulative) |
| 3038 | return 0; |
| 3039 | } |
| 3040 | } |
| 3041 | } |
Brandon Williams | 585c0e2 | 2018-02-14 10:59:28 -0800 | [diff] [blame] | 3042 | return sum_changes; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3043 | } |
| 3044 | |
Linus Torvalds | 441bca0 | 2008-08-28 16:19:08 -0700 | [diff] [blame] | 3045 | static int dirstat_compare(const void *_a, const void *_b) |
| 3046 | { |
| 3047 | const struct dirstat_file *a = _a; |
| 3048 | const struct dirstat_file *b = _b; |
| 3049 | return strcmp(a->name, b->name); |
| 3050 | } |
| 3051 | |
Junio C Hamano | 34a9489 | 2023-05-05 14:19:16 -0700 | [diff] [blame] | 3052 | static void conclude_dirstat(struct diff_options *options, |
| 3053 | struct dirstat_dir *dir, |
| 3054 | unsigned long changed) |
| 3055 | { |
Junio C Hamano | 8397398 | 2023-05-05 14:19:17 -0700 | [diff] [blame] | 3056 | struct dirstat_file *to_free = dir->files; |
Junio C Hamano | 34a9489 | 2023-05-05 14:19:16 -0700 | [diff] [blame] | 3057 | |
Junio C Hamano | 8397398 | 2023-05-05 14:19:17 -0700 | [diff] [blame] | 3058 | if (!changed) { |
| 3059 | /* This can happen even with many files, if everything was renames */ |
| 3060 | ; |
| 3061 | } else { |
| 3062 | /* Show all directories with more than x% of the changes */ |
| 3063 | QSORT(dir->files, dir->nr, dirstat_compare); |
| 3064 | gather_dirstat(options, dir, changed, "", 0); |
| 3065 | } |
| 3066 | |
| 3067 | free(to_free); |
Junio C Hamano | 34a9489 | 2023-05-05 14:19:16 -0700 | [diff] [blame] | 3068 | } |
| 3069 | |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3070 | static void show_dirstat(struct diff_options *options) |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3071 | { |
| 3072 | int i; |
| 3073 | unsigned long changed; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3074 | struct dirstat_dir dir; |
| 3075 | struct diff_queue_struct *q = &diff_queued_diff; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3076 | |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3077 | dir.files = NULL; |
| 3078 | dir.alloc = 0; |
| 3079 | dir.nr = 0; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 3080 | dir.permille = options->dirstat_permille; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3081 | dir.cumulative = options->flags.dirstat_cumulative; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3082 | |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3083 | changed = 0; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3084 | for (i = 0; i < q->nr; i++) { |
| 3085 | struct diff_filepair *p = q->queue[i]; |
| 3086 | const char *name; |
| 3087 | unsigned long copied, added, damage; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3088 | struct diff_populate_filespec_options dpf_options = { |
| 3089 | .check_size_only = 1, |
| 3090 | }; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3091 | |
Johan Herland | 2ca8671 | 2011-04-12 11:24:34 +0200 | [diff] [blame] | 3092 | name = p->two->path ? p->two->path : p->one->path; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3093 | |
Jeff King | d9f62df | 2018-08-28 17:23:03 -0400 | [diff] [blame] | 3094 | if (p->one->oid_valid && p->two->oid_valid && |
| 3095 | oideq(&p->one->oid, &p->two->oid)) { |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3096 | /* |
| 3097 | * The SHA1 has not changed, so pre-/post-content is |
| 3098 | * identical. We can therefore skip looking at the |
| 3099 | * file contents altogether. |
| 3100 | */ |
| 3101 | damage = 0; |
| 3102 | goto found_damage; |
| 3103 | } |
| 3104 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3105 | if (options->flags.dirstat_by_file) { |
Johan Herland | 0133dab | 2011-04-11 00:48:51 +0200 | [diff] [blame] | 3106 | /* |
| 3107 | * In --dirstat-by-file mode, we don't really need to |
| 3108 | * look at the actual file contents at all. |
| 3109 | * The fact that the SHA1 changed is enough for us to |
| 3110 | * add this file to the list of results |
| 3111 | * (with each file contributing equal damage). |
| 3112 | */ |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3113 | damage = 1; |
Johan Herland | 0133dab | 2011-04-11 00:48:51 +0200 | [diff] [blame] | 3114 | goto found_damage; |
| 3115 | } |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3116 | |
| 3117 | if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3118 | diff_populate_filespec(options->repo, p->one, NULL); |
| 3119 | 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] | 3120 | diffcore_count_changes(options->repo, |
| 3121 | p->one, p->two, NULL, NULL, |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3122 | &copied, &added); |
| 3123 | diff_free_filespec_data(p->one); |
| 3124 | diff_free_filespec_data(p->two); |
| 3125 | } else if (DIFF_FILE_VALID(p->one)) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3126 | diff_populate_filespec(options->repo, p->one, &dpf_options); |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3127 | copied = added = 0; |
| 3128 | diff_free_filespec_data(p->one); |
| 3129 | } else if (DIFF_FILE_VALID(p->two)) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3130 | diff_populate_filespec(options->repo, p->two, &dpf_options); |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3131 | copied = 0; |
| 3132 | added = p->two->size; |
| 3133 | diff_free_filespec_data(p->two); |
| 3134 | } else |
Junio C Hamano | 2b0b551 | 2008-02-24 17:37:15 -0800 | [diff] [blame] | 3135 | continue; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3136 | |
| 3137 | /* |
| 3138 | * Original minus copied is the removed material, |
| 3139 | * added is the new material. They are both damages |
Johan Herland | 0133dab | 2011-04-11 00:48:51 +0200 | [diff] [blame] | 3140 | * made to the preimage. |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3141 | * If the resulting damage is zero, we know that |
| 3142 | * diffcore_count_changes() considers the two entries to |
Jeff King | d9f62df | 2018-08-28 17:23:03 -0400 | [diff] [blame] | 3143 | * be identical, but since the oid changed, we |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3144 | * know that there must have been _some_ kind of change, |
| 3145 | * so we force all entries to have damage > 0. |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3146 | */ |
| 3147 | damage = (p->one->size - copied) + added; |
Johan Herland | 2ff3a80 | 2011-04-11 00:48:52 +0200 | [diff] [blame] | 3148 | if (!damage) |
Heikki Orsila | fd33777 | 2008-09-05 22:27:35 +0300 | [diff] [blame] | 3149 | damage = 1; |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3150 | |
Johan Herland | 0133dab | 2011-04-11 00:48:51 +0200 | [diff] [blame] | 3151 | found_damage: |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 3152 | ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc); |
| 3153 | dir.files[dir.nr].name = name; |
| 3154 | dir.files[dir.nr].changed = damage; |
| 3155 | changed += damage; |
| 3156 | dir.nr++; |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3157 | } |
| 3158 | |
Junio C Hamano | 34a9489 | 2023-05-05 14:19:16 -0700 | [diff] [blame] | 3159 | conclude_dirstat(options, &dir, changed); |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 3160 | } |
| 3161 | |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 3162 | static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options) |
| 3163 | { |
| 3164 | int i; |
| 3165 | unsigned long changed; |
| 3166 | struct dirstat_dir dir; |
| 3167 | |
| 3168 | if (data->nr == 0) |
| 3169 | return; |
| 3170 | |
| 3171 | dir.files = NULL; |
| 3172 | dir.alloc = 0; |
| 3173 | dir.nr = 0; |
| 3174 | dir.permille = options->dirstat_permille; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3175 | dir.cumulative = options->flags.dirstat_cumulative; |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 3176 | |
| 3177 | changed = 0; |
| 3178 | for (i = 0; i < data->nr; i++) { |
| 3179 | struct diffstat_file *file = data->files[i]; |
| 3180 | unsigned long damage = file->added + file->deleted; |
| 3181 | if (file->is_binary) |
| 3182 | /* |
| 3183 | * binary files counts bytes, not lines. Must find some |
| 3184 | * way to normalize binary bytes vs. textual lines. |
| 3185 | * The following heuristic assumes that there are 64 |
| 3186 | * bytes per "line". |
| 3187 | * This is stupid and ugly, but very cheap... |
| 3188 | */ |
René Scharfe | 42c78a2 | 2017-07-08 12:35:35 +0200 | [diff] [blame] | 3189 | damage = DIV_ROUND_UP(damage, 64); |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 3190 | ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc); |
| 3191 | dir.files[dir.nr].name = file->name; |
| 3192 | dir.files[dir.nr].changed = damage; |
| 3193 | changed += damage; |
| 3194 | dir.nr++; |
| 3195 | } |
| 3196 | |
Junio C Hamano | 34a9489 | 2023-05-05 14:19:16 -0700 | [diff] [blame] | 3197 | conclude_dirstat(options, &dir, changed); |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 3198 | } |
| 3199 | |
Matthew Rogers | 1cf3d5d | 2020-08-20 00:41:32 +0000 | [diff] [blame] | 3200 | static void free_diffstat_file(struct diffstat_file *f) |
| 3201 | { |
| 3202 | free(f->print_name); |
| 3203 | free(f->name); |
| 3204 | free(f->from_name); |
| 3205 | free(f); |
| 3206 | } |
| 3207 | |
Daniel Ferreira | e4cb659 | 2019-11-13 12:40:58 +0000 | [diff] [blame] | 3208 | void free_diffstat_info(struct diffstat_t *diffstat) |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 3209 | { |
| 3210 | int i; |
Matthew Rogers | 1cf3d5d | 2020-08-20 00:41:32 +0000 | [diff] [blame] | 3211 | for (i = 0; i < diffstat->nr; i++) |
| 3212 | free_diffstat_file(diffstat->files[i]); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 3213 | free(diffstat->files); |
| 3214 | } |
| 3215 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3216 | struct checkdiff_t { |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3217 | const char *filename; |
Junio C Hamano | 1ba111d | 2008-06-26 15:36:34 -0700 | [diff] [blame] | 3218 | int lineno; |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3219 | int conflict_marker_size; |
Junio C Hamano | 1ba111d | 2008-06-26 15:36:34 -0700 | [diff] [blame] | 3220 | struct diff_options *o; |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 3221 | unsigned ws_rule; |
Wincent Colaiuta | 62c6489 | 2007-12-13 21:24:52 +0100 | [diff] [blame] | 3222 | unsigned status; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3223 | }; |
| 3224 | |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3225 | 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] | 3226 | { |
| 3227 | char firstchar; |
| 3228 | int cnt; |
| 3229 | |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3230 | if (len < marker_size + 1) |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3231 | return 0; |
| 3232 | firstchar = line[0]; |
| 3233 | switch (firstchar) { |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3234 | case '=': case '>': case '<': case '|': |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3235 | break; |
| 3236 | default: |
| 3237 | return 0; |
| 3238 | } |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3239 | for (cnt = 1; cnt < marker_size; cnt++) |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3240 | if (line[cnt] != firstchar) |
| 3241 | return 0; |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 3242 | /* line[1] through line[marker_size-1] are same as firstchar */ |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3243 | if (len < marker_size + 1 || !isspace(line[marker_size])) |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3244 | return 0; |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3245 | return 1; |
| 3246 | } |
| 3247 | |
Jeff King | 75ab763 | 2018-11-02 02:39:03 -0400 | [diff] [blame] | 3248 | static void checkdiff_consume_hunk(void *priv, |
Jeff King | 61bdc7c | 2022-12-13 06:13:48 -0500 | [diff] [blame] | 3249 | long ob UNUSED, long on UNUSED, |
| 3250 | long nb, long nn UNUSED, |
| 3251 | const char *func UNUSED, long funclen UNUSED) |
Jeff King | 75ab763 | 2018-11-02 02:39:03 -0400 | [diff] [blame] | 3252 | |
| 3253 | { |
| 3254 | struct checkdiff_t *data = priv; |
| 3255 | data->lineno = nb - 1; |
| 3256 | } |
| 3257 | |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 3258 | static int checkdiff_consume(void *priv, char *line, unsigned long len) |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3259 | { |
| 3260 | struct checkdiff_t *data = priv; |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3261 | int marker_size = data->conflict_marker_size; |
Jeff King | f1c9626 | 2011-08-17 22:03:12 -0700 | [diff] [blame] | 3262 | const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE); |
| 3263 | const char *reset = diff_get_color(data->o->use_color, DIFF_RESET); |
| 3264 | 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] | 3265 | char *err; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 3266 | const char *line_prefix; |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 3267 | |
| 3268 | assert(data->o); |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 3269 | line_prefix = diff_line_prefix(data->o); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3270 | |
| 3271 | if (line[0] == '+') { |
Junio C Hamano | 18374e5 | 2008-06-26 13:16:33 -0700 | [diff] [blame] | 3272 | unsigned bad; |
Junio C Hamano | 0ef617f | 2008-02-15 20:30:05 -0800 | [diff] [blame] | 3273 | data->lineno++; |
Junio C Hamano | a757c64 | 2010-03-24 19:21:32 -0700 | [diff] [blame] | 3274 | if (is_conflict_marker(line + 1, marker_size, len - 1)) { |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3275 | data->status |= 1; |
| 3276 | fprintf(data->o->file, |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 3277 | "%s%s:%d: leftover conflict marker\n", |
| 3278 | line_prefix, data->filename, data->lineno); |
Junio C Hamano | 0495404 | 2008-06-26 15:37:21 -0700 | [diff] [blame] | 3279 | } |
Junio C Hamano | 8f8841e | 2008-06-26 15:35:21 -0700 | [diff] [blame] | 3280 | bad = ws_check(line + 1, len - 1, data->ws_rule); |
Junio C Hamano | 18374e5 | 2008-06-26 13:16:33 -0700 | [diff] [blame] | 3281 | if (!bad) |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 3282 | return 0; |
Junio C Hamano | 18374e5 | 2008-06-26 13:16:33 -0700 | [diff] [blame] | 3283 | data->status |= bad; |
| 3284 | err = whitespace_error_string(bad); |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 3285 | fprintf(data->o->file, "%s%s:%d: %s.\n", |
| 3286 | line_prefix, data->filename, data->lineno, err); |
Wincent Colaiuta | c1795bb | 2007-12-13 14:32:29 +0100 | [diff] [blame] | 3287 | free(err); |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 3288 | emit_line(data->o, set, reset, line, 1); |
Junio C Hamano | 8f8841e | 2008-06-26 15:35:21 -0700 | [diff] [blame] | 3289 | ws_check_emit(line + 1, len - 1, data->ws_rule, |
Junio C Hamano | 1ba111d | 2008-06-26 15:36:34 -0700 | [diff] [blame] | 3290 | data->o->file, set, reset, ws); |
Junio C Hamano | 877f23c | 2008-06-26 15:36:59 -0700 | [diff] [blame] | 3291 | } else if (line[0] == ' ') { |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3292 | data->lineno++; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3293 | } |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 3294 | return 0; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3295 | } |
| 3296 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3297 | static unsigned char *deflate_it(char *data, |
| 3298 | unsigned long size, |
| 3299 | unsigned long *result_size) |
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 | int bound; |
| 3302 | unsigned char *deflated; |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 3303 | git_zstream stream; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3304 | |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 3305 | git_deflate_init(&stream, zlib_compression_level); |
Junio C Hamano | 225a6f1 | 2011-06-10 11:18:17 -0700 | [diff] [blame] | 3306 | bound = git_deflate_bound(&stream, size); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3307 | deflated = xmalloc(bound); |
| 3308 | stream.next_out = deflated; |
| 3309 | stream.avail_out = bound; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3310 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3311 | stream.next_in = (unsigned char *)data; |
| 3312 | stream.avail_in = size; |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 3313 | while (git_deflate(&stream, Z_FINISH) == Z_OK) |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3314 | ; /* nothing */ |
Junio C Hamano | 55bb5c9 | 2011-06-10 10:55:10 -0700 | [diff] [blame] | 3315 | git_deflate_end(&stream); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3316 | *result_size = stream.total_out; |
| 3317 | return deflated; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3318 | } |
| 3319 | |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3320 | static void emit_binary_diff_body(struct diff_options *o, |
| 3321 | mmfile_t *one, mmfile_t *two) |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3322 | { |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3323 | void *cp; |
| 3324 | void *delta; |
| 3325 | void *deflated; |
| 3326 | void *data; |
| 3327 | unsigned long orig_size; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3328 | unsigned long delta_size; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3329 | unsigned long deflate_size; |
| 3330 | unsigned long data_size; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3331 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3332 | /* We could do deflated delta, or we could do just deflated two, |
| 3333 | * whichever is smaller. |
| 3334 | */ |
| 3335 | delta = NULL; |
| 3336 | deflated = deflate_it(two->ptr, two->size, &deflate_size); |
| 3337 | if (one->size && two->size) { |
| 3338 | delta = diff_delta(one->ptr, one->size, |
| 3339 | two->ptr, two->size, |
| 3340 | &delta_size, deflate_size); |
| 3341 | if (delta) { |
| 3342 | void *to_free = delta; |
| 3343 | orig_size = delta_size; |
| 3344 | delta = deflate_it(delta, delta_size, &delta_size); |
| 3345 | free(to_free); |
| 3346 | } |
| 3347 | } |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3348 | |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3349 | if (delta && delta_size < deflate_size) { |
Torsten Bögershausen | ca473ce | 2018-11-11 08:05:04 +0100 | [diff] [blame] | 3350 | char *s = xstrfmt("%"PRIuMAX , (uintmax_t)orig_size); |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3351 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA, |
| 3352 | s, strlen(s), 0); |
| 3353 | free(s); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3354 | free(deflated); |
| 3355 | data = delta; |
| 3356 | data_size = delta_size; |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3357 | } else { |
| 3358 | char *s = xstrfmt("%lu", two->size); |
| 3359 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL, |
| 3360 | s, strlen(s), 0); |
| 3361 | free(s); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3362 | free(delta); |
| 3363 | data = deflated; |
| 3364 | data_size = deflate_size; |
| 3365 | } |
| 3366 | |
| 3367 | /* emit data encoded in base85 */ |
| 3368 | cp = data; |
| 3369 | while (data_size) { |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3370 | int len; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3371 | int bytes = (52 < data_size) ? 52 : data_size; |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3372 | char line[71]; |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3373 | data_size -= bytes; |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3374 | if (bytes <= 26) |
| 3375 | line[0] = bytes + 'A' - 1; |
| 3376 | else |
| 3377 | line[0] = bytes - 26 + 'a' - 1; |
| 3378 | encode_85(line + 1, cp, bytes); |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 3379 | cp = (char *) cp + bytes; |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3380 | |
| 3381 | len = strlen(line); |
| 3382 | line[len++] = '\n'; |
| 3383 | line[len] = '\0'; |
| 3384 | |
| 3385 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY, |
| 3386 | line, len, 0); |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3387 | } |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3388 | 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] | 3389 | free(data); |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3390 | } |
| 3391 | |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3392 | static void emit_binary_diff(struct diff_options *o, |
| 3393 | mmfile_t *one, mmfile_t *two) |
Junio C Hamano | d4c452f | 2006-08-16 16:08:14 -0700 | [diff] [blame] | 3394 | { |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3395 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0); |
| 3396 | emit_binary_diff_body(o, one, two); |
| 3397 | emit_binary_diff_body(o, two, one); |
Junio C Hamano | d4c452f | 2006-08-16 16:08:14 -0700 | [diff] [blame] | 3398 | } |
| 3399 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3400 | int diff_filespec_is_binary(struct repository *r, |
| 3401 | struct diff_filespec *one) |
Junio C Hamano | 29a3eef | 2007-07-06 00:18:54 -0700 | [diff] [blame] | 3402 | { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3403 | struct diff_populate_filespec_options dpf_options = { |
| 3404 | .check_binary = 1, |
| 3405 | }; |
| 3406 | |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3407 | if (one->is_binary == -1) { |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3408 | diff_filespec_load_driver(one, r->index); |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3409 | if (one->driver->binary != -1) |
| 3410 | one->is_binary = one->driver->binary; |
| 3411 | else { |
| 3412 | if (!one->data && DIFF_FILE_VALID(one)) |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3413 | diff_populate_filespec(r, one, &dpf_options); |
Nguyễn Thái Ngọc Duy | 6bf3b81 | 2014-08-16 10:08:05 +0700 | [diff] [blame] | 3414 | if (one->is_binary == -1 && one->data) |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3415 | one->is_binary = buffer_is_binary(one->data, |
| 3416 | one->size); |
| 3417 | if (one->is_binary == -1) |
| 3418 | one->is_binary = 0; |
| 3419 | } |
| 3420 | } |
Junio C Hamano | 29a3eef | 2007-07-06 00:18:54 -0700 | [diff] [blame] | 3421 | return one->is_binary; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3422 | } |
| 3423 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3424 | static const struct userdiff_funcname * |
| 3425 | diff_funcname_pattern(struct diff_options *o, struct diff_filespec *one) |
Junio C Hamano | f258475 | 2007-07-06 00:45:10 -0700 | [diff] [blame] | 3426 | { |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3427 | diff_filespec_load_driver(one, o->repo->index); |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3428 | return one->driver->funcname.pattern ? &one->driver->funcname : NULL; |
Junio C Hamano | f258475 | 2007-07-06 00:45:10 -0700 | [diff] [blame] | 3429 | } |
| 3430 | |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3431 | void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b) |
| 3432 | { |
| 3433 | if (!options->a_prefix) |
| 3434 | options->a_prefix = a; |
| 3435 | if (!options->b_prefix) |
| 3436 | options->b_prefix = b; |
| 3437 | } |
| 3438 | |
Jeff King | 6799aad | 2023-03-09 01:07:06 -0500 | [diff] [blame] | 3439 | void diff_set_noprefix(struct diff_options *options) |
| 3440 | { |
| 3441 | options->a_prefix = options->b_prefix = ""; |
| 3442 | } |
| 3443 | |
| 3444 | void diff_set_default_prefix(struct diff_options *options) |
| 3445 | { |
Patrick Steinhardt | f9c1989 | 2024-05-27 13:46:20 +0200 | [diff] [blame] | 3446 | options->a_prefix = diff_src_prefix ? diff_src_prefix : "a/"; |
| 3447 | options->b_prefix = diff_dst_prefix ? diff_dst_prefix : "b/"; |
Jeff King | 6799aad | 2023-03-09 01:07:06 -0500 | [diff] [blame] | 3448 | } |
| 3449 | |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 3450 | 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] | 3451 | struct diff_filespec *one) |
Jeff King | 04427ac | 2008-10-26 00:44:53 -0400 | [diff] [blame] | 3452 | { |
| 3453 | if (!DIFF_FILE_VALID(one)) |
| 3454 | return NULL; |
Jeff King | d391c0f | 2010-09-21 17:01:24 -0400 | [diff] [blame] | 3455 | |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 3456 | diff_filespec_load_driver(one, r->index); |
| 3457 | return userdiff_get_textconv(r, one->driver); |
Jeff King | 04427ac | 2008-10-26 00:44:53 -0400 | [diff] [blame] | 3458 | } |
| 3459 | |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 3460 | static struct string_list *additional_headers(struct diff_options *o, |
| 3461 | const char *path) |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3462 | { |
| 3463 | if (!o->additional_path_headers) |
| 3464 | return NULL; |
| 3465 | return strmap_get(o->additional_path_headers, path); |
| 3466 | } |
| 3467 | |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 3468 | static void add_formatted_header(struct strbuf *msg, |
| 3469 | const char *header, |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3470 | const char *line_prefix, |
| 3471 | const char *meta, |
| 3472 | const char *reset) |
| 3473 | { |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 3474 | const char *next, *newline; |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3475 | |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 3476 | for (next = header; *next; next = newline) { |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3477 | newline = strchrnul(next, '\n'); |
| 3478 | strbuf_addf(msg, "%s%s%.*s%s\n", line_prefix, meta, |
| 3479 | (int)(newline - next), next, reset); |
| 3480 | if (*newline) |
| 3481 | newline++; |
| 3482 | } |
| 3483 | } |
| 3484 | |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 3485 | static void add_formatted_headers(struct strbuf *msg, |
| 3486 | struct string_list *more_headers, |
| 3487 | const char *line_prefix, |
| 3488 | const char *meta, |
| 3489 | const char *reset) |
| 3490 | { |
| 3491 | int i; |
| 3492 | |
| 3493 | for (i = 0; i < more_headers->nr; i++) |
| 3494 | add_formatted_header(msg, more_headers->items[i].string, |
| 3495 | line_prefix, meta, reset); |
| 3496 | } |
| 3497 | |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3498 | static int diff_filepair_is_phoney(struct diff_filespec *one, |
| 3499 | struct diff_filespec *two) |
| 3500 | { |
| 3501 | /* |
| 3502 | * This function specifically looks for pairs injected by |
| 3503 | * create_filepairs_for_header_only_notifications(). Such |
| 3504 | * pairs are "phoney" in that they do not represent any |
| 3505 | * content or even mode difference, but were inserted because |
| 3506 | * diff_queued_diff previously had no pair associated with |
| 3507 | * that path but we needed some pair to avoid losing the |
| 3508 | * "remerge CONFLICT" header associated with the path. |
| 3509 | */ |
| 3510 | return !DIFF_FILE_VALID(one) && !DIFF_FILE_VALID(two); |
| 3511 | } |
| 3512 | |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 3513 | static int set_diff_algorithm(struct diff_options *opts, |
| 3514 | const char *alg) |
| 3515 | { |
| 3516 | long value = parse_algorithm_value(alg); |
| 3517 | |
| 3518 | if (value < 0) |
| 3519 | return -1; |
| 3520 | |
| 3521 | /* clear out previous settings */ |
| 3522 | DIFF_XDL_CLR(opts, NEED_MINIMAL); |
| 3523 | opts->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK; |
| 3524 | opts->xdl_opts |= value; |
| 3525 | |
| 3526 | return 0; |
| 3527 | } |
| 3528 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3529 | static void builtin_diff(const char *name_a, |
| 3530 | const char *name_b, |
| 3531 | struct diff_filespec *one, |
| 3532 | struct diff_filespec *two, |
| 3533 | const char *xfrm_msg, |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3534 | int must_show_header, |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 3535 | struct diff_options *o, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3536 | int complete_rewrite) |
| 3537 | { |
| 3538 | mmfile_t mf1, mf2; |
| 3539 | const char *lbl[2]; |
| 3540 | char *a_one, *b_two; |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3541 | const char *meta = diff_get_color_opt(o, DIFF_METAINFO); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 3542 | const char *reset = diff_get_color_opt(o, DIFF_RESET); |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3543 | const char *a_prefix, *b_prefix; |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 3544 | struct userdiff_driver *textconv_one = NULL; |
| 3545 | struct userdiff_driver *textconv_two = NULL; |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3546 | struct strbuf header = STRBUF_INIT; |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 3547 | const char *line_prefix = diff_line_prefix(o); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3548 | |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3549 | diff_set_mnemonic_prefix(o, "a/", "b/"); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3550 | if (o->flags.reverse_diff) { |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3551 | a_prefix = o->b_prefix; |
| 3552 | b_prefix = o->a_prefix; |
| 3553 | } else { |
| 3554 | a_prefix = o->a_prefix; |
| 3555 | b_prefix = o->b_prefix; |
| 3556 | } |
| 3557 | |
Jacob Keller | 61cfbc0 | 2016-08-31 16:27:21 -0700 | [diff] [blame] | 3558 | if (o->submodule_format == DIFF_SUBMODULE_LOG && |
| 3559 | (!one->mode || S_ISGITLINK(one->mode)) && |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3560 | (!two->mode || S_ISGITLINK(two->mode)) && |
| 3561 | (!diff_filepair_is_phoney(one, two))) { |
Shourya Shukla | 180b154 | 2020-08-13 01:14:02 +0530 | [diff] [blame] | 3562 | show_submodule_diff_summary(o, one->path ? one->path : two->path, |
Jacob Keller | 602a283 | 2016-08-31 16:27:23 -0700 | [diff] [blame] | 3563 | &one->oid, &two->oid, |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 3564 | two->dirty_submodule); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3565 | return; |
Jacob Keller | fd47ae6 | 2016-08-31 16:27:25 -0700 | [diff] [blame] | 3566 | } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF && |
| 3567 | (!one->mode || S_ISGITLINK(one->mode)) && |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3568 | (!two->mode || S_ISGITLINK(two->mode)) && |
| 3569 | (!diff_filepair_is_phoney(one, two))) { |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 3570 | show_submodule_inline_diff(o, one->path ? one->path : two->path, |
Jacob Keller | fd47ae6 | 2016-08-31 16:27:25 -0700 | [diff] [blame] | 3571 | &one->oid, &two->oid, |
Stefan Beller | f359713 | 2017-06-29 17:07:00 -0700 | [diff] [blame] | 3572 | two->dirty_submodule); |
Jacob Keller | fd47ae6 | 2016-08-31 16:27:25 -0700 | [diff] [blame] | 3573 | return; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3574 | } |
| 3575 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3576 | if (o->flags.allow_textconv) { |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 3577 | textconv_one = get_textconv(o->repo, one); |
| 3578 | textconv_two = get_textconv(o->repo, two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3579 | } |
| 3580 | |
Linus Torvalds | 71b989e | 2008-10-05 15:35:15 -0400 | [diff] [blame] | 3581 | /* Never use a non-valid filename anywhere if at all possible */ |
| 3582 | name_a = DIFF_FILE_VALID(one) ? name_a : name_b; |
| 3583 | name_b = DIFF_FILE_VALID(two) ? name_b : name_a; |
| 3584 | |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 3585 | a_one = quote_two(a_prefix, name_a + (*name_a == '/')); |
| 3586 | b_two = quote_two(b_prefix, name_b + (*name_b == '/')); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3587 | lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null"; |
| 3588 | lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null"; |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3589 | if (diff_filepair_is_phoney(one, two)) { |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3590 | /* |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3591 | * We should only reach this point for pairs generated from |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3592 | * create_filepairs_for_header_only_notifications(). For |
Elijah Newren | 9b08091 | 2022-09-02 03:53:28 +0000 | [diff] [blame] | 3593 | * these, we want to avoid the "/dev/null" special casing |
| 3594 | * above, because we do not want such pairs shown as either |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 3595 | * "new file" or "deleted file" below. |
| 3596 | */ |
| 3597 | lbl[0] = a_one; |
| 3598 | lbl[1] = b_two; |
| 3599 | } |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3600 | 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] | 3601 | if (lbl[0][0] == '/') { |
| 3602 | /* /dev/null */ |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3603 | 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] | 3604 | if (xfrm_msg) |
| 3605 | strbuf_addstr(&header, xfrm_msg); |
Junio C Hamano | c9a3e72 | 2023-08-18 16:59:29 -0700 | [diff] [blame] | 3606 | o->found_changes = 1; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3607 | must_show_header = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3608 | } |
| 3609 | else if (lbl[1][0] == '/') { |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3610 | 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] | 3611 | if (xfrm_msg) |
| 3612 | strbuf_addstr(&header, xfrm_msg); |
Junio C Hamano | c9a3e72 | 2023-08-18 16:59:29 -0700 | [diff] [blame] | 3613 | o->found_changes = 1; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3614 | must_show_header = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3615 | } |
| 3616 | else { |
| 3617 | if (one->mode != two->mode) { |
Jeff King | d9c552f | 2012-11-13 21:12:46 +0530 | [diff] [blame] | 3618 | strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset); |
| 3619 | 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] | 3620 | o->found_changes = 1; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3621 | must_show_header = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3622 | } |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 3623 | if (xfrm_msg) |
| 3624 | strbuf_addstr(&header, xfrm_msg); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3625 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3626 | /* |
| 3627 | * we do not run diff between different kind |
| 3628 | * of objects. |
| 3629 | */ |
| 3630 | if ((one->mode ^ two->mode) & S_IFMT) |
| 3631 | goto free_ab_and_return; |
Jeff King | 0c01857 | 2008-12-09 03:12:28 -0500 | [diff] [blame] | 3632 | if (complete_rewrite && |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3633 | (textconv_one || !diff_filespec_is_binary(o->repo, one)) && |
| 3634 | (textconv_two || !diff_filespec_is_binary(o->repo, two))) { |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3635 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3636 | header.buf, header.len, 0); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3637 | strbuf_reset(&header); |
Jeff King | 3aa1f7c | 2008-12-09 03:13:21 -0500 | [diff] [blame] | 3638 | 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] | 3639 | textconv_one, textconv_two, o); |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 3640 | o->found_changes = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3641 | goto free_ab_and_return; |
| 3642 | } |
| 3643 | } |
| 3644 | |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 3645 | if (o->irreversible_delete && lbl[1][0] == '/') { |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3646 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, |
| 3647 | header.len, 0); |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 3648 | strbuf_reset(&header); |
| 3649 | goto free_ab_and_return; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3650 | } else if (!o->flags.text && |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3651 | ( (!textconv_one && diff_filespec_is_binary(o->repo, one)) || |
| 3652 | (!textconv_two && diff_filespec_is_binary(o->repo, two)) )) { |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 3653 | struct strbuf sb = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | 1aaf69e | 2014-08-16 10:08:06 +0700 | [diff] [blame] | 3654 | if (!one->data && !two->data && |
| 3655 | S_ISREG(one->mode) && S_ISREG(two->mode) && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3656 | !o->flags.binary) { |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 3657 | if (oideq(&one->oid, &two->oid)) { |
Nguyễn Thái Ngọc Duy | 1aaf69e | 2014-08-16 10:08:06 +0700 | [diff] [blame] | 3658 | if (must_show_header) |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3659 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3660 | header.buf, header.len, |
| 3661 | 0); |
Nguyễn Thái Ngọc Duy | 1aaf69e | 2014-08-16 10:08:06 +0700 | [diff] [blame] | 3662 | goto free_ab_and_return; |
| 3663 | } |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3664 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3665 | header.buf, header.len, 0); |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 3666 | strbuf_addf(&sb, "%sBinary files %s and %s differ\n", |
| 3667 | diff_line_prefix(o), lbl[0], lbl[1]); |
| 3668 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES, |
| 3669 | sb.buf, sb.len, 0); |
| 3670 | strbuf_release(&sb); |
Nguyễn Thái Ngọc Duy | 1aaf69e | 2014-08-16 10:08:06 +0700 | [diff] [blame] | 3671 | goto free_ab_and_return; |
| 3672 | } |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3673 | if (fill_mmfile(o->repo, &mf1, one) < 0 || |
| 3674 | fill_mmfile(o->repo, &mf2, two) < 0) |
Jeff King | b337398 | 2010-04-01 20:14:24 -0400 | [diff] [blame] | 3675 | die("unable to read files to diff"); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3676 | /* Quite common confusing case */ |
| 3677 | if (mf1.size == mf2.size && |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3678 | !memcmp(mf1.ptr, mf2.ptr, mf1.size)) { |
| 3679 | if (must_show_header) |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3680 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3681 | header.buf, header.len, 0); |
Junio C Hamano | 0660626 | 2006-05-05 02:41:53 -0700 | [diff] [blame] | 3682 | goto free_ab_and_return; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 3683 | } |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3684 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3685 | strbuf_reset(&header); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3686 | if (o->flags.binary) |
Stefan Beller | 4eed0eb | 2017-06-29 17:07:01 -0700 | [diff] [blame] | 3687 | emit_binary_diff(o, &mf1, &mf2); |
Stefan Beller | 4acaaa7 | 2017-06-29 17:06:58 -0700 | [diff] [blame] | 3688 | else { |
| 3689 | strbuf_addf(&sb, "%sBinary files %s and %s differ\n", |
| 3690 | diff_line_prefix(o), lbl[0], lbl[1]); |
| 3691 | emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES, |
| 3692 | sb.buf, sb.len, 0); |
| 3693 | strbuf_release(&sb); |
| 3694 | } |
Johannes Schindelin | 34a5e1a | 2007-02-25 23:34:54 +0100 | [diff] [blame] | 3695 | o->found_changes = 1; |
Junio C Hamano | 467ddc1 | 2011-02-28 16:11:55 -0800 | [diff] [blame] | 3696 | } else { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3697 | /* Crazy xdl interfaces.. */ |
Jeff King | 0da0e92 | 2019-01-11 17:17:22 -0500 | [diff] [blame] | 3698 | const char *diffopts; |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 3699 | const char *v; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3700 | xpparam_t xpp; |
| 3701 | xdemitconf_t xecfg; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3702 | struct emit_callback ecbdata; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 3703 | const struct userdiff_funcname *pe; |
Junio C Hamano | f258475 | 2007-07-06 00:45:10 -0700 | [diff] [blame] | 3704 | |
Junio C Hamano | b3f01ff | 2012-02-29 18:14:16 -0800 | [diff] [blame] | 3705 | if (must_show_header) { |
Stefan Beller | a29b0a1 | 2017-06-29 17:06:57 -0700 | [diff] [blame] | 3706 | emit_diff_symbol(o, DIFF_SYMBOL_HEADER, |
| 3707 | header.buf, header.len, 0); |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3708 | strbuf_reset(&header); |
| 3709 | } |
| 3710 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 3711 | mf1.size = fill_textconv(o->repo, textconv_one, one, &mf1.ptr); |
| 3712 | mf2.size = fill_textconv(o->repo, textconv_two, two, &mf2.ptr); |
Jeff King | 04427ac | 2008-10-26 00:44:53 -0400 | [diff] [blame] | 3713 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3714 | pe = diff_funcname_pattern(o, one); |
Brandon Casey | 45e7ca0 | 2008-09-18 17:40:48 -0500 | [diff] [blame] | 3715 | if (!pe) |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 3716 | pe = diff_funcname_pattern(o, two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3717 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 3718 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 3719 | memset(&xecfg, 0, sizeof(xecfg)); |
Johannes Schindelin | cd112ce | 2006-06-13 18:45:44 +0200 | [diff] [blame] | 3720 | memset(&ecbdata, 0, sizeof(ecbdata)); |
Johannes Schindelin | 1cdde29 | 2018-08-13 04:33:11 -0700 | [diff] [blame] | 3721 | if (o->flags.suppress_diff_headers) |
| 3722 | lbl[0] = NULL; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3723 | ecbdata.label_path = lbl; |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 3724 | 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] | 3725 | ecbdata.ws_rule = whitespace_rule(o->repo->index, name_b); |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 3726 | if (ecbdata.ws_rule & WS_BLANK_AT_EOF) |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 3727 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
Bo Yang | a3c158d | 2010-05-26 15:08:02 +0800 | [diff] [blame] | 3728 | ecbdata.opt = o; |
Johannes Schindelin | 1cdde29 | 2018-08-13 04:33:11 -0700 | [diff] [blame] | 3729 | if (header.len && !o->flags.suppress_diff_headers) |
| 3730 | ecbdata.header = &header; |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 3731 | xpp.flags = o->xdl_opts; |
Michał Kępień | 296d4a9 | 2020-10-20 08:48:09 +0200 | [diff] [blame] | 3732 | xpp.ignore_regex = o->ignore_regex; |
| 3733 | xpp.ignore_regex_nr = o->ignore_regex_nr; |
Jonathan Tan | 2477ab2 | 2017-11-27 11:47:47 -0800 | [diff] [blame] | 3734 | xpp.anchors = o->anchors; |
| 3735 | xpp.anchors_nr = o->anchors_nr; |
Linus Torvalds | ee1e541 | 2006-05-13 13:23:48 -0700 | [diff] [blame] | 3736 | xecfg.ctxlen = o->context; |
René Scharfe | 6d0e674 | 2008-12-28 19:45:32 +0100 | [diff] [blame] | 3737 | xecfg.interhunkctxlen = o->interhunkcontext; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3738 | xecfg.flags = XDL_EMIT_FUNCNAMES; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3739 | if (o->flags.funccontext) |
René Scharfe | 14937c2 | 2011-10-09 13:36:57 +0200 | [diff] [blame] | 3740 | xecfg.flags |= XDL_EMIT_FUNCCONTEXT; |
Brandon Casey | 45e7ca0 | 2008-09-18 17:40:48 -0500 | [diff] [blame] | 3741 | if (pe) |
Brandon Casey | a013585 | 2008-09-18 17:42:48 -0500 | [diff] [blame] | 3742 | xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags); |
Jeff King | 0da0e92 | 2019-01-11 17:17:22 -0500 | [diff] [blame] | 3743 | |
| 3744 | diffopts = getenv("GIT_DIFF_OPTS"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3745 | if (!diffopts) |
| 3746 | ; |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 3747 | else if (skip_prefix(diffopts, "--unified=", &v)) |
| 3748 | xecfg.ctxlen = strtoul(v, NULL, 10); |
| 3749 | else if (skip_prefix(diffopts, "-u", &v)) |
| 3750 | xecfg.ctxlen = strtoul(v, NULL, 10); |
Jeff King | 0da0e92 | 2019-01-11 17:17:22 -0500 | [diff] [blame] | 3751 | |
Thomas Rast | 77d1a52 | 2012-03-14 19:24:08 +0100 | [diff] [blame] | 3752 | if (o->word_diff) |
| 3753 | init_diff_words_data(&ecbdata, o, one, two); |
Jeff King | 9346d6d | 2018-11-02 02:35:45 -0400 | [diff] [blame] | 3754 | if (xdi_diff_outf(&mf1, &mf2, NULL, fn_out_consume, |
| 3755 | &ecbdata, &xpp, &xecfg)) |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 3756 | die("unable to generate diff for %s", one->path); |
Thomas Rast | 882749a | 2010-04-14 17:59:06 +0200 | [diff] [blame] | 3757 | if (o->word_diff) |
Johannes Schindelin | f59a59e | 2006-07-28 23:56:15 +0200 | [diff] [blame] | 3758 | free_diff_words_data(&ecbdata); |
Jeff King | 04427ac | 2008-10-26 00:44:53 -0400 | [diff] [blame] | 3759 | if (textconv_one) |
| 3760 | free(mf1.ptr); |
| 3761 | if (textconv_two) |
| 3762 | free(mf2.ptr); |
René Scharfe | 8cfe5f1 | 2009-07-02 00:01:43 +0200 | [diff] [blame] | 3763 | xdiff_clear_find_func(&xecfg); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3764 | } |
| 3765 | |
| 3766 | free_ab_and_return: |
Greg Bacon | 3e97c7c | 2009-11-19 15:12:24 -0600 | [diff] [blame] | 3767 | strbuf_release(&header); |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3768 | diff_free_filespec_data(one); |
| 3769 | diff_free_filespec_data(two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3770 | free(a_one); |
| 3771 | free(b_two); |
| 3772 | return; |
| 3773 | } |
| 3774 | |
Patrick Steinhardt | b567004 | 2024-06-07 08:37:39 +0200 | [diff] [blame] | 3775 | static const char *get_compact_summary(const struct diff_filepair *p, int is_renamed) |
Nguyễn Thái Ngọc Duy | ddf88fa | 2018-02-24 21:09:59 +0700 | [diff] [blame] | 3776 | { |
| 3777 | if (!is_renamed) { |
| 3778 | if (p->status == DIFF_STATUS_ADDED) { |
| 3779 | if (S_ISLNK(p->two->mode)) |
| 3780 | return "new +l"; |
| 3781 | else if ((p->two->mode & 0777) == 0755) |
| 3782 | return "new +x"; |
| 3783 | else |
| 3784 | return "new"; |
| 3785 | } else if (p->status == DIFF_STATUS_DELETED) |
| 3786 | return "gone"; |
| 3787 | } |
| 3788 | if (S_ISLNK(p->one->mode) && !S_ISLNK(p->two->mode)) |
| 3789 | return "mode -l"; |
| 3790 | else if (!S_ISLNK(p->one->mode) && S_ISLNK(p->two->mode)) |
| 3791 | return "mode +l"; |
| 3792 | else if ((p->one->mode & 0777) == 0644 && |
| 3793 | (p->two->mode & 0777) == 0755) |
| 3794 | return "mode +x"; |
| 3795 | else if ((p->one->mode & 0777) == 0755 && |
| 3796 | (p->two->mode & 0777) == 0644) |
| 3797 | return "mode -x"; |
| 3798 | return NULL; |
| 3799 | } |
| 3800 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3801 | static void builtin_diffstat(const char *name_a, const char *name_b, |
| 3802 | struct diff_filespec *one, |
| 3803 | struct diff_filespec *two, |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 3804 | struct diffstat_t *diffstat, |
Johannes Schindelin | 0d21efa | 2006-06-14 17:40:23 +0200 | [diff] [blame] | 3805 | struct diff_options *o, |
Linus Torvalds | 74faaa1 | 2012-10-17 10:00:37 -0700 | [diff] [blame] | 3806 | struct diff_filepair *p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3807 | { |
| 3808 | mmfile_t mf1, mf2; |
| 3809 | struct diffstat_file *data; |
Thomas Guyot-Sionnest | ff0c7fa | 2020-09-24 03:41:41 -0400 | [diff] [blame] | 3810 | int may_differ; |
Linus Torvalds | 74faaa1 | 2012-10-17 10:00:37 -0700 | [diff] [blame] | 3811 | int complete_rewrite = 0; |
| 3812 | |
| 3813 | if (!DIFF_PAIR_UNMERGED(p)) { |
| 3814 | if (p->status == DIFF_STATUS_MODIFIED && p->score) |
| 3815 | complete_rewrite = 1; |
| 3816 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3817 | |
| 3818 | data = diffstat_add(diffstat, name_a, name_b); |
Junio C Hamano | 99bfd40 | 2012-11-27 11:17:14 -0800 | [diff] [blame] | 3819 | 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] | 3820 | if (o->flags.stat_with_summary) |
| 3821 | data->comments = get_compact_summary(p, data->is_renamed); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3822 | |
| 3823 | if (!one || !two) { |
| 3824 | data->is_unmerged = 1; |
| 3825 | return; |
| 3826 | } |
Jeff King | ded0abc | 2011-02-19 03:04:56 -0500 | [diff] [blame] | 3827 | |
Thomas Guyot-Sionnest | ff0c7fa | 2020-09-24 03:41:41 -0400 | [diff] [blame] | 3828 | /* saves some reads if true, not a guarantee of diff outcome */ |
| 3829 | may_differ = !(one->oid_valid && two->oid_valid && |
| 3830 | oideq(&one->oid, &two->oid)); |
Zbigniew Jędrzejewski-Szmek | 352ca4e | 2012-05-01 19:10:15 +0200 | [diff] [blame] | 3831 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3832 | if (diff_filespec_is_binary(o->repo, one) || |
| 3833 | diff_filespec_is_binary(o->repo, two)) { |
Jeff King | ded0abc | 2011-02-19 03:04:56 -0500 | [diff] [blame] | 3834 | data->is_binary = 1; |
Thomas Guyot-Sionnest | ff0c7fa | 2020-09-24 03:41:41 -0400 | [diff] [blame] | 3835 | if (!may_differ) { |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 3836 | data->added = 0; |
| 3837 | data->deleted = 0; |
| 3838 | } else { |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3839 | data->added = diff_filespec_size(o->repo, two); |
| 3840 | data->deleted = diff_filespec_size(o->repo, one); |
Zbigniew Jędrzejewski-Szmek | e18872b | 2012-05-01 19:10:14 +0200 | [diff] [blame] | 3841 | } |
Jeff King | ded0abc | 2011-02-19 03:04:56 -0500 | [diff] [blame] | 3842 | } |
| 3843 | |
| 3844 | else if (complete_rewrite) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 3845 | diff_populate_filespec(o->repo, one, NULL); |
| 3846 | diff_populate_filespec(o->repo, two, NULL); |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 3847 | data->deleted = count_lines(one->data, one->size); |
| 3848 | data->added = count_lines(two->data, two->size); |
Junio C Hamano | 710158e | 2006-04-25 23:40:09 -0700 | [diff] [blame] | 3849 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3850 | |
Thomas Guyot-Sionnest | ff0c7fa | 2020-09-24 03:41:41 -0400 | [diff] [blame] | 3851 | else if (may_differ) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3852 | /* Crazy xdl interfaces.. */ |
| 3853 | xpparam_t xpp; |
| 3854 | xdemitconf_t xecfg; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3855 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3856 | if (fill_mmfile(o->repo, &mf1, one) < 0 || |
| 3857 | fill_mmfile(o->repo, &mf2, two) < 0) |
Jeff King | ded0abc | 2011-02-19 03:04:56 -0500 | [diff] [blame] | 3858 | die("unable to read files to diff"); |
| 3859 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 3860 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 3861 | memset(&xecfg, 0, sizeof(xecfg)); |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 3862 | xpp.flags = o->xdl_opts; |
Michał Kępień | 296d4a9 | 2020-10-20 08:48:09 +0200 | [diff] [blame] | 3863 | xpp.ignore_regex = o->ignore_regex; |
| 3864 | xpp.ignore_regex_nr = o->ignore_regex_nr; |
Jonathan Tan | 2477ab2 | 2017-11-27 11:47:47 -0800 | [diff] [blame] | 3865 | xpp.anchors = o->anchors; |
| 3866 | xpp.anchors_nr = o->anchors_nr; |
Junio C Hamano | f01cae9 | 2011-09-22 10:54:47 -0700 | [diff] [blame] | 3867 | xecfg.ctxlen = o->context; |
| 3868 | xecfg.interhunkctxlen = o->interhunkcontext; |
Ævar Arnfjörð Bjarmason | 5d93460 | 2021-04-12 19:15:29 +0200 | [diff] [blame] | 3869 | xecfg.flags = XDL_EMIT_NO_HUNK_HDR; |
| 3870 | if (xdi_diff_outf(&mf1, &mf2, NULL, |
Jeff King | 3b40a09 | 2018-11-02 02:36:06 -0400 | [diff] [blame] | 3871 | diffstat_consume, diffstat, &xpp, &xecfg)) |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 3872 | die("unable to generate diffstat for %s", one->path); |
Matthew Rogers | 1cf3d5d | 2020-08-20 00:41:32 +0000 | [diff] [blame] | 3873 | |
| 3874 | if (DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two)) { |
| 3875 | struct diffstat_file *file = |
| 3876 | diffstat->files[diffstat->nr - 1]; |
| 3877 | /* |
| 3878 | * Omit diffstats of modified files where nothing changed. |
Thomas Guyot-Sionnest | ff0c7fa | 2020-09-24 03:41:41 -0400 | [diff] [blame] | 3879 | * Even if may_differ, this might be the case due to |
Matthew Rogers | 1cf3d5d | 2020-08-20 00:41:32 +0000 | [diff] [blame] | 3880 | * ignoring whitespace changes, etc. |
| 3881 | * |
| 3882 | * But note that we special-case additions, deletions, |
| 3883 | * renames, and mode changes as adding an empty file, |
| 3884 | * for example is still of interest. |
| 3885 | */ |
| 3886 | if ((p->status == DIFF_STATUS_MODIFIED) |
| 3887 | && !file->added |
| 3888 | && !file->deleted |
| 3889 | && one->mode == two->mode) { |
| 3890 | free_diffstat_file(file); |
| 3891 | diffstat->nr--; |
| 3892 | } |
| 3893 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3894 | } |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3895 | |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3896 | diff_free_filespec_data(one); |
| 3897 | diff_free_filespec_data(two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3898 | } |
| 3899 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3900 | 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] | 3901 | const char *attr_path, |
Junio C Hamano | 5ff10dd | 2008-06-26 15:34:54 -0700 | [diff] [blame] | 3902 | struct diff_filespec *one, |
| 3903 | struct diff_filespec *two, |
| 3904 | struct diff_options *o) |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3905 | { |
| 3906 | mmfile_t mf1, mf2; |
| 3907 | struct checkdiff_t data; |
| 3908 | |
| 3909 | if (!two) |
| 3910 | return; |
| 3911 | |
| 3912 | memset(&data, 0, sizeof(data)); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3913 | data.filename = name_b ? name_b : name_a; |
| 3914 | data.lineno = 0; |
Junio C Hamano | 1ba111d | 2008-06-26 15:36:34 -0700 | [diff] [blame] | 3915 | data.o = o; |
Nguyễn Thái Ngọc Duy | 26d024e | 2018-09-21 17:57:37 +0200 | [diff] [blame] | 3916 | 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] | 3917 | 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] | 3918 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3919 | if (fill_mmfile(o->repo, &mf1, one) < 0 || |
| 3920 | fill_mmfile(o->repo, &mf2, two) < 0) |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3921 | die("unable to read files to diff"); |
| 3922 | |
Junio C Hamano | 5ff10dd | 2008-06-26 15:34:54 -0700 | [diff] [blame] | 3923 | /* |
| 3924 | * All the other codepaths check both sides, but not checking |
| 3925 | * the "old" side here is deliberate. We are checking the newly |
| 3926 | * introduced changes, and as long as the "new" side is text, we |
| 3927 | * can and should check what it introduces. |
| 3928 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 3929 | if (diff_filespec_is_binary(o->repo, two)) |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3930 | goto free_and_return; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3931 | else { |
| 3932 | /* Crazy xdl interfaces.. */ |
| 3933 | xpparam_t xpp; |
| 3934 | xdemitconf_t xecfg; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3935 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 3936 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 3937 | memset(&xecfg, 0, sizeof(xecfg)); |
Junio C Hamano | c35539e | 2008-08-20 11:47:55 -0700 | [diff] [blame] | 3938 | xecfg.ctxlen = 1; /* at least one context line */ |
René Scharfe | 582aa00 | 2010-05-02 15:04:41 +0200 | [diff] [blame] | 3939 | xpp.flags = 0; |
Jeff King | 75ab763 | 2018-11-02 02:39:03 -0400 | [diff] [blame] | 3940 | if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume_hunk, |
| 3941 | checkdiff_consume, &data, |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 3942 | &xpp, &xecfg)) |
| 3943 | die("unable to generate checkdiff for %s", one->path); |
Junio C Hamano | 877f23c | 2008-06-26 15:36:59 -0700 | [diff] [blame] | 3944 | |
Junio C Hamano | 467babf | 2009-09-03 23:39:43 -0700 | [diff] [blame] | 3945 | if (data.ws_rule & WS_BLANK_AT_EOF) { |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 3946 | struct emit_callback ecbdata; |
| 3947 | int blank_at_eof; |
| 3948 | |
| 3949 | ecbdata.ws_rule = data.ws_rule; |
| 3950 | check_blank_at_eof(&mf1, &mf2, &ecbdata); |
Christoph Mallon | 8837d33 | 2010-10-10 19:24:06 +0200 | [diff] [blame] | 3951 | blank_at_eof = ecbdata.blank_at_eof_in_postimage; |
Junio C Hamano | d68fe26 | 2009-09-14 22:05:57 -0700 | [diff] [blame] | 3952 | |
Junio C Hamano | 467babf | 2009-09-03 23:39:43 -0700 | [diff] [blame] | 3953 | if (blank_at_eof) { |
| 3954 | static char *err; |
| 3955 | if (!err) |
| 3956 | err = whitespace_error_string(WS_BLANK_AT_EOF); |
| 3957 | fprintf(o->file, "%s:%d: %s.\n", |
| 3958 | data.filename, blank_at_eof, err); |
| 3959 | data.status = 1; /* report errors */ |
| 3960 | } |
Junio C Hamano | 877f23c | 2008-06-26 15:36:59 -0700 | [diff] [blame] | 3961 | } |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3962 | } |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 3963 | free_and_return: |
| 3964 | diff_free_filespec_data(one); |
| 3965 | diff_free_filespec_data(two); |
Wincent Colaiuta | 62c6489 | 2007-12-13 21:24:52 +0100 | [diff] [blame] | 3966 | if (data.status) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 3967 | o->flags.check_failed = 1; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 3968 | } |
| 3969 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3970 | struct diff_filespec *alloc_filespec(const char *path) |
| 3971 | { |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 3972 | struct diff_filespec *spec; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3973 | |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 3974 | FLEXPTR_ALLOC_STR(spec, path, path); |
Linus Torvalds | 9fb8841 | 2007-10-25 11:19:10 -0700 | [diff] [blame] | 3975 | spec->count = 1; |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 3976 | spec->is_binary = -1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3977 | return spec; |
| 3978 | } |
| 3979 | |
Linus Torvalds | 9fb8841 | 2007-10-25 11:19:10 -0700 | [diff] [blame] | 3980 | void free_filespec(struct diff_filespec *spec) |
| 3981 | { |
| 3982 | if (!--spec->count) { |
| 3983 | diff_free_filespec_data(spec); |
| 3984 | free(spec); |
| 3985 | } |
| 3986 | } |
| 3987 | |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 3988 | void fill_filespec(struct diff_filespec *spec, const struct object_id *oid, |
| 3989 | int oid_valid, unsigned short mode) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3990 | { |
| 3991 | if (mode) { |
| 3992 | spec->mode = canon_mode(mode); |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 3993 | oidcpy(&spec->oid, oid); |
| 3994 | spec->oid_valid = oid_valid; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 3995 | } |
| 3996 | } |
| 3997 | |
| 3998 | /* |
Jakub Narebski | 5adf317 | 2007-05-26 00:37:40 +0200 | [diff] [blame] | 3999 | * 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] | 4000 | * the work tree has that object contents, return true, so that |
| 4001 | * prepare_temp_file() does not have to inflate and extract. |
| 4002 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4003 | static int reuse_worktree_file(struct index_state *istate, |
| 4004 | const char *name, |
| 4005 | const struct object_id *oid, |
| 4006 | int want_file) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4007 | { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 4008 | const struct cache_entry *ce; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4009 | struct stat st; |
| 4010 | int pos, len; |
| 4011 | |
Junio C Hamano | 150115a | 2009-03-22 15:26:07 -0700 | [diff] [blame] | 4012 | /* |
| 4013 | * We do not read the cache ourselves here, because the |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4014 | * benchmark with my previous version that always reads cache |
| 4015 | * shows that it makes things worse for diff-tree comparing |
| 4016 | * two linux-2.6 kernel trees in an already checked out work |
| 4017 | * tree. This is because most diff-tree comparisons deal with |
| 4018 | * only a small number of files, while reading the cache is |
| 4019 | * expensive for a large project, and its cost outweighs the |
| 4020 | * savings we get by not inflating the object to a temporary |
| 4021 | * file. Practically, this code only helps when we are used |
| 4022 | * by diff-cache --cached, which does read the cache before |
| 4023 | * calling us. |
| 4024 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4025 | if (!istate->cache) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4026 | return 0; |
| 4027 | |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 4028 | /* We want to avoid the working directory if our caller |
| 4029 | * doesn't need the data in a normal file, this system |
| 4030 | * is rather slow with its stat/open/mmap/close syscalls, |
| 4031 | * and the object is contained in a pack file. The pack |
| 4032 | * is probably already open and will be faster to obtain |
| 4033 | * the data through than the working directory. Loose |
| 4034 | * objects however would tend to be slower as they need |
| 4035 | * to be individually opened and inflated. |
| 4036 | */ |
brian m. carlson | 14c3c80 | 2018-05-02 00:25:33 +0000 | [diff] [blame] | 4037 | if (!FAST_WORKING_DIRECTORY && !want_file && has_object_pack(oid)) |
Shawn O. Pearce | 1510fea | 2006-12-14 06:15:57 -0500 | [diff] [blame] | 4038 | return 0; |
| 4039 | |
Jeff King | 06dec43 | 2016-07-22 11:27:53 -0400 | [diff] [blame] | 4040 | /* |
| 4041 | * Similarly, if we'd have to convert the file contents anyway, that |
| 4042 | * makes the optimization not worthwhile. |
| 4043 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4044 | if (!want_file && would_convert_to_git(istate, name)) |
Jeff King | 06dec43 | 2016-07-22 11:27:53 -0400 | [diff] [blame] | 4045 | return 0; |
| 4046 | |
Derrick Stolee | ad90da7 | 2021-09-08 11:23:56 +0000 | [diff] [blame] | 4047 | /* |
| 4048 | * If this path does not match our sparse-checkout definition, |
| 4049 | * then the file will not be in the working directory. |
| 4050 | */ |
| 4051 | if (!path_in_sparse_checkout(name, istate)) |
| 4052 | return 0; |
| 4053 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4054 | len = strlen(name); |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4055 | pos = index_name_pos(istate, name, len); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4056 | if (pos < 0) |
| 4057 | return 0; |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4058 | ce = istate->cache[pos]; |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 4059 | |
| 4060 | /* |
| 4061 | * This is not the sha1 we are looking for, or |
| 4062 | * unreusable because it is not a regular file. |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4063 | */ |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 4064 | if (!oideq(oid, &ce->oid) || !S_ISREG(ce->ce_mode)) |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 4065 | return 0; |
| 4066 | |
| 4067 | /* |
Junio C Hamano | 150115a | 2009-03-22 15:26:07 -0700 | [diff] [blame] | 4068 | * If ce is marked as "assume unchanged", there is no |
| 4069 | * guarantee that work tree matches what we are looking for. |
| 4070 | */ |
Nguyễn Thái Ngọc Duy | b4d1690 | 2009-08-20 20:46:58 +0700 | [diff] [blame] | 4071 | if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) |
Junio C Hamano | 150115a | 2009-03-22 15:26:07 -0700 | [diff] [blame] | 4072 | return 0; |
| 4073 | |
| 4074 | /* |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 4075 | * If ce matches the file in the work tree, we can reuse it. |
| 4076 | */ |
| 4077 | if (ce_uptodate(ce) || |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4078 | (!lstat(name, &st) && !ie_match_stat(istate, ce, &st, 0))) |
Junio C Hamano | eadb583 | 2008-01-18 23:45:24 -0800 | [diff] [blame] | 4079 | return 1; |
| 4080 | |
| 4081 | return 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4082 | } |
| 4083 | |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 4084 | static int diff_populate_gitlink(struct diff_filespec *s, int size_only) |
| 4085 | { |
Jeff King | b1ddfb9 | 2016-02-22 17:45:08 -0500 | [diff] [blame] | 4086 | struct strbuf buf = STRBUF_INIT; |
Patrick Steinhardt | b567004 | 2024-06-07 08:37:39 +0200 | [diff] [blame] | 4087 | const char *dirty = ""; |
Junio C Hamano | 8e08b41 | 2010-01-16 18:42:53 +0100 | [diff] [blame] | 4088 | |
| 4089 | /* Are we looking at the work tree? */ |
Jens Lehmann | 85adbf2 | 2010-03-12 22:23:52 +0100 | [diff] [blame] | 4090 | if (s->dirty_submodule) |
Junio C Hamano | 8e08b41 | 2010-01-16 18:42:53 +0100 | [diff] [blame] | 4091 | dirty = "-dirty"; |
| 4092 | |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 4093 | strbuf_addf(&buf, "Subproject commit %s%s\n", |
| 4094 | oid_to_hex(&s->oid), dirty); |
Jeff King | b1ddfb9 | 2016-02-22 17:45:08 -0500 | [diff] [blame] | 4095 | s->size = buf.len; |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 4096 | if (size_only) { |
| 4097 | s->data = NULL; |
Jeff King | b1ddfb9 | 2016-02-22 17:45:08 -0500 | [diff] [blame] | 4098 | strbuf_release(&buf); |
| 4099 | } else { |
| 4100 | s->data = strbuf_detach(&buf, NULL); |
| 4101 | s->should_free = 1; |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 4102 | } |
| 4103 | return 0; |
| 4104 | } |
| 4105 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4106 | /* |
| 4107 | * While doing rename detection and pickaxe operation, we may need to |
| 4108 | * grab the data for the blob (or file) for our own in-core comparison. |
| 4109 | * diff_filespec has data and size fields for this purpose. |
| 4110 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4111 | int diff_populate_filespec(struct repository *r, |
| 4112 | struct diff_filespec *s, |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4113 | const struct diff_populate_filespec_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4114 | { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4115 | int size_only = options ? options->check_size_only : 0; |
| 4116 | int check_binary = options ? options->check_binary : 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4117 | int err = 0; |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 4118 | int conv_flags = global_conv_flags_eol; |
Junio C Hamano | 5430bb2 | 2013-06-24 14:35:04 -0700 | [diff] [blame] | 4119 | /* |
| 4120 | * demote FAIL to WARN to allow inspecting the situation |
| 4121 | * instead of refusing. |
| 4122 | */ |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 4123 | if (conv_flags & CONV_EOL_RNDTRP_DIE) |
| 4124 | conv_flags = CONV_EOL_RNDTRP_WARN; |
Junio C Hamano | 5430bb2 | 2013-06-24 14:35:04 -0700 | [diff] [blame] | 4125 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4126 | if (!DIFF_FILE_VALID(s)) |
| 4127 | die("internal error: asking to populate invalid file."); |
| 4128 | if (S_ISDIR(s->mode)) |
| 4129 | return -1; |
| 4130 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4131 | if (s->data) |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 4132 | return 0; |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 4133 | |
Junio C Hamano | 6e0b8ed | 2007-05-07 01:14:21 -0700 | [diff] [blame] | 4134 | if (size_only && 0 < s->size) |
| 4135 | return 0; |
| 4136 | |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 4137 | if (S_ISGITLINK(s->mode)) |
Linus Torvalds | 0478675 | 2007-04-15 11:14:28 -0700 | [diff] [blame] | 4138 | return diff_populate_gitlink(s, size_only); |
| 4139 | |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4140 | if (!s->oid_valid || |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4141 | reuse_worktree_file(r->index, s->path, &s->oid, 0)) { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 4142 | struct strbuf buf = STRBUF_INIT; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4143 | struct stat st; |
| 4144 | int fd; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 4145 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4146 | if (lstat(s->path, &st) < 0) { |
Andrey Okoshkin | 10e0ca8 | 2017-10-27 12:33:25 +0300 | [diff] [blame] | 4147 | err_empty: |
| 4148 | err = -1; |
| 4149 | empty: |
| 4150 | s->data = (char *)""; |
| 4151 | s->size = 0; |
| 4152 | return err; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4153 | } |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 4154 | s->size = xsize_t(st.st_size); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4155 | if (!s->size) |
| 4156 | goto empty; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4157 | if (S_ISLNK(st.st_mode)) { |
Linus Torvalds | cf219d8 | 2008-12-17 10:26:13 -0800 | [diff] [blame] | 4158 | struct strbuf sb = STRBUF_INIT; |
| 4159 | |
| 4160 | if (strbuf_readlink(&sb, s->path, s->size)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4161 | goto err_empty; |
René Scharfe | 0956a6d | 2008-12-18 17:56:51 +0100 | [diff] [blame] | 4162 | s->size = sb.len; |
| 4163 | s->data = strbuf_detach(&sb, NULL); |
Linus Torvalds | cf219d8 | 2008-12-17 10:26:13 -0800 | [diff] [blame] | 4164 | s->should_free = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4165 | return 0; |
| 4166 | } |
Junio C Hamano | 12426e1 | 2017-03-01 18:04:44 +0100 | [diff] [blame] | 4167 | |
| 4168 | /* |
| 4169 | * Even if the caller would be happy with getting |
| 4170 | * only the size, we cannot return early at this |
| 4171 | * point if the path requires us to run the content |
| 4172 | * conversion. |
| 4173 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4174 | if (size_only && !would_convert_to_git(r->index, s->path)) |
Linus Torvalds | cf219d8 | 2008-12-17 10:26:13 -0800 | [diff] [blame] | 4175 | return 0; |
Junio C Hamano | 12426e1 | 2017-03-01 18:04:44 +0100 | [diff] [blame] | 4176 | |
| 4177 | /* |
| 4178 | * Note: this check uses xsize_t(st.st_size) that may |
| 4179 | * not be the true size of the blob after it goes |
| 4180 | * through convert_to_git(). This may not strictly be |
| 4181 | * correct, but the whole point of big_file_threshold |
| 4182 | * and is_binary check being that we want to avoid |
| 4183 | * opening the file and inspecting the contents, this |
| 4184 | * is probably fine. |
| 4185 | */ |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4186 | if (check_binary && |
Nguyễn Thái Ngọc Duy | 6bf3b81 | 2014-08-16 10:08:05 +0700 | [diff] [blame] | 4187 | s->size > big_file_threshold && s->is_binary == -1) { |
| 4188 | s->is_binary = 1; |
| 4189 | return 0; |
| 4190 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4191 | fd = open(s->path, O_RDONLY); |
| 4192 | if (fd < 0) |
| 4193 | goto err_empty; |
Shawn O. Pearce | c4712e4 | 2006-12-24 00:47:23 -0500 | [diff] [blame] | 4194 | 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] | 4195 | close(fd); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4196 | s->should_munmap = 1; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 4197 | |
| 4198 | /* |
| 4199 | * Convert from working tree format to canonical git format |
| 4200 | */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4201 | 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] | 4202 | size_t size = 0; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 4203 | munmap(s->data, s->size); |
| 4204 | s->should_munmap = 0; |
René Scharfe | c32f749 | 2007-10-21 11:23:49 +0200 | [diff] [blame] | 4205 | s->data = strbuf_detach(&buf, &size); |
| 4206 | s->size = size; |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 4207 | s->should_free = 1; |
| 4208 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4209 | } |
| 4210 | else { |
Jonathan Tan | c14b6f8 | 2020-04-07 15:11:42 -0700 | [diff] [blame] | 4211 | struct object_info info = { |
| 4212 | .sizep = &s->size |
| 4213 | }; |
| 4214 | |
| 4215 | if (!(size_only || check_binary)) |
| 4216 | /* |
| 4217 | * Set contentp, since there is no chance that merely |
| 4218 | * the size is sufficient. |
| 4219 | */ |
| 4220 | info.contentp = &s->data; |
| 4221 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 4222 | if (options && options->missing_object_cb) { |
| 4223 | if (!oid_object_info_extended(r, &s->oid, &info, |
| 4224 | OBJECT_INFO_LOOKUP_REPLACE | |
| 4225 | OBJECT_INFO_SKIP_FETCH_OBJECT)) |
| 4226 | goto object_read; |
| 4227 | options->missing_object_cb(options->missing_object_data); |
| 4228 | } |
Jonathan Tan | c14b6f8 | 2020-04-07 15:11:42 -0700 | [diff] [blame] | 4229 | if (oid_object_info_extended(r, &s->oid, &info, |
| 4230 | OBJECT_INFO_LOOKUP_REPLACE)) |
| 4231 | die("unable to read %s", oid_to_hex(&s->oid)); |
| 4232 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 4233 | object_read: |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4234 | if (size_only || check_binary) { |
Nguyễn Thái Ngọc Duy | 6bf3b81 | 2014-08-16 10:08:05 +0700 | [diff] [blame] | 4235 | if (size_only) |
| 4236 | return 0; |
| 4237 | if (s->size > big_file_threshold && s->is_binary == -1) { |
| 4238 | s->is_binary = 1; |
| 4239 | return 0; |
| 4240 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4241 | } |
Jonathan Tan | c14b6f8 | 2020-04-07 15:11:42 -0700 | [diff] [blame] | 4242 | if (!info.contentp) { |
| 4243 | info.contentp = &s->data; |
| 4244 | if (oid_object_info_extended(r, &s->oid, &info, |
| 4245 | OBJECT_INFO_LOOKUP_REPLACE)) |
| 4246 | die("unable to read %s", oid_to_hex(&s->oid)); |
| 4247 | } |
Nguyễn Thái Ngọc Duy | 6bf3b81 | 2014-08-16 10:08:05 +0700 | [diff] [blame] | 4248 | s->should_free = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4249 | } |
| 4250 | return 0; |
| 4251 | } |
| 4252 | |
Junio C Hamano | 8ae92e6 | 2007-10-02 21:01:03 -0700 | [diff] [blame] | 4253 | void diff_free_filespec_blob(struct diff_filespec *s) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4254 | { |
| 4255 | if (s->should_free) |
| 4256 | free(s->data); |
| 4257 | else if (s->should_munmap) |
| 4258 | munmap(s->data, s->size); |
Junio C Hamano | fc3abdf | 2007-05-03 13:05:48 -0700 | [diff] [blame] | 4259 | |
| 4260 | if (s->should_free || s->should_munmap) { |
| 4261 | s->should_free = s->should_munmap = 0; |
| 4262 | s->data = NULL; |
| 4263 | } |
Jeff King | eede7b7 | 2007-09-25 15:29:42 -0400 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | void diff_free_filespec_data(struct diff_filespec *s) |
| 4267 | { |
Jinoh Kang | 2469593 | 2020-11-06 17:14:52 +0000 | [diff] [blame] | 4268 | if (!s) |
| 4269 | return; |
| 4270 | |
Junio C Hamano | 8ae92e6 | 2007-10-02 21:01:03 -0700 | [diff] [blame] | 4271 | diff_free_filespec_blob(s); |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 4272 | FREE_AND_NULL(s->cnt_data); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4273 | } |
| 4274 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4275 | static void prep_temp_blob(struct index_state *istate, |
| 4276 | const char *path, struct diff_tempfile *temp, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4277 | void *blob, |
| 4278 | unsigned long size, |
brian m. carlson | 09bdff2 | 2016-06-24 23:09:29 +0000 | [diff] [blame] | 4279 | const struct object_id *oid, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4280 | int mode) |
| 4281 | { |
Johannes Schindelin | 4e218f5 | 2009-03-21 12:42:52 +0100 | [diff] [blame] | 4282 | struct strbuf buf = STRBUF_INIT; |
David Aguilar | 003b33a | 2009-05-31 01:35:52 -0700 | [diff] [blame] | 4283 | char *path_dup = xstrdup(path); |
| 4284 | const char *base = basename(path_dup); |
brian m. carlson | c397aac | 2020-03-16 18:05:03 +0000 | [diff] [blame] | 4285 | struct checkout_metadata meta; |
| 4286 | |
| 4287 | init_checkout_metadata(&meta, NULL, NULL, oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4288 | |
René Scharfe | 8af7593 | 2022-04-20 22:30:10 +0200 | [diff] [blame] | 4289 | temp->tempfile = mks_tempfile_dt("git-blob-XXXXXX", base); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 4290 | if (!temp->tempfile) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 4291 | die_errno("unable to create temp-file"); |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4292 | if (convert_to_working_tree(istate, path, |
brian m. carlson | c397aac | 2020-03-16 18:05:03 +0000 | [diff] [blame] | 4293 | (const char *)blob, (size_t)size, &buf, &meta)) { |
Johannes Schindelin | 4e218f5 | 2009-03-21 12:42:52 +0100 | [diff] [blame] | 4294 | blob = buf.buf; |
| 4295 | size = buf.len; |
| 4296 | } |
Junio C Hamano | c50424a | 2017-09-25 15:24:06 +0900 | [diff] [blame] | 4297 | if (write_in_full(temp->tempfile->fd, blob, size) < 0 || |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 4298 | close_tempfile_gently(temp->tempfile)) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 4299 | die_errno("unable to write temp-file"); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 4300 | temp->name = get_tempfile_path(temp->tempfile); |
brian m. carlson | 09bdff2 | 2016-06-24 23:09:29 +0000 | [diff] [blame] | 4301 | oid_to_hex_r(temp->hex, oid); |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 4302 | xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode); |
Johannes Schindelin | 4e218f5 | 2009-03-21 12:42:52 +0100 | [diff] [blame] | 4303 | strbuf_release(&buf); |
David Aguilar | 003b33a | 2009-05-31 01:35:52 -0700 | [diff] [blame] | 4304 | free(path_dup); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4305 | } |
| 4306 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4307 | 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] | 4308 | struct diff_filespec *one) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4309 | { |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 4310 | struct diff_tempfile *temp = claim_diff_tempfile(); |
| 4311 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4312 | if (!DIFF_FILE_VALID(one)) { |
| 4313 | not_a_valid_file: |
| 4314 | /* A '-' entry produces this for file-2, and |
| 4315 | * a '+' entry produces this for file-1. |
| 4316 | */ |
| 4317 | temp->name = "/dev/null"; |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 4318 | xsnprintf(temp->hex, sizeof(temp->hex), "."); |
| 4319 | xsnprintf(temp->mode, sizeof(temp->mode), "."); |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 4320 | return temp; |
| 4321 | } |
| 4322 | |
Thomas Rast | aba4727 | 2014-02-16 17:52:34 +0100 | [diff] [blame] | 4323 | if (!S_ISGITLINK(one->mode) && |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4324 | (!one->oid_valid || |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4325 | reuse_worktree_file(r->index, one->path, &one->oid, 1))) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4326 | struct stat st; |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4327 | if (lstat(one->path, &st) < 0) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4328 | if (errno == ENOENT) |
| 4329 | goto not_a_valid_file; |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4330 | die_errno("stat(%s)", one->path); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4331 | } |
| 4332 | if (S_ISLNK(st.st_mode)) { |
Jeff King | 3cd7388 | 2009-05-25 06:46:09 -0400 | [diff] [blame] | 4333 | struct strbuf sb = STRBUF_INIT; |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4334 | if (strbuf_readlink(&sb, one->path, st.st_size) < 0) |
| 4335 | die_errno("readlink(%s)", one->path); |
| 4336 | 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] | 4337 | (one->oid_valid ? |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 4338 | &one->oid : null_oid()), |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4339 | (one->oid_valid ? |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4340 | one->mode : S_IFLNK)); |
Jeff King | 3cd7388 | 2009-05-25 06:46:09 -0400 | [diff] [blame] | 4341 | strbuf_release(&sb); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4342 | } |
| 4343 | else { |
| 4344 | /* we can borrow from the file in the work tree */ |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4345 | temp->name = one->path; |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4346 | if (!one->oid_valid) |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 4347 | oid_to_hex_r(temp->hex, null_oid()); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4348 | else |
René Scharfe | 2490574 | 2017-01-28 23:03:03 +0100 | [diff] [blame] | 4349 | oid_to_hex_r(temp->hex, &one->oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4350 | /* Even though we may sometimes borrow the |
| 4351 | * contents from the work tree, we always want |
| 4352 | * one->mode. mode is trustworthy even when |
Brandon Williams | 7401415 | 2017-05-30 10:30:52 -0700 | [diff] [blame] | 4353 | * !(one->oid_valid), as long as |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4354 | * DIFF_FILE_VALID(one). |
| 4355 | */ |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 4356 | xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4357 | } |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 4358 | return temp; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4359 | } |
| 4360 | else { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 4361 | if (diff_populate_filespec(r, one, NULL)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4362 | die("cannot read data blob for %s", one->path); |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4363 | 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] | 4364 | one->data, one->size, |
brian m. carlson | 09bdff2 | 2016-06-24 23:09:29 +0000 | [diff] [blame] | 4365 | &one->oid, one->mode); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4366 | } |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 4367 | return temp; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4368 | } |
| 4369 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4370 | static void add_external_diff_name(struct repository *r, |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 4371 | struct strvec *argv, |
Jeff King | f3efe78 | 2014-04-19 15:22:25 -0400 | [diff] [blame] | 4372 | struct diff_filespec *df) |
| 4373 | { |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 4374 | struct diff_tempfile *temp = prepare_temp_file(r, df); |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 4375 | strvec_push(argv, temp->name); |
| 4376 | strvec_push(argv, temp->hex); |
| 4377 | strvec_push(argv, temp->mode); |
Jeff King | f3efe78 | 2014-04-19 15:22:25 -0400 | [diff] [blame] | 4378 | } |
| 4379 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4380 | /* An external diff command takes: |
| 4381 | * |
| 4382 | * diff-cmd name infile1 infile1-sha1 infile1-mode \ |
| 4383 | * infile2 infile2-sha1 infile2-mode [ rename-to ] |
| 4384 | * |
| 4385 | */ |
René Scharfe | 54443bb | 2024-06-09 09:39:08 +0200 | [diff] [blame] | 4386 | static void run_external_diff(const struct external_diff *pgm, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4387 | const char *name, |
| 4388 | const char *other, |
| 4389 | struct diff_filespec *one, |
| 4390 | struct diff_filespec *two, |
| 4391 | const char *xfrm_msg, |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4392 | struct diff_options *o) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4393 | { |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 4394 | struct child_process cmd = CHILD_PROCESS_INIT; |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4395 | struct diff_queue_struct *q = &diff_queued_diff; |
René Scharfe | d7b97b7 | 2024-06-09 09:41:44 +0200 | [diff] [blame] | 4396 | int quiet = !(o->output_format & DIFF_FORMAT_PATCH); |
| 4397 | int rc; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4398 | |
René Scharfe | d7b97b7 | 2024-06-09 09:41:44 +0200 | [diff] [blame] | 4399 | /* |
| 4400 | * Trivial equality is handled by diff_unmodified_pair() before |
| 4401 | * we get here. If we don't need to show the diff and the |
| 4402 | * external diff program lacks the ability to tell us whether |
| 4403 | * it's empty then we consider it non-empty without even asking. |
| 4404 | */ |
| 4405 | if (!pgm->trust_exit_code && quiet) { |
| 4406 | o->found_changes = 1; |
| 4407 | return; |
| 4408 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4409 | |
René Scharfe | 54443bb | 2024-06-09 09:39:08 +0200 | [diff] [blame] | 4410 | strvec_push(&cmd.args, pgm->cmd); |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 4411 | strvec_push(&cmd.args, name); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4412 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4413 | if (one && two) { |
Jeff King | de8f14e | 2023-01-06 06:04:18 -0500 | [diff] [blame] | 4414 | add_external_diff_name(o->repo, &cmd.args, one); |
| 4415 | add_external_diff_name(o->repo, &cmd.args, two); |
| 4416 | if (other) { |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 4417 | strvec_push(&cmd.args, other); |
Jeff King | 85a9a63 | 2024-01-28 20:57:08 -0500 | [diff] [blame] | 4418 | if (xfrm_msg) |
| 4419 | strvec_push(&cmd.args, xfrm_msg); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4420 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4421 | } |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4422 | |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 4423 | strvec_pushf(&cmd.env, "GIT_DIFF_PATH_COUNTER=%d", |
| 4424 | ++o->diff_path_counter); |
| 4425 | strvec_pushf(&cmd.env, "GIT_DIFF_PATH_TOTAL=%d", q->nr); |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4426 | |
Johannes Schindelin | 3aef54e | 2019-07-11 01:23:41 -0700 | [diff] [blame] | 4427 | diff_free_filespec_data(one); |
| 4428 | diff_free_filespec_data(two); |
René Scharfe | 0e90673 | 2022-10-30 12:51:14 +0100 | [diff] [blame] | 4429 | cmd.use_shell = 1; |
René Scharfe | d7b97b7 | 2024-06-09 09:41:44 +0200 | [diff] [blame] | 4430 | cmd.no_stdout = quiet; |
| 4431 | rc = run_command(&cmd); |
| 4432 | if (!pgm->trust_exit_code && rc == 0) |
| 4433 | o->found_changes = 1; |
| 4434 | else if (pgm->trust_exit_code && rc == 0) |
| 4435 | ; /* nothing */ |
| 4436 | else if (pgm->trust_exit_code && rc == 1) |
| 4437 | o->found_changes = 1; |
| 4438 | else |
Junio C Hamano | e37423f | 2024-05-16 10:08:35 -0700 | [diff] [blame] | 4439 | die(_("external diff died, stopping at %s"), name); |
Jeff King | 89294d1 | 2014-04-19 15:19:19 -0400 | [diff] [blame] | 4440 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4441 | remove_tempfile(); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4442 | } |
| 4443 | |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4444 | static int similarity_index(struct diff_filepair *p) |
| 4445 | { |
| 4446 | return p->score * 100 / MAX_SCORE; |
| 4447 | } |
| 4448 | |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 4449 | static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev) |
| 4450 | { |
| 4451 | if (startup_info->have_repository) |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 4452 | return repo_find_unique_abbrev(the_repository, oid, abbrev); |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 4453 | else { |
| 4454 | char *hex = oid_to_hex(oid); |
Junio C Hamano | 0d9c527 | 2016-10-27 14:58:48 -0700 | [diff] [blame] | 4455 | if (abbrev < 0) |
| 4456 | abbrev = FALLBACK_DEFAULT_ABBREV; |
brian m. carlson | 02afca1 | 2018-07-16 01:28:05 +0000 | [diff] [blame] | 4457 | if (abbrev > the_hash_algo->hexsz) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 4458 | BUG("oid abbreviation out of range: %d", abbrev); |
Jack Bates | 43d1948 | 2016-12-06 09:56:14 -0700 | [diff] [blame] | 4459 | if (abbrev) |
| 4460 | hex[abbrev] = '\0'; |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 4461 | return hex; |
| 4462 | } |
| 4463 | } |
| 4464 | |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4465 | static void fill_metainfo(struct strbuf *msg, |
| 4466 | const char *name, |
| 4467 | const char *other, |
| 4468 | struct diff_filespec *one, |
| 4469 | struct diff_filespec *two, |
| 4470 | struct diff_options *o, |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4471 | struct diff_filepair *p, |
Junio C Hamano | 5977744 | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4472 | int *must_show_header, |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4473 | int use_color) |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4474 | { |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4475 | const char *set = diff_get_color(use_color, DIFF_METAINFO); |
| 4476 | const char *reset = diff_get_color(use_color, DIFF_RESET); |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 4477 | const char *line_prefix = diff_line_prefix(o); |
Johannes Schindelin | 2715e8a | 2022-06-18 00:20:55 +0000 | [diff] [blame] | 4478 | struct string_list *more_headers = NULL; |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 4479 | |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 4480 | *must_show_header = 1; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4481 | strbuf_init(msg, PATH_MAX * 2 + 300); |
| 4482 | switch (p->status) { |
| 4483 | case DIFF_STATUS_COPIED: |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4484 | strbuf_addf(msg, "%s%ssimilarity index %d%%", |
| 4485 | line_prefix, set, similarity_index(p)); |
| 4486 | strbuf_addf(msg, "%s\n%s%scopy from ", |
| 4487 | reset, line_prefix, set); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4488 | quote_c_style(name, msg, NULL, 0); |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4489 | 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] | 4490 | quote_c_style(other, msg, NULL, 0); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4491 | strbuf_addf(msg, "%s\n", reset); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4492 | break; |
| 4493 | case DIFF_STATUS_RENAMED: |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4494 | strbuf_addf(msg, "%s%ssimilarity index %d%%", |
| 4495 | line_prefix, set, similarity_index(p)); |
| 4496 | strbuf_addf(msg, "%s\n%s%srename from ", |
| 4497 | reset, line_prefix, set); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4498 | quote_c_style(name, msg, NULL, 0); |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4499 | strbuf_addf(msg, "%s\n%s%srename to ", |
| 4500 | reset, line_prefix, set); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4501 | quote_c_style(other, msg, NULL, 0); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4502 | strbuf_addf(msg, "%s\n", reset); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4503 | break; |
| 4504 | case DIFF_STATUS_MODIFIED: |
| 4505 | if (p->score) { |
Junio C Hamano | 98ad90f | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4506 | strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n", |
| 4507 | line_prefix, |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4508 | set, similarity_index(p), reset); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4509 | break; |
| 4510 | } |
| 4511 | /* fallthru */ |
| 4512 | default: |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 4513 | *must_show_header = 0; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4514 | } |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 4515 | if ((more_headers = additional_headers(o, name))) { |
| 4516 | add_formatted_headers(msg, more_headers, |
| 4517 | line_prefix, set, reset); |
| 4518 | *must_show_header = 1; |
| 4519 | } |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 4520 | if (one && two && !oideq(&one->oid, &two->oid)) { |
brian m. carlson | de1d81d | 2018-05-02 00:25:52 +0000 | [diff] [blame] | 4521 | const unsigned hexsz = the_hash_algo->hexsz; |
Đoàn Trần Công Danh | 3046c7f | 2020-08-21 18:51:47 +0700 | [diff] [blame] | 4522 | int abbrev = o->abbrev ? o->abbrev : DEFAULT_ABBREV; |
| 4523 | |
| 4524 | if (o->flags.full_index) |
| 4525 | abbrev = hexsz; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4526 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4527 | if (o->flags.binary) { |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4528 | mmfile_t mf; |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4529 | if ((!fill_mmfile(o->repo, &mf, one) && |
| 4530 | diff_filespec_is_binary(o->repo, one)) || |
| 4531 | (!fill_mmfile(o->repo, &mf, two) && |
| 4532 | diff_filespec_is_binary(o->repo, two))) |
brian m. carlson | de1d81d | 2018-05-02 00:25:52 +0000 | [diff] [blame] | 4533 | abbrev = hexsz; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4534 | } |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 4535 | strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set, |
| 4536 | diff_abbrev_oid(&one->oid, abbrev), |
| 4537 | diff_abbrev_oid(&two->oid, abbrev)); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4538 | if (one->mode == two->mode) |
| 4539 | strbuf_addf(msg, " %06o", one->mode); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4540 | strbuf_addf(msg, "%s\n", reset); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4541 | } |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4542 | } |
| 4543 | |
René Scharfe | 54443bb | 2024-06-09 09:39:08 +0200 | [diff] [blame] | 4544 | static void run_diff_cmd(const struct external_diff *pgm, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4545 | const char *name, |
| 4546 | const char *other, |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4547 | const char *attr_path, |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4548 | struct diff_filespec *one, |
| 4549 | struct diff_filespec *two, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4550 | struct strbuf *msg, |
Junio C Hamano | 051308f | 2006-05-04 16:51:44 -0700 | [diff] [blame] | 4551 | struct diff_options *o, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4552 | struct diff_filepair *p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4553 | { |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4554 | const char *xfrm_msg = NULL; |
| 4555 | int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score; |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 4556 | int must_show_header = 0; |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4557 | struct userdiff_driver *drv = NULL; |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4558 | |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4559 | 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] | 4560 | drv = userdiff_find_by_path(o->repo->index, attr_path); |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4561 | |
René Scharfe | 54443bb | 2024-06-09 09:39:08 +0200 | [diff] [blame] | 4562 | if (o->flags.allow_external && drv && drv->external.cmd) |
| 4563 | pgm = &drv->external; |
Junio C Hamano | f1af60b | 2007-04-22 17:52:55 -0700 | [diff] [blame] | 4564 | |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4565 | if (msg) { |
| 4566 | /* |
| 4567 | * don't use colors when the header is intended for an |
| 4568 | * external diff driver |
| 4569 | */ |
| 4570 | fill_metainfo(msg, name, other, one, two, o, p, |
Junio C Hamano | 5977744 | 2010-06-18 11:16:57 -0700 | [diff] [blame] | 4571 | &must_show_header, |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 4572 | want_color(o->use_color) && !pgm); |
Bert Wesarg | 3746644 | 2010-05-04 00:38:07 +0200 | [diff] [blame] | 4573 | xfrm_msg = msg->len ? msg->buf : NULL; |
| 4574 | } |
| 4575 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4576 | if (pgm) { |
Jeff King | 4bc1792 | 2019-02-14 00:49:42 -0500 | [diff] [blame] | 4577 | run_external_diff(pgm, name, other, one, two, xfrm_msg, o); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4578 | return; |
| 4579 | } |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4580 | if (one && two) { |
| 4581 | if (!o->ignore_driver_algorithm && drv && drv->algorithm) |
| 4582 | set_diff_algorithm(o, drv->algorithm); |
| 4583 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4584 | builtin_diff(name, other ? other : name, |
Christian Couder | 296c6bb | 2010-05-26 04:50:12 +0200 | [diff] [blame] | 4585 | one, two, xfrm_msg, must_show_header, |
| 4586 | o, complete_rewrite); |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4587 | } else { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 4588 | fprintf(o->file, "* Unmerged path %s\n", name); |
René Scharfe | 7b30c3a | 2024-05-05 12:19:56 +0200 | [diff] [blame] | 4589 | o->found_changes = 1; |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4590 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4591 | } |
| 4592 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4593 | 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] | 4594 | { |
| 4595 | if (DIFF_FILE_VALID(one)) { |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 4596 | if (!one->oid_valid) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4597 | struct stat st; |
Junio C Hamano | 4682d85 | 2012-06-27 20:14:47 -0700 | [diff] [blame] | 4598 | if (one->is_stdin) { |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 4599 | oidclr(&one->oid); |
Johannes Schindelin | 5332b2a | 2007-02-25 23:36:10 +0100 | [diff] [blame] | 4600 | return; |
| 4601 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4602 | if (lstat(one->path, &st) < 0) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 4603 | die_errno("stat '%s'", one->path); |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4604 | if (index_path(istate, &one->oid, one->path, &st, 0)) |
Alexander Potashev | d753070 | 2009-01-04 21:38:41 +0300 | [diff] [blame] | 4605 | die("cannot hash %s", one->path); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4606 | } |
| 4607 | } |
| 4608 | else |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 4609 | oidclr(&one->oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4610 | } |
| 4611 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4612 | static void strip_prefix(int prefix_length, const char **namep, const char **otherp) |
| 4613 | { |
| 4614 | /* Strip the prefix but do not molest /dev/null and absolute paths */ |
Johannes Sixt | ffd04e9 | 2018-10-19 18:58:07 +0200 | [diff] [blame] | 4615 | if (*namep && !is_absolute_path(*namep)) { |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4616 | *namep += prefix_length; |
Jakub Narebski | d8faea9 | 2010-08-09 10:50:53 -0400 | [diff] [blame] | 4617 | if (**namep == '/') |
| 4618 | ++*namep; |
| 4619 | } |
Johannes Sixt | ffd04e9 | 2018-10-19 18:58:07 +0200 | [diff] [blame] | 4620 | if (*otherp && !is_absolute_path(*otherp)) { |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4621 | *otherp += prefix_length; |
Jakub Narebski | d8faea9 | 2010-08-09 10:50:53 -0400 | [diff] [blame] | 4622 | if (**otherp == '/') |
| 4623 | ++*otherp; |
| 4624 | } |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4625 | } |
| 4626 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4627 | static void run_diff(struct diff_filepair *p, struct diff_options *o) |
| 4628 | { |
René Scharfe | 54443bb | 2024-06-09 09:39:08 +0200 | [diff] [blame] | 4629 | const struct external_diff *pgm = external_diff(); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 4630 | struct strbuf msg; |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 4631 | struct diff_filespec *one = p->one; |
| 4632 | struct diff_filespec *two = p->two; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4633 | const char *name; |
| 4634 | const char *other; |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4635 | const char *attr_path; |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 4636 | |
Stefan Beller | f2d2a5d | 2017-06-29 17:06:46 -0700 | [diff] [blame] | 4637 | name = one->path; |
| 4638 | other = (strcmp(name, two->path) ? two->path : NULL); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4639 | attr_path = name; |
| 4640 | if (o->prefix_length) |
| 4641 | strip_prefix(o->prefix_length, &name, &other); |
| 4642 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4643 | if (!o->flags.allow_external) |
Junio C Hamano | bd8c1a9 | 2012-07-17 22:08:59 -0700 | [diff] [blame] | 4644 | pgm = NULL; |
| 4645 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4646 | if (DIFF_PAIR_UNMERGED(p)) { |
| 4647 | run_diff_cmd(pgm, name, NULL, attr_path, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4648 | NULL, NULL, NULL, o, p); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4649 | return; |
| 4650 | } |
| 4651 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4652 | diff_fill_oid_info(one, o->repo->index); |
| 4653 | diff_fill_oid_info(two, o->repo->index); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4654 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4655 | if (!pgm && |
| 4656 | DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) && |
| 4657 | (S_IFMT & one->mode) != (S_IFMT & two->mode)) { |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4658 | /* |
| 4659 | * a filepair that changes between file and symlink |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4660 | * needs to be split into deletion and creation. |
| 4661 | */ |
| 4662 | struct diff_filespec *null = alloc_filespec(two->path); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4663 | 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] | 4664 | one, null, &msg, |
| 4665 | o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4666 | free(null); |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4667 | strbuf_release(&msg); |
| 4668 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4669 | null = alloc_filespec(one->path); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4670 | run_diff_cmd(NULL, name, other, attr_path, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4671 | null, two, &msg, o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4672 | free(null); |
| 4673 | } |
| 4674 | else |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4675 | run_diff_cmd(pgm, name, other, attr_path, |
Junio C Hamano | b67b961 | 2009-01-26 00:08:24 -0800 | [diff] [blame] | 4676 | one, two, &msg, o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4677 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 4678 | strbuf_release(&msg); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4679 | } |
| 4680 | |
| 4681 | static void run_diffstat(struct diff_filepair *p, struct diff_options *o, |
| 4682 | struct diffstat_t *diffstat) |
| 4683 | { |
| 4684 | const char *name; |
| 4685 | const char *other; |
| 4686 | |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 4687 | if (!o->ignore_driver_algorithm) { |
| 4688 | struct userdiff_driver *drv = userdiff_find_by_path(o->repo->index, |
| 4689 | p->one->path); |
| 4690 | |
| 4691 | if (drv && drv->algorithm) |
| 4692 | set_diff_algorithm(o, drv->algorithm); |
| 4693 | } |
| 4694 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4695 | if (DIFF_PAIR_UNMERGED(p)) { |
| 4696 | /* unmerged */ |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4697 | builtin_diffstat(p->one->path, NULL, NULL, NULL, |
| 4698 | diffstat, o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4699 | return; |
| 4700 | } |
| 4701 | |
| 4702 | name = p->one->path; |
| 4703 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); |
| 4704 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4705 | if (o->prefix_length) |
| 4706 | strip_prefix(o->prefix_length, &name, &other); |
| 4707 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4708 | diff_fill_oid_info(p->one, o->repo->index); |
| 4709 | diff_fill_oid_info(p->two, o->repo->index); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4710 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 4711 | builtin_diffstat(name, other, p->one, p->two, |
| 4712 | diffstat, o, p); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4713 | } |
| 4714 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4715 | static void run_checkdiff(struct diff_filepair *p, struct diff_options *o) |
| 4716 | { |
| 4717 | const char *name; |
| 4718 | const char *other; |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4719 | const char *attr_path; |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4720 | |
| 4721 | if (DIFF_PAIR_UNMERGED(p)) { |
| 4722 | /* unmerged */ |
| 4723 | return; |
| 4724 | } |
| 4725 | |
| 4726 | name = p->one->path; |
| 4727 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4728 | attr_path = other ? other : name; |
| 4729 | |
| 4730 | if (o->prefix_length) |
| 4731 | strip_prefix(o->prefix_length, &name, &other); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4732 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 4733 | diff_fill_oid_info(p->one, o->repo->index); |
| 4734 | diff_fill_oid_info(p->two, o->repo->index); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4735 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4736 | builtin_checkdiff(name, other, attr_path, p->one, p->two, o); |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 4737 | } |
| 4738 | |
Nguyễn Thái Ngọc Duy | e675765 | 2018-09-21 17:57:24 +0200 | [diff] [blame] | 4739 | void repo_diff_setup(struct repository *r, struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4740 | { |
Johannes Schindelin | be4f2b4 | 2010-08-05 10:49:55 +0200 | [diff] [blame] | 4741 | memcpy(options, &default_diff_options, sizeof(*options)); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 4742 | |
| 4743 | options->file = stdout; |
Nguyễn Thái Ngọc Duy | e675765 | 2018-09-21 17:57:24 +0200 | [diff] [blame] | 4744 | options->repo = r; |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 4745 | |
Stefan Beller | 7648b79 | 2018-08-17 13:43:52 -0700 | [diff] [blame] | 4746 | options->output_indicators[OUTPUT_INDICATOR_NEW] = '+'; |
| 4747 | options->output_indicators[OUTPUT_INDICATOR_OLD] = '-'; |
| 4748 | options->output_indicators[OUTPUT_INDICATOR_CONTEXT] = ' '; |
Jack Bates | 43d1948 | 2016-12-06 09:56:14 -0700 | [diff] [blame] | 4749 | options->abbrev = DEFAULT_ABBREV; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4750 | options->line_termination = '\n'; |
| 4751 | options->break_opt = -1; |
| 4752 | options->rename_limit = -1; |
Johan Herland | 712d2c7 | 2011-04-29 11:36:20 +0200 | [diff] [blame] | 4753 | options->dirstat_permille = diff_dirstat_permille_default; |
Jeff Muizelaar | 6468a4e | 2012-09-27 15:12:52 -0400 | [diff] [blame] | 4754 | options->context = diff_context_default; |
Vegard Nossum | c488867 | 2017-01-12 13:21:11 +0100 | [diff] [blame] | 4755 | options->interhunkcontext = diff_interhunk_context_default; |
Junio C Hamano | a17505f | 2016-10-04 15:26:27 -0700 | [diff] [blame] | 4756 | options->ws_error_highlight = ws_error_highlight_default; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4757 | options->flags.rename_empty = 1; |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 4758 | options->flags.relative_name = diff_relative; |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 4759 | options->objfind = NULL; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4760 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 4761 | /* pathchange left =NULL by default */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4762 | options->change = diff_change; |
| 4763 | options->add_remove = diff_addremove; |
Jeff King | f1c9626 | 2011-08-17 22:03:12 -0700 | [diff] [blame] | 4764 | options->use_color = diff_use_color_default; |
Eric Wong | b68ea12 | 2006-07-07 04:01:23 -0700 | [diff] [blame] | 4765 | options->detect_rename = diff_detect_rename_default; |
Michal Privoznik | 07ab4de | 2013-01-16 08:51:57 +0100 | [diff] [blame] | 4766 | options->xdl_opts |= diff_algorithm; |
Michael Haggerty | 433860f | 2016-09-05 11:44:51 +0200 | [diff] [blame] | 4767 | if (diff_indent_heuristic) |
| 4768 | DIFF_XDL_SET(options, INDENT_HEURISTIC); |
Johannes Schindelin | eab9a40 | 2007-12-18 19:32:14 +0000 | [diff] [blame] | 4769 | |
Samuel Bronson | 6d8940b | 2013-12-18 19:08:12 -0500 | [diff] [blame] | 4770 | options->orderfile = diff_order_file_cfg; |
| 4771 | |
Sangeeta Jain | 8ef9312 | 2020-11-10 14:09:00 +0530 | [diff] [blame] | 4772 | if (!options->flags.ignore_submodule_set) |
| 4773 | options->flags.ignore_untracked_in_submodules = 1; |
| 4774 | |
Eli Collins | f89504d | 2010-05-02 19:03:41 -0700 | [diff] [blame] | 4775 | if (diff_no_prefix) { |
Jeff King | 6799aad | 2023-03-09 01:07:06 -0500 | [diff] [blame] | 4776 | diff_set_noprefix(options); |
Eli Collins | f89504d | 2010-05-02 19:03:41 -0700 | [diff] [blame] | 4777 | } else if (!diff_mnemonic_prefix) { |
Jeff King | 6799aad | 2023-03-09 01:07:06 -0500 | [diff] [blame] | 4778 | diff_set_default_prefix(options); |
Junio C Hamano | a5a818e | 2008-08-18 20:08:09 -0700 | [diff] [blame] | 4779 | } |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 4780 | |
| 4781 | options->color_moved = diff_color_moved_default; |
Stefan Beller | 626c0b5 | 2018-07-18 12:31:56 -0700 | [diff] [blame] | 4782 | options->color_moved_ws_handling = diff_color_moved_ws_default; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4783 | } |
| 4784 | |
Johannes Schindelin | 4d4d4ea | 2022-01-28 12:02:49 +0000 | [diff] [blame] | 4785 | static const char diff_status_letters[] = { |
| 4786 | DIFF_STATUS_ADDED, |
| 4787 | DIFF_STATUS_COPIED, |
| 4788 | DIFF_STATUS_DELETED, |
| 4789 | DIFF_STATUS_MODIFIED, |
| 4790 | DIFF_STATUS_RENAMED, |
| 4791 | DIFF_STATUS_TYPE_CHANGED, |
| 4792 | DIFF_STATUS_UNKNOWN, |
| 4793 | DIFF_STATUS_UNMERGED, |
| 4794 | DIFF_STATUS_FILTER_AON, |
| 4795 | DIFF_STATUS_FILTER_BROKEN, |
| 4796 | '\0', |
| 4797 | }; |
| 4798 | |
| 4799 | static unsigned int filter_bit['Z' + 1]; |
| 4800 | |
| 4801 | static void prepare_filter_bits(void) |
| 4802 | { |
| 4803 | int i; |
| 4804 | |
| 4805 | if (!filter_bit[DIFF_STATUS_ADDED]) { |
| 4806 | for (i = 0; diff_status_letters[i]; i++) |
| 4807 | filter_bit[(int) diff_status_letters[i]] = (1 << i); |
| 4808 | } |
| 4809 | } |
| 4810 | |
| 4811 | static unsigned filter_bit_tst(char status, const struct diff_options *opt) |
| 4812 | { |
| 4813 | return opt->filter & filter_bit[(int) status]; |
| 4814 | } |
| 4815 | |
| 4816 | unsigned diff_filter_bit(char status) |
| 4817 | { |
| 4818 | prepare_filter_bits(); |
| 4819 | return filter_bit[(int) status]; |
| 4820 | } |
| 4821 | |
Jeff King | 9eac595 | 2023-06-01 13:41:06 -0400 | [diff] [blame] | 4822 | int diff_check_follow_pathspec(struct pathspec *ps, int die_on_error) |
| 4823 | { |
Jeff King | 8260bc5 | 2023-06-01 13:43:51 -0400 | [diff] [blame] | 4824 | unsigned forbidden_magic; |
| 4825 | |
Jeff King | 9eac595 | 2023-06-01 13:41:06 -0400 | [diff] [blame] | 4826 | if (ps->nr != 1) { |
| 4827 | if (die_on_error) |
| 4828 | die(_("--follow requires exactly one pathspec")); |
| 4829 | return 0; |
| 4830 | } |
Jeff King | 8260bc5 | 2023-06-01 13:43:51 -0400 | [diff] [blame] | 4831 | |
| 4832 | forbidden_magic = ps->items[0].magic & ~(PATHSPEC_FROMTOP | |
| 4833 | PATHSPEC_LITERAL); |
| 4834 | if (forbidden_magic) { |
| 4835 | if (die_on_error) { |
| 4836 | struct strbuf sb = STRBUF_INIT; |
| 4837 | pathspec_magic_names(forbidden_magic, &sb); |
| 4838 | die(_("pathspec magic not supported by --follow: %s"), |
| 4839 | sb.buf); |
| 4840 | } |
| 4841 | return 0; |
| 4842 | } |
| 4843 | |
Jeff King | 9eac595 | 2023-06-01 13:41:06 -0400 | [diff] [blame] | 4844 | return 1; |
| 4845 | } |
| 4846 | |
Thomas Rast | 2845265 | 2012-08-03 14:16:24 +0200 | [diff] [blame] | 4847 | void diff_setup_done(struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4848 | { |
Stefan Beller | 4d8c51a | 2018-01-04 14:50:44 -0800 | [diff] [blame] | 4849 | unsigned check_mask = DIFF_FORMAT_NAME | |
| 4850 | DIFF_FORMAT_NAME_STATUS | |
| 4851 | DIFF_FORMAT_CHECKDIFF | |
| 4852 | DIFF_FORMAT_NO_OUTPUT; |
brian m. carlson | de1d81d | 2018-05-02 00:25:52 +0000 | [diff] [blame] | 4853 | /* |
| 4854 | * This must be signed because we're comparing against a potentially |
| 4855 | * negative value. |
| 4856 | */ |
| 4857 | const int hexsz = the_hash_algo->hexsz; |
Timo Hirvonen | d7de00f | 2006-06-24 20:26:49 +0300 | [diff] [blame] | 4858 | |
Junio C Hamano | 6c37400 | 2013-05-10 17:10:11 +0200 | [diff] [blame] | 4859 | if (options->set_default) |
| 4860 | options->set_default(options); |
| 4861 | |
Stefan Beller | 4d8c51a | 2018-01-04 14:50:44 -0800 | [diff] [blame] | 4862 | if (HAS_MULTI_BITS(options->output_format & check_mask)) |
Jean-Noël Avila | 246cac8 | 2022-01-05 20:02:24 +0000 | [diff] [blame] | 4863 | die(_("options '%s', '%s', '%s', and '%s' cannot be used together"), |
| 4864 | "--name-only", "--name-status", "--check", "-s"); |
Timo Hirvonen | d7de00f | 2006-06-24 20:26:49 +0300 | [diff] [blame] | 4865 | |
Stefan Beller | 5e50525 | 2018-01-04 14:50:43 -0800 | [diff] [blame] | 4866 | 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] | 4867 | die(_("options '%s', '%s', and '%s' cannot be used together"), |
| 4868 | "-G", "-S", "--find-object"); |
Stefan Beller | 5e50525 | 2018-01-04 14:50:43 -0800 | [diff] [blame] | 4869 | |
Ævar Arnfjörð Bjarmason | 188e9e2 | 2021-04-12 19:15:14 +0200 | [diff] [blame] | 4870 | 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] | 4871 | die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s'"), |
| 4872 | "-G", "--pickaxe-regex", "--pickaxe-regex", "-S"); |
Ævar Arnfjörð Bjarmason | 188e9e2 | 2021-04-12 19:15:14 +0200 | [diff] [blame] | 4873 | |
Ævar Arnfjörð Bjarmason | d26ec88 | 2021-04-12 19:15:15 +0200 | [diff] [blame] | 4874 | 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] | 4875 | die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s' and '%s'"), |
| 4876 | "--pickaxe-all", "--find-object", "--pickaxe-all", "-G", "-S"); |
Ævar Arnfjörð Bjarmason | d26ec88 | 2021-04-12 19:15:15 +0200 | [diff] [blame] | 4877 | |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 4878 | /* |
| 4879 | * Most of the time we can say "there are changes" |
| 4880 | * only by checking if there are changed paths, but |
| 4881 | * --ignore-whitespace* options force us to look |
Jim Meyering | 97bf2a0 | 2009-08-30 22:27:02 +0200 | [diff] [blame] | 4882 | * inside contents. |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 4883 | */ |
| 4884 | |
Junio C Hamano | 50f0439 | 2020-12-16 17:27:13 -0800 | [diff] [blame] | 4885 | if ((options->xdl_opts & XDF_WHITESPACE_FLAGS) || |
| 4886 | options->ignore_regex_nr) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4887 | options->flags.diff_from_contents = 1; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 4888 | else |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4889 | options->flags.diff_from_contents = 0; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 4890 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4891 | if (options->flags.find_copies_harder) |
Junio C Hamano | 03b9d56 | 2006-08-09 13:17:19 -0700 | [diff] [blame] | 4892 | options->detect_rename = DIFF_DETECT_COPY; |
| 4893 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4894 | if (!options->flags.relative_name) |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 4895 | options->prefix = NULL; |
| 4896 | if (options->prefix) |
| 4897 | options->prefix_length = strlen(options->prefix); |
| 4898 | else |
| 4899 | options->prefix_length = 0; |
| 4900 | |
Junio C Hamano | a64f8b2 | 2023-08-18 16:59:32 -0700 | [diff] [blame] | 4901 | /* |
| 4902 | * --name-only, --name-status, --checkdiff, and -s |
| 4903 | * turn other output format off. |
| 4904 | */ |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4905 | if (options->output_format & (DIFF_FORMAT_NAME | |
| 4906 | DIFF_FORMAT_NAME_STATUS | |
| 4907 | DIFF_FORMAT_CHECKDIFF | |
| 4908 | DIFF_FORMAT_NO_OUTPUT)) |
| 4909 | options->output_format &= ~(DIFF_FORMAT_RAW | |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 4910 | DIFF_FORMAT_NUMSTAT | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4911 | DIFF_FORMAT_DIFFSTAT | |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 4912 | DIFF_FORMAT_SHORTSTAT | |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 4913 | DIFF_FORMAT_DIRSTAT | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4914 | DIFF_FORMAT_SUMMARY | |
| 4915 | DIFF_FORMAT_PATCH); |
| 4916 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4917 | /* |
| 4918 | * These cases always need recursive; we do not drop caller-supplied |
| 4919 | * recursive bits for other formats here. |
| 4920 | */ |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4921 | if (options->output_format & (DIFF_FORMAT_PATCH | |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 4922 | DIFF_FORMAT_NUMSTAT | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4923 | DIFF_FORMAT_DIFFSTAT | |
Nicolas Pitre | ebd124c | 2006-12-14 23:15:44 -0500 | [diff] [blame] | 4924 | DIFF_FORMAT_SHORTSTAT | |
Linus Torvalds | 7df7c01 | 2008-02-12 13:26:31 -0800 | [diff] [blame] | 4925 | DIFF_FORMAT_DIRSTAT | |
Johannes Schindelin | d7014dc | 2006-10-03 23:09:56 +0200 | [diff] [blame] | 4926 | DIFF_FORMAT_SUMMARY | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 4927 | DIFF_FORMAT_CHECKDIFF)) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4928 | options->flags.recursive = 1; |
Junio C Hamano | 5e36354 | 2006-05-22 00:31:02 -0700 | [diff] [blame] | 4929 | /* |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 4930 | * 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] | 4931 | */ |
Stefan Beller | cf63051 | 2018-01-04 14:50:41 -0800 | [diff] [blame] | 4932 | if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4933 | options->flags.recursive = 1; |
Jens Lehmann | ae6d5c1 | 2010-03-11 22:50:25 +0100 | [diff] [blame] | 4934 | /* |
| 4935 | * When patches are generated, submodules diffed against the work tree |
| 4936 | * must be checked for dirtiness too so it can be shown in the output |
| 4937 | */ |
| 4938 | if (options->output_format & DIFF_FORMAT_PATCH) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4939 | options->flags.dirty_submodules = 1; |
Junio C Hamano | 5e36354 | 2006-05-22 00:31:02 -0700 | [diff] [blame] | 4940 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4941 | if (options->detect_rename && options->rename_limit < 0) |
| 4942 | options->rename_limit = diff_rename_limit_default; |
brian m. carlson | de1d81d | 2018-05-02 00:25:52 +0000 | [diff] [blame] | 4943 | if (hexsz < options->abbrev) |
| 4944 | options->abbrev = hexsz; /* full */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4945 | |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 4946 | /* |
| 4947 | * It does not make sense to show the first hit we happened |
| 4948 | * to have found. It does not make sense not to return with |
| 4949 | * exit code in such a case either. |
| 4950 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4951 | if (options->flags.quick) { |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 4952 | options->output_format = DIFF_FORMAT_NO_OUTPUT; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 4953 | options->flags.exit_with_status = 1; |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 4954 | } |
Zoltan Klinger | ee7fb0b | 2013-12-06 10:38:46 +1100 | [diff] [blame] | 4955 | |
René Scharfe | d7b97b7 | 2024-06-09 09:41:44 +0200 | [diff] [blame] | 4956 | /* |
| 4957 | * External diffs could declare non-identical contents equal |
| 4958 | * (think diff --ignore-space-change). |
| 4959 | */ |
| 4960 | if (options->flags.allow_external && options->flags.exit_with_status) |
| 4961 | options->flags.diff_from_contents = 1; |
| 4962 | |
Jeff King | 0ea7d5b | 2013-12-16 15:02:21 -0500 | [diff] [blame] | 4963 | options->diff_path_counter = 0; |
Junio C Hamano | b0e2c99 | 2014-06-16 10:07:09 -0700 | [diff] [blame] | 4964 | |
Jeff King | 9eac595 | 2023-06-01 13:41:06 -0400 | [diff] [blame] | 4965 | if (options->flags.follow_renames) |
| 4966 | diff_check_follow_pathspec(&options->pathspec, 1); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 4967 | |
| 4968 | if (!options->use_color || external_diff()) |
| 4969 | options->color_moved = 0; |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 4970 | |
Johannes Schindelin | 75408ca | 2022-01-28 12:02:50 +0000 | [diff] [blame] | 4971 | if (options->filter_not) { |
| 4972 | if (!options->filter) |
| 4973 | options->filter = ~filter_bit[DIFF_STATUS_FILTER_AON]; |
| 4974 | options->filter &= ~options->filter_not; |
| 4975 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 4976 | } |
| 4977 | |
Matthieu Moy | dea007f | 2010-08-05 10:22:52 +0200 | [diff] [blame] | 4978 | int parse_long_opt(const char *opt, const char **argv, |
| 4979 | const char **optarg) |
| 4980 | { |
| 4981 | const char *arg = argv[0]; |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 4982 | if (!skip_prefix(arg, "--", &arg)) |
Matthieu Moy | dea007f | 2010-08-05 10:22:52 +0200 | [diff] [blame] | 4983 | return 0; |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 4984 | if (!skip_prefix(arg, opt, &arg)) |
Matthieu Moy | dea007f | 2010-08-05 10:22:52 +0200 | [diff] [blame] | 4985 | return 0; |
Nicolas Vigier | b0d12fc | 2013-10-31 12:08:28 +0100 | [diff] [blame] | 4986 | if (*arg == '=') { /* stuck form: --option=value */ |
Matthieu Moy | dea007f | 2010-08-05 10:22:52 +0200 | [diff] [blame] | 4987 | *optarg = arg + 1; |
| 4988 | return 1; |
| 4989 | } |
| 4990 | if (*arg != '\0') |
| 4991 | return 0; |
| 4992 | /* separate form: --option value */ |
| 4993 | if (!argv[1]) |
| 4994 | die("Option '--%s' requires a value", opt); |
| 4995 | *optarg = argv[1]; |
| 4996 | return 2; |
| 4997 | } |
| 4998 | |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 4999 | 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] | 5000 | { |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 5001 | struct diff_options *options = opt->value; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 5002 | int width = options->stat_width; |
| 5003 | int name_width = options->stat_name_width; |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 5004 | int graph_width = options->stat_graph_width; |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 5005 | int count = options->stat_count; |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 5006 | char *end; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 5007 | |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 5008 | BUG_ON_OPT_NEG(unset); |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 5009 | |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 5010 | if (!strcmp(opt->long_name, "stat")) { |
| 5011 | if (value) { |
| 5012 | width = strtoul(value, &end, 10); |
| 5013 | if (*end == ',') |
| 5014 | name_width = strtoul(end+1, &end, 10); |
| 5015 | if (*end == ',') |
| 5016 | count = strtoul(end+1, &end, 10); |
| 5017 | if (*end) |
| 5018 | return error(_("invalid --stat value: %s"), value); |
Matthieu Moy | 1e57208 | 2010-08-05 10:22:54 +0200 | [diff] [blame] | 5019 | } |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 5020 | } else if (!strcmp(opt->long_name, "stat-width")) { |
| 5021 | width = strtoul(value, &end, 10); |
| 5022 | if (*end) |
| 5023 | return error(_("%s expects a numerical value"), |
| 5024 | opt->long_name); |
| 5025 | } else if (!strcmp(opt->long_name, "stat-name-width")) { |
| 5026 | name_width = strtoul(value, &end, 10); |
| 5027 | if (*end) |
| 5028 | return error(_("%s expects a numerical value"), |
| 5029 | opt->long_name); |
| 5030 | } else if (!strcmp(opt->long_name, "stat-graph-width")) { |
| 5031 | graph_width = strtoul(value, &end, 10); |
| 5032 | if (*end) |
| 5033 | return error(_("%s expects a numerical value"), |
| 5034 | opt->long_name); |
| 5035 | } else if (!strcmp(opt->long_name, "stat-count")) { |
| 5036 | count = strtoul(value, &end, 10); |
| 5037 | if (*end) |
| 5038 | return error(_("%s expects a numerical value"), |
| 5039 | opt->long_name); |
| 5040 | } else |
| 5041 | BUG("%s should not get here", opt->long_name); |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 5042 | |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5043 | options->output_format &= ~DIFF_FORMAT_NO_OUTPUT; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 5044 | options->output_format |= DIFF_FORMAT_DIFFSTAT; |
| 5045 | options->stat_name_width = name_width; |
Zbigniew Jędrzejewski-Szmek | 969fe57 | 2012-03-01 13:26:45 +0100 | [diff] [blame] | 5046 | options->stat_graph_width = graph_width; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 5047 | options->stat_width = width; |
Michael J Gruber | 808e1db | 2011-05-27 14:36:41 +0200 | [diff] [blame] | 5048 | options->stat_count = count; |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 5049 | return 0; |
Jonathan Nieder | 4d7f7a4 | 2010-08-05 10:22:53 +0200 | [diff] [blame] | 5050 | } |
| 5051 | |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 5052 | static int parse_dirstat_opt(struct diff_options *options, const char *params) |
| 5053 | { |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 5054 | struct strbuf errmsg = STRBUF_INIT; |
| 5055 | if (parse_dirstat_params(options, params, &errmsg)) |
Johan Herland | 7478ac5 | 2011-04-29 11:36:23 +0200 | [diff] [blame] | 5056 | die(_("Failed to parse --dirstat/-X option parameter:\n%s"), |
Johan Herland | 51670fc | 2011-04-29 11:36:22 +0200 | [diff] [blame] | 5057 | errmsg.buf); |
| 5058 | strbuf_release(&errmsg); |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 5059 | /* |
| 5060 | * The caller knows a dirstat-related option is given from the command |
| 5061 | * line; allow it to say "return this_function();" |
| 5062 | */ |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5063 | options->output_format &= ~DIFF_FORMAT_NO_OUTPUT; |
Johan Herland | 333f3fb | 2011-04-29 11:36:18 +0200 | [diff] [blame] | 5064 | options->output_format |= DIFF_FORMAT_DIRSTAT; |
| 5065 | return 1; |
| 5066 | } |
| 5067 | |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 5068 | static int diff_opt_diff_filter(const struct option *option, |
| 5069 | const char *optarg, int unset) |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5070 | { |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 5071 | struct diff_options *opt = option->value; |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5072 | int i, optch; |
| 5073 | |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 5074 | BUG_ON_OPT_NEG(unset); |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5075 | prepare_filter_bits(); |
Junio C Hamano | 7f2ea5f | 2013-07-17 16:19:19 -0700 | [diff] [blame] | 5076 | |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5077 | for (i = 0; (optch = optarg[i]) != '\0'; i++) { |
| 5078 | unsigned int bit; |
Junio C Hamano | 7f2ea5f | 2013-07-17 16:19:19 -0700 | [diff] [blame] | 5079 | int negate; |
| 5080 | |
| 5081 | if ('a' <= optch && optch <= 'z') { |
| 5082 | negate = 1; |
| 5083 | optch = toupper(optch); |
| 5084 | } else { |
| 5085 | negate = 0; |
| 5086 | } |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5087 | |
| 5088 | bit = (0 <= optch && optch <= 'Z') ? filter_bit[optch] : 0; |
| 5089 | if (!bit) |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 5090 | return error(_("unknown change class '%c' in --diff-filter=%s"), |
| 5091 | optarg[i], optarg); |
Junio C Hamano | 7f2ea5f | 2013-07-17 16:19:19 -0700 | [diff] [blame] | 5092 | if (negate) |
Johannes Schindelin | 75408ca | 2022-01-28 12:02:50 +0000 | [diff] [blame] | 5093 | opt->filter_not |= bit; |
Junio C Hamano | 7f2ea5f | 2013-07-17 16:19:19 -0700 | [diff] [blame] | 5094 | else |
| 5095 | opt->filter |= bit; |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 5096 | } |
| 5097 | return 0; |
| 5098 | } |
| 5099 | |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 5100 | static void enable_patch_output(int *fmt) |
| 5101 | { |
Matthieu Moy | 71482d3 | 2013-07-16 10:05:37 +0200 | [diff] [blame] | 5102 | *fmt &= ~DIFF_FORMAT_NO_OUTPUT; |
| 5103 | *fmt |= DIFF_FORMAT_PATCH; |
| 5104 | } |
| 5105 | |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 5106 | static int diff_opt_ws_error_highlight(const struct option *option, |
| 5107 | const char *arg, int unset) |
Junio C Hamano | b8767f7 | 2015-05-26 10:11:28 -0700 | [diff] [blame] | 5108 | { |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 5109 | struct diff_options *opt = option->value; |
Junio C Hamano | 077965f | 2016-10-04 15:03:33 -0700 | [diff] [blame] | 5110 | int val = parse_ws_error_highlight(arg); |
Junio C Hamano | b8767f7 | 2015-05-26 10:11:28 -0700 | [diff] [blame] | 5111 | |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 5112 | BUG_ON_OPT_NEG(unset); |
| 5113 | if (val < 0) |
| 5114 | return error(_("unknown value after ws-error-highlight=%.*s"), |
| 5115 | -1 - val, arg); |
Junio C Hamano | b8767f7 | 2015-05-26 10:11:28 -0700 | [diff] [blame] | 5116 | opt->ws_error_highlight = val; |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 5117 | return 0; |
Junio C Hamano | b8767f7 | 2015-05-26 10:11:28 -0700 | [diff] [blame] | 5118 | } |
| 5119 | |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5120 | static int diff_opt_find_object(const struct option *option, |
| 5121 | const char *arg, int unset) |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 5122 | { |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5123 | struct diff_options *opt = option->value; |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 5124 | struct object_id oid; |
| 5125 | |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5126 | BUG_ON_OPT_NEG(unset); |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 5127 | 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] | 5128 | return error(_("unable to resolve '%s'"), arg); |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 5129 | |
| 5130 | if (!opt->objfind) |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 5131 | CALLOC_ARRAY(opt->objfind, 1); |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 5132 | |
| 5133 | opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND; |
| 5134 | opt->flags.recursive = 1; |
| 5135 | opt->flags.tree_in_recursive = 1; |
| 5136 | oidset_insert(opt->objfind, &oid); |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5137 | return 0; |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 5138 | } |
| 5139 | |
Nguyễn Thái Ngọc Duy | df84a43 | 2019-03-05 19:30:11 +0700 | [diff] [blame] | 5140 | static int diff_opt_anchored(const struct option *opt, |
| 5141 | const char *arg, int unset) |
| 5142 | { |
| 5143 | struct diff_options *options = opt->value; |
| 5144 | |
| 5145 | BUG_ON_OPT_NEG(unset); |
| 5146 | options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF); |
| 5147 | ALLOC_GROW(options->anchors, options->anchors_nr + 1, |
| 5148 | options->anchors_alloc); |
| 5149 | options->anchors[options->anchors_nr++] = xstrdup(arg); |
| 5150 | return 0; |
| 5151 | } |
| 5152 | |
Nguyễn Thái Ngọc Duy | 6d9af6f | 2019-03-05 19:30:12 +0700 | [diff] [blame] | 5153 | static int diff_opt_binary(const struct option *opt, |
| 5154 | const char *arg, int unset) |
| 5155 | { |
| 5156 | struct diff_options *options = opt->value; |
| 5157 | |
| 5158 | BUG_ON_OPT_NEG(unset); |
| 5159 | BUG_ON_OPT_ARG(arg); |
| 5160 | enable_patch_output(&options->output_format); |
| 5161 | options->flags.binary = 1; |
| 5162 | return 0; |
| 5163 | } |
| 5164 | |
Nguyễn Thái Ngọc Duy | ced4e17 | 2019-02-21 18:16:13 +0700 | [diff] [blame] | 5165 | static int diff_opt_break_rewrites(const struct option *opt, |
| 5166 | const char *arg, int unset) |
| 5167 | { |
| 5168 | int *break_opt = opt->value; |
| 5169 | int opt1, opt2; |
| 5170 | |
| 5171 | BUG_ON_OPT_NEG(unset); |
| 5172 | if (!arg) |
| 5173 | arg = ""; |
| 5174 | opt1 = parse_rename_score(&arg); |
| 5175 | if (*arg == 0) |
| 5176 | opt2 = 0; |
| 5177 | else if (*arg != '/') |
| 5178 | return error(_("%s expects <n>/<m> form"), opt->long_name); |
| 5179 | else { |
| 5180 | arg++; |
| 5181 | opt2 = parse_rename_score(&arg); |
| 5182 | } |
| 5183 | if (*arg != 0) |
| 5184 | return error(_("%s expects <n>/<m> form"), opt->long_name); |
| 5185 | *break_opt = opt1 | (opt2 << 16); |
| 5186 | return 0; |
| 5187 | } |
| 5188 | |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5189 | static int diff_opt_char(const struct option *opt, |
| 5190 | const char *arg, int unset) |
| 5191 | { |
| 5192 | char *value = opt->value; |
| 5193 | |
| 5194 | BUG_ON_OPT_NEG(unset); |
| 5195 | if (arg[1]) |
| 5196 | return error(_("%s expects a character, got '%s'"), |
| 5197 | opt->long_name, arg); |
| 5198 | *value = arg[0]; |
| 5199 | return 0; |
| 5200 | } |
| 5201 | |
Nguyễn Thái Ngọc Duy | 59311a9 | 2019-03-24 15:20:09 +0700 | [diff] [blame] | 5202 | static int diff_opt_color_moved(const struct option *opt, |
| 5203 | const char *arg, int unset) |
| 5204 | { |
| 5205 | struct diff_options *options = opt->value; |
| 5206 | |
| 5207 | if (unset) { |
| 5208 | options->color_moved = COLOR_MOVED_NO; |
| 5209 | } else if (!arg) { |
| 5210 | if (diff_color_moved_default) |
| 5211 | options->color_moved = diff_color_moved_default; |
| 5212 | if (options->color_moved == COLOR_MOVED_NO) |
| 5213 | options->color_moved = COLOR_MOVED_DEFAULT; |
| 5214 | } else { |
| 5215 | int cm = parse_color_moved(arg); |
| 5216 | if (cm < 0) |
| 5217 | return error(_("bad --color-moved argument: %s"), arg); |
| 5218 | options->color_moved = cm; |
| 5219 | } |
| 5220 | return 0; |
| 5221 | } |
| 5222 | |
Nguyễn Thái Ngọc Duy | 8ce2020 | 2019-03-24 15:20:10 +0700 | [diff] [blame] | 5223 | static int diff_opt_color_moved_ws(const struct option *opt, |
| 5224 | const char *arg, int unset) |
| 5225 | { |
| 5226 | struct diff_options *options = opt->value; |
| 5227 | unsigned cm; |
| 5228 | |
Nguyễn Thái Ngọc Duy | bb98729 | 2019-03-24 15:20:11 +0700 | [diff] [blame] | 5229 | if (unset) { |
| 5230 | options->color_moved_ws_handling = 0; |
| 5231 | return 0; |
| 5232 | } |
| 5233 | |
Nguyễn Thái Ngọc Duy | 8ce2020 | 2019-03-24 15:20:10 +0700 | [diff] [blame] | 5234 | cm = parse_color_moved_ws(arg); |
| 5235 | if (cm & COLOR_MOVED_WS_ERROR) |
| 5236 | return error(_("invalid mode '%s' in --color-moved-ws"), arg); |
| 5237 | options->color_moved_ws_handling = cm; |
| 5238 | return 0; |
| 5239 | } |
| 5240 | |
Nguyễn Thái Ngọc Duy | 212db69 | 2019-03-05 19:30:20 +0700 | [diff] [blame] | 5241 | static int diff_opt_color_words(const struct option *opt, |
| 5242 | const char *arg, int unset) |
| 5243 | { |
| 5244 | struct diff_options *options = opt->value; |
| 5245 | |
| 5246 | BUG_ON_OPT_NEG(unset); |
| 5247 | options->use_color = 1; |
| 5248 | options->word_diff = DIFF_WORDS_COLOR; |
| 5249 | options->word_regex = arg; |
| 5250 | return 0; |
| 5251 | } |
| 5252 | |
Nguyễn Thái Ngọc Duy | 7d7942b | 2019-02-21 18:16:11 +0700 | [diff] [blame] | 5253 | static int diff_opt_compact_summary(const struct option *opt, |
| 5254 | const char *arg, int unset) |
| 5255 | { |
| 5256 | struct diff_options *options = opt->value; |
| 5257 | |
| 5258 | BUG_ON_OPT_ARG(arg); |
| 5259 | if (unset) { |
| 5260 | options->flags.stat_with_summary = 0; |
| 5261 | } else { |
| 5262 | options->flags.stat_with_summary = 1; |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5263 | options->output_format &= ~DIFF_FORMAT_NO_OUTPUT; |
Nguyễn Thái Ngọc Duy | 7d7942b | 2019-02-21 18:16:11 +0700 | [diff] [blame] | 5264 | options->output_format |= DIFF_FORMAT_DIFFSTAT; |
| 5265 | } |
| 5266 | return 0; |
| 5267 | } |
| 5268 | |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5269 | static int diff_opt_diff_algorithm(const struct option *opt, |
| 5270 | const char *arg, int unset) |
| 5271 | { |
| 5272 | struct diff_options *options = opt->value; |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5273 | |
| 5274 | BUG_ON_OPT_NEG(unset); |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 5275 | |
| 5276 | if (set_diff_algorithm(options, arg)) |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5277 | return error(_("option diff-algorithm accepts \"myers\", " |
| 5278 | "\"minimal\", \"patience\" and \"histogram\"")); |
| 5279 | |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 5280 | options->ignore_driver_algorithm = 1; |
| 5281 | |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 5282 | return 0; |
| 5283 | } |
| 5284 | |
| 5285 | static int diff_opt_diff_algorithm_no_arg(const struct option *opt, |
| 5286 | const char *arg, int unset) |
| 5287 | { |
| 5288 | struct diff_options *options = opt->value; |
| 5289 | |
| 5290 | BUG_ON_OPT_NEG(unset); |
| 5291 | BUG_ON_OPT_ARG(arg); |
| 5292 | |
| 5293 | if (set_diff_algorithm(options, opt->long_name)) |
| 5294 | BUG("available diff algorithms include \"myers\", " |
| 5295 | "\"minimal\", \"patience\" and \"histogram\""); |
| 5296 | |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 5297 | options->ignore_driver_algorithm = 1; |
| 5298 | |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5299 | return 0; |
| 5300 | } |
| 5301 | |
Nguyễn Thái Ngọc Duy | 4ce7aab | 2019-02-21 18:16:03 +0700 | [diff] [blame] | 5302 | static int diff_opt_dirstat(const struct option *opt, |
| 5303 | const char *arg, int unset) |
| 5304 | { |
| 5305 | struct diff_options *options = opt->value; |
| 5306 | |
| 5307 | BUG_ON_OPT_NEG(unset); |
| 5308 | if (!strcmp(opt->long_name, "cumulative")) { |
| 5309 | if (arg) |
| 5310 | BUG("how come --cumulative take a value?"); |
| 5311 | arg = "cumulative"; |
| 5312 | } else if (!strcmp(opt->long_name, "dirstat-by-file")) |
| 5313 | parse_dirstat_opt(options, "files"); |
| 5314 | parse_dirstat_opt(options, arg ? arg : ""); |
| 5315 | return 0; |
| 5316 | } |
| 5317 | |
Nguyễn Thái Ngọc Duy | 7f64850 | 2019-02-21 18:16:16 +0700 | [diff] [blame] | 5318 | static int diff_opt_find_copies(const struct option *opt, |
| 5319 | const char *arg, int unset) |
| 5320 | { |
| 5321 | struct diff_options *options = opt->value; |
| 5322 | |
| 5323 | BUG_ON_OPT_NEG(unset); |
| 5324 | if (!arg) |
| 5325 | arg = ""; |
| 5326 | options->rename_score = parse_rename_score(&arg); |
| 5327 | if (*arg != 0) |
| 5328 | return error(_("invalid argument to %s"), opt->long_name); |
| 5329 | |
| 5330 | if (options->detect_rename == DIFF_DETECT_COPY) |
| 5331 | options->flags.find_copies_harder = 1; |
| 5332 | else |
| 5333 | options->detect_rename = DIFF_DETECT_COPY; |
| 5334 | |
| 5335 | return 0; |
| 5336 | } |
| 5337 | |
Nguyễn Thái Ngọc Duy | f476308 | 2019-02-21 18:16:14 +0700 | [diff] [blame] | 5338 | static int diff_opt_find_renames(const struct option *opt, |
| 5339 | const char *arg, int unset) |
| 5340 | { |
| 5341 | struct diff_options *options = opt->value; |
| 5342 | |
| 5343 | BUG_ON_OPT_NEG(unset); |
| 5344 | if (!arg) |
| 5345 | arg = ""; |
| 5346 | options->rename_score = parse_rename_score(&arg); |
| 5347 | if (*arg != 0) |
| 5348 | return error(_("invalid argument to %s"), opt->long_name); |
| 5349 | |
| 5350 | options->detect_rename = DIFF_DETECT_RENAME; |
| 5351 | return 0; |
| 5352 | } |
| 5353 | |
Nguyễn Thái Ngọc Duy | 1e9250b | 2019-03-05 19:30:16 +0700 | [diff] [blame] | 5354 | static int diff_opt_follow(const struct option *opt, |
| 5355 | const char *arg, int unset) |
| 5356 | { |
| 5357 | struct diff_options *options = opt->value; |
| 5358 | |
| 5359 | BUG_ON_OPT_ARG(arg); |
| 5360 | if (unset) { |
| 5361 | options->flags.follow_renames = 0; |
| 5362 | options->flags.default_follow_renames = 0; |
| 5363 | } else { |
| 5364 | options->flags.follow_renames = 1; |
| 5365 | } |
| 5366 | return 0; |
| 5367 | } |
| 5368 | |
Nguyễn Thái Ngọc Duy | b680ee1 | 2019-03-05 19:30:25 +0700 | [diff] [blame] | 5369 | static int diff_opt_ignore_submodules(const struct option *opt, |
| 5370 | const char *arg, int unset) |
| 5371 | { |
| 5372 | struct diff_options *options = opt->value; |
| 5373 | |
| 5374 | BUG_ON_OPT_NEG(unset); |
| 5375 | if (!arg) |
| 5376 | arg = "all"; |
| 5377 | options->flags.override_submodule_config = 1; |
| 5378 | handle_ignore_submodules_arg(options, arg); |
| 5379 | return 0; |
| 5380 | } |
| 5381 | |
Nguyễn Thái Ngọc Duy | 2f81cf9 | 2019-03-24 15:20:06 +0700 | [diff] [blame] | 5382 | static int diff_opt_line_prefix(const struct option *opt, |
| 5383 | const char *optarg, int unset) |
| 5384 | { |
| 5385 | struct diff_options *options = opt->value; |
| 5386 | |
| 5387 | BUG_ON_OPT_NEG(unset); |
| 5388 | options->line_prefix = optarg; |
| 5389 | options->line_prefix_length = strlen(options->line_prefix); |
| 5390 | graph_setup_line_prefix(options); |
| 5391 | return 0; |
| 5392 | } |
| 5393 | |
Nguyễn Thái Ngọc Duy | 11c659d | 2019-03-24 15:20:07 +0700 | [diff] [blame] | 5394 | static int diff_opt_no_prefix(const struct option *opt, |
| 5395 | const char *optarg, int unset) |
| 5396 | { |
| 5397 | struct diff_options *options = opt->value; |
| 5398 | |
| 5399 | BUG_ON_OPT_NEG(unset); |
| 5400 | BUG_ON_OPT_ARG(optarg); |
Jeff King | 6799aad | 2023-03-09 01:07:06 -0500 | [diff] [blame] | 5401 | diff_set_noprefix(options); |
Nguyễn Thái Ngọc Duy | 11c659d | 2019-03-24 15:20:07 +0700 | [diff] [blame] | 5402 | return 0; |
| 5403 | } |
| 5404 | |
Jeff King | b39a569 | 2023-03-09 01:09:54 -0500 | [diff] [blame] | 5405 | static int diff_opt_default_prefix(const struct option *opt, |
| 5406 | const char *optarg, int unset) |
| 5407 | { |
| 5408 | struct diff_options *options = opt->value; |
| 5409 | |
| 5410 | BUG_ON_OPT_NEG(unset); |
| 5411 | BUG_ON_OPT_ARG(optarg); |
Patrick Steinhardt | f9c1989 | 2024-05-27 13:46:20 +0200 | [diff] [blame] | 5412 | FREE_AND_NULL(diff_src_prefix); |
| 5413 | FREE_AND_NULL(diff_dst_prefix); |
Jeff King | b39a569 | 2023-03-09 01:09:54 -0500 | [diff] [blame] | 5414 | diff_set_default_prefix(options); |
Ramkumar Ramachandra | c47ef57 | 2012-11-13 21:12:45 +0530 | [diff] [blame] | 5415 | return 0; |
| 5416 | } |
| 5417 | |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5418 | static enum parse_opt_result diff_opt_output(struct parse_opt_ctx_t *ctx, |
| 5419 | const struct option *opt, |
| 5420 | const char *arg, int unset) |
| 5421 | { |
| 5422 | struct diff_options *options = opt->value; |
| 5423 | char *path; |
| 5424 | |
| 5425 | BUG_ON_OPT_NEG(unset); |
| 5426 | path = prefix_filename(ctx->prefix, arg); |
| 5427 | options->file = xfopen(path, "w"); |
| 5428 | options->close_file = 1; |
| 5429 | if (options->use_color != GIT_COLOR_ALWAYS) |
| 5430 | options->use_color = GIT_COLOR_NEVER; |
| 5431 | free(path); |
| 5432 | return 0; |
| 5433 | } |
| 5434 | |
Nguyễn Thái Ngọc Duy | 31fd640 | 2019-03-05 19:30:08 +0700 | [diff] [blame] | 5435 | static int diff_opt_patience(const struct option *opt, |
| 5436 | const char *arg, int unset) |
| 5437 | { |
| 5438 | struct diff_options *options = opt->value; |
| 5439 | int i; |
| 5440 | |
| 5441 | BUG_ON_OPT_NEG(unset); |
| 5442 | BUG_ON_OPT_ARG(arg); |
Nguyễn Thái Ngọc Duy | 31fd640 | 2019-03-05 19:30:08 +0700 | [diff] [blame] | 5443 | /* |
| 5444 | * Both --patience and --anchored use PATIENCE_DIFF |
| 5445 | * internally, so remove any anchors previously |
| 5446 | * specified. |
| 5447 | */ |
| 5448 | for (i = 0; i < options->anchors_nr; i++) |
| 5449 | free(options->anchors[i]); |
| 5450 | options->anchors_nr = 0; |
John Cai | a4cf900 | 2023-02-20 21:04:42 +0000 | [diff] [blame] | 5451 | options->ignore_driver_algorithm = 1; |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 5452 | |
| 5453 | return set_diff_algorithm(options, "patience"); |
Nguyễn Thái Ngọc Duy | 31fd640 | 2019-03-05 19:30:08 +0700 | [diff] [blame] | 5454 | } |
| 5455 | |
Michał Kępień | 296d4a9 | 2020-10-20 08:48:09 +0200 | [diff] [blame] | 5456 | static int diff_opt_ignore_regex(const struct option *opt, |
| 5457 | const char *arg, int unset) |
| 5458 | { |
| 5459 | struct diff_options *options = opt->value; |
| 5460 | regex_t *regex; |
| 5461 | |
| 5462 | BUG_ON_OPT_NEG(unset); |
| 5463 | regex = xmalloc(sizeof(*regex)); |
| 5464 | if (regcomp(regex, arg, REG_EXTENDED | REG_NEWLINE)) |
| 5465 | return error(_("invalid regex given to -I: '%s'"), arg); |
| 5466 | ALLOC_GROW(options->ignore_regex, options->ignore_regex_nr + 1, |
| 5467 | options->ignore_regex_alloc); |
| 5468 | options->ignore_regex[options->ignore_regex_nr++] = regex; |
| 5469 | return 0; |
| 5470 | } |
| 5471 | |
Nguyễn Thái Ngọc Duy | a41cfb3 | 2019-03-24 15:19:59 +0700 | [diff] [blame] | 5472 | static int diff_opt_pickaxe_regex(const struct option *opt, |
| 5473 | const char *arg, int unset) |
| 5474 | { |
| 5475 | struct diff_options *options = opt->value; |
| 5476 | |
| 5477 | BUG_ON_OPT_NEG(unset); |
| 5478 | options->pickaxe = arg; |
| 5479 | options->pickaxe_opts |= DIFF_PICKAXE_KIND_G; |
| 5480 | return 0; |
| 5481 | } |
| 5482 | |
| 5483 | static int diff_opt_pickaxe_string(const struct option *opt, |
| 5484 | const char *arg, int unset) |
| 5485 | { |
| 5486 | struct diff_options *options = opt->value; |
| 5487 | |
| 5488 | BUG_ON_OPT_NEG(unset); |
| 5489 | options->pickaxe = arg; |
| 5490 | options->pickaxe_opts |= DIFF_PICKAXE_KIND_S; |
| 5491 | return 0; |
| 5492 | } |
| 5493 | |
Nguyễn Thái Ngọc Duy | 0b1c5b5 | 2019-02-21 18:16:19 +0700 | [diff] [blame] | 5494 | static int diff_opt_relative(const struct option *opt, |
| 5495 | const char *arg, int unset) |
| 5496 | { |
| 5497 | struct diff_options *options = opt->value; |
| 5498 | |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 5499 | options->flags.relative_name = !unset; |
Nguyễn Thái Ngọc Duy | 0b1c5b5 | 2019-02-21 18:16:19 +0700 | [diff] [blame] | 5500 | if (arg) |
| 5501 | options->prefix = arg; |
| 5502 | return 0; |
| 5503 | } |
| 5504 | |
Nguyễn Thái Ngọc Duy | 125dcea | 2019-03-05 19:30:26 +0700 | [diff] [blame] | 5505 | static int diff_opt_submodule(const struct option *opt, |
| 5506 | const char *arg, int unset) |
| 5507 | { |
| 5508 | struct diff_options *options = opt->value; |
| 5509 | |
| 5510 | BUG_ON_OPT_NEG(unset); |
| 5511 | if (!arg) |
| 5512 | arg = "log"; |
| 5513 | if (parse_submodule_params(options, arg)) |
| 5514 | return error(_("failed to parse --submodule option parameter: '%s'"), |
| 5515 | arg); |
| 5516 | return 0; |
| 5517 | } |
| 5518 | |
Nguyễn Thái Ngọc Duy | 8ab7697 | 2019-03-05 19:30:24 +0700 | [diff] [blame] | 5519 | static int diff_opt_textconv(const struct option *opt, |
| 5520 | const char *arg, int unset) |
| 5521 | { |
| 5522 | struct diff_options *options = opt->value; |
| 5523 | |
| 5524 | BUG_ON_OPT_ARG(arg); |
| 5525 | if (unset) { |
| 5526 | options->flags.allow_textconv = 0; |
| 5527 | } else { |
| 5528 | options->flags.allow_textconv = 1; |
| 5529 | options->flags.textconv_set_via_cmdline = 1; |
| 5530 | } |
| 5531 | return 0; |
| 5532 | } |
| 5533 | |
Nguyễn Thái Ngọc Duy | d473e2e | 2019-01-27 07:35:33 +0700 | [diff] [blame] | 5534 | static int diff_opt_unified(const struct option *opt, |
| 5535 | const char *arg, int unset) |
| 5536 | { |
| 5537 | struct diff_options *options = opt->value; |
| 5538 | char *s; |
| 5539 | |
| 5540 | BUG_ON_OPT_NEG(unset); |
| 5541 | |
Nguyễn Thái Ngọc Duy | 8ef0519 | 2019-05-29 16:11:15 +0700 | [diff] [blame] | 5542 | if (arg) { |
| 5543 | options->context = strtol(arg, &s, 10); |
| 5544 | if (*s) |
| 5545 | return error(_("%s expects a numerical value"), "--unified"); |
| 5546 | } |
Nguyễn Thái Ngọc Duy | d473e2e | 2019-01-27 07:35:33 +0700 | [diff] [blame] | 5547 | enable_patch_output(&options->output_format); |
| 5548 | |
| 5549 | return 0; |
| 5550 | } |
| 5551 | |
Nguyễn Thái Ngọc Duy | e9fb39b | 2019-03-05 19:30:18 +0700 | [diff] [blame] | 5552 | static int diff_opt_word_diff(const struct option *opt, |
| 5553 | const char *arg, int unset) |
| 5554 | { |
| 5555 | struct diff_options *options = opt->value; |
| 5556 | |
| 5557 | BUG_ON_OPT_NEG(unset); |
| 5558 | if (arg) { |
| 5559 | if (!strcmp(arg, "plain")) |
| 5560 | options->word_diff = DIFF_WORDS_PLAIN; |
| 5561 | else if (!strcmp(arg, "color")) { |
| 5562 | options->use_color = 1; |
| 5563 | options->word_diff = DIFF_WORDS_COLOR; |
| 5564 | } |
| 5565 | else if (!strcmp(arg, "porcelain")) |
| 5566 | options->word_diff = DIFF_WORDS_PORCELAIN; |
| 5567 | else if (!strcmp(arg, "none")) |
| 5568 | options->word_diff = DIFF_WORDS_NONE; |
| 5569 | else |
| 5570 | return error(_("bad --word-diff argument: %s"), arg); |
| 5571 | } else { |
| 5572 | if (options->word_diff == DIFF_WORDS_NONE) |
| 5573 | options->word_diff = DIFF_WORDS_PLAIN; |
| 5574 | } |
| 5575 | return 0; |
| 5576 | } |
| 5577 | |
Nguyễn Thái Ngọc Duy | 797df11 | 2019-03-05 19:30:19 +0700 | [diff] [blame] | 5578 | static int diff_opt_word_diff_regex(const struct option *opt, |
| 5579 | const char *arg, int unset) |
| 5580 | { |
| 5581 | struct diff_options *options = opt->value; |
| 5582 | |
| 5583 | BUG_ON_OPT_NEG(unset); |
| 5584 | if (options->word_diff == DIFF_WORDS_NONE) |
| 5585 | options->word_diff = DIFF_WORDS_PLAIN; |
| 5586 | options->word_regex = arg; |
| 5587 | return 0; |
| 5588 | } |
| 5589 | |
Junio C Hamano | 1eb4136 | 2021-02-11 11:57:50 -0800 | [diff] [blame] | 5590 | static int diff_opt_rotate_to(const struct option *opt, const char *arg, int unset) |
| 5591 | { |
| 5592 | struct diff_options *options = opt->value; |
| 5593 | |
| 5594 | BUG_ON_OPT_NEG(unset); |
| 5595 | if (!strcmp(opt->long_name, "skip-to")) |
| 5596 | options->skip_instead_of_rotate = 1; |
| 5597 | else |
| 5598 | options->skip_instead_of_rotate = 0; |
| 5599 | options->rotate_to = arg; |
| 5600 | return 0; |
| 5601 | } |
| 5602 | |
Philippe Blain | 0a86803 | 2023-06-26 16:24:48 +0000 | [diff] [blame] | 5603 | /* |
| 5604 | * Consider adding new flags to __git_diff_common_options |
| 5605 | * in contrib/completion/git-completion.bash |
| 5606 | */ |
René Scharfe | c5630c4 | 2022-12-01 23:49:11 +0100 | [diff] [blame] | 5607 | struct option *add_diff_options(const struct option *opts, |
| 5608 | struct diff_options *options) |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5609 | { |
| 5610 | struct option parseopts[] = { |
Nguyễn Thái Ngọc Duy | cc013c2 | 2019-01-27 07:35:32 +0700 | [diff] [blame] | 5611 | OPT_GROUP(N_("Diff output format options")), |
| 5612 | OPT_BITOP('p', "patch", &options->output_format, |
| 5613 | N_("generate patch"), |
| 5614 | DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5615 | OPT_SET_INT('s', "no-patch", &options->output_format, |
| 5616 | 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] | 5617 | OPT_BITOP('u', NULL, &options->output_format, |
| 5618 | N_("generate patch"), |
| 5619 | DIFF_FORMAT_PATCH, DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | d473e2e | 2019-01-27 07:35:33 +0700 | [diff] [blame] | 5620 | OPT_CALLBACK_F('U', "unified", options, N_("<n>"), |
| 5621 | N_("generate diffs with <n> lines context"), |
Junio C Hamano | 20aa7c5 | 2019-05-30 10:50:44 -0700 | [diff] [blame] | 5622 | 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] | 5623 | OPT_BOOL('W', "function-context", &options->flags.funccontext, |
| 5624 | N_("generate diffs with <n> lines context")), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5625 | OPT_BITOP(0, "raw", &options->output_format, |
Nguyễn Thái Ngọc Duy | ed88148 | 2019-01-27 07:35:35 +0700 | [diff] [blame] | 5626 | N_("generate the diff in raw format"), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5627 | DIFF_FORMAT_RAW, DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | c659f30 | 2019-02-16 18:36:35 +0700 | [diff] [blame] | 5628 | OPT_BITOP(0, "patch-with-raw", &options->output_format, |
| 5629 | N_("synonym for '-p --raw'"), |
| 5630 | DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW, |
| 5631 | DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | e550f58 | 2019-02-21 18:16:06 +0700 | [diff] [blame] | 5632 | OPT_BITOP(0, "patch-with-stat", &options->output_format, |
| 5633 | N_("synonym for '-p --stat'"), |
| 5634 | DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT, |
| 5635 | DIFF_FORMAT_NO_OUTPUT), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5636 | OPT_BITOP(0, "numstat", &options->output_format, |
Nguyễn Thái Ngọc Duy | e567362 | 2019-02-16 18:36:36 +0700 | [diff] [blame] | 5637 | N_("machine friendly --stat"), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5638 | DIFF_FORMAT_NUMSTAT, DIFF_FORMAT_NO_OUTPUT), |
| 5639 | OPT_BITOP(0, "shortstat", &options->output_format, |
Nguyễn Thái Ngọc Duy | e567362 | 2019-02-16 18:36:36 +0700 | [diff] [blame] | 5640 | N_("output only the last line of --stat"), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5641 | DIFF_FORMAT_SHORTSTAT, DIFF_FORMAT_NO_OUTPUT), |
Jiang Xin | 5e7013a | 2024-02-14 16:46:41 +0800 | [diff] [blame] | 5642 | 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] | 5643 | N_("output the distribution of relative amount of changes for each sub-directory"), |
| 5644 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5645 | diff_opt_dirstat), |
| 5646 | OPT_CALLBACK_F(0, "cumulative", options, NULL, |
| 5647 | N_("synonym for --dirstat=cumulative"), |
| 5648 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, |
| 5649 | diff_opt_dirstat), |
Jiang Xin | 5e7013a | 2024-02-14 16:46:41 +0800 | [diff] [blame] | 5650 | OPT_CALLBACK_F(0, "dirstat-by-file", options, N_("<param1>,<param2>..."), |
| 5651 | N_("synonym for --dirstat=files,<param1>,<param2>..."), |
Nguyễn Thái Ngọc Duy | 4ce7aab | 2019-02-21 18:16:03 +0700 | [diff] [blame] | 5652 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5653 | diff_opt_dirstat), |
Nguyễn Thái Ngọc Duy | fc6af3e | 2019-02-21 18:16:04 +0700 | [diff] [blame] | 5654 | OPT_BIT_F(0, "check", &options->output_format, |
| 5655 | N_("warn if changes introduce conflict markers or whitespace errors"), |
| 5656 | DIFF_FORMAT_CHECKDIFF, PARSE_OPT_NONEG), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5657 | OPT_BITOP(0, "summary", &options->output_format, |
Nguyễn Thái Ngọc Duy | 70a3041 | 2019-02-21 18:16:05 +0700 | [diff] [blame] | 5658 | N_("condensed summary such as creations, renames and mode changes"), |
Junio C Hamano | 9d484b9 | 2023-05-05 09:59:52 -0700 | [diff] [blame] | 5659 | DIFF_FORMAT_SUMMARY, DIFF_FORMAT_NO_OUTPUT), |
Nguyễn Thái Ngọc Duy | 0e840e2 | 2019-02-21 18:16:07 +0700 | [diff] [blame] | 5660 | OPT_BIT_F(0, "name-only", &options->output_format, |
| 5661 | N_("show only names of changed files"), |
| 5662 | DIFF_FORMAT_NAME, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | a238747 | 2019-02-21 18:16:08 +0700 | [diff] [blame] | 5663 | OPT_BIT_F(0, "name-status", &options->output_format, |
| 5664 | N_("show only names and status of changed files"), |
| 5665 | DIFF_FORMAT_NAME_STATUS, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | 84b5089 | 2019-02-21 18:16:10 +0700 | [diff] [blame] | 5666 | OPT_CALLBACK_F(0, "stat", options, N_("<width>[,<name-width>[,<count>]]"), |
| 5667 | N_("generate diffstat"), |
| 5668 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, diff_opt_stat), |
| 5669 | OPT_CALLBACK_F(0, "stat-width", options, N_("<width>"), |
| 5670 | N_("generate diffstat with a given width"), |
| 5671 | PARSE_OPT_NONEG, diff_opt_stat), |
| 5672 | OPT_CALLBACK_F(0, "stat-name-width", options, N_("<width>"), |
| 5673 | N_("generate diffstat with a given name width"), |
| 5674 | PARSE_OPT_NONEG, diff_opt_stat), |
| 5675 | OPT_CALLBACK_F(0, "stat-graph-width", options, N_("<width>"), |
| 5676 | N_("generate diffstat with a given graph width"), |
| 5677 | PARSE_OPT_NONEG, diff_opt_stat), |
| 5678 | OPT_CALLBACK_F(0, "stat-count", options, N_("<count>"), |
| 5679 | N_("generate diffstat with limited lines"), |
| 5680 | PARSE_OPT_NONEG, diff_opt_stat), |
Nguyễn Thái Ngọc Duy | 7d7942b | 2019-02-21 18:16:11 +0700 | [diff] [blame] | 5681 | OPT_CALLBACK_F(0, "compact-summary", options, NULL, |
| 5682 | N_("generate compact summary in diffstat"), |
| 5683 | PARSE_OPT_NOARG, diff_opt_compact_summary), |
Nguyễn Thái Ngọc Duy | 6d9af6f | 2019-03-05 19:30:12 +0700 | [diff] [blame] | 5684 | OPT_CALLBACK_F(0, "binary", options, NULL, |
| 5685 | N_("output a binary diff that can be applied"), |
| 5686 | 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] | 5687 | OPT_BOOL(0, "full-index", &options->flags.full_index, |
| 5688 | 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] | 5689 | OPT_COLOR_FLAG(0, "color", &options->use_color, |
| 5690 | N_("show colored diff")), |
Nguyễn Thái Ngọc Duy | 5a749d9 | 2019-03-24 15:19:55 +0700 | [diff] [blame] | 5691 | OPT_CALLBACK_F(0, "ws-error-highlight", options, N_("<kind>"), |
| 5692 | N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"), |
| 5693 | 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] | 5694 | OPT_SET_INT('z', NULL, &options->line_termination, |
| 5695 | N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"), |
| 5696 | 0), |
Nguyễn Thái Ngọc Duy | d877418 | 2019-03-24 15:20:04 +0700 | [diff] [blame] | 5697 | OPT__ABBREV(&options->abbrev), |
Nguyễn Thái Ngọc Duy | 31fba9d | 2019-03-24 15:20:05 +0700 | [diff] [blame] | 5698 | OPT_STRING_F(0, "src-prefix", &options->a_prefix, N_("<prefix>"), |
| 5699 | N_("show the given source prefix instead of \"a/\""), |
| 5700 | PARSE_OPT_NONEG), |
| 5701 | OPT_STRING_F(0, "dst-prefix", &options->b_prefix, N_("<prefix>"), |
Jiang Xin | 8a1569d | 2019-06-02 23:11:22 +0800 | [diff] [blame] | 5702 | 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] | 5703 | PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | 2f81cf9 | 2019-03-24 15:20:06 +0700 | [diff] [blame] | 5704 | OPT_CALLBACK_F(0, "line-prefix", options, N_("<prefix>"), |
| 5705 | N_("prepend an additional prefix to every line of output"), |
| 5706 | PARSE_OPT_NONEG, diff_opt_line_prefix), |
Nguyễn Thái Ngọc Duy | 11c659d | 2019-03-24 15:20:07 +0700 | [diff] [blame] | 5707 | OPT_CALLBACK_F(0, "no-prefix", options, NULL, |
| 5708 | N_("do not show any source or destination prefix"), |
| 5709 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, diff_opt_no_prefix), |
Jeff King | b39a569 | 2023-03-09 01:09:54 -0500 | [diff] [blame] | 5710 | OPT_CALLBACK_F(0, "default-prefix", options, NULL, |
| 5711 | N_("use default prefixes a/ and b/"), |
| 5712 | 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] | 5713 | OPT_INTEGER_F(0, "inter-hunk-context", &options->interhunkcontext, |
| 5714 | N_("show context between diff hunks up to the specified number of lines"), |
| 5715 | PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5716 | OPT_CALLBACK_F(0, "output-indicator-new", |
| 5717 | &options->output_indicators[OUTPUT_INDICATOR_NEW], |
| 5718 | N_("<char>"), |
| 5719 | N_("specify the character to indicate a new line instead of '+'"), |
| 5720 | PARSE_OPT_NONEG, diff_opt_char), |
| 5721 | OPT_CALLBACK_F(0, "output-indicator-old", |
| 5722 | &options->output_indicators[OUTPUT_INDICATOR_OLD], |
| 5723 | N_("<char>"), |
| 5724 | N_("specify the character to indicate an old line instead of '-'"), |
| 5725 | PARSE_OPT_NONEG, diff_opt_char), |
| 5726 | OPT_CALLBACK_F(0, "output-indicator-context", |
| 5727 | &options->output_indicators[OUTPUT_INDICATOR_CONTEXT], |
| 5728 | N_("<char>"), |
| 5729 | N_("specify the character to indicate a context instead of ' '"), |
| 5730 | PARSE_OPT_NONEG, diff_opt_char), |
| 5731 | |
Nguyễn Thái Ngọc Duy | ced4e17 | 2019-02-21 18:16:13 +0700 | [diff] [blame] | 5732 | OPT_GROUP(N_("Diff rename options")), |
| 5733 | OPT_CALLBACK_F('B', "break-rewrites", &options->break_opt, N_("<n>[/<m>]"), |
| 5734 | N_("break complete rewrite changes into pairs of delete and create"), |
| 5735 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5736 | diff_opt_break_rewrites), |
Nguyễn Thái Ngọc Duy | f476308 | 2019-02-21 18:16:14 +0700 | [diff] [blame] | 5737 | OPT_CALLBACK_F('M', "find-renames", options, N_("<n>"), |
| 5738 | N_("detect renames"), |
| 5739 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5740 | diff_opt_find_renames), |
Nguyễn Thái Ngọc Duy | 1e53329 | 2019-02-21 18:16:15 +0700 | [diff] [blame] | 5741 | OPT_SET_INT_F('D', "irreversible-delete", &options->irreversible_delete, |
| 5742 | N_("omit the preimage for deletes"), |
| 5743 | 1, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | 7f64850 | 2019-02-21 18:16:16 +0700 | [diff] [blame] | 5744 | OPT_CALLBACK_F('C', "find-copies", options, N_("<n>"), |
| 5745 | N_("detect copies"), |
| 5746 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5747 | diff_opt_find_copies), |
Nguyễn Thái Ngọc Duy | bdd4741 | 2019-02-21 18:16:17 +0700 | [diff] [blame] | 5748 | OPT_BOOL(0, "find-copies-harder", &options->flags.find_copies_harder, |
| 5749 | 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] | 5750 | OPT_SET_INT_F(0, "no-renames", &options->detect_rename, |
| 5751 | N_("disable rename detection"), |
| 5752 | 0, PARSE_OPT_NONEG), |
| 5753 | OPT_BOOL(0, "rename-empty", &options->flags.rename_empty, |
| 5754 | N_("use empty blobs as rename source")), |
Nguyễn Thái Ngọc Duy | 1e9250b | 2019-03-05 19:30:16 +0700 | [diff] [blame] | 5755 | OPT_CALLBACK_F(0, "follow", options, NULL, |
| 5756 | N_("continue listing the history of a file beyond renames"), |
| 5757 | PARSE_OPT_NOARG, diff_opt_follow), |
Nguyễn Thái Ngọc Duy | bffee74 | 2019-03-24 15:19:58 +0700 | [diff] [blame] | 5758 | OPT_INTEGER('l', NULL, &options->rename_limit, |
| 5759 | 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] | 5760 | |
Nguyễn Thái Ngọc Duy | 2e75f92 | 2019-02-21 18:16:20 +0700 | [diff] [blame] | 5761 | OPT_GROUP(N_("Diff algorithm options")), |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 5762 | OPT_CALLBACK_F(0, "minimal", options, NULL, |
| 5763 | N_("produce the smallest possible diff"), |
| 5764 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, |
| 5765 | diff_opt_diff_algorithm_no_arg), |
Nguyễn Thái Ngọc Duy | 87649a1 | 2019-02-21 18:16:21 +0700 | [diff] [blame] | 5766 | OPT_BIT_F('w', "ignore-all-space", &options->xdl_opts, |
| 5767 | N_("ignore whitespace when comparing lines"), |
| 5768 | XDF_IGNORE_WHITESPACE, PARSE_OPT_NONEG), |
| 5769 | OPT_BIT_F('b', "ignore-space-change", &options->xdl_opts, |
| 5770 | N_("ignore changes in amount of whitespace"), |
| 5771 | XDF_IGNORE_WHITESPACE_CHANGE, PARSE_OPT_NONEG), |
| 5772 | OPT_BIT_F(0, "ignore-space-at-eol", &options->xdl_opts, |
| 5773 | N_("ignore changes in whitespace at EOL"), |
| 5774 | XDF_IGNORE_WHITESPACE_AT_EOL, PARSE_OPT_NONEG), |
| 5775 | OPT_BIT_F(0, "ignore-cr-at-eol", &options->xdl_opts, |
| 5776 | N_("ignore carrier-return at the end of line"), |
| 5777 | XDF_IGNORE_CR_AT_EOL, PARSE_OPT_NONEG), |
| 5778 | OPT_BIT_F(0, "ignore-blank-lines", &options->xdl_opts, |
| 5779 | N_("ignore changes whose lines are all blank"), |
| 5780 | XDF_IGNORE_BLANK_LINES, PARSE_OPT_NONEG), |
Michał Kępień | 296d4a9 | 2020-10-20 08:48:09 +0200 | [diff] [blame] | 5781 | OPT_CALLBACK_F('I', "ignore-matching-lines", options, N_("<regex>"), |
| 5782 | N_("ignore changes whose all lines match <regex>"), |
| 5783 | 0, diff_opt_ignore_regex), |
Nguyễn Thái Ngọc Duy | 06f7751 | 2019-03-05 19:30:07 +0700 | [diff] [blame] | 5784 | OPT_BIT(0, "indent-heuristic", &options->xdl_opts, |
| 5785 | N_("heuristic to shift diff hunk boundaries for easy reading"), |
| 5786 | XDF_INDENT_HEURISTIC), |
Nguyễn Thái Ngọc Duy | 31fd640 | 2019-03-05 19:30:08 +0700 | [diff] [blame] | 5787 | OPT_CALLBACK_F(0, "patience", options, NULL, |
| 5788 | N_("generate diff using the \"patience diff\" algorithm"), |
| 5789 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, |
| 5790 | diff_opt_patience), |
John Cai | 11e95e1 | 2023-02-20 21:04:41 +0000 | [diff] [blame] | 5791 | OPT_CALLBACK_F(0, "histogram", options, NULL, |
| 5792 | N_("generate diff using the \"histogram diff\" algorithm"), |
| 5793 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, |
| 5794 | diff_opt_diff_algorithm_no_arg), |
Nguyễn Thái Ngọc Duy | 10f35b1 | 2019-03-05 19:30:10 +0700 | [diff] [blame] | 5795 | OPT_CALLBACK_F(0, "diff-algorithm", options, N_("<algorithm>"), |
| 5796 | N_("choose a diff algorithm"), |
| 5797 | PARSE_OPT_NONEG, diff_opt_diff_algorithm), |
Nguyễn Thái Ngọc Duy | df84a43 | 2019-03-05 19:30:11 +0700 | [diff] [blame] | 5798 | OPT_CALLBACK_F(0, "anchored", options, N_("<text>"), |
| 5799 | N_("generate diff using the \"anchored diff\" algorithm"), |
| 5800 | PARSE_OPT_NONEG, diff_opt_anchored), |
Nguyễn Thái Ngọc Duy | e9fb39b | 2019-03-05 19:30:18 +0700 | [diff] [blame] | 5801 | OPT_CALLBACK_F(0, "word-diff", options, N_("<mode>"), |
| 5802 | N_("show word diff, using <mode> to delimit changed words"), |
| 5803 | 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] | 5804 | OPT_CALLBACK_F(0, "word-diff-regex", options, N_("<regex>"), |
| 5805 | N_("use <regex> to decide what a word is"), |
| 5806 | 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] | 5807 | OPT_CALLBACK_F(0, "color-words", options, N_("<regex>"), |
| 5808 | N_("equivalent to --word-diff=color --word-diff-regex=<regex>"), |
| 5809 | 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] | 5810 | OPT_CALLBACK_F(0, "color-moved", options, N_("<mode>"), |
Jean-Noël Avila | 8bcd8f4 | 2019-05-17 21:26:19 +0200 | [diff] [blame] | 5811 | 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] | 5812 | PARSE_OPT_OPTARG, diff_opt_color_moved), |
Nguyễn Thái Ngọc Duy | 8ce2020 | 2019-03-24 15:20:10 +0700 | [diff] [blame] | 5813 | OPT_CALLBACK_F(0, "color-moved-ws", options, N_("<mode>"), |
| 5814 | 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] | 5815 | 0, diff_opt_color_moved_ws), |
Nguyễn Thái Ngọc Duy | 2e75f92 | 2019-02-21 18:16:20 +0700 | [diff] [blame] | 5816 | |
Jean-Noël Avila | 8bcd8f4 | 2019-05-17 21:26:19 +0200 | [diff] [blame] | 5817 | OPT_GROUP(N_("Other diff options")), |
Nguyễn Thái Ngọc Duy | 0b1c5b5 | 2019-02-21 18:16:19 +0700 | [diff] [blame] | 5818 | OPT_CALLBACK_F(0, "relative", options, N_("<prefix>"), |
| 5819 | N_("when run from subdir, exclude changes outside and show relative paths"), |
Laurent Arnoud | c28ded8 | 2020-05-22 12:46:18 +0200 | [diff] [blame] | 5820 | PARSE_OPT_OPTARG, |
Nguyễn Thái Ngọc Duy | 0b1c5b5 | 2019-02-21 18:16:19 +0700 | [diff] [blame] | 5821 | diff_opt_relative), |
Nguyễn Thái Ngọc Duy | c37d4c0 | 2019-03-05 19:30:14 +0700 | [diff] [blame] | 5822 | OPT_BOOL('a', "text", &options->flags.text, |
| 5823 | N_("treat all files as text")), |
Nguyễn Thái Ngọc Duy | 4f99f29 | 2019-03-05 19:30:15 +0700 | [diff] [blame] | 5824 | OPT_BOOL('R', NULL, &options->flags.reverse_diff, |
| 5825 | N_("swap two inputs, reverse the diff")), |
Nguyễn Thái Ngọc Duy | 1086ea0 | 2019-03-05 19:30:21 +0700 | [diff] [blame] | 5826 | OPT_BOOL(0, "exit-code", &options->flags.exit_with_status, |
| 5827 | 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] | 5828 | OPT_BOOL(0, "quiet", &options->flags.quick, |
| 5829 | N_("disable all output of the program")), |
Nguyễn Thái Ngọc Duy | 0cda100 | 2019-03-05 19:30:23 +0700 | [diff] [blame] | 5830 | OPT_BOOL(0, "ext-diff", &options->flags.allow_external, |
| 5831 | 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] | 5832 | OPT_CALLBACK_F(0, "textconv", options, NULL, |
| 5833 | N_("run external text conversion filters when comparing binary files"), |
| 5834 | PARSE_OPT_NOARG, diff_opt_textconv), |
Nguyễn Thái Ngọc Duy | b680ee1 | 2019-03-05 19:30:25 +0700 | [diff] [blame] | 5835 | OPT_CALLBACK_F(0, "ignore-submodules", options, N_("<when>"), |
| 5836 | N_("ignore changes to submodules in the diff generation"), |
| 5837 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5838 | diff_opt_ignore_submodules), |
Nguyễn Thái Ngọc Duy | 125dcea | 2019-03-05 19:30:26 +0700 | [diff] [blame] | 5839 | OPT_CALLBACK_F(0, "submodule", options, N_("<format>"), |
| 5840 | N_("specify how differences in submodules are shown"), |
| 5841 | PARSE_OPT_NONEG | PARSE_OPT_OPTARG, |
| 5842 | diff_opt_submodule), |
Nguyễn Thái Ngọc Duy | 96860e4 | 2019-03-24 15:19:56 +0700 | [diff] [blame] | 5843 | OPT_SET_INT_F(0, "ita-invisible-in-index", &options->ita_invisible_in_index, |
| 5844 | N_("hide 'git add -N' entries from the index"), |
| 5845 | 1, PARSE_OPT_NONEG), |
| 5846 | OPT_SET_INT_F(0, "ita-visible-in-index", &options->ita_invisible_in_index, |
| 5847 | N_("treat 'git add -N' entries as real in the index"), |
| 5848 | 0, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | a41cfb3 | 2019-03-24 15:19:59 +0700 | [diff] [blame] | 5849 | OPT_CALLBACK_F('S', NULL, options, N_("<string>"), |
| 5850 | N_("look for differences that change the number of occurrences of the specified string"), |
| 5851 | 0, diff_opt_pickaxe_string), |
| 5852 | OPT_CALLBACK_F('G', NULL, options, N_("<regex>"), |
| 5853 | N_("look for differences that change the number of occurrences of the specified regex"), |
| 5854 | 0, diff_opt_pickaxe_regex), |
Nguyễn Thái Ngọc Duy | 85f8e88 | 2019-03-24 15:20:00 +0700 | [diff] [blame] | 5855 | OPT_BIT_F(0, "pickaxe-all", &options->pickaxe_opts, |
| 5856 | N_("show all changes in the changeset with -S or -G"), |
| 5857 | DIFF_PICKAXE_ALL, PARSE_OPT_NONEG), |
| 5858 | OPT_BIT_F(0, "pickaxe-regex", &options->pickaxe_opts, |
| 5859 | N_("treat <string> in -S as extended POSIX regular expression"), |
| 5860 | DIFF_PICKAXE_REGEX, PARSE_OPT_NONEG), |
Nguyễn Thái Ngọc Duy | f731814 | 2019-03-24 15:20:01 +0700 | [diff] [blame] | 5861 | OPT_FILENAME('O', NULL, &options->orderfile, |
| 5862 | N_("control the order in which files appear in the output")), |
Junio C Hamano | 1eb4136 | 2021-02-11 11:57:50 -0800 | [diff] [blame] | 5863 | OPT_CALLBACK_F(0, "rotate-to", options, N_("<path>"), |
| 5864 | N_("show the change in the specified path first"), |
| 5865 | PARSE_OPT_NONEG, diff_opt_rotate_to), |
| 5866 | OPT_CALLBACK_F(0, "skip-to", options, N_("<path>"), |
| 5867 | N_("skip the output to the specified path"), |
| 5868 | PARSE_OPT_NONEG, diff_opt_rotate_to), |
Nguyễn Thái Ngọc Duy | a75f28c | 2019-03-24 15:20:02 +0700 | [diff] [blame] | 5869 | OPT_CALLBACK_F(0, "find-object", options, N_("<object-id>"), |
| 5870 | N_("look for differences that change the number of occurrences of the specified object"), |
| 5871 | PARSE_OPT_NONEG, diff_opt_find_object), |
Nguyễn Thái Ngọc Duy | d2d3f27 | 2019-03-24 15:20:03 +0700 | [diff] [blame] | 5872 | OPT_CALLBACK_F(0, "diff-filter", options, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"), |
| 5873 | N_("select files by diff type"), |
| 5874 | PARSE_OPT_NONEG, diff_opt_diff_filter), |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5875 | { OPTION_CALLBACK, 0, "output", options, N_("<file>"), |
Abhradeep Chakraborty | 9e1f22c | 2022-02-23 14:27:34 +0000 | [diff] [blame] | 5876 | N_("output to a specific file"), |
Nguyễn Thái Ngọc Duy | af2f368 | 2019-02-21 18:16:12 +0700 | [diff] [blame] | 5877 | PARSE_OPT_NONEG, NULL, 0, diff_opt_output }, |
| 5878 | |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5879 | OPT_END() |
| 5880 | }; |
| 5881 | |
René Scharfe | 189e97b | 2022-12-01 23:53:17 +0100 | [diff] [blame] | 5882 | return parse_options_concat(opts, parseopts); |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5883 | } |
| 5884 | |
Duy Nguyen | a97262c | 2016-01-21 18:48:44 +0700 | [diff] [blame] | 5885 | int diff_opt_parse(struct diff_options *options, |
| 5886 | const char **av, int ac, const char *prefix) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5887 | { |
René Scharfe | 6c6048f | 2022-12-01 23:51:21 +0100 | [diff] [blame] | 5888 | struct option no_options[] = { OPT_END() }; |
| 5889 | struct option *parseopts = add_diff_options(no_options, options); |
| 5890 | |
Duy Nguyen | a97262c | 2016-01-21 18:48:44 +0700 | [diff] [blame] | 5891 | if (!prefix) |
| 5892 | prefix = ""; |
| 5893 | |
René Scharfe | 6c6048f | 2022-12-01 23:51:21 +0100 | [diff] [blame] | 5894 | 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] | 5895 | PARSE_OPT_KEEP_DASHDASH | |
SZEDER Gábor | 99d86d6 | 2022-08-19 18:03:57 +0200 | [diff] [blame] | 5896 | PARSE_OPT_KEEP_UNKNOWN_OPT | |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5897 | PARSE_OPT_NO_INTERNAL_HELP | |
| 5898 | PARSE_OPT_ONE_SHOT | |
| 5899 | PARSE_OPT_STOP_AT_NON_OPTION); |
René Scharfe | 6c6048f | 2022-12-01 23:51:21 +0100 | [diff] [blame] | 5900 | free(parseopts); |
Nguyễn Thái Ngọc Duy | 4a28847 | 2019-01-27 07:35:31 +0700 | [diff] [blame] | 5901 | |
Nguyễn Thái Ngọc Duy | 8ce2020 | 2019-03-24 15:20:10 +0700 | [diff] [blame] | 5902 | return ac; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5903 | } |
| 5904 | |
Kevin Ballard | 10ae752 | 2010-09-27 16:58:25 -0700 | [diff] [blame] | 5905 | int parse_rename_score(const char **cp_p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5906 | { |
| 5907 | unsigned long num, scale; |
| 5908 | int ch, dot; |
| 5909 | const char *cp = *cp_p; |
| 5910 | |
| 5911 | num = 0; |
| 5912 | scale = 1; |
| 5913 | dot = 0; |
Brian Gianforcaro | eeefa7c | 2009-09-01 01:35:10 -0400 | [diff] [blame] | 5914 | for (;;) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5915 | ch = *cp; |
| 5916 | if ( !dot && ch == '.' ) { |
| 5917 | scale = 1; |
| 5918 | dot = 1; |
| 5919 | } else if ( ch == '%' ) { |
| 5920 | scale = dot ? scale*100 : 100; |
| 5921 | cp++; /* % is always at the end */ |
| 5922 | break; |
| 5923 | } else if ( ch >= '0' && ch <= '9' ) { |
| 5924 | if ( scale < 100000 ) { |
| 5925 | scale *= 10; |
| 5926 | num = (num*10) + (ch-'0'); |
| 5927 | } |
| 5928 | } else { |
| 5929 | break; |
| 5930 | } |
| 5931 | cp++; |
| 5932 | } |
| 5933 | *cp_p = cp; |
| 5934 | |
| 5935 | /* user says num divided by scale and we say internally that |
| 5936 | * is MAX_SCORE * num / scale. |
| 5937 | */ |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 5938 | return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale)); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5939 | } |
| 5940 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5941 | struct diff_queue_struct diff_queued_diff; |
| 5942 | |
| 5943 | void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp) |
| 5944 | { |
Dmitry S. Dolzhenko | 4c960a4 | 2014-03-04 02:31:53 +0400 | [diff] [blame] | 5945 | ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5946 | queue->queue[queue->nr++] = dp; |
| 5947 | } |
| 5948 | |
| 5949 | struct diff_filepair *diff_queue(struct diff_queue_struct *queue, |
| 5950 | struct diff_filespec *one, |
| 5951 | struct diff_filespec *two) |
| 5952 | { |
Junio C Hamano | ef67768 | 2006-08-03 12:01:01 -0700 | [diff] [blame] | 5953 | struct diff_filepair *dp = xcalloc(1, sizeof(*dp)); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5954 | dp->one = one; |
| 5955 | dp->two = two; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5956 | if (queue) |
| 5957 | diff_q(queue, dp); |
| 5958 | return dp; |
| 5959 | } |
| 5960 | |
| 5961 | void diff_free_filepair(struct diff_filepair *p) |
| 5962 | { |
Linus Torvalds | 9fb8841 | 2007-10-25 11:19:10 -0700 | [diff] [blame] | 5963 | free_filespec(p->one); |
| 5964 | free_filespec(p->two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5965 | free(p); |
| 5966 | } |
| 5967 | |
SZEDER Gábor | 04ae000 | 2022-11-02 23:01:40 +0100 | [diff] [blame] | 5968 | void diff_free_queue(struct diff_queue_struct *q) |
| 5969 | { |
| 5970 | for (int i = 0; i < q->nr; i++) |
| 5971 | diff_free_filepair(q->queue[i]); |
| 5972 | free(q->queue); |
| 5973 | } |
| 5974 | |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5975 | 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] | 5976 | { |
| 5977 | int abblen; |
| 5978 | const char *abbrev; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5979 | |
Ann T Ropea | 7cb6ac1 | 2017-12-03 22:27:42 +0100 | [diff] [blame] | 5980 | /* Do we want all 40 hex characters? */ |
brian m. carlson | 02afca1 | 2018-07-16 01:28:05 +0000 | [diff] [blame] | 5981 | if (len == the_hash_algo->hexsz) |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 5982 | return oid_to_hex(oid); |
| 5983 | |
Ann T Ropea | 7cb6ac1 | 2017-12-03 22:27:42 +0100 | [diff] [blame] | 5984 | /* An abbreviated value is fine, possibly followed by an ellipsis. */ |
Jeff King | 4f03666 | 2016-10-20 02:21:25 -0400 | [diff] [blame] | 5985 | abbrev = diff_abbrev_oid(oid, len); |
Ann T Ropea | 7cb6ac1 | 2017-12-03 22:27:42 +0100 | [diff] [blame] | 5986 | |
| 5987 | if (!print_sha1_ellipsis()) |
| 5988 | return abbrev; |
| 5989 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 5990 | abblen = strlen(abbrev); |
Junio C Hamano | d709f1f | 2016-09-30 10:42:05 -0700 | [diff] [blame] | 5991 | |
| 5992 | /* |
Ann T Ropea | 7cb6ac1 | 2017-12-03 22:27:42 +0100 | [diff] [blame] | 5993 | * In well-behaved cases, where the abbreviated result is the |
Junio C Hamano | d709f1f | 2016-09-30 10:42:05 -0700 | [diff] [blame] | 5994 | * same as the requested length, append three dots after the |
| 5995 | * abbreviation (hence the whole logic is limited to the case |
| 5996 | * where abblen < 37); when the actual abbreviated result is a |
| 5997 | * bit longer than the requested length, we reduce the number |
| 5998 | * of dots so that they match the well-behaved ones. However, |
| 5999 | * if the actual abbreviation is longer than the requested |
| 6000 | * length by more than three, we give up on aligning, and add |
| 6001 | * three dots anyway, to indicate that the output is not the |
| 6002 | * full object name. Yes, this may be suboptimal, but this |
| 6003 | * appears only in "diff --raw --abbrev" output and it is not |
| 6004 | * worth the effort to change it now. Note that this would |
| 6005 | * likely to work fine when the automatic sizing of default |
| 6006 | * abbreviation length is used--we would be fed -1 in "len" in |
| 6007 | * that case, and will end up always appending three-dots, but |
| 6008 | * the automatic sizing is supposed to give abblen that ensures |
| 6009 | * uniqueness across all objects (statistically speaking). |
| 6010 | */ |
brian m. carlson | 02afca1 | 2018-07-16 01:28:05 +0000 | [diff] [blame] | 6011 | if (abblen < the_hash_algo->hexsz - 3) { |
brian m. carlson | dc01505 | 2017-03-26 16:01:24 +0000 | [diff] [blame] | 6012 | static char hex[GIT_MAX_HEXSZ + 1]; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6013 | if (len < abblen && abblen <= len + 2) |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 6014 | xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, ".."); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6015 | else |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 6016 | xsnprintf(hex, sizeof(hex), "%s...", abbrev); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6017 | return hex; |
| 6018 | } |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 6019 | |
| 6020 | return oid_to_hex(oid); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6021 | } |
| 6022 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6023 | 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] | 6024 | { |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6025 | int line_termination = opt->line_termination; |
| 6026 | int inter_name_termination = line_termination ? '\t' : '\0'; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6027 | |
John Keeping | 30997bb | 2013-02-07 20:15:27 +0000 | [diff] [blame] | 6028 | fprintf(opt->file, "%s", diff_line_prefix(opt)); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6029 | if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6030 | fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode, |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 6031 | diff_aligned_abbrev(&p->one->oid, opt->abbrev)); |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 6032 | fprintf(opt->file, "%s ", |
Jeff King | d6cece5 | 2016-10-20 02:20:07 -0400 | [diff] [blame] | 6033 | diff_aligned_abbrev(&p->two->oid, opt->abbrev)); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6034 | } |
| 6035 | if (p->score) { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6036 | fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p), |
| 6037 | inter_name_termination); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6038 | } else { |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6039 | fprintf(opt->file, "%c%c", p->status, inter_name_termination); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6040 | } |
| 6041 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 6042 | if (p->status == DIFF_STATUS_COPIED || |
| 6043 | p->status == DIFF_STATUS_RENAMED) { |
| 6044 | const char *name_a, *name_b; |
| 6045 | name_a = p->one->path; |
| 6046 | name_b = p->two->path; |
| 6047 | strip_prefix(opt->prefix_length, &name_a, &name_b); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6048 | write_name_quoted(name_a, opt->file, inter_name_termination); |
| 6049 | write_name_quoted(name_b, opt->file, line_termination); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6050 | } else { |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 6051 | const char *name_a, *name_b; |
| 6052 | name_a = p->one->mode ? p->one->path : p->two->path; |
| 6053 | name_b = NULL; |
| 6054 | strip_prefix(opt->prefix_length, &name_a, &name_b); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6055 | write_name_quoted(name_a, opt->file, line_termination); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6056 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6057 | } |
| 6058 | |
| 6059 | int diff_unmodified_pair(struct diff_filepair *p) |
| 6060 | { |
| 6061 | /* This function is written stricter than necessary to support |
| 6062 | * the currently implemented transformers, but the idea is to |
| 6063 | * let transformers to produce diff_filepairs any way they want, |
| 6064 | * and filter and clean them up here before producing the output. |
| 6065 | */ |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6066 | struct diff_filespec *one = p->one, *two = p->two; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6067 | |
| 6068 | if (DIFF_PAIR_UNMERGED(p)) |
| 6069 | return 0; /* unmerged is interesting */ |
| 6070 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6071 | /* deletion, addition, mode or type change |
| 6072 | * and rename are all interesting. |
| 6073 | */ |
| 6074 | if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) || |
| 6075 | DIFF_PAIR_MODE_CHANGED(p) || |
| 6076 | strcmp(one->path, two->path)) |
| 6077 | return 0; |
| 6078 | |
| 6079 | /* both are valid and point at the same path. that is, we are |
| 6080 | * dealing with a change. |
| 6081 | */ |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 6082 | if (one->oid_valid && two->oid_valid && |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 6083 | oideq(&one->oid, &two->oid) && |
Jens Lehmann | 85adbf2 | 2010-03-12 22:23:52 +0100 | [diff] [blame] | 6084 | !one->dirty_submodule && !two->dirty_submodule) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6085 | return 1; /* no change */ |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 6086 | if (!one->oid_valid && !two->oid_valid) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6087 | return 1; /* both look at the same file on the filesystem. */ |
| 6088 | return 0; |
| 6089 | } |
| 6090 | |
| 6091 | static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o) |
| 6092 | { |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6093 | int include_conflict_headers = |
| 6094 | (additional_headers(o, p->one->path) && |
Elijah Newren | 71a146d | 2022-09-02 03:53:29 +0000 | [diff] [blame] | 6095 | !o->pickaxe_opts && |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6096 | (!o->filter || filter_bit_tst(DIFF_STATUS_UNMERGED, o))); |
| 6097 | |
| 6098 | /* |
| 6099 | * Check if we can return early without showing a diff. Note that |
| 6100 | * diff_filepair only stores {oid, path, mode, is_valid} |
| 6101 | * information for each path, and thus diff_unmodified_pair() only |
| 6102 | * considers those bits of info. However, we do not want pairs |
| 6103 | * created by create_filepairs_for_header_only_notifications() |
| 6104 | * (which always look like unmodified pairs) to be ignored, so |
| 6105 | * return early if both p is unmodified AND we don't want to |
| 6106 | * include_conflict_headers. |
| 6107 | */ |
| 6108 | if (diff_unmodified_pair(p) && !include_conflict_headers) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6109 | return; |
| 6110 | |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6111 | /* Actually, we can also return early to avoid showing tree diffs */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6112 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 6113 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6114 | return; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6115 | |
| 6116 | run_diff(p, o); |
| 6117 | } |
| 6118 | |
| 6119 | static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o, |
| 6120 | struct diffstat_t *diffstat) |
| 6121 | { |
| 6122 | if (diff_unmodified_pair(p)) |
| 6123 | return; |
| 6124 | |
| 6125 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 6126 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
Yann Dirson | c3fced6 | 2010-10-24 23:03:10 +0200 | [diff] [blame] | 6127 | return; /* no useful stat for tree diffs */ |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6128 | |
| 6129 | run_diffstat(p, o, diffstat); |
| 6130 | } |
| 6131 | |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 6132 | static void diff_flush_checkdiff(struct diff_filepair *p, |
| 6133 | struct diff_options *o) |
| 6134 | { |
| 6135 | if (diff_unmodified_pair(p)) |
| 6136 | return; |
| 6137 | |
| 6138 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 6139 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
Yann Dirson | c3fced6 | 2010-10-24 23:03:10 +0200 | [diff] [blame] | 6140 | return; /* nothing to check in tree diffs */ |
Johannes Schindelin | 8824689 | 2006-05-20 23:43:13 +0200 | [diff] [blame] | 6141 | |
| 6142 | run_checkdiff(p, o); |
| 6143 | } |
| 6144 | |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6145 | int diff_queue_is_empty(struct diff_options *o) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6146 | { |
| 6147 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6148 | int i; |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6149 | int include_conflict_headers = |
| 6150 | (o->additional_path_headers && |
Elijah Newren | 67360b7 | 2022-09-02 03:53:30 +0000 | [diff] [blame] | 6151 | strmap_get_size(o->additional_path_headers) && |
Elijah Newren | 71a146d | 2022-09-02 03:53:29 +0000 | [diff] [blame] | 6152 | !o->pickaxe_opts && |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6153 | (!o->filter || filter_bit_tst(DIFF_STATUS_UNMERGED, o))); |
| 6154 | |
| 6155 | if (include_conflict_headers) |
| 6156 | return 0; |
| 6157 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6158 | for (i = 0; i < q->nr; i++) |
| 6159 | if (!diff_unmodified_pair(q->queue[i])) |
| 6160 | return 0; |
| 6161 | return 1; |
| 6162 | } |
| 6163 | |
| 6164 | #if DIFF_DEBUG |
| 6165 | void diff_debug_filespec(struct diff_filespec *s, int x, const char *one) |
| 6166 | { |
| 6167 | fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n", |
| 6168 | x, one ? one : "", |
| 6169 | s->path, |
| 6170 | DIFF_FILE_VALID(s) ? "valid" : "invalid", |
| 6171 | s->mode, |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 6172 | s->oid_valid ? oid_to_hex(&s->oid) : ""); |
Jeff King | 428d52a | 2014-01-16 20:21:59 -0500 | [diff] [blame] | 6173 | fprintf(stderr, "queue[%d] %s size %lu\n", |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6174 | x, one ? one : "", |
Jeff King | 428d52a | 2014-01-16 20:21:59 -0500 | [diff] [blame] | 6175 | s->size); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6176 | } |
| 6177 | |
| 6178 | void diff_debug_filepair(const struct diff_filepair *p, int i) |
| 6179 | { |
| 6180 | diff_debug_filespec(p->one, i, "one"); |
| 6181 | diff_debug_filespec(p->two, i, "two"); |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6182 | 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] | 6183 | p->score, p->status ? p->status : '?', |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6184 | p->one->rename_used, p->broken_pair); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6185 | } |
| 6186 | |
| 6187 | void diff_debug_queue(const char *msg, struct diff_queue_struct *q) |
| 6188 | { |
| 6189 | int i; |
| 6190 | if (msg) |
| 6191 | fprintf(stderr, "%s\n", msg); |
| 6192 | fprintf(stderr, "q->nr = %d\n", q->nr); |
| 6193 | for (i = 0; i < q->nr; i++) { |
| 6194 | struct diff_filepair *p = q->queue[i]; |
| 6195 | diff_debug_filepair(p, i); |
| 6196 | } |
| 6197 | } |
| 6198 | #endif |
| 6199 | |
| 6200 | static void diff_resolve_rename_copy(void) |
| 6201 | { |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6202 | int i; |
| 6203 | struct diff_filepair *p; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6204 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6205 | |
| 6206 | diff_debug_queue("resolve-rename-copy", q); |
| 6207 | |
| 6208 | for (i = 0; i < q->nr; i++) { |
| 6209 | p = q->queue[i]; |
| 6210 | p->status = 0; /* undecided */ |
| 6211 | if (DIFF_PAIR_UNMERGED(p)) |
| 6212 | p->status = DIFF_STATUS_UNMERGED; |
| 6213 | else if (!DIFF_FILE_VALID(p->one)) |
| 6214 | p->status = DIFF_STATUS_ADDED; |
| 6215 | else if (!DIFF_FILE_VALID(p->two)) |
| 6216 | p->status = DIFF_STATUS_DELETED; |
| 6217 | else if (DIFF_PAIR_TYPE_CHANGED(p)) |
| 6218 | p->status = DIFF_STATUS_TYPE_CHANGED; |
| 6219 | |
| 6220 | /* from this point on, we are dealing with a pair |
| 6221 | * whose both sides are valid and of the same type, i.e. |
| 6222 | * either in-place edit or rename/copy edit. |
| 6223 | */ |
| 6224 | else if (DIFF_PAIR_RENAME(p)) { |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6225 | /* |
| 6226 | * A rename might have re-connected a broken |
| 6227 | * pair up, causing the pathnames to be the |
| 6228 | * same again. If so, that's not a rename at |
| 6229 | * all, just a modification.. |
| 6230 | * |
| 6231 | * Otherwise, see if this source was used for |
| 6232 | * multiple renames, in which case we decrement |
| 6233 | * the count, and call it a copy. |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6234 | */ |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6235 | if (!strcmp(p->one->path, p->two->path)) |
| 6236 | p->status = DIFF_STATUS_MODIFIED; |
| 6237 | else if (--p->one->rename_used > 0) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6238 | p->status = DIFF_STATUS_COPIED; |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 6239 | else |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6240 | p->status = DIFF_STATUS_RENAMED; |
| 6241 | } |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 6242 | else if (!oideq(&p->one->oid, &p->two->oid) || |
Johannes Schindelin | d516c2d | 2007-02-22 21:50:10 +0100 | [diff] [blame] | 6243 | p->one->mode != p->two->mode || |
Jens Lehmann | 85adbf2 | 2010-03-12 22:23:52 +0100 | [diff] [blame] | 6244 | p->one->dirty_submodule || |
| 6245 | p->two->dirty_submodule || |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 6246 | is_null_oid(&p->one->oid)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6247 | p->status = DIFF_STATUS_MODIFIED; |
| 6248 | else { |
| 6249 | /* This is a "no-change" entry and should not |
| 6250 | * happen anymore, but prepare for broken callers. |
| 6251 | */ |
| 6252 | error("feeding unmodified %s to diffcore", |
| 6253 | p->one->path); |
| 6254 | p->status = DIFF_STATUS_UNKNOWN; |
| 6255 | } |
| 6256 | } |
| 6257 | diff_debug_queue("resolve-rename-copy done", q); |
| 6258 | } |
| 6259 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6260 | static int check_pair_status(struct diff_filepair *p) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6261 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6262 | switch (p->status) { |
| 6263 | case DIFF_STATUS_UNKNOWN: |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6264 | return 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6265 | case 0: |
| 6266 | die("internal error in diff-resolve-rename-copy"); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6267 | default: |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6268 | return 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6269 | } |
| 6270 | } |
| 6271 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6272 | static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt) |
| 6273 | { |
| 6274 | int fmt = opt->output_format; |
| 6275 | |
| 6276 | if (fmt & DIFF_FORMAT_CHECKDIFF) |
| 6277 | diff_flush_checkdiff(p, opt); |
| 6278 | else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) |
| 6279 | diff_flush_raw(p, opt); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 6280 | else if (fmt & DIFF_FORMAT_NAME) { |
| 6281 | const char *name_a, *name_b; |
| 6282 | name_a = p->two->path; |
| 6283 | name_b = NULL; |
| 6284 | strip_prefix(opt->prefix_length, &name_a, &name_b); |
Jeff King | f5022b5 | 2017-02-08 15:31:15 -0500 | [diff] [blame] | 6285 | fprintf(opt->file, "%s", diff_line_prefix(opt)); |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6286 | write_name_quoted(name_a, opt->file, opt->line_termination); |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 6287 | } |
Junio C Hamano | a64f8b2 | 2023-08-18 16:59:32 -0700 | [diff] [blame] | 6288 | |
| 6289 | opt->found_changes = 1; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6290 | } |
| 6291 | |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6292 | 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] | 6293 | { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6294 | struct strbuf sb = STRBUF_INIT; |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6295 | if (fs->mode) |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6296 | strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6297 | else |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6298 | strbuf_addf(&sb, " %s ", newdelete); |
| 6299 | |
| 6300 | quote_c_style(fs->path, &sb, NULL, 0); |
| 6301 | strbuf_addch(&sb, '\n'); |
| 6302 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
| 6303 | sb.buf, sb.len, 0); |
| 6304 | strbuf_release(&sb); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6305 | } |
| 6306 | |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6307 | static void show_mode_change(struct diff_options *opt, struct diff_filepair *p, |
| 6308 | int show_name) |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6309 | { |
| 6310 | 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] | 6311 | struct strbuf sb = STRBUF_INIT; |
| 6312 | strbuf_addf(&sb, " mode change %06o => %06o", |
| 6313 | p->one->mode, p->two->mode); |
Alexandre Julliard | 0d26a64 | 2007-02-10 15:37:48 +0100 | [diff] [blame] | 6314 | if (show_name) { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6315 | strbuf_addch(&sb, ' '); |
| 6316 | quote_c_style(p->two->path, &sb, NULL, 0); |
Alexandre Julliard | 0d26a64 | 2007-02-10 15:37:48 +0100 | [diff] [blame] | 6317 | } |
Stefan Beller | 58aaced | 2017-09-27 15:51:26 -0700 | [diff] [blame] | 6318 | strbuf_addch(&sb, '\n'); |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6319 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
| 6320 | sb.buf, sb.len, 0); |
| 6321 | strbuf_release(&sb); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6322 | } |
| 6323 | } |
| 6324 | |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6325 | static void show_rename_copy(struct diff_options *opt, const char *renamecopy, |
| 6326 | struct diff_filepair *p) |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6327 | { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6328 | struct strbuf sb = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 6329 | struct strbuf names = STRBUF_INIT; |
| 6330 | |
| 6331 | pprint_rename(&names, p->one->path, p->two->path); |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6332 | strbuf_addf(&sb, " %s %s (%d%%)\n", |
Nguyễn Thái Ngọc Duy | c905cbc | 2018-02-01 20:02:20 +0700 | [diff] [blame] | 6333 | renamecopy, names.buf, similarity_index(p)); |
| 6334 | strbuf_release(&names); |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6335 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
| 6336 | sb.buf, sb.len, 0); |
| 6337 | show_mode_change(opt, p, 0); |
Rene Scharfe | 348eda2 | 2017-08-30 19:49:42 +0200 | [diff] [blame] | 6338 | strbuf_release(&sb); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6339 | } |
| 6340 | |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 6341 | static void diff_summary(struct diff_options *opt, struct diff_filepair *p) |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6342 | { |
| 6343 | switch(p->status) { |
| 6344 | case DIFF_STATUS_DELETED: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6345 | show_file_mode_name(opt, "delete", p->one); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6346 | break; |
| 6347 | case DIFF_STATUS_ADDED: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6348 | show_file_mode_name(opt, "create", p->two); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6349 | break; |
| 6350 | case DIFF_STATUS_COPIED: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6351 | show_rename_copy(opt, "copy", p); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6352 | break; |
| 6353 | case DIFF_STATUS_RENAMED: |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6354 | show_rename_copy(opt, "rename", p); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6355 | break; |
| 6356 | default: |
| 6357 | if (p->score) { |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6358 | struct strbuf sb = STRBUF_INIT; |
| 6359 | strbuf_addstr(&sb, " rewrite "); |
| 6360 | quote_c_style(p->two->path, &sb, NULL, 0); |
| 6361 | strbuf_addf(&sb, " (%d%%)\n", similarity_index(p)); |
| 6362 | emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY, |
| 6363 | sb.buf, sb.len, 0); |
Rene Scharfe | fa842d8 | 2017-08-30 19:49:41 +0200 | [diff] [blame] | 6364 | strbuf_release(&sb); |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6365 | } |
Stefan Beller | 146fdb0 | 2017-06-29 17:07:05 -0700 | [diff] [blame] | 6366 | show_mode_change(opt, p, !p->score); |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6367 | break; |
| 6368 | } |
| 6369 | } |
| 6370 | |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6371 | struct patch_id_t { |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6372 | git_hash_ctx *ctx; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6373 | int patchlen; |
| 6374 | }; |
| 6375 | |
| 6376 | static int remove_space(char *line, int len) |
| 6377 | { |
| 6378 | int i; |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6379 | char *dst = line; |
| 6380 | unsigned char c; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6381 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6382 | for (i = 0; i < len; i++) |
| 6383 | if (!isspace((c = line[i]))) |
| 6384 | *dst++ = c; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6385 | |
Pierre Habouzit | 663af34 | 2007-09-20 00:42:15 +0200 | [diff] [blame] | 6386 | return dst - line; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6387 | } |
| 6388 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6389 | void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx) |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6390 | { |
| 6391 | unsigned char hash[GIT_MAX_RAWSZ]; |
| 6392 | unsigned short carry = 0; |
| 6393 | int i; |
| 6394 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6395 | the_hash_algo->final_fn(hash, ctx); |
| 6396 | the_hash_algo->init_fn(ctx); |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6397 | /* 20-byte sum, with carry */ |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6398 | for (i = 0; i < the_hash_algo->rawsz; ++i) { |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6399 | carry += result->hash[i] + hash[i]; |
| 6400 | result->hash[i] = carry; |
| 6401 | carry >>= 8; |
| 6402 | } |
| 6403 | } |
| 6404 | |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 6405 | static int patch_id_consume(void *priv, char *line, unsigned long len) |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6406 | { |
| 6407 | struct patch_id_t *data = priv; |
| 6408 | int new_len; |
| 6409 | |
René Scharfe | 82a6201 | 2020-08-19 00:08:54 +0200 | [diff] [blame] | 6410 | if (len > 12 && starts_with(line, "\\ ")) |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 6411 | return 0; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6412 | new_len = remove_space(line, len); |
| 6413 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6414 | the_hash_algo->update_fn(data->ctx, line, new_len); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6415 | data->patchlen += new_len; |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 6416 | return 0; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6417 | } |
| 6418 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6419 | 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] | 6420 | { |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6421 | the_hash_algo->update_fn(ctx, str, strlen(str)); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6422 | } |
| 6423 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6424 | static void patch_id_add_mode(git_hash_ctx *ctx, unsigned mode) |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6425 | { |
| 6426 | /* large enough for 2^32 in octal */ |
| 6427 | char buf[12]; |
| 6428 | int len = xsnprintf(buf, sizeof(buf), "%06o", mode); |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6429 | the_hash_algo->update_fn(ctx, buf, len); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6430 | } |
| 6431 | |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6432 | /* returns 0 upon success, and writes result into oid */ |
Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 +0000 | [diff] [blame] | 6433 | 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] | 6434 | { |
| 6435 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6436 | int i; |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6437 | git_hash_ctx ctx; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6438 | struct patch_id_t data; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6439 | |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6440 | the_hash_algo->init_fn(&ctx); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6441 | memset(&data, 0, sizeof(struct patch_id_t)); |
| 6442 | data.ctx = &ctx; |
Stephen Boyd | a8f6855 | 2019-04-26 16:51:57 -0700 | [diff] [blame] | 6443 | oidclr(oid); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6444 | |
| 6445 | for (i = 0; i < q->nr; i++) { |
| 6446 | xpparam_t xpp; |
| 6447 | xdemitconf_t xecfg; |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6448 | mmfile_t mf1, mf2; |
| 6449 | struct diff_filepair *p = q->queue[i]; |
| 6450 | int len1, len2; |
| 6451 | |
Brian Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 6452 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 6453 | memset(&xecfg, 0, sizeof(xecfg)); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6454 | if (p->status == 0) |
| 6455 | return error("internal diff status error"); |
| 6456 | if (p->status == DIFF_STATUS_UNKNOWN) |
| 6457 | continue; |
| 6458 | if (diff_unmodified_pair(p)) |
| 6459 | continue; |
| 6460 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || |
| 6461 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) |
| 6462 | continue; |
| 6463 | if (DIFF_PAIR_UNMERGED(p)) |
| 6464 | continue; |
| 6465 | |
Nguyễn Thái Ngọc Duy | 58bf2a4 | 2018-09-21 17:57:31 +0200 | [diff] [blame] | 6466 | diff_fill_oid_info(p->one, options->repo->index); |
| 6467 | diff_fill_oid_info(p->two, options->repo->index); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6468 | |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6469 | len1 = remove_space(p->one->path, strlen(p->one->path)); |
| 6470 | len2 = remove_space(p->two->path, strlen(p->two->path)); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6471 | patch_id_add_string(&ctx, "diff--git"); |
| 6472 | patch_id_add_string(&ctx, "a/"); |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6473 | the_hash_algo->update_fn(&ctx, p->one->path, len1); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6474 | patch_id_add_string(&ctx, "b/"); |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6475 | the_hash_algo->update_fn(&ctx, p->two->path, len2); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6476 | |
| 6477 | if (p->one->mode == 0) { |
| 6478 | patch_id_add_string(&ctx, "newfilemode"); |
| 6479 | patch_id_add_mode(&ctx, p->two->mode); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6480 | } else if (p->two->mode == 0) { |
| 6481 | patch_id_add_string(&ctx, "deletedfilemode"); |
| 6482 | patch_id_add_mode(&ctx, p->one->mode); |
Jerry Zhang | 93105ab | 2022-10-24 20:07:42 +0000 | [diff] [blame] | 6483 | } else if (p->one->mode != p->two->mode) { |
| 6484 | patch_id_add_string(&ctx, "oldmode"); |
| 6485 | patch_id_add_mode(&ctx, p->one->mode); |
| 6486 | patch_id_add_string(&ctx, "newmode"); |
| 6487 | patch_id_add_mode(&ctx, p->two->mode); |
Jeff King | 977db6b | 2017-03-30 14:26:05 -0400 | [diff] [blame] | 6488 | } |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6489 | |
Jerry Zhang | 0570be7 | 2022-10-24 20:07:39 +0000 | [diff] [blame] | 6490 | if (diff_header_only) { |
| 6491 | /* don't do anything since we're only populating header info */ |
| 6492 | } 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] | 6493 | diff_filespec_is_binary(options->repo, p->two)) { |
brian m. carlson | 36261e4 | 2019-08-18 20:04:03 +0000 | [diff] [blame] | 6494 | the_hash_algo->update_fn(&ctx, oid_to_hex(&p->one->oid), |
| 6495 | the_hash_algo->hexsz); |
| 6496 | the_hash_algo->update_fn(&ctx, oid_to_hex(&p->two->oid), |
| 6497 | the_hash_algo->hexsz); |
Jerry Zhang | 0570be7 | 2022-10-24 20:07:39 +0000 | [diff] [blame] | 6498 | } else { |
| 6499 | if (p->one->mode == 0) { |
| 6500 | patch_id_add_string(&ctx, "---/dev/null"); |
| 6501 | patch_id_add_string(&ctx, "+++b/"); |
| 6502 | the_hash_algo->update_fn(&ctx, p->two->path, len2); |
| 6503 | } else if (p->two->mode == 0) { |
| 6504 | patch_id_add_string(&ctx, "---a/"); |
| 6505 | the_hash_algo->update_fn(&ctx, p->one->path, len1); |
| 6506 | patch_id_add_string(&ctx, "+++/dev/null"); |
| 6507 | } else { |
| 6508 | patch_id_add_string(&ctx, "---a/"); |
| 6509 | the_hash_algo->update_fn(&ctx, p->one->path, len1); |
| 6510 | patch_id_add_string(&ctx, "+++b/"); |
| 6511 | the_hash_algo->update_fn(&ctx, p->two->path, len2); |
| 6512 | } |
| 6513 | |
| 6514 | if (fill_mmfile(options->repo, &mf1, p->one) < 0 || |
| 6515 | fill_mmfile(options->repo, &mf2, p->two) < 0) |
| 6516 | return error("unable to read files to diff"); |
| 6517 | xpp.flags = 0; |
| 6518 | xecfg.ctxlen = 3; |
| 6519 | xecfg.flags = XDL_EMIT_NO_HUNK_HDR; |
| 6520 | if (xdi_diff_outf(&mf1, &mf2, NULL, |
| 6521 | patch_id_consume, &data, &xpp, &xecfg)) |
| 6522 | return error("unable to generate patch-id diff for %s", |
| 6523 | p->one->path); |
Clemens Buchacher | 34597c1 | 2010-08-15 09:20:43 +0200 | [diff] [blame] | 6524 | } |
Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 +0000 | [diff] [blame] | 6525 | flush_one_hunk(oid, &ctx); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6526 | } |
| 6527 | |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6528 | return 0; |
| 6529 | } |
| 6530 | |
Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 +0000 | [diff] [blame] | 6531 | 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] | 6532 | { |
| 6533 | struct diff_queue_struct *q = &diff_queued_diff; |
Jerry Zhang | 51276c1 | 2022-10-24 20:07:40 +0000 | [diff] [blame] | 6534 | int result = diff_get_patch_id(options, oid, diff_header_only); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6535 | |
SZEDER Gábor | 586d8b5 | 2022-11-02 23:01:42 +0100 | [diff] [blame] | 6536 | diff_free_queue(q); |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 6537 | DIFF_QUEUE_CLEAR(q); |
Johannes Schindelin | fcb3d0a | 2006-06-25 03:51:08 +0200 | [diff] [blame] | 6538 | |
| 6539 | return result; |
| 6540 | } |
| 6541 | |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6542 | static int is_summary_empty(const struct diff_queue_struct *q) |
| 6543 | { |
| 6544 | int i; |
| 6545 | |
| 6546 | for (i = 0; i < q->nr; i++) { |
| 6547 | const struct diff_filepair *p = q->queue[i]; |
| 6548 | |
| 6549 | switch (p->status) { |
| 6550 | case DIFF_STATUS_DELETED: |
| 6551 | case DIFF_STATUS_ADDED: |
| 6552 | case DIFF_STATUS_COPIED: |
| 6553 | case DIFF_STATUS_RENAMED: |
| 6554 | return 0; |
| 6555 | default: |
| 6556 | if (p->score) |
| 6557 | return 0; |
| 6558 | if (p->one->mode && p->two->mode && |
| 6559 | p->one->mode != p->two->mode) |
| 6560 | return 0; |
| 6561 | break; |
| 6562 | } |
| 6563 | } |
| 6564 | return 1; |
| 6565 | } |
| 6566 | |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6567 | static const char rename_limit_warning[] = |
Elijah Newren | 05d2c61 | 2021-07-15 00:45:21 +0000 | [diff] [blame] | 6568 | N_("exhaustive rename detection was skipped due to too many files."); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6569 | |
| 6570 | static const char degrade_cc_to_c_warning[] = |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6571 | 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] | 6572 | |
| 6573 | static const char rename_limit_advice[] = |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6574 | N_("you may want to set your %s variable to at least " |
| 6575 | "%d and retry the command."); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6576 | |
| 6577 | void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc) |
| 6578 | { |
Nguyễn Thái Ngọc Duy | 4e056c9 | 2018-01-16 16:23:49 +0700 | [diff] [blame] | 6579 | fflush(stdout); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6580 | if (degraded_cc) |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6581 | warning(_(degrade_cc_to_c_warning)); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6582 | else if (needed) |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6583 | warning(_(rename_limit_warning)); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6584 | else |
| 6585 | return; |
Elijah Newren | 9f7e4bf | 2017-11-13 12:15:59 -0800 | [diff] [blame] | 6586 | if (0 < needed) |
Vasco Almeida | db42497 | 2016-10-17 13:15:29 +0000 | [diff] [blame] | 6587 | warning(_(rename_limit_advice), varname, needed); |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 6588 | } |
| 6589 | |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6590 | static void create_filepairs_for_header_only_notifications(struct diff_options *o) |
| 6591 | { |
| 6592 | struct strset present; |
| 6593 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6594 | struct hashmap_iter iter; |
| 6595 | struct strmap_entry *e; |
| 6596 | int i; |
| 6597 | |
| 6598 | strset_init_with_options(&present, /*pool*/ NULL, /*strdup*/ 0); |
| 6599 | |
| 6600 | /* |
| 6601 | * Find out which paths exist in diff_queued_diff, preferring |
| 6602 | * one->path for any pair that has multiple paths. |
| 6603 | */ |
| 6604 | for (i = 0; i < q->nr; i++) { |
| 6605 | struct diff_filepair *p = q->queue[i]; |
| 6606 | char *path = p->one->path ? p->one->path : p->two->path; |
| 6607 | |
| 6608 | if (strmap_contains(o->additional_path_headers, path)) |
| 6609 | strset_add(&present, path); |
| 6610 | } |
| 6611 | |
| 6612 | /* |
| 6613 | * Loop over paths in additional_path_headers; for each NOT already |
| 6614 | * in diff_queued_diff, create a synthetic filepair and insert that |
| 6615 | * into diff_queued_diff. |
| 6616 | */ |
| 6617 | strmap_for_each_entry(o->additional_path_headers, &iter, e) { |
| 6618 | if (!strset_contains(&present, e->key)) { |
| 6619 | struct diff_filespec *one, *two; |
| 6620 | struct diff_filepair *p; |
| 6621 | |
| 6622 | one = alloc_filespec(e->key); |
| 6623 | two = alloc_filespec(e->key); |
| 6624 | fill_filespec(one, null_oid(), 0, 0); |
| 6625 | fill_filespec(two, null_oid(), 0, 0); |
| 6626 | p = diff_queue(q, one, two); |
| 6627 | p->status = DIFF_STATUS_MODIFIED; |
| 6628 | } |
| 6629 | } |
| 6630 | |
| 6631 | /* Re-sort the filepairs */ |
| 6632 | diffcore_fix_diff_index(); |
| 6633 | |
| 6634 | /* Cleanup */ |
| 6635 | strset_clear(&present); |
| 6636 | } |
| 6637 | |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6638 | static void diff_flush_patch_all_file_pairs(struct diff_options *o) |
| 6639 | { |
| 6640 | int i; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6641 | static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT; |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6642 | struct diff_queue_struct *q = &diff_queued_diff; |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 6643 | |
| 6644 | if (WSEH_NEW & WS_RULE_MASK) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 6645 | BUG("WS rules bit mask overlaps with diff symbol flags"); |
Stefan Beller | 091f8e2 | 2017-06-29 17:06:53 -0700 | [diff] [blame] | 6646 | |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6647 | if (o->color_moved) |
| 6648 | o->emitted_symbols = &esm; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6649 | |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6650 | if (o->additional_path_headers) |
| 6651 | create_filepairs_for_header_only_notifications(o); |
| 6652 | |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6653 | for (i = 0; i < q->nr; i++) { |
| 6654 | struct diff_filepair *p = q->queue[i]; |
| 6655 | if (check_pair_status(p)) |
| 6656 | diff_flush_patch(p, o); |
| 6657 | } |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6658 | |
| 6659 | if (o->emitted_symbols) { |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6660 | if (o->color_moved) { |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 6661 | struct mem_pool entry_pool; |
| 6662 | struct moved_entry_list *entry_list; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6663 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 6664 | mem_pool_init(&entry_pool, 1024 * 1024); |
| 6665 | entry_list = add_lines_to_move_detection(o, |
| 6666 | &entry_pool); |
| 6667 | mark_color_as_moved(o, entry_list); |
Stefan Beller | 86b452e | 2017-06-30 13:53:09 -0700 | [diff] [blame] | 6668 | if (o->color_moved == COLOR_MOVED_ZEBRA_DIM) |
| 6669 | dim_moved_lines(o); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6670 | |
Phillip Wood | 72962e8 | 2021-12-09 10:30:09 +0000 | [diff] [blame] | 6671 | mem_pool_discard(&entry_pool, 0); |
| 6672 | free(entry_list); |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6673 | } |
| 6674 | |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6675 | for (i = 0; i < esm.nr; i++) |
| 6676 | emit_diff_symbol_from_struct(o, &esm.buf[i]); |
| 6677 | |
| 6678 | for (i = 0; i < esm.nr; i++) |
| 6679 | free((void *)esm.buf[i].line); |
Jeff King | 48edf3a | 2019-01-24 07:32:41 -0500 | [diff] [blame] | 6680 | esm.nr = 0; |
| 6681 | |
| 6682 | o->emitted_symbols = NULL; |
Stefan Beller | e6e045f | 2017-06-29 17:07:06 -0700 | [diff] [blame] | 6683 | } |
Stefan Beller | ec33150 | 2017-06-29 17:06:48 -0700 | [diff] [blame] | 6684 | } |
| 6685 | |
Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 11:45:34 +0100 | [diff] [blame] | 6686 | static void diff_free_file(struct diff_options *options) |
| 6687 | { |
| 6688 | if (options->close_file) |
| 6689 | fclose(options->file); |
| 6690 | } |
| 6691 | |
Ævar Arnfjörð Bjarmason | c45dc9c | 2021-02-11 11:45:35 +0100 | [diff] [blame] | 6692 | static void diff_free_ignore_regex(struct diff_options *options) |
| 6693 | { |
| 6694 | int i; |
| 6695 | |
| 6696 | for (i = 0; i < options->ignore_regex_nr; i++) { |
| 6697 | regfree(options->ignore_regex[i]); |
| 6698 | free(options->ignore_regex[i]); |
| 6699 | } |
| 6700 | free(options->ignore_regex); |
| 6701 | } |
| 6702 | |
Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 11:45:34 +0100 | [diff] [blame] | 6703 | void diff_free(struct diff_options *options) |
| 6704 | { |
| 6705 | if (options->no_free) |
| 6706 | return; |
| 6707 | |
| 6708 | diff_free_file(options); |
Ævar Arnfjörð Bjarmason | c45dc9c | 2021-02-11 11:45:35 +0100 | [diff] [blame] | 6709 | diff_free_ignore_regex(options); |
Ævar Arnfjörð Bjarmason | 244c272 | 2022-02-16 11:56:28 +0100 | [diff] [blame] | 6710 | clear_pathspec(&options->pathspec); |
Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 11:45:34 +0100 | [diff] [blame] | 6711 | } |
| 6712 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6713 | void diff_flush(struct diff_options *options) |
| 6714 | { |
| 6715 | struct diff_queue_struct *q = &diff_queued_diff; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6716 | int i, output_format = options->output_format; |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6717 | int separator = 0; |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 6718 | int dirstat_by_line = 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6719 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6720 | /* |
| 6721 | * Order: raw, stat, summary, patch |
| 6722 | * or: name/name-status/checkdiff (other bits clear) |
| 6723 | */ |
Elijah Newren | 95433ee | 2022-02-02 02:37:34 +0000 | [diff] [blame] | 6724 | if (!q->nr && !options->additional_path_headers) |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6725 | goto free_queue; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6726 | |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6727 | if (output_format & (DIFF_FORMAT_RAW | |
| 6728 | DIFF_FORMAT_NAME | |
| 6729 | DIFF_FORMAT_NAME_STATUS | |
| 6730 | DIFF_FORMAT_CHECKDIFF)) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6731 | for (i = 0; i < q->nr; i++) { |
| 6732 | struct diff_filepair *p = q->queue[i]; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6733 | if (check_pair_status(p)) |
| 6734 | flush_one_pair(p, options); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6735 | } |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6736 | separator++; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6737 | } |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6738 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6739 | if (output_format & DIFF_FORMAT_DIRSTAT && options->flags.dirstat_by_line) |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 6740 | dirstat_by_line = 1; |
| 6741 | |
| 6742 | if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT) || |
| 6743 | dirstat_by_line) { |
Timo Hirvonen | 5e2b063 | 2006-06-25 14:28:19 +0300 | [diff] [blame] | 6744 | struct diffstat_t diffstat; |
Timo Hirvonen | c674434 | 2006-06-24 20:21:53 +0300 | [diff] [blame] | 6745 | |
Daniel Ferreira | e4cb659 | 2019-11-13 12:40:58 +0000 | [diff] [blame] | 6746 | compute_diffstat(options, &diffstat, q); |
Junio C Hamano | 74e2abe | 2006-10-12 03:01:00 -0700 | [diff] [blame] | 6747 | if (output_format & DIFF_FORMAT_NUMSTAT) |
| 6748 | show_numstat(&diffstat, options); |
| 6749 | if (output_format & DIFF_FORMAT_DIFFSTAT) |
| 6750 | show_stats(&diffstat, options); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 6751 | if (output_format & DIFF_FORMAT_SHORTSTAT) |
Daniel Barkalow | c0c7773 | 2008-03-09 22:43:39 -0400 | [diff] [blame] | 6752 | show_shortstats(&diffstat, options); |
Mårten Kongstad | ab27389 | 2015-03-02 16:05:39 +0100 | [diff] [blame] | 6753 | if (output_format & DIFF_FORMAT_DIRSTAT && dirstat_by_line) |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 6754 | show_dirstat_by_line(&diffstat, options); |
Junio C Hamano | f604652 | 2007-12-11 23:46:30 -0800 | [diff] [blame] | 6755 | free_diffstat_info(&diffstat); |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 6756 | separator++; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6757 | } |
Johan Herland | 1c57a62 | 2011-04-29 11:36:21 +0200 | [diff] [blame] | 6758 | if ((output_format & DIFF_FORMAT_DIRSTAT) && !dirstat_by_line) |
Junio C Hamano | c04a715 | 2008-02-12 17:06:58 -0800 | [diff] [blame] | 6759 | show_dirstat(options); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6760 | |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6761 | if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) { |
Bo Yang | 7be5761 | 2010-05-26 15:23:54 +0800 | [diff] [blame] | 6762 | for (i = 0; i < q->nr; i++) { |
| 6763 | diff_summary(options, q->queue[i]); |
| 6764 | } |
Junio C Hamano | 3969cf7 | 2006-06-27 15:08:19 -0700 | [diff] [blame] | 6765 | separator++; |
Sean | 4bbd261 | 2006-05-14 08:13:49 -0400 | [diff] [blame] | 6766 | } |
| 6767 | |
Junio C Hamano | 5f107ca | 2023-08-18 16:59:28 -0700 | [diff] [blame] | 6768 | if (output_format & DIFF_FORMAT_PATCH) { |
| 6769 | if (separator) { |
| 6770 | emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0); |
| 6771 | if (options->stat_sep) |
| 6772 | /* attach patch instead of inline */ |
| 6773 | emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP, |
| 6774 | NULL, 0, 0); |
| 6775 | } |
| 6776 | |
| 6777 | diff_flush_patch_all_file_pairs(options); |
| 6778 | } |
| 6779 | |
| 6780 | if (output_format & DIFF_FORMAT_CALLBACK) |
| 6781 | options->format_callback(q, options, options->format_callback_data); |
| 6782 | |
Junio C Hamano | e37423f | 2024-05-16 10:08:35 -0700 | [diff] [blame] | 6783 | if (output_format & DIFF_FORMAT_NO_OUTPUT && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6784 | options->flags.exit_with_status && |
| 6785 | options->flags.diff_from_contents) { |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6786 | /* |
| 6787 | * run diff_flush_patch for the exit status. setting |
Ondřej Bílka | 749f763 | 2013-07-22 23:02:23 +0200 | [diff] [blame] | 6788 | * options->file to /dev/null should be safe, because we |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6789 | * aren't supposed to produce any output anyway. |
| 6790 | */ |
Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 11:45:34 +0100 | [diff] [blame] | 6791 | diff_free_file(options); |
Nguyễn Thái Ngọc Duy | 23a9e07 | 2017-05-03 17:16:46 +0700 | [diff] [blame] | 6792 | options->file = xfopen("/dev/null", "w"); |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6793 | options->close_file = 1; |
Stefan Beller | 2e2d5ac | 2017-06-30 13:53:07 -0700 | [diff] [blame] | 6794 | options->color_moved = 0; |
Larry D'Anna | 6977c25 | 2010-02-16 01:55:21 -0500 | [diff] [blame] | 6795 | for (i = 0; i < q->nr; i++) { |
| 6796 | struct diff_filepair *p = q->queue[i]; |
| 6797 | if (check_pair_status(p)) |
| 6798 | diff_flush_patch(p, options); |
| 6799 | if (options->found_changes) |
| 6800 | break; |
| 6801 | } |
| 6802 | } |
| 6803 | |
Timo Hirvonen | 946c378 | 2006-06-27 15:09:17 +0300 | [diff] [blame] | 6804 | free_queue: |
SZEDER Gábor | 586d8b5 | 2022-11-02 23:01:42 +0100 | [diff] [blame] | 6805 | diff_free_queue(q); |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 6806 | DIFF_QUEUE_CLEAR(q); |
Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 11:45:34 +0100 | [diff] [blame] | 6807 | diff_free(options); |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6808 | |
| 6809 | /* |
Jim Meyering | 97bf2a0 | 2009-08-30 22:27:02 +0200 | [diff] [blame] | 6810 | * Report the content-level differences with HAS_CHANGES; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6811 | * diff_addremove/diff_change does not set the bit when |
| 6812 | * DIFF_FROM_CONTENTS is in effect (e.g. with -w). |
| 6813 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6814 | if (options->flags.diff_from_contents) { |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6815 | if (options->found_changes) |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6816 | options->flags.has_changes = 1; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6817 | else |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6818 | options->flags.has_changes = 0; |
Junio C Hamano | f245194 | 2009-05-22 12:45:29 -0700 | [diff] [blame] | 6819 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6820 | } |
| 6821 | |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6822 | static int match_filter(const struct diff_options *options, const struct diff_filepair *p) |
| 6823 | { |
| 6824 | return (((p->status == DIFF_STATUS_MODIFIED) && |
| 6825 | ((p->score && |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6826 | filter_bit_tst(DIFF_STATUS_FILTER_BROKEN, options)) || |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6827 | (!p->score && |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6828 | filter_bit_tst(DIFF_STATUS_MODIFIED, options)))) || |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6829 | ((p->status != DIFF_STATUS_MODIFIED) && |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6830 | filter_bit_tst(p->status, options))); |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6831 | } |
| 6832 | |
Junio C Hamano | 949226f | 2013-07-17 14:19:24 -0700 | [diff] [blame] | 6833 | static void diffcore_apply_filter(struct diff_options *options) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6834 | { |
| 6835 | int i; |
| 6836 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6837 | struct diff_queue_struct outq; |
Junio C Hamano | 949226f | 2013-07-17 14:19:24 -0700 | [diff] [blame] | 6838 | |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 6839 | DIFF_QUEUE_CLEAR(&outq); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6840 | |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6841 | if (!options->filter) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6842 | return; |
| 6843 | |
Junio C Hamano | 1ecc1cb | 2013-07-17 15:05:46 -0700 | [diff] [blame] | 6844 | if (filter_bit_tst(DIFF_STATUS_FILTER_AON, options)) { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6845 | int found; |
| 6846 | for (i = found = 0; !found && i < q->nr; i++) { |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6847 | if (match_filter(options, q->queue[i])) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6848 | found++; |
| 6849 | } |
| 6850 | if (found) |
| 6851 | return; |
| 6852 | |
| 6853 | /* otherwise we will clear the whole queue |
| 6854 | * by copying the empty outq at the end of this |
| 6855 | * function, but first clear the current entries |
| 6856 | * in the queue. |
| 6857 | */ |
| 6858 | for (i = 0; i < q->nr; i++) |
| 6859 | diff_free_filepair(q->queue[i]); |
| 6860 | } |
| 6861 | else { |
| 6862 | /* Only the matching ones */ |
| 6863 | for (i = 0; i < q->nr; i++) { |
| 6864 | struct diff_filepair *p = q->queue[i]; |
Junio C Hamano | 08578fa | 2013-07-17 15:09:34 -0700 | [diff] [blame] | 6865 | if (match_filter(options, p)) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 6866 | diff_q(&outq, p); |
| 6867 | else |
| 6868 | diff_free_filepair(p); |
| 6869 | } |
| 6870 | } |
| 6871 | free(q->queue); |
| 6872 | *q = outq; |
| 6873 | } |
| 6874 | |
Sven Verdoolaege | 5701115 | 2007-09-08 12:30:22 +0200 | [diff] [blame] | 6875 | /* 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] | 6876 | static int diff_filespec_is_identical(struct repository *r, |
| 6877 | struct diff_filespec *one, |
Sven Verdoolaege | 5701115 | 2007-09-08 12:30:22 +0200 | [diff] [blame] | 6878 | struct diff_filespec *two) |
| 6879 | { |
Junio C Hamano | 2b459b4 | 2008-03-02 00:07:59 -0800 | [diff] [blame] | 6880 | if (S_ISGITLINK(one->mode)) |
| 6881 | return 0; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6882 | if (diff_populate_filespec(r, one, NULL)) |
Sven Verdoolaege | 5701115 | 2007-09-08 12:30:22 +0200 | [diff] [blame] | 6883 | return 0; |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6884 | if (diff_populate_filespec(r, two, NULL)) |
Sven Verdoolaege | 5701115 | 2007-09-08 12:30:22 +0200 | [diff] [blame] | 6885 | return 0; |
| 6886 | return !memcmp(one->data, two->data, one->size); |
| 6887 | } |
| 6888 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6889 | static int diff_filespec_check_stat_unmatch(struct repository *r, |
| 6890 | struct diff_filepair *p) |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6891 | { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6892 | struct diff_populate_filespec_options dpf_options = { |
| 6893 | .check_size_only = 1, |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6894 | .missing_object_cb = diff_queued_diff_prefetch, |
| 6895 | .missing_object_data = r, |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6896 | }; |
| 6897 | |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 6898 | if (p->done_skip_stat_unmatch) |
| 6899 | return p->skip_stat_unmatch_result; |
| 6900 | |
| 6901 | p->done_skip_stat_unmatch = 1; |
| 6902 | p->skip_stat_unmatch_result = 0; |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6903 | /* |
| 6904 | * 1. Entries that come from stat info dirtiness |
| 6905 | * always have both sides (iow, not create/delete), |
| 6906 | * one side of the object name is unknown, with |
| 6907 | * the same mode and size. Keep the ones that |
| 6908 | * do not match these criteria. They have real |
| 6909 | * differences. |
| 6910 | * |
| 6911 | * 2. At this point, the file is known to be modified, |
| 6912 | * with the same mode and size, and the object |
| 6913 | * name of one side is unknown. Need to inspect |
| 6914 | * the identical contents. |
| 6915 | */ |
| 6916 | if (!DIFF_FILE_VALID(p->one) || /* (1) */ |
| 6917 | !DIFF_FILE_VALID(p->two) || |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 6918 | (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] | 6919 | (p->one->mode != p->two->mode) || |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 6920 | diff_populate_filespec(r, p->one, &dpf_options) || |
| 6921 | 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] | 6922 | (p->one->size != p->two->size) || |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6923 | !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] | 6924 | p->skip_stat_unmatch_result = 1; |
| 6925 | return p->skip_stat_unmatch_result; |
Nguyễn Thái Ngọc Duy | fceb907 | 2014-01-25 13:46:49 +0700 | [diff] [blame] | 6926 | } |
| 6927 | |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6928 | static void diffcore_skip_stat_unmatch(struct diff_options *diffopt) |
| 6929 | { |
| 6930 | int i; |
| 6931 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6932 | struct diff_queue_struct outq; |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 6933 | DIFF_QUEUE_CLEAR(&outq); |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6934 | |
| 6935 | for (i = 0; i < q->nr; i++) { |
| 6936 | struct diff_filepair *p = q->queue[i]; |
| 6937 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 6938 | if (diff_filespec_check_stat_unmatch(diffopt->repo, p)) |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6939 | diff_q(&outq, p); |
| 6940 | else { |
| 6941 | /* |
| 6942 | * The caller can subtract 1 from skip_stat_unmatch |
| 6943 | * to determine how many paths were dirty only |
| 6944 | * due to stat info mismatch. |
| 6945 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 6946 | if (!diffopt->flags.no_index) |
René Scharfe | 6d2d9e8 | 2007-08-15 00:41:00 +0200 | [diff] [blame] | 6947 | diffopt->skip_stat_unmatch++; |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 6948 | diff_free_filepair(p); |
| 6949 | } |
| 6950 | } |
| 6951 | free(q->queue); |
| 6952 | *q = outq; |
| 6953 | } |
| 6954 | |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 6955 | static int diffnamecmp(const void *a_, const void *b_) |
| 6956 | { |
| 6957 | const struct diff_filepair *a = *((const struct diff_filepair **)a_); |
| 6958 | const struct diff_filepair *b = *((const struct diff_filepair **)b_); |
| 6959 | const char *name_a, *name_b; |
| 6960 | |
| 6961 | name_a = a->one ? a->one->path : a->two->path; |
| 6962 | name_b = b->one ? b->one->path : b->two->path; |
| 6963 | return strcmp(name_a, name_b); |
| 6964 | } |
| 6965 | |
Jeff King | 784c0da | 2019-02-14 00:48:03 -0500 | [diff] [blame] | 6966 | void diffcore_fix_diff_index(void) |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 6967 | { |
| 6968 | struct diff_queue_struct *q = &diff_queued_diff; |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 6969 | QSORT(q->queue, q->nr, diffnamecmp); |
Junio C Hamano | 730f728 | 2009-09-20 00:03:39 -0700 | [diff] [blame] | 6970 | } |
| 6971 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6972 | void diff_add_if_missing(struct repository *r, |
| 6973 | struct oid_array *to_fetch, |
| 6974 | const struct diff_filespec *filespec) |
Jonathan Tan | 7fbbcb2 | 2019-04-05 10:09:34 -0700 | [diff] [blame] | 6975 | { |
| 6976 | if (filespec && filespec->oid_valid && |
Jonathan Tan | a63694f | 2019-08-20 13:53:20 -0700 | [diff] [blame] | 6977 | !S_ISGITLINK(filespec->mode) && |
Jonathan Tan | 7fbbcb2 | 2019-04-05 10:09:34 -0700 | [diff] [blame] | 6978 | oid_object_info_extended(r, &filespec->oid, NULL, |
| 6979 | OBJECT_INFO_FOR_PREFETCH)) |
| 6980 | oid_array_append(to_fetch, &filespec->oid); |
| 6981 | } |
| 6982 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 6983 | void diff_queued_diff_prefetch(void *repository) |
| 6984 | { |
| 6985 | struct repository *repo = repository; |
| 6986 | int i; |
| 6987 | struct diff_queue_struct *q = &diff_queued_diff; |
| 6988 | struct oid_array to_fetch = OID_ARRAY_INIT; |
| 6989 | |
| 6990 | for (i = 0; i < q->nr; i++) { |
| 6991 | struct diff_filepair *p = q->queue[i]; |
| 6992 | diff_add_if_missing(repo, &to_fetch, p->one); |
| 6993 | diff_add_if_missing(repo, &to_fetch, p->two); |
| 6994 | } |
| 6995 | |
| 6996 | /* |
| 6997 | * NEEDSWORK: Consider deduplicating the OIDs sent. |
| 6998 | */ |
| 6999 | promisor_remote_get_direct(repo, to_fetch.oid, to_fetch.nr); |
| 7000 | |
| 7001 | oid_array_clear(&to_fetch); |
| 7002 | } |
| 7003 | |
Dragan Simic | 4ca7a3f | 2023-09-23 06:01:14 +0200 | [diff] [blame] | 7004 | void init_diffstat_widths(struct diff_options *options) |
| 7005 | { |
| 7006 | options->stat_width = -1; /* use full terminal width */ |
| 7007 | options->stat_name_width = -1; /* respect diff.statNameWidth config */ |
| 7008 | options->stat_graph_width = -1; /* respect diff.statGraphWidth config */ |
| 7009 | } |
| 7010 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7011 | void diffcore_std(struct diff_options *options) |
| 7012 | { |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 7013 | int output_formats_to_prefetch = DIFF_FORMAT_DIFFSTAT | |
| 7014 | DIFF_FORMAT_NUMSTAT | |
| 7015 | DIFF_FORMAT_PATCH | |
| 7016 | DIFF_FORMAT_SHORTSTAT | |
| 7017 | DIFF_FORMAT_DIRSTAT; |
Jonathan Tan | 7fbbcb2 | 2019-04-05 10:09:34 -0700 | [diff] [blame] | 7018 | |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 7019 | /* |
| 7020 | * Check if the user requested a blob-data-requiring diff output and/or |
| 7021 | * break-rewrite detection (which requires blob data). If yes, prefetch |
| 7022 | * the diff pairs. |
| 7023 | * |
| 7024 | * If no prefetching occurs, diffcore_rename() will prefetch if it |
| 7025 | * decides that it needs inexact rename detection. |
| 7026 | */ |
Ævar Arnfjörð Bjarmason | a5183d7 | 2023-03-28 15:58:53 +0200 | [diff] [blame] | 7027 | if (options->repo == the_repository && repo_has_promisor_remote(the_repository) && |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 7028 | (options->output_format & output_formats_to_prefetch || |
| 7029 | options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)) |
| 7030 | diff_queued_diff_prefetch(options->repo); |
Jonathan Tan | 7fbbcb2 | 2019-04-05 10:09:34 -0700 | [diff] [blame] | 7031 | |
Kirill Smelkov | 7195fbf | 2014-02-24 20:21:51 +0400 | [diff] [blame] | 7032 | /* NOTE please keep the following in sync with diff_tree_combined() */ |
Junio C Hamano | 9d86535 | 2008-09-06 19:09:16 -0700 | [diff] [blame] | 7033 | if (options->skip_stat_unmatch) |
Junio C Hamano | fb13227 | 2007-08-03 13:33:31 -0700 | [diff] [blame] | 7034 | diffcore_skip_stat_unmatch(options); |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 7035 | if (!options->found_follow) { |
| 7036 | /* See try_to_follow_renames() in tree-diff.c */ |
| 7037 | if (options->break_opt != -1) |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 7038 | diffcore_break(options->repo, |
| 7039 | options->break_opt); |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 7040 | if (options->detect_rename) |
| 7041 | diffcore_rename(options); |
| 7042 | if (options->break_opt != -1) |
| 7043 | diffcore_merge_broken(); |
| 7044 | } |
Stefan Beller | cf63051 | 2018-01-04 14:50:41 -0800 | [diff] [blame] | 7045 | if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) |
Junio C Hamano | 382f013 | 2010-08-31 13:44:39 -0700 | [diff] [blame] | 7046 | diffcore_pickaxe(options); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7047 | if (options->orderfile) |
| 7048 | diffcore_order(options->orderfile); |
Junio C Hamano | 1eb4136 | 2021-02-11 11:57:50 -0800 | [diff] [blame] | 7049 | if (options->rotate_to) |
| 7050 | diffcore_rotate(options); |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 7051 | if (!options->found_follow) |
| 7052 | /* See try_to_follow_renames() in tree-diff.c */ |
| 7053 | diff_resolve_rename_copy(); |
Junio C Hamano | 949226f | 2013-07-17 14:19:24 -0700 | [diff] [blame] | 7054 | diffcore_apply_filter(options); |
Junio C Hamano | 68aacb2 | 2007-03-14 11:12:13 -0700 | [diff] [blame] | 7055 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7056 | if (diff_queued_diff.nr && !options->flags.diff_from_contents) |
| 7057 | options->flags.has_changes = 1; |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 7058 | else |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7059 | options->flags.has_changes = 0; |
Bo Yang | 1da6175 | 2010-05-06 21:52:28 -0700 | [diff] [blame] | 7060 | |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 7061 | options->found_follow = 0; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7062 | } |
| 7063 | |
Jeff King | 5cc6b2d | 2023-08-21 16:20:46 -0400 | [diff] [blame] | 7064 | int diff_result_code(struct diff_options *opt) |
Junio C Hamano | da31b35 | 2007-12-13 23:40:27 -0800 | [diff] [blame] | 7065 | { |
| 7066 | int result = 0; |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 7067 | |
Max Nanasy | c9fc441 | 2013-03-21 12:53:38 -0700 | [diff] [blame] | 7068 | diff_warn_rename_limit("diff.renameLimit", |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 7069 | opt->needed_rename_limit, |
| 7070 | opt->degraded_cc_to_c); |
Jeff King | 5cc6b2d | 2023-08-21 16:20:46 -0400 | [diff] [blame] | 7071 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7072 | if (opt->flags.exit_with_status && |
| 7073 | opt->flags.has_changes) |
Junio C Hamano | da31b35 | 2007-12-13 23:40:27 -0800 | [diff] [blame] | 7074 | result |= 01; |
| 7075 | if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7076 | opt->flags.check_failed) |
Junio C Hamano | da31b35 | 2007-12-13 23:40:27 -0800 | [diff] [blame] | 7077 | result |= 02; |
| 7078 | return result; |
| 7079 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7080 | |
Junio C Hamano | 28b9264 | 2011-05-31 09:14:17 -0700 | [diff] [blame] | 7081 | int diff_can_quit_early(struct diff_options *opt) |
| 7082 | { |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7083 | return (opt->flags.quick && |
Junio C Hamano | 28b9264 | 2011-05-31 09:14:17 -0700 | [diff] [blame] | 7084 | !opt->filter && |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7085 | opt->flags.has_changes); |
Junio C Hamano | 28b9264 | 2011-05-31 09:14:17 -0700 | [diff] [blame] | 7086 | } |
| 7087 | |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 7088 | /* |
| 7089 | * Shall changes to this submodule be ignored? |
| 7090 | * |
| 7091 | * Submodule changes can be configured to be ignored separately for each path, |
| 7092 | * but that configuration can be overridden from the command line. |
| 7093 | */ |
| 7094 | static int is_submodule_ignored(const char *path, struct diff_options *options) |
| 7095 | { |
| 7096 | int ignored = 0; |
Brandon Williams | 02f2f56 | 2017-10-31 11:19:05 -0700 | [diff] [blame] | 7097 | struct diff_flags orig_flags = options->flags; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7098 | if (!options->flags.override_submodule_config) |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 7099 | set_diffopt_flags_from_submodule_config(options, path); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7100 | if (options->flags.ignore_submodules) |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 7101 | ignored = 1; |
| 7102 | options->flags = orig_flags; |
| 7103 | return ignored; |
| 7104 | } |
| 7105 | |
Daniel Ferreira | e4cb659 | 2019-11-13 12:40:58 +0000 | [diff] [blame] | 7106 | void compute_diffstat(struct diff_options *options, |
| 7107 | struct diffstat_t *diffstat, |
| 7108 | struct diff_queue_struct *q) |
| 7109 | { |
| 7110 | int i; |
| 7111 | |
| 7112 | memset(diffstat, 0, sizeof(struct diffstat_t)); |
| 7113 | for (i = 0; i < q->nr; i++) { |
| 7114 | struct diff_filepair *p = q->queue[i]; |
| 7115 | if (check_pair_status(p)) |
| 7116 | diff_flush_stat(p, options, diffstat); |
| 7117 | } |
Junio C Hamano | e8efd86 | 2023-08-18 16:59:30 -0700 | [diff] [blame] | 7118 | options->found_changes = !!diffstat->nr; |
Daniel Ferreira | e4cb659 | 2019-11-13 12:40:58 +0000 | [diff] [blame] | 7119 | } |
| 7120 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7121 | void diff_addremove(struct diff_options *options, |
| 7122 | int addremove, unsigned mode, |
Brandon Williams | c26022e | 2017-05-30 10:30:47 -0700 | [diff] [blame] | 7123 | const struct object_id *oid, |
| 7124 | int oid_valid, |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 7125 | const char *concatpath, unsigned dirty_submodule) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7126 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7127 | struct diff_filespec *one, *two; |
| 7128 | |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 7129 | if (S_ISGITLINK(mode) && is_submodule_ignored(concatpath, options)) |
Johannes Schindelin | 50fd9bd | 2008-05-14 18:03:31 +0100 | [diff] [blame] | 7130 | return; |
| 7131 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7132 | /* This may look odd, but it is a preparation for |
| 7133 | * feeding "there are unchanged files which should |
| 7134 | * not produce diffs, but when you are doing copy |
| 7135 | * detection you would need them, so here they are" |
| 7136 | * entries to the diff-core. They will be prefixed |
| 7137 | * with something like '=' or '*' (I haven't decided |
| 7138 | * which but should not make any difference). |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 7139 | * Feeding the same new and old to diff_change() |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7140 | * also has the same effect. |
| 7141 | * Before the final output happens, they are pruned after |
| 7142 | * merged into rename/copy pairs as appropriate. |
| 7143 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7144 | if (options->flags.reverse_diff) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7145 | addremove = (addremove == '+' ? '-' : |
| 7146 | addremove == '-' ? '+' : addremove); |
| 7147 | |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 7148 | if (options->prefix && |
| 7149 | strncmp(concatpath, options->prefix, options->prefix_length)) |
| 7150 | return; |
| 7151 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7152 | one = alloc_filespec(concatpath); |
| 7153 | two = alloc_filespec(concatpath); |
| 7154 | |
| 7155 | if (addremove != '+') |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 7156 | fill_filespec(one, oid, oid_valid, mode); |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 7157 | if (addremove != '-') { |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 7158 | fill_filespec(two, oid, oid_valid, mode); |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 7159 | two->dirty_submodule = dirty_submodule; |
| 7160 | } |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7161 | |
| 7162 | diff_queue(&diff_queued_diff, one, two); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7163 | if (!options->flags.diff_from_contents) |
| 7164 | options->flags.has_changes = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7165 | } |
| 7166 | |
| 7167 | void diff_change(struct diff_options *options, |
| 7168 | unsigned old_mode, unsigned new_mode, |
Brandon Williams | 94a0097 | 2017-05-30 10:30:49 -0700 | [diff] [blame] | 7169 | const struct object_id *old_oid, |
| 7170 | const struct object_id *new_oid, |
| 7171 | int old_oid_valid, int new_oid_valid, |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 7172 | const char *concatpath, |
| 7173 | unsigned old_dirty_submodule, unsigned new_dirty_submodule) |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7174 | { |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7175 | struct diff_filespec *one, *two; |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 7176 | struct diff_filepair *p; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7177 | |
Jens Lehmann | aee9c7d | 2010-08-06 00:39:25 +0200 | [diff] [blame] | 7178 | if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) && |
| 7179 | is_submodule_ignored(concatpath, options)) |
Johannes Schindelin | 50fd9bd | 2008-05-14 18:03:31 +0100 | [diff] [blame] | 7180 | return; |
| 7181 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7182 | if (options->flags.reverse_diff) { |
René Scharfe | 35d803b | 2017-01-28 22:40:58 +0100 | [diff] [blame] | 7183 | SWAP(old_mode, new_mode); |
Brandon Williams | 94a0097 | 2017-05-30 10:30:49 -0700 | [diff] [blame] | 7184 | SWAP(old_oid, new_oid); |
| 7185 | SWAP(old_oid_valid, new_oid_valid); |
René Scharfe | 35d803b | 2017-01-28 22:40:58 +0100 | [diff] [blame] | 7186 | SWAP(old_dirty_submodule, new_dirty_submodule); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7187 | } |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 7188 | |
| 7189 | if (options->prefix && |
| 7190 | strncmp(concatpath, options->prefix, options->prefix_length)) |
| 7191 | return; |
| 7192 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7193 | one = alloc_filespec(concatpath); |
| 7194 | two = alloc_filespec(concatpath); |
Brandon Williams | f9704c2 | 2017-05-30 10:30:50 -0700 | [diff] [blame] | 7195 | fill_filespec(one, old_oid, old_oid_valid, old_mode); |
| 7196 | fill_filespec(two, new_oid, new_oid_valid, new_mode); |
Jens Lehmann | e3d42c4 | 2010-01-18 21:26:18 +0100 | [diff] [blame] | 7197 | one->dirty_submodule = old_dirty_submodule; |
| 7198 | two->dirty_submodule = new_dirty_submodule; |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 7199 | p = diff_queue(&diff_queued_diff, one, two); |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7200 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7201 | if (options->flags.diff_from_contents) |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 7202 | return; |
| 7203 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7204 | if (options->flags.quick && options->skip_stat_unmatch && |
Jeff King | d2d7fbe | 2020-06-01 16:22:18 -0400 | [diff] [blame] | 7205 | !diff_filespec_check_stat_unmatch(options->repo, p)) { |
| 7206 | diff_free_filespec_data(p->one); |
| 7207 | diff_free_filespec_data(p->two); |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 7208 | return; |
Jeff King | d2d7fbe | 2020-06-01 16:22:18 -0400 | [diff] [blame] | 7209 | } |
Nguyễn Thái Ngọc Duy | f34b205 | 2014-01-25 13:46:50 +0700 | [diff] [blame] | 7210 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7211 | options->flags.has_changes = 1; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7212 | } |
| 7213 | |
Junio C Hamano | fa7b290 | 2011-04-22 16:05:58 -0700 | [diff] [blame] | 7214 | 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] | 7215 | { |
Junio C Hamano | 76399c0 | 2011-04-22 15:55:55 -0700 | [diff] [blame] | 7216 | struct diff_filepair *pair; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7217 | struct diff_filespec *one, *two; |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 7218 | |
| 7219 | if (options->prefix && |
| 7220 | strncmp(path, options->prefix, options->prefix_length)) |
Junio C Hamano | 76399c0 | 2011-04-22 15:55:55 -0700 | [diff] [blame] | 7221 | return NULL; |
Junio C Hamano | cd676a5 | 2008-02-12 14:26:02 -0800 | [diff] [blame] | 7222 | |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7223 | one = alloc_filespec(path); |
| 7224 | two = alloc_filespec(path); |
Junio C Hamano | 76399c0 | 2011-04-22 15:55:55 -0700 | [diff] [blame] | 7225 | pair = diff_queue(&diff_queued_diff, one, two); |
| 7226 | pair->is_unmerged = 1; |
| 7227 | return pair; |
Junio C Hamano | 6973dca | 2006-04-21 23:57:45 -0700 | [diff] [blame] | 7228 | } |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7229 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 7230 | static char *run_textconv(struct repository *r, |
| 7231 | const char *pgm, |
| 7232 | struct diff_filespec *spec, |
| 7233 | size_t *outsize) |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7234 | { |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 7235 | struct diff_tempfile *temp; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 7236 | struct child_process child = CHILD_PROCESS_INIT; |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7237 | struct strbuf buf = STRBUF_INIT; |
Johannes Sixt | da1fbed | 2010-03-30 19:36:03 +0200 | [diff] [blame] | 7238 | int err = 0; |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7239 | |
Jeff King | f034bb1 | 2023-01-06 06:05:00 -0500 | [diff] [blame] | 7240 | temp = prepare_temp_file(r, spec); |
Ævar Arnfjörð Bjarmason | 7f14609 | 2021-11-25 23:52:21 +0100 | [diff] [blame] | 7241 | strvec_push(&child.args, pgm); |
| 7242 | strvec_push(&child.args, temp->name); |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7243 | |
Jeff King | 41a457e | 2009-12-30 06:01:09 -0500 | [diff] [blame] | 7244 | child.use_shell = 1; |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7245 | child.out = -1; |
Johannes Sixt | da1fbed | 2010-03-30 19:36:03 +0200 | [diff] [blame] | 7246 | if (start_command(&child)) { |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 7247 | remove_tempfile(); |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7248 | return NULL; |
| 7249 | } |
Johannes Sixt | da1fbed | 2010-03-30 19:36:03 +0200 | [diff] [blame] | 7250 | |
| 7251 | if (strbuf_read(&buf, child.out, 0) < 0) |
| 7252 | err = error("error reading from textconv command '%s'", pgm); |
Jeff King | 70d7099 | 2009-12-30 04:02:53 -0500 | [diff] [blame] | 7253 | close(child.out); |
Johannes Sixt | da1fbed | 2010-03-30 19:36:03 +0200 | [diff] [blame] | 7254 | |
| 7255 | if (finish_command(&child) || err) { |
| 7256 | strbuf_release(&buf); |
| 7257 | remove_tempfile(); |
| 7258 | return NULL; |
| 7259 | } |
Jeff King | 479b0ae | 2009-01-22 00:59:56 -0500 | [diff] [blame] | 7260 | remove_tempfile(); |
Jeff King | 9cb92c3 | 2008-10-05 17:43:45 -0400 | [diff] [blame] | 7261 | |
| 7262 | return strbuf_detach(&buf, outsize); |
| 7263 | } |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7264 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 7265 | size_t fill_textconv(struct repository *r, |
| 7266 | struct userdiff_driver *driver, |
Axel Bonnet | a788d7d | 2010-06-07 17:23:36 +0200 | [diff] [blame] | 7267 | struct diff_filespec *df, |
| 7268 | char **outbuf) |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7269 | { |
| 7270 | size_t size; |
| 7271 | |
Jeff King | a64e6a4 | 2016-02-22 13:28:54 -0500 | [diff] [blame] | 7272 | if (!driver) { |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7273 | if (!DIFF_FILE_VALID(df)) { |
Patrick Steinhardt | 86badd4 | 2024-06-07 08:38:06 +0200 | [diff] [blame] | 7274 | *outbuf = (char *) ""; |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7275 | return 0; |
| 7276 | } |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 7277 | if (diff_populate_filespec(r, df, NULL)) |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7278 | die("unable to read files to diff"); |
| 7279 | *outbuf = df->data; |
| 7280 | return df->size; |
| 7281 | } |
| 7282 | |
Jeff King | a64e6a4 | 2016-02-22 13:28:54 -0500 | [diff] [blame] | 7283 | if (!driver->textconv) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 7284 | BUG("fill_textconv called with non-textconv driver"); |
Jeff King | a64e6a4 | 2016-02-22 13:28:54 -0500 | [diff] [blame] | 7285 | |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 7286 | if (driver->textconv_cache && df->oid_valid) { |
brian m. carlson | a0d12c4 | 2016-06-24 23:09:23 +0000 | [diff] [blame] | 7287 | *outbuf = notes_cache_get(driver->textconv_cache, |
brian m. carlson | 569aa37 | 2017-05-06 22:09:58 +0000 | [diff] [blame] | 7288 | &df->oid, |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 7289 | &size); |
| 7290 | if (*outbuf) |
| 7291 | return size; |
| 7292 | } |
| 7293 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 7294 | *outbuf = run_textconv(r, driver->textconv, df, &size); |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7295 | if (!*outbuf) |
| 7296 | die("unable to read files to diff"); |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 7297 | |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 7298 | if (driver->textconv_cache && df->oid_valid) { |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 7299 | /* ignore errors, as we might be in a readonly repository */ |
brian m. carlson | 569aa37 | 2017-05-06 22:09:58 +0000 | [diff] [blame] | 7300 | notes_cache_put(driver->textconv_cache, &df->oid, *outbuf, |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 7301 | size); |
| 7302 | /* |
| 7303 | * we could save up changes and flush them all at the end, |
| 7304 | * but we would need an extra call after all diffing is done. |
| 7305 | * Since generating a cache entry is the slow path anyway, |
| 7306 | * this extra overhead probably isn't a big deal. |
| 7307 | */ |
| 7308 | notes_cache_write(driver->textconv_cache); |
| 7309 | } |
| 7310 | |
Jeff King | 840383b | 2010-04-01 20:09:26 -0400 | [diff] [blame] | 7311 | return size; |
| 7312 | } |
Nguyễn Thái Ngọc Duy | 4914c96 | 2012-10-26 22:53:52 +0700 | [diff] [blame] | 7313 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 7314 | int textconv_object(struct repository *r, |
| 7315 | const char *path, |
Jeff Smith | 3a35cb2 | 2017-05-24 00:15:10 -0500 | [diff] [blame] | 7316 | unsigned mode, |
| 7317 | const struct object_id *oid, |
| 7318 | int oid_valid, |
| 7319 | char **buf, |
| 7320 | unsigned long *buf_size) |
| 7321 | { |
| 7322 | struct diff_filespec *df; |
| 7323 | struct userdiff_driver *textconv; |
| 7324 | |
| 7325 | df = alloc_filespec(path); |
Junio C Hamano | a6f38c1 | 2017-06-19 12:38:44 -0700 | [diff] [blame] | 7326 | fill_filespec(df, oid, oid_valid, mode); |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 7327 | textconv = get_textconv(r, df); |
Jeff Smith | 3a35cb2 | 2017-05-24 00:15:10 -0500 | [diff] [blame] | 7328 | if (!textconv) { |
| 7329 | free_filespec(df); |
| 7330 | return 0; |
| 7331 | } |
| 7332 | |
Nguyễn Thái Ngọc Duy | 6afaf80 | 2018-09-21 17:57:22 +0200 | [diff] [blame] | 7333 | *buf_size = fill_textconv(r, textconv, df, buf); |
Jeff Smith | 3a35cb2 | 2017-05-24 00:15:10 -0500 | [diff] [blame] | 7334 | free_filespec(df); |
| 7335 | return 1; |
| 7336 | } |
| 7337 | |
Nguyễn Thái Ngọc Duy | 4914c96 | 2012-10-26 22:53:52 +0700 | [diff] [blame] | 7338 | void setup_diff_pager(struct diff_options *opt) |
| 7339 | { |
| 7340 | /* |
| 7341 | * If the user asked for our exit code, then either they want --quiet |
| 7342 | * or --exit-code. We should definitely not bother with a pager in the |
| 7343 | * former case, as we will generate no output. Since we still properly |
| 7344 | * report our exit code even when a pager is run, we _could_ run a |
| 7345 | * pager with --exit-code. But since we have not done so historically, |
| 7346 | * and because it is easy to find people oneline advising "git diff |
| 7347 | * --exit-code" in hooks and other scripts, we do not do so. |
| 7348 | */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 7349 | if (!opt->flags.exit_with_status && |
Nguyễn Thái Ngọc Duy | 4914c96 | 2012-10-26 22:53:52 +0700 | [diff] [blame] | 7350 | check_pager_config("diff") != 0) |
| 7351 | setup_pager(); |
| 7352 | } |