Skip to content
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

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
@@ -1,3 +1,7 @@
target
Cargo.lock
src/main.rs

.vscode/
.history/
.idea/
77 changes: 76 additions & 1 deletion libgit2-sys/lib.rs
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 @@ -1305,6 +1306,65 @@ git_enum! {
}
}

#[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,
}

pub type git_transport_cb = Option<
extern "C" fn(
out: *mut *mut git_transport,
Expand Down Expand Up @@ -3072,7 +3132,6 @@ extern "C" {
pub fn git_repository_state_cleanup(repo: *mut git_repository) -> c_int;

// merge analysis

pub fn git_merge_analysis(
analysis_out: *mut git_merge_analysis_t,
pref_out: *mut git_merge_preference_t,
Expand All @@ -3081,6 +3140,22 @@ extern "C" {
their_heads_len: usize,
) -> c_int;

// For git_merge_file
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);

// notes
pub fn git_note_author(note: *const git_note) -> *const git_signature;
pub fn git_note_committer(note: *const git_note) -> *const git_signature;
Expand Down
18 changes: 17 additions & 1 deletion src/lib.rs
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,20 @@ pub enum FileMode {
Commit,
}

impl FileMode {
fn from(mode: u32) -> Self {
match mode {
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),
alexcrichton marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

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