refs.con commit builtin/reset: convert to use struct object_id (3a5d7c5)
   1/*
   2 * The backend-independent part of the reference module.
   3 */
   4
   5#include "cache.h"
   6#include "lockfile.h"
   7#include "refs.h"
   8#include "refs/refs-internal.h"
   9#include "object.h"
  10#include "tag.h"
  11
  12/*
  13 * How to handle various characters in refnames:
  14 * 0: An acceptable character for refs
  15 * 1: End-of-component
  16 * 2: ., look for a preceding . to reject .. in refs
  17 * 3: {, look for a preceding @ to reject @{ in refs
  18 * 4: A bad character: ASCII control characters, and
  19 *    ":", "?", "[", "\", "^", "~", SP, or TAB
  20 * 5: *, reject unless REFNAME_REFSPEC_PATTERN is set
  21 */
  22static unsigned char refname_disposition[256] = {
  23        1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  24        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  25        4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 1,
  26        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4,
  27        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  28        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0,
  29        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  30        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
  31};
  32
  33/*
  34 * Try to read one refname component from the front of refname.
  35 * Return the length of the component found, or -1 if the component is
  36 * not legal.  It is legal if it is something reasonable to have under
  37 * ".git/refs/"; We do not like it if:
  38 *
  39 * - any path component of it begins with ".", or
  40 * - it has double dots "..", or
  41 * - it has ASCII control characters, or
  42 * - it has ":", "?", "[", "\", "^", "~", SP, or TAB anywhere, or
  43 * - it has "*" anywhere unless REFNAME_REFSPEC_PATTERN is set, or
  44 * - it ends with a "/", or
  45 * - it ends with ".lock", or
  46 * - it contains a "@{" portion
  47 */
  48static int check_refname_component(const char *refname, int *flags)
  49{
  50        const char *cp;
  51        char last = '\0';
  52
  53        for (cp = refname; ; cp++) {
  54                int ch = *cp & 255;
  55                unsigned char disp = refname_disposition[ch];
  56                switch (disp) {
  57                case 1:
  58                        goto out;
  59                case 2:
  60                        if (last == '.')
  61                                return -1; /* Refname contains "..". */
  62                        break;
  63                case 3:
  64                        if (last == '@')
  65                                return -1; /* Refname contains "@{". */
  66                        break;
  67                case 4:
  68                        return -1;
  69                case 5:
  70                        if (!(*flags & REFNAME_REFSPEC_PATTERN))
  71                                return -1; /* refspec can't be a pattern */
  72
  73                        /*
  74                         * Unset the pattern flag so that we only accept
  75                         * a single asterisk for one side of refspec.
  76                         */
  77                        *flags &= ~ REFNAME_REFSPEC_PATTERN;
  78                        break;
  79                }
  80                last = ch;
  81        }
  82out:
  83        if (cp == refname)
  84                return 0; /* Component has zero length. */
  85        if (refname[0] == '.')
  86                return -1; /* Component starts with '.'. */
  87        if (cp - refname >= LOCK_SUFFIX_LEN &&
  88            !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN))
  89                return -1; /* Refname ends with ".lock". */
  90        return cp - refname;
  91}
  92
  93int check_refname_format(const char *refname, int flags)
  94{
  95        int component_len, component_count = 0;
  96
  97        if (!strcmp(refname, "@"))
  98                /* Refname is a single character '@'. */
  99                return -1;
 100
 101        while (1) {
 102                /* We are at the start of a path component. */
 103                component_len = check_refname_component(refname, &flags);
 104                if (component_len <= 0)
 105                        return -1;
 106
 107                component_count++;
 108                if (refname[component_len] == '\0')
 109                        break;
 110                /* Skip to next component. */
 111                refname += component_len + 1;
 112        }
 113
 114        if (refname[component_len - 1] == '.')
 115                return -1; /* Refname ends with '.'. */
 116        if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2)
 117                return -1; /* Refname has only one component. */
 118        return 0;
 119}
 120
 121int refname_is_safe(const char *refname)
 122{
 123        const char *rest;
 124
 125        if (skip_prefix(refname, "refs/", &rest)) {
 126                char *buf;
 127                int result;
 128                size_t restlen = strlen(rest);
 129
 130                /* rest must not be empty, or start or end with "/" */
 131                if (!restlen || *rest == '/' || rest[restlen - 1] == '/')
 132                        return 0;
 133
 134                /*
 135                 * Does the refname try to escape refs/?
 136                 * For example: refs/foo/../bar is safe but refs/foo/../../bar
 137                 * is not.
 138                 */
 139                buf = xmallocz(restlen);
 140                result = !normalize_path_copy(buf, rest) && !strcmp(buf, rest);
 141                free(buf);
 142                return result;
 143        }
 144
 145        do {
 146                if (!isupper(*refname) && *refname != '_')
 147                        return 0;
 148                refname++;
 149        } while (*refname);
 150        return 1;
 151}
 152
 153char *resolve_refdup(const char *refname, int resolve_flags,
 154                     unsigned char *sha1, int *flags)
 155{
 156        return xstrdup_or_null(resolve_ref_unsafe(refname, resolve_flags,
 157                                                  sha1, flags));
 158}
 159
 160/* The argument to filter_refs */
 161struct ref_filter {
 162        const char *pattern;
 163        each_ref_fn *fn;
 164        void *cb_data;
 165};
 166
 167int read_ref_full(const char *refname, int resolve_flags, unsigned char *sha1, int *flags)
 168{
 169        if (resolve_ref_unsafe(refname, resolve_flags, sha1, flags))
 170                return 0;
 171        return -1;
 172}
 173
 174int read_ref(const char *refname, unsigned char *sha1)
 175{
 176        return read_ref_full(refname, RESOLVE_REF_READING, sha1, NULL);
 177}
 178
 179int ref_exists(const char *refname)
 180{
 181        unsigned char sha1[20];
 182        return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, sha1, NULL);
 183}
 184
 185static int filter_refs(const char *refname, const struct object_id *oid,
 186                           int flags, void *data)
 187{
 188        struct ref_filter *filter = (struct ref_filter *)data;
 189
 190        if (wildmatch(filter->pattern, refname, 0, NULL))
 191                return 0;
 192        return filter->fn(refname, oid, flags, filter->cb_data);
 193}
 194
 195enum peel_status peel_object(const unsigned char *name, unsigned char *sha1)
 196{
 197        struct object *o = lookup_unknown_object(name);
 198
 199        if (o->type == OBJ_NONE) {
 200                int type = sha1_object_info(name, NULL);
 201                if (type < 0 || !object_as_type(o, type, 0))
 202                        return PEEL_INVALID;
 203        }
 204
 205        if (o->type != OBJ_TAG)
 206                return PEEL_NON_TAG;
 207
 208        o = deref_tag_noverify(o);
 209        if (!o)
 210                return PEEL_INVALID;
 211
 212        hashcpy(sha1, o->oid.hash);
 213        return PEEL_PEELED;
 214}
 215
 216struct warn_if_dangling_data {
 217        FILE *fp;
 218        const char *refname;
 219        const struct string_list *refnames;
 220        const char *msg_fmt;
 221};
 222
 223static int warn_if_dangling_symref(const char *refname, const struct object_id *oid,
 224                                   int flags, void *cb_data)
 225{
 226        struct warn_if_dangling_data *d = cb_data;
 227        const char *resolves_to;
 228        struct object_id junk;
 229
 230        if (!(flags & REF_ISSYMREF))
 231                return 0;
 232
 233        resolves_to = resolve_ref_unsafe(refname, 0, junk.hash, NULL);
 234        if (!resolves_to
 235            || (d->refname
 236                ? strcmp(resolves_to, d->refname)
 237                : !string_list_has_string(d->refnames, resolves_to))) {
 238                return 0;
 239        }
 240
 241        fprintf(d->fp, d->msg_fmt, refname);
 242        fputc('\n', d->fp);
 243        return 0;
 244}
 245
 246void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
 247{
 248        struct warn_if_dangling_data data;
 249
 250        data.fp = fp;
 251        data.refname = refname;
 252        data.refnames = NULL;
 253        data.msg_fmt = msg_fmt;
 254        for_each_rawref(warn_if_dangling_symref, &data);
 255}
 256
 257void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames)
 258{
 259        struct warn_if_dangling_data data;
 260
 261        data.fp = fp;
 262        data.refname = NULL;
 263        data.refnames = refnames;
 264        data.msg_fmt = msg_fmt;
 265        for_each_rawref(warn_if_dangling_symref, &data);
 266}
 267
 268int for_each_tag_ref(each_ref_fn fn, void *cb_data)
 269{
 270        return for_each_ref_in("refs/tags/", fn, cb_data);
 271}
 272
 273int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
 274{
 275        return for_each_ref_in_submodule(submodule, "refs/tags/", fn, cb_data);
 276}
 277
 278int for_each_branch_ref(each_ref_fn fn, void *cb_data)
 279{
 280        return for_each_ref_in("refs/heads/", fn, cb_data);
 281}
 282
 283int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
 284{
 285        return for_each_ref_in_submodule(submodule, "refs/heads/", fn, cb_data);
 286}
 287
 288int for_each_remote_ref(each_ref_fn fn, void *cb_data)
 289{
 290        return for_each_ref_in("refs/remotes/", fn, cb_data);
 291}
 292
 293int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
 294{
 295        return for_each_ref_in_submodule(submodule, "refs/remotes/", fn, cb_data);
 296}
 297
 298int head_ref_namespaced(each_ref_fn fn, void *cb_data)
 299{
 300        struct strbuf buf = STRBUF_INIT;
 301        int ret = 0;
 302        struct object_id oid;
 303        int flag;
 304
 305        strbuf_addf(&buf, "%sHEAD", get_git_namespace());
 306        if (!read_ref_full(buf.buf, RESOLVE_REF_READING, oid.hash, &flag))
 307                ret = fn(buf.buf, &oid, flag, cb_data);
 308        strbuf_release(&buf);
 309
 310        return ret;
 311}
 312
 313int for_each_glob_ref_in(each_ref_fn fn, const char *pattern,
 314        const char *prefix, void *cb_data)
 315{
 316        struct strbuf real_pattern = STRBUF_INIT;
 317        struct ref_filter filter;
 318        int ret;
 319
 320        if (!prefix && !starts_with(pattern, "refs/"))
 321                strbuf_addstr(&real_pattern, "refs/");
 322        else if (prefix)
 323                strbuf_addstr(&real_pattern, prefix);
 324        strbuf_addstr(&real_pattern, pattern);
 325
 326        if (!has_glob_specials(pattern)) {
 327                /* Append implied '/' '*' if not present. */
 328                strbuf_complete(&real_pattern, '/');
 329                /* No need to check for '*', there is none. */
 330                strbuf_addch(&real_pattern, '*');
 331        }
 332
 333        filter.pattern = real_pattern.buf;
 334        filter.fn = fn;
 335        filter.cb_data = cb_data;
 336        ret = for_each_ref(filter_refs, &filter);
 337
 338        strbuf_release(&real_pattern);
 339        return ret;
 340}
 341
 342int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
 343{
 344        return for_each_glob_ref_in(fn, pattern, NULL, cb_data);
 345}
 346
 347const char *prettify_refname(const char *name)
 348{
 349        return name + (
 350                starts_with(name, "refs/heads/") ? 11 :
 351                starts_with(name, "refs/tags/") ? 10 :
 352                starts_with(name, "refs/remotes/") ? 13 :
 353                0);
 354}
 355
 356static const char *ref_rev_parse_rules[] = {
 357        "%.*s",
 358        "refs/%.*s",
 359        "refs/tags/%.*s",
 360        "refs/heads/%.*s",
 361        "refs/remotes/%.*s",
 362        "refs/remotes/%.*s/HEAD",
 363        NULL
 364};
 365
 366int refname_match(const char *abbrev_name, const char *full_name)
 367{
 368        const char **p;
 369        const int abbrev_name_len = strlen(abbrev_name);
 370
 371        for (p = ref_rev_parse_rules; *p; p++) {
 372                if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) {
 373                        return 1;
 374                }
 375        }
 376
 377        return 0;
 378}
 379
 380/*
 381 * *string and *len will only be substituted, and *string returned (for
 382 * later free()ing) if the string passed in is a magic short-hand form
 383 * to name a branch.
 384 */
 385static char *substitute_branch_name(const char **string, int *len)
 386{
 387        struct strbuf buf = STRBUF_INIT;
 388        int ret = interpret_branch_name(*string, *len, &buf);
 389
 390        if (ret == *len) {
 391                size_t size;
 392                *string = strbuf_detach(&buf, &size);
 393                *len = size;
 394                return (char *)*string;
 395        }
 396
 397        return NULL;
 398}
 399
 400int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
 401{
 402        char *last_branch = substitute_branch_name(&str, &len);
 403        const char **p, *r;
 404        int refs_found = 0;
 405
 406        *ref = NULL;
 407        for (p = ref_rev_parse_rules; *p; p++) {
 408                char fullref[PATH_MAX];
 409                unsigned char sha1_from_ref[20];
 410                unsigned char *this_result;
 411                int flag;
 412
 413                this_result = refs_found ? sha1_from_ref : sha1;
 414                mksnpath(fullref, sizeof(fullref), *p, len, str);
 415                r = resolve_ref_unsafe(fullref, RESOLVE_REF_READING,
 416                                       this_result, &flag);
 417                if (r) {
 418                        if (!refs_found++)
 419                                *ref = xstrdup(r);
 420                        if (!warn_ambiguous_refs)
 421                                break;
 422                } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD")) {
 423                        warning("ignoring dangling symref %s.", fullref);
 424                } else if ((flag & REF_ISBROKEN) && strchr(fullref, '/')) {
 425                        warning("ignoring broken ref %s.", fullref);
 426                }
 427        }
 428        free(last_branch);
 429        return refs_found;
 430}
 431
 432int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
 433{
 434        char *last_branch = substitute_branch_name(&str, &len);
 435        const char **p;
 436        int logs_found = 0;
 437
 438        *log = NULL;
 439        for (p = ref_rev_parse_rules; *p; p++) {
 440                unsigned char hash[20];
 441                char path[PATH_MAX];
 442                const char *ref, *it;
 443
 444                mksnpath(path, sizeof(path), *p, len, str);
 445                ref = resolve_ref_unsafe(path, RESOLVE_REF_READING,
 446                                         hash, NULL);
 447                if (!ref)
 448                        continue;
 449                if (reflog_exists(path))
 450                        it = path;
 451                else if (strcmp(ref, path) && reflog_exists(ref))
 452                        it = ref;
 453                else
 454                        continue;
 455                if (!logs_found++) {
 456                        *log = xstrdup(it);
 457                        hashcpy(sha1, hash);
 458                }
 459                if (!warn_ambiguous_refs)
 460                        break;
 461        }
 462        free(last_branch);
 463        return logs_found;
 464}
 465
 466static int is_per_worktree_ref(const char *refname)
 467{
 468        return !strcmp(refname, "HEAD") ||
 469                starts_with(refname, "refs/bisect/");
 470}
 471
 472static int is_pseudoref_syntax(const char *refname)
 473{
 474        const char *c;
 475
 476        for (c = refname; *c; c++) {
 477                if (!isupper(*c) && *c != '-' && *c != '_')
 478                        return 0;
 479        }
 480
 481        return 1;
 482}
 483
 484enum ref_type ref_type(const char *refname)
 485{
 486        if (is_per_worktree_ref(refname))
 487                return REF_TYPE_PER_WORKTREE;
 488        if (is_pseudoref_syntax(refname))
 489                return REF_TYPE_PSEUDOREF;
 490       return REF_TYPE_NORMAL;
 491}
 492
 493static int write_pseudoref(const char *pseudoref, const unsigned char *sha1,
 494                           const unsigned char *old_sha1, struct strbuf *err)
 495{
 496        const char *filename;
 497        int fd;
 498        static struct lock_file lock;
 499        struct strbuf buf = STRBUF_INIT;
 500        int ret = -1;
 501
 502        strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1));
 503
 504        filename = git_path("%s", pseudoref);
 505        fd = hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
 506        if (fd < 0) {
 507                strbuf_addf(err, "could not open '%s' for writing: %s",
 508                            filename, strerror(errno));
 509                return -1;
 510        }
 511
 512        if (old_sha1) {
 513                unsigned char actual_old_sha1[20];
 514
 515                if (read_ref(pseudoref, actual_old_sha1))
 516                        die("could not read ref '%s'", pseudoref);
 517                if (hashcmp(actual_old_sha1, old_sha1)) {
 518                        strbuf_addf(err, "unexpected sha1 when writing '%s'", pseudoref);
 519                        rollback_lock_file(&lock);
 520                        goto done;
 521                }
 522        }
 523
 524        if (write_in_full(fd, buf.buf, buf.len) != buf.len) {
 525                strbuf_addf(err, "could not write to '%s'", filename);
 526                rollback_lock_file(&lock);
 527                goto done;
 528        }
 529
 530        commit_lock_file(&lock);
 531        ret = 0;
 532done:
 533        strbuf_release(&buf);
 534        return ret;
 535}
 536
 537static int delete_pseudoref(const char *pseudoref, const unsigned char *old_sha1)
 538{
 539        static struct lock_file lock;
 540        const char *filename;
 541
 542        filename = git_path("%s", pseudoref);
 543
 544        if (old_sha1 && !is_null_sha1(old_sha1)) {
 545                int fd;
 546                unsigned char actual_old_sha1[20];
 547
 548                fd = hold_lock_file_for_update(&lock, filename,
 549                                               LOCK_DIE_ON_ERROR);
 550                if (fd < 0)
 551                        die_errno(_("Could not open '%s' for writing"), filename);
 552                if (read_ref(pseudoref, actual_old_sha1))
 553                        die("could not read ref '%s'", pseudoref);
 554                if (hashcmp(actual_old_sha1, old_sha1)) {
 555                        warning("Unexpected sha1 when deleting %s", pseudoref);
 556                        rollback_lock_file(&lock);
 557                        return -1;
 558                }
 559
 560                unlink(filename);
 561                rollback_lock_file(&lock);
 562        } else {
 563                unlink(filename);
 564        }
 565
 566        return 0;
 567}
 568
 569int delete_ref(const char *refname, const unsigned char *old_sha1,
 570               unsigned int flags)
 571{
 572        struct ref_transaction *transaction;
 573        struct strbuf err = STRBUF_INIT;
 574
 575        if (ref_type(refname) == REF_TYPE_PSEUDOREF)
 576                return delete_pseudoref(refname, old_sha1);
 577
 578        transaction = ref_transaction_begin(&err);
 579        if (!transaction ||
 580            ref_transaction_delete(transaction, refname, old_sha1,
 581                                   flags, NULL, &err) ||
 582            ref_transaction_commit(transaction, &err)) {
 583                error("%s", err.buf);
 584                ref_transaction_free(transaction);
 585                strbuf_release(&err);
 586                return 1;
 587        }
 588        ref_transaction_free(transaction);
 589        strbuf_release(&err);
 590        return 0;
 591}
 592
 593int copy_reflog_msg(char *buf, const char *msg)
 594{
 595        char *cp = buf;
 596        char c;
 597        int wasspace = 1;
 598
 599        *cp++ = '\t';
 600        while ((c = *msg++)) {
 601                if (wasspace && isspace(c))
 602                        continue;
 603                wasspace = isspace(c);
 604                if (wasspace)
 605                        c = ' ';
 606                *cp++ = c;
 607        }
 608        while (buf < cp && isspace(cp[-1]))
 609                cp--;
 610        *cp++ = '\n';
 611        return cp - buf;
 612}
 613
 614int should_autocreate_reflog(const char *refname)
 615{
 616        if (!log_all_ref_updates)
 617                return 0;
 618        return starts_with(refname, "refs/heads/") ||
 619                starts_with(refname, "refs/remotes/") ||
 620                starts_with(refname, "refs/notes/") ||
 621                !strcmp(refname, "HEAD");
 622}
 623
 624int is_branch(const char *refname)
 625{
 626        return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/");
 627}
 628
 629struct read_ref_at_cb {
 630        const char *refname;
 631        unsigned long at_time;
 632        int cnt;
 633        int reccnt;
 634        unsigned char *sha1;
 635        int found_it;
 636
 637        unsigned char osha1[20];
 638        unsigned char nsha1[20];
 639        int tz;
 640        unsigned long date;
 641        char **msg;
 642        unsigned long *cutoff_time;
 643        int *cutoff_tz;
 644        int *cutoff_cnt;
 645};
 646
 647static int read_ref_at_ent(unsigned char *osha1, unsigned char *nsha1,
 648                const char *email, unsigned long timestamp, int tz,
 649                const char *message, void *cb_data)
 650{
 651        struct read_ref_at_cb *cb = cb_data;
 652
 653        cb->reccnt++;
 654        cb->tz = tz;
 655        cb->date = timestamp;
 656
 657        if (timestamp <= cb->at_time || cb->cnt == 0) {
 658                if (cb->msg)
 659                        *cb->msg = xstrdup(message);
 660                if (cb->cutoff_time)
 661                        *cb->cutoff_time = timestamp;
 662                if (cb->cutoff_tz)
 663                        *cb->cutoff_tz = tz;
 664                if (cb->cutoff_cnt)
 665                        *cb->cutoff_cnt = cb->reccnt - 1;
 666                /*
 667                 * we have not yet updated cb->[n|o]sha1 so they still
 668                 * hold the values for the previous record.
 669                 */
 670                if (!is_null_sha1(cb->osha1)) {
 671                        hashcpy(cb->sha1, nsha1);
 672                        if (hashcmp(cb->osha1, nsha1))
 673                                warning("Log for ref %s has gap after %s.",
 674                                        cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
 675                }
 676                else if (cb->date == cb->at_time)
 677                        hashcpy(cb->sha1, nsha1);
 678                else if (hashcmp(nsha1, cb->sha1))
 679                        warning("Log for ref %s unexpectedly ended on %s.",
 680                                cb->refname, show_date(cb->date, cb->tz,
 681                                                       DATE_MODE(RFC2822)));
 682                hashcpy(cb->osha1, osha1);
 683                hashcpy(cb->nsha1, nsha1);
 684                cb->found_it = 1;
 685                return 1;
 686        }
 687        hashcpy(cb->osha1, osha1);
 688        hashcpy(cb->nsha1, nsha1);
 689        if (cb->cnt > 0)
 690                cb->cnt--;
 691        return 0;
 692}
 693
 694static int read_ref_at_ent_oldest(unsigned char *osha1, unsigned char *nsha1,
 695                                  const char *email, unsigned long timestamp,
 696                                  int tz, const char *message, void *cb_data)
 697{
 698        struct read_ref_at_cb *cb = cb_data;
 699
 700        if (cb->msg)
 701                *cb->msg = xstrdup(message);
 702        if (cb->cutoff_time)
 703                *cb->cutoff_time = timestamp;
 704        if (cb->cutoff_tz)
 705                *cb->cutoff_tz = tz;
 706        if (cb->cutoff_cnt)
 707                *cb->cutoff_cnt = cb->reccnt;
 708        hashcpy(cb->sha1, osha1);
 709        if (is_null_sha1(cb->sha1))
 710                hashcpy(cb->sha1, nsha1);
 711        /* We just want the first entry */
 712        return 1;
 713}
 714
 715int read_ref_at(const char *refname, unsigned int flags, unsigned long at_time, int cnt,
 716                unsigned char *sha1, char **msg,
 717                unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
 718{
 719        struct read_ref_at_cb cb;
 720
 721        memset(&cb, 0, sizeof(cb));
 722        cb.refname = refname;
 723        cb.at_time = at_time;
 724        cb.cnt = cnt;
 725        cb.msg = msg;
 726        cb.cutoff_time = cutoff_time;
 727        cb.cutoff_tz = cutoff_tz;
 728        cb.cutoff_cnt = cutoff_cnt;
 729        cb.sha1 = sha1;
 730
 731        for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb);
 732
 733        if (!cb.reccnt) {
 734                if (flags & GET_SHA1_QUIETLY)
 735                        exit(128);
 736                else
 737                        die("Log for %s is empty.", refname);
 738        }
 739        if (cb.found_it)
 740                return 0;
 741
 742        for_each_reflog_ent(refname, read_ref_at_ent_oldest, &cb);
 743
 744        return 1;
 745}
 746
 747struct ref_transaction *ref_transaction_begin(struct strbuf *err)
 748{
 749        assert(err);
 750
 751        return xcalloc(1, sizeof(struct ref_transaction));
 752}
 753
 754void ref_transaction_free(struct ref_transaction *transaction)
 755{
 756        int i;
 757
 758        if (!transaction)
 759                return;
 760
 761        for (i = 0; i < transaction->nr; i++) {
 762                free(transaction->updates[i]->msg);
 763                free(transaction->updates[i]);
 764        }
 765        free(transaction->updates);
 766        free(transaction);
 767}
 768
 769struct ref_update *ref_transaction_add_update(
 770                struct ref_transaction *transaction,
 771                const char *refname, unsigned int flags,
 772                const unsigned char *new_sha1,
 773                const unsigned char *old_sha1,
 774                const char *msg)
 775{
 776        struct ref_update *update;
 777
 778        if (transaction->state != REF_TRANSACTION_OPEN)
 779                die("BUG: update called for transaction that is not open");
 780
 781        if ((flags & REF_ISPRUNING) && !(flags & REF_NODEREF))
 782                die("BUG: REF_ISPRUNING set without REF_NODEREF");
 783
 784        FLEX_ALLOC_STR(update, refname, refname);
 785        ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc);
 786        transaction->updates[transaction->nr++] = update;
 787
 788        update->flags = flags;
 789
 790        if (flags & REF_HAVE_NEW)
 791                hashcpy(update->new_sha1, new_sha1);
 792        if (flags & REF_HAVE_OLD)
 793                hashcpy(update->old_sha1, old_sha1);
 794        if (msg)
 795                update->msg = xstrdup(msg);
 796        return update;
 797}
 798
 799int ref_transaction_update(struct ref_transaction *transaction,
 800                           const char *refname,
 801                           const unsigned char *new_sha1,
 802                           const unsigned char *old_sha1,
 803                           unsigned int flags, const char *msg,
 804                           struct strbuf *err)
 805{
 806        assert(err);
 807
 808        if ((new_sha1 && !is_null_sha1(new_sha1)) ?
 809            check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
 810            !refname_is_safe(refname)) {
 811                strbuf_addf(err, "refusing to update ref with bad name '%s'",
 812                            refname);
 813                return -1;
 814        }
 815
 816        flags |= (new_sha1 ? REF_HAVE_NEW : 0) | (old_sha1 ? REF_HAVE_OLD : 0);
 817
 818        ref_transaction_add_update(transaction, refname, flags,
 819                                   new_sha1, old_sha1, msg);
 820        return 0;
 821}
 822
 823int ref_transaction_create(struct ref_transaction *transaction,
 824                           const char *refname,
 825                           const unsigned char *new_sha1,
 826                           unsigned int flags, const char *msg,
 827                           struct strbuf *err)
 828{
 829        if (!new_sha1 || is_null_sha1(new_sha1))
 830                die("BUG: create called without valid new_sha1");
 831        return ref_transaction_update(transaction, refname, new_sha1,
 832                                      null_sha1, flags, msg, err);
 833}
 834
 835int ref_transaction_delete(struct ref_transaction *transaction,
 836                           const char *refname,
 837                           const unsigned char *old_sha1,
 838                           unsigned int flags, const char *msg,
 839                           struct strbuf *err)
 840{
 841        if (old_sha1 && is_null_sha1(old_sha1))
 842                die("BUG: delete called with old_sha1 set to zeros");
 843        return ref_transaction_update(transaction, refname,
 844                                      null_sha1, old_sha1,
 845                                      flags, msg, err);
 846}
 847
 848int ref_transaction_verify(struct ref_transaction *transaction,
 849                           const char *refname,
 850                           const unsigned char *old_sha1,
 851                           unsigned int flags,
 852                           struct strbuf *err)
 853{
 854        if (!old_sha1)
 855                die("BUG: verify called with old_sha1 set to NULL");
 856        return ref_transaction_update(transaction, refname,
 857                                      NULL, old_sha1,
 858                                      flags, NULL, err);
 859}
 860
 861int update_ref_oid(const char *msg, const char *refname,
 862               const struct object_id *new_oid, const struct object_id *old_oid,
 863               unsigned int flags, enum action_on_err onerr)
 864{
 865        return update_ref(msg, refname, new_oid ? new_oid->hash : NULL,
 866                old_oid ? old_oid->hash : NULL, flags, onerr);
 867}
 868
 869int update_ref(const char *msg, const char *refname,
 870               const unsigned char *new_sha1, const unsigned char *old_sha1,
 871               unsigned int flags, enum action_on_err onerr)
 872{
 873        struct ref_transaction *t = NULL;
 874        struct strbuf err = STRBUF_INIT;
 875        int ret = 0;
 876
 877        if (ref_type(refname) == REF_TYPE_PSEUDOREF) {
 878                ret = write_pseudoref(refname, new_sha1, old_sha1, &err);
 879        } else {
 880                t = ref_transaction_begin(&err);
 881                if (!t ||
 882                    ref_transaction_update(t, refname, new_sha1, old_sha1,
 883                                           flags, msg, &err) ||
 884                    ref_transaction_commit(t, &err)) {
 885                        ret = 1;
 886                        ref_transaction_free(t);
 887                }
 888        }
 889        if (ret) {
 890                const char *str = "update_ref failed for ref '%s': %s";
 891
 892                switch (onerr) {
 893                case UPDATE_REFS_MSG_ON_ERR:
 894                        error(str, refname, err.buf);
 895                        break;
 896                case UPDATE_REFS_DIE_ON_ERR:
 897                        die(str, refname, err.buf);
 898                        break;
 899                case UPDATE_REFS_QUIET_ON_ERR:
 900                        break;
 901                }
 902                strbuf_release(&err);
 903                return 1;
 904        }
 905        strbuf_release(&err);
 906        if (t)
 907                ref_transaction_free(t);
 908        return 0;
 909}
 910
 911char *shorten_unambiguous_ref(const char *refname, int strict)
 912{
 913        int i;
 914        static char **scanf_fmts;
 915        static int nr_rules;
 916        char *short_name;
 917
 918        if (!nr_rules) {
 919                /*
 920                 * Pre-generate scanf formats from ref_rev_parse_rules[].
 921                 * Generate a format suitable for scanf from a
 922                 * ref_rev_parse_rules rule by interpolating "%s" at the
 923                 * location of the "%.*s".
 924                 */
 925                size_t total_len = 0;
 926                size_t offset = 0;
 927
 928                /* the rule list is NULL terminated, count them first */
 929                for (nr_rules = 0; ref_rev_parse_rules[nr_rules]; nr_rules++)
 930                        /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
 931                        total_len += strlen(ref_rev_parse_rules[nr_rules]) - 2 + 1;
 932
 933                scanf_fmts = xmalloc(st_add(st_mult(sizeof(char *), nr_rules), total_len));
 934
 935                offset = 0;
 936                for (i = 0; i < nr_rules; i++) {
 937                        assert(offset < total_len);
 938                        scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset;
 939                        offset += snprintf(scanf_fmts[i], total_len - offset,
 940                                           ref_rev_parse_rules[i], 2, "%s") + 1;
 941                }
 942        }
 943
 944        /* bail out if there are no rules */
 945        if (!nr_rules)
 946                return xstrdup(refname);
 947
 948        /* buffer for scanf result, at most refname must fit */
 949        short_name = xstrdup(refname);
 950
 951        /* skip first rule, it will always match */
 952        for (i = nr_rules - 1; i > 0 ; --i) {
 953                int j;
 954                int rules_to_fail = i;
 955                int short_name_len;
 956
 957                if (1 != sscanf(refname, scanf_fmts[i], short_name))
 958                        continue;
 959
 960                short_name_len = strlen(short_name);
 961
 962                /*
 963                 * in strict mode, all (except the matched one) rules
 964                 * must fail to resolve to a valid non-ambiguous ref
 965                 */
 966                if (strict)
 967                        rules_to_fail = nr_rules;
 968
 969                /*
 970                 * check if the short name resolves to a valid ref,
 971                 * but use only rules prior to the matched one
 972                 */
 973                for (j = 0; j < rules_to_fail; j++) {
 974                        const char *rule = ref_rev_parse_rules[j];
 975                        char refname[PATH_MAX];
 976
 977                        /* skip matched rule */
 978                        if (i == j)
 979                                continue;
 980
 981                        /*
 982                         * the short name is ambiguous, if it resolves
 983                         * (with this previous rule) to a valid ref
 984                         * read_ref() returns 0 on success
 985                         */
 986                        mksnpath(refname, sizeof(refname),
 987                                 rule, short_name_len, short_name);
 988                        if (ref_exists(refname))
 989                                break;
 990                }
 991
 992                /*
 993                 * short name is non-ambiguous if all previous rules
 994                 * haven't resolved to a valid ref
 995                 */
 996                if (j == rules_to_fail)
 997                        return short_name;
 998        }
 999
1000        free(short_name);
1001        return xstrdup(refname);
1002}
1003
1004static struct string_list *hide_refs;
1005
1006int parse_hide_refs_config(const char *var, const char *value, const char *section)
1007{
1008        if (!strcmp("transfer.hiderefs", var) ||
1009            /* NEEDSWORK: use parse_config_key() once both are merged */
1010            (starts_with(var, section) && var[strlen(section)] == '.' &&
1011             !strcmp(var + strlen(section), ".hiderefs"))) {
1012                char *ref;
1013                int len;
1014
1015                if (!value)
1016                        return config_error_nonbool(var);
1017                ref = xstrdup(value);
1018                len = strlen(ref);
1019                while (len && ref[len - 1] == '/')
1020                        ref[--len] = '\0';
1021                if (!hide_refs) {
1022                        hide_refs = xcalloc(1, sizeof(*hide_refs));
1023                        hide_refs->strdup_strings = 1;
1024                }
1025                string_list_append(hide_refs, ref);
1026        }
1027        return 0;
1028}
1029
1030int ref_is_hidden(const char *refname, const char *refname_full)
1031{
1032        int i;
1033
1034        if (!hide_refs)
1035                return 0;
1036        for (i = hide_refs->nr - 1; i >= 0; i--) {
1037                const char *match = hide_refs->items[i].string;
1038                const char *subject;
1039                int neg = 0;
1040                int len;
1041
1042                if (*match == '!') {
1043                        neg = 1;
1044                        match++;
1045                }
1046
1047                if (*match == '^') {
1048                        subject = refname_full;
1049                        match++;
1050                } else {
1051                        subject = refname;
1052                }
1053
1054                /* refname can be NULL when namespaces are used. */
1055                if (!subject || !starts_with(subject, match))
1056                        continue;
1057                len = strlen(match);
1058                if (!subject[len] || subject[len] == '/')
1059                        return !neg;
1060        }
1061        return 0;
1062}
1063
1064const char *find_descendant_ref(const char *dirname,
1065                                const struct string_list *extras,
1066                                const struct string_list *skip)
1067{
1068        int pos;
1069
1070        if (!extras)
1071                return NULL;
1072
1073        /*
1074         * Look at the place where dirname would be inserted into
1075         * extras. If there is an entry at that position that starts
1076         * with dirname (remember, dirname includes the trailing
1077         * slash) and is not in skip, then we have a conflict.
1078         */
1079        for (pos = string_list_find_insert_index(extras, dirname, 0);
1080             pos < extras->nr; pos++) {
1081                const char *extra_refname = extras->items[pos].string;
1082
1083                if (!starts_with(extra_refname, dirname))
1084                        break;
1085
1086                if (!skip || !string_list_has_string(skip, extra_refname))
1087                        return extra_refname;
1088        }
1089        return NULL;
1090}
1091
1092int rename_ref_available(const char *oldname, const char *newname)
1093{
1094        struct string_list skip = STRING_LIST_INIT_NODUP;
1095        struct strbuf err = STRBUF_INIT;
1096        int ret;
1097
1098        string_list_insert(&skip, oldname);
1099        ret = !verify_refname_available(newname, NULL, &skip, &err);
1100        if (!ret)
1101                error("%s", err.buf);
1102
1103        string_list_clear(&skip, 0);
1104        strbuf_release(&err);
1105        return ret;
1106}
1107
1108int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1109{
1110        struct object_id oid;
1111        int flag;
1112
1113        if (submodule) {
1114                if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0)
1115                        return fn("HEAD", &oid, 0, cb_data);
1116
1117                return 0;
1118        }
1119
1120        if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag))
1121                return fn("HEAD", &oid, flag, cb_data);
1122
1123        return 0;
1124}
1125
1126int head_ref(each_ref_fn fn, void *cb_data)
1127{
1128        return head_ref_submodule(NULL, fn, cb_data);
1129}
1130
1131/*
1132 * Call fn for each reference in the specified submodule for which the
1133 * refname begins with prefix. If trim is non-zero, then trim that
1134 * many characters off the beginning of each refname before passing
1135 * the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to
1136 * include broken references in the iteration. If fn ever returns a
1137 * non-zero value, stop the iteration and return that value;
1138 * otherwise, return 0.
1139 */
1140static int do_for_each_ref(const char *submodule, const char *prefix,
1141                           each_ref_fn fn, int trim, int flags, void *cb_data)
1142{
1143        struct ref_iterator *iter;
1144
1145        iter = files_ref_iterator_begin(submodule, prefix, flags);
1146        iter = prefix_ref_iterator_begin(iter, prefix, trim);
1147
1148        return do_for_each_ref_iterator(iter, fn, cb_data);
1149}
1150
1151int for_each_ref(each_ref_fn fn, void *cb_data)
1152{
1153        return do_for_each_ref(NULL, "", fn, 0, 0, cb_data);
1154}
1155
1156int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1157{
1158        return do_for_each_ref(submodule, "", fn, 0, 0, cb_data);
1159}
1160
1161int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
1162{
1163        return do_for_each_ref(NULL, prefix, fn, strlen(prefix), 0, cb_data);
1164}
1165
1166int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
1167{
1168        unsigned int flag = 0;
1169
1170        if (broken)
1171                flag = DO_FOR_EACH_INCLUDE_BROKEN;
1172        return do_for_each_ref(NULL, prefix, fn, 0, flag, cb_data);
1173}
1174
1175int for_each_ref_in_submodule(const char *submodule, const char *prefix,
1176                each_ref_fn fn, void *cb_data)
1177{
1178        return do_for_each_ref(submodule, prefix, fn, strlen(prefix), 0, cb_data);
1179}
1180
1181int for_each_replace_ref(each_ref_fn fn, void *cb_data)
1182{
1183        return do_for_each_ref(NULL, git_replace_ref_base, fn,
1184                               strlen(git_replace_ref_base), 0, cb_data);
1185}
1186
1187int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1188{
1189        struct strbuf buf = STRBUF_INIT;
1190        int ret;
1191        strbuf_addf(&buf, "%srefs/", get_git_namespace());
1192        ret = do_for_each_ref(NULL, buf.buf, fn, 0, 0, cb_data);
1193        strbuf_release(&buf);
1194        return ret;
1195}
1196
1197int for_each_rawref(each_ref_fn fn, void *cb_data)
1198{
1199        return do_for_each_ref(NULL, "", fn, 0,
1200                               DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1201}
1202
1203/* This function needs to return a meaningful errno on failure */
1204const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1205                               unsigned char *sha1, int *flags)
1206{
1207        static struct strbuf sb_refname = STRBUF_INIT;
1208        int unused_flags;
1209        int symref_count;
1210
1211        if (!flags)
1212                flags = &unused_flags;
1213
1214        *flags = 0;
1215
1216        if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1217                if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1218                    !refname_is_safe(refname)) {
1219                        errno = EINVAL;
1220                        return NULL;
1221                }
1222
1223                /*
1224                 * dwim_ref() uses REF_ISBROKEN to distinguish between
1225                 * missing refs and refs that were present but invalid,
1226                 * to complain about the latter to stderr.
1227                 *
1228                 * We don't know whether the ref exists, so don't set
1229                 * REF_ISBROKEN yet.
1230                 */
1231                *flags |= REF_BAD_NAME;
1232        }
1233
1234        for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
1235                unsigned int read_flags = 0;
1236
1237                if (read_raw_ref(refname, sha1, &sb_refname, &read_flags)) {
1238                        *flags |= read_flags;
1239                        if (errno != ENOENT || (resolve_flags & RESOLVE_REF_READING))
1240                                return NULL;
1241                        hashclr(sha1);
1242                        if (*flags & REF_BAD_NAME)
1243                                *flags |= REF_ISBROKEN;
1244                        return refname;
1245                }
1246
1247                *flags |= read_flags;
1248
1249                if (!(read_flags & REF_ISSYMREF)) {
1250                        if (*flags & REF_BAD_NAME) {
1251                                hashclr(sha1);
1252                                *flags |= REF_ISBROKEN;
1253                        }
1254                        return refname;
1255                }
1256
1257                refname = sb_refname.buf;
1258                if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
1259                        hashclr(sha1);
1260                        return refname;
1261                }
1262                if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1263                        if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1264                            !refname_is_safe(refname)) {
1265                                errno = EINVAL;
1266                                return NULL;
1267                        }
1268
1269                        *flags |= REF_ISBROKEN | REF_BAD_NAME;
1270                }
1271        }
1272
1273        errno = ELOOP;
1274        return NULL;
1275}