|  | 
|  | 1 | +/// The error returned by [`commit()`](crate::commit()). | 
|  | 2 | +#[derive(Debug, thiserror::Error)] | 
|  | 3 | +#[allow(missing_docs)] | 
|  | 4 | +pub enum Error { | 
|  | 5 | +    #[error(transparent)] | 
|  | 6 | +    MergeBase(#[from] gix_revision::merge_base::Error), | 
|  | 7 | +    #[error(transparent)] | 
|  | 8 | +    MergeTree(#[from] crate::tree::Error), | 
|  | 9 | +    #[error("Failed to write tree for merged merge-base or virtual commit")] | 
|  | 10 | +    WriteObject(gix_object::write::Error), | 
|  | 11 | +    #[error("No common ancestor between {our_commit_id} and {their_commit_id}")] | 
|  | 12 | +    NoMergeBase { | 
|  | 13 | +        /// The commit on our side that was to be merged. | 
|  | 14 | +        our_commit_id: gix_hash::ObjectId, | 
|  | 15 | +        /// The commit on their side that was to be merged. | 
|  | 16 | +        their_commit_id: gix_hash::ObjectId, | 
|  | 17 | +    }, | 
|  | 18 | +    #[error( | 
|  | 19 | +        "Conflicts occurred when trying to resolve multiple merge-bases by merging them. This is most certainly a bug." | 
|  | 20 | +    )] | 
|  | 21 | +    VirtualMergeBaseConflict, | 
|  | 22 | +    #[error("Could not find ancestor, our or their commit to extract tree from")] | 
|  | 23 | +    FindCommit(#[from] gix_object::find::existing_object::Error), | 
|  | 24 | +} | 
|  | 25 | + | 
|  | 26 | +/// A way to configure [`commit()`](crate::commit()). | 
|  | 27 | +#[derive(Default, Debug, Clone)] | 
|  | 28 | +pub struct Options { | 
|  | 29 | +    /// If `true`, merging unrelated commits is allowed, with the merge-base being assumed as empty tree. | 
|  | 30 | +    pub allow_missing_merge_base: bool, | 
|  | 31 | +    /// Options to define how trees should be merged. | 
|  | 32 | +    pub tree_merge: crate::tree::Options, | 
|  | 33 | +    /// If `true`, do not merge multiple merge-bases into one. Instead, just use the first one. | 
|  | 34 | +    // TODO: test | 
|  | 35 | +    #[doc(alias = "no_recursive", alias = "git2")] | 
|  | 36 | +    pub use_first_merge_base: bool, | 
|  | 37 | +} | 
|  | 38 | + | 
|  | 39 | +/// The result of [`commit()`](crate::commit()). | 
|  | 40 | +#[derive(Clone)] | 
|  | 41 | +pub struct Outcome<'a> { | 
|  | 42 | +    /// The outcome of the actual tree-merge. | 
|  | 43 | +    pub tree_merge: crate::tree::Outcome<'a>, | 
|  | 44 | +    /// The tree id of the base commit we used. This is either… | 
|  | 45 | +    /// * the single merge-base we found | 
|  | 46 | +    /// * the first of multiple merge-bases if [`use_first_merge_base`](Options::use_first_merge_base) was `true`. | 
|  | 47 | +    /// * the merged tree of all merge-bases, which then isn't linked to an actual commit. | 
|  | 48 | +    /// * an empty tree, if [`allow_missing_merge_base`](Options::allow_missing_merge_base) is enabled. | 
|  | 49 | +    pub merge_base_tree_id: gix_hash::ObjectId, | 
|  | 50 | +    /// The object ids of all the commits which were found to be merge-bases, or `None` if there was no merge-base. | 
|  | 51 | +    pub merge_bases: Option<Vec<gix_hash::ObjectId>>, | 
|  | 52 | +    /// A list of virtual commits that were created to merge multiple merge-bases into one. | 
|  | 53 | +    /// As they are not reachable by anything they will be garbage collected, but knowing them provides options. | 
|  | 54 | +    pub virtual_merge_bases: Vec<gix_hash::ObjectId>, | 
|  | 55 | +} | 
|  | 56 | + | 
|  | 57 | +pub(super) mod function { | 
|  | 58 | +    use crate::blob::builtin_driver; | 
|  | 59 | +    use crate::commit::{Error, Options}; | 
|  | 60 | +    use crate::tree::UnresolvedConflict; | 
|  | 61 | +    use gix_object::FindExt; | 
|  | 62 | +    use std::borrow::Cow; | 
|  | 63 | + | 
|  | 64 | +    /// Like [`tree()`](crate::tree()), but it takes only two commits, `our_commit` and `their_commit` to automatically | 
|  | 65 | +    /// compute the merge-bases among them. | 
|  | 66 | +    /// If there are multiple merge bases, these will be auto-merged into one, recursively, if | 
|  | 67 | +    /// [`allow_missing_merge_base`](Options::allow_missing_merge_base) is `true`. | 
|  | 68 | +    /// | 
|  | 69 | +    /// `labels` are names where [`current`](crate::blob::builtin_driver::text::Labels::current) is a name for `our_commit` | 
|  | 70 | +    /// and [`other`](crate::blob::builtin_driver::text::Labels::other) is a name for `their_commit`. | 
|  | 71 | +    /// If [`ancestor`](crate::blob::builtin_driver::text::Labels::ancestor) is unset, it will be set by us based on the | 
|  | 72 | +    /// merge-bases of `our_commit` and `their_commit`. | 
|  | 73 | +    /// | 
|  | 74 | +    /// The `graph` is used to find the merge-base between `our_commit` and `their_commit`, and can also act as cache | 
|  | 75 | +    /// to speed up subsequent merge-base queries. | 
|  | 76 | +    /// | 
|  | 77 | +    /// Use `abbreviate_hash(id)` to shorten the given `id` according to standard git shortening rules. It's used in case | 
|  | 78 | +    /// the ancestor-label isn't explicitly set so that the merge base label becomes the shortened `id`. | 
|  | 79 | +    /// Note that it's a dyn closure only to make it possible to recursively call this function in case of multiple merge-bases. | 
|  | 80 | +    /// | 
|  | 81 | +    /// `write_object` is used only if it's allowed to merge multiple merge-bases into one, and if there | 
|  | 82 | +    /// are multiple merge bases, and to write merged buffers as blobs. | 
|  | 83 | +    /// | 
|  | 84 | +    /// ### Performance | 
|  | 85 | +    /// | 
|  | 86 | +    /// Note that `objects` *should* have an object cache to greatly accelerate tree-retrieval. | 
|  | 87 | +    /// | 
|  | 88 | +    /// ### Notes | 
|  | 89 | +    /// | 
|  | 90 | +    /// When merging merge-bases recursively, the options are adjusted automatically to act like Git, i.e. merge binary | 
|  | 91 | +    /// blobs and resolve with *ours*, while resorting to using the base/ancestor in case of unresolvable conflicts. | 
|  | 92 | +    /// | 
|  | 93 | +    /// ### Deviation | 
|  | 94 | +    /// | 
|  | 95 | +    /// * It's known that certain conflicts around symbolic links can be auto-resolved. We don't have an option for this | 
|  | 96 | +    ///   at all, yet, primarily as Git seems to not implement the *ours*/*theirs* choice in other places even though it | 
|  | 97 | +    ///   reasonably could. So we leave it to the caller to continue processing the returned tree at will. | 
|  | 98 | +    #[allow(clippy::too_many_arguments)] | 
|  | 99 | +    pub fn commit<'objects>( | 
|  | 100 | +        our_commit: gix_hash::ObjectId, | 
|  | 101 | +        their_commit: gix_hash::ObjectId, | 
|  | 102 | +        labels: builtin_driver::text::Labels<'_>, | 
|  | 103 | +        graph: &mut gix_revwalk::Graph<'_, '_, gix_revwalk::graph::Commit<gix_revision::merge_base::Flags>>, | 
|  | 104 | +        diff_resource_cache: &mut gix_diff::blob::Platform, | 
|  | 105 | +        blob_merge: &mut crate::blob::Platform, | 
|  | 106 | +        objects: &'objects (impl gix_object::FindObjectOrHeader + gix_object::Write), | 
|  | 107 | +        abbreviate_hash: &mut dyn FnMut(&gix_hash::oid) -> String, | 
|  | 108 | +        options: Options, | 
|  | 109 | +    ) -> Result<super::Outcome<'objects>, Error> { | 
|  | 110 | +        let merge_bases = gix_revision::merge_base(our_commit, &[their_commit], graph)?; | 
|  | 111 | +        let mut virtual_merge_bases = Vec::new(); | 
|  | 112 | +        let mut state = gix_diff::tree::State::default(); | 
|  | 113 | +        let mut commit_to_tree = | 
|  | 114 | +            |commit_id: gix_hash::ObjectId| objects.find_commit(&commit_id, &mut state.buf1).map(|c| c.tree()); | 
|  | 115 | + | 
|  | 116 | +        let (merge_base_tree_id, ancestor_name): (_, Cow<'_, str>) = match merge_bases.clone() { | 
|  | 117 | +            Some(base_commit) if base_commit.len() == 1 => { | 
|  | 118 | +                (commit_to_tree(base_commit[0])?, abbreviate_hash(&base_commit[0]).into()) | 
|  | 119 | +            } | 
|  | 120 | +            Some(mut base_commits) => { | 
|  | 121 | +                let virtual_base_tree = if options.use_first_merge_base { | 
|  | 122 | +                    let first = *base_commits.first().expect("if Some() there is at least one."); | 
|  | 123 | +                    commit_to_tree(first)? | 
|  | 124 | +                } else { | 
|  | 125 | +                    let mut merged_commit_id = base_commits.pop().expect("at least one base"); | 
|  | 126 | +                    let mut options = options.clone(); | 
|  | 127 | +                    options.tree_merge.allow_lossy_resolution = true; | 
|  | 128 | +                    options.tree_merge.blob_merge.is_virtual_ancestor = true; | 
|  | 129 | +                    options.tree_merge.blob_merge.text.conflict = builtin_driver::text::Conflict::ResolveWithOurs; | 
|  | 130 | +                    let favor_ancestor = Some(builtin_driver::binary::ResolveWith::Ancestor); | 
|  | 131 | +                    options.tree_merge.blob_merge.resolve_binary_with = favor_ancestor; | 
|  | 132 | +                    options.tree_merge.symlink_conflicts = favor_ancestor; | 
|  | 133 | +                    let labels = builtin_driver::text::Labels { | 
|  | 134 | +                        current: Some("Temporary merge branch 1".into()), | 
|  | 135 | +                        other: Some("Temporary merge branch 2".into()), | 
|  | 136 | +                        ..labels | 
|  | 137 | +                    }; | 
|  | 138 | +                    while let Some(next_commit_id) = base_commits.pop() { | 
|  | 139 | +                        options.tree_merge.marker_size_multiplier += 1; | 
|  | 140 | +                        let mut out = commit( | 
|  | 141 | +                            merged_commit_id, | 
|  | 142 | +                            next_commit_id, | 
|  | 143 | +                            labels, | 
|  | 144 | +                            graph, | 
|  | 145 | +                            diff_resource_cache, | 
|  | 146 | +                            blob_merge, | 
|  | 147 | +                            objects, | 
|  | 148 | +                            abbreviate_hash, | 
|  | 149 | +                            options.clone(), | 
|  | 150 | +                        )?; | 
|  | 151 | +                        // This shouldn't happen, but if for some buggy reason it does, we rather bail. | 
|  | 152 | +                        if out | 
|  | 153 | +                            .tree_merge | 
|  | 154 | +                            .has_unresolved_conflicts(UnresolvedConflict::ConflictMarkers) | 
|  | 155 | +                        { | 
|  | 156 | +                            return Err(Error::VirtualMergeBaseConflict); | 
|  | 157 | +                        } | 
|  | 158 | +                        let merged_tree_id = out | 
|  | 159 | +                            .tree_merge | 
|  | 160 | +                            .tree | 
|  | 161 | +                            .write(|tree| objects.write(tree)) | 
|  | 162 | +                            .map_err(Error::WriteObject)?; | 
|  | 163 | + | 
|  | 164 | +                        merged_commit_id = | 
|  | 165 | +                            create_virtual_commit(objects, merged_commit_id, next_commit_id, merged_tree_id)?; | 
|  | 166 | + | 
|  | 167 | +                        virtual_merge_bases.extend(out.virtual_merge_bases); | 
|  | 168 | +                        virtual_merge_bases.push(merged_commit_id); | 
|  | 169 | +                    } | 
|  | 170 | +                    commit_to_tree(merged_commit_id)? | 
|  | 171 | +                }; | 
|  | 172 | +                (virtual_base_tree, "merged common ancestors".into()) | 
|  | 173 | +            } | 
|  | 174 | +            None => { | 
|  | 175 | +                if options.allow_missing_merge_base { | 
|  | 176 | +                    (gix_hash::ObjectId::empty_tree(our_commit.kind()), "empty tree".into()) | 
|  | 177 | +                } else { | 
|  | 178 | +                    return Err(Error::NoMergeBase { | 
|  | 179 | +                        our_commit_id: our_commit, | 
|  | 180 | +                        their_commit_id: their_commit, | 
|  | 181 | +                    }); | 
|  | 182 | +                } | 
|  | 183 | +            } | 
|  | 184 | +        }; | 
|  | 185 | + | 
|  | 186 | +        let mut labels = labels; // TODO(borrowchk): this re-assignment shouldn't be needed. | 
|  | 187 | +        if labels.ancestor.is_none() { | 
|  | 188 | +            labels.ancestor = Some(ancestor_name.as_ref().into()); | 
|  | 189 | +        } | 
|  | 190 | + | 
|  | 191 | +        let our_tree_id = objects.find_commit(&our_commit, &mut state.buf1)?.tree(); | 
|  | 192 | +        let their_tree_id = objects.find_commit(&their_commit, &mut state.buf1)?.tree(); | 
|  | 193 | + | 
|  | 194 | +        let outcome = crate::tree( | 
|  | 195 | +            &merge_base_tree_id, | 
|  | 196 | +            &our_tree_id, | 
|  | 197 | +            &their_tree_id, | 
|  | 198 | +            labels, | 
|  | 199 | +            objects, | 
|  | 200 | +            |buf| objects.write_buf(gix_object::Kind::Blob, buf), | 
|  | 201 | +            &mut state, | 
|  | 202 | +            diff_resource_cache, | 
|  | 203 | +            blob_merge, | 
|  | 204 | +            options.tree_merge, | 
|  | 205 | +        )?; | 
|  | 206 | + | 
|  | 207 | +        Ok(super::Outcome { | 
|  | 208 | +            tree_merge: outcome, | 
|  | 209 | +            merge_bases, | 
|  | 210 | +            merge_base_tree_id, | 
|  | 211 | +            virtual_merge_bases, | 
|  | 212 | +        }) | 
|  | 213 | +    } | 
|  | 214 | + | 
|  | 215 | +    fn create_virtual_commit( | 
|  | 216 | +        objects: &(impl gix_object::Find + gix_object::Write), | 
|  | 217 | +        parent_a: gix_hash::ObjectId, | 
|  | 218 | +        parent_b: gix_hash::ObjectId, | 
|  | 219 | +        tree_id: gix_hash::ObjectId, | 
|  | 220 | +    ) -> Result<gix_hash::ObjectId, Error> { | 
|  | 221 | +        let mut buf = Vec::new(); | 
|  | 222 | +        let mut commit: gix_object::Commit = objects.find_commit(&parent_a, &mut buf)?.into(); | 
|  | 223 | +        commit.parents = vec![parent_a, parent_b].into(); | 
|  | 224 | +        commit.tree = tree_id; | 
|  | 225 | +        objects.write(&commit).map_err(Error::WriteObject) | 
|  | 226 | +    } | 
|  | 227 | +} | 
0 commit comments