repo: scan for .jj/ directory up the tree

It's kind of silly that I haven't fixed this earlier. I just don't
need it often myself.
This commit is contained in:
Martin von Zweigbergk 2021-05-16 10:33:52 -07:00
parent c8f2de1ecc
commit 4d51cd96a1
2 changed files with 32 additions and 6 deletions

View File

@ -17,7 +17,7 @@ use std::fmt::{Debug, Formatter};
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard};
use thiserror::Error;
@ -384,16 +384,27 @@ pub struct RepoLoader {
index_store: Arc<IndexStore>,
}
fn find_repo_dir(mut wc_dir: &Path) -> Option<PathBuf> {
loop {
let repo_path = wc_dir.join(".jj");
if repo_path.is_dir() {
return Some(repo_path);
}
if let Some(wc_dir_parent) = wc_dir.parent() {
wc_dir = wc_dir_parent;
} else {
return None;
}
}
}
impl RepoLoader {
pub fn init(
user_settings: &UserSettings,
wc_path: PathBuf,
) -> Result<RepoLoader, RepoLoadError> {
let repo_path = wc_path.join(".jj");
// TODO: Check if ancestor directory has a .jj/
if !repo_path.is_dir() {
return Err(RepoLoadError::NoRepoHere(wc_path));
}
let repo_path = find_repo_dir(&wc_path).ok_or(RepoLoadError::NoRepoHere(wc_path))?;
let wc_path = repo_path.parent().unwrap().to_owned();
let store = StoreWrapper::load_store(&repo_path);
let repo_settings = user_settings.with_repo(&repo_path).unwrap();
let op_store: Arc<dyn OpStore> = Arc::new(SimpleOpStore::load(repo_path.join("op_store")));

View File

@ -26,6 +26,21 @@ fn test_load_bad_path() {
assert_eq!(result.err(), Some(RepoLoadError::NoRepoHere(wc_path)));
}
#[test_case(false ; "local store")]
#[test_case(true ; "git store")]
fn test_load_from_subdir(use_git: bool) {
let settings = testutils::user_settings();
let (_temp_dir, repo) = testutils::init_repo(&settings, use_git);
let subdir = repo.working_copy_path().join("dir").join("subdir");
std::fs::create_dir_all(subdir.clone()).unwrap();
let same_repo = ReadonlyRepo::load(&settings, subdir);
assert!(same_repo.is_ok());
let same_repo = same_repo.unwrap();
assert_eq!(same_repo.repo_path(), repo.repo_path());
assert_eq!(same_repo.working_copy_path(), repo.working_copy_path());
}
#[test_case(false ; "local store")]
#[test_case(true ; "git store")]
fn test_load_at_operation(use_git: bool) {