Skip to content

Commit

Permalink
doc: make really clear that Repository::worktrees() lists linked wo…
Browse files Browse the repository at this point in the history
…rktrees.

Excluding the main worktree which isn't always present.
  • Loading branch information
Byron committed Jan 12, 2025
1 parent bc02284 commit 9db2160
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
9 changes: 7 additions & 2 deletions gix/src/repository/worktree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ use crate::{worktree, Worktree};

/// Interact with individual worktrees and their information.
impl crate::Repository {
/// Return a list of all _linked_ worktrees sorted by private git dir path as a lightweight proxy.
/// Return a list of all **linked** worktrees sorted by private git dir path as a lightweight proxy.
///
/// This means the number is `0` even if there is the main worktree, as it is not counted as linked worktree.
/// This also means it will be `1` if there is one linked worktree next to the main worktree.
/// It's worth noting that a *bare* repository may have one or more linked worktrees, but has no *main* worktree,
/// which is the reason why the *possibly* available main worktree isn't listed here.
///
/// Note that these need additional processing to become usable, but provide a first glimpse a typical worktree information.
pub fn worktrees(&self) -> std::io::Result<Vec<worktree::Proxy<'_>>> {
let mut res = Vec::new();
let iter = match std::fs::read_dir(self.common_dir().join("worktrees")) {
let iter = match std::fs::read_dir(dbg!(self.common_dir()).join("worktrees")) {
Ok(iter) => iter,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(res),
Err(err) => return Err(err),
Expand Down
23 changes: 23 additions & 0 deletions gix/tests/gix/submodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,29 @@ mod open {
wd.join("this").is_file(),
"The submodule itself has the file, so it should be in the worktree"
);

assert_eq!(sm_repo.worktrees()?.len(), 1, "only a single linked worktree");
Ok(())
}

#[test]
fn list_submodule_worktrees() -> crate::Result {
let sm_repo = named_subrepo_opts(
"make_submodule_with_worktree.sh",
"submodule-with-extra-worktree-host/m1",
gix::open::Options::isolated(),
)?;
let wd = sm_repo.work_dir().expect("workdir is present");
assert!(
sm_repo.rev_parse_single(":this").is_ok(),
"the file is in the submodule"
);
assert!(
wd.join("this").is_file(),
"The submodule itself has the file, so it should be in the worktree"
);

assert_eq!(sm_repo.worktrees()?.len(), 1, "only a single linked worktree");
Ok(())
}

Expand Down

0 comments on commit 9db2160

Please sign in to comment.