aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorTeddy Wing2020-09-17 00:04:06 +0200
committerTeddy Wing2020-09-17 00:04:06 +0200
commite7d1bd377a7b43cb2392751dd4e6e954c2bc875b (patch)
tree2177dabb585b9a88738c5a5805b74b6ab086cd96 /src/main.rs
parent9b1e1480f1162b1771d8de14143b2eacb6dffddc (diff)
downloadgit-todo-e7d1bd377a7b43cb2392751dd4e6e954c2bc875b.tar.bz2
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.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs48
1 files changed, 47 insertions, 1 deletions
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<String> = 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();
}