| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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 |
|
| 17 |
pub change_id: String, |
| 18 |
pub heads: usize, |
| 19 |
pub dirty: bool, |
| 20 |
pub conflicted: bool, |
| 21 |
|
| 22 |
pub ahead: Option<usize>, |
| 23 |
pub behind: Option<usize>, |
| 24 |
} |
| 25 |
|
| 26 |
impl PromptInfo { |
| 27 |
|
| 28 |
|
| 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 |
|
| 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 |
|
| 51 |
|
| 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 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 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 |
|
| 128 |
|
| 129 |
|
| 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 |
|
| 137 |
|
| 138 |
pub graph: Option<[u8; 32]>, |
| 139 |
|
| 140 |
|
| 141 |
pub exec: bool, |
| 142 |
} |
| 143 |
|
| 144 |
#[derive(Clone, Debug, Default)] |
| 145 |
pub(crate) struct Manifest { |
| 146 |
|
| 147 |
pub written_ns: u128, |
| 148 |
pub entries: BTreeMap<String, ManifestEntry>, |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 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 |
|
| 165 |
pub const ICON: &str = "⚛"; |
| 166 |
|
| 167 |
pub const GLYPH_PROMPT_FORMAT: &str = "%i %b %c %s%U%O"; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
const MANIFEST_MAGIC: &[u8; 8] = b"oxyman2\0"; |
| 179 |
|
| 180 |
const MANIFEST_WRITTEN: std::ops::Range<usize> = 10..26; |
| 181 |
|
| 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 |
|
| 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 |
|
| 271 |
|
| 272 |
|
| 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 |
|
| 279 |
|
| 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 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 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 |
|
| 339 |
|
| 340 |
|
| 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 |
|
| 367 |
|
| 368 |
|
| 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 |
|
| 376 |
|
| 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 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
pub fn clean_by_manifest(&self) -> Result<bool> { |
| 420 |
let _t = crate::timing::phase("status: check the manifest"); |
| 421 |
|
| 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 |
|
| 460 |
|
| 461 |
pub fn quick_dirty(&self) -> Result<Option<bool>> { |
| 462 |
self.quick_dirty_keeping(false) |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 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 |
|
| 483 |
let Ok(listing) = super::walk::list(&self.root, &in_scope) else { |
| 484 |
return Ok(Some(true)); |
| 485 |
}; |
| 486 |
let differs = || -> Result<bool> { |
| 487 |
let links_differ = listing.symlinks.iter().any(|(rel, target)| { |
| 488 |
manifest |
| 489 |
.entries |
| 490 |
.get(rel) |
| 491 |
.is_none_or(|me| *blake3::hash(target).as_bytes() != me.hash) |
| 492 |
}); |
| 493 |
if links_differ { |
| 494 |
return Ok(true); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
let files_differ = listing.files.par_iter().any(|(rel, path, m)| { |
| 499 |
manifest.entries.get(rel).is_none_or(|me| { |
| 500 |
m.len() != me.size |
| 501 |
|| super::is_executable(m) != me.exec |
| 502 |
|| (mtime_ns(m) != me.mtime_ns |
| 503 |
&& !fs::read(path) |
| 504 |
.is_ok_and(|b| *blake3::hash(&b).as_bytes() == me.hash)) |
| 505 |
}) |
| 506 |
}); |
| 507 |
if files_differ { |
| 508 |
return Ok(true); |
| 509 |
} |
| 510 |
let seen = listing.files.len() + listing.symlinks.len(); |
| 511 |
if seen == manifest.entries.len() { |
| 512 |
return Ok(false); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
for (rel, me) in &manifest.entries { |
| 517 |
let path = self.root.join(rel); |
| 518 |
let Ok(m) = fs::symlink_metadata(&path) else { |
| 519 |
return Ok(true); |
| 520 |
}; |
| 521 |
if m.file_type().is_symlink() { |
| 522 |
continue; |
| 523 |
} |
| 524 |
if m.len() != me.size |
| 525 |
|| super::is_executable(&m) != me.exec |
| 526 |
|| (mtime_ns(&m) != me.mtime_ns |
| 527 |
&& *blake3::hash(&fs::read(&path)?).as_bytes() != me.hash) |
| 528 |
{ |
| 529 |
return Ok(true); |
| 530 |
} |
| 531 |
} |
| 532 |
Ok(false) |
| 533 |
}; |
| 534 |
let dirty = differs()?; |
| 535 |
if dirty && keep { |
| 536 |
*self.listing_cache.borrow_mut() = Some(listing); |
| 537 |
} |
| 538 |
Ok(Some(dirty)) |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
pub fn quick_status(&self) -> Result<PromptInfo> { |
| 543 |
let view = self.view()?; |
| 544 |
let bookmark = self.config.bookmark.clone(); |
| 545 |
let heads = self.heads_of(&view, &bookmark)?; |
| 546 |
let wc_hash = view.workspaces.get(&self.workspace).copied(); |
| 547 |
let change_id = match wc_hash { |
| 548 |
Some(h) => self.store.get_change_meta(&h)?.change_id.short(8), |
| 549 |
None => String::new(), |
| 550 |
}; |
| 551 |
|
| 552 |
|
| 553 |
let manifest = self.read_manifest()?; |
| 554 |
let conflicted = manifest.as_ref().map(|(c, _, _)| *c).unwrap_or(false); |
| 555 |
let dirty = self.quick_dirty()?.unwrap_or(false); |
| 556 |
let (ahead, behind) = match view.remotes.get("origin").and_then(|b| b.get(&bookmark)) { |
| 557 |
Some(st) => { |
| 558 |
let remote_heads = self.store.get_state(st)?.heads; |
| 559 |
let ahead = self.negotiate(&remote_heads, &heads).map(|v| v.len()).ok(); |
| 560 |
let behind = self.negotiate(&heads, &remote_heads).map(|v| v.len()).ok(); |
| 561 |
(ahead, behind) |
| 562 |
} |
| 563 |
None => (None, None), |
| 564 |
}; |
| 565 |
Ok(PromptInfo { |
| 566 |
bookmark, |
| 567 |
change_id, |
| 568 |
heads: heads.len(), |
| 569 |
dirty, |
| 570 |
conflicted, |
| 571 |
ahead, |
| 572 |
behind, |
| 573 |
}) |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
#[cfg(test)] |
| 578 |
mod tests { |
| 579 |
use super::*; |
| 580 |
|
| 581 |
#[test] |
| 582 |
fn placeholders_and_glyphs() { |
| 583 |
let clean = PromptInfo { |
| 584 |
bookmark: "main".into(), |
| 585 |
change_id: "658990aa".into(), |
| 586 |
heads: 1, |
| 587 |
dirty: false, |
| 588 |
conflicted: false, |
| 589 |
ahead: Some(0), |
| 590 |
behind: None, |
| 591 |
}; |
| 592 |
assert_eq!(clean.format(DEFAULT_PROMPT_FORMAT), "main 658990aa"); |
| 593 |
assert_eq!(clean.format(GLYPH_PROMPT_FORMAT), "⚛ main 658990aa ✔"); |
| 594 |
let busy = PromptInfo { |
| 595 |
dirty: true, |
| 596 |
ahead: Some(2), |
| 597 |
behind: Some(3), |
| 598 |
..clean.clone() |
| 599 |
}; |
| 600 |
assert_eq!(busy.format(DEFAULT_PROMPT_FORMAT), "main 658990aa*+2-3"); |
| 601 |
assert_eq!(busy.format(GLYPH_PROMPT_FORMAT), "⚛ main 658990aa ✎↑2↓3"); |
| 602 |
let bad = PromptInfo { |
| 603 |
conflicted: true, |
| 604 |
..busy.clone() |
| 605 |
}; |
| 606 |
assert_eq!(bad.format("%s%x%d %h %a/%B %%"), "‼!* 1 2/3 %"); |
| 607 |
assert_eq!(bad.state_color(), 31); |
| 608 |
assert_eq!( |
| 609 |
busy.format_colored("%b", ("\\[", "\\]")), |
| 610 |
"\\[\x1b[33m\\]main\\[\x1b[0m\\]" |
| 611 |
); |
| 612 |
assert_eq!(clean.format_colored("%b", ("", "")), "\x1b[36mmain\x1b[0m"); |
| 613 |
assert_eq!(clean.format("%q"), "%q"); |
| 614 |
} |
| 615 |
} |