Skip to content

Commit 174d845

Browse files
committed
Make target dir self-ignoring
1 parent f15df8f commit 174d845

File tree

12 files changed

+26
-37
lines changed

12 files changed

+26
-37
lines changed

src/cargo/core/workspace.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,16 @@ impl<'gctx> Workspace<'gctx> {
402402
}
403403

404404
pub fn target_dir(&self) -> Filesystem {
405-
self.target_dir
405+
let target_dir = self
406+
.target_dir
406407
.clone()
407-
.unwrap_or_else(|| self.default_target_dir())
408+
.unwrap_or_else(|| self.default_target_dir());
409+
410+
let path = target_dir.as_path_unlocked();
411+
paths::create_dir_all(path).unwrap();
412+
std::fs::write(path.join(".gitignore"), "*\n").unwrap();
413+
414+
target_dir
408415
}
409416

410417
fn default_target_dir(&self) -> Filesystem {

src/cargo/ops/cargo_new.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ fn write_ignore_file(base_path: &Path, list: &IgnoreList, vcs: VersionControl) -
680680
}
681681

682682
for fp_ignore in match vcs {
683-
VersionControl::Git => vec![base_path.join(".gitignore")],
683+
VersionControl::Git => return Ok(()), // target-dir already contains wildcard gitignore file
684684
VersionControl::Hg => vec![base_path.join(".hgignore")],
685685
VersionControl::Pijul => vec![base_path.join(".ignore")],
686686
// Fossil has a cleaning functionality configured in a separate file.

tests/testsuite/cargo_init/auto_git/out/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/testsuite/cargo_init/explicit_bin_with_git/out/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/testsuite/cargo_init/git_autodetect/out/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
**/some.file
2-
3-
# Added by cargo
4-
5-
/target
1+
**/some.file

tests/testsuite/cargo_init/inferred_bin_with_git/out/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/testsuite/cargo_init/inferred_lib_with_git/out/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/testsuite/cargo_init/simple_git/out/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
11
/target
2-
**/some.file
3-
4-
# Added by cargo
5-
#
6-
# already existing elements were commented out
7-
8-
#/target
2+
**/some.file

0 commit comments

Comments
 (0)