| 1 |
use std::io; |
| 2 |
use std::marker; |
| 3 |
use std::mem; |
| 4 |
use std::slice; |
| 5 |
|
| 6 |
use crate::util::Binding; |
| 7 |
use crate::{raw, Error, Object, Oid}; |
| 8 |
|
| 9 |
/// A structure to represent a git [blob][1] |
| 10 |
/// |
| 11 |
/// [1]: http://git-scm.com/book/en/Git-Internals-Git-Objects |
| 12 |
pub struct Blob<'repo> { |
| 13 |
raw: *mut raw::git_blob, |
| 14 |
_marker: marker::PhantomData<Object<'repo>>, |
| 15 |
} |
| 16 |
|
| 17 |
impl<'repo> Blob<'repo> { |
| 18 |
/// Get the id (SHA1) of a repository blob |
| 19 |
pub fn id(&self) -> Oid { |
| 20 |
unsafe { Binding::from_raw(raw::git_blob_id(&*self.raw)) } |
| 21 |
} |
| 22 |
|
| 23 |
/// Determine if the blob content is most certainly binary or not. |
| 24 |
pub fn is_binary(&self) -> bool { |
| 25 |
unsafe { raw::git_blob_is_binary(&*self.raw) == 1 } |
| 26 |
} |
| 27 |
|
| 28 |
/// Get the content of this blob. |
| 29 |
pub fn content(&self) -> &[u8] { |
| 30 |
unsafe { |
| 31 |
let data = raw::git_blob_rawcontent(&*self.raw) as *const u8; |
| 32 |
let len = raw::git_blob_rawsize(&*self.raw) as usize; |
| 33 |
slice::from_raw_parts(data, len) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/// Get the size in bytes of the contents of this blob. |
| 38 |
pub fn size(&self) -> usize { |
| 39 |
unsafe { raw::git_blob_rawsize(&*self.raw) as usize } |
| 40 |
} |
| 41 |
|
| 42 |
/// Casts this Blob to be usable as an `Object` |
| 43 |
pub fn as_object(&self) -> &Object<'repo> { |
| 44 |
unsafe { &*(self as *const _ as *const Object<'repo>) } |
| 45 |
} |
| 46 |
|
| 47 |
/// Consumes Blob to be returned as an `Object` |
| 48 |
pub fn into_object(self) -> Object<'repo> { |
| 49 |
assert_eq!(mem::size_of_val(&self), mem::size_of::<Object<'_>>()); |
| 50 |
unsafe { mem::transmute(self) } |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
impl<'repo> Binding for Blob<'repo> { |
| 55 |
type Raw = *mut raw::git_blob; |
| 56 |
|
| 57 |
unsafe fn from_raw(raw: *mut raw::git_blob) -> Blob<'repo> { |
| 58 |
Blob { |
| 59 |
raw, |
| 60 |
_marker: marker::PhantomData, |
| 61 |
} |
| 62 |
} |
| 63 |
fn raw(&self) -> *mut raw::git_blob { |
| 64 |
self.raw |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
impl<'repo> std::fmt::Debug for Blob<'repo> { |
| 69 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { |
| 70 |
f.debug_struct("Blob").field("id", &self.id()).finish() |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
impl<'repo> Clone for Blob<'repo> { |
| 75 |
fn clone(&self) -> Self { |
| 76 |
self.as_object().clone().into_blob().ok().unwrap() |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
impl<'repo> Drop for Blob<'repo> { |
| 81 |
fn drop(&mut self) { |
| 82 |
unsafe { raw::git_blob_free(self.raw) } |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/// A structure to represent a git writestream for blobs |
| 87 |
pub struct BlobWriter<'repo> { |
| 88 |
raw: *mut raw::git_writestream, |
| 89 |
need_cleanup: bool, |
| 90 |
_marker: marker::PhantomData<Object<'repo>>, |
| 91 |
} |
| 92 |
|
| 93 |
impl<'repo> BlobWriter<'repo> { |
| 94 |
/// Finalize blob writing stream and write the blob to the object db |
| 95 |
pub fn commit(mut self) -> Result<Oid, Error> { |
| 96 |
// After commit we already doesn't need cleanup on drop |
| 97 |
self.need_cleanup = false; |
| 98 |
let mut raw = crate::util::zeroed_raw_oid(); |
| 99 |
unsafe { |
| 100 |
try_call!(raw::git_blob_create_fromstream_commit(&mut raw, self.raw)); |
| 101 |
Ok(Binding::from_raw(&raw as *const _)) |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
impl<'repo> Binding for BlobWriter<'repo> { |
| 107 |
type Raw = *mut raw::git_writestream; |
| 108 |
|
| 109 |
unsafe fn from_raw(raw: *mut raw::git_writestream) -> BlobWriter<'repo> { |
| 110 |
BlobWriter { |
| 111 |
raw, |
| 112 |
need_cleanup: true, |
| 113 |
_marker: marker::PhantomData, |
| 114 |
} |
| 115 |
} |
| 116 |
fn raw(&self) -> *mut raw::git_writestream { |
| 117 |
self.raw |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
impl<'repo> Drop for BlobWriter<'repo> { |
| 122 |
fn drop(&mut self) { |
| 123 |
// We need cleanup in case the stream has not been committed |
| 124 |
if self.need_cleanup { |
| 125 |
unsafe { |
| 126 |
if let Some(f) = (*self.raw).free { |
| 127 |
f(self.raw) |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
impl<'repo> io::Write for BlobWriter<'repo> { |
| 135 |
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 136 |
unsafe { |
| 137 |
if let Some(f) = (*self.raw).write { |
| 138 |
let res = f(self.raw, buf.as_ptr() as *const _, buf.len()); |
| 139 |
if res < 0 { |
| 140 |
Err(io::Error::new(io::ErrorKind::Other, "Write error")) |
| 141 |
} else { |
| 142 |
Ok(buf.len()) |
| 143 |
} |
| 144 |
} else { |
| 145 |
Err(io::Error::new(io::ErrorKind::Other, "no write callback")) |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
fn flush(&mut self) -> io::Result<()> { |
| 150 |
Ok(()) |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
#[cfg(test)] |
| 155 |
mod tests { |
| 156 |
use crate::Repository; |
| 157 |
use std::fs::File; |
| 158 |
use std::io::prelude::*; |
| 159 |
use std::path::Path; |
| 160 |
use tempfile::TempDir; |
| 161 |
|
| 162 |
#[test] |
| 163 |
fn buffer() { |
| 164 |
let td = TempDir::new().unwrap(); |
| 165 |
let repo = Repository::init(td.path()).unwrap(); |
| 166 |
let id = repo.blob(&[5, 4, 6]).unwrap(); |
| 167 |
let blob = repo.find_blob(id).unwrap(); |
| 168 |
|
| 169 |
assert_eq!(blob.id(), id); |
| 170 |
assert_eq!(blob.size(), 3); |
| 171 |
assert_eq!(blob.content(), [5, 4, 6]); |
| 172 |
assert!(blob.is_binary()); |
| 173 |
|
| 174 |
repo.find_object(id, None).unwrap().as_blob().unwrap(); |
| 175 |
repo.find_object(id, None) |
| 176 |
.unwrap() |
| 177 |
.into_blob() |
| 178 |
.ok() |
| 179 |
.unwrap(); |
| 180 |
} |
| 181 |
|
| 182 |
#[test] |
| 183 |
fn path() { |
| 184 |
let td = TempDir::new().unwrap(); |
| 185 |
let path = td.path().join("foo"); |
| 186 |
File::create(&path).unwrap().write_all(&[7, 8, 9]).unwrap(); |
| 187 |
let repo = Repository::init(td.path()).unwrap(); |
| 188 |
let id = repo.blob_path(&path).unwrap(); |
| 189 |
let blob = repo.find_blob(id).unwrap(); |
| 190 |
assert_eq!(blob.content(), [7, 8, 9]); |
| 191 |
blob.into_object(); |
| 192 |
} |
| 193 |
|
| 194 |
#[test] |
| 195 |
fn stream() { |
| 196 |
let td = TempDir::new().unwrap(); |
| 197 |
let repo = Repository::init(td.path()).unwrap(); |
| 198 |
let mut ws = repo.blob_writer(Some(Path::new("foo"))).unwrap(); |
| 199 |
let wl = ws.write(&[10, 11, 12]).unwrap(); |
| 200 |
assert_eq!(wl, 3); |
| 201 |
let id = ws.commit().unwrap(); |
| 202 |
let blob = repo.find_blob(id).unwrap(); |
| 203 |
assert_eq!(blob.content(), [10, 11, 12]); |
| 204 |
blob.into_object(); |
| 205 |
} |
| 206 |
} |