6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 1 | use std::{ |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 2 | env, fs, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 3 | os::unix::fs::{DirBuilderExt, PermissionsExt}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 4 | path::{Path, PathBuf}, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 5 | }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 6 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 7 | fn home() -> PathBuf { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 8 | env::var_os("HOME").map_or_else(|| PathBuf::from("/"), PathBuf::from) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 9 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 10 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 11 | fn xdg(var: &str, fallback: &str) -> PathBuf { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 12 | env::var_os(var) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 13 | .map(PathBuf::from) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 14 | .filter(|p| p.is_absolute()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 15 | .unwrap_or_else(|| home().join(fallback)) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 16 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 17 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 18 | pub fn config_file() -> PathBuf { |
61c6e94355d9 | atagen | 2026-09-27 03:59 UTC | 19 | if let Some(p) = env::var_os("ROUNDABOUT_CONFIG") { |
61c6e94355d9 | atagen | 2026-09-27 03:59 UTC | 20 | return p.into(); |
61c6e94355d9 | atagen | 2026-09-27 03:59 UTC | 21 | } |
61c6e94355d9 | atagen | 2026-09-27 03:59 UTC | 22 | let user = xdg("XDG_CONFIG_HOME", ".config").join("roundabout/config.toml"); |
61c6e94355d9 | atagen | 2026-09-27 03:59 UTC | 23 | let system = PathBuf::from("/etc/roundabout/config.toml"); |
61c6e94355d9 | atagen | 2026-09-27 03:59 UTC | 24 | if !user.exists() && system.exists() { |
61c6e94355d9 | atagen | 2026-09-27 03:59 UTC | 25 | system |
61c6e94355d9 | atagen | 2026-09-27 03:59 UTC | 26 | } else { |
61c6e94355d9 | atagen | 2026-09-27 03:59 UTC | 27 | user |
61c6e94355d9 | atagen | 2026-09-27 03:59 UTC | 28 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 29 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 30 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 31 | pub fn state_dir() -> PathBuf { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 32 | let dir = env::var_os("ROUNDABOUT_STATE_DIR").map_or_else( |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 33 | || xdg("XDG_STATE_HOME", ".local/state").join("roundabout"), |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 34 | PathBuf::from, |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 35 | ); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 36 | let _ = fs::create_dir_all(&dir); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 37 | dir |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 38 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 39 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 40 | pub fn runtime_dir() -> PathBuf { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 41 | if let Some(dir) = env::var_os("ROUNDABOUT_RUNTIME_DIR") { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 42 | let dir = PathBuf::from(dir); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 43 | let _ = fs::DirBuilder::new() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 44 | .recursive(true) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 45 | .mode(0o700) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 46 | .create(&dir); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 47 | return dir; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 48 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 49 | if let Some(dir) = env::var_os("XDG_RUNTIME_DIR").map(PathBuf::from) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 50 | && dir.is_absolute() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 51 | && dir.is_dir() |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 52 | { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 53 | return dir; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 54 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 55 | let uid = unsafe { libc::getuid() }; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 56 | let dir = env::temp_dir().join(format!("roundabout-{uid}")); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 57 | let _ = fs::DirBuilder::new().mode(0o700).create(&dir); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 58 | let _ = fs::set_permissions(&dir, fs::Permissions::from_mode(0o700)); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 59 | dir |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 60 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 61 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 62 | pub fn socket() -> PathBuf { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 63 | runtime_dir().join("roundabout.sock") |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 64 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 65 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 66 | pub fn spawn_lock() -> PathBuf { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 67 | runtime_dir().join("roundabout.lock") |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 68 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 69 | |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 70 | pub fn git_root(cwd: &Path) -> Option<PathBuf> { |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 71 | cwd.ancestors() |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 72 | .find(|dir| dir.join(".git").exists()) |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 73 | .map(Path::to_path_buf) |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 74 | } |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 75 | |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 76 | pub fn project_config_file(cwd: &Path) -> Option<PathBuf> { |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 77 | let root = git_root(cwd)?; |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 78 | for dir in cwd.ancestors() { |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 79 | if dir.join(".resrc").is_file() { |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 80 | return Some(dir.join(".resrc")); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 81 | } |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 82 | if dir == root { |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 83 | break; |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 84 | } |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 85 | } |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 86 | None |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 87 | } |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 88 | |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 89 | pub fn project_root(cwd: &Path) -> PathBuf { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 90 | const MARKERS: [&str; 6] = [ |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 91 | ".jj", |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 92 | ".git", |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 93 | ".hg", |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 94 | "flake.nix", |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 95 | "Cargo.toml", |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 96 | "package.json", |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 97 | ]; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 98 | let mut best = None; |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 99 | for dir in cwd.ancestors() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 100 | if dir.join(".jj").exists() || dir.join(".git").exists() || dir.join(".hg").exists() { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 101 | return dir.to_path_buf(); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 102 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 103 | if best.is_none() && MARKERS.iter().any(|m| dir.join(m).exists()) { |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 104 | best = Some(dir.to_path_buf()); |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 105 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 106 | } |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 107 | best.unwrap_or_else(|| cwd.to_path_buf()) |
6f5b84fad3ef | atagen | 2026-09-27 03:59 UTC | 108 | } |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 109 | |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 110 | #[cfg(test)] |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 111 | mod tests { |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 112 | use std::{ |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 113 | fs, |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 114 | path::PathBuf, |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 115 | time::{SystemTime, UNIX_EPOCH}, |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 116 | }; |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 117 | |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 118 | use super::project_config_file; |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 119 | |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 120 | fn temp_tree() -> PathBuf { |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 121 | let stamp = SystemTime::now() |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 122 | .duration_since(UNIX_EPOCH) |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 123 | .expect("clock before epoch") |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 124 | .as_nanos(); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 125 | let root = std::env::temp_dir().join(format!("roundabout-paths-{stamp}")); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 126 | fs::create_dir_all(&root).expect("create temporary tree"); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 127 | root |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 128 | } |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 129 | |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 130 | #[test] |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 131 | fn project_config_is_nearest_and_git_bounded() { |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 132 | let outer = temp_tree(); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 133 | let root = outer.join("repo"); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 134 | fs::create_dir(&root).expect("create repository tree"); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 135 | let child = root.join("child"); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 136 | let nested = child.join("nested"); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 137 | fs::create_dir_all(&nested).expect("create nested tree"); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 138 | fs::create_dir(root.join(".git")).expect("create git marker"); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 139 | fs::write(root.join(".resrc"), "").expect("write root config"); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 140 | fs::write(child.join(".resrc"), "").expect("write child config"); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 141 | fs::write(nested.join(".resrc"), "").expect("write nested config"); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 142 | assert_eq!(project_config_file(&nested), Some(nested.join(".resrc"))); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 143 | fs::remove_file(nested.join(".resrc")).expect("remove nested config"); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 144 | assert_eq!(project_config_file(&nested), Some(child.join(".resrc"))); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 145 | fs::remove_file(child.join(".resrc")).expect("remove child config"); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 146 | assert_eq!(project_config_file(&nested), Some(root.join(".resrc"))); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 147 | fs::remove_file(root.join(".resrc")).expect("remove root config"); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 148 | fs::write(outer.join(".resrc"), "").expect("write outside config"); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 149 | assert_eq!(project_config_file(&nested), None); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 150 | fs::remove_file(outer.join(".resrc")).expect("remove outside config"); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 151 | fs::remove_dir_all(outer).expect("remove temporary tree"); |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 152 | } |
9117440444eb | atagen | 2026-10-26 02:44 UTC | 153 | } |