range-diff.con commit range-diff: use a hunk callback (d2eb809)
   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"
   9#include "diffcore.h"
  10#include "commit.h"
  11#include "pretty.h"
  12#include "userdiff.h"
  13
  14struct patch_util {
  15        /* For the search for an exact match */
  16        struct hashmap_entry e;
  17        const char *diff, *patch;
  18
  19        int i, shown;
  20        int diffsize;
  21        size_t diff_offset;
  22        /* the index of the matching item in the other branch, or -1 */
  23        int matching;
  24        struct object_id oid;
  25};
  26
  27/*
  28 * Reads the patches into a string list, with the `util` field being populated
  29 * as struct object_id (will need to be free()d).
  30 */
  31static int read_patches(const char *range, struct string_list *list)
  32{
  33        struct child_process cp = CHILD_PROCESS_INIT;
  34        FILE *in;
  35        struct strbuf buf = STRBUF_INIT, line = STRBUF_INIT;
  36        struct patch_util *util = NULL;
  37        int in_header = 1;
  38
  39        argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
  40                        "--reverse", "--date-order", "--decorate=no",
  41                        "--no-abbrev-commit", range,
  42                        NULL);
  43        cp.out = -1;
  44        cp.no_stdin = 1;
  45        cp.git_cmd = 1;
  46
  47        if (start_command(&cp))
  48                return error_errno(_("could not start `log`"));
  49        in = fdopen(cp.out, "r");
  50        if (!in) {
  51                error_errno(_("could not read `log` output"));
  52                finish_command(&cp);
  53                return -1;
  54        }
  55
  56        while (strbuf_getline(&line, in) != EOF) {
  57                const char *p;
  58
  59                if (skip_prefix(line.buf, "commit ", &p)) {
  60                        if (util) {
  61                                string_list_append(list, buf.buf)->util = util;
  62                                strbuf_reset(&buf);
  63                        }
  64                        util = xcalloc(sizeof(*util), 1);
  65                        if (get_oid(p, &util->oid)) {
  66                                error(_("could not parse commit '%s'"), p);
  67                                free(util);
  68                                string_list_clear(list, 1);
  69                                strbuf_release(&buf);
  70                                strbuf_release(&line);
  71                                fclose(in);
  72                                finish_command(&cp);
  73                                return -1;
  74                        }
  75                        util->matching = -1;
  76                        in_header = 1;
  77                        continue;
  78                }
  79
  80                if (starts_with(line.buf, "diff --git")) {
  81                        in_header = 0;
  82                        strbuf_addch(&buf, '\n');
  83                        if (!util->diff_offset)
  84                                util->diff_offset = buf.len;
  85                        strbuf_addbuf(&buf, &line);
  86                } else if (in_header) {
  87                        if (starts_with(line.buf, "Author: ")) {
  88                                strbuf_addbuf(&buf, &line);
  89                                strbuf_addstr(&buf, "\n\n");
  90                        } else if (starts_with(line.buf, "    ")) {
  91                                strbuf_rtrim(&line);
  92                                strbuf_addbuf(&buf, &line);
  93                                strbuf_addch(&buf, '\n');
  94                        }
  95                        continue;
  96                } else if (starts_with(line.buf, "@@ "))
  97                        strbuf_addstr(&buf, "@@");
  98                else if (!line.buf[0] || starts_with(line.buf, "index "))
  99                        /*
 100                         * A completely blank (not ' \n', which is context)
 101                         * line is not valid in a diff.  We skip it
 102                         * silently, because this neatly handles the blank
 103                         * separator line between commits in git-log
 104                         * output.
 105                         *
 106                         * We also want to ignore the diff's `index` lines
 107                         * because they contain exact blob hashes in which
 108                         * we are not interested.
 109                         */
 110                        continue;
 111                else
 112                        strbuf_addbuf(&buf, &line);
 113
 114                strbuf_addch(&buf, '\n');
 115                util->diffsize++;
 116        }
 117        fclose(in);
 118        strbuf_release(&line);
 119
 120        if (util)
 121                string_list_append(list, buf.buf)->util = util;
 122        strbuf_release(&buf);
 123
 124        if (finish_command(&cp))
 125                return -1;
 126
 127        return 0;
 128}
 129
 130static int patch_util_cmp(const void *dummy, const struct patch_util *a,
 131                     const struct patch_util *b, const char *keydata)
 132{
 133        return strcmp(a->diff, keydata ? keydata : b->diff);
 134}
 135
 136static void find_exact_matches(struct string_list *a, struct string_list *b)
 137{
 138        struct hashmap map;
 139        int i;
 140
 141        hashmap_init(&map, (hashmap_cmp_fn)patch_util_cmp, NULL, 0);
 142
 143        /* First, add the patches of a to a hash map */
 144        for (i = 0; i < a->nr; i++) {
 145                struct patch_util *util = a->items[i].util;
 146
 147                util->i = i;
 148                util->patch = a->items[i].string;
 149                util->diff = util->patch + util->diff_offset;
 150                hashmap_entry_init(util, strhash(util->diff));
 151                hashmap_add(&map, util);
 152        }
 153
 154        /* Now try to find exact matches in b */
 155        for (i = 0; i < b->nr; i++) {
 156                struct patch_util *util = b->items[i].util, *other;
 157
 158                util->i = i;
 159                util->patch = b->items[i].string;
 160                util->diff = util->patch + util->diff_offset;
 161                hashmap_entry_init(util, strhash(util->diff));
 162                other = hashmap_remove(&map, util, NULL);
 163                if (other) {
 164                        if (other->matching >= 0)
 165                                BUG("already assigned!");
 166
 167                        other->matching = i;
 168                        util->matching = other->i;
 169                }
 170        }
 171
 172        hashmap_free(&map, 0);
 173}
 174
 175static void diffsize_consume(void *data, char *line, unsigned long len)
 176{
 177        (*(int *)data)++;
 178}
 179
 180static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
 181                          const char *funcline, long funclen)
 182{
 183        diffsize_consume(data, NULL, 0);
 184}
 185
 186static int diffsize(const char *a, const char *b)
 187{
 188        xpparam_t pp = { 0 };
 189        xdemitconf_t cfg = { 0 };
 190        mmfile_t mf1, mf2;
 191        int count = 0;
 192
 193        mf1.ptr = (char *)a;
 194        mf1.size = strlen(a);
 195        mf2.ptr = (char *)b;
 196        mf2.size = strlen(b);
 197
 198        cfg.ctxlen = 3;
 199        if (!xdi_diff_outf(&mf1, &mf2,
 200                           diffsize_hunk, diffsize_consume, &count,
 201                           &pp, &cfg))
 202                return count;
 203
 204        error(_("failed to generate diff"));
 205        return COST_MAX;
 206}
 207
 208static void get_correspondences(struct string_list *a, struct string_list *b,
 209                                int creation_factor)
 210{
 211        int n = a->nr + b->nr;
 212        int *cost, c, *a2b, *b2a;
 213        int i, j;
 214
 215        ALLOC_ARRAY(cost, st_mult(n, n));
 216        ALLOC_ARRAY(a2b, n);
 217        ALLOC_ARRAY(b2a, n);
 218
 219        for (i = 0; i < a->nr; i++) {
 220                struct patch_util *a_util = a->items[i].util;
 221
 222                for (j = 0; j < b->nr; j++) {
 223                        struct patch_util *b_util = b->items[j].util;
 224
 225                        if (a_util->matching == j)
 226                                c = 0;
 227                        else if (a_util->matching < 0 && b_util->matching < 0)
 228                                c = diffsize(a_util->diff, b_util->diff);
 229                        else
 230                                c = COST_MAX;
 231                        cost[i + n * j] = c;
 232                }
 233
 234                c = a_util->matching < 0 ?
 235                        a_util->diffsize * creation_factor / 100 : COST_MAX;
 236                for (j = b->nr; j < n; j++)
 237                        cost[i + n * j] = c;
 238        }
 239
 240        for (j = 0; j < b->nr; j++) {
 241                struct patch_util *util = b->items[j].util;
 242
 243                c = util->matching < 0 ?
 244                        util->diffsize * creation_factor / 100 : COST_MAX;
 245                for (i = a->nr; i < n; i++)
 246                        cost[i + n * j] = c;
 247        }
 248
 249        for (i = a->nr; i < n; i++)
 250                for (j = b->nr; j < n; j++)
 251                        cost[i + n * j] = 0;
 252
 253        compute_assignment(n, n, cost, a2b, b2a);
 254
 255        for (i = 0; i < a->nr; i++)
 256                if (a2b[i] >= 0 && a2b[i] < b->nr) {
 257                        struct patch_util *a_util = a->items[i].util;
 258                        struct patch_util *b_util = b->items[a2b[i]].util;
 259
 260                        a_util->matching = a2b[i];
 261                        b_util->matching = i;
 262                }
 263
 264        free(cost);
 265        free(a2b);
 266        free(b2a);
 267}
 268
 269static void output_pair_header(struct diff_options *diffopt,
 270                               int patch_no_width,
 271                               struct strbuf *buf,
 272                               struct strbuf *dashes,
 273                               struct patch_util *a_util,
 274                               struct patch_util *b_util)
 275{
 276        struct object_id *oid = a_util ? &a_util->oid : &b_util->oid;
 277        struct commit *commit;
 278        char status;
 279        const char *color_reset = diff_get_color_opt(diffopt, DIFF_RESET);
 280        const char *color_old = diff_get_color_opt(diffopt, DIFF_FILE_OLD);
 281        const char *color_new = diff_get_color_opt(diffopt, DIFF_FILE_NEW);
 282        const char *color_commit = diff_get_color_opt(diffopt, DIFF_COMMIT);
 283        const char *color;
 284
 285        if (!dashes->len)
 286                strbuf_addchars(dashes, '-',
 287                                strlen(find_unique_abbrev(oid,
 288                                                          DEFAULT_ABBREV)));
 289
 290        if (!b_util) {
 291                color = color_old;
 292                status = '<';
 293        } else if (!a_util) {
 294                color = color_new;
 295                status = '>';
 296        } else if (strcmp(a_util->patch, b_util->patch)) {
 297                color = color_commit;
 298                status = '!';
 299        } else {
 300                color = color_commit;
 301                status = '=';
 302        }
 303
 304        strbuf_reset(buf);
 305        strbuf_addstr(buf, status == '!' ? color_old : color);
 306        if (!a_util)
 307                strbuf_addf(buf, "%*s:  %s ", patch_no_width, "-", dashes->buf);
 308        else
 309                strbuf_addf(buf, "%*d:  %s ", patch_no_width, a_util->i + 1,
 310                            find_unique_abbrev(&a_util->oid, DEFAULT_ABBREV));
 311
 312        if (status == '!')
 313                strbuf_addf(buf, "%s%s", color_reset, color);
 314        strbuf_addch(buf, status);
 315        if (status == '!')
 316                strbuf_addf(buf, "%s%s", color_reset, color_new);
 317
 318        if (!b_util)
 319                strbuf_addf(buf, " %*s:  %s", patch_no_width, "-", dashes->buf);
 320        else
 321                strbuf_addf(buf, " %*d:  %s", patch_no_width, b_util->i + 1,
 322                            find_unique_abbrev(&b_util->oid, DEFAULT_ABBREV));
 323
 324        commit = lookup_commit_reference(the_repository, oid);
 325        if (commit) {
 326                if (status == '!')
 327                        strbuf_addf(buf, "%s%s", color_reset, color);
 328
 329                strbuf_addch(buf, ' ');
 330                pp_commit_easy(CMIT_FMT_ONELINE, commit, buf);
 331        }
 332        strbuf_addf(buf, "%s\n", color_reset);
 333
 334        fwrite(buf->buf, buf->len, 1, stdout);
 335}
 336
 337static struct userdiff_driver no_func_name = {
 338        .funcname = { "$^", 0 }
 339};
 340
 341static struct diff_filespec *get_filespec(const char *name, const char *p)
 342{
 343        struct diff_filespec *spec = alloc_filespec(name);
 344
 345        fill_filespec(spec, &null_oid, 0, 0644);
 346        spec->data = (char *)p;
 347        spec->size = strlen(p);
 348        spec->should_munmap = 0;
 349        spec->is_stdin = 1;
 350        spec->driver = &no_func_name;
 351
 352        return spec;
 353}
 354
 355static void patch_diff(const char *a, const char *b,
 356                              struct diff_options *diffopt)
 357{
 358        diff_queue(&diff_queued_diff,
 359                   get_filespec("a", a), get_filespec("b", b));
 360
 361        diffcore_std(diffopt);
 362        diff_flush(diffopt);
 363}
 364
 365static void output(struct string_list *a, struct string_list *b,
 366                   struct diff_options *diffopt)
 367{
 368        struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
 369        int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
 370        int i = 0, j = 0;
 371
 372        /*
 373         * We assume the user is really more interested in the second argument
 374         * ("newer" version). To that end, we print the output in the order of
 375         * the RHS (the `b` parameter). To put the LHS (the `a` parameter)
 376         * commits that are no longer in the RHS into a good place, we place
 377         * them once we have shown all of their predecessors in the LHS.
 378         */
 379
 380        while (i < a->nr || j < b->nr) {
 381                struct patch_util *a_util, *b_util;
 382                a_util = i < a->nr ? a->items[i].util : NULL;
 383                b_util = j < b->nr ? b->items[j].util : NULL;
 384
 385                /* Skip all the already-shown commits from the LHS. */
 386                while (i < a->nr && a_util->shown)
 387                        a_util = ++i < a->nr ? a->items[i].util : NULL;
 388
 389                /* Show unmatched LHS commit whose predecessors were shown. */
 390                if (i < a->nr && a_util->matching < 0) {
 391                        output_pair_header(diffopt, patch_no_width,
 392                                           &buf, &dashes, a_util, NULL);
 393                        i++;
 394                        continue;
 395                }
 396
 397                /* Show unmatched RHS commits. */
 398                while (j < b->nr && b_util->matching < 0) {
 399                        output_pair_header(diffopt, patch_no_width,
 400                                           &buf, &dashes, NULL, b_util);
 401                        b_util = ++j < b->nr ? b->items[j].util : NULL;
 402                }
 403
 404                /* Show matching LHS/RHS pair. */
 405                if (j < b->nr) {
 406                        a_util = a->items[b_util->matching].util;
 407                        output_pair_header(diffopt, patch_no_width,
 408                                           &buf, &dashes, a_util, b_util);
 409                        if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
 410                                patch_diff(a->items[b_util->matching].string,
 411                                           b->items[j].string, diffopt);
 412                        a_util->shown = 1;
 413                        j++;
 414                }
 415        }
 416        strbuf_release(&buf);
 417        strbuf_release(&dashes);
 418}
 419
 420int show_range_diff(const char *range1, const char *range2,
 421                    int creation_factor, struct diff_options *diffopt)
 422{
 423        int res = 0;
 424
 425        struct string_list branch1 = STRING_LIST_INIT_DUP;
 426        struct string_list branch2 = STRING_LIST_INIT_DUP;
 427
 428        if (read_patches(range1, &branch1))
 429                res = error(_("could not parse log for '%s'"), range1);
 430        if (!res && read_patches(range2, &branch2))
 431                res = error(_("could not parse log for '%s'"), range2);
 432
 433        if (!res) {
 434                find_exact_matches(&branch1, &branch2);
 435                get_correspondences(&branch1, &branch2, creation_factor);
 436                output(&branch1, &branch2, diffopt);
 437        }
 438
 439        string_list_clear(&branch1, 1);
 440        string_list_clear(&branch2, 1);
 441
 442        return res;
 443}