-
-
Notifications
You must be signed in to change notification settings - Fork 357
feat: refs support pseudo refs #2061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
orthros
wants to merge
2
commits into
GitoxideLabs:main
Choose a base branch
from
orthros:pseudo-refs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use std::path::PathBuf; | ||
|
||
use gix_features::fs::walkdir::DirEntryIter; | ||
use gix_object::bstr::ByteSlice; | ||
|
||
use crate::{file::overlay_iter, BString, FullName, Reference}; | ||
|
||
/// An iterator over all pseudo references in a given git directory | ||
pub struct SortedPseudoRefPaths { | ||
pub(crate) git_dir: PathBuf, | ||
file_walk: Option<DirEntryIter>, | ||
} | ||
|
||
impl SortedPseudoRefPaths { | ||
/// Returns an iterator over the pseudo ref paths found at the given git_dir | ||
pub fn at(git_dir: PathBuf, precompose_unicode: bool) -> Self { | ||
SortedPseudoRefPaths { | ||
git_dir: git_dir.to_owned(), | ||
file_walk: git_dir.is_dir().then(|| { | ||
// serial iteration as we expect most refs in packed-refs anyway. | ||
gix_features::fs::walkdir_sorted_new( | ||
&git_dir, | ||
gix_features::fs::walkdir::Parallelism::Serial, | ||
// In a given git directory pseudo refs are only at the root | ||
1, | ||
precompose_unicode, | ||
) | ||
.into_iter() | ||
}), | ||
} | ||
} | ||
} | ||
|
||
impl Iterator for SortedPseudoRefPaths { | ||
type Item = std::io::Result<(PathBuf, FullName)>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
for entry in self.file_walk.as_mut()?.by_ref() { | ||
match entry { | ||
Ok(entry) => { | ||
if !entry.file_type().is_ok_and(|ft| ft.is_file()) { | ||
continue; | ||
} | ||
let full_path = entry.path().into_owned(); | ||
let full_name = full_path | ||
.strip_prefix(&self.git_dir) | ||
.expect("prefix-stripping cannot fail as base is within our root"); | ||
let Ok(full_name) = gix_path::try_into_bstr(full_name) | ||
.map(|name| gix_path::to_unix_separators_on_windows(name).into_owned()) | ||
else { | ||
continue; | ||
}; | ||
// Pseudo refs must end with "HEAD" | ||
if !full_name.ends_with(&BString::from("HEAD")) { | ||
continue; | ||
} | ||
if gix_validate::reference::name_partial(full_name.as_bstr()).is_ok() { | ||
let name = FullName(full_name); | ||
return Some(Ok((full_path, name))); | ||
} else { | ||
continue; | ||
} | ||
} | ||
Err(err) => return Some(Err(err.into_io_error().expect("no symlink related errors"))), | ||
} | ||
} | ||
None | ||
} | ||
} | ||
|
||
/// An iterator over all pseudo references in a given git directory | ||
pub struct SortedPseudoRefIterator { | ||
pub(crate) git_dir: PathBuf, | ||
inner: SortedPseudoRefPaths, | ||
buf: Vec<u8>, | ||
} | ||
|
||
impl SortedPseudoRefIterator { | ||
/// Returns an iterator over the pseudo ref paths found at the given git_dir | ||
pub fn at(git_dir: &PathBuf, precompose_unicode: bool) -> Self { | ||
SortedPseudoRefIterator { | ||
inner: SortedPseudoRefPaths::at(git_dir.to_owned(), precompose_unicode), | ||
git_dir: git_dir.to_owned(), | ||
buf: vec![], | ||
} | ||
} | ||
} | ||
|
||
impl Iterator for SortedPseudoRefIterator { | ||
type Item = Result<Reference, overlay_iter::Error>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.inner | ||
.next() | ||
.map(|r| overlay_iter::convert_loose(&mut self.buf, &self.git_dir, None, None, r)) | ||
} | ||
} |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env bash | ||
set -eu -o pipefail | ||
|
||
git init -q | ||
|
||
git checkout -q -b main | ||
git commit -q --allow-empty -m c1 | ||
git branch dt1 | ||
git branch d1 | ||
git branch A | ||
|
||
mkdir -p .git/refs/remotes/origin | ||
mkdir -p .git/refs/prefix/feature/sub/dir | ||
|
||
cp .git/refs/heads/main .git/refs/remotes/origin/ | ||
cp .git/refs/heads/main .git/refs/d1 | ||
cp .git/refs/heads/main .git/refs/prefix/feature-suffix | ||
cp .git/refs/heads/main .git/refs/prefix/feature/sub/dir/algo | ||
|
||
echo "ref: refs/remotes/origin/main" > .git/refs/remotes/origin/HEAD | ||
echo "notahexsha" > .git/refs/broken | ||
|
||
git rev-parse HEAD > .git/JIRI_HEAD | ||
touch .git/SOME_ALL_CAPS_FILE | ||
touch .git/refs/SHOULD_BE_EXCLUDED_HEAD | ||
|
||
cat <<EOF >> .git/FETCH_HEAD | ||
9064ea31fae4dc59a56bdd3a06c0ddc990ee689e branch 'main' of https://github.com/Byron/gitoxide | ||
1b8d9e6a408e480ae1912e919c37a26e5c46639d not-for-merge branch 'faster-discovery' of https://github.com/Byron/gitoxide | ||
43f695a9607f1f85f859f2ef944b785b5b6dd238 not-for-merge branch 'fix-823' of https://github.com/Byron/gitoxide | ||
96267708958ead2646aae8766a50fa060739003c not-for-merge branch 'fix-bare-with-index' of https://github.com/Byron/gitoxide | ||
1397e19375bb98522f951b8a452b08c1b35ffbac not-for-merge branch 'gix-archive' of https://github.com/Byron/gitoxide | ||
db71ec8b7c7f2730c47dde3bb662ab56ae89ae7d not-for-merge branch 'index-from-files' of https://github.com/Byron/gitoxide | ||
9f0c71917e57653d2e7121eae65d9385a188a8df not-for-merge branch 'moonwalk' of https://github.com/Byron/gitoxide | ||
44d2b67de5639d4ea3d08ab030ecfe4bdfc8cbfb not-for-merge branch 'release-gix' of https://github.com/Byron/gitoxide | ||
37c3d073b15dafcb52b2040e4b92a413c69a726d not-for-merge branch 'smart-release-without-git2' of https://github.com/Byron/gitoxide | ||
af3608ad397784795c3758a1ac99ec6a367de9be not-for-merge branch 'walk-with-commitgraph' of https://github.com/Byron/gitoxide | ||
EOF | ||
|
||
git tag t1 | ||
git tag -m "tag object" dt1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ mod partialname { | |
} | ||
mod namespace; | ||
mod packed; | ||
mod pseudo_refs; | ||
mod reference; | ||
mod store; | ||
mod transaction; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use crate::file::store_at; | ||
|
||
#[test] | ||
fn pseudo_refs_iterate_valid_pseudorefs() -> crate::Result { | ||
let store = store_at("make_pref_repository.sh")?; | ||
|
||
let prefs = store | ||
.iter_pseudo_refs()? | ||
.map(Result::unwrap) | ||
.map(|r: gix_ref::Reference| r.name) | ||
.collect::<Vec<_>>(); | ||
|
||
let expected_prefs = vec!["FETCH_HEAD", "HEAD", "JIRI_HEAD"]; | ||
|
||
assert_eq!( | ||
prefs.iter().map(gix_ref::FullName::as_bstr).collect::<Vec<_>>(), | ||
expected_prefs | ||
); | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,7 @@ fn common_values_and_names_by_path() -> crate::Result { | |
|
||
fn module_files() -> impl Iterator<Item = (PathBuf, PathBuf)> { | ||
let dir = gix_testtools::scripted_fixture_read_only("basic.sh").expect("valid fixture"); | ||
gix_features::fs::walkdir_sorted_new(&dir, Parallelism::Serial, false) | ||
gix_features::fs::walkdir_sorted_new(&dir, Parallelism::Serial, usize::MAX, false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should go into a separate commit right after the one changing |
||
.follow_links(false) | ||
.into_iter() | ||
.filter_map(move |entry| { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.