Skip to content

Commit

Permalink
Add test repo, start refactoring app
Browse files Browse the repository at this point in the history
  • Loading branch information
sondr3 committed May 12, 2020
1 parent 5bf3d24 commit 8358880
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 38 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "tests/repo"]
path = tests/repo
url = git@github.com:sondr3/git-anger-management-test-repo.git
45 changes: 7 additions & 38 deletions src/bin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use console::Term;
use git2::{Commit, Repository};
use git_anger_management::{naughty_word, split_into_clean_words, Repo};
use git2::Repository;
use git_anger_management::Repo;
use std::env;
use std::error::Error;
use std::path::PathBuf;
Expand All @@ -21,6 +21,9 @@ struct Cli {
#[structopt(short, long)]
/// Only display information about repo
repo: bool,
#[structopt(short, long)]
/// Print output as JSON instead of a prettified table
json: bool,
#[structopt(parse(from_os_str))]
/// Directory to parse commits from
directory: Option<PathBuf>,
Expand All @@ -36,16 +39,7 @@ pub fn main() -> Result<(), Box<dyn Error>> {
let verbose = opt.verbose;

let repo = Repository::open(&path)?;
let commits = {
let mut revwalk = repo.revwalk()?;
let mut commits: Vec<Commit> = Vec::new();
revwalk.push_head()?;
for commit_id in revwalk {
let commit = repo.find_commit(commit_id?)?;
commits.push(commit);
}
commits
};
let commits = Repo::commits(&repo)?;

let mut repo = Repo::new(match path.file_name() {
Some(path) => path.to_str().unwrap().to_owned(),
Expand All @@ -55,32 +49,7 @@ pub fn main() -> Result<(), Box<dyn Error>> {
let term = Term::stderr();

term.write_line("Crunching commits...")?;
for commit in &commits {
if let (Some(author_name), Some(commit_message)) = (
commit.author().name(),
commit.message().map(|w| w.to_lowercase()),
) {
let mut curses_added = 0;
{
let author = repo.author(author_name);
author.total_commits += 1;
for word in split_into_clean_words(&commit_message) {
if naughty_word(word) {
author.total_curses += 1;
curses_added += 1;
author.update_occurrence(word);
}
}
}
repo.total_commits += 1;
repo.total_curses += curses_added;
} else {
eprintln!(
"Skipping commit {:?} because either the commit author or message is missing",
commit
);
}
}
repo.build(commits);

term.clear_last_lines(1)?;
repo.count_curses();
Expand Down
46 changes: 46 additions & 0 deletions src/repo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::author::Author;
use crate::{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;

/// A simple representation of a git repository.
Expand Down Expand Up @@ -136,4 +139,47 @@ impl Repo {
curses.push(Cell::new(&format!("{}", total)));
table.add_row(Row::new(curses));
}

/// Build a list of commits by walking the history of a repository.
pub fn commits(repo: &Repository) -> Result<Vec<Commit>, Box<dyn Error>> {
let mut revwalk = repo.revwalk()?;
let mut commits: Vec<Commit> = Vec::new();
revwalk.push_head()?;
for commit_id in revwalk {
let commit = repo.find_commit(commit_id?)?;
commits.push(commit);
}

Ok(commits)
}

/// Documentation dammit!
pub fn build(&mut self, commits: Vec<Commit>) {
for commit in &commits {
if let (Some(author_name), Some(commit_message)) = (
commit.author().name(),
commit.message().map(|w| w.to_lowercase()),
) {
let mut curses_added = 0;
{
let author = self.author(author_name);
author.total_commits += 1;
for word in split_into_clean_words(&commit_message) {
if naughty_word(word) {
author.total_curses += 1;
curses_added += 1;
author.update_occurrence(word);
}
}
}
self.total_commits += 1;
self.total_curses += curses_added;
} else {
eprintln!(
"Skipping commit {:?} because either the commit author or message is missing",
commit
);
}
}
}
}
1 change: 1 addition & 0 deletions tests/repo
Submodule repo added at 057ac4
35 changes: 35 additions & 0 deletions tests/repo_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use git2::Repository;
use git_anger_management::Repo;
use std::path::{Path, PathBuf};

#[test]
fn test_commit_count() {
let repo = Repository::open(Path::new("./tests/repo").to_path_buf()).unwrap();
let commits = Repo::commits(&repo).unwrap();
assert_eq!(commits.len(), 4);
}

#[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);

assert_eq!(repo.authors.len(), 2);
assert_eq!(repo.total_commits, 4);
assert_eq!(repo.total_curses, 6);

let john = repo.authors.get("John Doe").unwrap();
assert_eq!(john.total_curses, 2);
assert_eq!(john.total_commits, 1);
assert_eq!(john.curses.get("bloody").unwrap(), &1);
assert_eq!(john.curses.get("damn").unwrap(), &1);

let me = repo.authors.get("Sondre Nilsen").unwrap();
assert_eq!(me.total_curses, 3);
assert_eq!(me.total_commits, 3);
assert_eq!(me.curses.get("fuck").unwrap(), &1);
assert_eq!(me.curses.get("fucking").unwrap(), &1);
assert_eq!(me.curses.get("shitty").unwrap(), &1);
}

0 comments on commit 8358880

Please sign in to comment.