mit_commit_message_lints/external/
git2.rs1use std::{collections::BTreeMap, convert::TryFrom, path::PathBuf};
2
3use git2::{Config, Repository, RepositoryState};
4use miette::{IntoDiagnostic, Report, Result};
5
6use crate::{
7 external::{vcs::RepoState, Vcs},
8 mit::{Author, Authors},
9};
10
11#[allow(missing_debug_implementations)]
13pub struct Git2 {
14 config_snapshot: Config,
15 config_live: Config,
16 state: Option<RepositoryState>,
17}
18
19impl Git2 {
20 #[must_use]
24 pub fn new(mut config: Config, state: Option<RepositoryState>) -> Self {
25 Self {
26 config_snapshot: config.snapshot().unwrap(),
27 config_live: config,
28 state,
29 }
30 }
31
32 fn config_defined(&self, lint_name: &str) -> Result<bool> {
33 Ok(self
34 .config_snapshot
35 .entries(Some(lint_name))
36 .into_diagnostic()?
37 .next()
38 .is_some())
39 }
40}
41
42impl Vcs for Git2 {
43 fn entries(&self, glob: Option<&str>) -> Result<Vec<String>> {
44 let mut entries = vec![];
45 let mut item = self.config_snapshot.entries(glob).into_diagnostic()?;
46 while let Some(entry) = item.next() {
47 if let Some(name) = entry.into_diagnostic()?.name() {
48 entries.push(name.into());
49 }
50 }
51
52 Ok(entries)
53 }
54
55 fn get_bool(&self, name: &str) -> Result<Option<bool>> {
56 if self.config_defined(name)? {
57 Ok(Some(self.config_snapshot.get_bool(name).into_diagnostic()?))
58 } else {
59 Ok(None)
60 }
61 }
62
63 fn get_str(&self, name: &str) -> Result<Option<&str>> {
64 let defined = self.config_defined(name)?;
65
66 if defined {
67 self.config_snapshot
68 .get_str(name)
69 .map(Some)
70 .into_diagnostic()
71 } else {
72 Ok(None)
73 }
74 }
75
76 fn get_i64(&self, name: &str) -> Result<Option<i64>> {
77 let defined = self.config_defined(name)?;
78
79 if defined {
80 self.config_snapshot
81 .get_i64(name)
82 .map(Some)
83 .into_diagnostic()
84 } else {
85 Ok(None)
86 }
87 }
88
89 fn set_str(&mut self, name: &str, value: &str) -> Result<()> {
90 self.config_live.set_str(name, value).into_diagnostic()?;
91
92 let config = self.config_live.snapshot().into_diagnostic()?;
93
94 self.config_snapshot = config;
95
96 Ok(())
97 }
98
99 fn set_i64(&mut self, name: &str, value: i64) -> Result<()> {
100 self.config_live.set_i64(name, value).into_diagnostic()?;
101
102 let config = self.config_live.snapshot().into_diagnostic()?;
103 self.config_snapshot = config;
104
105 Ok(())
106 }
107
108 fn remove(&mut self, name: &str) -> Result<()> {
109 self.config_live.remove(name).into_diagnostic()?;
110
111 let config = self.config_live.snapshot().into_diagnostic()?;
112 self.config_snapshot = config;
113
114 Ok(())
115 }
116
117 fn state(&self) -> Option<RepoState> {
118 match self.state {
119 None => None,
120 Some(RepositoryState::ApplyMailbox) => Some(RepoState::ApplyMailbox),
121 Some(RepositoryState::Clean) => Some(RepoState::Clean),
122 Some(RepositoryState::Merge) => Some(RepoState::Merge),
123 Some(RepositoryState::Revert) => Some(RepoState::Revert),
124 Some(RepositoryState::RevertSequence) => Some(RepoState::RevertSequence),
125 Some(RepositoryState::CherryPick) => Some(RepoState::CherryPick),
126 Some(RepositoryState::CherryPickSequence) => Some(RepoState::CherryPickSequence),
127 Some(RepositoryState::Bisect) => Some(RepoState::Bisect),
128 Some(RepositoryState::Rebase) => Some(RepoState::Rebase),
129 Some(RepositoryState::RebaseInteractive) => Some(RepoState::RebaseInteractive),
130 Some(RepositoryState::RebaseMerge) => Some(RepoState::RebaseMerge),
131 Some(RepositoryState::ApplyMailboxOrRebase) => Some(RepoState::ApplyMailboxOrRebase),
132 }
133 }
134}
135
136impl TryFrom<PathBuf> for Git2 {
137 type Error = Report;
138
139 fn try_from(current_dir: PathBuf) -> Result<Self, Self::Error> {
140 Repository::discover(current_dir)
141 .and_then(|repo| {
142 let state = repo.state();
143 repo.config().map(|config| (config, Some(state)))
144 })
145 .or_else(|_| Config::open_default().map(|config| (config, None)))
146 .map(|(config, state)| Self::new(config, state))
147 .into_diagnostic()
148 }
149}
150
151impl TryFrom<&'_ Git2> for Authors<'_> {
152 type Error = Report;
153
154 fn try_from(vcs: &'_ Git2) -> Result<Self, Self::Error> {
155 let raw_entries: BTreeMap<String, BTreeMap<String, String>> = vcs
156 .entries(Some("mit.author.config.*"))?
157 .iter()
158 .map(|key| (key, key.trim_start_matches("mit.author.config.")))
159 .map(|(key, parts)| (key, parts.split_terminator('.').collect::<Vec<_>>()))
160 .try_fold::<_, _, Result<_, Self::Error>>(
161 BTreeMap::new(),
162 |mut acc, (key, fragments)| {
163 let mut fragment_iterator = fragments.iter();
164 let initial = String::from(*fragment_iterator.next().unwrap());
165 let part = String::from(*fragment_iterator.next().unwrap());
166
167 let mut existing: BTreeMap<String, String> =
168 acc.get(&initial).cloned().unwrap_or_default();
169 existing.insert(part, String::from(vcs.get_str(key)?.unwrap()));
170
171 acc.insert(initial, existing);
172 Ok(acc)
173 },
174 )?;
175
176 Ok(Self::new(
177 raw_entries
178 .iter()
179 .filter_map(|(key, cfg)| {
180 let name = cfg.get("name").cloned();
181 let email = cfg.get("email").cloned();
182 let signingkey: Option<String> = cfg.get("signingkey").cloned();
183
184 match (name, email, signingkey) {
185 (Some(name), Some(email), None) => {
186 Some((key, Author::new(name.into(), email.into(), None)))
187 }
188 (Some(name), Some(email), Some(signingkey)) => Some((
189 key,
190 Author::new(name.into(), email.into(), Some(signingkey.into())),
191 )),
192 _ => None,
193 }
194 })
195 .fold(
196 BTreeMap::new(),
197 |mut acc: BTreeMap<String, Author<'_>>, (key, value): (&String, Author<'_>)| {
198 acc.insert(key.clone(), value);
199 acc
200 },
201 ),
202 ))
203 }
204}