diff options
| -rw-r--r-- | Cargo.lock | 23 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/main.rs | 48 | 
3 files changed, 72 insertions, 1 deletions
| @@ -28,9 +28,26 @@ 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",  ] @@ -236,6 +253,12 @@ dependencies = [  ]  [[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"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -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<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();  } | 
