Skip to content

Commit 2eb8c84

Browse files
committed
fix: review comments
1 parent e889d31 commit 2eb8c84

File tree

4 files changed

+53
-57
lines changed

4 files changed

+53
-57
lines changed

libgit2-sys/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,19 +1362,14 @@ pub struct git_merge_file_options {
13621362
}
13631363

13641364
#[repr(C)]
1365-
#[derive(Copy)]
1365+
#[derive(Clone, Copy)]
13661366
pub struct git_merge_file_result {
13671367
pub automergeable: c_uint,
13681368
pub path: *const c_char,
13691369
pub mode: c_uint,
13701370
pub ptr: *const c_char,
13711371
pub len: size_t,
13721372
}
1373-
impl Clone for git_merge_file_result {
1374-
fn clone(&self) -> git_merge_file_result {
1375-
*self
1376-
}
1377-
}
13781373

13791374
git_enum! {
13801375
pub enum git_merge_flag_t {

src/index.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,52 @@ impl Index {
614614
}
615615
}
616616

617+
impl IndexEntry {
618+
/// Create a raw index entry.
619+
///
620+
/// The returned `raw::git_index_entry` contains a pointer to a `CString` path, which is also
621+
/// returned because it's lifetime must exceed the lifetime of the `raw::git_index_entry`.
622+
pub fn to_raw(&self) -> Result<(raw::git_index_entry, CString), Error> {
623+
let path = CString::new(&self.path[..])?;
624+
625+
// libgit2 encodes the length of the path in the lower bits of the
626+
// `flags` entry, so mask those out and recalculate here to ensure we
627+
// don't corrupt anything.
628+
let mut flags = self.flags & !raw::GIT_INDEX_ENTRY_NAMEMASK;
629+
630+
if self.path.len() < raw::GIT_INDEX_ENTRY_NAMEMASK as usize {
631+
flags |= self.path.len() as u16;
632+
} else {
633+
flags |= raw::GIT_INDEX_ENTRY_NAMEMASK;
634+
}
635+
636+
unsafe {
637+
let raw = raw::git_index_entry {
638+
dev: self.dev,
639+
ino: self.ino,
640+
mode: self.mode,
641+
uid: self.uid,
642+
gid: self.gid,
643+
file_size: self.file_size,
644+
id: *self.id.raw(),
645+
flags,
646+
flags_extended: self.flags_extended,
647+
path: path.as_ptr(),
648+
mtime: raw::git_index_time {
649+
seconds: self.mtime.seconds(),
650+
nanoseconds: self.mtime.nanoseconds(),
651+
},
652+
ctime: raw::git_index_time {
653+
seconds: self.ctime.seconds(),
654+
nanoseconds: self.ctime.nanoseconds(),
655+
},
656+
};
657+
658+
Ok((raw, path))
659+
}
660+
}
661+
}
662+
617663
impl Binding for Index {
618664
type Raw = *mut raw::git_index;
619665
unsafe fn from_raw(raw: *mut raw::git_index) -> Index {

src/merge.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ impl<'repo> Drop for MergeFileResult<'repo> {
402402
}
403403
}
404404

405-
impl<'repo> std::fmt::Display for MergeFileResult<'repo> {
405+
impl<'repo> std::fmt::Debug for MergeFileResult<'repo> {
406406
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
407407
let mut ds = f.debug_struct("MergeFileResult");
408408
if let Some(path) = &self.path() {

src/repo.rs

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2516,58 +2516,12 @@ impl Repository {
25162516
theirs: &IndexEntry,
25172517
opts: Option<&mut MergeFileOptions>,
25182518
) -> Result<MergeFileResult<'_>, Error> {
2519-
let create_raw_entry = |entry: &IndexEntry| -> Result<raw::git_index_entry, Error> {
2520-
let path = CString::new(&entry.path[..])?;
2521-
2522-
// libgit2 encodes the length of the path in the lower bits of the
2523-
// `flags` entry, so mask those out and recalculate here to ensure we
2524-
// don't corrupt anything.
2525-
let mut flags = entry.flags & !raw::GIT_INDEX_ENTRY_NAMEMASK;
2526-
2527-
if entry.path.len() < raw::GIT_INDEX_ENTRY_NAMEMASK as usize {
2528-
flags |= entry.path.len() as u16;
2529-
} else {
2530-
flags |= raw::GIT_INDEX_ENTRY_NAMEMASK;
2531-
}
2532-
2533-
unsafe {
2534-
let raw = raw::git_index_entry {
2535-
dev: entry.dev,
2536-
ino: entry.ino,
2537-
mode: entry.mode,
2538-
uid: entry.uid,
2539-
gid: entry.gid,
2540-
file_size: entry.file_size,
2541-
id: *entry.id.raw(),
2542-
flags,
2543-
flags_extended: entry.flags_extended,
2544-
path: path.as_ptr(),
2545-
mtime: raw::git_index_time {
2546-
seconds: entry.mtime.seconds(),
2547-
nanoseconds: entry.mtime.nanoseconds(),
2548-
},
2549-
ctime: raw::git_index_time {
2550-
seconds: entry.ctime.seconds(),
2551-
nanoseconds: entry.ctime.nanoseconds(),
2552-
},
2553-
};
2554-
2555-
Ok(raw)
2556-
}
2557-
};
2558-
2559-
let mut ret = raw::git_merge_file_result {
2560-
automergeable: 0,
2561-
path: ptr::null_mut(),
2562-
mode: 0,
2563-
ptr: ptr::null_mut(),
2564-
len: 0,
2565-
};
2566-
let ancestor = create_raw_entry(ancestor)?;
2567-
let ours = create_raw_entry(ours)?;
2568-
let theirs = create_raw_entry(theirs)?;
2519+
let (ancestor, _ancestor_path) = ancestor.to_raw()?;
2520+
let (ours, _ours_path) = ours.to_raw()?;
2521+
let (theirs, _theirs_path) = theirs.to_raw()?;
25692522

25702523
unsafe {
2524+
let mut ret = mem::zeroed();
25712525
try_call!(raw::git_merge_file_from_index(
25722526
&mut ret,
25732527
self.raw(),
@@ -4104,6 +4058,7 @@ mod tests {
41044058
.unwrap();
41054059

41064060
assert!(!merge_file_result.is_automergeable());
4061+
assert_eq!(merge_file_result.path(), Some("file"));
41074062
assert_eq!(
41084063
String::from_utf8_lossy(merge_file_result.content()).to_string(),
41094064
r"<<<<<<< ours

0 commit comments

Comments
 (0)