Skip to content

Commit

Permalink
fix: review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeJerred committed Nov 5, 2024
1 parent e889d31 commit 2eb8c84
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 57 deletions.
7 changes: 1 addition & 6 deletions libgit2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1362,19 +1362,14 @@ pub struct git_merge_file_options {
}

#[repr(C)]
#[derive(Copy)]
#[derive(Clone, Copy)]
pub struct git_merge_file_result {
pub automergeable: c_uint,
pub path: *const c_char,
pub mode: c_uint,
pub ptr: *const c_char,
pub len: size_t,
}
impl Clone for git_merge_file_result {
fn clone(&self) -> git_merge_file_result {
*self
}
}

git_enum! {
pub enum git_merge_flag_t {
Expand Down
46 changes: 46 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,52 @@ impl Index {
}
}

impl IndexEntry {
/// Create a raw index entry.
///
/// The returned `raw::git_index_entry` contains a pointer to a `CString` path, which is also
/// returned because it's lifetime must exceed the lifetime of the `raw::git_index_entry`.
pub fn to_raw(&self) -> Result<(raw::git_index_entry, CString), Error> {
let path = CString::new(&self.path[..])?;

// libgit2 encodes the length of the path in the lower bits of the
// `flags` entry, so mask those out and recalculate here to ensure we
// don't corrupt anything.
let mut flags = self.flags & !raw::GIT_INDEX_ENTRY_NAMEMASK;

if self.path.len() < raw::GIT_INDEX_ENTRY_NAMEMASK as usize {
flags |= self.path.len() as u16;
} else {
flags |= raw::GIT_INDEX_ENTRY_NAMEMASK;
}

unsafe {
let raw = raw::git_index_entry {
dev: self.dev,
ino: self.ino,
mode: self.mode,
uid: self.uid,
gid: self.gid,
file_size: self.file_size,
id: *self.id.raw(),
flags,
flags_extended: self.flags_extended,
path: path.as_ptr(),
mtime: raw::git_index_time {
seconds: self.mtime.seconds(),
nanoseconds: self.mtime.nanoseconds(),
},
ctime: raw::git_index_time {
seconds: self.ctime.seconds(),
nanoseconds: self.ctime.nanoseconds(),
},
};

Ok((raw, path))
}
}
}

impl Binding for Index {
type Raw = *mut raw::git_index;
unsafe fn from_raw(raw: *mut raw::git_index) -> Index {
Expand Down
2 changes: 1 addition & 1 deletion src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ impl<'repo> Drop for MergeFileResult<'repo> {
}
}

impl<'repo> std::fmt::Display for MergeFileResult<'repo> {
impl<'repo> std::fmt::Debug for MergeFileResult<'repo> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut ds = f.debug_struct("MergeFileResult");
if let Some(path) = &self.path() {
Expand Down
55 changes: 5 additions & 50 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2516,58 +2516,12 @@ impl Repository {
theirs: &IndexEntry,
opts: Option<&mut MergeFileOptions>,
) -> Result<MergeFileResult<'_>, Error> {
let create_raw_entry = |entry: &IndexEntry| -> Result<raw::git_index_entry, Error> {
let path = CString::new(&entry.path[..])?;

// libgit2 encodes the length of the path in the lower bits of the
// `flags` entry, so mask those out and recalculate here to ensure we
// don't corrupt anything.
let mut flags = entry.flags & !raw::GIT_INDEX_ENTRY_NAMEMASK;

if entry.path.len() < raw::GIT_INDEX_ENTRY_NAMEMASK as usize {
flags |= entry.path.len() as u16;
} else {
flags |= raw::GIT_INDEX_ENTRY_NAMEMASK;
}

unsafe {
let raw = raw::git_index_entry {
dev: entry.dev,
ino: entry.ino,
mode: entry.mode,
uid: entry.uid,
gid: entry.gid,
file_size: entry.file_size,
id: *entry.id.raw(),
flags,
flags_extended: entry.flags_extended,
path: path.as_ptr(),
mtime: raw::git_index_time {
seconds: entry.mtime.seconds(),
nanoseconds: entry.mtime.nanoseconds(),
},
ctime: raw::git_index_time {
seconds: entry.ctime.seconds(),
nanoseconds: entry.ctime.nanoseconds(),
},
};

Ok(raw)
}
};

let mut ret = raw::git_merge_file_result {
automergeable: 0,
path: ptr::null_mut(),
mode: 0,
ptr: ptr::null_mut(),
len: 0,
};
let ancestor = create_raw_entry(ancestor)?;
let ours = create_raw_entry(ours)?;
let theirs = create_raw_entry(theirs)?;
let (ancestor, _ancestor_path) = ancestor.to_raw()?;
let (ours, _ours_path) = ours.to_raw()?;
let (theirs, _theirs_path) = theirs.to_raw()?;

unsafe {
let mut ret = mem::zeroed();
try_call!(raw::git_merge_file_from_index(
&mut ret,
self.raw(),
Expand Down Expand Up @@ -4104,6 +4058,7 @@ mod tests {
.unwrap();

assert!(!merge_file_result.is_automergeable());
assert_eq!(merge_file_result.path(), Some("file"));
assert_eq!(
String::from_utf8_lossy(merge_file_result.content()).to_string(),
r"<<<<<<< ours
Expand Down

0 comments on commit 2eb8c84

Please sign in to comment.