Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libgit2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2435,7 +2435,9 @@ extern "C" {
pub fn git_oid_cmp(a: *const git_oid, b: *const git_oid) -> c_int;
pub fn git_oid_equal(a: *const git_oid, b: *const git_oid) -> c_int;
pub fn git_oid_streq(id: *const git_oid, str: *const c_char) -> c_int;
#[deprecated = "use `git_oid_is_zero`"]
pub fn git_oid_iszero(id: *const git_oid) -> c_int;
pub fn git_oid_is_zero(id: *const git_oid) -> c_int;

// error
pub fn git_error_last() -> *const git_error;
Expand Down
4 changes: 1 addition & 3 deletions src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ impl<'repo> BlobWriter<'repo> {
pub fn commit(mut self) -> Result<Oid, Error> {
// After commit we already doesn't need cleanup on drop
self.need_cleanup = false;
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_blob_create_fromstream_commit(&mut raw, self.raw));
Ok(Binding::from_raw(&raw as *const _))
Expand Down
8 changes: 2 additions & 6 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,9 +723,7 @@ impl TreeUpdateBuilder {
self.paths.push(path);
self.updates.push(raw::git_tree_update {
action: raw::GIT_TREE_UPDATE_REMOVE,
id: raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
},
id: crate::util::zeroed_raw_oid(),
filemode: raw::GIT_FILEMODE_UNREADABLE,
path: path_ptr,
});
Expand Down Expand Up @@ -754,9 +752,7 @@ impl TreeUpdateBuilder {
///
/// The baseline tree must exist in the specified repository.
pub fn create_updated(&mut self, repo: &Repository, baseline: &Tree<'_>) -> Result<Oid, Error> {
let mut ret = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut ret = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_tree_create_updated(
&mut ret,
Expand Down
4 changes: 1 addition & 3 deletions src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,7 @@ impl<'repo> Commit<'repo> {
message: Option<&str>,
tree: Option<&Tree<'repo>>,
) -> Result<Oid, Error> {
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();
let update_ref = crate::opt_cstr(update_ref)?;
let encoding = crate::opt_cstr(message_encoding)?;
let message = crate::opt_cstr(message)?;
Expand Down
4 changes: 1 addition & 3 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,7 @@ impl<'repo> Diff<'repo> {

/// Create a patch ID from a diff.
pub fn patchid(&self, opts: Option<&mut DiffPatchidOptions>) -> Result<Oid, Error> {
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_diff_patchid(
&mut raw,
Expand Down
8 changes: 2 additions & 6 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,7 @@ impl Index {
///
/// The index must not contain any file in conflict.
pub fn write_tree(&mut self) -> Result<Oid, Error> {
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_index_write_tree(&mut raw, self.raw));
Ok(Binding::from_raw(&raw as *const _))
Expand All @@ -630,9 +628,7 @@ impl Index {
/// This is the same as `write_tree` except that the destination repository
/// can be chosen.
pub fn write_tree_to(&mut self, repo: &Repository) -> Result<Oid, Error> {
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_index_write_tree_to(&mut raw, self.raw, repo.raw()));
Ok(Binding::from_raw(&raw as *const _))
Expand Down
4 changes: 1 addition & 3 deletions src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ impl<'repo> Binding for Notes<'repo> {
impl<'repo> Iterator for Notes<'repo> {
type Item = Result<(Oid, Oid), Error>;
fn next(&mut self) -> Option<Result<(Oid, Oid), Error>> {
let mut note_id = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut note_id = crate::util::zeroed_raw_oid();
let mut annotated_id = note_id;
unsafe {
try_call_iter!(raw::git_note_next(
Expand Down
12 changes: 3 additions & 9 deletions src/odb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ impl<'repo> Odb<'repo> {
/// Write an object to the database.
pub fn write(&self, kind: ObjectType, data: &[u8]) -> Result<Oid, Error> {
unsafe {
let mut out = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut out = crate::util::zeroed_raw_oid();
try_call!(raw::git_odb_write(
&mut out,
self.raw,
Expand Down Expand Up @@ -194,9 +192,7 @@ impl<'repo> Odb<'repo> {
/// Potentially finds an object that starts with the given prefix.
pub fn exists_prefix(&self, short_oid: Oid, len: usize) -> Result<Oid, Error> {
unsafe {
let mut out = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut out = crate::util::zeroed_raw_oid();
try_call!(raw::git_odb_exists_prefix(
&mut out,
self.raw,
Expand Down Expand Up @@ -388,9 +384,7 @@ impl<'repo> OdbWriter<'repo> {
/// This method will fail if the total number of received bytes differs from the size declared with odb_writer()
/// Attempting write after finishing will be ignored.
pub fn finalize(&mut self) -> Result<Oid, Error> {
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_odb_stream_finalize_write(&mut raw, self.raw));
Ok(Binding::from_raw(&raw as *const _))
Expand Down
25 changes: 8 additions & 17 deletions src/oid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ impl Oid {
/// characters, or contains any non-hex characters.
pub fn from_str(s: &str) -> Result<Oid, Error> {
crate::init();
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_oid_fromstrn(
&mut raw,
Expand All @@ -42,9 +40,7 @@ impl Oid {
/// If the array given is not 20 bytes in length, an error is returned.
pub fn from_bytes(bytes: &[u8]) -> Result<Oid, Error> {
crate::init();
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();
if bytes.len() != raw::GIT_OID_MAX_SIZE {
Err(Error::from_str("raw byte array must be 20 bytes"))
} else {
Expand All @@ -57,10 +53,9 @@ impl Oid {

/// Creates an all zero Oid structure.
pub fn zero() -> Oid {
let out = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
Oid { raw: out }
Oid {
raw: crate::util::zeroed_raw_oid(),
}
}

/// Hashes the provided data as an object of the provided type, and returns
Expand All @@ -69,9 +64,7 @@ impl Oid {
pub fn hash_object(kind: ObjectType, bytes: &[u8]) -> Result<Oid, Error> {
crate::init();

let mut out = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut out = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_odb_hash(
&mut out,
Expand All @@ -93,9 +86,7 @@ impl Oid {
// Normal file path OK (does not need Windows conversion).
let rpath = path.as_ref().into_c_string()?;

let mut out = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut out = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_odb_hashfile(&mut out, rpath, kind.raw()));
}
Expand All @@ -110,7 +101,7 @@ impl Oid {

/// Test if this OID is all zeros.
pub fn is_zero(&self) -> bool {
unsafe { raw::git_oid_iszero(&self.raw) == 1 }
unsafe { raw::git_oid_is_zero(&self.raw) == 1 }
}
}

Expand Down
56 changes: 14 additions & 42 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,9 +1132,7 @@ impl Repository {
/// The Oid returned can in turn be passed to `find_blob` to get a handle to
/// the blob.
pub fn blob(&self, data: &[u8]) -> Result<Oid, Error> {
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();
unsafe {
let ptr = data.as_ptr() as *const c_void;
let len = data.len() as size_t;
Expand All @@ -1156,9 +1154,7 @@ impl Repository {
pub fn blob_path(&self, path: &Path) -> Result<Oid, Error> {
// Normal file path OK (does not need Windows conversion).
let path = path.into_c_string()?;
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_blob_create_fromdisk(&mut raw, self.raw(), path));
Ok(Binding::from_raw(&raw as *const _))
Expand Down Expand Up @@ -1309,9 +1305,7 @@ impl Repository {
.map(|p| p.raw() as *const raw::git_commit)
.collect::<Vec<_>>();
let message = CString::new(message)?;
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_commit_create(
&mut raw,
Expand Down Expand Up @@ -1383,9 +1377,7 @@ impl Repository {
let commit_content = CString::new(commit_content)?;
let signature = CString::new(signature)?;
let signature_field = crate::opt_cstr(signature_field)?;
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_commit_create_with_signature(
&mut raw,
Expand Down Expand Up @@ -1682,9 +1674,7 @@ impl Repository {
/// allocate or free any `Reference` objects for simple situations.
pub fn refname_to_id(&self, name: &str) -> Result<Oid, Error> {
let name = CString::new(name)?;
let mut ret = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut ret = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_reference_name_to_id(&mut ret, self.raw(), name));
Ok(Binding::from_raw(&ret as *const _))
Expand Down Expand Up @@ -1910,9 +1900,7 @@ impl Repository {
) -> Result<Oid, Error> {
let name = CString::new(name)?;
let message = CString::new(message)?;
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_tag_create(
&mut raw,
Expand Down Expand Up @@ -1943,9 +1931,7 @@ impl Repository {
) -> Result<Oid, Error> {
let name = CString::new(name)?;
let message = CString::new(message)?;
let mut raw_oid = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw_oid = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_tag_annotation_create(
&mut raw_oid,
Expand All @@ -1971,9 +1957,7 @@ impl Repository {
force: bool,
) -> Result<Oid, Error> {
let name = CString::new(name)?;
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_tag_create_lightweight(
&mut raw,
Expand Down Expand Up @@ -2347,9 +2331,7 @@ impl Repository {
) -> Result<Oid, Error> {
let notes_ref = crate::opt_cstr(notes_ref)?;
let note = CString::new(note)?;
let mut ret = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut ret = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_note_create(
&mut ret,
Expand Down Expand Up @@ -2463,9 +2445,7 @@ impl Repository {

/// Find a merge base between two commits
pub fn merge_base(&self, one: Oid, two: Oid) -> Result<Oid, Error> {
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_merge_base(
&mut raw,
Expand Down Expand Up @@ -2511,9 +2491,7 @@ impl Repository {
/// If you're looking to recieve the common merge base between all the
/// given commits, use [`Self::merge_base_octopus`].
pub fn merge_base_many(&self, oids: &[Oid]) -> Result<Oid, Error> {
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();

unsafe {
try_call!(raw::git_merge_base_many(
Expand All @@ -2528,9 +2506,7 @@ impl Repository {

/// Find a common merge base between all given a list of commits
pub fn merge_base_octopus(&self, oids: &[Oid]) -> Result<Oid, Error> {
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();

unsafe {
try_call!(raw::git_merge_base_octopus(
Expand Down Expand Up @@ -2973,9 +2949,7 @@ impl Repository {
flags: Option<StashFlags>,
) -> Result<Oid, Error> {
unsafe {
let mut raw_oid = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw_oid = crate::util::zeroed_raw_oid();
let message = crate::opt_cstr(message)?;
let flags = flags.unwrap_or_else(StashFlags::empty);
try_call!(raw::git_stash_save(
Expand All @@ -2995,9 +2969,7 @@ impl Repository {
opts: Option<&mut StashSaveOptions<'_>>,
) -> Result<Oid, Error> {
unsafe {
let mut raw_oid = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw_oid = crate::util::zeroed_raw_oid();
let opts = opts.map(|opts| opts.raw());
try_call!(raw::git_stash_save_with_opts(
&mut raw_oid,
Expand Down
4 changes: 1 addition & 3 deletions src/revwalk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ impl<'repo> Drop for Revwalk<'repo> {
impl<'repo> Iterator for Revwalk<'repo> {
type Item = Result<Oid, Error>;
fn next(&mut self) -> Option<Result<Oid, Error>> {
let mut out: raw::git_oid = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut out: raw::git_oid = crate::util::zeroed_raw_oid();
unsafe {
try_call_iter!(raw::git_revwalk_next(&mut out, self.raw()));
Some(Ok(Binding::from_raw(&out as *const _)))
Expand Down
4 changes: 1 addition & 3 deletions src/treebuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ impl<'repo> TreeBuilder<'repo> {
/// Write the contents of the TreeBuilder as a Tree object and
/// return its Oid
pub fn write(&self) -> Result<Oid, Error> {
let mut raw = raw::git_oid {
id: [0; raw::GIT_OID_MAX_SIZE],
};
let mut raw = crate::util::zeroed_raw_oid();
unsafe {
try_call!(raw::git_treebuilder_write(&mut raw, self.raw()));
Ok(Binding::from_raw(&raw as *const _))
Expand Down
6 changes: 6 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ fn fixup_windows_path(path: CString) -> Result<CString, Error> {
Ok(path)
}

/// Creates a zeroed git_oid structure.
#[inline]
pub(crate) fn zeroed_raw_oid() -> raw::git_oid {
unsafe { std::mem::zeroed() }
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down