refs.con commit Add "S_IFDIRLNK" file mode infrastructure for git links (9eec479)
   1#include "cache.h"
   2#include "refs.h"
   3#include "object.h"
   4#include "tag.h"
   5
   6/* ISSYMREF=01 and ISPACKED=02 are public interfaces */
   7#define REF_KNOWS_PEELED 04
   8
   9struct ref_list {
  10        struct ref_list *next;
  11        unsigned char flag; /* ISSYMREF? ISPACKED? */
  12        unsigned char sha1[20];
  13        unsigned char peeled[20];
  14        char name[FLEX_ARRAY];
  15};
  16
  17static const char *parse_ref_line(char *line, unsigned char *sha1)
  18{
  19        /*
  20         * 42: the answer to everything.
  21         *
  22         * In this case, it happens to be the answer to
  23         *  40 (length of sha1 hex representation)
  24         *  +1 (space in between hex and name)
  25         *  +1 (newline at the end of the line)
  26         */
  27        int len = strlen(line) - 42;
  28
  29        if (len <= 0)
  30                return NULL;
  31        if (get_sha1_hex(line, sha1) < 0)
  32                return NULL;
  33        if (!isspace(line[40]))
  34                return NULL;
  35        line += 41;
  36        if (isspace(*line))
  37                return NULL;
  38        if (line[len] != '\n')
  39                return NULL;
  40        line[len] = 0;
  41
  42        return line;
  43}
  44
  45static struct ref_list *add_ref(const char *name, const unsigned char *sha1,
  46                                int flag, struct ref_list *list,
  47                                struct ref_list **new_entry)
  48{
  49        int len;
  50        struct ref_list **p = &list, *entry;
  51
  52        /* Find the place to insert the ref into.. */
  53        while ((entry = *p) != NULL) {
  54                int cmp = strcmp(entry->name, name);
  55                if (cmp > 0)
  56                        break;
  57
  58                /* Same as existing entry? */
  59                if (!cmp) {
  60                        if (new_entry)
  61                                *new_entry = entry;
  62                        return list;
  63                }
  64                p = &entry->next;
  65        }
  66
  67        /* Allocate it and add it in.. */
  68        len = strlen(name) + 1;
  69        entry = xmalloc(sizeof(struct ref_list) + len);
  70        hashcpy(entry->sha1, sha1);
  71        hashclr(entry->peeled);
  72        memcpy(entry->name, name, len);
  73        entry->flag = flag;
  74        entry->next = *p;
  75        *p = entry;
  76        if (new_entry)
  77                *new_entry = entry;
  78        return list;
  79}
  80
  81/*
  82 * Future: need to be in "struct repository"
  83 * when doing a full libification.
  84 */
  85struct cached_refs {
  86        char did_loose;
  87        char did_packed;
  88        struct ref_list *loose;
  89        struct ref_list *packed;
  90} cached_refs;
  91
  92static void free_ref_list(struct ref_list *list)
  93{
  94        struct ref_list *next;
  95        for ( ; list; list = next) {
  96                next = list->next;
  97                free(list);
  98        }
  99}
 100
 101static void invalidate_cached_refs(void)
 102{
 103        struct cached_refs *ca = &cached_refs;
 104
 105        if (ca->did_loose && ca->loose)
 106                free_ref_list(ca->loose);
 107        if (ca->did_packed && ca->packed)
 108                free_ref_list(ca->packed);
 109        ca->loose = ca->packed = NULL;
 110        ca->did_loose = ca->did_packed = 0;
 111}
 112
 113static void read_packed_refs(FILE *f, struct cached_refs *cached_refs)
 114{
 115        struct ref_list *list = NULL;
 116        struct ref_list *last = NULL;
 117        char refline[PATH_MAX];
 118        int flag = REF_ISPACKED;
 119
 120        while (fgets(refline, sizeof(refline), f)) {
 121                unsigned char sha1[20];
 122                const char *name;
 123                static const char header[] = "# pack-refs with:";
 124
 125                if (!strncmp(refline, header, sizeof(header)-1)) {
 126                        const char *traits = refline + sizeof(header) - 1;
 127                        if (strstr(traits, " peeled "))
 128                                flag |= REF_KNOWS_PEELED;
 129                        /* perhaps other traits later as well */
 130                        continue;
 131                }
 132
 133                name = parse_ref_line(refline, sha1);
 134                if (name) {
 135                        list = add_ref(name, sha1, flag, list, &last);
 136                        continue;
 137                }
 138                if (last &&
 139                    refline[0] == '^' &&
 140                    strlen(refline) == 42 &&
 141                    refline[41] == '\n' &&
 142                    !get_sha1_hex(refline + 1, sha1))
 143                        hashcpy(last->peeled, sha1);
 144        }
 145        cached_refs->packed = list;
 146}
 147
 148static struct ref_list *get_packed_refs(void)
 149{
 150        if (!cached_refs.did_packed) {
 151                FILE *f = fopen(git_path("packed-refs"), "r");
 152                cached_refs.packed = NULL;
 153                if (f) {
 154                        read_packed_refs(f, &cached_refs);
 155                        fclose(f);
 156                }
 157                cached_refs.did_packed = 1;
 158        }
 159        return cached_refs.packed;
 160}
 161
 162static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
 163{
 164        DIR *dir = opendir(git_path("%s", base));
 165
 166        if (dir) {
 167                struct dirent *de;
 168                int baselen = strlen(base);
 169                char *ref = xmalloc(baselen + 257);
 170
 171                memcpy(ref, base, baselen);
 172                if (baselen && base[baselen-1] != '/')
 173                        ref[baselen++] = '/';
 174
 175                while ((de = readdir(dir)) != NULL) {
 176                        unsigned char sha1[20];
 177                        struct stat st;
 178                        int flag;
 179                        int namelen;
 180
 181                        if (de->d_name[0] == '.')
 182                                continue;
 183                        namelen = strlen(de->d_name);
 184                        if (namelen > 255)
 185                                continue;
 186                        if (has_extension(de->d_name, ".lock"))
 187                                continue;
 188                        memcpy(ref + baselen, de->d_name, namelen+1);
 189                        if (stat(git_path("%s", ref), &st) < 0)
 190                                continue;
 191                        if (S_ISDIR(st.st_mode)) {
 192                                list = get_ref_dir(ref, list);
 193                                continue;
 194                        }
 195                        if (!resolve_ref(ref, sha1, 1, &flag)) {
 196                                error("%s points nowhere!", ref);
 197                                continue;
 198                        }
 199                        list = add_ref(ref, sha1, flag, list, NULL);
 200                }
 201                free(ref);
 202                closedir(dir);
 203        }
 204        return list;
 205}
 206
 207static struct ref_list *get_loose_refs(void)
 208{
 209        if (!cached_refs.did_loose) {
 210                cached_refs.loose = get_ref_dir("refs", NULL);
 211                cached_refs.did_loose = 1;
 212        }
 213        return cached_refs.loose;
 214}
 215
 216/* We allow "recursive" symbolic refs. Only within reason, though */
 217#define MAXDEPTH 5
 218#define MAXREFLEN (1024)
 219
 220static int resolve_gitlink_packed_ref(char *name, int pathlen, const char *refname, unsigned char *result)
 221{
 222        FILE *f;
 223        struct cached_refs refs;
 224        struct ref_list *ref;
 225        int retval;
 226
 227        strcpy(name + pathlen, "packed-refs");
 228        f = fopen(name, "r");
 229        if (!f)
 230                return -1;
 231        read_packed_refs(f, &refs);
 232        fclose(f);
 233        ref = refs.packed;
 234        retval = -1;
 235        while (ref) {
 236                if (!strcmp(ref->name, refname)) {
 237                        retval = 0;
 238                        memcpy(result, ref->sha1, 20);
 239                        break;
 240                }
 241                ref = ref->next;
 242        }
 243        free_ref_list(refs.packed);
 244        return retval;
 245}
 246
 247static int resolve_gitlink_ref_recursive(char *name, int pathlen, const char *refname, unsigned char *result, int recursion)
 248{
 249        int fd, len = strlen(refname);
 250        char buffer[128], *p;
 251
 252        if (recursion > MAXDEPTH || len > MAXREFLEN)
 253                return -1;
 254        memcpy(name + pathlen, refname, len+1);
 255        fd = open(name, O_RDONLY);
 256        if (fd < 0)
 257                return resolve_gitlink_packed_ref(name, pathlen, refname, result);
 258
 259        len = read(fd, buffer, sizeof(buffer)-1);
 260        close(fd);
 261        if (len < 0)
 262                return -1;
 263        while (len && isspace(buffer[len-1]))
 264                len--;
 265        buffer[len] = 0;
 266
 267        /* Was it a detached head or an old-fashioned symlink? */
 268        if (!get_sha1_hex(buffer, result))
 269                return 0;
 270
 271        /* Symref? */
 272        if (strncmp(buffer, "ref:", 4))
 273                return -1;
 274        p = buffer + 4;
 275        while (isspace(*p))
 276                p++;
 277
 278        return resolve_gitlink_ref_recursive(name, pathlen, p, result, recursion+1);
 279}
 280
 281int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *result)
 282{
 283        int len = strlen(path), retval;
 284        char *gitdir;
 285
 286        while (len && path[len-1] == '/')
 287                len--;
 288        if (!len)
 289                return -1;
 290        gitdir = xmalloc(len + MAXREFLEN + 8);
 291        memcpy(gitdir, path, len);
 292        memcpy(gitdir + len, "/.git/", 7);
 293
 294        retval = resolve_gitlink_ref_recursive(gitdir, len+6, refname, result, 0);
 295        free(gitdir);
 296        return retval;
 297}
 298
 299const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *flag)
 300{
 301        int depth = MAXDEPTH, len;
 302        char buffer[256];
 303        static char ref_buffer[256];
 304
 305        if (flag)
 306                *flag = 0;
 307
 308        for (;;) {
 309                const char *path = git_path("%s", ref);
 310                struct stat st;
 311                char *buf;
 312                int fd;
 313
 314                if (--depth < 0)
 315                        return NULL;
 316
 317                /* Special case: non-existing file.
 318                 * Not having the refs/heads/new-branch is OK
 319                 * if we are writing into it, so is .git/HEAD
 320                 * that points at refs/heads/master still to be
 321                 * born.  It is NOT OK if we are resolving for
 322                 * reading.
 323                 */
 324                if (lstat(path, &st) < 0) {
 325                        struct ref_list *list = get_packed_refs();
 326                        while (list) {
 327                                if (!strcmp(ref, list->name)) {
 328                                        hashcpy(sha1, list->sha1);
 329                                        if (flag)
 330                                                *flag |= REF_ISPACKED;
 331                                        return ref;
 332                                }
 333                                list = list->next;
 334                        }
 335                        if (reading || errno != ENOENT)
 336                                return NULL;
 337                        hashclr(sha1);
 338                        return ref;
 339                }
 340
 341                /* Follow "normalized" - ie "refs/.." symlinks by hand */
 342                if (S_ISLNK(st.st_mode)) {
 343                        len = readlink(path, buffer, sizeof(buffer)-1);
 344                        if (len >= 5 && !memcmp("refs/", buffer, 5)) {
 345                                buffer[len] = 0;
 346                                strcpy(ref_buffer, buffer);
 347                                ref = ref_buffer;
 348                                if (flag)
 349                                        *flag |= REF_ISSYMREF;
 350                                continue;
 351                        }
 352                }
 353
 354                /* Is it a directory? */
 355                if (S_ISDIR(st.st_mode)) {
 356                        errno = EISDIR;
 357                        return NULL;
 358                }
 359
 360                /*
 361                 * Anything else, just open it and try to use it as
 362                 * a ref
 363                 */
 364                fd = open(path, O_RDONLY);
 365                if (fd < 0)
 366                        return NULL;
 367                len = read_in_full(fd, buffer, sizeof(buffer)-1);
 368                close(fd);
 369
 370                /*
 371                 * Is it a symbolic ref?
 372                 */
 373                if (len < 4 || memcmp("ref:", buffer, 4))
 374                        break;
 375                buf = buffer + 4;
 376                len -= 4;
 377                while (len && isspace(*buf))
 378                        buf++, len--;
 379                while (len && isspace(buf[len-1]))
 380                        len--;
 381                buf[len] = 0;
 382                memcpy(ref_buffer, buf, len + 1);
 383                ref = ref_buffer;
 384                if (flag)
 385                        *flag |= REF_ISSYMREF;
 386        }
 387        if (len < 40 || get_sha1_hex(buffer, sha1))
 388                return NULL;
 389        return ref;
 390}
 391
 392int read_ref(const char *ref, unsigned char *sha1)
 393{
 394        if (resolve_ref(ref, sha1, 1, NULL))
 395                return 0;
 396        return -1;
 397}
 398
 399static int do_one_ref(const char *base, each_ref_fn fn, int trim,
 400                      void *cb_data, struct ref_list *entry)
 401{
 402        if (strncmp(base, entry->name, trim))
 403                return 0;
 404        if (is_null_sha1(entry->sha1))
 405                return 0;
 406        if (!has_sha1_file(entry->sha1)) {
 407                error("%s does not point to a valid object!", entry->name);
 408                return 0;
 409        }
 410        return fn(entry->name + trim, entry->sha1, entry->flag, cb_data);
 411}
 412
 413int peel_ref(const char *ref, unsigned char *sha1)
 414{
 415        int flag;
 416        unsigned char base[20];
 417        struct object *o;
 418
 419        if (!resolve_ref(ref, base, 1, &flag))
 420                return -1;
 421
 422        if ((flag & REF_ISPACKED)) {
 423                struct ref_list *list = get_packed_refs();
 424
 425                while (list) {
 426                        if (!strcmp(list->name, ref)) {
 427                                if (list->flag & REF_KNOWS_PEELED) {
 428                                        hashcpy(sha1, list->peeled);
 429                                        return 0;
 430                                }
 431                                /* older pack-refs did not leave peeled ones */
 432                                break;
 433                        }
 434                        list = list->next;
 435                }
 436        }
 437
 438        /* fallback - callers should not call this for unpacked refs */
 439        o = parse_object(base);
 440        if (o->type == OBJ_TAG) {
 441                o = deref_tag(o, ref, 0);
 442                if (o) {
 443                        hashcpy(sha1, o->sha1);
 444                        return 0;
 445                }
 446        }
 447        return -1;
 448}
 449
 450static int do_for_each_ref(const char *base, each_ref_fn fn, int trim,
 451                           void *cb_data)
 452{
 453        int retval;
 454        struct ref_list *packed = get_packed_refs();
 455        struct ref_list *loose = get_loose_refs();
 456
 457        while (packed && loose) {
 458                struct ref_list *entry;
 459                int cmp = strcmp(packed->name, loose->name);
 460                if (!cmp) {
 461                        packed = packed->next;
 462                        continue;
 463                }
 464                if (cmp > 0) {
 465                        entry = loose;
 466                        loose = loose->next;
 467                } else {
 468                        entry = packed;
 469                        packed = packed->next;
 470                }
 471                retval = do_one_ref(base, fn, trim, cb_data, entry);
 472                if (retval)
 473                        return retval;
 474        }
 475
 476        for (packed = packed ? packed : loose; packed; packed = packed->next) {
 477                retval = do_one_ref(base, fn, trim, cb_data, packed);
 478                if (retval)
 479                        return retval;
 480        }
 481        return 0;
 482}
 483
 484int head_ref(each_ref_fn fn, void *cb_data)
 485{
 486        unsigned char sha1[20];
 487        int flag;
 488
 489        if (resolve_ref("HEAD", sha1, 1, &flag))
 490                return fn("HEAD", sha1, flag, cb_data);
 491        return 0;
 492}
 493
 494int for_each_ref(each_ref_fn fn, void *cb_data)
 495{
 496        return do_for_each_ref("refs/", fn, 0, cb_data);
 497}
 498
 499int for_each_tag_ref(each_ref_fn fn, void *cb_data)
 500{
 501        return do_for_each_ref("refs/tags/", fn, 10, cb_data);
 502}
 503
 504int for_each_branch_ref(each_ref_fn fn, void *cb_data)
 505{
 506        return do_for_each_ref("refs/heads/", fn, 11, cb_data);
 507}
 508
 509int for_each_remote_ref(each_ref_fn fn, void *cb_data)
 510{
 511        return do_for_each_ref("refs/remotes/", fn, 13, cb_data);
 512}
 513
 514/* NEEDSWORK: This is only used by ssh-upload and it should go; the
 515 * caller should do resolve_ref or read_ref like everybody else.  Or
 516 * maybe everybody else should use get_ref_sha1() instead of doing
 517 * read_ref().
 518 */
 519int get_ref_sha1(const char *ref, unsigned char *sha1)
 520{
 521        if (check_ref_format(ref))
 522                return -1;
 523        return read_ref(mkpath("refs/%s", ref), sha1);
 524}
 525
 526/*
 527 * Make sure "ref" is something reasonable to have under ".git/refs/";
 528 * We do not like it if:
 529 *
 530 * - any path component of it begins with ".", or
 531 * - it has double dots "..", or
 532 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
 533 * - it ends with a "/".
 534 */
 535
 536static inline int bad_ref_char(int ch)
 537{
 538        return (((unsigned) ch) <= ' ' ||
 539                ch == '~' || ch == '^' || ch == ':' ||
 540                /* 2.13 Pattern Matching Notation */
 541                ch == '?' || ch == '*' || ch == '[');
 542}
 543
 544int check_ref_format(const char *ref)
 545{
 546        int ch, level;
 547        const char *cp = ref;
 548
 549        level = 0;
 550        while (1) {
 551                while ((ch = *cp++) == '/')
 552                        ; /* tolerate duplicated slashes */
 553                if (!ch)
 554                        return -1; /* should not end with slashes */
 555
 556                /* we are at the beginning of the path component */
 557                if (ch == '.' || bad_ref_char(ch))
 558                        return -1;
 559
 560                /* scan the rest of the path component */
 561                while ((ch = *cp++) != 0) {
 562                        if (bad_ref_char(ch))
 563                                return -1;
 564                        if (ch == '/')
 565                                break;
 566                        if (ch == '.' && *cp == '.')
 567                                return -1;
 568                }
 569                level++;
 570                if (!ch) {
 571                        if (level < 2)
 572                                return -2; /* at least of form "heads/blah" */
 573                        return 0;
 574                }
 575        }
 576}
 577
 578static struct ref_lock *verify_lock(struct ref_lock *lock,
 579        const unsigned char *old_sha1, int mustexist)
 580{
 581        if (!resolve_ref(lock->ref_name, lock->old_sha1, mustexist, NULL)) {
 582                error("Can't verify ref %s", lock->ref_name);
 583                unlock_ref(lock);
 584                return NULL;
 585        }
 586        if (hashcmp(lock->old_sha1, old_sha1)) {
 587                error("Ref %s is at %s but expected %s", lock->ref_name,
 588                        sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
 589                unlock_ref(lock);
 590                return NULL;
 591        }
 592        return lock;
 593}
 594
 595static int remove_empty_dir_recursive(char *path, int len)
 596{
 597        DIR *dir = opendir(path);
 598        struct dirent *e;
 599        int ret = 0;
 600
 601        if (!dir)
 602                return -1;
 603        if (path[len-1] != '/')
 604                path[len++] = '/';
 605        while ((e = readdir(dir)) != NULL) {
 606                struct stat st;
 607                int namlen;
 608                if ((e->d_name[0] == '.') &&
 609                    ((e->d_name[1] == 0) ||
 610                     ((e->d_name[1] == '.') && e->d_name[2] == 0)))
 611                        continue; /* "." and ".." */
 612
 613                namlen = strlen(e->d_name);
 614                if ((len + namlen < PATH_MAX) &&
 615                    strcpy(path + len, e->d_name) &&
 616                    !lstat(path, &st) &&
 617                    S_ISDIR(st.st_mode) &&
 618                    !remove_empty_dir_recursive(path, len + namlen))
 619                        continue; /* happy */
 620
 621                /* path too long, stat fails, or non-directory still exists */
 622                ret = -1;
 623                break;
 624        }
 625        closedir(dir);
 626        if (!ret) {
 627                path[len] = 0;
 628                ret = rmdir(path);
 629        }
 630        return ret;
 631}
 632
 633static int remove_empty_directories(char *file)
 634{
 635        /* we want to create a file but there is a directory there;
 636         * if that is an empty directory (or a directory that contains
 637         * only empty directories), remove them.
 638         */
 639        char path[PATH_MAX];
 640        int len = strlen(file);
 641
 642        if (len >= PATH_MAX) /* path too long ;-) */
 643                return -1;
 644        strcpy(path, file);
 645        return remove_empty_dir_recursive(path, len);
 646}
 647
 648static int is_refname_available(const char *ref, const char *oldref,
 649                                struct ref_list *list, int quiet)
 650{
 651        int namlen = strlen(ref); /* e.g. 'foo/bar' */
 652        while (list) {
 653                /* list->name could be 'foo' or 'foo/bar/baz' */
 654                if (!oldref || strcmp(oldref, list->name)) {
 655                        int len = strlen(list->name);
 656                        int cmplen = (namlen < len) ? namlen : len;
 657                        const char *lead = (namlen < len) ? list->name : ref;
 658                        if (!strncmp(ref, list->name, cmplen) &&
 659                            lead[cmplen] == '/') {
 660                                if (!quiet)
 661                                        error("'%s' exists; cannot create '%s'",
 662                                              list->name, ref);
 663                                return 0;
 664                        }
 665                }
 666                list = list->next;
 667        }
 668        return 1;
 669}
 670
 671static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1, int *flag)
 672{
 673        char *ref_file;
 674        const char *orig_ref = ref;
 675        struct ref_lock *lock;
 676        struct stat st;
 677        int last_errno = 0;
 678        int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
 679
 680        lock = xcalloc(1, sizeof(struct ref_lock));
 681        lock->lock_fd = -1;
 682
 683        ref = resolve_ref(ref, lock->old_sha1, mustexist, flag);
 684        if (!ref && errno == EISDIR) {
 685                /* we are trying to lock foo but we used to
 686                 * have foo/bar which now does not exist;
 687                 * it is normal for the empty directory 'foo'
 688                 * to remain.
 689                 */
 690                ref_file = git_path("%s", orig_ref);
 691                if (remove_empty_directories(ref_file)) {
 692                        last_errno = errno;
 693                        error("there are still refs under '%s'", orig_ref);
 694                        goto error_return;
 695                }
 696                ref = resolve_ref(orig_ref, lock->old_sha1, mustexist, flag);
 697        }
 698        if (!ref) {
 699                last_errno = errno;
 700                error("unable to resolve reference %s: %s",
 701                        orig_ref, strerror(errno));
 702                goto error_return;
 703        }
 704        /* When the ref did not exist and we are creating it,
 705         * make sure there is no existing ref that is packed
 706         * whose name begins with our refname, nor a ref whose
 707         * name is a proper prefix of our refname.
 708         */
 709        if (is_null_sha1(lock->old_sha1) &&
 710            !is_refname_available(ref, NULL, get_packed_refs(), 0))
 711                goto error_return;
 712
 713        lock->lk = xcalloc(1, sizeof(struct lock_file));
 714
 715        lock->ref_name = xstrdup(ref);
 716        lock->orig_ref_name = xstrdup(orig_ref);
 717        ref_file = git_path("%s", ref);
 718        lock->force_write = lstat(ref_file, &st) && errno == ENOENT;
 719
 720        if (safe_create_leading_directories(ref_file)) {
 721                last_errno = errno;
 722                error("unable to create directory for %s", ref_file);
 723                goto error_return;
 724        }
 725        lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, 1);
 726
 727        return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
 728
 729 error_return:
 730        unlock_ref(lock);
 731        errno = last_errno;
 732        return NULL;
 733}
 734
 735struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
 736{
 737        char refpath[PATH_MAX];
 738        if (check_ref_format(ref))
 739                return NULL;
 740        strcpy(refpath, mkpath("refs/%s", ref));
 741        return lock_ref_sha1_basic(refpath, old_sha1, NULL);
 742}
 743
 744struct ref_lock *lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1)
 745{
 746        if (check_ref_format(ref) == -1)
 747                return NULL;
 748        return lock_ref_sha1_basic(ref, old_sha1, NULL);
 749}
 750
 751static struct lock_file packlock;
 752
 753static int repack_without_ref(const char *refname)
 754{
 755        struct ref_list *list, *packed_ref_list;
 756        int fd;
 757        int found = 0;
 758
 759        packed_ref_list = get_packed_refs();
 760        for (list = packed_ref_list; list; list = list->next) {
 761                if (!strcmp(refname, list->name)) {
 762                        found = 1;
 763                        break;
 764                }
 765        }
 766        if (!found)
 767                return 0;
 768        fd = hold_lock_file_for_update(&packlock, git_path("packed-refs"), 0);
 769        if (fd < 0)
 770                return error("cannot delete '%s' from packed refs", refname);
 771
 772        for (list = packed_ref_list; list; list = list->next) {
 773                char line[PATH_MAX + 100];
 774                int len;
 775
 776                if (!strcmp(refname, list->name))
 777                        continue;
 778                len = snprintf(line, sizeof(line), "%s %s\n",
 779                               sha1_to_hex(list->sha1), list->name);
 780                /* this should not happen but just being defensive */
 781                if (len > sizeof(line))
 782                        die("too long a refname '%s'", list->name);
 783                write_or_die(fd, line, len);
 784        }
 785        return commit_lock_file(&packlock);
 786}
 787
 788int delete_ref(const char *refname, unsigned char *sha1)
 789{
 790        struct ref_lock *lock;
 791        int err, i, ret = 0, flag = 0;
 792
 793        lock = lock_ref_sha1_basic(refname, sha1, &flag);
 794        if (!lock)
 795                return 1;
 796        if (!(flag & REF_ISPACKED)) {
 797                /* loose */
 798                i = strlen(lock->lk->filename) - 5; /* .lock */
 799                lock->lk->filename[i] = 0;
 800                err = unlink(lock->lk->filename);
 801                if (err) {
 802                        ret = 1;
 803                        error("unlink(%s) failed: %s",
 804                              lock->lk->filename, strerror(errno));
 805                }
 806                lock->lk->filename[i] = '.';
 807        }
 808        /* removing the loose one could have resurrected an earlier
 809         * packed one.  Also, if it was not loose we need to repack
 810         * without it.
 811         */
 812        ret |= repack_without_ref(refname);
 813
 814        err = unlink(git_path("logs/%s", lock->ref_name));
 815        if (err && errno != ENOENT)
 816                fprintf(stderr, "warning: unlink(%s) failed: %s",
 817                        git_path("logs/%s", lock->ref_name), strerror(errno));
 818        invalidate_cached_refs();
 819        unlock_ref(lock);
 820        return ret;
 821}
 822
 823int rename_ref(const char *oldref, const char *newref, const char *logmsg)
 824{
 825        static const char renamed_ref[] = "RENAMED-REF";
 826        unsigned char sha1[20], orig_sha1[20];
 827        int flag = 0, logmoved = 0;
 828        struct ref_lock *lock;
 829        struct stat loginfo;
 830        int log = !lstat(git_path("logs/%s", oldref), &loginfo);
 831
 832        if (S_ISLNK(loginfo.st_mode))
 833                return error("reflog for %s is a symlink", oldref);
 834
 835        if (!resolve_ref(oldref, orig_sha1, 1, &flag))
 836                return error("refname %s not found", oldref);
 837
 838        if (!is_refname_available(newref, oldref, get_packed_refs(), 0))
 839                return 1;
 840
 841        if (!is_refname_available(newref, oldref, get_loose_refs(), 0))
 842                return 1;
 843
 844        lock = lock_ref_sha1_basic(renamed_ref, NULL, NULL);
 845        if (!lock)
 846                return error("unable to lock %s", renamed_ref);
 847        lock->force_write = 1;
 848        if (write_ref_sha1(lock, orig_sha1, logmsg))
 849                return error("unable to save current sha1 in %s", renamed_ref);
 850
 851        if (log && rename(git_path("logs/%s", oldref), git_path("tmp-renamed-log")))
 852                return error("unable to move logfile logs/%s to tmp-renamed-log: %s",
 853                        oldref, strerror(errno));
 854
 855        if (delete_ref(oldref, orig_sha1)) {
 856                error("unable to delete old %s", oldref);
 857                goto rollback;
 858        }
 859
 860        if (resolve_ref(newref, sha1, 1, &flag) && delete_ref(newref, sha1)) {
 861                if (errno==EISDIR) {
 862                        if (remove_empty_directories(git_path("%s", newref))) {
 863                                error("Directory not empty: %s", newref);
 864                                goto rollback;
 865                        }
 866                } else {
 867                        error("unable to delete existing %s", newref);
 868                        goto rollback;
 869                }
 870        }
 871
 872        if (log && safe_create_leading_directories(git_path("logs/%s", newref))) {
 873                error("unable to create directory for %s", newref);
 874                goto rollback;
 875        }
 876
 877 retry:
 878        if (log && rename(git_path("tmp-renamed-log"), git_path("logs/%s", newref))) {
 879                if (errno==EISDIR || errno==ENOTDIR) {
 880                        /*
 881                         * rename(a, b) when b is an existing
 882                         * directory ought to result in ISDIR, but
 883                         * Solaris 5.8 gives ENOTDIR.  Sheesh.
 884                         */
 885                        if (remove_empty_directories(git_path("logs/%s", newref))) {
 886                                error("Directory not empty: logs/%s", newref);
 887                                goto rollback;
 888                        }
 889                        goto retry;
 890                } else {
 891                        error("unable to move logfile tmp-renamed-log to logs/%s: %s",
 892                                newref, strerror(errno));
 893                        goto rollback;
 894                }
 895        }
 896        logmoved = log;
 897
 898        lock = lock_ref_sha1_basic(newref, NULL, NULL);
 899        if (!lock) {
 900                error("unable to lock %s for update", newref);
 901                goto rollback;
 902        }
 903
 904        lock->force_write = 1;
 905        hashcpy(lock->old_sha1, orig_sha1);
 906        if (write_ref_sha1(lock, orig_sha1, logmsg)) {
 907                error("unable to write current sha1 into %s", newref);
 908                goto rollback;
 909        }
 910
 911        return 0;
 912
 913 rollback:
 914        lock = lock_ref_sha1_basic(oldref, NULL, NULL);
 915        if (!lock) {
 916                error("unable to lock %s for rollback", oldref);
 917                goto rollbacklog;
 918        }
 919
 920        lock->force_write = 1;
 921        flag = log_all_ref_updates;
 922        log_all_ref_updates = 0;
 923        if (write_ref_sha1(lock, orig_sha1, NULL))
 924                error("unable to write current sha1 into %s", oldref);
 925        log_all_ref_updates = flag;
 926
 927 rollbacklog:
 928        if (logmoved && rename(git_path("logs/%s", newref), git_path("logs/%s", oldref)))
 929                error("unable to restore logfile %s from %s: %s",
 930                        oldref, newref, strerror(errno));
 931        if (!logmoved && log &&
 932            rename(git_path("tmp-renamed-log"), git_path("logs/%s", oldref)))
 933                error("unable to restore logfile %s from tmp-renamed-log: %s",
 934                        oldref, strerror(errno));
 935
 936        return 1;
 937}
 938
 939void unlock_ref(struct ref_lock *lock)
 940{
 941        if (lock->lock_fd >= 0) {
 942                close(lock->lock_fd);
 943                /* Do not free lock->lk -- atexit() still looks at them */
 944                if (lock->lk)
 945                        rollback_lock_file(lock->lk);
 946        }
 947        free(lock->ref_name);
 948        free(lock->orig_ref_name);
 949        free(lock);
 950}
 951
 952static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
 953                         const unsigned char *new_sha1, const char *msg)
 954{
 955        int logfd, written, oflags = O_APPEND | O_WRONLY;
 956        unsigned maxlen, len;
 957        int msglen;
 958        char *log_file, *logrec;
 959        const char *committer;
 960
 961        if (log_all_ref_updates < 0)
 962                log_all_ref_updates = !is_bare_repository();
 963
 964        log_file = git_path("logs/%s", ref_name);
 965
 966        if (log_all_ref_updates &&
 967            (!prefixcmp(ref_name, "refs/heads/") ||
 968             !prefixcmp(ref_name, "refs/remotes/") ||
 969             !strcmp(ref_name, "HEAD"))) {
 970                if (safe_create_leading_directories(log_file) < 0)
 971                        return error("unable to create directory for %s",
 972                                     log_file);
 973                oflags |= O_CREAT;
 974        }
 975
 976        logfd = open(log_file, oflags, 0666);
 977        if (logfd < 0) {
 978                if (!(oflags & O_CREAT) && errno == ENOENT)
 979                        return 0;
 980
 981                if ((oflags & O_CREAT) && errno == EISDIR) {
 982                        if (remove_empty_directories(log_file)) {
 983                                return error("There are still logs under '%s'",
 984                                             log_file);
 985                        }
 986                        logfd = open(log_file, oflags, 0666);
 987                }
 988
 989                if (logfd < 0)
 990                        return error("Unable to append to %s: %s",
 991                                     log_file, strerror(errno));
 992        }
 993
 994        adjust_shared_perm(log_file);
 995
 996        msglen = 0;
 997        if (msg) {
 998                /* clean up the message and make sure it is a single line */
 999                for ( ; *msg; msg++)
1000                        if (!isspace(*msg))
1001                                break;
1002                if (*msg) {
1003                        const char *ep = strchr(msg, '\n');
1004                        if (ep)
1005                                msglen = ep - msg;
1006                        else
1007                                msglen = strlen(msg);
1008                }
1009        }
1010
1011        committer = git_committer_info(-1);
1012        maxlen = strlen(committer) + msglen + 100;
1013        logrec = xmalloc(maxlen);
1014        len = sprintf(logrec, "%s %s %s\n",
1015                      sha1_to_hex(old_sha1),
1016                      sha1_to_hex(new_sha1),
1017                      committer);
1018        if (msglen)
1019                len += sprintf(logrec + len - 1, "\t%.*s\n", msglen, msg) - 1;
1020        written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;
1021        free(logrec);
1022        close(logfd);
1023        if (written != len)
1024                return error("Unable to append to %s", log_file);
1025        return 0;
1026}
1027
1028int write_ref_sha1(struct ref_lock *lock,
1029        const unsigned char *sha1, const char *logmsg)
1030{
1031        static char term = '\n';
1032
1033        if (!lock)
1034                return -1;
1035        if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
1036                unlock_ref(lock);
1037                return 0;
1038        }
1039        if (write_in_full(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
1040            write_in_full(lock->lock_fd, &term, 1) != 1
1041                || close(lock->lock_fd) < 0) {
1042                error("Couldn't write %s", lock->lk->filename);
1043                unlock_ref(lock);
1044                return -1;
1045        }
1046        invalidate_cached_refs();
1047        if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 ||
1048            (strcmp(lock->ref_name, lock->orig_ref_name) &&
1049             log_ref_write(lock->orig_ref_name, lock->old_sha1, sha1, logmsg) < 0)) {
1050                unlock_ref(lock);
1051                return -1;
1052        }
1053        if (strcmp(lock->orig_ref_name, "HEAD") != 0) {
1054                /*
1055                 * Special hack: If a branch is updated directly and HEAD
1056                 * points to it (may happen on the remote side of a push
1057                 * for example) then logically the HEAD reflog should be
1058                 * updated too.
1059                 * A generic solution implies reverse symref information,
1060                 * but finding all symrefs pointing to the given branch
1061                 * would be rather costly for this rare event (the direct
1062                 * update of a branch) to be worth it.  So let's cheat and
1063                 * check with HEAD only which should cover 99% of all usage
1064                 * scenarios (even 100% of the default ones).
1065                 */
1066                unsigned char head_sha1[20];
1067                int head_flag;
1068                const char *head_ref;
1069                head_ref = resolve_ref("HEAD", head_sha1, 1, &head_flag);
1070                if (head_ref && (head_flag & REF_ISSYMREF) &&
1071                    !strcmp(head_ref, lock->ref_name))
1072                        log_ref_write("HEAD", lock->old_sha1, sha1, logmsg);
1073        }
1074        if (commit_lock_file(lock->lk)) {
1075                error("Couldn't set %s", lock->ref_name);
1076                unlock_ref(lock);
1077                return -1;
1078        }
1079        lock->lock_fd = -1;
1080        unlock_ref(lock);
1081        return 0;
1082}
1083
1084int create_symref(const char *ref_target, const char *refs_heads_master,
1085                  const char *logmsg)
1086{
1087        const char *lockpath;
1088        char ref[1000];
1089        int fd, len, written;
1090        char *git_HEAD = xstrdup(git_path("%s", ref_target));
1091        unsigned char old_sha1[20], new_sha1[20];
1092
1093        if (logmsg && read_ref(ref_target, old_sha1))
1094                hashclr(old_sha1);
1095
1096        if (safe_create_leading_directories(git_HEAD) < 0)
1097                return error("unable to create directory for %s", git_HEAD);
1098
1099#ifndef NO_SYMLINK_HEAD
1100        if (prefer_symlink_refs) {
1101                unlink(git_HEAD);
1102                if (!symlink(refs_heads_master, git_HEAD))
1103                        goto done;
1104                fprintf(stderr, "no symlink - falling back to symbolic ref\n");
1105        }
1106#endif
1107
1108        len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
1109        if (sizeof(ref) <= len) {
1110                error("refname too long: %s", refs_heads_master);
1111                goto error_free_return;
1112        }
1113        lockpath = mkpath("%s.lock", git_HEAD);
1114        fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666);
1115        if (fd < 0) {
1116                error("Unable to open %s for writing", lockpath);
1117                goto error_free_return;
1118        }
1119        written = write_in_full(fd, ref, len);
1120        close(fd);
1121        if (written != len) {
1122                error("Unable to write to %s", lockpath);
1123                goto error_unlink_return;
1124        }
1125        if (rename(lockpath, git_HEAD) < 0) {
1126                error("Unable to create %s", git_HEAD);
1127                goto error_unlink_return;
1128        }
1129        if (adjust_shared_perm(git_HEAD)) {
1130                error("Unable to fix permissions on %s", lockpath);
1131        error_unlink_return:
1132                unlink(lockpath);
1133        error_free_return:
1134                free(git_HEAD);
1135                return -1;
1136        }
1137
1138#ifndef NO_SYMLINK_HEAD
1139        done:
1140#endif
1141        if (logmsg && !read_ref(refs_heads_master, new_sha1))
1142                log_ref_write(ref_target, old_sha1, new_sha1, logmsg);
1143
1144        free(git_HEAD);
1145        return 0;
1146}
1147
1148static char *ref_msg(const char *line, const char *endp)
1149{
1150        const char *ep;
1151        char *msg;
1152
1153        line += 82;
1154        for (ep = line; ep < endp && *ep != '\n'; ep++)
1155                ;
1156        msg = xmalloc(ep - line + 1);
1157        memcpy(msg, line, ep - line);
1158        msg[ep - line] = 0;
1159        return msg;
1160}
1161
1162int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *sha1, char **msg, unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt)
1163{
1164        const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec;
1165        char *tz_c;
1166        int logfd, tz, reccnt = 0;
1167        struct stat st;
1168        unsigned long date;
1169        unsigned char logged_sha1[20];
1170        void *log_mapped;
1171        size_t mapsz;
1172
1173        logfile = git_path("logs/%s", ref);
1174        logfd = open(logfile, O_RDONLY, 0);
1175        if (logfd < 0)
1176                die("Unable to read log %s: %s", logfile, strerror(errno));
1177        fstat(logfd, &st);
1178        if (!st.st_size)
1179                die("Log %s is empty.", logfile);
1180        mapsz = xsize_t(st.st_size);
1181        log_mapped = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, logfd, 0);
1182        logdata = log_mapped;
1183        close(logfd);
1184
1185        lastrec = NULL;
1186        rec = logend = logdata + st.st_size;
1187        while (logdata < rec) {
1188                reccnt++;
1189                if (logdata < rec && *(rec-1) == '\n')
1190                        rec--;
1191                lastgt = NULL;
1192                while (logdata < rec && *(rec-1) != '\n') {
1193                        rec--;
1194                        if (*rec == '>')
1195                                lastgt = rec;
1196                }
1197                if (!lastgt)
1198                        die("Log %s is corrupt.", logfile);
1199                date = strtoul(lastgt + 1, &tz_c, 10);
1200                if (date <= at_time || cnt == 0) {
1201                        tz = strtoul(tz_c, NULL, 10);
1202                        if (msg)
1203                                *msg = ref_msg(rec, logend);
1204                        if (cutoff_time)
1205                                *cutoff_time = date;
1206                        if (cutoff_tz)
1207                                *cutoff_tz = tz;
1208                        if (cutoff_cnt)
1209                                *cutoff_cnt = reccnt - 1;
1210                        if (lastrec) {
1211                                if (get_sha1_hex(lastrec, logged_sha1))
1212                                        die("Log %s is corrupt.", logfile);
1213                                if (get_sha1_hex(rec + 41, sha1))
1214                                        die("Log %s is corrupt.", logfile);
1215                                if (hashcmp(logged_sha1, sha1)) {
1216                                        fprintf(stderr,
1217                                                "warning: Log %s has gap after %s.\n",
1218                                                logfile, show_rfc2822_date(date, tz));
1219                                }
1220                        }
1221                        else if (date == at_time) {
1222                                if (get_sha1_hex(rec + 41, sha1))
1223                                        die("Log %s is corrupt.", logfile);
1224                        }
1225                        else {
1226                                if (get_sha1_hex(rec + 41, logged_sha1))
1227                                        die("Log %s is corrupt.", logfile);
1228                                if (hashcmp(logged_sha1, sha1)) {
1229                                        fprintf(stderr,
1230                                                "warning: Log %s unexpectedly ended on %s.\n",
1231                                                logfile, show_rfc2822_date(date, tz));
1232                                }
1233                        }
1234                        munmap(log_mapped, mapsz);
1235                        return 0;
1236                }
1237                lastrec = rec;
1238                if (cnt > 0)
1239                        cnt--;
1240        }
1241
1242        rec = logdata;
1243        while (rec < logend && *rec != '>' && *rec != '\n')
1244                rec++;
1245        if (rec == logend || *rec == '\n')
1246                die("Log %s is corrupt.", logfile);
1247        date = strtoul(rec + 1, &tz_c, 10);
1248        tz = strtoul(tz_c, NULL, 10);
1249        if (get_sha1_hex(logdata, sha1))
1250                die("Log %s is corrupt.", logfile);
1251        if (msg)
1252                *msg = ref_msg(logdata, logend);
1253        munmap(log_mapped, mapsz);
1254
1255        if (cutoff_time)
1256                *cutoff_time = date;
1257        if (cutoff_tz)
1258                *cutoff_tz = tz;
1259        if (cutoff_cnt)
1260                *cutoff_cnt = reccnt;
1261        return 1;
1262}
1263
1264int for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
1265{
1266        const char *logfile;
1267        FILE *logfp;
1268        char buf[1024];
1269        int ret = 0;
1270
1271        logfile = git_path("logs/%s", ref);
1272        logfp = fopen(logfile, "r");
1273        if (!logfp)
1274                return -1;
1275        while (fgets(buf, sizeof(buf), logfp)) {
1276                unsigned char osha1[20], nsha1[20];
1277                char *email_end, *message;
1278                unsigned long timestamp;
1279                int len, tz;
1280
1281                /* old SP new SP name <email> SP time TAB msg LF */
1282                len = strlen(buf);
1283                if (len < 83 || buf[len-1] != '\n' ||
1284                    get_sha1_hex(buf, osha1) || buf[40] != ' ' ||
1285                    get_sha1_hex(buf + 41, nsha1) || buf[81] != ' ' ||
1286                    !(email_end = strchr(buf + 82, '>')) ||
1287                    email_end[1] != ' ' ||
1288                    !(timestamp = strtoul(email_end + 2, &message, 10)) ||
1289                    !message || message[0] != ' ' ||
1290                    (message[1] != '+' && message[1] != '-') ||
1291                    !isdigit(message[2]) || !isdigit(message[3]) ||
1292                    !isdigit(message[4]) || !isdigit(message[5]))
1293                        continue; /* corrupt? */
1294                email_end[1] = '\0';
1295                tz = strtol(message + 1, NULL, 10);
1296                if (message[6] != '\t')
1297                        message += 6;
1298                else
1299                        message += 7;
1300                ret = fn(osha1, nsha1, buf+82, timestamp, tz, message, cb_data);
1301                if (ret)
1302                        break;
1303        }
1304        fclose(logfp);
1305        return ret;
1306}
1307
1308static int do_for_each_reflog(const char *base, each_ref_fn fn, void *cb_data)
1309{
1310        DIR *dir = opendir(git_path("logs/%s", base));
1311        int retval = 0;
1312
1313        if (dir) {
1314                struct dirent *de;
1315                int baselen = strlen(base);
1316                char *log = xmalloc(baselen + 257);
1317
1318                memcpy(log, base, baselen);
1319                if (baselen && base[baselen-1] != '/')
1320                        log[baselen++] = '/';
1321
1322                while ((de = readdir(dir)) != NULL) {
1323                        struct stat st;
1324                        int namelen;
1325
1326                        if (de->d_name[0] == '.')
1327                                continue;
1328                        namelen = strlen(de->d_name);
1329                        if (namelen > 255)
1330                                continue;
1331                        if (has_extension(de->d_name, ".lock"))
1332                                continue;
1333                        memcpy(log + baselen, de->d_name, namelen+1);
1334                        if (stat(git_path("logs/%s", log), &st) < 0)
1335                                continue;
1336                        if (S_ISDIR(st.st_mode)) {
1337                                retval = do_for_each_reflog(log, fn, cb_data);
1338                        } else {
1339                                unsigned char sha1[20];
1340                                if (!resolve_ref(log, sha1, 0, NULL))
1341                                        retval = error("bad ref for %s", log);
1342                                else
1343                                        retval = fn(log, sha1, 0, cb_data);
1344                        }
1345                        if (retval)
1346                                break;
1347                }
1348                free(log);
1349                closedir(dir);
1350        }
1351        else if (*base)
1352                return errno;
1353        return retval;
1354}
1355
1356int for_each_reflog(each_ref_fn fn, void *cb_data)
1357{
1358        return do_for_each_reflog("", fn, cb_data);
1359}