Skip to content
This repository has been archived by the owner on Jun 21, 2019. It is now read-only.

Commit

Permalink
Merge pull request #139 from froydnj/honor-package-include
Browse files Browse the repository at this point in the history
use cargo::sources to determine files to copy for vendoring
  • Loading branch information
alexcrichton committed Oct 31, 2018
2 parents 8abf24f + d019f1b commit f03f126
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/main.rs
Expand Up @@ -365,8 +365,10 @@ fn sync(workspaces: &[Workspace],
&format!("{} ({}) to {}", id, src.to_string_lossy(), dst.display()))?;

let _ = fs::remove_dir_all(&dst);
let pathsource = cargo::sources::path::PathSource::new(&src, id.source_id(), config);
let paths = pathsource.list_files(&pkg)?;
let mut map = BTreeMap::new();
cp_r(&src, &dst, &dst, &mut map).chain_err(|| {
cp_sources(&src, &paths, &dst, &mut map).chain_err(|| {
format!("failed to copy over vendored sources for: {}", id)
})?;

Expand Down Expand Up @@ -481,15 +483,14 @@ fn sync(workspaces: &[Workspace],
Ok(VendorConfig { source: config })
}

fn cp_r(src: &Path,
dst: &Path,
root: &Path,
cksums: &mut BTreeMap<String, String>) -> io::Result<()> {
fs::create_dir(dst)?;
for entry in src.read_dir()? {
let entry = entry?;
fn cp_sources(src: &Path,
paths: &Vec<PathBuf>,
dst: &Path,
cksums: &mut BTreeMap<String, String>) -> CargoResult<()> {
for p in paths {
let relative = p.strip_prefix(&src).unwrap();

match entry.file_name().to_str() {
match relative.to_str() {
// Skip git config files as they're not relevant to builds most of
// the time and if we respect them (e.g. in git) then it'll
// probably mess with the checksums when a vendor dir is checked
Expand All @@ -510,17 +511,22 @@ fn cp_r(src: &Path,
}
}
_ => ()
}
};

let src = entry.path();
let dst = dst.join(entry.file_name());
if entry.file_type()?.is_dir() {
cp_r(&src, &dst, root, cksums)?;
} else {
fs::copy(&src, &dst)?;
let rel = dst.strip_prefix(root).unwrap().to_str().unwrap();
cksums.insert(rel.replace("\\", "/"), sha256(&dst)?);
}
// Join pathname components individually to make sure that the joined
// path uses the correct directory separators everywhere, since
// `relative` may use Unix-style and `dst` may require Windows-style
// backslashes.
let dst = relative.iter().fold(dst.to_owned(), |acc, component| {
acc.join(&component)
});

fs::create_dir_all(dst.parent().unwrap())?;

fs::copy(&p, &dst).chain_err(|| {
format!("failed to copy `{}` to `{}`", p.display(), dst.display())
})?;
cksums.insert(relative.to_str().unwrap().replace("\\", "/"), sha256(&dst)?);
}
Ok(())
}
Expand Down
44 changes: 44 additions & 0 deletions tests/vendor.rs
Expand Up @@ -304,6 +304,50 @@ fn ignore_files() {
assert!(!csum.contains("\"Cargo.toml.orig\""));
}

#[test]
fn included_files_only() {
let (dir, _lock) = dir();
// Use a fixed commit so we know what files are excluded.
file(&dir, "Cargo.toml", r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies.libc]
git = "https://github.com/rust-lang/libc"
rev = "b95fa265332df919e53eb66de5e6bd37fcd94041"
"#);
file(&dir, "src/lib.rs", "");

run(&mut vendor(&dir));
let csum = read(&dir.join("vendor/libc/.cargo-checksum.json"));
assert!(!csum.contains("\"ci/README.md\""));
assert!(!csum.contains("\"ci/docker/aarch64-linux-android\""));
}

#[test]
fn dependent_crates_in_crates() {
let (dir, _lock) = dir();

file(&dir, "Cargo.toml", r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies.winapi]
git = 'https://github.com/retep998/winapi-rs/'
rev = '3792048cb07f9b762f8f0913293027759ea78db2'
"#);
file(&dir, "src/lib.rs", "");

run(&mut vendor(&dir));
let csum = read(&dir.join("vendor/winapi/.cargo-checksum.json"));
assert!(!csum.contains("\"tests/\""));
assert!(!csum.contains("\"x86_64/lib/\""));
let csum = read(&dir.join("vendor/winapi-i686-pc-windows-gnu/.cargo-checksum.json"));
assert!(!csum.contains("\"def/\""));
}

#[test]
fn git_simple() {
let (dir, _lock) = dir();
Expand Down

0 comments on commit f03f126

Please sign in to comment.