feat: GitDir tracks when repo is cloned by git-next
All checks were successful
ci/woodpecker/cron/cron-docker-builder Pipeline was successful
ci/woodpecker/cron/push-next Pipeline was successful
ci/woodpecker/cron/tag-created Pipeline was successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
Rust / build (push) Successful in 1m21s

This commit is contained in:
Paul Campbell 2024-07-06 14:25:43 +01:00
parent 425241196d
commit df352443b7
18 changed files with 190 additions and 62 deletions

View file

@ -1,20 +1,43 @@
//
use std::path::PathBuf;
crate::newtype!(GitDir: PathBuf, Default: "The path to the directory containing the git repository.");
use derive_more::Deref;
/// The path to the directory containing the git repository.
#[derive(
Clone,
Debug,
derive_more::From,
PartialEq,
Eq,
PartialOrd,
Ord,
derive_more::AsRef,
derive_more::Constructor,
)]
pub struct GitDir {
pathbuf: PathBuf,
/// Whether the directory is under the control of git-next (Internal) or not (External).
storage_path_type: StoragePathType,
}
impl GitDir {
pub const fn pathbuf(&self) -> &PathBuf {
&self.0
&self.pathbuf
}
pub const fn storage_path_type(&self) -> &StoragePathType {
&self.storage_path_type
}
}
impl std::fmt::Display for GitDir {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &self.0.display())
write!(f, "{}", &self.pathbuf.display())
}
}
impl From<&str> for GitDir {
fn from(value: &str) -> Self {
Self(value.into())
impl Deref for GitDir {
type Target = PathBuf;
fn deref(&self) -> &Self::Target {
&self.pathbuf
}
}
impl From<&GitDir> for PathBuf {
@ -22,3 +45,9 @@ impl From<&GitDir> for PathBuf {
value.to_path_buf()
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum StoragePathType {
Internal,
External,
}

View file

@ -30,6 +30,7 @@ pub use forge_config::ForgeConfig;
pub use forge_details::ForgeDetails;
pub use forge_type::ForgeType;
pub use git_dir::GitDir;
pub use git_dir::StoragePathType;
pub use host_name::Hostname;
pub use registered_webhook::RegisteredWebhook;
pub use repo_alias::RepoAlias;

View file

@ -35,7 +35,10 @@ impl ServerRepoConfig {
}
pub fn gitdir(&self) -> Option<GitDir> {
self.gitdir.clone().map(GitDir::from)
self.gitdir
.clone()
// Provenance is external as the gitdir is only used to specify non-internal paths
.map(|dir| GitDir::new(dir, crate::git_dir::StoragePathType::External))
}
/// Returns a RepoConfig from the server configuration if ALL THREE branches were provided

View file

@ -90,7 +90,10 @@ mod server_repo_config {
assert_eq!(
src.gitdir(),
Some(GitDir::new(PathBuf::default().join(gitdir)))
Some(GitDir::new(
PathBuf::default().join(gitdir),
git_dir::StoragePathType::External
))
);
}
}
@ -382,47 +385,29 @@ mod gitdir {
#[test]
fn should_return_pathbuf() {
let pathbuf = PathBuf::default().join(given::a_name());
let gitdir = GitDir::new(&pathbuf);
let gitdir = GitDir::new(pathbuf.clone(), git_dir::StoragePathType::Internal);
let result = gitdir.unwrap();
let result = gitdir.to_path_buf();
assert_eq!(result, pathbuf);
}
#[test]
fn should_display() {
let pathbuf = PathBuf::default().join("foo");
let gitdir = GitDir::new(pathbuf);
let gitdir = GitDir::new(pathbuf, git_dir::StoragePathType::Internal);
let result = gitdir.to_string();
assert_eq!(result, "foo");
}
#[test]
fn should_convert_from_str() {
let path = given::a_name();
let pathbuf = PathBuf::default().join(path.clone());
let gitdir: GitDir = path.as_str().into();
assert_eq!(gitdir, GitDir::new(pathbuf));
}
#[test]
fn should_convert_to_pathbuf_from_ref() {
let path = given::a_name();
let pathbuf = PathBuf::default().join(path.clone());
let gitdir: GitDir = path.as_str().into();
let pathbuf = PathBuf::default().join(path);
let gitdir: GitDir = GitDir::new(pathbuf.clone(), git_dir::StoragePathType::Internal);
let result: PathBuf = (&gitdir).into();
assert_eq!(result, pathbuf);
}
#[test]
fn should_convert_to_pathbuf_from_inst() {
let path = given::a_name();
let pathbuf = PathBuf::default().join(path.clone());
let gitdir: GitDir = path.as_str().into();
let result: PathBuf = gitdir.into();
assert_eq!(result, pathbuf);
}
}
@ -503,7 +488,7 @@ mod server {
let branch = server_repo_config.branch();
let gitdir = server_repo_config
.gitdir()
.map(|gitdir| gitdir.unwrap())
.map(|gitdir| gitdir.to_path_buf())
.map(|pathbuf| pathbuf.display().to_string())
.map(|gitdir| format!(r#", gitdir = "{gitdir}" "#))
.unwrap_or_default();

View file

@ -734,7 +734,7 @@ mod forgejo {
pub fn a_git_dir(fs: &kxio::fs::FileSystem) -> config::GitDir {
let dir_name = a_name();
let dir = fs.base().join(dir_name);
config::GitDir::new(dir)
config::GitDir::new(dir, config::git_dir::StoragePathType::Internal)
}
pub fn a_forge_config() -> config::ForgeConfig {

View file

@ -508,6 +508,8 @@ mod github {
mod given {
use std::path::PathBuf;
use git_next_config::{server::Webhook, WebhookId};
use kxio::network::{MockNetwork, StatusCode};
use rand::RngCore;
@ -624,7 +626,10 @@ mod github {
a_name(),
BTreeMap::default(),
),
GitDir::default(),
GitDir::new(
PathBuf::default(),
config::git_dir::StoragePathType::External,
),
)
}

View file

@ -37,6 +37,9 @@ fn given_repo_details(forge_type: config::ForgeType) -> RepoDetails {
1,
config::RepoConfigSource::Repo,
)),
config::GitDir::new(fs.base()),
config::GitDir::new(
fs.base().to_path_buf(),
config::git_dir::StoragePathType::Internal,
),
)
}

View file

@ -7,7 +7,7 @@ use git_next_config as config;
use super::{Generation, GitRemote};
/// The derived information about a repo, used to interact with it
#[derive(Clone, Default, Debug, derive_more::Display, derive_with::With)]
#[derive(Clone, Debug, derive_more::Display, derive_with::With)]
#[display("gen-{}:{}:{}/{}", generation, forge.forge_type(), forge.forge_alias(), repo_alias )]
pub struct RepoDetails {
pub generation: Generation,

View file

@ -4,7 +4,10 @@ use super::*;
// assumes running in the git-next repo which should have main, next and dev as remote branches
fn should_return_single_item_in_commit_log_when_not_searching() -> TestResult {
let_assert!(Ok(fs) = kxio::fs::temp());
let gitdir: config::GitDir = fs.base().to_path_buf().into();
let gitdir = GitDir::new(
fs.base().to_path_buf(),
config::git_dir::StoragePathType::Internal,
);
let test_repository = git::repository::test(fs.clone());
let_assert!(Ok(open_repository) = test_repository.open(&gitdir));
let repo_config = &given::a_repo_config();
@ -21,7 +24,10 @@ fn should_return_single_item_in_commit_log_when_not_searching() -> TestResult {
fn should_return_capacity_50_in_commit_log_when_searching_for_garbage() -> TestResult {
let_assert!(Ok(fs) = kxio::fs::temp());
let branch_name = given::a_branch_name();
let gitdir: config::GitDir = fs.base().to_path_buf().into();
let gitdir = GitDir::new(
fs.base().to_path_buf(),
config::git_dir::StoragePathType::Internal,
);
let test_repository = git::repository::test(fs.clone());
let_assert!(Ok(open_repository) = test_repository.open(&gitdir));
for _ in [0; 60] {
@ -38,7 +44,10 @@ fn should_return_capacity_50_in_commit_log_when_searching_for_garbage() -> TestR
fn should_return_5_in_commit_log_when_searching_for_5th_item() -> TestResult {
let_assert!(Ok(fs) = kxio::fs::temp(), "create temp directory");
let branch_name = given::a_branch_name();
let gitdir: config::GitDir = fs.base().to_path_buf().into();
let gitdir = GitDir::new(
fs.base().to_path_buf(),
config::git_dir::StoragePathType::Internal,
);
let test_repository = git::repository::test(fs.clone());
let_assert!(
Ok(open_repository) = test_repository.open(&gitdir),

View file

@ -5,7 +5,10 @@ use super::*;
fn should_fetch_from_repo() {
// uses the current repo and fetches from the remote server
let_assert!(Ok(cwd) = std::env::current_dir());
let gitdir = GitDir::from(cwd.join("../.."));
let gitdir = GitDir::new(
cwd.join("../.."),
config::git_dir::StoragePathType::External,
);
let_assert!(Ok(repo) = crate::repository::factory::real().open(&gitdir));
let_assert!(Ok(_) = repo.fetch());
}

View file

@ -4,7 +4,10 @@ use super::*;
fn should_find_default_push_remote() {
// uses the current repo
let_assert!(Ok(cwd) = std::env::current_dir());
let gitdir = config::GitDir::from(cwd.join("../..")); // from ./crate/git directory to the project rook
let gitdir = config::GitDir::new(
cwd.join("../.."),
config::git_dir::StoragePathType::External,
); // from ./crate/git directory to the project root
let_assert!(Ok(repo) = git::repository::factory::real().open(&gitdir));
let_assert!(Some(remote) = repo.find_default_remote(crate::repository::Direction::Push));
assert_eq!(

View file

@ -7,7 +7,10 @@ fn should_return_file() -> TestResult {
let repo_config = given::a_repo_config();
let file_name = given::a_pathbuf();
let contents = given::a_name();
let gitdir: config::GitDir = fs.base().to_path_buf().into();
let gitdir = GitDir::new(
fs.base().to_path_buf(),
config::git_dir::StoragePathType::Internal,
);
let test_repository = git::repository::test(fs.clone());
let_assert!(Ok(open_repository) = test_repository.open(&gitdir));
@ -31,7 +34,10 @@ fn should_return_file() -> TestResult {
// assumes running in the git-next repo which should have main, next and dev as remote branches
fn should_error_on_missing_file() -> TestResult {
let_assert!(Ok(fs) = kxio::fs::temp());
let gitdir: config::GitDir = fs.base().to_path_buf().into();
let gitdir = GitDir::new(
fs.base().to_path_buf(),
config::git_dir::StoragePathType::Internal,
);
let test_repository = git::repository::test(fs.clone());
let_assert!(Ok(open_repository) = test_repository.open(&gitdir));
let repo_config = &given::a_repo_config();

View file

@ -79,6 +79,9 @@ fn should_return_gitdir() {
assert_eq!(
src.gitdir(),
Some(config::GitDir::new(PathBuf::default().join(gitdir)))
Some(GitDir::new(
PathBuf::default().join(gitdir),
config::StoragePathType::External,
))
);
}

View file

@ -0,0 +1,52 @@
use git_next_config::GitDir;
use super::*;
use crate::tests::given;
// clone - can't test are this required a remote server (git_clone only works with https origins)
// open
// - outside storage path
#[test_log::test]
fn open_where_storage_external() -> TestResult {
//given
let fs = given::a_filesystem();
// create a bare repo
let _x = gix::prepare_clone_bare("https://user:auth@git.kemitix.net/user/repo.git", fs.base())?;
let gitdir = GitDir::new(
fs.base().to_path_buf(),
git_next_config::git_dir::Provenance::Internal,
);
let factory = crate::repository::factory::real();
//when
let result = factory.open(&gitdir);
//then
tracing::debug!(?result, "open");
assert!(result.is_ok());
Ok(())
}
// - in storage path
#[test_log::test]
fn open_where_storage_internal() -> TestResult {
//given
let fs = given::a_filesystem();
// create a bare repo
let _x = gix::prepare_clone_bare("https://user:auth@git.kemitix.net/user/repo.git", fs.base())?;
let gitdir = GitDir::new(
fs.base().to_path_buf(),
git_next_config::git_dir::Provenance::Internal,
);
let factory = crate::repository::factory::real();
//when
let result = factory.open(&gitdir);
//then
tracing::debug!(?result, "open");
assert!(result.is_ok());
Ok(())
}
// - - auth matches
// - - NEW: auth does not match

View file

@ -144,7 +144,8 @@ mod repo_details {
use std::{collections::BTreeMap, path::PathBuf};
use git_next_config::{
ForgeAlias, ForgeConfig, ForgeType, GitDir, Hostname, RepoAlias, RepoPath, ServerRepoConfig,
git_dir::StoragePathType, ForgeAlias, ForgeConfig, ForgeType, GitDir, Hostname, RepoAlias,
RepoPath, ServerRepoConfig,
};
use secrecy::ExposeSecret;
@ -171,7 +172,7 @@ mod repo_details {
"token".to_string(),
BTreeMap::new(),
),
GitDir::from(PathBuf::default().join("foo")),
GitDir::new(PathBuf::default().join("foo"), StoragePathType::Internal),
);
assert_eq!(
@ -200,7 +201,7 @@ mod repo_details {
"token".to_string(),
BTreeMap::new(),
),
GitDir::from(PathBuf::default().join("foo")),
GitDir::new(PathBuf::default().join("foo"), StoragePathType::Internal),
);
assert_eq!(
@ -215,7 +216,7 @@ mod repo_details {
pub mod given {
use std::path::PathBuf;
use crate as git;
use crate::{self as git, repository::open::MockOpenRepositoryLike};
use config::{
BranchName, ForgeAlias, ForgeConfig, ForgeType, GitDir, RepoAlias, RepoBranches,
RepoConfig, ServerRepoConfig,
@ -263,7 +264,7 @@ pub mod given {
pub fn a_git_dir(fs: &kxio::fs::FileSystem) -> GitDir {
let dir_name = a_name();
let dir = fs.base().join(dir_name);
GitDir::new(dir)
GitDir::new(dir, config::git_dir::StoragePathType::Internal)
}
pub fn a_forge_config() -> ForgeConfig {
@ -326,6 +327,25 @@ pub mod given {
kxio::fs::temp().unwrap_or_else(|e| panic!("{}", e))
}
pub fn an_open_repository(
fs: &kxio::fs::FileSystem,
) -> (
git::repository::open::MockOpenRepositoryLike,
git::RepoDetails,
) {
let open_repository = MockOpenRepositoryLike::new();
let gitdir = given::a_git_dir(fs);
let hostname = given::a_hostname();
let repo_details = given::repo_details(fs)
.with_gitdir(gitdir)
.with_hostname(hostname);
(open_repository, repo_details)
}
pub fn a_hostname() -> config::Hostname {
config::Hostname::new(given::a_name())
}
pub fn repo_details(fs: &kxio::fs::FileSystem) -> git::RepoDetails {
let generation = git::Generation::default();
let repo_alias = a_repo_alias();

View file

@ -10,6 +10,8 @@ use assert2::let_assert;
mod repos {
use config::{git_dir::StoragePathType, GitDir};
use crate::repository::Direction;
use super::*;
@ -17,7 +19,7 @@ mod repos {
#[test]
fn where_repo_has_no_push_remote() {
let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs");
let gitdir: config::GitDir = fs.base().to_path_buf().into();
let gitdir = GitDir::new(fs.base().to_path_buf(), StoragePathType::Internal);
let mut mock_open_repository = git::repository::open::mock();
mock_open_repository
.expect_find_default_remote()
@ -49,6 +51,7 @@ mod positions {
mod validate_positions {
use config::{git_dir::StoragePathType, GitDir};
use git::validation::positions::validate_positions;
use git::tests::then;
@ -58,7 +61,7 @@ mod positions {
#[test]
fn where_fetch_fails_should_error() {
let fs = given::a_filesystem();
let gitdir: config::GitDir = fs.base().to_path_buf().into();
let gitdir = GitDir::new(fs.base().to_path_buf(), StoragePathType::Internal);
let mut mock_open_repository = git::repository::open::mock();
mock_open_repository
.expect_fetch()
@ -87,7 +90,7 @@ mod positions {
#[test]
fn where_main_branch_is_missing_or_commit_log_is_empty_should_error() {
let fs = given::a_filesystem();
let gitdir: config::GitDir = fs.base().to_path_buf().into();
let gitdir = GitDir::new(fs.base().to_path_buf(), StoragePathType::Internal);
let repo_config = given::a_repo_config();
let main_branch = repo_config.branches().main();
let mut mock_open_repository = git::repository::open::mock();
@ -126,7 +129,7 @@ mod positions {
#[test]
fn where_next_branch_is_missing_or_commit_log_is_empty_should_error() {
let fs = given::a_filesystem();
let gitdir: config::GitDir = fs.base().to_path_buf().into();
let gitdir = GitDir::new(fs.base().to_path_buf(), StoragePathType::Internal);
let repo_config = given::a_repo_config();
let next_branch = repo_config.branches().next();
let mut mock_open_repository = git::repository::open::mock();
@ -165,7 +168,7 @@ mod positions {
#[test]
fn where_dev_branch_is_missing_or_commit_log_is_empty_should_error() {
let fs = given::a_filesystem();
let gitdir: config::GitDir = fs.base().to_path_buf().into();
let gitdir = GitDir::new(fs.base().to_path_buf(), StoragePathType::Internal);
let repo_config = given::a_repo_config();
let dev_branch = repo_config.branches().dev();
let mut mock_open_repository = git::repository::open::mock();
@ -205,7 +208,7 @@ mod positions {
fn where_dev_branch_is_not_based_on_main_should_error() {
//given
let fs = given::a_filesystem();
let gitdir: config::GitDir = fs.base().to_path_buf().into();
let gitdir = GitDir::new(fs.base().to_path_buf(), StoragePathType::Internal);
let mut test_repository = git::repository::test(fs.clone());
let repo_config = given::a_repo_config();
test_repository.on_fetch(git::repository::OnFetch::new(
@ -255,7 +258,7 @@ mod positions {
{
//given
let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs");
let gitdir: config::GitDir = fs.base().to_path_buf().into();
let gitdir = GitDir::new(fs.base().to_path_buf(), StoragePathType::Internal);
let mut test_repository = git::repository::test(fs.clone());
let repo_config = given::a_repo_config();
test_repository.on_fetch(git::repository::OnFetch::new(
@ -341,7 +344,7 @@ mod positions {
fn where_next_branch_is_not_based_on_main_and_reset_of_next_fails_should_error() {
//given
let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs");
let gitdir: config::GitDir = fs.base().to_path_buf().into();
let gitdir = GitDir::new(fs.base().to_path_buf(), StoragePathType::Internal);
let mut test_repository = git::repository::test(fs.clone());
let repo_config = given::a_repo_config();
test_repository.on_fetch(git::repository::OnFetch::new(
@ -414,7 +417,7 @@ mod positions {
fn where_dev_branch_is_not_based_on_next_should_reset_next_branch_to_main_and_then_error() {
//given
let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs");
let gitdir: config::GitDir = fs.base().to_path_buf().into();
let gitdir = GitDir::new(fs.base().to_path_buf(), StoragePathType::Internal);
let mut test_repository = git::repository::test(fs.clone());
let repo_config = given::a_repo_config();
test_repository.on_fetch(git::repository::OnFetch::new(
@ -498,7 +501,7 @@ mod positions {
fn where_dev_branch_is_not_based_on_next_and_reset_of_next_fails_should_error() {
//given
let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs");
let gitdir: config::GitDir = fs.base().to_path_buf().into();
let gitdir = GitDir::new(fs.base().to_path_buf(), StoragePathType::Internal);
let mut test_repository = git::repository::test(fs.clone());
let repo_config = given::a_repo_config();
test_repository.on_fetch(git::repository::OnFetch::new(
@ -569,7 +572,7 @@ mod positions {
fn where_branches_are_all_valid_should_return_positions() {
//given
let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs");
let gitdir: config::GitDir = fs.base().to_path_buf().into();
let gitdir = GitDir::new(fs.base().to_path_buf(), StoragePathType::Internal);
let mut test_repository = git::repository::test(fs.clone());
let repo_config = given::a_repo_config();
test_repository.on_fetch(git::repository::OnFetch::new(

View file

@ -1,3 +1,5 @@
use config::git_dir::StoragePathType;
//
use super::*;
@ -81,7 +83,7 @@ pub fn a_branch_name(prefix: impl Into<String>) -> BranchName {
pub fn a_git_dir(fs: &kxio::fs::FileSystem) -> GitDir {
let dir_name = a_name();
let dir = fs.base().join(dir_name);
GitDir::new(dir)
GitDir::new(dir, StoragePathType::Internal)
}
pub fn a_forge_config() -> ForgeConfig {

View file

@ -200,11 +200,12 @@ impl Server {
info!("Creating Repo");
let gitdir = server_repo_config.gitdir().map_or_else(
|| {
GitDir::from(
GitDir::new(
server_storage
.path()
.join(forge_name.to_string())
.join(repo_alias.to_string()),
config::git_dir::StoragePathType::Internal,
)
},
|gitdir| gitdir,