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
use git;
use std::str;
use commit::{Commit, CommitList, Line};
use std::collections::HashMap;
use chrono::MIN_DATE;
use chrono::prelude::*;
use input::{Configuration, Conventions};
#[derive(Debug, Default, Serialize, Eq, PartialEq)]
pub struct ChangeLog {
pub scopes: Vec<Scope>,
pub commits: Vec<Commit>,
pub remote_url: Option<String>,
pub range: String,
pub date: String,
}
#[derive(Debug, Default, Serialize, Eq, PartialEq)]
pub struct Scope {
pub title: String,
pub categories: Vec<Category>,
}
#[derive(Debug, Default, Serialize, Eq, PartialEq)]
pub struct Category {
pub title: String,
pub changes: Vec<String>,
}
impl ChangeLog {
pub fn new() -> Self {
Self::from_log(Vec::new(), &Configuration::new())
}
pub fn from_range(range: &str, config: &Configuration) -> Self {
Self::from_log(vec![range.to_string()], config)
}
pub fn from_log(mut args: Vec<String>, config: &Configuration) -> Self {
if args.is_empty() {
if let Ok(Some(tag)) = git::last_tag() {
args.push(format!("{}..HEAD", tag))
} else {
args.push(String::from("HEAD^..HEAD"))
}
}
let header = args.join(" ");
let range = CommitList::from(args);
info!("Using revision range '{}'", range);
let mut log = Self::from(range, config);
log.range = header;
log
}
pub fn from<T: Iterator<Item = Commit>>(commits: T, config: &Configuration) -> Self {
let mut raw = RawReport::new();
let mut changelog = ChangeLog::default();
for commit in commits {
if raw.add(&commit, &config.conventions) {
trace!("Interesting commit {}", commit);
changelog.commits.push(commit);
} else {
debug!("No interesting changes in commit {}", &commit);
}
}
for scope in config.conventions.scope_titles() {
if let Some(mut categorized) = raw.slots.remove(&scope) {
let title = scope.to_owned();
let mut categories = Vec::new();
for category in config.conventions.category_titles() {
let title = category.to_owned();
if let Some(mut changes) = categorized.remove(&category) {
categories.push(Category { title, changes });
}
}
changelog.scopes.push({ Scope { title, categories } })
}
}
let remote = match config.output.remote {
Some(ref r) => r,
None => "origin",
};
changelog.remote_url = git::get_remote_url(remote).unwrap_or(None);
changelog.date = raw.date.format("%Y-%m-%d").to_string();
changelog
}
}
struct RawReport<'a> {
date: Date<Utc>,
slots: HashMap<&'a str, HashMap<&'a str, Vec<String>>>,
}
impl<'a> RawReport<'a> {
fn new() -> Self {
Self {
date: MIN_DATE,
slots: HashMap::default(),
}
}
fn add<'c>(&mut self, commit: &'c Commit, conventions: &'a Conventions) -> bool {
let mut interesting = false;
let mut current = Line::default();
for line in commit {
if line.category.is_some() {
interesting |= self.record(current, conventions);
current = Line::default();
current.scope = line.scope;
current.category = line.category;
}
if current.text.is_none() {
current.text = line.text
} else if let Some(mut text) = current.text.as_mut() {
text.push('\n');
text.push_str(&line.text.unwrap_or_default());
}
}
interesting |= self.record(current, conventions);
if let Ok(time) = DateTime::parse_from_rfc2822(&commit.time) {
let date = time.with_timezone(&Utc).date();
if date > self.date {
self.date = date;
}
}
interesting
}
fn record(&mut self, current: Line, conventions: &'a Conventions) -> bool {
let scope = conventions.scope_title(current.scope);
let category = conventions.category_title(current.category);
let interesting = category.is_some() && scope.is_some() && current.text.is_some();
if interesting {
self.slots
.entry(scope.unwrap())
.or_insert_with(HashMap::new)
.entry(category.unwrap())
.or_insert_with(Vec::new)
.push(current.text.unwrap());
}
interesting
}
}