use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::{Path, PathBuf};
use getrandom::SysRng;
use getrandom::rand_core::UnwrapErr;
use nibkeep_base::{EntityId, SpaceId, StatementId};
use nibkeep_crypto::rand_core::CryptoRng;
use nibkeep_model::storage::Statements;
use nibkeep_model::{FieldName, Name, Operation, State, Value, fields};
use nibkeep_std::{
GIT_BRANCH, GIT_COMMIT, GIT_REMOTE, GIT_REPOSITORY, GIT_UPSTREAM, IN_TREE, TREE_PATH,
};
use super::commit::{clearing, deleting, moving, replacing};
use super::git::{cloneable, is_commit, is_safe_name, recordable_address};
use super::index::Index;
use super::locate::{Blocked, Located, located, place_for_new};
use super::walk::walk;
use super::{Change, Committed, TREE_TYPES, TreeChange, is_safe_tree_path, tree_entities};
use crate::NodeError;
use crate::library::Library;
use crate::library::entities::NewValue;
use crate::library::policies;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GitRepository {
pub path: String,
pub remotes: Vec<(String, String)>,
pub branch: Option<String>,
pub upstream: Option<String>,
pub commit: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RecordedRepository {
pub entity: EntityId,
pub path: String,
pub remotes: Vec<(String, String)>,
pub branch: Option<String>,
pub upstream: Option<String>,
pub pinned: Option<String>,
pub here: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ToClone {
pub entity: EntityId,
pub path: String,
pub remote: String,
pub address: String,
pub others: Vec<(String, String)>,
pub branch: Option<String>,
pub pinned: Option<String>,
pub recorded_by: String,
pub refused: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CloneFolder {
pub folder: PathBuf,
pub made: Vec<PathBuf>,
}
#[derive(Clone, Debug)]
pub(super) struct Recorded {
pub(super) entity: EntityId,
pub(super) path: String,
remotes: BTreeMap<String, Vec<(String, StatementId)>>,
branch: Option<String>,
upstream: Option<String>,
pinned: Option<String>,
statements: Vec<StatementId>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
struct Snapshot {
remotes: BTreeMap<String, String>,
branch: Option<String>,
upstream: Option<String>,
head: Option<String>,
}
impl Snapshot {
fn of(repository: &GitRepository) -> Self {
let remotes = recordable_remotes(repository);
let upstream = repository
.upstream
.clone()
.filter(|upstream| remotes.contains_key(upstream));
Self {
remotes,
branch: repository.branch.clone(),
upstream,
head: repository.commit.clone(),
}
}
fn to_record(&self) -> String {
let mut lines: Vec<String> = self
.remotes
.iter()
.map(|(name, address)| format!("remote {name} {address}"))
.collect();
for (key, value) in [
("branch", &self.branch),
("upstream", &self.upstream),
("head", &self.head),
] {
if let Some(value) = value.as_ref().filter(|value| !value.contains('\n')) {
lines.push(format!("{key} {value}"));
}
}
lines.join("\n")
}
fn from_record(record: &str) -> Self {
let mut snapshot = Self::default();
for line in record.lines() {
let Some((key, rest)) = line.split_once(' ') else {
continue;
};
match key {
"remote" => {
if let Some((name, address)) = rest.split_once(' ') {
snapshot.remotes.insert(name.to_owned(), address.to_owned());
}
}
"branch" => snapshot.branch = Some(rest.to_owned()),
"upstream" => snapshot.upstream = Some(rest.to_owned()),
"head" => snapshot.head = Some(rest.to_owned()),
_ => {}
}
}
snapshot
}
}
#[derive(Default)]
pub(super) struct Plan {
pub(super) operations: Vec<(EntityId, Vec<Operation>)>,
pub(super) changes: Vec<TreeChange>,
pub(super) hold: Vec<(String, EntityId, String)>,
pub(super) forget: Vec<String>,
}
pub(super) struct Scope<'a> {
pub(super) root: &'a Path,
pub(super) apart: &'a [PathBuf],
pub(super) paths: &'a [String],
}
pub(super) fn read_repositories<S: Statements>(
state: &State<S>,
tree: EntityId,
entities: &[EntityId],
) -> Result<Vec<Recorded>, NodeError>
where
NodeError: From<S::Error>,
{
let tree = state.canonical(tree)?;
let mut recorded = Vec::new();
for &entity in entities {
if !state.types(entity)?.contains(&GIT_REPOSITORY) || state.is_deleted(entity)? {
continue;
}
let mut in_tree = false;
for statement in state.current(entity, &IN_TREE)? {
if let Value::Reference(of) = statement.value {
in_tree |= state.canonical(of)? == tree;
}
}
if !in_tree {
continue;
}
let first = |field: &FieldName| -> Result<Option<String>, NodeError> {
let mut statements = state.current(entity, field)?;
statements.sort_by_key(|statement| statement.id);
Ok(statements
.into_iter()
.find_map(|statement| match &statement.value {
Value::Text(text) => Some(text.clone()),
_ => None,
}))
};
let Some(path) = first(&TREE_PATH)? else {
continue;
};
let mut remotes: BTreeMap<String, Vec<(String, StatementId)>> = BTreeMap::new();
let mut current = state.current(entity, &GIT_REMOTE)?;
current.sort_by_key(|statement| statement.id);
for statement in current {
if let Value::Text(text) = &statement.value
&& let Some((name, address)) = text.split_once(' ')
{
remotes
.entry(name.to_owned())
.or_default()
.push((address.to_owned(), statement.id));
}
}
let mut statements = Vec::new();
for field in [
IN_TREE,
TREE_PATH,
GIT_REMOTE,
GIT_BRANCH,
GIT_UPSTREAM,
GIT_COMMIT,
] {
statements.extend(
state
.current(entity, &field)?
.iter()
.map(|statement| statement.id),
);
}
recorded.push(Recorded {
entity,
path,
remotes,
branch: first(&GIT_BRANCH)?,
upstream: first(&GIT_UPSTREAM)?,
pinned: first(&GIT_COMMIT)?,
statements,
});
}
recorded.sort_by(|a, b| a.path.cmp(&b.path));
Ok(recorded)
}
fn recordable_remotes(repository: &GitRepository) -> BTreeMap<String, String> {
let plain = |text: &str| !text.chars().any(char::is_control);
repository
.remotes
.iter()
.filter(|(name, address)| {
!name.is_empty() && !name.contains(char::is_whitespace) && plain(name) && plain(address)
})
.map(|(name, address)| (name.clone(), recordable_address(address)))
.filter(|(_, address)| !address.contains(char::is_whitespace))
.collect()
}
fn changed<S: Statements>(
state: &State<S>,
(recorded, repository, before): (&Recorded, &GitRepository, &Snapshot),
plan: &mut Plan,
rng: &mut impl CryptoRng,
) -> Result<(), NodeError>
where
NodeError: From<S::Error>,
{
let entity = recorded.entity;
let now = Snapshot::of(repository);
let mut operations = Vec::new();
let mut retracted = Vec::new();
for (name, address) in &now.remotes {
if before.remotes.get(name) == Some(address) {
continue;
}
let values = recorded.remotes.get(name).map_or_default(Vec::as_slice);
retracted.extend(
values
.iter()
.filter(|(other, _)| other != address)
.map(|(_, statement)| *statement),
);
if !values.iter().any(|(other, _)| other == address) {
let value = Value::Text(format!("{name} {address}"));
operations.push(policies::assert(entity, GIT_REMOTE, value, rng));
}
}
for name in before.remotes.keys() {
if !now.remotes.contains_key(name)
&& let Some(values) = recorded.remotes.get(name)
{
retracted.extend(values.iter().map(|(_, statement)| *statement));
}
}
if !retracted.is_empty() {
operations.push(Operation::retract(entity, retracted));
}
for (field, here, then, recorded) in [
(GIT_BRANCH, &now.branch, &before.branch, &recorded.branch),
(
GIT_UPSTREAM,
&now.upstream,
&before.upstream,
&recorded.upstream,
),
] {
if here != then && here != recorded {
match here {
Some(value) => {
let value = Value::Text(value.clone());
operations.push(replacing(state, entity, field, value, rng)?);
}
None => operations.extend(clearing(state, entity, &field)?),
}
}
}
if recorded.pinned.is_some()
&& let Some(commit) = now.head.as_ref().filter(|commit| is_commit(commit))
&& before.head.as_ref() != Some(commit)
&& recorded.pinned.as_ref() != Some(commit)
{
let value = Value::Text(commit.clone());
operations.push(replacing(state, entity, GIT_COMMIT, value, rng)?);
}
if !operations.is_empty() {
plan.operations.push((entity, operations));
plan.changes.push(TreeChange {
path: repository.path.clone(),
change: Change::Edited,
});
}
Ok(())
}
fn inside_repository(
root: &Path,
path: &str,
(recorded, marked): (&[Recorded], &BTreeMap<String, String>),
) -> bool {
folders_of(path).any(|folder| {
marked.contains_key(folder)
|| recorded.iter().any(|other| other.path == folder)
|| fs::symlink_metadata(root.join(folder).join(".git")).is_ok()
})
}
fn gone_here(scope: &Scope<'_>, path: &str) -> bool {
match located(scope.root, path, scope.apart) {
Located::Absent => true,
Located::Here(folder) => {
fs::symlink_metadata(&folder).is_ok_and(|metadata| metadata.is_dir())
&& matches!(
fs::symlink_metadata(folder.join(".git")),
Err(error) if error.kind() == std::io::ErrorKind::NotFound
)
}
Located::Unknown(_) => false,
}
}
fn last_name(path: &str) -> &str {
path.rsplit('/').next().unwrap_or(path)
}
fn folders_of(path: &str) -> impl Iterator<Item = &str> {
path.match_indices('/').map(move |(at, _)| &path[..at])
}
fn within(path: &str, paths: &[String]) -> bool {
paths.is_empty()
|| paths.iter().any(|given| {
given.is_empty() || path == given || path.starts_with(&format!("{given}/"))
})
}
impl Library {
pub fn tree_repositories(
&self,
space: SpaceId,
tree: EntityId,
) -> Result<Vec<RecordedRepository>, NodeError> {
let held: BTreeSet<EntityId> = match self.index_if_any()? {
Some(index) => index
.repositories(tree)?
.into_values()
.map(|(entity, _)| entity)
.collect(),
None => BTreeSet::new(),
};
Ok(self
.recorded_repositories(space, tree)?
.into_iter()
.map(|recorded| RecordedRepository {
entity: recorded.entity,
here: held.contains(&recorded.entity),
path: recorded.path,
remotes: recorded
.remotes
.into_iter()
.filter_map(|(name, values)| {
values
.into_iter()
.next()
.map(|(address, _)| (name, address))
})
.collect(),
branch: recorded.branch,
upstream: recorded.upstream,
pinned: recorded.pinned,
})
.collect())
}
pub fn tree_repository_folders(
&self,
space: SpaceId,
tree: EntityId,
) -> Result<Vec<String>, NodeError> {
let root = self.root_here(space, tree)?;
let index = self.index()?;
let left_out = self.left_out(space, tree, &root, &index)?;
Ok(walk(&root, &left_out, &self.apart()).repositories)
}
pub(super) fn recorded_repositories(
&self,
space: SpaceId,
tree: EntityId,
) -> Result<Vec<Recorded>, NodeError> {
let open = self.space(space)?;
open.stored.reading(|stored| {
let state = stored.log().state();
let entities = tree_entities(state, tree)?;
read_repositories(state, tree, &entities)
})
}
pub fn repository_changes(
&self,
space: SpaceId,
tree: EntityId,
(found, described): (&[String], &[GitRepository]),
) -> Result<Vec<TreeChange>, NodeError> {
let root = self.root_here(space, tree)?;
let apart = self.apart();
let index = self.index()?;
let scope = Scope {
root: &root,
apart: &apart,
paths: &[],
};
let mut rng = UnwrapErr(SysRng);
let plan =
self.plan_repositories(space, (tree, &index, &scope), (found, described), &mut rng)?;
Ok(plan.changes)
}
pub(super) fn plan_repositories(
&self,
space: SpaceId,
(tree, index, scope): (EntityId, &Index, &Scope<'_>),
(found, described): (&[String], &[GitRepository]),
rng: &mut impl CryptoRng,
) -> Result<Plan, NodeError> {
let recorded = self.recorded_repositories(space, tree)?;
let by_path: BTreeMap<&str, &Recorded> = recorded
.iter()
.map(|recorded| (recorded.path.as_str(), recorded))
.collect();
let by_entity: BTreeMap<EntityId, &Recorded> = recorded
.iter()
.map(|recorded| (recorded.entity, recorded))
.collect();
let held = index.repositories(tree)?;
let found: BTreeSet<&str> = found.iter().map(String::as_str).collect();
let mut plan = Plan::default();
let mut new = Vec::new();
let open = self.space(space)?;
let state = open.stored.log().state();
for repository in described {
let path = repository.path.as_str();
if !found.contains(path) || !is_safe_tree_path(path) {
continue;
}
let now = Snapshot::of(repository).to_record();
if let Some((entity, record)) = held.get(path) {
if let Some(recorded) = by_entity
.get(entity)
.filter(|recorded| recorded.path == path)
&& within(path, scope.paths)
{
let before = Snapshot::from_record(record);
changed(state, (recorded, repository, &before), &mut plan, rng)?;
plan.hold.push((path.to_owned(), *entity, now));
}
continue;
}
match by_path.get(path) {
Some(recorded) => plan.hold.push((path.to_owned(), recorded.entity, now)),
None => new.push(repository),
}
}
for (path, (entity, record)) in &held {
if found.contains(path.as_str()) || !gone_here(scope, path) {
continue;
}
let recorded = by_entity
.get(entity)
.filter(|recorded| recorded.path == *path);
let before = Snapshot::from_record(record);
let moved = recorded.and_then(|_| {
new.iter().position(|repository| {
!before.remotes.is_empty() && recordable_remotes(repository) == before.remotes
})
});
let chosen = |other: &str| within(path, scope.paths) || within(other, scope.paths);
match (recorded, moved) {
(Some(recorded), Some(position)) if chosen(&new[position].path) => {
let repository = new.remove(position);
plan.forget.push(path.clone());
let mut operations = moving(state, *entity, path, &repository.path, rng)?;
let mut edits = Plan::default();
changed(state, (recorded, repository, &before), &mut edits, rng)?;
operations.extend(edits.operations.into_iter().flat_map(|(_, edits)| edits));
plan.operations.push((*entity, operations));
let now = Snapshot::of(repository).to_record();
plan.hold.push((repository.path.clone(), *entity, now));
plan.changes.push(TreeChange {
path: repository.path.clone(),
change: Change::Moved { from: path.clone() },
});
}
(Some(_), Some(position)) => {
new.remove(position);
}
(Some(_), None) if within(path, scope.paths) => {
plan.forget.push(path.clone());
plan.operations
.push((*entity, vec![deleting(*entity, rng)]));
plan.changes.push(TreeChange {
path: path.clone(),
change: Change::Deleted,
});
}
(None, _) if within(path, scope.paths) => plan.forget.push(path.clone()),
(Some(_) | None, _) => {}
}
}
for repository in new {
if !within(&repository.path, scope.paths) {
continue;
}
let (entity, operations) = self.new_repository(space, tree, repository, rng)?;
plan.operations.push((entity, operations));
let now = Snapshot::of(repository).to_record();
plan.hold.push((repository.path.clone(), entity, now));
plan.changes.push(TreeChange {
path: repository.path.clone(),
change: Change::New,
});
}
plan.changes.sort_by(|a, b| a.path.cmp(&b.path));
Ok(plan)
}
fn new_repository(
&self,
space: SpaceId,
tree: EntityId,
repository: &GitRepository,
rng: &mut impl CryptoRng,
) -> Result<(EntityId, Vec<Operation>), NodeError> {
let mut values = vec![
NewValue::new(
fields::NAME,
Value::Name(Name::new(last_name(&repository.path))),
),
NewValue::new(IN_TREE, Value::Reference(tree)),
NewValue::new(TREE_PATH, Value::Text(repository.path.clone())),
];
let said = Snapshot::of(repository);
for (name, address) in &said.remotes {
values.push(NewValue::new(
GIT_REMOTE,
Value::Text(format!("{name} {address}")),
));
}
if let Some(branch) = said.branch {
values.push(NewValue::new(GIT_BRANCH, Value::Text(branch)));
}
if let Some(upstream) = said.upstream {
values.push(NewValue::new(GIT_UPSTREAM, Value::Text(upstream)));
}
self.new_entity(space, &[GIT_REPOSITORY], values, rng)
}
pub fn pin_repository(
&mut self,
space: SpaceId,
tree: EntityId,
path: &str,
commit: Option<&str>,
wall_millis: u64,
rng: &mut impl CryptoRng,
) -> Result<Committed, NodeError> {
self.check_not_revoked(space)?;
let _locked = self.lock_trees()?;
let recorded = self
.recorded_repositories(space, tree)?
.into_iter()
.find(|recorded| recorded.path == path)
.ok_or_else(|| NodeError::NoRepository(path.to_owned()))?;
if let Some(commit) = commit
&& !is_commit(commit)
{
return Err(NodeError::NotACommit(commit.to_owned()));
}
if recorded.pinned.as_deref() == commit {
return Ok(Committed::default());
}
self.install_standard(space, &TREE_TYPES, wall_millis, rng)?;
let operations = {
let state = self.space(space)?.stored.log().state();
match commit {
Some(commit) => {
let value = Value::Text(commit.to_owned());
vec![replacing(state, recorded.entity, GIT_COMMIT, value, rng)?]
}
None => clearing(state, recorded.entity, &GIT_COMMIT)?,
}
};
let message = match commit {
Some(commit) => format!("Pin {path} at {}", &commit[..12]),
None => format!("Unpin {path}"),
};
let revision = self.batch_whole(space, |library| {
let (revision, made) =
library.revision_operations(space, tree, Some(&message), 2, rng)?;
library.commit(space, made, wall_millis, rng)?;
let entity = recorded.entity;
let operations = library.part_operations(space, revision, entity, operations, rng)?;
library.commit(space, operations, wall_millis, rng)?;
Ok(revision)
})?;
Ok(Committed {
revision: Some(revision),
parts: 2,
changes: Vec::new(),
repositories: vec![TreeChange {
path: path.to_owned(),
change: Change::Edited,
}],
..Committed::default()
})
}
pub fn repositories_to_clone(
&self,
space: SpaceId,
tree: EntityId,
) -> Result<Vec<ToClone>, NodeError> {
let root = self.root_here(space, tree)?;
let apart = self.apart();
let index = self.index()?;
let held: BTreeSet<EntityId> = index
.repositories(tree)?
.into_values()
.map(|(entity, _)| entity)
.collect();
let marked = index.marked(tree)?;
let recorded = self.recorded_repositories(space, tree)?;
let mut to_clone = Vec::new();
for repository in &recorded {
if held.contains(&repository.entity) || marked.contains_key(&repository.path) {
continue;
}
let there = is_safe_tree_path(&repository.path)
&& matches!(located(&root, &repository.path, &apart), Located::Here(_));
if there {
continue;
}
let nested = inside_repository(&root, &repository.path, (&recorded, &marked));
to_clone.push(self.to_clone(space, repository, nested));
}
Ok(to_clone)
}
fn made_by_person(&self, space: SpaceId, statement: StatementId) -> bool {
let Ok(open) = self.space(space) else {
return false;
};
open.stored
.reading(|stored| -> Result<bool, NodeError> {
let log = stored.log();
let Some(key) = log.author_of(statement)? else {
return Ok(false);
};
Ok(log.is_listed(&key, &open.keys)?
&& !log.is_revoked(&key, &open.keys)?
&& !log.is_ai_agent(&key, &open.keys)?)
})
.unwrap_or(false)
}
fn to_clone(&self, space: SpaceId, recorded: &Recorded, nested: bool) -> ToClone {
let trusted = recorded
.statements
.iter()
.all(|statement| self.made_by_person(space, *statement));
let may_take = |name: &str, address: &str| is_safe_name(name) && cloneable(address).is_ok();
let first = |name: &str| {
recorded
.remotes
.get(name)
.and_then(|values| values.first())
.map(|(address, statement)| (address.clone(), *statement))
};
let preferred: Vec<&String> = recorded
.upstream
.iter()
.filter(|upstream| recorded.remotes.contains_key(*upstream))
.chain(
recorded
.remotes
.get_key_value("origin")
.map(|(name, _)| name),
)
.chain(recorded.remotes.keys())
.collect();
let chosen = preferred
.iter()
.find(|name| first(name).is_some_and(|(address, _)| may_take(name, &address)))
.or_else(|| preferred.first())
.map(|name| (*name).clone());
let (remote, address, statement) = match chosen
.as_ref()
.and_then(|name| first(name).map(|first| (name, first)))
{
Some((name, (address, statement))) => (name.clone(), address.clone(), Some(statement)),
None => (String::new(), String::new(), None),
};
let refused = if statement.is_none() {
Some("it has no remote".to_owned())
} else if !is_safe_tree_path(&recorded.path) || recorded.path.contains(char::is_control) {
Some("its path is not one a tree may hold".to_owned())
} else if !trusted {
Some(
"an AI agent's replica, or one this library does not know, recorded part of it"
.to_owned(),
)
} else if nested {
Some("it is inside another Git repository".to_owned())
} else if let Err(why) = cloneable(&address) {
Some(why.to_owned())
} else if !is_safe_name(&remote) {
Some("its remote's name could be read as an option".to_owned())
} else if recorded
.branch
.as_ref()
.is_some_and(|branch| !is_safe_name(branch))
{
Some("its branch's name could be read as an option".to_owned())
} else if recorded
.pinned
.as_ref()
.is_some_and(|pinned| !is_commit(pinned))
{
Some("its pinned commit is not one".to_owned())
} else {
None
};
let others = recorded
.remotes
.keys()
.filter(|name| **name != remote)
.filter_map(|name| first(name).map(|(other, _)| (name.clone(), other)))
.filter(|(name, other)| may_take(name, other))
.collect();
let recorded_by = statement
.and_then(|statement| self.device_name_of(space, statement))
.unwrap_or_else(|| "elsewhere".to_owned());
ToClone {
entity: recorded.entity,
path: recorded.path.clone(),
remote,
address,
others,
branch: recorded.branch.clone(),
pinned: recorded.pinned.clone(),
recorded_by,
refused,
}
}
pub fn prepare_clone(
&self,
space: SpaceId,
tree: EntityId,
to: &ToClone,
) -> Result<CloneFolder, NodeError> {
let _locked = self.lock_trees()?;
let root = self.root_here(space, tree)?;
let apart = self.apart();
let all = self.recorded_repositories(space, tree)?;
let marked = self.index()?.marked(tree)?;
let changed = || NodeError::RepositoryChanged(to.path.clone());
let recorded = all
.iter()
.find(|recorded| recorded.entity == to.entity)
.ok_or_else(changed)?;
let path = recorded.path.as_str();
let nested = inside_repository(&root, path, (&all, &marked));
let now = self.to_clone(space, recorded, nested);
if now != *to || now.refused.is_some() {
return Err(changed());
}
if !is_safe_tree_path(path) || path.contains(char::is_control) {
return Err(NodeError::UnsafePath(path.to_owned()));
}
let blocked = |why| match why {
Blocked::Taken => NodeError::PlaceTaken(root.join(path)),
Blocked::Unsafe => NodeError::UnsafePath(path.to_owned()),
};
let target = place_for_new(&root, path, &apart).map_err(blocked)?;
let mut made: Vec<PathBuf> = target
.ancestors()
.skip(1)
.take_while(|folder| folder.starts_with(&root) && *folder != root)
.filter(|folder| fs::symlink_metadata(folder).is_err())
.map(Path::to_path_buf)
.collect();
made.reverse();
if let Some(parent) = target.parent() {
fs::create_dir_all(parent).map_err(NodeError::file(parent))?;
}
if place_for_new(&root, path, &apart).map_err(blocked)? != target {
return Err(NodeError::UnsafePath(path.to_owned()));
}
fs::create_dir(&target).map_err(|error| match error.kind() {
std::io::ErrorKind::AlreadyExists => NodeError::PlaceTaken(target.clone()),
_ => NodeError::file(&target)(error),
})?;
Ok(CloneFolder {
folder: target,
made,
})
}
pub fn repository_cloned(
&self,
space: SpaceId,
tree: EntityId,
(to, described): (&ToClone, &GitRepository),
) -> Result<(), NodeError> {
let _locked = self.lock_trees()?;
let still = self
.recorded_repositories(space, tree)?
.into_iter()
.any(|recorded| recorded.entity == to.entity && recorded.path == to.path);
if !still {
return Err(NodeError::RepositoryChanged(to.path.clone()));
}
let record = Snapshot::of(described).to_record();
self.index()?
.hold_repository(tree, &to.path, (to.entity, &record))
}
}
#[cfg(test)]
mod tests {
use super::{GitRepository, Snapshot, folders_of};
#[test]
fn what_git_said_is_kept_and_read_back_as_it_was() {
let said = Snapshot::of(&GitRepository {
path: "proj".to_owned(),
remotes: vec![
(
"origin".to_owned(),
"https://u:t@example.com/p.git".to_owned(),
),
(
"bad name".to_owned(),
"https://example.com/x.git".to_owned(),
),
("lines".to_owned(), "https://example.com/\ny.git".to_owned()),
],
branch: Some("main".to_owned()),
upstream: Some("origin".to_owned()),
commit: Some("a".repeat(40)),
});
assert_eq!(said.remotes.len(), 1);
assert_eq!(said.remotes["origin"], "https://example.com/p.git");
assert_eq!(Snapshot::from_record(&said.to_record()), said);
}
#[test]
fn a_path_s_folders_are_listed_from_the_outermost() {
assert_eq!(folders_of("a/b/c").collect::<Vec<_>>(), ["a", "a/b"]);
assert_eq!(folders_of("c").count(), 0);
}
}