2024-08-15 22:09:51 +03:00
|
|
|
#![feature(byte_slice_trim_ascii)]
|
|
|
|
#![feature(let_chains)]
|
|
|
|
#![allow(dead_code)]
|
|
|
|
|
2024-08-24 14:21:26 +03:00
|
|
|
use std::{
|
|
|
|
cmp::Reverse,
|
|
|
|
collections::HashSet,
|
|
|
|
path::{self, PathBuf},
|
|
|
|
sync::OnceLock,
|
|
|
|
};
|
2024-08-15 22:09:51 +03:00
|
|
|
|
2024-08-17 01:48:06 +03:00
|
|
|
use anyhow::{anyhow, Context, Result};
|
2024-08-16 21:00:37 +03:00
|
|
|
use chrono::{DateTime, Duration, Local, NaiveDate, TimeZone};
|
2024-08-15 22:09:51 +03:00
|
|
|
use clap::Parser;
|
2024-08-17 02:40:20 +03:00
|
|
|
use gix::{bstr::ByteSlice, traverse::commit::simple::Sorting, ObjectId};
|
2024-08-20 00:07:19 +03:00
|
|
|
use heatmap::{ColorLogic, HeatmapColors};
|
2024-08-17 02:40:20 +03:00
|
|
|
use itertools::Itertools;
|
2024-08-17 16:13:38 +03:00
|
|
|
use mailmap::Mailmap;
|
2024-08-15 22:09:51 +03:00
|
|
|
use rgb::Rgb;
|
|
|
|
|
|
|
|
use crate::{cli::CliArgs, heatmap::Heatmap};
|
|
|
|
|
|
|
|
mod cli;
|
|
|
|
mod heatmap;
|
2024-08-17 16:13:38 +03:00
|
|
|
mod mailmap;
|
2024-08-15 22:09:51 +03:00
|
|
|
mod rgb;
|
|
|
|
|
|
|
|
pub const ESCAPE: &str = "\x1B";
|
|
|
|
pub const RESET: &str = "\x1B[0m";
|
|
|
|
pub const DAYS: [&str; 7] = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"];
|
|
|
|
pub static CHAR: OnceLock<char> = OnceLock::new();
|
2024-08-20 00:07:19 +03:00
|
|
|
static COLOR_LOGIC: OnceLock<ColorLogic> = OnceLock::new();
|
2024-08-15 22:09:51 +03:00
|
|
|
pub static COLOR_MAP: OnceLock<Vec<String>> = OnceLock::new();
|
|
|
|
|
|
|
|
const GREEN_COLOR_MAP: [Rgb; 5] = [
|
|
|
|
Rgb(0, 0, 0),
|
|
|
|
Rgb(14, 68, 41),
|
|
|
|
Rgb(0, 109, 50),
|
|
|
|
Rgb(38, 166, 65),
|
2024-08-16 12:50:24 +03:00
|
|
|
Rgb(25, 255, 64),
|
2024-08-15 22:09:51 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
const RED_COLOR_MAP: [Rgb; 5] = [
|
|
|
|
Rgb(0, 0, 0),
|
|
|
|
Rgb(208, 169, 35),
|
|
|
|
Rgb(208, 128, 35),
|
|
|
|
Rgb(208, 78, 35),
|
2024-08-16 21:00:37 +03:00
|
|
|
Rgb(255, 0, 0),
|
2024-08-15 22:09:51 +03:00
|
|
|
];
|
|
|
|
|
2024-08-17 16:13:38 +03:00
|
|
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
2024-08-15 22:09:51 +03:00
|
|
|
struct Commit {
|
|
|
|
id: ObjectId,
|
|
|
|
title: String,
|
2024-08-17 16:13:38 +03:00
|
|
|
author: Author,
|
2024-08-16 02:27:50 +03:00
|
|
|
time: DateTime<Local>,
|
2024-08-15 22:09:51 +03:00
|
|
|
}
|
|
|
|
|
2024-08-17 16:13:38 +03:00
|
|
|
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
|
|
|
struct Author {
|
|
|
|
name: String,
|
|
|
|
email: String,
|
|
|
|
}
|
|
|
|
|
2024-08-15 22:09:51 +03:00
|
|
|
fn main() -> Result<()> {
|
|
|
|
clear_screen();
|
|
|
|
|
|
|
|
let args = CliArgs::parse();
|
|
|
|
|
2024-08-17 01:48:06 +03:00
|
|
|
// dbg!(&args);
|
|
|
|
|
2024-08-15 22:09:51 +03:00
|
|
|
CHAR.set(args.char).unwrap();
|
2024-08-20 00:07:19 +03:00
|
|
|
COLOR_LOGIC.set(args.counting.clone()).unwrap();
|
2024-08-15 22:09:51 +03:00
|
|
|
let color_map = match args.color_scheme {
|
|
|
|
HeatmapColors::Green => GREEN_COLOR_MAP,
|
|
|
|
HeatmapColors::Red => RED_COLOR_MAP,
|
|
|
|
};
|
|
|
|
let color_map = color_map
|
|
|
|
.into_iter()
|
|
|
|
.map(|c| c.to_ansi())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
COLOR_MAP.set(color_map).unwrap();
|
|
|
|
|
2024-08-16 21:00:37 +03:00
|
|
|
let since = NaiveDate::parse_from_str(&args.since, "%Y-%m-%d").unwrap();
|
2024-08-15 22:09:51 +03:00
|
|
|
|
2024-08-16 21:00:37 +03:00
|
|
|
let until = args
|
|
|
|
.until
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| get_default_until(since));
|
|
|
|
let until = NaiveDate::parse_from_str(&until, "%Y-%m-%d").unwrap();
|
2024-08-15 22:09:51 +03:00
|
|
|
|
2024-08-24 16:41:51 +03:00
|
|
|
let split_months = args.split_months;
|
|
|
|
|
2024-09-01 11:15:58 +03:00
|
|
|
let commits = get_commits(args, since, until).with_context(|| "Could not fetch commit list")?;
|
2024-08-16 21:00:37 +03:00
|
|
|
|
2024-08-24 16:41:51 +03:00
|
|
|
let heatmap = Heatmap::new(since, until, commits.0, commits.1, split_months);
|
2024-08-16 21:00:37 +03:00
|
|
|
|
|
|
|
println!("{heatmap}");
|
2024-08-15 22:09:51 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:00:37 +03:00
|
|
|
fn get_default_until(since: NaiveDate) -> String {
|
|
|
|
let mut until = Local::now().date_naive();
|
|
|
|
if since + Duration::days(365) < until {
|
|
|
|
until = since + Duration::days(365);
|
|
|
|
}
|
|
|
|
until.format("%Y-%m-%d").to_string()
|
|
|
|
}
|
|
|
|
|
2024-08-16 12:50:24 +03:00
|
|
|
fn get_color(val: i32, high: i32) -> usize {
|
2024-08-20 00:07:19 +03:00
|
|
|
match COLOR_LOGIC.get() {
|
|
|
|
Some(logic) => match logic {
|
|
|
|
ColorLogic::ByAmount => match val {
|
|
|
|
0 => 0,
|
|
|
|
x if x < 2 => 1,
|
|
|
|
x if x < 4 => 2,
|
|
|
|
x if x < 6 => 3,
|
2024-08-20 00:23:50 +03:00
|
|
|
x if x >= 6 => 4,
|
2024-08-20 00:07:19 +03:00
|
|
|
_ => 0,
|
|
|
|
},
|
|
|
|
ColorLogic::ByWeight => {
|
|
|
|
let color = val as f32 / high as f32;
|
|
|
|
match color {
|
|
|
|
0.0 => 0,
|
|
|
|
x if x <= 0.2 => 1,
|
|
|
|
x if x <= 0.4 => 2,
|
|
|
|
x if x <= 0.8 => 3,
|
|
|
|
x if x > 0.8 => 4,
|
|
|
|
_ => 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => 0,
|
2024-08-15 22:09:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_screen() {
|
|
|
|
print!("\x1b[2J\x1b[1;1H");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_char() -> char {
|
|
|
|
*CHAR.get_or_init(|| '▩')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_color_map() -> Vec<String> {
|
|
|
|
COLOR_MAP
|
|
|
|
.get_or_init(|| {
|
|
|
|
GREEN_COLOR_MAP
|
|
|
|
.into_iter()
|
|
|
|
.map(|c| c.to_ansi())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
})
|
|
|
|
.to_vec()
|
|
|
|
}
|
|
|
|
|
2024-08-24 14:21:26 +03:00
|
|
|
fn find_git_repos(scan_path: &path::Path, repos: &mut Vec<PathBuf>, _args: &CliArgs) {
|
|
|
|
let Ok(dirs) = scan_path.read_dir()
|
|
|
|
else {
|
|
|
|
return;
|
2024-08-16 21:00:37 +03:00
|
|
|
};
|
|
|
|
|
2024-08-24 14:21:26 +03:00
|
|
|
let dirs: Vec<_> = dirs
|
|
|
|
.filter_map(|f| f.ok())
|
|
|
|
.filter(|f| f.file_type().is_ok_and(|t| t.is_dir()))
|
|
|
|
.collect_vec();
|
|
|
|
|
|
|
|
let dirs = dirs.iter().map(|f| f.path());
|
2024-08-17 01:48:06 +03:00
|
|
|
|
2024-08-24 14:21:26 +03:00
|
|
|
for dir in dirs {
|
|
|
|
let filename = dir.file_name().unwrap_or_default().to_string_lossy();
|
|
|
|
match filename.as_ref() {
|
|
|
|
".git" => repos.push(dir),
|
|
|
|
_ => find_git_repos(&dir, repos, _args),
|
|
|
|
}
|
2024-08-17 01:48:06 +03:00
|
|
|
}
|
2024-08-24 14:21:26 +03:00
|
|
|
}
|
|
|
|
|
2024-09-01 11:15:58 +03:00
|
|
|
fn get_commits(
|
|
|
|
args: CliArgs,
|
|
|
|
start_date: NaiveDate,
|
|
|
|
end_date: NaiveDate,
|
|
|
|
) -> Result<(usize, Vec<Commit>)> {
|
2024-08-24 14:21:26 +03:00
|
|
|
let mut commits: HashSet<Commit> = HashSet::new();
|
|
|
|
|
|
|
|
let (repos, branches) = match &args.root_dir {
|
|
|
|
Some(root) => {
|
|
|
|
let mut repos: Vec<PathBuf> = vec![];
|
|
|
|
find_git_repos(root, &mut repos, &args);
|
|
|
|
let branches = vec!["".to_string(); repos.len()];
|
|
|
|
(repos, branches)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let repos = match args.repos {
|
|
|
|
Some(r) => r,
|
|
|
|
None => vec![PathBuf::from(".")],
|
|
|
|
};
|
|
|
|
|
2024-08-24 16:41:51 +03:00
|
|
|
let branches = args.branches.unwrap_or_else(|| vec!["".to_string()]);
|
2024-08-24 14:21:26 +03:00
|
|
|
|
|
|
|
if repos.len() > 1 && repos.len() != branches.len() {
|
|
|
|
return Err(anyhow!(
|
|
|
|
"Number of repos ({}) needs to match the number of branch lists ({})!",
|
|
|
|
repos.len(),
|
|
|
|
branches.len()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
(repos, branches)
|
|
|
|
}
|
|
|
|
};
|
2024-08-17 01:48:06 +03:00
|
|
|
|
2024-08-17 16:13:38 +03:00
|
|
|
let current_time = Local::now().time();
|
|
|
|
let start_date = start_date.and_time(current_time);
|
|
|
|
let start_date = Local.from_local_datetime(&start_date).unwrap();
|
|
|
|
|
2024-09-01 11:15:58 +03:00
|
|
|
let end_date = end_date.and_time(current_time);
|
|
|
|
let end_date = Local.from_local_datetime(&end_date).unwrap();
|
|
|
|
|
2024-08-17 16:13:38 +03:00
|
|
|
let authors = args.authors.unwrap_or_default();
|
2024-08-24 15:47:15 +03:00
|
|
|
let mut repos_count = 0;
|
2024-08-17 16:13:38 +03:00
|
|
|
|
|
|
|
for (i, repo_path) in repos.iter().enumerate() {
|
|
|
|
let repo = gix::open(repo_path).unwrap();
|
2024-08-17 01:48:06 +03:00
|
|
|
|
|
|
|
let branch_names = &*branches[i];
|
|
|
|
let mut branches = vec![];
|
|
|
|
if branch_names.is_empty() {
|
|
|
|
branches.extend(repo.branch_names().iter());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let branch_names = branch_names.split(' ');
|
|
|
|
branches.extend(branch_names);
|
|
|
|
}
|
|
|
|
|
2024-08-17 16:13:38 +03:00
|
|
|
let mailmap = Mailmap::new(repo_path);
|
2024-08-24 15:47:15 +03:00
|
|
|
let mut has_commits = false;
|
2024-08-17 02:40:20 +03:00
|
|
|
|
2024-08-17 01:48:06 +03:00
|
|
|
for branch in branches {
|
|
|
|
let branch_commits = repo
|
|
|
|
.rev_parse(branch)?
|
|
|
|
.single()
|
|
|
|
.unwrap()
|
|
|
|
.ancestors()
|
2024-08-17 02:40:20 +03:00
|
|
|
.sorting(Sorting::ByCommitTimeNewestFirstCutoffOlderThan {
|
|
|
|
seconds: start_date.timestamp(),
|
|
|
|
})
|
2024-08-17 01:48:06 +03:00
|
|
|
.all()?;
|
|
|
|
|
|
|
|
branch_commits
|
|
|
|
.filter_map(|c| c.ok())
|
|
|
|
.filter_map(|c| c.object().ok())
|
|
|
|
.filter_map(|c| {
|
|
|
|
let title = c
|
|
|
|
.message()
|
|
|
|
.ok()?
|
|
|
|
.title
|
|
|
|
.trim_ascii()
|
|
|
|
.to_str()
|
|
|
|
.ok()?
|
|
|
|
.to_string();
|
|
|
|
|
2024-08-17 23:59:44 +03:00
|
|
|
if args.no_merges {
|
|
|
|
let is_merge = c.parent_ids().count() > 1;
|
|
|
|
if is_merge {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-17 16:13:38 +03:00
|
|
|
let author = c.author().ok()?;
|
|
|
|
|
|
|
|
let email = author.email.to_string();
|
|
|
|
let name = author.name.to_string();
|
|
|
|
|
|
|
|
let author = Author { name, email };
|
|
|
|
let author = mailmap.resolve(author);
|
|
|
|
|
|
|
|
if !authors.is_empty() && !authors.contains(&author.name) {
|
2024-08-17 01:48:06 +03:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let time = c.time().ok()?;
|
|
|
|
let time =
|
|
|
|
DateTime::from_timestamp_millis(time.seconds * 1000)?.with_timezone(&Local);
|
2024-09-01 11:15:58 +03:00
|
|
|
if time <= start_date + Duration::days(1) || time > end_date {
|
2024-08-17 01:48:06 +03:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2024-08-24 15:47:15 +03:00
|
|
|
has_commits = true;
|
|
|
|
|
2024-08-17 01:48:06 +03:00
|
|
|
Some(Commit {
|
|
|
|
id: c.id,
|
|
|
|
title,
|
|
|
|
author,
|
|
|
|
time,
|
|
|
|
})
|
2024-08-16 21:00:37 +03:00
|
|
|
})
|
2024-08-17 01:48:06 +03:00
|
|
|
.for_each(|c| {
|
|
|
|
commits.insert(c);
|
|
|
|
});
|
|
|
|
}
|
2024-08-24 15:47:15 +03:00
|
|
|
|
|
|
|
if has_commits {
|
|
|
|
repos_count += 1;
|
|
|
|
}
|
2024-08-16 21:00:37 +03:00
|
|
|
}
|
2024-08-15 22:09:51 +03:00
|
|
|
|
2024-08-16 21:00:37 +03:00
|
|
|
let commits = commits
|
|
|
|
.into_iter()
|
|
|
|
.sorted_by_cached_key(|a| Reverse(a.time))
|
|
|
|
.collect_vec();
|
2024-08-15 22:09:51 +03:00
|
|
|
|
2024-08-24 15:47:15 +03:00
|
|
|
Ok((repos_count, commits))
|
2024-08-15 22:09:51 +03:00
|
|
|
}
|