Skip to content

git-ref refactor #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 26, 2021
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions cargo-smart-release/src/command/release/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub(in crate::command::release_impl) fn create_version_tag<'repo>(
skip_tag,
..
}: Options,
) -> anyhow::Result<Option<refs::mutable::FullName>> {
) -> anyhow::Result<Option<refs::FullName>> {
if skip_tag {
return Ok(None);
}
Expand Down Expand Up @@ -170,10 +170,7 @@ pub(in crate::command::release_impl) fn create_version_tag<'repo>(
}

// TODO: Make this gitoxide
pub fn push_tags_and_head(
tag_names: impl IntoIterator<Item = refs::mutable::FullName>,
options: Options,
) -> anyhow::Result<()> {
pub fn push_tags_and_head(tag_names: impl IntoIterator<Item = refs::FullName>, options: Options) -> anyhow::Result<()> {
if options.skip_push {
return Ok(());
}
Expand Down
2 changes: 1 addition & 1 deletion git-actor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "git-actor"
version = "0.4.0"
version = "0.5.0"
description = "A way to identify git actors"
authors = ["Sebastian Thiel <sebastian.thiel@icloud.com>"]
repository = "https://github.com/Byron/gitoxide"
Expand Down
2 changes: 0 additions & 2 deletions git-actor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use bstr::{BStr, BString};

///
pub mod signature;
///
pub mod signature_ref;

const SPACE: &[u8; 1] = b" ";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,10 @@ mod tests {
use git_testtools::to_bstr_err;
use nom::IResult;

use crate::{
signature_ref::{self, SignatureRef},
Sign, Time,
};
use crate::{signature, Sign, SignatureRef, Time};

fn decode(i: &[u8]) -> IResult<&[u8], SignatureRef<'_>, nom::error::VerboseError<&[u8]>> {
signature_ref::decode(i)
signature::decode(i)
}

fn signature(
Expand Down
17 changes: 17 additions & 0 deletions git-actor/src/signature.rs → git-actor/src/signature/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
mod _ref {
use crate::{signature::decode, SignatureRef};

impl<'a> SignatureRef<'a> {
/// Deserialize a signature from the given `data`.
pub fn from_bytes<E: nom::error::ParseError<&'a [u8]> + nom::error::ContextError<&'a [u8]>>(
data: &'a [u8],
) -> Result<SignatureRef<'a>, nom::Err<E>> {
decode(data).map(|(_, t)| t)
}
}
}

mod convert {
use crate::{Sign, Signature, SignatureRef, Time};

Expand Down Expand Up @@ -85,3 +98,7 @@ mod write {
Ok(name)
}
}

///
mod decode;
pub use decode::decode;
14 changes: 0 additions & 14 deletions git-actor/src/signature_ref/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion git-object/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ all-features = true
[dependencies]
git-hash = { version = "^0.5.0", path = "../git-hash" }
git-validate = { version = "^0.5.0", path = "../git-validate" }
git-actor = { version ="^0.4.0", path = "../git-actor" }
git-actor = { version ="^0.5.0", path = "../git-actor" }

quick-error = "2.0.0"
hex = "0.4.2"
Expand Down
2 changes: 1 addition & 1 deletion git-object/src/immutable/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ pub fn hex_hash<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &
pub(crate) fn signature<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
i: &'a [u8],
) -> IResult<&'a [u8], git_actor::SignatureRef<'a>, E> {
git_actor::signature_ref::decode(i)
git_actor::signature::decode(i)
}
2 changes: 1 addition & 1 deletion git-object/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl fmt::Display for Kind {
pub mod tree {
/// The mode of items storable in a tree, similar to the file mode on a unix file system.
///
/// Used in [mutable::Entry][crate::mutable::tree::Entry] and [signature_ref::Entry][crate::immutable::tree::Entry].
/// Used in [mutable::Entry][crate::mutable::tree::Entry] and [tree::Entry][crate::immutable::tree::Entry].
#[derive(Clone, Copy, PartialEq, Eq, Debug, Ord, PartialOrd, Hash)]
#[repr(u16)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
Expand Down
2 changes: 1 addition & 1 deletion git-ref/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ git-features = { version = "^0.16.0", path = "../git-features", features = ["wal
git-hash = { version = "^0.5.0", path = "../git-hash" }
git-object = { version ="0.12.0", path = "../git-object" }
git-validate = { version = "^0.5.0", path = "../git-validate" }
git-actor = { version ="^0.4.0", path = "../git-actor" }
git-actor = { version ="^0.5.0", path = "../git-actor" }
git-lock = { version ="^1.0.0", path = "../git-lock" }
git-tempfile = { version ="^1.0.0", path = "../git-tempfile" }

Expand Down
71 changes: 71 additions & 0 deletions git-ref/src/fullname.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use std::{borrow::Cow, convert::TryFrom, path::Path};

use bstr::{BStr, BString, ByteSlice};

use crate::FullName;

impl TryFrom<&str> for FullName {
type Error = git_validate::refname::Error;

fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(FullName(git_validate::refname(value.as_bytes().as_bstr())?.into()))
}
}

impl TryFrom<String> for FullName {
type Error = git_validate::refname::Error;

fn try_from(value: String) -> Result<Self, Self::Error> {
git_validate::refname(value.as_bytes().as_bstr())?;
Ok(FullName(value.into()))
}
}

impl TryFrom<&BStr> for FullName {
type Error = git_validate::refname::Error;

fn try_from(value: &BStr) -> Result<Self, Self::Error> {
Ok(FullName(git_validate::refname(value)?.into()))
}
}

impl TryFrom<BString> for FullName {
type Error = git_validate::refname::Error;

fn try_from(value: BString) -> Result<Self, Self::Error> {
git_validate::refname(value.as_ref())?;
Ok(FullName(value))
}
}

impl<'a> From<crate::FullNameRef<'a>> for FullName {
fn from(value: crate::FullNameRef<'a>) -> Self {
FullName(value.as_bstr().into())
}
}

impl FullName {
/// Interpret this fully qualified reference name as partial name.
pub fn to_partial(&self) -> crate::PartialNameRef<'_> {
crate::PartialNameRef(self.0.as_bstr())
}

/// Interpret this fully qualified reference as shared full name
pub fn to_ref(&self) -> crate::FullNameRef<'_> {
crate::FullNameRef(self.0.as_bstr())
}

/// Convert this name into the relative path, lossily, identifying the reference location relative to a repository
pub fn to_path(&self) -> Cow<'_, Path> {
self.0.to_path().expect("UTF-8 conversion always succeeds").into()
}

/// Dissolve this instance and return the buffer.
pub fn into_inner(self) -> BString {
self.0
}
/// Return ourselves as byte string which is a valid refname
pub fn as_bstr(&self) -> &BStr {
self.0.as_bstr()
}
}
28 changes: 22 additions & 6 deletions git-ref/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,30 @@
#![forbid(unsafe_code)]
#![deny(missing_docs, rust_2018_idioms)]
use bstr::{BStr, BString};
use git_hash::oid;
use git_hash::{oid, ObjectId};

mod store;
pub use store::{file, packed};
///
pub mod mutable;

mod fullname;
///
pub mod name;
///
pub mod namespace;
///
pub mod transaction;

/// Indicate that the given BString is a validate reference name or path that can be used as path on disk or written as target
/// of a symbolic reference
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
pub struct FullName(pub(crate) BString);

/// A validated and potentially partial reference name - it can safely be used for common operations.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
pub struct FullName<'a>(&'a BStr);
pub struct FullNameRef<'a>(&'a BStr);
/// A validated complete and fully qualified reference name, safe to use for all operations.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
pub struct PartialName<'a>(&'a BStr);
pub struct PartialNameRef<'a>(&'a BStr);

/// A validated prefix for references to act as a namespace.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
Expand All @@ -55,9 +60,20 @@ pub enum Kind {
Symbolic,
}

/// Denotes a ref target, equivalent to [`Kind`], but with mutable data.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
pub enum Target {
/// A ref that points to an object id
Peeled(ObjectId),
/// A ref that points to another reference by its validated name, adding a level of indirection.
///
/// Note that this is an extension of gitoxide which will be helpful in logging all reference changes.
Symbolic(FullName),
}

/// Denotes a ref target, equivalent to [`Kind`], but with signature_ref data.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
pub enum Target<'a> {
pub enum TargetRef<'a> {
/// A ref that points to an object id
Peeled(&'a oid),
/// A ref that points to another reference by its validated name, adding a level of indirection.
Expand Down
Loading