Onset: version control where nobody waits and nothing gets lost. The on command, its core and onset-server.
| 5 |
| 6 | use anyhow::{Context, Result}; |
| 7 | use onset_core::tree::{self, Entry, Files}; |
| 8 | use onset_core::version::Version; |
| 9 | use onset_core::Id; |
| 10 |
| 11 | use crate::db::{Change, Db, Project, Save, Workspace}; |
| 12 | use crate::host::{Host, Local}; |
| 13 | use crate::ids; |
| 14 | use crate::objects::Store; |
| 15 | use crate::out::Fail; |
| 16 | use crate::paths; |
| 17 | use crate::remote::Remote; |
| 18 | use crate::ws::{self, Index, Lock}; |
| 19 |
| 20 | pub struct App { |
| 21 | pub db: Db, |
| 22 | pub store: Store, |
| 23 | pub json: bool, |
| 24 | pub cwd: PathBuf, |
| 25 | /// Things that happened on the side, printed after the answer. |
| 26 | pub notes: std::cell::RefCell<Vec<String>>, |
| 27 | /// The workspaces this command worked in (their `.git` views follow after). |
| 28 | pub touched: std::cell::RefCell<Vec<PathBuf>>, |
| 29 | } |
| 30 |
| 31 | /// An open workspace, locked for the command's lifetime. |
| 32 | pub struct Session { |
| 33 | pub root: PathBuf, |
| 34 | pub ws: Workspace, |
| 35 | pub project: Project, |
| 36 | _lock: Lock, |
| 37 | } |
| 38 |
| 39 | impl App { |
| 40 | pub fn open(json: bool, cwd: PathBuf) -> Result<App> { |
| 41 | let home = paths::home(); |
| 42 | let db = Db::open(&home)?; |
| 43 | // Sealed folders' keys: their files read as they are here. |
| 44 | crate::seal::load_ring(&db)?; |
| 45 | Ok(App { db, store: Store::open(&paths::objects())?, json, cwd, notes: Default::default(), touched: Default::default() }) |
| 46 | } |
| 47 |
| 48 | pub fn host(&self, project: &Project) -> Box<dyn Host> { |
| 49 | if project.hosted() { Box::new(Remote::for_project(project)) } else { Box::new(Local) } |
| 50 | } |
| 51 |
| 52 | /// The workspace containing the current folder. |
| 53 | pub fn session(&self) -> Result<Session, Fail> { |
| 54 | // A lazy workspace whose mount stopped (a restart) or whose process |
| 55 | // died (a crash): it starts again. It's found from this machine's |
| 56 | // list, not the folder, which can't be trusted then: a dead mount on |
| 57 | // macOS still answers from the system's cache while reading it |
| 58 | // fails, and on Windows the folder still lists every file. |
| 59 | if let Some(w) = crate::lazy::workspace_at(self, &crate::sys::canonical(&self.cwd).unwrap_or_else(|_| self.cwd.clone()))? { |
| 60 | crate::lazy::mount(&w)?; |
| 61 | } |
| 62 | let root = ws::find(&self.cwd).ok_or_else(|| { |
| 63 | Fail::new("not_found", "this folder isn't in an Onset workspace") |
| 64 | .state("nothing changed") |
| 65 | .next("cd into a workspace, or on get <project> <folder>") |
| 66 | })?; |
| 67 | self.session_at(&root) |
| 68 | } |
| 69 |
| 70 | pub fn session_at(&self, root: &Path) -> Result<Session, Fail> { |
| 71 | let id = ws::read_id(root)?; |
| 72 | let lock = ws::lock(root)?; |
| 73 | let mut workspace = self.db.workspace(&id)?.ok_or_else(|| { |
| 74 | Fail::new("not_found", format!("{} is a workspace this machine doesn't know", root.display())) |
| 75 | .state("nothing changed") |
| 76 | .next("on get <project> <new-folder> to make a new workspace") |
| 77 | })?; |
| 78 | let canonical = crate::sys::canonical(root).unwrap_or_else(|_| root.to_path_buf()); |
| 79 | if workspace.path != canonical { |
| 80 | // The folder moved; follow it. |
| 81 | workspace.path = canonical.clone(); |
| 82 | self.db.put_workspace(&workspace)?; |
| 83 | } |
| 84 | let project = self.db.project(&workspace.project)?.ok_or_else(|| Fail::new("not_found", "this workspace's project is missing"))?; |
| 85 | let mut touched = self.touched.borrow_mut(); |
| 86 | if !touched.contains(&canonical) { |
| 87 | touched.push(canonical.clone()); |
| 88 | } |
| 89 | Ok(Session { root: canonical, ws: workspace, project, _lock: lock }) |
| 90 | } |
| 91 |
| 92 | pub fn change(&self, id: &str) -> Result<Change, Fail> { |
| 93 | self.db.change(id)?.ok_or_else(|| Fail::new("not_found", format!("no change {id}")).state("nothing changed")) |
| 94 | } |
| 95 |
| 96 | /// A change named by its ID or a unique prefix of four letters or more. |
| 97 | pub fn find_change(&self, project: &Project, text: &str) -> Result<Change, Fail> { |
| 98 | if !ids::looks_like_change(text) { |
| 99 | return Err(Fail::new("bad_usage", format!("{text} isn't a change ID (changes are 4 or more letters)")) |
| 100 | .state("nothing changed") |
| 101 | .next("on log --changes to see change IDs")); |
| 102 | } |
| 103 | let mut found = self.db.changes_by_prefix(&project.id, text)?; |
| 104 | if found.is_empty() && project.hosted() { |
| 105 | // Someone else's change, or one from another machine. |
| 106 | if let Some(c) = Remote::for_project(project).fetch_change(&self.db, &self.store, project, text)? { |
| 107 | found.push(c); |
| 108 | } |
| 109 | } |
| 110 | match found.len() { |
| 111 | 0 => Err(Fail::new("not_found", format!("no change {text} in this project")) |
| 112 | .state("nothing changed") |
| 113 | .next("on log --changes")), |
| 114 | 1 => Ok(found.remove(0)), |
| 115 | _ => Err(Fail::new("bad_usage", format!("{text} matches {} changes; use more letters", found.len())) |
| 116 | .state("nothing changed") |
| 117 | .next("on log --changes")), |
| 118 | } |
| 119 | } |
| 120 |
| 121 | pub fn short(&self, project: &Project, id: &str) -> String { |
| 122 | let others = self.db.change_ids(&project.id).unwrap_or_default(); |
| 123 | ids::short(id, &others) |
| 124 | } |
| 125 |
| 126 | pub fn author(&self, project: &Project) -> String { |
| 127 | let me = identity(project, &self.db); |
| 128 | match crate::paths::var("AGENT").filter(|a| !a.is_empty()) { |
| 129 | Some(agent) => format!("{} (via {agent}) <{}>", me.0, me.1), |
| 130 | None => format!("{} <{}>", me.0, me.1), |
| 131 | } |
| 132 | } |
| 133 |
| 134 | pub fn main(&self, project: &Project) -> Result<Option<Version>> { |
| 135 | self.db.latest_version(&project.id) |
| 136 | } |
| 137 |
| 138 | /// The latest main, asking the host when the project is hosted. |
| 139 | pub fn refresh(&self, project: &Project) -> Result<Option<Version>> { |
| 140 | let mut v = self.host(project).refresh(&self.db, &self.store, project)?; |
| 141 | // Someone purged paths from the host's history: out of this machine's too. |
| 142 | let (seen, host) = self.db.purges(&project.id)?; |
| 143 | if host > seen { |
| 144 | crate::purge::catch_up(self, project)?; |
| 145 | v = self.host(project).refresh(&self.db, &self.store, project)?; |
| 146 | } |
| 147 | // Sealed folders changed on the host: their keys and files, here. |
| 148 | let (seen, host) = crate::seal::state(&self.db, &project.id)?; |
| 149 | if host != seen { |
| 150 | crate::seal::sync(self, project)?; |
| 151 | } |
| 152 | self.db.set_checked(&project.id, ids::now())?; |
| 153 | Ok(v) |
| 154 | } |
| 155 |
| 156 | /// Version `n` of main, fetching history this machine doesn't keep yet |
| 157 | /// (older than it started with, or newer than it last looked). |
| 158 | pub fn version(&self, project: &Project, n: u64) -> Result<Option<Version>> { |
| 159 | if let Some(v) = self.db.version(&project.id, n)? { |
| 160 | return Ok(Some(v)); |
| 161 | } |
| 162 | if n < self.db.history_from(&project.id)? { |
| 163 | self.host(project).fetch_version(&self.db, project, n)?; |
| 164 | } else { |
| 165 | self.refresh(project)?; |
| 166 | } |
| 167 | self.db.version(&project.id, n) |
| 168 | } |
| 169 |
| 170 | /// Makes sure this machine keeps main's history from `n` on. |
| 171 | pub fn history(&self, project: &Project, n: u64) -> Result<()> { |
| 172 | self.host(project).fetch_history(&self.db, project, n) |
| 173 | } |
| 174 |
| 175 | pub fn files(&self, tree: &Id) -> Result<Files> { |
| 176 | Ok(tree::flatten(&self.store, tree)?) |
| 177 | } |
| 178 |
| 179 | /// Saves the workspace into its change. Returns the change as saved |
| 180 | /// (a new one if another workspace took the change over meanwhile). |
| 181 | pub fn save(&self, s: &mut Session) -> Result<Change> { |
| 182 | self.save_with(s, true) |
| 183 | } |
| 184 |
| 185 | /// Saves without telling the host (a landing carries the change itself). |
| 186 | pub fn save_local(&self, s: &mut Session) -> Result<Change> { |
| 187 | self.save_with(s, false) |
| 188 | } |
| 189 |
| 190 | fn save_with(&self, s: &mut Session, publish: bool) -> Result<Change> { |
| 191 | let mut change = self.change(&s.ws.change).map_err(|f| f.into_anyhow())?; |
| 192 | let index = Index::load(&s.root); |
| 193 | // Sealed folders: their files are sealed as they're saved, and ones |
| 194 | // this machine has no key for are kept as the change has them. |
| 195 | let seals = crate::seal::Seals::load(&self.db, &s.project.id)?; |
| 196 | let skip = seals.unreadable(); |
| 197 | // A whole workspace's files are its index's; with --only (or a |
| 198 | // folder skipped), the change's tree gives the rest. |
| 199 | let known: Files = if s.ws.only.is_empty() && skip.is_empty() { Files::new() } else { self.files(&change.tree())? }; |
| 200 | let personal = crate::access::personal(self, &s.project.id)?; |
| 201 | let base_tree = change.base_tree; |
| 202 | let keep = |path: &str, bytes: Vec<u8>| seals.keep(path, bytes); |
| 203 | let mut snap = ws::snapshot(&s.root, &self.store, &known, &s.ws.only, &skip, &index, &personal, &|| self.base_paths(s, &base_tree), &keep)?; |
| 204 | if let Some(next) = &mut snap.index |
| 205 | && !index.same_as(next) |
| 206 | { |
| 207 | next.save(&s.root)?; |
| 208 | } |
| 209 | let saved_mine = self.db.mine(&s.project.id)?; |
| 210 | let mut mine_changed = false; |
| 211 | for (path, id, exec) in &snap.mine { |
| 212 | if !saved_mine.iter().any(|(p, i, x)| p == path && i == id && x == exec) { |
| 213 | self.db.put_mine(&s.project.id, path, id, *exec)?; |
| 214 | mine_changed = true; |
| 215 | } |
| 216 | } |
| 217 | let _ = self.db.conn.execute("INSERT OR IGNORE INTO complete (tree) VALUES (?1)", [snap.tree.hex()]); |
| 218 | let mut dirty = false; |
| 219 | if snap.tree != change.tree() { |
| 220 | change.head_tree = Some(snap.tree); |
| 221 | change.saves += 1; |
| 222 | self.db.add_save( |
| 223 | &Save { change: change.id.clone(), n: change.saves, tree: snap.tree, base_tree: change.base_tree, at: ids::now(), uploaded: false }, |
| 224 | &s.ws.id, |
| 225 | )?; |
| 226 | dirty = true; |
| 227 | } |
| 228 | if !change.conflicts.is_empty() { |
| 229 | let before = change.conflicts.len(); |
| 230 | change.conflicts.retain(|c| std::fs::read(s.root.join(&c.path)).is_ok_and(|b| ws::has_markers(&b))); |
| 231 | dirty |= change.conflicts.len() != before; |
| 232 | } |
| 233 | if dirty { |
| 234 | self.db.put_change(&change)?; |
| 235 | } |
| 236 | if s.project.hosted() && publish { |
| 237 | if mine_changed { |
| 238 | let _ = crate::remote::push_mine(self, &s.project); |
| 239 | } |
| 240 | if dirty || !self.db.unuploaded_saves(&change.id)?.is_empty() { |
| 241 | // The change follows you to other machines. Offline, it |
| 242 | // uploads on a later command. |
| 243 | if let Err(e) = self.host(&s.project).publish_change(&self.db, &self.store, &s.project, &change, &s.ws.id) { |
| 244 | let fail = Fail::from(e); |
| 245 | if fail.code == "held_elsewhere" { |
| 246 | return self.rescue(s, &change, fail.message.clone()); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | Ok(change) |
| 252 | } |
| 253 |
| 254 | /// Another workspace took this workspace's change over. What this one |
| 255 | /// has goes into a new change, so nothing is lost. |
| 256 | fn rescue(&self, s: &mut Session, taken: &Change, why: String) -> Result<Change> { |
| 257 | let mut fresh = Change::new(ids::new_change_id(), &s.project.id, taken.base_tree, taken.base_version, &self.author(&s.project)); |
| 258 | fresh.head_tree = taken.head_tree; |
| 259 | fresh.saves = 1; |
| 260 | fresh.on_change = taken.on_change.clone(); |
| 261 | fresh.title = format!("Rescued from {}", taken.id); |
| 262 | self.db.put_change(&fresh)?; |
| 263 | self.db.add_save(&Save { change: fresh.id.clone(), n: 1, tree: fresh.tree(), base_tree: fresh.base_tree, at: ids::now(), uploaded: false }, &s.ws.id)?; |
| 264 | // The taken change's newest state is the host's. |
| 265 | if let Some(remote) = Some(crate::remote::Remote::for_project(&s.project)) { |
| 266 | let _ = remote.fetch_change(&self.db, &self.store, &s.project, &taken.id); |
| 267 | } |
| 268 | s.ws.change = fresh.id.clone(); |
| 269 | self.db.put_workspace(&s.ws)?; |
| 270 | let _ = self.host(&s.project).publish_change(&self.db, &self.store, &s.project, &fresh, &s.ws.id); |
| 271 | self.notes.borrow_mut().push(format!("{why}; what this workspace had is in new change {}", fresh.id)); |
| 272 | Ok(fresh) |
| 273 | } |
| 274 |
| 275 | /// The paths a base tree holds, cached in the workspace (bases change |
| 276 | /// only on sync, new and land). |
| 277 | pub fn base_paths(&self, s: &Session, base: &Id) -> Result<std::collections::HashSet<String>> { |
| 278 | let cache = s.root.join(ws::DIR).join("base"); |
| 279 | if let Ok(text) = std::fs::read_to_string(&cache) |
| 280 | && let Some((id, rest)) = text.split_once('\n') |
| 281 | && id == base.hex() |
| 282 | { |
| 283 | return Ok(rest.lines().map(str::to_string).collect()); |
| 284 | } |
| 285 | let files = self.files(base)?; |
| 286 | let mut text = format!("{}\n", base.hex()); |
| 287 | for p in files.keys() { |
| 288 | text.push_str(p); |
| 289 | text.push('\n'); |
| 290 | } |
| 291 | std::fs::write(&cache, text)?; |
| 292 | Ok(files.into_keys().collect()) |
| 293 | } |
| 294 |
| 295 | /// Makes the workspace folder hold `tree`. |
| 296 | pub fn show_tree(&self, s: &Session, tree: &Id) -> Result<()> { |
| 297 | self.host(&s.project).fetch_tree(&self.store, &s.project, tree, None)?; |
| 298 | let index = Index::load(&s.root); |
| 299 | let files = self.files(tree)?; |
| 300 | let skip = crate::seal::Seals::load(&self.db, &s.project.id)?.unreadable(); |
| 301 | let mut next = ws::materialize(&s.root, &self.store, &index, &files, &s.ws.only, &skip, None)?; |
| 302 | next.save(&s.root)?; |
| 303 | Ok(()) |
| 304 | } |
| 305 |
| 306 | /// Writes the user's own files (like .env) into a workspace if missing. |
| 307 | pub fn place_mine(&self, project: &Project, root: &Path) -> Result<usize> { |
| 308 | let mut n = 0; |
| 309 | for (path, id, exec) in self.db.mine(&project.id)? { |
| 310 | let abs = root.join(&path); |
| 311 | if abs.exists() { |
| 312 | continue; |
| 313 | } |
| 314 | if let Some(parent) = abs.parent() { |
| 315 | std::fs::create_dir_all(parent)?; |
| 316 | } |
| 317 | std::fs::write(&abs, self.store.read_file(&id)?)?; |
| 318 | crate::sys::owner_only(&abs, exec)?; |
| 319 | n += 1; |
| 320 | } |
| 321 | Ok(n) |
| 322 | } |
| 323 |
| 324 | pub fn put_workspace_change(&self, s: &mut Session, change: &str) -> Result<()> { |
| 325 | s.ws.change = change.to_string(); |
| 326 | self.db.put_workspace(&s.ws)?; |
| 327 | Ok(()) |
| 328 | } |
| 329 |
| 330 | /// Files under `only` for a tree, as a [`Files`] map (for workspaces |
| 331 | /// limited to some folders). |
| 332 | pub fn scoped(&self, only: &[String], files: &Files) -> Files { |
| 333 | if only.is_empty() { |
| 334 | return files.clone(); |
| 335 | } |
| 336 | files.iter().filter(|(p, _)| only.iter().any(|o| tree::under(p, o))).map(|(p, e)| (p.clone(), *e)).collect() |
| 337 | } |
| 338 | } |
| 339 |
| 340 | /// Who's working: ONSET_NAME and ONSET_EMAIL, then the host's account, then |
| 341 | /// ~/.gitconfig's user, then the login name. |
| 342 | pub fn identity(project: &Project, db: &Db) -> (String, String) { |
| 343 | let _ = (project, db); |
| 344 | let mut name = crate::paths::var("NAME").filter(|s| !s.is_empty()); |
| 345 | let mut email = crate::paths::var("EMAIL").filter(|s| !s.is_empty()); |
| 346 | if (name.is_none() || email.is_none()) |
| 347 | && project.hosted() |
| 348 | && let Some(account) = crate::remote::account(project) |
| 349 | { |
| 350 | name = name.or(Some(account.0)); |
| 351 | email = email.or(Some(account.1)); |
| 352 | } |
| 353 | if name.is_none() || email.is_none() { |
| 354 | let (n, e) = gitconfig_user(); |
| 355 | name = name.or(n); |
| 356 | email = email.or(e); |
| 357 | } |
| 358 | let name = name.unwrap_or_else(|| crate::sys::user_name().unwrap_or_else(|| "someone".into())); |
| 359 | let email = email.unwrap_or_else(|| format!("{}@localhost", name.to_lowercase().replace(' ', "."))); |
| 360 | (name, email) |
| 361 | } |
| 362 |
| 363 | fn gitconfig_user() -> (Option<String>, Option<String>) { |
| 364 | let Some(home) = crate::sys::user_home() else { return (None, None) }; |
| 365 | let Ok(text) = std::fs::read_to_string(home.join(".gitconfig")) else { return (None, None) }; |
| 366 | let mut in_user = false; |
| 367 | let (mut name, mut email) = (None, None); |
| 368 | for line in text.lines() { |
| 369 | let line = line.trim(); |
| 370 | if line.starts_with('[') { |
| 371 | in_user = line == "[user]"; |
| 372 | continue; |
| 373 | } |
| 374 | if in_user && let Some((k, v)) = line.split_once('=') { |
| 375 | match k.trim() { |
| 376 | "name" => name = Some(v.trim().to_string()), |
| 377 | "email" => email = Some(v.trim().to_string()), |
| 378 | _ => {} |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | (name, email) |
| 383 | } |
| 384 |
| 385 | /// Index entries as a [`Files`] map. |
| 386 | pub fn index_files(index: &Index) -> Files { |
| 387 | index.entries.iter().map(|(p, e)| (p.clone(), e.entry)).collect::<Files>() |
| 388 | } |
| 389 |
| 390 | pub fn entry_map(files: &Files) -> Vec<(String, Entry)> { |
| 391 | files.iter().map(|(p, e)| (p.clone(), *e)).collect() |
| 392 | } |
| 393 |
| 394 | pub fn ensure_dir_empty(path: &Path) -> Result<(), Fail> { |
| 395 | if path.exists() { |
| 396 | let empty = std::fs::read_dir(path).with_context(|| format!("reading {}", path.display()))?.next().is_none(); |
| 397 | if !empty { |
| 398 | return Err(Fail::new("bad_usage", format!("{} already exists and isn't empty", path.display())) |
| 399 | .state("nothing changed") |
| 400 | .next("pick a new folder name")); |
| 401 | } |
| 402 | } |
| 403 | Ok(()) |
| 404 | } |
| 405 |