Skip to content

Commit

Permalink
chore: prepare for parallelization
Browse files Browse the repository at this point in the history
  • Loading branch information
paolobarbolini committed Apr 6, 2024
1 parent e97a51a commit 316c32d
Show file tree
Hide file tree
Showing 3 changed files with 305 additions and 254 deletions.
76 changes: 43 additions & 33 deletions src/git.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
cmp::Ordering,
collections::BTreeSet,
fmt::{self, Display},
path::{Path, PathBuf},
process::Command,
str,
Expand All @@ -12,6 +13,9 @@ use url::Url;

use crate::package::Package;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct GitUrl(Url);

#[derive(Debug)]
pub struct GitRepository {
repo_dir: PathBuf,
Expand All @@ -32,21 +36,15 @@ pub struct GitTag<'a> {
}

impl GitRepository {
pub fn obtain(dir: &Path, url: Url) -> Result<Self> {
let normalized_url = normalize_url(url)?;

let name = format!(
"{}-{}",
normalized_url.host().unwrap(),
normalized_url.path().replace('/', "-")
);
pub fn obtain(dir: &Path, GitUrl(url): GitUrl) -> Result<Self> {
let name = format!("{}-{}", url.host().unwrap(), url.path().replace('/', "-"));
let repo_dir = dir.join(name);
if !repo_dir.try_exists()? {
let out = Command::new("git")
.arg("clone")
.arg("--filter=blob:none")
.arg("--")
.arg(normalized_url.to_string())
.arg(url.to_string())
.arg(&repo_dir)
.env("GIT_TERMINAL_PROMPT", "0")
.output()?;
Expand Down Expand Up @@ -227,28 +225,40 @@ impl<'a> Ord for GitTag<'a> {
}
}

fn normalize_url(url: Url) -> Result<Url> {
ensure!(
matches!(url.scheme(), "http" | "https"),
"Bad repository scheme"
);
let host = url
.host()
.context("repository doesn't have a `host`")?
.to_string();

Ok(if host == "github.com" || host.starts_with("gitlab.") {
let mut url = url;
let mut path = url.path().strip_prefix('/').unwrap().split('/');
url.set_path(&format!(
"/{}/{}.git",
path.next().context("repository is missing user/org")?,
path.next()
.context("repository is missing repo name")?
.trim_end_matches(".git")
));
url
} else {
url
})
impl TryFrom<Url> for GitUrl {
type Error = anyhow::Error;

fn try_from(url: Url) -> Result<Self, Self::Error> {
ensure!(
matches!(url.scheme(), "http" | "https"),
"Bad repository scheme"
);
let host = url
.host()
.context("repository doesn't have a `host`")?
.to_string();

Ok(Self(
if host == "github.com" || host.starts_with("gitlab.") {
let mut url = url;
let mut path = url.path().strip_prefix('/').unwrap().split('/');
url.set_path(&format!(
"/{}/{}.git",
path.next().context("repository is missing user/org")?,
path.next()
.context("repository is missing repo name")?
.trim_end_matches(".git")
));
url
} else {
url
},
))
}
}

impl Display for GitUrl {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
Loading

0 comments on commit 316c32d

Please sign in to comment.