Skip to content

Commit

Permalink
feat: ref iteration for worktrees. (#301)
Browse files Browse the repository at this point in the history
It merges the iteration result of private worktree refs along with
all shared common references references.
  • Loading branch information
Byron committed May 18, 2022
1 parent 7571be5 commit 4a5176a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
12 changes: 8 additions & 4 deletions git-ref/src/store/file/overlay_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct LooseThenPacked<'p, 's> {

enum Kind {
GitDir,
GitDirConsumeCommonDir,
CommonDir,
}

Expand All @@ -51,6 +52,10 @@ impl<'p, 's> LooseThenPacked<'p, 's> {

fn loose_iter(&mut self, kind: Kind) -> &mut Peekable<SortedLoosePaths> {
match kind {
Kind::GitDirConsumeCommonDir => {
drop(self.iter_common_dir.as_mut().map(|iter| iter.next()));
&mut self.iter_git_dir
}
Kind::GitDir => &mut self.iter_git_dir,
Kind::CommonDir => self
.iter_common_dir
Expand Down Expand Up @@ -117,6 +122,8 @@ impl<'p, 's> Iterator for LooseThenPacked<'p, 's> {
while let Some(Ok((_path, name))) = iter.peek() {
if name.category().map_or(true, |cat| cat.is_worktree_private()) {
iter.next();
} else {
break;
}
}
}
Expand All @@ -136,10 +143,7 @@ impl<'p, 's> Iterator for LooseThenPacked<'p, 's> {
(Some(r_gitdir @ Ok((_, git_dir_name))), Some(r_cd @ Ok((_, common_dir_name)))) => {
match git_dir_name.cmp(&common_dir_name) {
Ordering::Less => Some((r_gitdir, Kind::GitDir)),
Ordering::Equal => {
drop(common_dir.next());
Some((r_gitdir, Kind::GitDir))
}
Ordering::Equal => Some((r_gitdir, Kind::GitDirConsumeCommonDir)),
Ordering::Greater => Some((r_cd, Kind::CommonDir)),
}
}
Expand Down
30 changes: 28 additions & 2 deletions git-ref/tests/file/worktree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,10 @@ mod writable {

#[test]
fn linked() -> crate::Result {
let new_id = hex_to_id("134385f6d781b7e97062102c6a483440bfda2a03");
let new_id_main = hex_to_id("22222222222222227062102c6a483440bfda2a03");
let new_id_str = "134385f6d781b7e97062102c6a483440bfda2a03";
let new_id = hex_to_id(new_id_str);
let new_id_main_str = "22222222222222227062102c6a483440bfda2a03";
let new_id_main = hex_to_id(new_id_main_str);
for packed in [false, true] {
let (store, _odb, _tmp) = worktree_store(packed, "w1", Mode::Write)?;

Expand Down Expand Up @@ -508,6 +510,30 @@ mod writable {
.commit(committer().to_ref())
.expect("successful commit as even similar resolved names live in different base locations");

assert_eq!(
store
.iter()?
.all()?
.map(Result::unwrap)
.map(|r| (r.name.to_string(), r.target.to_string()))
.collect::<Vec<_>>(),
[
("refs/bisect/bad", "9902e3c3e8f0c569b4ab295ddf473e6de763e1e7"),
("refs/bisect/good", new_id_str),
("refs/heads/main", "9556057aee5abb06912922e9f26c46386a816822"),
("refs/heads/new", new_id_main_str),
("refs/heads/shared", new_id_str),
("refs/heads/w1", "9902e3c3e8f0c569b4ab295ddf473e6de763e1e7"),
("refs/tags/dt1", "d3ba65e5e3be5cdd7210da9998307a4762999cc5"),
("refs/tags/t1", "9556057aee5abb06912922e9f26c46386a816822"),
("refs/worktree/private", new_id_str)
]
.iter()
.map(|(a, b)| (a.to_string(), b.to_string()))
.collect::<Vec<_>>(),
"we traverse only refs of the main worktree"
);

let mut buf = Vec::new();

{
Expand Down

0 comments on commit 4a5176a

Please sign in to comment.