Skip to content

Commit 29040a8

Browse files
authored
Merge pull request GitoxideLabs#2433 from GitoxideLabs/codex/nonempty-rewrite
Use nonempty where possible
2 parents 228caf7 + e033441 commit 29040a8

File tree

26 files changed

+109
-73
lines changed

26 files changed

+109
-73
lines changed

Cargo.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gix-commitgraph/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ gix-error = { version = "^0.1.0", path = "../gix-error" }
2626

2727
bstr = { version = "1.12.0", default-features = false, features = ["std"] }
2828
memmap2 = "0.9.7"
29+
nonempty = "0.12.0"
2930
serde = { version = "1.0.114", optional = true, default-features = false, features = ["derive"] }
3031

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

gix-commitgraph/src/access.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,8 @@ impl Graph {
1414
/// The kind of hash used in this `Graph`.
1515
///
1616
/// Note that it is always conforming to the hash used in the owning repository.
17-
///
18-
/// # Panics
19-
///
20-
/// If the graph does not contain any `File`.
2117
pub fn object_hash(&self) -> gix_hash::Kind {
22-
self.files
23-
.first()
24-
.map(super::File::object_hash)
25-
.expect("graph to have at least one file")
18+
self.files.first().object_hash()
2619
}
2720

2821
/// Returns the commit matching the given `id`.

gix-commitgraph/src/init.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,17 @@ impl Graph {
5454

5555
/// Create a new commit graph from a list of `files`.
5656
pub fn new(files: Vec<File>) -> Result<Self, Message> {
57+
let files = nonempty::NonEmpty::from_vec(files)
58+
.ok_or_else(|| message!("Commit-graph must contain at least one file"))?;
5759
let num_commits: u64 = files.iter().map(|f| u64::from(f.num_commits())).sum();
5860
if num_commits > u64::from(MAX_COMMITS) {
5961
return Err(message!(
6062
"Commit-graph files contain {num_commits} commits altogether, but only {MAX_COMMITS} commits are allowed"
6163
));
6264
}
6365

64-
for window in files.windows(2) {
65-
let f1 = &window[0];
66-
let f2 = &window[1];
66+
let mut f1 = files.first();
67+
for f2 in files.tail() {
6768
if f1.object_hash() != f2.object_hash() {
6869
return Err(message!(
6970
"Commit-graph files mismatch: '{path1}' uses hash {hash1:?}, but '{path2}' uses hash {hash2:?}",
@@ -73,6 +74,7 @@ impl Graph {
7374
hash2 = f2.object_hash(),
7475
));
7576
}
77+
f1 = f2;
7678
}
7779

7880
Ok(Self { files })

gix-commitgraph/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub struct File {
4141
/// may come from one or more `objects/info/commit-graphs/graph-*.graph` files. These files are
4242
/// generated via `git commit-graph write ...` commands.
4343
pub struct Graph {
44-
files: Vec<File>,
44+
files: nonempty::NonEmpty<File>,
4545
}
4646

4747
/// Instantiate a commit graph from an `.git/objects/info` directory, or one of the various commit-graph files.

gix-commitgraph/src/verify.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ impl Graph {
6969
.raise());
7070
}
7171

72-
for (base_graph_index, (expected, actual)) in self.files[..file_index]
72+
for (base_graph_index, (expected, actual)) in self
73+
.files
7374
.iter()
75+
.take(file_index)
7476
.map(crate::File::checksum)
7577
.zip(file.iter_base_graph_ids())
7678
.enumerate()

gix-merge/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ gix-index = { version = "^0.47.0", path = "../gix-index" }
3737
thiserror = "2.0.18"
3838
imara-diff = { version = "0.1.8" }
3939
bstr = { version = "1.12.0", default-features = false }
40+
nonempty = "0.12.0"
4041
serde = { version = "1.0.114", optional = true, default-features = false, features = ["derive"] }
4142

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

gix-merge/src/commit/function.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ pub fn commit<'objects>(
6060
|commit_id: gix_hash::ObjectId| objects.find_commit(&commit_id, &mut state.buf1).map(|c| c.tree());
6161

6262
let (merge_base_tree_id, ancestor_name): (_, Cow<'_, str>) = match merge_bases.clone() {
63-
Some(base_commit) if base_commit.len() == 1 => {
64-
(commit_to_tree(base_commit[0])?, abbreviate_hash(&base_commit[0]).into())
65-
}
66-
Some(mut base_commits) => {
63+
Some(base_commit) if base_commit.len() == 1 => (
64+
commit_to_tree(*base_commit.first())?,
65+
abbreviate_hash(base_commit.first()).into(),
66+
),
67+
Some(base_commits) => {
6768
let virtual_base_tree = if options.use_first_merge_base {
68-
let first = base_commits.first().expect("if Some() there is at least one.");
69-
commit_to_tree(*first)?
69+
commit_to_tree(*base_commits.first())?
7070
} else {
71+
let mut base_commits: Vec<_> = base_commits.into();
7172
let first = base_commits.pop().expect("at least two");
7273
let second = base_commits.pop().expect("at least one left");
7374
let out = crate::commit::virtual_merge_base(
@@ -81,7 +82,7 @@ pub fn commit<'objects>(
8182
abbreviate_hash,
8283
options.tree_merge.clone(),
8384
)?;
84-
virtual_merge_bases = out.virtual_merge_bases;
85+
virtual_merge_bases = Vec::from(out.virtual_merge_bases);
8586
out.tree_id
8687
};
8788
(virtual_base_tree, "merged common ancestors".into())

gix-merge/src/commit/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub struct Outcome<'a> {
4444
/// * an empty tree, if [`allow_missing_merge_base`](Options::allow_missing_merge_base) is enabled.
4545
pub merge_base_tree_id: gix_hash::ObjectId,
4646
/// The object ids of all the commits which were found to be merge-bases, or `None` if there was no merge-base.
47-
pub merge_bases: Option<Vec<gix_hash::ObjectId>>,
47+
pub merge_bases: Option<nonempty::NonEmpty<gix_hash::ObjectId>>,
4848
/// A list of virtual commits that were created to merge multiple merge-bases into one, the last one being
4949
/// the one we used as merge-base for the merge.
5050
/// As they are not reachable by anything they will be garbage collected, but knowing them provides options.

gix-merge/src/commit/virtual_merge_base.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub struct Outcome {
44
/// As they have been written to the object database, they are still available until they are garbage collected.
55
/// The last one is the most recently produced and the one returned as `commit_id`.
66
/// This is never empty.
7-
pub virtual_merge_bases: Vec<gix_hash::ObjectId>,
7+
pub virtual_merge_bases: nonempty::NonEmpty<gix_hash::ObjectId>,
88
/// The id of the commit that was created to hold the merged tree.
99
pub commit_id: gix_hash::ObjectId,
1010
/// The hash of the merged tree.
@@ -111,7 +111,8 @@ pub(super) mod function {
111111
}
112112

113113
Ok(super::Outcome {
114-
virtual_merge_bases,
114+
virtual_merge_bases: nonempty::NonEmpty::from_vec(virtual_merge_bases)
115+
.expect("the virtual merge-base process always creates at least one commit"),
115116
commit_id: merged_commit_id,
116117
tree_id: tree_id.map_or_else(
117118
|| {

0 commit comments

Comments
 (0)