Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 87fb932

Browse files
committed
Add method to return borrowed values for new name/email (GitoxideLabs#366)
That way, one _can_ optimize for performance and be zero copy after the mail map has been created. Maybe this can be useful in the estimate-hours program.
1 parent 1038dab commit 87fb932

3 files changed

Lines changed: 32 additions & 11 deletions

File tree

git-mailmap/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn parse_ignore_errors(buf: &[u8]) -> impl Iterator<Item = Entry<'_>> {
1414

1515
mod entry;
1616

17-
mod snapshot;
17+
pub mod snapshot;
1818

1919
#[derive(Default, Clone)]
2020
pub struct Snapshot {

git-mailmap/src/snapshot.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
use crate::Snapshot;
22
use bstr::{BStr, BString, ByteSlice};
3-
use git_actor::{Signature, SignatureRef};
3+
use git_actor::SignatureRef;
44
use std::cmp::Ordering;
55
use std::ops::Deref;
66

7+
pub struct ResolvedSignature<'a> {
8+
pub name: Option<&'a BStr>,
9+
pub email: Option<&'a BStr>,
10+
}
11+
12+
impl<'a> ResolvedSignature<'a> {
13+
fn try_new(new_email: Option<&'a BString>, new_name: Option<&'a BString>) -> Option<Self> {
14+
match (new_email, new_name) {
15+
(None, None) => None,
16+
(new_email, new_name) => Some(ResolvedSignature {
17+
email: new_email.map(|v| v.as_bstr()),
18+
name: new_name.map(|v| v.as_bstr()),
19+
}),
20+
}
21+
}
22+
}
23+
724
#[cfg_attr(test, derive(Debug))]
825
#[derive(Clone)]
926
enum EncodedString {
@@ -178,7 +195,7 @@ impl Snapshot {
178195
self
179196
}
180197

181-
pub fn try_resolve(&self, signature: &git_actor::SignatureRef<'_>) -> Option<git_actor::Signature> {
198+
pub fn try_resolve_ref<'a>(&'a self, signature: &git_actor::SignatureRef<'_>) -> Option<ResolvedSignature<'a>> {
182199
let email: EncodedStringRef<'_> = signature.email.into();
183200
let pos = self
184201
.entries_by_old_email
@@ -191,24 +208,28 @@ impl Snapshot {
191208
match entry.entries_by_old_name.binary_search_by(|e| e.old_name.cmp_ref(name)) {
192209
Ok(pos) => {
193210
let entry = &entry.entries_by_old_name[pos];
194-
enriched_signature(signature, entry.new_email.as_ref(), entry.new_name.as_ref())
211+
ResolvedSignature::try_new(entry.new_email.as_ref(), entry.new_name.as_ref())
195212
}
196-
Err(_) => enriched_signature(signature, entry.new_email.as_ref(), entry.new_name.as_ref()),
213+
Err(_) => ResolvedSignature::try_new(entry.new_email.as_ref(), entry.new_name.as_ref()),
197214
}
198215
}
199216

217+
pub fn try_resolve(&self, signature: &git_actor::SignatureRef<'_>) -> Option<git_actor::Signature> {
218+
let new = self.try_resolve_ref(signature)?;
219+
enriched_signature(signature, new)
220+
}
221+
200222
pub fn resolve(&self, signature: &git_actor::SignatureRef<'_>) -> git_actor::Signature {
201223
self.try_resolve(signature).unwrap_or_else(|| signature.to_owned())
202224
}
203225
}
204226

205227
fn enriched_signature(
206228
SignatureRef { name, email, time }: &SignatureRef<'_>,
207-
new_email: Option<&BString>,
208-
new_name: Option<&BString>,
209-
) -> Option<Signature> {
229+
new: ResolvedSignature<'_>,
230+
) -> Option<git_actor::Signature> {
210231
let time = *time;
211-
match (new_email, new_name) {
232+
match (new.email, new.name) {
212233
(Some(new_email), Some(new_name)) => git_actor::Signature {
213234
email: new_email.to_owned(),
214235
name: new_name.to_owned(),
@@ -227,7 +248,7 @@ fn enriched_signature(
227248
time,
228249
}
229250
.into(),
230-
(None, None) => None,
251+
(None, None) => unreachable!("BUG: ResolvedSignatures don't exist here when nothing is set"),
231252
}
232253
}
233254

git-mailmap/tests/snapshot/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn try_resolve() {
2323
assert_eq!(
2424
snapshot.try_resolve(&signature("Jane", "jane@desktop.(none)").to_ref()),
2525
Some(signature("Jane Doe", "[email protected]")),
26-
"fix name and email by email"
26+
"fix name and email by other email"
2727
);
2828

2929
assert_eq!(

0 commit comments

Comments
 (0)