cdcf84b826234d92f057305e2431a6d9b609202d
   1#include "cache.h"
   2#include "config.h"
   3#include "tag.h"
   4#include "commit.h"
   5#include "tree.h"
   6#include "blob.h"
   7#include "tree-walk.h"
   8#include "refs.h"
   9#include "remote.h"
  10#include "dir.h"
  11#include "sha1-array.h"
  12#include "packfile.h"
  13#include "object-store.h"
  14#include "repository.h"
  15#include "midx.h"
  16#include "commit-reach.h"
  17
  18static int get_oid_oneline(const char *, struct object_id *, struct commit_list *);
  19
  20typedef int (*disambiguate_hint_fn)(struct repository *, const struct object_id *, void *);
  21
  22struct disambiguate_state {
  23        int len; /* length of prefix in hex chars */
  24        char hex_pfx[GIT_MAX_HEXSZ + 1];
  25        struct object_id bin_pfx;
  26
  27        struct repository *repo;
  28        disambiguate_hint_fn fn;
  29        void *cb_data;
  30        struct object_id candidate;
  31        unsigned candidate_exists:1;
  32        unsigned candidate_checked:1;
  33        unsigned candidate_ok:1;
  34        unsigned disambiguate_fn_used:1;
  35        unsigned ambiguous:1;
  36        unsigned always_call_fn:1;
  37};
  38
  39static void update_candidates(struct disambiguate_state *ds, const struct object_id *current)
  40{
  41        if (ds->always_call_fn) {
  42                ds->ambiguous = ds->fn(ds->repo, current, ds->cb_data) ? 1 : 0;
  43                return;
  44        }
  45        if (!ds->candidate_exists) {
  46                /* this is the first candidate */
  47                oidcpy(&ds->candidate, current);
  48                ds->candidate_exists = 1;
  49                return;
  50        } else if (oideq(&ds->candidate, current)) {
  51                /* the same as what we already have seen */
  52                return;
  53        }
  54
  55        if (!ds->fn) {
  56                /* cannot disambiguate between ds->candidate and current */
  57                ds->ambiguous = 1;
  58                return;
  59        }
  60
  61        if (!ds->candidate_checked) {
  62                ds->candidate_ok = ds->fn(ds->repo, &ds->candidate, ds->cb_data);
  63                ds->disambiguate_fn_used = 1;
  64                ds->candidate_checked = 1;
  65        }
  66
  67        if (!ds->candidate_ok) {
  68                /* discard the candidate; we know it does not satisfy fn */
  69                oidcpy(&ds->candidate, current);
  70                ds->candidate_checked = 0;
  71                return;
  72        }
  73
  74        /* if we reach this point, we know ds->candidate satisfies fn */
  75        if (ds->fn(ds->repo, current, ds->cb_data)) {
  76                /*
  77                 * if both current and candidate satisfy fn, we cannot
  78                 * disambiguate.
  79                 */
  80                ds->candidate_ok = 0;
  81                ds->ambiguous = 1;
  82        }
  83
  84        /* otherwise, current can be discarded and candidate is still good */
  85}
  86
  87static int match_sha(unsigned, const unsigned char *, const unsigned char *);
  88
  89static void find_short_object_filename(struct disambiguate_state *ds)
  90{
  91        struct object_directory *odb;
  92
  93        for (odb = ds->repo->objects->odb; odb && !ds->ambiguous; odb = odb->next) {
  94                int pos;
  95                struct oid_array *loose_objects;
  96
  97                loose_objects = odb_loose_cache(odb, &ds->bin_pfx);
  98                pos = oid_array_lookup(loose_objects, &ds->bin_pfx);
  99                if (pos < 0)
 100                        pos = -1 - pos;
 101                while (!ds->ambiguous && pos < loose_objects->nr) {
 102                        const struct object_id *oid;
 103                        oid = loose_objects->oid + pos;
 104                        if (!match_sha(ds->len, ds->bin_pfx.hash, oid->hash))
 105                                break;
 106                        update_candidates(ds, oid);
 107                        pos++;
 108                }
 109        }
 110}
 111
 112static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b)
 113{
 114        do {
 115                if (*a != *b)
 116                        return 0;
 117                a++;
 118                b++;
 119                len -= 2;
 120        } while (len > 1);
 121        if (len)
 122                if ((*a ^ *b) & 0xf0)
 123                        return 0;
 124        return 1;
 125}
 126
 127static void unique_in_midx(struct multi_pack_index *m,
 128                           struct disambiguate_state *ds)
 129{
 130        uint32_t num, i, first = 0;
 131        const struct object_id *current = NULL;
 132        num = m->num_objects;
 133
 134        if (!num)
 135                return;
 136
 137        bsearch_midx(&ds->bin_pfx, m, &first);
 138
 139        /*
 140         * At this point, "first" is the location of the lowest object
 141         * with an object name that could match "bin_pfx".  See if we have
 142         * 0, 1 or more objects that actually match(es).
 143         */
 144        for (i = first; i < num && !ds->ambiguous; i++) {
 145                struct object_id oid;
 146                current = nth_midxed_object_oid(&oid, m, i);
 147                if (!match_sha(ds->len, ds->bin_pfx.hash, current->hash))
 148                        break;
 149                update_candidates(ds, current);
 150        }
 151}
 152
 153static void unique_in_pack(struct packed_git *p,
 154                           struct disambiguate_state *ds)
 155{
 156        uint32_t num, i, first = 0;
 157        const struct object_id *current = NULL;
 158
 159        if (open_pack_index(p) || !p->num_objects)
 160                return;
 161
 162        num = p->num_objects;
 163        bsearch_pack(&ds->bin_pfx, p, &first);
 164
 165        /*
 166         * At this point, "first" is the location of the lowest object
 167         * with an object name that could match "bin_pfx".  See if we have
 168         * 0, 1 or more objects that actually match(es).
 169         */
 170        for (i = first; i < num && !ds->ambiguous; i++) {
 171                struct object_id oid;
 172                current = nth_packed_object_oid(&oid, p, i);
 173                if (!match_sha(ds->len, ds->bin_pfx.hash, current->hash))
 174                        break;
 175                update_candidates(ds, current);
 176        }
 177}
 178
 179static void find_short_packed_object(struct disambiguate_state *ds)
 180{
 181        struct multi_pack_index *m;
 182        struct packed_git *p;
 183
 184        for (m = get_multi_pack_index(ds->repo); m && !ds->ambiguous;
 185             m = m->next)
 186                unique_in_midx(m, ds);
 187        for (p = get_packed_git(ds->repo); p && !ds->ambiguous;
 188             p = p->next)
 189                unique_in_pack(p, ds);
 190}
 191
 192static int finish_object_disambiguation(struct disambiguate_state *ds,
 193                                        struct object_id *oid)
 194{
 195        if (ds->ambiguous)
 196                return SHORT_NAME_AMBIGUOUS;
 197
 198        if (!ds->candidate_exists)
 199                return MISSING_OBJECT;
 200
 201        if (!ds->candidate_checked)
 202                /*
 203                 * If this is the only candidate, there is no point
 204                 * calling the disambiguation hint callback.
 205                 *
 206                 * On the other hand, if the current candidate
 207                 * replaced an earlier candidate that did _not_ pass
 208                 * the disambiguation hint callback, then we do have
 209                 * more than one objects that match the short name
 210                 * given, so we should make sure this one matches;
 211                 * otherwise, if we discovered this one and the one
 212                 * that we previously discarded in the reverse order,
 213                 * we would end up showing different results in the
 214                 * same repository!
 215                 */
 216                ds->candidate_ok = (!ds->disambiguate_fn_used ||
 217                                    ds->fn(ds->repo, &ds->candidate, ds->cb_data));
 218
 219        if (!ds->candidate_ok)
 220                return SHORT_NAME_AMBIGUOUS;
 221
 222        oidcpy(oid, &ds->candidate);
 223        return 0;
 224}
 225
 226static int disambiguate_commit_only(struct repository *r,
 227                                    const struct object_id *oid,
 228                                    void *cb_data_unused)
 229{
 230        int kind = oid_object_info(r, oid, NULL);
 231        return kind == OBJ_COMMIT;
 232}
 233
 234static int disambiguate_committish_only(struct repository *r,
 235                                        const struct object_id *oid,
 236                                        void *cb_data_unused)
 237{
 238        struct object *obj;
 239        int kind;
 240
 241        kind = oid_object_info(r, oid, NULL);
 242        if (kind == OBJ_COMMIT)
 243                return 1;
 244        if (kind != OBJ_TAG)
 245                return 0;
 246
 247        /* We need to do this the hard way... */
 248        obj = deref_tag(r, parse_object(r, oid), NULL, 0);
 249        if (obj && obj->type == OBJ_COMMIT)
 250                return 1;
 251        return 0;
 252}
 253
 254static int disambiguate_tree_only(struct repository *r,
 255                                  const struct object_id *oid,
 256                                  void *cb_data_unused)
 257{
 258        int kind = oid_object_info(r, oid, NULL);
 259        return kind == OBJ_TREE;
 260}
 261
 262static int disambiguate_treeish_only(struct repository *r,
 263                                     const struct object_id *oid,
 264                                     void *cb_data_unused)
 265{
 266        struct object *obj;
 267        int kind;
 268
 269        kind = oid_object_info(r, oid, NULL);
 270        if (kind == OBJ_TREE || kind == OBJ_COMMIT)
 271                return 1;
 272        if (kind != OBJ_TAG)
 273                return 0;
 274
 275        /* We need to do this the hard way... */
 276        obj = deref_tag(r, parse_object(r, oid), NULL, 0);
 277        if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT))
 278                return 1;
 279        return 0;
 280}
 281
 282static int disambiguate_blob_only(struct repository *r,
 283                                  const struct object_id *oid,
 284                                  void *cb_data_unused)
 285{
 286        int kind = oid_object_info(r, oid, NULL);
 287        return kind == OBJ_BLOB;
 288}
 289
 290static disambiguate_hint_fn default_disambiguate_hint;
 291
 292int set_disambiguate_hint_config(const char *var, const char *value)
 293{
 294        static const struct {
 295                const char *name;
 296                disambiguate_hint_fn fn;
 297        } hints[] = {
 298                { "none", NULL },
 299                { "commit", disambiguate_commit_only },
 300                { "committish", disambiguate_committish_only },
 301                { "tree", disambiguate_tree_only },
 302                { "treeish", disambiguate_treeish_only },
 303                { "blob", disambiguate_blob_only }
 304        };
 305        int i;
 306
 307        if (!value)
 308                return config_error_nonbool(var);
 309
 310        for (i = 0; i < ARRAY_SIZE(hints); i++) {
 311                if (!strcasecmp(value, hints[i].name)) {
 312                        default_disambiguate_hint = hints[i].fn;
 313                        return 0;
 314                }
 315        }
 316
 317        return error("unknown hint type for '%s': %s", var, value);
 318}
 319
 320static int init_object_disambiguation(struct repository *r,
 321                                      const char *name, int len,
 322                                      struct disambiguate_state *ds)
 323{
 324        int i;
 325
 326        if (len < MINIMUM_ABBREV || len > the_hash_algo->hexsz)
 327                return -1;
 328
 329        memset(ds, 0, sizeof(*ds));
 330
 331        for (i = 0; i < len ;i++) {
 332                unsigned char c = name[i];
 333                unsigned char val;
 334                if (c >= '0' && c <= '9')
 335                        val = c - '0';
 336                else if (c >= 'a' && c <= 'f')
 337                        val = c - 'a' + 10;
 338                else if (c >= 'A' && c <='F') {
 339                        val = c - 'A' + 10;
 340                        c -= 'A' - 'a';
 341                }
 342                else
 343                        return -1;
 344                ds->hex_pfx[i] = c;
 345                if (!(i & 1))
 346                        val <<= 4;
 347                ds->bin_pfx.hash[i >> 1] |= val;
 348        }
 349
 350        ds->len = len;
 351        ds->hex_pfx[len] = '\0';
 352        ds->repo = r;
 353        prepare_alt_odb(r);
 354        return 0;
 355}
 356
 357static int show_ambiguous_object(const struct object_id *oid, void *data)
 358{
 359        const struct disambiguate_state *ds = data;
 360        struct strbuf desc = STRBUF_INIT;
 361        int type;
 362
 363        if (ds->fn && !ds->fn(ds->repo, oid, ds->cb_data))
 364                return 0;
 365
 366        type = oid_object_info(ds->repo, oid, NULL);
 367        if (type == OBJ_COMMIT) {
 368                struct commit *commit = lookup_commit(ds->repo, oid);
 369                if (commit) {
 370                        struct pretty_print_context pp = {0};
 371                        pp.date_mode.type = DATE_SHORT;
 372                        format_commit_message(commit, " %ad - %s", &desc, &pp);
 373                }
 374        } else if (type == OBJ_TAG) {
 375                struct tag *tag = lookup_tag(ds->repo, oid);
 376                if (!parse_tag(tag) && tag->tag)
 377                        strbuf_addf(&desc, " %s", tag->tag);
 378        }
 379
 380        advise("  %s %s%s",
 381               repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV),
 382               type_name(type) ? type_name(type) : "unknown type",
 383               desc.buf);
 384
 385        strbuf_release(&desc);
 386        return 0;
 387}
 388
 389static int collect_ambiguous(const struct object_id *oid, void *data)
 390{
 391        oid_array_append(data, oid);
 392        return 0;
 393}
 394
 395static int repo_collect_ambiguous(struct repository *r,
 396                                  const struct object_id *oid,
 397                                  void *data)
 398{
 399        return collect_ambiguous(oid, data);
 400}
 401
 402static struct repository *sort_ambiguous_repo;
 403static int sort_ambiguous(const void *a, const void *b)
 404{
 405        int a_type = oid_object_info(sort_ambiguous_repo, a, NULL);
 406        int b_type = oid_object_info(sort_ambiguous_repo, b, NULL);
 407        int a_type_sort;
 408        int b_type_sort;
 409
 410        /*
 411         * Sorts by hash within the same object type, just as
 412         * oid_array_for_each_unique() would do.
 413         */
 414        if (a_type == b_type)
 415                return oidcmp(a, b);
 416
 417        /*
 418         * Between object types show tags, then commits, and finally
 419         * trees and blobs.
 420         *
 421         * The object_type enum is commit, tree, blob, tag, but we
 422         * want tag, commit, tree blob. Cleverly (perhaps too
 423         * cleverly) do that with modulus, since the enum assigns 1 to
 424         * commit, so tag becomes 0.
 425         */
 426        a_type_sort = a_type % 4;
 427        b_type_sort = b_type % 4;
 428        return a_type_sort > b_type_sort ? 1 : -1;
 429}
 430
 431static void sort_ambiguous_oid_array(struct repository *r, struct oid_array *a)
 432{
 433        /* mutex will be needed if this code is to be made thread safe */
 434        sort_ambiguous_repo = r;
 435        QSORT(a->oid, a->nr, sort_ambiguous);
 436        sort_ambiguous_repo = NULL;
 437}
 438
 439static enum get_oid_result get_short_oid(const char *name, int len,
 440                                         struct object_id *oid,
 441                                         unsigned flags)
 442{
 443        int status;
 444        struct disambiguate_state ds;
 445        int quietly = !!(flags & GET_OID_QUIETLY);
 446
 447        if (init_object_disambiguation(the_repository, name, len, &ds) < 0)
 448                return -1;
 449
 450        if (HAS_MULTI_BITS(flags & GET_OID_DISAMBIGUATORS))
 451                BUG("multiple get_short_oid disambiguator flags");
 452
 453        if (flags & GET_OID_COMMIT)
 454                ds.fn = disambiguate_commit_only;
 455        else if (flags & GET_OID_COMMITTISH)
 456                ds.fn = disambiguate_committish_only;
 457        else if (flags & GET_OID_TREE)
 458                ds.fn = disambiguate_tree_only;
 459        else if (flags & GET_OID_TREEISH)
 460                ds.fn = disambiguate_treeish_only;
 461        else if (flags & GET_OID_BLOB)
 462                ds.fn = disambiguate_blob_only;
 463        else
 464                ds.fn = default_disambiguate_hint;
 465
 466        find_short_object_filename(&ds);
 467        find_short_packed_object(&ds);
 468        status = finish_object_disambiguation(&ds, oid);
 469
 470        if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) {
 471                struct oid_array collect = OID_ARRAY_INIT;
 472
 473                error(_("short SHA1 %s is ambiguous"), ds.hex_pfx);
 474
 475                /*
 476                 * We may still have ambiguity if we simply saw a series of
 477                 * candidates that did not satisfy our hint function. In
 478                 * that case, we still want to show them, so disable the hint
 479                 * function entirely.
 480                 */
 481                if (!ds.ambiguous)
 482                        ds.fn = NULL;
 483
 484                advise(_("The candidates are:"));
 485                for_each_abbrev(ds.hex_pfx, collect_ambiguous, &collect);
 486                sort_ambiguous_oid_array(the_repository, &collect);
 487
 488                if (oid_array_for_each(&collect, show_ambiguous_object, &ds))
 489                        BUG("show_ambiguous_object shouldn't return non-zero");
 490                oid_array_clear(&collect);
 491        }
 492
 493        return status;
 494}
 495
 496int repo_for_each_abbrev(struct repository *r, const char *prefix,
 497                         each_abbrev_fn fn, void *cb_data)
 498{
 499        struct oid_array collect = OID_ARRAY_INIT;
 500        struct disambiguate_state ds;
 501        int ret;
 502
 503        if (init_object_disambiguation(r, prefix, strlen(prefix), &ds) < 0)
 504                return -1;
 505
 506        ds.always_call_fn = 1;
 507        ds.fn = repo_collect_ambiguous;
 508        ds.cb_data = &collect;
 509        find_short_object_filename(&ds);
 510        find_short_packed_object(&ds);
 511
 512        ret = oid_array_for_each_unique(&collect, fn, cb_data);
 513        oid_array_clear(&collect);
 514        return ret;
 515}
 516
 517/*
 518 * Return the slot of the most-significant bit set in "val". There are various
 519 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
 520 * probably not a big deal here.
 521 */
 522static unsigned msb(unsigned long val)
 523{
 524        unsigned r = 0;
 525        while (val >>= 1)
 526                r++;
 527        return r;
 528}
 529
 530struct min_abbrev_data {
 531        unsigned int init_len;
 532        unsigned int cur_len;
 533        char *hex;
 534        struct repository *repo;
 535        const struct object_id *oid;
 536};
 537
 538static inline char get_hex_char_from_oid(const struct object_id *oid,
 539                                         unsigned int pos)
 540{
 541        static const char hex[] = "0123456789abcdef";
 542
 543        if ((pos & 1) == 0)
 544                return hex[oid->hash[pos >> 1] >> 4];
 545        else
 546                return hex[oid->hash[pos >> 1] & 0xf];
 547}
 548
 549static int extend_abbrev_len(const struct object_id *oid, void *cb_data)
 550{
 551        struct min_abbrev_data *mad = cb_data;
 552
 553        unsigned int i = mad->init_len;
 554        while (mad->hex[i] && mad->hex[i] == get_hex_char_from_oid(oid, i))
 555                i++;
 556
 557        if (i < GIT_MAX_RAWSZ && i >= mad->cur_len)
 558                mad->cur_len = i + 1;
 559
 560        return 0;
 561}
 562
 563static int repo_extend_abbrev_len(struct repository *r,
 564                                  const struct object_id *oid,
 565                                  void *cb_data)
 566{
 567        return extend_abbrev_len(oid, cb_data);
 568}
 569
 570static void find_abbrev_len_for_midx(struct multi_pack_index *m,
 571                                     struct min_abbrev_data *mad)
 572{
 573        int match = 0;
 574        uint32_t num, first = 0;
 575        struct object_id oid;
 576        const struct object_id *mad_oid;
 577
 578        if (!m->num_objects)
 579                return;
 580
 581        num = m->num_objects;
 582        mad_oid = mad->oid;
 583        match = bsearch_midx(mad_oid, m, &first);
 584
 585        /*
 586         * first is now the position in the packfile where we would insert
 587         * mad->hash if it does not exist (or the position of mad->hash if
 588         * it does exist). Hence, we consider a maximum of two objects
 589         * nearby for the abbreviation length.
 590         */
 591        mad->init_len = 0;
 592        if (!match) {
 593                if (nth_midxed_object_oid(&oid, m, first))
 594                        extend_abbrev_len(&oid, mad);
 595        } else if (first < num - 1) {
 596                if (nth_midxed_object_oid(&oid, m, first + 1))
 597                        extend_abbrev_len(&oid, mad);
 598        }
 599        if (first > 0) {
 600                if (nth_midxed_object_oid(&oid, m, first - 1))
 601                        extend_abbrev_len(&oid, mad);
 602        }
 603        mad->init_len = mad->cur_len;
 604}
 605
 606static void find_abbrev_len_for_pack(struct packed_git *p,
 607                                     struct min_abbrev_data *mad)
 608{
 609        int match = 0;
 610        uint32_t num, first = 0;
 611        struct object_id oid;
 612        const struct object_id *mad_oid;
 613
 614        if (open_pack_index(p) || !p->num_objects)
 615                return;
 616
 617        num = p->num_objects;
 618        mad_oid = mad->oid;
 619        match = bsearch_pack(mad_oid, p, &first);
 620
 621        /*
 622         * first is now the position in the packfile where we would insert
 623         * mad->hash if it does not exist (or the position of mad->hash if
 624         * it does exist). Hence, we consider a maximum of two objects
 625         * nearby for the abbreviation length.
 626         */
 627        mad->init_len = 0;
 628        if (!match) {
 629                if (nth_packed_object_oid(&oid, p, first))
 630                        extend_abbrev_len(&oid, mad);
 631        } else if (first < num - 1) {
 632                if (nth_packed_object_oid(&oid, p, first + 1))
 633                        extend_abbrev_len(&oid, mad);
 634        }
 635        if (first > 0) {
 636                if (nth_packed_object_oid(&oid, p, first - 1))
 637                        extend_abbrev_len(&oid, mad);
 638        }
 639        mad->init_len = mad->cur_len;
 640}
 641
 642static void find_abbrev_len_packed(struct min_abbrev_data *mad)
 643{
 644        struct multi_pack_index *m;
 645        struct packed_git *p;
 646
 647        for (m = get_multi_pack_index(mad->repo); m; m = m->next)
 648                find_abbrev_len_for_midx(m, mad);
 649        for (p = get_packed_git(mad->repo); p; p = p->next)
 650                find_abbrev_len_for_pack(p, mad);
 651}
 652
 653int repo_find_unique_abbrev_r(struct repository *r, char *hex,
 654                              const struct object_id *oid, int len)
 655{
 656        struct disambiguate_state ds;
 657        struct min_abbrev_data mad;
 658        struct object_id oid_ret;
 659        const unsigned hexsz = r->hash_algo->hexsz;
 660
 661        if (len < 0) {
 662                unsigned long count = repo_approximate_object_count(r);
 663                /*
 664                 * Add one because the MSB only tells us the highest bit set,
 665                 * not including the value of all the _other_ bits (so "15"
 666                 * is only one off of 2^4, but the MSB is the 3rd bit.
 667                 */
 668                len = msb(count) + 1;
 669                /*
 670                 * We now know we have on the order of 2^len objects, which
 671                 * expects a collision at 2^(len/2). But we also care about hex
 672                 * chars, not bits, and there are 4 bits per hex. So all
 673                 * together we need to divide by 2 and round up.
 674                 */
 675                len = DIV_ROUND_UP(len, 2);
 676                /*
 677                 * For very small repos, we stick with our regular fallback.
 678                 */
 679                if (len < FALLBACK_DEFAULT_ABBREV)
 680                        len = FALLBACK_DEFAULT_ABBREV;
 681        }
 682
 683        oid_to_hex_r(hex, oid);
 684        if (len == hexsz || !len)
 685                return hexsz;
 686
 687        mad.repo = r;
 688        mad.init_len = len;
 689        mad.cur_len = len;
 690        mad.hex = hex;
 691        mad.oid = oid;
 692
 693        find_abbrev_len_packed(&mad);
 694
 695        if (init_object_disambiguation(r, hex, mad.cur_len, &ds) < 0)
 696                return -1;
 697
 698        ds.fn = repo_extend_abbrev_len;
 699        ds.always_call_fn = 1;
 700        ds.cb_data = (void *)&mad;
 701
 702        find_short_object_filename(&ds);
 703        (void)finish_object_disambiguation(&ds, &oid_ret);
 704
 705        hex[mad.cur_len] = 0;
 706        return mad.cur_len;
 707}
 708
 709const char *repo_find_unique_abbrev(struct repository *r,
 710                                    const struct object_id *oid,
 711                                    int len)
 712{
 713        static int bufno;
 714        static char hexbuffer[4][GIT_MAX_HEXSZ + 1];
 715        char *hex = hexbuffer[bufno];
 716        bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
 717        repo_find_unique_abbrev_r(r, hex, oid, len);
 718        return hex;
 719}
 720
 721static int ambiguous_path(const char *path, int len)
 722{
 723        int slash = 1;
 724        int cnt;
 725
 726        for (cnt = 0; cnt < len; cnt++) {
 727                switch (*path++) {
 728                case '\0':
 729                        break;
 730                case '/':
 731                        if (slash)
 732                                break;
 733                        slash = 1;
 734                        continue;
 735                case '.':
 736                        continue;
 737                default:
 738                        slash = 0;
 739                        continue;
 740                }
 741                break;
 742        }
 743        return slash;
 744}
 745
 746static inline int at_mark(const char *string, int len,
 747                          const char **suffix, int nr)
 748{
 749        int i;
 750
 751        for (i = 0; i < nr; i++) {
 752                int suffix_len = strlen(suffix[i]);
 753                if (suffix_len <= len
 754                    && !strncasecmp(string, suffix[i], suffix_len))
 755                        return suffix_len;
 756        }
 757        return 0;
 758}
 759
 760static inline int upstream_mark(const char *string, int len)
 761{
 762        const char *suffix[] = { "@{upstream}", "@{u}" };
 763        return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
 764}
 765
 766static inline int push_mark(const char *string, int len)
 767{
 768        const char *suffix[] = { "@{push}" };
 769        return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
 770}
 771
 772static enum get_oid_result get_oid_1(const char *name, int len, struct object_id *oid, unsigned lookup_flags);
 773static int interpret_nth_prior_checkout(const char *name, int namelen, struct strbuf *buf);
 774
 775static int get_oid_basic(const char *str, int len, struct object_id *oid,
 776                          unsigned int flags)
 777{
 778        static const char *warn_msg = "refname '%.*s' is ambiguous.";
 779        static const char *object_name_msg = N_(
 780        "Git normally never creates a ref that ends with 40 hex characters\n"
 781        "because it will be ignored when you just specify 40-hex. These refs\n"
 782        "may be created by mistake. For example,\n"
 783        "\n"
 784        "  git checkout -b $br $(git rev-parse ...)\n"
 785        "\n"
 786        "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
 787        "examine these refs and maybe delete them. Turn this message off by\n"
 788        "running \"git config advice.objectNameWarning false\"");
 789        struct object_id tmp_oid;
 790        char *real_ref = NULL;
 791        int refs_found = 0;
 792        int at, reflog_len, nth_prior = 0;
 793
 794        if (len == the_hash_algo->hexsz && !get_oid_hex(str, oid)) {
 795                if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
 796                        refs_found = dwim_ref(str, len, &tmp_oid, &real_ref);
 797                        if (refs_found > 0) {
 798                                warning(warn_msg, len, str);
 799                                if (advice_object_name_warning)
 800                                        fprintf(stderr, "%s\n", _(object_name_msg));
 801                        }
 802                        free(real_ref);
 803                }
 804                return 0;
 805        }
 806
 807        /* basic@{time or number or -number} format to query ref-log */
 808        reflog_len = at = 0;
 809        if (len && str[len-1] == '}') {
 810                for (at = len-4; at >= 0; at--) {
 811                        if (str[at] == '@' && str[at+1] == '{') {
 812                                if (str[at+2] == '-') {
 813                                        if (at != 0)
 814                                                /* @{-N} not at start */
 815                                                return -1;
 816                                        nth_prior = 1;
 817                                        continue;
 818                                }
 819                                if (!upstream_mark(str + at, len - at) &&
 820                                    !push_mark(str + at, len - at)) {
 821                                        reflog_len = (len-1) - (at+2);
 822                                        len = at;
 823                                }
 824                                break;
 825                        }
 826                }
 827        }
 828
 829        /* Accept only unambiguous ref paths. */
 830        if (len && ambiguous_path(str, len))
 831                return -1;
 832
 833        if (nth_prior) {
 834                struct strbuf buf = STRBUF_INIT;
 835                int detached;
 836
 837                if (interpret_nth_prior_checkout(str, len, &buf) > 0) {
 838                        detached = (buf.len == the_hash_algo->hexsz && !get_oid_hex(buf.buf, oid));
 839                        strbuf_release(&buf);
 840                        if (detached)
 841                                return 0;
 842                }
 843        }
 844
 845        if (!len && reflog_len)
 846                /* allow "@{...}" to mean the current branch reflog */
 847                refs_found = dwim_ref("HEAD", 4, oid, &real_ref);
 848        else if (reflog_len)
 849                refs_found = dwim_log(str, len, oid, &real_ref);
 850        else
 851                refs_found = dwim_ref(str, len, oid, &real_ref);
 852
 853        if (!refs_found)
 854                return -1;
 855
 856        if (warn_ambiguous_refs && !(flags & GET_OID_QUIETLY) &&
 857            (refs_found > 1 ||
 858             !get_short_oid(str, len, &tmp_oid, GET_OID_QUIETLY)))
 859                warning(warn_msg, len, str);
 860
 861        if (reflog_len) {
 862                int nth, i;
 863                timestamp_t at_time;
 864                timestamp_t co_time;
 865                int co_tz, co_cnt;
 866
 867                /* Is it asking for N-th entry, or approxidate? */
 868                for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
 869                        char ch = str[at+2+i];
 870                        if ('0' <= ch && ch <= '9')
 871                                nth = nth * 10 + ch - '0';
 872                        else
 873                                nth = -1;
 874                }
 875                if (100000000 <= nth) {
 876                        at_time = nth;
 877                        nth = -1;
 878                } else if (0 <= nth)
 879                        at_time = 0;
 880                else {
 881                        int errors = 0;
 882                        char *tmp = xstrndup(str + at + 2, reflog_len);
 883                        at_time = approxidate_careful(tmp, &errors);
 884                        free(tmp);
 885                        if (errors) {
 886                                free(real_ref);
 887                                return -1;
 888                        }
 889                }
 890                if (read_ref_at(get_main_ref_store(the_repository),
 891                                real_ref, flags, at_time, nth, oid, NULL,
 892                                &co_time, &co_tz, &co_cnt)) {
 893                        if (!len) {
 894                                if (starts_with(real_ref, "refs/heads/")) {
 895                                        str = real_ref + 11;
 896                                        len = strlen(real_ref + 11);
 897                                } else {
 898                                        /* detached HEAD */
 899                                        str = "HEAD";
 900                                        len = 4;
 901                                }
 902                        }
 903                        if (at_time) {
 904                                if (!(flags & GET_OID_QUIETLY)) {
 905                                        warning("Log for '%.*s' only goes "
 906                                                "back to %s.", len, str,
 907                                                show_date(co_time, co_tz, DATE_MODE(RFC2822)));
 908                                }
 909                        } else {
 910                                if (flags & GET_OID_QUIETLY) {
 911                                        exit(128);
 912                                }
 913                                die("Log for '%.*s' only has %d entries.",
 914                                    len, str, co_cnt);
 915                        }
 916                }
 917        }
 918
 919        free(real_ref);
 920        return 0;
 921}
 922
 923static enum get_oid_result get_parent(const char *name, int len,
 924                                      struct object_id *result, int idx)
 925{
 926        struct object_id oid;
 927        enum get_oid_result ret = get_oid_1(name, len, &oid,
 928                                            GET_OID_COMMITTISH);
 929        struct commit *commit;
 930        struct commit_list *p;
 931
 932        if (ret)
 933                return ret;
 934        commit = lookup_commit_reference(the_repository, &oid);
 935        if (parse_commit(commit))
 936                return MISSING_OBJECT;
 937        if (!idx) {
 938                oidcpy(result, &commit->object.oid);
 939                return FOUND;
 940        }
 941        p = commit->parents;
 942        while (p) {
 943                if (!--idx) {
 944                        oidcpy(result, &p->item->object.oid);
 945                        return FOUND;
 946                }
 947                p = p->next;
 948        }
 949        return MISSING_OBJECT;
 950}
 951
 952static enum get_oid_result get_nth_ancestor(const char *name, int len,
 953                                            struct object_id *result,
 954                                            int generation)
 955{
 956        struct object_id oid;
 957        struct commit *commit;
 958        int ret;
 959
 960        ret = get_oid_1(name, len, &oid, GET_OID_COMMITTISH);
 961        if (ret)
 962                return ret;
 963        commit = lookup_commit_reference(the_repository, &oid);
 964        if (!commit)
 965                return MISSING_OBJECT;
 966
 967        while (generation--) {
 968                if (parse_commit(commit) || !commit->parents)
 969                        return MISSING_OBJECT;
 970                commit = commit->parents->item;
 971        }
 972        oidcpy(result, &commit->object.oid);
 973        return FOUND;
 974}
 975
 976struct object *peel_to_type(const char *name, int namelen,
 977                            struct object *o, enum object_type expected_type)
 978{
 979        if (name && !namelen)
 980                namelen = strlen(name);
 981        while (1) {
 982                if (!o || (!o->parsed && !parse_object(the_repository, &o->oid)))
 983                        return NULL;
 984                if (expected_type == OBJ_ANY || o->type == expected_type)
 985                        return o;
 986                if (o->type == OBJ_TAG)
 987                        o = ((struct tag*) o)->tagged;
 988                else if (o->type == OBJ_COMMIT)
 989                        o = &(get_commit_tree(((struct commit *)o))->object);
 990                else {
 991                        if (name)
 992                                error("%.*s: expected %s type, but the object "
 993                                      "dereferences to %s type",
 994                                      namelen, name, type_name(expected_type),
 995                                      type_name(o->type));
 996                        return NULL;
 997                }
 998        }
 999}
1000
1001static int peel_onion(const char *name, int len, struct object_id *oid,
1002                      unsigned lookup_flags)
1003{
1004        struct object_id outer;
1005        const char *sp;
1006        unsigned int expected_type = 0;
1007        struct object *o;
1008
1009        /*
1010         * "ref^{type}" dereferences ref repeatedly until you cannot
1011         * dereference anymore, or you get an object of given type,
1012         * whichever comes first.  "ref^{}" means just dereference
1013         * tags until you get a non-tag.  "ref^0" is a shorthand for
1014         * "ref^{commit}".  "commit^{tree}" could be used to find the
1015         * top-level tree of the given commit.
1016         */
1017        if (len < 4 || name[len-1] != '}')
1018                return -1;
1019
1020        for (sp = name + len - 1; name <= sp; sp--) {
1021                int ch = *sp;
1022                if (ch == '{' && name < sp && sp[-1] == '^')
1023                        break;
1024        }
1025        if (sp <= name)
1026                return -1;
1027
1028        sp++; /* beginning of type name, or closing brace for empty */
1029        if (starts_with(sp, "commit}"))
1030                expected_type = OBJ_COMMIT;
1031        else if (starts_with(sp, "tag}"))
1032                expected_type = OBJ_TAG;
1033        else if (starts_with(sp, "tree}"))
1034                expected_type = OBJ_TREE;
1035        else if (starts_with(sp, "blob}"))
1036                expected_type = OBJ_BLOB;
1037        else if (starts_with(sp, "object}"))
1038                expected_type = OBJ_ANY;
1039        else if (sp[0] == '}')
1040                expected_type = OBJ_NONE;
1041        else if (sp[0] == '/')
1042                expected_type = OBJ_COMMIT;
1043        else
1044                return -1;
1045
1046        lookup_flags &= ~GET_OID_DISAMBIGUATORS;
1047        if (expected_type == OBJ_COMMIT)
1048                lookup_flags |= GET_OID_COMMITTISH;
1049        else if (expected_type == OBJ_TREE)
1050                lookup_flags |= GET_OID_TREEISH;
1051
1052        if (get_oid_1(name, sp - name - 2, &outer, lookup_flags))
1053                return -1;
1054
1055        o = parse_object(the_repository, &outer);
1056        if (!o)
1057                return -1;
1058        if (!expected_type) {
1059                o = deref_tag(the_repository, o, name, sp - name - 2);
1060                if (!o || (!o->parsed && !parse_object(the_repository, &o->oid)))
1061                        return -1;
1062                oidcpy(oid, &o->oid);
1063                return 0;
1064        }
1065
1066        /*
1067         * At this point, the syntax look correct, so
1068         * if we do not get the needed object, we should
1069         * barf.
1070         */
1071        o = peel_to_type(name, len, o, expected_type);
1072        if (!o)
1073                return -1;
1074
1075        oidcpy(oid, &o->oid);
1076        if (sp[0] == '/') {
1077                /* "$commit^{/foo}" */
1078                char *prefix;
1079                int ret;
1080                struct commit_list *list = NULL;
1081
1082                /*
1083                 * $commit^{/}. Some regex implementation may reject.
1084                 * We don't need regex anyway. '' pattern always matches.
1085                 */
1086                if (sp[1] == '}')
1087                        return 0;
1088
1089                prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
1090                commit_list_insert((struct commit *)o, &list);
1091                ret = get_oid_oneline(prefix, oid, list);
1092                free(prefix);
1093                return ret;
1094        }
1095        return 0;
1096}
1097
1098static int get_describe_name(const char *name, int len, struct object_id *oid)
1099{
1100        const char *cp;
1101        unsigned flags = GET_OID_QUIETLY | GET_OID_COMMIT;
1102
1103        for (cp = name + len - 1; name + 2 <= cp; cp--) {
1104                char ch = *cp;
1105                if (!isxdigit(ch)) {
1106                        /* We must be looking at g in "SOMETHING-g"
1107                         * for it to be describe output.
1108                         */
1109                        if (ch == 'g' && cp[-1] == '-') {
1110                                cp++;
1111                                len -= cp - name;
1112                                return get_short_oid(cp, len, oid, flags);
1113                        }
1114                }
1115        }
1116        return -1;
1117}
1118
1119static enum get_oid_result get_oid_1(const char *name, int len,
1120                                     struct object_id *oid,
1121                                     unsigned lookup_flags)
1122{
1123        int ret, has_suffix;
1124        const char *cp;
1125
1126        /*
1127         * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1128         */
1129        has_suffix = 0;
1130        for (cp = name + len - 1; name <= cp; cp--) {
1131                int ch = *cp;
1132                if ('0' <= ch && ch <= '9')
1133                        continue;
1134                if (ch == '~' || ch == '^')
1135                        has_suffix = ch;
1136                break;
1137        }
1138
1139        if (has_suffix) {
1140                int num = 0;
1141                int len1 = cp - name;
1142                cp++;
1143                while (cp < name + len)
1144                        num = num * 10 + *cp++ - '0';
1145                if (!num && len1 == len - 1)
1146                        num = 1;
1147                if (has_suffix == '^')
1148                        return get_parent(name, len1, oid, num);
1149                /* else if (has_suffix == '~') -- goes without saying */
1150                return get_nth_ancestor(name, len1, oid, num);
1151        }
1152
1153        ret = peel_onion(name, len, oid, lookup_flags);
1154        if (!ret)
1155                return FOUND;
1156
1157        ret = get_oid_basic(name, len, oid, lookup_flags);
1158        if (!ret)
1159                return FOUND;
1160
1161        /* It could be describe output that is "SOMETHING-gXXXX" */
1162        ret = get_describe_name(name, len, oid);
1163        if (!ret)
1164                return FOUND;
1165
1166        return get_short_oid(name, len, oid, lookup_flags);
1167}
1168
1169/*
1170 * This interprets names like ':/Initial revision of "git"' by searching
1171 * through history and returning the first commit whose message starts
1172 * the given regular expression.
1173 *
1174 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1175 *
1176 * For a literal '!' character at the beginning of a pattern, you have to repeat
1177 * that, like: ':/!!foo'
1178 *
1179 * For future extension, all other sequences beginning with ':/!' are reserved.
1180 */
1181
1182/* Remember to update object flag allocation in object.h */
1183#define ONELINE_SEEN (1u<<20)
1184
1185static int handle_one_ref(const char *path, const struct object_id *oid,
1186                          int flag, void *cb_data)
1187{
1188        struct commit_list **list = cb_data;
1189        struct object *object = parse_object(the_repository, oid);
1190        if (!object)
1191                return 0;
1192        if (object->type == OBJ_TAG) {
1193                object = deref_tag(the_repository, object, path,
1194                                   strlen(path));
1195                if (!object)
1196                        return 0;
1197        }
1198        if (object->type != OBJ_COMMIT)
1199                return 0;
1200        commit_list_insert((struct commit *)object, list);
1201        return 0;
1202}
1203
1204static int get_oid_oneline(const char *prefix, struct object_id *oid,
1205                            struct commit_list *list)
1206{
1207        struct commit_list *backup = NULL, *l;
1208        int found = 0;
1209        int negative = 0;
1210        regex_t regex;
1211
1212        if (prefix[0] == '!') {
1213                prefix++;
1214
1215                if (prefix[0] == '-') {
1216                        prefix++;
1217                        negative = 1;
1218                } else if (prefix[0] != '!') {
1219                        return -1;
1220                }
1221        }
1222
1223        if (regcomp(&regex, prefix, REG_EXTENDED))
1224                return -1;
1225
1226        for (l = list; l; l = l->next) {
1227                l->item->object.flags |= ONELINE_SEEN;
1228                commit_list_insert(l->item, &backup);
1229        }
1230        while (list) {
1231                const char *p, *buf;
1232                struct commit *commit;
1233                int matches;
1234
1235                commit = pop_most_recent_commit(&list, ONELINE_SEEN);
1236                if (!parse_object(the_repository, &commit->object.oid))
1237                        continue;
1238                buf = get_commit_buffer(commit, NULL);
1239                p = strstr(buf, "\n\n");
1240                matches = negative ^ (p && !regexec(&regex, p + 2, 0, NULL, 0));
1241                unuse_commit_buffer(commit, buf);
1242
1243                if (matches) {
1244                        oidcpy(oid, &commit->object.oid);
1245                        found = 1;
1246                        break;
1247                }
1248        }
1249        regfree(&regex);
1250        free_commit_list(list);
1251        for (l = backup; l; l = l->next)
1252                clear_commit_marks(l->item, ONELINE_SEEN);
1253        free_commit_list(backup);
1254        return found ? 0 : -1;
1255}
1256
1257struct grab_nth_branch_switch_cbdata {
1258        int remaining;
1259        struct strbuf buf;
1260};
1261
1262static int grab_nth_branch_switch(struct object_id *ooid, struct object_id *noid,
1263                                  const char *email, timestamp_t timestamp, int tz,
1264                                  const char *message, void *cb_data)
1265{
1266        struct grab_nth_branch_switch_cbdata *cb = cb_data;
1267        const char *match = NULL, *target = NULL;
1268        size_t len;
1269
1270        if (skip_prefix(message, "checkout: moving from ", &match))
1271                target = strstr(match, " to ");
1272
1273        if (!match || !target)
1274                return 0;
1275        if (--(cb->remaining) == 0) {
1276                len = target - match;
1277                strbuf_reset(&cb->buf);
1278                strbuf_add(&cb->buf, match, len);
1279                return 1; /* we are done */
1280        }
1281        return 0;
1282}
1283
1284/*
1285 * Parse @{-N} syntax, return the number of characters parsed
1286 * if successful; otherwise signal an error with negative value.
1287 */
1288static int interpret_nth_prior_checkout(const char *name, int namelen,
1289                                        struct strbuf *buf)
1290{
1291        long nth;
1292        int retval;
1293        struct grab_nth_branch_switch_cbdata cb;
1294        const char *brace;
1295        char *num_end;
1296
1297        if (namelen < 4)
1298                return -1;
1299        if (name[0] != '@' || name[1] != '{' || name[2] != '-')
1300                return -1;
1301        brace = memchr(name, '}', namelen);
1302        if (!brace)
1303                return -1;
1304        nth = strtol(name + 3, &num_end, 10);
1305        if (num_end != brace)
1306                return -1;
1307        if (nth <= 0)
1308                return -1;
1309        cb.remaining = nth;
1310        strbuf_init(&cb.buf, 20);
1311
1312        retval = 0;
1313        if (0 < for_each_reflog_ent_reverse("HEAD", grab_nth_branch_switch, &cb)) {
1314                strbuf_reset(buf);
1315                strbuf_addbuf(buf, &cb.buf);
1316                retval = brace - name + 1;
1317        }
1318
1319        strbuf_release(&cb.buf);
1320        return retval;
1321}
1322
1323int get_oid_mb(const char *name, struct object_id *oid)
1324{
1325        struct commit *one, *two;
1326        struct commit_list *mbs;
1327        struct object_id oid_tmp;
1328        const char *dots;
1329        int st;
1330
1331        dots = strstr(name, "...");
1332        if (!dots)
1333                return get_oid(name, oid);
1334        if (dots == name)
1335                st = get_oid("HEAD", &oid_tmp);
1336        else {
1337                struct strbuf sb;
1338                strbuf_init(&sb, dots - name);
1339                strbuf_add(&sb, name, dots - name);
1340                st = get_oid_committish(sb.buf, &oid_tmp);
1341                strbuf_release(&sb);
1342        }
1343        if (st)
1344                return st;
1345        one = lookup_commit_reference_gently(the_repository, &oid_tmp, 0);
1346        if (!one)
1347                return -1;
1348
1349        if (get_oid_committish(dots[3] ? (dots + 3) : "HEAD", &oid_tmp))
1350                return -1;
1351        two = lookup_commit_reference_gently(the_repository, &oid_tmp, 0);
1352        if (!two)
1353                return -1;
1354        mbs = get_merge_bases(one, two);
1355        if (!mbs || mbs->next)
1356                st = -1;
1357        else {
1358                st = 0;
1359                oidcpy(oid, &mbs->item->object.oid);
1360        }
1361        free_commit_list(mbs);
1362        return st;
1363}
1364
1365/* parse @something syntax, when 'something' is not {.*} */
1366static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf)
1367{
1368        const char *next;
1369
1370        if (len || name[1] == '{')
1371                return -1;
1372
1373        /* make sure it's a single @, or @@{.*}, not @foo */
1374        next = memchr(name + len + 1, '@', namelen - len - 1);
1375        if (next && next[1] != '{')
1376                return -1;
1377        if (!next)
1378                next = name + namelen;
1379        if (next != name + 1)
1380                return -1;
1381
1382        strbuf_reset(buf);
1383        strbuf_add(buf, "HEAD", 4);
1384        return 1;
1385}
1386
1387static int reinterpret(const char *name, int namelen, int len,
1388                       struct strbuf *buf, unsigned allowed)
1389{
1390        /* we have extra data, which might need further processing */
1391        struct strbuf tmp = STRBUF_INIT;
1392        int used = buf->len;
1393        int ret;
1394
1395        strbuf_add(buf, name + len, namelen - len);
1396        ret = interpret_branch_name(buf->buf, buf->len, &tmp, allowed);
1397        /* that data was not interpreted, remove our cruft */
1398        if (ret < 0) {
1399                strbuf_setlen(buf, used);
1400                return len;
1401        }
1402        strbuf_reset(buf);
1403        strbuf_addbuf(buf, &tmp);
1404        strbuf_release(&tmp);
1405        /* tweak for size of {-N} versus expanded ref name */
1406        return ret - used + len;
1407}
1408
1409static void set_shortened_ref(struct strbuf *buf, const char *ref)
1410{
1411        char *s = shorten_unambiguous_ref(ref, 0);
1412        strbuf_reset(buf);
1413        strbuf_addstr(buf, s);
1414        free(s);
1415}
1416
1417static int branch_interpret_allowed(const char *refname, unsigned allowed)
1418{
1419        if (!allowed)
1420                return 1;
1421
1422        if ((allowed & INTERPRET_BRANCH_LOCAL) &&
1423            starts_with(refname, "refs/heads/"))
1424                return 1;
1425        if ((allowed & INTERPRET_BRANCH_REMOTE) &&
1426            starts_with(refname, "refs/remotes/"))
1427                return 1;
1428
1429        return 0;
1430}
1431
1432static int interpret_branch_mark(const char *name, int namelen,
1433                                 int at, struct strbuf *buf,
1434                                 int (*get_mark)(const char *, int),
1435                                 const char *(*get_data)(struct branch *,
1436                                                         struct strbuf *),
1437                                 unsigned allowed)
1438{
1439        int len;
1440        struct branch *branch;
1441        struct strbuf err = STRBUF_INIT;
1442        const char *value;
1443
1444        len = get_mark(name + at, namelen - at);
1445        if (!len)
1446                return -1;
1447
1448        if (memchr(name, ':', at))
1449                return -1;
1450
1451        if (at) {
1452                char *name_str = xmemdupz(name, at);
1453                branch = branch_get(name_str);
1454                free(name_str);
1455        } else
1456                branch = branch_get(NULL);
1457
1458        value = get_data(branch, &err);
1459        if (!value)
1460                die("%s", err.buf);
1461
1462        if (!branch_interpret_allowed(value, allowed))
1463                return -1;
1464
1465        set_shortened_ref(buf, value);
1466        return len + at;
1467}
1468
1469int repo_interpret_branch_name(struct repository *r,
1470                               const char *name, int namelen,
1471                               struct strbuf *buf,
1472                               unsigned allowed)
1473{
1474        char *at;
1475        const char *start;
1476        int len;
1477
1478        if (r != the_repository)
1479                BUG("interpret_branch_name() does not really use 'r' yet");
1480        if (!namelen)
1481                namelen = strlen(name);
1482
1483        if (!allowed || (allowed & INTERPRET_BRANCH_LOCAL)) {
1484                len = interpret_nth_prior_checkout(name, namelen, buf);
1485                if (!len) {
1486                        return len; /* syntax Ok, not enough switches */
1487                } else if (len > 0) {
1488                        if (len == namelen)
1489                                return len; /* consumed all */
1490                        else
1491                                return reinterpret(name, namelen, len, buf, allowed);
1492                }
1493        }
1494
1495        for (start = name;
1496             (at = memchr(start, '@', namelen - (start - name)));
1497             start = at + 1) {
1498
1499                if (!allowed || (allowed & INTERPRET_BRANCH_HEAD)) {
1500                        len = interpret_empty_at(name, namelen, at - name, buf);
1501                        if (len > 0)
1502                                return reinterpret(name, namelen, len, buf,
1503                                                   allowed);
1504                }
1505
1506                len = interpret_branch_mark(name, namelen, at - name, buf,
1507                                            upstream_mark, branch_get_upstream,
1508                                            allowed);
1509                if (len > 0)
1510                        return len;
1511
1512                len = interpret_branch_mark(name, namelen, at - name, buf,
1513                                            push_mark, branch_get_push,
1514                                            allowed);
1515                if (len > 0)
1516                        return len;
1517        }
1518
1519        return -1;
1520}
1521
1522void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
1523{
1524        int len = strlen(name);
1525        int used = interpret_branch_name(name, len, sb, allowed);
1526
1527        if (used < 0)
1528                used = 0;
1529        strbuf_add(sb, name + used, len - used);
1530}
1531
1532int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
1533{
1534        if (startup_info->have_repository)
1535                strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
1536        else
1537                strbuf_addstr(sb, name);
1538
1539        /*
1540         * This splice must be done even if we end up rejecting the
1541         * name; builtin/branch.c::copy_or_rename_branch() still wants
1542         * to see what the name expanded to so that "branch -m" can be
1543         * used as a tool to correct earlier mistakes.
1544         */
1545        strbuf_splice(sb, 0, 0, "refs/heads/", 11);
1546
1547        if (*name == '-' ||
1548            !strcmp(sb->buf, "refs/heads/HEAD"))
1549                return -1;
1550
1551        return check_refname_format(sb->buf, 0);
1552}
1553
1554/*
1555 * This is like "get_oid_basic()", except it allows "object ID expressions",
1556 * notably "xyz^" for "parent of xyz"
1557 */
1558int get_oid(const char *name, struct object_id *oid)
1559{
1560        struct object_context unused;
1561        return get_oid_with_context(the_repository, name, 0, oid, &unused);
1562}
1563
1564
1565/*
1566 * Many callers know that the user meant to name a commit-ish by
1567 * syntactical positions where the object name appears.  Calling this
1568 * function allows the machinery to disambiguate shorter-than-unique
1569 * abbreviated object names between commit-ish and others.
1570 *
1571 * Note that this does NOT error out when the named object is not a
1572 * commit-ish. It is merely to give a hint to the disambiguation
1573 * machinery.
1574 */
1575int get_oid_committish(const char *name, struct object_id *oid)
1576{
1577        struct object_context unused;
1578        return get_oid_with_context(the_repository,
1579                                    name, GET_OID_COMMITTISH,
1580                                    oid, &unused);
1581}
1582
1583int get_oid_treeish(const char *name, struct object_id *oid)
1584{
1585        struct object_context unused;
1586        return get_oid_with_context(the_repository,
1587                                    name, GET_OID_TREEISH,
1588                                    oid, &unused);
1589}
1590
1591int get_oid_commit(const char *name, struct object_id *oid)
1592{
1593        struct object_context unused;
1594        return get_oid_with_context(the_repository,
1595                                    name, GET_OID_COMMIT,
1596                                    oid, &unused);
1597}
1598
1599int get_oid_tree(const char *name, struct object_id *oid)
1600{
1601        struct object_context unused;
1602        return get_oid_with_context(the_repository,
1603                                    name, GET_OID_TREE,
1604                                    oid, &unused);
1605}
1606
1607int get_oid_blob(const char *name, struct object_id *oid)
1608{
1609        struct object_context unused;
1610        return get_oid_with_context(the_repository,
1611                                    name, GET_OID_BLOB,
1612                                    oid, &unused);
1613}
1614
1615/* Must be called only when object_name:filename doesn't exist. */
1616static void diagnose_invalid_oid_path(const char *prefix,
1617                                      const char *filename,
1618                                      const struct object_id *tree_oid,
1619                                      const char *object_name,
1620                                      int object_name_len)
1621{
1622        struct object_id oid;
1623        unsigned mode;
1624
1625        if (!prefix)
1626                prefix = "";
1627
1628        if (file_exists(filename))
1629                die("Path '%s' exists on disk, but not in '%.*s'.",
1630                    filename, object_name_len, object_name);
1631        if (is_missing_file_error(errno)) {
1632                char *fullname = xstrfmt("%s%s", prefix, filename);
1633
1634                if (!get_tree_entry(tree_oid, fullname, &oid, &mode)) {
1635                        die("Path '%s' exists, but not '%s'.\n"
1636                            "Did you mean '%.*s:%s' aka '%.*s:./%s'?",
1637                            fullname,
1638                            filename,
1639                            object_name_len, object_name,
1640                            fullname,
1641                            object_name_len, object_name,
1642                            filename);
1643                }
1644                die("Path '%s' does not exist in '%.*s'",
1645                    filename, object_name_len, object_name);
1646        }
1647}
1648
1649/* Must be called only when :stage:filename doesn't exist. */
1650static void diagnose_invalid_index_path(struct index_state *istate,
1651                                        int stage,
1652                                        const char *prefix,
1653                                        const char *filename)
1654{
1655        const struct cache_entry *ce;
1656        int pos;
1657        unsigned namelen = strlen(filename);
1658        struct strbuf fullname = STRBUF_INIT;
1659
1660        if (!prefix)
1661                prefix = "";
1662
1663        /* Wrong stage number? */
1664        pos = index_name_pos(istate, filename, namelen);
1665        if (pos < 0)
1666                pos = -pos - 1;
1667        if (pos < istate->cache_nr) {
1668                ce = istate->cache[pos];
1669                if (ce_namelen(ce) == namelen &&
1670                    !memcmp(ce->name, filename, namelen))
1671                        die("Path '%s' is in the index, but not at stage %d.\n"
1672                            "Did you mean ':%d:%s'?",
1673                            filename, stage,
1674                            ce_stage(ce), filename);
1675        }
1676
1677        /* Confusion between relative and absolute filenames? */
1678        strbuf_addstr(&fullname, prefix);
1679        strbuf_addstr(&fullname, filename);
1680        pos = index_name_pos(istate, fullname.buf, fullname.len);
1681        if (pos < 0)
1682                pos = -pos - 1;
1683        if (pos < istate->cache_nr) {
1684                ce = istate->cache[pos];
1685                if (ce_namelen(ce) == fullname.len &&
1686                    !memcmp(ce->name, fullname.buf, fullname.len))
1687                        die("Path '%s' is in the index, but not '%s'.\n"
1688                            "Did you mean ':%d:%s' aka ':%d:./%s'?",
1689                            fullname.buf, filename,
1690                            ce_stage(ce), fullname.buf,
1691                            ce_stage(ce), filename);
1692        }
1693
1694        if (file_exists(filename))
1695                die("Path '%s' exists on disk, but not in the index.", filename);
1696        if (is_missing_file_error(errno))
1697                die("Path '%s' does not exist (neither on disk nor in the index).",
1698                    filename);
1699
1700        strbuf_release(&fullname);
1701}
1702
1703
1704static char *resolve_relative_path(const char *rel)
1705{
1706        if (!starts_with(rel, "./") && !starts_with(rel, "../"))
1707                return NULL;
1708
1709        if (!is_inside_work_tree())
1710                die("relative path syntax can't be used outside working tree.");
1711
1712        /* die() inside prefix_path() if resolved path is outside worktree */
1713        return prefix_path(startup_info->prefix,
1714                           startup_info->prefix ? strlen(startup_info->prefix) : 0,
1715                           rel);
1716}
1717
1718static enum get_oid_result get_oid_with_context_1(struct repository *repo,
1719                                  const char *name,
1720                                  unsigned flags,
1721                                  const char *prefix,
1722                                  struct object_id *oid,
1723                                  struct object_context *oc)
1724{
1725        int ret, bracket_depth;
1726        int namelen = strlen(name);
1727        const char *cp;
1728        int only_to_die = flags & GET_OID_ONLY_TO_DIE;
1729
1730        if (only_to_die)
1731                flags |= GET_OID_QUIETLY;
1732
1733        memset(oc, 0, sizeof(*oc));
1734        oc->mode = S_IFINVALID;
1735        strbuf_init(&oc->symlink_path, 0);
1736        ret = get_oid_1(name, namelen, oid, flags);
1737        if (!ret)
1738                return ret;
1739        /*
1740         * sha1:path --> object name of path in ent sha1
1741         * :path -> object name of absolute path in index
1742         * :./path -> object name of path relative to cwd in index
1743         * :[0-3]:path -> object name of path in index at stage
1744         * :/foo -> recent commit matching foo
1745         */
1746        if (name[0] == ':') {
1747                int stage = 0;
1748                const struct cache_entry *ce;
1749                char *new_path = NULL;
1750                int pos;
1751                if (!only_to_die && namelen > 2 && name[1] == '/') {
1752                        struct commit_list *list = NULL;
1753
1754                        for_each_ref(handle_one_ref, &list);
1755                        head_ref(handle_one_ref, &list);
1756                        commit_list_sort_by_date(&list);
1757                        return get_oid_oneline(name + 2, oid, list);
1758                }
1759                if (namelen < 3 ||
1760                    name[2] != ':' ||
1761                    name[1] < '0' || '3' < name[1])
1762                        cp = name + 1;
1763                else {
1764                        stage = name[1] - '0';
1765                        cp = name + 3;
1766                }
1767                new_path = resolve_relative_path(cp);
1768                if (!new_path) {
1769                        namelen = namelen - (cp - name);
1770                } else {
1771                        cp = new_path;
1772                        namelen = strlen(cp);
1773                }
1774
1775                if (flags & GET_OID_RECORD_PATH)
1776                        oc->path = xstrdup(cp);
1777
1778                if (!repo->index->cache)
1779                        repo_read_index(the_repository);
1780                pos = index_name_pos(repo->index, cp, namelen);
1781                if (pos < 0)
1782                        pos = -pos - 1;
1783                while (pos < repo->index->cache_nr) {
1784                        ce = repo->index->cache[pos];
1785                        if (ce_namelen(ce) != namelen ||
1786                            memcmp(ce->name, cp, namelen))
1787                                break;
1788                        if (ce_stage(ce) == stage) {
1789                                oidcpy(oid, &ce->oid);
1790                                oc->mode = ce->ce_mode;
1791                                free(new_path);
1792                                return 0;
1793                        }
1794                        pos++;
1795                }
1796                if (only_to_die && name[1] && name[1] != '/')
1797                        diagnose_invalid_index_path(repo->index, stage, prefix, cp);
1798                free(new_path);
1799                return -1;
1800        }
1801        for (cp = name, bracket_depth = 0; *cp; cp++) {
1802                if (*cp == '{')
1803                        bracket_depth++;
1804                else if (bracket_depth && *cp == '}')
1805                        bracket_depth--;
1806                else if (!bracket_depth && *cp == ':')
1807                        break;
1808        }
1809        if (*cp == ':') {
1810                struct object_id tree_oid;
1811                int len = cp - name;
1812                unsigned sub_flags = flags;
1813
1814                sub_flags &= ~GET_OID_DISAMBIGUATORS;
1815                sub_flags |= GET_OID_TREEISH;
1816
1817                if (!get_oid_1(name, len, &tree_oid, sub_flags)) {
1818                        const char *filename = cp+1;
1819                        char *new_filename = NULL;
1820
1821                        new_filename = resolve_relative_path(filename);
1822                        if (new_filename)
1823                                filename = new_filename;
1824                        if (flags & GET_OID_FOLLOW_SYMLINKS) {
1825                                ret = get_tree_entry_follow_symlinks(&tree_oid,
1826                                        filename, oid, &oc->symlink_path,
1827                                        &oc->mode);
1828                        } else {
1829                                ret = get_tree_entry(&tree_oid, filename, oid,
1830                                                     &oc->mode);
1831                                if (ret && only_to_die) {
1832                                        diagnose_invalid_oid_path(prefix,
1833                                                                   filename,
1834                                                                   &tree_oid,
1835                                                                   name, len);
1836                                }
1837                        }
1838                        if (flags & GET_OID_RECORD_PATH)
1839                                oc->path = xstrdup(filename);
1840
1841                        free(new_filename);
1842                        return ret;
1843                } else {
1844                        if (only_to_die)
1845                                die("Invalid object name '%.*s'.", len, name);
1846                }
1847        }
1848        return ret;
1849}
1850
1851/*
1852 * Call this function when you know "name" given by the end user must
1853 * name an object but it doesn't; the function _may_ die with a better
1854 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
1855 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
1856 * you have a chance to diagnose the error further.
1857 */
1858void maybe_die_on_misspelt_object_name(const char *name, const char *prefix)
1859{
1860        struct object_context oc;
1861        struct object_id oid;
1862        get_oid_with_context_1(the_repository, name, GET_OID_ONLY_TO_DIE,
1863                               prefix, &oid, &oc);
1864}
1865
1866enum get_oid_result get_oid_with_context(struct repository *repo,
1867                                         const char *str,
1868                                         unsigned flags,
1869                                         struct object_id *oid,
1870                                         struct object_context *oc)
1871{
1872        if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE)
1873                BUG("incompatible flags for get_sha1_with_context");
1874        return get_oid_with_context_1(repo, str, flags, NULL, oid, oc);
1875}