Skip to content

Commit

Permalink
feat: Use Git CLI for cloning repositories by default
Browse files Browse the repository at this point in the history
  • Loading branch information
siketyan committed Nov 22, 2022
1 parent 1dbebb9 commit 22d6df1
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/cmd/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use tracing::info;

use crate::config::Config;
use crate::console::create_spinner;
use crate::git::CloneRepository;
use crate::path::Path;
use crate::root::Root;
use crate::url::Url;
Expand Down Expand Up @@ -41,13 +42,15 @@ impl Cmd {
});

let url = Url::from_str(&self.repo)?;
let path = Path::resolve(&root, &url);
let path = PathBuf::from(Path::resolve(&root, &url));
let profile = config
.rules
.resolve(&url)
.and_then(|r| config.profiles.resolve(&r.profile));

let repo = Repository::clone(&url.to_string(), PathBuf::from(&path))?;
config.git.strategy.clone.clone_repository(url, &path)?;

let repo = Repository::open(&path)?;
if let Some((name, p)) = profile {
p.apply(&mut repo.config()?)?;

Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ use std::fs::read_to_string;
use anyhow::Result;
use serde::Deserialize;

use crate::git::Config as GitConfig;
use crate::profile::Profiles;
use crate::root::Root;
use crate::rule::Rules;

#[derive(Debug, Default, Deserialize)]
pub struct Config {
#[serde(default)]
pub git: GitConfig,
#[serde(default)]
pub profiles: Profiles,
#[serde(default)]
Expand Down
15 changes: 15 additions & 0 deletions src/git/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use serde::Deserialize;

use crate::git::strategy::Strategy;

#[derive(Debug, Default, Deserialize)]
pub struct StrategyConfig {
#[serde(default)]
pub clone: Strategy,
}

#[derive(Debug, Default, Deserialize)]
pub struct Config {
#[serde(default)]
pub strategy: StrategyConfig,
}
15 changes: 15 additions & 0 deletions src/git/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mod config;
mod strategy;

pub use config::Config;

use std::path::Path;

use anyhow::Result;

pub trait CloneRepository {
fn clone_repository<U, P>(&self, url: U, path: P) -> Result<()>
where
U: ToString,
P: AsRef<Path>;
}
24 changes: 24 additions & 0 deletions src/git/strategy/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::path::Path;
use std::process::Command;

use tracing::debug;

use crate::git::CloneRepository;

pub struct Cli;

impl CloneRepository for Cli {
fn clone_repository<U, P>(&self, url: U, path: P) -> anyhow::Result<()>
where
U: ToString,
P: AsRef<Path>,
{
debug!("Cloning the repository using CLI strategy");

let _ = Command::new("git")
.args(["clone", &url.to_string(), path.as_ref().to_str().unwrap()])
.output()?;

Ok(())
}
}
22 changes: 22 additions & 0 deletions src/git/strategy/git2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::path::Path;

use git2::Repository;
use tracing::debug;

use crate::git::CloneRepository;

pub struct Git2;

impl CloneRepository for Git2 {
fn clone_repository<U, P>(&self, url: U, path: P) -> anyhow::Result<()>
where
U: ToString,
P: AsRef<Path>,
{
debug!("Cloning the repository using Git2 strategy");

let _ = Repository::clone(&url.to_string(), path)?;

Ok(())
}
}
30 changes: 30 additions & 0 deletions src/git/strategy/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
mod cli;
mod git2;

pub use {self::git2::Git2, cli::Cli};

use std::path::Path;

use serde::Deserialize;

use crate::git::CloneRepository;

#[derive(Debug, Default, Deserialize)]
pub enum Strategy {
#[default]
Cli,
Git2,
}

impl CloneRepository for Strategy {
fn clone_repository<U, P>(&self, url: U, path: P) -> anyhow::Result<()>
where
U: ToString,
P: AsRef<Path>,
{
match self {
Self::Cli => Cli.clone_repository(url, path),
Self::Git2 => Git2.clone_repository(url, path),
}
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod cmd;
mod config;
mod console;
mod git;
mod path;
mod profile;
mod root;
Expand Down

0 comments on commit 22d6df1

Please sign in to comment.