1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use crate::{
commit,
mutable::{encode, NL},
};
use bstr::{BStr, BString, ByteSlice};
use smallvec::SmallVec;
use std::io;
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct Commit {
pub tree: git_hash::ObjectId,
pub parents: SmallVec<[git_hash::ObjectId; 1]>,
pub author: git_actor::Signature,
pub committer: git_actor::Signature,
pub encoding: Option<BString>,
pub message: BString,
pub extra_headers: Vec<(BString, BString)>,
}
impl Commit {
pub fn extra_headers(&self) -> commit::ExtraHeaders<impl Iterator<Item = (&BStr, &BStr)>> {
commit::ExtraHeaders::new(self.extra_headers.iter().map(|(k, v)| (k.as_bstr(), v.as_bstr())))
}
pub fn write_to(&self, mut out: impl io::Write) -> io::Result<()> {
encode::trusted_header_id(b"tree", &self.tree, &mut out)?;
for parent in &self.parents {
encode::trusted_header_id(b"parent", parent, &mut out)?;
}
encode::trusted_header_signature(b"author", &self.author, &mut out)?;
encode::trusted_header_signature(b"committer", &self.committer, &mut out)?;
if let Some(encoding) = self.encoding.as_ref() {
encode::header_field(b"encoding", encoding, &mut out)?;
}
for (name, value) in &self.extra_headers {
let has_newline = value.find_byte(b'\n').is_some();
if has_newline {
encode::header_field_multi_line(name, value, &mut out)?;
} else {
encode::trusted_header_field(name, value, &mut out)?;
}
}
out.write_all(NL)?;
out.write_all(&self.message)
}
}