Skip to content

Commit ecf4848

Browse files
codexByron
andcommitted
testsupport: drop test-only git2 repo extension methods
Co-authored-by: Sebastian Thiel <sebastian.thiel@icloud.com>
1 parent f216f1f commit ecf4848

5 files changed

Lines changed: 94 additions & 115 deletions

File tree

crates/but-testsupport/src/legacy/mod.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
use but_core::commit::Headers;
12
use but_ctx::Context;
2-
use but_oxidize::OidExt;
3+
use but_oxidize::{ObjectIdExt as _, OidExt, git2_signature_to_gix_signature};
34
use but_workspace::{legacy::StacksFilter, ui::StackDetails};
5+
use gitbutler_reference::Refname;
46
use gitbutler_stack::StackId;
5-
use gix::prelude::ObjectIdExt;
7+
use gix::prelude::ObjectIdExt as _;
68

79
pub const VAR_NO_CLEANUP: &str = "GITBUTLER_TESTS_NO_CLEANUP";
810

@@ -72,6 +74,52 @@ pub fn init_opts_bare() -> git2::RepositoryInitOptions {
7274
opts
7375
}
7476

77+
#[expect(clippy::too_many_arguments)]
78+
pub(crate) fn commit_with_signature(
79+
repo: &git2::Repository,
80+
update_ref: Option<&Refname>,
81+
author: &git2::Signature<'_>,
82+
committer: &git2::Signature<'_>,
83+
message: &str,
84+
tree: &git2::Tree<'_>,
85+
parents: &[&git2::Commit<'_>],
86+
commit_headers: Option<Headers>,
87+
) -> anyhow::Result<git2::Oid> {
88+
let repo_gix = crate::open_repo(repo.path())?;
89+
gitbutler_repo::commit_with_signature_gix(
90+
&repo_gix,
91+
update_ref,
92+
git2_signature_to_gix_signature(author),
93+
git2_signature_to_gix_signature(committer),
94+
message.as_bytes().into(),
95+
tree.id().to_gix(),
96+
&parents
97+
.iter()
98+
.map(|commit| commit.id().to_gix())
99+
.collect::<Vec<_>>(),
100+
commit_headers,
101+
)
102+
.map(|oid| oid.to_git2())
103+
}
104+
105+
pub(crate) fn maybe_find_branch_by_refname<'repo>(
106+
repo: &'repo git2::Repository,
107+
name: &Refname,
108+
) -> anyhow::Result<Option<git2::Branch<'repo>>> {
109+
let branch = repo.find_branch(
110+
&name.simple_name(),
111+
match name {
112+
Refname::Virtual(_) | Refname::Local(_) | Refname::Other(_) => git2::BranchType::Local,
113+
Refname::Remote(_) => git2::BranchType::Remote,
114+
},
115+
);
116+
match branch {
117+
Ok(branch) => Ok(Some(branch)),
118+
Err(err) if err.code() == git2::ErrorCode::NotFound => Ok(None),
119+
Err(err) => Err(err.into()),
120+
}
121+
}
122+
75123
pub fn visualize_git2_tree(tree_id: git2::Oid, repo: &git2::Repository) -> termtree::Tree<String> {
76124
let repo = gix::open_opts(repo.path(), gix::open::Options::isolated()).unwrap();
77125
crate::visualize_tree(tree_id.to_gix().attach(&repo))

crates/but-testsupport/src/legacy/suite.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ use std::{
66

77
use but_ctx::{Context, RepoOpenMode};
88
use but_settings::AppSettings;
9-
use gitbutler_repo::RepositoryExt;
109
use tempfile::{TempDir, tempdir};
1110

12-
use super::{VAR_NO_CLEANUP, init_opts, init_opts_bare, test_project::setup_config};
11+
use super::{
12+
VAR_NO_CLEANUP, commit_with_signature, init_opts, init_opts_bare, test_project::setup_config,
13+
};
1314

1415
pub struct Suite {
1516
pub local_app_data: Option<TempDir>,
@@ -153,17 +154,17 @@ pub fn test_repository() -> (git2::Repository, TempDir) {
153154
let mut index = git2_repo.index().expect("failed to get index");
154155
let oid = index.write_tree().expect("failed to write tree");
155156
let signature = git2::Signature::now("test", "test@email.com").unwrap();
156-
git2_repo
157-
.commit_with_signature(
158-
Some(&"refs/heads/master".parse().unwrap()),
159-
&signature,
160-
&signature,
161-
"Initial commit",
162-
&git2_repo.find_tree(oid).expect("failed to find tree"),
163-
&[],
164-
None,
165-
)
166-
.expect("failed to commit");
157+
commit_with_signature(
158+
&git2_repo,
159+
Some(&"refs/heads/master".parse().unwrap()),
160+
&signature,
161+
&signature,
162+
"Initial commit",
163+
&git2_repo.find_tree(oid).expect("failed to find tree"),
164+
&[],
165+
None,
166+
)
167+
.expect("failed to commit");
167168
(git2_repo, tmp)
168169
}
169170

@@ -176,9 +177,8 @@ pub fn commit_all(repository: &git2::Repository) -> git2::Oid {
176177
let oid = index.write_tree().expect("failed to write tree");
177178
let signature = git2::Signature::now("test", "test@email.com").unwrap();
178179
let head = repository.head().expect("failed to get head");
179-
let repo: &git2::Repository = repository;
180-
181-
repo.commit_with_signature(
180+
commit_with_signature(
181+
repository,
182182
Some(&head.name().map(|name| name.parse().unwrap()).unwrap()),
183183
&signature,
184184
&signature,

crates/but-testsupport/src/legacy/test_project.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use gitbutler_reference::{LocalRefname, Refname};
55
use gitbutler_repo::RepositoryExt;
66
use tempfile::TempDir;
77

8-
use super::{VAR_NO_CLEANUP, init_opts};
8+
use super::{VAR_NO_CLEANUP, commit_with_signature, init_opts, maybe_find_branch_by_refname};
99

1010
pub fn temp_dir() -> TempDir {
1111
tempfile::tempdir().unwrap()
@@ -36,8 +36,8 @@ impl Default for TestProject {
3636
let mut index = local_repository.index().expect("failed to get index");
3737
let oid = index.write_tree().expect("failed to write tree");
3838
let signature = git2::Signature::now("test", "test@email.com").unwrap();
39-
let repo: &git2::Repository = &local_repository;
40-
repo.commit_with_signature(
39+
commit_with_signature(
40+
&local_repository,
4141
Some(&"refs/heads/master".parse().unwrap()),
4242
&signature,
4343
&signature,
@@ -137,17 +137,12 @@ impl TestProject {
137137
Refname::Remote(remote) => format!("refs/heads/{}", remote.branch()).parse().unwrap(),
138138
_ => "INVALID".parse().unwrap(),
139139
};
140-
let branch = self
141-
.remote_repo
142-
.maybe_find_branch_by_refname(&branch_name)
143-
.unwrap();
140+
let branch = maybe_find_branch_by_refname(&self.remote_repo, &branch_name).unwrap();
144141
let branch_commit = branch.unwrap().get().peel_to_commit().unwrap();
145142

146143
let master_branch = {
147144
let name: Refname = "refs/heads/master".parse().unwrap();
148-
self.remote_repo
149-
.maybe_find_branch_by_refname(&name)
150-
.unwrap()
145+
maybe_find_branch_by_refname(&self.remote_repo, &name).unwrap()
151146
};
152147
let master_branch_commit = master_branch.unwrap().get().peel_to_commit().unwrap();
153148

@@ -213,15 +208,13 @@ impl TestProject {
213208
Refname::Remote(remote) => format!("refs/heads/{}", remote.branch()).parse()?,
214209
_ => "INVALID".parse()?,
215210
};
216-
let branch = self
217-
.remote_repo
218-
.maybe_find_branch_by_refname(&branch_name)?
219-
.expect("branch exists");
211+
let branch =
212+
maybe_find_branch_by_refname(&self.remote_repo, &branch_name)?.expect("branch exists");
220213
let branch_commit = branch.get().peel_to_commit()?;
221214

222215
let master_branch = {
223216
let name: Refname = "refs/heads/master".parse()?;
224-
self.remote_repo.maybe_find_branch_by_refname(&name)?
217+
maybe_find_branch_by_refname(&self.remote_repo, &name)?
225218
};
226219
let master_branch_commit = master_branch
227220
.as_ref()
@@ -248,8 +241,8 @@ impl TestProject {
248241
self.remote_repo.find_tree(tree_id.to_git2())?
249242
};
250243

251-
let repo: &git2::Repository = &self.remote_repo;
252-
repo.commit_with_signature(
244+
commit_with_signature(
245+
&self.remote_repo,
253246
Some(&"refs/heads/master".parse()?),
254247
&branch_commit.author(),
255248
&branch_commit.committer(),
@@ -280,7 +273,7 @@ impl TestProject {
280273
pub fn checkout(&self, branch: &LocalRefname) {
281274
let refname: Refname = branch.into();
282275
let head_commit = self.local_repo.head().unwrap().peel_to_commit().unwrap();
283-
let tree = match self.local_repo.maybe_find_branch_by_refname(&refname) {
276+
let tree = match maybe_find_branch_by_refname(&self.local_repo, &refname) {
284277
Ok(branch) => match branch {
285278
Some(branch) => branch.get().peel_to_tree().unwrap(),
286279
None => {
@@ -310,8 +303,8 @@ impl TestProject {
310303
let oid = index.write_tree().expect("failed to write tree");
311304
let signature = git2::Signature::now("test", "test@email.com").unwrap();
312305
let refname: Refname = head.name().unwrap().parse().unwrap();
313-
let repo: &git2::Repository = &self.local_repo;
314-
repo.commit_with_signature(
306+
commit_with_signature(
307+
&self.local_repo,
315308
Some(&refname),
316309
&signature,
317310
&signature,

crates/but-testsupport/src/legacy/testing_repository.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ use std::fs;
22

33
use but_core::{ChangeId, commit::Headers};
44
use but_oxidize::OidExt;
5-
use gitbutler_repo::RepositoryExt;
65
use gix_testtools::bstr::ByteSlice as _;
76
use tempfile::{TempDir, tempdir};
87
use uuid::Uuid;
98

10-
use super::init_opts;
9+
use super::{commit_with_signature, init_opts};
1110

1211
pub struct TestingRepository {
1312
pub repository: git2::Repository,
@@ -148,21 +147,20 @@ impl TestingRepository {
148147
conflicted: None,
149148
});
150149

151-
let commit = self
152-
.repository
153-
.commit_with_signature(
154-
None,
155-
&signature,
156-
&signature,
157-
message,
158-
&self
159-
.repository
160-
.find_tree(index.write_tree().unwrap())
161-
.unwrap(),
162-
parent.map(|c| vec![c]).unwrap_or_default().as_slice(),
163-
Some(commit_headers),
164-
)
165-
.unwrap();
150+
let commit = commit_with_signature(
151+
&self.repository,
152+
None,
153+
&signature,
154+
&signature,
155+
message,
156+
&self
157+
.repository
158+
.find_tree(index.write_tree().unwrap())
159+
.unwrap(),
160+
parent.map(|c| vec![c]).unwrap_or_default().as_slice(),
161+
Some(commit_headers),
162+
)
163+
.unwrap();
166164

167165
self.repository.find_commit(commit).unwrap()
168166
}

crates/gitbutler-repo/src/repository_ext.rs

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use std::str;
2-
31
use anyhow::{Context as _, Result, bail};
42
use bstr::BStr;
53
use but_core::{
64
RepositoryExt as RepositoryExtGix,
75
commit::{Headers, SignCommit},
86
};
9-
use but_oxidize::{ObjectIdExt as _, OidExt, git2_signature_to_gix_signature};
107
use gitbutler_reference::Refname;
118

129
/// Extension trait for `git2::Repository`.
@@ -22,19 +19,6 @@ pub trait RepositoryExt {
2219
/// gets merged.
2320
fn merge_base_octopussy(&self, ids: &[git2::Oid]) -> Result<git2::Oid>;
2421
fn checkout_tree_builder<'a>(&'a self, tree: &'a git2::Tree<'a>) -> CheckoutTreeBuidler<'a>;
25-
fn maybe_find_branch_by_refname(&self, name: &Refname) -> Result<Option<git2::Branch<'_>>>;
26-
27-
#[expect(clippy::too_many_arguments)]
28-
fn commit_with_signature(
29-
&self,
30-
update_ref: Option<&Refname>,
31-
author: &git2::Signature<'_>,
32-
committer: &git2::Signature<'_>,
33-
message: &str,
34-
tree: &git2::Tree<'_>,
35-
parents: &[&git2::Commit<'_>],
36-
commit_headers: Option<Headers>,
37-
) -> Result<git2::Oid>;
3822
}
3923

4024
/// Create a commit with GitButler signing and trailer behavior using `gix`-native inputs.
@@ -134,50 +118,6 @@ impl RepositoryExt for git2::Repository {
134118
}
135119
}
136120

137-
fn maybe_find_branch_by_refname(&self, name: &Refname) -> Result<Option<git2::Branch<'_>>> {
138-
let branch = self.find_branch(
139-
&name.simple_name(),
140-
match name {
141-
Refname::Virtual(_) | Refname::Local(_) | Refname::Other(_) => {
142-
git2::BranchType::Local
143-
}
144-
Refname::Remote(_) => git2::BranchType::Remote,
145-
},
146-
);
147-
match branch {
148-
Ok(branch) => Ok(Some(branch)),
149-
Err(e) if e.code() == git2::ErrorCode::NotFound => Ok(None),
150-
Err(e) => Err(e.into()),
151-
}
152-
}
153-
154-
fn commit_with_signature(
155-
&self,
156-
update_ref: Option<&Refname>,
157-
author: &git2::Signature<'_>,
158-
committer: &git2::Signature<'_>,
159-
message: &str,
160-
tree: &git2::Tree<'_>,
161-
parents: &[&git2::Commit<'_>],
162-
commit_headers: Option<Headers>,
163-
) -> Result<git2::Oid> {
164-
let repo = gix::open(self.path())?;
165-
commit_with_signature_gix(
166-
&repo,
167-
update_ref,
168-
git2_signature_to_gix_signature(author),
169-
git2_signature_to_gix_signature(committer),
170-
message.into(),
171-
tree.id().to_gix(),
172-
&parents
173-
.iter()
174-
.map(|commit| commit.id().to_gix())
175-
.collect::<Vec<_>>(),
176-
commit_headers,
177-
)
178-
.map(|oid| oid.to_git2())
179-
}
180-
181121
fn merge_base_octopussy(&self, ids: &[git2::Oid]) -> Result<git2::Oid> {
182122
if ids.len() < 2 {
183123
bail!("Merge base octopussy requires at least two commit ids to operate on");

0 commit comments

Comments
 (0)