The external-odb.{c,h} files will contain the functions that are called by the rest of Git mostly from "sha1_file.c" to access the objects managed by the external odbs. The odb-helper.{c,h} files will contain the functions to actually implement communication with either the internal functions or the external scripts or processes that will manage and provide external git objects. For now only infrastructure to create helpers from the config and to manage a cache for the 'have' command is implemented. Helped-by: Jeff King <peff@xxxxxxxx> Signed-off-by: Christian Couder <chriscool@xxxxxxxxxxxxx> --- Makefile | 2 ++ external-odb.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ external-odb.h | 7 +++++ odb-helper.c | 54 +++++++++++++++++++++++++++++++++++++ odb-helper.h | 24 +++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 external-odb.c create mode 100644 external-odb.h create mode 100644 odb-helper.c create mode 100644 odb-helper.h diff --git a/Makefile b/Makefile index a1d8775adb..11dd620155 100644 --- a/Makefile +++ b/Makefile @@ -808,6 +808,7 @@ LIB_OBJS += ewah/ewah_bitmap.o LIB_OBJS += ewah/ewah_io.o LIB_OBJS += ewah/ewah_rlw.o LIB_OBJS += exec_cmd.o +LIB_OBJS += external-odb.o LIB_OBJS += fetch-object.o LIB_OBJS += fetch-pack.o LIB_OBJS += fsck.o @@ -843,6 +844,7 @@ LIB_OBJS += notes-cache.o LIB_OBJS += notes-merge.o LIB_OBJS += notes-utils.o LIB_OBJS += object.o +LIB_OBJS += odb-helper.o LIB_OBJS += oidmap.o LIB_OBJS += oidset.o LIB_OBJS += packfile.o diff --git a/external-odb.c b/external-odb.c new file mode 100644 index 0000000000..f3ea491333 --- /dev/null +++ b/external-odb.c @@ -0,0 +1,72 @@ +#include "cache.h" +#include "external-odb.h" +#include "odb-helper.h" +#include "config.h" + +static struct odb_helper *helpers; +static struct odb_helper **helpers_tail = &helpers; + +static struct odb_helper *find_or_create_helper(const char *name, int len) +{ + struct odb_helper *o; + + for (o = helpers; o; o = o->next) + if (!strncmp(o->name, name, len) && !o->name[len]) + return o; + + o = odb_helper_new(name, len); + *helpers_tail = o; + helpers_tail = &o->next; + + return o; +} + +static int external_odb_config(const char *var, const char *value, void *data) +{ + struct odb_helper *o; + const char *name; + int namelen; + const char *subkey; + + if (parse_config_key(var, "odb", &name, &namelen, &subkey) < 0) + return 0; + + o = find_or_create_helper(name, namelen); + + if (!strcmp(subkey, "promisorremote")) + return git_config_string(&o->dealer, var, value); + + return 0; +} + +static void external_odb_init(void) +{ + static int initialized; + + if (initialized) + return; + initialized = 1; + + git_config(external_odb_config, NULL); +} + +const char *external_odb_root(void) +{ + static const char *root; + if (!root) + root = git_pathdup("objects/external"); + return root; +} + +int external_odb_has_object(const unsigned char *sha1) +{ + struct odb_helper *o; + + external_odb_init(); + + for (o = helpers; o; o = o->next) + if (odb_helper_has_object(o, sha1)) + return 1; + return 0; +} + diff --git a/external-odb.h b/external-odb.h new file mode 100644 index 0000000000..ae2b228792 --- /dev/null +++ b/external-odb.h @@ -0,0 +1,7 @@ +#ifndef EXTERNAL_ODB_H +#define EXTERNAL_ODB_H + +extern const char *external_odb_root(void); +extern int external_odb_has_object(const unsigned char *sha1); + +#endif /* EXTERNAL_ODB_H */ diff --git a/odb-helper.c b/odb-helper.c new file mode 100644 index 0000000000..67196e6317 --- /dev/null +++ b/odb-helper.c @@ -0,0 +1,54 @@ +#include "cache.h" +#include "object.h" +#include "argv-array.h" +#include "odb-helper.h" +#include "run-command.h" +#include "sha1-lookup.h" + +struct odb_helper *odb_helper_new(const char *name, int namelen) +{ + struct odb_helper *o; + + o = xcalloc(1, sizeof(*o)); + o->name = xmemdupz(name, namelen); + + return o; +} + +struct odb_helper_cmd { + struct argv_array argv; + struct child_process child; +}; + +static void odb_helper_load_have(struct odb_helper *o) +{ + if (o->have_valid) + return; + o->have_valid = 1; + + /* TODO */ +} + +static const unsigned char *have_sha1_access(size_t index, void *table) +{ + struct odb_helper_object *have = table; + return have[index].oid.hash; +} + +static struct odb_helper_object *odb_helper_lookup(struct odb_helper *o, + const unsigned char *sha1) +{ + int idx; + + odb_helper_load_have(o); + idx = sha1_pos(sha1, o->have, o->have_nr, have_sha1_access); + if (idx < 0) + return NULL; + return &o->have[idx]; +} + +int odb_helper_has_object(struct odb_helper *o, const unsigned char *sha1) +{ + return !!odb_helper_lookup(o, sha1); +} + diff --git a/odb-helper.h b/odb-helper.h new file mode 100644 index 0000000000..e3c9c1cfe4 --- /dev/null +++ b/odb-helper.h @@ -0,0 +1,24 @@ +#ifndef ODB_HELPER_H +#define ODB_HELPER_H + +struct odb_helper { + const char *name; + const char *dealer; + + struct odb_helper_object { + struct object_id oid; + unsigned long size; + enum object_type type; + } *have; + int have_nr; + int have_alloc; + int have_valid; + + struct odb_helper *next; +}; + +extern struct odb_helper *odb_helper_new(const char *name, int namelen); +extern int odb_helper_has_object(struct odb_helper *o, + const unsigned char *sha1); + +#endif /* ODB_HELPER_H */ -- 2.17.0.rc0.37.g8f476fabe9