use std::path::{Path, PathBuf};
use crate::{Error, floor};
fn redirected(reason: String) -> Error {
Error::FloorRefusal {
feature: floor::REDIRECTED_GIT_DIRECTORY.to_string(),
reason,
}
}
fn alternates(reason: String) -> Error {
Error::FloorRefusal {
feature: floor::OBJECT_ALTERNATES.to_string(),
reason,
}
}
fn refuse_if_symlink(p: &Path) -> Result<(), Error> {
if std::fs::symlink_metadata(p).is_ok_and(|m| m.file_type().is_symlink()) {
return Err(redirected(format!(
"{p:?} is a symlink; brygge reads only the repository it is given; replace the symlink \
with the real directory, or point brygge at the directory it links to"
)));
}
Ok(())
}
fn git_dir_of(path: &Path) -> PathBuf {
let dot_git = path.join(".git");
if dot_git.is_dir() {
dot_git
} else {
path.to_path_buf()
}
}
fn check_repository_shape(path: &Path) -> Result<(), Error> {
let dot_git = path.join(".git");
refuse_if_symlink(&dot_git)?;
if dot_git.is_file() {
return Err(redirected(format!(
"{} is a `gitdir:` redirect (a linked worktree or a submodule checkout); point brygge \
at the repository's main git directory (`git rev-parse --git-common-dir`)",
path.display()
)));
}
let git_dir = git_dir_of(path);
if git_dir.join("commondir").is_file() {
return Err(redirected(format!(
"{} is a linked worktree's git directory (it contains `commondir`); point brygge at the \
repository's main git directory (`git rev-parse --git-common-dir`)",
git_dir.display()
)));
}
let objects = git_dir.join("objects");
refuse_if_symlink(&objects)?;
refuse_if_symlink(&objects.join("info"))?;
let pack = objects.join("pack");
refuse_if_symlink(&pack)?;
if let Ok(entries) = std::fs::read_dir(&pack) {
for entry in entries.flatten() {
refuse_if_symlink(&entry.path())?;
}
}
let info = objects.join("info");
if std::fs::metadata(info.join("alternates")).is_ok_and(|m| m.len() > 0) {
return Err(alternates(
"the repository borrows objects from another store; brygge reads only the repository \
it is given; run `git repack -a -d` in it (which copies borrowed objects in) and then \
remove `objects/info/alternates`"
.to_string(),
));
}
if info.join("http-alternates").exists() {
return Err(alternates(
"the repository borrows objects from another store; brygge reads only the repository \
it is given; run `git repack -a -d` in it (which copies borrowed objects in) and then \
remove `objects/info/http-alternates`"
.to_string(),
));
}
Ok(())
}
const MAX_CONFIG_PROBE_BYTES: u64 = 1024 * 1024;
fn declares_sha256_object_format(config: &Path) -> bool {
let Ok(meta) = std::fs::metadata(config) else {
return false;
};
if meta.len() > MAX_CONFIG_PROBE_BYTES {
return false;
}
let Ok(text) = std::fs::read_to_string(config) else {
return false;
};
let mut in_extensions = false;
for line in text.lines() {
let trimmed = line.trim();
if let Some(section) = trimmed.strip_prefix('[').and_then(|s| s.strip_suffix(']')) {
in_extensions = section.trim().eq_ignore_ascii_case("extensions");
continue;
}
if !in_extensions {
continue;
}
let Some((key, value)) = trimmed.split_once('=') else {
continue;
};
if key.trim().eq_ignore_ascii_case("objectformat") {
let value = value.trim().trim_matches('"');
if value.eq_ignore_ascii_case("sha256") {
return true;
}
}
}
false
}
pub fn open(path: &Path) -> Result<gix::Repository, Error> {
if let Err(e) = std::fs::symlink_metadata(path) {
if e.kind() == std::io::ErrorKind::NotFound {
return Err(Error::Open(format!("source not found: {}", path.display())));
}
}
check_repository_shape(path)?;
if declares_sha256_object_format(&git_dir_of(path).join("config")) {
return Err(Error::FloorRefusal {
feature: floor::SHA256_OBJECT_FORMAT.to_string(),
reason: "the repository's object database is content-addressed with SHA-256 \
(`extensions.objectFormat = sha256`); this build reads SHA-1 repositories only"
.to_string(),
});
}
gix::open_opts(path, gix::open::Options::isolated()).map_err(|e| {
Error::Open(format!(
"cannot open Git repository at {}: {e}",
path.display()
))
})
}
pub fn check_repo_floor(repo: &gix::Repository) -> Result<(), Error> {
let git_dir = repo.git_dir();
if git_dir.join("shallow").exists() {
return Err(Error::FloorRefusal {
feature: floor::SHALLOW_CLONE.to_string(),
reason: "a shallow clone is a truncated history that would look whole; refused rather \
than imported as if complete"
.to_string(),
});
}
if git_dir.join("info").join("grafts").exists() {
return Err(Error::FloorRefusal {
feature: floor::GRAFTS.to_string(),
reason:
"grafts rewrite the ancestry a reader would see; refused rather than importing \
the rewritten view silently"
.to_string(),
});
}
Ok(())
}