OXYGENHUB
# c3d2ef6104c2 oxygen / crates / oxy-core / src / repo / prompt.rs
24.0 KiB · Rust · last changed Projects coming from Git: .gitignore honoured, nested Git repositories named (closes #43, closes #44) 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
}
150
151
/// A parsed manifest and the identity (size, time, inode) of the file it
152
/// came from: reused while the file is the same one.
153
pub(crate) type CachedManifest = ((u64, u128, u64), (bool, bool, std::sync::Arc<Manifest>));
154
155
pub(crate) fn mtime_ns(m: &fs::Metadata) -> u128 {
156
    m.modified()
157
        .ok()
158
        .and_then(|t| t.duration_since(UNIX_EPOCH).ok())
159
        .map(|d| d.as_nanos())
160
        .unwrap_or(0)
161
}
162
163
pub const DEFAULT_PROMPT_FORMAT: &str = "%b %c%d%x%A%D";
164
/// The repository icon for `%i` (an atom, for oxygen).
165
pub const ICON: &str = "⚛";
166
/// A format with glyphs: icon, bookmark, change id, state, ahead/behind.
167
pub const GLYPH_PROMPT_FORMAT: &str = "%i %b %c %s%U%O";
168
169
/// The manifest file (oxygen#41): a flat binary layout read in one pass.
170
/// A magic number; the conflicted and uncommitted flags; when it was
171
/// written, at a fixed offset so an otherwise equal manifest can be
172
/// recognised; BLAKE3 of everything else; then each entry in path order:
173
/// path, size, mtime in nanoseconds, BLAKE3 of the content, the stored
174
/// blob of an opaque file, the graph key it was found equal to, and
175
/// whether it is executable. A file that does not check, or in any other
176
/// layout (an older manifest), reads as no manifest, and the next snapshot
177
/// writes it again: it is a cache, written without waiting for the disk.
178
const MANIFEST_MAGIC: &[u8; 8] = b"oxyman2\0";
179
/// Where the written time sits: after the magic and the two flags.
180
const MANIFEST_WRITTEN: std::ops::Range<usize> = 10..26;
181
/// Where the checksum sits, after the written time.
182
const MANIFEST_SUM: std::ops::Range<usize> = 26..58;
183
184
fn manifest_sum(buf: &[u8]) -> [u8; 32] {
185
    let mut h = blake3::Hasher::new();
186
    h.update(&buf[..MANIFEST_SUM.start]);
187
    h.update_rayon(&buf[MANIFEST_SUM.end..]);
188
    *h.finalize().as_bytes()
189
}
190
191
fn encode_manifest(
192
    conflicted: bool,
193
    uncommitted: bool,
194
    written_ns: u128,
195
    entries: &mut dyn Iterator<Item = (&str, ManifestEntry)>,
196
) -> Vec<u8> {
197
    let mut w = crate::flat::Writer::default();
198
    w.fixed(MANIFEST_MAGIC);
199
    w.u8(conflicted as u8);
200
    w.u8(uncommitted as u8);
201
    w.fixed(&written_ns.to_le_bytes());
202
    w.fixed(&[0; 32]);
203
    for (path, e) in entries {
204
        w.bytes(path.as_bytes());
205
        w.u64(e.size);
206
        w.fixed(&e.mtime_ns.to_le_bytes());
207
        w.fixed(&e.hash);
208
        w.opt_object(&e.blob);
209
        match e.graph {
210
            None => w.u8(0),
211
            Some(k) => {
212
                w.u8(1);
213
                w.fixed(&k);
214
            }
215
        }
216
        w.u8(e.exec as u8);
217
    }
218
    let sum = manifest_sum(&w.0);
219
    w.0[MANIFEST_SUM].copy_from_slice(&sum);
220
    w.0
221
}
222
223
fn decode_manifest(buf: &[u8]) -> Option<(bool, bool, Manifest)> {
224
    let mut r = crate::flat::Reader::new(buf);
225
    if &r.fixed::<8>().ok()? != MANIFEST_MAGIC {
226
        return None;
227
    }
228
    let conflicted = r.u8().ok()? == 1;
229
    let uncommitted = r.u8().ok()? == 1;
230
    let written_ns = u128::from_le_bytes(r.fixed().ok()?);
231
    if r.fixed::<32>().ok()? != manifest_sum(buf) {
232
        return None;
233
    }
234
    let mut entries = Vec::new();
235
    while !r.at_end() {
236
        let path = std::str::from_utf8(r.bytes().ok()?).ok()?.to_string();
237
        let e = ManifestEntry {
238
            size: r.u64().ok()?,
239
            mtime_ns: u128::from_le_bytes(r.fixed().ok()?),
240
            hash: r.fixed().ok()?,
241
            blob: r.opt_object().ok()?,
242
            graph: match r.u8().ok()? {
243
                0 => None,
244
                _ => Some(r.fixed().ok()?),
245
            },
246
            exec: r.u8().ok()? == 1,
247
        };
248
        entries.push((path, e));
249
    }
250
    // Written in path order, so this builds the map without searching.
251
    let entries = entries.into_iter().collect();
252
    Some((
253
        conflicted,
254
        uncommitted,
255
        Manifest {
256
            written_ns,
257
            entries,
258
        },
259
    ))
260
}
261
262
impl Repo {
263
    pub(crate) fn manifest_path(&self) -> PathBuf {
264
        self.oxy
265
            .join("workspaces")
266
            .join(self.workspace.to_hex())
267
            .join("manifest")
268
    }
269
270
    /// Record the working copy as it is now (after a snapshot, commit, or
271
    /// checkout), with header lines saying whether the base state is
272
    /// conflicted and whether the working-copy change holds uncommitted work.
273
    pub(crate) fn write_manifest(&self, conflicted: bool, uncommitted: bool) -> Result<()> {
274
        let entries = self.walk_working_copy()?;
275
        self.write_manifest_entries(conflicted, uncommitted, &entries, &HashMap::new())
276
    }
277
278
    /// Write the manifest from entries already walked; `blobs` names the
279
    /// stored blob of each opaque path so the next walk can skip reading it.
280
    pub(crate) fn write_manifest_entries(
281
        &self,
282
        conflicted: bool,
283
        uncommitted: bool,
284
        entries: &BTreeMap<String, FsEntry>,
285
        blobs: &HashMap<String, ObjectRef>,
286
    ) -> Result<()> {
287
        self.manifest_cache.take();
288
        self.listing_cache.take();
289
        let written = std::time::SystemTime::now()
290
            .duration_since(UNIX_EPOCH)
291
            .map(|d| d.as_nanos())
292
            .unwrap_or(0);
293
        let mut newest_ns: u128 = 0;
294
        let mut rows = entries.iter().map(|(path, e)| {
295
            let (size, mtime_ns) = match e.stat {
296
                Some(known) => known,
297
                None => match fs::symlink_metadata(self.root.join(path)) {
298
                    Ok(m) => (m.len(), mtime_ns(&m)),
299
                    Err(_) => (e.content.len() as u64, 0),
300
                },
301
            };
302
            newest_ns = newest_ns.max(mtime_ns);
303
            let entry = ManifestEntry {
304
                size,
305
                mtime_ns,
306
                hash: e
307
                    .content_hash
308
                    .unwrap_or_else(|| *blake3::hash(&e.content).as_bytes()),
309
                blob: blobs.get(path).copied().or(e.blob),
310
                graph: e.graph_key,
311
                exec: e.exec,
312
            };
313
            (path.as_str(), entry)
314
        });
315
        let new = encode_manifest(conflicted, uncommitted, written, &mut rows);
316
        drop(rows);
317
        let p = self.manifest_path();
318
        // Leave an unchanged manifest alone when its stamp already trusts
319
        // every entry (none is within the racy window): rewriting it would
320
        // change nothing but the file, which editors watching the tree
321
        // (and subrepos inside it) take as a change.
322
        if let Ok(old) = fs::read(&p) {
323
            let (w, sum) = (MANIFEST_WRITTEN, MANIFEST_SUM);
324
            if old.len() == new.len()
325
                && old[..w.start] == new[..w.start]
326
                && old[sum.end..] == new[sum.end..]
327
            {
328
                let old_written = u128::from_le_bytes(old[w.clone()].try_into().unwrap());
329
                if newest_ns + 2_000_000_000 < old_written {
330
                    return Ok(());
331
                }
332
            }
333
        }
334
        fs::create_dir_all(p.parent().unwrap())?;
335
        write_cache(&p, &new)
336
    }
337
338
    /// Change the header flags, and set the graph key of each path in
339
    /// `keys` (after a commit, the new base's keys of the files it holds as
340
    /// they are on disk).
341
    pub(crate) fn rewrite_manifest(
342
        &self,
343
        conflicted: bool,
344
        uncommitted: bool,
345
        keys: &HashMap<String, [u8; 32]>,
346
    ) -> Result<()> {
347
        self.manifest_cache.take();
348
        self.listing_cache.take();
349
        let p = self.manifest_path();
350
        let Some((_, _, m)) = fs::read(&p).ok().and_then(|b| decode_manifest(&b)) else {
351
            return self.write_manifest(conflicted, uncommitted);
352
        };
353
        let mut rows = m.entries.iter().map(|(path, e)| {
354
            let mut e = e.clone();
355
            if let Some(k) = keys.get(path) {
356
                e.graph = Some(*k);
357
            }
358
            (path.as_str(), e)
359
        });
360
        write_cache(
361
            &p,
362
            &encode_manifest(conflicted, uncommitted, m.written_ns, &mut rows),
363
        )
364
    }
365
366
    /// Forget the manifest after an operation that changed the working-copy
367
    /// change without a snapshot (undo, rewrites); the prompt shows blank
368
    /// markers until the next snapshot.
369
    pub(crate) fn drop_manifest(&self) {
370
        self.manifest_cache.take();
371
        self.listing_cache.take();
372
        let _ = fs::remove_file(self.manifest_path());
373
    }
374
375
    /// The manifest, with whether it records a conflict and uncommitted
376
    /// work; parsed once while the file stays the same one.
377
    pub(crate) fn read_manifest(&self) -> Result<Option<(bool, bool, std::sync::Arc<Manifest>)>> {
378
        let path = self.manifest_path();
379
        let id = match fs::metadata(&path) {
380
            Ok(m) => {
381
                #[cfg(unix)]
382
                let ino = std::os::unix::fs::MetadataExt::ino(&m);
383
                #[cfg(not(unix))]
384
                let ino = 0;
385
                (m.len(), mtime_ns(&m), ino)
386
            }
387
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
388
            Err(e) => return Err(e.into()),
389
        };
390
        if let Some((cached_id, m)) = &*self.manifest_cache.borrow() {
391
            if *cached_id == id {
392
                return Ok(Some(m.clone()));
393
            }
394
        }
395
        let Some((c, u, m)) = self.parse_manifest(&path)? else {
396
            return Ok(None);
397
        };
398
        let m = (c, u, std::sync::Arc::new(m));
399
        *self.manifest_cache.borrow_mut() = Some((id, m.clone()));
400
        Ok(Some(m))
401
    }
402
403
    fn parse_manifest(&self, path: &std::path::Path) -> Result<Option<(bool, bool, Manifest)>> {
404
        let buf = match fs::read(path) {
405
            Ok(b) => b,
406
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
407
            Err(e) => return Err(e.into()),
408
        };
409
        Ok(decode_manifest(&buf))
410
    }
411
412
    /// Whether the working copy is certainly clean, known from the manifest
413
    /// alone, without loading any history (spec §12.4): the last snapshot
414
    /// or commit found no uncommitted work and no conflict, there are no
415
    /// subrepos (whose state the manifest does not hold), and no file was
416
    /// added, removed or changed since. An entry inside the racy window,
417
    /// where an equal size and time do not prove equal content, is checked
418
    /// by hashing the file. `false` means only that a snapshot must decide.
419
    pub fn clean_by_manifest(&self) -> Result<bool> {
420
        let _t = crate::timing::phase("status: check the manifest");
421
        // Why a snapshot must decide, with timings on.
422
        let not = |why: &str| {
423
            if crate::timing::enabled() {
424
                eprintln!("timing status: not clean by the manifest: {why}");
425
            }
426
            Ok(false)
427
        };
428
        let Some((conflicted, uncommitted, m)) = self.read_manifest()? else {
429
            return not("no manifest");
430
        };
431
        if conflicted || uncommitted || self.root.join(".oxymodules").exists() {
432
            return not("conflicted, uncommitted or subrepos");
433
        }
434
        for (rel, e) in &m.entries {
435
            if e.mtime_ns + 2_000_000_000 < m.written_ns {
436
                continue;
437
            }
438
            let path = self.root.join(rel);
439
            let same = match fs::symlink_metadata(&path) {
440
                Ok(md) if md.file_type().is_symlink() => fs::read_link(&path).is_ok_and(|t| {
441
                    *blake3::hash(t.to_string_lossy().as_bytes()).as_bytes() == e.hash
442
                }),
443
                Ok(md) if md.is_file() => {
444
                    md.len() == e.size
445
                        && fs::read(&path).is_ok_and(|b| *blake3::hash(&b).as_bytes() == e.hash)
446
                }
447
                _ => false,
448
            };
449
            if !same {
450
                return not(&format!("{rel} changed within the racy window"));
451
            }
452
        }
453
        if self.quick_dirty_keeping(true)? != Some(false) {
454
            return not("a file differs");
455
        }
456
        Ok(true)
457
    }
458
459
    /// Whether the working copy differs from the last manifest. Without a
460
    /// manifest the answer is `None`.
461
    pub fn quick_dirty(&self) -> Result<Option<bool>> {
462
        self.quick_dirty_keeping(false)
463
    }
464
465
    /// `quick_dirty`; with `keep`, the listing it made is left for the
466
    /// next walk of the working copy.
467
    fn quick_dirty_keeping(&self, keep: bool) -> Result<Option<bool>> {
468
        let Some((_, uncommitted, manifest)) = self.read_manifest()? else {
469
            return Ok(None);
470
        };
471
        if uncommitted {
472
            return Ok(Some(true));
473
        }
474
        use rayon::prelude::*;
475
        let sparse = &self.config.sparse_paths;
476
        let in_scope = |p: &str| {
477
            sparse.is_empty()
478
                || sparse
479
                    .iter()
480
                    .any(|s| p == s || p.starts_with(&format!("{s}/")))
481
        };
482
        // A working copy that cannot be listed counts as changed.
483
        // Known files are not matched against the ignore rules: with no
484
        // uncommitted work (the clean check), the manifest's files are the
485
        // base's, and a prompt's answer is the same either way.
486
        let known = |p: &str| manifest.entries.contains_key(p);
487
        let Ok(listing) = super::walk::list_knowing(&self.root, &in_scope, &known) else {
488
            return Ok(Some(true));
489
        };
490
        let differs = || -> Result<bool> {
491
            let links_differ = listing.symlinks.iter().any(|(rel, target)| {
492
                manifest
493
                    .entries
494
                    .get(rel)
495
                    .is_none_or(|me| *blake3::hash(target).as_bytes() != me.hash)
496
            });
497
            if links_differ {
498
                return Ok(true);
499
            }
500
            // Each file is checked on its own, in parallel, stopping at the
501
            // first difference; an unreadable one counts as different.
502
            let files_differ = listing.files.par_iter().any(|(rel, path, m)| {
503
                manifest.entries.get(rel).is_none_or(|me| {
504
                    m.len() != me.size
505
                        || super::is_executable(m) != me.exec
506
                        || (mtime_ns(m) != me.mtime_ns
507
                            && !fs::read(path)
508
                                .is_ok_and(|b| *blake3::hash(&b).as_bytes() == me.hash))
509
                })
510
            });
511
            if files_differ {
512
                return Ok(true);
513
            }
514
            let seen = listing.files.len() + listing.symlinks.len();
515
            if seen == manifest.entries.len() {
516
                return Ok(false);
517
            }
518
            // Manifest entries the walk skipped: tracked files that ignore
519
            // rules hide (Linux tracks some its own .gitignore names). Found
520
            // by merging the two sorted lists; they count as present when
521
            // still on disk and unchanged, checked on every core.
522
            let mut listed: Vec<&str> = listing
523
                .files
524
                .iter()
525
                .map(|(r, _, _)| r.as_str())
526
                .chain(listing.symlinks.iter().map(|(r, _)| r.as_str()))
527
                .collect();
528
            listed.sort_unstable();
529
            let mut at = listed.iter().peekable();
530
            let skipped: Vec<(&String, &ManifestEntry)> = manifest
531
                .entries
532
                .iter()
533
                .filter(|(rel, _)| {
534
                    while at.next_if(|l| **l < rel.as_str()).is_some() {}
535
                    at.peek().is_none_or(|l| **l != rel.as_str())
536
                })
537
                .collect();
538
            let root = &self.root;
539
            Ok(skipped.par_iter().any(|(rel, me)| {
540
                let path = root.join(rel.as_str());
541
                let Ok(m) = fs::symlink_metadata(&path) else {
542
                    return true;
543
                };
544
                !m.file_type().is_symlink()
545
                    && (m.len() != me.size
546
                        || super::is_executable(&m) != me.exec
547
                        || (mtime_ns(&m) != me.mtime_ns
548
                            && !fs::read(&path)
549
                                .is_ok_and(|b| *blake3::hash(&b).as_bytes() == me.hash)))
550
            }))
551
        };
552
        let dirty = differs()?;
553
        if !dirty {
554
            // Nested repositories the manifest has no entry for are not
555
            // subrepos: nothing in them is tracked (oxygen#43).
556
            *self.untracked_nested.borrow_mut() = listing
557
                .nested
558
                .iter()
559
                .map(|d| self.rel_path(d))
560
                .filter(|rel| !manifest.entries.contains_key(rel))
561
                .collect();
562
        }
563
        if dirty && keep {
564
            *self.listing_cache.borrow_mut() = Some(listing);
565
        }
566
        Ok(Some(dirty))
567
    }
568
569
    /// Prompt information without recording anything.
570
    pub fn quick_status(&self) -> Result<PromptInfo> {
571
        let view = self.view()?;
572
        let bookmark = self.config.bookmark.clone();
573
        let heads = self.heads_of(&view, &bookmark)?;
574
        let wc_hash = view.workspaces.get(&self.workspace).copied();
575
        let change_id = match wc_hash {
576
            Some(h) => self.store.get_change_meta(&h)?.change_id.short(8),
577
            None => String::new(),
578
        };
579
        // Conflictedness was recorded by the last snapshot; without a
580
        // manifest (never snapshotted) both flags are unknown and shown clear.
581
        let manifest = self.read_manifest()?;
582
        let conflicted = manifest.as_ref().map(|(c, _, _)| *c).unwrap_or(false);
583
        let dirty = self.quick_dirty()?.unwrap_or(false);
584
        let (ahead, behind) = match view.remotes.get("origin").and_then(|b| b.get(&bookmark)) {
585
            Some(st) => {
586
                let remote_heads = self.store.get_state(st)?.heads;
587
                let ahead = self.negotiate(&remote_heads, &heads).map(|v| v.len()).ok();
588
                let behind = self.negotiate(&heads, &remote_heads).map(|v| v.len()).ok();
589
                (ahead, behind)
590
            }
591
            None => (None, None),
592
        };
593
        Ok(PromptInfo {
594
            bookmark,
595
            change_id,
596
            heads: heads.len(),
597
            dirty,
598
            conflicted,
599
            ahead,
600
            behind,
601
        })
602
    }
603
}
604
605
#[cfg(test)]
606
mod tests {
607
    use super::*;
608
609
    #[test]
610
    fn placeholders_and_glyphs() {
611
        let clean = PromptInfo {
612
            bookmark: "main".into(),
613
            change_id: "658990aa".into(),
614
            heads: 1,
615
            dirty: false,
616
            conflicted: false,
617
            ahead: Some(0),
618
            behind: None,
619
        };
620
        assert_eq!(clean.format(DEFAULT_PROMPT_FORMAT), "main 658990aa");
621
        assert_eq!(clean.format(GLYPH_PROMPT_FORMAT), "⚛ main 658990aa ✔");
622
        let busy = PromptInfo {
623
            dirty: true,
624
            ahead: Some(2),
625
            behind: Some(3),
626
            ..clean.clone()
627
        };
628
        assert_eq!(busy.format(DEFAULT_PROMPT_FORMAT), "main 658990aa*+2-3");
629
        assert_eq!(busy.format(GLYPH_PROMPT_FORMAT), "⚛ main 658990aa ✎↑2↓3");
630
        let bad = PromptInfo {
631
            conflicted: true,
632
            ..busy.clone()
633
        };
634
        assert_eq!(bad.format("%s%x%d %h %a/%B %%"), "‼!* 1 2/3 %");
635
        assert_eq!(bad.state_color(), 31);
636
        assert_eq!(
637
            busy.format_colored("%b", ("\\[", "\\]")),
638
            "\\[\x1b[33m\\]main\\[\x1b[0m\\]"
639
        );
640
        assert_eq!(clean.format_colored("%b", ("", "")), "\x1b[36mmain\x1b[0m");
641
        assert_eq!(clean.format("%q"), "%q");
642
    }
643
}