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

include dotfiles in packages #7680

Merged
merged 7 commits into from
Dec 12, 2019
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
28 changes: 24 additions & 4 deletions src/cargo/sources/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ impl<'cfg> PathSource<'cfg> {
if let Some(result) = self.discover_git_and_list_files(pkg, root, &mut filter) {
return result;
}
// no include option and not git repo discovered (see rust-lang/cargo#7183).
return self.list_files_walk_except_dot_files_and_dirs(pkg, &mut filter);
}
self.list_files_walk(pkg, &mut filter)
}
Expand Down Expand Up @@ -329,6 +331,28 @@ impl<'cfg> PathSource<'cfg> {
}
}

fn list_files_walk_except_dot_files_and_dirs(
&self,
pkg: &Package,
filter: &mut dyn FnMut(&Path) -> CargoResult<bool>,
) -> CargoResult<Vec<PathBuf>> {
let root = pkg.root();
let mut exclude_dot_files_dir_builder = GitignoreBuilder::new(root);
exclude_dot_files_dir_builder.add_line(None, ".*")?;
let ignore_dot_files_and_dirs = exclude_dot_files_dir_builder.build()?;

let mut filter_ignore_dot_files_and_dirs = |path: &Path| -> CargoResult<bool> {
let relative_path = path.strip_prefix(root)?;
match ignore_dot_files_and_dirs
.matched_path_or_any_parents(relative_path, /* is_dir */ false)
{
Match::Ignore(_) => Ok(false),
_ => filter(path),
}
};
self.list_files_walk(pkg, &mut filter_ignore_dot_files_and_dirs)
}

fn list_files_walk(
&self,
pkg: &Package,
Expand Down Expand Up @@ -368,10 +392,6 @@ impl<'cfg> PathSource<'cfg> {
entries.sort_unstable_by(|a, b| a.as_os_str().cmp(b.as_os_str()));
for path in entries {
let name = path.file_name().and_then(|s| s.to_str());
// Skip dotfile directories.
if name.map(|s| s.starts_with('.')) == Some(true) {
continue;
}
if is_root && name == Some("target") {
// Skip Cargo artifacts.
continue;
Expand Down
27 changes: 26 additions & 1 deletion tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,12 @@ fn include() {
version = "0.0.1"
authors = []
exclude = ["*.txt"]
include = ["foo.txt", "**/*.rs", "Cargo.toml"]
include = ["foo.txt", "**/*.rs", "Cargo.toml", ".dotfile"]
"#,
)
.file("foo.txt", "")
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
.file(".dotfile", "")
// Should be ignored when packaging.
.file("src/bar.txt", "")
.build();
Expand All @@ -442,6 +443,7 @@ fn include() {
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
[WARNING] both package.include and package.exclude are specified; the exclude list will be ignored
[PACKAGING] foo v0.0.1 ([..])
[ARCHIVING] .dotfile
[ARCHIVING] Cargo.toml
[ARCHIVING] foo.txt
[ARCHIVING] src/main.rs
Expand Down Expand Up @@ -1420,3 +1422,26 @@ fn gitignore_negate() {
",
);
}

#[cargo_test]
fn exclude_dot_files_and_directories_by_default() {
include_exclude_test(
"[]",
"[]",
&["src/lib.rs", ".dotfile", ".dotdir/file"],
"Cargo.toml\n\
src/lib.rs\n\
",
);

include_exclude_test(
r#"["Cargo.toml", "src/lib.rs", ".dotfile", ".dotdir/file"]"#,
"[]",
&["src/lib.rs", ".dotfile", ".dotdir/file"],
".dotdir/file\n\
.dotfile\n\
Cargo.toml\n\
src/lib.rs\n\
",
);
}