//! Fast, side-effect-free status for shell prompts. //! //! Every snapshot and checkout writes a manifest of the working copy as it //! was last seen: `size`, `mtime`, and BLAKE3 of each file. `quick_status` //! walks the working copy and compares against that manifest, reading a //! file's content only when its size or mtime changed, and records no //! operation. Ahead/behind counts come from walking the store between the //! local bookmark and its `origin` tracking state. use super::*; use std::time::UNIX_EPOCH; #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct PromptInfo { pub bookmark: String, /// Working-copy ChangeId prefix. pub change_id: String, pub heads: usize, pub dirty: bool, pub conflicted: bool, /// Changes local `main` has that `origin/main` lacks, when tracking exists. pub ahead: Option, pub behind: Option, } impl PromptInfo { /// The glyph for the working copy's state: `‼` conflicted, `✎` dirty, /// `✔` clean. pub fn state_glyph(&self) -> &'static str { if self.conflicted { "‼" } else if self.dirty { "✎" } else { "✔" } } /// The ANSI colour (SGR parameter) for the state: red, yellow, or cyan. pub fn state_color(&self) -> u8 { if self.conflicted { 31 } else if self.dirty { 33 } else { 36 } } /// `format` wrapped in the state colour. `guard` is `("\\[", "\\]")` for /// bash prompts, `("%{", "%}")` for zsh, or empty for raw output. pub fn format_colored(&self, fmt: &str, guard: (&str, &str)) -> String { format!( "{}\x1b[{}m{}{}{}\x1b[0m{}", guard.0, self.state_color(), guard.1, self.format(fmt), guard.0, guard.1 ) } /// Expand `%b` bookmark, `%c` change id, `%h` heads, `%d` dirty marker /// (`*`), `%x` conflict marker (`!`), `%a`/`%B` ahead/behind counts /// (empty when unknown), `%A`/`%D` `+n`/`-n` when non-zero, `%s` the /// state glyph, `%U`/`%O` `↑n`/`↓n` when non-zero, `%i` the repository /// icon, `%%` percent. pub fn format(&self, fmt: &str) -> String { let mut out = String::new(); let mut chars = fmt.chars(); while let Some(c) = chars.next() { if c != '%' { out.push(c); continue; } match chars.next() { Some('b') => out.push_str(&self.bookmark), Some('c') => out.push_str(&self.change_id), Some('h') => out.push_str(&self.heads.to_string()), Some('d') => { if self.dirty { out.push('*'); } } Some('x') => { if self.conflicted { out.push('!'); } } Some('a') => out.push_str(&self.ahead.map(|n| n.to_string()).unwrap_or_default()), Some('B') => out.push_str(&self.behind.map(|n| n.to_string()).unwrap_or_default()), Some('A') => { if let Some(n) = self.ahead.filter(|n| *n > 0) { out.push_str(&format!("+{n}")); } } Some('D') => { if let Some(n) = self.behind.filter(|n| *n > 0) { out.push_str(&format!("-{n}")); } } Some('s') => out.push_str(self.state_glyph()), Some('U') => { if let Some(n) = self.ahead.filter(|n| *n > 0) { out.push_str(&format!("↑{n}")); } } Some('O') => { if let Some(n) = self.behind.filter(|n| *n > 0) { out.push_str(&format!("↓{n}")); } } Some('i') => out.push_str(ICON), Some('%') => out.push('%'), Some(other) => { out.push('%'); out.push(other); } None => out.push('%'), } } out } } /// Path → (size, mtime in nanoseconds, BLAKE3 hex) as last seen. /// One manifest entry: the file's stat, its content hash, and the stored /// blob for an opaque file. #[derive(Clone, Debug)] pub(crate) struct ManifestEntry { pub size: u64, pub mtime_ns: u128, pub hash: [u8; 32], pub blob: Option, /// The graph key (`State::graph_key`) of the base file this text file /// was found equal to, rendered, byte for byte. pub graph: Option<[u8; 32]>, /// Whether the file was executable: a mode change alters neither size /// nor time. pub exec: bool, } #[derive(Clone, Debug, Default)] pub(crate) struct Manifest { /// When the manifest was written, for the racy-stat guard. pub written_ns: u128, pub entries: BTreeMap, } /// A parsed manifest and the identity (size, time, inode) of the file it /// came from: reused while the file is the same one. pub(crate) type CachedManifest = ((u64, u128, u64), (bool, bool, std::sync::Arc)); pub(crate) fn mtime_ns(m: &fs::Metadata) -> u128 { m.modified() .ok() .and_then(|t| t.duration_since(UNIX_EPOCH).ok()) .map(|d| d.as_nanos()) .unwrap_or(0) } pub const DEFAULT_PROMPT_FORMAT: &str = "%b %c%d%x%A%D"; /// The repository icon for `%i` (an atom, for oxygen). pub const ICON: &str = "⚛"; /// A format with glyphs: icon, bookmark, change id, state, ahead/behind. pub const GLYPH_PROMPT_FORMAT: &str = "%i %b %c %s%U%O"; /// The manifest file (oxygen#41): a flat binary layout read in one pass. /// A magic number; the conflicted and uncommitted flags; when it was /// written, at a fixed offset so an otherwise equal manifest can be /// recognised; BLAKE3 of everything else; then each entry in path order: /// path, size, mtime in nanoseconds, BLAKE3 of the content, the stored /// blob of an opaque file, the graph key it was found equal to, and /// whether it is executable. A file that does not check, or in any other /// layout (an older manifest), reads as no manifest, and the next snapshot /// writes it again: it is a cache, written without waiting for the disk. const MANIFEST_MAGIC: &[u8; 8] = b"oxyman2\0"; /// Where the written time sits: after the magic and the two flags. const MANIFEST_WRITTEN: std::ops::Range = 10..26; /// Where the checksum sits, after the written time. const MANIFEST_SUM: std::ops::Range = 26..58; fn manifest_sum(buf: &[u8]) -> [u8; 32] { let mut h = blake3::Hasher::new(); h.update(&buf[..MANIFEST_SUM.start]); h.update_rayon(&buf[MANIFEST_SUM.end..]); *h.finalize().as_bytes() } fn encode_manifest( conflicted: bool, uncommitted: bool, written_ns: u128, entries: &mut dyn Iterator, ) -> Vec { let mut w = crate::flat::Writer::default(); w.fixed(MANIFEST_MAGIC); w.u8(conflicted as u8); w.u8(uncommitted as u8); w.fixed(&written_ns.to_le_bytes()); w.fixed(&[0; 32]); for (path, e) in entries { w.bytes(path.as_bytes()); w.u64(e.size); w.fixed(&e.mtime_ns.to_le_bytes()); w.fixed(&e.hash); w.opt_object(&e.blob); match e.graph { None => w.u8(0), Some(k) => { w.u8(1); w.fixed(&k); } } w.u8(e.exec as u8); } let sum = manifest_sum(&w.0); w.0[MANIFEST_SUM].copy_from_slice(&sum); w.0 } fn decode_manifest(buf: &[u8]) -> Option<(bool, bool, Manifest)> { let mut r = crate::flat::Reader::new(buf); if &r.fixed::<8>().ok()? != MANIFEST_MAGIC { return None; } let conflicted = r.u8().ok()? == 1; let uncommitted = r.u8().ok()? == 1; let written_ns = u128::from_le_bytes(r.fixed().ok()?); if r.fixed::<32>().ok()? != manifest_sum(buf) { return None; } let mut entries = Vec::new(); while !r.at_end() { let path = std::str::from_utf8(r.bytes().ok()?).ok()?.to_string(); let e = ManifestEntry { size: r.u64().ok()?, mtime_ns: u128::from_le_bytes(r.fixed().ok()?), hash: r.fixed().ok()?, blob: r.opt_object().ok()?, graph: match r.u8().ok()? { 0 => None, _ => Some(r.fixed().ok()?), }, exec: r.u8().ok()? == 1, }; entries.push((path, e)); } // Written in path order, so this builds the map without searching. let entries = entries.into_iter().collect(); Some(( conflicted, uncommitted, Manifest { written_ns, entries, }, )) } impl Repo { pub(crate) fn manifest_path(&self) -> PathBuf { self.oxy .join("workspaces") .join(self.workspace.to_hex()) .join("manifest") } /// Record the working copy as it is now (after a snapshot, commit, or /// checkout), with header lines saying whether the base state is /// conflicted and whether the working-copy change holds uncommitted work. pub(crate) fn write_manifest(&self, conflicted: bool, uncommitted: bool) -> Result<()> { let entries = self.walk_working_copy()?; self.write_manifest_entries(conflicted, uncommitted, &entries, &HashMap::new()) } /// Write the manifest from entries already walked; `blobs` names the /// stored blob of each opaque path so the next walk can skip reading it. pub(crate) fn write_manifest_entries( &self, conflicted: bool, uncommitted: bool, entries: &BTreeMap, blobs: &HashMap, ) -> Result<()> { self.manifest_cache.take(); self.listing_cache.take(); let written = std::time::SystemTime::now() .duration_since(UNIX_EPOCH) .map(|d| d.as_nanos()) .unwrap_or(0); let mut newest_ns: u128 = 0; let mut rows = entries.iter().map(|(path, e)| { let (size, mtime_ns) = match e.stat { Some(known) => known, None => match fs::symlink_metadata(self.root.join(path)) { Ok(m) => (m.len(), mtime_ns(&m)), Err(_) => (e.content.len() as u64, 0), }, }; newest_ns = newest_ns.max(mtime_ns); let entry = ManifestEntry { size, mtime_ns, hash: e .content_hash .unwrap_or_else(|| *blake3::hash(&e.content).as_bytes()), blob: blobs.get(path).copied().or(e.blob), graph: e.graph_key, exec: e.exec, }; (path.as_str(), entry) }); let new = encode_manifest(conflicted, uncommitted, written, &mut rows); drop(rows); let p = self.manifest_path(); // Leave an unchanged manifest alone when its stamp already trusts // every entry (none is within the racy window): rewriting it would // change nothing but the file, which editors watching the tree // (and subrepos inside it) take as a change. if let Ok(old) = fs::read(&p) { let (w, sum) = (MANIFEST_WRITTEN, MANIFEST_SUM); if old.len() == new.len() && old[..w.start] == new[..w.start] && old[sum.end..] == new[sum.end..] { let old_written = u128::from_le_bytes(old[w.clone()].try_into().unwrap()); if newest_ns + 2_000_000_000 < old_written { return Ok(()); } } } fs::create_dir_all(p.parent().unwrap())?; write_cache(&p, &new) } /// Change the header flags, and set the graph key of each path in /// `keys` (after a commit, the new base's keys of the files it holds as /// they are on disk). pub(crate) fn rewrite_manifest( &self, conflicted: bool, uncommitted: bool, keys: &HashMap, ) -> Result<()> { self.manifest_cache.take(); self.listing_cache.take(); let p = self.manifest_path(); let Some((_, _, m)) = fs::read(&p).ok().and_then(|b| decode_manifest(&b)) else { return self.write_manifest(conflicted, uncommitted); }; let mut rows = m.entries.iter().map(|(path, e)| { let mut e = e.clone(); if let Some(k) = keys.get(path) { e.graph = Some(*k); } (path.as_str(), e) }); write_cache( &p, &encode_manifest(conflicted, uncommitted, m.written_ns, &mut rows), ) } /// Forget the manifest after an operation that changed the working-copy /// change without a snapshot (undo, rewrites); the prompt shows blank /// markers until the next snapshot. pub(crate) fn drop_manifest(&self) { self.manifest_cache.take(); self.listing_cache.take(); let _ = fs::remove_file(self.manifest_path()); } /// The manifest, with whether it records a conflict and uncommitted /// work; parsed once while the file stays the same one. pub(crate) fn read_manifest(&self) -> Result)>> { let path = self.manifest_path(); let id = match fs::metadata(&path) { Ok(m) => { #[cfg(unix)] let ino = std::os::unix::fs::MetadataExt::ino(&m); #[cfg(not(unix))] let ino = 0; (m.len(), mtime_ns(&m), ino) } Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), Err(e) => return Err(e.into()), }; if let Some((cached_id, m)) = &*self.manifest_cache.borrow() { if *cached_id == id { return Ok(Some(m.clone())); } } let Some((c, u, m)) = self.parse_manifest(&path)? else { return Ok(None); }; let m = (c, u, std::sync::Arc::new(m)); *self.manifest_cache.borrow_mut() = Some((id, m.clone())); Ok(Some(m)) } fn parse_manifest(&self, path: &std::path::Path) -> Result> { let buf = match fs::read(path) { Ok(b) => b, Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), Err(e) => return Err(e.into()), }; Ok(decode_manifest(&buf)) } /// Whether the working copy is certainly clean, known from the manifest /// alone, without loading any history (spec §12.4): the last snapshot /// or commit found no uncommitted work and no conflict, there are no /// subrepos (whose state the manifest does not hold), and no file was /// added, removed or changed since. An entry inside the racy window, /// where an equal size and time do not prove equal content, is checked /// by hashing the file. `false` means only that a snapshot must decide. pub fn clean_by_manifest(&self) -> Result { let _t = crate::timing::phase("status: check the manifest"); // Why a snapshot must decide, with timings on. let not = |why: &str| { if crate::timing::enabled() { eprintln!("timing status: not clean by the manifest: {why}"); } Ok(false) }; let Some((conflicted, uncommitted, m)) = self.read_manifest()? else { return not("no manifest"); }; if conflicted || uncommitted || self.root.join(".oxymodules").exists() { return not("conflicted, uncommitted or subrepos"); } // Entries in the racy window are hashed, in parallel (a checkout // just wrote them all). use rayon::prelude::*; let racy: Vec<(&String, &ManifestEntry)> = m .entries .iter() .filter(|(_, e)| e.mtime_ns + 2_000_000_000 >= m.written_ns) .collect(); let root = &self.root; let changed = racy.par_iter().find_any(|(rel, e)| { let path = root.join(rel.as_str()); let same = match fs::symlink_metadata(&path) { Ok(md) if md.file_type().is_symlink() => fs::read_link(&path).is_ok_and(|t| { *blake3::hash(t.to_string_lossy().as_bytes()).as_bytes() == e.hash }), Ok(md) if md.is_file() => { md.len() == e.size && fs::read(&path).is_ok_and(|b| *blake3::hash(&b).as_bytes() == e.hash) } _ => false, }; !same }); if let Some((rel, _)) = changed { return not(&format!("{rel} changed within the racy window")); } if self.quick_dirty_keeping(true)? != Some(false) { return not("a file differs"); } Ok(true) } /// Whether the working copy differs from the last manifest. Without a /// manifest the answer is `None`. pub fn quick_dirty(&self) -> Result> { self.quick_dirty_keeping(false) } /// `quick_dirty`; with `keep`, the listing it made is left for the /// next walk of the working copy. fn quick_dirty_keeping(&self, keep: bool) -> Result> { let Some((_, uncommitted, manifest)) = self.read_manifest()? else { return Ok(None); }; if uncommitted { return Ok(Some(true)); } use rayon::prelude::*; let sparse = &self.config.sparse_paths; let in_scope = |p: &str| { sparse.is_empty() || sparse .iter() .any(|s| p == s || p.starts_with(&format!("{s}/"))) }; // A working copy that cannot be listed counts as changed. // Known files are not matched against the ignore rules: with no // uncommitted work (the clean check), the manifest's files are the // base's, and a prompt's answer is the same either way. let known = |p: &str| manifest.entries.contains_key(p); let Ok(listing) = super::walk::list_knowing(&self.root, &in_scope, &known) else { return Ok(Some(true)); }; let differs = || -> Result { let links_differ = listing.symlinks.iter().any(|(rel, target)| { manifest .entries .get(rel) .is_none_or(|me| *blake3::hash(target).as_bytes() != me.hash) }); if links_differ { return Ok(true); } // Each file is checked on its own, in parallel, stopping at the // first difference; an unreadable one counts as different. let files_differ = listing.files.par_iter().any(|(rel, path, m)| { manifest.entries.get(rel).is_none_or(|me| { m.len() != me.size || super::is_executable(m) != me.exec || (mtime_ns(m) != me.mtime_ns && !fs::read(path) .is_ok_and(|b| *blake3::hash(&b).as_bytes() == me.hash)) }) }); if files_differ { return Ok(true); } let seen = listing.files.len() + listing.symlinks.len(); if seen == manifest.entries.len() { return Ok(false); } // Manifest entries the walk skipped: tracked files that ignore // rules hide (Linux tracks some its own .gitignore names). Found // by merging the two sorted lists; they count as present when // still on disk and unchanged, checked on every core. let mut listed: Vec<&str> = listing .files .iter() .map(|(r, _, _)| r.as_str()) .chain(listing.symlinks.iter().map(|(r, _)| r.as_str())) .collect(); listed.sort_unstable(); let mut at = listed.iter().peekable(); let skipped: Vec<(&String, &ManifestEntry)> = manifest .entries .iter() .filter(|(rel, _)| { while at.next_if(|l| **l < rel.as_str()).is_some() {} at.peek().is_none_or(|l| **l != rel.as_str()) }) .collect(); let root = &self.root; Ok(skipped.par_iter().any(|(rel, me)| { let path = root.join(rel.as_str()); let Ok(m) = fs::symlink_metadata(&path) else { return true; }; !m.file_type().is_symlink() && (m.len() != me.size || super::is_executable(&m) != me.exec || (mtime_ns(&m) != me.mtime_ns && !fs::read(&path) .is_ok_and(|b| *blake3::hash(&b).as_bytes() == me.hash))) })) }; let dirty = differs()?; if !dirty { // Nested repositories the manifest has no entry for are not // subrepos: nothing in them is tracked (oxygen#43). let mut nested: Vec = listing .nested .iter() .map(|d| self.rel_path(d)) .filter(|rel| !manifest.entries.contains_key(rel)) .collect(); nested.sort(); *self.untracked_nested.borrow_mut() = nested; } if dirty && keep { *self.listing_cache.borrow_mut() = Some(listing); } Ok(Some(dirty)) } /// Prompt information without recording anything. pub fn quick_status(&self) -> Result { let view = self.view()?; let bookmark = self.config.bookmark.clone(); let heads = self.heads_of(&view, &bookmark)?; let wc_hash = view.workspaces.get(&self.workspace).copied(); let change_id = match wc_hash { Some(h) => self.store.get_change_meta(&h)?.change_id.short(8), None => String::new(), }; // Conflictedness was recorded by the last snapshot; without a // manifest (never snapshotted) both flags are unknown and shown clear. let manifest = self.read_manifest()?; let conflicted = manifest.as_ref().map(|(c, _, _)| *c).unwrap_or(false); let dirty = self.quick_dirty()?.unwrap_or(false); let (ahead, behind) = match view.remotes.get("origin").and_then(|b| b.get(&bookmark)) { Some(st) => { let remote_heads = self.store.get_state(st)?.heads; let ahead = self.negotiate(&remote_heads, &heads).map(|v| v.len()).ok(); let behind = self.negotiate(&heads, &remote_heads).map(|v| v.len()).ok(); (ahead, behind) } None => (None, None), }; Ok(PromptInfo { bookmark, change_id, heads: heads.len(), dirty, conflicted, ahead, behind, }) } } #[cfg(test)] mod tests { use super::*; #[test] fn placeholders_and_glyphs() { let clean = PromptInfo { bookmark: "main".into(), change_id: "658990aa".into(), heads: 1, dirty: false, conflicted: false, ahead: Some(0), behind: None, }; assert_eq!(clean.format(DEFAULT_PROMPT_FORMAT), "main 658990aa"); assert_eq!(clean.format(GLYPH_PROMPT_FORMAT), "⚛ main 658990aa ✔"); let busy = PromptInfo { dirty: true, ahead: Some(2), behind: Some(3), ..clean.clone() }; assert_eq!(busy.format(DEFAULT_PROMPT_FORMAT), "main 658990aa*+2-3"); assert_eq!(busy.format(GLYPH_PROMPT_FORMAT), "⚛ main 658990aa ✎↑2↓3"); let bad = PromptInfo { conflicted: true, ..busy.clone() }; assert_eq!(bad.format("%s%x%d %h %a/%B %%"), "‼!* 1 2/3 %"); assert_eq!(bad.state_color(), 31); assert_eq!( busy.format_colored("%b", ("\\[", "\\]")), "\\[\x1b[33m\\]main\\[\x1b[0m\\]" ); assert_eq!(clean.format_colored("%b", ("", "")), "\x1b[36mmain\x1b[0m"); assert_eq!(clean.format("%q"), "%q"); } }