.cargo/
crates/
brain/
diff/
files/
highlight/
repo/
src/
git/
refs/
check.rs
4.0 KiB
lit.rs
5.7 KiB
name.rs
14.4 KiB
namespaced.rs
3.2 KiB
qualified.rs
6.2 KiB
refspec.rs
18.0 KiB
refs.rs
2.3 KiB
git.rs
54.7 KiB
jj.rs
4.7 KiB
lib.rs
12.2 KiB
oid.rs
3.9 KiB
Cargo.toml
330 B
signals/
tui/
tui-core/
tui-termion/
tui-widgets/
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/repo/src/git/refs/refspec.rs
raw
| 1 | // Copyright (c) Kim Altintop <kim@eagain.st> |
| 2 | // Copyright (c) The Radicle Team |
| 3 | |
| 4 | //! Git refspec patterns. |
| 5 | use super::*; |
| 6 | |
| 7 | /// Iterator over slash-separated refspec pattern components. |
| 8 | pub type Iter<'a> = std::str::Split<'a, char>; |
| 9 | |
| 10 | /// The `*` refspec pattern. |
| 11 | pub const STAR: &PatternStr = PatternStr::from_str("*"); |
| 12 | |
| 13 | /// Validation options for refspec patterns. |
| 14 | const CHECK_OPTS: check::Options = check::Options { |
| 15 | allow_onelevel: true, |
| 16 | allow_pattern: true, |
| 17 | }; |
| 18 | |
| 19 | /// A borrowed valid Git refspec pattern. |
| 20 | #[repr(transparent)] |
| 21 | #[derive(Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] |
| 22 | pub struct PatternStr(str); |
| 23 | |
| 24 | impl PatternStr { |
| 25 | /// Validates `s` and returns it as a borrowed pattern. |
| 26 | #[inline] |
| 27 | pub fn try_from_str(s: &str) -> Result<&Self, check::Error> { |
| 28 | TryFrom::try_from(s) |
| 29 | } |
| 30 | |
| 31 | /// Returns the pattern as a string slice. |
| 32 | #[inline] |
| 33 | pub fn as_str(&self) -> &str { |
| 34 | self |
| 35 | } |
| 36 | |
| 37 | /// Joins another refname suffix to this pattern. |
| 38 | pub fn join<R>(&self, other: R) -> PatternString |
| 39 | where |
| 40 | R: AsRef<RefStr>, |
| 41 | { |
| 42 | let mut buf = self.to_owned(); |
| 43 | buf.push(other); |
| 44 | buf |
| 45 | } |
| 46 | |
| 47 | /// Returns this pattern as qualified if it starts with `refs/`. |
| 48 | #[inline] |
| 49 | pub fn qualified(&self) -> Option<QualifiedPattern<'_>> { |
| 50 | QualifiedPattern::from_patternstr(self) |
| 51 | } |
| 52 | |
| 53 | /// Returns this pattern as namespaced if it has a namespace prefix. |
| 54 | #[inline] |
| 55 | pub fn to_namespaced(&self) -> Option<NamespacedPattern<'_>> { |
| 56 | self.into() |
| 57 | } |
| 58 | |
| 59 | /// Returns slash-separated string components. |
| 60 | #[inline] |
| 61 | pub fn iter(&self) -> Iter<'_> { |
| 62 | self.0.split('/') |
| 63 | } |
| 64 | |
| 65 | /// Returns slash-separated pattern components. |
| 66 | #[inline] |
| 67 | pub fn components(&self) -> Components<'_> { |
| 68 | Components::from(self) |
| 69 | } |
| 70 | |
| 71 | /// Converts a known-valid string slice to a pattern. |
| 72 | pub(crate) const fn from_str(s: &str) -> &PatternStr { |
| 73 | // SAFETY: `PatternStr` is a transparent wrapper around `str`. |
| 74 | unsafe { &*(s as *const str as *const PatternStr) } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | impl Deref for PatternStr { |
| 79 | type Target = str; |
| 80 | |
| 81 | #[inline] |
| 82 | fn deref(&self) -> &Self::Target { |
| 83 | &self.0 |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | impl AsRef<str> for PatternStr { |
| 88 | #[inline] |
| 89 | fn as_ref(&self) -> &str { |
| 90 | self |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl AsRef<Self> for PatternStr { |
| 95 | #[inline] |
| 96 | fn as_ref(&self) -> &Self { |
| 97 | self |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | impl<'a> TryFrom<&'a str> for &'a PatternStr { |
| 102 | type Error = check::Error; |
| 103 | |
| 104 | #[inline] |
| 105 | fn try_from(s: &'a str) -> Result<Self, Self::Error> { |
| 106 | check::ref_format(CHECK_OPTS, s).map(|()| PatternStr::from_str(s)) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | impl<'a> From<&'a RefStr> for &'a PatternStr { |
| 111 | #[inline] |
| 112 | fn from(refname: &'a RefStr) -> Self { |
| 113 | PatternStr::from_str(refname.as_str()) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | impl<'a> From<&'a PatternStr> for Cow<'a, PatternStr> { |
| 118 | #[inline] |
| 119 | fn from(pattern: &'a PatternStr) -> Cow<'a, PatternStr> { |
| 120 | Cow::Borrowed(pattern) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | impl Display for PatternStr { |
| 125 | #[inline] |
| 126 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 127 | f.write_str(self) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /// An owned valid Git refspec pattern. |
| 132 | #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] |
| 133 | pub struct PatternString(pub(crate) String); |
| 134 | |
| 135 | impl PatternString { |
| 136 | /// Returns the pattern as a string slice. |
| 137 | #[inline] |
| 138 | pub fn as_str(&self) -> &str { |
| 139 | self.as_ref() |
| 140 | } |
| 141 | |
| 142 | /// Returns this owned pattern as a borrowed pattern. |
| 143 | #[inline] |
| 144 | pub fn as_pattern_str(&self) -> &PatternStr { |
| 145 | self.as_ref() |
| 146 | } |
| 147 | |
| 148 | /// Builds a pattern from slash-separated pattern components. |
| 149 | #[inline] |
| 150 | pub fn from_components<'a, T>(iter: T) -> Result<Self, DuplicateGlob> |
| 151 | where |
| 152 | T: IntoIterator<Item = Component<'a>>, |
| 153 | { |
| 154 | iter.into_iter().collect() |
| 155 | } |
| 156 | |
| 157 | /// Joins another refname suffix to this pattern. |
| 158 | #[inline] |
| 159 | pub fn and<R>(mut self, other: R) -> Self |
| 160 | where |
| 161 | R: AsRef<RefStr>, |
| 162 | { |
| 163 | self.push(other); |
| 164 | self |
| 165 | } |
| 166 | |
| 167 | /// Pushes another refname suffix onto this pattern. |
| 168 | #[inline] |
| 169 | pub fn push<R>(&mut self, other: R) |
| 170 | where |
| 171 | R: AsRef<RefStr>, |
| 172 | { |
| 173 | self.0.push('/'); |
| 174 | self.0.push_str(other.as_ref().as_str()); |
| 175 | } |
| 176 | |
| 177 | /// Removes the final slash-separated component. |
| 178 | pub fn pop(&mut self) -> bool { |
| 179 | match self.0.rfind('/') { |
| 180 | Some(index) => { |
| 181 | self.0.truncate(index); |
| 182 | true |
| 183 | } |
| 184 | None => false, |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | impl Deref for PatternString { |
| 190 | type Target = PatternStr; |
| 191 | |
| 192 | #[inline] |
| 193 | fn deref(&self) -> &Self::Target { |
| 194 | self.borrow() |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | impl AsRef<PatternStr> for PatternString { |
| 199 | #[inline] |
| 200 | fn as_ref(&self) -> &PatternStr { |
| 201 | self |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | impl AsRef<str> for PatternString { |
| 206 | #[inline] |
| 207 | fn as_ref(&self) -> &str { |
| 208 | self.0.as_str() |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | impl Borrow<PatternStr> for PatternString { |
| 213 | #[inline] |
| 214 | fn borrow(&self) -> &PatternStr { |
| 215 | PatternStr::from_str(self.0.as_str()) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | impl ToOwned for PatternStr { |
| 220 | type Owned = PatternString; |
| 221 | |
| 222 | #[inline] |
| 223 | fn to_owned(&self) -> Self::Owned { |
| 224 | PatternString(self.0.to_owned()) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | impl From<RefString> for PatternString { |
| 229 | #[inline] |
| 230 | fn from(refname: RefString) -> Self { |
| 231 | Self(refname.into()) |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | impl<'a> From<&'a PatternString> for Cow<'a, PatternStr> { |
| 236 | #[inline] |
| 237 | fn from(pattern: &'a PatternString) -> Cow<'a, PatternStr> { |
| 238 | Cow::Borrowed(pattern.as_ref()) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | impl From<PatternString> for String { |
| 243 | #[inline] |
| 244 | fn from(pattern: PatternString) -> Self { |
| 245 | pattern.0 |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | impl TryFrom<&str> for PatternString { |
| 250 | type Error = check::Error; |
| 251 | |
| 252 | #[inline] |
| 253 | fn try_from(s: &str) -> Result<Self, Self::Error> { |
| 254 | PatternStr::try_from_str(s).map(ToOwned::to_owned) |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | impl TryFrom<String> for PatternString { |
| 259 | type Error = check::Error; |
| 260 | |
| 261 | #[inline] |
| 262 | fn try_from(s: String) -> Result<Self, Self::Error> { |
| 263 | check::ref_format(CHECK_OPTS, s.as_str()).map(|()| PatternString(s)) |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /// Error returned when a refspec pattern has multiple glob components. |
| 268 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 269 | pub struct DuplicateGlob; |
| 270 | |
| 271 | impl Display for DuplicateGlob { |
| 272 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 273 | f.write_str("more than one '*' encountered") |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | impl std::error::Error for DuplicateGlob {} |
| 278 | |
| 279 | impl<'a> FromIterator<Component<'a>> for Result<PatternString, DuplicateGlob> { |
| 280 | fn from_iter<T>(iter: T) -> Self |
| 281 | where |
| 282 | T: IntoIterator<Item = Component<'a>>, |
| 283 | { |
| 284 | let mut buf = String::new(); |
| 285 | let mut seen_glob = false; |
| 286 | for component in iter { |
| 287 | if matches!(component, Component::Glob(_)) { |
| 288 | if seen_glob { |
| 289 | return Err(DuplicateGlob); |
| 290 | } |
| 291 | seen_glob = true; |
| 292 | } |
| 293 | if !buf.is_empty() { |
| 294 | buf.push('/'); |
| 295 | } |
| 296 | buf.push_str(component.as_str()); |
| 297 | } |
| 298 | assert!(!buf.is_empty(), "PatternString::from_iter: empty iterator"); |
| 299 | Ok(PatternString(buf)) |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | impl Display for PatternString { |
| 304 | #[inline] |
| 305 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 306 | f.write_str(&self.0) |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /// A component of a refspec pattern. |
| 311 | pub enum Component<'a> { |
| 312 | /// A `*` glob or a component containing `*`. |
| 313 | Glob(Option<&'a PatternStr>), |
| 314 | /// A normal refname component. |
| 315 | Normal(&'a RefStr), |
| 316 | } |
| 317 | |
| 318 | impl Component<'_> { |
| 319 | /// Returns this component as a string slice. |
| 320 | #[inline] |
| 321 | pub fn as_str(&self) -> &str { |
| 322 | self.as_ref() |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | impl AsRef<str> for Component<'_> { |
| 327 | #[inline] |
| 328 | fn as_ref(&self) -> &str { |
| 329 | match self { |
| 330 | Self::Glob(None) => "*", |
| 331 | Self::Glob(Some(pattern)) => pattern.as_str(), |
| 332 | Self::Normal(refname) => refname.as_str(), |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | impl Display for Component<'_> { |
| 338 | #[inline] |
| 339 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 340 | f.write_str(self.as_str()) |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | impl<T: lit::Lit> From<T> for Component<'static> { |
| 345 | #[inline] |
| 346 | fn from(_: T) -> Self { |
| 347 | Self::Normal(T::NAME) |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /// Iterator over refspec pattern components. |
| 352 | #[must_use = "iterators are lazy and do nothing unless consumed"] |
| 353 | #[derive(Clone)] |
| 354 | pub struct Components<'a> { |
| 355 | /// Inner string splitter. |
| 356 | inner: Iter<'a>, |
| 357 | } |
| 358 | |
| 359 | impl<'a> Iterator for Components<'a> { |
| 360 | type Item = Component<'a>; |
| 361 | |
| 362 | #[inline] |
| 363 | fn next(&mut self) -> Option<Self::Item> { |
| 364 | self.inner.next().map(pattern_component) |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | impl<'a> DoubleEndedIterator for Components<'a> { |
| 369 | #[inline] |
| 370 | fn next_back(&mut self) -> Option<Self::Item> { |
| 371 | self.inner.next_back().map(pattern_component) |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | impl<'a> From<&'a PatternStr> for Components<'a> { |
| 376 | #[inline] |
| 377 | fn from(pattern: &'a PatternStr) -> Self { |
| 378 | Self { |
| 379 | inner: pattern.as_str().split('/'), |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /// Converts a string component to a pattern component. |
| 385 | fn pattern_component(component: &str) -> Component<'_> { |
| 386 | match component { |
| 387 | "*" => Component::Glob(None), |
| 388 | value if value.contains('*') => Component::Glob(Some(PatternStr::from_str(value))), |
| 389 | value => Component::Normal(RefStr::from_str(value)), |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /// Common refspec pattern components. |
| 394 | pub mod component { |
| 395 | use super::{Component, PatternStr}; |
| 396 | use crate::git::refs::name; |
| 397 | |
| 398 | /// The `*` component. |
| 399 | pub const STAR: Component<'static> = Component::Glob(None); |
| 400 | /// The `heads` component. |
| 401 | pub const HEADS: Component<'static> = Component::Normal(name::HEADS); |
| 402 | /// The `main` component. |
| 403 | pub const MAIN: Component<'static> = Component::Normal(name::MAIN); |
| 404 | /// The `master` component. |
| 405 | pub const MASTER: Component<'static> = Component::Normal(name::MASTER); |
| 406 | /// The `namespaces` component. |
| 407 | pub const NAMESPACES: Component<'static> = Component::Normal(name::NAMESPACES); |
| 408 | /// The `notes` component. |
| 409 | pub const NOTES: Component<'static> = Component::Normal(name::NOTES); |
| 410 | /// The `origin` component. |
| 411 | pub const ORIGIN: Component<'static> = Component::Normal(name::ORIGIN); |
| 412 | /// The `refs` component. |
| 413 | pub const REFS: Component<'static> = Component::Normal(name::REFS); |
| 414 | /// The `remotes` component. |
| 415 | pub const REMOTES: Component<'static> = Component::Normal(name::REMOTES); |
| 416 | /// The `tags` component. |
| 417 | pub const TAGS: Component<'static> = Component::Normal(name::TAGS); |
| 418 | /// A typed reference to the `*` pattern. |
| 419 | pub const STAR_PATTERN: &PatternStr = super::STAR; |
| 420 | } |
| 421 | |
| 422 | /// A fully qualified refspec pattern. |
| 423 | #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] |
| 424 | pub struct QualifiedPattern<'a>(pub(crate) Cow<'a, PatternStr>); |
| 425 | |
| 426 | impl<'a> QualifiedPattern<'a> { |
| 427 | /// Returns `pattern` as qualified if it starts with `refs/` and has a category and name. |
| 428 | pub fn from_patternstr(pattern: impl Into<Cow<'a, PatternStr>>) -> Option<Self> { |
| 429 | let pattern = pattern.into(); |
| 430 | let mut iter = pattern.iter(); |
| 431 | match (iter.next(), iter.next(), iter.next()) { |
| 432 | (Some("refs"), Some(_), Some(_)) => Some(Self(pattern)), |
| 433 | _ => None, |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /// Returns the qualified pattern as a string. |
| 438 | #[inline] |
| 439 | pub fn as_str(&self) -> &str { |
| 440 | self.as_ref() |
| 441 | } |
| 442 | |
| 443 | /// Joins another refname suffix. |
| 444 | #[inline] |
| 445 | pub fn join<'b, R>(&self, other: R) -> QualifiedPattern<'b> |
| 446 | where |
| 447 | R: AsRef<RefStr>, |
| 448 | { |
| 449 | QualifiedPattern(Cow::Owned(self.0.join(other))) |
| 450 | } |
| 451 | |
| 452 | /// Returns this pattern as namespaced if it has a namespace prefix. |
| 453 | #[inline] |
| 454 | pub fn to_namespaced(&self) -> Option<NamespacedPattern<'_>> { |
| 455 | self.0.as_ref().into() |
| 456 | } |
| 457 | |
| 458 | /// Adds `refs/namespaces/<namespace>/` before this pattern. |
| 459 | pub fn with_namespace<'b>( |
| 460 | &self, |
| 461 | namespace: crate::git::refs::Component<'b>, |
| 462 | ) -> Result<NamespacedPattern<'a>, DuplicateGlob> { |
| 463 | let prefix = [ |
| 464 | Component::Normal(name::REFS), |
| 465 | Component::Normal(name::NAMESPACES), |
| 466 | Component::Normal(namespace.as_ref()), |
| 467 | ]; |
| 468 | PatternString::from_components(IntoIterator::into_iter(prefix).chain(self.components())) |
| 469 | .map(|pattern| NamespacedPattern(Cow::Owned(pattern))) |
| 470 | } |
| 471 | |
| 472 | /// Returns the first three string components and the remaining iterator. |
| 473 | #[allow(clippy::panic)] |
| 474 | pub fn non_empty_iter(&self) -> (&str, &str, &str, Iter<'_>) { |
| 475 | let mut iter = self.iter(); |
| 476 | let (Some(first), Some(second), Some(third)) = (iter.next(), iter.next(), iter.next()) |
| 477 | else { |
| 478 | panic!("QualifiedPattern::non_empty_iter: missing component"); |
| 479 | }; |
| 480 | (first, second, third, iter) |
| 481 | } |
| 482 | |
| 483 | /// Returns the first three components and the remaining iterator. |
| 484 | #[allow(clippy::panic)] |
| 485 | pub fn non_empty_components( |
| 486 | &self, |
| 487 | ) -> (Component<'_>, Component<'_>, Component<'_>, Components<'_>) { |
| 488 | let mut components = self.components(); |
| 489 | let (Some(first), Some(second), Some(third)) = |
| 490 | (components.next(), components.next(), components.next()) |
| 491 | else { |
| 492 | panic!("QualifiedPattern::non_empty_components: missing component"); |
| 493 | }; |
| 494 | (first, second, third, components) |
| 495 | } |
| 496 | |
| 497 | /// Returns an owned qualified pattern. |
| 498 | #[inline] |
| 499 | pub fn to_owned<'b>(&self) -> QualifiedPattern<'b> { |
| 500 | QualifiedPattern(Cow::Owned(self.0.clone().into_owned())) |
| 501 | } |
| 502 | |
| 503 | /// Converts this value into an owned qualified pattern. |
| 504 | #[inline] |
| 505 | pub fn into_owned<'b>(self) -> QualifiedPattern<'b> { |
| 506 | QualifiedPattern(Cow::Owned(self.0.into_owned())) |
| 507 | } |
| 508 | |
| 509 | /// Converts this qualified pattern into an owned pattern. |
| 510 | #[inline] |
| 511 | pub fn into_patternstring(self) -> PatternString { |
| 512 | self.into() |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | impl Deref for QualifiedPattern<'_> { |
| 517 | type Target = PatternStr; |
| 518 | |
| 519 | #[inline] |
| 520 | fn deref(&self) -> &Self::Target { |
| 521 | &self.0 |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | impl AsRef<PatternStr> for QualifiedPattern<'_> { |
| 526 | #[inline] |
| 527 | fn as_ref(&self) -> &PatternStr { |
| 528 | self |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | impl AsRef<str> for QualifiedPattern<'_> { |
| 533 | #[inline] |
| 534 | fn as_ref(&self) -> &str { |
| 535 | self.0.as_str() |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | impl AsRef<Self> for QualifiedPattern<'_> { |
| 540 | #[inline] |
| 541 | fn as_ref(&self) -> &Self { |
| 542 | self |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | impl<'a> From<QualifiedPattern<'a>> for Cow<'a, PatternStr> { |
| 547 | #[inline] |
| 548 | fn from(pattern: QualifiedPattern<'a>) -> Self { |
| 549 | pattern.0 |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | impl From<QualifiedPattern<'_>> for PatternString { |
| 554 | #[inline] |
| 555 | fn from(pattern: QualifiedPattern<'_>) -> Self { |
| 556 | pattern.0.into_owned() |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | /// A qualified refspec pattern under a Git namespace. |
| 561 | #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] |
| 562 | pub struct NamespacedPattern<'a>(Cow<'a, PatternStr>); |
| 563 | |
| 564 | impl<'a> NamespacedPattern<'a> { |
| 565 | /// Returns the namespace component. |
| 566 | #[allow(clippy::panic)] |
| 567 | pub fn namespace(&self) -> Component<'_> { |
| 568 | let Some(namespace) = self.components().nth(2) else { |
| 569 | panic!("NamespacedPattern::namespace: missing namespace component"); |
| 570 | }; |
| 571 | namespace |
| 572 | } |
| 573 | |
| 574 | /// Removes the outer namespace. |
| 575 | #[allow(clippy::panic)] |
| 576 | pub fn strip_namespace(&self) -> PatternString { |
| 577 | match PatternString::from_components(self.components().skip(3)) { |
| 578 | Ok(pattern) => pattern, |
| 579 | Err(_) => panic!("NamespacedPattern::strip_namespace: duplicate glob"), |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /// Removes all nested namespaces. |
| 584 | pub fn strip_namespace_recursive(&self) -> PatternString { |
| 585 | let mut stripped = self.strip_namespace(); |
| 586 | while let Some(namespaced) = stripped.to_namespaced() { |
| 587 | stripped = namespaced.strip_namespace(); |
| 588 | } |
| 589 | stripped |
| 590 | } |
| 591 | |
| 592 | /// Returns an owned namespaced pattern. |
| 593 | #[inline] |
| 594 | pub fn to_owned<'b>(&self) -> NamespacedPattern<'b> { |
| 595 | NamespacedPattern(Cow::Owned(self.0.clone().into_owned())) |
| 596 | } |
| 597 | |
| 598 | /// Converts this value into an owned namespaced pattern. |
| 599 | #[inline] |
| 600 | pub fn into_owned<'b>(self) -> NamespacedPattern<'b> { |
| 601 | NamespacedPattern(Cow::Owned(self.0.into_owned())) |
| 602 | } |
| 603 | |
| 604 | /// Converts this value into a qualified pattern. |
| 605 | #[inline] |
| 606 | pub fn into_qualified(self) -> QualifiedPattern<'a> { |
| 607 | QualifiedPattern(self.0) |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | impl Deref for NamespacedPattern<'_> { |
| 612 | type Target = PatternStr; |
| 613 | |
| 614 | #[inline] |
| 615 | fn deref(&self) -> &Self::Target { |
| 616 | self.borrow() |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | impl AsRef<PatternStr> for NamespacedPattern<'_> { |
| 621 | #[inline] |
| 622 | fn as_ref(&self) -> &PatternStr { |
| 623 | self.0.as_ref() |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | impl AsRef<str> for NamespacedPattern<'_> { |
| 628 | #[inline] |
| 629 | fn as_ref(&self) -> &str { |
| 630 | self.0.as_str() |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | impl Borrow<PatternStr> for NamespacedPattern<'_> { |
| 635 | #[inline] |
| 636 | fn borrow(&self) -> &PatternStr { |
| 637 | PatternStr::from_str(self.0.as_str()) |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | impl<'a> From<NamespacedPattern<'a>> for QualifiedPattern<'a> { |
| 642 | #[inline] |
| 643 | fn from(pattern: NamespacedPattern<'a>) -> Self { |
| 644 | Self(pattern.0) |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | impl<'a> From<&'a PatternStr> for Option<NamespacedPattern<'a>> { |
| 649 | fn from(pattern: &'a PatternStr) -> Self { |
| 650 | let mut components = pattern.iter(); |
| 651 | match ( |
| 652 | components.next(), |
| 653 | components.next(), |
| 654 | components.next(), |
| 655 | components.next(), |
| 656 | ) { |
| 657 | (Some("refs"), Some("namespaces"), Some(_), Some("refs")) => { |
| 658 | Some(NamespacedPattern(Cow::from(pattern))) |
| 659 | } |
| 660 | _ => None, |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | impl Display for NamespacedPattern<'_> { |
| 666 | #[inline] |
| 667 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 668 | self.0.fmt(f) |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | /// A Git refspec with source, destination, and force flag. |
| 673 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 674 | pub struct Refspec<T, U> { |
| 675 | /// Source refspec side. |
| 676 | pub src: T, |
| 677 | /// Destination refspec side. |
| 678 | pub dst: U, |
| 679 | /// Whether this is a forced refspec. |
| 680 | pub force: bool, |
| 681 | } |
| 682 | |
| 683 | impl<T, U> Display for Refspec<T, U> |
| 684 | where |
| 685 | T: Display, |
| 686 | U: Display, |
| 687 | { |
| 688 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 689 | if self.force { |
| 690 | f.write_char('+')?; |
| 691 | } |
| 692 | write!(f, "{}:{}", self.src, self.dst) |
| 693 | } |
| 694 | } |