Skip to content
This repository was archived by the owner on Jan 19, 2021. It is now read-only.

Commit 3609884

Browse files
committed
Updated ODB API
1 parent 3ce2cb3 commit 3609884

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -971,9 +971,9 @@ The project is available under the [MIT](https://opensource.org/licenses/MIT) li
971971

972972
| libgit2 | cppgit2:: |
973973
| --- | --- |
974-
| `git_odb_add_alternate` | |
975-
| `git_odb_add_backend` | |
976-
| `git_odb_add_disk_alternate` | |
974+
| `git_odb_add_alternate` | `odb::add_alternate_backend` |
975+
| `git_odb_add_backend` | `odb::add_backend` |
976+
| `git_odb_add_disk_alternate` | `odb::add_disk_alternate_backend` |
977977
| `git_odb_backend_loose` | `odb::create_backend_for_loose_objects` |
978978
| `git_odb_backend_one_pack` | `odb::create_backend_for_one_packfile` |
979979
| `git_odb_backend_pack` | `odb::create_backend_for_packfiles` |
@@ -1004,8 +1004,8 @@ The project is available under the [MIT](https://opensource.org/licenses/MIT) li
10041004
| `git_odb_stream_free` | `odb::stream::~stream` |
10051005
| `git_odb_stream_read` | `odb::stream::read` |
10061006
| `git_odb_stream_write` | `odb::stream::write` |
1007-
| `git_odb_write` | |
1008-
| `git_odb_write_pack` | |
1007+
| `git_odb_write` | `odb::write` |
1008+
| `git_odb_write_pack` | **Not Implemented** |
10091009

10101010

10111011
### oid

include/cppgit2/odb.hpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
#include <cppgit2/oid.hpp>
77
#include <cppgit2/ownership.hpp>
88
#include <git2.h>
9+
#include <tuple>
910
#include <utility>
1011
#include <vector>
11-
#include <tuple>
1212

1313
namespace cppgit2 {
1414

@@ -40,6 +40,25 @@ class odb : public libgit2_api {
4040
git_odb_backend *c_ptr_;
4141
};
4242

43+
// Add a custom backend to an existing Object DB; this backend will work as an
44+
// alternate.
45+
//
46+
// Alternate backends are always checked for objects after all the main
47+
// backends have been exhausted. The backends are checked in relative
48+
// ordering, based on the value of the priority parameter. Writing is disabled
49+
// on alternate backends.
50+
void add_alternate_backend(const backend &backend, int priority);
51+
52+
// Add a custom backend to an existing Object DB
53+
// The backends are checked in relative ordering, based on the value of the priority parameter.
54+
void add_backend(const backend &backend, int priority);
55+
56+
// Add an on-disk alternate to an existing Object DB.
57+
// Note that the added path must point to an objects, not to a full repository, to use it as an alternate store.
58+
// Alternate backends are always checked for objects after all the main backends have been exhausted.
59+
// Writing is disabled on alternate backends.
60+
void add_disk_alternate_backend(const std::string &path);
61+
4362
// Create a backend for loose objects
4463
static backend
4564
create_backend_for_loose_objects(const std::string &objects_dir,
@@ -298,15 +317,19 @@ class odb : public libgit2_api {
298317
// all backends.
299318
//
300319
// Returns {stream, length of object, type of object}
301-
std::tuple<stream, size_t, cppgit2::object::object_type> open_rstream(const oid &id);
320+
std::tuple<stream, size_t, cppgit2::object::object_type>
321+
open_rstream(const oid &id);
302322

303323
// Open a stream to write an object into the ODB
304324
// The type and final length of the object must be specified when opening the
305325
// stream. The returned stream will be of type GIT_STREAM_WRONLY, and it won't
306326
// be effective until git_odb_stream_finalize_write is called and returns
307327
// without an error
308328
stream open_wstream(cppgit2::object::object_size size,
309-
cppgit2::object::object_type type);
329+
cppgit2::object::object_type type);
330+
331+
// Write an object directly into the ODB
332+
oid write(const void *data, size_t length, cppgit2::object::object_type type);
310333

311334
// Access libgit2 C ptr
312335
const git_odb *c_ptr() const;

src/odb.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ odb::~odb() {
1212
git_odb_free(c_ptr_);
1313
}
1414

15+
void odb::add_alternate_backend(const backend &backend, int priority) {
16+
if (git_odb_add_alternate(c_ptr_, backend.c_ptr_, priority))
17+
throw git_exception();
18+
}
19+
20+
void odb::add_backend(const backend &backend, int priority) {
21+
if (git_odb_add_backend(c_ptr_, backend.c_ptr_, priority))
22+
throw git_exception();
23+
}
24+
25+
void odb::add_disk_alternate_backend(const std::string &path) {
26+
if (git_odb_add_disk_alternate(c_ptr_, path.c_str()))
27+
throw git_exception();
28+
}
29+
1530
odb::backend
1631
odb::create_backend_for_loose_objects(const std::string &objects_dir,
1732
int compression_level, bool do_fsync,
@@ -141,7 +156,8 @@ odb odb::open(const std::string &objects_dir) {
141156
return result;
142157
}
143158

144-
std::tuple<odb::stream, size_t, cppgit2::object::object_type> odb::open_rstream(const oid &id) {
159+
std::tuple<odb::stream, size_t, cppgit2::object::object_type>
160+
odb::open_rstream(const oid &id) {
145161
stream result(nullptr);
146162
size_t length;
147163
git_object_t type;
@@ -151,10 +167,20 @@ std::tuple<odb::stream, size_t, cppgit2::object::object_type> odb::open_rstream(
151167
}
152168

153169
odb::stream odb::open_wstream(cppgit2::object::object_size size,
154-
cppgit2::object::object_type type) {
170+
cppgit2::object::object_type type) {
155171
stream result(nullptr);
156-
if (git_odb_open_wstream(&result.c_ptr_, c_ptr_, static_cast<git_object_size_t>(size),
157-
static_cast<git_object_t>(type)))
172+
if (git_odb_open_wstream(&result.c_ptr_, c_ptr_,
173+
static_cast<git_object_size_t>(size),
174+
static_cast<git_object_t>(type)))
175+
throw git_exception();
176+
return result;
177+
}
178+
179+
oid odb::write(const void *data, size_t length,
180+
cppgit2::object::object_type type) {
181+
oid result;
182+
if (git_odb_write(result.c_ptr(), c_ptr_, data, length,
183+
static_cast<git_object_t>(type)))
158184
throw git_exception();
159185
return result;
160186
}

0 commit comments

Comments
 (0)