paline 0.5.0

一个用 Rust 编写的代码行数统计命令行工具。递归扫描目录,按编程语言统计行数,并以彩色横向条形图在终端中可视化展示。
use std::path::Path;

use git2::{Commit, Repository};

use crate::logger::{Level, Log, Verbosity};

pub fn open_repository(path: &Path) -> Result<Repository, Log> {
    match git2::Repository::open(path) {
        Ok(r) => Ok(r),
        Err(e) => Err(Log::new(
            Level::Error,
            format!("Can't open the git repository: {}", e),
            Some(Verbosity::Normal),
        )),
    }
}

pub fn get_commit_from<'repo>(
    repo: &'repo Repository,
    commit_hash: &str,
) -> Result<Commit<'repo>, Log> {
    let obj = repo.revparse_single(commit_hash).map_err(|e| {
        Log::new(
            Level::Error,
            format!("Unable to parse commit hash \"{}\": {}", commit_hash, e),
            None,
        )
    })?;

    obj.peel_to_commit().map_err(|e| {
        Log::new(
            Level::Error,
            format!("{} is not a commit: {}", commit_hash, e),
            None,
        )
    })
}