2021-07-07 04:14:20 -05:00
|
|
|
use std::ffi::OsStr;
|
2024-03-08 14:41:29 -06:00
|
|
|
use std::hash::{Hash, Hasher};
|
2022-10-23 09:22:55 -05:00
|
|
|
use std::path::{Path, PathBuf};
|
2021-07-07 04:14:20 -05:00
|
|
|
use std::process::Command;
|
2024-10-03 05:15:15 -05:00
|
|
|
use std::{fs, io};
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2023-08-09 13:20:12 -05:00
|
|
|
use crate::path::{Dirs, RelPath};
|
2024-09-23 06:20:46 -05:00
|
|
|
use crate::utils::{copy_dir_recursively, ensure_empty_dir, spawn_and_wait};
|
2022-12-14 12:30:46 -06:00
|
|
|
|
|
|
|
pub(crate) fn prepare(dirs: &Dirs) {
|
2023-06-15 12:56:01 -05:00
|
|
|
RelPath::DOWNLOAD.ensure_exists(dirs);
|
2023-08-09 13:20:12 -05:00
|
|
|
crate::tests::RAND_REPO.fetch(dirs);
|
|
|
|
crate::tests::REGEX_REPO.fetch(dirs);
|
2021-07-07 04:14:20 -05:00
|
|
|
}
|
|
|
|
|
2022-10-23 09:22:55 -05:00
|
|
|
pub(crate) struct GitRepo {
|
|
|
|
url: GitRepoUrl,
|
|
|
|
rev: &'static str,
|
2023-06-15 12:56:01 -05:00
|
|
|
content_hash: &'static str,
|
2022-10-23 09:22:55 -05:00
|
|
|
patch_name: &'static str,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum GitRepoUrl {
|
|
|
|
Github { user: &'static str, repo: &'static str },
|
|
|
|
}
|
|
|
|
|
2023-06-15 12:56:01 -05:00
|
|
|
// Note: This uses a hasher which is not cryptographically secure. This is fine as the hash is meant
|
|
|
|
// to protect against accidental modification and outdated downloads, not against manipulation.
|
|
|
|
fn hash_file(file: &std::path::Path) -> u64 {
|
|
|
|
let contents = std::fs::read(file).unwrap();
|
|
|
|
#[allow(deprecated)]
|
|
|
|
let mut hasher = std::hash::SipHasher::new();
|
2024-03-08 14:41:29 -06:00
|
|
|
// The following is equivalent to
|
|
|
|
// std::hash::Hash::hash(&contents, &mut hasher);
|
|
|
|
// but gives the same result independent of host byte order.
|
|
|
|
hasher.write_usize(contents.len().to_le());
|
|
|
|
Hash::hash_slice(&contents, &mut hasher);
|
2023-06-15 12:56:01 -05:00
|
|
|
std::hash::Hasher::finish(&hasher)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hash_dir(dir: &std::path::Path) -> u64 {
|
|
|
|
let mut sub_hashes = std::collections::BTreeMap::new();
|
|
|
|
for entry in std::fs::read_dir(dir).unwrap() {
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
if entry.file_type().unwrap().is_dir() {
|
2024-03-08 14:41:29 -06:00
|
|
|
sub_hashes.insert(
|
|
|
|
entry.file_name().to_str().unwrap().to_owned(),
|
|
|
|
hash_dir(&entry.path()).to_le(),
|
|
|
|
);
|
2023-06-15 12:56:01 -05:00
|
|
|
} else {
|
2024-03-08 14:41:29 -06:00
|
|
|
sub_hashes.insert(
|
|
|
|
entry.file_name().to_str().unwrap().to_owned(),
|
|
|
|
hash_file(&entry.path()).to_le(),
|
|
|
|
);
|
2023-06-15 12:56:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[allow(deprecated)]
|
|
|
|
let mut hasher = std::hash::SipHasher::new();
|
2024-03-08 14:41:29 -06:00
|
|
|
// The following is equivalent to
|
|
|
|
// std::hash::Hash::hash(&sub_hashes, &mut hasher);
|
|
|
|
// but gives the same result independent of host byte order.
|
|
|
|
hasher.write_usize(sub_hashes.len().to_le());
|
|
|
|
for elt in sub_hashes {
|
|
|
|
elt.hash(&mut hasher);
|
|
|
|
}
|
2023-06-15 12:56:01 -05:00
|
|
|
std::hash::Hasher::finish(&hasher)
|
|
|
|
}
|
|
|
|
|
2022-10-23 09:22:55 -05:00
|
|
|
impl GitRepo {
|
2022-12-14 12:30:46 -06:00
|
|
|
pub(crate) const fn github(
|
2022-10-23 09:22:55 -05:00
|
|
|
user: &'static str,
|
|
|
|
repo: &'static str,
|
|
|
|
rev: &'static str,
|
2023-06-15 12:56:01 -05:00
|
|
|
content_hash: &'static str,
|
2022-10-23 09:22:55 -05:00
|
|
|
patch_name: &'static str,
|
|
|
|
) -> GitRepo {
|
2023-06-15 12:56:01 -05:00
|
|
|
GitRepo { url: GitRepoUrl::Github { user, repo }, rev, content_hash, patch_name }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn download_dir(&self, dirs: &Dirs) -> PathBuf {
|
|
|
|
match self.url {
|
|
|
|
GitRepoUrl::Github { user: _, repo } => RelPath::DOWNLOAD.join(repo).to_path(dirs),
|
|
|
|
}
|
2022-10-23 09:22:55 -05:00
|
|
|
}
|
|
|
|
|
2022-12-14 12:30:46 -06:00
|
|
|
pub(crate) const fn source_dir(&self) -> RelPath {
|
2022-10-23 09:22:55 -05:00
|
|
|
match self.url {
|
2023-06-15 12:56:01 -05:00
|
|
|
GitRepoUrl::Github { user: _, repo } => RelPath::BUILD.join(repo),
|
2022-10-23 09:22:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-03 05:13:20 -05:00
|
|
|
fn verify_checksum(&self, dirs: &Dirs) {
|
|
|
|
let download_dir = self.download_dir(dirs);
|
|
|
|
let actual_hash = format!("{:016x}", hash_dir(&download_dir));
|
|
|
|
if actual_hash != self.content_hash {
|
|
|
|
eprintln!(
|
|
|
|
"Mismatched content hash for {download_dir}: {actual_hash} != {content_hash}. Please run ./y.sh prepare again.",
|
|
|
|
download_dir = download_dir.display(),
|
|
|
|
content_hash = self.content_hash,
|
|
|
|
);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 05:38:16 -06:00
|
|
|
pub(crate) fn fetch(&self, dirs: &Dirs) {
|
2023-06-15 12:56:01 -05:00
|
|
|
let download_dir = self.download_dir(dirs);
|
|
|
|
|
|
|
|
if download_dir.exists() {
|
|
|
|
let actual_hash = format!("{:016x}", hash_dir(&download_dir));
|
|
|
|
if actual_hash == self.content_hash {
|
2023-10-09 03:52:46 -05:00
|
|
|
eprintln!("[FRESH] {}", download_dir.display());
|
2023-06-15 12:56:01 -05:00
|
|
|
return;
|
|
|
|
} else {
|
2023-10-09 03:52:46 -05:00
|
|
|
eprintln!(
|
2023-06-15 12:56:01 -05:00
|
|
|
"Mismatched content hash for {download_dir}: {actual_hash} != {content_hash}. Downloading again.",
|
|
|
|
download_dir = download_dir.display(),
|
|
|
|
content_hash = self.content_hash,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 09:22:55 -05:00
|
|
|
match self.url {
|
|
|
|
GitRepoUrl::Github { user, repo } => {
|
2024-09-23 06:20:46 -05:00
|
|
|
clone_repo(
|
|
|
|
&download_dir,
|
|
|
|
&format!("https://github.com/{}/{}.git", user, repo),
|
|
|
|
self.rev,
|
|
|
|
);
|
2022-10-23 09:22:55 -05:00
|
|
|
}
|
|
|
|
}
|
2023-06-15 12:56:01 -05:00
|
|
|
|
|
|
|
let source_lockfile =
|
|
|
|
RelPath::PATCHES.to_path(dirs).join(format!("{}-lock.toml", self.patch_name));
|
|
|
|
let target_lockfile = download_dir.join("Cargo.lock");
|
|
|
|
if source_lockfile.exists() {
|
2023-10-21 14:54:51 -05:00
|
|
|
assert!(!target_lockfile.exists());
|
2023-06-15 12:56:01 -05:00
|
|
|
fs::copy(source_lockfile, target_lockfile).unwrap();
|
|
|
|
} else {
|
|
|
|
assert!(target_lockfile.exists());
|
|
|
|
}
|
|
|
|
|
2024-10-03 05:13:20 -05:00
|
|
|
self.verify_checksum(dirs);
|
2023-06-15 12:56:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn patch(&self, dirs: &Dirs) {
|
2024-10-03 05:13:20 -05:00
|
|
|
self.verify_checksum(dirs);
|
2023-06-15 12:56:01 -05:00
|
|
|
apply_patches(
|
|
|
|
dirs,
|
|
|
|
self.patch_name,
|
|
|
|
&self.download_dir(dirs),
|
|
|
|
&self.source_dir().to_path(dirs),
|
|
|
|
);
|
2022-10-23 09:22:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clone_repo(download_dir: &Path, repo: &str, rev: &str) {
|
2021-07-07 04:14:20 -05:00
|
|
|
eprintln!("[CLONE] {}", repo);
|
2024-10-03 05:15:15 -05:00
|
|
|
|
|
|
|
match fs::remove_dir_all(download_dir) {
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
|
|
|
|
Err(err) => panic!("Failed to remove {path}: {err}", path = download_dir.display()),
|
|
|
|
}
|
|
|
|
|
2021-07-07 04:14:20 -05:00
|
|
|
// Ignore exit code as the repo may already have been checked out
|
2023-01-24 11:56:42 -06:00
|
|
|
git_command(None, "clone").arg(repo).arg(download_dir).spawn().unwrap().wait().unwrap();
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
let mut clean_cmd = git_command(download_dir, "checkout");
|
|
|
|
clean_cmd.arg("--").arg(".");
|
2021-07-07 04:14:20 -05:00
|
|
|
spawn_and_wait(clean_cmd);
|
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
let mut checkout_cmd = git_command(download_dir, "checkout");
|
|
|
|
checkout_cmd.arg("-q").arg(rev);
|
2021-07-07 04:14:20 -05:00
|
|
|
spawn_and_wait(checkout_cmd);
|
2023-06-15 12:56:01 -05:00
|
|
|
|
|
|
|
std::fs::remove_dir_all(download_dir.join(".git")).unwrap();
|
2021-07-07 04:14:20 -05:00
|
|
|
}
|
|
|
|
|
2022-03-20 10:55:21 -05:00
|
|
|
fn init_git_repo(repo_dir: &Path) {
|
2023-01-24 11:56:42 -06:00
|
|
|
let mut git_init_cmd = git_command(repo_dir, "init");
|
|
|
|
git_init_cmd.arg("-q");
|
2022-03-20 10:55:21 -05:00
|
|
|
spawn_and_wait(git_init_cmd);
|
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
let mut git_add_cmd = git_command(repo_dir, "add");
|
|
|
|
git_add_cmd.arg(".");
|
2022-03-20 10:55:21 -05:00
|
|
|
spawn_and_wait(git_add_cmd);
|
|
|
|
|
2023-01-24 11:56:42 -06:00
|
|
|
let mut git_commit_cmd = git_command(repo_dir, "commit");
|
|
|
|
git_commit_cmd.arg("-m").arg("Initial commit").arg("-q");
|
2022-03-20 10:55:21 -05:00
|
|
|
spawn_and_wait(git_commit_cmd);
|
|
|
|
}
|
|
|
|
|
2022-12-14 12:30:46 -06:00
|
|
|
fn get_patches(dirs: &Dirs, crate_name: &str) -> Vec<PathBuf> {
|
|
|
|
let mut patches: Vec<_> = fs::read_dir(RelPath::PATCHES.to_path(dirs))
|
2021-07-07 04:14:20 -05:00
|
|
|
.unwrap()
|
|
|
|
.map(|entry| entry.unwrap().path())
|
|
|
|
.filter(|path| path.extension() == Some(OsStr::new("patch")))
|
2022-10-23 09:22:55 -05:00
|
|
|
.filter(|path| {
|
|
|
|
path.file_name()
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.split_once("-")
|
|
|
|
.unwrap()
|
|
|
|
.1
|
|
|
|
.starts_with(crate_name)
|
2021-07-07 04:14:20 -05:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
patches.sort();
|
|
|
|
patches
|
|
|
|
}
|
|
|
|
|
2023-06-15 12:56:01 -05:00
|
|
|
pub(crate) fn apply_patches(dirs: &Dirs, crate_name: &str, source_dir: &Path, target_dir: &Path) {
|
|
|
|
// FIXME avoid copy and patch if src, patches and target are unchanged
|
|
|
|
|
|
|
|
eprintln!("[COPY] {crate_name} source");
|
|
|
|
|
2024-09-23 06:20:46 -05:00
|
|
|
ensure_empty_dir(target_dir);
|
2024-09-22 14:02:30 -05:00
|
|
|
if crate_name == "stdlib" {
|
|
|
|
fs::create_dir(target_dir.join("library")).unwrap();
|
|
|
|
copy_dir_recursively(&source_dir.join("library"), &target_dir.join("library"));
|
|
|
|
} else {
|
|
|
|
copy_dir_recursively(source_dir, target_dir);
|
|
|
|
}
|
2023-06-15 12:56:01 -05:00
|
|
|
|
|
|
|
init_git_repo(target_dir);
|
|
|
|
|
2022-10-23 09:22:55 -05:00
|
|
|
if crate_name == "<none>" {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-14 12:30:46 -06:00
|
|
|
for patch in get_patches(dirs, crate_name) {
|
2022-10-23 09:22:55 -05:00
|
|
|
eprintln!(
|
|
|
|
"[PATCH] {:?} <- {:?}",
|
|
|
|
target_dir.file_name().unwrap(),
|
|
|
|
patch.file_name().unwrap()
|
|
|
|
);
|
2023-01-24 11:56:42 -06:00
|
|
|
let mut apply_patch_cmd = git_command(target_dir, "am");
|
|
|
|
apply_patch_cmd.arg(patch).arg("-q");
|
2021-07-07 04:14:20 -05:00
|
|
|
spawn_and_wait(apply_patch_cmd);
|
|
|
|
}
|
|
|
|
}
|
2024-09-23 06:20:46 -05:00
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
fn git_command<'a>(repo_dir: impl Into<Option<&'a Path>>, cmd: &str) -> Command {
|
|
|
|
let mut git_cmd = Command::new("git");
|
|
|
|
git_cmd
|
|
|
|
.arg("-c")
|
|
|
|
.arg("user.name=Dummy")
|
|
|
|
.arg("-c")
|
|
|
|
.arg("user.email=dummy@example.com")
|
|
|
|
.arg("-c")
|
|
|
|
.arg("core.autocrlf=false")
|
|
|
|
.arg("-c")
|
|
|
|
.arg("commit.gpgSign=false")
|
|
|
|
.arg(cmd);
|
|
|
|
if let Some(repo_dir) = repo_dir.into() {
|
|
|
|
git_cmd.current_dir(repo_dir);
|
|
|
|
}
|
|
|
|
git_cmd
|
|
|
|
}
|