Skip to content

Commit

Permalink
refactor: completely separate Commits and Repo structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Apr 4, 2022
1 parent d00ab45 commit 7b34b0a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 50 deletions.
28 changes: 15 additions & 13 deletions src/info/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::cli::{self, Config};
use crate::repo::Commits;
use crate::ui::get_ascii_colors;
use crate::ui::text_colors::TextColors;
use anyhow::{Context, Result};
Expand Down Expand Up @@ -183,23 +184,24 @@ impl Info {
}
});

let mut internal_repo = Repo::new(
&repo,
let repo = Repo::new(&repo)?;
let mut commits = Commits::new(
repo.gitoxide(),
config.no_merges,
&config.bot_regex_pattern,
config.number_of_authors,
)?;
let (repo_name, repo_url) = internal_repo.get_name_and_url()?;
let head_refs = internal_repo.get_head_refs()?;
let version = internal_repo.get_version()?;
let git_username = internal_repo.get_git_username()?;
let number_of_tags = internal_repo.get_number_of_tags()?;
let number_of_branches = internal_repo.get_number_of_branches()?;
let creation_date = internal_repo.get_creation_date(config.iso_time);
let number_of_commits = internal_repo.get_number_of_commits();
let (authors, contributors) = internal_repo.take_authors(config.show_email);
let last_change = internal_repo.get_date_of_last_commit(config.iso_time);
let (repo_size, file_count) = internal_repo.get_repo_size();
let (repo_name, repo_url) = repo.get_name_and_url()?;
let head_refs = repo.get_head_refs()?;
let version = repo.get_version()?;
let git_username = repo.get_git_username()?;
let number_of_tags = repo.get_number_of_tags()?;
let number_of_branches = repo.get_number_of_branches()?;
let creation_date = commits.get_creation_date(config.iso_time);
let number_of_commits = commits.count();
let (authors, contributors) = commits.take_authors(config.show_email);
let last_change = commits.get_date_of_last_commit(config.iso_time);
let (repo_size, file_count) = repo.get_repo_size();
let license = Detector::new()?.get_license(&workdir)?;
let dependencies = DependencyDetector::new().get_dependencies(&workdir)?;

Expand Down
56 changes: 19 additions & 37 deletions src/info/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub struct Commits {
pub struct Repo<'a> {
git2_repo: &'a Repository,
repo: git::Repository,
commits: Commits,
}

#[derive(Hash, PartialOrd, Ord, Eq, PartialEq)]
Expand Down Expand Up @@ -127,59 +126,42 @@ impl Commits {
time_of_most_recent_commit,
})
}
}

impl<'a> Repo<'a> {
pub fn new(
git2_repo: &'a Repository,
no_merges: bool,
bot_regex_pattern: &Option<Regex>,
number_of_authors_to_display: usize,
) -> Result<Self> {
let repo = git::open(git2_repo.path())?;
let commits = Commits::new(
repo.clone(),
no_merges,
bot_regex_pattern,
number_of_authors_to_display,
)?;

Ok(Self {
repo,
git2_repo,
commits,
})
}

pub fn get_creation_date(&self, iso_time: bool) -> String {
gitoxide_time_to_formatted_time(self.commits.time_of_first_commit, iso_time)
gitoxide_time_to_formatted_time(self.time_of_first_commit, iso_time)
}

pub fn get_number_of_commits(&self) -> String {
pub fn count(&self) -> String {
format!(
"{}{}",
self.commits.num_commits,
self.commits
.is_shallow
.then(|| " (shallow)")
.unwrap_or_default()
self.num_commits,
self.is_shallow.then(|| " (shallow)").unwrap_or_default()
)
}

pub fn take_authors(&mut self, show_email: bool) -> (Vec<Author>, usize) {
if !show_email {
for author in &mut self.commits.authors {
for author in &mut self.authors {
author.clear_email();
}
}
(
std::mem::take(&mut self.commits.authors),
self.commits.total_num_authors,
)
(std::mem::take(&mut self.authors), self.total_num_authors)
}

pub fn get_date_of_last_commit(&self, iso_time: bool) -> String {
gitoxide_time_to_formatted_time(self.commits.time_of_most_recent_commit, iso_time)
gitoxide_time_to_formatted_time(self.time_of_most_recent_commit, iso_time)
}
}

impl<'a> Repo<'a> {
pub fn new(git2_repo: &'a Repository) -> Result<Self> {
let repo = git::open(git2_repo.path())?;

Ok(Self { repo, git2_repo })
}

pub fn gitoxide(&self) -> git::Repository {
self.repo.clone()
}

// This collects the repo size excluding .git
Expand Down

0 comments on commit 7b34b0a

Please sign in to comment.