tree-diff.con commit Merge branch 'pe/cleanup' into next (00cbdec)
   1/*
   2 * Helper functions for tree diff generation
   3 */
   4#include "cache.h"
   5#include "diff.h"
   6#include "tree.h"
   7
   8// What paths are we interested in?
   9static int nr_paths = 0;
  10static const char **paths = NULL;
  11static int *pathlens = NULL;
  12
  13static char *malloc_base(const char *base, const char *path, int pathlen)
  14{
  15        int baselen = strlen(base);
  16        char *newbase = xmalloc(baselen + pathlen + 2);
  17        memcpy(newbase, base, baselen);
  18        memcpy(newbase + baselen, path, pathlen);
  19        memcpy(newbase + baselen + pathlen, "/", 2);
  20        return newbase;
  21}
  22
  23static int show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base);
  24
  25static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
  26{
  27        unsigned mode1, mode2;
  28        const char *path1, *path2;
  29        const unsigned char *sha1, *sha2;
  30        int cmp, pathlen1, pathlen2;
  31
  32        sha1 = tree_entry_extract(t1, &path1, &mode1);
  33        sha2 = tree_entry_extract(t2, &path2, &mode2);
  34
  35        pathlen1 = strlen(path1);
  36        pathlen2 = strlen(path2);
  37        cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
  38        if (cmp < 0) {
  39                show_entry(opt, "-", t1, base);
  40                return -1;
  41        }
  42        if (cmp > 0) {
  43                show_entry(opt, "+", t2, base);
  44                return 1;
  45        }
  46        if (!opt->find_copies_harder &&
  47            !memcmp(sha1, sha2, 20) && mode1 == mode2)
  48                return 0;
  49
  50        /*
  51         * If the filemode has changed to/from a directory from/to a regular
  52         * file, we need to consider it a remove and an add.
  53         */
  54        if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
  55                show_entry(opt, "-", t1, base);
  56                show_entry(opt, "+", t2, base);
  57                return 0;
  58        }
  59
  60        if (opt->recursive && S_ISDIR(mode1)) {
  61                int retval;
  62                char *newbase = malloc_base(base, path1, pathlen1);
  63                if (opt->tree_in_recursive)
  64                        opt->change(opt, mode1, mode2,
  65                                    sha1, sha2, base, path1);
  66                retval = diff_tree_sha1(sha1, sha2, newbase, opt);
  67                free(newbase);
  68                return retval;
  69        }
  70
  71        opt->change(opt, mode1, mode2, sha1, sha2, base, path1);
  72        return 0;
  73}
  74
  75static int interesting(struct tree_desc *desc, const char *base)
  76{
  77        const char *path;
  78        unsigned mode;
  79        int i;
  80        int baselen, pathlen;
  81
  82        if (!nr_paths)
  83                return 1;
  84
  85        (void)tree_entry_extract(desc, &path, &mode);
  86
  87        pathlen = strlen(path);
  88        baselen = strlen(base);
  89
  90        for (i=0; i < nr_paths; i++) {
  91                const char *match = paths[i];
  92                int matchlen = pathlens[i];
  93
  94                if (baselen >= matchlen) {
  95                        /* If it doesn't match, move along... */
  96                        if (strncmp(base, match, matchlen))
  97                                continue;
  98
  99                        /* The base is a subdirectory of a path which was specified. */
 100                        return 1;
 101                }
 102
 103                /* Does the base match? */
 104                if (strncmp(base, match, baselen))
 105                        continue;
 106
 107                match += baselen;
 108                matchlen -= baselen;
 109
 110                if (pathlen > matchlen)
 111                        continue;
 112
 113                if (matchlen > pathlen) {
 114                        if (match[pathlen] != '/')
 115                                continue;
 116                        if (!S_ISDIR(mode))
 117                                continue;
 118                }
 119
 120                if (strncmp(path, match, pathlen))
 121                        continue;
 122
 123                return 1;
 124        }
 125        return 0; /* No matches */
 126}
 127
 128/* A whole sub-tree went away or appeared */
 129static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base)
 130{
 131        while (desc->size) {
 132                if (interesting(desc, base))
 133                        show_entry(opt, prefix, desc, base);
 134                update_tree_entry(desc);
 135        }
 136}
 137
 138/* A file entry went away or appeared */
 139static int show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base)
 140{
 141        unsigned mode;
 142        const char *path;
 143        const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
 144
 145        if (opt->recursive && S_ISDIR(mode)) {
 146                char type[20];
 147                char *newbase = malloc_base(base, path, strlen(path));
 148                struct tree_desc inner;
 149                void *tree;
 150
 151                tree = read_sha1_file(sha1, type, &inner.size);
 152                if (!tree || strcmp(type, tree_type))
 153                        die("corrupt tree sha %s", sha1_to_hex(sha1));
 154
 155                inner.buf = tree;
 156                show_tree(opt, prefix, &inner, newbase);
 157
 158                free(tree);
 159                free(newbase);
 160                return 0;
 161        }
 162
 163        opt->add_remove(opt, prefix[0], mode, sha1, base, path);
 164        return 0;
 165}
 166
 167int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
 168{
 169        while (t1->size | t2->size) {
 170                if (nr_paths && t1->size && !interesting(t1, base)) {
 171                        update_tree_entry(t1);
 172                        continue;
 173                }
 174                if (nr_paths && t2->size && !interesting(t2, base)) {
 175                        update_tree_entry(t2);
 176                        continue;
 177                }
 178                if (!t1->size) {
 179                        show_entry(opt, "+", t2, base);
 180                        update_tree_entry(t2);
 181                        continue;
 182                }
 183                if (!t2->size) {
 184                        show_entry(opt, "-", t1, base);
 185                        update_tree_entry(t1);
 186                        continue;
 187                }
 188                switch (compare_tree_entry(t1, t2, base, opt)) {
 189                case -1:
 190                        update_tree_entry(t1);
 191                        continue;
 192                case 0:
 193                        update_tree_entry(t1);
 194                        /* Fallthrough */
 195                case 1:
 196                        update_tree_entry(t2);
 197                        continue;
 198                }
 199                die("git-diff-tree: internal error");
 200        }
 201        return 0;
 202}
 203
 204int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
 205{
 206        void *tree1, *tree2;
 207        struct tree_desc t1, t2;
 208        int retval;
 209
 210        tree1 = read_object_with_reference(old, tree_type, &t1.size, NULL);
 211        if (!tree1)
 212                die("unable to read source tree (%s)", sha1_to_hex(old));
 213        tree2 = read_object_with_reference(new, tree_type, &t2.size, NULL);
 214        if (!tree2)
 215                die("unable to read destination tree (%s)", sha1_to_hex(new));
 216        t1.buf = tree1;
 217        t2.buf = tree2;
 218        retval = diff_tree(&t1, &t2, base, opt);
 219        free(tree1);
 220        free(tree2);
 221        return retval;
 222}
 223
 224static int count_paths(const char **paths)
 225{
 226        int i = 0;
 227        while (*paths++)
 228                i++;
 229        return i;
 230}
 231
 232void diff_tree_setup_paths(const char **p)
 233{
 234        if (p) {
 235                int i;
 236
 237                paths = p;
 238                nr_paths = count_paths(paths);
 239                if (nr_paths == 0) {
 240                        pathlens = NULL;
 241                        return;
 242                }
 243                pathlens = xmalloc(nr_paths * sizeof(int));
 244                for (i=0; i<nr_paths; i++)
 245                        pathlens[i] = strlen(paths[i]);
 246        }
 247}