From 8ba214d6cff478947ef665f1da9bef5bea7116af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20K=C3=B6rber?= Date: Mon, 22 Nov 2021 20:48:19 +0100 Subject: [PATCH] Fix setting for root if there is a root-level repo --- src/lib.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index db9665b..d9b7890 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -303,10 +303,14 @@ fn get_actual_git_directory(path: &Path, is_worktree: bool) -> PathBuf { } } -fn find_repos(root: &Path) -> Option> { +fn find_repos(root: &Path) -> Option<(Vec, bool)> { let mut repos: Vec = Vec::new(); + let mut repo_in_root = false; for (path, is_worktree) in find_repos_without_details(root).unwrap() { + if path == root { + repo_in_root = true; + } let repo = match open_repo(&path, is_worktree) { Ok(r) => r, Err(e) => { @@ -406,16 +410,25 @@ fn find_repos(root: &Path) -> Option> { worktree_setup: is_worktree, }); } - Some(repos) + Some((repos, repo_in_root)) } fn find_in_tree(path: &Path) -> Option { - let repos: Vec = match find_repos(path) { - Some(vec) => vec, - None => Vec::new(), + let (repos, repo_in_root): (Vec, bool) = match find_repos(path) { + Some((vec, repo_in_root)) => (vec, repo_in_root), + None => (Vec::new(), false), }; let mut root = path.to_path_buf(); + if repo_in_root { + root = match root.parent() { + Some(root) => root.to_path_buf(), + None => { + print_error("Cannot detect root directory. Are you working in /?"); + process::exit(1); + } + } + } let home = env_home(); if root.starts_with(&home) { // The tilde is not handled differently, it's just a normal path component for `Path`.