Skip to content

Commit 0736d2c

Browse files
author
K
committed
Improve
1 parent 2b5b1dd commit 0736d2c

File tree

2 files changed

+45
-38
lines changed

2 files changed

+45
-38
lines changed

Diff for: src/merge.rs

+32-26
Original file line numberDiff line numberDiff line change
@@ -402,53 +402,59 @@ impl std::fmt::Debug for MergeFileInput<'_> {
402402

403403
/// For git_merge_file_result
404404
pub struct MergeFileResult {
405-
/// True if the output was automerged, false if the output contains
406-
/// conflict markers.
407-
pub automergeable: bool,
408-
409-
/// The path that the resultant merge file should use, or NULL if a
410-
/// filename conflict would occur.
411-
pub path: Option<String>,
412-
413-
/// The mode that the resultant merge file should use.
414-
pub mode: FileMode,
415-
416-
/// The contents of the merge.
417-
pub content: Option<Vec<u8>>,
405+
raw: raw::git_merge_file_result,
418406
}
419407

420408
impl MergeFileResult {
421409
/// Create MergeFileResult from C
422410
pub unsafe fn from_raw(raw: raw::git_merge_file_result) -> MergeFileResult {
423-
let c_str: &CStr = CStr::from_ptr(raw.path);
411+
MergeFileResult { raw }
412+
}
413+
414+
/// True if the output was automerged, false if the output contains
415+
/// conflict markers.
416+
pub fn automergeable(&self) -> bool {
417+
self.raw.automergeable > 0
418+
}
419+
420+
/// The path that the resultant merge file should use, or NULL if a
421+
/// filename conflict would occur.
422+
pub unsafe fn path(&self) -> Option<String> {
423+
let c_str: &CStr = CStr::from_ptr(self.raw.path);
424424
let str_slice: &str = c_str.to_str().unwrap();
425425
let path: String = str_slice.to_owned();
426+
Some(path)
427+
}
426428

427-
let content = slice::from_raw_parts(raw.ptr as *const u8, raw.len as usize).to_vec();
429+
/// The mode that the resultant merge file should use.
430+
pub fn mode(&self) -> FileMode {
431+
FileMode::from(self.raw.mode.try_into().unwrap())
432+
}
428433

429-
MergeFileResult {
430-
automergeable: raw.automergeable > 0,
431-
path: Some(path),
432-
mode: FileMode::from(raw.mode.try_into().unwrap()),
433-
content: Some(content),
434-
}
434+
/// The contents of the merge.
435+
pub unsafe fn content(&self) -> Option<Vec<u8>> {
436+
let content =
437+
slice::from_raw_parts(self.raw.ptr as *const u8, self.raw.len as usize).to_vec();
438+
Some(content)
435439
}
436440
}
437441

438442
impl std::fmt::Debug for MergeFileResult {
439443
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
440444
let mut ds = f.debug_struct("MergeFileResult");
441-
if let Some(path) = &self.path {
442-
ds.field("path", path);
445+
unsafe {
446+
if let Some(path) = &self.path() {
447+
ds.field("path", path);
448+
}
443449
}
444-
ds.field("mode", &self.mode);
450+
ds.field("mode", &self.mode());
445451

446-
match self.mode {
452+
match self.mode() {
447453
FileMode::Unreadable => {}
448454
FileMode::Tree => {}
449455
FileMode::Blob => unsafe {
450456
let content = self
451-
.content
457+
.content()
452458
.as_ref()
453459
.map(|c| String::from_utf8_unchecked(c.clone()))
454460
.unwrap_or("unknown content".to_string());

Diff for: src/repo.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -3436,15 +3436,11 @@ mod tests {
34363436
let ours_blob;
34373437
let theirs_blob;
34383438

3439-
let ancestor_content;
3440-
let ours_content;
3441-
let theirs_content;
3442-
34433439
if let Some(ancestor) = conflict.ancestor {
34443440
ancestor_blob = repo
34453441
.find_blob(ancestor.id.clone())
34463442
.expect("failed to find blob of index entry to make MergeFileInput");
3447-
ancestor_content = ancestor_blob.content();
3443+
let ancestor_content = ancestor_blob.content();
34483444
let mut input = MergeFileInput::new();
34493445
input.path(String::from_utf8(ancestor.path).unwrap());
34503446
input.mode(Some(FileMode::from(ancestor.mode.try_into().unwrap())));
@@ -3457,7 +3453,7 @@ mod tests {
34573453
ours_blob = repo
34583454
.find_blob(ours.id.clone())
34593455
.expect("failed to find blob of index entry to make MergeFileInput");
3460-
ours_content = ours_blob.content();
3456+
let ours_content = ours_blob.content();
34613457
let mut input = MergeFileInput::new();
34623458
input.path(String::from_utf8(ours.path).unwrap());
34633459
input.mode(Some(FileMode::from(ours.mode.try_into().unwrap())));
@@ -3470,7 +3466,7 @@ mod tests {
34703466
theirs_blob = repo
34713467
.find_blob(theirs.id.clone())
34723468
.expect("failed to find blob of index entry to make MergeFileInput");
3473-
theirs_content = theirs_blob.content();
3469+
let theirs_content = theirs_blob.content();
34743470
let mut input = MergeFileInput::new();
34753471
input.path(String::from_utf8(theirs.path).unwrap());
34763472
input.mode(Some(FileMode::from(theirs.mode.try_into().unwrap())));
@@ -3489,12 +3485,17 @@ mod tests {
34893485
)
34903486
.unwrap();
34913487

3492-
assert_eq!(merge_file_result.mode, FileMode::Blob);
3493-
assert_eq!(merge_file_result.path.unwrap().as_str(), "file_a");
3488+
assert_eq!(merge_file_result.mode(), FileMode::Blob);
3489+
assert_eq!(
3490+
unsafe { merge_file_result.path().unwrap().as_str() },
3491+
"file_a"
3492+
);
34943493
assert_eq!(
3495-
String::from_utf8(merge_file_result.content.unwrap())
3496-
.unwrap()
3497-
.as_str(),
3494+
unsafe {
3495+
String::from_utf8(merge_file_result.content().unwrap())
3496+
.unwrap()
3497+
.as_str()
3498+
},
34983499
merge_file_result_content
34993500
);
35003501
}

0 commit comments

Comments
 (0)