This categorizes them semantically without using the overloaded names of owning and borrowing, which mean different things in Rust.
Showing the changes to git-object/src/mutable/commit.rs (1 of 34 files). Show all
Unified · Split
| @@ -0,0 +1,60 @@ | ||
| 1 | +use crate::{ | |
| 2 | + commit, | |
| 3 | + mutable::{self, ser, NL}, | |
| 4 | +}; | |
| 5 | +use bstr::{BStr, BString, ByteSlice}; | |
| 6 | +use smallvec::SmallVec; | |
| 7 | +use std::io; | |
| 8 | + | |
| 9 | +/// A mutable git commit, representing an annotated state of a working tree along with a reference to its historical commits. | |
| 10 | +#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)] | |
| 11 | +#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))] | |
| 12 | +pub struct Commit { | |
| 13 | + /// The hash of recorded working tree state. | |
| 14 | + pub tree: git_hash::ObjectId, | |
| 15 | + /// Hash of each parent commit. Empty for the first commit in repository. | |
| 16 | + pub parents: SmallVec<[git_hash::ObjectId; 1]>, | |
| 17 | + /// Who wrote this commit. | |
| 18 | + pub author: mutable::Signature, | |
| 19 | + /// Who committed this commit. | |
| 20 | + /// | |
| 21 | + /// This may be different from the `author` in case the author couldn't write to the repository themselves and | |
| 22 | + /// is commonly encountered with contributed commits. | |
| 23 | + pub committer: mutable::Signature, | |
| 24 | + /// The name of the message encoding, otherwise [UTF-8 should be assumed](https://github.com/git/git/blob/e67fbf927dfdf13d0b21dc6ea15dc3c7ef448ea0/commit.c#L1493:L1493). | |
| 25 | + pub encoding: Option<BString>, | |
| 26 | + /// The commit message documenting the change. | |
| 27 | + pub message: BString, | |
| 28 | + /// Extra header fields, in order of them being encountered, made accessible with the iterator returned | |
| 29 | + /// by [`extra_headers()`][Commit::extra_headers()]. | |
| 30 | + pub extra_headers: Vec<(BString, BString)>, | |
| 31 | +} | |
| 32 | + | |
| 33 | +impl Commit { | |
| 34 | + /// Returns a convenient iterator over all extra headers. | |
| 35 | + pub fn extra_headers(&self) -> commit::ExtraHeaders<impl Iterator<Item = (&BStr, &BStr)>> { | |
| 36 | + commit::ExtraHeaders::new(self.extra_headers.iter().map(|(k, v)| (k.as_bstr(), v.as_bstr()))) | |
| 37 | + } | |
| 38 | + /// Serializes this instance to `out` in the git serialization format. | |
| 39 | + pub fn write_to(&self, mut out: impl io::Write) -> io::Result<()> { | |
| 40 | + ser::trusted_header_id(b"tree", &self.tree, &mut out)?; | |
| 41 | + for parent in &self.parents { | |
| 42 | + ser::trusted_header_id(b"parent", parent, &mut out)?; | |
| 43 | + } | |
| 44 | + ser::trusted_header_signature(b"author", &self.author, &mut out)?; | |
| 45 | + ser::trusted_header_signature(b"committer", &self.committer, &mut out)?; | |
| 46 | + if let Some(encoding) = self.encoding.as_ref() { | |
| 47 | + ser::header_field(b"encoding", encoding, &mut out)?; | |
| 48 | + } | |
| 49 | + for (name, value) in &self.extra_headers { | |
| 50 | + let has_newline = value.find_byte(b'\n').is_some(); | |
| 51 | + if has_newline { | |
| 52 | + ser::header_field_multi_line(name, value, &mut out)?; | |
| 53 | + } else { | |
| 54 | + ser::trusted_header_field(name, value, &mut out)?; | |
| 55 | + } | |
| 56 | + } | |
| 57 | + out.write_all(NL)?; | |
| 58 | + out.write_all(&self.message) | |
| 59 | + } | |
| 60 | +} | |