Skip to main content

worktree_io/git/
clone.rs

1use anyhow::{bail, Context, Result};
2use std::path::Path;
3use std::process::Command;
4
5pub fn bare_clone(url: &str, dest: &Path) -> Result<()> {
6    if let Some(parent) = dest.parent() {
7        std::fs::create_dir_all(parent)
8            .with_context(|| format!("Failed to create directory {}", parent.display()))?;
9    }
10
11    let status = Command::new("git")
12        .args(["clone", "--bare", url])
13        .arg(dest)
14        .status()
15        .context("Failed to run `git clone --bare`")?;
16
17    if !status.success() {
18        bail!("git clone --bare failed for {url}"); // LLVM_COV_EXCL_LINE
19    }
20
21    let fetch_refspec = "+refs/heads/*:refs/remotes/origin/*";
22    let status = Command::new("git")
23        .args(["-C"])
24        .arg(dest)
25        .args(["config", "remote.origin.fetch", fetch_refspec])
26        .status()
27        .context("Failed to configure remote.origin.fetch")?;
28
29    if !status.success() {
30        bail!("Failed to set remote.origin.fetch"); // LLVM_COV_EXCL_LINE
31    }
32
33    let status = Command::new("git")
34        .args(["-C"])
35        .arg(dest)
36        .args(["fetch", "origin"])
37        .status()
38        .context("Failed to run `git fetch origin`")?;
39
40    if !status.success() {
41        bail!("git fetch origin failed after bare clone"); // LLVM_COV_EXCL_LINE
42    }
43
44    Ok(())
45}
46
47pub fn git_fetch(bare: &Path) -> Result<()> {
48    let status = Command::new("git")
49        .args(["-C"])
50        .arg(bare)
51        .args(["fetch", "origin"])
52        .status()
53        .context("Failed to run `git fetch`")?;
54
55    if !status.success() {
56        bail!("git fetch origin failed"); // LLVM_COV_EXCL_LINE
57    }
58    Ok(())
59}