git-ref-format: add Refspec type

Add a Refspec type that represents a `[+]<src>:<dst>` refspec[[0]].

[0]: https://git-scm.com/book/en/v2/Git-Internals-The-Refspec

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny
2023-05-05 09:50:53 +01:00
parent 24e27f6e87
commit 64fef2f0ad
@@ -6,7 +6,7 @@
use std::{
borrow::{Borrow, Cow},
convert::TryFrom,
fmt::{self, Display},
fmt::{self, Display, Write as _},
iter::FromIterator,
ops::Deref,
};
@@ -558,3 +558,30 @@ impl Display for NamespacedPattern<'_> {
self.0.fmt(f)
}
}
/// A Git [refspec].
///
/// **Note** that this is a simplified version of a [refspec] where
/// the `src` and `dst` are required and there is no way to construct
/// a negative refspec, e.g. `^refs/heads/no-thanks`.
///
/// [refspec]: https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Refspec<T, U> {
pub src: T,
pub dst: U,
pub force: bool,
}
impl<T, U> fmt::Display for Refspec<T, U>
where
T: fmt::Display,
U: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.force {
f.write_char('+')?;
}
write!(f, "{}:{}", self.src, self.dst)
}
}