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.

Make Git panel file actions work with multiple selections (#64640)

Zhaokang Jiang651d693d4298
diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rs
index 4820ab07f93..f04282446f2 100644
--- a/crates/git_ui/src/git_panel.rs
+++ b/crates/git_ui/src/git_panel.rs
@@ -39,8 +39,8 @@ use git::repository::{
use git::stash::GitStash;
use git::status::{DiffStat, StageStatus};
use git::{
- AddToGitInfoExclude, AddToGitignore, Amend, Commit, RestoreFile, Signoff, SkipHooks,
- ToggleStaged, repository::RepoPath, status::FileStatus,
+ Amend, Commit, RestoreFile, Signoff, SkipHooks, ToggleStaged, repository::RepoPath,
+ status::FileStatus,
};
use git::{
ExpandCommitEditor, GitHostingProviderRegistry, GitRemote, RestoreTrackedFiles, StageAll,
@@ -1610,10 +1610,12 @@ impl GitPanel {
fn marked_file_entries(&self) -> Vec<GitStatusEntry> {
let mut coverage = MarkedDirectoryCoverage::default();
let mut marked = Vec::new();
+ let mut seen_paths = HashSet::default();
for entry in &self.entries {
let covered = coverage.observe(entry, &self.marked_directories);
if let Some(status_entry) = entry.status_entry()
&& (covered || self.marked_entries.contains(&status_entry.repo_path))
+ && seen_paths.insert(status_entry.repo_path.clone())
{
marked.push(status_entry.clone());
}
@@ -1753,6 +1755,40 @@ impl GitPanel {
.collect()
}
+ fn effective_repo_paths(&self) -> Vec<RepoPath> {
+ let selected_index = self.selected_entry;
+ let selected_path = self.get_selected_entry().and_then(GitListEntry::repo_path);
+ if let Some(path) = selected_path {
+ let selected_is_marked = selected_index
+ .and_then(|index| self.row_mark(index))
+ .is_some_and(|mark| match mark {
+ RowMark::File(path) => self.marked_entries.contains(&path),
+ RowMark::Directory(key) => self.marked_directories.contains(&key),
+ });
+ let mark_count = self.marked_entries.len() + self.marked_directories.len();
+ if mark_count == 0 || mark_count == 1 && !selected_is_marked {
+ return vec![path.clone()];
+ }
+ }
+
+ let mut paths = Vec::new();
+ let mut seen_paths = HashSet::default();
+ for (index, entry) in self.entries.iter().enumerate() {
+ let marked = match self.row_mark(index) {
+ Some(RowMark::File(path)) => self.marked_entries.contains(&path),
+ Some(RowMark::Directory(key)) => self.marked_directories.contains(&key),
+ None => false,
+ };
+ if marked
+ && let Some(path) = entry.repo_path()
+ && seen_paths.insert(path.clone())
+ {
+ paths.push(path.clone());
+ }
+ }
+ paths
+ }
+
fn cancel(&mut self, _: &menu::Cancel, window: &mut Window, cx: &mut Context<Self>) {
if self.active_tab == GitPanelTab::Changes && self.has_marks() {
self.clear_marks();
@@ -2492,10 +2528,11 @@ impl GitPanel {
self.open_selected_history_commit(window, cx);
return;
}
- if let Some(GitListEntry::Directory(dir_entry)) = self
- .selected_entry
- .and_then(|i| self.entries.get(i))
- .cloned()
+ if self.selection_target_kind() == Some(SelectionTargetKind::Directory)
+ && let Some(GitListEntry::Directory(dir_entry)) = self
+ .selected_entry
+ .and_then(|i| self.entries.get(i))
+ .cloned()
{
self.toggle_directory(&dir_entry.key, window, cx);
return;
@@ -2547,66 +2584,118 @@ impl GitPanel {
window: &mut Window,
cx: &mut Context<Self>,
) {
- maybe!({
- let entry = self
- .entries
- .get(self.selected_entry?)?
- .status_entry()?
- .clone();
- let repository = self.active_repository.clone()?;
-
- SoloDiffView::open_or_focus(entry, repository, self.workspace.clone(), window, cx)
- .detach_and_notify_err(self.workspace.clone(), window, cx);
-
- Some(())
- });
+ let Some(repository) = self.active_repository.clone() else {
+ return;
+ };
+ for entry in self.effective_status_entries() {
+ SoloDiffView::open_or_focus(
+ entry,
+ repository.clone(),
+ self.workspace.clone(),
+ window,
+ cx,
+ )
+ .detach_and_notify_err(self.workspace.clone(), window, cx);
+ }
}
fn view_file(&mut self, _: &ViewFile, window: &mut Window, cx: &mut Context<Self>) {
- maybe!({
- let entry = self.entries.get(self.selected_entry?)?.status_entry()?;
- let project_path = self
- .active_repository
- .as_ref()?
- .read(cx)
- .repo_path_to_project_path(&entry.repo_path, cx)?;
+ let Some(repository) = self.active_repository.as_ref() else {
+ return;
+ };
+ let project_paths = self
+ .effective_status_entries()
+ .iter()
+ .filter_map(|entry| {
+ repository
+ .read(cx)
+ .repo_path_to_project_path(&entry.repo_path, cx)
+ })
+ .collect::<Vec<_>>();
+ for project_path in project_paths {
self.workspace
.update(cx, |workspace, cx| {
workspace
.open_path_preview(project_path, None, false, false, true, window, cx)
- .detach_and_log_err(cx);
+ .detach_and_notify_err(self.workspace.clone(), window, cx);
})
- .ok()?;
+ .log_err();
+ }
+ }
- Some(())
- });
+ fn view_file_history(
+ &mut self,
+ _: &git::FileHistory,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ let entries = self.effective_status_entries();
+ if entries.len() <= 1 {
+ cx.propagate();
+ return;
+ }
+ let Some(repository) = self.active_repository.as_ref() else {
+ return;
+ };
+ let repository_id = repository.read(cx).id;
+ let git_store = self.project.read(cx).git_store().clone();
+ self.workspace
+ .update(cx, |workspace, cx| {
+ for entry in entries {
+ if !entry.status.is_created() {
+ crate::git_graph::open_or_reuse_graph(
+ workspace,
+ repository_id,
+ git_store.clone(),
+ LogSource::Path(entry.repo_path),
+ None,
+ window,
+ cx,
+ );
+ }
+ }
+ })
+ .log_err();
+ cx.stop_propagation();
}
fn copy_path(&mut self, _: &CopyPath, _: &mut Window, cx: &mut Context<Self>) {
- if let Some((repo_path, repo)) = self
- .get_selected_entry()
- .and_then(GitListEntry::repo_path)
- .zip(self.active_repository.as_ref())
- {
- let path = repo.read(cx).repo_path_to_abs_path(repo_path);
- cx.write_to_clipboard(ClipboardItem::new_string(
- path.to_string_lossy().into_owned(),
- ));
- } else {
+ let Some(repo) = self.active_repository.as_ref() else {
cx.propagate();
+ return;
+ };
+ let paths = self.effective_repo_paths();
+ if paths.is_empty() {
+ cx.propagate();
+ return;
}
+ let repo = repo.read(cx);
+ cx.write_to_clipboard(ClipboardItem::new_string(
+ paths
+ .into_iter()
+ .map(|path| {
+ repo.repo_path_to_abs_path(&path)
+ .to_string_lossy()
+ .into_owned()
+ })
+ .join("\n"),
+ ));
}
fn copy_relative_path(&mut self, _: &CopyRelativePath, _: &mut Window, cx: &mut Context<Self>) {
- if let Some(repo_path) = self.get_selected_entry().and_then(GitListEntry::repo_path) {
- let path_style = self.project.read(cx).path_style(cx);
- cx.write_to_clipboard(ClipboardItem::new_string(
- repo_path.display(path_style).into_owned(),
- ));
- } else {
+ let paths = self.effective_repo_paths();
+ if paths.is_empty() {
cx.propagate();
+ return;
}
+ let path_style = self.project.read(cx).path_style(cx);
+ cx.write_to_clipboard(ClipboardItem::new_string(
+ paths
+ .into_iter()
+ .map(|path| path.display(path_style).into_owned())
+ .join("\n"),
+ ));
}
fn open_selected_entry_on_click(
@@ -2673,35 +2762,57 @@ impl GitPanel {
}
}
+ fn selected_ignorable_paths(&self) -> Vec<(RepoPath, bool)> {
+ if self.selection_target_kind() == Some(SelectionTargetKind::Multiple) {
+ let entries = self.effective_status_entries();
+ if entries.iter().any(|entry| !entry.status.is_created()) {
+ return Vec::new();
+ }
+ entries
+ .into_iter()
+ .map(|entry| (entry.repo_path, false))
+ .collect()
+ } else {
+ self.selected_ignorable_path().into_iter().collect()
+ }
+ }
+
fn add_to_gitignore(
&mut self,
_: &git::AddToGitignore,
_window: &mut Window,
cx: &mut Context<Self>,
) {
- let Some((repo_path, is_dir)) = self.selected_ignorable_path() else {
+ let paths = self.selected_ignorable_paths();
+ if paths.is_empty() {
return;
- };
+ }
let Some(active_repository) = self.active_repository.clone() else {
return;
};
- let workspace = self.workspace.clone();
- let receiver =
- active_repository.update(cx, |repo, _| repo.add_path_to_gitignore(&repo_path, is_dir));
+ let receivers = active_repository.update(cx, |repo, _| {
+ paths
+ .iter()
+ .map(|(repo_path, is_dir)| repo.add_path_to_gitignore(repo_path, *is_dir))
+ .collect::<Vec<_>>()
+ });
- cx.spawn(async move |_, cx| {
- if let Err(e) = receiver.await? {
- if let Some(workspace) = workspace.upgrade() {
- cx.update(|cx| {
- show_error_toast(workspace, "add to .gitignore", e, cx);
- });
+ for receiver in receivers {
+ let workspace = self.workspace.clone();
+ cx.spawn(async move |_, cx| {
+ if let Err(error) = receiver.await? {
+ if let Some(workspace) = workspace.upgrade() {
+ cx.update(|cx| {
+ show_error_toast(workspace, "add to .gitignore", error, cx);
+ });
+ }
}
- }
- anyhow::Ok(())
- })
- .detach_and_log_err(cx);
+ anyhow::Ok(())
+ })
+ .detach_and_log_err(cx);
+ }
}
fn add_to_git_info_exclude(
@@ -2710,30 +2821,36 @@ impl GitPanel {
_window: &mut Window,
cx: &mut Context<Self>,
) {
- let Some((repo_path, is_dir)) = self.selected_ignorable_path() else {
+ let paths = self.selected_ignorable_paths();
+ if paths.is_empty() {
return;
- };
+ }
let Some(active_repository) = self.active_repository.clone() else {
return;
};
- let workspace = self.workspace.clone();
- let receiver = active_repository.update(cx, |repo, _| {
- repo.add_path_to_git_info_exclude(&repo_path, is_dir)
+ let receivers = active_repository.update(cx, |repo, _| {
+ paths
+ .iter()
+ .map(|(repo_path, is_dir)| repo.add_path_to_git_info_exclude(repo_path, *is_dir))
+ .collect::<Vec<_>>()
});
- cx.spawn(async move |_, cx| {
- if let Err(e) = receiver.await? {
- if let Some(workspace) = workspace.upgrade() {
- cx.update(|cx| {
- show_error_toast(workspace, "add to .git/info/exclude", e, cx);
- });
+ for receiver in receivers {
+ let workspace = self.workspace.clone();
+ cx.spawn(async move |_, cx| {
+ if let Err(error) = receiver.await? {
+ if let Some(workspace) = workspace.upgrade() {
+ cx.update(|cx| {
+ show_error_toast(workspace, "add to .git/info/exclude", error, cx);
+ });
+ }
}
- }
- anyhow::Ok(())
- })
- .detach_and_log_err(cx);
+ anyhow::Ok(())
+ })
+ .detach_and_log_err(cx);
+ }
}
fn revert_entries(
@@ -4991,10 +5108,13 @@ impl GitPanel {
window: &mut Window,
cx: &mut Context<Self>,
) {
- let entry = self
- .get_selected_entry()
- .and_then(|entry| entry.status_entry())
- .cloned();
+ let entry = if self.effective_status_entries().len() > 1 {
+ None
+ } else {
+ self.get_selected_entry()
+ .and_then(GitListEntry::status_entry)
+ .cloned()
+ };
if let Some(workspace) = self.workspace.upgrade() {
workspace.update(cx, |workspace, cx| {
StagedDiff::deploy_at(workspace, entry, window, cx);
@@ -5008,10 +5128,13 @@ impl GitPanel {
window: &mut Window,
cx: &mut Context<Self>,
) {
- let entry = self
- .get_selected_entry()
- .and_then(|entry| entry.status_entry())
- .cloned();
+ let entry = if self.effective_status_entries().len() > 1 {
+ None
+ } else {
+ self.get_selected_entry()
+ .and_then(GitListEntry::status_entry)
+ .cloned()
+ };
if let Some(workspace) = self.workspace.upgrade() {
workspace.update(cx, |workspace, cx| {
UnstagedDiff::deploy_at(workspace, entry, window, cx);
@@ -8155,10 +8278,28 @@ impl GitPanel {
&format!("Discard Changes to {file_count} {file_label}")
}
};
-
let is_bulk = matches!(target_kind, SelectionTargetKind::Multiple);
- let is_file = matches!(target_kind, SelectionTargetKind::File);
-
+ let is_file_or_bulk = !matches!(target_kind, SelectionTargetKind::Directory);
+ let has_tracked = !all_created;
+ let plural = file_count > 1;
+ let copy_path_title = if plural { "Copy Paths" } else { "Copy Path" };
+ let copy_relative_path_title = if plural {
+ "Copy Relative Paths"
+ } else {
+ "Copy Relative Path"
+ };
+ let open_file_diff_title = if plural {
+ "Open File Diffs"
+ } else {
+ "Open File Diff"
+ };
+ let view_file_title = if plural { "View Files" } else { "View File" };
+ let open_diff_title = if plural { "Open Diffs" } else { "Open Diff" };
+ let view_file_history_title = if plural {
+ "View File Histories"
+ } else {
+ "View File History"
+ };
ContextMenu::build(window, cx, |context_menu, _, _| {
context_menu
.context(self.focus_handle.clone())
@@ -8168,30 +8309,38 @@ impl GitPanel {
.action("Unstaged Changes", ViewUnstagedChanges.boxed_clone())
.action("Staged Changes", ViewStagedChanges.boxed_clone())
.separator()
- .action("Copy Path", CopyPath.boxed_clone())
- .action("Copy Relative Path", CopyRelativePath.boxed_clone())
+ .action(copy_path_title, CopyPath.boxed_clone())
+ .action(copy_relative_path_title, CopyRelativePath.boxed_clone())
.separator()
.action_disabled_when(
- !all_created || is_bulk,
- "Add to .gitignore",
- AddToGitignore.boxed_clone(),
+ !all_created,
+ if is_bulk && plural {
+ "Add Files to .gitignore"
+ } else {
+ "Add to .gitignore"
+ },
+ git::AddToGitignore.boxed_clone(),
)
.action_disabled_when(
- !all_created || is_bulk,
- "Add to .git/info/exclude",
- AddToGitInfoExclude.boxed_clone(),
+ !all_created,
+ if is_bulk && plural {
+ "Add Files to .git/info/exclude"
+ } else {
+ "Add to .git/info/exclude"
+ },
+ git::AddToGitInfoExclude.boxed_clone(),
)
- .when(is_file, |context_menu| {
+ .when(is_file_or_bulk, |context_menu| {
context_menu
.separator()
- .action("Open Diff", menu::Confirm.boxed_clone())
- .action("Open File Diff", menu::SecondaryConfirm.boxed_clone())
- .action("View File", ViewFile.boxed_clone())
+ .action(open_diff_title, menu::Confirm.boxed_clone())
+ .action(open_file_diff_title, menu::SecondaryConfirm.boxed_clone())
+ .action(view_file_title, ViewFile.boxed_clone())
})
- .when(is_file && !all_created, |context_menu| {
+ .when(is_file_or_bulk && has_tracked, |context_menu| {
context_menu
.separator()
- .action("View File History", Box::new(git::FileHistory))
+ .action(view_file_history_title, Box::new(git::FileHistory))
})
})
}
@@ -9126,6 +9275,7 @@ impl Render for GitPanel {
.on_action(cx.listener(Self::open_diff))
.on_action(cx.listener(Self::open_solo_diff))
.on_action(cx.listener(Self::view_file))
+ .on_action(cx.listener(Self::view_file_history))
.on_action(cx.listener(Self::copy_path))
.on_action(cx.listener(Self::copy_relative_path))
.on_action(cx.listener(Self::view_unstaged_changes))
@@ -10308,6 +10458,114 @@ mod tests {
assert_editor_opened_with_path(&workspace, Path::new("src/a/foo.rs"), &mut cx);
}
+ #[gpui::test]
+ async fn test_file_actions_use_multiple_selected_entries(cx: &mut TestAppContext) {
+ init_test(cx);
+ let (_, _, workspace, panel, mut cx) = setup_git_panel_with_changes(
+ cx,
+ json!({
+ ".git": {},
+ "a.txt": "a",
+ "b.txt": "b",
+ "c.txt": "c",
+ }),
+ &[
+ ("a.txt", StatusCode::Modified),
+ ("b.txt", StatusCode::Modified),
+ ("c.txt", StatusCode::Modified),
+ ],
+ )
+ .await;
+
+ panel.update_in(&mut cx, |panel, window, cx| {
+ panel.selected_entry = entry_index_for_repo_path(panel, &repo_path("b.txt"));
+ panel.marked_entries = HashSet::from_iter([repo_path("a.txt"), repo_path("b.txt")]);
+ panel.copy_relative_path(&CopyRelativePath, window, cx);
+ assert_eq!(
+ cx.read_from_clipboard().and_then(|item| item.text()),
+ Some("a.txt\nb.txt".to_owned())
+ );
+ panel.copy_path(&CopyPath, window, cx);
+ assert_eq!(
+ cx.read_from_clipboard().and_then(|item| item.text()),
+ Some(format!(
+ "{}\n{}",
+ path!("/project/a.txt"),
+ path!("/project/b.txt")
+ ))
+ );
+ panel.view_file(&ViewFile, window, cx);
+ });
+ cx.run_until_parked();
+
+ workspace.read_with(&cx, |workspace, cx| {
+ let opened_paths = workspace
+ .items_of_type::<Editor>(cx)
+ .filter_map(|editor| {
+ let buffer = editor.read(cx).active_buffer(cx)?;
+ let file = buffer.read(cx).file()?.clone();
+ Some(file.path().as_ref().as_std_path().to_path_buf())
+ })
+ .collect::<HashSet<_>>();
+ assert_eq!(
+ opened_paths,
+ HashSet::from_iter([
+ Path::new("a.txt").to_path_buf(),
+ Path::new("b.txt").to_path_buf(),
+ ])
+ );
+ });
+
+ panel.update_in(&mut cx, |panel, window, cx| {
+ panel.open_diff(&menu::Confirm, window, cx);
+ });
+ cx.run_until_parked();
+ panel.update_in(&mut cx, |panel, window, cx| {
+ panel.open_solo_diff(&menu::SecondaryConfirm, window, cx);
+ panel.view_file_history(&git::FileHistory, window, cx);
+ });
+ cx.run_until_parked();
+
+ workspace.read_with(&cx, |workspace, cx| {
+ assert_eq!(workspace.items_of_type::<SoloDiffView>(cx).count(), 2);
+ assert_eq!(
+ workspace
+ .items_of_type::<crate::git_graph::GitGraph>(cx)
+ .count(),
+ 2
+ );
+ });
+ }
+
+ #[gpui::test]
+ async fn test_ignore_actions_use_multiple_selected_entries(cx: &mut TestAppContext) {
+ init_test(cx);
+ let (fs, _, panel, mut cx) = setup_flat_marks_fixture(cx).await;
+
+ panel.update_in(&mut cx, |panel, window, cx| {
+ panel.selected_entry = entry_index_for_repo_path(panel, &repo_path("new1.txt"));
+ panel.marked_entries =
+ HashSet::from_iter([repo_path("new1.txt"), repo_path("new2.txt")]);
+ panel.add_to_gitignore(&git::AddToGitignore, window, cx);
+ panel.add_to_git_info_exclude(&git::AddToGitInfoExclude, window, cx);
+ });
+ cx.run_until_parked();
+
+ let expected = "new1.txt\nnew2.txt\n";
+ assert_eq!(
+ fs.load(Path::new(path!("/project/.gitignore")))
+ .await
+ .expect(".gitignore should exist"),
+ expected
+ );
+ assert_eq!(
+ fs.load(Path::new(path!("/project/.git/info/exclude")))
+ .await
+ .expect(".git/info/exclude should exist"),
+ expected
+ );
+ }
+
#[gpui::test]
async fn test_copy_paths(cx: &mut TestAppContext) {
init_test(cx);
@@ -10326,8 +10584,12 @@ mod tests {
"src": {
"main.rs": "fn main() {}",
},
+ "other.txt": "other",
}),
- &[("src/main.rs", StatusCode::Modified)],
+ &[
+ ("src/main.rs", StatusCode::Modified),
+ ("other.txt", StatusCode::Modified),
+ ],
)
.await;
@@ -10377,6 +10639,26 @@ mod tests {
cx.read_from_clipboard().and_then(|item| item.text()),
Some(path!("/project/src").to_owned())
);
+
+ panel.update(&mut cx, |panel, _| {
+ let directory_key = panel
+ .get_selected_entry()
+ .and_then(GitListEntry::directory_entry)
+ .expect("src directory should be selected")
+ .key
+ .clone();
+ panel.marked_directories.insert(directory_key);
+ panel.marked_entries.insert(repo_path("other.txt"));
+ });
+ cx.dispatch_action(CopyRelativePath);
+ let copied_paths = cx
+ .read_from_clipboard()
+ .and_then(|item| item.text())
+ .expect("selected paths should be copied");
+ assert_eq!(
+ copied_paths.lines().collect::<HashSet<_>>(),
+ HashSet::from_iter([path!("src"), path!("other.txt")]),
+ );
}
async fn history_panel_for_project(
@@ -14726,7 +15008,7 @@ mod tests {
.expect("`src` directory should be present in the Git panel");
panel.selected_entry = Some(index);
- panel.add_to_gitignore(&AddToGitignore, window, cx);
+ panel.add_to_gitignore(&git::AddToGitignore, window, cx);
});
cx.run_until_parked();
@@ -14764,7 +15046,7 @@ mod tests {
.expect("`docs` directory should be present in the Git panel");
panel.selected_entry = Some(index);
- panel.add_to_gitignore(&AddToGitignore, window, cx);
+ panel.add_to_gitignore(&git::AddToGitignore, window, cx);
});
cx.run_until_parked();
@@ -14817,7 +15099,7 @@ mod tests {
.expect("`src` directory should be present in the Git panel");
panel.selected_entry = Some(index);
- panel.add_to_git_info_exclude(&AddToGitInfoExclude, window, cx);
+ panel.add_to_git_info_exclude(&git::AddToGitInfoExclude, window, cx);
});
cx.run_until_parked();
@@ -14855,7 +15137,7 @@ mod tests {
.expect("`docs` directory should be present in the Git panel");
panel.selected_entry = Some(index);
- panel.add_to_git_info_exclude(&AddToGitInfoExclude, window, cx);
+ panel.add_to_git_info_exclude(&git::AddToGitInfoExclude, window, cx);
});
cx.run_until_parked();
@@ -15394,6 +15676,23 @@ mod tests {
vec![repo_path("b.txt"), repo_path("c.txt")],
"multiple marks win, in panel order",
);
+
+ let duplicate = panel
+ .entries
+ .iter()
+ .find(|entry| {
+ entry
+ .status_entry()
+ .is_some_and(|entry| entry.repo_path == repo_path("b.txt"))
+ })
+ .cloned()
+ .expect("b.txt should be present");
+ panel.entries.push(duplicate);
+ assert_eq!(
+ paths(panel),
+ vec![repo_path("b.txt"), repo_path("c.txt")],
+ "a partially staged file shown in two sections should be included once",
+ );
});
}