Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Add optional message support to git stash (#62439)
Priyadharshanc6b01d8a203a
diff --git a/crates/fs/src/fake_git_repo.rs b/crates/fs/src/fake_git_repo.rsindex 268fdc7e6ca..0a87c4584d0 100644--- a/crates/fs/src/fake_git_repo.rs+++ b/crates/fs/src/fake_git_repo.rs@@ -1040,6 +1040,7 @@ impl GitRepository for FakeGitRepository {fn stash_paths(&self,_paths: Vec<RepoPath>,+ _message: Option<String>,_env: Arc<HashMap<String, String>>,) -> BoxFuture<'_, Result<()>> {unimplemented!()diff --git a/crates/git/src/repository.rs b/crates/git/src/repository.rsindex b6393d90936..9d3e4086f30 100644--- a/crates/git/src/repository.rs+++ b/crates/git/src/repository.rs@@ -934,6 +934,7 @@ pub trait GitRepository: Send + Sync {fn stash_paths(&self,paths: Vec<RepoPath>,+ message: Option<String>,env: Arc<HashMap<String, String>>,) -> BoxFuture<'_, Result<()>>;@@ -2490,14 +2491,20 @@ impl GitRepository for RealGitRepository {fn stash_paths(&self,paths: Vec<RepoPath>,+ message: Option<String>,env: Arc<HashMap<String, String>>,) -> BoxFuture<'_, Result<()>> {let git = self.git_binary_in_worktree();self.executor.spawn(async move {let git = git?;+ let mut args = vec!["stash", "push", "--quiet", "--include-untracked"];+ if let Some(message) = message.as_deref() {+ args.extend_from_slice(&["--message", message]);+ }+ args.push("--");let output = git- .build_command(&["stash", "push", "--quiet", "--include-untracked", "--"])+ .build_command(&args).envs(env.iter()).args(paths.iter().map(|p| p.as_unix_str())).output()diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rsindex cb92cacfcd3..1f880c59d35 100644--- a/crates/git_ui/src/git_panel.rs+++ b/crates/git_ui/src/git_panel.rs@@ -98,7 +98,7 @@ use util::paths::PathStyle;use util::{ResultExt, TryFutureExt, markdown::MarkdownInlineCode, maybe, rel_path::RelPath};use workspace::SERIALIZATION_THROTTLE_TIME;use workspace::{- Item, Workspace,+ Item, ModalView, Workspace,dock::{DockPosition, Panel, PanelEvent},notifications::{DetachAndPromptErr, NotificationId, NotifyTaskExt},};@@ -201,6 +201,67 @@ struct GitPanelViewOptionsMenuState {tree_view: bool,}+/// Prompts for an optional stash name. Confirming with an empty editor stashes+/// without `--message`, letting git generate its usual "WIP on ..." description.+struct StashMessageModal {+ editor: Entity<Editor>,+ panel: WeakEntity<GitPanel>,+}++impl StashMessageModal {+ fn new(panel: WeakEntity<GitPanel>, window: &mut Window, cx: &mut Context<Self>) -> Self {+ let editor = cx.new(|cx| {+ let mut editor = Editor::single_line(window, cx);+ editor.set_placeholder_text("Optionally provide a stash message", window, cx);+ editor+ });+ Self { editor, panel }+ }++ fn cancel(&mut self, _: &menu::Cancel, _window: &mut Window, cx: &mut Context<Self>) {+ cx.emit(DismissEvent);+ }++ fn confirm(&mut self, _: &menu::Confirm, _window: &mut Window, cx: &mut Context<Self>) {+ let message = self.editor.read(cx).text(cx).trim().to_owned();+ let message = (!message.is_empty()).then_some(message);+ self.panel+ .update(cx, |panel, cx| panel.perform_stash(message, cx))+ .ok();+ cx.emit(DismissEvent);+ }+}++impl EventEmitter<DismissEvent> for StashMessageModal {}+impl ModalView for StashMessageModal {}+impl Focusable for StashMessageModal {+ fn focus_handle(&self, cx: &App) -> FocusHandle {+ self.editor.focus_handle(cx)+ }+}++impl Render for StashMessageModal {+ fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {+ v_flex()+ .key_context("StashMessageModal")+ .on_action(cx.listener(Self::cancel))+ .on_action(cx.listener(Self::confirm))+ .elevation_2(cx)+ .w(rems(34.))+ .child(+ h_flex()+ .px_3()+ .pt_2()+ .pb_1()+ .w_full()+ .gap_1p5()+ .child(Icon::new(IconName::GitBranch).size(IconSize::XSmall))+ .child(Headline::new("Stash All").size(HeadlineSize::XSmall)),+ )+ .child(div().px_3().pb_3().w_full().child(self.editor.clone()))+ }+}+fn git_panel_context_menu(has_tracked_changes: bool,has_staged_changes: bool,@@ -2731,7 +2792,25 @@ impl GitPanel {.detach();}- pub fn stash_all(&mut self, _: &StashAll, _window: &mut Window, cx: &mut Context<Self>) {+ pub fn stash_all(&mut self, _: &StashAll, window: &mut Window, cx: &mut Context<Self>) {+ if self.active_repository.is_none() {+ return;+ }+ // `git::StashAll` is also registered on the workspace, which dispatches it while+ // `Workspace` is leased, so opening the modal inline would re-enter that update.+ cx.defer_in(window, |this, window, cx| {+ let panel = cx.entity().downgrade();+ this.workspace+ .update(cx, |workspace, cx| {+ workspace.toggle_modal(window, cx, |window, cx| {+ StashMessageModal::new(panel, window, cx)+ });+ })+ .ok();+ });+ }++ fn perform_stash(&mut self, message: Option<String>, cx: &mut Context<Self>) {let Some(active_repository) = self.active_repository.clone() else {return;};@@ -2739,7 +2818,7 @@ impl GitPanel {cx.spawn({async move |this, cx| {let stash_task = active_repository- .update(cx, |repo, cx| repo.stash_all(cx))+ .update(cx, |repo, cx| repo.stash_all(message, cx)).await;this.update(cx, |this, cx| {stash_taskdiff --git a/crates/project/src/git_store.rs b/crates/project/src/git_store.rsindex a25fd88bece..6e88c809129 100644--- a/crates/project/src/git_store.rs+++ b/crates/project/src/git_store.rs@@ -3063,6 +3063,8 @@ impl GitStore {let repository_id = RepositoryId::from_proto(envelope.payload.repository_id);let repository_handle = Self::repository_for_request(&this, repository_id, &mut cx)?;+ let message = envelope.payload.message;+let entries = envelope.payload.paths@@ -3072,7 +3074,7 @@ impl GitStore {repository_handle.update(&mut cx, |repository_handle, cx| {- repository_handle.stash_entries(entries, cx)+ repository_handle.stash_entries(entries, message, cx)}).await?;@@ -7473,15 +7475,20 @@ impl Repository {})}- pub fn stash_all(&mut self, cx: &mut Context<Self>) -> Task<anyhow::Result<()>> {+ pub fn stash_all(+ &mut self,+ message: Option<String>,+ cx: &mut Context<Self>,+ ) -> Task<anyhow::Result<()>> {let to_stash = self.cached_status().map(|entry| entry.repo_path).collect();- self.stash_entries(to_stash, cx)+ self.stash_entries(to_stash, message, cx)}pub fn stash_entries(&mut self,entries: Vec<RepoPath>,+ message: Option<String>,cx: &mut Context<Self>,) -> Task<anyhow::Result<()>> {let id = self.id;@@ -7494,7 +7501,7 @@ impl Repository {backend,environment,..- }) => backend.stash_paths(entries, environment).await,+ }) => backend.stash_paths(entries, message, environment).await,RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {client.request(proto::Stash {@@ -7504,6 +7511,7 @@ impl Repository {.into_iter().map(|repo_path| repo_path.as_unix_str().to_owned()).collect(),+ message,}).await?;Ok(())diff --git a/crates/proto/proto/git.proto b/crates/proto/proto/git.protoindex 5320328f766..eb5bb543235 100644--- a/crates/proto/proto/git.proto+++ b/crates/proto/proto/git.proto@@ -378,6 +378,7 @@ message Stash {uint64 project_id = 1;uint64 repository_id = 2;repeated string paths = 3;+ optional string message = 4;}message StashPop {diff --git a/docs/src/git.md b/docs/src/git.mdindex 07f2a00556e..e599c860344 100644--- a/docs/src/git.md+++ b/docs/src/git.md@@ -244,6 +244,8 @@ Git stash allows you to temporarily save your uncommitted changes and revert youTo stash all your current changes, use the {#action git::StashAll} action. This will save both staged and unstaged changes to a new stash entry and clean your working directory.+The action first prompts for an optional stash name. Confirming with an empty prompt lets git generate its usual `WIP on ...` description.+### Managing StashesZed provides a stash picker accessible via {#action git::ViewStash} or from the Git Panel's overflow menu. From the stash picker, you can: