sha1_name.con commit Merge branch 'fixes' (02b54b3)
   1#include "cache.h"
   2#include "tag.h"
   3#include "commit.h"
   4#include "tree.h"
   5#include "blob.h"
   6
   7static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
   8{
   9        struct alternate_object_database *alt;
  10        char hex[40];
  11        int found = 0;
  12        static struct alternate_object_database *fakeent;
  13
  14        if (!fakeent) {
  15                const char *objdir = get_object_directory();
  16                int objdir_len = strlen(objdir);
  17                int entlen = objdir_len + 43;
  18                fakeent = xmalloc(sizeof(*fakeent) + entlen);
  19                memcpy(fakeent->base, objdir, objdir_len);
  20                fakeent->name = fakeent->base + objdir_len + 1;
  21                fakeent->name[-1] = '/';
  22        }
  23        fakeent->next = alt_odb_list;
  24
  25        sprintf(hex, "%.2s", name);
  26        for (alt = fakeent; alt && found < 2; alt = alt->next) {
  27                struct dirent *de;
  28                DIR *dir;
  29                sprintf(alt->name, "%.2s/", name);
  30                dir = opendir(alt->base);
  31                if (!dir)
  32                        continue;
  33                while ((de = readdir(dir)) != NULL) {
  34                        if (strlen(de->d_name) != 38)
  35                                continue;
  36                        if (memcmp(de->d_name, name + 2, len - 2))
  37                                continue;
  38                        if (!found) {
  39                                memcpy(hex + 2, de->d_name, 38);
  40                                found++;
  41                        }
  42                        else if (memcmp(hex + 2, de->d_name, 38)) {
  43                                found = 2;
  44                                break;
  45                        }
  46                }
  47                closedir(dir);
  48        }
  49        if (found == 1)
  50                return get_sha1_hex(hex, sha1) == 0;
  51        return found;
  52}
  53
  54static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b)
  55{
  56        do {
  57                if (*a != *b)
  58                        return 0;
  59                a++;
  60                b++;
  61                len -= 2;
  62        } while (len > 1);
  63        if (len)
  64                if ((*a ^ *b) & 0xf0)
  65                        return 0;
  66        return 1;
  67}
  68
  69static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1)
  70{
  71        struct packed_git *p;
  72        unsigned char found_sha1[20];
  73        int found = 0;
  74
  75        prepare_packed_git();
  76        for (p = packed_git; p && found < 2; p = p->next) {
  77                unsigned num = num_packed_objects(p);
  78                unsigned first = 0, last = num;
  79                while (first < last) {
  80                        unsigned mid = (first + last) / 2;
  81                        unsigned char now[20];
  82                        int cmp;
  83
  84                        nth_packed_object_sha1(p, mid, now);
  85                        cmp = memcmp(match, now, 20);
  86                        if (!cmp) {
  87                                first = mid;
  88                                break;
  89                        }
  90                        if (cmp > 0) {
  91                                first = mid+1;
  92                                continue;
  93                        }
  94                        last = mid;
  95                }
  96                if (first < num) {
  97                        unsigned char now[20], next[20];
  98                        nth_packed_object_sha1(p, first, now);
  99                        if (match_sha(len, match, now)) {
 100                                if (nth_packed_object_sha1(p, first+1, next) ||
 101                                    !match_sha(len, match, next)) {
 102                                        /* unique within this pack */
 103                                        if (!found) {
 104                                                memcpy(found_sha1, now, 20);
 105                                                found++;
 106                                        }
 107                                        else if (memcmp(found_sha1, now, 20)) {
 108                                                found = 2;
 109                                                break;
 110                                        }
 111                                }
 112                                else {
 113                                        /* not even unique within this pack */
 114                                        found = 2;
 115                                        break;
 116                                }
 117                        }
 118                }
 119        }
 120        if (found == 1)
 121                memcpy(sha1, found_sha1, 20);
 122        return found;
 123}
 124
 125static int find_unique_short_object(int len, char *canonical,
 126                                    unsigned char *res, unsigned char *sha1)
 127{
 128        int has_unpacked, has_packed;
 129        unsigned char unpacked_sha1[20], packed_sha1[20];
 130
 131        has_unpacked = find_short_object_filename(len, canonical, unpacked_sha1);
 132        has_packed = find_short_packed_object(len, res, packed_sha1);
 133        if (!has_unpacked && !has_packed)
 134                return -1;
 135        if (1 < has_unpacked || 1 < has_packed)
 136                return error("short SHA1 %.*s is ambiguous.", len, canonical);
 137        if (has_unpacked != has_packed) {
 138                memcpy(sha1, (has_packed ? packed_sha1 : unpacked_sha1), 20);
 139                return 0;
 140        }
 141        /* Both have unique ones -- do they match? */
 142        if (memcmp(packed_sha1, unpacked_sha1, 20))
 143                return error("short SHA1 %.*s is ambiguous.", len, canonical);
 144        memcpy(sha1, packed_sha1, 20);
 145        return 0;
 146}
 147
 148static int get_short_sha1(const char *name, int len, unsigned char *sha1)
 149{
 150        int i;
 151        char canonical[40];
 152        unsigned char res[20];
 153
 154        if (len < 4)
 155                return -1;
 156        memset(res, 0, 20);
 157        memset(canonical, 'x', 40);
 158        for (i = 0; i < len ;i++) {
 159                unsigned char c = name[i];
 160                unsigned char val;
 161                if (c >= '0' && c <= '9')
 162                        val = c - '0';
 163                else if (c >= 'a' && c <= 'f')
 164                        val = c - 'a' + 10;
 165                else if (c >= 'A' && c <='F') {
 166                        val = c - 'A' + 10;
 167                        c -= 'A' - 'a';
 168                }
 169                else
 170                        return -1;
 171                canonical[i] = c;
 172                if (!(i & 1))
 173                        val <<= 4;
 174                res[i >> 1] |= val;
 175        }
 176
 177        return find_unique_short_object(i, canonical, res, sha1);
 178}
 179
 180static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
 181{
 182        static const char *prefix[] = {
 183                "",
 184                "refs",
 185                "refs/tags",
 186                "refs/heads",
 187                NULL
 188        };
 189        const char **p;
 190
 191        if (len == 40 && !get_sha1_hex(str, sha1))
 192                return 0;
 193
 194        for (p = prefix; *p; p++) {
 195                char *pathname = git_path("%s/%.*s", *p, len, str);
 196                if (!read_ref(pathname, sha1))
 197                        return 0;
 198        }
 199
 200        return -1;
 201}
 202
 203static int get_sha1_1(const char *name, int len, unsigned char *sha1);
 204
 205static int get_parent(const char *name, int len,
 206                      unsigned char *result, int idx)
 207{
 208        unsigned char sha1[20];
 209        int ret = get_sha1_1(name, len, sha1);
 210        struct commit *commit;
 211        struct commit_list *p;
 212
 213        if (ret)
 214                return ret;
 215        commit = lookup_commit_reference(sha1);
 216        if (!commit)
 217                return -1;
 218        if (parse_commit(commit))
 219                return -1;
 220        if (!idx) {
 221                memcpy(result, commit->object.sha1, 20);
 222                return 0;
 223        }
 224        p = commit->parents;
 225        while (p) {
 226                if (!--idx) {
 227                        memcpy(result, p->item->object.sha1, 20);
 228                        return 0;
 229                }
 230                p = p->next;
 231        }
 232        return -1;
 233}
 234
 235static int get_nth_ancestor(const char *name, int len,
 236                            unsigned char *result, int generation)
 237{
 238        unsigned char sha1[20];
 239        int ret = get_sha1_1(name, len, sha1);
 240        if (ret)
 241                return ret;
 242
 243        while (generation--) {
 244                struct commit *commit = lookup_commit_reference(sha1);
 245
 246                if (!commit || parse_commit(commit) || !commit->parents)
 247                        return -1;
 248                memcpy(sha1, commit->parents->item->object.sha1, 20);
 249        }
 250        memcpy(result, sha1, 20);
 251        return 0;
 252}
 253
 254static int peel_onion(const char *name, int len, unsigned char *sha1)
 255{
 256        unsigned char outer[20];
 257        const char *sp;
 258        const char *type_string = NULL;
 259        struct object *o;
 260
 261        /*
 262         * "ref^{type}" dereferences ref repeatedly until you cannot
 263         * dereference anymore, or you get an object of given type,
 264         * whichever comes first.  "ref^{}" means just dereference
 265         * tags until you get a non-tag.  "ref^0" is a shorthand for
 266         * "ref^{commit}".  "commit^{tree}" could be used to find the
 267         * top-level tree of the given commit.
 268         */
 269        if (len < 4 || name[len-1] != '}')
 270                return -1;
 271
 272        for (sp = name + len - 1; name <= sp; sp--) {
 273                int ch = *sp;
 274                if (ch == '{' && name < sp && sp[-1] == '^')
 275                        break;
 276        }
 277        if (sp <= name)
 278                return -1;
 279
 280        sp++; /* beginning of type name, or closing brace for empty */
 281        if (!strncmp(commit_type, sp, 6) && sp[6] == '}')
 282                type_string = commit_type;
 283        else if (!strncmp(tree_type, sp, 4) && sp[4] == '}')
 284                type_string = tree_type;
 285        else if (!strncmp(blob_type, sp, 4) && sp[4] == '}')
 286                type_string = blob_type;
 287        else if (sp[0] == '}')
 288                type_string = NULL;
 289        else
 290                return -1;
 291
 292        if (get_sha1_1(name, sp - name - 2, outer))
 293                return -1;
 294
 295        o = parse_object(outer);
 296        if (!o)
 297                return -1;
 298        if (!type_string) {
 299                o = deref_tag(o);
 300                memcpy(sha1, o->sha1, 20);
 301        }
 302        else {
 303                /* At this point, the syntax look correct, so
 304                 * if we do not get the needed object, we should
 305                 * barf.
 306                 */
 307
 308                while (1) {
 309                        if (!o)
 310                                return -1;
 311                        if (o->type == type_string) {
 312                                memcpy(sha1, o->sha1, 20);
 313                                return 0;
 314                        }
 315                        if (o->type == tag_type)
 316                                o = ((struct tag*) o)->tagged;
 317                        else if (o->type == commit_type)
 318                                o = &(((struct commit *) o)->tree->object);
 319                        else
 320                                return error("%.*s: expected %s type, but the object dereferences to %s type",
 321                                             len, name, type_string,
 322                                             o->type);
 323                        if (!o->parsed)
 324                                parse_object(o->sha1);
 325                }
 326        }
 327        return 0;
 328}
 329
 330static int get_sha1_1(const char *name, int len, unsigned char *sha1)
 331{
 332        int parent, ret;
 333        const char *cp;
 334
 335        /* foo^[0-9] or foo^ (== foo^1); we do not do more than 9 parents. */
 336        if (len > 2 && name[len-2] == '^' &&
 337            name[len-1] >= '0' && name[len-1] <= '9') {
 338                parent = name[len-1] - '0';
 339                len -= 2;
 340        }
 341        else if (len > 1 && name[len-1] == '^') {
 342                parent = 1;
 343                len--;
 344        } else
 345                parent = -1;
 346
 347        if (parent >= 0)
 348                return get_parent(name, len, sha1, parent);
 349
 350        /* "name~3" is "name^^^",
 351         * "name~12" is "name^^^^^^^^^^^^", and
 352         * "name~" and "name~0" are name -- not "name^0"!
 353         */
 354        parent = 0;
 355        for (cp = name + len - 1; name <= cp; cp--) {
 356                int ch = *cp;
 357                if ('0' <= ch && ch <= '9')
 358                        continue;
 359                if (ch != '~')
 360                        parent = -1;
 361                break;
 362        }
 363        if (!parent && *cp == '~') {
 364                int len1 = cp - name;
 365                cp++;
 366                while (cp < name + len)
 367                        parent = parent * 10 + *cp++ - '0';
 368                return get_nth_ancestor(name, len1, sha1, parent);
 369        }
 370
 371        ret = peel_onion(name, len, sha1);
 372        if (!ret)
 373                return 0;
 374
 375        ret = get_sha1_basic(name, len, sha1);
 376        if (!ret)
 377                return 0;
 378        return get_short_sha1(name, len, sha1);
 379}
 380
 381/*
 382 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
 383 * notably "xyz^" for "parent of xyz"
 384 */
 385int get_sha1(const char *name, unsigned char *sha1)
 386{
 387        prepare_alt_odb();
 388        return get_sha1_1(name, strlen(name), sha1);
 389}