sha1_name.con commit Log message printout cleanups (#3): fix --pretty=oneline (a4d34e2)
   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
 125#define SHORT_NAME_NOT_FOUND (-1)
 126#define SHORT_NAME_AMBIGUOUS (-2)
 127
 128static int find_unique_short_object(int len, char *canonical,
 129                                    unsigned char *res, unsigned char *sha1)
 130{
 131        int has_unpacked, has_packed;
 132        unsigned char unpacked_sha1[20], packed_sha1[20];
 133
 134        has_unpacked = find_short_object_filename(len, canonical, unpacked_sha1);
 135        has_packed = find_short_packed_object(len, res, packed_sha1);
 136        if (!has_unpacked && !has_packed)
 137                return SHORT_NAME_NOT_FOUND;
 138        if (1 < has_unpacked || 1 < has_packed)
 139                return SHORT_NAME_AMBIGUOUS;
 140        if (has_unpacked != has_packed) {
 141                memcpy(sha1, (has_packed ? packed_sha1 : unpacked_sha1), 20);
 142                return 0;
 143        }
 144        /* Both have unique ones -- do they match? */
 145        if (memcmp(packed_sha1, unpacked_sha1, 20))
 146                return SHORT_NAME_AMBIGUOUS;
 147        memcpy(sha1, packed_sha1, 20);
 148        return 0;
 149}
 150
 151static int get_short_sha1(const char *name, int len, unsigned char *sha1,
 152                          int quietly)
 153{
 154        int i, status;
 155        char canonical[40];
 156        unsigned char res[20];
 157
 158        if (len < MINIMUM_ABBREV)
 159                return -1;
 160        memset(res, 0, 20);
 161        memset(canonical, 'x', 40);
 162        for (i = 0; i < len ;i++) {
 163                unsigned char c = name[i];
 164                unsigned char val;
 165                if (c >= '0' && c <= '9')
 166                        val = c - '0';
 167                else if (c >= 'a' && c <= 'f')
 168                        val = c - 'a' + 10;
 169                else if (c >= 'A' && c <='F') {
 170                        val = c - 'A' + 10;
 171                        c -= 'A' - 'a';
 172                }
 173                else
 174                        return -1;
 175                canonical[i] = c;
 176                if (!(i & 1))
 177                        val <<= 4;
 178                res[i >> 1] |= val;
 179        }
 180
 181        status = find_unique_short_object(i, canonical, res, sha1);
 182        if (!quietly && (status == SHORT_NAME_AMBIGUOUS))
 183                return error("short SHA1 %.*s is ambiguous.", len, canonical);
 184        return status;
 185}
 186
 187const char *find_unique_abbrev(const unsigned char *sha1, int len)
 188{
 189        int status, is_null;
 190        static char hex[41];
 191
 192        is_null = !memcmp(sha1, null_sha1, 20);
 193        memcpy(hex, sha1_to_hex(sha1), 40);
 194        if (len == 40)
 195                return hex;
 196        while (len < 40) {
 197                unsigned char sha1_ret[20];
 198                status = get_short_sha1(hex, len, sha1_ret, 1);
 199                if (!status ||
 200                    (is_null && status != SHORT_NAME_AMBIGUOUS)) {
 201                        hex[len] = 0;
 202                        return hex;
 203                }
 204                if (status != SHORT_NAME_AMBIGUOUS)
 205                        return NULL;
 206                len++;
 207        }
 208        return NULL;
 209}
 210
 211static int ambiguous_path(const char *path, int len)
 212{
 213        int slash = 1;
 214        int cnt;
 215
 216        for (cnt = 0; cnt < len; cnt++) {
 217                switch (*path++) {
 218                case '\0':
 219                        break;
 220                case '/':
 221                        if (slash)
 222                                break;
 223                        slash = 1;
 224                        continue;
 225                case '.':
 226                        continue;
 227                default:
 228                        slash = 0;
 229                        continue;
 230                }
 231                break;
 232        }
 233        return slash;
 234}
 235
 236static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
 237{
 238        static const char *fmt[] = {
 239                "%.*s",
 240                "refs/%.*s",
 241                "refs/tags/%.*s",
 242                "refs/heads/%.*s",
 243                "refs/remotes/%.*s",
 244                "refs/remotes/%.*s/HEAD",
 245                NULL
 246        };
 247        const char **p;
 248        const char *warning = "warning: refname '%.*s' is ambiguous.\n";
 249        char *pathname;
 250        int already_found = 0;
 251        unsigned char *this_result;
 252        unsigned char sha1_from_ref[20];
 253
 254        if (len == 40 && !get_sha1_hex(str, sha1))
 255                return 0;
 256
 257        /* Accept only unambiguous ref paths. */
 258        if (ambiguous_path(str, len))
 259                return -1;
 260
 261        for (p = fmt; *p; p++) {
 262                this_result = already_found ? sha1_from_ref : sha1;
 263                pathname = git_path(*p, len, str);
 264                if (!read_ref(pathname, this_result)) {
 265                        if (warn_ambiguous_refs) {
 266                                if (already_found)
 267                                        fprintf(stderr, warning, len, str);
 268                                already_found++;
 269                        }
 270                        else
 271                                return 0;
 272                }
 273        }
 274        if (already_found)
 275                return 0;
 276        return -1;
 277}
 278
 279static int get_sha1_1(const char *name, int len, unsigned char *sha1);
 280
 281static int get_parent(const char *name, int len,
 282                      unsigned char *result, int idx)
 283{
 284        unsigned char sha1[20];
 285        int ret = get_sha1_1(name, len, sha1);
 286        struct commit *commit;
 287        struct commit_list *p;
 288
 289        if (ret)
 290                return ret;
 291        commit = lookup_commit_reference(sha1);
 292        if (!commit)
 293                return -1;
 294        if (parse_commit(commit))
 295                return -1;
 296        if (!idx) {
 297                memcpy(result, commit->object.sha1, 20);
 298                return 0;
 299        }
 300        p = commit->parents;
 301        while (p) {
 302                if (!--idx) {
 303                        memcpy(result, p->item->object.sha1, 20);
 304                        return 0;
 305                }
 306                p = p->next;
 307        }
 308        return -1;
 309}
 310
 311static int get_nth_ancestor(const char *name, int len,
 312                            unsigned char *result, int generation)
 313{
 314        unsigned char sha1[20];
 315        int ret = get_sha1_1(name, len, sha1);
 316        if (ret)
 317                return ret;
 318
 319        while (generation--) {
 320                struct commit *commit = lookup_commit_reference(sha1);
 321
 322                if (!commit || parse_commit(commit) || !commit->parents)
 323                        return -1;
 324                memcpy(sha1, commit->parents->item->object.sha1, 20);
 325        }
 326        memcpy(result, sha1, 20);
 327        return 0;
 328}
 329
 330static int peel_onion(const char *name, int len, unsigned char *sha1)
 331{
 332        unsigned char outer[20];
 333        const char *sp;
 334        const char *type_string = NULL;
 335        struct object *o;
 336
 337        /*
 338         * "ref^{type}" dereferences ref repeatedly until you cannot
 339         * dereference anymore, or you get an object of given type,
 340         * whichever comes first.  "ref^{}" means just dereference
 341         * tags until you get a non-tag.  "ref^0" is a shorthand for
 342         * "ref^{commit}".  "commit^{tree}" could be used to find the
 343         * top-level tree of the given commit.
 344         */
 345        if (len < 4 || name[len-1] != '}')
 346                return -1;
 347
 348        for (sp = name + len - 1; name <= sp; sp--) {
 349                int ch = *sp;
 350                if (ch == '{' && name < sp && sp[-1] == '^')
 351                        break;
 352        }
 353        if (sp <= name)
 354                return -1;
 355
 356        sp++; /* beginning of type name, or closing brace for empty */
 357        if (!strncmp(commit_type, sp, 6) && sp[6] == '}')
 358                type_string = commit_type;
 359        else if (!strncmp(tree_type, sp, 4) && sp[4] == '}')
 360                type_string = tree_type;
 361        else if (!strncmp(blob_type, sp, 4) && sp[4] == '}')
 362                type_string = blob_type;
 363        else if (sp[0] == '}')
 364                type_string = NULL;
 365        else
 366                return -1;
 367
 368        if (get_sha1_1(name, sp - name - 2, outer))
 369                return -1;
 370
 371        o = parse_object(outer);
 372        if (!o)
 373                return -1;
 374        if (!type_string) {
 375                o = deref_tag(o, name, sp - name - 2);
 376                if (!o || (!o->parsed && !parse_object(o->sha1)))
 377                        return -1;
 378                memcpy(sha1, o->sha1, 20);
 379        }
 380        else {
 381                /* At this point, the syntax look correct, so
 382                 * if we do not get the needed object, we should
 383                 * barf.
 384                 */
 385
 386                while (1) {
 387                        if (!o || (!o->parsed && !parse_object(o->sha1)))
 388                                return -1;
 389                        if (o->type == type_string) {
 390                                memcpy(sha1, o->sha1, 20);
 391                                return 0;
 392                        }
 393                        if (o->type == tag_type)
 394                                o = ((struct tag*) o)->tagged;
 395                        else if (o->type == commit_type)
 396                                o = &(((struct commit *) o)->tree->object);
 397                        else
 398                                return error("%.*s: expected %s type, but the object dereferences to %s type",
 399                                             len, name, type_string,
 400                                             o->type);
 401                        if (!o->parsed)
 402                                parse_object(o->sha1);
 403                }
 404        }
 405        return 0;
 406}
 407
 408static int get_sha1_1(const char *name, int len, unsigned char *sha1)
 409{
 410        int ret, has_suffix;
 411        const char *cp;
 412
 413        /* "name~3" is "name^^^",
 414         * "name~" and "name~0" are name -- not "name^0"!
 415         * "name^" is not "name^0"; it is "name^1".
 416         */
 417        has_suffix = 0;
 418        for (cp = name + len - 1; name <= cp; cp--) {
 419                int ch = *cp;
 420                if ('0' <= ch && ch <= '9')
 421                        continue;
 422                if (ch == '~' || ch == '^')
 423                        has_suffix = ch;
 424                break;
 425        }
 426
 427        if (has_suffix) {
 428                int num = 0;
 429                int len1 = cp - name;
 430                cp++;
 431                while (cp < name + len)
 432                        num = num * 10 + *cp++ - '0';
 433                if (has_suffix == '^') {
 434                        if (!num && len1 == len - 1)
 435                                num = 1;
 436                        return get_parent(name, len1, sha1, num);
 437                }
 438                /* else if (has_suffix == '~') -- goes without saying */
 439                return get_nth_ancestor(name, len1, sha1, num);
 440        }
 441
 442        ret = peel_onion(name, len, sha1);
 443        if (!ret)
 444                return 0;
 445
 446        ret = get_sha1_basic(name, len, sha1);
 447        if (!ret)
 448                return 0;
 449        return get_short_sha1(name, len, sha1, 0);
 450}
 451
 452/*
 453 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
 454 * notably "xyz^" for "parent of xyz"
 455 */
 456int get_sha1(const char *name, unsigned char *sha1)
 457{
 458        prepare_alt_odb();
 459        return get_sha1_1(name, strlen(name), sha1);
 460}