aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/lib.rs91
-rw-r--r--src/main.rs9
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<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());
}