diff --git a/libgit2-sys/lib.rs b/libgit2-sys/lib.rs index ea66159f0c..2ccee1d893 100644 --- a/libgit2-sys/lib.rs +++ b/libgit2-sys/lib.rs @@ -25,6 +25,7 @@ pub const GIT_REFDB_BACKEND_VERSION: c_uint = 1; pub const GIT_CHERRYPICK_OPTIONS_VERSION: c_uint = 1; pub const GIT_APPLY_OPTIONS_VERSION: c_uint = 1; pub const GIT_REVERT_OPTIONS_VERSION: c_uint = 1; +pub const GIT_INDEXER_OPTIONS_VERSION: c_uint = 1; macro_rules! git_enum { (pub enum $name:ident { $($variants:tt)* }) => { @@ -91,6 +92,7 @@ pub enum git_odb_object {} pub enum git_worktree {} pub enum git_transaction {} pub enum git_mailmap {} +pub enum git_indexer {} #[repr(C)] pub struct git_revspec { @@ -354,6 +356,14 @@ pub type git_indexer_progress_cb = )] pub type git_transfer_progress = git_indexer_progress; +#[repr(C)] +pub struct git_indexer_options { + pub version: c_uint, + pub progress_cb: git_indexer_progress_cb, + pub progress_cb_payload: *mut c_void, + pub verify: bool, +} + pub type git_remote_ready_cb = Option c_int>; #[repr(C)] @@ -3801,6 +3811,27 @@ extern "C" { ) -> c_int; pub fn git_packbuilder_free(pb: *mut git_packbuilder); + // indexer + pub fn git_indexer_new( + out: *mut *mut git_indexer, + path: *const c_char, + mode: c_uint, + odb: *mut git_odb, + opts: *mut git_indexer_options, + ) -> c_int; + pub fn git_indexer_append( + idx: *mut git_indexer, + data: *mut c_void, + size: size_t, + stats: *mut git_indexer_progress, + ) -> c_int; + pub fn git_indexer_commit(idx: *mut git_indexer, stats: *mut git_indexer_progress) -> c_int; + pub fn git_indexer_hash(idx: *mut git_indexer) -> *const git_oid; + pub fn git_indexer_name(idx: *mut git_indexer) -> *const c_char; + pub fn git_indexer_free(idx: *mut git_indexer); + + pub fn git_indexer_options_init(opts: *mut git_indexer_options, version: c_uint) -> c_int; + // odb pub fn git_repository_odb(out: *mut *mut git_odb, repo: *mut git_repository) -> c_int; pub fn git_odb_new(db: *mut *mut git_odb) -> c_int; diff --git a/src/lib.rs b/src/lib.rs index 40be0c4b5b..ec90b2886f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -109,7 +109,7 @@ pub use crate::message::{ }; pub use crate::note::{Note, Notes}; pub use crate::object::Object; -pub use crate::odb::{Odb, OdbObject, OdbPackwriter, OdbReader, OdbWriter}; +pub use crate::odb::{Indexer, Odb, OdbObject, OdbPackwriter, OdbReader, OdbWriter}; pub use crate::oid::Oid; pub use crate::packbuilder::{PackBuilder, PackBuilderStage}; pub use crate::patch::Patch; diff --git a/src/odb.rs b/src/odb.rs index 66166913fe..885040fc72 100644 --- a/src/odb.rs +++ b/src/odb.rs @@ -1,6 +1,9 @@ +use std::ffi::CStr; use std::io; use std::marker; +use std::mem; use std::mem::MaybeUninit; +use std::path::Path; use std::ptr; use std::slice; @@ -10,6 +13,7 @@ use libc::{c_char, c_int, c_uint, c_void, size_t}; use crate::panic; use crate::util::Binding; +use crate::IntoCString; use crate::{ raw, Error, IndexerProgress, Mempack, Object, ObjectType, OdbLookupFlags, Oid, Progress, }; @@ -183,6 +187,41 @@ impl<'repo> Odb<'repo> { }) } + /// Create a stream for writing a pack file to an arbitrary path + /// + /// This [`Odb`] is used to resolve objects if the written pack is "thin", i.e. depends on + /// already-known objects. + /// + /// `mode` are the file permissions to use on the output. + pub fn indexer(&self, path: &Path, mode: u32) -> Result, Error> { + let path = path.into_c_string()?; + let mut out = ptr::null_mut(); + let progress = MaybeUninit::uninit(); + let progress_cb: raw::git_indexer_progress_cb = Some(write_pack_progress_cb); + let progress_payload = Box::new(OdbPackwriterCb { cb: None }); + let progress_payload_ptr = Box::into_raw(progress_payload); + + unsafe { + let mut opts = mem::zeroed(); + try_call!(raw::git_indexer_options_init( + &mut opts, + raw::GIT_INDEXER_OPTIONS_VERSION + )); + opts.progress_cb = progress_cb; + opts.progress_cb_payload = progress_payload_ptr as *mut c_void; + + try_call!(raw::git_indexer_new( + &mut out, path, mode, self.raw, &mut opts + )); + } + + Ok(Indexer { + raw: out, + progress, + progress_payload_ptr, + }) + } + /// Checks if the object database has an object. pub fn exists(&self, oid: Oid) -> bool { unsafe { raw::git_odb_exists(self.raw, oid.raw()) != 0 } @@ -519,6 +558,78 @@ impl<'repo> Drop for OdbPackwriter<'repo> { } } +/// A stream to write and index a packfile +/// +/// This is a lower-level interface than [`OdbPackwriter`] which allows to write the pack data and +/// index to an arbitrary path, but is otherwise equivalent. +pub struct Indexer<'odb> { + raw: *mut raw::git_indexer, + progress: MaybeUninit, + progress_payload_ptr: *mut OdbPackwriterCb<'odb>, +} + +impl<'a> Indexer<'a> { + /// Finalize the pack and index + /// + /// Resolves any pending deltas and writes out the index file. The returned string is the + /// hexadecimal checksum of the packfile, which is also used to name the pack and index files + /// (`pack-.pack` and `pack-.idx` respectively). + pub fn commit(mut self) -> Result { + unsafe { + try_call!(raw::git_indexer_commit( + self.raw, + self.progress.as_mut_ptr() + )); + + let name = CStr::from_ptr(raw::git_indexer_name(self.raw)); + Ok(name.to_str().expect("pack name not utf8").to_owned()) + } + } + + /// The callback through which progress is monitored. Be aware that this is + /// called inline, so performance may be affected. + pub fn progress(&mut self, cb: F) -> &mut Self + where + F: FnMut(Progress<'_>) -> bool + 'a, + { + let progress_payload = + unsafe { &mut *(self.progress_payload_ptr as *mut OdbPackwriterCb<'_>) }; + progress_payload.cb = Some(Box::new(cb) as Box>); + + self + } +} + +impl io::Write for Indexer<'_> { + fn write(&mut self, buf: &[u8]) -> io::Result { + unsafe { + let ptr = buf.as_ptr() as *mut c_void; + let len = buf.len(); + + let res = raw::git_indexer_append(self.raw, ptr, len, self.progress.as_mut_ptr()); + + if res < 0 { + Err(io::Error::new(io::ErrorKind::Other, "Write error")) + } else { + Ok(buf.len()) + } + } + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +impl Drop for Indexer<'_> { + fn drop(&mut self) { + unsafe { + raw::git_indexer_free(self.raw); + drop(Box::from_raw(self.progress_payload_ptr)) + } + } +} + pub type ForeachCb<'a> = dyn FnMut(&Oid) -> bool + 'a; struct ForeachCbData<'a> { @@ -728,4 +839,39 @@ mod tests { t!(repo.reset(commit1.as_object(), ResetType::Hard, None)); assert!(foo_file.exists()); } + + #[test] + fn indexer() { + let (_td, repo_source) = crate::test::repo_init(); + let (_td, repo_target) = crate::test::repo_init(); + + let mut progress_called = false; + + // Create an in-memory packfile + let mut builder = t!(repo_source.packbuilder()); + let mut buf = Buf::new(); + let (commit_source_id, _tree) = crate::test::commit(&repo_source); + t!(builder.insert_object(commit_source_id, None)); + t!(builder.write_buf(&mut buf)); + + // Write it to the standard location in the target repo, but via indexer + let odb = repo_source.odb().unwrap(); + let mut indexer = odb + .indexer( + repo_target.path().join("objects").join("pack").as_path(), + 0o644, + ) + .unwrap(); + indexer.progress(|_| { + progress_called = true; + true + }); + indexer.write(&buf).unwrap(); + indexer.commit().unwrap(); + + // Assert that target repo picks it up as valid + let commit_target = repo_target.find_commit(commit_source_id).unwrap(); + assert_eq!(commit_target.id(), commit_source_id); + assert!(progress_called); + } }