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