Skip to content

Commit f862341

Browse files
committed
Add bindings to git_indexer
The indexer API is a lower-level interface for storing and indexing pack files, which, unlike `git_odb_write_pack`, allows the ouput to be written to an arbitrary directory. This can be useful when working with unusual validation requirements or non-standard repository layouts.
1 parent 3aa9013 commit f862341

File tree

4 files changed

+202
-6
lines changed

4 files changed

+202
-6
lines changed

Diff for: libgit2-sys/lib.rs

+32
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub const GIT_REFDB_BACKEND_VERSION: c_uint = 1;
2525
pub const GIT_CHERRYPICK_OPTIONS_VERSION: c_uint = 1;
2626
pub const GIT_APPLY_OPTIONS_VERSION: c_uint = 1;
2727
pub const GIT_REVERT_OPTIONS_VERSION: c_uint = 1;
28+
pub const GIT_INDEXER_OPTIONS_VERSION: c_uint = 1;
2829

2930
macro_rules! git_enum {
3031
(pub enum $name:ident { $($variants:tt)* }) => {
@@ -91,6 +92,7 @@ pub enum git_odb_object {}
9192
pub enum git_worktree {}
9293
pub enum git_transaction {}
9394
pub enum git_mailmap {}
95+
pub enum git_indexer {}
9496

9597
#[repr(C)]
9698
pub struct git_revspec {
@@ -354,6 +356,14 @@ pub type git_indexer_progress_cb =
354356
)]
355357
pub type git_transfer_progress = git_indexer_progress;
356358

359+
#[repr(C)]
360+
pub struct git_indexer_options {
361+
pub version: c_uint,
362+
pub progress_cb: git_indexer_progress_cb,
363+
pub progress_cb_payload: *mut c_void,
364+
pub verify: c_uchar,
365+
}
366+
357367
pub type git_remote_ready_cb = Option<extern "C" fn(*mut git_remote, c_int, *mut c_void) -> c_int>;
358368

359369
#[repr(C)]
@@ -3801,6 +3811,28 @@ extern "C" {
38013811
) -> c_int;
38023812
pub fn git_packbuilder_free(pb: *mut git_packbuilder);
38033813

3814+
// indexer
3815+
pub fn git_indexer_new(
3816+
out: *mut *mut git_indexer,
3817+
path: *const c_char,
3818+
mode: c_uint,
3819+
odb: *mut git_odb,
3820+
opts: *mut git_indexer_options,
3821+
) -> c_int;
3822+
pub fn git_indexer_append(
3823+
idx: *mut git_indexer,
3824+
data: *const c_void,
3825+
size: size_t,
3826+
stats: *mut git_indexer_progress,
3827+
) -> c_int;
3828+
pub fn git_indexer_commit(idx: *mut git_indexer, stats: *mut git_indexer_progress) -> c_int;
3829+
#[deprecated = "use `git_indexer_name` to retrieve the filename"]
3830+
pub fn git_indexer_hash(idx: *const git_indexer) -> *const git_oid;
3831+
pub fn git_indexer_name(idx: *const git_indexer) -> *const c_char;
3832+
pub fn git_indexer_free(idx: *mut git_indexer);
3833+
3834+
pub fn git_indexer_options_init(opts: *mut git_indexer_options, version: c_uint) -> c_int;
3835+
38043836
// odb
38053837
pub fn git_repository_odb(out: *mut *mut git_odb, repo: *mut git_repository) -> c_int;
38063838
pub fn git_odb_new(db: *mut *mut git_odb) -> c_int;

Diff for: src/indexer.rs

+164-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
use std::marker;
1+
use std::ffi::CStr;
2+
use std::mem::MaybeUninit;
3+
use std::path::Path;
4+
use std::{io, marker, mem, ptr};
25

3-
use crate::raw;
6+
use libc::c_void;
7+
8+
use crate::odb::{write_pack_progress_cb, OdbPackwriterCb};
49
use crate::util::Binding;
10+
use crate::{raw, Error, IntoCString, Odb};
511

612
/// Struct representing the progress by an in-flight transfer.
713
pub struct Progress<'a> {
@@ -94,3 +100,159 @@ impl<'a> Binding for Progress<'a> {
94100
)]
95101
#[allow(dead_code)]
96102
pub type TransportProgress<'a> = IndexerProgress<'a>;
103+
104+
/// A stream to write and index a packfile
105+
///
106+
/// This is equivalent to [`crate::OdbPackwriter`], but allows to store the pack
107+
/// and index at an arbitrary path. It also does not require access to an object
108+
/// database if, and only if, the pack file is self-contained (i.e. not "thin").
109+
pub struct Indexer<'odb> {
110+
raw: *mut raw::git_indexer,
111+
progress: MaybeUninit<raw::git_indexer_progress>,
112+
progress_payload_ptr: *mut OdbPackwriterCb<'odb>,
113+
}
114+
115+
impl<'a> Indexer<'a> {
116+
/// Create a new indexer
117+
///
118+
/// The [`Odb`] is used to resolve base objects when fixing thin packs. It
119+
/// can be `None` if no thin pack is expected, in which case missing bases
120+
/// will result in an error.
121+
///
122+
/// `mode` is the permissions to use for the output files, use `0` for defaults.
123+
///
124+
/// If `verify` is `false`, the indexer will bypass object connectivity checks.
125+
pub fn new(odb: Option<&Odb<'a>>, path: &Path, mode: u32, verify: bool) -> Result<Self, Error> {
126+
let path = path.into_c_string()?;
127+
128+
let odb = odb.map(Binding::raw).unwrap_or_else(ptr::null_mut);
129+
130+
let mut out = ptr::null_mut();
131+
let progress = MaybeUninit::uninit();
132+
let progress_cb: raw::git_indexer_progress_cb = Some(write_pack_progress_cb);
133+
let progress_payload = Box::new(OdbPackwriterCb { cb: None });
134+
let progress_payload_ptr = Box::into_raw(progress_payload);
135+
136+
unsafe {
137+
let mut opts = mem::zeroed();
138+
try_call!(raw::git_indexer_options_init(
139+
&mut opts,
140+
raw::GIT_INDEXER_OPTIONS_VERSION
141+
));
142+
opts.progress_cb = progress_cb;
143+
opts.progress_cb_payload = progress_payload_ptr as *mut c_void;
144+
opts.verify = verify.into();
145+
146+
try_call!(raw::git_indexer_new(&mut out, path, mode, odb, &mut opts));
147+
}
148+
149+
Ok(Self {
150+
raw: out,
151+
progress,
152+
progress_payload_ptr,
153+
})
154+
}
155+
156+
/// Finalize the pack and index
157+
///
158+
/// Resolves any pending deltas and writes out the index file. The returned
159+
/// string is the hexadecimal checksum of the packfile, which is also used
160+
/// to name the pack and index files (`pack-<checksum>.pack` and
161+
/// `pack-<checksum>.idx` respectively).
162+
pub fn commit(mut self) -> Result<String, Error> {
163+
unsafe {
164+
try_call!(raw::git_indexer_commit(
165+
self.raw,
166+
self.progress.as_mut_ptr()
167+
));
168+
169+
let name = CStr::from_ptr(raw::git_indexer_name(self.raw));
170+
Ok(name.to_str().expect("pack name not utf8").to_owned())
171+
}
172+
}
173+
174+
/// The callback through which progress is monitored. Be aware that this is
175+
/// called inline, so performance may be affected.
176+
pub fn progress<F>(&mut self, cb: F) -> &mut Self
177+
where
178+
F: FnMut(Progress<'_>) -> bool + 'a,
179+
{
180+
let progress_payload =
181+
unsafe { &mut *(self.progress_payload_ptr as *mut OdbPackwriterCb<'_>) };
182+
progress_payload.cb = Some(Box::new(cb) as Box<IndexerProgress<'a>>);
183+
184+
self
185+
}
186+
}
187+
188+
impl io::Write for Indexer<'_> {
189+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
190+
unsafe {
191+
let ptr = buf.as_ptr() as *mut c_void;
192+
let len = buf.len();
193+
194+
let res = raw::git_indexer_append(self.raw, ptr, len, self.progress.as_mut_ptr());
195+
196+
if res < 0 {
197+
Err(io::Error::new(io::ErrorKind::Other, "Write error"))
198+
} else {
199+
Ok(buf.len())
200+
}
201+
}
202+
}
203+
204+
fn flush(&mut self) -> io::Result<()> {
205+
Ok(())
206+
}
207+
}
208+
209+
impl Drop for Indexer<'_> {
210+
fn drop(&mut self) {
211+
unsafe {
212+
raw::git_indexer_free(self.raw);
213+
drop(Box::from_raw(self.progress_payload_ptr))
214+
}
215+
}
216+
}
217+
218+
#[cfg(test)]
219+
mod tests {
220+
use crate::{Buf, Indexer};
221+
use std::io::prelude::*;
222+
223+
#[test]
224+
fn indexer() {
225+
let (_td, repo_source) = crate::test::repo_init();
226+
let (_td, repo_target) = crate::test::repo_init();
227+
228+
let mut progress_called = false;
229+
230+
// Create an in-memory packfile
231+
let mut builder = t!(repo_source.packbuilder());
232+
let mut buf = Buf::new();
233+
let (commit_source_id, _tree) = crate::test::commit(&repo_source);
234+
t!(builder.insert_object(commit_source_id, None));
235+
t!(builder.write_buf(&mut buf));
236+
237+
// Write it to the standard location in the target repo, but via indexer
238+
let odb = repo_source.odb().unwrap();
239+
let mut indexer = Indexer::new(
240+
Some(&odb),
241+
repo_target.path().join("objects").join("pack").as_path(),
242+
0o644,
243+
true,
244+
)
245+
.unwrap();
246+
indexer.progress(|_| {
247+
progress_called = true;
248+
true
249+
});
250+
indexer.write(&buf).unwrap();
251+
indexer.commit().unwrap();
252+
253+
// Assert that target repo picks it up as valid
254+
let commit_target = repo_target.find_commit(commit_source_id).unwrap();
255+
assert_eq!(commit_target.id(), commit_source_id);
256+
assert!(progress_called);
257+
}
258+
}

Diff for: src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub use crate::error::Error;
9898
pub use crate::index::{
9999
Index, IndexConflict, IndexConflicts, IndexEntries, IndexEntry, IndexMatchedPath,
100100
};
101-
pub use crate::indexer::{IndexerProgress, Progress};
101+
pub use crate::indexer::{Indexer, IndexerProgress, Progress};
102102
pub use crate::mailmap::Mailmap;
103103
pub use crate::mempack::Mempack;
104104
pub use crate::merge::{AnnotatedCommit, MergeOptions};

Diff for: src/odb.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::io;
22
use std::marker;
33
use std::mem::MaybeUninit;
4+
45
use std::ptr;
56
use std::slice;
67

@@ -10,6 +11,7 @@ use libc::{c_char, c_int, c_uint, c_void, size_t};
1011

1112
use crate::panic;
1213
use crate::util::Binding;
14+
1315
use crate::{
1416
raw, Error, IndexerProgress, Mempack, Object, ObjectType, OdbLookupFlags, Oid, Progress,
1517
};
@@ -438,8 +440,8 @@ impl<'repo> io::Write for OdbWriter<'repo> {
438440
}
439441
}
440442

441-
struct OdbPackwriterCb<'repo> {
442-
cb: Option<Box<IndexerProgress<'repo>>>,
443+
pub(crate) struct OdbPackwriterCb<'repo> {
444+
pub(crate) cb: Option<Box<IndexerProgress<'repo>>>,
443445
}
444446

445447
/// A stream to write a packfile to the ODB
@@ -542,7 +544,7 @@ extern "C" fn foreach_cb(id: *const raw::git_oid, payload: *mut c_void) -> c_int
542544
.unwrap_or(1)
543545
}
544546

545-
extern "C" fn write_pack_progress_cb(
547+
pub(crate) extern "C" fn write_pack_progress_cb(
546548
stats: *const raw::git_indexer_progress,
547549
payload: *mut c_void,
548550
) -> c_int {

0 commit comments

Comments
 (0)