cli/tests: add gitoxide helpers

This commit is contained in:
Baltasar Dinis
2025-02-03 21:01:13 +00:00
committed by Baltasar Dinis
parent 769dbd8f5f
commit cd6141693b
4 changed files with 308 additions and 0 deletions

145
Cargo.lock generated
View File

@@ -1133,8 +1133,10 @@ dependencies = [
"gix-command",
"gix-commitgraph",
"gix-config",
"gix-credentials",
"gix-date",
"gix-diff",
"gix-dir",
"gix-discover",
"gix-features",
"gix-filter",
@@ -1145,11 +1147,13 @@ dependencies = [
"gix-ignore",
"gix-index",
"gix-lock",
"gix-negotiate",
"gix-object",
"gix-odb",
"gix-pack",
"gix-path",
"gix-pathspec",
"gix-prompt",
"gix-protocol",
"gix-ref",
"gix-refspec",
@@ -1157,14 +1161,17 @@ dependencies = [
"gix-revwalk",
"gix-sec",
"gix-shallow",
"gix-status",
"gix-submodule",
"gix-tempfile",
"gix-trace",
"gix-transport",
"gix-traverse",
"gix-url",
"gix-utils",
"gix-validate",
"gix-worktree",
"gix-worktree-state",
"once_cell",
"smallvec",
"thiserror 2.0.11",
@@ -1279,6 +1286,23 @@ dependencies = [
"thiserror 2.0.11",
]
[[package]]
name = "gix-credentials"
version = "0.27.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf950f9ee1690bb9c4388b5152baa8a9f41ad61e5cf1ba0ec8c207b08dab9e45"
dependencies = [
"bstr",
"gix-command",
"gix-config-value",
"gix-path",
"gix-prompt",
"gix-sec",
"gix-trace",
"gix-url",
"thiserror 2.0.11",
]
[[package]]
name = "gix-date"
version = "0.9.3"
@@ -1298,12 +1322,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62afb7f4ca0acdf4e9dad92065b2eb1bf2993bcc5014b57bc796e3a365b17c4d"
dependencies = [
"bstr",
"gix-attributes",
"gix-command",
"gix-filter",
"gix-fs",
"gix-hash",
"gix-index",
"gix-object",
"gix-path",
"gix-pathspec",
"gix-tempfile",
"gix-trace",
"gix-traverse",
@@ -1312,6 +1339,26 @@ dependencies = [
"thiserror 2.0.11",
]
[[package]]
name = "gix-dir"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1d78db3927a12f7d1b788047b84efacaab03ef25738bd1c77856ad8966bd57b"
dependencies = [
"bstr",
"gix-discover",
"gix-fs",
"gix-ignore",
"gix-index",
"gix-object",
"gix-path",
"gix-pathspec",
"gix-trace",
"gix-utils",
"gix-worktree",
"thiserror 2.0.11",
]
[[package]]
name = "gix-discover"
version = "0.38.0"
@@ -1467,6 +1514,22 @@ dependencies = [
"thiserror 2.0.11",
]
[[package]]
name = "gix-negotiate"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6a8af1ef7bbe303d30b55312b7f4d33e955de43a3642ae9b7347c623d80ef80"
dependencies = [
"bitflags 2.6.0",
"gix-commitgraph",
"gix-date",
"gix-hash",
"gix-object",
"gix-revwalk",
"smallvec",
"thiserror 2.0.11",
]
[[package]]
name = "gix-object"
version = "0.47.0"
@@ -1522,7 +1585,9 @@ dependencies = [
"gix-hashtable",
"gix-object",
"gix-path",
"gix-tempfile",
"memmap2 0.9.4",
"parking_lot",
"smallvec",
"thiserror 2.0.11",
"uluru",
@@ -1580,6 +1645,19 @@ dependencies = [
"thiserror 2.0.11",
]
[[package]]
name = "gix-prompt"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79f2185958e1512b989a007509df8d61dca014aa759a22bee80cfa6c594c3b6d"
dependencies = [
"gix-command",
"gix-config-value",
"parking_lot",
"rustix",
"thiserror 2.0.11",
]
[[package]]
name = "gix-protocol"
version = "0.48.0"
@@ -1587,11 +1665,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c61bd61afc6b67d213241e2100394c164be421e3f7228d3521b04f48ca5ba90"
dependencies = [
"bstr",
"gix-credentials",
"gix-date",
"gix-features",
"gix-hash",
"gix-lock",
"gix-negotiate",
"gix-object",
"gix-ref",
"gix-refspec",
"gix-revwalk",
"gix-shallow",
"gix-trace",
"gix-transport",
"gix-utils",
"maybe-async",
@@ -1699,6 +1784,29 @@ dependencies = [
"thiserror 2.0.11",
]
[[package]]
name = "gix-status"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "414cc1d85079d7ca32c3ab4a6479bf7e174cd251c74a82339c6cc393da3f4883"
dependencies = [
"bstr",
"filetime",
"gix-diff",
"gix-dir",
"gix-features",
"gix-filter",
"gix-fs",
"gix-hash",
"gix-index",
"gix-object",
"gix-path",
"gix-pathspec",
"gix-worktree",
"portable-atomic",
"thiserror 2.0.11",
]
[[package]]
name = "gix-submodule"
version = "0.17.0"
@@ -1787,6 +1895,7 @@ version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff08f24e03ac8916c478c8419d7d3c33393da9bb41fa4c24455d5406aeefd35f"
dependencies = [
"bstr",
"fastrand",
"unicode-normalization",
]
@@ -1820,6 +1929,26 @@ dependencies = [
"gix-validate",
]
[[package]]
name = "gix-worktree-state"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86f5e199ad5af972086683bd31d640c82cb85885515bf86d86236c73ce575bf0"
dependencies = [
"bstr",
"gix-features",
"gix-filter",
"gix-fs",
"gix-glob",
"gix-hash",
"gix-index",
"gix-object",
"gix-path",
"gix-worktree",
"io-close",
"thiserror 2.0.11",
]
[[package]]
name = "glob"
version = "0.3.2"
@@ -2136,6 +2265,16 @@ dependencies = [
"syn 2.0.98",
]
[[package]]
name = "io-close"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cadcf447f06744f8ce713d2d6239bb5bde2c357a452397a9ed90c625da390bc"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "is-terminal"
version = "0.4.13"
@@ -2994,6 +3133,12 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22686f4785f02a4fcc856d3b3bb19bf6c8160d103f7a99cc258bddd0251dc7f2"
[[package]]
name = "portable-atomic"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "280dc24453071f1b63954171985a0b0d30058d287960968b9b2aca264c8d4ee6"
[[package]]
name = "ppv-lite86"
version = "0.2.20"

View File

@@ -103,6 +103,12 @@ anyhow = { workspace = true }
assert_cmd = { workspace = true }
assert_matches = { workspace = true }
async-trait = { workspace = true }
gix = { workspace = true, features = [
"blocking-network-client",
"status",
"tree-editor",
"worktree-mutation",
] }
insta = { workspace = true }
test-case = { workspace = true }
testutils = { workspace = true }

156
cli/tests/common/git.rs Normal file
View File

@@ -0,0 +1,156 @@
// Copyright 2025 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![allow(unused)]
use std::path::Path;
use std::path::PathBuf;
pub const GIT_USER: &str = "Someone";
pub const GIT_EMAIL: &str = "someone@example.org";
fn git_config() -> Vec<bstr::BString> {
vec![
format!("user.name = {GIT_USER}").into(),
format!("user.email = {GIT_EMAIL}").into(),
"init.defaultBranch = master".into(),
]
}
fn open_options() -> gix::open::Options {
gix::open::Options::isolated().config_overrides(git_config())
}
pub fn open(directory: impl Into<PathBuf>) -> gix::Repository {
gix::open_opts(directory, open_options()).unwrap()
}
pub fn init(directory: impl AsRef<Path>) -> gix::Repository {
gix::ThreadSafeRepository::init_opts(
directory,
gix::create::Kind::WithWorktree,
gix::create::Options::default(),
open_options(),
)
.unwrap()
.to_thread_local()
}
pub fn init_bare(directory: impl AsRef<Path>) -> gix::Repository {
gix::ThreadSafeRepository::init_opts(
directory,
gix::create::Kind::Bare,
gix::create::Options::default(),
open_options(),
)
.unwrap()
.to_thread_local()
}
pub fn clone(dest_path: &Path, url: &str) -> gix::Repository {
let mut prepare_fetch = gix::clone::PrepareFetch::new(
url,
dest_path,
gix::create::Kind::WithWorktree,
gix::create::Options::default(),
open_options(),
)
.unwrap();
let (mut prepare_checkout, _outcome) = prepare_fetch
.fetch_then_checkout(gix::progress::Discard, &gix::interrupt::IS_INTERRUPTED)
.unwrap();
let (repo, _outcome) = prepare_checkout
.main_worktree(gix::progress::Discard, &gix::interrupt::IS_INTERRUPTED)
.unwrap();
repo
}
pub struct CommitResult {
pub tree_id: gix::ObjectId,
pub commit_id: gix::ObjectId,
}
pub fn add_commit(
repo: &gix::Repository,
reference: &str,
filename: &str,
content: &[u8],
message: &str,
parents: &[gix::ObjectId],
) -> CommitResult {
let blob_oid = repo.write_blob(content).unwrap();
let parent_tree_editor = parents.first().map(|commit_id| {
repo.find_commit(*commit_id)
.unwrap()
.tree()
.unwrap()
.edit()
.unwrap()
});
let empty_tree_editor_fn = || {
repo.edit_tree(gix::ObjectId::empty_tree(repo.object_hash()))
.unwrap()
};
let mut tree_editor = parent_tree_editor.unwrap_or_else(empty_tree_editor_fn);
tree_editor
.upsert(filename, gix::object::tree::EntryKind::Blob, blob_oid)
.unwrap();
let tree_id = tree_editor.write().unwrap().detach();
let commit_id = write_commit(repo, reference, tree_id, message, parents);
CommitResult { tree_id, commit_id }
}
pub fn write_commit(
repo: &gix::Repository,
reference: &str,
tree_id: gix::ObjectId,
message: &str,
parents: &[gix::ObjectId],
) -> gix::ObjectId {
let signature = signature();
repo.commit_as(
&signature,
&signature,
reference,
message,
tree_id,
parents.iter().copied(),
)
.unwrap()
.detach()
}
pub fn set_head_to_id(repo: &gix::Repository, target: gix::ObjectId) {
repo.edit_reference(gix::refs::transaction::RefEdit {
change: gix::refs::transaction::Change::Update {
log: gix::refs::transaction::LogChange::default(),
expected: gix::refs::transaction::PreviousValue::Any,
new: gix::refs::Target::Object(target),
},
name: "HEAD".try_into().unwrap(),
deref: false,
})
.unwrap();
}
fn signature() -> gix::actor::Signature {
gix::actor::Signature {
name: bstr::BString::from(GIT_USER),
email: bstr::BString::from(GIT_EMAIL),
time: gix::date::Time::new(0, 0),
}
}

View File

@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
pub mod git;
mod test_environment;
pub use self::test_environment::TestEnvironment;