use crate::error::Result;
use colored::*;
#[cfg(unix)]
use std::fs;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::process::Command;
pub fn init_repository(project_path: &Path) -> Result<()> {
let status = Command::new("git")
.arg("init")
.current_dir(project_path)
.status()?;
if !status.success() {
return Err(anyhow::anyhow!("Failed to initialize git repository"));
}
println!("{}", "✓ Initialized git repository".green());
Ok(())
}
pub fn add_remote(project_path: &Path, remote_url: &str) -> Result<()> {
let status = Command::new("git")
.args(["remote", "add", "origin", remote_url])
.current_dir(project_path)
.status()?;
if !status.success() {
return Err(anyhow::anyhow!("Failed to add git remote"));
}
Ok(())
}
pub fn initial_commit(project_path: &Path) -> Result<()> {
Command::new("git")
.args(["add", "."])
.current_dir(project_path)
.status()?;
let status = Command::new("git")
.args(["commit", "-m", "Initial commit"])
.current_dir(project_path)
.status()?;
if !status.success() {
return Err(anyhow::anyhow!("Failed to create initial commit"));
}
println!("{}", "✓ Created initial commit".green());
Ok(())
}
pub fn push_to_remote(project_path: &Path) -> Result<()> {
let status = Command::new("git")
.args(["push", "-u", "origin", "main"])
.current_dir(project_path)
.status()?;
if !status.success() {
return Err(anyhow::anyhow!("Failed to push to remote"));
}
println!("{}", "✓ Pushed to remote repository".green());
Ok(())
}
pub fn make_hooks_executable(project_path: &Path) -> Result<()> {
#[cfg(unix)]
{
let hook_path = project_path.join(".githooks/pre-commit");
if hook_path.exists() {
let mut permissions = fs::metadata(&hook_path)?.permissions();
permissions.set_mode(permissions.mode() | 0o111);
fs::set_permissions(&hook_path, permissions)?;
}
}
Ok(())
}
pub fn configure_hooks(project_path: &Path) -> Result<()> {
make_hooks_executable(project_path)?;
let status = Command::new("git")
.args(["config", "core.hooksPath", ".githooks"])
.current_dir(project_path)
.status()?;
if status.success() {
println!("{}", "✓ Configured git hooks".green());
}
Ok(())
}