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