Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "range-diff.h" |
| 3 | #include "string-list.h" |
| 4 | #include "run-command.h" |
| 5 | #include "argv-array.h" |
| 6 | #include "hashmap.h" |
| 7 | #include "xdiff-interface.h" |
| 8 | #include "linear-assignment.h" |
Johannes Schindelin | c8c5e43 | 2018-08-13 04:33:07 -0700 | [diff] [blame] | 9 | #include "diffcore.h" |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 10 | #include "commit.h" |
| 11 | #include "pretty.h" |
Johannes Schindelin | 4eba1fe | 2018-08-13 04:33:14 -0700 | [diff] [blame] | 12 | #include "userdiff.h" |
Thomas Gummerer | b66885a | 2019-07-11 17:08:49 +0100 | [diff] [blame] | 13 | #include "apply.h" |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 14 | |
| 15 | struct patch_util { |
| 16 | /* For the search for an exact match */ |
| 17 | struct hashmap_entry e; |
| 18 | const char *diff, *patch; |
| 19 | |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 20 | int i, shown; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 21 | int diffsize; |
| 22 | size_t diff_offset; |
| 23 | /* the index of the matching item in the other branch, or -1 */ |
| 24 | int matching; |
| 25 | struct object_id oid; |
| 26 | }; |
| 27 | |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 28 | static size_t find_end_of_line(char *buffer, unsigned long size) |
| 29 | { |
| 30 | char *eol = memchr(buffer, '\n', size); |
| 31 | |
| 32 | if (!eol) |
| 33 | return size; |
| 34 | |
| 35 | *eol = '\0'; |
| 36 | return eol + 1 - buffer; |
| 37 | } |
| 38 | |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 39 | /* |
| 40 | * Reads the patches into a string list, with the `util` field being populated |
| 41 | * as struct object_id (will need to be free()d). |
| 42 | */ |
| 43 | static int read_patches(const char *range, struct string_list *list) |
| 44 | { |
| 45 | struct child_process cp = CHILD_PROCESS_INIT; |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 46 | struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 47 | struct patch_util *util = NULL; |
| 48 | int in_header = 1; |
Thomas Gummerer | 444e096 | 2019-07-11 17:08:50 +0100 | [diff] [blame] | 49 | char *line, *current_filename = NULL; |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 50 | int offset, len; |
| 51 | size_t size; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 52 | |
| 53 | argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges", |
| 54 | "--reverse", "--date-order", "--decorate=no", |
Stefan Beller | 8d5ccb5 | 2018-08-17 13:43:53 -0700 | [diff] [blame] | 55 | /* |
| 56 | * Choose indicators that are not used anywhere |
| 57 | * else in diffs, but still look reasonable |
| 58 | * (e.g. will not be confusing when debugging) |
| 59 | */ |
| 60 | "--output-indicator-new=>", |
| 61 | "--output-indicator-old=<", |
| 62 | "--output-indicator-context=#", |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 63 | "--no-abbrev-commit", range, |
| 64 | NULL); |
| 65 | cp.out = -1; |
| 66 | cp.no_stdin = 1; |
| 67 | cp.git_cmd = 1; |
| 68 | |
| 69 | if (start_command(&cp)) |
| 70 | return error_errno(_("could not start `log`")); |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 71 | if (strbuf_read(&contents, cp.out, 0) < 0) { |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 72 | error_errno(_("could not read `log` output")); |
| 73 | finish_command(&cp); |
| 74 | return -1; |
| 75 | } |
| 76 | |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 77 | line = contents.buf; |
| 78 | size = contents.len; |
| 79 | for (offset = 0; size > 0; offset += len, size -= len, line += len) { |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 80 | const char *p; |
| 81 | |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 82 | len = find_end_of_line(line, size); |
| 83 | line[len - 1] = '\0'; |
| 84 | if (skip_prefix(line, "commit ", &p)) { |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 85 | if (util) { |
| 86 | string_list_append(list, buf.buf)->util = util; |
| 87 | strbuf_reset(&buf); |
| 88 | } |
| 89 | util = xcalloc(sizeof(*util), 1); |
| 90 | if (get_oid(p, &util->oid)) { |
| 91 | error(_("could not parse commit '%s'"), p); |
| 92 | free(util); |
| 93 | string_list_clear(list, 1); |
| 94 | strbuf_release(&buf); |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 95 | strbuf_release(&contents); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 96 | finish_command(&cp); |
| 97 | return -1; |
| 98 | } |
| 99 | util->matching = -1; |
| 100 | in_header = 1; |
| 101 | continue; |
| 102 | } |
| 103 | |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 104 | if (starts_with(line, "diff --git")) { |
Thomas Gummerer | b66885a | 2019-07-11 17:08:49 +0100 | [diff] [blame] | 105 | struct patch patch = { 0 }; |
| 106 | struct strbuf root = STRBUF_INIT; |
| 107 | int linenr = 0; |
| 108 | |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 109 | in_header = 0; |
| 110 | strbuf_addch(&buf, '\n'); |
| 111 | if (!util->diff_offset) |
| 112 | util->diff_offset = buf.len; |
Thomas Gummerer | b66885a | 2019-07-11 17:08:49 +0100 | [diff] [blame] | 113 | line[len - 1] = '\n'; |
| 114 | len = parse_git_diff_header(&root, &linenr, 1, line, |
| 115 | len, size, &patch); |
| 116 | if (len < 0) |
| 117 | die(_("could not parse git header '%.*s'"), (int)len, line); |
| 118 | strbuf_addstr(&buf, " ## "); |
| 119 | if (patch.is_new > 0) |
| 120 | strbuf_addf(&buf, "%s (new)", patch.new_name); |
| 121 | else if (patch.is_delete > 0) |
| 122 | strbuf_addf(&buf, "%s (deleted)", patch.old_name); |
| 123 | else if (patch.is_rename) |
| 124 | strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name); |
| 125 | else |
| 126 | strbuf_addstr(&buf, patch.new_name); |
| 127 | |
Thomas Gummerer | 444e096 | 2019-07-11 17:08:50 +0100 | [diff] [blame] | 128 | free(current_filename); |
| 129 | if (patch.is_delete > 0) |
| 130 | current_filename = xstrdup(patch.old_name); |
| 131 | else |
| 132 | current_filename = xstrdup(patch.new_name); |
| 133 | |
Thomas Gummerer | b66885a | 2019-07-11 17:08:49 +0100 | [diff] [blame] | 134 | if (patch.new_mode && patch.old_mode && |
| 135 | patch.old_mode != patch.new_mode) |
| 136 | strbuf_addf(&buf, " (mode change %06o => %06o)", |
| 137 | patch.old_mode, patch.new_mode); |
| 138 | |
| 139 | strbuf_addstr(&buf, " ##"); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 140 | } else if (in_header) { |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 141 | if (starts_with(line, "Author: ")) { |
Thomas Gummerer | 499352c | 2019-07-11 17:08:51 +0100 | [diff] [blame] | 142 | strbuf_addstr(&buf, " ## Metadata ##\n"); |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 143 | strbuf_addstr(&buf, line); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 144 | strbuf_addstr(&buf, "\n\n"); |
Thomas Gummerer | 499352c | 2019-07-11 17:08:51 +0100 | [diff] [blame] | 145 | strbuf_addstr(&buf, " ## Commit message ##\n"); |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 146 | } else if (starts_with(line, " ")) { |
| 147 | p = line + len - 2; |
| 148 | while (isspace(*p) && p >= line) |
| 149 | p--; |
| 150 | strbuf_add(&buf, line, p - line + 1); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 151 | strbuf_addch(&buf, '\n'); |
| 152 | } |
| 153 | continue; |
Thomas Gummerer | e1db263 | 2019-07-11 17:08:47 +0100 | [diff] [blame] | 154 | } else if (skip_prefix(line, "@@ ", &p)) { |
| 155 | p = strstr(p, "@@"); |
Thomas Gummerer | 444e096 | 2019-07-11 17:08:50 +0100 | [diff] [blame] | 156 | strbuf_addstr(&buf, "@@"); |
| 157 | if (current_filename && p[2]) |
| 158 | strbuf_addf(&buf, " %s:", current_filename); |
| 159 | if (p) |
| 160 | strbuf_addstr(&buf, p + 2); |
Thomas Gummerer | b66885a | 2019-07-11 17:08:49 +0100 | [diff] [blame] | 161 | } else if (!line[0]) |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 162 | /* |
| 163 | * A completely blank (not ' \n', which is context) |
| 164 | * line is not valid in a diff. We skip it |
| 165 | * silently, because this neatly handles the blank |
| 166 | * separator line between commits in git-log |
| 167 | * output. |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 168 | */ |
| 169 | continue; |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 170 | else if (line[0] == '>') { |
Stefan Beller | 8d5ccb5 | 2018-08-17 13:43:53 -0700 | [diff] [blame] | 171 | strbuf_addch(&buf, '+'); |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 172 | strbuf_addstr(&buf, line + 1); |
| 173 | } else if (line[0] == '<') { |
Stefan Beller | 8d5ccb5 | 2018-08-17 13:43:53 -0700 | [diff] [blame] | 174 | strbuf_addch(&buf, '-'); |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 175 | strbuf_addstr(&buf, line + 1); |
| 176 | } else if (line[0] == '#') { |
Stefan Beller | 8d5ccb5 | 2018-08-17 13:43:53 -0700 | [diff] [blame] | 177 | strbuf_addch(&buf, ' '); |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 178 | strbuf_addstr(&buf, line + 1); |
Stefan Beller | 8d5ccb5 | 2018-08-17 13:43:53 -0700 | [diff] [blame] | 179 | } else { |
Stefan Beller | 2543a64 | 2018-08-17 13:43:54 -0700 | [diff] [blame] | 180 | strbuf_addch(&buf, ' '); |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 181 | strbuf_addstr(&buf, line); |
Stefan Beller | 8d5ccb5 | 2018-08-17 13:43:53 -0700 | [diff] [blame] | 182 | } |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 183 | |
| 184 | strbuf_addch(&buf, '\n'); |
| 185 | util->diffsize++; |
| 186 | } |
Thomas Gummerer | 44b67cb | 2019-07-11 17:08:46 +0100 | [diff] [blame] | 187 | strbuf_release(&contents); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 188 | |
| 189 | if (util) |
| 190 | string_list_append(list, buf.buf)->util = util; |
| 191 | strbuf_release(&buf); |
Thomas Gummerer | 444e096 | 2019-07-11 17:08:50 +0100 | [diff] [blame] | 192 | free(current_filename); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 193 | |
| 194 | if (finish_command(&cp)) |
| 195 | return -1; |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static int patch_util_cmp(const void *dummy, const struct patch_util *a, |
Thomas Gummerer | 1ca6922 | 2019-07-11 17:08:45 +0100 | [diff] [blame] | 201 | const struct patch_util *b, const char *keydata) |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 202 | { |
| 203 | return strcmp(a->diff, keydata ? keydata : b->diff); |
| 204 | } |
| 205 | |
| 206 | static void find_exact_matches(struct string_list *a, struct string_list *b) |
| 207 | { |
| 208 | struct hashmap map; |
| 209 | int i; |
| 210 | |
| 211 | hashmap_init(&map, (hashmap_cmp_fn)patch_util_cmp, NULL, 0); |
| 212 | |
| 213 | /* First, add the patches of a to a hash map */ |
| 214 | for (i = 0; i < a->nr; i++) { |
| 215 | struct patch_util *util = a->items[i].util; |
| 216 | |
| 217 | util->i = i; |
| 218 | util->patch = a->items[i].string; |
| 219 | util->diff = util->patch + util->diff_offset; |
| 220 | hashmap_entry_init(util, strhash(util->diff)); |
| 221 | hashmap_add(&map, util); |
| 222 | } |
| 223 | |
| 224 | /* Now try to find exact matches in b */ |
| 225 | for (i = 0; i < b->nr; i++) { |
| 226 | struct patch_util *util = b->items[i].util, *other; |
| 227 | |
| 228 | util->i = i; |
| 229 | util->patch = b->items[i].string; |
| 230 | util->diff = util->patch + util->diff_offset; |
| 231 | hashmap_entry_init(util, strhash(util->diff)); |
| 232 | other = hashmap_remove(&map, util, NULL); |
| 233 | if (other) { |
| 234 | if (other->matching >= 0) |
| 235 | BUG("already assigned!"); |
| 236 | |
| 237 | other->matching = i; |
| 238 | util->matching = other->i; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | hashmap_free(&map, 0); |
| 243 | } |
| 244 | |
| 245 | static void diffsize_consume(void *data, char *line, unsigned long len) |
| 246 | { |
| 247 | (*(int *)data)++; |
| 248 | } |
| 249 | |
Jeff King | d2eb809 | 2018-11-02 02:39:40 -0400 | [diff] [blame] | 250 | static void diffsize_hunk(void *data, long ob, long on, long nb, long nn, |
| 251 | const char *funcline, long funclen) |
| 252 | { |
| 253 | diffsize_consume(data, NULL, 0); |
| 254 | } |
| 255 | |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 256 | static int diffsize(const char *a, const char *b) |
| 257 | { |
| 258 | xpparam_t pp = { 0 }; |
| 259 | xdemitconf_t cfg = { 0 }; |
| 260 | mmfile_t mf1, mf2; |
| 261 | int count = 0; |
| 262 | |
| 263 | mf1.ptr = (char *)a; |
| 264 | mf1.size = strlen(a); |
| 265 | mf2.ptr = (char *)b; |
| 266 | mf2.size = strlen(b); |
| 267 | |
| 268 | cfg.ctxlen = 3; |
Jeff King | d2eb809 | 2018-11-02 02:39:40 -0400 | [diff] [blame] | 269 | if (!xdi_diff_outf(&mf1, &mf2, |
| 270 | diffsize_hunk, diffsize_consume, &count, |
| 271 | &pp, &cfg)) |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 272 | return count; |
| 273 | |
| 274 | error(_("failed to generate diff")); |
| 275 | return COST_MAX; |
| 276 | } |
| 277 | |
| 278 | static void get_correspondences(struct string_list *a, struct string_list *b, |
| 279 | int creation_factor) |
| 280 | { |
| 281 | int n = a->nr + b->nr; |
| 282 | int *cost, c, *a2b, *b2a; |
| 283 | int i, j; |
| 284 | |
| 285 | ALLOC_ARRAY(cost, st_mult(n, n)); |
| 286 | ALLOC_ARRAY(a2b, n); |
| 287 | ALLOC_ARRAY(b2a, n); |
| 288 | |
| 289 | for (i = 0; i < a->nr; i++) { |
| 290 | struct patch_util *a_util = a->items[i].util; |
| 291 | |
| 292 | for (j = 0; j < b->nr; j++) { |
| 293 | struct patch_util *b_util = b->items[j].util; |
| 294 | |
| 295 | if (a_util->matching == j) |
| 296 | c = 0; |
| 297 | else if (a_util->matching < 0 && b_util->matching < 0) |
| 298 | c = diffsize(a_util->diff, b_util->diff); |
| 299 | else |
| 300 | c = COST_MAX; |
| 301 | cost[i + n * j] = c; |
| 302 | } |
| 303 | |
| 304 | c = a_util->matching < 0 ? |
| 305 | a_util->diffsize * creation_factor / 100 : COST_MAX; |
| 306 | for (j = b->nr; j < n; j++) |
| 307 | cost[i + n * j] = c; |
| 308 | } |
| 309 | |
| 310 | for (j = 0; j < b->nr; j++) { |
| 311 | struct patch_util *util = b->items[j].util; |
| 312 | |
| 313 | c = util->matching < 0 ? |
| 314 | util->diffsize * creation_factor / 100 : COST_MAX; |
| 315 | for (i = a->nr; i < n; i++) |
| 316 | cost[i + n * j] = c; |
| 317 | } |
| 318 | |
| 319 | for (i = a->nr; i < n; i++) |
| 320 | for (j = b->nr; j < n; j++) |
| 321 | cost[i + n * j] = 0; |
| 322 | |
| 323 | compute_assignment(n, n, cost, a2b, b2a); |
| 324 | |
| 325 | for (i = 0; i < a->nr; i++) |
| 326 | if (a2b[i] >= 0 && a2b[i] < b->nr) { |
| 327 | struct patch_util *a_util = a->items[i].util; |
| 328 | struct patch_util *b_util = b->items[a2b[i]].util; |
| 329 | |
| 330 | a_util->matching = a2b[i]; |
| 331 | b_util->matching = i; |
| 332 | } |
| 333 | |
| 334 | free(cost); |
| 335 | free(a2b); |
| 336 | free(b2a); |
| 337 | } |
| 338 | |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 339 | static void output_pair_header(struct diff_options *diffopt, |
Johannes Schindelin | d1f87a2 | 2018-08-13 04:33:28 -0700 | [diff] [blame] | 340 | int patch_no_width, |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 341 | struct strbuf *buf, |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 342 | struct strbuf *dashes, |
| 343 | struct patch_util *a_util, |
| 344 | struct patch_util *b_util) |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 345 | { |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 346 | struct object_id *oid = a_util ? &a_util->oid : &b_util->oid; |
| 347 | struct commit *commit; |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 348 | char status; |
| 349 | const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET); |
| 350 | const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD); |
| 351 | const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW); |
| 352 | const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT); |
| 353 | const char *color; |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 354 | |
| 355 | if (!dashes->len) |
| 356 | strbuf_addchars(dashes, '-', |
| 357 | strlen(find_unique_abbrev(oid, |
| 358 | DEFAULT_ABBREV))); |
| 359 | |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 360 | if (!b_util) { |
| 361 | color = color_old; |
| 362 | status = '<'; |
| 363 | } else if (!a_util) { |
| 364 | color = color_new; |
| 365 | status = '>'; |
| 366 | } else if (strcmp(a_util->patch, b_util->patch)) { |
| 367 | color = color_commit; |
| 368 | status = '!'; |
| 369 | } else { |
| 370 | color = color_commit; |
| 371 | status = '='; |
| 372 | } |
| 373 | |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 374 | strbuf_reset(buf); |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 375 | strbuf_addstr(buf, status == '!' ? color_old : color); |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 376 | if (!a_util) |
Johannes Schindelin | d1f87a2 | 2018-08-13 04:33:28 -0700 | [diff] [blame] | 377 | strbuf_addf(buf, "%*s: %s ", patch_no_width, "-", dashes->buf); |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 378 | else |
Johannes Schindelin | d1f87a2 | 2018-08-13 04:33:28 -0700 | [diff] [blame] | 379 | strbuf_addf(buf, "%*d: %s ", patch_no_width, a_util->i + 1, |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 380 | find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV)); |
| 381 | |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 382 | if (status == '!') |
| 383 | strbuf_addf(buf, "%s%s", color_reset, color); |
| 384 | strbuf_addch(buf, status); |
| 385 | if (status == '!') |
| 386 | strbuf_addf(buf, "%s%s", color_reset, color_new); |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 387 | |
| 388 | if (!b_util) |
Johannes Schindelin | d1f87a2 | 2018-08-13 04:33:28 -0700 | [diff] [blame] | 389 | strbuf_addf(buf, " %*s: %s", patch_no_width, "-", dashes->buf); |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 390 | else |
Johannes Schindelin | d1f87a2 | 2018-08-13 04:33:28 -0700 | [diff] [blame] | 391 | strbuf_addf(buf, " %*d: %s", patch_no_width, b_util->i + 1, |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 392 | find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV)); |
| 393 | |
| 394 | commit = lookup_commit_reference(the_repository, oid); |
| 395 | if (commit) { |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 396 | if (status == '!') |
| 397 | strbuf_addf(buf, "%s%s", color_reset, color); |
| 398 | |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 399 | strbuf_addch(buf, ' '); |
| 400 | pp_commit_easy(CMIT_FMT_ONELINE, commit, buf); |
| 401 | } |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 402 | strbuf_addf(buf, "%s\n", color_reset); |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 403 | |
Eric Sunshine | 87f1b2d | 2018-07-22 05:57:10 -0400 | [diff] [blame] | 404 | fwrite(buf->buf, buf->len, 1, diffopt->file); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Thomas Gummerer | 499352c | 2019-07-11 17:08:51 +0100 | [diff] [blame] | 407 | static struct userdiff_driver section_headers = { |
| 408 | .funcname = { "^ ## (.*) ##$\n" |
| 409 | "^.?@@ (.*)$", REG_EXTENDED } |
Johannes Schindelin | 4eba1fe | 2018-08-13 04:33:14 -0700 | [diff] [blame] | 410 | }; |
| 411 | |
Johannes Schindelin | c8c5e43 | 2018-08-13 04:33:07 -0700 | [diff] [blame] | 412 | static struct diff_filespec *get_filespec(const char *name, const char *p) |
| 413 | { |
| 414 | struct diff_filespec *spec = alloc_filespec(name); |
| 415 | |
Lucas De Marchi | 0e573e8 | 2018-10-24 12:46:17 -0700 | [diff] [blame] | 416 | fill_filespec(spec, &null_oid, 0, 0100644); |
Johannes Schindelin | c8c5e43 | 2018-08-13 04:33:07 -0700 | [diff] [blame] | 417 | spec->data = (char *)p; |
| 418 | spec->size = strlen(p); |
| 419 | spec->should_munmap = 0; |
| 420 | spec->is_stdin = 1; |
Thomas Gummerer | 499352c | 2019-07-11 17:08:51 +0100 | [diff] [blame] | 421 | spec->driver = §ion_headers; |
Johannes Schindelin | c8c5e43 | 2018-08-13 04:33:07 -0700 | [diff] [blame] | 422 | |
| 423 | return spec; |
| 424 | } |
| 425 | |
| 426 | static void patch_diff(const char *a, const char *b, |
Thomas Gummerer | 1ca6922 | 2019-07-11 17:08:45 +0100 | [diff] [blame] | 427 | struct diff_options *diffopt) |
Johannes Schindelin | c8c5e43 | 2018-08-13 04:33:07 -0700 | [diff] [blame] | 428 | { |
| 429 | diff_queue(&diff_queued_diff, |
| 430 | get_filespec("a", a), get_filespec("b", b)); |
| 431 | |
| 432 | diffcore_std(diffopt); |
| 433 | diff_flush(diffopt); |
| 434 | } |
| 435 | |
| 436 | static void output(struct string_list *a, struct string_list *b, |
| 437 | struct diff_options *diffopt) |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 438 | { |
Johannes Schindelin | eb0be38 | 2018-08-13 04:33:13 -0700 | [diff] [blame] | 439 | struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT; |
Johannes Schindelin | d1f87a2 | 2018-08-13 04:33:28 -0700 | [diff] [blame] | 440 | 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] | 441 | int i = 0, j = 0; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 442 | |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 443 | /* |
| 444 | * We assume the user is really more interested in the second argument |
| 445 | * ("newer" version). To that end, we print the output in the order of |
| 446 | * the RHS (the `b` parameter). To put the LHS (the `a` parameter) |
| 447 | * commits that are no longer in the RHS into a good place, we place |
| 448 | * them once we have shown all of their predecessors in the LHS. |
| 449 | */ |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 450 | |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 451 | while (i < a->nr || j < b->nr) { |
| 452 | struct patch_util *a_util, *b_util; |
| 453 | a_util = i < a->nr ? a->items[i].util : NULL; |
| 454 | b_util = j < b->nr ? b->items[j].util : NULL; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 455 | |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 456 | /* Skip all the already-shown commits from the LHS. */ |
| 457 | while (i < a->nr && a_util->shown) |
| 458 | a_util = ++i < a->nr ? a->items[i].util : NULL; |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 459 | |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 460 | /* Show unmatched LHS commit whose predecessors were shown. */ |
| 461 | if (i < a->nr && a_util->matching < 0) { |
Johannes Schindelin | d1f87a2 | 2018-08-13 04:33:28 -0700 | [diff] [blame] | 462 | output_pair_header(diffopt, patch_no_width, |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 463 | &buf, &dashes, a_util, NULL); |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 464 | i++; |
| 465 | continue; |
| 466 | } |
| 467 | |
| 468 | /* Show unmatched RHS commits. */ |
| 469 | while (j < b->nr && b_util->matching < 0) { |
Johannes Schindelin | d1f87a2 | 2018-08-13 04:33:28 -0700 | [diff] [blame] | 470 | output_pair_header(diffopt, patch_no_width, |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 471 | &buf, &dashes, NULL, b_util); |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 472 | b_util = ++j < b->nr ? b->items[j].util : NULL; |
| 473 | } |
| 474 | |
| 475 | /* Show matching LHS/RHS pair. */ |
| 476 | if (j < b->nr) { |
| 477 | a_util = a->items[b_util->matching].util; |
Johannes Schindelin | d1f87a2 | 2018-08-13 04:33:28 -0700 | [diff] [blame] | 478 | output_pair_header(diffopt, patch_no_width, |
Johannes Schindelin | faa1df8 | 2018-08-13 04:33:18 -0700 | [diff] [blame] | 479 | &buf, &dashes, a_util, b_util); |
Johannes Schindelin | c8c5e43 | 2018-08-13 04:33:07 -0700 | [diff] [blame] | 480 | if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT)) |
| 481 | patch_diff(a->items[b_util->matching].string, |
| 482 | b->items[j].string, diffopt); |
Johannes Schindelin | 9dc46e0 | 2018-08-13 04:33:05 -0700 | [diff] [blame] | 483 | a_util->shown = 1; |
| 484 | j++; |
| 485 | } |
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 | strbuf_release(&buf); |
| 488 | strbuf_release(&dashes); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Eric Sunshine | 73a834e | 2018-07-22 05:57:12 -0400 | [diff] [blame] | 491 | static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data) |
| 492 | { |
| 493 | return data; |
| 494 | } |
| 495 | |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 496 | int show_range_diff(const char *range1, const char *range2, |
Eric Sunshine | 73a834e | 2018-07-22 05:57:12 -0400 | [diff] [blame] | 497 | int creation_factor, int dual_color, |
| 498 | struct diff_options *diffopt) |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 499 | { |
| 500 | int res = 0; |
| 501 | |
| 502 | struct string_list branch1 = STRING_LIST_INIT_DUP; |
| 503 | struct string_list branch2 = STRING_LIST_INIT_DUP; |
| 504 | |
| 505 | if (read_patches(range1, &branch1)) |
| 506 | res = error(_("could not parse log for '%s'"), range1); |
| 507 | if (!res && read_patches(range2, &branch2)) |
| 508 | res = error(_("could not parse log for '%s'"), range2); |
| 509 | |
| 510 | if (!res) { |
Eric Sunshine | 73a834e | 2018-07-22 05:57:12 -0400 | [diff] [blame] | 511 | struct diff_options opts; |
| 512 | struct strbuf indent = STRBUF_INIT; |
| 513 | |
Junio C Hamano | d8981c3 | 2018-11-30 13:27:11 +0900 | [diff] [blame] | 514 | if (diffopt) |
| 515 | memcpy(&opts, diffopt, sizeof(opts)); |
| 516 | else |
| 517 | diff_setup(&opts); |
| 518 | |
Ævar Arnfjörð Bjarmason | a48e12e | 2018-11-13 18:55:58 +0000 | [diff] [blame] | 519 | if (!opts.output_format) |
| 520 | opts.output_format = DIFF_FORMAT_PATCH; |
Eric Sunshine | 73a834e | 2018-07-22 05:57:12 -0400 | [diff] [blame] | 521 | opts.flags.suppress_diff_headers = 1; |
| 522 | opts.flags.dual_color_diffed_diffs = dual_color; |
Thomas Gummerer | 430be36 | 2019-07-11 17:08:48 +0100 | [diff] [blame] | 523 | opts.flags.suppress_hunk_header_line_count = 1; |
Eric Sunshine | 73a834e | 2018-07-22 05:57:12 -0400 | [diff] [blame] | 524 | opts.output_prefix = output_prefix_cb; |
| 525 | strbuf_addstr(&indent, " "); |
| 526 | opts.output_prefix_data = &indent; |
| 527 | diff_setup_done(&opts); |
| 528 | |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 529 | find_exact_matches(&branch1, &branch2); |
| 530 | get_correspondences(&branch1, &branch2, creation_factor); |
Eric Sunshine | 73a834e | 2018-07-22 05:57:12 -0400 | [diff] [blame] | 531 | output(&branch1, &branch2, &opts); |
| 532 | |
| 533 | strbuf_release(&indent); |
Johannes Schindelin | d9c66f0 | 2018-08-13 04:33:04 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | string_list_clear(&branch1, 1); |
| 537 | string_list_clear(&branch2, 1); |
| 538 | |
| 539 | return res; |
| 540 | } |