Marketplace: gix cache with credentialed fetch, pins and GitTree
Anonymous fetches of private repos map to Auth, error text drops gix source locations and names the innermost network cause, and valid_branch is pub(crate) for the add form (pre-flight F1, F2, F13). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -40,6 +40,105 @@ pub(crate) fn hex(bytes: &[u8]) -> String {
|
||||
bytes.iter().map(|b| format!("{:02x}", b)).collect()
|
||||
}
|
||||
|
||||
/// A tree at one commit of a bare gix repository.
|
||||
pub struct GitTree {
|
||||
repo: gix::Repository,
|
||||
tree_id: gix::ObjectId,
|
||||
}
|
||||
|
||||
impl GitTree {
|
||||
pub fn open(repo_path: &std::path::Path, commit: &str) -> Result<Self, String> {
|
||||
let repo = gix::open(repo_path)
|
||||
.map_err(|e| format!("Could not open the marketplace cache: {}", e))?;
|
||||
let oid = gix::ObjectId::from_hex(commit.as_bytes())
|
||||
.map_err(|e| format!("Invalid commit id {}: {}", commit, e))?;
|
||||
let tree_id = repo
|
||||
.find_commit(oid)
|
||||
.map_err(|e| format!("Commit {} is not in the marketplace cache: {}", commit, e))?
|
||||
.tree_id()
|
||||
.map_err(|e| format!("Commit {} has no tree: {}", commit, e))?
|
||||
.detach();
|
||||
Ok(Self { repo, tree_id })
|
||||
}
|
||||
|
||||
fn root(&self) -> Result<gix::Tree<'_>, String> {
|
||||
self.repo
|
||||
.find_tree(self.tree_id)
|
||||
.map_err(|e| format!("Could not read tree {}: {}", self.tree_id, e))
|
||||
}
|
||||
|
||||
/// `(object id, mode)` of the entry at `path`, or `None`.
|
||||
fn lookup(
|
||||
&self,
|
||||
path: &str,
|
||||
) -> Result<Option<(gix::ObjectId, gix::object::tree::EntryMode)>, String> {
|
||||
if path.is_empty() {
|
||||
return Ok(Some((
|
||||
self.tree_id,
|
||||
gix::object::tree::EntryKind::Tree.into(),
|
||||
)));
|
||||
}
|
||||
let root = self.root()?;
|
||||
let entry = root
|
||||
.lookup_entry_by_path(path)
|
||||
.map_err(|e| format!("Could not look up {}: {}", path, e))?;
|
||||
Ok(entry.map(|e| (e.object_id(), e.mode())))
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeView for GitTree {
|
||||
fn list_dir(&self, path: &str) -> Result<Option<Vec<DirEntry>>, String> {
|
||||
let Some((id, mode)) = self.lookup(path)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
if !mode.is_tree() {
|
||||
return Ok(None);
|
||||
}
|
||||
let tree = self
|
||||
.repo
|
||||
.find_tree(id)
|
||||
.map_err(|e| format!("Could not read {}: {}", path, e))?;
|
||||
let mut out = Vec::new();
|
||||
for entry in tree.iter() {
|
||||
let entry = entry.map_err(|e| format!("Could not read {}: {:?}", path, e))?;
|
||||
let mode = entry.mode();
|
||||
let kind = if mode.is_tree() {
|
||||
EntryKind::Dir
|
||||
} else if mode.is_link() {
|
||||
EntryKind::Symlink
|
||||
} else if mode.is_blob() {
|
||||
EntryKind::File
|
||||
} else {
|
||||
EntryKind::Other
|
||||
};
|
||||
out.push(DirEntry {
|
||||
name: entry.filename().to_string(),
|
||||
kind,
|
||||
executable: mode.is_executable(),
|
||||
});
|
||||
}
|
||||
Ok(Some(out))
|
||||
}
|
||||
|
||||
fn read_file(&self, path: &str) -> Result<Option<Vec<u8>>, String> {
|
||||
let Some((id, mode)) = self.lookup(path)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
if !mode.is_blob() {
|
||||
return Ok(None);
|
||||
}
|
||||
let blob = self
|
||||
.repo
|
||||
.find_blob(id)
|
||||
.map_err(|e| format!("Could not read {}: {}", path, e))?;
|
||||
Ok(Some(blob.data.clone()))
|
||||
}
|
||||
|
||||
fn entry_id(&self, path: &str) -> Result<Option<String>, String> {
|
||||
Ok(self.lookup(path)?.map(|(id, _)| id.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[derive(Debug, Clone)]
|
||||
enum MemNode {
|
||||
|
||||
Reference in New Issue
Block a user