use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use thiserror::Error;
use crate::config::{Marketplace, MarketplaceSource};
use crate::git;
use crate::paths::expand_tilde;
#[derive(Debug, Error)]
pub enum SyncError {
#[error("marketplace '{name}' not yet cloned (run `llmenv plugin-sync` to fetch)")]
NotCloned { name: String },
#[error("git clone failed for '{name}': {source}")]
CloneFailed {
name: String,
#[source]
source: anyhow::Error,
},
#[error(transparent)]
Other(#[from] anyhow::Error),
}
#[must_use]
pub fn marketplace_cache_root(cache_dir: &Path) -> PathBuf {
cache_dir.join("marketplaces")
}
#[must_use]
pub fn marketplace_path(cache_dir: &Path, name: &str) -> PathBuf {
marketplace_cache_root(cache_dir).join(name)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MarketplaceState {
pub install_location: PathBuf,
pub head: Option<String>,
}
pub trait GitBackend {
fn clone(&self, source: &str, dest: &Path) -> Result<()>;
fn pull(&self, repo: &Path) -> Result<()>;
fn head(&self, repo: &Path) -> Option<String>;
}
#[derive(Debug, Default, Clone, Copy)]
pub struct SystemGit;
impl GitBackend for SystemGit {
fn clone(&self, source: &str, dest: &Path) -> Result<()> {
git_clone(source, dest)
}
fn pull(&self, repo: &Path) -> Result<()> {
git_pull(repo)
}
fn head(&self, repo: &Path) -> Option<String> {
git_head(repo)
}
}
pub fn sync_marketplace(
cache_dir: &Path,
m: &Marketplace,
refresh: bool,
) -> Result<MarketplaceState, SyncError> {
sync_marketplace_with(cache_dir, m, refresh, &SystemGit)
}
pub fn sync_marketplace_with(
cache_dir: &Path,
m: &Marketplace,
refresh: bool,
git: &dyn GitBackend,
) -> Result<MarketplaceState, SyncError> {
match m.classify_source() {
MarketplaceSource::Path => sync_path(m),
MarketplaceSource::Git => sync_git(cache_dir, m, refresh, git),
}
}
fn sync_path(m: &Marketplace) -> Result<MarketplaceState, SyncError> {
let expanded = expand_tilde(&m.source);
let path = PathBuf::from(&expanded);
if !path.exists() {
return Err(SyncError::Other(anyhow::anyhow!(
"marketplace '{}': path source does not exist: {}",
m.name,
path.display()
)));
}
let canonical = std::fs::canonicalize(&path).map_err(|e| {
SyncError::Other(anyhow::anyhow!(
"marketplace '{}': canonicalizing path source {}: {e}",
m.name,
path.display()
))
})?;
Ok(MarketplaceState {
install_location: canonical,
head: None,
})
}
fn sync_git(
cache_dir: &Path,
m: &Marketplace,
refresh: bool,
git: &dyn GitBackend,
) -> Result<MarketplaceState, SyncError> {
reject_unsafe_source(&m.source).map_err(SyncError::Other)?;
let dest = marketplace_path(cache_dir, &m.name);
if dest.join(".git").exists() {
if refresh {
git.pull(&dest).map_err(SyncError::Other)?;
}
} else if !refresh {
return Err(SyncError::NotCloned {
name: m.name.clone(),
});
} else {
std::fs::create_dir_all(marketplace_cache_root(cache_dir)).map_err(|e| {
SyncError::Other(anyhow::anyhow!("creating marketplace cache root: {e}"))
})?;
git.clone(&m.source, &dest)
.map_err(|e| SyncError::CloneFailed {
name: m.name.clone(),
source: e,
})?;
}
let head = git.head(&dest);
Ok(MarketplaceState {
install_location: dest,
head,
})
}
fn reject_unsafe_source(source: &str) -> Result<()> {
if source.starts_with('-') {
return Err(anyhow::anyhow!(
"marketplace source may not start with '-': {source}"
));
}
if source.starts_with("ext::") || source.starts_with("fd::") {
return Err(anyhow::anyhow!(
"marketplace source uses a disallowed git transport: {source}"
));
}
Ok(())
}
fn git_clone(source: &str, dest: &Path) -> Result<()> {
let output = git::secure_git()
.args(["clone", "--depth", "1", "--", source])
.arg(dest)
.output()
.context("spawning git clone")?;
if !output.status.success() {
anyhow::bail!(
"git clone failed for {}: {}",
git::sanitize_git_url(source),
git::git_failure_detail(&output.stderr, &output.stdout, output.status)
);
}
Ok(())
}
fn git_pull(repo: &Path) -> Result<()> {
let fetch_out = git::secure_git()
.args(["fetch", "--depth", "1"])
.current_dir(repo)
.output()
.context("spawning git fetch")?;
if !fetch_out.status.success() {
anyhow::bail!(
"git fetch failed at {}: {}",
repo.display(),
git::git_failure_detail(&fetch_out.stderr, &fetch_out.stdout, fetch_out.status)
);
}
let reset_out = git::secure_git()
.args(["reset", "--hard", "@{u}"])
.current_dir(repo)
.output()
.context("spawning git reset")?;
if !reset_out.status.success() {
tracing::debug!(
"marketplace refresh did not fast-forward at {}: {}",
repo.display(),
git::git_failure_detail(&reset_out.stderr, &reset_out.stdout, reset_out.status)
);
}
Ok(())
}
fn git_head(repo: &Path) -> Option<String> {
let output = match git::secure_git()
.args(["rev-parse", "HEAD"])
.current_dir(repo)
.output()
{
Ok(out) => out,
Err(e) => {
tracing::debug!("git rev-parse HEAD failed at {}: {}", repo.display(), e);
return None;
}
};
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
tracing::debug!(
"git rev-parse HEAD failed at {} with exit {}: {}",
repo.display(),
output.status,
stderr
);
return None;
}
match String::from_utf8(output.stdout) {
Ok(sha) => {
let sha = sha.trim().to_string();
if sha.is_empty() { None } else { Some(sha) }
}
Err(e) => {
tracing::debug!(
"git rev-parse HEAD output invalid UTF-8 at {}: {}",
repo.display(),
e
);
None
}
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
use super::*;
#[test]
fn path_source_resolves_in_place() {
let tmp = tempfile::tempdir().unwrap();
let src = tmp.path().join("my-plugins");
std::fs::create_dir(&src).unwrap();
let m = Marketplace {
name: "local".into(),
source: src.to_string_lossy().into_owned(),
};
let cache = tempfile::tempdir().unwrap();
let state = sync_marketplace(cache.path(), &m, false).unwrap();
assert_eq!(state.head, None);
assert_eq!(
std::fs::canonicalize(&state.install_location).unwrap(),
std::fs::canonicalize(&src).unwrap()
);
}
#[test]
fn missing_path_source_errors() {
let m = Marketplace {
name: "gone".into(),
source: "/nonexistent/path/to/marketplace".into(),
};
let cache = tempfile::tempdir().unwrap();
assert!(sync_marketplace(cache.path(), &m, false).is_err());
}
#[test]
fn git_not_cloned_on_export_returns_notcloned() {
struct NoGit;
impl GitBackend for NoGit {
fn clone(&self, _: &str, _: &std::path::Path) -> Result<()> {
unreachable!("should not attempt clone on export (refresh=false)")
}
fn pull(&self, _: &std::path::Path) -> Result<()> {
unreachable!("should not attempt pull")
}
fn head(&self, _: &std::path::Path) -> Option<String> {
None
}
}
let m = Marketplace {
name: "remote".into(),
source: "https://github.com/example/plugins".into(),
};
let cache = tempfile::tempdir().unwrap();
let result = sync_marketplace_with(cache.path(), &m, false, &NoGit);
match result {
Err(SyncError::NotCloned { name }) => {
assert_eq!(name, "remote");
}
other => panic!("expected NotCloned, got {other:?}"),
}
}
#[test]
fn git_clone_failure_returns_clonefailed() {
struct FailClone;
impl GitBackend for FailClone {
fn clone(&self, _: &str, _: &std::path::Path) -> Result<()> {
anyhow::bail!("simulated clone failure")
}
fn pull(&self, _: &std::path::Path) -> Result<()> {
unreachable!()
}
fn head(&self, _: &std::path::Path) -> Option<String> {
None
}
}
let m = Marketplace {
name: "broken".into(),
source: "https://github.com/example/plugins".into(),
};
let cache = tempfile::tempdir().unwrap();
let result = sync_marketplace_with(cache.path(), &m, true, &FailClone);
match result {
Err(SyncError::CloneFailed { name, .. }) => {
assert_eq!(name, "broken");
}
other => panic!("expected CloneFailed, got {other:?}"),
}
}
#[test]
fn cache_paths_are_under_marketplaces_dir() {
let root = Path::new("/cache");
assert_eq!(
marketplace_path(root, "superpowers"),
PathBuf::from("/cache/marketplaces/superpowers")
);
}
#[test]
fn git_config_flags_protect_against_hooks() {
use crate::git::GIT_CONFIG_FLAGS;
let flags = GIT_CONFIG_FLAGS;
assert_eq!(
flags,
&[
"-c",
"core.fsmonitor=false",
"-c",
"core.hooksPath=/dev/null"
]
);
}
#[test]
fn git_commands_with_null_stdin_fail_fast_not_hang() {
let tmp = tempfile::tempdir().unwrap();
let head = git_head(tmp.path());
assert!(head.is_none(), "git_head on non-repo should return None");
let dest = tmp.path().join("clone_dest");
let err = git_clone("file:///nonexistent/repo", &dest);
assert!(
err.is_err(),
"git_clone on invalid source should fail, not hang"
);
let err = git_pull(tmp.path());
assert!(err.is_err(), "git_pull on non-repo should fail, not hang");
}
}