From 64fef2f0ad67b14353ee75e1d0099b48da5aac52 Mon Sep 17 00:00:00 2001 From: Fintan Halpenny Date: Fri, 5 May 2023 09:50:53 +0100 Subject: [PATCH] git-ref-format: add Refspec type Add a Refspec type that represents a `[+]:` refspec[[0]]. [0]: https://git-scm.com/book/en/v2/Git-Internals-The-Refspec Signed-off-by: Fintan Halpenny X-Clacks-Overhead: GNU Terry Pratchett --- .../git-ref-format/core/src/refspec.rs | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/radicle-git-ext/git-ref-format/core/src/refspec.rs b/radicle-git-ext/git-ref-format/core/src/refspec.rs index 2b1d018..0e1967e 100644 --- a/radicle-git-ext/git-ref-format/core/src/refspec.rs +++ b/radicle-git-ext/git-ref-format/core/src/refspec.rs @@ -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 { + pub src: T, + pub dst: U, + pub force: bool, +} + +impl fmt::Display for Refspec +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) + } +}