text/x-rust
•
1.40 KB
•
63 lines
use std::{io, path::PathBuf};
use anyhow::Result;
use gix::Repository;
use tokio::{fs, process::Command};
use crate::{services::validation::validate_slug, state::GlobalState};
fn ensure_safe_component(value: &str) -> io::Result<()> {
if value.is_empty() {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Invalid path component",
));
}
if let Err(msg) = validate_slug(value) {
return Err(io::Error::new(io::ErrorKind::InvalidInput, msg));
}
Ok(())
}
pub async fn create_bare_repo(
state: &GlobalState,
user: String,
project: String,
) -> Result<(), std::io::Error> {
ensure_safe_component(&user)?;
ensure_safe_component(&project)?;
let path = state.config.git_root.join(user);
fs::create_dir_all(&path).await?;
let path = path.join(project);
let status = Command::new("git")
.arg("init")
.arg("--bare")
.arg(path)
.kill_on_drop(true) // makes shutdowns cleaner
.status()
.await?;
if status.success() {
Ok(())
} else {
Err(std::io::Error::other("git init --bare failed"))
}
}
pub struct GitRepository {
path: PathBuf,
repo: Repository,
}
impl GitRepository {
pub fn new(path: PathBuf) -> Result<Self> {
let repo = gix::open(path.clone())?;
Ok(Self {
path,
repo,
})
}
}