Skip to content

Commit 40b774f

Browse files
committed
refactor
- fix typo - move and simplify test; minimize its fixture - adjust docs
1 parent 2affbab commit 40b774f

File tree

10 files changed

+90
-132
lines changed

10 files changed

+90
-132
lines changed

gix-features/src/fs.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,12 @@ pub mod walkdir {
208208
///
209209
/// Use `precompose_unicode` to represent the `core.precomposeUnicode` configuration option.
210210
/// Use `max_depth` to limit the depth of the recursive walk.
211-
/// * 0 -> Returns only the root path with no children
212-
/// * 1 -> Root directory and children.
213-
/// * 2..n -> Root directory, children and {n}-grandchildren
211+
/// * `0`
212+
/// - Returns only the root path with no children
213+
/// * `1`
214+
/// - Root directory and children.
215+
/// * `1..n`
216+
/// - Root directory, children and {n}-grandchildren
214217
pub fn walkdir_sorted_new(root: &Path, _: Parallelism, max_depth: usize, precompose_unicode: bool) -> WalkDir {
215218
WalkDir {
216219
inner: WalkDirImpl::new(root)

gix-ref/src/store/file/loose/iter.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ impl SortedLoosePaths {
2222
base: PathBuf,
2323
prefix: Option<BString>,
2424
suffix: Option<BString>,
25-
root_only: bool,
2625
precompose_unicode: bool,
2726
) -> Self {
28-
let depth = if root_only { 1 } else { usize::MAX };
27+
let depth = if suffix.is_some() { 1 } else { usize::MAX };
2928
SortedLoosePaths {
3029
base,
3130
prefix,

gix-ref/src/store/file/overlay_iter.rs

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use gix_object::bstr::ByteSlice;
2+
use gix_path::RelativePath;
13
use std::{
24
borrow::Cow,
35
cmp::Ordering,
@@ -6,9 +8,6 @@ use std::{
68
path::{Path, PathBuf},
79
};
810

9-
use gix_object::bstr::ByteSlice;
10-
use gix_path::RelativePath;
11-
1211
use crate::{
1312
file::loose::{self, iter::SortedLoosePaths},
1413
store_impl::{file, packed},
@@ -85,46 +84,40 @@ impl<'p> LooseThenPacked<'p, '_> {
8584
}
8685

8786
fn convert_loose(&mut self, res: std::io::Result<(PathBuf, FullName)>) -> Result<Reference, Error> {
88-
convert_loose(&mut self.buf, self.git_dir, self.common_dir, self.namespace, res)
89-
}
90-
}
91-
92-
pub(crate) fn convert_loose(
93-
buf: &mut Vec<u8>,
94-
git_dir: &Path,
95-
common_dir: Option<&Path>,
96-
namespace: Option<&Namespace>,
97-
res: std::io::Result<(PathBuf, FullName)>,
98-
) -> Result<Reference, Error> {
99-
let (refpath, name) = res.map_err(Error::Traversal)?;
100-
std::fs::File::open(&refpath)
101-
.and_then(|mut f| {
102-
buf.clear();
103-
f.read_to_end(buf)
104-
})
105-
.map_err(|err| Error::ReadFileContents {
106-
source: err,
107-
path: refpath.to_owned(),
108-
})?;
109-
loose::Reference::try_from_path(name, buf)
110-
.map_err(|err| {
111-
let relative_path = refpath
112-
.strip_prefix(git_dir)
113-
.ok()
114-
.or_else(|| common_dir.and_then(|common_dir| refpath.strip_prefix(common_dir).ok()))
115-
.expect("one of our bases contains the path");
116-
Error::ReferenceCreation {
87+
let buf = &mut self.buf;
88+
let git_dir = self.git_dir;
89+
let common_dir = self.common_dir;
90+
let namespace = self.namespace;
91+
let (refpath, name) = res.map_err(Error::Traversal)?;
92+
std::fs::File::open(&refpath)
93+
.and_then(|mut f| {
94+
buf.clear();
95+
f.read_to_end(buf)
96+
})
97+
.map_err(|err| Error::ReadFileContents {
11798
source: err,
118-
relative_path: relative_path.into(),
119-
}
120-
})
121-
.map(Into::into)
122-
.map(|mut r: Reference| {
123-
if let Some(namespace) = namespace {
124-
r.strip_namespace(namespace);
125-
}
126-
r
127-
})
99+
path: refpath.to_owned(),
100+
})?;
101+
loose::Reference::try_from_path(name, buf)
102+
.map_err(|err| {
103+
let relative_path = refpath
104+
.strip_prefix(git_dir)
105+
.ok()
106+
.or_else(|| common_dir.and_then(|common_dir| refpath.strip_prefix(common_dir).ok()))
107+
.expect("one of our bases contains the path");
108+
Error::ReferenceCreation {
109+
source: err,
110+
relative_path: relative_path.into(),
111+
}
112+
})
113+
.map(Into::into)
114+
.map(|mut r: Reference| {
115+
if let Some(namespace) = namespace {
116+
r.strip_namespace(namespace);
117+
}
118+
r
119+
})
120+
}
128121
}
129122

130123
impl Iterator for LooseThenPacked<'_, '_> {
@@ -203,9 +196,9 @@ impl Iterator for LooseThenPacked<'_, '_> {
203196
}
204197

205198
impl Platform<'_> {
206-
/// Return an iterator over all references, loose or `packed`, sorted by their name.
199+
/// Return an iterator over all references, loose or packed, sorted by their name.
207200
///
208-
/// Errors are returned similarly to what would happen when loose and packed refs where iterated by themselves.
201+
/// Errors are returned similarly to what would happen when loose and packed refs were iterated by themselves.
209202
pub fn all(&self) -> std::io::Result<LooseThenPacked<'_, '_>> {
210203
self.store.iter_packed(self.packed.as_ref().map(|b| &***b))
211204
}
@@ -224,15 +217,15 @@ impl Platform<'_> {
224217
}
225218

226219
/// Return an iterator over the pseudo references
227-
pub fn psuedo_refs(&self) -> std::io::Result<LooseThenPacked<'_, '_>> {
228-
self.store.iter_pseudo_refs()
220+
pub fn pseudo_refs(&self) -> std::io::Result<LooseThenPacked<'_, '_>> {
221+
self.store.iter_pseudo()
229222
}
230223
}
231224

232225
impl file::Store {
233226
/// Return a platform to obtain iterator over all references, or prefixed ones, loose or packed, sorted by their name.
234227
///
235-
/// Errors are returned similarly to what would happen when loose and packed refs where iterated by themselves.
228+
/// Errors are returned similarly to what would happen when loose and packed refs were iterated by themselves.
236229
///
237230
/// Note that since packed-refs are storing refs as precomposed unicode if [`Self::precompose_unicode`] is true, for consistency
238231
/// we also return loose references as precomposed unicode.
@@ -271,7 +264,7 @@ pub(crate) enum IterInfo<'a> {
271264
/// If `true`, we will convert decomposed into precomposed unicode.
272265
precompose_unicode: bool,
273266
},
274-
PseudoRefs {
267+
Pseudo {
275268
base: &'a Path,
276269
precompose_unicode: bool,
277270
},
@@ -284,7 +277,7 @@ impl<'a> IterInfo<'a> {
284277
IterInfo::PrefixAndBase { prefix, .. } => Some(gix_path::into_bstr(*prefix)),
285278
IterInfo::BaseAndIterRoot { prefix, .. } => Some(gix_path::into_bstr(prefix.clone())),
286279
IterInfo::ComputedIterationRoot { prefix, .. } => Some(prefix.clone()),
287-
IterInfo::PseudoRefs { .. } => None,
280+
IterInfo::Pseudo { .. } => None,
288281
}
289282
}
290283

@@ -293,18 +286,18 @@ impl<'a> IterInfo<'a> {
293286
IterInfo::Base {
294287
base,
295288
precompose_unicode,
296-
} => SortedLoosePaths::at(&base.join("refs"), base.into(), None, None, false, precompose_unicode),
289+
} => SortedLoosePaths::at(&base.join("refs"), base.into(), None, None, precompose_unicode),
297290
IterInfo::BaseAndIterRoot {
298291
base,
299292
iter_root,
300293
prefix: _,
301294
precompose_unicode,
302-
} => SortedLoosePaths::at(&iter_root, base.into(), None, None, false, precompose_unicode),
295+
} => SortedLoosePaths::at(&iter_root, base.into(), None, None, precompose_unicode),
303296
IterInfo::PrefixAndBase {
304297
base,
305298
prefix,
306299
precompose_unicode,
307-
} => SortedLoosePaths::at(&base.join(prefix), base.into(), None, None, false, precompose_unicode),
300+
} => SortedLoosePaths::at(&base.join(prefix), base.into(), None, None, precompose_unicode),
308301
IterInfo::ComputedIterationRoot {
309302
iter_root,
310303
base,
@@ -315,13 +308,12 @@ impl<'a> IterInfo<'a> {
315308
base.into(),
316309
Some(prefix.into_owned()),
317310
None,
318-
false,
319311
precompose_unicode,
320312
),
321-
IterInfo::PseudoRefs {
313+
IterInfo::Pseudo {
322314
base,
323315
precompose_unicode,
324-
} => SortedLoosePaths::at(base, base.into(), None, Some("HEAD".into()), true, precompose_unicode),
316+
} => SortedLoosePaths::at(base, base.into(), None, Some("HEAD".into()), precompose_unicode),
325317
}
326318
.peekable()
327319
}
@@ -354,7 +346,7 @@ impl<'a> IterInfo<'a> {
354346
impl file::Store {
355347
/// Return an iterator over all references, loose or `packed`, sorted by their name.
356348
///
357-
/// Errors are returned similarly to what would happen when loose and packed refs where iterated by themselves.
349+
/// Errors are returned similarly to what would happen when loose and packed refs were iterated by themselves.
358350
pub fn iter_packed<'s, 'p>(
359351
&'s self,
360352
packed: Option<&'p packed::Buffer>,
@@ -387,12 +379,12 @@ impl file::Store {
387379
}
388380
}
389381

390-
/// Return an iterator over all pseudo references, loose or `packed`, sorted by their name.
382+
/// Return an iterator over all pseudo references, sorted by their name.
391383
///
392-
/// Errors are returned similarly to what would happen when loose and packed refs where iterated by themselves.
393-
pub fn iter_pseudo_refs<'p>(&'_ self) -> std::io::Result<LooseThenPacked<'p, '_>> {
384+
/// Errors are returned similarly to what would happen when loose refs were iterated by themselves.
385+
pub fn iter_pseudo<'p>(&'_ self) -> std::io::Result<LooseThenPacked<'p, '_>> {
394386
self.iter_from_info(
395-
IterInfo::PseudoRefs {
387+
IterInfo::Pseudo {
396388
base: self.git_dir(),
397389
precompose_unicode: self.precompose_unicode,
398390
},

gix-ref/tests/fixtures/make_pref_repository.sh

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
set -eu -o pipefail
3+
4+
git init -q
5+
git commit -m "init" --allow-empty
6+
7+
git rev-parse HEAD > .git/JIRI_HEAD
8+
touch .git/SOME_ALL_CAPS_FILE
9+
touch .git/refs/SHOULD_BE_EXCLUDED_HEAD
10+
11+
cat <<EOF >> .git/FETCH_HEAD
12+
9064ea31fae4dc59a56bdd3a06c0ddc990ee689e branch 'main' of https://github.com/Byron/gitoxide
13+
1b8d9e6a408e480ae1912e919c37a26e5c46639d not-for-merge branch 'faster-discovery' of https://github.com/Byron/gitoxide
14+
EOF

gix-ref/tests/refs/file/store/iter.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use gix_object::bstr::ByteSlice;
2-
31
use crate::{
42
file::{store, store_at, store_with_packed_refs},
53
hex_to_id,
64
};
5+
use gix_object::bstr::ByteSlice;
76

87
mod with_namespace {
98
use gix_object::bstr::{BString, ByteSlice};
@@ -257,6 +256,20 @@ fn packed_file_iter() -> crate::Result {
257256
Ok(())
258257
}
259258

259+
#[test]
260+
fn pseudo_refs_iter() -> crate::Result {
261+
let store = store_at("make_pseudo_ref_repository.sh")?;
262+
263+
let actual = store
264+
.iter_pseudo()?
265+
.map(Result::unwrap)
266+
.map(|r: gix_ref::Reference| r.name.as_bstr().to_string())
267+
.collect::<Vec<_>>();
268+
269+
assert_eq!(actual, ["FETCH_HEAD", "HEAD", "JIRI_HEAD"]);
270+
Ok(())
271+
}
272+
260273
#[test]
261274
fn loose_iter_with_broken_refs() -> crate::Result {
262275
let store = store()?;

gix-ref/tests/refs/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ mod partialname {
3939
}
4040
mod namespace;
4141
mod packed;
42-
mod pseudo_refs;
4342
mod reference;
4443
mod store;
4544
mod transaction;

gix-ref/tests/refs/pseudo_refs.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

gix/src/reference/iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ impl Platform<'_> {
7272

7373
// TODO: tests
7474
/// Return an iterator over all local pseudo references.
75-
pub fn pseudo_refs(&self) -> Result<Iter<'_>, init::Error> {
76-
Ok(Iter::new(self.repo, self.platform.psuedo_refs()?))
75+
pub fn pseudo(&self) -> Result<Iter<'_>, init::Error> {
76+
Ok(Iter::new(self.repo, self.platform.pseudo_refs()?))
7777
}
7878

7979
// TODO: tests

0 commit comments

Comments
 (0)