Skip to content

Commit 8ad43f1

Browse files
committed
refactor
- make the test more specific to what it's testing - fix a bug in the way multi-line headers are written so the object can be serialised.
1 parent a0660fe commit 8ad43f1

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

gix-object/src/encode.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ pub(crate) fn header_field_multi_line(name: &[u8], value: &[u8], out: &mut dyn i
3838
let mut lines = value.as_bstr().lines_with_terminator();
3939
out.write_all(name)?;
4040
out.write_all(SPACE)?;
41-
out.write_all(lines.next().ok_or(Error::EmptyValue)?)?;
41+
if let Some(line) = lines.next() {
42+
out.write_all(line)?;
43+
}
4244
for line in lines {
4345
out.write_all(SPACE)?;
4446
out.write_all(line)?;

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use gix_actor::SignatureRef;
2-
use gix_object::{bstr::ByteSlice, commit::message::body::TrailerRef, CommitRef};
2+
use gix_object::{bstr::ByteSlice, commit::message::body::TrailerRef, CommitRef, WriteTo};
33
use smallvec::SmallVec;
44

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

1010
#[test]
@@ -347,15 +347,22 @@ fn newline_right_after_signature_multiline_header() -> crate::Result {
347347
fn bogus_multi_gpgsig_header() -> crate::Result {
348348
let fixture = fixture_name("commit", "bogus-gpgsig-lines-in-git.git.txt");
349349
let commit = CommitRef::from_bytes(&fixture)?;
350-
let pgp_sig = crate::commit::BEGIN_PGP_SIGNATURE.as_bstr();
351-
assert_eq!(commit.extra_headers[0].1.as_ref(), pgp_sig);
350+
let pgp_sig = b"-----BEGIN PGP SIGNATURE-----".as_bstr();
352351
assert_eq!(commit.extra_headers().pgp_signature(), Some(pgp_sig));
353352
assert_eq!(
354-
commit.extra_headers().find(gix_object::commit::SIGNATURE_FIELD_NAME),
355-
Some(pgp_sig)
353+
commit.extra_headers().find_all("gpgsig").count(),
354+
17,
355+
"Each signature header line is prefixed with `gpgsig` here, so we parse it as extra header"
356356
);
357-
assert_eq!(commit.extra_headers().find_pos("gpgsig"), Some(0));
358-
assert_eq!(commit.extra_headers().find_pos("something else"), None);
359357
assert!(commit.message.starts_with(b"pretty: %G[?GS] placeholders"));
358+
359+
let mut buf = Vec::<u8>::new();
360+
commit.write_to(&mut buf)?;
361+
let actual = gix_object::compute_hash(gix_hash::Kind::Sha1, gix_object::Kind::Commit, &buf)?;
362+
assert_eq!(
363+
actual,
364+
hex_to_id("5f549aa2f78314ac37bbd436c8f80aea4c752e07"),
365+
"round-tripping works despite the strangeness"
366+
);
360367
Ok(())
361368
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ iyBBl69jASy41Ug/BlFJbw4+ItkShpXwkJKuBBV/JExChmvbxYWaS7QnyYC9UO0=
149149
150150
";
151151

152-
const BEGIN_PGP_SIGNATURE: &[u8] = b"-----BEGIN PGP SIGNATURE-----";
153-
154152
mod method {
155153
use gix_object::CommitRef;
156154
use pretty_assertions::assert_eq;

0 commit comments

Comments
 (0)