2e7b1a366eb2e054acc996b7da7038541b84b12d
   1#ifndef REFS_REF_CACHE_H
   2#define REFS_REF_CACHE_H
   3
   4/*
   5 * Information used (along with the information in ref_entry) to
   6 * describe a single cached reference.  This data structure only
   7 * occurs embedded in a union in struct ref_entry, and only when
   8 * (ref_entry->flag & REF_DIR) is zero.
   9 */
  10struct ref_value {
  11        /*
  12         * The name of the object to which this reference resolves
  13         * (which may be a tag object).  If REF_ISBROKEN, this is
  14         * null.  If REF_ISSYMREF, then this is the name of the object
  15         * referred to by the last reference in the symlink chain.
  16         */
  17        struct object_id oid;
  18
  19        /*
  20         * If REF_KNOWS_PEELED, then this field holds the peeled value
  21         * of this reference, or null if the reference is known not to
  22         * be peelable.  See the documentation for peel_ref() for an
  23         * exact definition of "peelable".
  24         */
  25        struct object_id peeled;
  26};
  27
  28/*
  29 * Information used (along with the information in ref_entry) to
  30 * describe a level in the hierarchy of references.  This data
  31 * structure only occurs embedded in a union in struct ref_entry, and
  32 * only when (ref_entry.flag & REF_DIR) is set.  In that case,
  33 * (ref_entry.flag & REF_INCOMPLETE) determines whether the references
  34 * in the directory have already been read:
  35 *
  36 *     (ref_entry.flag & REF_INCOMPLETE) unset -- a directory of loose
  37 *         or packed references, already read.
  38 *
  39 *     (ref_entry.flag & REF_INCOMPLETE) set -- a directory of loose
  40 *         references that hasn't been read yet (nor has any of its
  41 *         subdirectories).
  42 *
  43 * Entries within a directory are stored within a growable array of
  44 * pointers to ref_entries (entries, nr, alloc).  Entries 0 <= i <
  45 * sorted are sorted by their component name in strcmp() order and the
  46 * remaining entries are unsorted.
  47 *
  48 * Loose references are read lazily, one directory at a time.  When a
  49 * directory of loose references is read, then all of the references
  50 * in that directory are stored, and REF_INCOMPLETE stubs are created
  51 * for any subdirectories, but the subdirectories themselves are not
  52 * read.  The reading is triggered by get_ref_dir().
  53 */
  54struct ref_dir {
  55        int nr, alloc;
  56
  57        /*
  58         * Entries with index 0 <= i < sorted are sorted by name.  New
  59         * entries are appended to the list unsorted, and are sorted
  60         * only when required; thus we avoid the need to sort the list
  61         * after the addition of every reference.
  62         */
  63        int sorted;
  64
  65        /* A pointer to the files_ref_store that contains this ref_dir. */
  66        struct files_ref_store *ref_store;
  67
  68        struct ref_entry **entries;
  69};
  70
  71/*
  72 * Bit values for ref_entry::flag.  REF_ISSYMREF=0x01,
  73 * REF_ISPACKED=0x02, REF_ISBROKEN=0x04 and REF_BAD_NAME=0x08 are
  74 * public values; see refs.h.
  75 */
  76
  77/*
  78 * The field ref_entry->u.value.peeled of this value entry contains
  79 * the correct peeled value for the reference, which might be
  80 * null_sha1 if the reference is not a tag or if it is broken.
  81 */
  82#define REF_KNOWS_PEELED 0x10
  83
  84/* ref_entry represents a directory of references */
  85#define REF_DIR 0x20
  86
  87/*
  88 * Entry has not yet been read from disk (used only for REF_DIR
  89 * entries representing loose references)
  90 */
  91#define REF_INCOMPLETE 0x40
  92
  93/*
  94 * A ref_entry represents either a reference or a "subdirectory" of
  95 * references.
  96 *
  97 * Each directory in the reference namespace is represented by a
  98 * ref_entry with (flags & REF_DIR) set and containing a subdir member
  99 * that holds the entries in that directory that have been read so
 100 * far.  If (flags & REF_INCOMPLETE) is set, then the directory and
 101 * its subdirectories haven't been read yet.  REF_INCOMPLETE is only
 102 * used for loose reference directories.
 103 *
 104 * References are represented by a ref_entry with (flags & REF_DIR)
 105 * unset and a value member that describes the reference's value.  The
 106 * flag member is at the ref_entry level, but it is also needed to
 107 * interpret the contents of the value field (in other words, a
 108 * ref_value object is not very much use without the enclosing
 109 * ref_entry).
 110 *
 111 * Reference names cannot end with slash and directories' names are
 112 * always stored with a trailing slash (except for the top-level
 113 * directory, which is always denoted by "").  This has two nice
 114 * consequences: (1) when the entries in each subdir are sorted
 115 * lexicographically by name (as they usually are), the references in
 116 * a whole tree can be generated in lexicographic order by traversing
 117 * the tree in left-to-right, depth-first order; (2) the names of
 118 * references and subdirectories cannot conflict, and therefore the
 119 * presence of an empty subdirectory does not block the creation of a
 120 * similarly-named reference.  (The fact that reference names with the
 121 * same leading components can conflict *with each other* is a
 122 * separate issue that is regulated by refs_verify_refname_available().)
 123 *
 124 * Please note that the name field contains the fully-qualified
 125 * reference (or subdirectory) name.  Space could be saved by only
 126 * storing the relative names.  But that would require the full names
 127 * to be generated on the fly when iterating in do_for_each_ref(), and
 128 * would break callback functions, who have always been able to assume
 129 * that the name strings that they are passed will not be freed during
 130 * the iteration.
 131 */
 132struct ref_entry {
 133        unsigned char flag; /* ISSYMREF? ISPACKED? */
 134        union {
 135                struct ref_value value; /* if not (flags&REF_DIR) */
 136                struct ref_dir subdir; /* if (flags&REF_DIR) */
 137        } u;
 138        /*
 139         * The full name of the reference (e.g., "refs/heads/master")
 140         * or the full name of the directory with a trailing slash
 141         * (e.g., "refs/heads/"):
 142         */
 143        char name[FLEX_ARRAY];
 144};
 145
 146/*
 147 * Return the index of the entry with the given refname from the
 148 * ref_dir (non-recursively), sorting dir if necessary.  Return -1 if
 149 * no such entry is found.  dir must already be complete.
 150 */
 151int search_ref_dir(struct ref_dir *dir, const char *refname, size_t len);
 152
 153struct ref_dir *get_ref_dir(struct ref_entry *entry);
 154
 155/*
 156 * Create a struct ref_entry object for the specified dirname.
 157 * dirname is the name of the directory with a trailing slash (e.g.,
 158 * "refs/heads/") or "" for the top-level directory.
 159 */
 160struct ref_entry *create_dir_entry(struct files_ref_store *ref_store,
 161                                   const char *dirname, size_t len,
 162                                   int incomplete);
 163
 164struct ref_entry *create_ref_entry(const char *refname,
 165                                   const unsigned char *sha1, int flag,
 166                                   int check_name);
 167
 168void free_ref_entry(struct ref_entry *entry);
 169
 170/*
 171 * Add a ref_entry to the end of dir (unsorted).  Entry is always
 172 * stored directly in dir; no recursion into subdirectories is
 173 * done.
 174 */
 175void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry);
 176
 177/*
 178 * Remove the entry with the given name from dir, recursing into
 179 * subdirectories as necessary.  If refname is the name of a directory
 180 * (i.e., ends with '/'), then remove the directory and its contents.
 181 * If the removal was successful, return the number of entries
 182 * remaining in the directory entry that contained the deleted entry.
 183 * If the name was not found, return -1.  Please note that this
 184 * function only deletes the entry from the cache; it does not delete
 185 * it from the filesystem or ensure that other cache entries (which
 186 * might be symbolic references to the removed entry) are updated.
 187 * Nor does it remove any containing dir entries that might be made
 188 * empty by the removal.  dir must represent the top-level directory
 189 * and must already be complete.
 190 */
 191int remove_entry_from_dir(struct ref_dir *dir, const char *refname);
 192
 193/*
 194 * Add a ref_entry to the ref_dir (unsorted), recursing into
 195 * subdirectories as necessary.  dir must represent the top-level
 196 * directory.  Return 0 on success.
 197 */
 198int add_ref_entry(struct ref_dir *dir, struct ref_entry *ref);
 199
 200/*
 201 * If refname is a reference name, find the ref_dir within the dir
 202 * tree that should hold refname. If refname is a directory name
 203 * (i.e., it ends in '/'), then return that ref_dir itself. dir must
 204 * represent the top-level directory and must already be complete.
 205 * Sort ref_dirs and recurse into subdirectories as necessary. If
 206 * mkdir is set, then create any missing directories; otherwise,
 207 * return NULL if the desired directory cannot be found.
 208 */
 209struct ref_dir *find_containing_dir(struct ref_dir *dir,
 210                                    const char *refname, int mkdir);
 211
 212/*
 213 * Find the value entry with the given name in dir, sorting ref_dirs
 214 * and recursing into subdirectories as necessary.  If the name is not
 215 * found or it corresponds to a directory entry, return NULL.
 216 */
 217struct ref_entry *find_ref_entry(struct ref_dir *dir, const char *refname);
 218
 219struct ref_iterator *cache_ref_iterator_begin(struct ref_dir *dir);
 220
 221typedef int each_ref_entry_fn(struct ref_entry *entry, void *cb_data);
 222
 223/*
 224 * Call fn for each reference in dir that has index in the range
 225 * offset <= index < dir->nr.  Recurse into subdirectories that are in
 226 * that index range, sorting them before iterating.  This function
 227 * does not sort dir itself; it should be sorted beforehand.  fn is
 228 * called for all references, including broken ones.
 229 */
 230int do_for_each_entry_in_dir(struct ref_dir *dir, int offset,
 231                             each_ref_entry_fn fn, void *cb_data);
 232
 233/*
 234 * Peel the entry (if possible) and return its new peel_status.  If
 235 * repeel is true, re-peel the entry even if there is an old peeled
 236 * value that is already stored in it.
 237 *
 238 * It is OK to call this function with a packed reference entry that
 239 * might be stale and might even refer to an object that has since
 240 * been garbage-collected.  In such a case, if the entry has
 241 * REF_KNOWS_PEELED then leave the status unchanged and return
 242 * PEEL_PEELED or PEEL_NON_TAG; otherwise, return PEEL_INVALID.
 243 */
 244enum peel_status peel_entry(struct ref_entry *entry, int repeel);
 245
 246/*
 247 * Load all of the refs from `dir` into our in-memory cache.
 248 */
 249void prime_ref_dir(struct ref_dir *dir);
 250
 251#endif /* REFS_REF_CACHE_H */