Skip to content

Commit 17ff889

Browse files
committed
Avoid initializing nested Git repository
Previously a Git repository was initialized if a Cargo workspace was detected. However, it's also possible for users to initialize rustlings in an existing Git repository that doesn't contain a Cargo workspace. In that case, it's still undesirable to initialize a nested Git repository for rustlings. We therefore search all ancestors of the current working directory for `.git` or `.jj` directories to determine if rustlings is being initialized in an existing Git repository.
1 parent 75c06bb commit 17ff889

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

src/init.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ pub fn init() -> Result<()> {
2929
bail!(RUSTLINGS_DIR_ALREADY_EXISTS_ERR);
3030
}
3131

32+
let is_inside_vcs_repository = 'detect_repo: {
33+
let Ok(mut dir) = std::env::current_dir() else {
34+
break 'detect_repo false;
35+
};
36+
loop {
37+
if dir.join(".git").exists() || dir.join(".jj").exists() {
38+
break 'detect_repo true;
39+
}
40+
match dir.parent() {
41+
Some(parent) => dir = parent.into(),
42+
None => break 'detect_repo false,
43+
}
44+
}
45+
};
46+
3247
let locate_project_output = Command::new("cargo")
3348
.arg("locate-project")
3449
.arg("-q")
@@ -59,7 +74,7 @@ pub fn init() -> Result<()> {
5974
}
6075

6176
let mut stdout = io::stdout().lock();
62-
let mut init_git = true;
77+
let mut init_git = !is_inside_vcs_repository;
6378

6479
if locate_project_output.status.success() {
6580
if Path::new("exercises").exists() && Path::new("solutions").exists() {

0 commit comments

Comments
 (0)