2019-11-25 02:42:45 +00:00
|
|
|
//! Tests for git garbage collection.
|
|
|
|
|
2017-09-09 01:30:37 +00:00
|
|
|
use std::env;
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2019-09-12 17:14:29 +00:00
|
|
|
use cargo_test_support::git;
|
2022-12-01 12:45:25 +00:00
|
|
|
use cargo_test_support::git::cargo_uses_gitoxide;
|
2019-09-12 17:14:29 +00:00
|
|
|
use cargo_test_support::paths;
|
|
|
|
use cargo_test_support::project;
|
|
|
|
use cargo_test_support::registry::Package;
|
2017-09-09 01:30:37 +00:00
|
|
|
|
|
|
|
use url::Url;
|
|
|
|
|
2022-10-28 02:56:41 +00:00
|
|
|
pub fn find_index() -> PathBuf {
|
2017-09-09 01:30:37 +00:00
|
|
|
let dir = paths::home().join(".cargo/registry/index");
|
|
|
|
dir.read_dir().unwrap().next().unwrap().unwrap().path()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_test(path_env: Option<&OsStr>) {
|
|
|
|
const N: usize = 50;
|
|
|
|
|
2018-07-20 11:47:47 +00:00
|
|
|
let foo = project()
|
2018-03-14 15:17:44 +00:00
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
2020-09-27 00:59:58 +00:00
|
|
|
[package]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
|
|
|
|
[dependencies]
|
|
|
|
bar = "*"
|
|
|
|
"#,
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.file("src/lib.rs", "")
|
2017-07-22 03:12:21 +00:00
|
|
|
.build();
|
2017-09-09 01:30:37 +00:00
|
|
|
Package::new("bar", "0.1.0").publish();
|
|
|
|
|
2023-02-15 23:54:30 +00:00
|
|
|
foo.cargo("check").run();
|
2017-09-09 01:30:37 +00:00
|
|
|
|
|
|
|
let index = find_index();
|
|
|
|
let path = paths::home().join("tmp");
|
|
|
|
let url = Url::from_file_path(&path).unwrap().to_string();
|
|
|
|
let repo = git2::Repository::init(&path).unwrap();
|
|
|
|
let index = git2::Repository::open(&index).unwrap();
|
|
|
|
let mut cfg = repo.config().unwrap();
|
|
|
|
cfg.set_str("user.email", "foo@bar.com").unwrap();
|
|
|
|
cfg.set_str("user.name", "Foo Bar").unwrap();
|
|
|
|
let mut cfg = index.config().unwrap();
|
|
|
|
cfg.set_str("user.email", "foo@bar.com").unwrap();
|
|
|
|
cfg.set_str("user.name", "Foo Bar").unwrap();
|
|
|
|
|
|
|
|
for _ in 0..N {
|
|
|
|
git::commit(&repo);
|
2018-03-14 15:17:44 +00:00
|
|
|
index
|
|
|
|
.remote_anonymous(&url)
|
|
|
|
.unwrap()
|
|
|
|
.fetch(&["refs/heads/master:refs/remotes/foo/master"], None, None)
|
|
|
|
.unwrap();
|
2017-09-09 01:30:37 +00:00
|
|
|
}
|
|
|
|
drop((repo, index));
|
|
|
|
Package::new("bar", "0.1.1").publish();
|
|
|
|
|
2018-03-14 15:17:44 +00:00
|
|
|
let before = find_index()
|
|
|
|
.join(".git/objects/pack")
|
|
|
|
.read_dir()
|
|
|
|
.unwrap()
|
|
|
|
.count();
|
2017-09-09 01:30:37 +00:00
|
|
|
assert!(before > N);
|
|
|
|
|
|
|
|
let mut cmd = foo.cargo("update");
|
|
|
|
cmd.env("__CARGO_PACKFILE_LIMIT", "10");
|
|
|
|
if let Some(path) = path_env {
|
|
|
|
cmd.env("PATH", path);
|
|
|
|
}
|
2019-01-27 18:34:11 +00:00
|
|
|
cmd.env("CARGO_LOG", "trace");
|
2018-08-28 09:20:03 +00:00
|
|
|
cmd.run();
|
2018-03-14 15:17:44 +00:00
|
|
|
let after = find_index()
|
|
|
|
.join(".git/objects/pack")
|
|
|
|
.read_dir()
|
|
|
|
.unwrap()
|
|
|
|
.count();
|
|
|
|
assert!(
|
|
|
|
after < before,
|
|
|
|
"packfiles before: {}\n\
|
|
|
|
packfiles after: {}",
|
|
|
|
before,
|
|
|
|
after
|
|
|
|
);
|
2017-09-09 01:30:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 06:10:33 +00:00
|
|
|
#[cargo_test(requires_git)]
|
2017-09-09 01:30:37 +00:00
|
|
|
fn use_git_gc() {
|
|
|
|
run_test(None);
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2017-09-09 01:30:37 +00:00
|
|
|
fn avoid_using_git() {
|
2022-12-01 12:45:25 +00:00
|
|
|
if cargo_uses_gitoxide() {
|
|
|
|
// file protocol without git binary is currently not possible - needs built-in upload-pack.
|
|
|
|
// See https://github.com/Byron/gitoxide/issues/734 (support for the file protocol) progress updates.
|
|
|
|
return;
|
|
|
|
}
|
2017-09-24 14:26:37 +00:00
|
|
|
let path = env::var_os("PATH").unwrap_or_default();
|
2017-09-09 01:30:37 +00:00
|
|
|
let mut paths = env::split_paths(&path).collect::<Vec<_>>();
|
2018-03-14 15:17:44 +00:00
|
|
|
let idx = paths
|
|
|
|
.iter()
|
|
|
|
.position(|p| p.join("git").exists() || p.join("git.exe").exists());
|
2017-09-09 01:30:37 +00:00
|
|
|
match idx {
|
2018-03-14 15:17:44 +00:00
|
|
|
Some(i) => {
|
|
|
|
paths.remove(i);
|
|
|
|
}
|
2017-09-09 01:30:37 +00:00
|
|
|
None => return,
|
|
|
|
}
|
|
|
|
run_test(Some(&env::join_paths(&paths).unwrap()));
|
|
|
|
}
|