From b0aa9334017e433225175d323732472c35f51aed Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 16 Sep 2020 01:34:52 +0200 Subject: Move `main()` to `lib::write_since()` Move this to a library function for better organisation. This will facilitate splitting up and refining `write_since()`, and free up `main()` for command line argument parsing. --- src/lib.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 46 ++-------------------------------------------- 2 files changed, 50 insertions(+), 44 deletions(-) create mode 100644 src/lib.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..46e0fc8 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,48 @@ +#![warn(rust_2018_idioms)] + +use git2::Repository; + + +pub fn write_since() { + 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") { + print!( + "{}:{}:{}", + delta.new_file().path().unwrap().display(), + line_number, + l, + ); + } + } + + true + } + ), + ).unwrap(); +} diff --git a/src/main.rs b/src/main.rs index 8e818a4..2648353 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,50 +1,8 @@ #![warn(rust_2018_idioms)] -extern crate git2; - -use git2::Repository; +use git_todo::write_since; fn main() { - 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") { - print!( - "{}:{}:{}", - delta.new_file().path().unwrap().display(), - line_number, - l, - ); - } - } - - true - } - ), - ).unwrap(); + write_since(); } -- cgit v1.2.3