Skip to content

[git-object] Encode empty tags like git does #604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion git-object/src/tag/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ pub fn message<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&
let (i, _) = tag(NL)(i)?;
fn all_to_end<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&'a [u8], &'a [u8]), E> {
if i.is_empty() {
return Err(nom::Err::Error(E::from_error_kind(i, nom::error::ErrorKind::Eof)));
// Empty message. That's OK.
return Ok((&[], (&[], &[])));
}
// an empty signature message signals that there is none - the function signature is needed
// to work with 'alt(…)'. PGP signatures are never empty
Expand Down
16 changes: 4 additions & 12 deletions git-object/src/tag/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ impl crate::WriteTo for Tag {
encode::trusted_header_signature(b"tagger", &tagger.to_ref(), &mut out)?;
}

out.write_all(NL)?;
if !self.message.is_empty() {
out.write_all(NL)?;
out.write_all(&self.message)?;
}
if let Some(ref message) = self.pgp_signature {
Expand All @@ -49,11 +49,7 @@ impl crate::WriteTo for Tag {
.as_ref()
.map(|t| b"tagger".len() + 1 /* space */ + t.size() + 1 /* nl */)
.unwrap_or(0)
+ if self.message.is_empty() {
0
} else {
1 /* nl */ + self.message.len()
}
+ 1 /* nl */ + self.message.len()
+ self.pgp_signature.as_ref().map(|m| 1 /* nl */ + m.len() ).unwrap_or(0)
}

Expand All @@ -71,8 +67,8 @@ impl<'a> crate::WriteTo for TagRef<'a> {
encode::trusted_header_signature(b"tagger", tagger, &mut out)?;
}

out.write_all(NL)?;
if !self.message.is_empty() {
out.write_all(NL)?;
out.write_all(self.message)?;
}
if let Some(message) = self.pgp_signature {
Expand All @@ -91,11 +87,7 @@ impl<'a> crate::WriteTo for TagRef<'a> {
.as_ref()
.map(|t| b"tagger".len() + 1 /* space */ + t.size() + 1 /* nl */)
.unwrap_or(0)
+ if self.message.is_empty() {
0
} else {
1 /* nl */ + self.message.len()
}
+ 1 /* nl */ + self.message.len()
+ self.pgp_signature.as_ref().map(|m| 1 /* nl */ + m.len()).unwrap_or(0)
}

Expand Down
1 change: 1 addition & 0 deletions git-object/tests/fixtures/tag/empty.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ object 01dd4e2a978a9f5bd773dae6da7aa4a5ac1cdbbc
type commit
tag empty
tagger Sebastian Thiel <sebastian.thiel@icloud.com> 1592381636 +0800

6 changes: 5 additions & 1 deletion git-object/tests/immutable/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ mod iter {
Token::TargetKind(Kind::Commit),
Token::Name(b"empty".as_bstr()),
Token::Tagger(tagger),
Token::Body {
message: b"".as_bstr(),
pgp_signature: None,
}
]
);
assert_eq!(tag_iter.target_id()?, target_id);
Expand Down Expand Up @@ -103,7 +107,7 @@ KLMHist5yj0sw1E4hDTyQa0=
#[test]
fn error_handling() -> crate::Result {
let data = fixture_bytes("tag", "empty.txt");
let iter = TagRefIter::from_bytes(&data[..data.len() / 2]);
let iter = TagRefIter::from_bytes(&data[..data.len() / 3]);
let tokens = iter.collect::<Vec<_>>();
assert!(
tokens.last().expect("at least the errored token").is_err(),
Expand Down