| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | | use crate::attest::{Attest, AttestBody}; |
| 11 | | use crate::diff::{ranges, split_lines, text_edits}; |
| 12 | | use crate::hash::{hash_object, ChangeHash, KeyId, ObjectRef, ObjectType}; |
| 13 | | use crate::identity::{ |
| 14 | | verify as verify_signature, Delegate, Domain, Identity, Scope, SecretKey, Signature, |
| 15 | | }; |
| 16 | | use crate::ids::{ChangeId, NodeId, WorkspaceId}; |
| 17 | | use crate::objects::{ |
| 18 | | Blob, Change, ChangeKind, Edit, Hlc, Mode, NodeKind, OpRef, StateObject, TreeOp, |
| 19 | | }; |
| 20 | | use crate::oplog::{Format1Op, Op, View, SNAPSHOT_SUFFIX}; |
| 21 | | use crate::protocol::{ |
| 22 | | open_remote, Bundle, RefResult, RefUpdate, RefsResponse, RemoteOptions, RemoteRef, |
| 23 | | SignedAttest, SignedPolicy, |
| 24 | | }; |
| 25 | | use crate::state::{FileContent, Snapshot, State}; |
| 26 | | use crate::store::{Store, StoreError}; |
| 27 | | #[allow(unused_imports)] |
| 28 | | use crate::subrepo::Pin; |
| 29 | | use crate::tree::OpKey; |
| 30 | | use rand::RngCore; |
| 31 | | use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; |
| 32 | | use std::fs; |
| 33 | | use std::io::Write; |
| 34 | | use std::path::{Path, PathBuf}; |
| 35 | |
|
| 36 | | pub const OXY_DIR: &str = ".oxy"; |
| 37 | | pub const FORMAT_LINE: &str = "oxy repository format 2\n"; |
| 38 | | const FORMAT1_LINE: &str = "oxy repository format 1\n"; |
| 39 | | const BINARY_SNIFF: usize = 8 * 1024; |
| 40 | |
|
| 41 | | pub const AUTOPACK_THRESHOLD: usize = 4096; |
| 42 | |
|
| 43 | | #[derive(Debug, thiserror::Error)] |
| 44 | | pub enum RepoError { |
| 45 | | #[error("io: {0}")] |
| 46 | | Io(#[from] std::io::Error), |
| 47 | | #[error(transparent)] |
| 48 | | Store(#[from] StoreError), |
| 49 | | #[error(transparent)] |
| 50 | | Object(#[from] crate::objects::ObjectError), |
| 51 | | #[error(transparent)] |
| 52 | | State(#[from] crate::state::StateError), |
| 53 | | #[error("{0}")] |
| 54 | | Msg(String), |
| 55 | | |
| 56 | | |
| 57 | | #[error("{0}")] |
| 58 | | Conflicted(String), |
| 59 | | } |
| 60 | |
|
| 61 | | pub type Result<T> = std::result::Result<T, RepoError>; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | | pub const HEAD_RECORD: &str = "ops/head"; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | | const HEAD_TRIES: usize = 8; |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | 2805× | fn open_objects(oxy: &Path, config: &Config) -> Result<Store> { |
| 76 | 2805× | let Some(url) = &config.storage_url else { |
| 77 | 2800× | return Ok(Store::open(oxy.join("objects"))?); |
| 78 | | }; |
| 79 | 5× | let options = crate::protocol::RemoteOptions { |
| 80 | 5× | cache_dir: Some(oxy.join("cache")), |
| 81 | 5× | ..Default::default() |
| 82 | 5× | }; |
| 83 | 5× | let remote = crate::protocol::open_store(url, &options) |
| 84 | 5× | .ok_or_else(|| { |
| 85 | 1× | RepoError::Msg(format!( |
| 86 | 1× | "storage.url {url}: this oxy cannot open that kind of store (dir://, or s3://, gs:// and az:// in builds with cloud stores)" |
| 87 | 1× | )) |
| 88 | 1× | })? |
| 89 | 4× | .map_err(|e| RepoError::Msg(format!("open the repository's store {url}: {e}")))?; |
| 90 | 4× | let cached = crate::strata::CachingStore::open( |
| 91 | 4× | std::sync::Arc::from(remote), |
| 92 | 4× | oxy.join("cache"), |
| 93 | 4× | config.storage_cache, |
| 94 | | ) |
| 95 | 4× | .map_err(|e| RepoError::Msg(format!("open the cache for {url}: {e}")))?; |
| 96 | 4× | Ok(Store::open_on( |
| 97 | 4× | std::sync::Arc::new(cached), |
| 98 | 4× | oxy.join("objects"), |
| 99 | 0× | )?) |
| 100 | 2805× | } |
| 101 | |
|
| 102 | 173× | fn fail<T>(s: impl Into<String>) -> Result<T> { |
| 103 | 173× | Err(RepoError::Msg(s.into())) |
| 104 | 173× | } |
| 105 | |
|
| 106 | | mod bookmarks; |
| 107 | | mod checkout; |
| 108 | | mod checkpoint; |
| 109 | | mod config; |
| 110 | | mod doctor; |
| 111 | | mod fsck; |
| 112 | | mod gc; |
| 113 | | mod git; |
| 114 | | mod history; |
| 115 | | mod hooks; |
| 116 | | mod hosted; |
| 117 | | mod identity; |
| 118 | | mod inspect; |
| 119 | | mod keyless; |
| 120 | | mod ops; |
| 121 | | mod policy; |
| 122 | | mod prompt; |
| 123 | | mod receipts; |
| 124 | | mod revset; |
| 125 | | mod snapshot; |
| 126 | | pub mod strata; |
| 127 | | mod subrepos; |
| 128 | | mod sync; |
| 129 | | mod tags; |
| 130 | | #[cfg(test)] |
| 131 | | mod tests; |
| 132 | | mod touched; |
| 133 | | mod transparency; |
| 134 | | mod walk; |
| 135 | | mod workspace; |
| 136 | |
|
| 137 | | pub use bookmarks::{remote_bookmark, BookmarkInfo, MergeReport, RemoteOnlyBookmark, StateGap}; |
| 138 | | pub use checkout::CheckoutReport; |
| 139 | | pub use config::{parse_size, read_config_file, Config}; |
| 140 | | pub use doctor::{Check, DoctorReport, Health}; |
| 141 | | pub use fsck::{FsckReport, Problem, Severity}; |
| 142 | | pub use gc::GcReport; |
| 143 | | pub use git::{GitExportReport, GitImportReport, GitVerifyReport}; |
| 144 | | pub use hooks::{json_array, json_str, HookOutcome}; |
| 145 | | pub use hosted::FileEdit; |
| 146 | | pub use identity::{Rotation, SignatureStatus}; |
| 147 | | pub use inspect::{BisectStep, BlameLine, Description}; |
| 148 | | pub use keyless::home_key; |
| 149 | | pub use ops::{RestoreReport, RevertReport, Undone, ViewChange}; |
| 150 | | pub use policy::PolicyUpdate; |
| 151 | | pub use prompt::{PromptInfo, DEFAULT_PROMPT_FORMAT, GLYPH_PROMPT_FORMAT}; |
| 152 | | pub use receipts::ReceiptInfo; |
| 153 | | pub use revset::{Revset, REVSET_BUDGET}; |
| 154 | | pub use snapshot::{ |
| 155 | | added_node_paths, diff_files_of, has_conflict_markers, is_binary, referenced_changes, |
| 156 | | render_change, render_hunks, similarity, ChangeStatus, DiffHunk, FileDiff, Hunk, PathStatus, |
| 157 | | Selection, SnapshotResult, |
| 158 | | }; |
| 159 | | pub use subrepos::{SubrepoInfo, SubrepoSync}; |
| 160 | | pub use sync::{FetchReport, PullReport, PushReport, RefUpdated, OBJECTS_RESULT}; |
| 161 | | pub use tags::TagInfo; |
| 162 | | pub use transparency::LogAudit; |
| 163 | | pub use workspace::{FileLock, SnapshotLock, WorkingCopyLock, WorkspaceInfo}; |
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | 5389× | pub(crate) fn name_file(name: &str) -> String { |
| 169 | 5389× | name.replace('%', "%25").replace('/', "%2F") |
| 170 | 5389× | } |
| 171 | |
|
| 172 | |
|
| 173 | 58× | pub(crate) fn file_name_to_name(file: &str) -> String { |
| 174 | 58× | file.replace("%2F", "/").replace("%25", "%") |
| 175 | 58× | } |
| 176 | |
|
| 177 | 8451× | fn write_atomic(path: &Path, data: &[u8]) -> Result<()> { |
| 178 | 8451× | Ok(crate::durable::write_atomic(path, data)?) |
| 179 | 8451× | } |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | | const INDEX_REPLAY_LIMIT: usize = 16; |
| 187 | |
|
| 188 | |
|
| 189 | | const CHANGE_CACHE_COUNT: usize = 4; |
| 190 | | const CHANGE_CACHE_BYTES: usize = 512 << 20; |
| 191 | |
|
| 192 | | const STATE_INDEXES_KEPT: usize = 2; |
| 193 | | const INDEX_REPLAY_TIME: std::time::Duration = std::time::Duration::from_millis(50); |
| 194 | |
|
| 195 | 3218× | pub(crate) fn write_cache(path: &Path, data: &[u8]) -> Result<()> { |
| 196 | 3218× | let tmp = path.with_extension(format!("tmp-{}", std::process::id())); |
| 197 | 3218× | fs::write(&tmp, data)?; |
| 198 | 3218× | fs::rename(&tmp, path)?; |
| 199 | 3218× | Ok(()) |
| 200 | 3218× | } |
| 201 | |
|
| 202 | | pub struct LogEntry { |
| 203 | | pub hash: ChangeHash, |
| 204 | | |
| 205 | | pub change: crate::state::ChangeMeta, |
| 206 | | pub is_working_copy: bool, |
| 207 | | pub is_head: bool, |
| 208 | | } |
| 209 | |
|
| 210 | |
|
| 211 | | pub(crate) type KeyOf<'a> = dyn Fn(&str) -> Option<[u8; 32]> + Sync + 'a; |
| 212 | |
|
| 213 | |
|
| 214 | | #[derive(Clone)] |
| 215 | | pub(crate) struct FsEntry { |
| 216 | | pub kind: NodeKind, |
| 217 | | pub exec: bool, |
| 218 | | |
| 219 | | |
| 220 | | pub content: std::sync::Arc<Vec<u8>>, |
| 221 | | |
| 222 | | |
| 223 | | |
| 224 | | pub blob: Option<ObjectRef>, |
| 225 | | |
| 226 | | |
| 227 | | pub content_hash: Option<[u8; 32]>, |
| 228 | | |
| 229 | | |
| 230 | | |
| 231 | | pub unchanged: bool, |
| 232 | | |
| 233 | | pub graph_key: Option<[u8; 32]>, |
| 234 | | |
| 235 | | |
| 236 | | pub stat: Option<(u64, u128)>, |
| 237 | | } |
| 238 | |
|
| 239 | | impl FsEntry { |
| 240 | 23007× | pub(crate) fn new(kind: NodeKind, exec: bool, content: Vec<u8>) -> FsEntry { |
| 241 | 23007× | FsEntry { |
| 242 | 23007× | kind, |
| 243 | 23007× | exec, |
| 244 | 23007× | content: std::sync::Arc::new(content), |
| 245 | 23007× | blob: None, |
| 246 | 23007× | content_hash: None, |
| 247 | 23007× | unchanged: false, |
| 248 | 23007× | graph_key: None, |
| 249 | 23007× | stat: None, |
| 250 | 23007× | } |
| 251 | 23007× | } |
| 252 | | } |
| 253 | |
|
| 254 | | pub struct Repo { |
| 255 | | |
| 256 | | |
| 257 | | |
| 258 | | pub(crate) acceptor: Option<std::sync::Arc<(Identity, crate::identity::SecretKey)>>, |
| 259 | | |
| 260 | | |
| 261 | | |
| 262 | | pub(crate) keyless_key: std::cell::RefCell<Option<(KeyId, crate::identity::SecretKey, u64)>>, |
| 263 | | |
| 264 | | pub(crate) git_export_skipped: std::cell::Cell<usize>, |
| 265 | | |
| 266 | | |
| 267 | | pub(crate) manifest_cache: std::cell::RefCell<Option<prompt::CachedManifest>>, |
| 268 | | |
| 269 | | |
| 270 | | |
| 271 | | pub(crate) listing_cache: std::cell::RefCell<Option<walk::Listing>>, |
| 272 | | |
| 273 | | |
| 274 | | pub(crate) untracked_nested: std::cell::RefCell<Vec<String>>, |
| 275 | | |
| 276 | | |
| 277 | | pub(crate) skipped_large: std::cell::RefCell<Vec<(String, u64)>>, |
| 278 | | root: PathBuf, |
| 279 | | oxy: PathBuf, |
| 280 | | store: Store, |
| 281 | | workspace: WorkspaceId, |
| 282 | | |
| 283 | | bare: bool, |
| 284 | | keys_dir: PathBuf, |
| 285 | | user_config: PathBuf, |
| 286 | | pub config: Config, |
| 287 | | |
| 288 | | platform: crate::portable::Platform, |
| 289 | | |
| 290 | | |
| 291 | | last_change: std::cell::RefCell<Vec<(ChangeHash, std::sync::Arc<Vec<u8>>)>>, |
| 292 | | |
| 293 | | |
| 294 | | at_op: Option<ObjectRef>, |
| 295 | | |
| 296 | | |
| 297 | | |
| 298 | | head_seen: std::cell::Cell<Option<ObjectRef>>, |
| 299 | | |
| 300 | | |
| 301 | | pub(crate) walked_dirs: std::cell::RefCell<Option<WalkedDirs>>, |
| 302 | | |
| 303 | | pub(crate) listed_dirs: std::cell::RefCell<Option<WalkedDirs>>, |
| 304 | | } |
| 305 | |
|
| 306 | |
|
| 307 | | pub(crate) type WalkedDirs = (Vec<(String, u128)>, Vec<String>); |
| 308 | |
|
| 309 | |
|
| 310 | | const BARE_MARKER: &str = "bare"; |
| 311 | |
|
| 312 | |
|
| 313 | | const UNBOUND_MARKER: &str = "unbound"; |
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | 8694× | pub fn default_home() -> PathBuf { |
| 318 | 8694× | if let Ok(h) = std::env::var("OXY_HOME") { |
| 319 | 8667× | return PathBuf::from(h); |
| 320 | 27× | } |
| 321 | 27× | if let Ok(x) = std::env::var("XDG_CONFIG_HOME") { |
| 322 | 0× | return PathBuf::from(x).join("oxy"); |
| 323 | 27× | } |
| 324 | | |
| 325 | | |
| 326 | | #[cfg(windows)] |
| 327 | | if let Some(d) = std::env::var_os("APPDATA").or_else(|| std::env::var_os("USERPROFILE")) { |
| 328 | | return PathBuf::from(d).join("oxy"); |
| 329 | | } |
| 330 | 27× | let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); |
| 331 | 27× | PathBuf::from(home).join(".config").join("oxy") |
| 332 | 8694× | } |
| 333 | |
|
| 334 | 2806× | pub fn default_keys_dir() -> PathBuf { |
| 335 | 2806× | default_home().join("keys") |
| 336 | 2806× | } |
| 337 | |
|
| 338 | 5610× | pub fn default_user_config() -> PathBuf { |
| 339 | 5610× | default_home().join("config.toml") |
| 340 | 5610× | } |
| 341 | |
|
| 342 | |
|
| 343 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 344 | | pub enum IdentityScope { |
| 345 | | |
| 346 | | User, |
| 347 | | |
| 348 | | Repo, |
| 349 | | } |
| 350 | |
|
| 351 | | impl Repo { |
| 352 | | |
| 353 | |
|
| 354 | 263× | pub fn init(root: &Path) -> Result<Repo> { |
| 355 | 263× | let mut id = [0u8; 16]; |
| 356 | 263× | rand::rngs::OsRng.fill_bytes(&mut id); |
| 357 | 263× | Self::init_with_id(root, id) |
| 358 | 263× | } |
| 359 | |
|
| 360 | | |
| 361 | | |
| 362 | 550× | pub fn init_with_id(root: &Path, id: [u8; 16]) -> Result<Repo> { |
| 363 | 550× | Self::init_on(root, id, None) |
| 364 | 550× | } |
| 365 | |
|
| 366 | | |
| 367 | | |
| 368 | 12× | pub fn init_new(root: &Path, bare: bool, storage: Option<&str>) -> Result<Repo> { |
| 369 | 12× | let mut id = [0u8; 16]; |
| 370 | 12× | rand::rngs::OsRng.fill_bytes(&mut id); |
| 371 | 12× | if bare { |
| 372 | 0× | Self::init_bare_on(root, id, storage) |
| 373 | | } else { |
| 374 | 12× | Self::init_on(root, id, storage) |
| 375 | | } |
| 376 | 12× | } |
| 377 | |
|
| 378 | | |
| 379 | | |
| 380 | 565× | pub fn init_on(root: &Path, id: [u8; 16], storage: Option<&str>) -> Result<Repo> { |
| 381 | 565× | fs::create_dir_all(root)?; |
| 382 | 565× | let root = root.canonicalize().unwrap_or_else(|_| root.to_path_buf()); |
| 383 | 565× | let oxy = root.join(OXY_DIR); |
| 384 | 565× | if oxy.exists() { |
| 385 | 1× | return fail(format!("{} is already an oxy repository", root.display())); |
| 386 | 564× | } |
| 387 | 564× | fs::create_dir_all(oxy.join("ops"))?; |
| 388 | 564× | write_atomic(&oxy.join("format"), FORMAT_LINE.as_bytes())?; |
| 389 | 564× | write_atomic(&oxy.join("id"), hex::encode(id).as_bytes())?; |
| 390 | 564× | let mut config = Config::load(Some(&default_user_config()), &oxy.join("config"))?; |
| 391 | 564× | if let Some(url) = storage { |
| 392 | 3× | config.set("storage.url", url)?; |
| 393 | 561× | } |
| 394 | 564× | config.save(&oxy.join("config"))?; |
| 395 | 564× | let workspace = WorkspaceId::random(); |
| 396 | 564× | write_atomic(&oxy.join("workspace"), workspace.to_hex().as_bytes())?; |
| 397 | 564× | let store = open_objects(&oxy, &config)?; |
| 398 | 563× | let repo = Repo { |
| 399 | 563× | acceptor: None, |
| 400 | 563× | keyless_key: Default::default(), |
| 401 | 563× | last_change: Default::default(), |
| 402 | 563× | at_op: None, |
| 403 | 563× | head_seen: Default::default(), |
| 404 | 563× | walked_dirs: Default::default(), |
| 405 | 563× | listed_dirs: Default::default(), |
| 406 | 563× | git_export_skipped: Default::default(), |
| 407 | 563× | manifest_cache: Default::default(), |
| 408 | 563× | listing_cache: Default::default(), |
| 409 | 563× | untracked_nested: Default::default(), |
| 410 | 563× | skipped_large: Default::default(), |
| 411 | 563× | root, |
| 412 | 563× | oxy, |
| 413 | 563× | store, |
| 414 | 563× | workspace, |
| 415 | 563× | bare: false, |
| 416 | 563× | keys_dir: default_keys_dir(), |
| 417 | 563× | platform: crate::portable::Platform::current(), |
| 418 | 563× | user_config: default_user_config(), |
| 419 | 563× | config, |
| 420 | 563× | }; |
| 421 | 563× | repo.register_workspace(&workspace, &repo.root)?; |
| 422 | |
|
| 423 | 563× | let wc = repo.new_working_copy(vec![], ""); |
| 424 | 563× | let wc_hash = repo.store.put_change(&wc)?; |
| 425 | 563× | let mut view = View::default(); |
| 426 | 563× | view.bookmarks.insert( |
| 427 | 563× | repo.config.bookmark.clone(), |
| 428 | 563× | repo.store.put_state(&StateObject::new(vec![]))?, |
| 429 | | ); |
| 430 | 563× | view.workspaces.insert(workspace, wc_hash); |
| 431 | 563× | repo.record_op(&[], "oxy init", view)?; |
| 432 | 561× | Ok(repo) |
| 433 | 565× | } |
| 434 | |
|
| 435 | | |
| 436 | | |
| 437 | | |
| 438 | 5× | pub fn init_bare(root: &Path) -> Result<Repo> { |
| 439 | 5× | let mut id = [0u8; 16]; |
| 440 | 5× | rand::rngs::OsRng.fill_bytes(&mut id); |
| 441 | 5× | Self::init_bare_with_id(root, id) |
| 442 | 5× | } |
| 443 | |
|
| 444 | 45× | pub fn init_bare_with_id(root: &Path, id: [u8; 16]) -> Result<Repo> { |
| 445 | 45× | Self::init_bare_on(root, id, None) |
| 446 | 45× | } |
| 447 | |
|
| 448 | | |
| 449 | 45× | pub fn init_bare_on(root: &Path, id: [u8; 16], storage: Option<&str>) -> Result<Repo> { |
| 450 | 45× | fs::create_dir_all(root)?; |
| 451 | 45× | let root = root.canonicalize().unwrap_or_else(|_| root.to_path_buf()); |
| 452 | 45× | let oxy = root.join(OXY_DIR); |
| 453 | 45× | if oxy.exists() { |
| 454 | 0× | return fail(format!("{} is already an oxy repository", root.display())); |
| 455 | 45× | } |
| 456 | 45× | fs::create_dir_all(oxy.join("ops"))?; |
| 457 | 45× | write_atomic(&oxy.join("format"), FORMAT_LINE.as_bytes())?; |
| 458 | 45× | write_atomic(&oxy.join("id"), hex::encode(id).as_bytes())?; |
| 459 | 45× | write_atomic(&oxy.join(BARE_MARKER), b"")?; |
| 460 | 45× | let mut config = Config::load(Some(&default_user_config()), &oxy.join("config"))?; |
| 461 | 45× | if let Some(url) = storage { |
| 462 | 0× | config.set("storage.url", url)?; |
| 463 | 45× | } |
| 464 | 45× | config.save(&oxy.join("config"))?; |
| 465 | 45× | let store = open_objects(&oxy, &config)?; |
| 466 | 45× | let repo = Repo { |
| 467 | 45× | acceptor: None, |
| 468 | 45× | keyless_key: Default::default(), |
| 469 | 45× | last_change: Default::default(), |
| 470 | 45× | at_op: None, |
| 471 | 45× | head_seen: Default::default(), |
| 472 | 45× | walked_dirs: Default::default(), |
| 473 | 45× | listed_dirs: Default::default(), |
| 474 | 45× | git_export_skipped: Default::default(), |
| 475 | 45× | manifest_cache: Default::default(), |
| 476 | 45× | listing_cache: Default::default(), |
| 477 | 45× | untracked_nested: Default::default(), |
| 478 | 45× | skipped_large: Default::default(), |
| 479 | 45× | root, |
| 480 | 45× | oxy, |
| 481 | 45× | store, |
| 482 | 45× | workspace: WorkspaceId::ZERO, |
| 483 | 45× | bare: true, |
| 484 | 45× | keys_dir: default_keys_dir(), |
| 485 | 45× | platform: crate::portable::Platform::current(), |
| 486 | 45× | user_config: default_user_config(), |
| 487 | 45× | config, |
| 488 | 45× | }; |
| 489 | 45× | let mut view = View::default(); |
| 490 | 45× | view.bookmarks.insert( |
| 491 | 45× | repo.config.bookmark.clone(), |
| 492 | 45× | repo.store.put_state(&StateObject::new(vec![]))?, |
| 493 | | ); |
| 494 | 45× | repo.record_op(&[], "oxy init --bare", view)?; |
| 495 | 45× | Ok(repo) |
| 496 | 45× | } |
| 497 | |
|
| 498 | | |
| 499 | | |
| 500 | | |
| 501 | 19× | pub fn init_bare_unbound(root: &Path) -> Result<Repo> { |
| 502 | 19× | let repo = Self::init_bare_with_id(root, [0; 16])?; |
| 503 | 19× | fs::remove_file(repo.oxy.join("id"))?; |
| 504 | 19× | write_atomic(&repo.oxy.join(UNBOUND_MARKER), b"")?; |
| 505 | 19× | Ok(repo) |
| 506 | 19× | } |
| 507 | |
|
| 508 | 4216× | pub fn is_unbound(&self) -> bool { |
| 509 | 4216× | self.oxy.join(UNBOUND_MARKER).is_file() |
| 510 | 4216× | } |
| 511 | |
|
| 512 | | |
| 513 | 18× | pub fn bind_id(&self, id: [u8; 16]) -> Result<()> { |
| 514 | 18× | if !self.is_unbound() { |
| 515 | 0× | return fail("this repository already has an id"); |
| 516 | 18× | } |
| 517 | 18× | if id == [0; 16] { |
| 518 | 0× | return fail("a repository id cannot be all zero"); |
| 519 | 18× | } |
| 520 | 18× | write_atomic(&self.oxy.join("id"), hex::encode(id).as_bytes())?; |
| 521 | 18× | fs::remove_file(self.oxy.join(UNBOUND_MARKER))?; |
| 522 | 18× | Ok(()) |
| 523 | 18× | } |
| 524 | |
|
| 525 | | |
| 526 | 78× | pub fn is_bare(&self) -> bool { |
| 527 | 78× | self.bare |
| 528 | 78× | } |
| 529 | |
|
| 530 | | |
| 531 | 2196× | pub fn open(start: &Path) -> Result<Repo> { |
| 532 | 2196× | let start = start.canonicalize()?; |
| 533 | 2196× | let mut dir: Option<&Path> = Some(&start); |
| 534 | 2196× | while let Some(d) = dir { |
| 535 | 2196× | let oxy = d.join(OXY_DIR); |
| 536 | 2196× | if oxy.is_file() { |
| 537 | | |
| 538 | 9× | let text = fs::read_to_string(&oxy)?; |
| 539 | 9× | let Some((shared, workspace)) = workspace::parse_workspace_file(&text) else { |
| 540 | 0× | return fail(format!("{} is not a valid workspace file", oxy.display())); |
| 541 | | }; |
| 542 | 9× | let format = fs::read_to_string(shared.join("format"))?; |
| 543 | 9× | if format != FORMAT_LINE { |
| 544 | 0× | return fail(format!("unsupported repository format: {}", format.trim())); |
| 545 | 9× | } |
| 546 | 9× | let config = Config::load(Some(&default_user_config()), &shared.join("config"))?; |
| 547 | 9× | let store = open_objects(&shared, &config)?; |
| 548 | 9× | let mut repo = Repo { |
| 549 | 9× | acceptor: None, |
| 550 | 9× | keyless_key: Default::default(), |
| 551 | 9× | last_change: Default::default(), |
| 552 | 9× | at_op: None, |
| 553 | 9× | head_seen: Default::default(), |
| 554 | 9× | walked_dirs: Default::default(), |
| 555 | 9× | listed_dirs: Default::default(), |
| 556 | 9× | git_export_skipped: Default::default(), |
| 557 | 9× | manifest_cache: Default::default(), |
| 558 | 9× | listing_cache: Default::default(), |
| 559 | 9× | untracked_nested: Default::default(), |
| 560 | 9× | skipped_large: Default::default(), |
| 561 | 9× | root: d.to_path_buf(), |
| 562 | 9× | oxy: shared, |
| 563 | 9× | store, |
| 564 | 9× | workspace, |
| 565 | 9× | bare: false, |
| 566 | 9× | keys_dir: default_keys_dir(), |
| 567 | 9× | platform: crate::portable::Platform::current(), |
| 568 | 9× | user_config: default_user_config(), |
| 569 | 9× | config, |
| 570 | 9× | }; |
| 571 | 9× | repo.apply_workspace_bookmark(); |
| 572 | 9× | return Ok(repo); |
| 573 | 2187× | } |
| 574 | 2187× | if oxy.is_dir() { |
| 575 | 2187× | let format = fs::read_to_string(oxy.join("format"))?; |
| 576 | 2187× | let config = Config::load(Some(&default_user_config()), &oxy.join("config"))?; |
| 577 | 2187× | let bare = oxy.join(BARE_MARKER).is_file(); |
| 578 | 2187× | let workspace = if bare { |
| 579 | 189× | WorkspaceId::ZERO |
| 580 | | } else { |
| 581 | 1998× | WorkspaceId::from_hex(fs::read_to_string(oxy.join("workspace"))?.trim()) |
| 582 | 1998× | .ok_or_else(|| RepoError::Msg("corrupt .oxy/workspace".into()))? |
| 583 | | }; |
| 584 | 2187× | let store = open_objects(&oxy, &config)?; |
| 585 | 2187× | let mut repo = Repo { |
| 586 | 2187× | acceptor: None, |
| 587 | 2187× | keyless_key: Default::default(), |
| 588 | 2187× | last_change: Default::default(), |
| 589 | 2187× | at_op: None, |
| 590 | 2187× | head_seen: Default::default(), |
| 591 | 2187× | walked_dirs: Default::default(), |
| 592 | 2187× | listed_dirs: Default::default(), |
| 593 | 2187× | git_export_skipped: Default::default(), |
| 594 | 2187× | manifest_cache: Default::default(), |
| 595 | 2187× | listing_cache: Default::default(), |
| 596 | 2187× | untracked_nested: Default::default(), |
| 597 | 2187× | skipped_large: Default::default(), |
| 598 | 2187× | root: d.to_path_buf(), |
| 599 | 2187× | oxy, |
| 600 | 2187× | store, |
| 601 | 2187× | workspace, |
| 602 | 2187× | bare, |
| 603 | 2187× | keys_dir: default_keys_dir(), |
| 604 | 2187× | platform: crate::portable::Platform::current(), |
| 605 | 2187× | user_config: default_user_config(), |
| 606 | 2187× | config, |
| 607 | 2187× | }; |
| 608 | 2187× | if !bare { |
| 609 | 1998× | repo.apply_workspace_bookmark(); |
| 610 | 1998× | } |
| 611 | 2187× | if format == FORMAT1_LINE { |
| 612 | 1× | repo.migrate_format1()?; |
| 613 | 2186× | } else if format != FORMAT_LINE { |
| 614 | 0× | return fail(format!("unsupported repository format: {}", format.trim())); |
| 615 | 2186× | } |
| 616 | 2187× | return Ok(repo); |
| 617 | 0× | } |
| 618 | 0× | dir = d.parent(); |
| 619 | | } |
| 620 | 0× | fail(format!( |
| 621 | | "no oxy repository found at or above {}; start one with `oxy init` or `oxy clone URL`", |
| 622 | 0× | start.display() |
| 623 | | )) |
| 624 | 2196× | } |
| 625 | |
|
| 626 | | |
| 627 | | |
| 628 | 1× | fn migrate_format1(&self) -> Result<()> { |
| 629 | 1× | let (head, version) = self.read_head()?; |
| 630 | | |
| 631 | 1× | let mut chain: Vec<Format1Op> = Vec::new(); |
| 632 | 1× | let mut cur = Some(head); |
| 633 | 3× | while let Some(r) = cur { |
| 634 | 2× | let op = Op::decode_format1(&self.store.get_typed(&r, ObjectType::Op)?)?; |
| 635 | 2× | cur = op.parents.first().copied(); |
| 636 | 2× | chain.push(op); |
| 637 | | } |
| 638 | 1× | chain.reverse(); |
| 639 | 1× | let mut new_parent: Option<ObjectRef> = None; |
| 640 | 2× | for old in chain { |
| 641 | 2× | let mut view = View::default(); |
| 642 | 2× | for (name, heads) in &old.view.bookmarks { |
| 643 | 2× | view.bookmarks.insert( |
| 644 | 2× | name.clone(), |
| 645 | 2× | self.store.put_state(&StateObject::new(heads.clone()))?, |
| 646 | | ); |
| 647 | | } |
| 648 | 2× | view.workspaces = old.view.workspaces.clone(); |
| 649 | 2× | let op = Op { |
| 650 | 2× | parents: new_parent.into_iter().collect(), |
| 651 | 2× | hlc: old.hlc, |
| 652 | 2× | command: old.command, |
| 653 | 2× | view, |
| 654 | 2× | workspace: None, |
| 655 | 2× | undoes: None, |
| 656 | 2× | }; |
| 657 | 2× | new_parent = Some(self.store.put(ObjectType::Op, &op.encode())?); |
| 658 | | } |
| 659 | 1× | if !self.swap_head(&new_parent.unwrap(), version.as_ref())? { |
| 660 | 0× | return fail("the operation log changed while it was rewritten; run the command again"); |
| 661 | 1× | } |
| 662 | 1× | write_atomic(&self.oxy.join("format"), FORMAT_LINE.as_bytes())?; |
| 663 | 1× | Ok(()) |
| 664 | 1× | } |
| 665 | |
|
| 666 | | |
| 667 | | |
| 668 | | |
| 669 | 747× | pub(crate) fn maybe_autopack(&self) -> Result<()> { |
| 670 | 747× | self.store |
| 671 | 747× | .expire_retired(crate::store::RETIRED_FOR, &mut |_| {})?; |
| 672 | 747× | if self.store.loose_count()? > AUTOPACK_THRESHOLD { |
| 673 | 0× | self.store.pack_loose()?; |
| 674 | 0× | self.store.merge_packs()?; |
| 675 | 747× | } |
| 676 | 747× | Ok(()) |
| 677 | 747× | } |
| 678 | |
|
| 679 | | |
| 680 | 3873× | pub fn id(&self) -> Result<[u8; 16]> { |
| 681 | 3873× | if self.is_unbound() { |
| 682 | 27× | return Ok([0; 16]); |
| 683 | 3846× | } |
| 684 | 3846× | let path = self.oxy.join("id"); |
| 685 | 3846× | match fs::read_to_string(&path) { |
| 686 | 3846× | Ok(h) => hex::decode(h.trim()) |
| 687 | 3846× | .ok() |
| 688 | 3846× | .and_then(|v| v.try_into().ok()) |
| 689 | 3846× | .ok_or_else(|| RepoError::Msg("corrupt .oxy/id".into())), |
| 690 | 0× | Err(e) if e.kind() == std::io::ErrorKind::NotFound => { |
| 691 | 0× | let mut id = [0u8; 16]; |
| 692 | 0× | rand::rngs::OsRng.fill_bytes(&mut id); |
| 693 | 0× | write_atomic(&path, hex::encode(id).as_bytes())?; |
| 694 | 0× | Ok(id) |
| 695 | | } |
| 696 | 0× | Err(e) => Err(e.into()), |
| 697 | | } |
| 698 | 3873× | } |
| 699 | |
|
| 700 | 65× | pub fn root(&self) -> &Path { |
| 701 | 65× | &self.root |
| 702 | 65× | } |
| 703 | 579× | pub fn store(&self) -> &Store { |
| 704 | 579× | &self.store |
| 705 | 579× | } |
| 706 | 10× | pub fn workspace(&self) -> WorkspaceId { |
| 707 | 10× | self.workspace |
| 708 | 10× | } |
| 709 | |
|
| 710 | | |
| 711 | | |
| 712 | | |
| 713 | | |
| 714 | | |
| 715 | | |
| 716 | 48× | pub fn with_acceptor(mut self, identity: Identity, sk: crate::identity::SecretKey) -> Repo { |
| 717 | 48× | self.acceptor = Some(std::sync::Arc::new((identity, sk))); |
| 718 | 48× | self |
| 719 | 48× | } |
| 720 | |
|
| 721 | 2× | pub fn with_platform(mut self, platform: crate::portable::Platform) -> Repo { |
| 722 | 2× | self.platform = platform; |
| 723 | 2× | self |
| 724 | 2× | } |
| 725 | |
|
| 726 | 0× | pub fn platform(&self) -> crate::portable::Platform { |
| 727 | 0× | self.platform |
| 728 | 0× | } |
| 729 | |
|
| 730 | 206× | pub fn with_home(mut self, dir: impl Into<PathBuf>) -> Repo { |
| 731 | 206× | let dir = dir.into(); |
| 732 | 206× | self.keys_dir = dir.join("keys"); |
| 733 | 206× | self.user_config = dir.join("config.toml"); |
| 734 | 206× | if let Ok(c) = Config::load(Some(&self.user_config), &self.oxy.join("config")) { |
| 735 | 206× | self.config = c; |
| 736 | 206× | } |
| 737 | 206× | self.apply_workspace_bookmark(); |
| 738 | 206× | self |
| 739 | 206× | } |
| 740 | |
|
| 741 | | |
| 742 | | |
| 743 | 2213× | fn apply_workspace_bookmark(&mut self) { |
| 744 | 2213× | if let Ok(b) = fs::read_to_string( |
| 745 | 2213× | self.oxy |
| 746 | 2213× | .join("workspaces") |
| 747 | 2213× | .join(self.workspace.to_hex()) |
| 748 | 2213× | .join("bookmark"), |
| 749 | 2213× | ) { |
| 750 | 35× | let b = b.trim(); |
| 751 | 35× | if !b.is_empty() { |
| 752 | 35× | self.config.bookmark = b.to_string(); |
| 753 | 35× | } |
| 754 | 2178× | } |
| 755 | 2213× | } |
| 756 | |
|
| 757 | 2× | pub fn user_config_path(&self) -> &Path { |
| 758 | 2× | &self.user_config |
| 759 | 2× | } |
| 760 | |
|
| 761 | | |
| 762 | 28× | pub fn set_user_config(&mut self, key: &str, value: &str) -> Result<()> { |
| 763 | 28× | Config::set_user(&self.user_config, key, value)?; |
| 764 | 27× | self.config = Config::load(Some(&self.user_config), &self.oxy.join("config"))?; |
| 765 | 27× | Ok(()) |
| 766 | 28× | } |
| 767 | |
|
| 768 | 417× | pub fn save_config(&self) -> Result<()> { |
| 769 | 417× | self.config.save(&self.oxy.join("config")) |
| 770 | 417× | } |
| 771 | |
|
| 772 | 4918× | fn author(&self) -> KeyId { |
| 773 | 4918× | self.config.author_key.unwrap_or(ObjectRef::ZERO) |
| 774 | 4918× | } |
| 775 | | } |
| 776 | |
|
| 777 | | impl Repo { |
| 778 | 1891× | fn new_working_copy(&self, deps: Vec<ChangeHash>, message: &str) -> Change { |
| 779 | 1891× | let mut c = Change { |
| 780 | 1891× | version: crate::objects::CHANGE_VERSION, |
| 781 | 1891× | change_id: ChangeId::random(), |
| 782 | 1891× | deps, |
| 783 | 1891× | author: self.author(), |
| 784 | 1891× | hlc: Hlc::now(), |
| 785 | 1891× | message: message.into(), |
| 786 | 1891× | tree_ops: vec![], |
| 787 | 1891× | edits: vec![], |
| 788 | 1891× | kind: ChangeKind::WorkingCopy, |
| 789 | 1891× | }; |
| 790 | 1891× | c.normalize(); |
| 791 | 1891× | c |
| 792 | 1891× | } |
| 793 | |
|
| 794 | | |
| 795 | |
|
| 796 | 16661× | pub fn head_op(&self) -> Result<(ObjectRef, Op)> { |
| 797 | 16661× | if let Some(r) = self.at_op { |
| 798 | 5× | return Ok((r, Op::decode(&self.store.get_typed(&r, ObjectType::Op)?)?)); |
| 799 | 16656× | } |
| 800 | 16656× | let r = self.head_ref()?; |
| 801 | 16654× | let op = Op::decode(&self.store.get_typed(&r, ObjectType::Op)?)?; |
| 802 | 16654× | Ok((r, op)) |
| 803 | 16661× | } |
| 804 | |
|
| 805 | 11278× | pub fn view(&self) -> Result<View> { |
| 806 | 11278× | Ok(self.head_op()?.1.view) |
| 807 | 11278× | } |
| 808 | |
|
| 809 | 3913× | fn record_op(&self, parents: &[ObjectRef], command: &str, view: View) -> Result<ObjectRef> { |
| 810 | 3913× | self.record_op_undoing(parents, command, view, None) |
| 811 | 3913× | } |
| 812 | |
|
| 813 | | |
| 814 | 3916× | fn record_op_undoing( |
| 815 | 3916× | &self, |
| 816 | 3916× | parents: &[ObjectRef], |
| 817 | 3916× | command: &str, |
| 818 | 3916× | view: View, |
| 819 | 3916× | undoes: Option<ObjectRef>, |
| 820 | 3916× | ) -> Result<ObjectRef> { |
| 821 | 3916× | if self.at_op.is_some() { |
| 822 | 1× | return fail( |
| 823 | | "--at-op shows the repository as it was; this command would change it (run it without --at-op)", |
| 824 | | ); |
| 825 | 3915× | } |
| 826 | 3915× | let op = Op { |
| 827 | 3915× | parents: parents.to_vec(), |
| 828 | 3915× | hlc: Hlc::now(), |
| 829 | 3915× | command: command.into(), |
| 830 | 3915× | view, |
| 831 | 3915× | workspace: Some(self.workspace), |
| 832 | 3915× | undoes, |
| 833 | 3915× | }; |
| 834 | 3915× | let r = self.store.put(ObjectType::Op, &op.encode())?; |
| 835 | | |
| 836 | 3913× | let flushing = crate::timing::phase("op: flush the store"); |
| 837 | 3913× | self.store.flush()?; |
| 838 | 3911× | drop(flushing); |
| 839 | | |
| 840 | | |
| 841 | | |
| 842 | | |
| 843 | | |
| 844 | 3911× | let _lock = if self.store.is_local() { |
| 845 | 3899× | Some(self.lock_ops()?) |
| 846 | | } else { |
| 847 | 12× | None |
| 848 | | }; |
| 849 | 3911× | let Some(parent) = parents.first() else { |
| 850 | | |
| 851 | 608× | if !self.swap_head(&r, None)? { |
| 852 | 2× | return fail(format!( |
| 853 | | "{} already holds a repository's operation log", |
| 854 | 2× | self.store.backend().describe() |
| 855 | | )); |
| 856 | 606× | } |
| 857 | 606× | return Ok(r); |
| 858 | | }; |
| 859 | 3303× | let base = Op::decode(&self.store.get_typed(parent, ObjectType::Op)?)?.view; |
| 860 | 3303× | for _ in 0..HEAD_TRIES { |
| 861 | 3303× | let (current, version) = self.read_head()?; |
| 862 | 3303× | let to = if current == *parent { |
| 863 | 3277× | r |
| 864 | | } else { |
| 865 | 26× | let theirs = Op::decode(&self.store.get_typed(¤t, ObjectType::Op)?)?.view; |
| 866 | 25× | let merged = Op { |
| 867 | 26× | parents: vec![current, r], |
| 868 | 26× | hlc: Hlc::now(), |
| 869 | 26× | command: format!("{command} (merged with a concurrent operation)"), |
| 870 | 26× | view: self.merge_views(&base, &op.view, &theirs)?, |
| 871 | 25× | workspace: Some(self.workspace), |
| 872 | 25× | undoes: None, |
| 873 | | }; |
| 874 | 25× | let m = self.store.put(ObjectType::Op, &merged.encode())?; |
| 875 | 25× | self.store.flush()?; |
| 876 | 25× | m |
| 877 | | }; |
| 878 | 3302× | if self.swap_head(&to, version.as_ref())? { |
| 879 | 3296× | return Ok(to); |
| 880 | 0× | } |
| 881 | | } |
| 882 | 0× | fail("other oxy processes kept changing the operation log while this one recorded; run the command again") |
| 883 | 3916× | } |
| 884 | |
|
| 885 | | |
| 886 | 3909× | fn lock_ops(&self) -> Result<FileLock> { |
| 887 | 3909× | fs::create_dir_all(self.oxy.join("ops"))?; |
| 888 | 3909× | FileLock::acquire( |
| 889 | 3909× | self.oxy.join("ops").join("lock"), |
| 890 | 3909× | "another oxy process holds the operation log", |
| 891 | | ) |
| 892 | 3909× | } |
| 893 | |
|
| 894 | | |
| 895 | | |
| 896 | 16658× | fn head_ref(&self) -> Result<ObjectRef> { |
| 897 | 16658× | if let Some(r) = self.head_seen.get() { |
| 898 | 12989× | return Ok(r); |
| 899 | 3669× | } |
| 900 | 3669× | let r = self.read_head()?.0; |
| 901 | 3667× | self.head_seen.set(Some(r)); |
| 902 | 3667× | Ok(r) |
| 903 | 16658× | } |
| 904 | |
|
| 905 | | |
| 906 | | |
| 907 | | |
| 908 | | |
| 909 | | |
| 910 | | |
| 911 | | |
| 912 | 505× | pub fn refresh(&self) -> Result<()> { |
| 913 | 505× | self.forget_head(); |
| 914 | 505× | self.store.refresh()?; |
| 915 | 505× | Ok(()) |
| 916 | 505× | } |
| 917 | |
|
| 918 | | |
| 919 | | |
| 920 | 2380× | pub(crate) fn forget_head(&self) { |
| 921 | 2380× | self.head_seen.set(None); |
| 922 | 2380× | } |
| 923 | |
|
| 924 | | |
| 925 | | |
| 926 | | |
| 927 | | |
| 928 | 7063× | fn read_head(&self) -> Result<(ObjectRef, Option<crate::strata::Version>)> { |
| 929 | 7063× | if self.store.is_local() { |
| 930 | 7045× | let hex = fs::read_to_string(self.oxy.join("ops").join("head"))?; |
| 931 | 7045× | let r = ObjectRef::from_hex(hex.trim()) |
| 932 | 7045× | .ok_or_else(|| RepoError::Msg("corrupt .oxy/ops/head".into()))?; |
| 933 | 7042× | return Ok((r, None)); |
| 934 | 18× | } |
| 935 | 18× | let backend = self.store.backend(); |
| 936 | 18× | match backend.read_ref(HEAD_RECORD) { |
| 937 | 18× | Ok(Some((bytes, version))) => { |
| 938 | 18× | let r = ObjectRef::from_hex(String::from_utf8_lossy(&bytes).trim()).ok_or_else( |
| 939 | 0× | || { |
| 940 | 0× | RepoError::Msg(format!( |
| 941 | 0× | "the operation head in {} is corrupt", |
| 942 | 0× | backend.describe() |
| 943 | 0× | )) |
| 944 | 0× | }, |
| 945 | 0× | )?; |
| 946 | 18× | Ok((r, Some(version))) |
| 947 | | } |
| 948 | 0× | Ok(None) => fail(format!( |
| 949 | | "{} holds no operation log (no {HEAD_RECORD} record)", |
| 950 | 0× | backend.describe() |
| 951 | | )), |
| 952 | 0× | Err(e) => fail(format!("read the operation head: {e}")), |
| 953 | | } |
| 954 | 7063× | } |
| 955 | |
|
| 956 | | |
| 957 | | |
| 958 | | |
| 959 | | |
| 960 | | |
| 961 | 3923× | fn swap_head(&self, to: &ObjectRef, expected: Option<&crate::strata::Version>) -> Result<bool> { |
| 962 | 3923× | if self.store.is_local() { |
| 963 | 3909× | write_atomic(&self.oxy.join("ops").join("head"), to.to_hex().as_bytes())?; |
| 964 | 3903× | self.head_seen.set(Some(*to)); |
| 965 | 3903× | return Ok(true); |
| 966 | 14× | } |
| 967 | 14× | match self |
| 968 | 14× | .store |
| 969 | 14× | .backend() |
| 970 | 14× | .write_ref(HEAD_RECORD, to.to_hex().as_bytes(), expected) |
| 971 | | { |
| 972 | | Ok(_) => { |
| 973 | 11× | self.head_seen.set(Some(*to)); |
| 974 | 11× | Ok(true) |
| 975 | | } |
| 976 | 3× | Err(crate::strata::Error::Conflict(_)) => Ok(false), |
| 977 | 0× | Err(e) => fail(format!("move the operation head: {e}")), |
| 978 | | } |
| 979 | 3923× | } |
| 980 | |
|
| 981 | | |
| 982 | | |
| 983 | | |
| 984 | | |
| 985 | | |
| 986 | | |
| 987 | | |
| 988 | | |
| 989 | 26× | fn merge_views(&self, base: &View, ours: &View, theirs: &View) -> Result<View> { |
| 990 | 94× | fn pick<K: Ord + Clone, V: Clone + PartialEq>( |
| 991 | 94× | b: &BTreeMap<K, V>, |
| 992 | 94× | o: &BTreeMap<K, V>, |
| 993 | 94× | t: &BTreeMap<K, V>, |
| 994 | 94× | mut both: impl FnMut(&V, &V) -> Result<V>, |
| 995 | 94× | ) -> Result<BTreeMap<K, V>> { |
| 996 | 94× | let keys: BTreeSet<&K> = b.keys().chain(o.keys()).chain(t.keys()).collect(); |
| 997 | 94× | let mut out = BTreeMap::new(); |
| 998 | 97× | for k in keys { |
| 999 | 97× | let (bv, ov, tv) = (b.get(k), o.get(k), t.get(k)); |
| 1000 | 97× | let v = if ov == tv || tv == bv { |
| 1001 | 49× | ov.cloned() |
| 1002 | 48× | } else if ov == bv { |
| 1003 | 28× | tv.cloned() |
| 1004 | | } else { |
| 1005 | 20× | match (ov, tv) { |
| 1006 | 20× | (Some(x), Some(y)) => Some(both(x, y)?), |
| 1007 | 0× | (Some(x), None) | (None, Some(x)) => Some(x.clone()), |
| 1008 | 0× | (None, None) => None, |
| 1009 | | } |
| 1010 | | }; |
| 1011 | 96× | if let Some(v) = v { |
| 1012 | 96× | out.insert(k.clone(), v); |
| 1013 | 96× | } |
| 1014 | | } |
| 1015 | 93× | Ok(out) |
| 1016 | 94× | } |
| 1017 | 26× | let bookmarks = pick( |
| 1018 | 26× | &base.bookmarks, |
| 1019 | 26× | &ours.bookmarks, |
| 1020 | 26× | &theirs.bookmarks, |
| 1021 | 19× | |x, y| { |
| 1022 | 19× | let mut heads = self.store.get_state(x)?.heads; |
| 1023 | 19× | heads.extend(self.store.get_state(y)?.heads); |
| 1024 | 19× | let heads = self.load_state(&heads)?.maximal(&heads); |
| 1025 | 19× | Ok(self.store.put_state(&StateObject::new(heads))?) |
| 1026 | 19× | }, |
| 1027 | 0× | )?; |
| 1028 | | |
| 1029 | | |
| 1030 | | |
| 1031 | | |
| 1032 | 26× | let workspaces = pick( |
| 1033 | 26× | &base.workspaces, |
| 1034 | 26× | &ours.workspaces, |
| 1035 | 26× | &theirs.workspaces, |
| 1036 | 1× | |_, _| { |
| 1037 | 1× | fail( |
| 1038 | | "another oxy process changed this working copy at the same time; \ |
| 1039 | | this command was not recorded: check `oxy status` and run it again", |
| 1040 | | ) |
| 1041 | 1× | }, |
| 1042 | 1× | )?; |
| 1043 | 25× | let tags = pick(&base.tags, &ours.tags, &theirs.tags, |_, y| Ok(*y))?; |
| 1044 | 25× | let empty = BTreeMap::new(); |
| 1045 | 25× | let names: BTreeSet<&String> = base |
| 1046 | 25× | .remotes |
| 1047 | 25× | .keys() |
| 1048 | 25× | .chain(ours.remotes.keys()) |
| 1049 | 25× | .chain(theirs.remotes.keys()) |
| 1050 | 25× | .collect(); |
| 1051 | 25× | let mut remotes = BTreeMap::new(); |
| 1052 | 25× | for n in names { |
| 1053 | 17× | let m = pick( |
| 1054 | 17× | base.remotes.get(n).unwrap_or(&empty), |
| 1055 | 17× | ours.remotes.get(n).unwrap_or(&empty), |
| 1056 | 17× | theirs.remotes.get(n).unwrap_or(&empty), |
| 1057 | 0× | |x, _| Ok(*x), |
| 1058 | 0× | )?; |
| 1059 | 17× | if !m.is_empty() { |
| 1060 | 17× | remotes.insert(n.clone(), m); |
| 1061 | 17× | } |
| 1062 | | } |
| 1063 | 25× | let policy = if ours.policy == theirs.policy || theirs.policy == base.policy { |
| 1064 | 25× | ours.policy |
| 1065 | 0× | } else if ours.policy == base.policy { |
| 1066 | 0× | theirs.policy |
| 1067 | | } else { |
| 1068 | 0× | ours.policy.or(theirs.policy) |
| 1069 | | }; |
| 1070 | 25× | self.store.flush()?; |
| 1071 | 25× | Ok(View { |
| 1072 | 25× | bookmarks, |
| 1073 | 25× | workspaces, |
| 1074 | 25× | remotes, |
| 1075 | 25× | policy, |
| 1076 | 25× | tags, |
| 1077 | 25× | }) |
| 1078 | 26× | } |
| 1079 | |
|
| 1080 | 56× | pub fn op_log(&self) -> Result<Vec<(ObjectRef, Op)>> { |
| 1081 | 56× | let mut out = Vec::new(); |
| 1082 | 56× | let (mut r, mut op) = self.head_op()?; |
| 1083 | | loop { |
| 1084 | 297× | let next = op.parents.first().copied(); |
| 1085 | 297× | out.push((r, op)); |
| 1086 | 297× | match next { |
| 1087 | 241× | Some(p) => { |
| 1088 | 241× | r = p; |
| 1089 | 241× | op = Op::decode(&self.store.get_typed(&r, ObjectType::Op)?)?; |
| 1090 | | } |
| 1091 | 56× | None => break, |
| 1092 | | } |
| 1093 | | } |
| 1094 | 56× | Ok(out) |
| 1095 | 56× | } |
| 1096 | |
|
| 1097 | | |
| 1098 | | |
| 1099 | | |
| 1100 | | |
| 1101 | | |
| 1102 | | |
| 1103 | | |
| 1104 | | |
| 1105 | | |
| 1106 | | |
| 1107 | | |
| 1108 | | |
| 1109 | | |
| 1110 | | |
| 1111 | | |
| 1112 | | |
| 1113 | | |
| 1114 | | |
| 1115 | | |
| 1116 | | |
| 1117 | | |
| 1118 | | |
| 1119 | 16× | pub fn undo(&self) -> Result<Undone> { |
| 1120 | 16× | let _wc = self.lock_working_copy()?; |
| 1121 | 16× | let clean_now = |
| 1122 | 16× | self.clean_by_manifest()? || self.snapshot_status("oxy undo")?.statuses.is_empty(); |
| 1123 | 16× | let (head_ref, head) = self.head_op()?; |
| 1124 | 16× | let wc_now = head.view.workspaces.get(&self.workspace).copied(); |
| 1125 | 39× | let load = |r: &ObjectRef| -> Result<Op> { |
| 1126 | 39× | Ok(Op::decode(&self.store.get_typed(r, ObjectType::Op)?)?) |
| 1127 | 39× | }; |
| 1128 | 16× | let mut skip: BTreeSet<ObjectRef> = BTreeSet::new(); |
| 1129 | 16× | let mut pending_checkout: Option<Op> = None; |
| 1130 | 16× | let mut others = 0usize; |
| 1131 | 16× | let (mut r, mut op) = (head_ref, head.clone()); |
| 1132 | 15× | let (target_ref, target, parent) = loop { |
| 1133 | 40× | let parent = match op.parents.first() { |
| 1134 | 39× | Some(p) => Some((*p, load(p)?)), |
| 1135 | 1× | None => None, |
| 1136 | | }; |
| 1137 | 40× | if skip.contains(&r) { |
| 1138 | 1× | pending_checkout = None; |
| 1139 | 39× | } else if op.is_snapshot() || Self::is_push(&op, parent.as_ref().map(|(_, p)| p)) { |
| 1140 | 9× | |
| 1141 | 9× | |
| 1142 | 30× | } else if !self.op_is_ours(&op, parent.as_ref().map(|(_, p)| p)) { |
| 1143 | 10× | others += 1; |
| 1144 | 20× | } else if let Some(u) = op.undoes { |
| 1145 | 1× | skip.insert(u); |
| 1146 | 1× | pending_checkout = None; |
| 1147 | 19× | } else if op.command.ends_with(" (checkout)") { |
| 1148 | 3× | pending_checkout = Some(op.clone()); |
| 1149 | 3× | } else { |
| 1150 | 16× | match parent { |
| 1151 | 15× | Some((_, p)) => break (r, op, p), |
| 1152 | 1× | None => return fail("nothing to undo in this workspace"), |
| 1153 | | } |
| 1154 | | } |
| 1155 | 24× | match parent { |
| 1156 | 24× | Some((pr, p)) => (r, op) = (pr, p), |
| 1157 | 0× | None => return fail("nothing to undo in this workspace"), |
| 1158 | | } |
| 1159 | | }; |
| 1160 | 15× | let before = parent.view; |
| 1161 | 15× | let after = pending_checkout |
| 1162 | 15× | .map(|o| o.view) |
| 1163 | 15× | .unwrap_or_else(|| target.view.clone()); |
| 1164 | 15× | let changes = self.view_changes(&before, &after)?; |
| 1165 | 15× | let pushed = self.pushed_by(&before, &after, &head.view)?; |
| 1166 | 15× | let restored_wc = before.workspaces.get(&self.workspace).copied(); |
| 1167 | 15× | if others == 0 { |
| 1168 | | |
| 1169 | 10× | let _lock = if self.store.is_local() { |
| 1170 | 10× | Some(self.lock_ops()?) |
| 1171 | | } else { |
| 1172 | 0× | None |
| 1173 | | }; |
| 1174 | 10× | let parent_ref = *target.parents.first().expect("the target has a parent"); |
| 1175 | 10× | let (now, version) = self.read_head()?; |
| 1176 | 10× | if now != head_ref || !self.swap_head(&parent_ref, version.as_ref())? { |
| 1177 | 0× | return fail("another oxy command ran while undoing; run undo again"); |
| 1178 | 10× | } |
| 1179 | | } else { |
| 1180 | 5× | let view = self.revert_view(&head.view, &before, &after)?; |
| 1181 | 3× | self.record_op_undoing(&[head_ref], "oxy undo", view, Some(target_ref))?; |
| 1182 | | } |
| 1183 | 13× | self.drop_manifest(); |
| 1184 | 13× | if let Some(then) = restored_wc { |
| 1185 | 13× | if clean_now && Some(then) != wc_now { |
| 1186 | | |
| 1187 | | |
| 1188 | 10× | let Ok(wc) = self.store.get_change(&then) else { |
| 1189 | 0× | return Ok(Undone { |
| 1190 | 0× | command: target.command, |
| 1191 | 0× | changes, |
| 1192 | 0× | others_kept: others, |
| 1193 | 0× | pushed, |
| 1194 | 0× | }); |
| 1195 | | }; |
| 1196 | 10× | if wc.edits.is_empty() && wc.tree_ops.is_empty() { |
| 1197 | 8× | let base = self.load_state_history(&wc.deps)?.maximal(&wc.deps); |
| 1198 | | |
| 1199 | | |
| 1200 | | |
| 1201 | 8× | self.checkout(&base, true, "oxy undo (checkout)")?; |
| 1202 | 2× | } |
| 1203 | 3× | } |
| 1204 | 0× | } |
| 1205 | 13× | Ok(Undone { |
| 1206 | 13× | command: target.command, |
| 1207 | 13× | changes, |
| 1208 | 13× | others_kept: others, |
| 1209 | 13× | pushed, |
| 1210 | 13× | }) |
| 1211 | 16× | } |
| 1212 | |
|
| 1213 | | |
| 1214 | | |
| 1215 | 31× | fn is_push(op: &Op, parent: Option<&Op>) -> bool { |
| 1216 | 31× | let Some(p) = parent else { |
| 1217 | 1× | return false; |
| 1218 | | }; |
| 1219 | 30× | op.command.starts_with("oxy push") |
| 1220 | 1× | && op.view.bookmarks == p.view.bookmarks |
| 1221 | 1× | && op.view.tags == p.view.tags |
| 1222 | 1× | && op.view.workspaces == p.view.workspaces |
| 1223 | 1× | && op.view.policy == p.view.policy |
| 1224 | 31× | } |
| 1225 | |
|
| 1226 | | |
| 1227 | | |
| 1228 | | |
| 1229 | | |
| 1230 | 30× | fn op_is_ours(&self, op: &Op, parent: Option<&Op>) -> bool { |
| 1231 | 30× | if let Some(w) = op.workspace { |
| 1232 | 30× | return w == self.workspace; |
| 1233 | 0× | } |
| 1234 | 0× | let Some(p) = parent else { |
| 1235 | 0× | return true; |
| 1236 | | }; |
| 1237 | 0× | let ids: BTreeSet<&WorkspaceId> = op |
| 1238 | 0× | .view |
| 1239 | 0× | .workspaces |
| 1240 | 0× | .keys() |
| 1241 | 0× | .chain(p.view.workspaces.keys()) |
| 1242 | 0× | .collect(); |
| 1243 | 0× | let moved: Vec<&WorkspaceId> = ids |
| 1244 | 0× | .into_iter() |
| 1245 | 0× | .filter(|id| op.view.workspaces.get(id) != p.view.workspaces.get(id)) |
| 1246 | 0× | .collect(); |
| 1247 | 0× | moved.is_empty() || moved.contains(&&self.workspace) |
| 1248 | 30× | } |
| 1249 | |
|
| 1250 | | |
| 1251 | | |
| 1252 | | |
| 1253 | 5× | fn revert_view(&self, current: &View, before: &View, after: &View) -> Result<View> { |
| 1254 | 10× | fn back<K: Ord + Clone, V: Clone + PartialEq>( |
| 1255 | 10× | cur: &mut BTreeMap<K, V>, |
| 1256 | 10× | before: &BTreeMap<K, V>, |
| 1257 | 10× | after: &BTreeMap<K, V>, |
| 1258 | 10× | name: impl Fn(&K) -> String, |
| 1259 | 10× | clashes: &mut Vec<String>, |
| 1260 | 10× | ) { |
| 1261 | 10× | let keys: BTreeSet<&K> = before.keys().chain(after.keys()).collect(); |
| 1262 | 10× | for k in keys { |
| 1263 | 5× | let (b, a) = (before.get(k), after.get(k)); |
| 1264 | 5× | if a == b { |
| 1265 | 4× | continue; |
| 1266 | 1× | } |
| 1267 | 1× | if cur.get(k) != a { |
| 1268 | 1× | clashes.push(name(k)); |
| 1269 | 1× | continue; |
| 1270 | 0× | } |
| 1271 | 0× | match b { |
| 1272 | 0× | Some(v) => cur.insert(k.clone(), v.clone()), |
| 1273 | 0× | None => cur.remove(k), |
| 1274 | | }; |
| 1275 | | } |
| 1276 | 10× | } |
| 1277 | 5× | let mut out = current.clone(); |
| 1278 | 5× | let mut clashes = Vec::new(); |
| 1279 | | |
| 1280 | | |
| 1281 | | |
| 1282 | | |
| 1283 | 5× | let names: BTreeSet<&String> = before |
| 1284 | 5× | .bookmarks |
| 1285 | 5× | .keys() |
| 1286 | 5× | .chain(after.bookmarks.keys()) |
| 1287 | 5× | .collect(); |
| 1288 | 8× | for name in names { |
| 1289 | 8× | let (b, a) = (before.bookmarks.get(name), after.bookmarks.get(name)); |
| 1290 | 8× | let cur = current.bookmarks.get(name); |
| 1291 | 8× | if a == b { |
| 1292 | 4× | continue; |
| 1293 | 4× | } |
| 1294 | 4× | if cur == a { |
| 1295 | 2× | match b { |
| 1296 | 2× | Some(st) => out.bookmarks.insert(name.clone(), *st), |
| 1297 | 0× | None => out.bookmarks.remove(name), |
| 1298 | | }; |
| 1299 | 2× | continue; |
| 1300 | 2× | } |
| 1301 | 2× | let (Some(b), Some(a), Some(cur)) = (b, a, cur) else { |
| 1302 | 0× | clashes.push(format!("bookmark {name}")); |
| 1303 | 0× | continue; |
| 1304 | | }; |
| 1305 | 2× | let was: BTreeSet<ChangeHash> = self.store.get_state(b)?.heads.into_iter().collect(); |
| 1306 | 2× | let became: BTreeSet<ChangeHash> = self.store.get_state(a)?.heads.into_iter().collect(); |
| 1307 | 2× | let now: BTreeSet<ChangeHash> = self.store.get_state(cur)?.heads.into_iter().collect(); |
| 1308 | 2× | let added: BTreeSet<&ChangeHash> = became.difference(&was).collect(); |
| 1309 | 2× | if !added.iter().all(|h| now.contains(h)) { |
| 1310 | 1× | clashes.push(format!( |
| 1311 | | "bookmark {name} (a later change builds on this one)" |
| 1312 | | )); |
| 1313 | 1× | continue; |
| 1314 | 1× | } |
| 1315 | 1× | let mut heads: Vec<ChangeHash> = now |
| 1316 | 1× | .iter() |
| 1317 | 3× | .filter(|h| !added.contains(h)) |
| 1318 | 1× | .chain(was.difference(&became)) |
| 1319 | 1× | .copied() |
| 1320 | 1× | .collect(); |
| 1321 | 1× | heads.sort(); |
| 1322 | 1× | heads.dedup(); |
| 1323 | 1× | let heads = self.load_state(&heads)?.maximal(&heads); |
| 1324 | 1× | out.bookmarks.insert( |
| 1325 | 1× | name.clone(), |
| 1326 | 1× | self.store.put_state(&StateObject::new(heads))?, |
| 1327 | | ); |
| 1328 | | } |
| 1329 | |
|
| 1330 | 5× | back( |
| 1331 | 5× | &mut out.tags, |
| 1332 | 5× | &before.tags, |
| 1333 | 5× | &after.tags, |
| 1334 | 0× | |k| format!("tag {k}"), |
| 1335 | 5× | &mut clashes, |
| 1336 | | ); |
| 1337 | 5× | let mine = self.workspace; |
| 1338 | 15× | let others = |v: &View| -> BTreeMap<WorkspaceId, ChangeHash> { |
| 1339 | 15× | v.workspaces |
| 1340 | 15× | .iter() |
| 1341 | 29× | .filter(|(k, _)| **k != mine) |
| 1342 | 15× | .map(|(k, v)| (*k, *v)) |
| 1343 | 15× | .collect() |
| 1344 | 15× | }; |
| 1345 | 5× | let mut theirs = others(&out); |
| 1346 | 5× | let workspace_name = |id: &WorkspaceId| match self.workspace_path(id) { |
| 1347 | 1× | Some(p) => format!("the workspace at {}", p.display()), |
| 1348 | 0× | None => format!("workspace {}", id.to_hex()), |
| 1349 | 1× | }; |
| 1350 | 5× | back( |
| 1351 | 5× | &mut theirs, |
| 1352 | 5× | &others(before), |
| 1353 | 5× | &others(after), |
| 1354 | 5× | workspace_name, |
| 1355 | 5× | &mut clashes, |
| 1356 | | ); |
| 1357 | 5× | let empty = BTreeMap::new(); |
| 1358 | 5× | let names: BTreeSet<&String> = before.remotes.keys().chain(after.remotes.keys()).collect(); |
| 1359 | 5× | for n in names { |
| 1360 | 0× | let mut cur = out.remotes.get(n).cloned().unwrap_or_default(); |
| 1361 | 0× | back( |
| 1362 | 0× | &mut cur, |
| 1363 | 0× | before.remotes.get(n).unwrap_or(&empty), |
| 1364 | 0× | after.remotes.get(n).unwrap_or(&empty), |
| 1365 | 0× | |k| format!("{n}/{k}"), |
| 1366 | 0× | &mut clashes, |
| 1367 | | ); |
| 1368 | 0× | if cur.is_empty() { |
| 1369 | 0× | out.remotes.remove(n); |
| 1370 | 0× | } else { |
| 1371 | 0× | out.remotes.insert(n.clone(), cur); |
| 1372 | 0× | } |
| 1373 | | } |
| 1374 | 5× | if before.policy != after.policy { |
| 1375 | 0× | if out.policy == after.policy { |
| 1376 | 0× | out.policy = before.policy; |
| 1377 | 0× | } else { |
| 1378 | 0× | clashes.push("the policy".into()); |
| 1379 | 0× | } |
| 1380 | 5× | } |
| 1381 | 5× | if !clashes.is_empty() { |
| 1382 | 2× | return fail(format!( |
| 1383 | | "undo would also take back what another workspace has done since to {}; \ |
| 1384 | | see `oxy op log`, and restore a whole earlier state with `oxy op restore OP` if that is what you want", |
| 1385 | 2× | clashes.join(", ") |
| 1386 | | )); |
| 1387 | 3× | } |
| 1388 | 3× | out.workspaces = theirs; |
| 1389 | 3× | match before.workspaces.get(&mine) { |
| 1390 | 3× | Some(wc) => out.workspaces.insert(mine, *wc), |
| 1391 | 0× | None => out.workspaces.remove(&mine), |
| 1392 | | }; |
| 1393 | 3× | Ok(out) |
| 1394 | 5× | } |
| 1395 | |
|
| 1396 | | |
| 1397 | | |
| 1398 | 15× | fn pushed_by(&self, before: &View, after: &View, now: &View) -> Result<Vec<(String, String)>> { |
| 1399 | 15× | let mut out = Vec::new(); |
| 1400 | 18× | for (name, st) in &after.bookmarks { |
| 1401 | 18× | if before.bookmarks.get(name) == Some(st) { |
| 1402 | 4× | continue; |
| 1403 | 14× | } |
| 1404 | 14× | let had: BTreeSet<ChangeHash> = match before.bookmarks.get(name) { |
| 1405 | 14× | Some(b) => self.store.get_state(b)?.heads.into_iter().collect(), |
| 1406 | 0× | None => BTreeSet::new(), |
| 1407 | | }; |
| 1408 | 14× | let added: Vec<ChangeHash> = self |
| 1409 | 14× | .store |
| 1410 | 14× | .get_state(st)? |
| 1411 | | .heads |
| 1412 | 14× | .into_iter() |
| 1413 | 20× | .filter(|h| !had.contains(h)) |
| 1414 | 14× | .collect(); |
| 1415 | 14× | if added.is_empty() { |
| 1416 | 1× | continue; |
| 1417 | 13× | } |
| 1418 | 13× | for (remote, bookmarks) in &now.remotes { |
| 1419 | 1× | if let Some(rs) = bookmarks.get(name) { |
| 1420 | 1× | let theirs = self.store.get_state(rs)?.heads; |
| 1421 | 1× | if added.iter().any(|h| theirs.contains(h)) { |
| 1422 | 1× | out.push((name.clone(), remote.clone())); |
| 1423 | 1× | } |
| 1424 | 0× | } |
| 1425 | | } |
| 1426 | | } |
| 1427 | 15× | Ok(out) |
| 1428 | 15× | } |
| 1429 | |
|
| 1430 | | |
| 1431 | 2× | pub fn last_command(&self) -> Result<Op> { |
| 1432 | 2× | let (_, mut op) = self.head_op()?; |
| 1433 | 4× | while op.is_snapshot() { |
| 1434 | 2× | let Some(parent) = op.parents.first() else { |
| 1435 | 0× | break; |
| 1436 | | }; |
| 1437 | 2× | op = Op::decode(&self.store.get_typed(parent, ObjectType::Op)?)?; |
| 1438 | | } |
| 1439 | 2× | Ok(op) |
| 1440 | 2× | } |
| 1441 | |
|
| 1442 | | |
| 1443 | |
|
| 1444 | | |
| 1445 | 210× | pub fn load_state(&self, heads: &[ChangeHash]) -> Result<State> { |
| 1446 | 210× | if let Some(st) = self.load_state_checkpointed(heads)? { |
| 1447 | 4× | return Ok(st); |
| 1448 | 205× | } |
| 1449 | 205× | self.load_state_full(heads) |
| 1450 | 210× | } |
| 1451 | |
|
| 1452 | | |
| 1453 | | |
| 1454 | | |
| 1455 | | |
| 1456 | 2047× | pub fn load_state_history(&self, heads: &[ChangeHash]) -> Result<State> { |
| 1457 | 2047× | if let Some(st) = self.load_state_checkpointed_as(heads, true)? { |
| 1458 | 44× | return Ok(st); |
| 1459 | 2003× | } |
| 1460 | 2003× | self.load_state_replayed(heads, State::new_history_only()) |
| 1461 | 2047× | } |
| 1462 | |
|
| 1463 | | |
| 1464 | 2124× | pub fn load_graphs_of(&self, state: &mut State, nodes: &[crate::ids::NodeId]) -> Result<()> { |
| 1465 | 2124× | state |
| 1466 | 2124× | .load_graphs( |
| 1467 | 2124× | nodes, |
| 1468 | 54× | |refs| { |
| 1469 | 54× | self.read_blobs(refs).map_err(|e| { |
| 1470 | 3× | crate::objects::ObjectError::Schema(format!("graph and text blobs: {e}")) |
| 1471 | 3× | }) |
| 1472 | 54× | }, |
| 1473 | 2282× | |h| self.change_bytes(h), |
| 1474 | | ) |
| 1475 | 2124× | .map_err(|e| self.index_damage(e)) |
| 1476 | 2124× | } |
| 1477 | |
|
| 1478 | | |
| 1479 | | |
| 1480 | | |
| 1481 | 2365× | pub fn tree_view(&self, state: &mut State) -> Result<crate::tree::TreeView> { |
| 1482 | 2365× | let need = state.unify_candidates(); |
| 1483 | 2365× | if !need.is_empty() { |
| 1484 | 67× | self.load_graphs_of(state, &need)?; |
| 1485 | 2298× | } |
| 1486 | 2365× | Ok(state.view()) |
| 1487 | 2365× | } |
| 1488 | |
|
| 1489 | | |
| 1490 | | |
| 1491 | | |
| 1492 | | |
| 1493 | 4× | fn index_damage(&self, e: crate::objects::ObjectError) -> RepoError { |
| 1494 | 4× | let crate::objects::ObjectError::DamagedIndex(why) = &e else { |
| 1495 | 3× | return e.into(); |
| 1496 | | }; |
| 1497 | 2× | for path in self.state_indexes() { |
| 1498 | 2× | let _ = fs::remove_file(path); |
| 1499 | 2× | } |
| 1500 | 1× | RepoError::Msg(format!( |
| 1501 | 1× | "this workspace's state index was damaged ({why}) and has been removed; run the command again to rebuild it" |
| 1502 | 1× | )) |
| 1503 | 4× | } |
| 1504 | |
|
| 1505 | | |
| 1506 | | |
| 1507 | 77× | pub fn build_graphs_detached( |
| 1508 | 77× | &self, |
| 1509 | 77× | state: &State, |
| 1510 | 77× | nodes: &[crate::ids::NodeId], |
| 1511 | 77× | ) -> Result<HashMap<crate::ids::NodeId, crate::graph::FileGraph>> { |
| 1512 | 77× | Ok(state |
| 1513 | 77× | .build_graphs_detached( |
| 1514 | 77× | nodes, |
| 1515 | 0× | |refs| { |
| 1516 | 0× | self.read_blobs(refs).map_err(|e| { |
| 1517 | 0× | crate::objects::ObjectError::Schema(format!("graph and text blobs: {e}")) |
| 1518 | 0× | }) |
| 1519 | 0× | }, |
| 1520 | 83× | |h| self.change_bytes(h), |
| 1521 | | ) |
| 1522 | 77× | .map_err(|e| self.index_damage(e))? |
| 1523 | 77× | .into_iter() |
| 1524 | 77× | .collect()) |
| 1525 | 77× | } |
| 1526 | |
|
| 1527 | | |
| 1528 | | |
| 1529 | 410× | pub fn single_texts(&self, texts: &[crate::objects::TextRef]) -> Result<Vec<Vec<u8>>> { |
| 1530 | | use crate::objects::TextRef; |
| 1531 | 410× | let refs: Vec<ObjectRef> = texts |
| 1532 | 410× | .iter() |
| 1533 | 5555× | .filter_map(|t| match t { |
| 1534 | 1× | TextRef::Blob(r, _, _) => Some(*r), |
| 1535 | 5554× | TextRef::Inline { .. } => None, |
| 1536 | 5555× | }) |
| 1537 | 410× | .collect(); |
| 1538 | 410× | let mut blobs = self.read_blobs(&refs)?.into_iter(); |
| 1539 | 410× | let mut out = Vec::with_capacity(texts.len()); |
| 1540 | 5555× | for t in texts { |
| 1541 | 5555× | let lines = match t { |
| 1542 | 1× | TextRef::Blob(_, count, form) => { |
| 1543 | 1× | let content = blobs |
| 1544 | 1× | .next() |
| 1545 | 1× | .ok_or_else(|| RepoError::Msg("text blobs: wrong count".into()))?; |
| 1546 | 1× | crate::objects::text_lines(&content, *count, *form)? |
| 1547 | | } |
| 1548 | 5554× | TextRef::Inline { change, start, end } => { |
| 1549 | 5554× | let bytes = self.change_bytes(change)?; |
| 1550 | 5554× | let range = bytes.get(*start as usize..*end as usize).ok_or_else(|| { |
| 1551 | 0× | RepoError::Msg("an inline insert lies outside its change".into()) |
| 1552 | 0× | })?; |
| 1553 | 5554× | crate::objects::inline_lines(range)? |
| 1554 | | } |
| 1555 | | }; |
| 1556 | 5555× | out.push(lines.bytes().to_vec()); |
| 1557 | | } |
| 1558 | 410× | Ok(out) |
| 1559 | 410× | } |
| 1560 | |
|
| 1561 | | |
| 1562 | 2293× | pub fn load_all_graphs(&self, state: &mut State) -> Result<()> { |
| 1563 | 2293× | state |
| 1564 | 2293× | .load_all_graphs( |
| 1565 | 16× | |refs| { |
| 1566 | 16× | self.read_blobs(refs).map_err(|e| { |
| 1567 | 0× | crate::objects::ObjectError::Schema(format!("graph and text blobs: {e}")) |
| 1568 | 0× | }) |
| 1569 | 16× | }, |
| 1570 | 4670× | |h| self.change_bytes(h), |
| 1571 | | ) |
| 1572 | 2293× | .map_err(|e| self.index_damage(e)) |
| 1573 | 2293× | } |
| 1574 | |
|
| 1575 | | |
| 1576 | | |
| 1577 | | |
| 1578 | 12589× | fn change_bytes( |
| 1579 | 12589× | &self, |
| 1580 | 12589× | h: &ChangeHash, |
| 1581 | 12589× | ) -> std::result::Result<std::sync::Arc<Vec<u8>>, crate::objects::ObjectError> { |
| 1582 | 12589× | let mut cache = self.last_change.borrow_mut(); |
| 1583 | 21270× | if let Some(i) = cache.iter().position(|(k, _)| k == h) { |
| 1584 | 9900× | let hit = cache.remove(i); |
| 1585 | 9900× | let b = hit.1.clone(); |
| 1586 | 9900× | cache.push(hit); |
| 1587 | 9900× | return Ok(b); |
| 1588 | 2689× | } |
| 1589 | 2689× | let b = std::sync::Arc::new(self.store.get_typed(h, ObjectType::Change).map_err(|e| { |
| 1590 | 0× | crate::objects::ObjectError::Schema(format!("change {}: {e}", h.short(12))) |
| 1591 | 0× | })?); |
| 1592 | 2689× | cache.push((*h, b.clone())); |
| 1593 | | |
| 1594 | 4314× | while cache.len() > CHANGE_CACHE_COUNT |
| 1595 | 2689× | || (cache.len() > 1 |
| 1596 | 7973× | && cache.iter().map(|(_, b)| b.len()).sum::<usize>() > CHANGE_CACHE_BYTES) |
| 1597 | 1625× | { |
| 1598 | 1625× | cache.remove(0); |
| 1599 | 1625× | } |
| 1600 | 2689× | Ok(b) |
| 1601 | 12589× | } |
| 1602 | |
|
| 1603 | | |
| 1604 | 259× | pub fn load_state_full(&self, heads: &[ChangeHash]) -> Result<State> { |
| 1605 | 259× | self.load_state_replayed(heads, State::new()) |
| 1606 | 259× | } |
| 1607 | |
|
| 1608 | 3857× | fn load_state_replayed(&self, heads: &[ChangeHash], mut state: State) -> Result<State> { |
| 1609 | 3857× | let history = state.is_history_only(); |
| 1610 | 3857× | let mut stack: Vec<ChangeHash> = heads.to_vec(); |
| 1611 | 3857× | let mut seen: BTreeSet<ChangeHash> = BTreeSet::new(); |
| 1612 | | { |
| 1613 | 3857× | let _t = crate::timing::phase("load: read and add changes"); |
| 1614 | 3857× | let (mut read, mut add, mut fetch) = ( |
| 1615 | 3857× | std::time::Duration::ZERO, |
| 1616 | 3857× | std::time::Duration::ZERO, |
| 1617 | 3857× | std::time::Duration::ZERO, |
| 1618 | 3857× | ); |
| 1619 | 15462× | while let Some(h) = stack.pop() { |
| 1620 | | |
| 1621 | 11610× | if !seen.insert(h) || state.contains(&h) { |
| 1622 | 2778× | continue; |
| 1623 | 8832× | } |
| 1624 | 8832× | let t0 = std::time::Instant::now(); |
| 1625 | | |
| 1626 | | |
| 1627 | 8832× | let (c, texts) = if history { |
| 1628 | 7312× | let bytes = self.store.get_typed(&h, ObjectType::Change)?; |
| 1629 | 7309× | fetch += t0.elapsed(); |
| 1630 | 7309× | Change::decode_skeleton(&bytes, h) |
| 1631 | 7309× | .map_err(|e| crate::store::StoreError::Corrupt(h, e.to_string()))? |
| 1632 | | } else { |
| 1633 | 1520× | (self.store.get_change(&h)?, Vec::new()) |
| 1634 | | }; |
| 1635 | 8827× | read += t0.elapsed(); |
| 1636 | 8827× | stack.extend(c.deps.iter().copied()); |
| 1637 | 8827× | let t1 = std::time::Instant::now(); |
| 1638 | 8827× | state.add_known(h, c, texts); |
| 1639 | 8827× | add += t1.elapsed(); |
| 1640 | | } |
| 1641 | 3852× | crate::timing::record("load: of which reading", read); |
| 1642 | 3852× | crate::timing::record("load: of which fetching", fetch); |
| 1643 | 3852× | crate::timing::record("load: of which applying", add); |
| 1644 | | } |
| 1645 | 3852× | let _t = crate::timing::phase("load: validate"); |
| 1646 | 3852× | state.validate()?; |
| 1647 | 3852× | Ok(state) |
| 1648 | 3857× | } |
| 1649 | |
|
| 1650 | 2679× | pub fn working_copy(&self) -> Result<(ChangeHash, Change)> { |
| 1651 | 2679× | if self.bare { |
| 1652 | 2× | return fail("this is a bare repository: it has no working copy"); |
| 1653 | 2677× | } |
| 1654 | 2677× | let view = self.view()?; |
| 1655 | 2677× | let h = *view |
| 1656 | 2677× | .workspaces |
| 1657 | 2677× | .get(&self.workspace) |
| 1658 | 2677× | .ok_or_else(|| RepoError::Msg("this workspace is not in the current view".into()))?; |
| 1659 | 2676× | Ok((h, self.store.get_change(&h)?)) |
| 1660 | 2679× | } |
| 1661 | |
|
| 1662 | | |
| 1663 | 2003× | pub fn heads_of(&self, view: &View, bookmark: &str) -> Result<Vec<ChangeHash>> { |
| 1664 | 2003× | match view.bookmarks.get(bookmark) { |
| 1665 | 2003× | Some(st) => Ok(self.store.get_state(st)?.heads), |
| 1666 | 0× | None => Ok(vec![]), |
| 1667 | | } |
| 1668 | 2003× | } |
| 1669 | |
|
| 1670 | 568× | pub fn bookmark_heads(&self) -> Result<Vec<ChangeHash>> { |
| 1671 | 568× | self.heads_of(&self.view()?, &self.config.bookmark) |
| 1672 | 568× | } |
| 1673 | |
|
| 1674 | | |
| 1675 | |
|
| 1676 | 487× | fn blob_for(content: &[u8]) -> Blob { |
| 1677 | 487× | Blob::of(content) |
| 1678 | 487× | } |
| 1679 | |
|
| 1680 | 291× | pub fn blob_ref(content: &[u8]) -> ObjectRef { |
| 1681 | 291× | Self::blob_for(content).hash() |
| 1682 | 291× | } |
| 1683 | |
|
| 1684 | 194× | pub fn store_blob(&self, content: &[u8]) -> Result<ObjectRef> { |
| 1685 | 277× | for c in crate::chunk::chunks(content) { |
| 1686 | 277× | self.store.put_chunk(c)?; |
| 1687 | | } |
| 1688 | 194× | Ok(self |
| 1689 | 194× | .store |
| 1690 | 194× | .put(ObjectType::Blob, &Self::blob_for(content).encode())?) |
| 1691 | 194× | } |
| 1692 | |
|
| 1693 | | |
| 1694 | | |
| 1695 | | |
| 1696 | 484× | pub fn read_blobs(&self, refs: &[ObjectRef]) -> Result<Vec<Vec<u8>>> { |
| 1697 | 484× | let mut blobs: Vec<Option<Blob>> = Vec::with_capacity(refs.len()); |
| 1698 | 484× | let t = crate::timing::phase("blobs: get_many blobs"); |
| 1699 | 484× | let got_all = self.store.get_many(refs); |
| 1700 | 484× | drop(t); |
| 1701 | 484× | for (r, got) in refs.iter().zip(got_all) { |
| 1702 | 80× | blobs.push(match got { |
| 1703 | 75× | Ok((ObjectType::Blob, body)) => Some(Blob::decode(&body)?), |
| 1704 | 0× | Ok((ty, _)) => return fail(format!("{} is a {ty:?}, not a blob", r.short(12))), |
| 1705 | 5× | Err(crate::store::StoreError::NotFound(_)) => None, |
| 1706 | 0× | Err(e) => return Err(e.into()), |
| 1707 | | }); |
| 1708 | | } |
| 1709 | 484× | let chunk_refs: Vec<ObjectRef> = blobs |
| 1710 | 484× | .iter() |
| 1711 | 484× | .flatten() |
| 1712 | 484× | .flat_map(|b| b.chunks.iter().copied()) |
| 1713 | 484× | .collect(); |
| 1714 | 484× | let t = crate::timing::phase("blobs: get_many chunks"); |
| 1715 | 484× | let mut chunks = self.store.get_many(&chunk_refs).into_iter(); |
| 1716 | 484× | drop(t); |
| 1717 | 484× | if crate::timing::enabled() { |
| 1718 | 0× | eprintln!( |
| 1719 | | "timing blobs: {} blobs, {} chunks, {} one by one", |
| 1720 | 0× | refs.len(), |
| 1721 | 0× | chunk_refs.len(), |
| 1722 | 0× | blobs.iter().filter(|b| b.is_none()).count() |
| 1723 | | ); |
| 1724 | 484× | } |
| 1725 | 484× | let mut out = Vec::with_capacity(refs.len()); |
| 1726 | 484× | for (r, blob) in refs.iter().zip(blobs) { |
| 1727 | 80× | match blob { |
| 1728 | 5× | None => out.push(self.read_blob(r)?), |
| 1729 | 75× | Some(b) => { |
| 1730 | 75× | let mut content = Vec::with_capacity(b.length as usize); |
| 1731 | 114× | for c in &b.chunks { |
| 1732 | 114× | match chunks.next() { |
| 1733 | 114× | Some(Ok((ObjectType::Chunk, body))) => content.extend(body), |
| 1734 | 0× | Some(Ok(_)) => return fail(format!("{} is not a chunk", c.short(12))), |
| 1735 | 0× | Some(Err(e)) => return Err(e.into()), |
| 1736 | 0× | None => return fail("chunk list ended early"), |
| 1737 | | } |
| 1738 | | } |
| 1739 | 75× | out.push(content); |
| 1740 | | } |
| 1741 | | } |
| 1742 | | } |
| 1743 | 479× | Ok(out) |
| 1744 | 484× | } |
| 1745 | |
|
| 1746 | | |
| 1747 | | |
| 1748 | 151× | pub fn read_blob(&self, r: &ObjectRef) -> Result<Vec<u8>> { |
| 1749 | 151× | if !self.store.has(r) && self.lazy_remote(r).is_some() { |
| 1750 | 5× | self.fetch_lazy_blob(r)?; |
| 1751 | 146× | } |
| 1752 | 151× | let blob = Blob::decode(&self.store.get_typed(r, ObjectType::Blob)?)?; |
| 1753 | 146× | let mut out = Vec::with_capacity(blob.length as usize); |
| 1754 | 212× | for c in &blob.chunks { |
| 1755 | 212× | out.extend(self.store.get_typed(c, ObjectType::Chunk)?); |
| 1756 | | } |
| 1757 | 146× | Ok(out) |
| 1758 | 151× | } |
| 1759 | |
|
| 1760 | | |
| 1761 | |
|
| 1762 | | |
| 1763 | 2× | pub fn disk_attributes(&self) -> crate::attributes::Attributes { |
| 1764 | | |
| 1765 | 8× | let listing = walk::list(&self.root, &|p: &str| is_attributes(p)).unwrap_or_default(); |
| 1766 | 2× | let files: Vec<(String, Vec<u8>)> = listing |
| 1767 | 2× | .files |
| 1768 | 2× | .into_iter() |
| 1769 | 2× | .filter_map(|(rel, path, _)| Some((rel, fs::read(path).ok()?))) |
| 1770 | 2× | .collect(); |
| 1771 | 2× | crate::attributes::Attributes::from_files( |
| 1772 | 2× | files.iter().map(|(p, c)| (p.as_str(), c.as_slice())), |
| 1773 | | ) |
| 1774 | 2× | } |
| 1775 | |
|
| 1776 | | |
| 1777 | | |
| 1778 | 29× | pub fn untracked_nested(&self) -> Vec<String> { |
| 1779 | 29× | self.untracked_nested.borrow().clone() |
| 1780 | 29× | } |
| 1781 | |
|
| 1782 | | |
| 1783 | | |
| 1784 | 33× | pub fn skipped_large(&self) -> Vec<(String, u64)> { |
| 1785 | 33× | self.skipped_large.borrow().clone() |
| 1786 | 33× | } |
| 1787 | |
|
| 1788 | | |
| 1789 | 3× | pub fn state_attributes(snapshot: &Snapshot) -> crate::attributes::Attributes { |
| 1790 | 6× | crate::attributes::Attributes::from_files(snapshot.files.iter().filter_map(|(p, f)| { |
| 1791 | 6× | match &f.content { |
| 1792 | 6× | FileContent::Text { bytes, .. } |
| 1793 | 6× | if p == ".oxyattributes" || p.ends_with("/.oxyattributes") => |
| 1794 | | { |
| 1795 | 0× | Some((p.as_str(), bytes.as_slice())) |
| 1796 | | } |
| 1797 | 6× | _ => None, |
| 1798 | | } |
| 1799 | 6× | })) |
| 1800 | 3× | } |
| 1801 | |
|
| 1802 | | |
| 1803 | 174× | pub fn store_blob_for(&self, content: &[u8], nocompress: bool) -> Result<ObjectRef> { |
| 1804 | 174× | if !nocompress { |
| 1805 | 172× | return self.store_blob(content); |
| 1806 | 2× | } |
| 1807 | 2× | for c in crate::chunk::chunks(content) { |
| 1808 | 2× | self.store.put_chunk_nocompress(c)?; |
| 1809 | | } |
| 1810 | 2× | Ok(self |
| 1811 | 2× | .store |
| 1812 | 2× | .put(ObjectType::Blob, &Self::blob_for(content).encode())?) |
| 1813 | 174× | } |
| 1814 | |
|
| 1815 | | |
| 1816 | | |
| 1817 | | |
| 1818 | | |
| 1819 | | |
| 1820 | 2810× | fn state_index_path(&self, heads: &[ChangeHash]) -> PathBuf { |
| 1821 | 2810× | let mut heads = heads.to_vec(); |
| 1822 | 2810× | heads.sort(); |
| 1823 | 2810× | let mut h = blake3::Hasher::new(); |
| 1824 | 2810× | for c in &heads { |
| 1825 | 2764× | h.update(&c.0); |
| 1826 | 2764× | } |
| 1827 | 2810× | self.oxy.join("index").join(format!( |
| 1828 | 2810× | "{}{}", |
| 1829 | 2810× | self.state_index_prefix(), |
| 1830 | 2810× | &h.finalize().to_hex()[..16] |
| 1831 | 2810× | )) |
| 1832 | 2810× | } |
| 1833 | |
|
| 1834 | 5519× | fn state_index_prefix(&self) -> String { |
| 1835 | 5519× | format!("state-{}-", self.workspace.to_hex()) |
| 1836 | 5519× | } |
| 1837 | |
|
| 1838 | | |
| 1839 | 2709× | fn state_indexes(&self) -> Vec<PathBuf> { |
| 1840 | 2709× | let prefix = self.state_index_prefix(); |
| 1841 | 2709× | let mut found: Vec<(std::time::SystemTime, PathBuf)> = fs::read_dir(self.oxy.join("index")) |
| 1842 | 2709× | .into_iter() |
| 1843 | 2709× | .flatten() |
| 1844 | 2709× | .flatten() |
| 1845 | 2709× | .filter(|e| e.file_name().to_string_lossy().starts_with(&prefix)) |
| 1846 | 2709× | .filter_map(|e| Some((e.metadata().ok()?.modified().ok()?, e.path()))) |
| 1847 | 2709× | .collect(); |
| 1848 | 2709× | found.sort_by_key(|f| std::cmp::Reverse(f.0)); |
| 1849 | 2709× | found.into_iter().map(|(_, p)| p).collect() |
| 1850 | 2709× | } |
| 1851 | |
|
| 1852 | | |
| 1853 | | |
| 1854 | | |
| 1855 | | |
| 1856 | 2342× | pub fn load_state_indexed(&self, deps: &[ChangeHash]) -> Result<State> { |
| 1857 | 2342× | if let Some((st, heads)) = self.read_state_index(deps) { |
| 1858 | 1882× | let mut have = st.maximal(deps); |
| 1859 | 1882× | have.sort(); |
| 1860 | 1882× | if deps.iter().all(|d| st.contains(d)) && have == heads { |
| 1861 | | |
| 1862 | | |
| 1863 | | #[cfg(test)] |
| 1864 | 166× | if let Some(mine) = st.write_index() { |
| 1865 | 162× | assert_eq!( |
| 1866 | 162× | Some(mine), |
| 1867 | 162× | self.load_state_history(deps)?.write_index(), |
| 1868 | | "a state index holds the state history gives" |
| 1869 | | ); |
| 1870 | 4× | } |
| 1871 | 287× | return Ok(st); |
| 1872 | 1595× | } |
| 1873 | | |
| 1874 | | |
| 1875 | | |
| 1876 | | |
| 1877 | 1595× | let (indexed, started) = (st.len(), std::time::Instant::now()); |
| 1878 | 1595× | if let Ok(st) = self.load_state_replayed(deps, st) { |
| 1879 | 1592× | let mut all = deps.to_vec(); |
| 1880 | 1592× | all.extend(heads.iter().copied()); |
| 1881 | 1592× | let (mut within, mut want) = (st.maximal(&all), st.maximal(deps)); |
| 1882 | 1592× | within.sort(); |
| 1883 | 1592× | want.sort(); |
| 1884 | 1592× | if within == want { |
| 1885 | | #[cfg(test)] |
| 1886 | 905× | self.assert_same_state(&st, &self.load_state_history(deps)?); |
| 1887 | | |
| 1888 | | |
| 1889 | | |
| 1890 | 1591× | if st.len() - indexed > INDEX_REPLAY_LIMIT |
| 1891 | 1591× | || started.elapsed() > INDEX_REPLAY_TIME |
| 1892 | 3× | { |
| 1893 | 3× | self.write_state_index(deps, &st); |
| 1894 | 1588× | } |
| 1895 | 1591× | return Ok(st); |
| 1896 | 1× | } |
| 1897 | 3× | } |
| 1898 | 460× | } |
| 1899 | 464× | let st = self.load_state_history(deps)?; |
| 1900 | 464× | self.write_state_index(deps, &st); |
| 1901 | 464× | Ok(st) |
| 1902 | 2342× | } |
| 1903 | |
|
| 1904 | | |
| 1905 | | |
| 1906 | | |
| 1907 | | #[cfg(test)] |
| 1908 | 905× | fn assert_same_state(&self, a: &State, b: &State) { |
| 1909 | 1810× | let ids = |s: &State| s.changes().map(|(h, _)| *h).collect::<BTreeSet<_>>(); |
| 1910 | 905× | assert_eq!(ids(a), ids(b), "the same changes"); |
| 1911 | 1810× | let files = |s: &State| { |
| 1912 | 1810× | let mut s = s.clone(); |
| 1913 | 1810× | self.load_all_graphs(&mut s).unwrap(); |
| 1914 | 1810× | s.materialize().files |
| 1915 | 1810× | }; |
| 1916 | 905× | assert_eq!(files(a), files(b), "the same files"); |
| 1917 | 905× | } |
| 1918 | |
|
| 1919 | | |
| 1920 | | |
| 1921 | | |
| 1922 | | |
| 1923 | | |
| 1924 | | |
| 1925 | 2342× | fn read_state_index(&self, deps: &[ChangeHash]) -> Option<(State, Vec<ChangeHash>)> { |
| 1926 | | use std::io::Read; |
| 1927 | 2342× | let _t = crate::timing::phase("load: state index"); |
| 1928 | 2342× | let exact = self.state_index_path(deps); |
| 1929 | 2342× | let reading = crate::timing::phase("load: of which reading the file"); |
| 1930 | 2342× | let mut f = match fs::File::open(&exact) { |
| 1931 | 287× | Ok(f) => { |
| 1932 | | |
| 1933 | 287× | let _ = fs::File::options() |
| 1934 | 287× | .write(true) |
| 1935 | 287× | .open(&exact) |
| 1936 | 287× | .and_then(|f| f.set_modified(std::time::SystemTime::now())); |
| 1937 | 287× | f |
| 1938 | | } |
| 1939 | 2055× | Err(_) => fs::File::open(self.state_indexes().first()?).ok()?, |
| 1940 | | }; |
| 1941 | 1882× | let total = f.metadata().ok()?.len(); |
| 1942 | 1882× | let mut word = [0u8; 4]; |
| 1943 | 1882× | f.read_exact(&mut word).ok()?; |
| 1944 | 1882× | let n = u32::from_le_bytes(word) as usize; |
| 1945 | 1882× | let mut head = vec![0u8; 32 * n + 64 + 8]; |
| 1946 | 1882× | f.read_exact(&mut head).ok()?; |
| 1947 | 1882× | let heads: Vec<ChangeHash> = head[..32 * n] |
| 1948 | 1882× | .chunks_exact(32) |
| 1949 | 1882× | .map(|c| ObjectRef(c.try_into().unwrap())) |
| 1950 | 1882× | .collect(); |
| 1951 | 1882× | let (changes_sum, rest_sum) = (&head[32 * n..32 * n + 32], &head[32 * n + 32..32 * n + 64]); |
| 1952 | 1882× | let front_len = u64::from_le_bytes(head[32 * n + 64..].try_into().unwrap()); |
| 1953 | 1882× | let at = (4 + head.len()) as u64; |
| 1954 | 1882× | if at.checked_add(front_len).is_none_or(|end| end > total) { |
| 1955 | 0× | return None; |
| 1956 | 1882× | } |
| 1957 | | |
| 1958 | | |
| 1959 | 1882× | let mut front = vec![0u8; front_len as usize]; |
| 1960 | 1882× | f.read_exact(&mut front).ok()?; |
| 1961 | 1882× | drop(reading); |
| 1962 | 1882× | let checking = crate::timing::phase("load: of which checking the file"); |
| 1963 | 1882× | let split = 4 + u32::from_le_bytes(front.get(..4)?.try_into().unwrap()) as usize; |
| 1964 | 1882× | let (changes, rest) = (front.get(..split)?, front.get(split..)?); |
| 1965 | 3764× | let sum = |b: &[u8]| *blake3::Hasher::new().update_rayon(b).finalize().as_bytes(); |
| 1966 | 1882× | if sum(changes) != changes_sum || sum(rest) != rest_sum { |
| 1967 | 0× | return None; |
| 1968 | 1882× | } |
| 1969 | 1882× | drop(checking); |
| 1970 | 1882× | let histories = crate::state::Histories::File( |
| 1971 | 1882× | std::sync::Arc::new(f), |
| 1972 | 1882× | at + front_len, |
| 1973 | 1882× | total - at - front_len, |
| 1974 | 1882× | ); |
| 1975 | 1882× | Some((State::read_index_front(&front, histories).ok()?, heads)) |
| 1976 | 2342× | } |
| 1977 | |
|
| 1978 | | |
| 1979 | | |
| 1980 | | #[allow(clippy::type_complexity)] |
| 1981 | 184× | pub(crate) fn read_state_index_changes( |
| 1982 | 184× | &self, |
| 1983 | 184× | ) -> Option<(Vec<ChangeHash>, Vec<(ChangeHash, crate::state::ChangeMeta)>)> { |
| 1984 | | use std::io::Read; |
| 1985 | 184× | let _t = crate::timing::phase("load: state index changes"); |
| 1986 | 184× | let mut f = fs::File::open(self.state_indexes().first()?).ok()?; |
| 1987 | 167× | let mut word = [0u8; 4]; |
| 1988 | 167× | f.read_exact(&mut word).ok()?; |
| 1989 | 167× | let n = u32::from_le_bytes(word) as usize; |
| 1990 | | |
| 1991 | 167× | let mut head = vec![0u8; 32 * n + 64 + 8 + 4]; |
| 1992 | 167× | f.read_exact(&mut head).ok()?; |
| 1993 | 167× | let heads: Vec<ChangeHash> = head[..32 * n] |
| 1994 | 167× | .chunks_exact(32) |
| 1995 | 167× | .map(|c| ObjectRef(c.try_into().unwrap())) |
| 1996 | 167× | .collect(); |
| 1997 | 167× | let sum = &head[32 * n..32 * n + 32]; |
| 1998 | 167× | let len = u32::from_le_bytes(head[32 * n + 72..].try_into().unwrap()) as usize; |
| 1999 | 167× | let mut changes = vec![0u8; 4 + len]; |
| 2000 | 167× | changes[..4].copy_from_slice(&head[32 * n + 72..]); |
| 2001 | 167× | f.read_exact(&mut changes[4..]).ok()?; |
| 2002 | 167× | let checking = crate::timing::phase("load: of which checking"); |
| 2003 | 167× | let mut h = blake3::Hasher::new(); |
| 2004 | 167× | h.update_rayon(&changes); |
| 2005 | 167× | if h.finalize().as_bytes() != sum { |
| 2006 | 0× | return None; |
| 2007 | 167× | } |
| 2008 | 167× | drop(checking); |
| 2009 | 167× | let _decoding = crate::timing::phase("load: of which decoding"); |
| 2010 | 167× | Some((heads, State::read_index_changes_listed(&changes[4..]).ok()?)) |
| 2011 | 184× | } |
| 2012 | |
|
| 2013 | | |
| 2014 | | |
| 2015 | 468× | pub(crate) fn write_state_index(&self, deps: &[ChangeHash], st: &State) { |
| 2016 | 468× | let _t = crate::timing::phase("write: state index"); |
| 2017 | 468× | let encoding = crate::timing::phase("write: of which encoding"); |
| 2018 | 468× | let Some(body) = st.write_index() else { |
| 2019 | 0× | return; |
| 2020 | | }; |
| 2021 | 468× | drop(encoding); |
| 2022 | | #[cfg(test)] |
| 2023 | 272× | assert_eq!( |
| 2024 | 272× | State::read_index(&body).ok().and_then(|s| s.write_index()), |
| 2025 | 272× | Some(body.clone()), |
| 2026 | | "a state index reads back as written" |
| 2027 | | ); |
| 2028 | 468× | let mut heads = st.maximal(deps); |
| 2029 | 468× | heads.sort(); |
| 2030 | 468× | let mut w = crate::flat::Writer::default(); |
| 2031 | 468× | w.count(heads.len()); |
| 2032 | 468× | for h in &heads { |
| 2033 | 10× | w.object(h); |
| 2034 | 10× | } |
| 2035 | | |
| 2036 | | |
| 2037 | | |
| 2038 | 468× | let front_len = u64::from_le_bytes(body[..8].try_into().unwrap()) as usize; |
| 2039 | 468× | let front = &body[8..8 + front_len]; |
| 2040 | 468× | let split = 4 + u32::from_le_bytes(front[..4].try_into().unwrap()) as usize; |
| 2041 | 936× | let sum = |b: &[u8]| *blake3::Hasher::new().update_rayon(b).finalize().as_bytes(); |
| 2042 | 468× | w.fixed(&sum(&front[..split])); |
| 2043 | 468× | w.fixed(&sum(&front[split..])); |
| 2044 | 468× | w.fixed(&body); |
| 2045 | 468× | let path = self.state_index_path(&heads); |
| 2046 | 468× | let _ = fs::create_dir_all(self.oxy.join("index")); |
| 2047 | 468× | let _ = write_cache(&path, &w.0); |
| 2048 | | |
| 2049 | | |
| 2050 | 468× | for old in self.state_indexes().iter().skip(STATE_INDEXES_KEPT) { |
| 2051 | 1× | let _ = fs::remove_file(old); |
| 2052 | 1× | } |
| 2053 | 468× | let _ = fs::remove_file( |
| 2054 | 468× | self.oxy |
| 2055 | 468× | .join("index") |
| 2056 | 468× | .join(format!("state-{}", self.workspace.to_hex())), |
| 2057 | 468× | ); |
| 2058 | 468× | } |
| 2059 | |
|
| 2060 | 6× | fn locks_dir(&self) -> PathBuf { |
| 2061 | 6× | self.oxy.join("locks") |
| 2062 | 6× | } |
| 2063 | |
|
| 2064 | | |
| 2065 | 3× | pub fn is_locked(&self, path: &str) -> bool { |
| 2066 | 3× | self.locks_dir().join(path.replace('/', "%2F")).exists() |
| 2067 | 3× | } |
| 2068 | |
|
| 2069 | | |
| 2070 | 2× | pub fn lock_path(&self, path: &str) -> Result<()> { |
| 2071 | 2× | let attrs = self.disk_attributes(); |
| 2072 | 2× | if !attrs.lookup(path).lockable { |
| 2073 | 1× | return fail(format!("{path} is not marked lockable in .oxyattributes")); |
| 2074 | 1× | } |
| 2075 | 1× | fs::create_dir_all(self.locks_dir())?; |
| 2076 | 1× | write_atomic( |
| 2077 | 1× | &self.locks_dir().join(path.replace('/', "%2F")), |
| 2078 | 1× | std::process::id().to_string().as_bytes(), |
| 2079 | 0× | )?; |
| 2080 | 1× | set_readonly(&self.root.join(path), false)?; |
| 2081 | 1× | Ok(()) |
| 2082 | 2× | } |
| 2083 | |
|
| 2084 | | |
| 2085 | 1× | pub fn unlock_path(&self, path: &str) -> Result<()> { |
| 2086 | 1× | let _ = fs::remove_file(self.locks_dir().join(path.replace('/', "%2F"))); |
| 2087 | 1× | if self.root.join(path).exists() { |
| 2088 | 1× | set_readonly(&self.root.join(path), true)?; |
| 2089 | 0× | } |
| 2090 | 1× | Ok(()) |
| 2091 | 1× | } |
| 2092 | |
|
| 2093 | | |
| 2094 | 24831× | pub fn in_scope(&self, path: &str) -> bool { |
| 2095 | 24831× | self.config.sparse_paths.is_empty() |
| 2096 | 82× | || self |
| 2097 | 82× | .config |
| 2098 | 82× | .sparse_paths |
| 2099 | 82× | .iter() |
| 2100 | 99× | .any(|p| path == p || path.starts_with(&format!("{p}/"))) |
| 2101 | 24831× | } |
| 2102 | |
|
| 2103 | 1× | fn walk_working_copy(&self) -> Result<BTreeMap<String, FsEntry>> { |
| 2104 | 1× | self.walk_working_copy_with(None, &BTreeMap::new(), None) |
| 2105 | 1× | } |
| 2106 | |
|
| 2107 | | |
| 2108 | | |
| 2109 | | |
| 2110 | | |
| 2111 | | |
| 2112 | | |
| 2113 | | |
| 2114 | | |
| 2115 | | |
| 2116 | | |
| 2117 | | |
| 2118 | | |
| 2119 | | |
| 2120 | | |
| 2121 | | |
| 2122 | 1485× | fn walk_working_copy_with( |
| 2123 | 1485× | &self, |
| 2124 | 1485× | cache: Option<&prompt::Manifest>, |
| 2125 | 1485× | tracked: &BTreeMap<String, (NodeKind, Option<ObjectRef>)>, |
| 2126 | 1485× | keys: Option<&KeyOf>, |
| 2127 | 1485× | ) -> Result<BTreeMap<String, FsEntry>> { |
| 2128 | | let mut out: BTreeMap<String, FsEntry>; |
| 2129 | 1485× | let sparse = &self.config.sparse_paths; |
| 2130 | 9357× | let in_scope = |p: &str| { |
| 2131 | 9357× | sparse.is_empty() |
| 2132 | 7× | || sparse |
| 2133 | 7× | .iter() |
| 2134 | 7× | .any(|s| p == s || p.starts_with(&format!("{s}/"))) |
| 2135 | 9357× | }; |
| 2136 | 1485× | let walk_phase = crate::timing::phase("walk: of which listing"); |
| 2137 | | let walk::Listing { |
| 2138 | 1485× | files, |
| 2139 | 1485× | symlinks, |
| 2140 | 1485× | nested, |
| 2141 | 1485× | dirs, |
| 2142 | 1485× | } = match self.listing_cache.take() { |
| 2143 | 205× | Some(l) => l, |
| 2144 | | |
| 2145 | | |
| 2146 | | |
| 2147 | 9369× | None => walk::list_knowing(&self.root, &in_scope, &|p: &str| tracked.contains_key(p))?, |
| 2148 | | }; |
| 2149 | | |
| 2150 | | |
| 2151 | | |
| 2152 | | |
| 2153 | 1485× | let limit = self.config.max_new_file; |
| 2154 | 1485× | let mut skipped = Vec::new(); |
| 2155 | 1485× | let files: Vec<_> = files |
| 2156 | 1485× | .into_iter() |
| 2157 | 12397× | .filter(|(rel, _, meta)| { |
| 2158 | 12397× | let new = !tracked.contains_key(rel) |
| 2159 | 3209× | && !cache.is_some_and(|m| m.entries.contains_key(rel)); |
| 2160 | 12397× | if limit > 0 && new && meta.size > limit { |
| 2161 | 3× | skipped.push((rel.clone(), meta.size)); |
| 2162 | 3× | return false; |
| 2163 | 12394× | } |
| 2164 | 12394× | true |
| 2165 | 12397× | }) |
| 2166 | 1485× | .collect(); |
| 2167 | 1485× | *self.skipped_large.borrow_mut() = skipped; |
| 2168 | 1485× | let keys = keys.filter(|k| { |
| 2169 | 12381× | let on_disk: HashSet<&str> = files.iter().map(|(r, _, _)| r.as_str()).collect(); |
| 2170 | 1481× | let attrs_unchanged = |
| 2171 | 1481× | files |
| 2172 | 1481× | .iter() |
| 2173 | 12362× | .filter(|(r, _, _)| is_attributes(r)) |
| 2174 | 1481× | .all(|(rel, _, meta)| { |
| 2175 | 9× | stat_cached(rel, meta, cache) |
| 2176 | 9× | .is_some_and(|e| e.graph.is_some() && e.graph == k(rel)) |
| 2177 | 9× | }); |
| 2178 | 1481× | let none_removed = tracked |
| 2179 | 1481× | .keys() |
| 2180 | 9295× | .filter(|r| is_attributes(r)) |
| 2181 | 1481× | .all(|r| on_disk.contains(r.as_str())); |
| 2182 | 1481× | attrs_unchanged && none_removed |
| 2183 | 1481× | }); |
| 2184 | 1485× | *self.walked_dirs.borrow_mut() = |
| 2185 | 1485× | Some((dirs, nested.iter().map(|d| self.rel_path(d)).collect())); |
| 2186 | 1485× | drop(walk_phase); |
| 2187 | 1485× | let _read_phase = crate::timing::phase("walk: of which stat and compare"); |
| 2188 | | |
| 2189 | | { |
| 2190 | | use rayon::prelude::*; |
| 2191 | 1485× | let mut read: Vec<(String, FsEntry)> = files |
| 2192 | 1485× | .par_iter() |
| 2193 | 12394× | .map(|(rel, path, meta)| { |
| 2194 | | |
| 2195 | | |
| 2196 | 12394× | let key = keys |
| 2197 | 12394× | .filter(|_| !is_attributes(rel)) |
| 2198 | 12394× | .filter(|_| { |
| 2199 | 12353× | stat_cached(rel, meta, cache).is_some_and(|e| e.graph.is_some()) |
| 2200 | 12353× | }) |
| 2201 | 12394× | .and_then(|k| k(rel)); |
| 2202 | 12394× | Ok((rel.clone(), read_file_entry(rel, path, meta, cache, key)?)) |
| 2203 | 12394× | }) |
| 2204 | 1485× | .collect::<Result<Vec<_>>>()?; |
| 2205 | 12392× | for (rel, e) in read.iter_mut() { |
| 2206 | | |
| 2207 | 12392× | if e.content.is_empty() && e.blob.is_some_and(|b| !self.store.has(&b)) { |
| 2208 | 0× | e.content = std::sync::Arc::new(fs::read(self.root.join(rel.as_str()))?); |
| 2209 | 0× | e.blob = None; |
| 2210 | 12392× | } |
| 2211 | | } |
| 2212 | | |
| 2213 | | |
| 2214 | 1484× | out = read.into_iter().collect(); |
| 2215 | 1484× | for (rel, target) in symlinks { |
| 2216 | 2× | out.insert(rel, FsEntry::new(NodeKind::Symlink, false, target)); |
| 2217 | 2× | } |
| 2218 | | } |
| 2219 | 1484× | drop(_read_phase); |
| 2220 | 1484× | let _rest = crate::timing::phase("walk: of which subrepos and hidden"); |
| 2221 | | |
| 2222 | | |
| 2223 | | |
| 2224 | 1484× | let modules = self.modules_on_disk(); |
| 2225 | 1484× | let removed = self.removed_subrepos(); |
| 2226 | 1484× | for rel in &removed { |
| 2227 | 3× | if tracked.get(rel).map(|(k, _)| *k) != Some(NodeKind::Subrepo) { |
| 2228 | 1× | self.clear_removed_subrepo(rel); |
| 2229 | 2× | } |
| 2230 | | } |
| 2231 | 1484× | let mut untracked_nested = Vec::new(); |
| 2232 | 1484× | for dir in nested { |
| 2233 | 49× | let rel = self.rel_path(&dir); |
| 2234 | 49× | if !self.in_scope(&rel) || removed.contains(&rel) { |
| 2235 | 3× | continue; |
| 2236 | 46× | } |
| 2237 | 46× | let registered = modules.entries.get(&rel); |
| 2238 | 46× | if registered.is_none() && tracked.get(&rel).map(|(k, _)| *k) != Some(NodeKind::Subrepo) |
| 2239 | | { |
| 2240 | 1× | untracked_nested.push(rel); |
| 2241 | 1× | continue; |
| 2242 | 45× | } |
| 2243 | 45× | let pin = subrepos::read_pin(&dir, registered.and_then(|m| m.bookmark.as_deref()))?; |
| 2244 | 45× | out.insert(rel, FsEntry::new(NodeKind::Subrepo, false, pin.encode())); |
| 2245 | | } |
| 2246 | 1484× | untracked_nested.sort(); |
| 2247 | 1484× | *self.untracked_nested.borrow_mut() = untracked_nested; |
| 2248 | | |
| 2249 | 1484× | for (rel, (kind, blob)) in missing_from(tracked, &out) { |
| 2250 | 84× | if !self.in_scope(rel) { |
| 2251 | 10× | continue; |
| 2252 | 74× | } |
| 2253 | 74× | let path = self.root.join(rel); |
| 2254 | 74× | if *kind == NodeKind::Subrepo { |
| 2255 | 3× | if removed.contains(rel) { |
| 2256 | 2× | continue; |
| 2257 | 1× | } |
| 2258 | 1× | let present = |
| 2259 | 1× | path.is_dir() && (path.join(OXY_DIR).exists() || path.join(".git").exists()); |
| 2260 | 1× | if present { |
| 2261 | 0× | let pin = subrepos::read_pin( |
| 2262 | 0× | &path, |
| 2263 | 0× | modules.entries.get(rel).and_then(|m| m.bookmark.as_deref()), |
| 2264 | 0× | )?; |
| 2265 | 0× | out.insert( |
| 2266 | 0× | rel.clone(), |
| 2267 | 0× | FsEntry::new(NodeKind::Subrepo, false, pin.encode()), |
| 2268 | | ); |
| 2269 | 1× | } else if let Some(b) = blob { |
| 2270 | 1× | |
| 2271 | 1× | |
| 2272 | 1× | out.insert( |
| 2273 | 1× | rel.clone(), |
| 2274 | 1× | FsEntry { |
| 2275 | 1× | blob: Some(*b), |
| 2276 | 1× | ..FsEntry::new(NodeKind::Subrepo, false, vec![]) |
| 2277 | 1× | }, |
| 2278 | 1× | ); |
| 2279 | 1× | } |
| 2280 | 1× | continue; |
| 2281 | 71× | } |
| 2282 | 71× | let Ok(meta) = fs::symlink_metadata(&path) else { |
| 2283 | 71× | continue; |
| 2284 | | }; |
| 2285 | 0× | let ft = meta.file_type(); |
| 2286 | 0× | if ft.is_symlink() { |
| 2287 | 0× | let target = fs::read_link(&path)?; |
| 2288 | 0× | out.insert( |
| 2289 | 0× | rel.clone(), |
| 2290 | 0× | FsEntry::new( |
| 2291 | 0× | NodeKind::Symlink, |
| 2292 | | false, |
| 2293 | 0× | target.to_string_lossy().as_bytes().to_vec(), |
| 2294 | | ), |
| 2295 | | ); |
| 2296 | 0× | } else if ft.is_file() { |
| 2297 | 0× | let e = self.file_entry(rel, &path, &walk::Stat::of(&meta), cache)?; |
| 2298 | 0× | out.insert(rel.clone(), e); |
| 2299 | 0× | } |
| 2300 | | } |
| 2301 | 1484× | Ok(out) |
| 2302 | 1485× | } |
| 2303 | |
|
| 2304 | | |
| 2305 | 0× | fn file_entry( |
| 2306 | 0× | &self, |
| 2307 | 0× | rel: &str, |
| 2308 | 0× | path: &Path, |
| 2309 | 0× | meta: &walk::Stat, |
| 2310 | 0× | cache: Option<&prompt::Manifest>, |
| 2311 | 0× | ) -> Result<FsEntry> { |
| 2312 | 0× | let mut e = read_file_entry(rel, path, meta, cache, None)?; |
| 2313 | 0× | if e.content.is_empty() && e.blob.is_some_and(|b| !self.store.has(&b)) { |
| 2314 | 0× | e.content = std::sync::Arc::new(fs::read(path)?); |
| 2315 | 0× | e.blob = None; |
| 2316 | 0× | } |
| 2317 | 0× | Ok(e) |
| 2318 | 0× | } |
| 2319 | |
|
| 2320 | | |
| 2321 | | |
| 2322 | 3× | pub(crate) fn tracked_kinds( |
| 2323 | 3× | snapshot: &Snapshot, |
| 2324 | 3× | ) -> BTreeMap<String, (NodeKind, Option<ObjectRef>)> { |
| 2325 | 3× | snapshot |
| 2326 | 3× | .files |
| 2327 | 3× | .iter() |
| 2328 | 12× | .filter(|(_, f)| f.kind != NodeKind::Dir) |
| 2329 | 8× | .map(|(p, f)| { |
| 2330 | 8× | let blob = match &f.content { |
| 2331 | 4× | FileContent::Opaque { blobs } => blobs.first().copied(), |
| 2332 | 4× | _ => None, |
| 2333 | | }; |
| 2334 | 8× | (p.clone(), (f.kind, blob)) |
| 2335 | 8× | }) |
| 2336 | 3× | .collect() |
| 2337 | 3× | } |
| 2338 | |
|
| 2339 | | |
| 2340 | 106× | pub(crate) fn rel_path(&self, path: &Path) -> String { |
| 2341 | 106× | path.strip_prefix(&self.root) |
| 2342 | 106× | .unwrap_or(path) |
| 2343 | 106× | .components() |
| 2344 | 190× | .map(|c| c.as_os_str().to_string_lossy().to_string()) |
| 2345 | 106× | .collect::<Vec<_>>() |
| 2346 | 106× | .join("/") |
| 2347 | 106× | } |
| 2348 | | } |
| 2349 | |
|
| 2350 | | impl Repo { |
| 2351 | | |
| 2352 | 103× | pub fn log(&self) -> Result<Vec<LogEntry>> { |
| 2353 | 103× | let heads = self.bookmark_heads()?; |
| 2354 | 103× | if self.bare { |
| 2355 | 1× | return self.log_state(&heads); |
| 2356 | 102× | } |
| 2357 | 102× | let (wc_hash, wc) = self.working_copy()?; |
| 2358 | 102× | let mut all = heads.clone(); |
| 2359 | 102× | all.push(wc_hash); |
| 2360 | 102× | let head_set: BTreeSet<ChangeHash> = heads.into_iter().collect(); |
| 2361 | 102× | let mut entries: Vec<LogEntry> = Vec::new(); |
| 2362 | | |
| 2363 | | |
| 2364 | 102× | let metas = if self.is_shallow() { |
| 2365 | 2× | self.load_state_history(&all)? |
| 2366 | 2× | .changes() |
| 2367 | 12× | .map(|(h, m)| (*h, m.clone())) |
| 2368 | 2× | .collect() |
| 2369 | | } else { |
| 2370 | | |
| 2371 | | |
| 2372 | 100× | let mut below: Vec<ChangeHash> = head_set.iter().copied().collect(); |
| 2373 | 100× | below.extend(wc.deps.iter().copied()); |
| 2374 | 100× | let _t = crate::timing::phase("log: metadata"); |
| 2375 | 100× | let mut metas = self.metas_of(&below)?; |
| 2376 | 349× | if !metas.iter().any(|(h, _)| *h == wc_hash) { |
| 2377 | 100× | |
| 2378 | 100× | metas.insert(0, (wc_hash, crate::state::ChangeMeta::of(&wc))); |
| 2379 | 100× | } |
| 2380 | 100× | metas |
| 2381 | | }; |
| 2382 | 102× | entries.extend(metas.into_iter().map(|(h, meta)| LogEntry { |
| 2383 | 461× | hash: h, |
| 2384 | 461× | change: meta, |
| 2385 | 461× | is_working_copy: h == wc_hash, |
| 2386 | 461× | is_head: head_set.contains(&h), |
| 2387 | 461× | })); |
| 2388 | 102× | let _sorting = crate::timing::phase("log: sort"); |
| 2389 | | |
| 2390 | | |
| 2391 | 857× | let order = |a: &LogEntry, b: &LogEntry| { |
| 2392 | 857× | b.is_working_copy |
| 2393 | 857× | .cmp(&a.is_working_copy) |
| 2394 | 857× | .then(crate::state::log_order( |
| 2395 | 857× | (&a.hash, &a.change.hlc), |
| 2396 | 857× | (&b.hash, &b.change.hlc), |
| 2397 | | )) |
| 2398 | 857× | }; |
| 2399 | 223× | if !entries.is_sorted_by(|a, b| order(a, b).is_le()) { |
| 2400 | 49× | entries.sort_unstable_by(order); |
| 2401 | 53× | } |
| 2402 | 102× | Ok(entries) |
| 2403 | 103× | } |
| 2404 | |
|
| 2405 | | |
| 2406 | | |
| 2407 | 0× | pub fn log_window( |
| 2408 | 0× | &self, |
| 2409 | 0× | heads: &[ChangeHash], |
| 2410 | 0× | offset: usize, |
| 2411 | 0× | limit: usize, |
| 2412 | 0× | ) -> Result<(Vec<LogEntry>, usize)> { |
| 2413 | 0× | let all = self.log_state(heads)?; |
| 2414 | 0× | let total = all.len(); |
| 2415 | 0× | Ok((all.into_iter().skip(offset).take(limit).collect(), total)) |
| 2416 | 0× | } |
| 2417 | |
|
| 2418 | | |
| 2419 | | |
| 2420 | 10× | pub fn log_state(&self, heads: &[ChangeHash]) -> Result<Vec<LogEntry>> { |
| 2421 | | |
| 2422 | 10× | if self.is_shallow() { |
| 2423 | 0× | return Ok(self.log_of(&self.load_state_history(heads)?)); |
| 2424 | 10× | } |
| 2425 | 10× | let metas = self.metas_of(heads)?; |
| 2426 | 10× | let depended: BTreeSet<ChangeHash> = metas |
| 2427 | 10× | .iter() |
| 2428 | 30× | .flat_map(|(_, m)| m.deps.iter().copied()) |
| 2429 | 10× | .collect(); |
| 2430 | 10× | let mut entries: Vec<LogEntry> = metas |
| 2431 | 10× | .into_iter() |
| 2432 | 10× | .map(|(h, meta)| LogEntry { |
| 2433 | 30× | hash: h, |
| 2434 | 30× | is_head: !depended.contains(&h), |
| 2435 | 30× | change: meta, |
| 2436 | | is_working_copy: false, |
| 2437 | 30× | }) |
| 2438 | 10× | .collect(); |
| 2439 | 47× | let order = |a: &LogEntry, b: &LogEntry| { |
| 2440 | 47× | crate::state::log_order((&a.hash, &a.change.hlc), (&b.hash, &b.change.hlc)) |
| 2441 | 47× | }; |
| 2442 | 11× | if !entries.is_sorted_by(|a, b| order(a, b).is_le()) { |
| 2443 | 3× | entries.sort_unstable_by(order); |
| 2444 | 7× | } |
| 2445 | 10× | Ok(entries) |
| 2446 | 10× | } |
| 2447 | |
|
| 2448 | | |
| 2449 | | |
| 2450 | | |
| 2451 | 184× | pub fn metas_of( |
| 2452 | 184× | &self, |
| 2453 | 184× | heads: &[ChangeHash], |
| 2454 | 184× | ) -> Result<Vec<(ChangeHash, crate::state::ChangeMeta)>> { |
| 2455 | 184× | let boundary = self.boundary(); |
| 2456 | | |
| 2457 | | |
| 2458 | | |
| 2459 | 184× | let read = self.read_state_index_changes(); |
| 2460 | | |
| 2461 | | |
| 2462 | | |
| 2463 | 184× | if let Some((index_heads, all)) = &read { |
| 2464 | 167× | let asked: crate::fasthash::FastSet<&ChangeHash> = heads.iter().collect(); |
| 2465 | 167× | let in_index = || { |
| 2466 | 167× | let index_heads: crate::fasthash::FastSet<&ChangeHash> = |
| 2467 | 167× | index_heads.iter().collect(); |
| 2468 | | |
| 2469 | 167× | heads.iter().all(|h| index_heads.contains(h)) || { |
| 2470 | 167× | let all: crate::fasthash::FastSet<&ChangeHash> = |
| 2471 | 167× | all.iter().map(|(h, _)| h).collect(); |
| 2472 | 167× | heads.iter().all(|h| all.contains(h)) |
| 2473 | | } |
| 2474 | 167× | }; |
| 2475 | 167× | if index_heads.iter().all(|h| asked.contains(h)) && in_index() { |
| 2476 | 0× | let (_, all) = read.expect("matched above"); |
| 2477 | | #[cfg(test)] |
| 2478 | | { |
| 2479 | | |
| 2480 | 0× | let mut seen: HashSet<ChangeHash> = HashSet::new(); |
| 2481 | 0× | let mut stack = heads.to_vec(); |
| 2482 | 0× | while let Some(h) = stack.pop() { |
| 2483 | 0× | if seen.insert(h) { |
| 2484 | 0× | stack.extend(self.store.get_change_meta(&h)?.deps); |
| 2485 | 0× | } |
| 2486 | | } |
| 2487 | 0× | assert_eq!(seen, all.iter().map(|(h, _)| *h).collect::<HashSet<_>>()); |
| 2488 | | } |
| 2489 | 0× | return Ok(all); |
| 2490 | 167× | } |
| 2491 | 17× | } |
| 2492 | 184× | let mut indexed: Option<crate::fasthash::FastMap<ChangeHash, crate::state::ChangeMeta>> = |
| 2493 | 184× | read.map(|(_, all)| all.into_iter().collect()); |
| 2494 | 184× | let _walk = crate::timing::phase("metadata: walk"); |
| 2495 | 184× | let (mut stored, mut from_store) = (0usize, std::time::Duration::ZERO); |
| 2496 | 184× | let mut seen: crate::fasthash::FastSet<ChangeHash> = Default::default(); |
| 2497 | 184× | let mut stack: Vec<ChangeHash> = heads.to_vec(); |
| 2498 | 184× | let mut out = Vec::with_capacity(indexed.as_ref().map_or(0, |m| m.len())); |
| 2499 | 1330× | while let Some(h) = stack.pop() { |
| 2500 | 1146× | if !seen.insert(h) { |
| 2501 | 483× | continue; |
| 2502 | 663× | } |
| 2503 | | |
| 2504 | 663× | if let Some(meta) = indexed.as_mut().and_then(|m| m.remove(&h)) { |
| 2505 | | #[cfg(test)] |
| 2506 | 0× | assert_eq!( |
| 2507 | | meta, |
| 2508 | 0× | self.store.get_change_meta(&h)?, |
| 2509 | | "the state index holds a change's metadata" |
| 2510 | | ); |
| 2511 | 0× | stack.extend(meta.deps.iter().copied()); |
| 2512 | 0× | out.push((h, meta)); |
| 2513 | 0× | continue; |
| 2514 | 663× | } |
| 2515 | 663× | let started = std::time::Instant::now(); |
| 2516 | 663× | let meta = match self.store.get_change_meta(&h) { |
| 2517 | 663× | Ok(m) => m, |
| 2518 | 0× | Err(crate::store::StoreError::NotFound(_)) if boundary.contains(&h) => continue, |
| 2519 | 0× | Err(e) => return Err(e.into()), |
| 2520 | | }; |
| 2521 | 663× | stored += 1; |
| 2522 | 663× | from_store += started.elapsed(); |
| 2523 | 663× | stack.extend(meta.deps.iter().copied()); |
| 2524 | 663× | out.push((h, meta)); |
| 2525 | | } |
| 2526 | 184× | if stored > 0 && crate::timing::enabled() { |
| 2527 | 0× | eprintln!("timing metadata: {stored} changes read whole, {from_store:.2?}"); |
| 2528 | 184× | } |
| 2529 | 184× | Ok(out) |
| 2530 | 184× | } |
| 2531 | |
|
| 2532 | | |
| 2533 | | |
| 2534 | | |
| 2535 | | |
| 2536 | | |
| 2537 | 6× | pub fn line_history( |
| 2538 | 6× | &self, |
| 2539 | 6× | heads: &[ChangeHash], |
| 2540 | 6× | path: &str, |
| 2541 | 6× | first: usize, |
| 2542 | 6× | last: usize, |
| 2543 | 6× | ) -> Result<Vec<LogEntry>> { |
| 2544 | 6× | if first == 0 || last < first { |
| 2545 | 1× | return fail(format!( |
| 2546 | | "lines {first},{last}: expected FIRST,LAST from 1 with FIRST ≤ LAST" |
| 2547 | | )); |
| 2548 | 5× | } |
| 2549 | 5× | let mut state = self.load_state_history(heads)?; |
| 2550 | 5× | let Some(node) = self |
| 2551 | 5× | .tree_view(&mut state)? |
| 2552 | | .entries |
| 2553 | 5× | .get(path) |
| 2554 | 5× | .map(|e| e.node) |
| 2555 | | else { |
| 2556 | 1× | return fail(format!("{path} is not in that state")); |
| 2557 | | }; |
| 2558 | 4× | self.load_graphs_of(&mut state, &[node])?; |
| 2559 | 4× | let live = state.live_lines(&node); |
| 2560 | 4× | if last > live.len() { |
| 2561 | 1× | return fail(format!( |
| 2562 | | "{path} has {} lines; asked for {first},{last}", |
| 2563 | 1× | live.len() |
| 2564 | | )); |
| 2565 | 3× | } |
| 2566 | 3× | let Some(g) = state.file(&node) else { |
| 2567 | 0× | return fail(format!("{path} is not a text file")); |
| 2568 | | }; |
| 2569 | 3× | let atoms = g.between(&live[first - 1].0, &live[last - 1].0); |
| 2570 | 3× | let touched = g.touching(&atoms); |
| 2571 | 3× | Ok(self |
| 2572 | 3× | .log_of(&state) |
| 2573 | 3× | .into_iter() |
| 2574 | 13× | .filter(|e| touched.contains(&e.hash)) |
| 2575 | 3× | .collect()) |
| 2576 | 6× | } |
| 2577 | |
|
| 2578 | 8× | pub fn log_of(&self, state: &crate::state::State) -> Vec<LogEntry> { |
| 2579 | 8× | let head_set: BTreeSet<ChangeHash> = state.heads().into_iter().collect(); |
| 2580 | 8× | let mut entries: Vec<LogEntry> = state |
| 2581 | 8× | .changes() |
| 2582 | 8× | .map(|(h, meta)| LogEntry { |
| 2583 | 31× | hash: *h, |
| 2584 | 31× | change: meta.clone(), |
| 2585 | | is_working_copy: false, |
| 2586 | 31× | is_head: head_set.contains(h), |
| 2587 | 31× | }) |
| 2588 | 8× | .collect(); |
| 2589 | 30× | entries.sort_unstable_by(|a, b| b.change.hlc.cmp(&a.change.hlc).then(b.hash.cmp(&a.hash))); |
| 2590 | 8× | entries |
| 2591 | 8× | } |
| 2592 | |
|
| 2593 | | |
| 2594 | | |
| 2595 | | |
| 2596 | 67× | pub fn resolve_heads(&self, spec: &str) -> Result<Vec<ChangeHash>> { |
| 2597 | 67× | let view = self.view()?; |
| 2598 | 67× | if let Some(st) = view.bookmarks.get(spec) { |
| 2599 | 39× | return Ok(self.store.get_state(st)?.heads); |
| 2600 | 28× | } |
| 2601 | 28× | if let Some((_, _, st)) = remote_bookmark(&view, spec) { |
| 2602 | 2× | return Ok(self.store.get_state(&st)?.heads); |
| 2603 | 26× | } |
| 2604 | 26× | if let Some(heads) = self.tag_heads(spec)? { |
| 2605 | 2× | return Ok(heads); |
| 2606 | 24× | } |
| 2607 | 24× | Ok(vec![self.resolve_change(spec)?]) |
| 2608 | 67× | } |
| 2609 | |
|
| 2610 | | |
| 2611 | | |
| 2612 | 37× | pub fn resolve_change(&self, spec: &str) -> Result<ChangeHash> { |
| 2613 | 37× | if spec == "@" { |
| 2614 | 0× | return Ok(self.working_copy()?.0); |
| 2615 | 37× | } |
| 2616 | 37× | let view = self.view()?; |
| 2617 | 37× | let named = match view.bookmarks.get(spec) { |
| 2618 | 1× | Some(st) => Some(("bookmark", self.store.get_state(st)?.heads)), |
| 2619 | 36× | None => match remote_bookmark(&view, spec) { |
| 2620 | 1× | Some((_, _, st)) => Some(("bookmark", self.store.get_state(&st)?.heads)), |
| 2621 | 35× | None => self.tag_heads(spec)?.map(|h| ("tag", h)), |
| 2622 | | }, |
| 2623 | | }; |
| 2624 | 37× | if let Some((kind, heads)) = named { |
| 2625 | 3× | return match heads.len() { |
| 2626 | 3× | 1 => Ok(heads[0]), |
| 2627 | 0× | n => fail(format!("{kind} {spec} has {n} heads; name a change, or use it where a state is expected")), |
| 2628 | | }; |
| 2629 | 34× | } |
| 2630 | 34× | if spec.is_empty() || spec == "#" { |
| 2631 | 0× | return fail("empty change specifier"); |
| 2632 | 34× | } |
| 2633 | 34× | let entries = self.log()?; |
| 2634 | | |
| 2635 | | |
| 2636 | 34× | let full_hash = |
| 2637 | 128× | (spec.len() == 64 && spec.bytes().all(|b| b.is_ascii_hexdigit())).then_some(spec); |
| 2638 | 34× | let matches: Vec<&LogEntry> = if let Some(hp) = spec.strip_prefix('#').or(full_hash) { |
| 2639 | 28× | entries |
| 2640 | 28× | .iter() |
| 2641 | 112× | .filter(|e| e.hash.starts_with_hex(hp)) |
| 2642 | 28× | .collect() |
| 2643 | | } else { |
| 2644 | 6× | entries |
| 2645 | 6× | .iter() |
| 2646 | 21× | .filter(|e| e.change.change_id.starts_with_hex(spec)) |
| 2647 | 6× | .collect() |
| 2648 | | }; |
| 2649 | 34× | match matches.len() { |
| 2650 | 29× | 1 => Ok(matches[0].hash), |
| 2651 | 5× | 0 => match self.resolve_in_remotes(spec)? { |
| 2652 | 1× | Some(h) => Ok(h), |
| 2653 | 4× | None => fail(format!("no change matches {spec:?}")), |
| 2654 | | }, |
| 2655 | | _ => { |
| 2656 | 0× | let ids: BTreeSet<ChangeId> = matches.iter().map(|e| e.change.change_id).collect(); |
| 2657 | 0× | if ids.len() == 1 { |
| 2658 | 0× | fail(format!( |
| 2659 | | "change {spec} is divergent: {} variants", |
| 2660 | 0× | matches.len() |
| 2661 | | )) |
| 2662 | | } else { |
| 2663 | 0× | fail(format!("{spec:?} is ambiguous")) |
| 2664 | | } |
| 2665 | | } |
| 2666 | | } |
| 2667 | 37× | } |
| 2668 | |
|
| 2669 | | |
| 2670 | | |
| 2671 | | |
| 2672 | 5× | fn resolve_in_remotes(&self, spec: &str) -> Result<Option<ChangeHash>> { |
| 2673 | 5× | let view = self.view()?; |
| 2674 | 5× | let mut found: BTreeSet<ChangeHash> = BTreeSet::new(); |
| 2675 | 5× | for bookmarks in view.remotes.values() { |
| 2676 | 3× | for st in bookmarks.values() { |
| 2677 | 3× | let heads = self.store.get_state(st)?.heads; |
| 2678 | 7× | for e in self.log_state(&heads)? { |
| 2679 | 7× | let hit = match spec.strip_prefix('#') { |
| 2680 | 2× | Some(hp) => e.hash.starts_with_hex(hp), |
| 2681 | 5× | None => e.change.change_id.starts_with_hex(spec), |
| 2682 | | }; |
| 2683 | 7× | if hit { |
| 2684 | 1× | found.insert(e.hash); |
| 2685 | 6× | } |
| 2686 | | } |
| 2687 | | } |
| 2688 | | } |
| 2689 | 5× | Ok((found.len() == 1).then(|| *found.first().unwrap())) |
| 2690 | 5× | } |
| 2691 | |
|
| 2692 | | |
| 2693 | | |
| 2694 | | |
| 2695 | 4× | pub fn remote_only_location(&self, hash: &ChangeHash) -> Result<Option<String>> { |
| 2696 | 8× | if self.log()?.iter().any(|e| e.hash == *hash) { |
| 2697 | 3× | return Ok(None); |
| 2698 | 1× | } |
| 2699 | 1× | let view = self.view()?; |
| 2700 | 1× | for (remote, bookmarks) in &view.remotes { |
| 2701 | 1× | for (name, st) in bookmarks { |
| 2702 | 1× | let heads = self.store.get_state(st)?.heads; |
| 2703 | 1× | if self.log_state(&heads)?.iter().any(|e| e.hash == *hash) { |
| 2704 | 1× | return Ok(Some(format!("{remote}/{name}"))); |
| 2705 | 0× | } |
| 2706 | | } |
| 2707 | | } |
| 2708 | 0× | Ok(None) |
| 2709 | 4× | } |
| 2710 | |
|
| 2711 | | |
| 2712 | 4× | pub fn describe_change(&self, hash: &ChangeHash) -> Result<String> { |
| 2713 | 4× | let change = self.store.get_change(hash)?; |
| 2714 | 4× | let (base, base_snapshot) = self.change_base(&change)?; |
| 2715 | 4× | Ok(render_change(&change, &base, &base_snapshot)) |
| 2716 | 4× | } |
| 2717 | |
|
| 2718 | | |
| 2719 | | |
| 2720 | | |
| 2721 | | |
| 2722 | 8× | pub fn change_base(&self, change: &Change) -> Result<(State, Snapshot)> { |
| 2723 | 8× | let base = self.load_state_history(&change.deps)?; |
| 2724 | 8× | self.render_base_for(change, base) |
| 2725 | 8× | } |
| 2726 | |
|
| 2727 | | |
| 2728 | | |
| 2729 | | |
| 2730 | | |
| 2731 | 3× | pub(crate) fn tree_base_for( |
| 2732 | 3× | &self, |
| 2733 | 3× | change: &Change, |
| 2734 | 3× | mut base: State, |
| 2735 | 3× | ) -> Result<(State, Snapshot)> { |
| 2736 | 3× | let _t = crate::timing::phase("diff: the base's tree"); |
| 2737 | 3× | let edited: std::collections::HashSet<NodeId> = |
| 2738 | 8× | change.edits.iter().map(|e| e.node()).collect(); |
| 2739 | 3× | let nodes: Vec<NodeId> = edited.into_iter().collect(); |
| 2740 | 3× | self.load_graphs_of(&mut base, &nodes)?; |
| 2741 | 3× | let snapshot = Snapshot { |
| 2742 | 3× | tree: self.tree_view(&mut base)?, |
| 2743 | 3× | files: BTreeMap::new(), |
| 2744 | 3× | unrendered: BTreeSet::new(), |
| 2745 | | }; |
| 2746 | 3× | Ok((base, snapshot)) |
| 2747 | 3× | } |
| 2748 | |
|
| 2749 | | |
| 2750 | | |
| 2751 | 238× | pub(crate) fn render_base_for( |
| 2752 | 238× | &self, |
| 2753 | 238× | change: &Change, |
| 2754 | 238× | mut base: State, |
| 2755 | 238× | ) -> Result<(State, Snapshot)> { |
| 2756 | 238× | let _t = crate::timing::phase("diff: render the base"); |
| 2757 | 238× | let edited: std::collections::HashSet<NodeId> = |
| 2758 | 436× | change.edits.iter().map(|e| e.node()).collect(); |
| 2759 | 238× | let nodes: Vec<NodeId> = edited.iter().copied().collect(); |
| 2760 | 238× | self.load_graphs_of(&mut base, &nodes)?; |
| 2761 | 238× | self.tree_view(&mut base)?; |
| 2762 | 238× | let snapshot = { |
| 2763 | 238× | let _t = crate::timing::phase("diff: of which materialize"); |
| 2764 | 238× | base.materialize_nodes(&edited) |
| 2765 | | }; |
| 2766 | 238× | Ok((base, snapshot)) |
| 2767 | 238× | } |
| 2768 | | } |
| 2769 | |
|
| 2770 | 0× | fn remove_any(path: &Path) -> std::io::Result<()> { |
| 2771 | 0× | let m = fs::symlink_metadata(path)?; |
| 2772 | 0× | if m.is_dir() { |
| 2773 | 0× | fs::remove_dir_all(path) |
| 2774 | | } else { |
| 2775 | 0× | fs::remove_file(path) |
| 2776 | | } |
| 2777 | 0× | } |
| 2778 | |
|
| 2779 | 5× | fn set_readonly(path: &Path, readonly: bool) -> std::io::Result<()> { |
| 2780 | 5× | let mut perms = fs::metadata(path)?.permissions(); |
| 2781 | 5× | perms.set_readonly(readonly); |
| 2782 | 5× | fs::set_permissions(path, perms) |
| 2783 | 5× | } |
| 2784 | |
|
| 2785 | | #[cfg(unix)] |
| 2786 | 5900× | fn set_executable(path: &Path, exec: bool) -> std::io::Result<()> { |
| 2787 | | use std::os::unix::fs::PermissionsExt; |
| 2788 | 5900× | let mut perms = fs::metadata(path)?.permissions(); |
| 2789 | 5900× | let mode = perms.mode(); |
| 2790 | 5900× | let want = if exec { mode | 0o111 } else { mode & !0o111 }; |
| 2791 | 5900× | if want != mode { |
| 2792 | 6× | perms.set_mode(want); |
| 2793 | 6× | fs::set_permissions(path, perms)?; |
| 2794 | 5894× | } |
| 2795 | 5900× | Ok(()) |
| 2796 | 5900× | } |
| 2797 | | #[cfg(not(unix))] |
| 2798 | | fn set_executable(_path: &Path, _exec: bool) -> std::io::Result<()> { |
| 2799 | | Ok(()) |
| 2800 | | } |
| 2801 | |
|
| 2802 | | #[cfg(unix)] |
| 2803 | 16793× | pub(crate) fn is_executable(m: &fs::Metadata) -> bool { |
| 2804 | | use std::os::unix::fs::PermissionsExt; |
| 2805 | 16793× | m.permissions().mode() & 0o111 != 0 |
| 2806 | 16793× | } |
| 2807 | | #[cfg(not(unix))] |
| 2808 | | pub(crate) fn is_executable(_m: &fs::Metadata) -> bool { |
| 2809 | | false |
| 2810 | | } |
| 2811 | |
|
| 2812 | 13193× | fn op_ref(k: OpKey) -> OpRef { |
| 2813 | 13193× | OpRef { |
| 2814 | 13193× | change: k.0, |
| 2815 | 13193× | index: k.1, |
| 2816 | 13193× | } |
| 2817 | 13193× | } |
| 2818 | |
|
| 2819 | 6213× | fn split_path(path: &str) -> (&str, &str) { |
| 2820 | 6213× | match path.rfind('/') { |
| 2821 | 5061× | Some(i) => (&path[..i], &path[i + 1..]), |
| 2822 | 1152× | None => ("", path), |
| 2823 | | } |
| 2824 | 6213× | } |
| 2825 | |
|
| 2826 | |
|
| 2827 | 68664× | fn is_attributes(rel: &str) -> bool { |
| 2828 | 68664× | rel == ".oxyattributes" || rel.ends_with("/.oxyattributes") |
| 2829 | 68664× | } |
| 2830 | |
|
| 2831 | |
|
| 2832 | |
|
| 2833 | 24756× | fn stat_cached<'m>( |
| 2834 | 24756× | rel: &str, |
| 2835 | 24756× | meta: &walk::Stat, |
| 2836 | 24756× | cache: Option<&'m prompt::Manifest>, |
| 2837 | 24756× | ) -> Option<&'m prompt::ManifestEntry> { |
| 2838 | 24756× | let m = cache?; |
| 2839 | 19735× | let e = m.entries.get(rel)?; |
| 2840 | 18294× | let racy = meta.mtime_ns + 2_000_000_000 >= m.written_ns; |
| 2841 | 18294× | (e.size == meta.size && e.mtime_ns == meta.mtime_ns && !racy).then_some(e) |
| 2842 | 24756× | } |
| 2843 | |
|
| 2844 | |
|
| 2845 | |
|
| 2846 | |
|
| 2847 | |
|
| 2848 | |
|
| 2849 | |
|
| 2850 | |
|
| 2851 | |
|
| 2852 | 12394× | fn read_file_entry( |
| 2853 | 12394× | rel: &str, |
| 2854 | 12394× | path: &Path, |
| 2855 | 12394× | meta: &walk::Stat, |
| 2856 | 12394× | cache: Option<&prompt::Manifest>, |
| 2857 | 12394× | key: Option<[u8; 32]>, |
| 2858 | 12394× | ) -> Result<FsEntry> { |
| 2859 | 12394× | let exec = meta.exec; |
| 2860 | 12394× | let stat = Some((meta.size, meta.mtime_ns)); |
| 2861 | 12394× | let mut e = match stat_cached(rel, meta, cache) { |
| 2862 | 4247× | Some(e) if e.blob.is_some() => FsEntry { |
| 2863 | 1× | blob: e.blob, |
| 2864 | 1× | content_hash: Some(e.hash), |
| 2865 | 1× | ..FsEntry::new(NodeKind::File, exec, vec![]) |
| 2866 | 1× | }, |
| 2867 | 4246× | Some(e) if key.is_some() && e.graph == key => FsEntry { |
| 2868 | 4241× | content_hash: Some(e.hash), |
| 2869 | 4241× | unchanged: true, |
| 2870 | 4241× | graph_key: e.graph, |
| 2871 | 4241× | ..FsEntry::new(NodeKind::File, exec, vec![]) |
| 2872 | 4241× | }, |
| 2873 | 5× | Some(e) => FsEntry { |
| 2874 | 5× | content_hash: Some(e.hash), |
| 2875 | 5× | ..FsEntry::new(NodeKind::File, exec, fs::read(path)?) |
| 2876 | | }, |
| 2877 | 8147× | None => FsEntry::new(NodeKind::File, exec, fs::read(path)?), |
| 2878 | | }; |
| 2879 | | |
| 2880 | | |
| 2881 | 12393× | e.stat = stat; |
| 2882 | 12393× | Ok(e) |
| 2883 | 12394× | } |
| 2884 | |
|
| 2885 | |
|
| 2886 | |
|
| 2887 | 2988× | pub(crate) fn join<'a, A, B>( |
| 2888 | 2988× | a: &'a BTreeMap<String, A>, |
| 2889 | 2988× | b: &'a BTreeMap<String, B>, |
| 2890 | 2988× | ) -> impl Iterator<Item = (&'a String, Option<&'a A>, Option<&'a B>)> { |
| 2891 | 2988× | let (mut a, mut b) = (a.iter().peekable(), b.iter().peekable()); |
| 2892 | 36597× | std::iter::from_fn(move || match (a.peek(), b.peek()) { |
| 2893 | 2988× | (None, None) => None, |
| 2894 | 2997× | (Some(_), None) => a.next().map(|(k, v)| (k, Some(v), None)), |
| 2895 | 5960× | (None, Some(_)) => b.next().map(|(k, v)| (k, None, Some(v))), |
| 2896 | 24652× | (Some((ka, _)), Some((kb, _))) => match ka.cmp(kb) { |
| 2897 | 344× | std::cmp::Ordering::Less => a.next().map(|(k, v)| (k, Some(v), None)), |
| 2898 | 316× | std::cmp::Ordering::Greater => b.next().map(|(k, v)| (k, None, Some(v))), |
| 2899 | | std::cmp::Ordering::Equal => { |
| 2900 | 23992× | let (k, va) = a.next()?; |
| 2901 | 23992× | let (_, vb) = b.next()?; |
| 2902 | 23992× | Some((k, Some(va), Some(vb))) |
| 2903 | | } |
| 2904 | | }, |
| 2905 | 36597× | }) |
| 2906 | 2988× | } |
| 2907 | |
|
| 2908 | |
|
| 2909 | |
|
| 2910 | 1485× | pub(crate) fn missing_from<'a, A, B>( |
| 2911 | 1485× | a: &'a BTreeMap<String, A>, |
| 2912 | 1485× | b: &BTreeMap<String, B>, |
| 2913 | 1485× | ) -> Vec<(&'a String, &'a A)> { |
| 2914 | 1485× | let mut keys = b.keys().peekable(); |
| 2915 | 1485× | a.iter() |
| 2916 | 9305× | .filter(|(k, _)| { |
| 2917 | 17822× | while keys.next_if(|kb| *kb < *k).is_some() {} |
| 2918 | 9305× | keys.peek() != Some(k) |
| 2919 | 9305× | }) |
| 2920 | 1485× | .collect() |
| 2921 | 1485× | } |