object-store.hon commit shallow: add repository argument to register_shallow (19143f1)
   1#ifndef OBJECT_STORE_H
   2#define OBJECT_STORE_H
   3
   4#include "oidmap.h"
   5
   6struct alternate_object_database {
   7        struct alternate_object_database *next;
   8
   9        /* see alt_scratch_buf() */
  10        struct strbuf scratch;
  11        size_t base_len;
  12
  13        /*
  14         * Used to store the results of readdir(3) calls when searching
  15         * for unique abbreviated hashes.  This cache is never
  16         * invalidated, thus it's racy and not necessarily accurate.
  17         * That's fine for its purpose; don't use it for tasks requiring
  18         * greater accuracy!
  19         */
  20        char loose_objects_subdir_seen[256];
  21        struct oid_array loose_objects_cache;
  22
  23        /*
  24         * Path to the alternative object store. If this is a relative path,
  25         * it is relative to the current working directory.
  26         */
  27        char path[FLEX_ARRAY];
  28};
  29void prepare_alt_odb(struct repository *r);
  30char *compute_alternate_path(const char *path, struct strbuf *err);
  31typedef int alt_odb_fn(struct alternate_object_database *, void *);
  32int foreach_alt_odb(alt_odb_fn, void*);
  33
  34/*
  35 * Allocate a "struct alternate_object_database" but do _not_ actually
  36 * add it to the list of alternates.
  37 */
  38struct alternate_object_database *alloc_alt_odb(const char *dir);
  39
  40/*
  41 * Add the directory to the on-disk alternates file; the new entry will also
  42 * take effect in the current process.
  43 */
  44void add_to_alternates_file(const char *dir);
  45
  46/*
  47 * Add the directory to the in-memory list of alternates (along with any
  48 * recursive alternates it points to), but do not modify the on-disk alternates
  49 * file.
  50 */
  51void add_to_alternates_memory(const char *dir);
  52
  53/*
  54 * Returns a scratch strbuf pre-filled with the alternate object directory,
  55 * including a trailing slash, which can be used to access paths in the
  56 * alternate. Always use this over direct access to alt->scratch, as it
  57 * cleans up any previous use of the scratch buffer.
  58 */
  59struct strbuf *alt_scratch_buf(struct alternate_object_database *alt);
  60
  61struct packed_git {
  62        struct packed_git *next;
  63        struct list_head mru;
  64        struct pack_window *windows;
  65        off_t pack_size;
  66        const void *index_data;
  67        size_t index_size;
  68        uint32_t num_objects;
  69        uint32_t num_bad_objects;
  70        unsigned char *bad_object_sha1;
  71        int index_version;
  72        time_t mtime;
  73        int pack_fd;
  74        unsigned pack_local:1,
  75                 pack_keep:1,
  76                 freshened:1,
  77                 do_not_close:1,
  78                 pack_promisor:1;
  79        unsigned char sha1[20];
  80        struct revindex_entry *revindex;
  81        /* something like ".git/objects/pack/xxxxx.pack" */
  82        char pack_name[FLEX_ARRAY]; /* more */
  83};
  84
  85struct raw_object_store {
  86        /*
  87         * Path to the repository's object store.
  88         * Cannot be NULL after initialization.
  89         */
  90        char *objectdir;
  91
  92        /* Path to extra alternate object database if not NULL */
  93        char *alternate_db;
  94
  95        struct alternate_object_database *alt_odb_list;
  96        struct alternate_object_database **alt_odb_tail;
  97
  98        /*
  99         * Objects that should be substituted by other objects
 100         * (see git-replace(1)).
 101         */
 102        struct oidmap *replace_map;
 103
 104        /*
 105         * private data
 106         *
 107         * should only be accessed directly by packfile.c
 108         */
 109
 110        struct packed_git *packed_git;
 111        /* A most-recently-used ordered version of the packed_git list. */
 112        struct list_head packed_git_mru;
 113
 114        /*
 115         * A fast, rough count of the number of objects in the repository.
 116         * These two fields are not meant for direct access. Use
 117         * approximate_object_count() instead.
 118         */
 119        unsigned long approximate_object_count;
 120        unsigned approximate_object_count_valid : 1;
 121
 122        /*
 123         * Whether packed_git has already been populated with this repository's
 124         * packs.
 125         */
 126        unsigned packed_git_initialized : 1;
 127};
 128
 129struct raw_object_store *raw_object_store_new(void);
 130void raw_object_store_clear(struct raw_object_store *o);
 131
 132/*
 133 * Put in `buf` the name of the file in the local object database that
 134 * would be used to store a loose object with the specified sha1.
 135 */
 136void sha1_file_name(struct repository *r, struct strbuf *buf, const unsigned char *sha1);
 137
 138void *map_sha1_file(struct repository *r, const unsigned char *sha1, unsigned long *size);
 139
 140extern void *read_object_file_extended(const struct object_id *oid,
 141                                       enum object_type *type,
 142                                       unsigned long *size, int lookup_replace);
 143static inline void *read_object_file(const struct object_id *oid, enum object_type *type, unsigned long *size)
 144{
 145        return read_object_file_extended(oid, type, size, 1);
 146}
 147
 148/* Read and unpack an object file into memory, write memory to an object file */
 149int oid_object_info(struct repository *r, const struct object_id *, unsigned long *);
 150
 151extern int hash_object_file(const void *buf, unsigned long len,
 152                            const char *type, struct object_id *oid);
 153
 154extern int write_object_file(const void *buf, unsigned long len,
 155                             const char *type, struct object_id *oid);
 156
 157extern int hash_object_file_literally(const void *buf, unsigned long len,
 158                                      const char *type, struct object_id *oid,
 159                                      unsigned flags);
 160
 161extern int pretend_object_file(void *, unsigned long, enum object_type,
 162                               struct object_id *oid);
 163
 164extern int force_object_loose(const struct object_id *oid, time_t mtime);
 165
 166/*
 167 * Open the loose object at path, check its hash, and return the contents,
 168 * type, and size. If the object is a blob, then "contents" may return NULL,
 169 * to allow streaming of large blobs.
 170 *
 171 * Returns 0 on success, negative on error (details may be written to stderr).
 172 */
 173int read_loose_object(const char *path,
 174                      const struct object_id *expected_oid,
 175                      enum object_type *type,
 176                      unsigned long *size,
 177                      void **contents);
 178
 179/*
 180 * Convenience for sha1_object_info_extended() with a NULL struct
 181 * object_info. OBJECT_INFO_SKIP_CACHED is automatically set; pass
 182 * nonzero flags to also set other flags.
 183 */
 184extern int has_sha1_file_with_flags(const unsigned char *sha1, int flags);
 185static inline int has_sha1_file(const unsigned char *sha1)
 186{
 187        return has_sha1_file_with_flags(sha1, 0);
 188}
 189
 190/* Same as the above, except for struct object_id. */
 191extern int has_object_file(const struct object_id *oid);
 192extern int has_object_file_with_flags(const struct object_id *oid, int flags);
 193
 194/*
 195 * Return true iff an alternate object database has a loose object
 196 * with the specified name.  This function does not respect replace
 197 * references.
 198 */
 199extern int has_loose_object_nonlocal(const unsigned char *sha1);
 200
 201extern void assert_oid_type(const struct object_id *oid, enum object_type expect);
 202
 203struct object_info {
 204        /* Request */
 205        enum object_type *typep;
 206        unsigned long *sizep;
 207        off_t *disk_sizep;
 208        unsigned char *delta_base_sha1;
 209        struct strbuf *type_name;
 210        void **contentp;
 211
 212        /* Response */
 213        enum {
 214                OI_CACHED,
 215                OI_LOOSE,
 216                OI_PACKED,
 217                OI_DBCACHED
 218        } whence;
 219        union {
 220                /*
 221                 * struct {
 222                 *      ... Nothing to expose in this case
 223                 * } cached;
 224                 * struct {
 225                 *      ... Nothing to expose in this case
 226                 * } loose;
 227                 */
 228                struct {
 229                        struct packed_git *pack;
 230                        off_t offset;
 231                        unsigned int is_delta;
 232                } packed;
 233        } u;
 234};
 235
 236/*
 237 * Initializer for a "struct object_info" that wants no items. You may
 238 * also memset() the memory to all-zeroes.
 239 */
 240#define OBJECT_INFO_INIT {NULL}
 241
 242/* Invoke lookup_replace_object() on the given hash */
 243#define OBJECT_INFO_LOOKUP_REPLACE 1
 244/* Allow reading from a loose object file of unknown/bogus type */
 245#define OBJECT_INFO_ALLOW_UNKNOWN_TYPE 2
 246/* Do not check cached storage */
 247#define OBJECT_INFO_SKIP_CACHED 4
 248/* Do not retry packed storage after checking packed and loose storage */
 249#define OBJECT_INFO_QUICK 8
 250/* Do not check loose object */
 251#define OBJECT_INFO_IGNORE_LOOSE 16
 252
 253int oid_object_info_extended(struct repository *r,
 254                             const struct object_id *,
 255                             struct object_info *, unsigned flags);
 256
 257#endif /* OBJECT_STORE_H */