Elijah Newren | 5e3f94d | 2023-04-22 20:17:23 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 2 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 3 | #include "gettext.h" |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 4 | #include "range-diff.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 5 | #include "object-name.h" |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 6 | #include "string-list.h" |
| 7 | #include "run-command.h" |
Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 8 | #include "strvec.h" |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 9 | #include "hashmap.h" |
| 10 | #include "xdiff-interface.h" |
| 11 | #include "linear-assignment.h" |
Johannes Schindelin | c8c5e43 | 2018-08-13 04:33:07 -0700 | [diff] [blame] | 12 | #include "diffcore.h" |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 13 | #include "commit.h" |
Elijah Newren | ca4eed7 | 2023-04-11 00:41:59 -0700 | [diff] [blame] | 14 | #include "pager.h" |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 15 | #include "pretty.h" |
Elijah Newren | df6e874 | 2023-05-16 06:34:00 +0000 | [diff] [blame] | 16 | #include "repository.h" |
Johannes Schindelin | 4eba1fe | 2018-08-13 04:33:14 -0700 | [diff] [blame] | 17 | #include "userdiff.h" |
Thomas Gummerer | b66885a | 2019-07-11 17:08:49 +0100 | [diff] [blame] | 18 | #include "apply.h" |
Johannes Schindelin | 359f0d7 | 2021-02-05 14:44:48 +0000 | [diff] [blame] | 19 | #include "revision.h" |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 20 | |
| 21 | struct patch_util { |
| 22 | /* For the search for an exact match */ |
| 23 | struct hashmap_entry e; |
| 24 | const char *diff, *patch; |
| 25 | |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 26 | int i, shown; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 27 | int diffsize; |
| 28 | size_t diff_offset; |
| 29 | /* the index of the matching item in the other branch, or -1 */ |
| 30 | int matching; |
| 31 | struct object_id oid; |
| 32 | }; |
| 33 | |
| 34 | /* |
| 35 | * Reads the patches into a string list, with the `util` field being populated |
| 36 | * as struct object_id (will need to be free()d). |
| 37 | */ |
Denton Liu | bd36191 | 2019-11-20 13:18:45 -0800 | [diff] [blame] | 38 | static int read_patches(const char *range, struct string_list *list, |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 39 | const struct strvec *other_arg) |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 40 | { |
| 41 | struct child_process cp = CHILD_PROCESS_INIT; |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 42 | struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 43 | struct patch_util *util = NULL; |
| 44 | int in_header = 1; |
Thomas Gummerer | 444e096 | 2019-07-11 17:08:50 +0100 | [diff] [blame] | 45 | char *line, *current_filename = NULL; |
Jeff King | c4d5907 | 2021-08-09 18:48:48 -0400 | [diff] [blame] | 46 | ssize_t len; |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 47 | size_t size; |
Ævar Arnfjörð Bjarmason | 2d102c2 | 2022-03-04 19:32:16 +0100 | [diff] [blame] | 48 | int ret = -1; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 49 | |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 50 | strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges", |
Jeff King | f6d8942 | 2020-07-28 16:26:31 -0400 | [diff] [blame] | 51 | "--reverse", "--date-order", "--decorate=no", |
Philippe Blain | 04b1f1f | 2022-06-06 20:59:13 +0000 | [diff] [blame] | 52 | "--no-prefix", "--submodule=short", |
Jeff King | f6d8942 | 2020-07-28 16:26:31 -0400 | [diff] [blame] | 53 | /* |
| 54 | * Choose indicators that are not used anywhere |
| 55 | * else in diffs, but still look reasonable |
| 56 | * (e.g. will not be confusing when debugging) |
| 57 | */ |
| 58 | "--output-indicator-new=>", |
| 59 | "--output-indicator-old=<", |
| 60 | "--output-indicator-context=#", |
| 61 | "--no-abbrev-commit", |
| 62 | "--pretty=medium", |
Kristoffer Haugsbakk | 2e0d30d | 2023-09-19 22:26:50 +0200 | [diff] [blame] | 63 | "--show-notes-by-default", |
Jeff King | f6d8942 | 2020-07-28 16:26:31 -0400 | [diff] [blame] | 64 | NULL); |
Johannes Schindelin | b757478 | 2022-08-26 09:39:30 +0000 | [diff] [blame] | 65 | strvec_push(&cp.args, range); |
Denton Liu | bd36191 | 2019-11-20 13:18:45 -0800 | [diff] [blame] | 66 | if (other_arg) |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 67 | strvec_pushv(&cp.args, other_arg->v); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 68 | cp.out = -1; |
| 69 | cp.no_stdin = 1; |
| 70 | cp.git_cmd = 1; |
| 71 | |
| 72 | if (start_command(&cp)) |
| 73 | return error_errno(_("could not start `log`")); |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 74 | if (strbuf_read(&contents, cp.out, 0) < 0) { |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 75 | error_errno(_("could not read `log` output")); |
| 76 | finish_command(&cp); |
Ævar Arnfjörð Bjarmason | 2d102c2 | 2022-03-04 19:32:16 +0100 | [diff] [blame] | 77 | goto cleanup; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 78 | } |
Johannes Schindelin | 5189bb8 | 2021-02-04 20:07:51 +0000 | [diff] [blame] | 79 | if (finish_command(&cp)) |
Ævar Arnfjörð Bjarmason | 2d102c2 | 2022-03-04 19:32:16 +0100 | [diff] [blame] | 80 | goto cleanup; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 81 | |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 82 | line = contents.buf; |
| 83 | size = contents.len; |
Jeff King | 47ac23d | 2021-08-09 18:47:42 -0400 | [diff] [blame] | 84 | for (; size > 0; size -= len, line += len) { |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 85 | const char *p; |
Jeff King | 7c86d36 | 2021-08-09 18:48:39 -0400 | [diff] [blame] | 86 | char *eol; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 87 | |
Jeff King | 7c86d36 | 2021-08-09 18:48:39 -0400 | [diff] [blame] | 88 | eol = memchr(line, '\n', size); |
| 89 | if (eol) { |
| 90 | *eol = '\0'; |
| 91 | len = eol + 1 - line; |
| 92 | } else { |
| 93 | len = size; |
| 94 | } |
| 95 | |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 96 | if (skip_prefix(line, "commit ", &p)) { |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 97 | if (util) { |
| 98 | string_list_append(list, buf.buf)->util = util; |
| 99 | strbuf_reset(&buf); |
| 100 | } |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 101 | CALLOC_ARRAY(util, 1); |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 102 | if (repo_get_oid(the_repository, p, &util->oid)) { |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 103 | error(_("could not parse commit '%s'"), p); |
Ævar Arnfjörð Bjarmason | 2d102c2 | 2022-03-04 19:32:16 +0100 | [diff] [blame] | 104 | FREE_AND_NULL(util); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 105 | string_list_clear(list, 1); |
Ævar Arnfjörð Bjarmason | 2d102c2 | 2022-03-04 19:32:16 +0100 | [diff] [blame] | 106 | goto cleanup; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 107 | } |
| 108 | util->matching = -1; |
| 109 | in_header = 1; |
| 110 | continue; |
| 111 | } |
| 112 | |
Vasil Dimov | 8cf5156 | 2020-04-15 20:32:24 +0000 | [diff] [blame] | 113 | if (!util) { |
| 114 | error(_("could not parse first line of `log` output: " |
| 115 | "did not start with 'commit ': '%s'"), |
| 116 | line); |
| 117 | string_list_clear(list, 1); |
Ævar Arnfjörð Bjarmason | 2d102c2 | 2022-03-04 19:32:16 +0100 | [diff] [blame] | 118 | goto cleanup; |
Vasil Dimov | 8cf5156 | 2020-04-15 20:32:24 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 121 | if (starts_with(line, "diff --git")) { |
Thomas Gummerer | b66885a | 2019-07-11 17:08:49 +0100 | [diff] [blame] | 122 | struct patch patch = { 0 }; |
| 123 | struct strbuf root = STRBUF_INIT; |
| 124 | int linenr = 0; |
Vasil Dimov | 8d1675e | 2020-04-15 20:32:25 +0000 | [diff] [blame] | 125 | int orig_len; |
Thomas Gummerer | b66885a | 2019-07-11 17:08:49 +0100 | [diff] [blame] | 126 | |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 127 | in_header = 0; |
| 128 | strbuf_addch(&buf, '\n'); |
| 129 | if (!util->diff_offset) |
| 130 | util->diff_offset = buf.len; |
Jeff King | 7c86d36 | 2021-08-09 18:48:39 -0400 | [diff] [blame] | 131 | if (eol) |
| 132 | *eol = '\n'; |
Vasil Dimov | 8d1675e | 2020-04-15 20:32:25 +0000 | [diff] [blame] | 133 | orig_len = len; |
Johannes Schindelin | 937b76e | 2019-10-02 14:10:47 -0700 | [diff] [blame] | 134 | len = parse_git_diff_header(&root, &linenr, 0, line, |
Thomas Gummerer | b66885a | 2019-07-11 17:08:49 +0100 | [diff] [blame] | 135 | len, size, &patch); |
Johannes Schindelin | a2d474a | 2021-02-04 20:07:50 +0000 | [diff] [blame] | 136 | if (len < 0) { |
| 137 | error(_("could not parse git header '%.*s'"), |
| 138 | orig_len, line); |
Ævar Arnfjörð Bjarmason | 2d102c2 | 2022-03-04 19:32:16 +0100 | [diff] [blame] | 139 | FREE_AND_NULL(util); |
Johannes Schindelin | a2d474a | 2021-02-04 20:07:50 +0000 | [diff] [blame] | 140 | string_list_clear(list, 1); |
Ævar Arnfjörð Bjarmason | 2d102c2 | 2022-03-04 19:32:16 +0100 | [diff] [blame] | 141 | goto cleanup; |
Johannes Schindelin | a2d474a | 2021-02-04 20:07:50 +0000 | [diff] [blame] | 142 | } |
Thomas Gummerer | b66885a | 2019-07-11 17:08:49 +0100 | [diff] [blame] | 143 | strbuf_addstr(&buf, " ## "); |
| 144 | if (patch.is_new > 0) |
| 145 | strbuf_addf(&buf, "%s (new)", patch.new_name); |
| 146 | else if (patch.is_delete > 0) |
| 147 | strbuf_addf(&buf, "%s (deleted)", patch.old_name); |
| 148 | else if (patch.is_rename) |
| 149 | strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name); |
| 150 | else |
| 151 | strbuf_addstr(&buf, patch.new_name); |
| 152 | |
Thomas Gummerer | 444e096 | 2019-07-11 17:08:50 +0100 | [diff] [blame] | 153 | free(current_filename); |
| 154 | if (patch.is_delete > 0) |
| 155 | current_filename = xstrdup(patch.old_name); |
| 156 | else |
| 157 | current_filename = xstrdup(patch.new_name); |
| 158 | |
Thomas Gummerer | b66885a | 2019-07-11 17:08:49 +0100 | [diff] [blame] | 159 | if (patch.new_mode && patch.old_mode && |
| 160 | patch.old_mode != patch.new_mode) |
| 161 | strbuf_addf(&buf, " (mode change %06o => %06o)", |
| 162 | patch.old_mode, patch.new_mode); |
| 163 | |
| 164 | strbuf_addstr(&buf, " ##"); |
Ævar Arnfjörð Bjarmason | 4998e93 | 2022-03-04 19:32:15 +0100 | [diff] [blame] | 165 | release_patch(&patch); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 166 | } else if (in_header) { |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 167 | if (starts_with(line, "Author: ")) { |
Thomas Gummerer | 499352c | 2019-07-11 17:08:51 +0100 | [diff] [blame] | 168 | strbuf_addstr(&buf, " ## Metadata ##\n"); |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 169 | strbuf_addstr(&buf, line); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 170 | strbuf_addstr(&buf, "\n\n"); |
Thomas Gummerer | 499352c | 2019-07-11 17:08:51 +0100 | [diff] [blame] | 171 | strbuf_addstr(&buf, " ## Commit message ##\n"); |
Denton Liu | 9f726e1 | 2019-11-20 13:18:43 -0800 | [diff] [blame] | 172 | } else if (starts_with(line, "Notes") && |
| 173 | line[strlen(line) - 1] == ':') { |
| 174 | strbuf_addstr(&buf, "\n\n"); |
| 175 | /* strip the trailing colon */ |
| 176 | strbuf_addf(&buf, " ## %.*s ##\n", |
| 177 | (int)(strlen(line) - 1), line); |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 178 | } else if (starts_with(line, " ")) { |
| 179 | p = line + len - 2; |
| 180 | while (isspace(*p) && p >= line) |
| 181 | p--; |
| 182 | strbuf_add(&buf, line, p - line + 1); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 183 | strbuf_addch(&buf, '\n'); |
| 184 | } |
| 185 | continue; |
Thomas Gummerer | e1db263 | 2019-07-11 17:08:47 +0100 | [diff] [blame] | 186 | } else if (skip_prefix(line, "@@ ", &p)) { |
| 187 | p = strstr(p, "@@"); |
Thomas Gummerer | 444e096 | 2019-07-11 17:08:50 +0100 | [diff] [blame] | 188 | strbuf_addstr(&buf, "@@"); |
| 189 | if (current_filename && p[2]) |
| 190 | strbuf_addf(&buf, " %s:", current_filename); |
| 191 | if (p) |
| 192 | strbuf_addstr(&buf, p + 2); |
Thomas Gummerer | b66885a | 2019-07-11 17:08:49 +0100 | [diff] [blame] | 193 | } else if (!line[0]) |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 194 | /* |
| 195 | * A completely blank (not ' \n', which is context) |
| 196 | * line is not valid in a diff. We skip it |
| 197 | * silently, because this neatly handles the blank |
| 198 | * separator line between commits in git-log |
| 199 | * output. |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 200 | */ |
| 201 | continue; |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 202 | else if (line[0] == '>') { |
Stefan Beller | 8d5ccb5 | 2018-08-17 13:43:53 -0700 | [diff] [blame] | 203 | strbuf_addch(&buf, '+'); |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 204 | strbuf_addstr(&buf, line + 1); |
| 205 | } else if (line[0] == '<') { |
Stefan Beller | 8d5ccb5 | 2018-08-17 13:43:53 -0700 | [diff] [blame] | 206 | strbuf_addch(&buf, '-'); |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 207 | strbuf_addstr(&buf, line + 1); |
| 208 | } else if (line[0] == '#') { |
Stefan Beller | 8d5ccb5 | 2018-08-17 13:43:53 -0700 | [diff] [blame] | 209 | strbuf_addch(&buf, ' '); |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 210 | strbuf_addstr(&buf, line + 1); |
Stefan Beller | 8d5ccb5 | 2018-08-17 13:43:53 -0700 | [diff] [blame] | 211 | } else { |
Stefan Beller | 2543a64 | 2018-08-17 13:43:54 -0700 | [diff] [blame] | 212 | strbuf_addch(&buf, ' '); |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 213 | strbuf_addstr(&buf, line); |
Stefan Beller | 8d5ccb5 | 2018-08-17 13:43:53 -0700 | [diff] [blame] | 214 | } |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 215 | |
| 216 | strbuf_addch(&buf, '\n'); |
| 217 | util->diffsize++; |
| 218 | } |
Ævar Arnfjörð Bjarmason | 2d102c2 | 2022-03-04 19:32:16 +0100 | [diff] [blame] | 219 | |
| 220 | ret = 0; |
| 221 | cleanup: |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 222 | strbuf_release(&contents); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 223 | |
| 224 | if (util) |
| 225 | string_list_append(list, buf.buf)->util = util; |
| 226 | strbuf_release(&buf); |
Thomas Gummerer | 444e096 | 2019-07-11 17:08:50 +0100 | [diff] [blame] | 227 | free(current_filename); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 228 | |
Ævar Arnfjörð Bjarmason | 2d102c2 | 2022-03-04 19:32:16 +0100 | [diff] [blame] | 229 | return ret; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 232 | static int patch_util_cmp(const void *cmp_data UNUSED, |
Jeff King | beaa1d9 | 2023-08-19 19:55:30 -0400 | [diff] [blame] | 233 | const struct hashmap_entry *ha, |
| 234 | const struct hashmap_entry *hb, |
| 235 | const void *keydata) |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 236 | { |
Jeff King | beaa1d9 | 2023-08-19 19:55:30 -0400 | [diff] [blame] | 237 | const struct patch_util |
| 238 | *a = container_of(ha, const struct patch_util, e), |
| 239 | *b = container_of(hb, const struct patch_util, e); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 240 | return strcmp(a->diff, keydata ? keydata : b->diff); |
| 241 | } |
| 242 | |
| 243 | static void find_exact_matches(struct string_list *a, struct string_list *b) |
| 244 | { |
Jeff King | beaa1d9 | 2023-08-19 19:55:30 -0400 | [diff] [blame] | 245 | struct hashmap map = HASHMAP_INIT(patch_util_cmp, NULL); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 246 | int i; |
| 247 | |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 248 | /* First, add the patches of a to a hash map */ |
| 249 | for (i = 0; i < a->nr; i++) { |
| 250 | struct patch_util *util = a->items[i].util; |
| 251 | |
| 252 | util->i = i; |
| 253 | util->patch = a->items[i].string; |
| 254 | util->diff = util->patch + util->diff_offset; |
Eric Wong | d22245a | 2019-10-06 23:30:27 +0000 | [diff] [blame] | 255 | hashmap_entry_init(&util->e, strhash(util->diff)); |
Eric Wong | b94e5c1 | 2019-10-06 23:30:29 +0000 | [diff] [blame] | 256 | hashmap_add(&map, &util->e); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* Now try to find exact matches in b */ |
| 260 | for (i = 0; i < b->nr; i++) { |
| 261 | struct patch_util *util = b->items[i].util, *other; |
| 262 | |
| 263 | util->i = i; |
| 264 | util->patch = b->items[i].string; |
| 265 | util->diff = util->patch + util->diff_offset; |
Eric Wong | d22245a | 2019-10-06 23:30:27 +0000 | [diff] [blame] | 266 | hashmap_entry_init(&util->e, strhash(util->diff)); |
Eric Wong | 404ab78 | 2019-10-06 23:30:42 +0000 | [diff] [blame] | 267 | other = hashmap_remove_entry(&map, util, e, NULL); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 268 | if (other) { |
| 269 | if (other->matching >= 0) |
| 270 | BUG("already assigned!"); |
| 271 | |
| 272 | other->matching = i; |
| 273 | util->matching = other->i; |
| 274 | } |
| 275 | } |
| 276 | |
Elijah Newren | 6da1a25 | 2020-11-02 18:55:05 +0000 | [diff] [blame] | 277 | hashmap_clear(&map); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Jeff King | 61bdc7c | 2022-12-13 06:13:48 -0500 | [diff] [blame] | 280 | static int diffsize_consume(void *data, |
| 281 | char *line UNUSED, |
| 282 | unsigned long len UNUSED) |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 283 | { |
| 284 | (*(int *)data)++; |
Ævar Arnfjörð Bjarmason | a8d5eb6 | 2021-04-12 19:15:24 +0200 | [diff] [blame] | 285 | return 0; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Jeff King | 61bdc7c | 2022-12-13 06:13:48 -0500 | [diff] [blame] | 288 | static void diffsize_hunk(void *data, |
| 289 | long ob UNUSED, long on UNUSED, |
| 290 | long nb UNUSED, long nn UNUSED, |
| 291 | const char *func UNUSED, long funclen UNUSED) |
Jeff King | d2eb809 | 2018-11-02 02:39:40 -0400 | [diff] [blame] | 292 | { |
| 293 | diffsize_consume(data, NULL, 0); |
| 294 | } |
| 295 | |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 296 | static int diffsize(const char *a, const char *b) |
| 297 | { |
| 298 | xpparam_t pp = { 0 }; |
| 299 | xdemitconf_t cfg = { 0 }; |
| 300 | mmfile_t mf1, mf2; |
| 301 | int count = 0; |
| 302 | |
| 303 | mf1.ptr = (char *)a; |
| 304 | mf1.size = strlen(a); |
| 305 | mf2.ptr = (char *)b; |
| 306 | mf2.size = strlen(b); |
| 307 | |
| 308 | cfg.ctxlen = 3; |
Jeff King | d2eb809 | 2018-11-02 02:39:40 -0400 | [diff] [blame] | 309 | if (!xdi_diff_outf(&mf1, &mf2, |
| 310 | diffsize_hunk, diffsize_consume, &count, |
| 311 | &pp, &cfg)) |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 312 | return count; |
| 313 | |
| 314 | error(_("failed to generate diff")); |
| 315 | return COST_MAX; |
| 316 | } |
| 317 | |
| 318 | static void get_correspondences(struct string_list *a, struct string_list *b, |
| 319 | int creation_factor) |
| 320 | { |
| 321 | int n = a->nr + b->nr; |
| 322 | int *cost, c, *a2b, *b2a; |
| 323 | int i, j; |
| 324 | |
| 325 | ALLOC_ARRAY(cost, st_mult(n, n)); |
| 326 | ALLOC_ARRAY(a2b, n); |
| 327 | ALLOC_ARRAY(b2a, n); |
| 328 | |
| 329 | for (i = 0; i < a->nr; i++) { |
| 330 | struct patch_util *a_util = a->items[i].util; |
| 331 | |
| 332 | for (j = 0; j < b->nr; j++) { |
| 333 | struct patch_util *b_util = b->items[j].util; |
| 334 | |
| 335 | if (a_util->matching == j) |
| 336 | c = 0; |
| 337 | else if (a_util->matching < 0 && b_util->matching < 0) |
| 338 | c = diffsize(a_util->diff, b_util->diff); |
| 339 | else |
| 340 | c = COST_MAX; |
| 341 | cost[i + n * j] = c; |
| 342 | } |
| 343 | |
| 344 | c = a_util->matching < 0 ? |
| 345 | a_util->diffsize * creation_factor / 100 : COST_MAX; |
| 346 | for (j = b->nr; j < n; j++) |
| 347 | cost[i + n * j] = c; |
| 348 | } |
| 349 | |
| 350 | for (j = 0; j < b->nr; j++) { |
| 351 | struct patch_util *util = b->items[j].util; |
| 352 | |
| 353 | c = util->matching < 0 ? |
| 354 | util->diffsize * creation_factor / 100 : COST_MAX; |
| 355 | for (i = a->nr; i < n; i++) |
| 356 | cost[i + n * j] = c; |
| 357 | } |
| 358 | |
| 359 | for (i = a->nr; i < n; i++) |
| 360 | for (j = b->nr; j < n; j++) |
| 361 | cost[i + n * j] = 0; |
| 362 | |
| 363 | compute_assignment(n, n, cost, a2b, b2a); |
| 364 | |
| 365 | for (i = 0; i < a->nr; i++) |
| 366 | if (a2b[i] >= 0 && a2b[i] < b->nr) { |
| 367 | struct patch_util *a_util = a->items[i].util; |
| 368 | struct patch_util *b_util = b->items[a2b[i]].util; |
| 369 | |
| 370 | a_util->matching = a2b[i]; |
| 371 | b_util->matching = i; |
| 372 | } |
| 373 | |
| 374 | free(cost); |
| 375 | free(a2b); |
| 376 | free(b2a); |
| 377 | } |
| 378 | |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 379 | static void output_pair_header(struct diff_options *diffopt, |
Johannes Schindelin | d1f87a2 | 2018-08-13 04:33:28 -0700 | [diff] [blame] | 380 | int patch_no_width, |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 381 | struct strbuf *buf, |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 382 | struct strbuf *dashes, |
| 383 | struct patch_util *a_util, |
| 384 | struct patch_util *b_util) |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 385 | { |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 386 | struct object_id *oid = a_util ? &a_util->oid : &b_util->oid; |
| 387 | struct commit *commit; |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 388 | char status; |
| 389 | const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET); |
| 390 | const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD); |
| 391 | const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW); |
| 392 | const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT); |
| 393 | const char *color; |
René Scharfe | d9165be | 2023-02-28 17:13:27 +0100 | [diff] [blame] | 394 | int abbrev = diffopt->abbrev; |
Teng Long | 2b15969 | 2023-02-20 22:24:44 +0800 | [diff] [blame] | 395 | |
| 396 | if (abbrev < 0) |
| 397 | abbrev = DEFAULT_ABBREV; |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 398 | |
| 399 | if (!dashes->len) |
| 400 | strbuf_addchars(dashes, '-', |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 401 | strlen(repo_find_unique_abbrev(the_repository, oid, abbrev))); |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 402 | |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 403 | if (!b_util) { |
| 404 | color = color_old; |
| 405 | status = '<'; |
| 406 | } else if (!a_util) { |
| 407 | color = color_new; |
| 408 | status = '>'; |
| 409 | } else if (strcmp(a_util->patch, b_util->patch)) { |
| 410 | color = color_commit; |
| 411 | status = '!'; |
| 412 | } else { |
| 413 | color = color_commit; |
| 414 | status = '='; |
| 415 | } |
| 416 | |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 417 | strbuf_reset(buf); |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 418 | strbuf_addstr(buf, status == '!' ? color_old : color); |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 419 | if (!a_util) |
Johannes Schindelin | d1f87a2 | 2018-08-13 04:33:28 -0700 | [diff] [blame] | 420 | strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf); |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 421 | else |
Johannes Schindelin | d1f87a2 | 2018-08-13 04:33:28 -0700 | [diff] [blame] | 422 | strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1, |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 423 | repo_find_unique_abbrev(the_repository, &a_util->oid, abbrev)); |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 424 | |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 425 | if (status == '!') |
| 426 | strbuf_addf(buf, "%s%s", color_reset, color); |
| 427 | strbuf_addch(buf, status); |
| 428 | if (status == '!') |
| 429 | strbuf_addf(buf, "%s%s", color_reset, color_new); |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 430 | |
| 431 | if (!b_util) |
Johannes Schindelin | d1f87a2 | 2018-08-13 04:33:28 -0700 | [diff] [blame] | 432 | strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf); |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 433 | else |
Johannes Schindelin | d1f87a2 | 2018-08-13 04:33:28 -0700 | [diff] [blame] | 434 | strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1, |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 435 | repo_find_unique_abbrev(the_repository, &b_util->oid, abbrev)); |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 436 | |
| 437 | commit = lookup_commit_reference(the_repository, oid); |
| 438 | if (commit) { |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 439 | if (status == '!') |
| 440 | strbuf_addf(buf, "%s%s", color_reset, color); |
| 441 | |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 442 | strbuf_addch(buf, ' '); |
| 443 | pp_commit_easy(CMIT_FMT_ONELINE, commit, buf); |
| 444 | } |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 445 | strbuf_addf(buf, "%s\n", color_reset); |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 446 | |
Eric Sunshine | 87f1b2d | 2018-07-22 05:57:10 -0400 | [diff] [blame] | 447 | fwrite(buf->buf, buf->len, 1, diffopt->file); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Thomas Gummerer | 499352c | 2019-07-11 17:08:51 +0100 | [diff] [blame] | 450 | static struct userdiff_driver section_headers = { |
| 451 | .funcname = { "^ ## (.*) ##$\n" |
| 452 | "^.?@@ (.*)$", REG_EXTENDED } |
Johannes Schindelin | 4eba1fe | 2018-08-13 04:33:14 -0700 | [diff] [blame] | 453 | }; |
| 454 | |
Johannes Schindelin | c8c5e43 | 2018-08-13 04:33:07 -0700 | [diff] [blame] | 455 | static struct diff_filespec *get_filespec(const char *name, const char *p) |
| 456 | { |
| 457 | struct diff_filespec *spec = alloc_filespec(name); |
| 458 | |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 459 | fill_filespec(spec, null_oid(), 0, 0100644); |
Johannes Schindelin | c8c5e43 | 2018-08-13 04:33:07 -0700 | [diff] [blame] | 460 | spec->data = (char *)p; |
| 461 | spec->size = strlen(p); |
| 462 | spec->should_munmap = 0; |
| 463 | spec->is_stdin = 1; |
Thomas Gummerer | 499352c | 2019-07-11 17:08:51 +0100 | [diff] [blame] | 464 | spec->driver = §ion_headers; |
Johannes Schindelin | c8c5e43 | 2018-08-13 04:33:07 -0700 | [diff] [blame] | 465 | |
| 466 | return spec; |
| 467 | } |
| 468 | |
| 469 | static void patch_diff(const char *a, const char *b, |
Thomas Gummerer | 1ca6922 | 2019-07-11 17:08:45 +0100 | [diff] [blame] | 470 | struct diff_options *diffopt) |
Johannes Schindelin | c8c5e43 | 2018-08-13 04:33:07 -0700 | [diff] [blame] | 471 | { |
| 472 | diff_queue(&diff_queued_diff, |
| 473 | get_filespec("a", a), get_filespec("b", b)); |
| 474 | |
| 475 | diffcore_std(diffopt); |
| 476 | diff_flush(diffopt); |
| 477 | } |
| 478 | |
Jeff King | 61bdc7c | 2022-12-13 06:13:48 -0500 | [diff] [blame] | 479 | static struct strbuf *output_prefix_cb(struct diff_options *opt UNUSED, void *data) |
Johannes Schindelin | 3e6046e | 2021-02-05 14:46:12 +0000 | [diff] [blame] | 480 | { |
| 481 | return data; |
| 482 | } |
| 483 | |
Johannes Schindelin | c8c5e43 | 2018-08-13 04:33:07 -0700 | [diff] [blame] | 484 | static void output(struct string_list *a, struct string_list *b, |
Johannes Schindelin | 3e6046e | 2021-02-05 14:46:12 +0000 | [diff] [blame] | 485 | struct range_diff_options *range_diff_opts) |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 486 | { |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 487 | struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT; |
Johannes Schindelin | d1f87a2 | 2018-08-13 04:33:28 -0700 | [diff] [blame] | 488 | int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr)); |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 489 | int i = 0, j = 0; |
Johannes Schindelin | 3e6046e | 2021-02-05 14:46:12 +0000 | [diff] [blame] | 490 | struct diff_options opts; |
| 491 | struct strbuf indent = STRBUF_INIT; |
| 492 | |
| 493 | if (range_diff_opts->diffopt) |
| 494 | memcpy(&opts, range_diff_opts->diffopt, sizeof(opts)); |
| 495 | else |
Ævar Arnfjörð Bjarmason | 0853903 | 2023-03-28 15:58:49 +0200 | [diff] [blame] | 496 | repo_diff_setup(the_repository, &opts); |
Johannes Schindelin | 3e6046e | 2021-02-05 14:46:12 +0000 | [diff] [blame] | 497 | |
René Scharfe | 709b3f3 | 2021-09-04 09:50:58 +0200 | [diff] [blame] | 498 | opts.no_free = 1; |
Johannes Schindelin | 3e6046e | 2021-02-05 14:46:12 +0000 | [diff] [blame] | 499 | if (!opts.output_format) |
| 500 | opts.output_format = DIFF_FORMAT_PATCH; |
| 501 | opts.flags.suppress_diff_headers = 1; |
| 502 | opts.flags.dual_color_diffed_diffs = |
| 503 | range_diff_opts->dual_color; |
| 504 | opts.flags.suppress_hunk_header_line_count = 1; |
| 505 | opts.output_prefix = output_prefix_cb; |
| 506 | strbuf_addstr(&indent, " "); |
| 507 | opts.output_prefix_data = &indent; |
| 508 | diff_setup_done(&opts); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 509 | |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 510 | /* |
| 511 | * We assume the user is really more interested in the second argument |
| 512 | * ("newer" version). To that end, we print the output in the order of |
| 513 | * the RHS (the `b` parameter). To put the LHS (the `a` parameter) |
| 514 | * commits that are no longer in the RHS into a good place, we place |
| 515 | * them once we have shown all of their predecessors in the LHS. |
| 516 | */ |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 517 | |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 518 | while (i < a->nr || j < b->nr) { |
| 519 | struct patch_util *a_util, *b_util; |
| 520 | a_util = i < a->nr ? a->items[i].util : NULL; |
| 521 | b_util = j < b->nr ? b->items[j].util : NULL; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 522 | |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 523 | /* Skip all the already-shown commits from the LHS. */ |
| 524 | while (i < a->nr && a_util->shown) |
| 525 | a_util = ++i < a->nr ? a->items[i].util : NULL; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 526 | |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 527 | /* Show unmatched LHS commit whose predecessors were shown. */ |
| 528 | if (i < a->nr && a_util->matching < 0) { |
Johannes Schindelin | 1e79f97 | 2021-02-05 14:46:13 +0000 | [diff] [blame] | 529 | if (!range_diff_opts->right_only) |
| 530 | output_pair_header(&opts, patch_no_width, |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 531 | &buf, &dashes, a_util, NULL); |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 532 | i++; |
| 533 | continue; |
| 534 | } |
| 535 | |
| 536 | /* Show unmatched RHS commits. */ |
| 537 | while (j < b->nr && b_util->matching < 0) { |
Johannes Schindelin | 1e79f97 | 2021-02-05 14:46:13 +0000 | [diff] [blame] | 538 | if (!range_diff_opts->left_only) |
| 539 | output_pair_header(&opts, patch_no_width, |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 540 | &buf, &dashes, NULL, b_util); |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 541 | b_util = ++j < b->nr ? b->items[j].util : NULL; |
| 542 | } |
| 543 | |
| 544 | /* Show matching LHS/RHS pair. */ |
| 545 | if (j < b->nr) { |
| 546 | a_util = a->items[b_util->matching].util; |
Johannes Schindelin | 3e6046e | 2021-02-05 14:46:12 +0000 | [diff] [blame] | 547 | output_pair_header(&opts, patch_no_width, |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 548 | &buf, &dashes, a_util, b_util); |
Johannes Schindelin | 3e6046e | 2021-02-05 14:46:12 +0000 | [diff] [blame] | 549 | if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT)) |
Johannes Schindelin | c8c5e43 | 2018-08-13 04:33:07 -0700 | [diff] [blame] | 550 | patch_diff(a->items[b_util->matching].string, |
Johannes Schindelin | 3e6046e | 2021-02-05 14:46:12 +0000 | [diff] [blame] | 551 | b->items[j].string, &opts); |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 552 | a_util->shown = 1; |
| 553 | j++; |
| 554 | } |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 555 | } |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 556 | strbuf_release(&buf); |
| 557 | strbuf_release(&dashes); |
Johannes Schindelin | 3e6046e | 2021-02-05 14:46:12 +0000 | [diff] [blame] | 558 | strbuf_release(&indent); |
René Scharfe | 709b3f3 | 2021-09-04 09:50:58 +0200 | [diff] [blame] | 559 | opts.no_free = 0; |
| 560 | diff_free(&opts); |
Eric Sunshine | 73a834e | 2018-07-22 05:57:12 -0400 | [diff] [blame] | 561 | } |
| 562 | |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 563 | int show_range_diff(const char *range1, const char *range2, |
Johannes Schindelin | f1ce6c1 | 2021-02-05 14:46:11 +0000 | [diff] [blame] | 564 | struct range_diff_options *range_diff_opts) |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 565 | { |
| 566 | int res = 0; |
| 567 | |
| 568 | struct string_list branch1 = STRING_LIST_INIT_DUP; |
| 569 | struct string_list branch2 = STRING_LIST_INIT_DUP; |
| 570 | |
Johannes Schindelin | 1e79f97 | 2021-02-05 14:46:13 +0000 | [diff] [blame] | 571 | if (range_diff_opts->left_only && range_diff_opts->right_only) |
Jean-Noël Avila | 43ea635 | 2022-01-05 20:02:14 +0000 | [diff] [blame] | 572 | res = error(_("options '%s' and '%s' cannot be used together"), "--left-only", "--right-only"); |
Johannes Schindelin | 1e79f97 | 2021-02-05 14:46:13 +0000 | [diff] [blame] | 573 | |
| 574 | if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg)) |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 575 | res = error(_("could not parse log for '%s'"), range1); |
Johannes Schindelin | f1ce6c1 | 2021-02-05 14:46:11 +0000 | [diff] [blame] | 576 | if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg)) |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 577 | res = error(_("could not parse log for '%s'"), range2); |
| 578 | |
| 579 | if (!res) { |
| 580 | find_exact_matches(&branch1, &branch2); |
Johannes Schindelin | f1ce6c1 | 2021-02-05 14:46:11 +0000 | [diff] [blame] | 581 | get_correspondences(&branch1, &branch2, |
| 582 | range_diff_opts->creation_factor); |
Johannes Schindelin | 3e6046e | 2021-02-05 14:46:12 +0000 | [diff] [blame] | 583 | output(&branch1, &branch2, range_diff_opts); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | string_list_clear(&branch1, 1); |
| 587 | string_list_clear(&branch2, 1); |
| 588 | |
| 589 | return res; |
| 590 | } |
Johannes Schindelin | 679b591 | 2021-01-27 16:37:22 +0000 | [diff] [blame] | 591 | |
| 592 | int is_range_diff_range(const char *arg) |
| 593 | { |
Johannes Schindelin | 359f0d7 | 2021-02-05 14:44:48 +0000 | [diff] [blame] | 594 | char *copy = xstrdup(arg); /* setup_revisions() modifies it */ |
| 595 | const char *argv[] = { "", copy, "--", NULL }; |
| 596 | int i, positive = 0, negative = 0; |
| 597 | struct rev_info revs; |
| 598 | |
Ævar Arnfjörð Bjarmason | 035c7de | 2023-03-28 15:58:56 +0200 | [diff] [blame] | 599 | repo_init_revisions(the_repository, &revs, NULL); |
Johannes Schindelin | 359f0d7 | 2021-02-05 14:44:48 +0000 | [diff] [blame] | 600 | if (setup_revisions(3, argv, &revs, NULL) == 1) { |
| 601 | for (i = 0; i < revs.pending.nr; i++) |
| 602 | if (revs.pending.objects[i].item->flags & UNINTERESTING) |
| 603 | negative++; |
| 604 | else |
| 605 | positive++; |
| 606 | for (i = 0; i < revs.pending.nr; i++) { |
| 607 | struct object *obj = revs.pending.objects[i].item; |
| 608 | |
| 609 | if (obj->type == OBJ_COMMIT) |
| 610 | clear_commit_marks((struct commit *)obj, |
| 611 | ALL_REV_FLAGS); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | free(copy); |
Ævar Arnfjörð Bjarmason | 1878b5e | 2022-04-13 22:01:35 +0200 | [diff] [blame] | 616 | release_revisions(&revs); |
Johannes Schindelin | 359f0d7 | 2021-02-05 14:44:48 +0000 | [diff] [blame] | 617 | return negative > 0 && positive > 0; |
Johannes Schindelin | 679b591 | 2021-01-27 16:37:22 +0000 | [diff] [blame] | 618 | } |