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

feat: use shallow clones for gritmodules #543

Merged
merged 2 commits into from
Oct 17, 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
10 changes: 5 additions & 5 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ indicatif-log-bridge = { version = "0.2.1" }
colored = { version = "2.0.4" }
log = { version = "0.4.19" }
env_logger = { version = "0.10.0" }
git2 = { version = "0.17.2" }
git2 = { version = "0.19.0" }
regex = { version = "1.7.3" }
grit-util = { path = "../grit-util" }
marzano-core = { path = "../core", features = [
Expand Down
2 changes: 1 addition & 1 deletion crates/gritmodule/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ serde_yaml = { version = "0.9.25" }
anyhow = { version = "1.0.70" }
futures = { version = "0.3.29" }
rand = { version = "0.8.5" }
git2 = { version = "0.17.2", default-features = false, features = [
git2 = { version = "0.19.0", default-features = false, features = [
"vendored-openssl",
] }
lazy_static = { version = "1.4.0" }
Expand Down
33 changes: 31 additions & 2 deletions crates/gritmodule/src/fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::{Path, PathBuf};

use anyhow::{anyhow, bail, Result};
use git2::Repository;
use git2::{build::RepoBuilder, FetchOptions, Repository};
use regex::Regex;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -271,7 +271,12 @@ fn clone_repo<'a>(
None => repo.remote.to_string(),
};

match Repository::clone(&remote, target_dir) {
let mut cloner = RepoBuilder::new();
let mut options = FetchOptions::new();
options.depth(1);
cloner.fetch_options(options);

match cloner.clone(&remote, target_dir) {
Ok(_) => {}
Err(e) => {
if !target_dir.exists() {
Expand Down Expand Up @@ -447,6 +452,30 @@ mod tests {
);
}

#[test]
fn shallow_clone() {
let dir = tempdir().unwrap();
let fetcher = CleanFetcher::new(dir.path().to_path_buf(), None);
let repo = ModuleRepo {
host: "github.com".to_string(),
full_name: "getgrit/stdlib".to_string(),
remote: "http://github.com/getgrit/stdlib.git".to_string(),
provider_name: "github.com/getgrit/stdlib".to_string(),
};
let gritmodule_dir = fetcher.fetch_grit_module(&repo).unwrap();
let module_dir = dir.path().join("github.com/getgrit/stdlib");
assert_eq!(gritmodule_dir, module_dir.to_str().unwrap());
let output = std::process::Command::new("git")
.arg("rev-parse")
.arg("--is-shallow-repository")
.current_dir(&module_dir)
.output()
.expect("Failed to execute git command");

let is_shallow = String::from_utf8_lossy(&output.stdout).trim() == "true";
assert!(is_shallow, "Repository is not shallow");
}

#[test]
fn module_repo_from_https_remote() {
let remote = "https://github.com/getgrit/rewriter.git";
Expand Down
Loading