use serde::{Deserialize, Serialize};
use crate::domain::Oid;
pub const DEFAULT_TOP: u32 = 50;
pub const MAX_TOP: u32 = 5000;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BlobState {
InHead,
Replaced,
Deleted,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlobRow {
pub path: String,
pub more_paths: u32,
pub blob: Oid,
pub size: u64,
pub disk_size: u64,
pub first_commit: Oid,
pub last_commit: Oid,
pub state: BlobState,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FolderRow {
pub path: String,
pub size: u64,
pub blobs: u64,
pub in_head: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DeletedRow {
pub path: String,
pub versions: u64,
pub size: u64,
pub first_commit: Oid,
pub last_commit: Oid,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct SizeAnalysis {
pub blobs: Vec<BlobRow>,
pub folders: Vec<FolderRow>,
pub deleted: Vec<DeletedRow>,
pub top: u32,
pub commits: u64,
pub blobs_total: u64,
pub bytes_total: u64,
pub disk_total: u64,
pub bytes_in_head: u64,
pub bytes_history_only: u64,
pub deleted_paths: u64,
pub blobs_cut: u64,
pub folders_cut: u64,
pub deleted_cut: u64,
}
#[must_use]
pub fn glob_for(path: &str) -> String {
let mut out = String::with_capacity(path.len() + 3);
if !path.contains('/') {
out.push('/');
}
for c in path.chars() {
match c {
'*' | '?' | '[' => {
out.push('[');
out.push(c);
out.push(']');
}
c => out.push(c),
}
}
out
}
#[must_use]
pub fn human_size(bytes: u64) -> String {
const UNITS: [&str; 5] = ["B", "KiB", "MiB", "GiB", "TiB"];
if bytes < 1024 {
return format!("{bytes} B");
}
let mut value = bytes as f64;
let mut unit = 0;
while value >= 1024.0 && unit < UNITS.len() - 1 {
value /= 1024.0;
unit += 1;
}
if value >= 100.0 {
format!("{value:.0} {}", UNITS[unit])
} else {
format!("{value:.1} {}", UNITS[unit])
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::rewrite::GlobSet;
fn set(p: &str) -> GlobSet {
GlobSet::new(&[glob_for(p)]).unwrap()
}
#[test]
fn a_glob_for_a_path_matches_that_path_only() {
let g = set("big.bin");
assert!(g.is_match("big.bin") && !g.is_match("sub/big.bin"));
let g = set("data/a[1].bin");
assert!(g.is_match("data/a[1].bin") && !g.is_match("data/a1.bin"));
let g = set("data/dump*.sql");
assert!(g.is_match("data/dump*.sql") && !g.is_match("data/dump1.sql"));
let g = set("data");
assert!(g.is_match("data/x/y.bin"));
}
#[test]
fn sizes_read_as_people_expect() {
assert_eq!(human_size(10), "10 B");
assert_eq!(human_size(1536), "1.5 KiB");
assert_eq!(human_size(5 * 1024 * 1024), "5.0 MiB");
assert_eq!(human_size(300 * 1024 * 1024), "300 MiB");
}
}