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