Skip to content

Commit a28b9ed

Browse files
committed
Delay inside_vcs_repo check until Git initialization
1 parent 802dcfc commit a28b9ed

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

src/init.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crossterm::{
55
};
66
use serde::Deserialize;
77
use std::{
8-
env::set_current_dir,
8+
env::{current_dir, set_current_dir},
99
fs::{self, create_dir},
1010
io::{self, Write},
1111
path::Path,
@@ -29,21 +29,6 @@ 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-
4732
let locate_project_output = Command::new("cargo")
4833
.arg("locate-project")
4934
.arg("-q")
@@ -74,7 +59,7 @@ pub fn init() -> Result<()> {
7459
}
7560

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

7964
if locate_project_output.status.success() {
8065
if Path::new("exercises").exists() && Path::new("solutions").exists() {
@@ -184,14 +169,27 @@ pub fn init() -> Result<()> {
184169
fs::write(".vscode/extensions.json", VS_CODE_EXTENSIONS_JSON)
185170
.context("Failed to create the file `rustlings/.vscode/extensions.json`")?;
186171

187-
if init_git {
188-
// Ignore any Git error because Git initialization is not required.
189-
let _ = Command::new("git")
190-
.arg("init")
191-
.stdin(Stdio::null())
192-
.stdout(Stdio::null())
193-
.stderr(Stdio::null())
194-
.status();
172+
if init_git && let Ok(dir) = current_dir() {
173+
let mut dir = dir.as_path();
174+
175+
loop {
176+
if dir.join(".git").exists() || dir.join(".jj").exists() {
177+
break;
178+
}
179+
180+
if let Some(parent) = dir.parent() {
181+
dir = parent;
182+
} else {
183+
// Ignore any Git error because Git initialization is not required.
184+
let _ = Command::new("git")
185+
.arg("init")
186+
.stdin(Stdio::null())
187+
.stdout(Stdio::null())
188+
.stderr(Stdio::null())
189+
.status();
190+
break;
191+
}
192+
}
195193
}
196194

197195
stdout.queue(SetForegroundColor(Color::Green))?;

0 commit comments

Comments
 (0)