Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/mono-repository/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use std::path::Path;
use std::process::Command;

pub mod commit;
pub mod commits;
mod error;
pub mod id;
pub mod versions;
Expand Down
13 changes: 6 additions & 7 deletions crates/mono-repository/src/repository/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ use super::id::Id;
use super::Repository;

mod delta;
mod iter;
mod deltas;
mod trailers;

pub use delta::{Delta, Deltas};
pub use iter::Commits;
pub use delta::Delta;
pub use deltas::Deltas;
pub use trailers::Trailers;

// ----------------------------------------------------------------------------
// Structs
Expand Down Expand Up @@ -166,10 +168,7 @@ impl fmt::Debug for Commit<'_> {
///
/// [`Error::Git`]: crate::repository::Error::Git
pub fn trim_trailers(message: &str) -> Result<&str> {
// We must add two line feeds to the message, or the trailers would not be
// discoverable, since git assumes that we pass the entire commit message
let prepared = format!("\n\n{message}");
let trailers = git2::message_trailers_strs(prepared.as_str())?;
let trailers: Trailers = message.parse()?;
if let Some((key, _)) = trailers.iter().next() {
Ok(message.split_once(key).map_or(message, |(body, _)| body))
} else {
Expand Down
4 changes: 0 additions & 4 deletions crates/mono-repository/src/repository/commit/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@

use std::path::PathBuf;

mod iter;

pub use iter::Deltas;

// ----------------------------------------------------------------------------
// Enums
// ----------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use crate::repository::commit::Commit;
use crate::repository::Result;

use super::Delta;
use super::delta::Delta;

// ----------------------------------------------------------------------------
// Structs
Expand Down
151 changes: 151 additions & 0 deletions crates/mono-repository/src/repository/commit/trailers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright (c) 2025 Zensical and contributors

// SPDX-License-Identifier: MIT
// Third-party contributions licensed under DCO

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

// ----------------------------------------------------------------------------

//! Commit trailer set.

use std::fmt;
use std::str::FromStr;

use crate::repository::commit::Commit;
use crate::repository::{Error, Result};

// ----------------------------------------------------------------------------
// Structs
// ----------------------------------------------------------------------------

/// Commit trailer set.
pub struct Trailers {
/// Git trailers.
inner: git2::MessageTrailersStrs,
}

// ----------------------------------------------------------------------------
// Implementations
// ----------------------------------------------------------------------------

impl Commit<'_> {
/// Returns the trailer set of the commit.
///
/// We must add two line feeds to the message, or the trailers would not be
/// discoverable, since git assumes that we pass the entire commit message
///
/// # Errors
///
/// This method returns [`Error::Git`] if the operation fails.
#[allow(clippy::missing_panics_doc)]
#[inline]
pub fn trailers(&self) -> Result<Trailers> {
Trailers::from_str(self.inner.body().unwrap_or_default())
}
}

// ----------------------------------------------------------------------------

impl Trailers {
/// Returns a reference to the value identified by the key.
pub fn get<K>(&self, key: K) -> Option<&str>
where
K: AsRef<str>,
{
let mut iter = self.inner.iter();
iter.find_map(|(candidate, value)| {
(candidate == key.as_ref()).then_some(value)
})
}

/// Returns whether the commit trailer set contains the key.
pub fn contains_key<K>(&self, key: K) -> bool
where
K: AsRef<str>,
{
let mut iter = self.inner.iter();
iter.any(|(candidate, _)| candidate == key.as_ref())
}

/// Creates an iterator over the commit trailer set.
#[inline]
#[must_use]
pub fn iter(&self) -> git2::MessageTrailersStrsIterator<'_> {
self.into_iter()
}
}

#[allow(clippy::must_use_candidate)]
impl Trailers {
/// Returns the number of trailers.
#[inline]
pub fn len(&self) -> usize {
self.inner.len()
}

/// Returns whether there are any trailers.
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.len() == 0
}
}

// ----------------------------------------------------------------------------
// Trait implementations
// ----------------------------------------------------------------------------

impl FromStr for Trailers {
type Err = Error;

/// Attempts to create a commit trailer set from a string.
///
/// # Errors
///
/// This method returns [`Error::Git`] if the operation fails.
fn from_str(value: &str) -> Result<Self> {
// We must add two line feeds to the message or the trailers wouldn't be
// discoverable, as git assumes that we pass the entire commit message
let prepared = format!("\n\n{value}");
Ok(Self {
inner: git2::message_trailers_strs(prepared.as_str())?,
})
}
}

// ----------------------------------------------------------------------------

impl<'a> IntoIterator for &'a Trailers {
type Item = (&'a str, &'a str);
type IntoIter = git2::MessageTrailersStrsIterator<'a>;

/// Creates an iterator over the commit trailer set.
fn into_iter(self) -> Self::IntoIter {
self.inner.iter()
}
}

// ----------------------------------------------------------------------------

impl fmt::Debug for Trailers {
/// Formats the commit trailer set for debugging.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_map().entries(self).finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use std::ops::{Bound, RangeBounds};
use crate::repository::id::Id;
use crate::repository::{Error, Repository, Result};

use super::Commit;
use super::commit::Commit;

// ----------------------------------------------------------------------------
// Structs
Expand Down
2 changes: 1 addition & 1 deletion crates/mono-repository/src/repository/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl From<git2::Oid> for Id {

impl fmt::Display for Id {
/// Formats the object identifier for display.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
2 changes: 1 addition & 1 deletion crates/mono-repository/src/repository/versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use std::fmt;
use std::iter::Rev;
use std::ops::RangeBounds;

use super::commit::Commits;
use super::commits::Commits;
use super::error::{Error, Result};
use super::id::Id;
use super::Repository;
Expand Down