Skip to content

Commit 01b072c

Browse files
committed
Add baseline tests for tree-merges along with first plain merge.
That way, Git can indicate what we need to match, and we have enough infrastructure to perform simple merges (seemingly) correctly.
1 parent 399fea9 commit 01b072c

File tree

10 files changed

+788
-16
lines changed

10 files changed

+788
-16
lines changed

Cargo.lock

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gix-merge/Cargo.toml

+13-12
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,34 @@ workspace = true
1515
doctest = false
1616

1717
[features]
18-
default = ["blob"]
19-
## Enable diffing of blobs using imara-diff, which also allows for a generic rewrite tracking implementation.
20-
blob = ["dep:imara-diff", "dep:gix-filter", "dep:gix-worktree", "dep:gix-path", "dep:gix-fs", "dep:gix-command", "dep:gix-tempfile", "dep:gix-trace", "dep:gix-quote"]
2118
## Data structures implement `serde::Serialize` and `serde::Deserialize`.
2219
serde = ["dep:serde", "gix-hash/serde", "gix-object/serde"]
2320

2421
[dependencies]
2522
gix-hash = { version = "^0.14.2", path = "../gix-hash" }
2623
gix-object = { version = "^0.44.0", path = "../gix-object" }
27-
gix-filter = { version = "^0.13.0", path = "../gix-filter", optional = true }
28-
gix-worktree = { version = "^0.36.0", path = "../gix-worktree", default-features = false, features = ["attributes"], optional = true }
29-
gix-command = { version = "^0.3.9", path = "../gix-command", optional = true }
30-
gix-path = { version = "^0.10.11", path = "../gix-path", optional = true }
31-
gix-fs = { version = "^0.11.3", path = "../gix-fs", optional = true }
32-
gix-tempfile = { version = "^14.0.0", path = "../gix-tempfile", optional = true }
33-
gix-trace = { version = "^0.1.10", path = "../gix-trace", optional = true }
34-
gix-quote = { version = "^0.4.12", path = "../gix-quote", optional = true }
24+
gix-filter = { version = "^0.13.0", path = "../gix-filter" }
25+
gix-worktree = { version = "^0.36.0", path = "../gix-worktree", default-features = false, features = ["attributes"] }
26+
gix-command = { version = "^0.3.9", path = "../gix-command" }
27+
gix-path = { version = "^0.10.11", path = "../gix-path" }
28+
gix-fs = { version = "^0.11.3", path = "../gix-fs" }
29+
gix-tempfile = { version = "^14.0.0", path = "../gix-tempfile" }
30+
gix-trace = { version = "^0.1.10", path = "../gix-trace" }
31+
gix-quote = { version = "^0.4.12", path = "../gix-quote" }
32+
gix-revision = { version = "^0.29.0", path = "../gix-revision", default-features = false, features = ["merge_base"] }
33+
gix-revwalk = { version = "^0.15.0", path = "../gix-revwalk" }
34+
gix-diff = { version = "^0.46.0", path = "../gix-diff", default-features = false, features = ["blob"] }
3535

3636
thiserror = "1.0.63"
37-
imara-diff = { version = "0.1.7", optional = true }
37+
imara-diff = { version = "0.1.7" }
3838
bstr = { version = "1.5.0", default-features = false }
3939
serde = { version = "1.0.114", optional = true, default-features = false, features = ["derive"] }
4040

4141
document-features = { version = "0.2.0", optional = true }
4242

4343
[dev-dependencies]
4444
gix-testtools = { path = "../tests/tools" }
45+
gix-odb = { path = "../gix-odb" }
4546
pretty_assertions = "1.4.0"
4647

4748
[package.metadata.docs.rs]

gix-merge/src/commit.rs

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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("No common ancestor between {our_commit_id} and {their_commit_id}")]
10+
NoMergeBase {
11+
/// The commit on our side that was to be merged.
12+
our_commit_id: gix_hash::ObjectId,
13+
/// The commit on their side that was to be merged.
14+
their_commit_id: gix_hash::ObjectId,
15+
},
16+
#[error("Could not find ancestor, our or their commit to extract tree from")]
17+
FindCommit(#[from] gix_object::find::existing_object::Error),
18+
}
19+
20+
/// A way to configure [`commit()`](crate::commit()).
21+
#[derive(Default, Debug, Copy, Clone)]
22+
pub struct Options {
23+
/// If `true`, merging unrelated commits is allowed, with the merge-base being assumed as empty tree.
24+
pub allow_missing_merge_base: bool,
25+
/// Options to define how trees should be merged.
26+
pub tree_merge: crate::tree::Options,
27+
/// Options to define how to merge blobs.
28+
///
29+
/// Note that these are temporarily overwritten if multiple merge-bases are merged into one.
30+
pub blob_merge: crate::blob::platform::merge::Options,
31+
}
32+
33+
pub(super) mod function {
34+
use crate::commit::{Error, Options};
35+
use gix_object::FindExt;
36+
37+
/// Like [`tree()`](crate::tree()), but it takes only two commits, `our_commit` and `their_commit` to automatically
38+
/// compute the merge-bases among them.
39+
/// If there are multiple merge bases, these will be auto-merged into one, recursively, if
40+
/// [`allow_missing_merge_base`](Options::allow_missing_merge_base) is `true`.
41+
///
42+
/// `labels` are names where [`current`](crate::blob::builtin_driver::text::Labels::current) is a name for `our_commit`
43+
/// and [`other`](crate::blob::builtin_driver::text::Labels::other) is a name for `their_commit`.
44+
/// If [`ancestor`](crate::blob::builtin_driver::text::Labels::ancestor) is unset, it will be set by us based on the
45+
/// merge-bases of `our_commit` and `their_commit`.
46+
///
47+
/// The `graph` is used to find the merge-base between `our_commit` and `their_commit`, and can also act as cache
48+
/// to speed up subsequent merge-base queries.
49+
///
50+
/// ### Performance
51+
///
52+
/// Note that `objects` *should* have an object cache to greatly accelerate tree-retrieval.
53+
#[allow(clippy::too_many_arguments)]
54+
pub fn commit<'objects>(
55+
our_commit: gix_hash::ObjectId,
56+
their_commit: gix_hash::ObjectId,
57+
mut labels: crate::blob::builtin_driver::text::Labels<'_>,
58+
graph: &mut gix_revwalk::Graph<'_, '_, gix_revwalk::graph::Commit<gix_revision::merge_base::Flags>>,
59+
diff_resource_cache: &mut gix_diff::blob::Platform,
60+
blob_merge: &mut crate::blob::Platform,
61+
objects: &'objects impl gix_object::FindObjectOrHeader,
62+
options: Options,
63+
) -> Result<crate::tree::Outcome<'objects>, Error> {
64+
let merge_bases_commit_ids = gix_revision::merge_base(our_commit, &[their_commit], graph)?;
65+
let (merge_base_commit_id, ancestor_name) = match merge_bases_commit_ids {
66+
Some(base_commit) if base_commit.len() == 1 => (base_commit[0], None),
67+
Some(base_commits) => {
68+
let virtual_base_tree = *base_commits.first().expect("TODO: merge multiple bases into one");
69+
(virtual_base_tree, Some("merged common ancestors".into()))
70+
}
71+
None => {
72+
if options.allow_missing_merge_base {
73+
(
74+
gix_hash::ObjectId::empty_tree(our_commit.kind()),
75+
Some("empty tree".into()),
76+
)
77+
} else {
78+
return Err(Error::NoMergeBase {
79+
our_commit_id: our_commit,
80+
their_commit_id: their_commit,
81+
});
82+
}
83+
}
84+
};
85+
if labels.ancestor.is_none() {
86+
labels.ancestor = ancestor_name;
87+
}
88+
89+
let mut state = gix_diff::tree::State::default();
90+
let merge_base_tree_id = objects.find_commit(&merge_base_commit_id, &mut state.buf1)?.tree();
91+
let our_tree_id = objects.find_commit(&our_commit, &mut state.buf1)?.tree();
92+
let their_tree_id = objects.find_commit(&their_commit, &mut state.buf1)?.tree();
93+
94+
Ok(crate::tree(
95+
&merge_base_tree_id,
96+
&our_tree_id,
97+
&their_tree_id,
98+
labels,
99+
objects,
100+
&mut state,
101+
diff_resource_cache,
102+
blob_merge,
103+
options.tree_merge,
104+
)?)
105+
}
106+
}

gix-merge/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
#![forbid(unsafe_code)]
33

44
///
5-
#[cfg(feature = "blob")]
65
pub mod blob;
6+
///
7+
pub mod commit;
8+
pub use commit::function::commit;
9+
///
10+
pub mod tree;
11+
pub use tree::function::tree;

0 commit comments

Comments
 (0)