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
use crate::vcs::git::error::Error;
use git2::Oid;
use std::{convert::TryFrom, str};
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Author {
pub name: String,
pub email: String,
pub time: git2::Time,
}
impl std::fmt::Debug for Author {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use std::cmp::Ordering;
let time = match self.time.offset_minutes().cmp(&0) {
Ordering::Equal => format!("{}", self.time.seconds()),
Ordering::Greater => format!("{}+{}", self.time.seconds(), self.time.offset_minutes()),
Ordering::Less => format!("{}{}", self.time.seconds(), self.time.offset_minutes()),
};
f.debug_struct("Author")
.field("name", &self.name)
.field("email", &self.email)
.field("time", &time)
.finish()
}
}
impl<'repo> TryFrom<git2::Signature<'repo>> for Author {
type Error = str::Utf8Error;
fn try_from(signature: git2::Signature) -> Result<Self, Self::Error> {
let name = str::from_utf8(signature.name_bytes())?.into();
let email = str::from_utf8(signature.email_bytes())?.into();
let time = signature.when();
Ok(Author { name, email, time })
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Commit {
pub id: Oid,
pub author: Author,
pub committer: Author,
pub message: String,
pub summary: String,
pub parents: Vec<Oid>,
}
impl<'repo> TryFrom<git2::Commit<'repo>> for Commit {
type Error = Error;
fn try_from(commit: git2::Commit) -> Result<Self, Self::Error> {
let id = commit.id();
let author = Author::try_from(commit.author())?;
let committer = Author::try_from(commit.committer())?;
let message_raw = commit.message_bytes();
let message = str::from_utf8(message_raw)?.into();
let summary_raw = commit.summary_bytes().ok_or(Error::MissingSummary)?;
let summary = str::from_utf8(summary_raw)?.into();
let parents = commit.parent_ids().collect();
Ok(Commit {
id,
author,
committer,
message,
summary,
parents,
})
}
}