Skip to content

Commit

Permalink
Move reading commits etc into Repo::new()
Browse files Browse the repository at this point in the history
  • Loading branch information
sondr3 committed May 12, 2020
1 parent d9f5571 commit 6130210
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
21 changes: 3 additions & 18 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use console::Term;
use git2::Repository;
use git_anger_management::repo::Repo;
use std::env;
use std::error::Error;
use std::path::PathBuf;
use std::time::Instant;
use structopt::clap::AppSettings;
use structopt::StructOpt;
use std::{env, error::Error, path::PathBuf, time::Instant};
use structopt::{clap::AppSettings, StructOpt};

#[derive(StructOpt, Debug)]
#[structopt(
Expand Down Expand Up @@ -38,21 +33,11 @@ pub fn main() -> Result<(), Box<dyn Error>> {
};
let verbose = opt.verbose;

let repo = Repository::open(&path)?;
let commits = Repo::commits(&repo)?;

let mut repo = Repo::new(match path.file_name() {
Some(path) => path.to_str().unwrap().to_owned(),
None => env::current_dir()?.to_str().unwrap().to_owned(),
});

let repo = Repo::new(&path)?;
let term = Term::stderr();

term.write_line("Crunching commits...")?;
repo.build(commits);

term.clear_last_lines(1)?;
repo.count_curses();
if verbose {
println!("Took {:?} to parse {}", start.elapsed(), repo.name);
}
Expand Down
31 changes: 22 additions & 9 deletions src/repo.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::author::Author;
use crate::core::{naughty_word, split_into_clean_words};
use crate::{
author::Author,
core::{naughty_word, split_into_clean_words},
};
use git2::{Commit, Repository};
use prettytable::{format, Cell, Row, Table};
use std::collections::HashMap;
use std::error::Error;
use std::fmt;
use std::{collections::HashMap, env, error::Error, fmt, path::Path};

/// A simple representation of a git repository.
#[derive(Debug)]
Expand All @@ -29,14 +29,27 @@ impl fmt::Display for Repo {

impl Repo {
/// Creates a new and empty repository.
pub fn new(name: impl Into<String>) -> Self {
Repo {
name: name.into(),
pub fn new(path: &Path) -> Result<Self, Box<dyn Error>> {
let repo = Repository::open(path)?;
let commits = Repo::commits(&repo)?;

let repo = match path.file_name() {
Some(path) => path.to_str().unwrap().to_owned(),
None => env::current_dir()?.to_str().unwrap().to_owned(),
};

let mut repo = Repo {
name: repo.into(),
total_commits: 0,
total_curses: 0,
curses: HashMap::new(),
authors: HashMap::new(),
}
};

repo.build(commits);
repo.count_curses();

Ok(repo)
}

/// Checks if an author exists and creates a new author if she/he doesn't
Expand Down
7 changes: 2 additions & 5 deletions tests/repo_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use git2::Repository;
use git_anger_management::repo::Repo;
use std::path::{Path, PathBuf};
use std::path::Path;

#[test]
fn test_commit_count() {
Expand All @@ -11,10 +11,7 @@ fn test_commit_count() {

#[test]
fn test_example_repo() {
let repo = Repository::open(Path::new("./tests/repo").to_path_buf()).unwrap();
let commits = Repo::commits(&repo).unwrap();
let mut repo = Repo::new("repo");
repo.build(commits);
let repo = Repo::new(Path::new("./tests/repo")).unwrap();

assert_eq!(repo.authors.len(), 2);
assert_eq!(repo.total_commits, 4);
Expand Down

0 comments on commit 6130210

Please sign in to comment.