use std::path::{Path, PathBuf};
use crate::error::{CoreError, ErrorCode};
use crate::fs::Fs;
pub struct ProjectRepo {
repo: git2::Repository,
workdir: PathBuf,
commondir: PathBuf,
}
impl std::fmt::Debug for ProjectRepo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ProjectRepo")
.field("workdir", &self.workdir)
.finish_non_exhaustive()
}
}
fn git_error(err: git2::Error) -> CoreError {
CoreError::new(ErrorCode::GitError, err.message().to_owned())
}
impl ProjectRepo {
pub fn discover(root: &Path) -> Result<Option<Self>, CoreError> {
let repo = match git2::Repository::discover(root) {
Ok(repo) => repo,
Err(err) if err.code() == git2::ErrorCode::NotFound => return Ok(None),
Err(err) => return Err(git_error(err).with_path(root)),
};
let Some(workdir) = repo.workdir().map(|dir| spelled_like(dir, root)) else {
return Ok(None);
};
let commondir = spelled_like(repo.commondir(), root);
Ok(Some(Self {
repo,
workdir,
commondir,
}))
}
pub fn workdir(&self) -> &Path {
&self.workdir
}
pub fn relative(&self, path: &Path) -> Option<String> {
let rest = match path.strip_prefix(&self.workdir) {
Ok(rest) => rest.to_path_buf(),
Err(_) => {
let base = std::fs::canonicalize(&self.workdir).ok()?;
let (existing, tail) = existing_ancestor(path)?;
let full = std::fs::canonicalize(existing).ok()?.join(tail);
full.strip_prefix(&base).ok()?.to_path_buf()
}
};
let parts: Vec<String> = rest
.components()
.map(|part| part.as_os_str().to_string_lossy().into_owned())
.collect();
(!parts.is_empty()).then(|| parts.join("/"))
}
pub fn is_tracked(&self, path: &Path) -> Result<bool, CoreError> {
let Some(relative) = self.relative(path) else {
return Ok(false);
};
let index = self.repo.index().map_err(git_error)?;
Ok(index.get_path(Path::new(&relative), 0).is_some())
}
pub fn tracks_any_under(&self, path: &Path) -> Result<bool, CoreError> {
let Some(relative) = self.relative(path) else {
return Ok(false);
};
let prefix = format!("{relative}/");
let index = self.repo.index().map_err(git_error)?;
Ok(index.iter().any(|entry| {
let tracked = String::from_utf8_lossy(&entry.path);
tracked == relative.as_str() || tracked.starts_with(&prefix)
}))
}
pub fn is_ignored(&self, path: &Path) -> Result<bool, CoreError> {
let Some(relative) = self.relative(path) else {
return Ok(false);
};
self.repo
.is_path_ignored(Path::new(&relative))
.map_err(git_error)
}
pub fn exclude_file(&self) -> PathBuf {
self.commondir.join("info").join("exclude")
}
pub fn state_files(&self, path: &Path) -> Vec<PathBuf> {
let gitdir = spelled_like(self.repo.path(), &self.workdir);
let mut files = vec![gitdir.join("index"), self.exclude_file()];
let mut dir = self.workdir.clone();
files.push(dir.join(".gitignore"));
if let Some(relative) = self.relative(path) {
let parts: Vec<&str> = relative.split('/').collect();
for part in &parts[..parts.len().saturating_sub(1)] {
dir.push(part);
files.push(dir.join(".gitignore"));
}
}
files
}
pub fn exclude(&self, fs: &dyn Fs, path: &Path) -> Result<bool, CoreError> {
let Some(relative) = self.relative(path) else {
return Ok(false);
};
if self.is_ignored(path)? {
return Ok(false);
}
let pattern = format!("/{relative}");
let file = self.exclude_file();
let io = |what: &str, at: &Path, err: std::io::Error| {
CoreError::new(ErrorCode::IoError, format!("{what}: {err}")).with_path(at)
};
let mut text = if fs.exists(&file) {
fs.read_to_string(&file)
.map_err(|err| io("cannot read file", &file, err))?
} else {
String::new()
};
if text
.lines()
.any(|line| line.trim() == pattern || line.trim() == relative)
{
return Ok(false);
}
if !text.is_empty() && !text.ends_with('\n') {
text.push('\n');
}
text.push_str(&pattern);
text.push('\n');
crate::apply::write_atomic_project(fs, &file, text.as_bytes())?;
Ok(true)
}
}
fn spelled_like(path: &Path, root: &Path) -> PathBuf {
if root.starts_with(path) || path.starts_with(root) {
return path.to_path_buf();
}
let Ok(real) = std::fs::canonicalize(path) else {
return path.to_path_buf();
};
for base in root.ancestors() {
let Ok(real_base) = std::fs::canonicalize(base) else {
continue;
};
if let Ok(rest) = real.strip_prefix(&real_base) {
return if rest.as_os_str().is_empty() {
base.to_path_buf()
} else {
base.join(rest)
};
}
}
path.to_path_buf()
}
fn existing_ancestor(path: &Path) -> Option<(&Path, PathBuf)> {
let mut tail: Vec<&std::ffi::OsStr> = Vec::new();
let mut current = path;
loop {
if current.exists() {
let mut rest = PathBuf::new();
for part in tail.iter().rev() {
rest.push(part);
}
return Some((current, rest));
}
tail.push(current.file_name()?);
current = current.parent()?;
}
}