diff options
| author | Teddy Wing | 2020-09-16 01:58:58 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2020-09-16 02:01:46 +0200 | 
| commit | e68aa0dfd1a200ff781f19ddffd69d01d9796ad3 (patch) | |
| tree | 7a4df375fcfe3070d9a47ce9944b08ba220b287b | |
| parent | cefd5dc50548b846b2f383c1a0d2a5168cd2553a (diff) | |
| download | git-todo-e68aa0dfd1a200ff781f19ddffd69d01d9796ad3.tar.bz2 | |
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.
| -rw-r--r-- | src/lib.rs | 91 | ||||
| -rw-r--r-- | src/main.rs | 9 | 
2 files changed, 55 insertions, 45 deletions
| @@ -2,50 +2,55 @@  use std::io::Write; -use git2::Repository; - - -pub fn write_since<W: Write>(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<W: Write>(&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());  } | 
