summaryrefslogtreecommitdiff
path: root/src/odb.c
diff options
context:
space:
mode:
authorRamsay Jones <ramsay@ramsay1.demon.co.uk>2009-02-10 21:49:48 +0000
committerShawn O. Pearce <spearce@spearce.org>2009-02-11 11:04:55 -0800
commit82324ac1a5ec79069877ed5d807c87762a16ed9d (patch)
tree8ef9060a954118a698fc10e20acdc430566a100f /src/odb.c
parente9f5e87781e88aa3afa7679783c204604479f9e9 (diff)
downloadlibgit2-82324ac1a5ec79069877ed5d807c87762a16ed9d.tar.gz
Add the git_odb_exists() object query function
This function determines if the given object can be found in the object database. At present, only the local object database is searched. Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'src/odb.c')
-rw-r--r--src/odb.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/odb.c b/src/odb.c
index fc58168ad..f34fd002b 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -819,6 +819,66 @@ static git_packlist *packlist_get(git_odb *db)
return pl;
}
+static int search_packs(git_pack **p, off_t *offset, git_odb *db, const git_oid *id)
+{
+ git_packlist *pl = packlist_get(db);
+ size_t j;
+
+ if (!pl)
+ return GIT_ENOTFOUND;
+
+ for (j = 0; j < pl->n_packs; j++) {
+
+ git_pack *pack = pl->packs[j];
+ off_t pos;
+ int res;
+
+ if (pack_openidx(pack))
+ continue;
+ res = pack->idx_search(&pos, pack, id);
+ pack_decidx(pack);
+
+ if (!res) {
+ packlist_dec(db,pl);
+ if (p)
+ *p = pack;
+ if (offset)
+ *offset = pos;
+ return GIT_SUCCESS;
+ }
+
+ }
+
+ packlist_dec(db,pl);
+ return GIT_ENOTFOUND;
+}
+
+static int exists_packed(git_odb *db, const git_oid *id)
+{
+ return !search_packs(NULL, NULL, db, id);
+}
+
+static int exists_loose(git_odb *db, const git_oid *id)
+{
+ char file[GIT_PATH_MAX];
+
+ if (object_file_name(file, sizeof(file), db->objects_dir, id) < 0)
+ return 0;
+
+ if (gitfo_exists(file) < 0)
+ return 0;
+
+ return 1;
+}
+
+int git_odb_exists(git_odb *db, const git_oid *id)
+{
+ /* TODO: extend to search alternate db's */
+ if (exists_packed(db, id))
+ return 1;
+ return exists_loose(db, id);
+}
+
int git_odb_open(git_odb **out, const char *objects_dir)
{
git_odb *db = git__calloc(1, sizeof(*db));