Skip to content

Commit 2f14246

Browse files
authored
Merge pull request #2252 from GitoxideLabs/improvements
improvements
2 parents c3beb20 + 70fcdb7 commit 2f14246

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

gix-macros/src/momo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,13 @@ fn convert(
184184
let pat_ident = match &mut *pat_type.pat {
185185
Pat::Ident(pat_ident) if pat_ident.by_ref.is_none() && pat_ident.subpat.is_none() => pat_ident,
186186
_ => {
187-
pat_type.pat = Box::new(Pat::Ident(PatIdent {
187+
*pat_type.pat = Pat::Ident(PatIdent {
188188
ident: Ident::new(&format!("arg_{i}_gen_by_momo_"), proc_macro2::Span::call_site()),
189189
attrs: Default::default(),
190190
by_ref: None,
191191
mutability: None,
192192
subpat: None,
193-
}));
193+
});
194194

195195
if let Pat::Ident(pat_ident) = &mut *pat_type.pat {
196196
pat_ident

gix/src/repository/worktree.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::bstr::BStr;
12
use crate::{worktree, Worktree};
23

34
/// Interact with individual worktrees and their information.
@@ -20,17 +21,22 @@ impl crate::Repository {
2021
for entry in iter {
2122
let entry = entry?;
2223
let worktree_git_dir = entry.path();
23-
if worktree_git_dir.join("gitdir").is_file() {
24-
res.push(worktree::Proxy {
25-
parent: self,
26-
git_dir: worktree_git_dir,
27-
});
28-
}
24+
res.extend(worktree::Proxy::new_if_gitdir_file_exists(self, worktree_git_dir));
2925
}
3026
res.sort_by(|a, b| a.git_dir.cmp(&b.git_dir));
3127
Ok(res)
3228
}
3329

30+
/// Return the worktree that [is identified](Worktree::id) by the given `id`, if it exists at
31+
/// `.git/worktrees/<id>` and its `gitdir` file exists.
32+
/// Return `None` otherwise.
33+
pub fn worktree_proxy_by_id<'a>(&self, id: impl Into<&'a BStr>) -> Option<worktree::Proxy<'_>> {
34+
worktree::Proxy::new_if_gitdir_file_exists(
35+
self,
36+
self.common_dir().join("worktrees").join(gix_path::from_bstr(id.into())),
37+
)
38+
}
39+
3440
/// Return the repository owning the main worktree, typically from a linked worktree.
3541
///
3642
/// Note that it might be the one that is currently open if this repository doesn't point to a linked worktree.

gix/src/worktree/proxy.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ impl<'repo> Proxy<'repo> {
3131
git_dir: git_dir.into(),
3232
}
3333
}
34+
35+
pub(crate) fn new_if_gitdir_file_exists(parent: &'repo Repository, git_dir: impl Into<PathBuf>) -> Option<Self> {
36+
let git_dir = git_dir.into();
37+
if git_dir.join("gitdir").is_file() {
38+
Some(Proxy::new(parent, git_dir))
39+
} else {
40+
None
41+
}
42+
}
3443
}
3544

3645
impl Proxy<'_> {

gix/tests/gix/repository/worktree.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,14 @@ fn run_assertions(main_repo: gix::Repository, should_be_bare: bool) {
350350
"in our case prunable repos have no worktree base"
351351
);
352352

353+
assert_eq!(
354+
main_repo.worktree_proxy_by_id(actual.id()).expect("exists").git_dir(),
355+
actual.git_dir(),
356+
"we can basically get the same proxy by its ID explicitly"
357+
);
358+
353359
let repo = if base.is_dir() {
354-
let repo = actual.into_repo().unwrap();
360+
let repo = actual.clone().into_repo().unwrap();
355361
assert_eq!(
356362
&gix::open(base).unwrap(),
357363
&repo,
@@ -366,7 +372,7 @@ fn run_assertions(main_repo: gix::Repository, should_be_bare: bool) {
366372
),
367373
"missing bases are detected"
368374
);
369-
actual.into_repo_with_possibly_inaccessible_worktree().unwrap()
375+
actual.clone().into_repo_with_possibly_inaccessible_worktree().unwrap()
370376
};
371377
let worktree = repo.worktree().expect("linked worktrees have at least a base path");
372378
assert!(!worktree.is_main());
@@ -378,5 +384,19 @@ fn run_assertions(main_repo: gix::Repository, should_be_bare: bool) {
378384
main_repo,
379385
"main repo from worktree repo is the actual main repo"
380386
);
387+
388+
let proxy_by_id = repo
389+
.worktree_proxy_by_id(actual.id())
390+
.expect("can get the proxy from a linked repo as well");
391+
assert_ne!(
392+
proxy_by_id.git_dir(),
393+
actual.git_dir(),
394+
"The git directories might not look the same…"
395+
);
396+
assert_eq!(
397+
gix_path::realpath(proxy_by_id.git_dir()).ok(),
398+
gix_path::realpath(actual.git_dir()).ok(),
399+
"…but they are the same effectively"
400+
);
381401
}
382402
}

0 commit comments

Comments
 (0)