Skip to content

Commit 31fcba3

Browse files
committed
fix: add .* pattern in the same gitignore instance
1 parent 1ca930b commit 31fcba3

File tree

1 file changed

+15
-34
lines changed

1 file changed

+15
-34
lines changed

src/cargo/sources/path.rs

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,17 @@ impl<'cfg> PathSource<'cfg> {
107107
fn _list_files(&self, pkg: &Package) -> CargoResult<Vec<PathBuf>> {
108108
let root = pkg.root();
109109
let no_include_option = pkg.manifest().include().is_empty();
110+
let git_repo = if no_include_option {
111+
self.discover_git_repo(root)?
112+
} else {
113+
None
114+
};
110115

111116
let mut exclude_builder = GitignoreBuilder::new(root);
117+
if no_include_option && git_repo.is_none() {
118+
// no include option and not git repo discovered (see rust-lang/cargo#7183).
119+
exclude_builder.add_line(None, ".*")?;
120+
}
112121
for rule in pkg.manifest().exclude() {
113122
exclude_builder.add_line(None, rule)?;
114123
}
@@ -160,23 +169,16 @@ impl<'cfg> PathSource<'cfg> {
160169

161170
// Attempt Git-prepopulate only if no `include` (see rust-lang/cargo#4135).
162171
if no_include_option {
163-
if let Some(result) = self.discover_git_and_list_files(pkg, root, &mut filter)? {
164-
return Ok(result);
172+
if let Some(repo) = git_repo {
173+
return self.list_files_git(pkg, &repo, &mut filter);
165174
}
166-
// no include option and not git repo discovered (see rust-lang/cargo#7183).
167-
return self.list_files_walk_except_dot_files_and_dirs(pkg, &mut filter);
168175
}
169176
self.list_files_walk(pkg, &mut filter)
170177
}
171178

172-
// Returns `Some(_)` if found sibling `Cargo.toml` and `.git` directory;
173-
// otherwise, caller should fall back on full file list.
174-
fn discover_git_and_list_files(
175-
&self,
176-
pkg: &Package,
177-
root: &Path,
178-
filter: &mut dyn FnMut(&Path, bool) -> CargoResult<bool>,
179-
) -> CargoResult<Option<Vec<PathBuf>>> {
179+
/// Returns `Some(git2::Repository)` if found sibling `Cargo.toml` and `.git`
180+
/// directory; otherwise, caller should fall back on full file list.
181+
fn discover_git_repo(&self, root: &Path) -> CargoResult<Option<git2::Repository>> {
180182
let repo = match git2::Repository::discover(root) {
181183
Ok(repo) => repo,
182184
Err(e) => {
@@ -211,7 +213,7 @@ impl<'cfg> PathSource<'cfg> {
211213
};
212214
let manifest_path = repo_relative_path.join("Cargo.toml");
213215
if index.get_path(&manifest_path, 0).is_some() {
214-
return Ok(Some(self.list_files_git(pkg, &repo, filter)?));
216+
return Ok(Some(repo));
215217
}
216218
// Package Cargo.toml is not in git, don't use git to guide our selection.
217219
Ok(None)
@@ -355,27 +357,6 @@ impl<'cfg> PathSource<'cfg> {
355357
}
356358
}
357359

358-
fn list_files_walk_except_dot_files_and_dirs(
359-
&self,
360-
pkg: &Package,
361-
filter: &mut dyn FnMut(&Path, bool) -> CargoResult<bool>,
362-
) -> CargoResult<Vec<PathBuf>> {
363-
let root = pkg.root();
364-
let mut exclude_dot_files_dir_builder = GitignoreBuilder::new(root);
365-
exclude_dot_files_dir_builder.add_line(None, ".*")?;
366-
let ignore_dot_files_and_dirs = exclude_dot_files_dir_builder.build()?;
367-
368-
let mut filter_ignore_dot_files_and_dirs =
369-
|path: &Path, is_dir: bool| -> CargoResult<bool> {
370-
let relative_path = path.strip_prefix(root)?;
371-
match ignore_dot_files_and_dirs.matched_path_or_any_parents(relative_path, is_dir) {
372-
Match::Ignore(_) => Ok(false),
373-
_ => filter(path, is_dir),
374-
}
375-
};
376-
self.list_files_walk(pkg, &mut filter_ignore_dot_files_and_dirs)
377-
}
378-
379360
fn list_files_walk(
380361
&self,
381362
pkg: &Package,

0 commit comments

Comments
 (0)