Skip to content

Commit

Permalink
Expose git_merge_file function from libgit2 to repo.rs of git2-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
K committed Nov 22, 2020
1 parent 2ba1852 commit 4a39ad8
Show file tree
Hide file tree
Showing 4 changed files with 629 additions and 4 deletions.
77 changes: 77 additions & 0 deletions libgit2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use libc::{c_char, c_int, c_uchar, c_uint, c_void, size_t};
#[cfg(feature = "ssh")]
use libssh2_sys as libssh2;
use std::ffi::CStr;
use std::os::raw::c_ushort;

pub const GIT_OID_RAWSZ: usize = 20;
pub const GIT_OID_HEXSZ: usize = GIT_OID_RAWSZ * 2;
Expand Down Expand Up @@ -3854,6 +3855,82 @@ extern "C" {
) -> c_int;
}

#[repr(C)]
pub struct git_merge_file_options {
pub version: c_uint,

/// Label for the ancestor file side of the conflict which will be prepended
/// to labels in diff3-format merge files.
pub ancestor_label: *const c_char,

/// Label for our file side of the conflict which will be prepended
/// to labels in merge files.
pub our_label: *const c_char,

/// Label for their file side of the conflict which will be prepended
/// to labels in merge files.
pub their_label: *const c_char,

/// The file to favor in region conflicts.
pub favor: git_merge_file_favor_t,

/// see `git_merge_file_flag_t`
pub flags: c_uint,
pub marker_size: c_ushort,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct git_merge_file_input {
pub version: c_uint,
/// Pointer to the contents of the file.
pub ptr: *const c_char,
/// Size of the contents pointed to in `ptr`.
pub size: size_t,
/// File name of the conflicted file, or `NULL` to not merge the path.
pub path: *const c_char,
/// File mode of the conflicted file, or `0` to not merge the mode.
pub mode: c_uint,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct git_merge_file_result {
/// True if the output was automerged, false if the output contains
/// conflict markers.
pub automergeable: c_uint,

/// The path that the resultant merge file should use, or NULL if a
/// filename conflict would occur.
pub path: *const c_char,

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

/// The contents of the merge.
pub ptr: *const c_char,

/// The length of the merge contents.
pub len: size_t,
}

extern "C" {
pub fn git_merge_file_options_init(opts: *mut git_merge_file_options, version: c_uint)
-> c_int;
pub fn git_merge_file_input_init(opts: *mut git_merge_file_input, version: c_uint) -> c_int;

pub fn git_merge_file(
out: *mut git_merge_file_result,
ancestor: *const git_merge_file_input,
ours: *const git_merge_file_input,
theirs: *const git_merge_file_input,
opts: *const git_merge_file_options,
) -> c_int;

// Not used?
pub fn git_merge_file_result_free(result: *mut git_merge_file_result);
}

pub fn init() {
use std::sync::Once;

Expand Down
32 changes: 31 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ pub use crate::index::{
};
pub use crate::indexer::{IndexerProgress, Progress};
pub use crate::mempack::Mempack;
pub use crate::merge::{AnnotatedCommit, MergeOptions};
pub use crate::merge::{
AnnotatedCommit, MergeFileInput, MergeFileOptions, MergeFileResult, MergeOptions,
};
pub use crate::message::{message_prettify, DEFAULT_COMMENT_CHAR};
pub use crate::note::{Note, Notes};
pub use crate::object::Object;
Expand Down Expand Up @@ -1049,6 +1051,34 @@ pub enum FileMode {
Commit,
}

impl From<u32> for FileMode {
fn from(mode: u32) -> Self {
match mode.into() {
raw::GIT_FILEMODE_UNREADABLE => FileMode::Unreadable,
raw::GIT_FILEMODE_TREE => FileMode::Tree,
raw::GIT_FILEMODE_BLOB => FileMode::Blob,
raw::GIT_FILEMODE_BLOB_EXECUTABLE => FileMode::BlobExecutable,
raw::GIT_FILEMODE_LINK => FileMode::Link,
raw::GIT_FILEMODE_COMMIT => FileMode::Commit,
mode => panic!("unknown file mode: {}", mode),
}
}
}

impl Into<u32> for FileMode {
fn into(self) -> u32 {
match self {
FileMode::Unreadable => raw::GIT_FILEMODE_UNREADABLE,
FileMode::Tree => raw::GIT_FILEMODE_TREE,
FileMode::Blob => raw::GIT_FILEMODE_BLOB,
FileMode::BlobExecutable => raw::GIT_FILEMODE_BLOB_EXECUTABLE,
FileMode::Link => raw::GIT_FILEMODE_LINK,
FileMode::Commit => raw::GIT_FILEMODE_COMMIT,
}
.into()
}
}

bitflags! {
/// Return codes for submodule status.
///
Expand Down
Loading

0 comments on commit 4a39ad8

Please sign in to comment.