Add stash picker

This commit is contained in:
Alvaro Parker 2025-08-09 12:53:33 -04:00
parent 3498ba894e
commit 6a09300c63
No known key found for this signature in database
11 changed files with 500 additions and 10 deletions

View file

@ -460,6 +460,7 @@ impl Server {
.add_request_handler(forward_mutating_project_request::<proto::Unstage>)
.add_request_handler(forward_mutating_project_request::<proto::Stash>)
.add_request_handler(forward_mutating_project_request::<proto::StashPop>)
.add_request_handler(forward_mutating_project_request::<proto::StashDrop>)
.add_request_handler(forward_mutating_project_request::<proto::Commit>)
.add_request_handler(forward_mutating_project_request::<proto::GitInit>)
.add_request_handler(forward_read_only_project_request::<proto::GetRemotes>)

View file

@ -416,7 +416,19 @@ impl GitRepository for FakeGitRepository {
unimplemented!()
}
fn stash_pop(&self, _env: Arc<HashMap<String, String>>) -> BoxFuture<'_, Result<()>> {
fn stash_pop(
&self,
_index: Option<usize>,
_env: Arc<HashMap<String, String>>,
) -> BoxFuture<'_, Result<()>> {
unimplemented!()
}
fn stash_drop(
&self,
_index: Option<usize>,
_env: Arc<HashMap<String, String>>,
) -> BoxFuture<'_, Result<()>> {
unimplemented!()
}

View file

@ -402,7 +402,17 @@ pub trait GitRepository: Send + Sync {
env: Arc<HashMap<String, String>>,
) -> BoxFuture<'_, Result<()>>;
fn stash_pop(&self, env: Arc<HashMap<String, String>>) -> BoxFuture<'_, Result<()>>;
fn stash_pop(
&self,
index: Option<usize>,
env: Arc<HashMap<String, String>>,
) -> BoxFuture<'_, Result<()>>;
fn stash_drop(
&self,
index: Option<usize>,
env: Arc<HashMap<String, String>>,
) -> BoxFuture<'_, Result<()>>;
fn push(
&self,
@ -1250,14 +1260,22 @@ impl GitRepository for RealGitRepository {
.boxed()
}
fn stash_pop(&self, env: Arc<HashMap<String, String>>) -> BoxFuture<'_, Result<()>> {
fn stash_pop(
&self,
index: Option<usize>,
env: Arc<HashMap<String, String>>,
) -> BoxFuture<'_, Result<()>> {
let working_directory = self.working_directory();
self.executor
.spawn(async move {
let mut cmd = new_smol_command("git");
let mut args = vec!["stash".to_string(), "pop".to_string()];
if let Some(index) = index {
args.push(format!("stash@{{{}}}", index));
}
cmd.current_dir(&working_directory?)
.envs(env.iter())
.args(["stash", "pop"]);
.args(args);
let output = cmd.output().await?;
@ -1271,6 +1289,35 @@ impl GitRepository for RealGitRepository {
.boxed()
}
fn stash_drop(
&self,
index: Option<usize>,
env: Arc<HashMap<String, String>>,
) -> BoxFuture<'_, Result<()>> {
let working_directory = self.working_directory();
self.executor
.spawn(async move {
let mut cmd = new_smol_command("git");
let mut args = vec!["stash".to_string(), "drop".to_string()];
if let Some(index) = index {
args.push(format!("stash@{{{}}}", index));
}
cmd.current_dir(&working_directory?)
.envs(env.iter())
.args(args);
let output = cmd.output().await?;
anyhow::ensure!(
output.status.success(),
"Failed to stash drop:\n{}",
String::from_utf8_lossy(&output.stderr)
);
Ok(())
})
.boxed()
}
fn commit(
&self,
message: SharedString,

View file

@ -1426,7 +1426,7 @@ impl GitPanel {
cx.spawn({
async move |this, cx| {
let stash_task = active_repository
.update(cx, |repo, cx| repo.stash_pop(cx))?
.update(cx, |repo, cx| repo.stash_pop(None, cx))?
.await;
this.update(cx, |this, cx| {
stash_task

View file

@ -36,6 +36,7 @@ pub mod picker_prompt;
pub mod project_diff;
pub(crate) mod remote_output;
pub mod repository_selector;
pub mod stash_picker;
pub mod text_diff_view;
actions!(
@ -62,6 +63,7 @@ pub fn init(cx: &mut App) {
git_panel::register(workspace);
repository_selector::register(workspace);
branch_picker::register(workspace);
stash_picker::register(workspace);
let project = workspace.project().read(cx);
if project.is_read_only(cx) {

View file

@ -0,0 +1,356 @@
use fuzzy::StringMatchCandidate;
use git::stash::StashEntry;
use gpui::{
App, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, InteractiveElement,
IntoElement, Modifiers, ModifiersChangedEvent, ParentElement, Render, SharedString, Styled,
Subscription, Task, Window, rems,
};
use picker::{Picker, PickerDelegate, PickerEditorPosition};
use project::git_store::Repository;
use std::sync::Arc;
use ui::{HighlightedLabel, ListItem, ListItemSpacing, prelude::*};
use util::ResultExt;
use workspace::notifications::DetachAndPromptErr;
use workspace::{ModalView, Workspace};
pub fn register(workspace: &mut Workspace) {
workspace.register_action(open);
}
pub fn open(
workspace: &mut Workspace,
_: &zed_actions::git::Stash,
window: &mut Window,
cx: &mut Context<Workspace>,
) {
let repository = workspace.project().read(cx).active_repository(cx).clone();
let style = StashListStyle::Modal;
workspace.toggle_modal(window, cx, |window, cx| {
StashList::new(repository, style, rems(34.), window, cx)
})
}
pub fn popover(
repository: Option<Entity<Repository>>,
window: &mut Window,
cx: &mut App,
) -> Entity<StashList> {
cx.new(|cx| {
let list = StashList::new(repository, StashListStyle::Popover, rems(20.), window, cx);
list.focus_handle(cx).focus(window);
list
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum StashListStyle {
Modal,
Popover,
}
pub struct StashList {
width: Rems,
pub picker: Entity<Picker<StashListDelegate>>,
_subscription: Subscription,
}
impl StashList {
fn new(
repository: Option<Entity<Repository>>,
style: StashListStyle,
width: Rems,
window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
let stash_request = repository
.clone()
.map(|repository| repository.read_with(cx, |repo, _| repo.stash_entries.clone()));
cx.spawn_in(window, async move |this, cx| {
let stash_entries = stash_request
.map(|git_stash| git_stash.entries.to_vec())
.unwrap_or_default();
this.update_in(cx, |this, window, cx| {
this.picker.update(cx, |picker, cx| {
picker.delegate.all_stash_entries = Some(stash_entries);
picker.refresh(window, cx);
})
})?;
anyhow::Ok(())
})
.detach_and_log_err(cx);
let delegate = StashListDelegate::new(repository.clone(), style);
let picker = cx.new(|cx| Picker::uniform_list(delegate, window, cx));
let _subscription = cx.subscribe(&picker, |_, _, _, cx| {
cx.emit(DismissEvent);
});
Self {
picker,
width,
_subscription,
}
}
fn handle_modifiers_changed(
&mut self,
ev: &ModifiersChangedEvent,
_: &mut Window,
cx: &mut Context<Self>,
) {
self.picker
.update(cx, |picker, _| picker.delegate.modifiers = ev.modifiers)
}
}
impl ModalView for StashList {}
impl EventEmitter<DismissEvent> for StashList {}
impl Focusable for StashList {
fn focus_handle(&self, cx: &App) -> FocusHandle {
self.picker.focus_handle(cx)
}
}
impl Render for StashList {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
v_flex()
.key_context("GitStashSelector")
.w(self.width)
.on_modifiers_changed(cx.listener(Self::handle_modifiers_changed))
.child(self.picker.clone())
.on_mouse_down_out({
cx.listener(move |this, _, window, cx| {
this.picker.update(cx, |this, cx| {
this.cancel(&Default::default(), window, cx);
})
})
})
}
}
#[derive(Debug, Clone)]
struct StashEntryMatch {
entry: StashEntry,
positions: Vec<usize>,
}
pub struct StashListDelegate {
matches: Vec<StashEntryMatch>,
all_stash_entries: Option<Vec<StashEntry>>,
repo: Option<Entity<Repository>>,
style: StashListStyle,
selected_index: usize,
last_query: String,
modifiers: Modifiers,
}
impl StashListDelegate {
fn new(repo: Option<Entity<Repository>>, style: StashListStyle) -> Self {
Self {
matches: vec![],
repo,
style,
all_stash_entries: None,
selected_index: 0,
last_query: Default::default(),
modifiers: Default::default(),
}
}
fn drop_stash(&self, stash_index: usize, window: &mut Window, cx: &mut Context<Picker<Self>>) {
let Some(repo) = self.repo.clone() else {
return;
};
cx.spawn(async move |_, cx| {
repo.update(cx, |repo, cx| repo.stash_drop(Some(stash_index), cx))?
.await?;
Ok(())
})
.detach_and_prompt_err("Failed to apply stash", window, cx, |e, _, _| {
Some(e.to_string())
});
cx.emit(DismissEvent);
}
fn pop_stash(&self, stash_index: usize, window: &mut Window, cx: &mut Context<Picker<Self>>) {
let Some(repo) = self.repo.clone() else {
return;
};
cx.spawn(async move |_, cx| {
repo.update(cx, |repo, cx| repo.stash_pop(Some(stash_index), cx))?
.await?;
Ok(())
})
.detach_and_prompt_err("Failed to pop stash", window, cx, |e, _, _| {
Some(e.to_string())
});
cx.emit(DismissEvent);
}
}
impl PickerDelegate for StashListDelegate {
type ListItem = ListItem;
fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> Arc<str> {
"Select stash&".into()
}
fn editor_position(&self) -> PickerEditorPosition {
match self.style {
StashListStyle::Modal => PickerEditorPosition::Start,
StashListStyle::Popover => PickerEditorPosition::End,
}
}
fn match_count(&self) -> usize {
self.matches.len()
}
fn selected_index(&self) -> usize {
self.selected_index
}
fn set_selected_index(
&mut self,
ix: usize,
_window: &mut Window,
_: &mut Context<Picker<Self>>,
) {
self.selected_index = ix;
}
fn update_matches(
&mut self,
query: String,
window: &mut Window,
cx: &mut Context<Picker<Self>>,
) -> Task<()> {
let Some(all_stash_entries) = self.all_stash_entries.clone() else {
return Task::ready(());
};
cx.spawn_in(window, async move |picker, cx| {
let matches: Vec<StashEntryMatch> = if query.is_empty() {
all_stash_entries
.into_iter()
.map(|entry| StashEntryMatch {
entry,
positions: Vec::new(),
})
.collect()
} else {
let candidates = all_stash_entries
.iter()
.enumerate()
.map(|(ix, entry)| StringMatchCandidate::new(ix, &entry.message))
.collect::<Vec<StringMatchCandidate>>();
fuzzy::match_strings(
&candidates,
&query,
true,
true,
10000,
&Default::default(),
cx.background_executor().clone(),
)
.await
.into_iter()
.map(|candidate| StashEntryMatch {
entry: all_stash_entries[candidate.candidate_id].clone(),
positions: candidate.positions,
})
.collect()
};
picker
.update(cx, |picker, _| {
let delegate = &mut picker.delegate;
delegate.matches = matches;
if delegate.matches.is_empty() {
delegate.selected_index = 0;
} else {
delegate.selected_index =
core::cmp::min(delegate.selected_index, delegate.matches.len() - 1);
}
delegate.last_query = query;
})
.log_err();
})
}
fn confirm(&mut self, secondary: bool, window: &mut Window, cx: &mut Context<Picker<Self>>) {
let Some(entry_match) = self.matches.get(self.selected_index()) else {
return;
};
let stash_index = entry_match.entry.index;
if secondary {
// Secondary action: remove stash (remove from stash list)
self.drop_stash(stash_index, window, cx);
} else {
// Primary action: apply stash (keep in stash list)
self.pop_stash(stash_index, window, cx);
}
}
fn dismissed(&mut self, _: &mut Window, cx: &mut Context<Picker<Self>>) {
cx.emit(DismissEvent);
}
fn render_match(
&self,
ix: usize,
selected: bool,
_: &mut Window,
_: &mut Context<Picker<Self>>,
) -> Option<Self::ListItem> {
let entry_match = &self.matches[ix];
let stash_name = HighlightedLabel::new(
entry_match.entry.message.clone(),
entry_match.positions.clone(),
)
.truncate()
.into_any_element();
let stash_index_label = Label::new(format!("stash@{{{}}}", entry_match.entry.index))
.size(LabelSize::Small)
.color(Color::Muted);
Some(
ListItem::new(SharedString::from(format!("stash-{ix}")))
.inset(true)
.spacing(ListItemSpacing::Sparse)
.toggle_state(selected)
.child(
v_flex()
.w_full()
.overflow_hidden()
.child(
h_flex()
.gap_6()
.justify_between()
.overflow_x_hidden()
.child(stash_name)
.child(stash_index_label.into_element()),
)
.when(self.style == StashListStyle::Modal, |el| {
el.child(div().max_w_96())
}),
),
)
}
fn no_matches_text(&self, _window: &mut Window, _cx: &mut App) -> Option<SharedString> {
Some("No stashes found".into())
}
}

View file

@ -427,6 +427,7 @@ impl GitStore {
client.add_entity_request_handler(Self::handle_unstage);
client.add_entity_request_handler(Self::handle_stash);
client.add_entity_request_handler(Self::handle_stash_pop);
client.add_entity_request_handler(Self::handle_stash_drop);
client.add_entity_request_handler(Self::handle_commit);
client.add_entity_request_handler(Self::handle_reset);
client.add_entity_request_handler(Self::handle_show);
@ -1780,10 +1781,29 @@ impl GitStore {
) -> Result<proto::Ack> {
let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
let stash_index = envelope.payload.stash_index.and_then(|i| Some(i as usize));
repository_handle
.update(&mut cx, |repository_handle, cx| {
repository_handle.stash_pop(cx)
repository_handle.stash_pop(stash_index, cx)
})?
.await?;
Ok(proto::Ack {})
}
async fn handle_stash_drop(
this: Entity<Self>,
envelope: TypedEnvelope<proto::StashDrop>,
mut cx: AsyncApp,
) -> Result<proto::Ack> {
let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);
let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;
let stash_index = envelope.payload.stash_index.and_then(|i| Some(i as usize));
repository_handle
.update(&mut cx, |repository_handle, cx| {
repository_handle.stash_drop(stash_index, cx)
})?
.await?;
@ -3733,7 +3753,11 @@ impl Repository {
})
}
pub fn stash_pop(&mut self, cx: &mut Context<Self>) -> Task<anyhow::Result<()>> {
pub fn stash_pop(
&mut self,
index: Option<usize>,
cx: &mut Context<Self>,
) -> Task<anyhow::Result<()>> {
let id = self.id;
cx.spawn(async move |this, cx| {
this.update(cx, |this, _| {
@ -3743,12 +3767,47 @@ impl Repository {
backend,
environment,
..
} => backend.stash_pop(environment).await,
} => backend.stash_pop(index, environment).await,
RepositoryState::Remote { project_id, client } => {
client
.request(proto::StashPop {
project_id: project_id.0,
repository_id: id.to_proto(),
stash_index: index.and_then(|i| Some(i as u64)),
})
.await
.context("sending stash pop request")?;
Ok(())
}
}
})
})?
.await??;
Ok(())
})
}
pub fn stash_drop(
&mut self,
index: Option<usize>,
cx: &mut Context<Self>,
) -> Task<anyhow::Result<()>> {
let id = self.id;
cx.spawn(async move |this, cx| {
this.update(cx, |this, _| {
this.send_job(None, move |git_repo, _cx| async move {
match git_repo {
RepositoryState::Local {
backend,
environment,
..
} => backend.stash_drop(index, environment).await,
RepositoryState::Remote { project_id, client } => {
client
.request(proto::StashDrop {
project_id: project_id.0,
repository_id: id.to_proto(),
stash_index: index.and_then(|i| Some(i as u64)),
})
.await
.context("sending stash pop request")?;

View file

@ -313,6 +313,13 @@ message Stash {
message StashPop {
uint64 project_id = 1;
uint64 repository_id = 2;
optional uint64 stash_index = 3;
}
message StashDrop {
uint64 project_id = 1;
uint64 repository_id = 2;
optional uint64 stash_index = 3;
}
message Commit {

View file

@ -396,7 +396,8 @@ message Envelope {
GitCloneResponse git_clone_response = 364;
LspQuery lsp_query = 365;
LspQueryResponse lsp_query_response = 366; // current max
LspQueryResponse lsp_query_response = 366;
StashDrop stash_drop = 367; // current max
}
reserved 87 to 88;

View file

@ -257,6 +257,7 @@ messages!(
(Unstage, Background),
(Stash, Background),
(StashPop, Background),
(StashDrop, Background),
(UpdateBuffer, Foreground),
(UpdateBufferFile, Foreground),
(UpdateChannelBuffer, Foreground),
@ -417,6 +418,7 @@ request_messages!(
(Unstage, Ack),
(Stash, Ack),
(StashPop, Ack),
(StashDrop, Ack),
(UpdateBuffer, Ack),
(UpdateParticipantLocation, Ack),
(UpdateProject, Ack),
@ -570,6 +572,7 @@ entity_messages!(
Unstage,
Stash,
StashPop,
StashDrop,
UpdateBuffer,
UpdateBufferFile,
UpdateDiagnosticSummary,

View file

@ -175,7 +175,9 @@ pub mod git {
SelectRepo,
/// Opens the git branch selector.
#[action(deprecated_aliases = ["branches::OpenRecent"])]
Branch
Branch,
/// Opens the git stash selector.
Stash
]
);
}