10000 Expose git_merge_file function from libgit2 to repo.rs of git2-rs by kvzn · Pull Request #635 · rust-lang/git2-rs · GitHub
[go: up one dir, main page]

Skip to content

Expose git_merge_file function from libgit2 to repo.rs of git2-rs #635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Improve
  • Loading branch information
K committed Nov 26, 2020
commit 0736d2ca00dd182260a3833ed13866db533d6cf7
58 changes: 32 additions & 26 deletions src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,53 +402,59 @@ impl std::fmt::Debug for MergeFileInput<'_> {

/// For git_merge_file_result
pub struct MergeFileResult {
/// True if the output was automerged, false if the output contains
/// conflict markers.
pub automergeable: bool,

/// The path that the resultant merge file should use, or NULL if a
/// filename conflict would occur.
pub path: Option<String>,

/// The mode that the resultant merge file should use.
pub mode: FileMode,

/// The contents of the merge.
pub content: Option<Vec<u8>>,
raw: raw::git_merge_file_result,
}

impl MergeFileResult {
/// Create MergeFileResult from C
pub unsafe fn from_raw(raw: raw::git_merge_file_result) -> MergeFileResult {
let c_str: &CStr = CStr::from_ptr(raw.path);
MergeFileResult { raw }
}

/// True if the output was automerged, false if the output contains
/// conflict markers.
pub fn automergeable(&self) -> bool {
self.raw.automergeable > 0
}

/// The path that the resultant merge file should use, or NULL if a
/// filename conflict would occur.
pub unsafe fn path(&self) -> Option<String> {
let c_str: &CStr = CStr::from_ptr(self.raw.path);
let str_slice: &str = c_str.to_str().unwrap();
let path: String = str_slice.to_owned();
Some(path)
}

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

MergeFileResult {
automergeable: raw.automergeable > 0,
path: Some(path),
mode: FileMode::from(raw.mode.try_into().unwrap()),
content: Some(content),
}
/// The contents of the merge.
pub unsafe fn content(&self) -> Option<Vec<u8>> {
let content =
slice::from_raw_parts(self.raw.ptr as *const u8, self.raw.len as usize).to_vec();
Some(content)
}
}

impl std::fmt::Debug for MergeFileResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
let mut ds = f.debug_struct("MergeFileResult");
if let Some(path) = &self.path {
ds.field("path", path);
unsafe {
if let Some(path) = &self.path() {
ds.field("path", path);
}
}
ds.field("mode", &self.mode);
ds.field("mode", &self.mode());

match self.mode {
match self.mode() {
FileMode::Unreadable => {}
FileMode::Tree => {}
FileMode::Blob => unsafe {
let content = self
.content
.content()
.as_ref()
.map(|c| String::from_utf8_unchecked(c.clone()))
.unwrap_or("unknown content".to_string());
Expand Down
25 changes: 13 additions & 12 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3436,15 +3436,11 @@ mod tests {
let ours_blob;
let theirs_blob;

let ancestor_content;
let ours_content;
let theirs_content;

if let Some(ancestor) = conflict.ancestor {
ancestor_blob = repo
.find_blob(ancestor.id.clone())
.expect("failed to find blob of index entry to make MergeFileInput");
ancestor_content = ancestor_blob.content();
let ancestor_content = ancestor_blob.content();
let mut input = MergeFileInput::new();
input.path(String::from_utf8(ancestor.path).unwrap());
input.mode(Some(FileMode::from(ancestor.mode.try_into().unwrap())));
Expand All @@ -3457,7 +3453,7 @@ mod tests {
ours_blob = repo
.find_blob(ours.id.clone())
.expect("failed to find blob of index entry to make MergeFileInput");
ours_content = ours_blob.content();
let ours_content = ours_blob.content();
let mut input = MergeFileInput::new();
input.path(String::from_utf8(ours.path).unwrap());
input.mode(Some(FileMode::from(ours.mode.try_into().unwrap())));
Expand All @@ -3470,7 +3466,7 @@ mod tests {
theirs_blob = repo
.find_blob(theirs.id.clone())
.expect("failed to find blob of index entry to make MergeFileInput");
theirs_content = theirs_blob.content();
let theirs_content = theirs_blob.content();
let mut input = MergeFileInput::new();
input.path(String::from_utf8(theirs.path).unwrap());
input.mode(Some(FileMode::from(theirs.mode.try_into().unwrap())));
Expand All @@ -3489,12 +3485,17 @@ mod tests {
)
.unwrap();

assert_eq!(merge_file_result.mode, FileMode::Blob);
assert_eq!(merge_file_result.path.unwrap().as_str(), "file_a");
assert_eq!(merge_file_result.mode(), FileMode::Blob);
assert_eq!(
unsafe { merge_file_result.path().unwrap().as_str() },
"file_a"
);
assert_eq!(
String::from_utf8(merge_file_result.content.unwrap())
.unwrap()
.as_str(),
unsafe {
String::from_utf8(merge_file_result.content().unwrap())
.unwrap()
.as_str()
},
merge_file_result_content
);
}
Expand Down
0