Skip to content

Commit 742c630

Browse files
MikeJerredehuss
authored andcommitted
fix: review comments
1 parent e80d1ab commit 742c630

File tree

4 files changed

+53
-57
lines changed

4 files changed

+53
-57
lines changed

libgit2-sys/lib.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1373,19 +1373,14 @@ pub struct git_merge_file_options {
13731373
}
13741374

13751375
#[repr(C)]
1376-
#[derive(Copy)]
1376+
#[derive(Clone, Copy)]
13771377
pub struct git_merge_file_result {
13781378
pub automergeable: c_uint,
13791379
pub path: *const c_char,
13801380
pub mode: c_uint,
13811381
pub ptr: *const c_char,
13821382
pub len: size_t,
13831383
}
1384-
impl Clone for git_merge_file_result {
1385-
fn clone(&self) -> git_merge_file_result {
1386-
*self
1387-
}
1388-
}
13891384

13901385
git_enum! {
13911386
pub enum git_merge_flag_t {

src/index.rs

+46
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,52 @@ impl Index {
656656
}
657657
}
658658

659+
impl IndexEntry {
660+
/// Create a raw index entry.
661+
///
662+
/// The returned `raw::git_index_entry` contains a pointer to a `CString` path, which is also
663+
/// returned because it's lifetime must exceed the lifetime of the `raw::git_index_entry`.
664+
pub fn to_raw(&self) -> Result<(raw::git_index_entry, CString), Error> {
665+
let path = CString::new(&self.path[..])?;
666+
667+
// libgit2 encodes the length of the path in the lower bits of the
668+
// `flags` entry, so mask those out and recalculate here to ensure we
669+
// don't corrupt anything.
670+
let mut flags = self.flags & !raw::GIT_INDEX_ENTRY_NAMEMASK;
671+
672+
if self.path.len() < raw::GIT_INDEX_ENTRY_NAMEMASK as usize {
673+
flags |= self.path.len() as u16;
674+
} else {
675+
flags |= raw::GIT_INDEX_ENTRY_NAMEMASK;
676+
}
677+
678+
unsafe {
679+
let raw = raw::git_index_entry {
680+
dev: self.dev,
681+
ino: self.ino,
682+
mode: self.mode,
683+
uid: self.uid,
684+
gid: self.gid,
685+
file_size: self.file_size,
686+
id: *self.id.raw(),
687+
flags,
688+
flags_extended: self.flags_extended,
689+
path: path.as_ptr(),
690+
mtime: raw::git_index_time {
691+
seconds: self.mtime.seconds(),
692+
nanoseconds: self.mtime.nanoseconds(),
693+
},
694+
ctime: raw::git_index_time {
695+
seconds: self.ctime.seconds(),
696+
nanoseconds: self.ctime.nanoseconds(),
697+
},
698+
};
699+
700+
Ok((raw, path))
701+
}
702+
}
703+
}
704+
659705
impl Binding for Index {
660706
type Raw = *mut raw::git_index;
661707
unsafe fn from_raw(raw: *mut raw::git_index) -> Index {

src/merge.rs

+1-1
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

+5-50
Original file line numberDiff line numberDiff line change
@@ -2577,58 +2577,12 @@ impl Repository {
25772577
theirs: &IndexEntry,
25782578
opts: Option<&mut MergeFileOptions>,
25792579
) -> Result<MergeFileResult<'_>, Error> {
2580-
let create_raw_entry = |entry: &IndexEntry| -> Result<raw::git_index_entry, Error> {
2581-
let path = CString::new(&entry.path[..])?;
2582-
2583-
// libgit2 encodes the length of the path in the lower bits of the
2584-
// `flags` entry, so mask those out and recalculate here to ensure we
2585-
// don't corrupt anything.
2586-
let mut flags = entry.flags & !raw::GIT_INDEX_ENTRY_NAMEMASK;
2587-
2588-
if entry.path.len() < raw::GIT_INDEX_ENTRY_NAMEMASK as usize {
2589-
flags |= entry.path.len() as u16;
2590-
} else {
2591-
flags |= raw::GIT_INDEX_ENTRY_NAMEMASK;
2592-
}
2593-
2594-
unsafe {
2595-
let raw = raw::git_index_entry {
2596-
dev: entry.dev,
2597-
ino: entry.ino,
2598-
mode: entry.mode,
2599-
uid: entry.uid,
2600-
gid: entry.gid,
2601-
file_size: entry.file_size,
2602-
id: *entry.id.raw(),
2603-
flags,
2604-
flags_extended: entry.flags_extended,
2605-
path: path.as_ptr(),
2606-
mtime: raw::git_index_time {
2607-
seconds: entry.mtime.seconds(),
2608-
nanoseconds: entry.mtime.nanoseconds(),
2609-
},
2610-
ctime: raw::git_index_time {
2611-
seconds: entry.ctime.seconds(),
2612-
nanoseconds: entry.ctime.nanoseconds(),
2613-
},
2614-
};
2615-
2616-
Ok(raw)
2617-
}
2618-
};
2619-
2620-
let mut ret = raw::git_merge_file_result {
2621-
automergeable: 0,
2622-
path: ptr::null_mut(),
2623-
mode: 0,
2624-
ptr: ptr::null_mut(),
2625-
len: 0,
2626-
};
2627-
let ancestor = create_raw_entry(ancestor)?;
2628-
let ours = create_raw_entry(ours)?;
2629-
let theirs = create_raw_entry(theirs)?;
2580+
let (ancestor, _ancestor_path) = ancestor.to_raw()?;
2581+
let (ours, _ours_path) = ours.to_raw()?;
2582+
let (theirs, _theirs_path) = theirs.to_raw()?;
26302583

26312584
unsafe {
2585+
let mut ret = mem::zeroed();
26322586
try_call!(raw::git_merge_file_from_index(
26332587
&mut ret,
26342588
self.raw(),
@@ -4182,6 +4136,7 @@ mod tests {
41824136
.unwrap();
41834137

41844138
assert!(!merge_file_result.is_automergeable());
4139+
assert_eq!(merge_file_result.path(), Some("file"));
41854140
assert_eq!(
41864141
String::from_utf8_lossy(merge_file_result.content()).to_string(),
41874142
r"<<<<<<< ours

0 commit comments

Comments
 (0)