mod diff_parser;
pub mod file;
pub mod git;
mod hg;
mod jj;
pub(crate) mod traits;
pub use file::FileBackend;
pub use git::GitBackend;
pub use hg::HgBackend;
pub use jj::JjBackend;
pub use traits::{CommitInfo, VcsBackend, VcsInfo};
use crate::error::{Result, TuicrError};
pub fn detect_vcs() -> Result<Box<dyn VcsBackend>> {
if let Ok(backend) = JjBackend::discover() {
return Ok(Box::new(backend));
}
if let Ok(backend) = GitBackend::discover() {
return Ok(Box::new(backend));
}
if let Ok(backend) = HgBackend::discover() {
return Ok(Box::new(backend));
}
Err(TuicrError::NotARepository)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::vcs::traits::VcsType;
use std::path::PathBuf;
#[test]
fn exports_are_accessible() {
let _: fn() -> Result<Box<dyn VcsBackend>> = detect_vcs;
let info = VcsInfo {
root_path: PathBuf::from("/test"),
head_commit: "abc".to_string(),
branch_name: None,
vcs_type: VcsType::Git,
};
assert_eq!(info.head_commit, "abc");
let commit = CommitInfo {
id: "abc".to_string(),
short_id: "abc".to_string(),
branch_name: Some("main".to_string()),
summary: "test".to_string(),
body: None,
author: "author".to_string(),
time: chrono::Utc::now(),
};
assert_eq!(commit.id, "abc");
}
#[test]
fn detect_vcs_outside_repo_returns_error() {
let result = detect_vcs();
match result {
Ok(backend) => {
let info = backend.info();
assert!(!info.head_commit.is_empty());
}
Err(TuicrError::NotARepository) => {
}
Err(e) => {
panic!("Unexpected error: {e:?}");
}
}
}
}