aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 4d810b024830a1e5d450a56d8aca8f10cd98017e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#![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(&e);
            process::exit(exitcode::NOINPUT);
        },
    };

    let repo = match Repository::open(".") {
        Ok(r) => r,
        Err(e) => {
            eprintln(&format!("unable to open repository: {}", e));
            process::exit(exitcode::NOINPUT);
        },
    };

    let todos = Todos { repo: &repo };

    let tree = if matches.free.is_empty() {
        match todos.master_tree() {
            Ok(t) => t,
            Err(e) => {
                eprintln(&e);
                process::exit(exitcode::USAGE);
            },
        }
    } else {
        // TODO: error if more than one ref given
        let refname = &matches.free[0];

        let object = match repo.revparse_single(&refname) {
            Ok(object) => object,
            Err(e) => {
                eprintln(&e);
                process::exit(exitcode::USAGE);
            },
        };

        match object.peel_to_tree() {
            Ok(t) => t,
            Err(e) => {
                eprintln(&e);
                process::exit(exitcode::USAGE);
            },
        }
    };

    match todos.write_since(tree, &mut std::io::stdout()) {
        Err(e) => {
            eprintln(&e);
            process::exit(exitcode::UNAVAILABLE);
        },
        _ => (),
    };
}

/// Print to standard error with a program-specific prefix.
fn eprintln<D: std::fmt::Display>(error: &D) {
    eprintln!("error: {}", error);
}