Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

git: Fix a bug where BranchDiff shows incorrect diffs (#46433)

Anthony Eid5f92b31a8a1c
diff --git a/crates/fs/src/fake_git_repo.rs b/crates/fs/src/fake_git_repo.rs
index a83e8197d26..347419ef9a8 100644
--- a/crates/fs/src/fake_git_repo.rs
+++ b/crates/fs/src/fake_git_repo.rs
@@ -720,8 +720,18 @@ impl GitRepository for FakeGitRepository {
unimplemented!()
}
- fn default_branch(&self) -> BoxFuture<'_, Result<Option<SharedString>>> {
- async { Ok(Some("main".into())) }.boxed()
+ fn default_branch(
+ &self,
+ include_remote_name: bool,
+ ) -> BoxFuture<'_, Result<Option<SharedString>>> {
+ async move {
+ Ok(Some(if include_remote_name {
+ "origin/main".into()
+ } else {
+ "main".into()
+ }))
+ }
+ .boxed()
}
fn create_remote(&self, name: String, url: String) -> BoxFuture<'_, Result<()>> {
diff --git a/crates/git/src/repository.rs b/crates/git/src/repository.rs
index 6889acd8700..04f345ab9e7 100644
--- a/crates/git/src/repository.rs
+++ b/crates/git/src/repository.rs
@@ -649,7 +649,10 @@ pub trait GitRepository: Send + Sync {
target_checkpoint: GitRepositoryCheckpoint,
) -> BoxFuture<'_, Result<String>>;
- fn default_branch(&self) -> BoxFuture<'_, Result<Option<SharedString>>>;
+ fn default_branch(
+ &self,
+ include_remote_name: bool,
+ ) -> BoxFuture<'_, Result<Option<SharedString>>>;
}
pub enum DiffType {
@@ -2305,7 +2308,10 @@ impl GitRepository for RealGitRepository {
.boxed()
}
- fn default_branch(&self) -> BoxFuture<'_, Result<Option<SharedString>>> {
+ fn default_branch(
+ &self,
+ include_remote_name: bool,
+ ) -> BoxFuture<'_, Result<Option<SharedString>>> {
let working_directory = self.working_directory();
let git_binary_path = self.any_git_binary_path.clone();
@@ -2315,19 +2321,31 @@ impl GitRepository for RealGitRepository {
let working_directory = working_directory?;
let git = GitBinary::new(git_binary_path, working_directory, executor);
+ let strip_prefix = if include_remote_name {
+ "refs/remotes/"
+ } else {
+ "refs/remotes/upstream/"
+ };
+
if let Ok(output) = git
.run(&["symbolic-ref", "refs/remotes/upstream/HEAD"])
.await
{
let output = output
- .strip_prefix("refs/remotes/upstream/")
+ .strip_prefix(strip_prefix)
.map(|s| SharedString::from(s.to_owned()));
return Ok(output);
}
+ let strip_prefix = if include_remote_name {
+ "refs/remotes/"
+ } else {
+ "refs/remotes/origin/"
+ };
+
if let Ok(output) = git.run(&["symbolic-ref", "refs/remotes/origin/HEAD"]).await {
return Ok(output
- .strip_prefix("refs/remotes/origin/")
+ .strip_prefix(strip_prefix)
.map(|s| SharedString::from(s.to_owned())));
}
diff --git a/crates/git_ui/src/branch_picker.rs b/crates/git_ui/src/branch_picker.rs
index 7d8e63081ba..befcf83d1f9 100644
--- a/crates/git_ui/src/branch_picker.rs
+++ b/crates/git_ui/src/branch_picker.rs
@@ -128,9 +128,9 @@ impl BranchList {
let all_branches_request = repository
.clone()
.map(|repository| repository.update(cx, |repository, _| repository.branches()));
- let default_branch_request = repository
- .clone()
- .map(|repository| repository.update(cx, |repository, _| repository.default_branch()));
+ let default_branch_request = repository.clone().map(|repository| {
+ repository.update(cx, |repository, _| repository.default_branch(false))
+ });
cx.spawn_in(window, async move |this, cx| {
let mut all_branches = all_branches_request
diff --git a/crates/git_ui/src/project_diff.rs b/crates/git_ui/src/project_diff.rs
index 2207b8c599a..ec06f6776cf 100644
--- a/crates/git_ui/src/project_diff.rs
+++ b/crates/git_ui/src/project_diff.rs
@@ -199,7 +199,7 @@ impl ProjectDiff {
let Some(repo) = project.read(cx).git_store().read(cx).active_repository() else {
return Task::ready(Err(anyhow!("No active repository")));
};
- let main_branch = repo.update(cx, |repo, _| repo.default_branch());
+ let main_branch = repo.update(cx, |repo, _| repo.default_branch(true));
window.spawn(cx, async move |cx| {
let main_branch = main_branch
.await??
diff --git a/crates/git_ui/src/worktree_picker.rs b/crates/git_ui/src/worktree_picker.rs
index 6e58de1ae14..3d71ff4abc2 100644
--- a/crates/git_ui/src/worktree_picker.rs
+++ b/crates/git_ui/src/worktree_picker.rs
@@ -60,9 +60,9 @@ impl WorktreeList {
.clone()
.map(|repository| repository.update(cx, |repository, _| repository.worktrees()));
- let default_branch_request = repository
- .clone()
- .map(|repository| repository.update(cx, |repository, _| repository.default_branch()));
+ let default_branch_request = repository.clone().map(|repository| {
+ repository.update(cx, |repository, _| repository.default_branch(false))
+ });
cx.spawn_in(window, async move |this, cx| {
let all_worktrees = all_worktrees_request
diff --git a/crates/project/src/git_store.rs b/crates/project/src/git_store.rs
index c0b5862b0b1..b1ee419a0d8 100644
--- a/crates/project/src/git_store.rs
+++ b/crates/project/src/git_store.rs
@@ -2229,7 +2229,7 @@ impl GitStore {
let branch = repository_handle
.update(&mut cx, |repository_handle, _| {
- repository_handle.default_branch()
+ repository_handle.default_branch(false)
})
.await??
.map(Into::into);
@@ -5189,12 +5189,15 @@ impl Repository {
)
}
- pub fn default_branch(&mut self) -> oneshot::Receiver<Result<Option<SharedString>>> {
+ pub fn default_branch(
+ &mut self,
+ include_remote_name: bool,
+ ) -> oneshot::Receiver<Result<Option<SharedString>>> {
let id = self.id;
self.send_job(None, move |repo, _| async move {
match repo {
RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
- backend.default_branch().await
+ backend.default_branch(include_remote_name).await
}
RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
let response = client