Use gix::refs::PartialName as even stronger guarantee for what's in there

This commit is contained in:
Sebastian Thiel 2024-08-08 20:11:38 +02:00
parent 712ce582cb
commit 6720e987fe
No known key found for this signature in database
GPG Key ID: 9CB5EE7895E8268B
7 changed files with 136 additions and 22 deletions

96
Cargo.lock generated
View File

@ -534,6 +534,9 @@ name = "bitflags"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
dependencies = [
"serde",
]
[[package]]
name = "bitvec"
@ -2575,6 +2578,7 @@ dependencies = [
"gix-index 0.33.1 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-lock 14.0.0 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-macros",
"gix-mailmap",
"gix-negotiate",
"gix-object 0.42.3 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-odb",
@ -2582,6 +2586,7 @@ dependencies = [
"gix-path 0.10.9 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-pathspec",
"gix-prompt",
"gix-protocol",
"gix-ref 0.45.0",
"gix-refspec",
"gix-revision",
@ -2590,12 +2595,14 @@ dependencies = [
"gix-submodule",
"gix-tempfile 14.0.1 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-trace 0.1.9 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-transport",
"gix-traverse 0.39.2 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-url",
"gix-utils 0.1.12 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-validate 0.8.5 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-worktree 0.34.1 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"once_cell",
"serde",
"smallvec",
"thiserror",
]
@ -2623,6 +2630,7 @@ dependencies = [
"gix-date 0.9.0",
"gix-utils 0.1.12 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"itoa 1.0.11",
"serde",
"thiserror",
"winnow 0.6.16",
]
@ -2655,6 +2663,7 @@ dependencies = [
"gix-quote 0.4.12 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-trace 0.1.9 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"kstring",
"serde",
"smallvec",
"thiserror",
"unicode-bom",
@ -2729,6 +2738,7 @@ dependencies = [
"gix-features 0.38.2 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-hash 0.14.2 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"memmap2",
"serde",
"thiserror",
]
@ -2777,6 +2787,7 @@ dependencies = [
"gix-sec 0.10.7 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-trace 0.1.9 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-url",
"serde",
"thiserror",
]
@ -2800,6 +2811,7 @@ dependencies = [
"bstr",
"itoa 1.0.11",
"jiff",
"serde",
"thiserror",
]
@ -2961,6 +2973,7 @@ dependencies = [
"bstr",
"gix-features 0.38.2 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-path 0.10.9 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"serde",
]
[[package]]
@ -2979,6 +2992,7 @@ version = "0.14.2"
source = "git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a#7dff44754e0fdc369f92221468fb953bad9be60a"
dependencies = [
"faster-hex",
"serde",
"thiserror",
]
@ -3025,6 +3039,7 @@ dependencies = [
"gix-glob 0.16.4 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-path 0.10.9 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-trace 0.1.9 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"serde",
"unicode-bom",
]
@ -3079,6 +3094,7 @@ dependencies = [
"libc",
"memmap2",
"rustix 0.38.34",
"serde",
"smallvec",
"thiserror",
]
@ -3114,6 +3130,18 @@ dependencies = [
"syn 2.0.68",
]
[[package]]
name = "gix-mailmap"
version = "0.23.5"
source = "git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a#7dff44754e0fdc369f92221468fb953bad9be60a"
dependencies = [
"bstr",
"gix-actor 0.31.5 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-date 0.9.0",
"serde",
"thiserror",
]
[[package]]
name = "gix-negotiate"
version = "0.13.2"
@ -3161,6 +3189,7 @@ dependencies = [
"gix-utils 0.1.12 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-validate 0.8.5 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"itoa 1.0.11",
"serde",
"smallvec",
"thiserror",
"winnow 0.6.16",
@ -3181,6 +3210,7 @@ dependencies = [
"gix-path 0.10.9 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-quote 0.4.12 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"parking_lot 0.12.3",
"serde",
"tempfile",
"thiserror",
]
@ -3198,11 +3228,23 @@ dependencies = [
"gix-object 0.42.3 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-path 0.10.9 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"memmap2",
"serde",
"smallvec",
"thiserror",
"uluru",
]
[[package]]
name = "gix-packetline"
version = "0.17.5"
source = "git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a#7dff44754e0fdc369f92221468fb953bad9be60a"
dependencies = [
"bstr",
"faster-hex",
"gix-trace 0.1.9 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"thiserror",
]
[[package]]
name = "gix-packetline-blocking"
version = "0.17.4"
@ -3265,6 +3307,24 @@ dependencies = [
"thiserror",
]
[[package]]
name = "gix-protocol"
version = "0.45.2"
source = "git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a#7dff44754e0fdc369f92221468fb953bad9be60a"
dependencies = [
"bstr",
"gix-credentials",
"gix-date 0.9.0",
"gix-features 0.38.2 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-hash 0.14.2 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-transport",
"gix-utils 0.1.12 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"maybe-async",
"serde",
"thiserror",
"winnow 0.6.16",
]
[[package]]
name = "gix-quote"
version = "0.4.12"
@ -3324,6 +3384,7 @@ dependencies = [
"gix-utils 0.1.12 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-validate 0.8.5 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"memmap2",
"serde",
"thiserror",
"winnow 0.6.16",
]
@ -3351,6 +3412,7 @@ dependencies = [
"gix-hash 0.14.2 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-object 0.42.3 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-revwalk 0.13.2 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"serde",
"thiserror",
]
@ -3403,6 +3465,7 @@ dependencies = [
"bitflags 2.6.0",
"gix-path 0.10.9 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"libc",
"serde",
"windows-sys 0.52.0",
]
@ -3484,6 +3547,22 @@ name = "gix-trace"
version = "0.1.9"
source = "git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a#7dff44754e0fdc369f92221468fb953bad9be60a"
[[package]]
name = "gix-transport"
version = "0.42.2"
source = "git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a#7dff44754e0fdc369f92221468fb953bad9be60a"
dependencies = [
"bstr",
"gix-command",
"gix-features 0.38.2 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-packetline",
"gix-quote 0.4.12 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-sec 0.10.7 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-url",
"serde",
"thiserror",
]
[[package]]
name = "gix-traverse"
version = "0.39.2"
@ -3526,6 +3605,7 @@ dependencies = [
"gix-features 0.38.2 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-path 0.10.9 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"home",
"serde",
"thiserror",
"url",
]
@ -3604,6 +3684,7 @@ dependencies = [
"gix-object 0.42.3 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-path 0.10.9 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"gix-validate 0.8.5 (git+https://github.com/Byron/gitoxide?rev=7dff44754e0fdc369f92221468fb953bad9be60a)",
"serde",
]
[[package]]
@ -4498,6 +4579,7 @@ version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec3066350882a1cd6d950d055997f379ac37fd39f81cd4d8ed186032eb3c5747"
dependencies = [
"serde",
"static_assertions",
]
@ -4688,6 +4770,17 @@ version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94"
[[package]]
name = "maybe-async"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5cf92c10c7e361d6b99666ec1c6f9805b0bea2c3bd8c78dc6fe98ac5bd78db11"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.68",
]
[[package]]
name = "md5"
version = "0.7.0"
@ -6679,6 +6772,9 @@ name = "smallvec"
version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
dependencies = [
"serde",
]
[[package]]
name = "socket2"

View File

@ -35,7 +35,7 @@ resolver = "2"
[workspace.dependencies]
bstr = { version = "1.10.0", features = ["serde"] }
# Add the `tracing` or `tracing-detail` features to see more of gitoxide in the logs. Useful to see which programs it invokes.
gix = { git = "https://github.com/Byron/gitoxide", rev = "7dff44754e0fdc369f92221468fb953bad9be60a", default-features = false, features = [] }
gix = { git = "https://github.com/Byron/gitoxide", rev = "7dff44754e0fdc369f92221468fb953bad9be60a", default-features = false, features = ["serde"] }
git2 = { version = "0.18.3", features = [
"vendored-openssl",
"vendored-libgit2",

View File

@ -35,9 +35,12 @@ pub fn list_branches(
for reference in platform.all()?.filter_map(Result::ok) {
// Loosely match on branch names
if let Some(branch_names) = &filter_branch_names {
let has_matching_name = branch_names
.iter()
.any(|branch_name| reference.name().as_bstr().ends_with_str(&branch_name.0));
let has_matching_name = branch_names.iter().any(|branch_name| {
reference
.name()
.as_bstr()
.ends_with_str(branch_name.as_bstr())
});
if !has_matching_name {
continue;
@ -324,7 +327,7 @@ fn should_list_git_branch(identity: &BranchIdentity) -> bool {
b"gitbutler/oplog",
b"HEAD",
];
!TECHNICAL_IDENTITIES.contains(&identity.0.as_bytes())
!TECHNICAL_IDENTITIES.contains(&identity.as_bytes())
}
/// A filter that can be applied to the branch listing
@ -413,9 +416,13 @@ pub struct VirtualBranchReference {
/// a list of enriched branch data in the form of `BranchData`.
pub fn get_branch_listing_details(
ctx: &CommandContext,
branch_names: impl IntoIterator<Item = impl Into<BranchIdentity>>,
branch_names: impl IntoIterator<Item = impl TryInto<BranchIdentity>>,
) -> Result<Vec<BranchListingDetails>> {
let branch_names: Vec<_> = branch_names.into_iter().map(Into::into).collect();
let branch_names: Vec<_> = branch_names
.into_iter()
.map(TryInto::try_into)
.filter_map(Result::ok)
.collect();
let repo = ctx.repository();
let branches = list_branches(ctx, None, Some(branch_names.clone()))?;
let default_target = ctx

View File

@ -154,7 +154,7 @@ mod util {
impl Default for ExpectedBranchListing<'static> {
fn default() -> Self {
ExpectedBranchListing {
identity: "<invalid identity - should always be specified".into(),
identity: "invalid-identity-should-always-be-specified".into(),
remotes: vec![],
virtual_branch_given_name: None,
virtual_branch_in_workspace: false,

View File

@ -8,7 +8,7 @@ publish = false
[dependencies]
anyhow = "1.0.86"
git2.workspace = true
gix.workspace = true
gix = { workspace = true, features = ["serde"] }
gitbutler-reference.workspace = true
gitbutler-serde.workspace = true
gitbutler-id.workspace = true

View File

@ -1,5 +1,5 @@
use anyhow::Result;
use bstr::{BStr, BString, ByteSlice};
use bstr::BStr;
use gitbutler_id::id::Id;
use gitbutler_reference::{normalize_branch_name, Refname, RemoteRefname, VirtualRefname};
use serde::{Deserialize, Serialize};
@ -149,29 +149,40 @@ pub struct BranchCreateRequest {
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct BranchIdentity(
/// The identity is always a valid reference name, full or partial.
// TODO(ST): make this a partial reference name
pub BString,
pub gix::refs::PartialName,
);
impl Deref for BranchIdentity {
type Target = BStr;
fn deref(&self) -> &Self::Target {
self.0.as_bstr()
self.0.as_ref().as_bstr()
}
}
/// Facilitate obtaining this type from the UI - otherwise it would be better not to have it as it should be
/// a particular thing, not any string.
impl From<String> for BranchIdentity {
fn from(value: String) -> Self {
BranchIdentity(value.into())
/// Facilitate obtaining this type from the UI.
impl TryFrom<String> for BranchIdentity {
type Error = gix::refs::name::Error;
fn try_from(value: String) -> std::result::Result<Self, Self::Error> {
gix::refs::PartialName::try_from(value).map(BranchIdentity)
}
}
/// Also not for testing.
/// Used in testing, and **panics** if the value isn't a valid partial ref name
impl From<&str> for BranchIdentity {
fn from(value: &str) -> Self {
BranchIdentity(value.into())
gix::refs::PartialName::try_from(value)
.map(BranchIdentity)
.expect("BUG: value must be valid ref name")
}
}
/// Used in for short-name conversions
impl TryFrom<&BStr> for BranchIdentity {
type Error = gix::refs::name::Error;
fn try_from(value: &BStr) -> std::result::Result<Self, Self::Error> {
gix::refs::PartialName::try_from(value.to_owned()).map(BranchIdentity)
}
}

View File

@ -71,7 +71,7 @@ impl ReferenceExtGix for &gix::refs::FullNameRef {
.category_and_short_name()
.context("Branch could not be categorized")?;
if !matches!(category, Category::RemoteBranch) {
return Ok(BranchIdentity(shorthand_name.into()));
return Ok(shorthand_name.try_into()?);
}
let longest_remote = remotes
@ -91,6 +91,6 @@ impl ReferenceExtGix for &gix::refs::FullNameRef {
))?
.into();
Ok(BranchIdentity(shorthand_name.into()))
Ok(shorthand_name.try_into()?)
}
}