Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallelization #27

Merged
merged 2 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ semver = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
anyhow = "1.0.14"
rayon = "1"
78 changes: 44 additions & 34 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,7 +13,10 @@ use url::Url;

use crate::package::Package;

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

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
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 @@ -228,28 +226,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