Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions git-branchless-lib/src/git/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,20 +577,7 @@ impl Repo {
return Ok(None);
}

// `git2` doesn't seem to support a way to directly look up the parent repository for the
// worktree.
let worktree_info_dir = self.get_path();
let parent_repo_path = match worktree_info_dir
.parent() // remove `.git`
.and_then(|path| path.parent()) // remove worktree name
.and_then(|path| path.parent()) // remove `worktrees`
{
Some(path) => path,
None => {
return Err(Error::OpenParentWorktreeRepository {
path: worktree_info_dir.to_owned()});
},
};
let parent_repo_path = self.inner.commondir();
let parent_repo = Self::from_dir(parent_repo_path)?;
Ok(Some(parent_repo))
}
Expand Down
47 changes: 46 additions & 1 deletion git-branchless-lib/tests/test_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use branchless::git::{
AmendFastOptions, BranchType, CherryPickFastOptions, FileMode, FileStatus, GitVersion, Repo,
StatusEntry,
};
use branchless::testing::{GitWorktreeWrapper, make_git, make_git_worktree};
use branchless::testing::{
GitWorktreeWrapper, GitWrapperWithRemoteRepo, make_git, make_git_with_remote_repo,
make_git_worktree,
};

#[test]
fn test_parse_git_version_output() {
Expand Down Expand Up @@ -303,3 +306,45 @@ fn test_worktree_working_copy_path() -> eyre::Result<()> {

Ok(())
}

#[test]
fn test_open_worktree_parent_repo_bare_clone() -> eyre::Result<()> {
let GitWrapperWithRemoteRepo {
temp_dir: _guard_original,
original_repo,
cloned_repo,
} = make_git_with_remote_repo()?;

// Create a bare clone of the original repo.
{
original_repo.init_repo()?;

// `git.clone_repo_into` reinitializes the destination after cloning,
// adding a `.git` directory and losing the bare state. Run `git clone
// --bare` directly instead.
original_repo.run(&[
"clone",
"--bare",
original_repo.repo_path.to_str().unwrap(),
cloned_repo.repo_path.to_str().unwrap(),
])?;
}

// Create a linked worktree from the bare clone.
let GitWorktreeWrapper {
temp_dir: _guard_worktree,
worktree,
} = make_git_worktree(&cloned_repo, "new-worktree")?;

let parent_repo = worktree
.get_repo()?
.open_worktree_parent_repo()?
.expect("expected to find parent repo for worktree of bare clone");

assert_eq!(
std::fs::canonicalize(parent_repo.get_path())?,
std::fs::canonicalize(cloned_repo.repo_path)?,
);

Ok(())
}
Loading