fad06fd
use std::path::Path;
use crate::GitBaby;
use crate::command::env::Env;
use crate::error::BabyError;
use crate::setup::helper;
use crate::setup::types::{FastImportCommit, ObjectFormat};
fn empty_env() -> Env {
Env::new()
}
impl GitBaby {
pub async fn init_repository(
&self,
path: &str,
bare: bool,
object_format: Option<ObjectFormat>,
) -> Result<(), BabyError> {
if path.is_empty() {
return Err(BabyError::Custom(
"repository path must not be empty".to_string(),
));
}
if path.starts_with('-') {
return Err(BabyError::InvalidGitArg(format!(
"repository path starts with `-`: `{}`",
path
)));
}
let env = empty_env();
let of = object_format.map(|f| f.as_str());
let mut cmd =
helper::build_init_cmd("gitbaby::setup", Path::new(path), env, path, bare, of)?;
let output = cmd.run_soft().await.map_err(BabyError::from)?;
if output.status.success() {
return Ok(());
}
Err(BabyError::Custom(helper::classify_stderr_init(
&String::from_utf8_lossy(&output.stderr),
)))
}
pub async fn is_repository_exists(&self, path: &str) -> bool {
if path.is_empty() {
return false;
}
if path.starts_with('-') {
return false;
}
let env = empty_env();
let p = Path::new(path);
let mut cmd = helper::build_is_repository_exists_cmd("gitbaby::setup", p, env);
match cmd.run().await {
Ok(output) => output.status.success(),
Err(_) => false,
}
}
pub async fn fast_import(
&self,
dir: &Path,
commits: Vec<FastImportCommit>,
) -> Result<(), BabyError> {
if commits.is_empty() {
return Err(BabyError::FastImportFailed(
"no commits provided".to_string(),
));
}
let env = empty_env();
let stream = helper::render_fast_import_stream(&commits)?;
let mut cmd = helper::build_fast_import_cmd("gitbaby::setup", dir, env);
cmd.spawn().await.map_err(BabyError::from)?;
cmd.feed(&stream).await.map_err(BabyError::from)?;
let output = cmd.run_soft().await.map_err(BabyError::from)?;
if output.status.success() {
let _ = crate::cache::invalidation::flush_cache().await;
return Ok(());
}
Err(BabyError::FastImportFailed(
String::from_utf8_lossy(&output.stderr).trim().to_string(),
))
}
}