match-trees.con commit Merge branch 'rt/bfg-ad-in-filter-branch-doc' (8a33472)
   1#include "cache.h"
   2#include "tree.h"
   3#include "tree-walk.h"
   4
   5static int score_missing(unsigned mode, const char *path)
   6{
   7        int score;
   8
   9        if (S_ISDIR(mode))
  10                score = -1000;
  11        else if (S_ISLNK(mode))
  12                score = -500;
  13        else
  14                score = -50;
  15        return score;
  16}
  17
  18static int score_differs(unsigned mode1, unsigned mode2, const char *path)
  19{
  20        int score;
  21
  22        if (S_ISDIR(mode1) != S_ISDIR(mode2))
  23                score = -100;
  24        else if (S_ISLNK(mode1) != S_ISLNK(mode2))
  25                score = -50;
  26        else
  27                score = -5;
  28        return score;
  29}
  30
  31static int score_matches(unsigned mode1, unsigned mode2, const char *path)
  32{
  33        int score;
  34
  35        /* Heh, we found SHA-1 collisions between different kind of objects */
  36        if (S_ISDIR(mode1) != S_ISDIR(mode2))
  37                score = -100;
  38        else if (S_ISLNK(mode1) != S_ISLNK(mode2))
  39                score = -50;
  40
  41        else if (S_ISDIR(mode1))
  42                score = 1000;
  43        else if (S_ISLNK(mode1))
  44                score = 500;
  45        else
  46                score = 250;
  47        return score;
  48}
  49
  50static void *fill_tree_desc_strict(struct tree_desc *desc,
  51                                   const unsigned char *hash)
  52{
  53        void *buffer;
  54        enum object_type type;
  55        unsigned long size;
  56
  57        buffer = read_sha1_file(hash, &type, &size);
  58        if (!buffer)
  59                die("unable to read tree (%s)", sha1_to_hex(hash));
  60        if (type != OBJ_TREE)
  61                die("%s is not a tree", sha1_to_hex(hash));
  62        init_tree_desc(desc, buffer, size);
  63        return buffer;
  64}
  65
  66static int base_name_entries_compare(const struct name_entry *a,
  67                                     const struct name_entry *b)
  68{
  69        return base_name_compare(a->path, tree_entry_len(a), a->mode,
  70                                 b->path, tree_entry_len(b), b->mode);
  71}
  72
  73/*
  74 * Inspect two trees, and give a score that tells how similar they are.
  75 */
  76static int score_trees(const unsigned char *hash1, const unsigned char *hash2)
  77{
  78        struct tree_desc one;
  79        struct tree_desc two;
  80        void *one_buf = fill_tree_desc_strict(&one, hash1);
  81        void *two_buf = fill_tree_desc_strict(&two, hash2);
  82        int score = 0;
  83
  84        for (;;) {
  85                struct name_entry e1, e2;
  86                int got_entry_from_one = tree_entry(&one, &e1);
  87                int got_entry_from_two = tree_entry(&two, &e2);
  88                int cmp;
  89
  90                if (got_entry_from_one && got_entry_from_two)
  91                        cmp = base_name_entries_compare(&e1, &e2);
  92                else if (got_entry_from_one)
  93                        /* two lacks this entry */
  94                        cmp = -1;
  95                else if (got_entry_from_two)
  96                        /* two has more entries */
  97                        cmp = 1;
  98                else
  99                        break;
 100
 101                if (cmp < 0)
 102                        /* path1 does not appear in two */
 103                        score += score_missing(e1.mode, e1.path);
 104                else if (cmp > 0)
 105                        /* path2 does not appear in one */
 106                        score += score_missing(e2.mode, e2.path);
 107                else if (hashcmp(e1.sha1, e2.sha1))
 108                        /* they are different */
 109                        score += score_differs(e1.mode, e2.mode, e1.path);
 110                else
 111                        /* same subtree or blob */
 112                        score += score_matches(e1.mode, e2.mode, e1.path);
 113        }
 114        free(one_buf);
 115        free(two_buf);
 116        return score;
 117}
 118
 119/*
 120 * Match one itself and its subtrees with two and pick the best match.
 121 */
 122static void match_trees(const unsigned char *hash1,
 123                        const unsigned char *hash2,
 124                        int *best_score,
 125                        char **best_match,
 126                        const char *base,
 127                        int recurse_limit)
 128{
 129        struct tree_desc one;
 130        void *one_buf = fill_tree_desc_strict(&one, hash1);
 131
 132        while (one.size) {
 133                const char *path;
 134                const unsigned char *elem;
 135                unsigned mode;
 136                int score;
 137
 138                elem = tree_entry_extract(&one, &path, &mode);
 139                if (!S_ISDIR(mode))
 140                        goto next;
 141                score = score_trees(elem, hash2);
 142                if (*best_score < score) {
 143                        char *newpath;
 144                        newpath = xmalloc(strlen(base) + strlen(path) + 1);
 145                        sprintf(newpath, "%s%s", base, path);
 146                        free(*best_match);
 147                        *best_match = newpath;
 148                        *best_score = score;
 149                }
 150                if (recurse_limit) {
 151                        char *newbase;
 152                        newbase = xmalloc(strlen(base) + strlen(path) + 2);
 153                        sprintf(newbase, "%s%s/", base, path);
 154                        match_trees(elem, hash2, best_score, best_match,
 155                                    newbase, recurse_limit - 1);
 156                        free(newbase);
 157                }
 158
 159        next:
 160                update_tree_entry(&one);
 161        }
 162        free(one_buf);
 163}
 164
 165/*
 166 * A tree "hash1" has a subdirectory at "prefix".  Come up with a
 167 * tree object by replacing it with another tree "hash2".
 168 */
 169static int splice_tree(const unsigned char *hash1,
 170                       const char *prefix,
 171                       const unsigned char *hash2,
 172                       unsigned char *result)
 173{
 174        char *subpath;
 175        int toplen;
 176        char *buf;
 177        unsigned long sz;
 178        struct tree_desc desc;
 179        unsigned char *rewrite_here;
 180        const unsigned char *rewrite_with;
 181        unsigned char subtree[20];
 182        enum object_type type;
 183        int status;
 184
 185        subpath = strchr(prefix, '/');
 186        if (!subpath)
 187                toplen = strlen(prefix);
 188        else {
 189                toplen = subpath - prefix;
 190                subpath++;
 191        }
 192
 193        buf = read_sha1_file(hash1, &type, &sz);
 194        if (!buf)
 195                die("cannot read tree %s", sha1_to_hex(hash1));
 196        init_tree_desc(&desc, buf, sz);
 197
 198        rewrite_here = NULL;
 199        while (desc.size) {
 200                const char *name;
 201                unsigned mode;
 202                const unsigned char *sha1;
 203
 204                sha1 = tree_entry_extract(&desc, &name, &mode);
 205                if (strlen(name) == toplen &&
 206                    !memcmp(name, prefix, toplen)) {
 207                        if (!S_ISDIR(mode))
 208                                die("entry %s in tree %s is not a tree",
 209                                    name, sha1_to_hex(hash1));
 210                        rewrite_here = (unsigned char *) sha1;
 211                        break;
 212                }
 213                update_tree_entry(&desc);
 214        }
 215        if (!rewrite_here)
 216                die("entry %.*s not found in tree %s",
 217                    toplen, prefix, sha1_to_hex(hash1));
 218        if (subpath) {
 219                status = splice_tree(rewrite_here, subpath, hash2, subtree);
 220                if (status)
 221                        return status;
 222                rewrite_with = subtree;
 223        }
 224        else
 225                rewrite_with = hash2;
 226        hashcpy(rewrite_here, rewrite_with);
 227        status = write_sha1_file(buf, sz, tree_type, result);
 228        free(buf);
 229        return status;
 230}
 231
 232/*
 233 * We are trying to come up with a merge between one and two that
 234 * results in a tree shape similar to one.  The tree two might
 235 * correspond to a subtree of one, in which case it needs to be
 236 * shifted down by prefixing otherwise empty directories.  On the
 237 * other hand, it could cover tree one and we might need to pick a
 238 * subtree of it.
 239 */
 240void shift_tree(const unsigned char *hash1,
 241                const unsigned char *hash2,
 242                unsigned char *shifted,
 243                int depth_limit)
 244{
 245        char *add_prefix;
 246        char *del_prefix;
 247        int add_score, del_score;
 248
 249        /*
 250         * NEEDSWORK: this limits the recursion depth to hardcoded
 251         * value '2' to avoid excessive overhead.
 252         */
 253        if (!depth_limit)
 254                depth_limit = 2;
 255
 256        add_score = del_score = score_trees(hash1, hash2);
 257        add_prefix = xcalloc(1, 1);
 258        del_prefix = xcalloc(1, 1);
 259
 260        /*
 261         * See if one's subtree resembles two; if so we need to prefix
 262         * two with a few fake trees to match the prefix.
 263         */
 264        match_trees(hash1, hash2, &add_score, &add_prefix, "", depth_limit);
 265
 266        /*
 267         * See if two's subtree resembles one; if so we need to
 268         * pick only subtree of two.
 269         */
 270        match_trees(hash2, hash1, &del_score, &del_prefix, "", depth_limit);
 271
 272        /* Assume we do not have to do any shifting */
 273        hashcpy(shifted, hash2);
 274
 275        if (add_score < del_score) {
 276                /* We need to pick a subtree of two */
 277                unsigned mode;
 278
 279                if (!*del_prefix)
 280                        return;
 281
 282                if (get_tree_entry(hash2, del_prefix, shifted, &mode))
 283                        die("cannot find path %s in tree %s",
 284                            del_prefix, sha1_to_hex(hash2));
 285                return;
 286        }
 287
 288        if (!*add_prefix)
 289                return;
 290
 291        splice_tree(hash1, add_prefix, hash2, shifted);
 292}
 293
 294/*
 295 * The user says the trees will be shifted by this much.
 296 * Unfortunately we cannot fundamentally tell which one to
 297 * be prefixed, as recursive merge can work in either direction.
 298 */
 299void shift_tree_by(const unsigned char *hash1,
 300                   const unsigned char *hash2,
 301                   unsigned char *shifted,
 302                   const char *shift_prefix)
 303{
 304        unsigned char sub1[20], sub2[20];
 305        unsigned mode1, mode2;
 306        unsigned candidate = 0;
 307
 308        /* Can hash2 be a tree at shift_prefix in tree hash1? */
 309        if (!get_tree_entry(hash1, shift_prefix, sub1, &mode1) &&
 310            S_ISDIR(mode1))
 311                candidate |= 1;
 312
 313        /* Can hash1 be a tree at shift_prefix in tree hash2? */
 314        if (!get_tree_entry(hash2, shift_prefix, sub2, &mode2) &&
 315            S_ISDIR(mode2))
 316                candidate |= 2;
 317
 318        if (candidate == 3) {
 319                /* Both are plausible -- we need to evaluate the score */
 320                int best_score = score_trees(hash1, hash2);
 321                int score;
 322
 323                candidate = 0;
 324                score = score_trees(sub1, hash2);
 325                if (score > best_score) {
 326                        candidate = 1;
 327                        best_score = score;
 328                }
 329                score = score_trees(sub2, hash1);
 330                if (score > best_score)
 331                        candidate = 2;
 332        }
 333
 334        if (!candidate) {
 335                /* Neither is plausible -- do not shift */
 336                hashcpy(shifted, hash2);
 337                return;
 338        }
 339
 340        if (candidate == 1)
 341                /*
 342                 * shift tree2 down by adding shift_prefix above it
 343                 * to match tree1.
 344                 */
 345                splice_tree(hash1, shift_prefix, hash2, shifted);
 346        else
 347                /*
 348                 * shift tree2 up by removing shift_prefix from it
 349                 * to match tree1.
 350                 */
 351                hashcpy(shifted, sub2);
 352}