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. --- Cargo.lock | 23 +++++++++++++++++++++++ Cargo.toml | 2 ++ src/main.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 4c250ac..abaac2e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,10 +27,27 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "exitcode" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de853764b47027c2e862a995c34978ffa63c1501f2e15f987ba11bd4f9bba193" + +[[package]] +name = "getopts" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" +dependencies = [ + "unicode-width", +] + [[package]] name = "git-todo" version = "0.0.1" dependencies = [ + "exitcode", + "getopts", "git2", "thiserror", ] @@ -235,6 +252,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + [[package]] name = "unicode-xid" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index 834bb4c..69bbb52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,7 @@ version = "0.0.1" edition = "2018" [dependencies] +exitcode = "1.1.2" +getopts = "0.2.21" git2 = "0.13.11" thiserror = "1.0.20" 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