1
1
use crate :: config:: cache:: util:: ApplyLeniencyDefault ;
2
2
use crate :: config:: tree;
3
- use crate :: repository:: { blob_merge_options, merge_resource_cache} ;
3
+ use crate :: repository:: { blob_merge_options, merge_resource_cache, merge_trees , tree_merge_options } ;
4
4
use crate :: Repository ;
5
5
use gix_merge:: blob:: builtin_driver:: text;
6
+ use gix_object:: Write ;
6
7
use std:: borrow:: Cow ;
7
8
8
9
/// Merge-utilities
9
10
impl Repository {
10
11
/// Create a resource cache that can hold the three resources needed for a three-way merge. `worktree_roots`
11
12
/// determines which side of the merge is read from the worktree, or from which worktree.
12
13
///
13
- /// The platform can be used to setup resources and finally perform a merge.
14
+ /// The platform can be used to set up resources and finally perform a merge among blobs .
14
15
///
15
16
/// Note that the current index is used for attribute queries.
16
17
pub fn merge_resource_cache (
@@ -55,7 +56,8 @@ impl Repository {
55
56
Ok ( gix_merge:: blob:: Platform :: new ( filter, mode, attrs, drivers, options) )
56
57
}
57
58
58
- /// Return options for use with [`gix_merge::blob::PlatformRef::merge()`].
59
+ /// Return options for use with [`gix_merge::blob::PlatformRef::merge()`], accessible through
60
+ /// [merge_resource_cache()](Self::merge_resource_cache).
59
61
pub fn blob_merge_options ( & self ) -> Result < gix_merge:: blob:: platform:: merge:: Options , blob_merge_options:: Error > {
60
62
Ok ( gix_merge:: blob:: platform:: merge:: Options {
61
63
is_virtual_ancestor : false ,
@@ -79,4 +81,58 @@ impl Repository {
79
81
} ,
80
82
} )
81
83
}
84
+
85
+ /// Read all relevant configuration options to instantiate options for use in [`merge_trees()`](Self::merge_trees).
86
+ pub fn tree_merge_options ( & self ) -> Result < gix_merge:: tree:: Options , tree_merge_options:: Error > {
87
+ Ok ( gix_merge:: tree:: Options {
88
+ rewrites : crate :: diff:: utils:: new_rewrites_inner (
89
+ & self . config . resolved ,
90
+ self . config . lenient_config ,
91
+ & tree:: Merge :: RENAMES ,
92
+ & tree:: Merge :: RENAME_LIMIT ,
93
+ ) ?,
94
+ blob_merge : self . blob_merge_options ( ) ?,
95
+ blob_merge_command_ctx : self . command_context ( ) ?,
96
+ fail_on_conflict : None ,
97
+ marker_size_multiplier : 0 ,
98
+ symlink_conflicts : None ,
99
+ allow_lossy_resolution : false ,
100
+ } )
101
+ }
102
+
103
+ /// Merge `our_tree` and `their_tree` together, assuming they have the same `ancestor_tree`, to yield a new tree
104
+ /// which is provided as [tree editor](gix_object::tree::Editor) to inspect and finalize results at will.
105
+ /// No change to the worktree or index is made, but objects may be written to the object database as merge results
106
+ /// are stored.
107
+ /// If these changes should not be observable outside of this instance, consider [enabling object memory](Self::with_object_memory).
108
+ ///
109
+ /// Note that `ancestor_tree` can be the [empty tree hash](gix_hash::ObjectId::empty_tree) to indicate no common ancestry.
110
+ ///
111
+ /// `labels` are typically chosen to identify the refs or names for `our_tree` and `their_tree` and `ancestor_tree` respectively.
112
+ ///
113
+ /// `options` should be initialized with [`tree_merge_options()`](Self::tree_merge_options()).
114
+ // TODO: Use `crate::merge::Options` here and add niceties such as setting the resolution strategy.
115
+ pub fn merge_trees (
116
+ & self ,
117
+ ancestor_tree : impl AsRef < gix_hash:: oid > ,
118
+ our_tree : impl AsRef < gix_hash:: oid > ,
119
+ their_tree : impl AsRef < gix_hash:: oid > ,
120
+ labels : gix_merge:: blob:: builtin_driver:: text:: Labels < ' _ > ,
121
+ options : gix_merge:: tree:: Options ,
122
+ ) -> Result < gix_merge:: tree:: Outcome < ' _ > , merge_trees:: Error > {
123
+ let mut diff_cache = self . diff_resource_cache_for_tree_diff ( ) ?;
124
+ let mut blob_merge = self . merge_resource_cache ( Default :: default ( ) ) ?;
125
+ Ok ( gix_merge:: tree (
126
+ ancestor_tree. as_ref ( ) ,
127
+ our_tree. as_ref ( ) ,
128
+ their_tree. as_ref ( ) ,
129
+ labels,
130
+ & self . objects ,
131
+ |buf| self . objects . write_buf ( gix_object:: Kind :: Blob , buf) ,
132
+ & mut Default :: default ( ) ,
133
+ & mut diff_cache,
134
+ & mut blob_merge,
135
+ options,
136
+ ) ?)
137
+ }
82
138
}
0 commit comments