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
use std::{borrow::Cow, time::SystemTime};
use crate::bstr::BString;
impl crate::Repository {
pub fn user_default(&self) -> git_actor::SignatureRef<'_> {
git_actor::SignatureRef {
name: "gitoxide".into(),
email: "gitoxide@localhost".into(),
time: git_date::Time::now_local_or_utc(),
}
}
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 committer_or_default(&self) -> git_actor::SignatureRef<'_> {
self.committer().unwrap_or_else(|| self.user_default())
}
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()
}
pub fn author_or_default(&self) -> git_actor::SignatureRef<'_> {
self.author().unwrap_or_else(|| self.user_default())
}
}
#[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<'_>, git_env: git_sec::Permission) -> Self {
fn env_var(name: &str) -> Option<BString> {
std::env::var_os(name).map(|value| git_path::into_bstr(Cow::Owned(value.into())).into_owned())
}
fn entity_in_section(name: &str, config: &git_config::File<'_>) -> (Option<BString>, Option<BString>) {
config
.section(name, None)
.map(|section| {
(
section.value("name").map(|v| v.into_owned()),
section.value("email").map(|v| v.into_owned()),
)
})
.unwrap_or_default()
}
let (mut committer_name, mut committer_email) = entity_in_section("committer", config);
let mut committer_date = None;
let (mut author_name, mut author_email) = entity_in_section("author", config);
let mut author_date = None;
let (user_name, mut user_email) = entity_in_section("user", config);
if git_env.eq(&git_sec::Permission::Allow) {
committer_name = committer_name.or_else(|| env_var("GIT_COMMITTER_NAME"));
committer_email = committer_email.or_else(|| env_var("GIT_COMMITTER_EMAIL"));
committer_date = std::env::var("GIT_COMMITTER_DATE")
.ok()
.and_then(|date| git_date::parse(&date, Some(SystemTime::now())).ok());
author_name = author_name.or_else(|| env_var("GIT_AUTHOR_NAME"));
author_email = author_email.or_else(|| env_var("GIT_AUTHOR_EMAIL"));
author_date = std::env::var("GIT_AUTHOR_DATE")
.ok()
.and_then(|date| git_date::parse(&date, Some(SystemTime::now())).ok());
user_email = user_email.or_else(|| env_var("EMAIL")); }
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,
},
}
}
}