This became possible now that `FullNameRef` is a proper ref, not just ref-like.
Unified · Split
| @@ -69,11 +69,6 @@ impl FullName { · 68 unmodified lines | ||
| 69 | 69 | crate::PartialNameCow(self.0.as_bstr().into()) |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | − /// Interpret this fully qualified reference as shared full name | |
| 73 | − pub fn to_ref(&self) -> &FullNameRef { | |
| 74 | − self.borrow() | |
| 75 | − } | |
| 76 | − | |
| 77 | 72 | /// Convert this name into the relative path, lossily, identifying the reference location relative to a repository |
| 78 | 73 | pub fn to_path(&self) -> &Path { |
| 79 | 74 | git_path::from_byte_slice(&self.0) |
| @@ -111,27 +106,22 @@ impl FullName { · 31 unmodified lines | ||
| 111 | 106 | /// |
| 112 | 107 | /// If there is no such prefix, the original name is returned. |
| 113 | 108 | pub fn shorten(&self) -> &BStr { |
| 114 | − self.to_ref().shorten() | |
| 109 | + self.as_ref().shorten() | |
| 115 | 110 | } |
| 116 | 111 | |
| 117 | 112 | /// Classify this name, or return `None` if it's unclassified. |
| 118 | 113 | pub fn category(&self) -> Option<crate::Category<'_>> { |
| 119 | − self.to_ref().category() | |
| 114 | + self.as_ref().category() | |
| 120 | 115 | } |
| 121 | 116 | |
| 122 | 117 | /// Classify this name, or return `None` if it's unclassified. If `Some`, |
| 123 | 118 | /// the shortened name is returned as well. |
| 124 | 119 | pub fn category_and_short_name(&self) -> Option<(crate::Category<'_>, &BStr)> { |
| 125 | − self.to_ref().category_and_short_name() | |
| 120 | + self.as_ref().category_and_short_name() | |
| 126 | 121 | } |
| 127 | 122 | } |
| 128 | 123 | |
| 129 | 124 | impl FullNameRef { |
| 130 | − /// Create an owned copy of ourself | |
| 131 | − pub fn to_owned(&self) -> FullName { | |
| 132 | − FullName(self.0.to_owned()) | |
| 133 | − } | |
| 134 | − | |
| 135 | 125 | /// Return the file name portion of a full name, for instance `main` if the |
| 136 | 126 | /// full name was `refs/heads/main`. |
| 137 | 127 | pub fn file_name(&self) -> &BStr { |
| @@ -111,6 +111,14 @@ pub struct FullNameRef(BStr); · 110 unmodified lines | ||
| 111 | 111 | #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)] |
| 112 | 112 | pub struct PartialNameCow<'a>(Cow<'a, BStr>); |
| 113 | 113 | |
| 114 | +/// A validated complete and fully qualified referenced reference name, safe to use for all operations. | |
| 115 | +#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd)] | |
| 116 | +pub struct PartialNameRef(BStr); | |
| 117 | + | |
| 118 | +/// A validated complete and fully qualified owned reference name, safe to use for all operations. | |
| 119 | +#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd)] | |
| 120 | +pub struct PartialName(BString); | |
| 121 | + | |
| 114 | 122 | /// A _validated_ prefix for references to act as a namespace. |
| 115 | 123 | #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)] |
| 116 | 124 | pub struct Namespace(BString); |
| @@ -125,7 +125,7 @@ impl file::Store { · 124 unmodified lines | ||
| 125 | 125 | let full_name = match &self.namespace { |
| 126 | 126 | Some(namespace) => { |
| 127 | 127 | full_name_backing = namespace.to_owned().into_namespaced_name(full_name); |
| 128 | − full_name_backing.to_ref() | |
| 128 | + full_name_backing.as_ref() | |
| 129 | 129 | } |
| 130 | 130 | None => full_name, |
| 131 | 131 | }; |
| @@ -18,7 +18,7 @@ impl Reference { · 17 unmodified lines | ||
| 18 | 18 | /// If the caller needs to know if it's readable, try to read the log instead with a reverse or forward iterator. |
| 19 | 19 | pub fn log_exists(&self, store: &file::Store) -> bool { |
| 20 | 20 | store |
| 21 | − .reflog_exists(self.name.to_ref()) | |
| 21 | + .reflog_exists(self.name.as_ref()) | |
| 22 | 22 | .expect("name conversion infallible") |
| 23 | 23 | } |
| 24 | 24 | /// Return a reflog reverse iterator for this ref, reading chunks from the back into the fixed buffer `buf`, in the given `store`. |
| @@ -30,7 +30,7 @@ impl Reference { · 5 unmodified lines | ||
| 30 | 30 | store: &file::Store, |
| 31 | 31 | buf: &'b mut [u8], |
| 32 | 32 | ) -> std::io::Result<Option<log::iter::Reverse<'b, std::fs::File>>> { |
| 33 | − store.reflog_iter_rev(self.name.to_ref(), buf).map_err(must_be_io_err) | |
| 33 | + store.reflog_iter_rev(self.name.as_ref(), buf).map_err(must_be_io_err) | |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | /// Return a reflog forward iterator for this ref and write its file contents into `buf`, in the given `store`. |
| @@ -42,6 +42,6 @@ impl Reference { · 5 unmodified lines | ||
| 42 | 42 | store: &file::Store, |
| 43 | 43 | buf: &'b mut Vec<u8>, |
| 44 | 44 | ) -> std::io::Result<Option<impl Iterator<Item = Result<log::LineRef<'b>, log::iter::decode::Error>> + 'a>> { |
| 45 | − store.reflog_iter(self.name.to_ref(), buf).map_err(must_be_io_err) | |
| 45 | + store.reflog_iter(self.name.as_ref(), buf).map_err(must_be_io_err) | |
| 46 | 46 | } |
| 47 | 47 | } |
| @@ -55,14 +55,14 @@ impl ReferenceExt for Reference { · 54 unmodified lines | ||
| 55 | 55 | fn log_iter<'a, 's>(&'a self, store: &'s file::Store) -> log::iter::Platform<'a, 's> { |
| 56 | 56 | log::iter::Platform { |
| 57 | 57 | store, |
| 58 | − name: self.name.to_ref(), | |
| 58 | + name: self.name.as_ref(), | |
| 59 | 59 | buf: Vec::new(), |
| 60 | 60 | } |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | fn log_exists(&self, store: &file::Store) -> bool { |
| 64 | 64 | store |
| 65 | − .reflog_exists(self.name.to_ref()) | |
| 65 | + .reflog_exists(self.name.as_ref()) | |
| 66 | 66 | .expect("infallible name conversion") |
| 67 | 67 | } |
| 68 | 68 | |
| @@ -95,7 +95,7 @@ impl ReferenceExt for Reference { · 26 unmodified lines | ||
| 95 | 95 | while let Some(next) = cursor.follow_packed(store, packed) { |
| 96 | 96 | let next = next?; |
| 97 | 97 | if seen.contains(&next.name) { |
| 98 | − return Err(peel::to_id::Error::Cycle(store.reference_path(cursor.name.to_ref()))); | |
| 98 | + return Err(peel::to_id::Error::Cycle(store.reference_path(cursor.name.as_ref()))); | |
| 99 | 99 | } |
| 100 | 100 | *cursor = next; |
| 101 | 101 | seen.insert(cursor.name.clone()); |
| @@ -97,7 +97,7 @@ impl<'s> Transaction<'s> { · 96 unmodified lines | ||
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | for change in updates.iter_mut() { |
| 100 | − let (reflog_root, relative_name) = self.store.reflog_base_and_relative_path(change.update.name.to_ref()); | |
| 100 | + let (reflog_root, relative_name) = self.store.reflog_base_and_relative_path(change.update.name.as_ref()); | |
| 101 | 101 | match &change.update.change { |
| 102 | 102 | Change::Update { .. } => {} |
| 103 | 103 | Change::Delete { .. } => { |
| @@ -139,7 +139,7 @@ impl<'s> Transaction<'s> { · 35 unmodified lines | ||
| 139 | 139 | }; |
| 140 | 140 | if take_lock_and_delete { |
| 141 | 141 | let lock = change.lock.take().expect("lock must still be present in delete mode"); |
| 142 | − let reference_path = self.store.reference_path(change.update.name.to_ref()); | |
| 142 | + let reference_path = self.store.reference_path(change.update.name.as_ref()); | |
| 143 | 143 | if let Err(err) = std::fs::remove_file(reference_path) { |
| 144 | 144 | if err.kind() != std::io::ErrorKind::NotFound { |
| 145 | 145 | return Err(Error::DeleteReference { |
| @@ -26,7 +26,7 @@ impl<'s> Transaction<'s> { · 25 unmodified lines | ||
| 26 | 26 | ); |
| 27 | 27 | |
| 28 | 28 | let existing_ref = store |
| 29 | − .ref_contents(change.update.name.to_ref()) | |
| 29 | + .ref_contents(change.update.name.as_ref()) | |
| 30 | 30 | .map_err(Error::from) |
| 31 | 31 | .and_then(|maybe_loose| { |
| 32 | 32 | maybe_loose |
| @@ -43,7 +43,7 @@ impl<'s> Transaction<'s> { · 10 unmodified lines | ||
| 43 | 43 | }) |
| 44 | 44 | .and_then(|maybe_loose| match (maybe_loose, packed) { |
| 45 | 45 | (None, Some(packed)) => packed |
| 46 | − .try_find(change.update.name.to_ref()) | |
| 46 | + .try_find(change.update.name.as_ref()) | |
| 47 | 47 | .map(|opt| opt.map(Into::into)) |
| 48 | 48 | .map_err(Error::from), |
| 49 | 49 | (None, None) => Ok(None), |
| @@ -51,7 +51,7 @@ impl<'s> Transaction<'s> { · 1 unmodified line | ||
| 51 | 51 | }); |
| 52 | 52 | let lock = match &mut change.update.change { |
| 53 | 53 | Change::Delete { expected, .. } => { |
| 54 | − let (base, relative_path) = store.reference_path_with_base(change.update.name.to_ref()); | |
| 54 | + let (base, relative_path) = store.reference_path_with_base(change.update.name.as_ref()); | |
| 55 | 55 | let lock = git_lock::Marker::acquire_to_hold_resource( |
| 56 | 56 | base.join(relative_path), |
| 57 | 57 | lock_fail_mode, |
| @@ -97,7 +97,7 @@ impl<'s> Transaction<'s> { · 39 unmodified lines | ||
| 97 | 97 | lock |
| 98 | 98 | } |
| 99 | 99 | Change::Update { expected, new, .. } => { |
| 100 | − let (base, relative_path) = store.reference_path_with_base(change.update.name.to_ref()); | |
| 100 | + let (base, relative_path) = store.reference_path_with_base(change.update.name.as_ref()); | |
| 101 | 101 | let mut lock = git_lock::File::acquire_to_update_resource( |
| 102 | 102 | base.join(relative_path), |
| 103 | 103 | lock_fail_mode, |
| @@ -45,7 +45,7 @@ impl packed::Transaction { · 44 unmodified lines | ||
| 45 | 45 | .into_iter() |
| 46 | 46 | .filter(|edit| { |
| 47 | 47 | if let Change::Delete { .. } = edit.change { |
| 48 | − buffer.as_ref().map_or(true, |b| b.find(edit.name.to_ref()).is_ok()) | |
| 48 | + buffer.as_ref().map_or(true, |b| b.find(edit.name.as_ref()).is_ok()) | |
| 49 | 49 | } else { |
| 50 | 50 | true |
| 51 | 51 | } |
| @@ -61,7 +61,7 @@ impl Target { · 60 unmodified lines | ||
| 61 | 61 | pub fn to_ref(&self) -> crate::TargetRef<'_> { |
| 62 | 62 | match self { |
| 63 | 63 | Target::Peeled(oid) => crate::TargetRef::Peeled(oid), |
| 64 | − Target::Symbolic(name) => crate::TargetRef::Symbolic(name.to_ref()), | |
| 64 | + Target::Symbolic(name) => crate::TargetRef::Symbolic(name.as_ref()), | |
| 65 | 65 | } |
| 66 | 66 | } |
| 67 | 67 | |
| @@ -5,7 +5,7 @@ use git_ref::Category; · 4 unmodified lines | ||
| 5 | 5 | #[test] |
| 6 | 6 | fn file_name() { |
| 7 | 7 | let name: git_ref::FullName = "refs/heads/main".try_into().unwrap(); |
| 8 | − assert_eq!(name.to_ref().file_name(), "main"); | |
| 8 | + assert_eq!(name.as_ref().file_name(), "main"); | |
| 9 | 9 | } |
| 10 | 10 | #[test] |
| 11 | 11 | fn shorten_and_category() { |
| @@ -72,14 +72,14 @@ fn shorten_and_category() { · 60 unmodified lines | ||
| 72 | 72 | let name: git_ref::FullName = input.try_into().unwrap(); |
| 73 | 73 | assert_eq!(category.is_worktree_private(), is_worktree_private); |
| 74 | 74 | let category = Some(category); |
| 75 | − assert_eq!(name.to_ref().shorten(), expected); | |
| 75 | + assert_eq!(name.as_ref().shorten(), expected); | |
| 76 | 76 | assert_eq!(name.shorten(), expected); |
| 77 | 77 | assert_eq!(name.category(), category); |
| 78 | 78 | assert_eq!( |
| 79 | 79 | name.category_and_short_name(), |
| 80 | 80 | category.map(|cat| (cat, expected.into())) |
| 81 | 81 | ); |
| 82 | − assert_eq!(name.to_ref().category(), category); | |
| 82 | + assert_eq!(name.as_ref().category(), category); | |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | for special in ["hello/world", "main-worktree/head"] { |