History

Improve error handling of encode::header_field_multi_line & simplify

`EmptyValue` is already used by `header_field` and seems suitable for the same case here. Also swap `lines()` for `split_str()` this way there's no need to handle the terminal edge case. And "deoptimise" `Commit[Ref]::write_to`: `header_field_multi_line` should work fine for single values, it *might* be slightly less efficient but I assume it's a pretty rare case anyway and the simplicity is more valuable, as well as avoiding redundant work.

Xavier Morel committed 5 years ago

Commit bab9fb51 parent 23040d32 files · +4 −23Browse files

Showing the changes to git-object/src/commit (1 of 2 files). Show all

Unified · Split

git-object/src/commit/write.rs+2−14
@@ -1,7 +1,5 @@
1use std::io;1use std::io;
2 2
3use bstr::ByteSlice;
4
5use crate::{encode, encode::NL, Commit, CommitRef, Kind};3use crate::{encode, encode::NL, Commit, CommitRef, Kind};
6 4
7impl crate::WriteTo for Commit {5impl crate::WriteTo for Commit {
@@ -17,12 +15,7 @@ impl crate::WriteTo for Commit { · 9 unmodified lines
17 encode::header_field(b"encoding", encoding, &mut out)?;15 encode::header_field(b"encoding", encoding, &mut out)?;
18 }16 }
19 for (name, value) in &self.extra_headers {17 for (name, value) in &self.extra_headers {
20 let has_newline = value.find_byte(b'\n').is_some();18 encode::header_field_multi_line(name, value, &mut out)?;
21 if has_newline {
22 encode::header_field_multi_line(name, value, &mut out)?;
23 } else {
24 encode::trusted_header_field(name, value, &mut out)?;
25 }
26 }19 }
27 out.write_all(NL)?;20 out.write_all(NL)?;
28 out.write_all(&self.message)21 out.write_all(&self.message)
@@ -46,12 +39,7 @@ impl<'a> crate::WriteTo for CommitRef<'a> { · 17 unmodified lines
46 encode::header_field(b"encoding", encoding, &mut out)?;39 encode::header_field(b"encoding", encoding, &mut out)?;
47 }40 }
48 for (name, value) in &self.extra_headers {41 for (name, value) in &self.extra_headers {
49 let has_newline = value.find_byte(b'\n').is_some();42 encode::header_field_multi_line(name, value, &mut out)?;
50 if has_newline {
51 encode::header_field_multi_line(name, value, &mut out)?;
52 } else {
53 encode::trusted_header_field(name, value, &mut out)?;
54 }
55 }43 }
56 out.write_all(NL)?;44 out.write_all(NL)?;
57 out.write_all(self.message)45 out.write_all(self.message)