OXYGENHUB
◆ v0.4.2 oxygen / crates / oxy-core / src / repo / prompt.rs
45.6 KiB · Rust · last changed prompt: compare a subrepo by its pin, not its directory's time 1 month ago source blame history raw
1
//! Fast, side-effect-free status for shell prompts.
2
//!
3
//! Every snapshot and checkout writes a manifest of the working copy as it
4
//! was last seen: `size`, `mtime`, and BLAKE3 of each file. `quick_status`
5
//! walks the working copy and compares against that manifest, reading a
6
//! file's content only when its size or mtime changed, and records no
7
//! operation. Ahead/behind counts come from walking the store between the
8
//! local bookmark and its `origin` tracking state.
9
10
use super::*;
11
use std::time::UNIX_EPOCH;
12
13
#[derive(Debug, Clone, Default, PartialEq, Eq)]
14
pub struct PromptInfo {
15
    pub bookmark: String,
16
    /// Working-copy ChangeId prefix.
17
    pub change_id: String,
18
    pub heads: usize,
19
    pub dirty: bool,
20
    pub conflicted: bool,
21
    /// Changes local `main` has that `origin/main` lacks, when tracking exists.
22
    pub ahead: Option<usize>,
23
    pub behind: Option<usize>,
24
}
25
26
impl PromptInfo {
27
    /// The glyph for the working copy's state: `‼` conflicted, `✎` dirty,
28
    /// `✔` clean.
29
    pub fn state_glyph(&self) -> &'static str {
30
        if self.conflicted {
31
            "‼"
32
        } else if self.dirty {
33
            "✎"
34
        } else {
35
            "✔"
36
        }
37
    }
38
39
    /// The ANSI colour (SGR parameter) for the state: red, yellow, or cyan.
40
    pub fn state_color(&self) -> u8 {
41
        if self.conflicted {
42
            31
43
        } else if self.dirty {
44
            33
45
        } else {
46
            36
47
        }
48
    }
49
50
    /// `format` wrapped in the state colour. `guard` is `("\\[", "\\]")` for
51
    /// bash prompts, `("%{", "%}")` for zsh, or empty for raw output.
52
    pub fn format_colored(&self, fmt: &str, guard: (&str, &str)) -> String {
53
        format!(
54
            "{}\x1b[{}m{}{}{}\x1b[0m{}",
55
            guard.0,
56
            self.state_color(),
57
            guard.1,
58
            self.format(fmt),
59
            guard.0,
60
            guard.1
61
        )
62
    }
63
64
    /// Expand `%b` bookmark, `%c` change id, `%h` heads, `%d` dirty marker
65
    /// (`*`), `%x` conflict marker (`!`), `%a`/`%B` ahead/behind counts
66
    /// (empty when unknown), `%A`/`%D` `+n`/`-n` when non-zero, `%s` the
67
    /// state glyph, `%U`/`%O` `↑n`/`↓n` when non-zero, `%i` the repository
68
    /// icon, `%%` percent.
69
    pub fn format(&self, fmt: &str) -> String {
70
        let mut out = String::new();
71
        let mut chars = fmt.chars();
72
        while let Some(c) = chars.next() {
73
            if c != '%' {
74
                out.push(c);
75
                continue;
76
            }
77
            match chars.next() {
78
                Some('b') => out.push_str(&self.bookmark),
79
                Some('c') => out.push_str(&self.change_id),
80
                Some('h') => out.push_str(&self.heads.to_string()),
81
                Some('d') => {
82
                    if self.dirty {
83
                        out.push('*');
84
                    }
85
                }
86
                Some('x') => {
87
                    if self.conflicted {
88
                        out.push('!');
89
                    }
90
                }
91
                Some('a') => out.push_str(&self.ahead.map(|n| n.to_string()).unwrap_or_default()),
92
                Some('B') => out.push_str(&self.behind.map(|n| n.to_string()).unwrap_or_default()),
93
                Some('A') => {
94
                    if let Some(n) = self.ahead.filter(|n| *n > 0) {
95
                        out.push_str(&format!("+{n}"));
96
                    }
97
                }
98
                Some('D') => {
99
                    if let Some(n) = self.behind.filter(|n| *n > 0) {
100
                        out.push_str(&format!("-{n}"));
101
                    }
102
                }
103
                Some('s') => out.push_str(self.state_glyph()),
104
                Some('U') => {
105
                    if let Some(n) = self.ahead.filter(|n| *n > 0) {
106
                        out.push_str(&format!("↑{n}"));
107
                    }
108
                }
109
                Some('O') => {
110
                    if let Some(n) = self.behind.filter(|n| *n > 0) {
111
                        out.push_str(&format!("↓{n}"));
112
                    }
113
                }
114
                Some('i') => out.push_str(ICON),
115
                Some('%') => out.push('%'),
116
                Some(other) => {
117
                    out.push('%');
118
                    out.push(other);
119
                }
120
                None => out.push('%'),
121
            }
122
        }
123
        out
124
    }
125
}
126
127
/// Path → (size, mtime in nanoseconds, BLAKE3 hex) as last seen.
128
/// One manifest entry: the file's stat, its content hash, and the stored
129
/// blob for an opaque file.
130
#[derive(Clone, Debug)]
131
pub(crate) struct ManifestEntry {
132
    pub size: u64,
133
    pub mtime_ns: u128,
134
    pub hash: [u8; 32],
135
    pub blob: Option<ObjectRef>,
136
    /// The graph key (`State::graph_key`) of the base file this text file
137
    /// was found equal to, rendered, byte for byte.
138
    pub graph: Option<[u8; 32]>,
139
    /// Whether the file was executable: a mode change alters neither size
140
    /// nor time.
141
    pub exec: bool,
142
}
143
144
#[derive(Clone, Debug, Default)]
145
pub(crate) struct Manifest {
146
    /// When the manifest was written, for the racy-stat guard.
147
    pub written_ns: u128,
148
    pub entries: BTreeMap<String, ManifestEntry>,
149
    /// Every directory the walk listed, with its time then; empty when the
150
    /// manifest was not written from a walk (a checkout), and then the
151
    /// check lists the tree.
152
    pub dirs: Vec<(String, u128)>,
153
    /// Directories holding a repository of their own, as the walk found.
154
    pub nested: Vec<String>,
155
}
156
157
/// A parsed manifest and the identity (size, time, inode) of the file it
158
/// came from: reused while the file is the same one.
159
pub(crate) type CachedManifest = ((u64, u128, u64), (bool, bool, std::sync::Arc<Manifest>));
160
161
/// Now, in nanoseconds since the epoch.
162
pub(crate) fn now_ns() -> u128 {
163
    std::time::SystemTime::now()
164
        .duration_since(UNIX_EPOCH)
165
        .map(|d| d.as_nanos())
166
        .unwrap_or(0)
167
}
168
169
pub(crate) fn mtime_ns(m: &fs::Metadata) -> u128 {
170
    m.modified()
171
        .ok()
172
        .and_then(|t| t.duration_since(UNIX_EPOCH).ok())
173
        .map(|d| d.as_nanos())
174
        .unwrap_or(0)
175
}
176
177
pub const DEFAULT_PROMPT_FORMAT: &str = "%b %c%d%x%A%D";
178
/// The repository icon for `%i` (an atom, for oxygen).
179
pub const ICON: &str = "⚛";
180
/// A format with glyphs: icon, bookmark, change id, state, ahead/behind.
181
pub const GLYPH_PROMPT_FORMAT: &str = "%i %b %c %s%U%O";
182
183
/// The manifest file (oxygen#41): a flat binary layout read in one pass.
184
/// A magic number; the conflicted and uncommitted flags; when it was
185
/// written, at a fixed offset so an otherwise equal manifest can be
186
/// recognised; BLAKE3 of everything else; then each entry in path order:
187
/// path, size, mtime in nanoseconds, BLAKE3 of the content, the stored
188
/// blob of an opaque file, the graph key it was found equal to, and
189
/// whether it is executable. Format 3 puts before the entries every
190
/// directory the walk listed with its time, and the nested repositories. A file that does not check, or in any other
191
/// layout (an older manifest), reads as no manifest, and the next snapshot
192
/// writes it again: it is a cache, written without waiting for the disk.
193
const MANIFEST_MAGIC: &[u8; 8] = b"oxyman3\0";
194
/// Where the written time sits: after the magic and the two flags.
195
const MANIFEST_WRITTEN: std::ops::Range<usize> = 10..26;
196
/// Where the checksum sits, after the written time.
197
const MANIFEST_SUM: std::ops::Range<usize> = 26..58;
198
199
fn manifest_sum(buf: &[u8]) -> [u8; 32] {
200
    let mut h = blake3::Hasher::new();
201
    h.update(&buf[..MANIFEST_SUM.start]);
202
    h.update_rayon(&buf[MANIFEST_SUM.end..]);
203
    *h.finalize().as_bytes()
204
}
205
206
fn encode_manifest(
207
    conflicted: bool,
208
    uncommitted: bool,
209
    written_ns: u128,
210
    dirs: &[(String, u128)],
211
    nested: &[String],
212
    entries: &mut dyn Iterator<Item = (&str, ManifestEntry)>,
213
) -> Vec<u8> {
214
    let mut w = crate::flat::Writer::default();
215
    w.fixed(MANIFEST_MAGIC);
216
    w.u8(conflicted as u8);
217
    w.u8(uncommitted as u8);
218
    w.fixed(&written_ns.to_le_bytes());
219
    w.fixed(&[0; 32]);
220
    w.count(dirs.len());
221
    for (d, t) in dirs {
222
        w.bytes(d.as_bytes());
223
        w.fixed(&t.to_le_bytes());
224
    }
225
    w.count(nested.len());
226
    for n in nested {
227
        w.bytes(n.as_bytes());
228
    }
229
    for (path, e) in entries {
230
        w.bytes(path.as_bytes());
231
        w.u64(e.size);
232
        w.fixed(&e.mtime_ns.to_le_bytes());
233
        w.fixed(&e.hash);
234
        w.opt_object(&e.blob);
235
        match e.graph {
236
            None => w.u8(0),
237
            Some(k) => {
238
                w.u8(1);
239
                w.fixed(&k);
240
            }
241
        }
242
        w.u8(e.exec as u8);
243
    }
244
    let sum = manifest_sum(&w.0);
245
    w.0[MANIFEST_SUM].copy_from_slice(&sum);
246
    w.0
247
}
248
249
fn decode_manifest(buf: &[u8]) -> Option<(bool, bool, Manifest)> {
250
    let mut r = crate::flat::Reader::new(buf);
251
    if &r.fixed::<8>().ok()? != MANIFEST_MAGIC {
252
        return None;
253
    }
254
    let conflicted = r.u8().ok()? == 1;
255
    let uncommitted = r.u8().ok()? == 1;
256
    let written_ns = u128::from_le_bytes(r.fixed().ok()?);
257
    if r.fixed::<32>().ok()? != manifest_sum(buf) {
258
        return None;
259
    }
260
    let mut dirs = Vec::new();
261
    for _ in 0..r.count().ok()? {
262
        let d = std::str::from_utf8(r.bytes().ok()?).ok()?.to_string();
263
        dirs.push((d, u128::from_le_bytes(r.fixed().ok()?)));
264
    }
265
    let mut nested = Vec::new();
266
    for _ in 0..r.count().ok()? {
267
        nested.push(std::str::from_utf8(r.bytes().ok()?).ok()?.to_string());
268
    }
269
    let mut entries = Vec::new();
270
    while !r.at_end() {
271
        let path = std::str::from_utf8(r.bytes().ok()?).ok()?.to_string();
272
        let e = ManifestEntry {
273
            size: r.u64().ok()?,
274
            mtime_ns: u128::from_le_bytes(r.fixed().ok()?),
275
            hash: r.fixed().ok()?,
276
            blob: r.opt_object().ok()?,
277
            graph: match r.u8().ok()? {
278
                0 => None,
279
                _ => Some(r.fixed().ok()?),
280
            },
281
            exec: r.u8().ok()? == 1,
282
        };
283
        entries.push((path, e));
284
    }
285
    // Written in path order, so this builds the map without searching.
286
    let entries = entries.into_iter().collect();
287
    Some((
288
        conflicted,
289
        uncommitted,
290
        Manifest {
291
            written_ns,
292
            entries,
293
            dirs,
294
            nested,
295
        },
296
    ))
297
}
298
299
/// A tracked path as it is now: a file's stat, or a symlink's target.
300
enum Tracked {
301
    File(super::walk::Stat),
302
    Link(Vec<u8>),
303
}
304
305
/// The stat of every path in `rels` (relative to `root`; `None` for one
306
/// gone or neither a file nor a symlink), in the same order. Paths are
307
/// grouped by directory and each directory is opened once: a stat relative
308
/// to it resolves one name, where a stat by full path walks every
309
/// component from the root again (most of a clean status's time, in the
310
/// kernel's path lookup, on the Linux tree).
311
fn stat_tracked(root: &Path, rels: &[&String]) -> Vec<Option<Tracked>> {
312
    use rayon::prelude::*;
313
    let mut by_dir: HashMap<&str, Vec<(usize, &str)>> = HashMap::new();
314
    for (i, rel) in rels.iter().enumerate() {
315
        let (dir, name) = rel.rsplit_once('/').unwrap_or(("", rel.as_str()));
316
        by_dir.entry(dir).or_default().push((i, name));
317
    }
318
    let groups: Vec<(&str, Vec<(usize, &str)>)> = by_dir.into_iter().collect();
319
    let found: Vec<(usize, Option<Tracked>)> = groups
320
        .par_iter()
321
        .flat_map_iter(|(dir, names)| {
322
            let handle = fs::File::open(root.join(dir)).ok();
323
            names
324
                .iter()
325
                .map(|(i, name)| {
326
                    let rel = rels[*i];
327
                    let now = match &handle {
328
                        Some(d) => stat_in(d, name, || root.join(rel)),
329
                        None => None,
330
                    };
331
                    (*i, now)
332
                })
333
                .collect::<Vec<_>>()
334
        })
335
        .collect();
336
    let mut out: Vec<Option<Tracked>> = (0..rels.len()).map(|_| None).collect();
337
    for (i, now) in found {
338
        out[i] = now;
339
    }
340
    out
341
}
342
343
/// One entry of the open directory `dir`; `path` builds its full path,
344
/// needed only for a symlink's target.
345
#[cfg(unix)]
346
fn stat_in(dir: &fs::File, name: &str, path: impl Fn() -> PathBuf) -> Option<Tracked> {
347
    use std::os::fd::AsRawFd;
348
    let c = std::ffi::CString::new(name).ok()?;
349
    let mut st: libc::stat = unsafe { std::mem::zeroed() };
350
    // SAFETY: a valid directory descriptor, a NUL-terminated name, and a
351
    // stat buffer of the right type that outlives the call.
352
    let r = unsafe {
353
        libc::fstatat(
354
            dir.as_raw_fd(),
355
            c.as_ptr(),
356
            &mut st,
357
            libc::AT_SYMLINK_NOFOLLOW,
358
        )
359
    };
360
    if r != 0 {
361
        return None;
362
    }
363
    match st.st_mode & libc::S_IFMT {
364
        libc::S_IFREG => Some(Tracked::File(super::walk::Stat {
365
            size: st.st_size as u64,
366
            // As `mtime_ns` reads it: nanoseconds since the epoch, 0 before it.
367
            mtime_ns: if st.st_mtime < 0 {
368
                0
369
            } else {
370
                st.st_mtime as u128 * 1_000_000_000 + st.st_mtime_nsec as u128
371
            },
372
            exec: st.st_mode & 0o111 != 0,
373
        })),
374
        libc::S_IFLNK => {
375
            let t = fs::read_link(path()).ok()?;
376
            Some(Tracked::Link(t.to_string_lossy().as_bytes().to_vec()))
377
        }
378
        _ => None,
379
    }
380
}
381
382
#[cfg(not(unix))]
383
fn stat_in(_dir: &fs::File, _name: &str, path: impl Fn() -> PathBuf) -> Option<Tracked> {
384
    let p = path();
385
    let m = fs::symlink_metadata(&p).ok()?;
386
    if m.file_type().is_symlink() {
387
        let t = fs::read_link(&p).ok()?;
388
        Some(Tracked::Link(t.to_string_lossy().as_bytes().to_vec()))
389
    } else if m.is_file() {
390
        Some(Tracked::File(super::walk::Stat::of(&m)))
391
    } else {
392
        None
393
    }
394
}
395
396
/// The status cache: a magic number, the working-copy change, the
397
/// manifest checksum, then each status line (a status byte and a path).
398
const STATUS_MAGIC: &[u8; 8] = b"oxystat1";
399
400
fn status_byte(s: ChangeStatus) -> u8 {
401
    match s {
402
        ChangeStatus::Added => 0,
403
        ChangeStatus::Modified => 1,
404
        ChangeStatus::Removed => 2,
405
        ChangeStatus::Renamed => 3,
406
        ChangeStatus::ModeChanged => 4,
407
        ChangeStatus::ConflictEdited => 5,
408
        ChangeStatus::Resolved => 6,
409
    }
410
}
411
412
fn encode_status_cache(wc: &ChangeHash, stamp: &[u8; 32], statuses: &[PathStatus]) -> Vec<u8> {
413
    let mut out = Vec::with_capacity(80 + statuses.len() * 40);
414
    out.extend_from_slice(STATUS_MAGIC);
415
    out.extend_from_slice(&wc.0);
416
    out.extend_from_slice(stamp);
417
    out.extend_from_slice(&(statuses.len() as u32).to_le_bytes());
418
    for s in statuses {
419
        out.push(status_byte(s.status));
420
        out.extend_from_slice(&(s.path.len() as u32).to_le_bytes());
421
        out.extend_from_slice(s.path.as_bytes());
422
    }
423
    out
424
}
425
426
fn decode_status_cache(b: &[u8]) -> Option<(ChangeHash, [u8; 32], Vec<PathStatus>)> {
427
    let rest = b.strip_prefix(STATUS_MAGIC)?;
428
    let wc = ObjectRef(rest.get(..32)?.try_into().ok()?);
429
    let stamp: [u8; 32] = rest.get(32..64)?.try_into().ok()?;
430
    let n = u32::from_le_bytes(rest.get(64..68)?.try_into().ok()?) as usize;
431
    let mut at = 68;
432
    let mut out = Vec::with_capacity(n.min(1 << 16));
433
    for _ in 0..n {
434
        let status = match *rest.get(at)? {
435
            0 => ChangeStatus::Added,
436
            1 => ChangeStatus::Modified,
437
            2 => ChangeStatus::Removed,
438
            3 => ChangeStatus::Renamed,
439
            4 => ChangeStatus::ModeChanged,
440
            5 => ChangeStatus::ConflictEdited,
441
            6 => ChangeStatus::Resolved,
442
            _ => return None,
443
        };
444
        let len = u32::from_le_bytes(rest.get(at + 1..at + 5)?.try_into().ok()?) as usize;
445
        let path = std::str::from_utf8(rest.get(at + 5..at + 5 + len)?)
446
            .ok()?
447
            .to_string();
448
        out.push(PathStatus { path, status });
449
        at += 5 + len;
450
    }
451
    (at == rest.len()).then_some((wc, stamp, out))
452
}
453
454
impl Repo {
455
    pub(crate) fn manifest_path(&self) -> PathBuf {
456
        self.oxy
457
            .join("workspaces")
458
            .join(self.workspace.to_hex())
459
            .join("manifest")
460
    }
461
462
    /// Record the working copy as it is now (after a snapshot, commit, or
463
    /// checkout), with header lines saying whether the base state is
464
    /// conflicted and whether the working-copy change holds uncommitted work.
465
    pub(crate) fn write_manifest(&self, conflicted: bool, uncommitted: bool) -> Result<()> {
466
        let entries = self.walk_working_copy()?;
467
        self.write_manifest_entries(conflicted, uncommitted, &entries, &HashMap::new())
468
    }
469
470
    /// Write the manifest from entries already walked; `blobs` names the
471
    /// stored blob of each opaque path so the next walk can skip reading it.
472
    pub(crate) fn write_manifest_entries(
473
        &self,
474
        conflicted: bool,
475
        uncommitted: bool,
476
        entries: &BTreeMap<String, FsEntry>,
477
        blobs: &HashMap<String, ObjectRef>,
478
    ) -> Result<()> {
479
        let dirs = self.walked_dirs.take().unwrap_or_default();
480
        self.write_manifest_entries_as_of(conflicted, uncommitted, entries, blobs, now_ns(), dirs)
481
    }
482
483
    /// `write_manifest_entries` stamped `written` (when the entries were
484
    /// read), with the directories of the walk they came from.
485
    pub(crate) fn write_manifest_entries_as_of(
486
        &self,
487
        conflicted: bool,
488
        uncommitted: bool,
489
        entries: &BTreeMap<String, FsEntry>,
490
        blobs: &HashMap<String, ObjectRef>,
491
        written: u128,
492
        (dirs, nested): WalkedDirs,
493
    ) -> Result<()> {
494
        self.manifest_cache.take();
495
        self.listing_cache.take();
496
        let mut newest_ns: u128 = 0;
497
        let mut rows = entries.iter().map(|(path, e)| {
498
            let (size, mtime_ns) = match e.stat {
499
                Some(known) => known,
500
                None => match fs::symlink_metadata(self.root.join(path)) {
501
                    Ok(m) => (m.len(), mtime_ns(&m)),
502
                    Err(_) => (e.content.len() as u64, 0),
503
                },
504
            };
505
            newest_ns = newest_ns.max(mtime_ns);
506
            let entry = ManifestEntry {
507
                size,
508
                mtime_ns,
509
                hash: e
510
                    .content_hash
511
                    .unwrap_or_else(|| *blake3::hash(&e.content).as_bytes()),
512
                blob: blobs.get(path).copied().or(e.blob),
513
                graph: e.graph_key,
514
                exec: e.exec,
515
            };
516
            (path.as_str(), entry)
517
        });
518
        let encoding = crate::timing::phase("manifest: rows and encoding");
519
        let new = encode_manifest(conflicted, uncommitted, written, &dirs, &nested, &mut rows);
520
        drop(rows);
521
        drop(encoding);
522
        if crate::timing::enabled() {
523
            let unstated = entries.values().filter(|e| e.stat.is_none()).count();
524
            let unhashed = entries
525
                .values()
526
                .filter(|e| e.content_hash.is_none())
527
                .count();
528
            eprintln!(
529
                "timing manifest: {} rows, {unstated} without a stat, {unhashed} without a hash",
530
                entries.len()
531
            );
532
        }
533
        let _t = crate::timing::phase("manifest: compare and write");
534
        let p = self.manifest_path();
535
        // Leave an unchanged manifest alone when its stamp already trusts
536
        // every entry (none is within the racy window): rewriting it would
537
        // change nothing but the file, which editors watching the tree
538
        // (and subrepos inside it) take as a change.
539
        if let Ok(old) = fs::read(&p) {
540
            let (w, sum) = (MANIFEST_WRITTEN, MANIFEST_SUM);
541
            if old.len() == new.len()
542
                && old[..w.start] == new[..w.start]
543
                && old[sum.end..] == new[sum.end..]
544
            {
545
                let old_written = u128::from_le_bytes(old[w.clone()].try_into().unwrap());
546
                if newest_ns + 2_000_000_000 < old_written {
547
                    return Ok(());
548
                }
549
            }
550
        }
551
        fs::create_dir_all(p.parent().unwrap())?;
552
        write_cache(&p, &new)
553
    }
554
555
    /// Change the header flags, and set the graph key of each path in
556
    /// `keys` (after a commit, the new base's keys of the files it holds as
557
    /// they are on disk).
558
    pub(crate) fn rewrite_manifest(
559
        &self,
560
        conflicted: bool,
561
        uncommitted: bool,
562
        keys: &HashMap<String, [u8; 32]>,
563
    ) -> Result<()> {
564
        self.manifest_cache.take();
565
        self.listing_cache.take();
566
        let p = self.manifest_path();
567
        let Some((_, _, m)) = fs::read(&p).ok().and_then(|b| decode_manifest(&b)) else {
568
            return self.write_manifest(conflicted, uncommitted);
569
        };
570
        let mut rows = m.entries.iter().map(|(path, e)| {
571
            let mut e = e.clone();
572
            if let Some(k) = keys.get(path) {
573
                e.graph = Some(*k);
574
            }
575
            (path.as_str(), e)
576
        });
577
        write_cache(
578
            &p,
579
            &encode_manifest(
580
                conflicted,
581
                uncommitted,
582
                m.written_ns,
583
                &m.dirs,
584
                &m.nested,
585
                &mut rows,
586
            ),
587
        )
588
    }
589
590
    /// Forget the manifest after an operation that changed the working-copy
591
    /// change without a snapshot (undo, rewrites); the prompt shows blank
592
    /// markers until the next snapshot.
593
    pub(crate) fn drop_manifest(&self) {
594
        self.manifest_cache.take();
595
        self.listing_cache.take();
596
        let _ = fs::remove_file(self.manifest_path());
597
    }
598
599
    /// The manifest, with whether it records a conflict and uncommitted
600
    /// work; parsed once while the file stays the same one.
601
    /// Whether the working copy was conflicted when last recorded (the
602
    /// manifest's flag), without looking at the files.
603
    pub fn recorded_conflicted(&self) -> Result<bool> {
604
        Ok(self.read_manifest()?.is_some_and(|(c, _, _)| c))
605
    }
606
607
    pub(crate) fn read_manifest(&self) -> Result<Option<(bool, bool, std::sync::Arc<Manifest>)>> {
608
        let path = self.manifest_path();
609
        let id = match fs::metadata(&path) {
610
            Ok(m) => {
611
                #[cfg(unix)]
612
                let ino = std::os::unix::fs::MetadataExt::ino(&m);
613
                #[cfg(not(unix))]
614
                let ino = 0;
615
                (m.len(), mtime_ns(&m), ino)
616
            }
617
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
618
            Err(e) => return Err(e.into()),
619
        };
620
        if let Some((cached_id, m)) = &*self.manifest_cache.borrow() {
621
            if *cached_id == id {
622
                return Ok(Some(m.clone()));
623
            }
624
        }
625
        let Some((c, u, m)) = self.parse_manifest(&path)? else {
626
            return Ok(None);
627
        };
628
        let m = (c, u, std::sync::Arc::new(m));
629
        *self.manifest_cache.borrow_mut() = Some((id, m.clone()));
630
        Ok(Some(m))
631
    }
632
633
    fn parse_manifest(&self, path: &std::path::Path) -> Result<Option<(bool, bool, Manifest)>> {
634
        let buf = match fs::read(path) {
635
            Ok(b) => b,
636
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
637
            Err(e) => return Err(e.into()),
638
        };
639
        Ok(decode_manifest(&buf))
640
    }
641
642
    /// Whether the working copy is certainly clean, known from the manifest
643
    /// alone, without loading any history (spec §12.4): the last snapshot
644
    /// or commit found no uncommitted work and no conflict, there are no
645
    /// subrepos (whose state the manifest does not hold), and no file was
646
    /// added, removed or changed since. An entry inside the racy window,
647
    /// where an equal size and time do not prove equal content, is checked
648
    /// by hashing the file. `false` means only that a snapshot must decide.
649
    pub fn clean_by_manifest(&self) -> Result<bool> {
650
        let _t = crate::timing::phase("status: check the manifest");
651
        // Why a snapshot must decide, with timings on.
652
        let not = |why: &str| {
653
            if crate::timing::enabled() {
654
                eprintln!("timing status: not clean by the manifest: {why}");
655
            }
656
            Ok(false)
657
        };
658
        let Some((conflicted, uncommitted, m)) = self.read_manifest()? else {
659
            return not("no manifest");
660
        };
661
        if conflicted || uncommitted || self.root.join(".oxymodules").exists() {
662
            return not("conflicted, uncommitted or subrepos");
663
        }
664
        let started = now_ns();
665
        if self.disk_differs(&m, true)? {
666
            return not("a file differs");
667
        }
668
        // Verified clean. Entries or directories within the racy window of
669
        // the manifest's time were hashed or listed just now; restamped with
670
        // the time this check began, the next check can trust them, as
671
        // Git restamps its index (anything changed within two seconds of
672
        // that time is still racy by the same rule).
673
        // A check that had to list the tree leaves the directories it
674
        // listed: kept, the next check need not list.
675
        let window = 2_000_000_000;
676
        let listed = self.listed_dirs.take();
677
        let racy = m.dirs.iter().any(|(_, t)| t + window >= m.written_ns)
678
            || m.entries
679
                .values()
680
                .any(|e| e.mtime_ns + window >= m.written_ns);
681
        if (racy || (m.dirs.is_empty() && listed.is_some())) && started > m.written_ns {
682
            let (dirs, nested) = listed.unwrap_or_else(|| (m.dirs.clone(), m.nested.clone()));
683
            self.restamp_manifest(&m, conflicted, uncommitted, started, &dirs, &nested);
684
        }
685
        Ok(true)
686
    }
687
688
    /// Rewrite the manifest with a new time and directory list, its entries
689
    /// unchanged.
690
    fn restamp_manifest(
691
        &self,
692
        m: &Manifest,
693
        conflicted: bool,
694
        uncommitted: bool,
695
        written_ns: u128,
696
        dirs: &[(String, u128)],
697
        nested: &[String],
698
    ) {
699
        let _t = crate::timing::phase("status: restamp the manifest");
700
        let mut rows = m.entries.iter().map(|(p, e)| (p.as_str(), e.clone()));
701
        let bytes = encode_manifest(conflicted, uncommitted, written_ns, dirs, nested, &mut rows);
702
        self.manifest_cache.take();
703
        let _ = write_cache(&self.manifest_path(), &bytes);
704
    }
705
706
    /// A manifest entry written within the racy window whose file no
707
    /// longer matches it. Such entries are hashed, in parallel (a checkout
708
    /// just wrote them all).
709
    fn racy_change(&self, m: &Manifest) -> Option<String> {
710
        use rayon::prelude::*;
711
        let racy: Vec<(&String, &ManifestEntry)> = m
712
            .entries
713
            .iter()
714
            .filter(|(_, e)| e.mtime_ns + 2_000_000_000 >= m.written_ns)
715
            .collect();
716
        let root = &self.root;
717
        racy.par_iter()
718
            .find_any(|(rel, e)| {
719
                let path = root.join(rel.as_str());
720
                let same = match fs::symlink_metadata(&path) {
721
                    Ok(md) if md.file_type().is_symlink() => fs::read_link(&path).is_ok_and(|t| {
722
                        *blake3::hash(t.to_string_lossy().as_bytes()).as_bytes() == e.hash
723
                    }),
724
                    Ok(md) if md.is_file() => {
725
                        md.len() == e.size
726
                            && fs::read(&path).is_ok_and(|b| *blake3::hash(&b).as_bytes() == e.hash)
727
                    }
728
                    // A subrepo: its pin is compared on its own (`subrepo_moved`),
729
                    // its directory's time says nothing (oxygen#75).
730
                    Ok(md) if md.is_dir() => {
731
                        path.join(OXY_DIR).exists() || path.join(".git").exists()
732
                    }
733
                    _ => false,
734
                };
735
                !same
736
            })
737
            .map(|(rel, _)| rel.to_string())
738
    }
739
740
    /// The status lines of the last snapshot, when the working copy is as
741
    /// that snapshot left it: the cache it wrote names the working-copy
742
    /// change and the manifest it recorded, and every file still matches
743
    /// that manifest. `None` when a snapshot must decide (no cache, a file
744
    /// changed, conflicts or subrepos). An empty list is a clean working
745
    /// copy.
746
    pub fn status_by_manifest(&self) -> Result<Option<Vec<PathStatus>>> {
747
        // A listing left by an earlier check describes the files as they
748
        // were then; this check makes its own.
749
        self.listing_cache.take();
750
        let Some((conflicted, uncommitted, m)) = self.read_manifest()? else {
751
            return Ok(None);
752
        };
753
        if !uncommitted {
754
            return Ok(self.clean_by_manifest()?.then(Vec::new));
755
        }
756
        let _t = crate::timing::phase("status: by the status cache");
757
        if conflicted || self.root.join(".oxymodules").exists() {
758
            return Ok(None);
759
        }
760
        let Some((wc, stamp, statuses)) = fs::read(self.status_cache_path())
761
            .ok()
762
            .and_then(|b| decode_status_cache(&b))
763
        else {
764
            return Ok(None);
765
        };
766
        let now = self.view()?.workspaces.get(&self.workspace).copied();
767
        if now != Some(wc) || self.manifest_stamp() != Some(stamp) {
768
            return Ok(None);
769
        }
770
        if self.disk_differs(&m, true)? {
771
            return Ok(None);
772
        }
773
        // Test builds check the cache against a snapshot of the files.
774
        #[cfg(test)]
775
        {
776
            let snap = self.snapshot_status("status cache check")?;
777
            let key = |v: &[PathStatus]| {
778
                let mut k: Vec<(String, u8)> = v
779
                    .iter()
780
                    .map(|p| (p.path.clone(), status_byte(p.status)))
781
                    .collect();
782
                k.sort();
783
                k
784
            };
785
            assert_eq!(
786
                key(&statuses),
787
                key(&snap.statuses),
788
                "status cache disagrees with a snapshot"
789
            );
790
        }
791
        Ok(Some(statuses))
792
    }
793
794
    fn status_cache_path(&self) -> PathBuf {
795
        self.manifest_path().with_file_name("status")
796
    }
797
798
    /// The manifest file's checksum, which names its content.
799
    fn manifest_stamp(&self) -> Option<[u8; 32]> {
800
        use std::io::Read;
801
        let mut head = [0u8; MANIFEST_SUM.end];
802
        fs::File::open(self.manifest_path())
803
            .and_then(|mut f| f.read_exact(&mut head))
804
            .ok()?;
805
        head[MANIFEST_SUM].try_into().ok()
806
    }
807
808
    /// Record `statuses` as the status of working-copy change `wc` against
809
    /// the manifest as it is now; with no uncommitted work there is nothing
810
    /// to record, and the cache goes.
811
    pub(crate) fn write_status_cache(&self, wc: Option<&ChangeHash>, statuses: &[PathStatus]) {
812
        let path = self.status_cache_path();
813
        let (Some(wc), Some(stamp)) = (wc, self.manifest_stamp()) else {
814
            let _ = fs::remove_file(&path);
815
            return;
816
        };
817
        let _ = write_cache(&path, &encode_status_cache(wc, &stamp, statuses));
818
    }
819
820
    /// Whether the working copy differs from the last manifest. Without a
821
    /// manifest the answer is `None`.
822
    pub fn quick_dirty(&self) -> Result<Option<bool>> {
823
        self.quick_dirty_keeping(false)
824
    }
825
826
    /// `quick_dirty`; with `keep`, the listing it made is left for the
827
    /// next walk of the working copy.
828
    fn quick_dirty_keeping(&self, keep: bool) -> Result<Option<bool>> {
829
        let Some((_, uncommitted, manifest)) = self.read_manifest()? else {
830
            return Ok(None);
831
        };
832
        if uncommitted {
833
            return Ok(Some(true));
834
        }
835
        Ok(Some(self.disk_differs(&manifest, keep)?))
836
    }
837
838
    /// Whether the working copy differs from `manifest`: a file added,
839
    /// gone or changed. With `keep`, the listing made is left for the next
840
    /// walk of the working copy when it does.
841
    fn disk_differs(&self, manifest: &Manifest, keep: bool) -> Result<bool> {
842
        // A file written within the racy window can change without its size
843
        // or time moving (a coarse clock): those entries are hashed. One
844
        // that changed still lets the directory check below hand its stats
845
        // to the snapshot that follows.
846
        let racy = self.racy_change(manifest).is_some();
847
        if let Some(differs) = self.disk_differs_by_dirs_keeping(manifest, keep, racy) {
848
            return Ok(differs || racy);
849
        }
850
        if racy {
851
            return Ok(true);
852
        }
853
        self.disk_differs_listing(manifest, keep)
854
    }
855
856
    /// The manifest's entries that are subrepos (nested repositories it
857
    /// tracks): a directory, compared by its pin, not its stat.
858
    /// Known from the walk's nested repositories, and from `.oxymodules`:
859
    /// a commit's manifest does not list the nested repositories.
860
    fn subrepo_entries<'m>(&self, manifest: &'m Manifest) -> Vec<(&'m String, &'m ManifestEntry)> {
861
        let modules = self.modules_on_disk();
862
        let mut out: Vec<(&'m String, &'m ManifestEntry)> = manifest
863
            .nested
864
            .iter()
865
            .map(String::as_str)
866
            .chain(modules.entries.keys().map(String::as_str))
867
            .filter_map(|n| manifest.entries.get_key_value(n))
868
            .collect();
869
        out.sort_by(|a, b| a.0.cmp(b.0));
870
        out.dedup_by(|a, b| a.0 == b.0);
871
        out
872
    }
873
874
    /// Whether a subrepo is at another pin than the manifest recorded: its
875
    /// pin now (the nested repository's base, or Git's `HEAD`), encoded as
876
    /// a snapshot records it, against the recorded hash. Absent, it keeps
877
    /// its recorded pin, as a snapshot does; unreadable, it counts as moved.
878
    fn subrepo_moved(&self, rel: &str, me: &ManifestEntry) -> bool {
879
        let dir = self.root.join(rel);
880
        if !(dir.join(OXY_DIR).exists() || dir.join(".git").exists()) {
881
            return false;
882
        }
883
        let modules = self.modules_on_disk();
884
        let hint = modules.entries.get(rel).and_then(|m| m.bookmark.clone());
885
        match super::subrepos::read_pin(&dir, hint.as_deref()) {
886
            // A commit's manifest names the pin by its blob (its hash is of
887
            // no bytes); a snapshot's by the bytes' hash.
888
            Ok(pin) => match me.blob {
889
                Some(b) => Repo::blob_ref(&pin.encode()) != b,
890
                None => *blake3::hash(&pin.encode()).as_bytes() != me.hash,
891
            },
892
            Err(_) => true,
893
        }
894
    }
895
896
    /// [`Repo::disk_differs`] without listing the tree, when the manifest
897
    /// knows its directories: a directory's time moves when an entry is
898
    /// added, removed or renamed in it, so with every directory's time as
899
    /// recorded (and none within the racy window) the files are the
900
    /// manifest's, and each is checked by its stat. `None` when a
901
    /// directory changed or the manifest has none: the tree must be listed.
902
    #[cfg(test)]
903
    pub(crate) fn disk_differs_by_dirs(&self, manifest: &Manifest) -> Option<bool> {
904
        self.disk_differs_by_dirs_keeping(manifest, false, false)
905
    }
906
907
    /// [`Repo::disk_differs_by_dirs`]; with `keep`, when a file differs
908
    /// (or `racy` says one does), the stats taken become the listing of the
909
    /// snapshot that follows: no directory's entries changed, so the
910
    /// manifest's files are the tree's.
911
    fn disk_differs_by_dirs_keeping(
912
        &self,
913
        manifest: &Manifest,
914
        keep: bool,
915
        racy: bool,
916
    ) -> Option<bool> {
917
        use rayon::prelude::*;
918
        if manifest.dirs.is_empty() || !self.config.sparse_paths.is_empty() {
919
            return None;
920
        }
921
        let _t = crate::timing::phase("status: directories and files by stat");
922
        let root = &self.root;
923
        let dirs_same = manifest.dirs.par_iter().all(|(rel, t)| {
924
            *t + 2_000_000_000 < manifest.written_ns
925
                && fs::metadata(root.join(rel)).is_ok_and(|m| m.is_dir() && mtime_ns(&m) == *t)
926
        });
927
        if !dirs_same {
928
            return None;
929
        }
930
        // Subrepos are directories, compared by their pins below.
931
        let subrepos = self.subrepo_entries(manifest);
932
        let is_subrepo = |rel: &str| subrepos.iter().any(|(r, _)| r.as_str() == rel);
933
        let tracked: Vec<(&String, &ManifestEntry)> = manifest
934
            .entries
935
            .iter()
936
            .filter(|(rel, _)| !is_subrepo(rel))
937
            .collect();
938
        let rels: Vec<&String> = tracked.iter().map(|(r, _)| *r).collect();
939
        let now = stat_tracked(root, &rels);
940
        let differs_from = |me: &ManifestEntry, rel: &str, now: &Tracked| match now {
941
            Tracked::Link(t) => *blake3::hash(t).as_bytes() != me.hash,
942
            Tracked::File(m) => {
943
                m.size != me.size
944
                    || m.exec != me.exec
945
                    || (m.mtime_ns != me.mtime_ns
946
                        && !fs::read(root.join(rel))
947
                            .is_ok_and(|b| *blake3::hash(&b).as_bytes() == me.hash))
948
            }
949
        };
950
        // A path gone or no longer a file: its directory moved, unless
951
        // within a clock tick; list the tree to be sure.
952
        if now.iter().any(Option::is_none) {
953
            return None;
954
        }
955
        // The entries in the order `rels` was taken in: no lookup by path.
956
        let entries: Vec<&ManifestEntry> = tracked.iter().map(|(_, me)| *me).collect();
957
        let differs = rels
958
            .par_iter()
959
            .zip(entries.par_iter())
960
            .zip(now.par_iter())
961
            .any(|((rel, me), n)| differs_from(me, rel, n.as_ref().unwrap()))
962
            || subrepos.iter().any(|(rel, me)| self.subrepo_moved(rel, me));
963
        if keep && (differs || racy) {
964
            let mut listing = super::walk::Listing {
965
                dirs: manifest.dirs.clone(),
966
                nested: manifest.nested.iter().map(|n| root.join(n)).collect(),
967
                ..Default::default()
968
            };
969
            for (rel, n) in rels.into_iter().zip(now) {
970
                match n.unwrap() {
971
                    Tracked::File(m) => listing.files.push((rel.clone(), root.join(rel), m)),
972
                    Tracked::Link(t) => listing.symlinks.push((rel.clone(), t)),
973
                }
974
            }
975
            *self.listing_cache.borrow_mut() = Some(listing);
976
        }
977
        if !differs {
978
            // Nested repositories the manifest has no entry for are not
979
            // subrepos: nothing in them is tracked (oxygen#43).
980
            *self.untracked_nested.borrow_mut() = manifest
981
                .nested
982
                .iter()
983
                .filter(|rel| !manifest.entries.contains_key(*rel))
984
                .cloned()
985
                .collect();
986
        }
987
        Some(differs)
988
    }
989
990
    /// [`Repo::disk_differs`] by listing the tree.
991
    fn disk_differs_listing(&self, manifest: &Manifest, keep: bool) -> Result<bool> {
992
        use rayon::prelude::*;
993
        let sparse = &self.config.sparse_paths;
994
        let in_scope = |p: &str| {
995
            sparse.is_empty()
996
                || sparse
997
                    .iter()
998
                    .any(|s| p == s || p.starts_with(&format!("{s}/")))
999
        };
1000
        // A working copy that cannot be listed counts as changed.
1001
        // Known files are not matched against the ignore rules: with no
1002
        // uncommitted work (the clean check), the manifest's files are the
1003
        // base's, and a prompt's answer is the same either way.
1004
        let known = |p: &str| manifest.entries.contains_key(p);
1005
        let Ok(listing) = super::walk::list_knowing(&self.root, &in_scope, &known) else {
1006
            return Ok(true);
1007
        };
1008
        let differs = || -> Result<bool> {
1009
            let links_differ = listing.symlinks.iter().any(|(rel, target)| {
1010
                manifest
1011
                    .entries
1012
                    .get(rel)
1013
                    .is_none_or(|me| *blake3::hash(target).as_bytes() != me.hash)
1014
            });
1015
            if links_differ {
1016
                return Ok(true);
1017
            }
1018
            // Each file is checked on its own, in parallel, stopping at the
1019
            // first difference; an unreadable one counts as different.
1020
            let files_differ = listing.files.par_iter().any(|(rel, path, m)| {
1021
                manifest.entries.get(rel).is_none_or(|me| {
1022
                    m.size != me.size
1023
                        || m.exec != me.exec
1024
                        || (m.mtime_ns != me.mtime_ns
1025
                            && !fs::read(path)
1026
                                .is_ok_and(|b| *blake3::hash(&b).as_bytes() == me.hash))
1027
                })
1028
            });
1029
            if files_differ {
1030
                return Ok(true);
1031
            }
1032
            let seen = listing.files.len() + listing.symlinks.len();
1033
            if seen == manifest.entries.len() {
1034
                return Ok(false);
1035
            }
1036
            // Manifest entries the walk skipped: tracked files that ignore
1037
            // rules hide (Linux tracks some its own .gitignore names). Found
1038
            // by merging the two sorted lists; they count as present when
1039
            // still on disk and unchanged, checked on every core.
1040
            let mut listed: Vec<&str> = listing
1041
                .files
1042
                .iter()
1043
                .map(|(r, _, _)| r.as_str())
1044
                .chain(listing.symlinks.iter().map(|(r, _)| r.as_str()))
1045
                .collect();
1046
            listed.sort_unstable();
1047
            let mut at = listed.iter().peekable();
1048
            let skipped: Vec<(&String, &ManifestEntry)> = manifest
1049
                .entries
1050
                .iter()
1051
                .filter(|(rel, _)| {
1052
                    while at.next_if(|l| **l < rel.as_str()).is_some() {}
1053
                    at.peek().is_none_or(|l| **l != rel.as_str())
1054
                })
1055
                .collect();
1056
            let root = &self.root;
1057
            // Subrepos are not listed as files: compared by their pins.
1058
            let subrepos = self.subrepo_entries(manifest);
1059
            if subrepos.iter().any(|(rel, me)| self.subrepo_moved(rel, me)) {
1060
                return Ok(true);
1061
            }
1062
            let skipped: Vec<_> = skipped
1063
                .into_iter()
1064
                .filter(|(rel, _)| !subrepos.iter().any(|(r, _)| r == rel))
1065
                .collect();
1066
            Ok(skipped.par_iter().any(|(rel, me)| {
1067
                let path = root.join(rel.as_str());
1068
                let Ok(m) = fs::symlink_metadata(&path) else {
1069
                    return true;
1070
                };
1071
                !m.file_type().is_symlink()
1072
                    && (m.len() != me.size
1073
                        || super::is_executable(&m) != me.exec
1074
                        || (mtime_ns(&m) != me.mtime_ns
1075
                            && !fs::read(&path)
1076
                                .is_ok_and(|b| *blake3::hash(&b).as_bytes() == me.hash)))
1077
            }))
1078
        };
1079
        let dirty = differs()?;
1080
        if !dirty {
1081
            // Nested repositories the manifest has no entry for are not
1082
            // subrepos: nothing in them is tracked (oxygen#43).
1083
            let mut nested: Vec<String> = listing
1084
                .nested
1085
                .iter()
1086
                .map(|d| self.rel_path(d))
1087
                .filter(|rel| !manifest.entries.contains_key(rel))
1088
                .collect();
1089
            nested.sort();
1090
            *self.untracked_nested.borrow_mut() = nested;
1091
            // A complete list of the directories, each timed before it was
1092
            // read, for a restamp to keep (see `clean_by_manifest`).
1093
            *self.listed_dirs.borrow_mut() = Some((
1094
                listing.dirs.clone(),
1095
                listing.nested.iter().map(|d| self.rel_path(d)).collect(),
1096
            ));
1097
        }
1098
        if dirty && keep {
1099
            *self.listing_cache.borrow_mut() = Some(listing);
1100
        }
1101
        Ok(dirty)
1102
    }
1103
1104
    /// Prompt information without recording anything.
1105
    pub fn quick_status(&self) -> Result<PromptInfo> {
1106
        let view = self.view()?;
1107
        let bookmark = self.config.bookmark.clone();
1108
        let heads = self.heads_of(&view, &bookmark)?;
1109
        let wc_hash = view.workspaces.get(&self.workspace).copied();
1110
        let change_id = match wc_hash {
1111
            Some(h) => self.store.get_change_meta(&h)?.change_id.short(8),
1112
            None => String::new(),
1113
        };
1114
        // Conflictedness was recorded by the last snapshot; without a
1115
        // manifest (never snapshotted) both flags are unknown and shown clear.
1116
        let manifest = self.read_manifest()?;
1117
        let conflicted = manifest.as_ref().map(|(c, _, _)| *c).unwrap_or(false);
1118
        let dirty = self.quick_dirty()?.unwrap_or(false);
1119
        let (ahead, behind) = match view
1120
            .remotes
1121
            .get(&self.default_remote())
1122
            .and_then(|b| b.get(&bookmark))
1123
        {
1124
            Some(st) => {
1125
                let remote_heads = self.store.get_state(st)?.heads;
1126
                let ahead = self.negotiate(&remote_heads, &heads).map(|v| v.len()).ok();
1127
                let behind = self.negotiate(&heads, &remote_heads).map(|v| v.len()).ok();
1128
                (ahead, behind)
1129
            }
1130
            None => (None, None),
1131
        };
1132
        Ok(PromptInfo {
1133
            bookmark,
1134
            change_id,
1135
            heads: heads.len(),
1136
            dirty,
1137
            conflicted,
1138
            ahead,
1139
            behind,
1140
        })
1141
    }
1142
}
1143
1144
#[cfg(test)]
1145
mod tests {
1146
    use super::*;
1147
1148
    #[test]
1149
    fn placeholders_and_glyphs() {
1150
        let clean = PromptInfo {
1151
            bookmark: "main".into(),
1152
            change_id: "658990aa".into(),
1153
            heads: 1,
1154
            dirty: false,
1155
            conflicted: false,
1156
            ahead: Some(0),
1157
            behind: None,
1158
        };
1159
        assert_eq!(clean.format(DEFAULT_PROMPT_FORMAT), "main 658990aa");
1160
        assert_eq!(clean.format(GLYPH_PROMPT_FORMAT), "⚛ main 658990aa ✔");
1161
        let busy = PromptInfo {
1162
            dirty: true,
1163
            ahead: Some(2),
1164
            behind: Some(3),
1165
            ..clean.clone()
1166
        };
1167
        assert_eq!(busy.format(DEFAULT_PROMPT_FORMAT), "main 658990aa*+2-3");
1168
        assert_eq!(busy.format(GLYPH_PROMPT_FORMAT), "⚛ main 658990aa ✎↑2↓3");
1169
        let bad = PromptInfo {
1170
            conflicted: true,
1171
            ..busy.clone()
1172
        };
1173
        assert_eq!(bad.format("%s%x%d %h %a/%B %%"), "‼!* 1 2/3 %");
1174
        assert_eq!(bad.state_color(), 31);
1175
        assert_eq!(
1176
            busy.format_colored("%b", ("\\[", "\\]")),
1177
            "\\[\x1b[33m\\]main\\[\x1b[0m\\]"
1178
        );
1179
        assert_eq!(clean.format_colored("%b", ("", "")), "\x1b[36mmain\x1b[0m");
1180
        assert_eq!(clean.format("%q"), "%q");
1181
    }
1182
}