Skip to content

[gix-object] Support empty tags with or without trailing NL #1903

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
Mar 22, 2025
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
4 changes: 2 additions & 2 deletions gix-object/src/tag/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub fn message<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> ModalResult<(&
const PGP_SIGNATURE_BEGIN: &[u8] = b"\n-----BEGIN PGP SIGNATURE-----";
const PGP_SIGNATURE_END: &[u8] = b"-----END PGP SIGNATURE-----";

if i.is_empty() {
return Ok((b"".as_bstr(), None));
if i.iter().all(|b| *b == b'\n') {
return i.map(|message: &[u8]| (message.as_bstr(), None)).parse_next(i);
}
delimited(
NL,
Expand Down
16 changes: 8 additions & 8 deletions gix-object/src/tag/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ impl crate::WriteTo for Tag {
encode::trusted_header_signature(b"tagger", &tagger.to_ref(), out)?;
}

out.write_all(NL)?;
if !self.message.is_empty() {
out.write_all(self.message.as_ref())?;
if !self.message.iter().all(|b| *b == b'\n') {
out.write_all(NL)?;
}
out.write_all(self.message.as_ref())?;
if let Some(message) = &self.pgp_signature {
out.write_all(NL)?;
out.write_all(message.as_ref())?;
Expand All @@ -52,7 +52,7 @@ impl crate::WriteTo for Tag {
.tagger
.as_ref()
.map_or(0, |t| b"tagger".len() + 1 /* space */ + t.size() + 1 /* nl */)
+ 1 /* nl */ + self.message.len()
+ if self.message.iter().all(|b| *b == b'\n') { 0 } else { 1 /* nl */ } + self.message.len()
+ self.pgp_signature.as_ref().map_or(0, |m| 1 /* nl */ + m.len())) as u64
}
}
Expand All @@ -66,10 +66,10 @@ impl crate::WriteTo for TagRef<'_> {
encode::trusted_header_signature(b"tagger", tagger, &mut out)?;
}

out.write_all(NL)?;
if !self.message.is_empty() {
out.write_all(self.message)?;
if !self.message.iter().all(|b| *b == b'\n') {
out.write_all(NL)?;
}
out.write_all(self.message)?;
if let Some(message) = self.pgp_signature {
out.write_all(NL)?;
out.write_all(message)?;
Expand All @@ -89,7 +89,7 @@ impl crate::WriteTo for TagRef<'_> {
.tagger
.as_ref()
.map_or(0, |t| b"tagger".len() + 1 /* space */ + t.size() + 1 /* nl */)
+ 1 /* nl */ + self.message.len()
+ if self.message.iter().all(|b| *b == b'\n') { 0 } else { 1 /* nl */ } + self.message.len()
+ self.pgp_signature.as_ref().map_or(0, |m| 1 /* nl */ + m.len())) as u64
}
}
Expand Down
4 changes: 4 additions & 0 deletions gix-object/tests/fixtures/tag/empty_missing_nl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object 01dd4e2a978a9f5bd773dae6da7aa4a5ac1cdbbc
type commit
tag empty
tagger Sebastian Thiel <sebastian.thiel@icloud.com> 1592381636 +0800
1 change: 1 addition & 0 deletions gix-object/tests/object/encode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ mod tag {
round_trip!(
gix_object::Tag,
gix_object::TagRef,
"tag/empty_missing_nl.txt",
"tag/empty.txt",
"tag/no-tagger.txt",
"tag/whitespace.txt",
Expand Down
28 changes: 25 additions & 3 deletions gix-object/tests/object/tag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod iter {
Token::Name(b"empty".as_bstr()),
Token::Tagger(tagger),
Token::Body {
message: b"".as_bstr(),
message: b"\n".as_bstr(),
pgp_signature: None,
}
]
Expand Down Expand Up @@ -155,7 +155,7 @@ fn invalid() {
mod from_bytes {
use gix_actor::SignatureRef;
use gix_date::Time;
use gix_object::{bstr::ByteSlice, Kind, TagRef};
use gix_object::{bstr::ByteSlice, Kind, TagRef, WriteTo};

use crate::{fixture_name, signature, tag::tag_fixture, Sign};

Expand All @@ -170,8 +170,29 @@ mod from_bytes {

#[test]
fn empty() -> crate::Result {
let fixture = fixture_name("tag", "empty.txt");
let tag_ref = TagRef::from_bytes(&fixture)?;
assert_eq!(
TagRef::from_bytes(&fixture_name("tag", "empty.txt"))?,
tag_ref,
TagRef {
target: b"01dd4e2a978a9f5bd773dae6da7aa4a5ac1cdbbc".as_bstr(),
name: b"empty".as_bstr(),
target_kind: Kind::Commit,
message: b"\n".as_bstr(),
tagger: Some(signature(1592381636)),
pgp_signature: None
}
);
assert_eq!(tag_ref.size(), 140);
Ok(())
}

#[test]
fn empty_missing_nl() -> crate::Result {
let fixture = fixture_name("tag", "empty_missing_nl.txt");
let tag_ref = TagRef::from_bytes(&fixture)?;
assert_eq!(
tag_ref,
TagRef {
target: b"01dd4e2a978a9f5bd773dae6da7aa4a5ac1cdbbc".as_bstr(),
name: b"empty".as_bstr(),
Expand All @@ -181,6 +202,7 @@ mod from_bytes {
pgp_signature: None
}
);
assert_eq!(tag_ref.size(), 139);
Ok(())
}

Expand Down
Loading