commitlint_rs/
git.rs

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use regex::Regex;
use std::{collections::HashMap, process::Command};
/// ReadCommitMessageOptions represents the options for reading commit messages.
/// Transparently, it is defined to be similar to the behavior of the git log command.
#[derive(Clone, Debug)]
pub struct ReadCommitMessageOptions {
    /// From is the starting commit hash to read from.
    pub from: Option<String>,

    /// Path is the path to read commit messages from.
    pub path: String,

    /// To is the ending commit hash to read to.
    pub to: Option<String>,
}

/// Get commit messages from git.
pub fn read(options: ReadCommitMessageOptions) -> Vec<String> {
    // Configure revision range following the git spec.
    //
    // See: https://git-scm.com/docs/git-log#Documentation/git-log.txt-ltrevision-rangegt
    //
    // Make a range if both `from` and `to` are specified, then assign from..to.
    // If both are not specified, then assign HEAD.
    let range = match (options.from, options.to) {
        (Some(from), Some(to)) => format!("{}..{}", from, to),
        (Some(from), None) => format!("{}..HEAD", from),
        (None, Some(to)) => format!("HEAD..{}", to),
        (None, None) => "HEAD".to_string(),
    };

    // See https://git-scm.com/docs/git-log
    let stdout = Command::new("git")
        .arg("log")
        .arg("--pretty=%B")
        .arg("--no-merges")
        .arg("--no-decorate")
        .arg("--reverse")
        .arg(range)
        .arg("--") // Explicitly specify the end of options as described https://git-scm.com/docs/git-log#Documentation/git-log.txt---ltpathgt82308203
        .arg(options.path)
        .output()
        .expect("Failed to execute git log")
        .stdout;

    let stdout = String::from_utf8_lossy(&stdout);
    extract_commit_messages(&stdout)
}

fn extract_commit_messages(input: &str) -> Vec<String> {
    let commit_delimiter = Regex::new(r"(?m)^commit [0-9a-f]{40}$").unwrap();
    let commits: Vec<&str> = commit_delimiter.split(input).collect();

    let mut messages: Vec<String> = Vec::new();

    for commit in commits {
        let message_lines: Vec<&str> = commit.trim().lines().collect();
        let message = message_lines.join("\n");
        messages.push(message);
    }

    messages
}

/// Parse a commit message and return the subject, body, and footers.
///
/// Please refer the official documentation for the commit message format.
/// See: https://www.conventionalcommits.org/en/v1.0.0/#summary
///
/// ```ignore
/// <type>[optional scope]: <description> <-- Subject
///
/// [optional body] <-- Body
///
/// [optional footer(s)] <-- Footer
/// ```
pub fn parse_commit_message(
    message: &str,
) -> (String, Option<String>, Option<HashMap<String, String>>) {
    let lines: Vec<&str> = message.lines().collect();
    let mut lines_iter = lines.iter();

    let subject = lines_iter.next().unwrap_or(&"").trim().to_string();
    let mut body = None;
    let mut footer = None;

    let mut in_body = false;
    let mut in_footer = false;

    for line in lines_iter {
        if line.trim().is_empty() {
            if in_body {
                in_body = false;
                in_footer = true;
            }
        } else if in_footer {
            let parts: Vec<&str> = line.splitn(2, ':').map(|part| part.trim()).collect();
            if parts.len() == 2 {
                let key = parts[0].to_string();
                let value = parts[1].to_string();
                let footer_map = footer.get_or_insert(HashMap::new());
                footer_map.insert(key, value);
            }
        } else if !in_body {
            in_body = true;
            body = Some(line.trim().to_string());
        } else if let Some(b) = body.as_mut() {
            b.push('\n');
            b.push_str(line.trim());
        }
    }

    (subject, body, footer)
}

/// Parse a commit message subject and return the type, scope, and description.
///
/// Note that exclamation mark is not respected as the existing commitlint
/// does not have any rules for it.
/// See: https://commitlint.js.org/reference/rules.html
pub fn parse_subject(subject: &str) -> (Option<String>, Option<String>, Option<String>) {
    let re = regex::Regex::new(
        r"^(?P<type>\w+)(?:\((?P<scope>[^\)]+)\))?(?:!)?\:\s?(?P<description>.*)$",
    )
    .unwrap();
    if let Some(captures) = re.captures(subject) {
        let r#type = captures.name("type").map(|m| m.as_str().to_string());
        let scope = captures.name("scope").map(|m| m.as_str().to_string());
        let description = captures.name("description").map(|m| m.as_str().to_string());

        return (r#type, scope, description);
    }
    // Fall back to the description.
    (None, None, Some(subject.to_string()))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_single_line_parse_commit_message() {
        let input = "feat(cli): add dummy option";
        let (subject, body, footer) = parse_commit_message(input);
        assert_eq!(subject, "feat(cli): add dummy option");
        assert_eq!(body, None);
        assert_eq!(footer, None);
    }

    #[test]
    fn test_body_parse_commit_message() {
        let input = "feat(cli): add dummy option

Hello, there!";
        let (subject, body, footer) = parse_commit_message(input);
        assert_eq!(subject, "feat(cli): add dummy option");
        assert_eq!(body, Some("Hello, there!".to_string()));
        assert_eq!(footer, None);
    }

    #[test]
    fn test_footer_parse_commit_message() {
        let input = "feat(cli): add dummy option

Hello, there!

Link: Hello";
        let (subject, body, footer) = parse_commit_message(input);

        let mut f = HashMap::new();
        f.insert("Link".to_string(), "Hello".to_string());
        assert_eq!(subject, "feat(cli): add dummy option");
        assert_eq!(body, Some("Hello, there!".to_string()));
        assert!(footer.is_some());
        assert_eq!(f.get("Link"), Some(&"Hello".to_string()));
    }

    #[test]
    fn test_footer_with_multiline_body_parse_commit_message() {
        let input = "feat(cli): add dummy option

Hello, there!
I'm from Japan!

Link: Hello";
        let (subject, body, footer) = parse_commit_message(input);

        let mut f = HashMap::new();
        f.insert("Link".to_string(), "Hello".to_string());
        assert_eq!(subject, "feat(cli): add dummy option");
        assert_eq!(
            body,
            Some(
                "Hello, there!
I'm from Japan!"
                    .to_string()
            )
        );
        assert!(footer.is_some());
        assert_eq!(f.get("Link"), Some(&"Hello".to_string()));
    }

    #[test]
    fn test_multiple_footers_parse_commit_message() {
        let input = "feat(cli): add dummy option

Hello, there!

Link: Hello
Name: Keke";
        let (subject, body, footer) = parse_commit_message(input);

        assert_eq!(subject, "feat(cli): add dummy option");
        assert_eq!(body, Some("Hello, there!".to_string()));
        assert!(footer.is_some());
        assert_eq!(
            footer.clone().unwrap().get("Link"),
            Some(&"Hello".to_string())
        );
        assert_eq!(footer.unwrap().get("Name"), Some(&"Keke".to_string()));
    }

    #[test]
    fn test_parse_subject_with_scope() {
        let input = "feat(cli): add dummy option";
        assert_eq!(
            parse_subject(input),
            (
                Some("feat".to_string()),
                Some("cli".to_string()),
                Some("add dummy option".to_string())
            )
        );
    }

    #[test]
    fn test_parse_subject_with_emphasized_type_with_scope() {
        let input = "feat(cli)!: add dummy option";
        assert_eq!(
            parse_subject(input),
            (
                Some("feat".to_string()),
                Some("cli".to_string()),
                Some("add dummy option".to_string())
            )
        );
    }

    #[test]
    fn test_parse_subject_without_scope() {
        let input = "feat: add dummy option";
        assert_eq!(
            parse_subject(input),
            (
                Some("feat".to_string()),
                None,
                Some("add dummy option".to_string())
            )
        );
    }

    #[test]
    fn test_parse_subject_with_emphasized_type_without_scope() {
        let input = "feat!: add dummy option";
        assert_eq!(
            parse_subject(input),
            (
                Some("feat".to_string()),
                None,
                Some("add dummy option".to_string())
            )
        );
    }

    #[test]
    fn test_parse_subject_with_empty_description() {
        let input = "feat(cli): ";
        assert_eq!(
            parse_subject(input),
            (
                Some("feat".to_string()),
                Some("cli".to_string()),
                Some("".to_string())
            )
        );
    }

    #[test]
    fn test_parse_subject_with_empty_scope() {
        let input = "feat: add dummy commit";
        assert_eq!(
            parse_subject(input),
            (
                Some("feat".to_string()),
                None,
                Some("add dummy commit".to_string())
            )
        );
    }

    #[test]
    fn test_parse_subject_without_message() {
        let input = "";
        assert_eq!(parse_subject(input), (None, None, Some("".to_string())));
    }

    #[test]
    fn test_parse_subject_with_error_message() {
        let input = "test";
        assert_eq!(parse_subject(input), (None, None, Some("test".to_string())));
    }
}