Skip to content

Commit

Permalink
Walk the commits, find the authors, see if they've been naughty
Browse files Browse the repository at this point in the history
IT WORKS! HOLY SHIT.
  • Loading branch information
sondr3 committed Oct 15, 2018
1 parent b4b14ec commit 395a617
Showing 1 changed file with 19 additions and 55 deletions.
74 changes: 19 additions & 55 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,31 @@ fn main() -> Result<(), Box<Error>> {
let path = env::current_dir()?;
let repo = Repository::open(path)?;
let mut revwalk = repo.revwalk()?;
let mut occurrences: HashMap<&str, usize> = HashMap::new();
let mut commits: Vec<Commit> = Vec::new();
for curse in &curses {
occurrences.entry(curse).or_insert(0);
}
revwalk.push_head()?;
for commit in revwalk {
let commit = repo.find_commit(commit?)?;
commits.push(commit);
}
let mut authors: Vec<Author> = find_authors(&commits);
println!("{:?}", repo.workdir());
filter_occurrences(&mut occurrences);
println!("{:#?}", occurrences);
println!("{:#?}", commits);
print!("{:#?}", authors);
for commit in &commits {
let text = commit.message_raw().unwrap().to_lowercase().to_string();
let author = commit.author().name().unwrap().to_string();
let index = authors.iter().position(|i| i.name == author).expect("Could not find author");
let mut author = authors.get_mut(index).unwrap();
for word in text.split_whitespace() {
if naughty_word(word, &curses) {
author.update_occurrence(word);
}
}
}
for mut author in authors {
author.filter_occurrences();
if !author.is_not_naughty() {
println!("{}", author.name);
println!("{:#?}", author.curses);
}
}
Ok(())
}

Expand All @@ -79,25 +88,17 @@ fn find_authors(commits: &[Commit]) -> Vec<Author> {
for commit in commits {
let name = commit.author().name().unwrap().to_string();
if !names.contains(&name) {
res.push(Author::new(name.as_str(), HashMap::new()));
res.push(Author::new(name.as_str()));
}
names.push(name);
}
res
}

fn update_occurrence<'a>(word: &'a str, map: &mut HashMap<&'a str, usize>) {
map.entry(word).and_modify(|i| *i += 1);
}

fn naughty_word(word: &str, naughty_list: &[&str]) -> bool {
naughty_list.contains(&word)
}

fn filter_occurrences(map: &mut HashMap<&str, usize>) {
map.retain(|_, val| val > &mut 0);
}

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -109,41 +110,4 @@ mod test {
assert!(naughty_word("cyberfuckers", &curses));
assert!(!naughty_word("pretty", &curses));
}

#[test]
fn test_update_occurrences() {
let curses: Vec<&str> = CURSES.lines().collect();
let mut occurrences: HashMap<&str, usize> = HashMap::new();
for curse in &curses {
occurrences.entry(curse).or_insert(0);
}
update_occurrence("boobs", &mut occurrences);
update_occurrence("crap", &mut occurrences);
update_occurrence("boobs", &mut occurrences);

assert_eq!(2, occurrences.remove("boobs").unwrap());
assert_eq!(1, occurrences.remove("crap").unwrap());
}

#[test]
fn test_filter_occurrences() {
let curses: Vec<&str> = CURSES.lines().collect();
let mut occurrences: HashMap<&str, usize> = HashMap::new();
let actual: HashMap<&str, usize> =
[("shite", 1), ("goddamn", 1), ("shit", 1), ("fucking", 1)]
.iter()
.cloned()
.collect();
for curse in &curses {
occurrences.entry(curse).or_insert(0);
}
for word in "this is a fucking shit sentence with no goddamn shite in it".split_whitespace()
{
if naughty_word(&word, &curses) {
update_occurrence(word, &mut occurrences);
}
}
filter_occurrences(&mut occurrences);
assert_eq!(actual, occurrences);
}
}

0 comments on commit 395a617

Please sign in to comment.