use std::path::{Component, Path};
use crate::embed::parity::edit::{
AdmittedEditTarget, BatchRenamePlan, EditError, EditErrorKind, EditGuard, EditRoute,
EditTarget, ResolvedEditTarget,
};
use super::embed_edit_body::RouteContext;
use super::activation::ProjectSourceAuthority;
use super::embed_query::EmbeddedQuerySnapshot;
use super::embedded::EmbeddedSourceHandle;
const GIT_ENTRY_MAX_BYTES: usize = 4096;
fn lexical(path: &Path) -> Vec<String> {
let mut parts: Vec<String> = Vec::new();
for component in dunce::simplified(path).components() {
match component {
Component::CurDir => {}
Component::ParentDir => {
parts.pop();
}
other => parts.push(other.as_os_str().to_string_lossy().into_owned()),
}
}
if cfg!(windows) {
for part in &mut parts {
part.make_ascii_lowercase();
}
}
parts
}
fn same_path(left: &Path, right: &Path) -> bool {
lexical(left) == lexical(right)
}
fn git_common_dir(authority: &ProjectSourceAuthority, root: &Path) -> Option<Vec<String>> {
if matches!(
authority.read_regular_beneath_anchor(Path::new(".git/HEAD"), GIT_ENTRY_MAX_BYTES),
Ok(Some(_))
) {
return Some(lexical(&root.join(".git")));
}
let bytes = authority
.read_regular_beneath_anchor(Path::new(".git"), GIT_ENTRY_MAX_BYTES)
.ok()??;
let gitdir = std::str::from_utf8(&bytes)
.ok()?
.trim()
.strip_prefix("gitdir:")?
.trim();
let gitdir = root.join(gitdir);
let worktrees = gitdir.parent()?;
if worktrees.file_name()? != "worktrees" {
return None;
}
Some(lexical(worktrees.parent()?))
}
pub(super) fn same_repository(left: &EmbeddedQuerySnapshot, right: &EmbeddedQuerySnapshot) -> bool {
let left_common = git_common_dir(&left.authority, &left.root);
left_common.is_some() && left_common == git_common_dir(&right.authority, &right.root)
}
impl EmbeddedSourceHandle {
pub fn route_edit<'a>(
&self,
path: &str,
working_directory: &Path,
admitted: &[AdmittedEditTarget<'a>],
) -> Result<EditRoute<'a>, EditError> {
let snapshot = self.capture_query_snapshot(b"route-edit")?;
crate::discovery::resolve_repo_path(&snapshot.root, path)
.map_err(|_| EditError::Edit(EditErrorKind::InvalidPath))?
.ok_or(EditError::Edit(EditErrorKind::FileNotAdmitted))?;
if snapshot.generation.live.get_file(path).is_none() {
return Err(EditError::Edit(EditErrorKind::FileNotAdmitted));
}
let bound_root = dunce::simplified(&snapshot.root).to_path_buf();
let indexed_path = bound_root.join(path);
let context = |rerouted: bool, rebased: bool, target_root: &Path| RouteContext {
rerouted,
rebased,
bound_root: bound_root.clone(),
target_root: target_root.to_path_buf(),
working_directory: working_directory.to_path_buf(),
bound_live: std::sync::Arc::clone(&snapshot.generation.live),
bound_impact: rerouted.then(|| {
super::embed_edit_body::impact_footer(&snapshot, &snapshot.generation.live, path)
}),
bound_trust: super::embed_edit_body::trust_suffix(&snapshot),
};
if same_path(working_directory, &bound_root) {
return Ok(EditRoute {
target: None,
resolved: ResolvedEditTarget {
working_directory: working_directory.to_path_buf(),
rerouted: false,
target_path: indexed_path.clone(),
indexed_path,
},
path: path.to_owned(),
bound_root: snapshot.root.clone(),
context: context(false, false, &bound_root),
});
}
let target = admitted
.iter()
.find(|candidate| {
candidate
.handle
.capture_health_context()
.is_some_and(|(_, root, _)| same_path(working_directory, &root))
})
.copied()
.ok_or(EditError::Edit(EditErrorKind::WorkingDirectoryNotAdmitted))?;
let target_snapshot = target.handle.capture_query_snapshot(b"route-edit-target")?;
if target.authority.root != target_snapshot.root {
return Err(EditError::Edit(EditErrorKind::WriteAuthorityRefused));
}
if !same_repository(&snapshot, &target_snapshot) {
return Err(EditError::Edit(EditErrorKind::WorkingDirectoryNotAWorktree));
}
let target_file = match crate::discovery::resolve_repo_path(&target_snapshot.root, path) {
Ok(Some(_)) => target_snapshot
.generation
.live
.get_file(path)
.ok_or(EditError::Edit(EditErrorKind::TargetFileMissing))?,
Ok(None) => return Err(EditError::Edit(EditErrorKind::TargetFileMissing)),
Err(_) => return Err(EditError::Edit(EditErrorKind::InvalidPath)),
};
let rebased = snapshot
.generation
.live
.get_file(path)
.is_none_or(|bound_file| bound_file.content != target_file.content);
let target_root = dunce::simplified(&target_snapshot.root).to_path_buf();
Ok(EditRoute {
target: Some(target),
resolved: ResolvedEditTarget {
working_directory: working_directory.to_path_buf(),
rerouted: true,
target_path: target_root.join(path),
indexed_path,
},
path: path.to_owned(),
bound_root: snapshot.root.clone(),
context: context(true, rebased, &target_root),
})
}
pub fn rebase_guard(
&self,
route: &EditRoute<'_>,
guard: &EditGuard,
) -> Result<EditGuard, EditError> {
self.check_routed_guard(route, guard)?;
match route.target {
None => Ok(guard.clone()),
Some(target) => rebase_selector(guard, |selector| target.handle.edit_plan(selector))
.map(|plan| EditGuard {
selector: guard.selector.clone(),
..plan.guard
}),
}
}
pub fn rebase_rename_plan(
&self,
route: &EditRoute<'_>,
plan: &BatchRenamePlan,
) -> Result<BatchRenamePlan, EditError> {
self.check_routed_guard(route, &plan.guard)?;
match route.target {
None => Ok(plan.clone()),
Some(target) => rebase_selector(&plan.guard, |selector| {
target.handle.plan_batch_rename(selector, plan.code_only)
}),
}
}
fn check_routed_guard(
&self,
route: &EditRoute<'_>,
guard: &EditGuard,
) -> Result<(), EditError> {
let snapshot = self.capture_query_snapshot(b"rebase-edit-guard")?;
if route.bound_root != snapshot.root || route.path != guard.path {
return Err(EditError::Edit(EditErrorKind::InvalidPath));
}
super::embed_mutation::validate_guard(&snapshot, guard).map_err(EditError::Edit)?;
Ok(())
}
}
fn rebase_selector<T>(
guard: &EditGuard,
plan: impl Fn(&EditTarget) -> Result<T, EditError>,
) -> Result<T, EditError> {
let mut selector = EditTarget {
path: guard.path.clone(),
name: guard.name.clone(),
kind: Some(guard.kind.clone()),
symbol_line: None,
};
match plan(&selector) {
Err(EditError::Edit(EditErrorKind::AmbiguousSymbol { .. })) => {
selector.symbol_line = Some(guard.symbol_line);
plan(&selector)
}
other => other,
}
}