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_ui: Ensure discarding tracked changes respects staging state (#61608)

Tai Nguyen11a8837bad66
diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rs
index 83ebe566f3c..9f01b2271bf 100644
--- a/crates/git_ui/src/git_panel.rs
+++ b/crates/git_ui/src/git_panel.rs
@@ -2263,7 +2263,9 @@ impl GitPanel {
) {
let entries = self
.change_entries_by_path()
- .filter(|status_entry| !status_entry.status.is_created())
+ .filter(|status_entry| {
+ !status_entry.status.is_created() && status_entry.status.staging().has_staged()
+ })
.cloned()
.collect::<Vec<_>>();
@@ -12555,4 +12557,84 @@ mod tests {
assert!(panel.commit_editor.focus_handle(cx).is_focused(window));
});
}
+
+ #[gpui::test]
+ async fn test_discard_tracked_changes_respects_staging(cx: &mut TestAppContext) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.background_executor.clone());
+ fs.insert_tree(
+ "/root",
+ json!({
+ "project": {
+ ".git": {},
+ "staged_a.rs": "staged a content\n",
+ "staged_b.rs": "staged b content\n",
+ "unstaged.rs": "unstaged content\n",
+ }
+ }),
+ )
+ .await;
+
+ fs.set_status_for_repo(
+ Path::new(path!("/root/project/.git")),
+ &[
+ ("staged_a.rs", FileStatus::index(StatusCode::Modified)),
+ ("staged_b.rs", FileStatus::index(StatusCode::Modified)),
+ ("unstaged.rs", StatusCode::Modified.worktree()),
+ ],
+ );
+
+ let project = Project::test(fs.clone(), [Path::new(path!("/root/project"))], cx).await;
+ let window_handle =
+ cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let workspace = window_handle
+ .read_with(cx, |mw, _| mw.workspace().clone())
+ .unwrap();
+ let cx = &mut VisualTestContext::from_window(window_handle.into(), cx);
+
+ cx.read(|cx| {
+ project
+ .read(cx)
+ .worktrees(cx)
+ .next()
+ .unwrap()
+ .read(cx)
+ .as_local()
+ .unwrap()
+ .scan_complete()
+ })
+ .await;
+
+ cx.executor().run_until_parked();
+
+ let panel = workspace.update_in(cx, GitPanel::new);
+
+ let handle = cx.update_window_entity(&panel, |panel, _, _| {
+ std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
+ });
+ cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
+ handle.await;
+ cx.executor().run_until_parked();
+
+ panel.update_in(cx, |panel, window, cx| {
+ panel.restore_tracked_files(&RestoreTrackedFiles, window, cx);
+ });
+
+ let (message, detail) = cx
+ .pending_prompt()
+ .expect("discard tracked should show a confirmation prompt");
+ assert_eq!(message, "Discard changes to these files?");
+ assert!(
+ detail.contains("staged_a.rs"),
+ "prompt should list staged_a.rs, got: {detail}"
+ );
+ assert!(
+ detail.contains("staged_b.rs"),
+ "prompt should list staged_b.rs, got: {detail}"
+ );
+ assert!(
+ !detail.contains("unstaged.rs"),
+ "prompt should NOT list unstaged.rs, got: {detail}"
+ );
+ }
}