Skip to content

Commit c51a925

Browse files
committed
feat: CommitRefIter::(author|committer)(), better usability (#364)
The methods returning an iterator are now consuming, which allows them to be nested by callers.
1 parent 91065cd commit c51a925

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

git-object/src/commit/ref_iter.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ impl<'a> CommitRefIter<'a> {
8383
_ => None,
8484
})
8585
}
86+
87+
/// Returns the author signature if there is no decoding error.
88+
/// Errors are coerced into options, hiding whether there was an error or not. The caller knows if there was an error or not.
89+
pub fn author(&mut self) -> Option<git_actor::SignatureRef<'_>> {
90+
self.find_map(|t| match t {
91+
Ok(Token::Author { signature }) => Some(signature),
92+
_ => None,
93+
})
94+
}
95+
96+
/// Returns the message if there is no decoding error.
97+
/// Errors are coerced into options, hiding whether there was an error or not. The caller knows if there was an error or not.
98+
pub fn message(&mut self) -> Option<&'a BStr> {
99+
self.find_map(|t| match t {
100+
Ok(Token::Message(msg)) => Some(msg),
101+
_ => None,
102+
})
103+
}
86104
}
87105

88106
impl<'a> CommitRefIter<'a> {

git-object/tests/immutable/commit/iter.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ fn newline_right_after_signature_multiline_header() -> crate::Result {
2424

2525
#[test]
2626
fn signed_with_encoding() -> crate::Result {
27+
let input = fixture_bytes("commit", "signed-with-encoding.txt");
28+
let mut iter = CommitRefIter::from_bytes(&input);
2729
assert_eq!(
28-
CommitRefIter::from_bytes(&fixture_bytes("commit", "signed-with-encoding.txt"))
29-
.collect::<Result<Vec<_>, _>>()?,
30+
iter.collect::<Result<Vec<_>, _>>()?,
3031
vec![
3132
Token::Tree {
3233
id: hex_to_id("1973afa74d87b2bb73fa884aaaa8752aec43ea88")
@@ -45,6 +46,9 @@ fn signed_with_encoding() -> crate::Result {
4546
Token::Message(b"encoding & sig".as_bstr()),
4647
]
4748
);
49+
50+
assert_eq!(iter.author(), Some(signature(1592448995)));
51+
assert_eq!(iter.committer(), Some(signature(1592449083)));
4852
Ok(())
4953
}
5054

@@ -135,8 +139,10 @@ fn error_handling() -> crate::Result {
135139

136140
#[test]
137141
fn mergetag() -> crate::Result {
142+
let input = fixture_bytes("commit", "mergetag.txt");
143+
let mut iter = CommitRefIter::from_bytes(&input);
138144
assert_eq!(
139-
CommitRefIter::from_bytes(&fixture_bytes("commit", "mergetag.txt")).collect::<Result<Vec<_>, _>>()?,
145+
iter.collect::<Result<Vec<_>, _>>()?,
140146
vec![
141147
Token::Tree {
142148
id: hex_to_id("1c61918031bf2c7fab9e17dde3c52a6a9884fcb5")
@@ -154,18 +160,17 @@ fn mergetag() -> crate::Result {
154160
signature: linus_signature(1591996221)
155161
},
156162
Token::ExtraHeader((b"mergetag".as_bstr(), MERGE_TAG.as_bytes().as_bstr().into())),
157-
Token::Message(LONG_MESSAGE.as_bytes().as_bstr()),
163+
Token::Message(LONG_MESSAGE.into()),
158164
]
159165
);
160166
assert_eq!(
161-
CommitRefIter::from_bytes(&fixture_bytes("commit", "mergetag.txt"))
162-
.parent_ids()
163-
.collect::<Vec<_>>(),
167+
iter.parent_ids().collect::<Vec<_>>(),
164168
vec![
165169
hex_to_id("44ebe016df3aad96e3be8f95ec52397728dd7701"),
166170
hex_to_id("8d485da0ddee79d0e6713405694253d401e41b93")
167171
]
168172
);
173+
assert_eq!(iter.message(), Some(LONG_MESSAGE.into()));
169174
Ok(())
170175
}
171176

@@ -179,33 +184,31 @@ mod method {
179184

180185
#[test]
181186
fn tree_id() -> crate::Result {
187+
let input = fixture_bytes("commit", "unsigned.txt");
188+
let iter = CommitRefIter::from_bytes(&input);
182189
assert_eq!(
183-
CommitRefIter::from_bytes(&fixture_bytes("commit", "unsigned.txt")).tree_id(),
190+
iter.clone().tree_id(),
184191
Some(hex_to_id("1b2dfb4ac5e42080b682fc676e9738c94ce6d54d"))
185192
);
186193
assert_eq!(
187-
CommitRefIter::from_bytes(&fixture_bytes("commit", "unsigned.txt"))
188-
.signatures()
189-
.collect::<Vec<_>>(),
194+
iter.signatures().collect::<Vec<_>>(),
190195
vec![signature(1592437401), signature(1592437401)]
191196
);
192-
assert_eq!(
193-
CommitRefIter::from_bytes(&fixture_bytes("commit", "unsigned.txt"))
194-
.parent_ids()
195-
.count(),
196-
0
197-
);
197+
assert_eq!(iter.parent_ids().count(), 0);
198198
Ok(())
199199
}
200200

201201
#[test]
202202
fn signatures() -> crate::Result {
203+
let input = fixture_bytes("commit", "unsigned.txt");
204+
let mut iter = CommitRefIter::from_bytes(&input);
203205
assert_eq!(
204-
CommitRefIter::from_bytes(&fixture_bytes("commit", "unsigned.txt"))
205-
.signatures()
206-
.collect::<Vec<_>>(),
206+
iter.signatures().collect::<Vec<_>>(),
207207
vec![signature(1592437401), signature(1592437401)]
208208
);
209+
assert_eq!(iter.author(), Some(signature(1592437401)));
210+
assert_eq!(iter.committer(), Some(signature(1592437401)));
211+
assert_eq!(iter.committer(), None, "it's consuming");
209212
Ok(())
210213
}
211214
}

0 commit comments

Comments
 (0)