summaryrefslogtreecommitdiff
path: root/sha1_file.c
diff options
context:
space:
mode:
authorLibravatar Junio C Hamano <gitster@pobox.com>2018-04-11 13:09:55 +0900
committerLibravatar Junio C Hamano <gitster@pobox.com>2018-04-11 13:09:55 +0900
commitcf0b1793ead9428d88e6592e624c7cb222913c58 (patch)
tree13906a437b3a68fdaf9cfa4617280dae44aa41db /sha1_file.c
parentMerge branch 'jc/test-must-be-empty' (diff)
parentsha1_file: allow sha1_loose_object_info to handle arbitrary repositories (diff)
downloadtgif-cf0b1793ead9428d88e6592e624c7cb222913c58.tar.xz
Merge branch 'sb/object-store'
Refactoring the internal global data structure to make it possible to open multiple repositories, work with and then close them. Rerolled by Duy on top of a separate preliminary clean-up topic. The resulting structure of the topics looked very sensible. * sb/object-store: (27 commits) sha1_file: allow sha1_loose_object_info to handle arbitrary repositories sha1_file: allow map_sha1_file to handle arbitrary repositories sha1_file: allow map_sha1_file_1 to handle arbitrary repositories sha1_file: allow open_sha1_file to handle arbitrary repositories sha1_file: allow stat_sha1_file to handle arbitrary repositories sha1_file: allow sha1_file_name to handle arbitrary repositories sha1_file: add repository argument to sha1_loose_object_info sha1_file: add repository argument to map_sha1_file sha1_file: add repository argument to map_sha1_file_1 sha1_file: add repository argument to open_sha1_file sha1_file: add repository argument to stat_sha1_file sha1_file: add repository argument to sha1_file_name sha1_file: allow prepare_alt_odb to handle arbitrary repositories sha1_file: allow link_alt_odb_entries to handle arbitrary repositories sha1_file: add repository argument to prepare_alt_odb sha1_file: add repository argument to link_alt_odb_entries sha1_file: add repository argument to read_info_alternates sha1_file: add repository argument to link_alt_odb_entry sha1_file: add raw_object_store argument to alt_odb_usable pack: move approximate object count to object store ...
Diffstat (limited to 'sha1_file.c')
-rw-r--r--sha1_file.c125
1 files changed, 67 insertions, 58 deletions
diff --git a/sha1_file.c b/sha1_file.c
index aea9124a78..aab3b58e03 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -22,6 +22,7 @@
#include "pack-revindex.h"
#include "sha1-lookup.h"
#include "bulk-checkin.h"
+#include "repository.h"
#include "streaming.h"
#include "dir.h"
#include "list.h"
@@ -29,6 +30,7 @@
#include "quote.h"
#include "packfile.h"
#include "fetch-object.h"
+#include "object-store.h"
/* The maximum size for an object header. */
#define MAX_HEADER_LEN 32
@@ -322,9 +324,9 @@ static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1)
}
}
-void sha1_file_name(struct strbuf *buf, const unsigned char *sha1)
+void sha1_file_name(struct repository *r, struct strbuf *buf, const unsigned char *sha1)
{
- strbuf_addstr(buf, get_object_directory());
+ strbuf_addstr(buf, r->objects->objectdir);
strbuf_addch(buf, '/');
fill_sha1_path(buf, sha1);
}
@@ -343,13 +345,12 @@ static const char *alt_sha1_path(struct alternate_object_database *alt,
return buf->buf;
}
-struct alternate_object_database *alt_odb_list;
-static struct alternate_object_database **alt_odb_tail;
-
/*
* Return non-zero iff the path is usable as an alternate object database.
*/
-static int alt_odb_usable(struct strbuf *path, const char *normalized_objdir)
+static int alt_odb_usable(struct raw_object_store *o,
+ struct strbuf *path,
+ const char *normalized_objdir)
{
struct alternate_object_database *alt;
@@ -365,7 +366,7 @@ static int alt_odb_usable(struct strbuf *path, const char *normalized_objdir)
* Prevent the common mistake of listing the same
* thing twice, or object directory itself.
*/
- for (alt = alt_odb_list; alt; alt = alt->next) {
+ for (alt = o->alt_odb_list; alt; alt = alt->next) {
if (!fspathcmp(path->buf, alt->path))
return 0;
}
@@ -390,9 +391,11 @@ static int alt_odb_usable(struct strbuf *path, const char *normalized_objdir)
* SHA1, an extra slash for the first level indirection, and the
* terminating NUL.
*/
-static void read_info_alternates(const char * relative_base, int depth);
-static int link_alt_odb_entry(const char *entry, const char *relative_base,
- int depth, const char *normalized_objdir)
+static void read_info_alternates(struct repository *r,
+ const char *relative_base,
+ int depth);
+static int link_alt_odb_entry(struct repository *r, const char *entry,
+ const char *relative_base, int depth, const char *normalized_objdir)
{
struct alternate_object_database *ent;
struct strbuf pathbuf = STRBUF_INIT;
@@ -417,7 +420,7 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,
while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
strbuf_setlen(&pathbuf, pathbuf.len - 1);
- if (!alt_odb_usable(&pathbuf, normalized_objdir)) {
+ if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir)) {
strbuf_release(&pathbuf);
return -1;
}
@@ -425,12 +428,12 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,
ent = alloc_alt_odb(pathbuf.buf);
/* add the alternate entry */
- *alt_odb_tail = ent;
- alt_odb_tail = &(ent->next);
+ *r->objects->alt_odb_tail = ent;
+ r->objects->alt_odb_tail = &(ent->next);
ent->next = NULL;
/* recursively add alternates */
- read_info_alternates(pathbuf.buf, depth + 1);
+ read_info_alternates(r, pathbuf.buf, depth + 1);
strbuf_release(&pathbuf);
return 0;
@@ -465,8 +468,8 @@ static const char *parse_alt_odb_entry(const char *string,
return end;
}
-static void link_alt_odb_entries(const char *alt, int sep,
- const char *relative_base, int depth)
+static void link_alt_odb_entries(struct repository *r, const char *alt,
+ int sep, const char *relative_base, int depth)
{
struct strbuf objdirbuf = STRBUF_INIT;
struct strbuf entry = STRBUF_INIT;
@@ -480,7 +483,7 @@ static void link_alt_odb_entries(const char *alt, int sep,
return;
}
- strbuf_add_absolute_path(&objdirbuf, get_object_directory());
+ strbuf_add_absolute_path(&objdirbuf, r->objects->objectdir);
if (strbuf_normalize_path(&objdirbuf) < 0)
die("unable to normalize object directory: %s",
objdirbuf.buf);
@@ -489,13 +492,16 @@ static void link_alt_odb_entries(const char *alt, int sep,
alt = parse_alt_odb_entry(alt, sep, &entry);
if (!entry.len)
continue;
- link_alt_odb_entry(entry.buf, relative_base, depth, objdirbuf.buf);
+ link_alt_odb_entry(r, entry.buf,
+ relative_base, depth, objdirbuf.buf);
}
strbuf_release(&entry);
strbuf_release(&objdirbuf);
}
-static void read_info_alternates(const char * relative_base, int depth)
+static void read_info_alternates(struct repository *r,
+ const char *relative_base,
+ int depth)
{
char *path;
struct strbuf buf = STRBUF_INIT;
@@ -507,7 +513,7 @@ static void read_info_alternates(const char * relative_base, int depth)
return;
}
- link_alt_odb_entries(buf.buf, '\n', relative_base, depth);
+ link_alt_odb_entries(r, buf.buf, '\n', relative_base, depth);
strbuf_release(&buf);
free(path);
}
@@ -560,8 +566,9 @@ void add_to_alternates_file(const char *reference)
fprintf_or_die(out, "%s\n", reference);
if (commit_lock_file(&lock))
die_errno("unable to move new alternates file into place");
- if (alt_odb_tail)
- link_alt_odb_entries(reference, '\n', NULL, 0);
+ if (the_repository->objects->alt_odb_tail)
+ link_alt_odb_entries(the_repository, reference,
+ '\n', NULL, 0);
}
free(alts);
}
@@ -572,9 +579,10 @@ void add_to_alternates_memory(const char *reference)
* Make sure alternates are initialized, or else our entry may be
* overwritten when they are.
*/
- prepare_alt_odb();
+ prepare_alt_odb(the_repository);
- link_alt_odb_entries(reference, '\n', NULL, 0);
+ link_alt_odb_entries(the_repository, reference,
+ '\n', NULL, 0);
}
/*
@@ -657,8 +665,8 @@ int foreach_alt_odb(alt_odb_fn fn, void *cb)
struct alternate_object_database *ent;
int r = 0;
- prepare_alt_odb();
- for (ent = alt_odb_list; ent; ent = ent->next) {
+ prepare_alt_odb(the_repository);
+ for (ent = the_repository->objects->alt_odb_list; ent; ent = ent->next) {
r = fn(ent, cb);
if (r)
break;
@@ -666,15 +674,15 @@ int foreach_alt_odb(alt_odb_fn fn, void *cb)
return r;
}
-void prepare_alt_odb(void)
+void prepare_alt_odb(struct repository *r)
{
- if (alt_odb_tail)
+ if (r->objects->alt_odb_tail)
return;
- alt_odb_tail = &alt_odb_list;
- link_alt_odb_entries(the_repository->alternate_db, PATH_SEP, NULL, 0);
+ r->objects->alt_odb_tail = &r->objects->alt_odb_list;
+ link_alt_odb_entries(r, r->objects->alternate_db, PATH_SEP, NULL, 0);
- read_info_alternates(get_object_directory(), 0);
+ read_info_alternates(r, r->objects->objectdir, 0);
}
/* Returns 1 if we have successfully freshened the file, 0 otherwise. */
@@ -706,7 +714,7 @@ static int check_and_freshen_local(const unsigned char *sha1, int freshen)
static struct strbuf buf = STRBUF_INIT;
strbuf_reset(&buf);
- sha1_file_name(&buf, sha1);
+ sha1_file_name(the_repository, &buf, sha1);
return check_and_freshen_file(buf.buf, freshen);
}
@@ -714,8 +722,8 @@ static int check_and_freshen_local(const unsigned char *sha1, int freshen)
static int check_and_freshen_nonlocal(const unsigned char *sha1, int freshen)
{
struct alternate_object_database *alt;
- prepare_alt_odb();
- for (alt = alt_odb_list; alt; alt = alt->next) {
+ prepare_alt_odb(the_repository);
+ for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
const char *path = alt_sha1_path(alt, sha1);
if (check_and_freshen_file(path, freshen))
return 1;
@@ -860,22 +868,22 @@ int git_open_cloexec(const char *name, int flags)
* Note that it may point to static storage and is only valid until another
* call to sha1_file_name(), etc.
*/
-static int stat_sha1_file(const unsigned char *sha1, struct stat *st,
- const char **path)
+static int stat_sha1_file(struct repository *r, const unsigned char *sha1,
+ struct stat *st, const char **path)
{
struct alternate_object_database *alt;
static struct strbuf buf = STRBUF_INIT;
strbuf_reset(&buf);
- sha1_file_name(&buf, sha1);
+ sha1_file_name(r, &buf, sha1);
*path = buf.buf;
if (!lstat(*path, st))
return 0;
- prepare_alt_odb();
+ prepare_alt_odb(r);
errno = ENOENT;
- for (alt = alt_odb_list; alt; alt = alt->next) {
+ for (alt = r->objects->alt_odb_list; alt; alt = alt->next) {
*path = alt_sha1_path(alt, sha1);
if (!lstat(*path, st))
return 0;
@@ -888,7 +896,8 @@ static int stat_sha1_file(const unsigned char *sha1, struct stat *st,
* Like stat_sha1_file(), but actually open the object and return the
* descriptor. See the caveats on the "path" parameter above.
*/
-static int open_sha1_file(const unsigned char *sha1, const char **path)
+static int open_sha1_file(struct repository *r,
+ const unsigned char *sha1, const char **path)
{
int fd;
struct alternate_object_database *alt;
@@ -896,7 +905,7 @@ static int open_sha1_file(const unsigned char *sha1, const char **path)
static struct strbuf buf = STRBUF_INIT;
strbuf_reset(&buf);
- sha1_file_name(&buf, sha1);
+ sha1_file_name(r, &buf, sha1);
*path = buf.buf;
fd = git_open(*path);
@@ -904,8 +913,8 @@ static int open_sha1_file(const unsigned char *sha1, const char **path)
return fd;
most_interesting_errno = errno;
- prepare_alt_odb();
- for (alt = alt_odb_list; alt; alt = alt->next) {
+ prepare_alt_odb(r);
+ for (alt = r->objects->alt_odb_list; alt; alt = alt->next) {
*path = alt_sha1_path(alt, sha1);
fd = git_open(*path);
if (fd >= 0)
@@ -921,9 +930,8 @@ static int open_sha1_file(const unsigned char *sha1, const char **path)
* Map the loose object at "path" if it is not NULL, or the path found by
* searching for a loose object named "sha1".
*/
-static void *map_sha1_file_1(const char *path,
- const unsigned char *sha1,
- unsigned long *size)
+static void *map_sha1_file_1(struct repository *r, const char *path,
+ const unsigned char *sha1, unsigned long *size)
{
void *map;
int fd;
@@ -931,7 +939,7 @@ static void *map_sha1_file_1(const char *path,
if (path)
fd = git_open(path);
else
- fd = open_sha1_file(sha1, &path);
+ fd = open_sha1_file(r, sha1, &path);
map = NULL;
if (fd >= 0) {
struct stat st;
@@ -950,9 +958,10 @@ static void *map_sha1_file_1(const char *path,
return map;
}
-void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
+void *map_sha1_file(struct repository *r,
+ const unsigned char *sha1, unsigned long *size)
{
- return map_sha1_file_1(NULL, sha1, size);
+ return map_sha1_file_1(r, NULL, sha1, size);
}
static int unpack_sha1_short_header(git_zstream *stream,
@@ -1141,9 +1150,9 @@ int parse_sha1_header(const char *hdr, unsigned long *sizep)
return parse_sha1_header_extended(hdr, &oi, 0);
}
-static int sha1_loose_object_info(const unsigned char *sha1,
- struct object_info *oi,
- int flags)
+static int sha1_loose_object_info(struct repository *r,
+ const unsigned char *sha1,
+ struct object_info *oi, int flags)
{
int status = 0;
unsigned long mapsize;
@@ -1167,14 +1176,14 @@ static int sha1_loose_object_info(const unsigned char *sha1,
if (!oi->typep && !oi->type_name && !oi->sizep && !oi->contentp) {
const char *path;
struct stat st;
- if (stat_sha1_file(sha1, &st, &path) < 0)
+ if (stat_sha1_file(r, sha1, &st, &path) < 0)
return -1;
if (oi->disk_sizep)
*oi->disk_sizep = st.st_size;
return 0;
}
- map = map_sha1_file(sha1, &mapsize);
+ map = map_sha1_file(r, sha1, &mapsize);
if (!map)
return -1;
@@ -1266,7 +1275,7 @@ int oid_object_info_extended(const struct object_id *oid, struct object_info *oi
return -1;
/* Most likely it's a loose object. */
- if (!sha1_loose_object_info(real->hash, oi, flags))
+ if (!sha1_loose_object_info(the_repository, real->hash, oi, flags))
return 0;
/* Not a loose object; someone else may have just packed it. */
@@ -1390,7 +1399,7 @@ void *read_object_file_extended(const struct object_id *oid,
die("replacement %s not found for %s",
oid_to_hex(repl), oid_to_hex(oid));
- if (!stat_sha1_file(repl->hash, &st, &path))
+ if (!stat_sha1_file(the_repository, repl->hash, &st, &path))
die("loose object %s (stored in %s) is corrupt",
oid_to_hex(repl), path);
@@ -1591,7 +1600,7 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
static struct strbuf filename = STRBUF_INIT;
strbuf_reset(&filename);
- sha1_file_name(&filename, oid->hash);
+ sha1_file_name(the_repository, &filename, oid->hash);
fd = create_tmpfile(&tmp_file, filename.buf);
if (fd < 0) {
@@ -2199,7 +2208,7 @@ int read_loose_object(const char *path,
*contents = NULL;
- map = map_sha1_file_1(path, NULL, &mapsize);
+ map = map_sha1_file_1(the_repository, path, NULL, &mapsize);
if (!map) {
error_errno("unable to mmap %s", path);
goto out;