@@ -18,6 +18,7 @@ use crate::string_array::StringArray;
1818use crate :: tagforeach:: { tag_foreach_cb, TagForeachCB , TagForeachData } ;
1919use crate :: util:: { self , path_to_repo_path, Binding } ;
2020use crate :: worktree:: { Worktree , WorktreeAddOptions } ;
21+ use crate :: CherrypickOptions ;
2122use crate :: RevertOptions ;
2223use crate :: {
2324 raw, AttrCheckFlags , Buf , Error , Object , Remote , RepositoryOpenFlags , RepositoryState , Revspec ,
@@ -29,7 +30,6 @@ use crate::{
2930use crate :: { ApplyLocation , ApplyOptions , Rebase , RebaseOptions } ;
3031use crate :: { Blame , BlameOptions , Reference , References , ResetType , Signature , Submodule } ;
3132use crate :: { Blob , BlobWriter , Branch , BranchType , Branches , Commit , Config , Index , Oid , Tree } ;
32- use crate :: { CherrypickOptions , IndexEntry } ;
3333use crate :: { Describe , IntoCString , Reflog , RepositoryInitMode , RevparseMode } ;
3434use crate :: { DescribeOptions , Diff , DiffOptions , Odb , PackBuilder , TreeBuilder } ;
3535use crate :: { Note , Notes , ObjectType , Revwalk , Status , StatusOptions , Statuses , Tag } ;
@@ -1848,47 +1848,13 @@ impl Repository {
18481848 ///
18491849 /// Note that this function does not reference a repository and any
18501850 /// configuration must be passed as `git_merge_file_options`.
1851- ///
1852- /// @param out The git_merge_file_result to be filled in
1853- /// @param ancestor The contents of the ancestor file
1854- /// @param ours The contents of the file in "our" side
1855- /// @param theirs The contents of the file in "their" side
1856- /// @param opts The merge file options or `NULL` for defaults
1857- /// @return 0 on success or error code
18581851 pub fn merge_file (
18591852 & self ,
1860- ancestor : Option < & IndexEntry > ,
1861- ours : Option < & IndexEntry > ,
1862- theirs : Option < & IndexEntry > ,
1853+ ancestor : Option < & MergeFileInput < ' _ > > ,
1854+ ours : Option < & MergeFileInput < ' _ > > ,
1855+ theirs : Option < & MergeFileInput < ' _ > > ,
18631856 options : Option < & MergeFileOptions > ,
18641857 ) -> Result < MergeFileResult , Error > {
1865- let ancestor_input;
1866- let ours_input;
1867- let theirs_input;
1868-
1869- let ancestor_raw;
1870- let ours_raw;
1871- let theirs_raw;
1872-
1873- if let Some ( ancestor) = ancestor {
1874- ancestor_input = MergeFileInput :: from ( & self , ancestor) ;
1875- ancestor_raw = ancestor_input. raw ( ) ;
1876- } else {
1877- ancestor_raw = ptr:: null ( ) ;
1878- }
1879- if let Some ( ours) = ours {
1880- ours_input = MergeFileInput :: from ( & self , ours) ;
1881- ours_raw = ours_input. raw ( ) ;
1882- } else {
1883- ours_raw = ptr:: null ( ) ;
1884- }
1885- if let Some ( theirs) = theirs {
1886- theirs_input = MergeFileInput :: from ( & self , theirs) ;
1887- theirs_raw = theirs_input. raw ( ) ;
1888- } else {
1889- theirs_raw = ptr:: null ( ) ;
1890- }
1891-
18921858 let mut ret = raw:: git_merge_file_result {
18931859 automergeable : 0 ,
18941860 path : ptr:: null ( ) ,
@@ -1900,9 +1866,9 @@ impl Repository {
19001866 unsafe {
19011867 try_call ! ( raw:: git_merge_file(
19021868 & mut ret,
1903- ancestor_raw ,
1904- ours_raw ,
1905- theirs_raw ,
1869+ ancestor . map ( |a| a . raw ( ) ) . unwrap_or ( ptr :: null ( ) ) ,
1870+ ours . map ( |a| a . raw ( ) ) . unwrap_or ( ptr :: null ( ) ) ,
1871+ theirs . map ( |a| a . raw ( ) ) . unwrap_or ( ptr :: null ( ) ) ,
19061872 options. map( |o| o. raw( ) )
19071873 ) ) ;
19081874
@@ -3104,8 +3070,9 @@ impl RepositoryInitOptions {
31043070#[ cfg( test) ]
31053071mod tests {
31063072 use crate :: build:: CheckoutBuilder ;
3107- use crate :: { CherrypickOptions , FileMode } ;
3073+ use crate :: { CherrypickOptions , FileMode , MergeFileInput } ;
31083074 use crate :: { ObjectType , Oid , Repository , ResetType } ;
3075+ use std:: convert:: TryInto ;
31093076 use std:: ffi:: OsStr ;
31103077 use std:: fs;
31113078 use std:: io:: Write ;
@@ -3457,11 +3424,63 @@ mod tests {
34573424 for conflict in index_conflicts {
34583425 let conflict = conflict. unwrap ( ) ;
34593426
3427+ let ancestor_input;
3428+ let ours_input;
3429+ let theirs_input;
3430+
3431+ let ancestor_blob;
3432+ let ours_blob;
3433+ let theirs_blob;
3434+
3435+ let ancestor_content;
3436+ let ours_content;
3437+ let theirs_content;
3438+
3439+ if let Some ( ancestor) = conflict. ancestor {
3440+ ancestor_blob = repo
3441+ . find_blob ( ancestor. id . clone ( ) )
3442+ . expect ( "failed to find blob of index entry to make MergeFileInput" ) ;
3443+ ancestor_content = ancestor_blob. content ( ) ;
3444+ let mut input = MergeFileInput :: new ( ) ;
3445+ input. path ( String :: from_utf8 ( ancestor. path ) . unwrap ( ) ) ;
3446+ input. mode ( Some ( FileMode :: from ( ancestor. mode . try_into ( ) . unwrap ( ) ) ) ) ;
3447+ input. content ( Some ( & ancestor_content) ) ;
3448+ ancestor_input = Some ( input) ;
3449+ } else {
3450+ ancestor_input = None ;
3451+ }
3452+ if let Some ( ours) = conflict. our {
3453+ ours_blob = repo
3454+ . find_blob ( ours. id . clone ( ) )
3455+ . expect ( "failed to find blob of index entry to make MergeFileInput" ) ;
3456+ ours_content = ours_blob. content ( ) ;
3457+ let mut input = MergeFileInput :: new ( ) ;
3458+ input. path ( String :: from_utf8 ( ours. path ) . unwrap ( ) ) ;
3459+ input. mode ( Some ( FileMode :: from ( ours. mode . try_into ( ) . unwrap ( ) ) ) ) ;
3460+ input. content ( Some ( & ours_content) ) ;
3461+ ours_input = Some ( input) ;
3462+ } else {
3463+ ours_input = None ;
3464+ }
3465+ if let Some ( theirs) = conflict. their {
3466+ theirs_blob = repo
3467+ . find_blob ( theirs. id . clone ( ) )
3468+ . expect ( "failed to find blob of index entry to make MergeFileInput" ) ;
3469+ theirs_content = theirs_blob. content ( ) ;
3470+ let mut input = MergeFileInput :: new ( ) ;
3471+ input. path ( String :: from_utf8 ( theirs. path ) . unwrap ( ) ) ;
3472+ input. mode ( Some ( FileMode :: from ( theirs. mode . try_into ( ) . unwrap ( ) ) ) ) ;
3473+ input. content ( Some ( & theirs_content) ) ;
3474+ theirs_input = Some ( input) ;
3475+ } else {
3476+ theirs_input = None ;
3477+ }
3478+
34603479 let merge_file_result = repo
34613480 . merge_file (
3462- conflict . ancestor . as_ref ( ) ,
3463- conflict . our . as_ref ( ) ,
3464- conflict . their . as_ref ( ) ,
3481+ ancestor_input . as_ref ( ) ,
3482+ ours_input . as_ref ( ) ,
3483+ theirs_input . as_ref ( ) ,
34653484 None ,
34663485 )
34673486 . unwrap ( ) ;
0 commit comments