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
use std::time::SystemTime;
use crate::bstr::{BString, ByteSlice};
impl crate::Repository {
pub fn committer(&self) -> Option<git_actor::SignatureRef<'_>> {
let p = self.config.personas();
git_actor::SignatureRef {
name: p.committer.name.as_ref().or(p.user.name.as_ref()).map(|v| v.as_ref())?,
email: p
.committer
.email
.as_ref()
.or(p.user.email.as_ref())
.map(|v| v.as_ref())?,
time: p.committer.time.unwrap_or_else(git_date::Time::now_local_or_utc),
}
.into()
}
pub fn author(&self) -> Option<git_actor::SignatureRef<'_>> {
let p = self.config.personas();
git_actor::SignatureRef {
name: p.author.name.as_ref().or(p.user.name.as_ref()).map(|v| v.as_ref())?,
email: p.author.email.as_ref().or(p.user.email.as_ref()).map(|v| v.as_ref())?,
time: p.author.time.unwrap_or_else(git_date::Time::now_local_or_utc),
}
.into()
}
}
#[derive(Debug, Clone)]
pub(crate) struct Entity {
pub name: Option<BString>,
pub email: Option<BString>,
pub time: Option<git_actor::Time>,
}
#[derive(Debug, Clone)]
pub(crate) struct Personas {
user: Entity,
committer: Entity,
author: Entity,
}
impl Personas {
pub fn from_config_and_env(config: &git_config::File<'_>) -> Self {
fn entity_in_section(
section_name: &str,
config: &git_config::File<'_>,
fallback: bool,
) -> (Option<BString>, Option<BString>) {
let fallback = fallback
.then(|| config.section("gitoxide", Some(section_name.into())).ok())
.flatten();
(
config
.string(section_name, None, "name")
.or_else(|| fallback.as_ref().and_then(|s| s.value("nameFallback")))
.map(|v| v.into_owned()),
config
.string(section_name, None, "email")
.or_else(|| fallback.as_ref().and_then(|s| s.value("emailFallback")))
.map(|v| v.into_owned()),
)
}
let now = SystemTime::now();
let parse_date = |key: &str| -> Option<git_date::Time> {
config.string_by_key(key).and_then(|date| {
date.to_str()
.ok()
.and_then(|date| git_date::parse(date, Some(now)).ok())
})
};
let (committer_name, committer_email) = entity_in_section("committer", config, true);
let (author_name, author_email) = entity_in_section("author", config, true);
let (user_name, mut user_email) = entity_in_section("user", config, false);
let committer_date = parse_date("gitoxide.commit.committerDate");
let author_date = parse_date("gitoxide.commit.authorDate");
user_email = user_email.or_else(|| config.string_by_key("gitoxide.user.email").map(|v| v.into_owned()));
Personas {
user: Entity {
name: user_name,
email: user_email,
time: None,
},
committer: Entity {
name: committer_name,
email: committer_email,
time: committer_date,
},
author: Entity {
name: author_name,
email: author_email,
time: author_date,
},
}
}
}