gix_object/commit/
write.rs

1use std::io;
2
3use bstr::ByteSlice;
4
5use crate::{encode, encode::NL, Commit, CommitRef, Kind};
6
7impl crate::WriteTo for Commit {
8    /// Serializes this instance to `out` in the git serialization format.
9    fn write_to(&self, mut out: &mut dyn io::Write) -> io::Result<()> {
10        encode::trusted_header_id(b"tree", &self.tree, &mut out)?;
11        for parent in &self.parents {
12            encode::trusted_header_id(b"parent", parent, &mut out)?;
13        }
14        let mut buf = gix_date::parse::TimeBuf::default();
15        encode::trusted_header_signature(b"author", &self.author.to_ref(&mut buf), &mut out)?;
16        encode::trusted_header_signature(b"committer", &self.committer.to_ref(&mut buf), &mut out)?;
17        if let Some(encoding) = self.encoding.as_ref() {
18            encode::header_field(b"encoding", encoding, &mut out)?;
19        }
20        for (name, value) in &self.extra_headers {
21            encode::header_field_multi_line(name, value, &mut out)?;
22        }
23        out.write_all(NL)?;
24        out.write_all(&self.message)
25    }
26
27    fn kind(&self) -> Kind {
28        Kind::Commit
29    }
30
31    fn size(&self) -> u64 {
32        let hash_in_hex = self.tree.kind().len_in_hex();
33        (b"tree".len() + 1 /*space*/ + hash_in_hex + 1 /* nl */
34        + self.parents.iter().count() * (b"parent".len() + 1 + hash_in_hex + 1)
35            + b"author".len() + 1 /* space */ + self.author.size() + 1 /* nl */
36            + b"committer".len() + 1 /* space */ + self.committer.size() + 1 /* nl */
37            + self
38                .encoding
39                .as_ref()
40                .map_or(0, |e| b"encoding".len() + 1 /* space */ + e.len() + 1 /* nl */)
41            + self
42                .extra_headers
43                .iter()
44                .map(|(name, value)| {
45                    // each header *value* is preceded by a space, and it starts right after the name.
46                    name.len() + value.lines_with_terminator().map(|s| s.len() + 1).sum::<usize>() + usize::from(!value.ends_with_str(b"\n"))
47                })
48                .sum::<usize>()
49            + 1 /* nl */
50            + self.message.len()) as u64
51    }
52}
53
54impl crate::WriteTo for CommitRef<'_> {
55    /// Serializes this instance to `out` in the git serialization format.
56    fn write_to(&self, mut out: &mut dyn io::Write) -> io::Result<()> {
57        encode::trusted_header_id(b"tree", &self.tree(), &mut out)?;
58        for parent in self.parents() {
59            encode::trusted_header_id(b"parent", &parent, &mut out)?;
60        }
61        encode::trusted_header_signature(b"author", &self.author, &mut out)?;
62        encode::trusted_header_signature(b"committer", &self.committer, &mut out)?;
63        if let Some(encoding) = self.encoding.as_ref() {
64            encode::header_field(b"encoding", encoding, &mut out)?;
65        }
66        for (name, value) in &self.extra_headers {
67            encode::header_field_multi_line(name, value, &mut out)?;
68        }
69        out.write_all(NL)?;
70        out.write_all(self.message)
71    }
72
73    fn kind(&self) -> Kind {
74        Kind::Commit
75    }
76
77    fn size(&self) -> u64 {
78        let hash_in_hex = self.tree().kind().len_in_hex();
79        (b"tree".len() + 1 /* space */ + hash_in_hex + 1 /* nl */
80            + self.parents.iter().count() * (b"parent".len() + 1 /* space */ + hash_in_hex + 1 /* nl */)
81            + b"author".len() + 1 /* space */ + self.author.size() + 1 /* nl */
82            + b"committer".len() + 1 /* space */ + self.committer.size() + 1 /* nl */
83            + self
84                .encoding
85                .as_ref()
86                .map_or(0, |e| b"encoding".len() + 1 /* space */ + e.len() + 1 /* nl */)
87            + self
88                .extra_headers
89                .iter()
90                .map(|(name, value)| {
91                    // each header *value* is preceded by a space, and it starts right after the name.
92                    name.len() + value.lines_with_terminator().map(|s| s.len() + 1).sum::<usize>() + usize::from(!value.ends_with_str(b"\n"))
93                })
94                .sum::<usize>()
95            + 1 /* nl */
96            + self.message.len()) as u64
97    }
98}