mod bootstrap;
pub mod diff;
mod lock;
mod log;
pub mod merge;
pub mod restore;
mod revision;
pub mod tags;
mod trailers;
mod transaction;
use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use git2::{Commit, Repository, RepositoryInitOptions, Signature};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use toml::value::{Date, Datetime, Offset, Time};
use crate::brand;
use crate::env::Env;
use crate::error::{CoreError, ErrorCode};
use crate::fs::Fs;
use crate::model::ids::{self, IdKind};
use crate::model::server::Server;
use crate::model::{SCHEMA_VERSION, StoreModel, TomlDocument};
use crate::paths::SurjectionHome;
use crate::secrets::scanner::SecretMemory;
pub use bootstrap::{bare_commit_subject, bare_refs, bare_store_id, check_bare, export_store_id};
pub use lock::{LOCK_FILE, LOCK_TIMEOUT, StoreLock};
pub use log::{HistoryFinding, LogEntry, LogFilter, entity_paths};
pub use revision::Revision;
pub use trailers::{ApplyRef, MachineRef, Scope, Trailers, compose_message, message_summary};
pub use transaction::{Committed, Operation, Transaction};
pub trait StoreRead {
fn read_file(&self, relative: &str) -> Result<Option<String>, CoreError>;
fn list_files(&self, dir: &str) -> Result<Vec<String>, CoreError>;
}
impl StoreRead for Store<'_> {
fn read_file(&self, relative: &str) -> Result<Option<String>, CoreError> {
self.read_to_string(relative)
}
fn list_files(&self, dir: &str) -> Result<Vec<String>, CoreError> {
let path = self.path(dir)?;
if !self.fs.is_dir(&path) {
return Ok(Vec::new());
}
let entries = self
.fs
.read_dir(&path)
.map_err(|err| io_error("cannot list directory", &path, &err))?;
let mut names: Vec<String> = entries
.iter()
.filter(|entry| !self.fs.is_dir(entry))
.filter_map(|entry| entry.file_name()?.to_str().map(str::to_owned))
.collect();
names.sort();
Ok(names)
}
}
pub const BRANCH: &str = "main";
pub const IDENTITY_FILE: &str = "loadout.toml";
pub const MACHINE_FILE: &str = "machine.toml";
const GITATTRIBUTES: (&str, &str) = (".gitattributes", "* -text\n");
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[schemars(title = "Surjection store identity (store/loadout.toml)")]
pub struct StoreIdentity {
pub schema: i64,
pub store_id: String,
#[schemars(with = "String")]
pub created_at: Datetime,
#[serde(flatten)]
#[schemars(skip)]
pub extra: toml::Table,
}
impl StoreModel for StoreIdentity {
const SCHEMA_NAME: &'static str = "store-identity";
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Machine {
pub id: String,
pub label: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[schemars(title = "Surjection machine identity (state/machine.toml)")]
struct MachineFile {
schema: i64,
machine_id: String,
label: String,
#[serde(flatten)]
#[schemars(skip)]
extra: toml::Table,
}
impl StoreModel for MachineFile {
const SCHEMA_NAME: &'static str = "machine-identity";
}
const DEFAULT_LABEL: &str = "machine";
impl Machine {
pub fn load(fs: &dyn Fs, state_dir: &Path) -> Result<Option<Self>, CoreError> {
let path = state_dir.join(MACHINE_FILE);
if !fs.exists(&path) {
return Ok(None);
}
let file = TomlDocument::<MachineFile>::load(fs, &path)?.into_value();
ids::check(IdKind::Machine, &file.machine_id).map_err(|err| err.with_path(&path))?;
Ok(Some(Self {
id: file.machine_id,
label: clean_label(&file.label),
}))
}
pub fn load_or_create(env: &Env, fs: &dyn Fs, state_dir: &Path) -> Result<Self, CoreError> {
if let Some(machine) = Self::load(fs, state_dir)? {
return Ok(machine);
}
let machine = Self {
id: ids::new_ulid(env.now())?,
label: clean_label(env.hostname().unwrap_or(DEFAULT_LABEL)),
};
let path = state_dir.join(MACHINE_FILE);
fs.create_dir_all(state_dir)
.map_err(|err| io_error("cannot create directory", state_dir, &err))?;
TomlDocument::new(MachineFile {
schema: SCHEMA_VERSION,
machine_id: machine.id.clone(),
label: machine.label.clone(),
extra: toml::Table::new(),
})
.save(fs, &path)?;
Ok(machine)
}
fn signature(&self, time: SystemTime) -> Result<Signature<'static>, CoreError> {
let seconds = time
.duration_since(UNIX_EPOCH)
.map_or(0, |elapsed| elapsed.as_secs());
Signature::new(
&format!("{} {}", brand::COMMIT_AUTHOR_NAME, self.label),
&format!("{}@{}", self.id, brand::COMMIT_EMAIL_DOMAIN),
&git2::Time::new(i64::try_from(seconds).unwrap_or(i64::MAX), 0),
)
.map_err(git_error)
}
}
fn clean_label(raw: &str) -> String {
let cleaned = raw
.chars()
.map(|c| if c.is_control() { ' ' } else { c })
.filter(|c| !matches!(c, '<' | '>'))
.collect::<String>()
.split_whitespace()
.collect::<Vec<_>>()
.join(" ");
if cleaned.is_empty() {
DEFAULT_LABEL.to_owned()
} else {
cleaned
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CommitId(String);
impl CommitId {
fn from_oid(oid: git2::Oid) -> Self {
Self(oid.to_string())
}
pub fn from_text(text: &str) -> Result<Self, CoreError> {
git2::Oid::from_str(text)
.ok()
.filter(|_| text.len() == 40)
.map(Self::from_oid)
.ok_or_else(|| {
CoreError::new(
ErrorCode::InvalidValue,
format!("{text:?} is not a commit id"),
)
})
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn short(&self) -> &str {
&self.0[..self.0.len().min(7)]
}
}
impl fmt::Display for CommitId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug)]
pub struct ServerEntry {
pub id: String,
pub document: Result<TomlDocument<Server>, CoreError>,
}
pub struct Store<'a> {
env: Env,
fs: &'a dyn Fs,
root: PathBuf,
state_dir: PathBuf,
repo: Repository,
identity: StoreIdentity,
client: String,
secrets: Arc<SecretMemory>,
}
impl fmt::Debug for Store<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Store")
.field("root", &self.root)
.field("store_id", &self.identity.store_id)
.field("client", &self.client)
.finish_non_exhaustive()
}
}
impl<'a> Store<'a> {
fn from_parts(
env: &Env,
fs: &'a dyn Fs,
home: &SurjectionHome,
client: &str,
repo: Repository,
identity: StoreIdentity,
) -> Result<Self, CoreError> {
Ok(Self {
env: env.clone(),
fs,
root: home.store_dir(),
state_dir: home.state_dir(),
repo,
identity,
client: client.to_owned(),
secrets: Arc::new(SecretMemory::new()?),
})
}
pub fn open(
env: &Env,
fs: &'a dyn Fs,
home: &SurjectionHome,
client: &str,
) -> Result<Option<Self>, CoreError> {
let root = home.store_dir();
if !fs.exists(&root.join(".git")) {
return Ok(None);
}
let repo = Repository::open(&root).map_err(git_error)?;
if repo.head().is_err() {
return Ok(None);
}
let path = root.join(IDENTITY_FILE);
let identity = TomlDocument::<StoreIdentity>::load(fs, &path)?.into_value();
ids::check(IdKind::Store, &identity.store_id).map_err(|err| err.with_path(&path))?;
Ok(Some(Self {
env: env.clone(),
fs,
root,
state_dir: home.state_dir(),
repo,
identity,
client: client.to_owned(),
secrets: Arc::new(SecretMemory::new()?),
}))
}
pub(crate) fn open_raw(
env: &Env,
fs: &'a dyn Fs,
home: &SurjectionHome,
client: &str,
) -> Result<Option<Self>, CoreError> {
let root = home.store_dir();
if !fs.exists(&root.join(".git")) {
return Ok(None);
}
let repo = Repository::open(&root).map_err(git_error)?;
if repo.head().is_err() {
return Ok(None);
}
let path = root.join(IDENTITY_FILE);
let text = fs
.read_to_string(&path)
.map_err(|err| io_error("cannot read file", &path, &err))?;
let table: toml::Table =
text.trim_start_matches('\u{feff}')
.parse()
.map_err(|err: toml::de::Error| {
CoreError::new(ErrorCode::ParseError, err.message().to_owned()).with_path(&path)
})?;
let identity = StoreIdentity {
schema: table
.get("schema")
.and_then(toml::Value::as_integer)
.unwrap_or(SCHEMA_VERSION),
store_id: table
.get("store_id")
.and_then(toml::Value::as_str)
.unwrap_or_default()
.to_owned(),
created_at: table
.get("created_at")
.and_then(toml::Value::as_datetime)
.copied()
.unwrap_or_else(|| utc_datetime(env.now())),
extra: toml::Table::new(),
};
Self::from_parts(env, fs, home, client, repo, identity).map(Some)
}
pub fn open_or_init(
env: &Env,
fs: &'a dyn Fs,
home: &SurjectionHome,
client: &str,
) -> Result<Self, CoreError> {
if let Some(store) = Self::open(env, fs, home, client)? {
return Ok(store);
}
let state_dir = home.state_dir();
let _lock = StoreLock::acquire(fs, &state_dir, LOCK_TIMEOUT)?;
if let Some(store) = Self::open(env, fs, home, client)? {
return Ok(store);
}
let machine = Machine::load_or_create(env, fs, &state_dir)?;
let root = home.store_dir();
fs.create_dir_all(&root)
.map_err(|err| io_error("cannot create directory", &root, &err))?;
let repo = if fs.exists(&root.join(".git")) {
Repository::open(&root)
} else {
let mut options = RepositoryInitOptions::new();
options
.bare(false)
.no_reinit(true)
.mkdir(true)
.external_template(false)
.initial_head(BRANCH);
Repository::init_opts(&root, &options)
}
.map_err(git_error)?;
let identity_path = root.join(IDENTITY_FILE);
let identity = TomlDocument::<StoreIdentity>::load(fs, &identity_path)
.ok()
.map(TomlDocument::into_value)
.filter(|identity| ids::is_valid(IdKind::Store, &identity.store_id));
let identity = match identity {
Some(identity) => identity,
None => StoreIdentity {
schema: SCHEMA_VERSION,
store_id: ids::new_ulid(env.now())?,
created_at: utc_datetime(env.now()),
extra: toml::Table::new(),
},
};
let identity_text = TomlDocument::new(identity.clone()).to_text()?;
let store = Self {
env: env.clone(),
fs,
root,
state_dir,
repo,
identity,
client: client.to_owned(),
secrets: Arc::new(SecretMemory::new()?),
};
let operation = Operation::new(
"store.init",
format!("init: store {}", store.identity.store_id),
);
let changes = [
(IDENTITY_FILE.to_owned(), Some(identity_text.into_bytes())),
(GITATTRIBUTES.0.to_owned(), Some(GITATTRIBUTES.1.into())),
]
.into_iter()
.collect();
store.commit_changes(&machine, &operation, changes)?;
Ok(store)
}
pub fn root(&self) -> &Path {
&self.root
}
pub fn fs(&self) -> &'a dyn Fs {
self.fs
}
pub fn state_dir(&self) -> &Path {
&self.state_dir
}
pub fn contains(&self, id: &CommitId) -> bool {
let (Ok(head), Ok(oid)) = (self.head(), git2::Oid::from_str(id.as_str())) else {
return false;
};
let Ok(head) = git2::Oid::from_str(head.as_str()) else {
return false;
};
head == oid || self.repo.graph_descendant_of(head, oid).unwrap_or(false)
}
pub fn reference_target(&self, name: &str) -> Result<Option<CommitId>, CoreError> {
match self.repo.find_reference(name) {
Ok(reference) => {
let commit = reference.peel_to_commit().map_err(git_error)?;
Ok(Some(CommitId::from_oid(commit.id())))
}
Err(err) if err.code() == git2::ErrorCode::NotFound => Ok(None),
Err(err) => Err(git_error(err)),
}
}
pub fn env(&self) -> &Env {
&self.env
}
pub fn secret_memory(&self) -> &Arc<SecretMemory> {
&self.secrets
}
pub fn identity(&self) -> &StoreIdentity {
&self.identity
}
pub fn store_id(&self) -> &str {
&self.identity.store_id
}
pub fn head(&self) -> Result<CommitId, CoreError> {
Ok(CommitId::from_oid(self.head_commit()?.id()))
}
pub fn path(&self, relative: &str) -> Result<PathBuf, CoreError> {
check_relative(relative)?;
Ok(relative
.split('/')
.fold(self.root.clone(), |path, part| path.join(part)))
}
pub fn read_to_string(&self, relative: &str) -> Result<Option<String>, CoreError> {
let path = self.path(relative)?;
if !self.fs.exists(&path) {
return Ok(None);
}
self.fs
.read_to_string(&path)
.map(Some)
.map_err(|err| io_error("cannot read file", &path, &err))
}
pub fn server(&self, id: &str) -> Result<Option<TomlDocument<Server>>, CoreError> {
let path = self.path(&server_path(id)?)?;
if !self.fs.exists(&path) {
return Ok(None);
}
TomlDocument::load(self.fs, &path).map(Some)
}
pub fn servers(&self) -> Result<Vec<ServerEntry>, CoreError> {
let dir = self.root.join("servers");
if !self.fs.is_dir(&dir) {
return Ok(Vec::new());
}
let paths = self
.fs
.read_dir(&dir)
.map_err(|err| io_error("cannot list directory", &dir, &err))?;
let entries = paths
.into_iter()
.filter_map(|path| {
let id = path
.file_name()?
.to_str()?
.strip_suffix(".toml")?
.to_owned();
Some((id, path))
})
.map(|(id, path)| {
let document =
TomlDocument::<Server>::load(self.fs, &path).and_then(|doc| {
match doc.value().validate(Some(&id)).first_error(Some(&path)) {
Some(err) => Err(err),
None => Ok(doc),
}
});
ServerEntry { id, document }
})
.collect();
Ok(entries)
}
fn head_commit(&self) -> Result<Commit<'_>, CoreError> {
self.repo
.head()
.and_then(|head| head.peel_to_commit())
.map_err(git_error)
}
}
pub fn server_path(id: &str) -> Result<String, CoreError> {
ids::check(IdKind::Server, id)?;
Ok(format!("servers/{id}.toml"))
}
fn check_relative(relative: &str) -> Result<(), CoreError> {
let valid = !relative.is_empty()
&& relative.split('/').all(|part| {
!part.is_empty() && part != "." && part != ".." && !part.contains(['\\', ':'])
})
&& !relative
.split('/')
.next()
.is_some_and(|first| first.eq_ignore_ascii_case(".git"));
if valid {
Ok(())
} else {
Err(CoreError::new(
ErrorCode::InvalidPath,
format!("{relative:?} is not a store-relative path"),
))
}
}
pub fn utc_datetime(time: SystemTime) -> Datetime {
let seconds = time
.duration_since(UNIX_EPOCH)
.map_or(0, |elapsed| elapsed.as_secs());
let seconds = i64::try_from(seconds).unwrap_or(i64::MAX);
let (year, month, day) = civil_from_days(seconds.div_euclid(86_400));
let of_day = seconds.rem_euclid(86_400);
Datetime {
date: Some(Date {
year: u16::try_from(year).unwrap_or(u16::MAX),
month,
day,
}),
time: Some(Time {
hour: (of_day / 3600) as u8,
minute: (of_day % 3600 / 60) as u8,
second: Some((of_day % 60) as u8),
nanosecond: None,
}),
offset: Some(Offset::Z),
}
}
fn civil_from_days(days: i64) -> (i64, u8, u8) {
let z = days + 719_468;
let era = z.div_euclid(146_097);
let doe = z.rem_euclid(146_097);
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let day = (doy - (153 * mp + 2) / 5 + 1) as u8;
let month = if mp < 10 { mp + 3 } else { mp - 9 } as u8;
let year = yoe + era * 400 + i64::from(month <= 2);
(year, month, day)
}
fn git_error(err: git2::Error) -> CoreError {
CoreError::new(ErrorCode::GitError, err.message().to_owned())
}
fn io_error(what: &str, path: &Path, err: &std::io::Error) -> CoreError {
CoreError::new(ErrorCode::IoError, format!("{what}: {err}")).with_path(path)
}