History

change!: remove `FullName::to_ref()` in favor of `•::as_ref()`. (#301)

This became possible now that `FullNameRef` is a proper ref, not just ref-like.

Sebastian Thiel committed 4 years ago

Commit f1dbb6b1 parent 9cf0c7b10 files · +29 −31Browse files

Unified · Split

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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t