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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use crate::git;
use crate::{App, Cache, Subcommand::*};
use std::path::PathBuf;

/// Parses a string into an inclusive range.
/// "5"   -> Some([5, 5])
/// "2-6" -> Some([2, 6])
/// "foo" -> None
fn parse_range(arg: &str) -> Option<[usize; 2]> {
    arg.parse().map(|v| Some([v, v])).unwrap_or_else(|_| {
        let (a, b) = arg.split_once("-")?;
        let a: usize = a.parse().ok()?;
        Some(b.parse().map(|b| [a.min(b), a.max(b)]).unwrap_or([a, a]))
    })
}

/// parse arguments before the git command
/// for a list of all git commands, see ./git_cmd.rs
fn pre_cmd<I: Iterator<Item = String>>(args: &mut I, app: &mut App) {
    let cmdr = git::Commander::new(app.cwd());
    while let Some(arg) = args.next() {
        let arg = arg.as_str();
        if let Some(subcommand) = cmdr.get_subcommand(arg) {
            app.set_subcommand(match subcommand {
                "status" => Status(true),
                _ => Number,
            });
            app.arg(arg);
            break;
        } else if arg.eq("--version") {
            app.set_subcommand(Version);
            app.arg(arg);
            break;
        }
        app.arg(arg);
    }
    app.set_argc();
}

/// parse arguments after the git command
/// for a list of all git commands, see ./git_cmd.rs
fn post_cmd<I: Iterator<Item = String>>(args: &mut I, app: &mut App) {
    let mut skip = false;
    while let Some(arg) = args.next() {
        match arg.as_str() {
            "--" => {
                app.arg(arg);
                break;
            }
            "--short" | "-s" | "--porcelain" => {
                app.set_subcommand(Status(false))
            }
            _ => (),
        }
        let isf = arg.starts_with('-') && !arg.starts_with("--"); // is short flag
        match (!skip && !isf, parse_range(&arg)) {
            (true, Some([s, e])) => app.load_range(s, e),
            _ => app.arg(arg),
        }
        skip = isf;
    }
    app.cmd.args(args);
}

/// Parses all command-line arguments and returns an App instance that is ready
/// to be ran.
pub fn parse<I: Iterator<Item = String>>(cwd: PathBuf, args: I) -> App {
    let mut app = App::new(cwd);
    let args = &mut args.skip(1);
    pre_cmd(args, &mut app);
    if app.subcommand == Number {
        app.load_cache_buffer();
    }
    post_cmd(args, &mut app);
    app
}

#[cfg(test)]
use std::process::Command;

macro_rules! test {
    ($name:ident, $args:expr, $test:expr) => {
        test!($name, std::env::temp_dir(), $args, $test);
    };
    ($name:ident, $cwd:expr, $args:expr, $test:expr) => {
        #[test]
        fn $name() {
            let args = std::iter::once(&"git")
                .chain($args.iter())
                .map(|v| v.to_string());
            let cwd = PathBuf::from($cwd);
            $test(parse(cwd, args));
        }
    };
}

#[cfg(test)]
macro_rules! assert_args {
    ($received:expr, $args:expr) => {{
        let mut expected = Command::new("");
        expected.current_dir(std::env::temp_dir());
        if atty::is(atty::Stream::Stdout) {
            expected.args(["-c", "color.ui=always"]);
        }
        expected.args($args);
        if !$received.get_args().eq(expected.get_args()) {
            panic!(
                "\nreceived: {:?}\nexpected: {:?}\n",
                $received.get_args(),
                expected.get_args()
            )
        }
    }};
}

test!(test_status, "/home", ["-C", "/tmp", "status"], |app: App| {
    assert_eq!(app.subcommand(), &Status(true));
});

test!(test_single, ["add", "1"], |app: App| {
    assert_args!(app.cmd(), ["add", "1"]);
});

test!(test_range, ["add", "2-4"], |app: App| {
    assert_args!(app.cmd(), ["add", "2", "3", "4"]);
});

test!(test_mix, ["add", "8", "2-4"], |app: App| {
    assert_args!(app.cmd(), ["add", "8", "2", "3", "4"]);
});

// Gitnu will not seek to interfere with these cases smartly.
test!(test_overlap, ["add", "3-5", "2-4"], |app: App| {
    assert_args!(app.cmd(), ["add", "3", "4", "5", "2", "3", "4"]);
});

// anything after `--` will not be processed.
test!(test_double_dash, ["add", "3-5", "--", "2-4"], |app: App| {
    assert_args!(app.cmd(), ["add", "3", "4", "5", "--", "2-4"]);
});

test!(test_zeroes_1, ["add", "0"], |app: App| {
    assert_args!(app.cmd(), ["add", "0"]);
});
test!(test_zeroes_2, ["add", "0-1"], |app: App| {
    assert_args!(app.cmd(), ["add", "0", "1"]);
});
test!(test_zeroes_3, ["add", "0-0"], |app: App| {
    assert_args!(app.cmd(), ["add", "0"]);
});