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
use regex::Regex;
use std::path::Path;
use std::sync::LazyLock;
use std::{cell::OnceCell, str::FromStr};
use crate::{Actor, Error, ModifiedFile, Repository};
/// Iterate all co-author matches in the haystack string formatting the return
/// string to be formatted as "Name <Email>"
fn iter_co_authors(haystack: &str) -> impl Iterator<Item = &str> {
const CO_AUTHOR_REGEX: &str = r"(?m)^Co-authored-by: (.*) <(.*?)>$";
// Regex should always compile hence bare unwrap.
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(CO_AUTHOR_REGEX).unwrap());
let prefix = "Co-authored-by:";
RE.find_iter(haystack).map(move |re_match| {
re_match
.as_str()
.strip_prefix(prefix)
.unwrap_or_default()
.trim()
})
}
/// A singular git commit for the repository being inspected
pub struct Commit<'repo> {
inner: git2::Commit<'repo>,
ctx: &'repo Repository,
cache: OnceCell<git2::Diff<'repo>>,
}
impl<'repo> Commit<'repo> {
/// Instantiate a new Commit object from a git2 commit
pub fn new(commit: git2::Commit<'repo>, repository: &'repo Repository) -> Self {
Self {
inner: commit.to_owned(),
ctx: repository,
cache: OnceCell::new(),
}
}
/// Return the commit hash
pub fn hash(&self) -> String {
self.inner.id().to_string()
}
/// Return the commit message if it exists
pub fn msg(&self) -> Option<&str> {
self.inner.message()
}
/// Return the commit author
pub fn author(&self) -> Actor {
Actor::new(self.inner.author())
}
/// Return the co-authors as listed in the commit message
///
/// Lazilly returning as an iterator means the co-authors, if entered more
/// than once, will **not** be de-duplicated.
pub fn co_authors(&self) -> impl Iterator<Item = Result<Actor, Error>> {
let commit_msg = self.msg().unwrap_or_default();
iter_co_authors(commit_msg).map(Actor::from_str)
}
/// Return the commit committer
pub fn committer(&self) -> Actor {
Actor::new(self.inner.committer())
}
/// Iterate all utf-8 branch names that the current commit is contained in
///
/// ## Note
///
/// Potentially expensive method. Take caution when using within a loop.
pub fn branches(&self) -> Result<impl Iterator<Item = Result<String, Error>>, Error> {
self.branch_iterator(None)
}
/// Iterate all **local** utf-8 branch names that the current commit is contained in
///
/// ## Note
///
/// Potentially expensive method. Take caution when using within a loop.
pub fn local_branches(&self) -> Result<impl Iterator<Item = Result<String, Error>>, Error> {
let flag = Some(git2::BranchType::Local);
self.branch_iterator(flag)
}
/// Iterate all **remote** utf-8 branch names that the current commit is contained in
///
/// ## Note
///
/// Potentially expensive method. Take caution when using within a loop.
pub fn remote_branches(&self) -> Result<impl Iterator<Item = Result<String, Error>>, Error> {
let flag = Some(git2::BranchType::Remote);
self.branch_iterator(flag)
}
/// Retrun the commit parents as objects
pub fn parent_commits(&self) -> impl Iterator<Item = Result<Commit<'_>, Error>> {
self.inner.parent_ids().map(|oid| {
self.ctx
.raw()
.find_commit(oid)
.map_err(Error::Git)
.map(|gitc| Commit::new(gitc, self.ctx))
})
}
/// Return the commit parents sha
pub fn parents(&self) -> impl Iterator<Item = String> {
self.inner.parent_ids().map(|p| p.to_string())
}
/// Return whether the commit is a merge commit
pub fn is_merge(&self) -> bool {
self.inner.parent_count() > 1
}
/// Checks if the current commit is reachable from "main" or "master"
pub fn in_main(&self) -> Result<bool, Error> {
let b = self
.local_branches()?
.collect::<Vec<Result<String, Error>>>();
Ok(b.contains(&Ok("main".to_string())) || b.contains(&Ok("master".to_string())))
}
/// Return an iterator over the modified files that belong to a commit
pub fn mod_files(&self) -> Result<impl Iterator<Item = ModifiedFile<'_>>, Error> {
let diff = self.diff()?;
Ok((0..diff.deltas().len()).map(move |n| ModifiedFile::new(diff, n)))
}
/// The number of insertions in the commit
pub fn insertions(&self) -> Result<usize, Error> {
Ok(self.stats()?.insertions())
}
/// The number of deletions in the commit
pub fn deletions(&self) -> Result<usize, Error> {
Ok(self.stats()?.deletions())
}
/// The total number of lines modified in the commit
pub fn lines(&self) -> Result<usize, Error> {
Ok(self.insertions()? + self.deletions()?)
}
/// The number of files modified in the commit
pub fn files(&self) -> Result<usize, Error> {
Ok(self.stats()?.files_changed())
}
/// Return the project path that the commit belongs to
pub fn project_path(&self) -> &Path {
let git_folder = self.ctx.raw().path();
// Parent dir should always be infallible
git_folder.parent().unwrap()
}
/// Return the project name based on the project path
pub fn project_name(&self) -> Option<&str> {
self.project_path().file_name().and_then(|s| s.to_str())
}
//TODO: Should stats also be cached?
/// Return the git2 Stats from the commits diff
fn stats(&self) -> Result<git2::DiffStats, Error> {
let diff = self.diff()?;
diff.stats().map_err(Error::Git)
}
/// Return the git diff for the current commit within the context of a
/// repository.
//TODO: https://github.com/segfault-merchant/git-stratum/issues/32
fn diff(&self) -> Result<&git2::Diff<'repo>, Error> {
let diff = self.calculate_diff()?;
Ok(self.cache.get_or_init(|| diff))
}
/// Diff the current commit to it's parent(s) adjusting strategy based on the
/// number of parents
fn calculate_diff(&self) -> Result<git2::Diff<'repo>, Error> {
let this_tree = self.inner.tree().ok();
let parent_tree = self.resolve_parent_tree()?;
self.ctx
.raw()
//TODO: Expose opts?
.diff_tree_to_tree(parent_tree.as_ref(), this_tree.as_ref(), None)
.map_err(Error::Git)
}
/// Resolve to the correct parent tree changing strategies based on number
/// of parents.
fn resolve_parent_tree(&self) -> Result<Option<git2::Tree<'_>>, Error> {
Ok(match self.inner.parent_count() {
0 => None,
1 => self.inner.parent(0).map_err(Error::Git)?.tree().ok(),
//TODO: Resolve merge commit process
_ => return Err(Error::PathError("Placeholder error".to_string())),
})
}
/// Check if a commit contains a branch
///
/// If an error occurs returns false, this is done so any erroring branches
/// are filtered out of any dependant processes
fn commit_contains_branch(&self, branch: git2::Oid, commit: git2::Oid) -> bool {
self.ctx.raw().graph_descendant_of(branch, commit).is_ok()
}
/// Iterate over the specified branch types, None will return all branches
fn branch_iterator(
&self,
bt: Option<git2::BranchType>,
) -> Result<impl Iterator<Item = Result<String, Error>>, Error> {
let commit_id = self.inner.id();
let branches = self.ctx.raw().branches(bt).map_err(Error::Git)?;
Ok(branches.filter_map(move |res| {
let branch = match res {
Ok(v) => v.0,
Err(e) => return Some(Err(Error::Git(e))),
};
// If a branch does not have a valid target then filter that
// branch out
// TODO: Is this excluding a subset of symbolic references
let oid = match branch.get().target() {
Some(v) => v,
None => return None,
};
// Filter out a branch if the commit does NOT contain it
if !self.commit_contains_branch(oid, commit_id) {
return None;
}
match branch.name() {
Ok(Some(name)) => Some(Ok(name.to_string())),
Ok(None) => None, // drop non-utf8 branches
Err(e) => Some(Err(Error::Git(e))),
}
}))
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::{Local, Repository, common::init_repo};
fn commit_fixture<F, R>(f: F) -> R
where
F: FnOnce(&Repository<Local>, &Commit) -> R,
{
let repo = init_repo();
let repo = Repository::<Local>::from_repository(repo);
let commit = repo.head().expect("Failed to get HEAD");
f(&repo, &commit)
}
#[test]
fn test_stat() {
commit_fixture(|_, commit| {
// Won't compile if return type is bad, stat otherwise checked in insertions
// and deletions test functions
let _: git2::DiffStats = commit
.stats()
.expect("Failed to construct git2 Stats object");
});
}
#[test]
fn test_iter_matches() {
let haystack = "Co-authored-by: John <john@example.com>";
assert_eq!(iter_co_authors(haystack).collect::<Vec<&str>>().len(), 1);
let haystack = "No matches expected";
assert_eq!(iter_co_authors(haystack).collect::<Vec<&str>>().len(), 0);
}
}