aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: dd2da1cad9f53608c392ea19f0873f14d59eb000 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#![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();
    opts.optflag("h", "help", "print this help menu");

    let matches = match opts.parse(&args[1..]) {
        Ok(m) => m,
        Err(e) => {
            eprintln(&e);
            process::exit(exitcode::NOINPUT);
        },
    };

    if matches.opt_present("h") {
        print_usage(&opts);
        process::exit(exitcode::USAGE);
    }

    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 if matches.free.len() > 1 {
        eprintln(&"too many ref arguments");
        process::exit(exitcode::USAGE);
    } else {
        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 command line usage.
fn print_usage(opts: &Options) {
    let brief = "usage: git todo [<commit>]";
    print!("{}", opts.usage(&brief));
}

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