Skip to content

Commit 6f7b23a

Browse files
committed
refactor
- remove unwraps() - reduce duplication
1 parent 1955b5b commit 6f7b23a

File tree

17 files changed

+113
-132
lines changed

17 files changed

+113
-132
lines changed

gix-merge/src/commit/virtual_merge_base.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ pub enum Error {
3030
}
3131

3232
pub(super) mod function {
33-
use std::convert::TryFrom;
34-
3533
use gix_object::FindExt;
3634

3735
use super::Error;
@@ -133,7 +131,7 @@ pub(super) mod function {
133131
) -> Result<gix_hash::ObjectId, Error> {
134132
let mut buf = Vec::new();
135133
let commit_ref = objects.find_commit(&parent_a, &mut buf)?;
136-
let mut commit = gix_object::Commit::try_from(commit_ref)?;
134+
let mut commit = commit_ref.to_owned()?;
137135
commit.parents = vec![parent_a, parent_b].into();
138136
commit.tree = tree_id;
139137
objects.write(&commit).map_err(Error::WriteObject)

gix-object/src/commit/decode.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,10 @@ pub fn commit<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8], StrContext>>(
4040
.context(StrContext::Expected(
4141
"zero or more 'parent <40 lowercase hex char>'".into(),
4242
)),
43-
(|i: &mut _| {
44-
parse::header_field(i, b"author", parse::signature_with_raw).map(|(signature, raw)| {
45-
let _ = signature;
46-
raw
47-
})
48-
})
49-
.context(StrContext::Expected("author <signature>".into())),
50-
(|i: &mut _| {
51-
parse::header_field(i, b"committer", parse::signature_with_raw).map(|(signature, raw)| {
52-
let _ = signature;
53-
raw
54-
})
55-
})
56-
.context(StrContext::Expected("committer <signature>".into())),
43+
(|i: &mut _| parse::header_field(i, b"author", parse::signature_and_consumed).map(|(_signature, raw)| raw))
44+
.context(StrContext::Expected("author <signature>".into())),
45+
(|i: &mut _| parse::header_field(i, b"committer", parse::signature_and_consumed).map(|(_signature, raw)| raw))
46+
.context(StrContext::Expected("committer <signature>".into())),
5747
opt(|i: &mut _| parse::header_field(i, b"encoding", take_till(0.., NL)))
5848
.context(StrContext::Expected("encoding <encoding>".into())),
5949
repeat(

gix-object/src/commit/mod.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::ops::Range;
33
use bstr::{BStr, BString, ByteSlice};
44
use winnow::prelude::*;
55

6+
use crate::parse::parse_signature;
67
use crate::{Commit, CommitRef, TagRef};
78

89
/// The well-known field name for gpg signatures.
@@ -74,11 +75,6 @@ impl<'a> CommitRef<'a> {
7475

7576
/// Access
7677
impl<'a> CommitRef<'a> {
77-
fn parse_signature(raw: &'a BStr) -> Result<gix_actor::SignatureRef<'a>, crate::decode::Error> {
78-
gix_actor::SignatureRef::from_bytes::<crate::decode::ParseError>(raw.as_ref())
79-
.map_err(|err| crate::decode::Error::with_err(err, raw.as_ref()))
80-
}
81-
8278
/// Return the `tree` fields hash digest.
8379
pub fn tree(&self) -> gix_hash::ObjectId {
8480
gix_hash::ObjectId::from_hex(self.tree).expect("prior validation of tree hash during parsing")
@@ -100,14 +96,14 @@ impl<'a> CommitRef<'a> {
10096
///
10197
/// This is different from the `author` field which may contain whitespace.
10298
pub fn author(&self) -> Result<gix_actor::SignatureRef<'a>, crate::decode::Error> {
103-
Self::parse_signature(self.author).map(|signature| signature.trim())
99+
parse_signature(self.author).map(|signature| signature.trim())
104100
}
105101

106102
/// Return the committer, with whitespace trimmed.
107103
///
108104
/// This is different from the `committer` field which may contain whitespace.
109105
pub fn committer(&self) -> Result<gix_actor::SignatureRef<'a>, crate::decode::Error> {
110-
Self::parse_signature(self.committer).map(|signature| signature.trim())
106+
parse_signature(self.committer).map(|signature| signature.trim())
111107
}
112108

113109
/// Returns a partially parsed message from which more information can be derived.
@@ -117,20 +113,20 @@ impl<'a> CommitRef<'a> {
117113

118114
/// Returns the time at which this commit was created, or a default time if it could not be parsed.
119115
pub fn time(&self) -> Result<gix_date::Time, crate::decode::Error> {
120-
Self::parse_signature(self.committer).map(|signature| signature.time().unwrap_or_default())
116+
parse_signature(self.committer).map(|signature| signature.time().unwrap_or_default())
121117
}
122118
}
123119

124120
/// Conversion
125121
impl CommitRef<'_> {
126122
/// Copy all fields of this instance into a fully owned commit, consuming this instance.
127123
pub fn into_owned(self) -> Result<Commit, crate::decode::Error> {
128-
Commit::try_from(self)
124+
self.try_into()
129125
}
130126

131127
/// Copy all fields of this instance into a fully owned commit, internally cloning this instance.
132128
pub fn to_owned(self) -> Result<Commit, crate::decode::Error> {
133-
Commit::try_from(self.clone())
129+
self.try_into()
134130
}
135131
}
136132

gix-object/src/object/convert.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::convert::TryFrom;
22

3+
use crate::parse::parse_signature;
34
use crate::{tree, Blob, BlobRef, Commit, CommitRef, Object, ObjectRef, Tag, TagRef, Tree, TreeRef};
45

56
impl TryFrom<TagRef<'_>> for Tag {
@@ -14,19 +15,13 @@ impl TryFrom<TagRef<'_>> for Tag {
1415
tagger,
1516
pgp_signature,
1617
} = other;
17-
let tagger = tagger
18-
.map(|raw| {
19-
gix_actor::SignatureRef::from_bytes::<crate::decode::ParseError>(raw.as_ref())
20-
.map_err(|err| crate::decode::Error::with_err(err, raw.as_ref()))
21-
})
22-
.transpose()?
23-
.map(Into::into);
18+
let untrimmed_tagger = tagger.map(parse_signature).transpose()?.map(Into::into);
2419
Ok(Tag {
2520
target: gix_hash::ObjectId::from_hex(target).expect("prior parser validation"),
2621
name: name.to_owned(),
2722
target_kind,
2823
message: message.to_owned(),
29-
tagger,
24+
tagger: untrimmed_tagger,
3025
pgp_signature: pgp_signature.map(ToOwned::to_owned),
3126
})
3227
}
@@ -39,24 +34,23 @@ impl TryFrom<CommitRef<'_>> for Commit {
3934
let CommitRef {
4035
tree,
4136
parents,
42-
author: author_raw,
43-
committer: committer_raw,
37+
author,
38+
committer,
4439
encoding,
4540
message,
4641
extra_headers,
4742
} = other;
48-
let author = gix_actor::SignatureRef::from_bytes::<crate::decode::ParseError>(author_raw.as_ref())
49-
.map_err(|err| crate::decode::Error::with_err(err, author_raw.as_ref()))?;
50-
let committer = gix_actor::SignatureRef::from_bytes::<crate::decode::ParseError>(committer_raw.as_ref())
51-
.map_err(|err| crate::decode::Error::with_err(err, committer_raw.as_ref()))?;
43+
44+
let untrimmed_author = parse_signature(author)?;
45+
let untrimmed_committer = parse_signature(committer)?;
5246
Ok(Commit {
5347
tree: gix_hash::ObjectId::from_hex(tree).expect("prior parser validation"),
5448
parents: parents
5549
.iter()
5650
.map(|parent| gix_hash::ObjectId::from_hex(parent).expect("prior parser validation"))
5751
.collect(),
58-
author: author.into(),
59-
committer: committer.into(),
52+
author: untrimmed_author.into(),
53+
committer: untrimmed_committer.into(),
6054
encoding: encoding.map(ToOwned::to_owned),
6155
message: message.to_owned(),
6256
extra_headers: extra_headers
@@ -113,8 +107,8 @@ impl TryFrom<ObjectRef<'_>> for Object {
113107
Ok(match v {
114108
ObjectRef::Tree(v) => Object::Tree(v.into()),
115109
ObjectRef::Blob(v) => Object::Blob(v.into()),
116-
ObjectRef::Commit(v) => Object::Commit(Commit::try_from(v)?),
117-
ObjectRef::Tag(v) => Object::Tag(Tag::try_from(v)?),
110+
ObjectRef::Commit(v) => Object::Commit(v.try_into()?),
111+
ObjectRef::Tag(v) => Object::Tag(v.try_into()?),
118112
})
119113
}
120114
}

gix-object/src/object/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,14 @@ impl<'a> ObjectRef<'a> {
217217
///
218218
/// Note that this is an expensive operation.
219219
pub fn into_owned(self) -> Result<Object, crate::decode::Error> {
220-
Object::try_from(self)
220+
self.try_into()
221221
}
222222

223223
/// Convert this immutable object into its mutable counterpart.
224224
///
225225
/// Note that this is an expensive operation.
226226
pub fn to_owned(&self) -> Result<Object, crate::decode::Error> {
227-
Object::try_from(self.clone())
227+
self.clone().try_into()
228228
}
229229
}
230230

gix-object/src/parse.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub(crate) fn signature<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8], StrC
7272
gix_actor::signature::decode(i)
7373
}
7474

75-
pub(crate) fn signature_with_raw<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8], StrContext>>(
75+
pub(crate) fn signature_and_consumed<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8], StrContext>>(
7676
i: &mut &'a [u8],
7777
) -> ModalResult<(gix_actor::SignatureRef<'a>, &'a BStr), E> {
7878
let original = *i;
@@ -81,3 +81,8 @@ pub(crate) fn signature_with_raw<'a, E: ParserError<&'a [u8]> + AddContext<&'a [
8181
(signature, original[..consumed].as_bstr())
8282
})
8383
}
84+
85+
pub(crate) fn parse_signature(raw: &BStr) -> Result<gix_actor::SignatureRef<'_>, crate::decode::Error> {
86+
gix_actor::SignatureRef::from_bytes::<crate::decode::ParseError>(raw.as_ref())
87+
.map_err(|err| crate::decode::Error::with_err(err, raw.as_ref()))
88+
}

gix-object/src/tag/decode.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,8 @@ pub fn git_tag<'a, E: ParserError<&'a [u8]> + AddContext<&'a [u8], StrContext>>(
1919
.context(StrContext::Expected("type <object kind>".into())),
2020
(|i: &mut _| parse::header_field(i, b"tag", take_while(1.., |b| b != NL[0])))
2121
.context(StrContext::Expected("tag <version>".into())),
22-
opt(|i: &mut _| {
23-
parse::header_field(i, b"tagger", parse::signature_with_raw).map(|(signature, raw)| {
24-
let _ = signature;
25-
raw
26-
})
27-
})
28-
.context(StrContext::Expected("tagger <signature>".into())),
22+
opt(|i: &mut _| parse::header_field(i, b"tagger", parse::signature_and_consumed).map(|(_signature, raw)| raw))
23+
.context(StrContext::Expected("tagger <signature>".into())),
2924
terminated(message, eof),
3025
)
3126
.map(|(target, kind, tag_version, tagger, (message, pgp_signature))| TagRef {

gix-object/src/tag/mod.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use winnow::prelude::*;
2-
1+
use crate::parse::parse_signature;
32
use crate::TagRef;
3+
use winnow::prelude::*;
44

55
mod decode;
66

@@ -26,17 +26,15 @@ impl<'a> TagRef<'a> {
2626

2727
/// Return the tagger, if present.
2828
pub fn tagger(&self) -> Result<Option<gix_actor::SignatureRef<'a>>, crate::decode::Error> {
29-
self.tagger
30-
.map(|raw| {
31-
gix_actor::SignatureRef::from_bytes::<crate::decode::ParseError>(raw.as_ref())
32-
.map_err(|err| crate::decode::Error::with_err(err, raw.as_ref()))
33-
.map(|signature| signature.trim())
34-
})
35-
.transpose()
29+
Ok(self
30+
.tagger
31+
.map(parse_signature)
32+
.transpose()?
33+
.map(|signature| signature.trim()))
3634
}
3735

3836
/// Copy all data into a fully-owned instance.
3937
pub fn into_owned(self) -> Result<crate::Tag, crate::decode::Error> {
40-
crate::Tag::try_from(self)
38+
self.try_into()
4139
}
4240
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object 01dd4e2a978a9f5bd773dae6da7aa4a5ac1cdbbc
2+
type commit
3+
tag empty
4+
tagger Sebastian Thiel <sebastian.thiel@icloud.com > 1592381636 +0800
5+

gix-object/tests/object/commit/from_bytes.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use smallvec::SmallVec;
44

55
use crate::{
66
commit::{LONG_MESSAGE, MERGE_TAG, SIGNATURE},
7-
fixture_name, hex_to_id, linus_signature, signature,
7+
fixture_name, hex_to_id, linus_signature,
88
};
99

1010
#[test]
@@ -26,12 +26,14 @@ fn invalid_timestsamp() {
2626
}
2727

2828
#[test]
29-
fn invalid_email_of_committer() {
29+
fn invalid_email_of_committer() -> crate::Result {
3030
let actor = gix_actor::SignatureRef {
3131
name: b"Gregor Hartmann".as_bstr(),
3232
email: b"gh <Gregor Hartmann<gh@openoffice.org".as_bstr(),
3333
time: "1282910542 +0200",
3434
};
35+
36+
let mut buf = vec![];
3537
let backing = fixture_name("commit", "invalid-actor.txt");
3638
let commit = CommitRef::from_bytes(&backing).expect("ignore strangely formed actor format");
3739
assert_eq!(
@@ -46,8 +48,17 @@ fn invalid_email_of_committer() {
4648
extra_headers: vec![]
4749
}
4850
);
49-
assert_eq!(commit.author().unwrap(), actor);
50-
assert_eq!(commit.committer().unwrap(), actor);
51+
assert_eq!(commit.author()?, actor);
52+
assert_eq!(commit.committer()?, actor);
53+
54+
commit.write_to(&mut buf).expect("we can write invalid actors back");
55+
assert_eq!(
56+
CommitRef::from_bytes(&buf).expect("this is the same commit and it can be parsed"),
57+
commit,
58+
"round-tripping works"
59+
);
60+
61+
Ok(())
5162
}
5263

5364
#[test]
@@ -123,8 +134,8 @@ fn mergetag() -> crate::Result {
123134
assert_eq!(commit, expected);
124135
assert_eq!(commit.extra_headers().find_all("mergetag").count(), 1);
125136
assert_eq!(commit.extra_headers().mergetags().count(), 1);
126-
assert_eq!(commit.author().unwrap(), linus_signature("1591996221 -0700"));
127-
assert_eq!(commit.committer().unwrap(), linus_signature("1591996221 -0700"));
137+
assert_eq!(commit.author()?, linus_signature("1591996221 -0700"));
138+
assert_eq!(commit.committer()?, linus_signature("1591996221 -0700"));
128139
Ok(())
129140
}
130141

@@ -244,8 +255,8 @@ Signed-off-by: Kim Altintop <kim@eagain.st>"
244255
extra_headers: vec![(b"gpgsig".as_bstr(), b"-----BEGIN PGP SIGNATURE-----\n\niHUEABYIAB0WIQSuZwcGWSQItmusNgR5URpSUCnwXQUCYT7xpAAKCRB5URpSUCnw\nXWB3AP9q323HlxnI8MyqszNOeYDwa7Y3yEZaUM2y/IRjz+z4YQEAq0yr1Syt3mrK\nOSFCqL2vDm3uStP+vF31f6FnzayhNg0=\n=Mhpp\n-----END PGP SIGNATURE-----\n".as_bstr().into())]
245256
}
246257
);
247-
assert_eq!(commit.author().unwrap(), kim);
248-
assert_eq!(commit.committer().unwrap(), kim);
258+
assert_eq!(commit.author()?, kim);
259+
assert_eq!(commit.committer()?, kim);
249260
let message = commit.message();
250261
assert_eq!(message.title, "test: use gitoxide for link-git-protocol tests");
251262
assert_eq!(
@@ -359,12 +370,3 @@ fn bogus_multi_gpgsig_header() -> crate::Result {
359370
);
360371
Ok(())
361372
}
362-
363-
#[test]
364-
fn author_method_returns_trimmed_signature() -> crate::Result {
365-
let backing = fixture_name("commit", "unsigned.txt");
366-
let commit = CommitRef::from_bytes(&backing)?;
367-
assert_eq!(commit.author().unwrap(), signature("1592437401 +0800"));
368-
assert_eq!(commit.committer().unwrap(), signature("1592437401 +0800"));
369-
Ok(())
370-
}

0 commit comments

Comments
 (0)