Compare commits

...

3 Commits

5 changed files with 111 additions and 20 deletions

View File

@ -4,6 +4,10 @@ cargo-features = ["codegen-backend"]
name = "git-heatmap"
version = "0.1.0"
edition = "2021"
authors = ["Wynd <wyndftw@proton.me>"]
description = "A simple and customizable heatmap for git repos"
readme = "README.md"
repository = "https://git.pixelatedw.xyz/wynd/git-heatmap"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -42,9 +42,17 @@ $ git-heatmap -r "/path/to/repo" -b "main" -r "/other/repo" -b "test"
# or to comply with the same number of branch lists per repo lists rule from above
$ git-heatmap -r "/path/to/repo" -b "main" -r "other/repo" -b ""
# alternatively you can simply pass a root directory and let the program search for all git projects
# do be warned that doing so means you're losing the customization aspect of choosing
# which branches should be checked per repo, in this case all branches will be checked.
$ git-heatmap --root-dir "/path"
# by default merges are counted so using --no-merges ensures they won't be counted
$ git-heatmap --no-merges
# splits the heatmap per month instead of one big chunk
$ git-heatmap --split-months
# by default it colors every day based on which one the maximum number of commits in a visible day
# shading all the others accordingly, with by-amount it will instead color each day based on
# the amount of commits in that day

View File

@ -6,8 +6,11 @@ use clap::{arg, Parser, ValueHint};
use crate::heatmap::{ColorLogic, HeatmapColors};
#[derive(Clone, Debug, Parser, PartialEq, Eq)]
#[command(version, about, long_about = None)]
#[command(version, about, author, long_about = None)]
pub struct CliArgs {
#[arg(long("root-dir"), value_hint = ValueHint::DirPath)]
pub root_dir: Option<PathBuf>,
#[arg(short, long, num_args(0..))]
pub authors: Option<Vec<String>>,
@ -29,6 +32,9 @@ pub struct CliArgs {
#[arg(long("until"))]
pub until: Option<String>,
#[arg(long("split-months"), help("Split months"), default_value_t = false)]
pub split_months: bool,
#[arg(long("no-merges"), default_value_t = false)]
pub no_merges: bool,
@ -39,4 +45,4 @@ pub struct CliArgs {
fn get_since_date() -> String {
let date = Local::now() - Duration::days(365);
date.format("%Y-%m-%d").to_string()
}
}

View File

@ -17,7 +17,13 @@ pub struct Heatmap {
}
impl Heatmap {
pub fn new(since: NaiveDate, until: NaiveDate, repos: usize, commits: Vec<Commit>) -> Self {
pub fn new(
since: NaiveDate,
until: NaiveDate,
repos: usize,
commits: Vec<Commit>,
split_months: bool,
) -> Self {
let mut heatmap = Self {
data: [vec![], vec![], vec![], vec![], vec![], vec![], vec![]],
since,
@ -46,6 +52,19 @@ impl Heatmap {
}
while current_day <= until {
if split_months {
let last_day_of_month =
heatmap.last_day_of_month(current_day.year_ce().1 as i32, current_day.month());
// If current day is the last of the month we add 2 weeks worth of empty space so
// months are more visible
if last_day_of_month == current_day {
for i in 0..14 {
heatmap.data[(i as usize) % 7].push(-1);
}
}
}
let month_name = current_day.format("%b").to_string();
if current_day == since {
@ -75,6 +94,13 @@ impl Heatmap {
heatmap
}
fn last_day_of_month(&self, year: i32, month: u32) -> NaiveDate {
NaiveDate::from_ymd_opt(year, month + 1, 1)
.unwrap_or_else(|| NaiveDate::from_ymd(year + 1, 1, 1))
.pred_opt()
.unwrap_or_default()
}
fn months_row(&self) -> String {
let mut row = " ".to_string();
@ -144,4 +170,3 @@ pub enum ColorLogic {
ByAmount,
ByWeight,
}

View File

@ -2,7 +2,12 @@
#![feature(let_chains)]
#![allow(dead_code)]
use std::{cmp::Reverse, collections::HashSet, path::PathBuf, sync::OnceLock};
use std::{
cmp::Reverse,
collections::HashSet,
path::{self, PathBuf},
sync::OnceLock,
};
use anyhow::{anyhow, Context, Result};
use chrono::{DateTime, Duration, Local, NaiveDate, TimeZone};
@ -84,9 +89,11 @@ fn main() -> Result<()> {
.unwrap_or_else(|| get_default_until(since));
let until = NaiveDate::parse_from_str(&until, "%Y-%m-%d").unwrap();
let split_months = args.split_months;
let commits = get_commits(args, since).with_context(|| "Could not fetch commit list")?;
let heatmap = Heatmap::new(since, until, commits.0, commits.1);
let heatmap = Heatmap::new(since, until, commits.0, commits.1, split_months);
println!("{heatmap}");
@ -147,29 +154,63 @@ fn get_color_map() -> Vec<String> {
.to_vec()
}
fn find_git_repos(scan_path: &path::Path, repos: &mut Vec<PathBuf>, _args: &CliArgs) {
let Ok(dirs) = scan_path.read_dir()
else {
return;
};
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());
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),
}
}
}
fn get_commits(args: CliArgs, start_date: NaiveDate) -> Result<(usize, Vec<Commit>)> {
let mut commits: HashSet<Commit> = HashSet::new();
let repos = match args.repos {
Some(r) => r,
None => vec![PathBuf::from(".")],
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(".")],
};
let branches = args.branches.unwrap_or_else(|| vec!["".to_string()]);
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)
}
};
let branches = args.branches.unwrap_or_else(|| vec!["@".to_string()]);
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()
));
}
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();
let authors = args.authors.unwrap_or_default();
let mut repos_count = 0;
for (i, repo_path) in repos.iter().enumerate() {
let repo = gix::open(repo_path).unwrap();
@ -185,6 +226,7 @@ fn get_commits(args: CliArgs, start_date: NaiveDate) -> Result<(usize, Vec<Commi
}
let mailmap = Mailmap::new(repo_path);
let mut has_commits = false;
for branch in branches {
let branch_commits = repo
@ -236,6 +278,8 @@ fn get_commits(args: CliArgs, start_date: NaiveDate) -> Result<(usize, Vec<Commi
return None;
}
has_commits = true;
Some(Commit {
id: c.id,
title,
@ -247,6 +291,10 @@ fn get_commits(args: CliArgs, start_date: NaiveDate) -> Result<(usize, Vec<Commi
commits.insert(c);
});
}
if has_commits {
repos_count += 1;
}
}
let commits = commits
@ -254,5 +302,5 @@ fn get_commits(args: CliArgs, start_date: NaiveDate) -> Result<(usize, Vec<Commi
.sorted_by_cached_key(|a| Reverse(a.time))
.collect_vec();
Ok((repos.len(), commits))
Ok((repos_count, commits))
}