From e7d1bd377a7b43cb2392751dd4e6e954c2bc875b Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 17 Sep 2020 00:04:06 +0200 Subject: main.rs: Accept a ref as a command line argument If a ref is given on the command line, use that as the diff base. Otherwise default to master. --- src/main.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index f743256..450b01b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,59 @@ #![warn(rust_2018_idioms)] +use std::env; +use std::process; + +use getopts::Options; use git2::Repository; use git_todo::Todos; fn main() { + let args: Vec = env::args().collect(); + + let mut opts = Options::new(); + + let matches = match opts.parse(&args[1..]) { + Ok(m) => m, + Err(e) => { + eprintln!("TODO {}", e); + process::exit(exitcode::NOINPUT); + }, + }; + let repo = Repository::open(".").unwrap(); let todos = Todos { repo: &repo }; - todos.write_since(todos.master_tree().unwrap(), &mut std::io::stdout()).unwrap(); + + let tree = if matches.free.is_empty() { + match todos.master_tree() { + Ok(t) => t, + Err(e) => { + eprintln!("TODO {}", e); + process::exit(exitcode::USAGE); + }, + } + } else { + // TODO: error if more than one ref given + let refname = &matches.free[0]; + + let oid = match repo.refname_to_id(&refname) { + Ok(oid) => oid, + Err(e) => { + eprintln!("TODO {}", e); + process::exit(exitcode::USAGE); + }, + }; + + match repo.find_tree(oid) { + Ok(t) => t, + Err(e) => { + eprintln!("TODO {}", e); + process::exit(exitcode::USAGE); + }, + } + }; + + todos.write_since(tree, &mut std::io::stdout()).unwrap(); } -- cgit v1.2.3