50c25d3d28b099b9fef9d47cf25e45eaf74da5f0
   1#include "refs.h"
   2#include "cache.h"
   3
   4#include <errno.h>
   5
   6struct ref_list {
   7        struct ref_list *next;
   8        unsigned char sha1[20];
   9        char name[FLEX_ARRAY];
  10};
  11
  12static const char *parse_ref_line(char *line, unsigned char *sha1)
  13{
  14        /*
  15         * 42: the answer to everything.
  16         *
  17         * In this case, it happens to be the answer to
  18         *  40 (length of sha1 hex representation)
  19         *  +1 (space in between hex and name)
  20         *  +1 (newline at the end of the line)
  21         */
  22        int len = strlen(line) - 42;
  23
  24        if (len <= 0)
  25                return NULL;
  26        if (get_sha1_hex(line, sha1) < 0)
  27                return NULL;
  28        if (!isspace(line[40]))
  29                return NULL;
  30        line += 41;
  31        if (line[len] != '\n')
  32                return NULL;
  33        line[len] = 0;
  34        return line;
  35}
  36
  37static struct ref_list *add_ref(const char *name, const unsigned char *sha1, struct ref_list *list)
  38{
  39        int len;
  40        struct ref_list **p = &list, *entry;
  41
  42        /* Find the place to insert the ref into.. */
  43        while ((entry = *p) != NULL) {
  44                int cmp = strcmp(entry->name, name);
  45                if (cmp > 0)
  46                        break;
  47
  48                /* Same as existing entry? */
  49                if (!cmp)
  50                        return list;
  51                p = &entry->next;
  52        }
  53
  54        /* Allocate it and add it in.. */
  55        len = strlen(name) + 1;
  56        entry = xmalloc(sizeof(struct ref_list) + len);
  57        hashcpy(entry->sha1, sha1);
  58        memcpy(entry->name, name, len);
  59        entry->next = *p;
  60        *p = entry;
  61        return list;
  62}
  63
  64static struct ref_list *get_packed_refs(void)
  65{
  66        static int did_refs = 0;
  67        static struct ref_list *refs = NULL;
  68
  69        if (!did_refs) {
  70                FILE *f = fopen(git_path("packed-refs"), "r");
  71                if (f) {
  72                        struct ref_list *list = NULL;
  73                        char refline[PATH_MAX];
  74                        while (fgets(refline, sizeof(refline), f)) {
  75                                unsigned char sha1[20];
  76                                const char *name = parse_ref_line(refline, sha1);
  77                                if (!name)
  78                                        continue;
  79                                list = add_ref(name, sha1, list);
  80                        }
  81                        fclose(f);
  82                        refs = list;
  83                }
  84                did_refs = 1;
  85        }
  86        return refs;
  87}
  88
  89static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
  90{
  91        DIR *dir = opendir(git_path("%s", base));
  92
  93        if (dir) {
  94                struct dirent *de;
  95                int baselen = strlen(base);
  96                char *ref = xmalloc(baselen + 257);
  97
  98                memcpy(ref, base, baselen);
  99                if (baselen && base[baselen-1] != '/')
 100                        ref[baselen++] = '/';
 101
 102                while ((de = readdir(dir)) != NULL) {
 103                        unsigned char sha1[20];
 104                        struct stat st;
 105                        int namelen;
 106
 107                        if (de->d_name[0] == '.')
 108                                continue;
 109                        namelen = strlen(de->d_name);
 110                        if (namelen > 255)
 111                                continue;
 112                        if (has_extension(de->d_name, ".lock"))
 113                                continue;
 114                        memcpy(ref + baselen, de->d_name, namelen+1);
 115                        if (stat(git_path("%s", ref), &st) < 0)
 116                                continue;
 117                        if (S_ISDIR(st.st_mode)) {
 118                                list = get_ref_dir(ref, list);
 119                                continue;
 120                        }
 121                        if (read_ref(ref, sha1) < 0) {
 122                                error("%s points nowhere!", ref);
 123                                continue;
 124                        }
 125                        list = add_ref(ref, sha1, list);
 126                }
 127                free(ref);
 128                closedir(dir);
 129        }
 130        return list;
 131}
 132
 133static struct ref_list *get_loose_refs(void)
 134{
 135        static int did_refs = 0;
 136        static struct ref_list *refs = NULL;
 137
 138        if (!did_refs) {
 139                refs = get_ref_dir("refs", NULL);
 140                did_refs = 1;
 141        }
 142        return refs;
 143}
 144
 145/* We allow "recursive" symbolic refs. Only within reason, though */
 146#define MAXDEPTH 5
 147
 148const char *resolve_ref(const char *ref, unsigned char *sha1, int reading)
 149{
 150        int depth = MAXDEPTH, len;
 151        char buffer[256];
 152        static char ref_buffer[256];
 153
 154        for (;;) {
 155                const char *path = git_path("%s", ref);
 156                struct stat st;
 157                char *buf;
 158                int fd;
 159
 160                if (--depth < 0)
 161                        return NULL;
 162
 163                /* Special case: non-existing file.
 164                 * Not having the refs/heads/new-branch is OK
 165                 * if we are writing into it, so is .git/HEAD
 166                 * that points at refs/heads/master still to be
 167                 * born.  It is NOT OK if we are resolving for
 168                 * reading.
 169                 */
 170                if (lstat(path, &st) < 0) {
 171                        if (reading || errno != ENOENT)
 172                                return NULL;
 173                        hashclr(sha1);
 174                        return ref;
 175                }
 176
 177                /* Follow "normalized" - ie "refs/.." symlinks by hand */
 178                if (S_ISLNK(st.st_mode)) {
 179                        len = readlink(path, buffer, sizeof(buffer)-1);
 180                        if (len >= 5 && !memcmp("refs/", buffer, 5)) {
 181                                buffer[len] = 0;
 182                                strcpy(ref_buffer, buffer);
 183                                ref = ref_buffer;
 184                                continue;
 185                        }
 186                }
 187
 188                /*
 189                 * Anything else, just open it and try to use it as
 190                 * a ref
 191                 */
 192                fd = open(path, O_RDONLY);
 193                if (fd < 0)
 194                        return NULL;
 195                len = read(fd, buffer, sizeof(buffer)-1);
 196                close(fd);
 197
 198                /*
 199                 * Is it a symbolic ref?
 200                 */
 201                if (len < 4 || memcmp("ref:", buffer, 4))
 202                        break;
 203                buf = buffer + 4;
 204                len -= 4;
 205                while (len && isspace(*buf))
 206                        buf++, len--;
 207                while (len && isspace(buf[len-1]))
 208                        len--;
 209                buf[len] = 0;
 210                memcpy(ref_buffer, buf, len + 1);
 211                ref = ref_buffer;
 212        }
 213        if (len < 40 || get_sha1_hex(buffer, sha1))
 214                return NULL;
 215        return ref;
 216}
 217
 218int create_symref(const char *ref_target, const char *refs_heads_master)
 219{
 220        const char *lockpath;
 221        char ref[1000];
 222        int fd, len, written;
 223        const char *git_HEAD = git_path("%s", ref_target);
 224
 225#ifndef NO_SYMLINK_HEAD
 226        if (prefer_symlink_refs) {
 227                unlink(git_HEAD);
 228                if (!symlink(refs_heads_master, git_HEAD))
 229                        return 0;
 230                fprintf(stderr, "no symlink - falling back to symbolic ref\n");
 231        }
 232#endif
 233
 234        len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master);
 235        if (sizeof(ref) <= len) {
 236                error("refname too long: %s", refs_heads_master);
 237                return -1;
 238        }
 239        lockpath = mkpath("%s.lock", git_HEAD);
 240        fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666); 
 241        written = write(fd, ref, len);
 242        close(fd);
 243        if (written != len) {
 244                unlink(lockpath);
 245                error("Unable to write to %s", lockpath);
 246                return -2;
 247        }
 248        if (rename(lockpath, git_HEAD) < 0) {
 249                unlink(lockpath);
 250                error("Unable to create %s", git_HEAD);
 251                return -3;
 252        }
 253        if (adjust_shared_perm(git_HEAD)) {
 254                unlink(lockpath);
 255                error("Unable to fix permissions on %s", lockpath);
 256                return -4;
 257        }
 258        return 0;
 259}
 260
 261int read_ref(const char *ref, unsigned char *sha1)
 262{
 263        if (resolve_ref(ref, sha1, 1))
 264                return 0;
 265        return -1;
 266}
 267
 268static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1), int trim)
 269{
 270        int retval;
 271        struct ref_list *packed = get_packed_refs();
 272        struct ref_list *loose = get_loose_refs();
 273
 274        while (packed && loose) {
 275                struct ref_list *entry;
 276                int cmp = strcmp(packed->name, loose->name);
 277                if (!cmp) {
 278                        packed = packed->next;
 279                        continue;
 280                }
 281                if (cmp > 0) {
 282                        entry = loose;
 283                        loose = loose->next;
 284                } else {
 285                        entry = packed;
 286                        packed = packed->next;
 287                }
 288                if (strncmp(base, entry->name, trim))
 289                        continue;
 290                if (is_null_sha1(entry->sha1))
 291                        continue;
 292                if (!has_sha1_file(entry->sha1)) {
 293                        error("%s does not point to a valid object!", entry->name);
 294                        continue;
 295                }
 296                retval = fn(entry->name + trim, entry->sha1);
 297                if (retval)
 298                        return retval;
 299        }
 300
 301        packed = packed ? packed : loose;
 302        while (packed) {
 303                if (!strncmp(base, packed->name, trim)) {
 304                        retval = fn(packed->name + trim, packed->sha1);
 305                        if (retval)
 306                                return retval;
 307                }
 308                packed = packed->next;
 309        }
 310        return 0;
 311}
 312
 313int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
 314{
 315        unsigned char sha1[20];
 316        if (!read_ref("HEAD", sha1))
 317                return fn("HEAD", sha1);
 318        return 0;
 319}
 320
 321int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
 322{
 323        return do_for_each_ref("refs/", fn, 0);
 324}
 325
 326int for_each_tag_ref(int (*fn)(const char *path, const unsigned char *sha1))
 327{
 328        return do_for_each_ref("refs/tags/", fn, 10);
 329}
 330
 331int for_each_branch_ref(int (*fn)(const char *path, const unsigned char *sha1))
 332{
 333        return do_for_each_ref("refs/heads/", fn, 11);
 334}
 335
 336int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *sha1))
 337{
 338        return do_for_each_ref("refs/remotes/", fn, 13);
 339}
 340
 341int get_ref_sha1(const char *ref, unsigned char *sha1)
 342{
 343        if (check_ref_format(ref))
 344                return -1;
 345        return read_ref(mkpath("refs/%s", ref), sha1);
 346}
 347
 348/*
 349 * Make sure "ref" is something reasonable to have under ".git/refs/";
 350 * We do not like it if:
 351 *
 352 * - any path component of it begins with ".", or
 353 * - it has double dots "..", or
 354 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
 355 * - it ends with a "/".
 356 */
 357
 358static inline int bad_ref_char(int ch)
 359{
 360        return (((unsigned) ch) <= ' ' ||
 361                ch == '~' || ch == '^' || ch == ':' ||
 362                /* 2.13 Pattern Matching Notation */
 363                ch == '?' || ch == '*' || ch == '[');
 364}
 365
 366int check_ref_format(const char *ref)
 367{
 368        int ch, level;
 369        const char *cp = ref;
 370
 371        level = 0;
 372        while (1) {
 373                while ((ch = *cp++) == '/')
 374                        ; /* tolerate duplicated slashes */
 375                if (!ch)
 376                        return -1; /* should not end with slashes */
 377
 378                /* we are at the beginning of the path component */
 379                if (ch == '.' || bad_ref_char(ch))
 380                        return -1;
 381
 382                /* scan the rest of the path component */
 383                while ((ch = *cp++) != 0) {
 384                        if (bad_ref_char(ch))
 385                                return -1;
 386                        if (ch == '/')
 387                                break;
 388                        if (ch == '.' && *cp == '.')
 389                                return -1;
 390                }
 391                level++;
 392                if (!ch) {
 393                        if (level < 2)
 394                                return -1; /* at least of form "heads/blah" */
 395                        return 0;
 396                }
 397        }
 398}
 399
 400static struct ref_lock *verify_lock(struct ref_lock *lock,
 401        const unsigned char *old_sha1, int mustexist)
 402{
 403        char buf[40];
 404        int nr, fd = open(lock->ref_file, O_RDONLY);
 405        if (fd < 0 && (mustexist || errno != ENOENT)) {
 406                error("Can't verify ref %s", lock->ref_file);
 407                unlock_ref(lock);
 408                return NULL;
 409        }
 410        nr = read(fd, buf, 40);
 411        close(fd);
 412        if (nr != 40 || get_sha1_hex(buf, lock->old_sha1) < 0) {
 413                error("Can't verify ref %s", lock->ref_file);
 414                unlock_ref(lock);
 415                return NULL;
 416        }
 417        if (hashcmp(lock->old_sha1, old_sha1)) {
 418                error("Ref %s is at %s but expected %s", lock->ref_file,
 419                        sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
 420                unlock_ref(lock);
 421                return NULL;
 422        }
 423        return lock;
 424}
 425
 426static struct ref_lock *lock_ref_sha1_basic(const char *ref,
 427        int plen,
 428        const unsigned char *old_sha1, int mustexist)
 429{
 430        const char *orig_ref = ref;
 431        struct ref_lock *lock;
 432        struct stat st;
 433
 434        lock = xcalloc(1, sizeof(struct ref_lock));
 435        lock->lock_fd = -1;
 436
 437        ref = resolve_ref(ref, lock->old_sha1, mustexist);
 438        if (!ref) {
 439                int last_errno = errno;
 440                error("unable to resolve reference %s: %s",
 441                        orig_ref, strerror(errno));
 442                unlock_ref(lock);
 443                errno = last_errno;
 444                return NULL;
 445        }
 446        lock->lk = xcalloc(1, sizeof(struct lock_file));
 447
 448        lock->ref_file = xstrdup(git_path("%s", ref));
 449        lock->log_file = xstrdup(git_path("logs/%s", ref));
 450        lock->force_write = lstat(lock->ref_file, &st) && errno == ENOENT;
 451
 452        if (safe_create_leading_directories(lock->ref_file))
 453                die("unable to create directory for %s", lock->ref_file);
 454        lock->lock_fd = hold_lock_file_for_update(lock->lk, lock->ref_file, 1);
 455
 456        return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
 457}
 458
 459struct ref_lock *lock_ref_sha1(const char *ref,
 460        const unsigned char *old_sha1, int mustexist)
 461{
 462        if (check_ref_format(ref))
 463                return NULL;
 464        return lock_ref_sha1_basic(mkpath("refs/%s", ref),
 465                5 + strlen(ref), old_sha1, mustexist);
 466}
 467
 468struct ref_lock *lock_any_ref_for_update(const char *ref,
 469        const unsigned char *old_sha1, int mustexist)
 470{
 471        return lock_ref_sha1_basic(ref, strlen(ref), old_sha1, mustexist);
 472}
 473
 474void unlock_ref(struct ref_lock *lock)
 475{
 476        if (lock->lock_fd >= 0) {
 477                close(lock->lock_fd);
 478                /* Do not free lock->lk -- atexit() still looks at them */
 479                if (lock->lk)
 480                        rollback_lock_file(lock->lk);
 481        }
 482        free(lock->ref_file);
 483        free(lock->log_file);
 484        free(lock);
 485}
 486
 487static int log_ref_write(struct ref_lock *lock,
 488        const unsigned char *sha1, const char *msg)
 489{
 490        int logfd, written, oflags = O_APPEND | O_WRONLY;
 491        unsigned maxlen, len;
 492        char *logrec;
 493        const char *committer;
 494
 495        if (log_all_ref_updates) {
 496                if (safe_create_leading_directories(lock->log_file) < 0)
 497                        return error("unable to create directory for %s",
 498                                lock->log_file);
 499                oflags |= O_CREAT;
 500        }
 501
 502        logfd = open(lock->log_file, oflags, 0666);
 503        if (logfd < 0) {
 504                if (!log_all_ref_updates && errno == ENOENT)
 505                        return 0;
 506                return error("Unable to append to %s: %s",
 507                        lock->log_file, strerror(errno));
 508        }
 509
 510        committer = git_committer_info(1);
 511        if (msg) {
 512                maxlen = strlen(committer) + strlen(msg) + 2*40 + 5;
 513                logrec = xmalloc(maxlen);
 514                len = snprintf(logrec, maxlen, "%s %s %s\t%s\n",
 515                        sha1_to_hex(lock->old_sha1),
 516                        sha1_to_hex(sha1),
 517                        committer,
 518                        msg);
 519        }
 520        else {
 521                maxlen = strlen(committer) + 2*40 + 4;
 522                logrec = xmalloc(maxlen);
 523                len = snprintf(logrec, maxlen, "%s %s %s\n",
 524                        sha1_to_hex(lock->old_sha1),
 525                        sha1_to_hex(sha1),
 526                        committer);
 527        }
 528        written = len <= maxlen ? write(logfd, logrec, len) : -1;
 529        free(logrec);
 530        close(logfd);
 531        if (written != len)
 532                return error("Unable to append to %s", lock->log_file);
 533        return 0;
 534}
 535
 536int write_ref_sha1(struct ref_lock *lock,
 537        const unsigned char *sha1, const char *logmsg)
 538{
 539        static char term = '\n';
 540
 541        if (!lock)
 542                return -1;
 543        if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
 544                unlock_ref(lock);
 545                return 0;
 546        }
 547        if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
 548            write(lock->lock_fd, &term, 1) != 1
 549                || close(lock->lock_fd) < 0) {
 550                error("Couldn't write %s", lock->lk->filename);
 551                unlock_ref(lock);
 552                return -1;
 553        }
 554        if (log_ref_write(lock, sha1, logmsg) < 0) {
 555                unlock_ref(lock);
 556                return -1;
 557        }
 558        if (commit_lock_file(lock->lk)) {
 559                error("Couldn't set %s", lock->ref_file);
 560                unlock_ref(lock);
 561                return -1;
 562        }
 563        lock->lock_fd = -1;
 564        unlock_ref(lock);
 565        return 0;
 566}
 567
 568int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1)
 569{
 570        const char *logfile, *logdata, *logend, *rec, *lastgt, *lastrec;
 571        char *tz_c;
 572        int logfd, tz;
 573        struct stat st;
 574        unsigned long date;
 575        unsigned char logged_sha1[20];
 576
 577        logfile = git_path("logs/%s", ref);
 578        logfd = open(logfile, O_RDONLY, 0);
 579        if (logfd < 0)
 580                die("Unable to read log %s: %s", logfile, strerror(errno));
 581        fstat(logfd, &st);
 582        if (!st.st_size)
 583                die("Log %s is empty.", logfile);
 584        logdata = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd, 0);
 585        close(logfd);
 586
 587        lastrec = NULL;
 588        rec = logend = logdata + st.st_size;
 589        while (logdata < rec) {
 590                if (logdata < rec && *(rec-1) == '\n')
 591                        rec--;
 592                lastgt = NULL;
 593                while (logdata < rec && *(rec-1) != '\n') {
 594                        rec--;
 595                        if (*rec == '>')
 596                                lastgt = rec;
 597                }
 598                if (!lastgt)
 599                        die("Log %s is corrupt.", logfile);
 600                date = strtoul(lastgt + 1, &tz_c, 10);
 601                if (date <= at_time) {
 602                        if (lastrec) {
 603                                if (get_sha1_hex(lastrec, logged_sha1))
 604                                        die("Log %s is corrupt.", logfile);
 605                                if (get_sha1_hex(rec + 41, sha1))
 606                                        die("Log %s is corrupt.", logfile);
 607                                if (hashcmp(logged_sha1, sha1)) {
 608                                        tz = strtoul(tz_c, NULL, 10);
 609                                        fprintf(stderr,
 610                                                "warning: Log %s has gap after %s.\n",
 611                                                logfile, show_rfc2822_date(date, tz));
 612                                }
 613                        }
 614                        else if (date == at_time) {
 615                                if (get_sha1_hex(rec + 41, sha1))
 616                                        die("Log %s is corrupt.", logfile);
 617                        }
 618                        else {
 619                                if (get_sha1_hex(rec + 41, logged_sha1))
 620                                        die("Log %s is corrupt.", logfile);
 621                                if (hashcmp(logged_sha1, sha1)) {
 622                                        tz = strtoul(tz_c, NULL, 10);
 623                                        fprintf(stderr,
 624                                                "warning: Log %s unexpectedly ended on %s.\n",
 625                                                logfile, show_rfc2822_date(date, tz));
 626                                }
 627                        }
 628                        munmap((void*)logdata, st.st_size);
 629                        return 0;
 630                }
 631                lastrec = rec;
 632        }
 633
 634        rec = logdata;
 635        while (rec < logend && *rec != '>' && *rec != '\n')
 636                rec++;
 637        if (rec == logend || *rec == '\n')
 638                die("Log %s is corrupt.", logfile);
 639        date = strtoul(rec + 1, &tz_c, 10);
 640        tz = strtoul(tz_c, NULL, 10);
 641        if (get_sha1_hex(logdata, sha1))
 642                die("Log %s is corrupt.", logfile);
 643        munmap((void*)logdata, st.st_size);
 644        fprintf(stderr, "warning: Log %s only goes back to %s.\n",
 645                logfile, show_rfc2822_date(date, tz));
 646        return 0;
 647}