From e68aa0dfd1a200ff781f19ddffd69d01d9796ad3 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 16 Sep 2020 01:58:58 +0200 Subject: write_since(): Take an arbitrary branch as input Allow the branch to be parameterised instead of forcing the base to be the local "master" branch. Added the `Todos` struct to store a `Repository` reference, since we need this for both the base ref and the diff. Decided to take a `Tree` instead of a `Branch` because we might want to compare against an arbitrary ref and allow users to specify that ref on the command line. --- src/lib.rs | 91 ++++++++++++++++++++++++++++++++----------------------------- src/main.rs | 9 ++++-- 2 files changed, 55 insertions(+), 45 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1b9689a..bc3c9b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,50 +2,55 @@ use std::io::Write; -use git2::Repository; - - -pub fn write_since(write_to: &mut W) { - let repo = Repository::open(".").unwrap(); - - // let head = repo.head().unwrap().target().unwrap(); - let master = repo.find_branch("master", git2::BranchType::Local).unwrap(); - // let merge_base = repo.merge_base(head, master.get().target().unwrap()).unwrap(); - - let tree = master.get().peel_to_tree().unwrap(); - let diff = repo.diff_tree_to_workdir(Some(&tree), None).unwrap(); - - diff.foreach( - &mut |_file, _progress| { - true - }, - None, - None, - Some( - &mut |delta, hunk, line| { - // println!( - // "d: {:?}, h: {:?}, l: {:?}", - // delta, - // hunk, - // std::str::from_utf8(line.content()).unwrap(), - // ); - - if let Some(line_number) = line.new_lineno() { - let l = std::str::from_utf8(line.content()).unwrap(); - - if l.contains("TODO") { - write!( - write_to, - "{}:{}:{}", - delta.new_file().path().unwrap().display(), - line_number, - l, - ).unwrap(); +use git2::{Repository, Tree}; + + +pub struct Todos<'a> { + pub repo: &'a Repository, +} + +impl Todos<'_> { + pub fn write_since(&self, tree: Tree<'_>, write_to: &mut W) { + let diff = self.repo.diff_tree_to_workdir(Some(&tree), None).unwrap(); + + diff.foreach( + &mut |_file, _progress| { + true + }, + None, + None, + Some( + &mut |delta, hunk, line| { + // println!( + // "d: {:?}, h: {:?}, l: {:?}", + // delta, + // hunk, + // std::str::from_utf8(line.content()).unwrap(), + // ); + + if let Some(line_number) = line.new_lineno() { + let l = std::str::from_utf8(line.content()).unwrap(); + + if l.contains("TODO") { + write!( + write_to, + "{}:{}:{}", + delta.new_file().path().unwrap().display(), + line_number, + l, + ).unwrap(); + } } + + true } + ), + ).unwrap(); + } - true - } - ), - ).unwrap(); + pub fn master_tree(&self) -> Tree<'_> { + let master = self.repo.find_branch("master", git2::BranchType::Local).unwrap(); + + master.get().peel_to_tree().unwrap() + } } diff --git a/src/main.rs b/src/main.rs index 60f12d8..48e8f5d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,13 @@ #![warn(rust_2018_idioms)] -use git_todo::write_since; +use git2::Repository; + +use git_todo::Todos; fn main() { - write_since(&mut std::io::stdout()); + let repo = Repository::open(".").unwrap(); + + let todos = Todos { repo: &repo }; + todos.write_since(todos.master_tree(), &mut std::io::stdout()); } -- cgit v1.2.3