Skip to content

Commit 0c00dc8

Browse files
committed
fix(repo-dir): check if the current cwd is .git before setting the default cwd
Fixes /issues/2732
1 parent 1d22485 commit 0c00dc8

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/args.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,24 @@ pub fn process_cmdline() -> Result<CliArgs> {
3434

3535
let workdir =
3636
arg_matches.get_one::<String>("workdir").map(PathBuf::from);
37-
let gitdir = arg_matches
38-
.get_one::<String>("directory")
39-
.map_or_else(|| PathBuf::from("."), PathBuf::from);
37+
let gitdir = 'blk: {
38+
let mut cwd = env::current_dir()?;
39+
let cwd_file_name = cwd
40+
.file_name()
41+
.ok_or(anyhow!("Coulden't get the CWD name"))?
42+
.as_encoded_bytes();
43+
if cwd_file_name == b".git" {
44+
cwd = cwd
45+
.parent()
46+
.ok_or(anyhow!(
47+
"Coulden't find the parent directory of .git"
48+
))?
49+
.to_path_buf();
50+
}
51+
break 'blk arg_matches
52+
.get_one::<String>("directory")
53+
.map_or_else(|| cwd, PathBuf::from);
54+
};
4055

4156
let repo_path = if let Some(w) = workdir {
4257
RepoPath::Workdir { gitdir, workdir: w }

0 commit comments

Comments
 (0)