vcs menu: Use project's repositories, do not open directly (#11652)

I ran into this when trying to get #11550 working: the VCS menu would
open repositories on its owned, based on paths, instead of going through
the worktree on which we already store the git repositories.



Release Notes:

- N/A
This commit is contained in:
Thorsten Ball 2024-05-10 11:06:32 +02:00 committed by GitHub
parent b3dc31d7c9
commit df190ea846
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 37 deletions

View file

@ -7901,6 +7901,16 @@ impl Project {
.local_git_repo(&project_path.path) .local_git_repo(&project_path.path)
} }
pub fn get_first_worktree_root_repo(
&self,
cx: &AppContext,
) -> Option<Arc<Mutex<dyn GitRepository>>> {
let worktree = self.visible_worktrees(cx).next()?.read(cx).as_local()?;
let root_entry = worktree.root_git_entry()?;
worktree.get_local_repo(&root_entry)?.repo().clone().into()
}
pub fn blame_buffer( pub fn blame_buffer(
&self, &self,
buffer: &Model<Buffer>, buffer: &Model<Buffer>,

View file

@ -1,4 +1,4 @@
use anyhow::{anyhow, bail, Result}; use anyhow::{Context, Result};
use fuzzy::{StringMatch, StringMatchCandidate}; use fuzzy::{StringMatch, StringMatchCandidate};
use git::repository::Branch; use git::repository::Branch;
use gpui::{ use gpui::{
@ -105,15 +105,10 @@ impl BranchListDelegate {
cx: &AppContext, cx: &AppContext,
) -> Result<Self> { ) -> Result<Self> {
let project = workspace.project().read(&cx); let project = workspace.project().read(&cx);
let Some(worktree) = project.visible_worktrees(cx).next() else { let repo = project
bail!("Cannot update branch list as there are no visible worktrees") .get_first_worktree_root_repo(cx)
}; .context("failed to get root repository for first worktree")?;
let mut cwd = worktree.read(cx).abs_path().to_path_buf();
cwd.push(".git");
let Some(repo) = project.fs().open_repo(&cwd) else {
bail!("Project does not have associated git repository.")
};
let all_branches = repo.lock().branches()?; let all_branches = repo.lock().branches()?;
Ok(Self { Ok(Self {
matches: vec![], matches: vec![],
@ -238,23 +233,10 @@ impl PickerDelegate for BranchListDelegate {
picker picker
.update(&mut cx, |this, cx| { .update(&mut cx, |this, cx| {
let project = this.delegate.workspace.read(cx).project().read(cx); let project = this.delegate.workspace.read(cx).project().read(cx);
let mut cwd = project let repo = project
.visible_worktrees(cx) .get_first_worktree_root_repo(cx)
.next() .context("failed to get root repository for first worktree")?;
.ok_or_else(|| anyhow!("There are no visisible worktrees."))? let status = repo
.read(cx)
.abs_path()
.to_path_buf();
cwd.push(".git");
let status = project
.fs()
.open_repo(&cwd)
.ok_or_else(|| {
anyhow!(
"Could not open repository at path `{}`",
cwd.as_os_str().to_string_lossy()
)
})?
.lock() .lock()
.change_branch(&current_pick); .change_branch(&current_pick);
if status.is_err() { if status.is_err() {
@ -331,18 +313,9 @@ impl PickerDelegate for BranchListDelegate {
picker.update(&mut cx, |this, cx| { picker.update(&mut cx, |this, cx| {
let project = this.delegate.workspace.read(cx).project().read(cx); let project = this.delegate.workspace.read(cx).project().read(cx);
let current_pick = &this.delegate.last_query; let current_pick = &this.delegate.last_query;
let mut cwd = project
.visible_worktrees(cx)
.next()
.ok_or_else(|| anyhow!("There are no visisible worktrees."))?
.read(cx)
.abs_path()
.to_path_buf();
cwd.push(".git");
let repo = project let repo = project
.fs() .get_first_worktree_root_repo(cx)
.open_repo(&cwd) .context("failed to get root repository for first worktree")?;
.ok_or_else(|| anyhow!("Could not open repository at path `{}`", cwd.as_os_str().to_string_lossy()))?;
let repo = repo let repo = repo
.lock(); .lock();
let status = repo let status = repo