| fce44ae9 Andy McCall ↶ |
1 |
|
| 2 |
|
| 94084087 Andy McCall ↶ |
3 |
use super::ui; |
| fce44ae9 Andy McCall ↶ |
4 |
use anyhow::Result; |
| 5 |
use oxy_core::hash::ChangeHash; |
| 6 |
use oxy_core::objects::Change; |
| 7 |
use oxy_core::repo::{Repo, SignatureStatus}; |
| 2863456a Andy McCall ↶ |
8 |
use oxy_core::state::ChangeMeta; |
| fce44ae9 Andy McCall ↶ |
9 |
|
| 10 |
pub fn short_id(c: &Change) -> String { |
| 11 |
c.change_id.short(8) |
| 12 |
} |
| 13 |
|
| 2863456a Andy McCall ↶ |
14 |
pub fn short_meta_id(c: &ChangeMeta) -> String { |
| 15 |
c.change_id.short(8) |
| 16 |
} |
| 17 |
|
| fce44ae9 Andy McCall ↶ |
18 |
|
| 19 |
pub fn plural(n: usize, word: &str) -> String { |
| 20 |
format!("{n} {word}{}", if n == 1 { "" } else { "s" }) |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
pub fn when(ms: u64) -> String { |
| 25 |
let secs = (ms / 1000) as i64; |
| 26 |
let days = secs.div_euclid(86_400); |
| 27 |
let rem = secs.rem_euclid(86_400); |
| 28 |
|
| 29 |
let z = days + 719_468; |
| 30 |
let era = z.div_euclid(146_097); |
| 31 |
let doe = z - era * 146_097; |
| 32 |
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; |
| 33 |
let y = yoe + era * 400; |
| 34 |
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); |
| 35 |
let mp = (5 * doy + 2) / 153; |
| 36 |
let d = doy - (153 * mp + 2) / 5 + 1; |
| 37 |
let m = if mp < 10 { mp + 3 } else { mp - 9 }; |
| 38 |
let y = if m <= 2 { y + 1 } else { y }; |
| 39 |
format!("{y:04}-{m:02}-{d:02} {:02}:{:02}:{:02}Z", rem / 3600, (rem % 3600) / 60, rem % 60) |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
pub fn signature_line(repo: &Repo, h: &ChangeHash) -> Result<String> { |
| 44 |
Ok(match repo.authenticate_change(h)? { |
| 45 |
SignatureStatus::Unsigned => "unsigned".to_string(), |
| 28fc23d7 Andy McCall ↶ |
46 |
SignatureStatus::Valid { key, name, email, sealed, revoked } => { |
| 47 |
format!("{} by {name} <{email}> key {}{}", if sealed { "sealed" } else { "signed" }, key.short(12), if revoked { " (KEY REVOKED)" } else { "" }) |
| fce44ae9 Andy McCall ↶ |
48 |
} |
| 49 |
SignatureStatus::Invalid(why) => format!("INVALID signature: {why}"), |
| 50 |
}) |
| 51 |
} |
| 52 |
|
| 94084087 Andy McCall ↶ |
53 |
|
| 54 |
pub fn signature_mark(repo: &Repo, h: &ChangeHash) -> Result<String> { |
| 55 |
Ok(match repo.authenticate_change(h)? { |
| 56 |
SignatureStatus::Unsigned => ui::dim(format!("{} unsigned", ui::glyph("·", "-"))), |
| 28fc23d7 Andy McCall ↶ |
57 |
SignatureStatus::Valid { sealed, revoked: true, .. } => ui::warn(format!("{} {} (key revoked)", ui::glyph("⚠", "!"), if sealed { "sealed" } else { "signed" })), |
| 94084087 Andy McCall ↶ |
58 |
SignatureStatus::Valid { sealed, .. } => ui::ok(format!("{} {}", ui::glyph("✔", "v"), if sealed { "sealed" } else { "signed" })), |
| 59 |
SignatureStatus::Invalid(why) => ui::err(format!("{} INVALID: {why}", ui::glyph("✖", "x"))), |
| 60 |
}) |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
pub fn author_name(repo: &Repo, c: &ChangeMeta) -> String { |
| 66 |
if !c.author.is_zero() { |
| 67 |
if let Ok(id) = repo.identity(&c.author) { |
| 68 |
return id.name; |
| 69 |
} |
| 70 |
return c.author.short(12); |
| 71 |
} |
| 72 |
c.message |
| 73 |
.lines() |
| 74 |
.rev() |
| 75 |
.find_map(|l| l.strip_prefix("Git-Author: ")) |
| 76 |
.map(|a| a.split('<').next().unwrap_or(a).trim().to_string()) |
| 77 |
.unwrap_or_else(|| "unknown".into()) |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
pub fn subject_of(message: &str) -> &str { |
| 82 |
match message.lines().next() { |
| 83 |
Some(l) if !l.trim().is_empty() => l, |
| 84 |
_ => "(no description)", |
| 85 |
} |
| 86 |
} |
| 87 |
|
| fce44ae9 Andy McCall ↶ |
88 |
pub fn deps_line(c: &Change) -> String { |
| 2863456a Andy McCall ↶ |
89 |
deps_of(&c.deps) |
| 90 |
} |
| 91 |
|
| 92 |
pub fn deps_of(deps: &[oxy_core::hash::ChangeHash]) -> String { |
| 93 |
deps.iter().map(|d| format!("#{}", d.short(12))).collect::<Vec<_>>().join(" ") |
| fce44ae9 Andy McCall ↶ |
94 |
} |
| 95 |
|
| 96 |
#[cfg(test)] |
| 97 |
mod tests { |
| 98 |
use super::*; |
| 99 |
|
| 100 |
#[test] |
| 101 |
fn dates() { |
| 102 |
assert_eq!(when(0), "1970-01-01 00:00:00Z"); |
| 103 |
assert_eq!(when(1_700_000_000_000), "2023-11-14 22:13:20Z"); |
| 104 |
assert_eq!(plural(1, "head"), "1 head"); |
| 105 |
assert_eq!(plural(2, "head"), "2 heads"); |
| 106 |
} |
| 107 |
} |