refs.con commit t1400: add tests around adding/deleting pseudorefs (65eb8fc)
   1/*
   2 * The backend-independent part of the reference module.
   3 */
   4
   5#include "cache.h"
   6#include "config.h"
   7#include "hashmap.h"
   8#include "lockfile.h"
   9#include "iterator.h"
  10#include "refs.h"
  11#include "refs/refs-internal.h"
  12#include "object.h"
  13#include "tag.h"
  14#include "submodule.h"
  15#include "worktree.h"
  16#include "argv-array.h"
  17#include "repository.h"
  18
  19/*
  20 * List of all available backends
  21 */
  22static struct ref_storage_be *refs_backends = &refs_be_files;
  23
  24static struct ref_storage_be *find_ref_storage_backend(const char *name)
  25{
  26        struct ref_storage_be *be;
  27        for (be = refs_backends; be; be = be->next)
  28                if (!strcmp(be->name, name))
  29                        return be;
  30        return NULL;
  31}
  32
  33int ref_storage_backend_exists(const char *name)
  34{
  35        return find_ref_storage_backend(name) != NULL;
  36}
  37
  38/*
  39 * How to handle various characters in refnames:
  40 * 0: An acceptable character for refs
  41 * 1: End-of-component
  42 * 2: ., look for a preceding . to reject .. in refs
  43 * 3: {, look for a preceding @ to reject @{ in refs
  44 * 4: A bad character: ASCII control characters, and
  45 *    ":", "?", "[", "\", "^", "~", SP, or TAB
  46 * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
  47 */
  48static unsigned char refname_disposition[256] = {
  49        1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  50        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  51        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
  52        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
  53        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  54        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0,
  55        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  56        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
  57};
  58
  59/*
  60 * Try to read one refname component from the front of refname.
  61 * Return the length of the component found, or -1 if the component is
  62 * not legal.  It is legal if it is something reasonable to have under
  63 * ".git/refs/"; We do not like it if:
  64 *
  65 * - any path component of it begins with ".", or
  66 * - it has double dots "..", or
  67 * - it has ASCII control characters, or
  68 * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or
  69 * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or
  70 * - it ends with a "/", or
  71 * - it ends with ".lock", or
  72 * - it contains a "@{" portion
  73 */
  74static int check_refname_component(const char *refname, int *flags)
  75{
  76        const char *cp;
  77        char last = '\0';
  78
  79        for (cp = refname; ; cp++) {
  80                int ch = *cp & 255;
  81                unsigned char disp = refname_disposition[ch];
  82                switch (disp) {
  83                case 1:
  84                        goto out;
  85                case 2:
  86                        if (last == '.')
  87                                return -1; /* Refname contains "..". */
  88                        break;
  89                case 3:
  90                        if (last == '@')
  91                                return -1; /* Refname contains "@{". */
  92                        break;
  93                case 4:
  94                        return -1;
  95                case 5:
  96                        if (!(*flags & REFNAME_REFSPEC_PATTERN))
  97                                return -1; /* refspec can't be a pattern */
  98
  99                        /*
 100                         * Unset the pattern flag so that we only accept
 101                         * a single asterisk for one side of refspec.
 102                         */
 103                        *flags &= ~ REFNAME_REFSPEC_PATTERN;
 104                        break;
 105                }
 106                last = ch;
 107        }
 108out:
 109        if (cp == refname)
 110                return 0; /* Component has zero length. */
 111        if (refname[0] == '.')
 112                return -1; /* Component starts with '.'. */
 113        if (cp - refname >= LOCK_SUFFIX_LEN &&
 114            !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN))
 115                return -1; /* Refname ends with ".lock". */
 116        return cp - refname;
 117}
 118
 119int check_refname_format(const char *refname, int flags)
 120{
 121        int component_len, component_count = 0;
 122
 123        if (!strcmp(refname, "@"))
 124                /* Refname is a single character '@'. */
 125                return -1;
 126
 127        while (1) {
 128                /* We are at the start of a path component. */
 129                component_len = check_refname_component(refname, &flags);
 130                if (component_len <= 0)
 131                        return -1;
 132
 133                component_count++;
 134                if (refname[component_len] == '\0')
 135                        break;
 136                /* Skip to next component. */
 137                refname += component_len + 1;
 138        }
 139
 140        if (refname[component_len - 1] == '.')
 141                return -1; /* Refname ends with '.'. */
 142        if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2)
 143                return -1; /* Refname has only one component. */
 144        return 0;
 145}
 146
 147int refname_is_safe(const char *refname)
 148{
 149        const char *rest;
 150
 151        if (skip_prefix(refname, "refs/", &rest)) {
 152                char *buf;
 153                int result;
 154                size_t restlen = strlen(rest);
 155
 156                /* rest must not be empty, or start or end with "/" */
 157                if (!restlen || *rest == '/' || rest[restlen - 1] == '/')
 158                        return 0;
 159
 160                /*
 161                 * Does the refname try to escape refs/?
 162                 * For example: refs/foo/../bar is safe but refs/foo/../../bar
 163                 * is not.
 164                 */
 165                buf = xmallocz(restlen);
 166                result = !normalize_path_copy(buf, rest) && !strcmp(buf, rest);
 167                free(buf);
 168                return result;
 169        }
 170
 171        do {
 172                if (!isupper(*refname) && *refname != '_')
 173                        return 0;
 174                refname++;
 175        } while (*refname);
 176        return 1;
 177}
 178
 179/*
 180 * Return true if refname, which has the specified oid and flags, can
 181 * be resolved to an object in the database. If the referred-to object
 182 * does not exist, emit a warning and return false.
 183 */
 184int ref_resolves_to_object(const char *refname,
 185                           const struct object_id *oid,
 186                           unsigned int flags)
 187{
 188        if (flags & REF_ISBROKEN)
 189                return 0;
 190        if (!has_sha1_file(oid->hash)) {
 191                error("%s does not point to a valid object!", refname);
 192                return 0;
 193        }
 194        return 1;
 195}
 196
 197char *refs_resolve_refdup(struct ref_store *refs,
 198                          const char *refname, int resolve_flags,
 199                          struct object_id *oid, int *flags)
 200{
 201        const char *result;
 202
 203        result = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
 204                                         oid, flags);
 205        return xstrdup_or_null(result);
 206}
 207
 208char *resolve_refdup(const char *refname, int resolve_flags,
 209                     struct object_id *oid, int *flags)
 210{
 211        return refs_resolve_refdup(get_main_ref_store(the_repository),
 212                                   refname, resolve_flags,
 213                                   oid, flags);
 214}
 215
 216/* The argument to filter_refs */
 217struct ref_filter {
 218        const char *pattern;
 219        each_ref_fn *fn;
 220        void *cb_data;
 221};
 222
 223int refs_read_ref_full(struct ref_store *refs, const char *refname,
 224                       int resolve_flags, struct object_id *oid, int *flags)
 225{
 226        if (refs_resolve_ref_unsafe(refs, refname, resolve_flags, oid, flags))
 227                return 0;
 228        return -1;
 229}
 230
 231int read_ref_full(const char *refname, int resolve_flags, struct object_id *oid, int *flags)
 232{
 233        return refs_read_ref_full(get_main_ref_store(the_repository), refname,
 234                                  resolve_flags, oid, flags);
 235}
 236
 237int read_ref(const char *refname, struct object_id *oid)
 238{
 239        return read_ref_full(refname, RESOLVE_REF_READING, oid, NULL);
 240}
 241
 242int ref_exists(const char *refname)
 243{
 244        return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, NULL, NULL);
 245}
 246
 247static int match_ref_pattern(const char *refname,
 248                             const struct string_list_item *item)
 249{
 250        int matched = 0;
 251        if (item->util == NULL) {
 252                if (!wildmatch(item->string, refname, 0))
 253                        matched = 1;
 254        } else {
 255                const char *rest;
 256                if (skip_prefix(refname, item->string, &rest) &&
 257                    (!*rest || *rest == '/'))
 258                        matched = 1;
 259        }
 260        return matched;
 261}
 262
 263int ref_filter_match(const char *refname,
 264                     const struct string_list *include_patterns,
 265                     const struct string_list *exclude_patterns)
 266{
 267        struct string_list_item *item;
 268
 269        if (exclude_patterns && exclude_patterns->nr) {
 270                for_each_string_list_item(item, exclude_patterns) {
 271                        if (match_ref_pattern(refname, item))
 272                                return 0;
 273                }
 274        }
 275
 276        if (include_patterns && include_patterns->nr) {
 277                int found = 0;
 278                for_each_string_list_item(item, include_patterns) {
 279                        if (match_ref_pattern(refname, item)) {
 280                                found = 1;
 281                                break;
 282                        }
 283                }
 284
 285                if (!found)
 286                        return 0;
 287        }
 288        return 1;
 289}
 290
 291static int filter_refs(const char *refname, const struct object_id *oid,
 292                           int flags, void *data)
 293{
 294        struct ref_filter *filter = (struct ref_filter *)data;
 295
 296        if (wildmatch(filter->pattern, refname, 0))
 297                return 0;
 298        return filter->fn(refname, oid, flags, filter->cb_data);
 299}
 300
 301enum peel_status peel_object(const struct object_id *name, struct object_id *oid)
 302{
 303        struct object *o = lookup_unknown_object(name->hash);
 304
 305        if (o->type == OBJ_NONE) {
 306                int type = oid_object_info(name, NULL);
 307                if (type < 0 || !object_as_type(o, type, 0))
 308                        return PEEL_INVALID;
 309        }
 310
 311        if (o->type != OBJ_TAG)
 312                return PEEL_NON_TAG;
 313
 314        o = deref_tag_noverify(o);
 315        if (!o)
 316                return PEEL_INVALID;
 317
 318        oidcpy(oid, &o->oid);
 319        return PEEL_PEELED;
 320}
 321
 322struct warn_if_dangling_data {
 323        FILE *fp;
 324        const char *refname;
 325        const struct string_list *refnames;
 326        const char *msg_fmt;
 327};
 328
 329static int warn_if_dangling_symref(const char *refname, const struct object_id *oid,
 330                                   int flags, void *cb_data)
 331{
 332        struct warn_if_dangling_data *d = cb_data;
 333        const char *resolves_to;
 334
 335        if (!(flags & REF_ISSYMREF))
 336                return 0;
 337
 338        resolves_to = resolve_ref_unsafe(refname, 0, NULL, NULL);
 339        if (!resolves_to
 340            || (d->refname
 341                ? strcmp(resolves_to, d->refname)
 342                : !string_list_has_string(d->refnames, resolves_to))) {
 343                return 0;
 344        }
 345
 346        fprintf(d->fp, d->msg_fmt, refname);
 347        fputc('\n', d->fp);
 348        return 0;
 349}
 350
 351void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
 352{
 353        struct warn_if_dangling_data data;
 354
 355        data.fp = fp;
 356        data.refname = refname;
 357        data.refnames = NULL;
 358        data.msg_fmt = msg_fmt;
 359        for_each_rawref(warn_if_dangling_symref, &data);
 360}
 361
 362void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames)
 363{
 364        struct warn_if_dangling_data data;
 365
 366        data.fp = fp;
 367        data.refname = NULL;
 368        data.refnames = refnames;
 369        data.msg_fmt = msg_fmt;
 370        for_each_rawref(warn_if_dangling_symref, &data);
 371}
 372
 373int refs_for_each_tag_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
 374{
 375        return refs_for_each_ref_in(refs, "refs/tags/", fn, cb_data);
 376}
 377
 378int for_each_tag_ref(each_ref_fn fn, void *cb_data)
 379{
 380        return refs_for_each_tag_ref(get_main_ref_store(the_repository), fn, cb_data);
 381}
 382
 383int refs_for_each_branch_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
 384{
 385        return refs_for_each_ref_in(refs, "refs/heads/", fn, cb_data);
 386}
 387
 388int for_each_branch_ref(each_ref_fn fn, void *cb_data)
 389{
 390        return refs_for_each_branch_ref(get_main_ref_store(the_repository), fn, cb_data);
 391}
 392
 393int refs_for_each_remote_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
 394{
 395        return refs_for_each_ref_in(refs, "refs/remotes/", fn, cb_data);
 396}
 397
 398int for_each_remote_ref(each_ref_fn fn, void *cb_data)
 399{
 400        return refs_for_each_remote_ref(get_main_ref_store(the_repository), fn, cb_data);
 401}
 402
 403int head_ref_namespaced(each_ref_fn fn, void *cb_data)
 404{
 405        struct strbuf buf = STRBUF_INIT;
 406        int ret = 0;
 407        struct object_id oid;
 408        int flag;
 409
 410        strbuf_addf(&buf, "%sHEAD", get_git_namespace());
 411        if (!read_ref_full(buf.buf, RESOLVE_REF_READING, &oid, &flag))
 412                ret = fn(buf.buf, &oid, flag, cb_data);
 413        strbuf_release(&buf);
 414
 415        return ret;
 416}
 417
 418void normalize_glob_ref(struct string_list_item *item, const char *prefix,
 419                        const char *pattern)
 420{
 421        struct strbuf normalized_pattern = STRBUF_INIT;
 422
 423        if (*pattern == '/')
 424                BUG("pattern must not start with '/'");
 425
 426        if (prefix) {
 427                strbuf_addstr(&normalized_pattern, prefix);
 428        }
 429        else if (!starts_with(pattern, "refs/"))
 430                strbuf_addstr(&normalized_pattern, "refs/");
 431        strbuf_addstr(&normalized_pattern, pattern);
 432        strbuf_strip_suffix(&normalized_pattern, "/");
 433
 434        item->string = strbuf_detach(&normalized_pattern, NULL);
 435        item->util = has_glob_specials(pattern) ? NULL : item->string;
 436        strbuf_release(&normalized_pattern);
 437}
 438
 439int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
 440        const char *prefix, void *cb_data)
 441{
 442        struct strbuf real_pattern = STRBUF_INIT;
 443        struct ref_filter filter;
 444        int ret;
 445
 446        if (!prefix && !starts_with(pattern, "refs/"))
 447                strbuf_addstr(&real_pattern, "refs/");
 448        else if (prefix)
 449                strbuf_addstr(&real_pattern, prefix);
 450        strbuf_addstr(&real_pattern, pattern);
 451
 452        if (!has_glob_specials(pattern)) {
 453                /* Append implied '/' '*' if not present. */
 454                strbuf_complete(&real_pattern, '/');
 455                /* No need to check for '*', there is none. */
 456                strbuf_addch(&real_pattern, '*');
 457        }
 458
 459        filter.pattern = real_pattern.buf;
 460        filter.fn = fn;
 461        filter.cb_data = cb_data;
 462        ret = for_each_ref(filter_refs, &filter);
 463
 464        strbuf_release(&real_pattern);
 465        return ret;
 466}
 467
 468int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
 469{
 470        return for_each_glob_ref_in(fn, pattern, NULL, cb_data);
 471}
 472
 473const char *prettify_refname(const char *name)
 474{
 475        if (skip_prefix(name, "refs/heads/", &name) ||
 476            skip_prefix(name, "refs/tags/", &name) ||
 477            skip_prefix(name, "refs/remotes/", &name))
 478                ; /* nothing */
 479        return name;
 480}
 481
 482static const char *ref_rev_parse_rules[] = {
 483        "%.*s",
 484        "refs/%.*s",
 485        "refs/tags/%.*s",
 486        "refs/heads/%.*s",
 487        "refs/remotes/%.*s",
 488        "refs/remotes/%.*s/HEAD",
 489        NULL
 490};
 491
 492int refname_match(const char *abbrev_name, const char *full_name)
 493{
 494        const char **p;
 495        const int abbrev_name_len = strlen(abbrev_name);
 496
 497        for (p = ref_rev_parse_rules; *p; p++) {
 498                if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) {
 499                        return 1;
 500                }
 501        }
 502
 503        return 0;
 504}
 505
 506/*
 507 * Given a 'prefix' expand it by the rules in 'ref_rev_parse_rules' and add
 508 * the results to 'prefixes'
 509 */
 510void expand_ref_prefix(struct argv_array *prefixes, const char *prefix)
 511{
 512        const char **p;
 513        int len = strlen(prefix);
 514
 515        for (p = ref_rev_parse_rules; *p; p++)
 516                argv_array_pushf(prefixes, *p, len, prefix);
 517}
 518
 519/*
 520 * *string and *len will only be substituted, and *string returned (for
 521 * later free()ing) if the string passed in is a magic short-hand form
 522 * to name a branch.
 523 */
 524static char *substitute_branch_name(const char **string, int *len)
 525{
 526        struct strbuf buf = STRBUF_INIT;
 527        int ret = interpret_branch_name(*string, *len, &buf, 0);
 528
 529        if (ret == *len) {
 530                size_t size;
 531                *string = strbuf_detach(&buf, &size);
 532                *len = size;
 533                return (char *)*string;
 534        }
 535
 536        return NULL;
 537}
 538
 539int dwim_ref(const char *str, int len, struct object_id *oid, char **ref)
 540{
 541        char *last_branch = substitute_branch_name(&str, &len);
 542        int   refs_found  = expand_ref(str, len, oid, ref);
 543        free(last_branch);
 544        return refs_found;
 545}
 546
 547int expand_ref(const char *str, int len, struct object_id *oid, char **ref)
 548{
 549        const char **p, *r;
 550        int refs_found = 0;
 551        struct strbuf fullref = STRBUF_INIT;
 552
 553        *ref = NULL;
 554        for (p = ref_rev_parse_rules; *p; p++) {
 555                struct object_id oid_from_ref;
 556                struct object_id *this_result;
 557                int flag;
 558
 559                this_result = refs_found ? &oid_from_ref : oid;
 560                strbuf_reset(&fullref);
 561                strbuf_addf(&fullref, *p, len, str);
 562                r = resolve_ref_unsafe(fullref.buf, RESOLVE_REF_READING,
 563                                       this_result, &flag);
 564                if (r) {
 565                        if (!refs_found++)
 566                                *ref = xstrdup(r);
 567                        if (!warn_ambiguous_refs)
 568                                break;
 569                } else if ((flag & REF_ISSYMREF) && strcmp(fullref.buf, "HEAD")) {
 570                        warning("ignoring dangling symref %s.", fullref.buf);
 571                } else if ((flag & REF_ISBROKEN) && strchr(fullref.buf, '/')) {
 572                        warning("ignoring broken ref %s.", fullref.buf);
 573                }
 574        }
 575        strbuf_release(&fullref);
 576        return refs_found;
 577}
 578
 579int dwim_log(const char *str, int len, struct object_id *oid, char **log)
 580{
 581        char *last_branch = substitute_branch_name(&str, &len);
 582        const char **p;
 583        int logs_found = 0;
 584        struct strbuf path = STRBUF_INIT;
 585
 586        *log = NULL;
 587        for (p = ref_rev_parse_rules; *p; p++) {
 588                struct object_id hash;
 589                const char *ref, *it;
 590
 591                strbuf_reset(&path);
 592                strbuf_addf(&path, *p, len, str);
 593                ref = resolve_ref_unsafe(path.buf, RESOLVE_REF_READING,
 594                                         &hash, NULL);
 595                if (!ref)
 596                        continue;
 597                if (reflog_exists(path.buf))
 598                        it = path.buf;
 599                else if (strcmp(ref, path.buf) && reflog_exists(ref))
 600                        it = ref;
 601                else
 602                        continue;
 603                if (!logs_found++) {
 604                        *log = xstrdup(it);
 605                        oidcpy(oid, &hash);
 606                }
 607                if (!warn_ambiguous_refs)
 608                        break;
 609        }
 610        strbuf_release(&path);
 611        free(last_branch);
 612        return logs_found;
 613}
 614
 615static int is_per_worktree_ref(const char *refname)
 616{
 617        return !strcmp(refname, "HEAD") ||
 618                starts_with(refname, "refs/bisect/");
 619}
 620
 621static int is_pseudoref_syntax(const char *refname)
 622{
 623        const char *c;
 624
 625        for (c = refname; *c; c++) {
 626                if (!isupper(*c) && *c != '-' && *c != '_')
 627                        return 0;
 628        }
 629
 630        return 1;
 631}
 632
 633enum ref_type ref_type(const char *refname)
 634{
 635        if (is_per_worktree_ref(refname))
 636                return REF_TYPE_PER_WORKTREE;
 637        if (is_pseudoref_syntax(refname))
 638                return REF_TYPE_PSEUDOREF;
 639       return REF_TYPE_NORMAL;
 640}
 641
 642long get_files_ref_lock_timeout_ms(void)
 643{
 644        static int configured = 0;
 645
 646        /* The default timeout is 100 ms: */
 647        static int timeout_ms = 100;
 648
 649        if (!configured) {
 650                git_config_get_int("core.filesreflocktimeout", &timeout_ms);
 651                configured = 1;
 652        }
 653
 654        return timeout_ms;
 655}
 656
 657static int write_pseudoref(const char *pseudoref, const struct object_id *oid,
 658                           const struct object_id *old_oid, struct strbuf *err)
 659{
 660        const char *filename;
 661        int fd;
 662        static struct lock_file lock;
 663        struct strbuf buf = STRBUF_INIT;
 664        int ret = -1;
 665
 666        if (!oid)
 667                return 0;
 668
 669        strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
 670
 671        filename = git_path("%s", pseudoref);
 672        fd = hold_lock_file_for_update_timeout(&lock, filename,
 673                                               LOCK_DIE_ON_ERROR,
 674                                               get_files_ref_lock_timeout_ms());
 675        if (fd < 0) {
 676                strbuf_addf(err, "could not open '%s' for writing: %s",
 677                            filename, strerror(errno));
 678                goto done;
 679        }
 680
 681        if (old_oid) {
 682                struct object_id actual_old_oid;
 683
 684                if (read_ref(pseudoref, &actual_old_oid))
 685                        die("could not read ref '%s'", pseudoref);
 686                if (oidcmp(&actual_old_oid, old_oid)) {
 687                        strbuf_addf(err, "unexpected object ID when writing '%s'",
 688                                    pseudoref);
 689                        rollback_lock_file(&lock);
 690                        goto done;
 691                }
 692        }
 693
 694        if (write_in_full(fd, buf.buf, buf.len) < 0) {
 695                strbuf_addf(err, "could not write to '%s'", filename);
 696                rollback_lock_file(&lock);
 697                goto done;
 698        }
 699
 700        commit_lock_file(&lock);
 701        ret = 0;
 702done:
 703        strbuf_release(&buf);
 704        return ret;
 705}
 706
 707static int delete_pseudoref(const char *pseudoref, const struct object_id *old_oid)
 708{
 709        static struct lock_file lock;
 710        const char *filename;
 711
 712        filename = git_path("%s", pseudoref);
 713
 714        if (old_oid && !is_null_oid(old_oid)) {
 715                int fd;
 716                struct object_id actual_old_oid;
 717
 718                fd = hold_lock_file_for_update_timeout(
 719                                &lock, filename, LOCK_DIE_ON_ERROR,
 720                                get_files_ref_lock_timeout_ms());
 721                if (fd < 0)
 722                        die_errno(_("Could not open '%s' for writing"), filename);
 723                if (read_ref(pseudoref, &actual_old_oid))
 724                        die("could not read ref '%s'", pseudoref);
 725                if (oidcmp(&actual_old_oid, old_oid)) {
 726                        error("unexpected object ID when deleting '%s'",
 727                              pseudoref);
 728                        rollback_lock_file(&lock);
 729                        return -1;
 730                }
 731
 732                unlink(filename);
 733                rollback_lock_file(&lock);
 734        } else {
 735                unlink(filename);
 736        }
 737
 738        return 0;
 739}
 740
 741int refs_delete_ref(struct ref_store *refs, const char *msg,
 742                    const char *refname,
 743                    const struct object_id *old_oid,
 744                    unsigned int flags)
 745{
 746        struct ref_transaction *transaction;
 747        struct strbuf err = STRBUF_INIT;
 748
 749        if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
 750                assert(refs == get_main_ref_store(the_repository));
 751                return delete_pseudoref(refname, old_oid);
 752        }
 753
 754        transaction = ref_store_transaction_begin(refs, &err);
 755        if (!transaction ||
 756            ref_transaction_delete(transaction, refname, old_oid,
 757                                   flags, msg, &err) ||
 758            ref_transaction_commit(transaction, &err)) {
 759                error("%s", err.buf);
 760                ref_transaction_free(transaction);
 761                strbuf_release(&err);
 762                return 1;
 763        }
 764        ref_transaction_free(transaction);
 765        strbuf_release(&err);
 766        return 0;
 767}
 768
 769int delete_ref(const char *msg, const char *refname,
 770               const struct object_id *old_oid, unsigned int flags)
 771{
 772        return refs_delete_ref(get_main_ref_store(the_repository), msg, refname,
 773                               old_oid, flags);
 774}
 775
 776int copy_reflog_msg(char *buf, const char *msg)
 777{
 778        char *cp = buf;
 779        char c;
 780        int wasspace = 1;
 781
 782        *cp++ = '\t';
 783        while ((c = *msg++)) {
 784                if (wasspace && isspace(c))
 785                        continue;
 786                wasspace = isspace(c);
 787                if (wasspace)
 788                        c = ' ';
 789                *cp++ = c;
 790        }
 791        while (buf < cp && isspace(cp[-1]))
 792                cp--;
 793        *cp++ = '\n';
 794        return cp - buf;
 795}
 796
 797int should_autocreate_reflog(const char *refname)
 798{
 799        switch (log_all_ref_updates) {
 800        case LOG_REFS_ALWAYS:
 801                return 1;
 802        case LOG_REFS_NORMAL:
 803                return starts_with(refname, "refs/heads/") ||
 804                        starts_with(refname, "refs/remotes/") ||
 805                        starts_with(refname, "refs/notes/") ||
 806                        !strcmp(refname, "HEAD");
 807        default:
 808                return 0;
 809        }
 810}
 811
 812int is_branch(const char *refname)
 813{
 814        return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/");
 815}
 816
 817struct read_ref_at_cb {
 818        const char *refname;
 819        timestamp_t at_time;
 820        int cnt;
 821        int reccnt;
 822        struct object_id *oid;
 823        int found_it;
 824
 825        struct object_id ooid;
 826        struct object_id noid;
 827        int tz;
 828        timestamp_t date;
 829        char **msg;
 830        timestamp_t *cutoff_time;
 831        int *cutoff_tz;
 832        int *cutoff_cnt;
 833};
 834
 835static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
 836                const char *email, timestamp_t timestamp, int tz,
 837                const char *message, void *cb_data)
 838{
 839        struct read_ref_at_cb *cb = cb_data;
 840
 841        cb->reccnt++;
 842        cb->tz = tz;
 843        cb->date = timestamp;
 844
 845        if (timestamp <= cb->at_time || cb->cnt == 0) {
 846                if (cb->msg)
 847                        *cb->msg = xstrdup(message);
 848                if (cb->cutoff_time)
 849                        *cb->cutoff_time = timestamp;
 850                if (cb->cutoff_tz)
 851                        *cb->cutoff_tz = tz;
 852                if (cb->cutoff_cnt)
 853                        *cb->cutoff_cnt = cb->reccnt - 1;
 854                /*
 855                 * we have not yet updated cb->[n|o]oid so they still
 856                 * hold the values for the previous record.
 857                 */
 858                if (!is_null_oid(&cb->ooid)) {
 859                        oidcpy(cb->oid, noid);
 860                        if (oidcmp(&cb->ooid, noid))
 861                                warning("Log for ref %s has gap after %s.",
 862                                        cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
 863                }
 864                else if (cb->date == cb->at_time)
 865                        oidcpy(cb->oid, noid);
 866                else if (oidcmp(noid, cb->oid))
 867                        warning("Log for ref %s unexpectedly ended on %s.",
 868                                cb->refname, show_date(cb->date, cb->tz,
 869                                                       DATE_MODE(RFC2822)));
 870                oidcpy(&cb->ooid, ooid);
 871                oidcpy(&cb->noid, noid);
 872                cb->found_it = 1;
 873                return 1;
 874        }
 875        oidcpy(&cb->ooid, ooid);
 876        oidcpy(&cb->noid, noid);
 877        if (cb->cnt > 0)
 878                cb->cnt--;
 879        return 0;
 880}
 881
 882static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid,
 883                                  const char *email, timestamp_t timestamp,
 884                                  int tz, const char *message, void *cb_data)
 885{
 886        struct read_ref_at_cb *cb = cb_data;
 887
 888        if (cb->msg)
 889                *cb->msg = xstrdup(message);
 890        if (cb->cutoff_time)
 891                *cb->cutoff_time = timestamp;
 892        if (cb->cutoff_tz)
 893                *cb->cutoff_tz = tz;
 894        if (cb->cutoff_cnt)
 895                *cb->cutoff_cnt = cb->reccnt;
 896        oidcpy(cb->oid, ooid);
 897        if (is_null_oid(cb->oid))
 898                oidcpy(cb->oid, noid);
 899        /* We just want the first entry */
 900        return 1;
 901}
 902
 903int read_ref_at(const char *refname, unsigned int flags, timestamp_t at_time, int cnt,
 904                struct object_id *oid, char **msg,
 905                timestamp_t *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
 906{
 907        struct read_ref_at_cb cb;
 908
 909        memset(&cb, 0, sizeof(cb));
 910        cb.refname = refname;
 911        cb.at_time = at_time;
 912        cb.cnt = cnt;
 913        cb.msg = msg;
 914        cb.cutoff_time = cutoff_time;
 915        cb.cutoff_tz = cutoff_tz;
 916        cb.cutoff_cnt = cutoff_cnt;
 917        cb.oid = oid;
 918
 919        for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb);
 920
 921        if (!cb.reccnt) {
 922                if (flags & GET_OID_QUIETLY)
 923                        exit(128);
 924                else
 925                        die("Log for %s is empty.", refname);
 926        }
 927        if (cb.found_it)
 928                return 0;
 929
 930        for_each_reflog_ent(refname, read_ref_at_ent_oldest, &cb);
 931
 932        return 1;
 933}
 934
 935struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs,
 936                                                    struct strbuf *err)
 937{
 938        struct ref_transaction *tr;
 939        assert(err);
 940
 941        tr = xcalloc(1, sizeof(struct ref_transaction));
 942        tr->ref_store = refs;
 943        return tr;
 944}
 945
 946struct ref_transaction *ref_transaction_begin(struct strbuf *err)
 947{
 948        return ref_store_transaction_begin(get_main_ref_store(the_repository), err);
 949}
 950
 951void ref_transaction_free(struct ref_transaction *transaction)
 952{
 953        size_t i;
 954
 955        if (!transaction)
 956                return;
 957
 958        switch (transaction->state) {
 959        case REF_TRANSACTION_OPEN:
 960        case REF_TRANSACTION_CLOSED:
 961                /* OK */
 962                break;
 963        case REF_TRANSACTION_PREPARED:
 964                die("BUG: free called on a prepared reference transaction");
 965                break;
 966        default:
 967                die("BUG: unexpected reference transaction state");
 968                break;
 969        }
 970
 971        for (i = 0; i < transaction->nr; i++) {
 972                free(transaction->updates[i]->msg);
 973                free(transaction->updates[i]);
 974        }
 975        free(transaction->updates);
 976        free(transaction);
 977}
 978
 979struct ref_update *ref_transaction_add_update(
 980                struct ref_transaction *transaction,
 981                const char *refname, unsigned int flags,
 982                const struct object_id *new_oid,
 983                const struct object_id *old_oid,
 984                const char *msg)
 985{
 986        struct ref_update *update;
 987
 988        if (transaction->state != REF_TRANSACTION_OPEN)
 989                die("BUG: update called for transaction that is not open");
 990
 991        FLEX_ALLOC_STR(update, refname, refname);
 992        ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
 993        transaction->updates[transaction->nr++] = update;
 994
 995        update->flags = flags;
 996
 997        if (flags & REF_HAVE_NEW)
 998                oidcpy(&update->new_oid, new_oid);
 999        if (flags & REF_HAVE_OLD)
1000                oidcpy(&update->old_oid, old_oid);
1001        update->msg = xstrdup_or_null(msg);
1002        return update;
1003}
1004
1005int ref_transaction_update(struct ref_transaction *transaction,
1006                           const char *refname,
1007                           const struct object_id *new_oid,
1008                           const struct object_id *old_oid,
1009                           unsigned int flags, const char *msg,
1010                           struct strbuf *err)
1011{
1012        assert(err);
1013
1014        if ((new_oid && !is_null_oid(new_oid)) ?
1015            check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
1016            !refname_is_safe(refname)) {
1017                strbuf_addf(err, "refusing to update ref with bad name '%s'",
1018                            refname);
1019                return -1;
1020        }
1021
1022        if (flags & ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS)
1023                BUG("illegal flags 0x%x passed to ref_transaction_update()", flags);
1024
1025        flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0);
1026
1027        ref_transaction_add_update(transaction, refname, flags,
1028                                   new_oid, old_oid, msg);
1029        return 0;
1030}
1031
1032int ref_transaction_create(struct ref_transaction *transaction,
1033                           const char *refname,
1034                           const struct object_id *new_oid,
1035                           unsigned int flags, const char *msg,
1036                           struct strbuf *err)
1037{
1038        if (!new_oid || is_null_oid(new_oid))
1039                die("BUG: create called without valid new_oid");
1040        return ref_transaction_update(transaction, refname, new_oid,
1041                                      &null_oid, flags, msg, err);
1042}
1043
1044int ref_transaction_delete(struct ref_transaction *transaction,
1045                           const char *refname,
1046                           const struct object_id *old_oid,
1047                           unsigned int flags, const char *msg,
1048                           struct strbuf *err)
1049{
1050        if (old_oid && is_null_oid(old_oid))
1051                die("BUG: delete called with old_oid set to zeros");
1052        return ref_transaction_update(transaction, refname,
1053                                      &null_oid, old_oid,
1054                                      flags, msg, err);
1055}
1056
1057int ref_transaction_verify(struct ref_transaction *transaction,
1058                           const char *refname,
1059                           const struct object_id *old_oid,
1060                           unsigned int flags,
1061                           struct strbuf *err)
1062{
1063        if (!old_oid)
1064                die("BUG: verify called with old_oid set to NULL");
1065        return ref_transaction_update(transaction, refname,
1066                                      NULL, old_oid,
1067                                      flags, NULL, err);
1068}
1069
1070int refs_update_ref(struct ref_store *refs, const char *msg,
1071                    const char *refname, const struct object_id *new_oid,
1072                    const struct object_id *old_oid, unsigned int flags,
1073                    enum action_on_err onerr)
1074{
1075        struct ref_transaction *t = NULL;
1076        struct strbuf err = STRBUF_INIT;
1077        int ret = 0;
1078
1079        if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
1080                assert(refs == get_main_ref_store(the_repository));
1081                ret = write_pseudoref(refname, new_oid, old_oid, &err);
1082        } else {
1083                t = ref_store_transaction_begin(refs, &err);
1084                if (!t ||
1085                    ref_transaction_update(t, refname, new_oid, old_oid,
1086                                           flags, msg, &err) ||
1087                    ref_transaction_commit(t, &err)) {
1088                        ret = 1;
1089                        ref_transaction_free(t);
1090                }
1091        }
1092        if (ret) {
1093                const char *str = "update_ref failed for ref '%s': %s";
1094
1095                switch (onerr) {
1096                case UPDATE_REFS_MSG_ON_ERR:
1097                        error(str, refname, err.buf);
1098                        break;
1099                case UPDATE_REFS_DIE_ON_ERR:
1100                        die(str, refname, err.buf);
1101                        break;
1102                case UPDATE_REFS_QUIET_ON_ERR:
1103                        break;
1104                }
1105                strbuf_release(&err);
1106                return 1;
1107        }
1108        strbuf_release(&err);
1109        if (t)
1110                ref_transaction_free(t);
1111        return 0;
1112}
1113
1114int update_ref(const char *msg, const char *refname,
1115               const struct object_id *new_oid,
1116               const struct object_id *old_oid,
1117               unsigned int flags, enum action_on_err onerr)
1118{
1119        return refs_update_ref(get_main_ref_store(the_repository), msg, refname, new_oid,
1120                               old_oid, flags, onerr);
1121}
1122
1123char *shorten_unambiguous_ref(const char *refname, int strict)
1124{
1125        int i;
1126        static char **scanf_fmts;
1127        static int nr_rules;
1128        char *short_name;
1129        struct strbuf resolved_buf = STRBUF_INIT;
1130
1131        if (!nr_rules) {
1132                /*
1133                 * Pre-generate scanf formats from ref_rev_parse_rules[].
1134                 * Generate a format suitable for scanf from a
1135                 * ref_rev_parse_rules rule by interpolating "%s" at the
1136                 * location of the "%.*s".
1137                 */
1138                size_t total_len = 0;
1139                size_t offset = 0;
1140
1141                /* the rule list is NULL terminated, count them first */
1142                for (nr_rules = 0; ref_rev_parse_rules[nr_rules]; nr_rules++)
1143                        /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
1144                        total_len += strlen(ref_rev_parse_rules[nr_rules]) - 2 + 1;
1145
1146                scanf_fmts = xmalloc(st_add(st_mult(sizeof(char *), nr_rules), total_len));
1147
1148                offset = 0;
1149                for (i = 0; i < nr_rules; i++) {
1150                        assert(offset < total_len);
1151                        scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset;
1152                        offset += snprintf(scanf_fmts[i], total_len - offset,
1153                                           ref_rev_parse_rules[i], 2, "%s") + 1;
1154                }
1155        }
1156
1157        /* bail out if there are no rules */
1158        if (!nr_rules)
1159                return xstrdup(refname);
1160
1161        /* buffer for scanf result, at most refname must fit */
1162        short_name = xstrdup(refname);
1163
1164        /* skip first rule, it will always match */
1165        for (i = nr_rules - 1; i > 0 ; --i) {
1166                int j;
1167                int rules_to_fail = i;
1168                int short_name_len;
1169
1170                if (1 != sscanf(refname, scanf_fmts[i], short_name))
1171                        continue;
1172
1173                short_name_len = strlen(short_name);
1174
1175                /*
1176                 * in strict mode, all (except the matched one) rules
1177                 * must fail to resolve to a valid non-ambiguous ref
1178                 */
1179                if (strict)
1180                        rules_to_fail = nr_rules;
1181
1182                /*
1183                 * check if the short name resolves to a valid ref,
1184                 * but use only rules prior to the matched one
1185                 */
1186                for (j = 0; j < rules_to_fail; j++) {
1187                        const char *rule = ref_rev_parse_rules[j];
1188
1189                        /* skip matched rule */
1190                        if (i == j)
1191                                continue;
1192
1193                        /*
1194                         * the short name is ambiguous, if it resolves
1195                         * (with this previous rule) to a valid ref
1196                         * read_ref() returns 0 on success
1197                         */
1198                        strbuf_reset(&resolved_buf);
1199                        strbuf_addf(&resolved_buf, rule,
1200                                    short_name_len, short_name);
1201                        if (ref_exists(resolved_buf.buf))
1202                                break;
1203                }
1204
1205                /*
1206                 * short name is non-ambiguous if all previous rules
1207                 * haven't resolved to a valid ref
1208                 */
1209                if (j == rules_to_fail) {
1210                        strbuf_release(&resolved_buf);
1211                        return short_name;
1212                }
1213        }
1214
1215        strbuf_release(&resolved_buf);
1216        free(short_name);
1217        return xstrdup(refname);
1218}
1219
1220static struct string_list *hide_refs;
1221
1222int parse_hide_refs_config(const char *var, const char *value, const char *section)
1223{
1224        const char *key;
1225        if (!strcmp("transfer.hiderefs", var) ||
1226            (!parse_config_key(var, section, NULL, NULL, &key) &&
1227             !strcmp(key, "hiderefs"))) {
1228                char *ref;
1229                int len;
1230
1231                if (!value)
1232                        return config_error_nonbool(var);
1233                ref = xstrdup(value);
1234                len = strlen(ref);
1235                while (len && ref[len - 1] == '/')
1236                        ref[--len] = '\0';
1237                if (!hide_refs) {
1238                        hide_refs = xcalloc(1, sizeof(*hide_refs));
1239                        hide_refs->strdup_strings = 1;
1240                }
1241                string_list_append(hide_refs, ref);
1242        }
1243        return 0;
1244}
1245
1246int ref_is_hidden(const char *refname, const char *refname_full)
1247{
1248        int i;
1249
1250        if (!hide_refs)
1251                return 0;
1252        for (i = hide_refs->nr - 1; i >= 0; i--) {
1253                const char *match = hide_refs->items[i].string;
1254                const char *subject;
1255                int neg = 0;
1256                const char *p;
1257
1258                if (*match == '!') {
1259                        neg = 1;
1260                        match++;
1261                }
1262
1263                if (*match == '^') {
1264                        subject = refname_full;
1265                        match++;
1266                } else {
1267                        subject = refname;
1268                }
1269
1270                /* refname can be NULL when namespaces are used. */
1271                if (subject &&
1272                    skip_prefix(subject, match, &p) &&
1273                    (!*p || *p == '/'))
1274                        return !neg;
1275        }
1276        return 0;
1277}
1278
1279const char *find_descendant_ref(const char *dirname,
1280                                const struct string_list *extras,
1281                                const struct string_list *skip)
1282{
1283        int pos;
1284
1285        if (!extras)
1286                return NULL;
1287
1288        /*
1289         * Look at the place where dirname would be inserted into
1290         * extras. If there is an entry at that position that starts
1291         * with dirname (remember, dirname includes the trailing
1292         * slash) and is not in skip, then we have a conflict.
1293         */
1294        for (pos = string_list_find_insert_index(extras, dirname, 0);
1295             pos < extras->nr; pos++) {
1296                const char *extra_refname = extras->items[pos].string;
1297
1298                if (!starts_with(extra_refname, dirname))
1299                        break;
1300
1301                if (!skip || !string_list_has_string(skip, extra_refname))
1302                        return extra_refname;
1303        }
1304        return NULL;
1305}
1306
1307int refs_rename_ref_available(struct ref_store *refs,
1308                              const char *old_refname,
1309                              const char *new_refname)
1310{
1311        struct string_list skip = STRING_LIST_INIT_NODUP;
1312        struct strbuf err = STRBUF_INIT;
1313        int ok;
1314
1315        string_list_insert(&skip, old_refname);
1316        ok = !refs_verify_refname_available(refs, new_refname,
1317                                            NULL, &skip, &err);
1318        if (!ok)
1319                error("%s", err.buf);
1320
1321        string_list_clear(&skip, 0);
1322        strbuf_release(&err);
1323        return ok;
1324}
1325
1326int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1327{
1328        struct object_id oid;
1329        int flag;
1330
1331        if (!refs_read_ref_full(refs, "HEAD", RESOLVE_REF_READING,
1332                                &oid, &flag))
1333                return fn("HEAD", &oid, flag, cb_data);
1334
1335        return 0;
1336}
1337
1338int head_ref(each_ref_fn fn, void *cb_data)
1339{
1340        return refs_head_ref(get_main_ref_store(the_repository), fn, cb_data);
1341}
1342
1343struct ref_iterator *refs_ref_iterator_begin(
1344                struct ref_store *refs,
1345                const char *prefix, int trim, int flags)
1346{
1347        struct ref_iterator *iter;
1348
1349        if (ref_paranoia < 0)
1350                ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 0);
1351        if (ref_paranoia)
1352                flags |= DO_FOR_EACH_INCLUDE_BROKEN;
1353
1354        iter = refs->be->iterator_begin(refs, prefix, flags);
1355
1356        /*
1357         * `iterator_begin()` already takes care of prefix, but we
1358         * might need to do some trimming:
1359         */
1360        if (trim)
1361                iter = prefix_ref_iterator_begin(iter, "", trim);
1362
1363        /* Sanity check for subclasses: */
1364        if (!iter->ordered)
1365                BUG("reference iterator is not ordered");
1366
1367        return iter;
1368}
1369
1370/*
1371 * Call fn for each reference in the specified submodule for which the
1372 * refname begins with prefix. If trim is non-zero, then trim that
1373 * many characters off the beginning of each refname before passing
1374 * the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to
1375 * include broken references in the iteration. If fn ever returns a
1376 * non-zero value, stop the iteration and return that value;
1377 * otherwise, return 0.
1378 */
1379static int do_for_each_ref(struct ref_store *refs, const char *prefix,
1380                           each_ref_fn fn, int trim, int flags, void *cb_data)
1381{
1382        struct ref_iterator *iter;
1383
1384        if (!refs)
1385                return 0;
1386
1387        iter = refs_ref_iterator_begin(refs, prefix, trim, flags);
1388
1389        return do_for_each_ref_iterator(iter, fn, cb_data);
1390}
1391
1392int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1393{
1394        return do_for_each_ref(refs, "", fn, 0, 0, cb_data);
1395}
1396
1397int for_each_ref(each_ref_fn fn, void *cb_data)
1398{
1399        return refs_for_each_ref(get_main_ref_store(the_repository), fn, cb_data);
1400}
1401
1402int refs_for_each_ref_in(struct ref_store *refs, const char *prefix,
1403                         each_ref_fn fn, void *cb_data)
1404{
1405        return do_for_each_ref(refs, prefix, fn, strlen(prefix), 0, cb_data);
1406}
1407
1408int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
1409{
1410        return refs_for_each_ref_in(get_main_ref_store(the_repository), prefix, fn, cb_data);
1411}
1412
1413int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
1414{
1415        unsigned int flag = 0;
1416
1417        if (broken)
1418                flag = DO_FOR_EACH_INCLUDE_BROKEN;
1419        return do_for_each_ref(get_main_ref_store(the_repository),
1420                               prefix, fn, 0, flag, cb_data);
1421}
1422
1423int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
1424                             each_ref_fn fn, void *cb_data,
1425                             unsigned int broken)
1426{
1427        unsigned int flag = 0;
1428
1429        if (broken)
1430                flag = DO_FOR_EACH_INCLUDE_BROKEN;
1431        return do_for_each_ref(refs, prefix, fn, 0, flag, cb_data);
1432}
1433
1434int for_each_replace_ref(struct repository *r, each_ref_fn fn, void *cb_data)
1435{
1436        return do_for_each_ref(get_main_ref_store(r),
1437                               git_replace_ref_base, fn,
1438                               strlen(git_replace_ref_base),
1439                               DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1440}
1441
1442int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1443{
1444        struct strbuf buf = STRBUF_INIT;
1445        int ret;
1446        strbuf_addf(&buf, "%srefs/", get_git_namespace());
1447        ret = do_for_each_ref(get_main_ref_store(the_repository),
1448                              buf.buf, fn, 0, 0, cb_data);
1449        strbuf_release(&buf);
1450        return ret;
1451}
1452
1453int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
1454{
1455        return do_for_each_ref(refs, "", fn, 0,
1456                               DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1457}
1458
1459int for_each_rawref(each_ref_fn fn, void *cb_data)
1460{
1461        return refs_for_each_rawref(get_main_ref_store(the_repository), fn, cb_data);
1462}
1463
1464int refs_read_raw_ref(struct ref_store *ref_store,
1465                      const char *refname, struct object_id *oid,
1466                      struct strbuf *referent, unsigned int *type)
1467{
1468        return ref_store->be->read_raw_ref(ref_store, refname, oid, referent, type);
1469}
1470
1471/* This function needs to return a meaningful errno on failure */
1472const char *refs_resolve_ref_unsafe(struct ref_store *refs,
1473                                    const char *refname,
1474                                    int resolve_flags,
1475                                    struct object_id *oid, int *flags)
1476{
1477        static struct strbuf sb_refname = STRBUF_INIT;
1478        struct object_id unused_oid;
1479        int unused_flags;
1480        int symref_count;
1481
1482        if (!oid)
1483                oid = &unused_oid;
1484        if (!flags)
1485                flags = &unused_flags;
1486
1487        *flags = 0;
1488
1489        if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1490                if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1491                    !refname_is_safe(refname)) {
1492                        errno = EINVAL;
1493                        return NULL;
1494                }
1495
1496                /*
1497                 * dwim_ref() uses REF_ISBROKEN to distinguish between
1498                 * missing refs and refs that were present but invalid,
1499                 * to complain about the latter to stderr.
1500                 *
1501                 * We don't know whether the ref exists, so don't set
1502                 * REF_ISBROKEN yet.
1503                 */
1504                *flags |= REF_BAD_NAME;
1505        }
1506
1507        for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
1508                unsigned int read_flags = 0;
1509
1510                if (refs_read_raw_ref(refs, refname,
1511                                      oid, &sb_refname, &read_flags)) {
1512                        *flags |= read_flags;
1513
1514                        /* In reading mode, refs must eventually resolve */
1515                        if (resolve_flags & RESOLVE_REF_READING)
1516                                return NULL;
1517
1518                        /*
1519                         * Otherwise a missing ref is OK. But the files backend
1520                         * may show errors besides ENOENT if there are
1521                         * similarly-named refs.
1522                         */
1523                        if (errno != ENOENT &&
1524                            errno != EISDIR &&
1525                            errno != ENOTDIR)
1526                                return NULL;
1527
1528                        oidclr(oid);
1529                        if (*flags & REF_BAD_NAME)
1530                                *flags |= REF_ISBROKEN;
1531                        return refname;
1532                }
1533
1534                *flags |= read_flags;
1535
1536                if (!(read_flags & REF_ISSYMREF)) {
1537                        if (*flags & REF_BAD_NAME) {
1538                                oidclr(oid);
1539                                *flags |= REF_ISBROKEN;
1540                        }
1541                        return refname;
1542                }
1543
1544                refname = sb_refname.buf;
1545                if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
1546                        oidclr(oid);
1547                        return refname;
1548                }
1549                if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1550                        if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1551                            !refname_is_safe(refname)) {
1552                                errno = EINVAL;
1553                                return NULL;
1554                        }
1555
1556                        *flags |= REF_ISBROKEN | REF_BAD_NAME;
1557                }
1558        }
1559
1560        errno = ELOOP;
1561        return NULL;
1562}
1563
1564/* backend functions */
1565int refs_init_db(struct strbuf *err)
1566{
1567        struct ref_store *refs = get_main_ref_store(the_repository);
1568
1569        return refs->be->init_db(refs, err);
1570}
1571
1572const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1573                               struct object_id *oid, int *flags)
1574{
1575        return refs_resolve_ref_unsafe(get_main_ref_store(the_repository), refname,
1576                                       resolve_flags, oid, flags);
1577}
1578
1579int resolve_gitlink_ref(const char *submodule, const char *refname,
1580                        struct object_id *oid)
1581{
1582        struct ref_store *refs;
1583        int flags;
1584
1585        refs = get_submodule_ref_store(submodule);
1586
1587        if (!refs)
1588                return -1;
1589
1590        if (!refs_resolve_ref_unsafe(refs, refname, 0, oid, &flags) ||
1591            is_null_oid(oid))
1592                return -1;
1593        return 0;
1594}
1595
1596struct ref_store_hash_entry
1597{
1598        struct hashmap_entry ent; /* must be the first member! */
1599
1600        struct ref_store *refs;
1601
1602        /* NUL-terminated identifier of the ref store: */
1603        char name[FLEX_ARRAY];
1604};
1605
1606static int ref_store_hash_cmp(const void *unused_cmp_data,
1607                              const void *entry, const void *entry_or_key,
1608                              const void *keydata)
1609{
1610        const struct ref_store_hash_entry *e1 = entry, *e2 = entry_or_key;
1611        const char *name = keydata ? keydata : e2->name;
1612
1613        return strcmp(e1->name, name);
1614}
1615
1616static struct ref_store_hash_entry *alloc_ref_store_hash_entry(
1617                const char *name, struct ref_store *refs)
1618{
1619        struct ref_store_hash_entry *entry;
1620
1621        FLEX_ALLOC_STR(entry, name, name);
1622        hashmap_entry_init(entry, strhash(name));
1623        entry->refs = refs;
1624        return entry;
1625}
1626
1627/* A hashmap of ref_stores, stored by submodule name: */
1628static struct hashmap submodule_ref_stores;
1629
1630/* A hashmap of ref_stores, stored by worktree id: */
1631static struct hashmap worktree_ref_stores;
1632
1633/*
1634 * Look up a ref store by name. If that ref_store hasn't been
1635 * registered yet, return NULL.
1636 */
1637static struct ref_store *lookup_ref_store_map(struct hashmap *map,
1638                                              const char *name)
1639{
1640        struct ref_store_hash_entry *entry;
1641
1642        if (!map->tablesize)
1643                /* It's initialized on demand in register_ref_store(). */
1644                return NULL;
1645
1646        entry = hashmap_get_from_hash(map, strhash(name), name);
1647        return entry ? entry->refs : NULL;
1648}
1649
1650/*
1651 * Create, record, and return a ref_store instance for the specified
1652 * gitdir.
1653 */
1654static struct ref_store *ref_store_init(const char *gitdir,
1655                                        unsigned int flags)
1656{
1657        const char *be_name = "files";
1658        struct ref_storage_be *be = find_ref_storage_backend(be_name);
1659        struct ref_store *refs;
1660
1661        if (!be)
1662                die("BUG: reference backend %s is unknown", be_name);
1663
1664        refs = be->init(gitdir, flags);
1665        return refs;
1666}
1667
1668struct ref_store *get_main_ref_store(struct repository *r)
1669{
1670        if (r->refs)
1671                return r->refs;
1672
1673        r->refs = ref_store_init(r->gitdir, REF_STORE_ALL_CAPS);
1674        return r->refs;
1675}
1676
1677/*
1678 * Associate a ref store with a name. It is a fatal error to call this
1679 * function twice for the same name.
1680 */
1681static void register_ref_store_map(struct hashmap *map,
1682                                   const char *type,
1683                                   struct ref_store *refs,
1684                                   const char *name)
1685{
1686        if (!map->tablesize)
1687                hashmap_init(map, ref_store_hash_cmp, NULL, 0);
1688
1689        if (hashmap_put(map, alloc_ref_store_hash_entry(name, refs)))
1690                die("BUG: %s ref_store '%s' initialized twice", type, name);
1691}
1692
1693struct ref_store *get_submodule_ref_store(const char *submodule)
1694{
1695        struct strbuf submodule_sb = STRBUF_INIT;
1696        struct ref_store *refs;
1697        char *to_free = NULL;
1698        size_t len;
1699
1700        if (!submodule)
1701                return NULL;
1702
1703        len = strlen(submodule);
1704        while (len && is_dir_sep(submodule[len - 1]))
1705                len--;
1706        if (!len)
1707                return NULL;
1708
1709        if (submodule[len])
1710                /* We need to strip off one or more trailing slashes */
1711                submodule = to_free = xmemdupz(submodule, len);
1712
1713        refs = lookup_ref_store_map(&submodule_ref_stores, submodule);
1714        if (refs)
1715                goto done;
1716
1717        strbuf_addstr(&submodule_sb, submodule);
1718        if (!is_nonbare_repository_dir(&submodule_sb))
1719                goto done;
1720
1721        if (submodule_to_gitdir(&submodule_sb, submodule))
1722                goto done;
1723
1724        /* assume that add_submodule_odb() has been called */
1725        refs = ref_store_init(submodule_sb.buf,
1726                              REF_STORE_READ | REF_STORE_ODB);
1727        register_ref_store_map(&submodule_ref_stores, "submodule",
1728                               refs, submodule);
1729
1730done:
1731        strbuf_release(&submodule_sb);
1732        free(to_free);
1733
1734        return refs;
1735}
1736
1737struct ref_store *get_worktree_ref_store(const struct worktree *wt)
1738{
1739        struct ref_store *refs;
1740        const char *id;
1741
1742        if (wt->is_current)
1743                return get_main_ref_store(the_repository);
1744
1745        id = wt->id ? wt->id : "/";
1746        refs = lookup_ref_store_map(&worktree_ref_stores, id);
1747        if (refs)
1748                return refs;
1749
1750        if (wt->id)
1751                refs = ref_store_init(git_common_path("worktrees/%s", wt->id),
1752                                      REF_STORE_ALL_CAPS);
1753        else
1754                refs = ref_store_init(get_git_common_dir(),
1755                                      REF_STORE_ALL_CAPS);
1756
1757        if (refs)
1758                register_ref_store_map(&worktree_ref_stores, "worktree",
1759                                       refs, id);
1760        return refs;
1761}
1762
1763void base_ref_store_init(struct ref_store *refs,
1764                         const struct ref_storage_be *be)
1765{
1766        refs->be = be;
1767}
1768
1769/* backend functions */
1770int refs_pack_refs(struct ref_store *refs, unsigned int flags)
1771{
1772        return refs->be->pack_refs(refs, flags);
1773}
1774
1775int refs_peel_ref(struct ref_store *refs, const char *refname,
1776                  struct object_id *oid)
1777{
1778        int flag;
1779        struct object_id base;
1780
1781        if (current_ref_iter && current_ref_iter->refname == refname) {
1782                struct object_id peeled;
1783
1784                if (ref_iterator_peel(current_ref_iter, &peeled))
1785                        return -1;
1786                oidcpy(oid, &peeled);
1787                return 0;
1788        }
1789
1790        if (refs_read_ref_full(refs, refname,
1791                               RESOLVE_REF_READING, &base, &flag))
1792                return -1;
1793
1794        return peel_object(&base, oid);
1795}
1796
1797int peel_ref(const char *refname, struct object_id *oid)
1798{
1799        return refs_peel_ref(get_main_ref_store(the_repository), refname, oid);
1800}
1801
1802int refs_create_symref(struct ref_store *refs,
1803                       const char *ref_target,
1804                       const char *refs_heads_master,
1805                       const char *logmsg)
1806{
1807        return refs->be->create_symref(refs, ref_target,
1808                                       refs_heads_master,
1809                                       logmsg);
1810}
1811
1812int create_symref(const char *ref_target, const char *refs_heads_master,
1813                  const char *logmsg)
1814{
1815        return refs_create_symref(get_main_ref_store(the_repository), ref_target,
1816                                  refs_heads_master, logmsg);
1817}
1818
1819int ref_update_reject_duplicates(struct string_list *refnames,
1820                                 struct strbuf *err)
1821{
1822        size_t i, n = refnames->nr;
1823
1824        assert(err);
1825
1826        for (i = 1; i < n; i++) {
1827                int cmp = strcmp(refnames->items[i - 1].string,
1828                                 refnames->items[i].string);
1829
1830                if (!cmp) {
1831                        strbuf_addf(err,
1832                                    "multiple updates for ref '%s' not allowed.",
1833                                    refnames->items[i].string);
1834                        return 1;
1835                } else if (cmp > 0) {
1836                        die("BUG: ref_update_reject_duplicates() received unsorted list");
1837                }
1838        }
1839        return 0;
1840}
1841
1842int ref_transaction_prepare(struct ref_transaction *transaction,
1843                            struct strbuf *err)
1844{
1845        struct ref_store *refs = transaction->ref_store;
1846
1847        switch (transaction->state) {
1848        case REF_TRANSACTION_OPEN:
1849                /* Good. */
1850                break;
1851        case REF_TRANSACTION_PREPARED:
1852                die("BUG: prepare called twice on reference transaction");
1853                break;
1854        case REF_TRANSACTION_CLOSED:
1855                die("BUG: prepare called on a closed reference transaction");
1856                break;
1857        default:
1858                die("BUG: unexpected reference transaction state");
1859                break;
1860        }
1861
1862        if (getenv(GIT_QUARANTINE_ENVIRONMENT)) {
1863                strbuf_addstr(err,
1864                              _("ref updates forbidden inside quarantine environment"));
1865                return -1;
1866        }
1867
1868        return refs->be->transaction_prepare(refs, transaction, err);
1869}
1870
1871int ref_transaction_abort(struct ref_transaction *transaction,
1872                          struct strbuf *err)
1873{
1874        struct ref_store *refs = transaction->ref_store;
1875        int ret = 0;
1876
1877        switch (transaction->state) {
1878        case REF_TRANSACTION_OPEN:
1879                /* No need to abort explicitly. */
1880                break;
1881        case REF_TRANSACTION_PREPARED:
1882                ret = refs->be->transaction_abort(refs, transaction, err);
1883                break;
1884        case REF_TRANSACTION_CLOSED:
1885                die("BUG: abort called on a closed reference transaction");
1886                break;
1887        default:
1888                die("BUG: unexpected reference transaction state");
1889                break;
1890        }
1891
1892        ref_transaction_free(transaction);
1893        return ret;
1894}
1895
1896int ref_transaction_commit(struct ref_transaction *transaction,
1897                           struct strbuf *err)
1898{
1899        struct ref_store *refs = transaction->ref_store;
1900        int ret;
1901
1902        switch (transaction->state) {
1903        case REF_TRANSACTION_OPEN:
1904                /* Need to prepare first. */
1905                ret = ref_transaction_prepare(transaction, err);
1906                if (ret)
1907                        return ret;
1908                break;
1909        case REF_TRANSACTION_PREPARED:
1910                /* Fall through to finish. */
1911                break;
1912        case REF_TRANSACTION_CLOSED:
1913                die("BUG: commit called on a closed reference transaction");
1914                break;
1915        default:
1916                die("BUG: unexpected reference transaction state");
1917                break;
1918        }
1919
1920        return refs->be->transaction_finish(refs, transaction, err);
1921}
1922
1923int refs_verify_refname_available(struct ref_store *refs,
1924                                  const char *refname,
1925                                  const struct string_list *extras,
1926                                  const struct string_list *skip,
1927                                  struct strbuf *err)
1928{
1929        const char *slash;
1930        const char *extra_refname;
1931        struct strbuf dirname = STRBUF_INIT;
1932        struct strbuf referent = STRBUF_INIT;
1933        struct object_id oid;
1934        unsigned int type;
1935        struct ref_iterator *iter;
1936        int ok;
1937        int ret = -1;
1938
1939        /*
1940         * For the sake of comments in this function, suppose that
1941         * refname is "refs/foo/bar".
1942         */
1943
1944        assert(err);
1945
1946        strbuf_grow(&dirname, strlen(refname) + 1);
1947        for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
1948                /* Expand dirname to the new prefix, not including the trailing slash: */
1949                strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len);
1950
1951                /*
1952                 * We are still at a leading dir of the refname (e.g.,
1953                 * "refs/foo"; if there is a reference with that name,
1954                 * it is a conflict, *unless* it is in skip.
1955                 */
1956                if (skip && string_list_has_string(skip, dirname.buf))
1957                        continue;
1958
1959                if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent, &type)) {
1960                        strbuf_addf(err, "'%s' exists; cannot create '%s'",
1961                                    dirname.buf, refname);
1962                        goto cleanup;
1963                }
1964
1965                if (extras && string_list_has_string(extras, dirname.buf)) {
1966                        strbuf_addf(err, "cannot process '%s' and '%s' at the same time",
1967                                    refname, dirname.buf);
1968                        goto cleanup;
1969                }
1970        }
1971
1972        /*
1973         * We are at the leaf of our refname (e.g., "refs/foo/bar").
1974         * There is no point in searching for a reference with that
1975         * name, because a refname isn't considered to conflict with
1976         * itself. But we still need to check for references whose
1977         * names are in the "refs/foo/bar/" namespace, because they
1978         * *do* conflict.
1979         */
1980        strbuf_addstr(&dirname, refname + dirname.len);
1981        strbuf_addch(&dirname, '/');
1982
1983        iter = refs_ref_iterator_begin(refs, dirname.buf, 0,
1984                                       DO_FOR_EACH_INCLUDE_BROKEN);
1985        while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
1986                if (skip &&
1987                    string_list_has_string(skip, iter->refname))
1988                        continue;
1989
1990                strbuf_addf(err, "'%s' exists; cannot create '%s'",
1991                            iter->refname, refname);
1992                ref_iterator_abort(iter);
1993                goto cleanup;
1994        }
1995
1996        if (ok != ITER_DONE)
1997                die("BUG: error while iterating over references");
1998
1999        extra_refname = find_descendant_ref(dirname.buf, extras, skip);
2000        if (extra_refname)
2001                strbuf_addf(err, "cannot process '%s' and '%s' at the same time",
2002                            refname, extra_refname);
2003        else
2004                ret = 0;
2005
2006cleanup:
2007        strbuf_release(&referent);
2008        strbuf_release(&dirname);
2009        return ret;
2010}
2011
2012int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2013{
2014        struct ref_iterator *iter;
2015
2016        iter = refs->be->reflog_iterator_begin(refs);
2017
2018        return do_for_each_ref_iterator(iter, fn, cb_data);
2019}
2020
2021int for_each_reflog(each_ref_fn fn, void *cb_data)
2022{
2023        return refs_for_each_reflog(get_main_ref_store(the_repository), fn, cb_data);
2024}
2025
2026int refs_for_each_reflog_ent_reverse(struct ref_store *refs,
2027                                     const char *refname,
2028                                     each_reflog_ent_fn fn,
2029                                     void *cb_data)
2030{
2031        return refs->be->for_each_reflog_ent_reverse(refs, refname,
2032                                                     fn, cb_data);
2033}
2034
2035int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn,
2036                                void *cb_data)
2037{
2038        return refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository),
2039                                                refname, fn, cb_data);
2040}
2041
2042int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname,
2043                             each_reflog_ent_fn fn, void *cb_data)
2044{
2045        return refs->be->for_each_reflog_ent(refs, refname, fn, cb_data);
2046}
2047
2048int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn,
2049                        void *cb_data)
2050{
2051        return refs_for_each_reflog_ent(get_main_ref_store(the_repository), refname,
2052                                        fn, cb_data);
2053}
2054
2055int refs_reflog_exists(struct ref_store *refs, const char *refname)
2056{
2057        return refs->be->reflog_exists(refs, refname);
2058}
2059
2060int reflog_exists(const char *refname)
2061{
2062        return refs_reflog_exists(get_main_ref_store(the_repository), refname);
2063}
2064
2065int refs_create_reflog(struct ref_store *refs, const char *refname,
2066                       int force_create, struct strbuf *err)
2067{
2068        return refs->be->create_reflog(refs, refname, force_create, err);
2069}
2070
2071int safe_create_reflog(const char *refname, int force_create,
2072                       struct strbuf *err)
2073{
2074        return refs_create_reflog(get_main_ref_store(the_repository), refname,
2075                                  force_create, err);
2076}
2077
2078int refs_delete_reflog(struct ref_store *refs, const char *refname)
2079{
2080        return refs->be->delete_reflog(refs, refname);
2081}
2082
2083int delete_reflog(const char *refname)
2084{
2085        return refs_delete_reflog(get_main_ref_store(the_repository), refname);
2086}
2087
2088int refs_reflog_expire(struct ref_store *refs,
2089                       const char *refname, const struct object_id *oid,
2090                       unsigned int flags,
2091                       reflog_expiry_prepare_fn prepare_fn,
2092                       reflog_expiry_should_prune_fn should_prune_fn,
2093                       reflog_expiry_cleanup_fn cleanup_fn,
2094                       void *policy_cb_data)
2095{
2096        return refs->be->reflog_expire(refs, refname, oid, flags,
2097                                       prepare_fn, should_prune_fn,
2098                                       cleanup_fn, policy_cb_data);
2099}
2100
2101int reflog_expire(const char *refname, const struct object_id *oid,
2102                  unsigned int flags,
2103                  reflog_expiry_prepare_fn prepare_fn,
2104                  reflog_expiry_should_prune_fn should_prune_fn,
2105                  reflog_expiry_cleanup_fn cleanup_fn,
2106                  void *policy_cb_data)
2107{
2108        return refs_reflog_expire(get_main_ref_store(the_repository),
2109                                  refname, oid, flags,
2110                                  prepare_fn, should_prune_fn,
2111                                  cleanup_fn, policy_cb_data);
2112}
2113
2114int initial_ref_transaction_commit(struct ref_transaction *transaction,
2115                                   struct strbuf *err)
2116{
2117        struct ref_store *refs = transaction->ref_store;
2118
2119        return refs->be->initial_transaction_commit(refs, transaction, err);
2120}
2121
2122int refs_delete_refs(struct ref_store *refs, const char *msg,
2123                     struct string_list *refnames, unsigned int flags)
2124{
2125        return refs->be->delete_refs(refs, msg, refnames, flags);
2126}
2127
2128int delete_refs(const char *msg, struct string_list *refnames,
2129                unsigned int flags)
2130{
2131        return refs_delete_refs(get_main_ref_store(the_repository), msg, refnames, flags);
2132}
2133
2134int refs_rename_ref(struct ref_store *refs, const char *oldref,
2135                    const char *newref, const char *logmsg)
2136{
2137        return refs->be->rename_ref(refs, oldref, newref, logmsg);
2138}
2139
2140int rename_ref(const char *oldref, const char *newref, const char *logmsg)
2141{
2142        return refs_rename_ref(get_main_ref_store(the_repository), oldref, newref, logmsg);
2143}
2144
2145int refs_copy_existing_ref(struct ref_store *refs, const char *oldref,
2146                    const char *newref, const char *logmsg)
2147{
2148        return refs->be->copy_ref(refs, oldref, newref, logmsg);
2149}
2150
2151int copy_existing_ref(const char *oldref, const char *newref, const char *logmsg)
2152{
2153        return refs_copy_existing_ref(get_main_ref_store(the_repository), oldref, newref, logmsg);
2154}