use anyhow::{bail, Context, Result};
use std::path::Path;
pub fn bare_clone(url: &str, dest: &Path) -> Result<()> {
if let Some(parent) = dest.parent() {
std::fs::create_dir_all(parent)
.with_context(|| format!("Failed to create directory {}", parent.display()))?;
}
let status = super::git_cmd()
.args(["clone", "--bare", url])
.arg(dest)
.status()
.context("Failed to run `git clone --bare`")?;
if !status.success() {
bail!("git clone --bare failed for {url}"); }
let fetch_refspec = "+refs/heads/*:refs/remotes/origin/*";
let status = super::git_cmd()
.args(["-C"])
.arg(dest)
.args(["config", "remote.origin.fetch", fetch_refspec])
.status()
.context("Failed to configure remote.origin.fetch")?;
if !status.success() {
bail!("Failed to set remote.origin.fetch"); }
let status = super::git_cmd()
.args(["-C"])
.arg(dest)
.args(["fetch", "origin"])
.status()
.context("Failed to run `git fetch origin`")?;
if !status.success() {
bail!("git fetch origin failed after bare clone"); }
Ok(())
}
pub fn git_fetch(bare: &Path) -> Result<()> {
let status = super::git_cmd()
.args(["-C"])
.arg(bare)
.args(["fetch", "origin"])
.status()
.context("Failed to run `git fetch`")?;
if !status.success() {
bail!("git fetch origin failed"); }
Ok(())
}