-
-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathcommit.rs
461 lines (444 loc) · 11.7 KB
/
commit.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use crate::command;
use crate::config::{
CommitParser,
CommitPreprocessor,
GitConfig,
LinkParser,
};
use crate::error::{
Error as AppError,
Result,
};
use git2::Commit as GitCommit;
use git_conventional::Commit as ConventionalCommit;
use serde::ser::{
Serialize,
SerializeStruct,
Serializer,
};
/// Common commit object that is parsed from a repository.
#[derive(Debug, Clone, PartialEq, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Commit<'a> {
/// Commit ID.
pub id: String,
/// Commit message including title, description and summary.
pub message: String,
/// Conventional commit.
#[serde(skip_deserializing)]
pub conv: Option<ConventionalCommit<'a>>,
/// Commit group based on a commit parser or its conventional type.
pub group: Option<String>,
/// Default commit scope based on (inherited from) conventional type or a
/// commit parser.
pub default_scope: Option<String>,
/// Commit scope for overriding the default one.
pub scope: Option<String>,
/// A list of links found in the commit
pub links: Vec<Link>,
}
/// Object representing a link
#[derive(Debug, Clone, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Link {
/// Text of the link.
pub text: String,
/// URL of the link
pub href: String,
}
/// A conventional commit footer.
#[derive(Debug, Clone, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
struct Footer<'a> {
/// Token of the footer.
///
/// This is the part of the footer preceding the separator. For example, for
/// the `Signed-off-by: <user.name>` footer, this would be `Signed-off-by`.
token: &'a str,
/// The separator between the footer token and its value.
///
/// This is typically either `:` or `#`.
separator: &'a str,
/// The value of the footer.
value: &'a str,
/// A flag to signal that the footer describes a breaking change.
breaking: bool,
}
impl<'a> From<&'a git_conventional::Footer<'a>> for Footer<'a> {
fn from(footer: &'a git_conventional::Footer<'a>) -> Self {
Self {
token: footer.token().as_str(),
separator: footer.separator().as_str(),
value: footer.value(),
breaking: footer.breaking(),
}
}
}
impl<'a> From<&GitCommit<'a>> for Commit<'a> {
fn from(commit: &GitCommit<'a>) -> Self {
Self::new(
commit.id().to_string(),
commit.message().unwrap_or_default().to_string(),
)
}
}
impl Commit<'_> {
/// Constructs a new instance.
pub fn new(id: String, message: String) -> Self {
Self {
id,
message,
conv: None,
group: None,
default_scope: None,
scope: None,
links: vec![],
}
}
/// Processes the commit.
///
/// * converts commit to a conventional commit
/// * sets the group for the commit
/// * extacts links and generates URLs
pub fn process(&self, config: &GitConfig) -> Result<Self> {
let mut commit = self.clone();
if let Some(preprocessors) = &config.commit_preprocessors {
commit = commit.preprocess(preprocessors)?;
}
if config.conventional_commits.unwrap_or(true) {
if config.filter_unconventional.unwrap_or(true) {
commit = commit.into_conventional()?;
} else if let Ok(conv_commit) = commit.clone().into_conventional() {
commit = conv_commit;
}
}
if let Some(parsers) = &config.commit_parsers {
commit =
commit.parse(parsers, config.filter_commits.unwrap_or(false))?;
}
if let Some(parsers) = &config.link_parsers {
commit = commit.parse_links(parsers)?;
}
Ok(commit)
}
/// Returns the commit with its conventional type set.
pub fn into_conventional(mut self) -> Result<Self> {
match ConventionalCommit::parse(Box::leak(
self.message.to_string().into_boxed_str(),
)) {
Ok(conv) => {
self.conv = Some(conv);
Ok(self)
}
Err(e) => Err(AppError::ParseError(e)),
}
}
/// Preprocesses the commit using [`CommitPreprocessor`]s.
///
/// Modifies the commit [`message`] using regex or custom OS command.
///
/// [`message`]: Commit::message
pub fn preprocess(
mut self,
preprocessors: &[CommitPreprocessor],
) -> Result<Self> {
preprocessors.iter().try_for_each(|preprocessor| {
if let Some(text) = &preprocessor.replace {
self.message = preprocessor
.pattern
.replace_all(&self.message, text)
.to_string();
} else if let Some(command) = &preprocessor.replace_command {
if preprocessor.pattern.is_match(&self.message) {
self.message = command::run(
command,
Some(self.message.to_string()),
vec![("COMMIT_SHA", &self.id)],
)?;
}
}
Ok::<(), AppError>(())
})?;
Ok(self)
}
/// Parses the commit using [`CommitParser`]s.
///
/// Sets the [`group`] and [`scope`] of the commit.
///
/// [`group`]: Commit::group
/// [`scope`]: Commit::scope
pub fn parse(mut self, parsers: &[CommitParser], filter: bool) -> Result<Self> {
for parser in parsers {
let mut regex_checks = Vec::new();
if let Some(message_regex) = parser.message.as_ref() {
regex_checks.push((message_regex, self.message.to_string()))
}
if let (Some(body_regex), Some(body)) = (
parser.body.as_ref(),
self.conv.as_ref().and_then(|v| v.body()),
) {
regex_checks.push((body_regex, body.to_string()))
}
for (regex, text) in regex_checks {
if regex.is_match(&text) {
if parser.skip != Some(true) {
self.group = parser.group.as_ref().cloned();
self.scope = parser.scope.as_ref().cloned();
self.default_scope = parser.default_scope.as_ref().cloned();
return Ok(self);
} else {
return Err(AppError::GroupError(String::from(
"Skipping commit",
)));
}
}
}
}
if !filter {
Ok(self)
} else {
Err(AppError::GroupError(String::from(
"Commit does not belong to any group",
)))
}
}
/// Parses the commit using [`LinkParser`]s.
///
/// Sets the [`links`] of the commit.
///
/// [`links`]: Commit::links
pub fn parse_links(mut self, parsers: &[LinkParser]) -> Result<Self> {
for parser in parsers {
let regex = &parser.pattern;
let replace = &parser.href;
for mat in regex.find_iter(&self.message) {
let m = mat.as_str();
let text = if let Some(text_replace) = &parser.text {
regex.replace(m, text_replace).to_string()
} else {
m.to_string()
};
let href = regex.replace(m, replace);
self.links.push(Link {
text,
href: href.to_string(),
});
}
}
Ok(self)
}
/// Returns an iterator over this commit's [`Footer`]s, if this is a
/// conventional commit.
///
/// If this commit is not conventional, the returned iterator will be empty.
fn footers(&self) -> impl Iterator<Item = Footer<'_>> {
self.conv
.iter()
.flat_map(|conv| conv.footers().iter().map(Footer::from))
}
}
impl Serialize for Commit<'_> {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
/// A wrapper to serialize commit footers from an iterator using
/// `Serializer::collect_seq` without having to allocate in order to
/// `collect` the footers into a new to `Vec`.
struct SerializeFooters<'a>(&'a Commit<'a>);
impl Serialize for SerializeFooters<'_> {
fn serialize<S>(
&self,
serializer: S,
) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_seq(self.0.footers())
}
}
let mut commit = serializer.serialize_struct("Commit", 9)?;
commit.serialize_field("id", &self.id)?;
match &self.conv {
Some(conv) => {
commit.serialize_field("message", conv.description())?;
commit.serialize_field("body", &conv.body())?;
commit.serialize_field("footers", &SerializeFooters(self))?;
commit.serialize_field(
"group",
self.group.as_ref().unwrap_or(&conv.type_().to_string()),
)?;
commit.serialize_field(
"breaking_description",
&conv.breaking_description(),
)?;
commit.serialize_field("breaking", &conv.breaking())?;
commit.serialize_field(
"scope",
&self
.scope
.as_deref()
.or_else(|| conv.scope().map(|v| v.as_str()))
.or(self.default_scope.as_deref()),
)?;
}
None => {
commit.serialize_field("message", &self.message)?;
commit.serialize_field("group", &self.group)?;
commit.serialize_field(
"scope",
&self.scope.as_deref().or(self.default_scope.as_deref()),
)?;
}
}
commit.serialize_field("links", &self.links)?;
commit.serialize_field("conventional", &self.conv.is_some())?;
commit.end()
}
}
#[cfg(test)]
mod test {
use super::*;
use regex::Regex;
#[test]
fn conventional_commit() {
let test_cases = vec![
(
Commit::new(
String::from("123123"),
String::from("test(commit): add test"),
),
true,
),
(
Commit::new(String::from("124124"), String::from("xyz")),
false,
),
];
for (commit, is_conventional) in &test_cases {
assert_eq!(is_conventional, &commit.clone().into_conventional().is_ok())
}
let commit = test_cases[0]
.0
.clone()
.parse(
&[CommitParser {
message: Regex::new("test*").ok(),
body: None,
group: Some(String::from("test_group")),
default_scope: Some(String::from("test_scope")),
scope: None,
skip: None,
}],
false,
)
.unwrap();
assert_eq!(Some(String::from("test_group")), commit.group);
assert_eq!(Some(String::from("test_scope")), commit.default_scope);
}
#[test]
fn conventional_footers() {
let cfg = crate::config::GitConfig {
conventional_commits: Some(true),
..Default::default()
};
let test_cases = vec![
(
Commit::new(
String::from("123123"),
String::from(
"test(commit): add test\n\nSigned-off-by: Test User \
),
),
vec![Footer {
token: "Signed-off-by",
separator: ":",
value: "Test User <[email protected]>",
breaking: false,
}],
),
(
Commit::new(
String::from("123124"),
String::from(
"fix(commit): break stuff\n\nBREAKING CHANGE: This commit \
breaks stuff\nSigned-off-by: Test User <[email protected]>",
),
),
vec![
Footer {
token: "BREAKING CHANGE",
separator: ":",
value: "This commit breaks stuff",
breaking: true,
},
Footer {
token: "Signed-off-by",
separator: ":",
value: "Test User <[email protected]>",
breaking: false,
},
],
),
];
for (commit, footers) in &test_cases {
let commit = commit.process(&cfg).expect("commit should process");
assert_eq!(&commit.footers().collect::<Vec<_>>(), footers);
}
}
#[test]
fn parse_link() {
let test_cases = vec![
(
Commit::new(
String::from("123123"),
String::from("test(commit): add test\n\nBody with issue #123"),
),
true,
),
(
Commit::new(
String::from("123123"),
String::from(
"test(commit): add test\n\nImlement RFC456\n\nFixes: #456",
),
),
true,
),
];
for (commit, is_conventional) in &test_cases {
assert_eq!(is_conventional, &commit.clone().into_conventional().is_ok())
}
let commit = Commit::new(
String::from("123123"),
String::from("test(commit): add test\n\nImlement RFC456\n\nFixes: #455"),
);
let commit = commit
.parse_links(&[
LinkParser {
pattern: Regex::new("RFC(\\d+)").unwrap(),
href: String::from("rfc://$1"),
text: None,
},
LinkParser {
pattern: Regex::new("#(\\d+)").unwrap(),
href: String::from("https://github.com/$1"),
text: None,
},
])
.unwrap();
assert_eq!(
vec![
Link {
text: String::from("RFC456"),
href: String::from("rfc://456"),
},
Link {
text: String::from("#455"),
href: String::from("https://github.com/455"),
}
],
commit.links
);
}
}