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: Preserve in-progress commit message across reconnect (#58180)

Matt Van Hornbfe0d7c8f696
diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rs
index d22220af2a2..800058f25f9 100644
--- a/crates/git_ui/src/git_panel.rs
+++ b/crates/git_ui/src/git_panel.rs
@@ -42,7 +42,7 @@ use gpui::{
UniformListScrollHandle, WeakEntity, actions, anchored, deferred, point, size, uniform_list,
};
use itertools::Itertools;
-use language::{Buffer, File};
+use language::{Buffer, BufferEvent, File};
use language_model::{
CompletionIntent, ConfiguredModel, Event as LanguageModelEvent, LanguageModelRegistry,
LanguageModelRequest, LanguageModelRequestMessage, Role,
@@ -371,12 +371,22 @@ pub enum Event {
Focus,
}
-#[derive(Serialize, Deserialize)]
+#[derive(Default, Serialize, Deserialize)]
struct SerializedGitPanel {
- #[serde(default)]
- amend_pending: bool,
#[serde(default)]
signoff_enabled: bool,
+ #[serde(default)]
+ commit_messages: BTreeMap<String, SerializedCommitMessage>,
+}
+
+#[derive(Clone, Default, Serialize, Deserialize)]
+struct SerializedCommitMessage {
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ message: Option<String>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ original_message: Option<String>,
+ #[serde(default)]
+ amend_pending: bool,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
@@ -760,6 +770,7 @@ pub struct GitPanel {
pending_remote_operation: Option<RemoteOperationKind>,
amend_pending: bool,
original_commit_message: Option<String>,
+ pending_commit_message_restores: BTreeMap<String, SerializedCommitMessage>,
signoff_enabled: bool,
pending_serialization: Task<()>,
pub(crate) project: Entity<Project>,
@@ -770,6 +781,7 @@ pub struct GitPanel {
tracked_count: usize,
tracked_staged_count: usize,
update_visible_entries_task: Task<()>,
+ reopen_commit_buffer_task: Task<()>,
pub(crate) workspace: WeakEntity<Workspace>,
context_menu: Option<(Entity<ContextMenu>, Point<Pixels>, Subscription)>,
modal_open: bool,
@@ -786,6 +798,7 @@ pub struct GitPanel {
commit_history_shas: Option<Vec<Oid>>,
focused_history_entry: Option<usize>,
history_keyboard_nav: bool,
+ _commit_message_buffer_subscription: Option<Subscription>,
_repo_subscriptions: Vec<Subscription>,
_settings_subscription: Subscription,
@@ -832,16 +845,60 @@ pub(crate) fn commit_message_editor(
}
impl GitPanel {
+ // Only the test-support constructors call this thin wrapper now; production
+ // registration goes through `new_with_serialized_panel` directly. Gate it to
+ // the same cfg as `new_test` so the non-test lib build doesn't see it as dead.
+ #[cfg(any(test, feature = "test-support"))]
fn new(
workspace: &mut Workspace,
window: &mut Window,
cx: &mut Context<Workspace>,
+ ) -> Entity<Self> {
+ Self::new_with_serialized_panel(workspace, None, window, cx)
+ }
+
+ fn new_with_serialized_panel(
+ workspace: &mut Workspace,
+ serialized_panel: Option<SerializedGitPanel>,
+ window: &mut Window,
+ cx: &mut Context<Workspace>,
) -> Entity<Self> {
let project = workspace.project().clone();
let app_state = workspace.app_state().clone();
let fs = app_state.fs.clone();
let git_store = project.read(cx).git_store().clone();
let active_repository = project.read(cx).active_repository(cx);
+ let signoff_enabled = serialized_panel
+ .as_ref()
+ .is_some_and(|panel| panel.signoff_enabled);
+ let active_work_directory_abs_path = active_repository.as_ref().map(|repository| {
+ repository
+ .read(cx)
+ .work_directory_abs_path
+ .to_string_lossy()
+ .into_owned()
+ });
+ let active_draft = serialized_panel.as_ref().and_then(|panel| {
+ let path = active_work_directory_abs_path.as_ref()?;
+ panel.commit_messages.get(path)
+ });
+ // Seed the placeholder editor with the restored draft when the active
+ // repository already matches the serialized one, so the message is
+ // present immediately on restart instead of only after the commit
+ // buffer finishes loading in `reopen_commit_buffer`. Sourced from the
+ // serialized draft rather than a live buffer snapshot and scoped to the
+ // matching repository, so it neither replays cleared text nor leaks a
+ // draft across repositories. `reopen_commit_buffer` still performs the
+ // one-shot restore into the loaded buffer; applying the same draft
+ // there is idempotent.
+ let amend_pending = active_draft.is_some_and(|draft| draft.amend_pending);
+ let original_commit_message = active_draft.and_then(|draft| draft.original_message.clone());
+ let initial_commit_message = active_draft
+ .and_then(|draft| draft.message.clone())
+ .unwrap_or_default();
+ let pending_commit_message_restores = serialized_panel
+ .map(|panel| panel.commit_messages)
+ .unwrap_or_default();
cx.new(|cx| {
let focus_handle = cx.focus_handle();
@@ -905,15 +962,11 @@ impl GitPanel {
// just to let us render a placeholder editor.
// Once the active git repo is set, this buffer will be replaced.
- let temporary_buffer = cx.new(|cx| Buffer::local("", cx));
+ let temporary_buffer = cx.new(|cx| Buffer::local(initial_commit_message, cx));
let commit_editor = cx.new(|cx| {
commit_message_editor(temporary_buffer, None, project.clone(), true, window, cx)
});
- commit_editor.update(cx, |editor, cx| {
- editor.clear(window, cx);
- });
-
let scroll_handle = UniformListScrollHandle::new();
let mut was_ai_enabled = AgentSettings::get_global(cx).enabled(cx);
@@ -987,9 +1040,10 @@ impl GitPanel {
diff_stat_total: DiffStat::default(),
pending_commit: None,
pending_remote_operation: None,
- amend_pending: false,
- original_commit_message: None,
- signoff_enabled: false,
+ amend_pending,
+ original_commit_message,
+ pending_commit_message_restores,
+ signoff_enabled,
pending_serialization: Task::ready(()),
single_staged_entry: None,
single_tracked_entry: None,
@@ -1001,6 +1055,7 @@ impl GitPanel {
tracked_count: 0,
tracked_staged_count: 0,
update_visible_entries_task: Task::ready(()),
+ reopen_commit_buffer_task: Task::ready(()),
show_placeholders: false,
local_committer: None,
local_committer_task: None,
@@ -1016,6 +1071,7 @@ impl GitPanel {
commit_history_shas: None,
focused_history_entry: None,
history_keyboard_nav: false,
+ _commit_message_buffer_subscription: None,
_repo_subscriptions: Vec::new(),
_settings_subscription,
git_access: GitAccess::Yes,
@@ -1101,8 +1157,8 @@ impl GitPanel {
}
fn serialize(&mut self, cx: &mut Context<Self>) {
- let amend_pending = self.amend_pending;
let signoff_enabled = self.signoff_enabled;
+ let commit_messages = self.serialized_commit_messages(cx);
let kvp = KeyValueStore::global(cx);
self.pending_serialization = cx.spawn(async move |git_panel, cx| {
@@ -1127,8 +1183,8 @@ impl GitPanel {
kvp.write_kvp(
serialization_key,
serde_json::to_string(&SerializedGitPanel {
- amend_pending,
signoff_enabled,
+ commit_messages,
})?,
)
.await?;
@@ -1140,6 +1196,62 @@ impl GitPanel {
});
}
+ fn serialized_commit_messages(&self, cx: &App) -> BTreeMap<String, SerializedCommitMessage> {
+ let active_work_directory_abs_path = self.active_repository.as_ref().map(|repository| {
+ repository
+ .read(cx)
+ .work_directory_abs_path
+ .to_string_lossy()
+ .into_owned()
+ });
+ let git_store = self.project.read(cx).git_store().clone();
+ let mut commit_messages = self.pending_commit_message_restores.clone();
+ for repository in git_store.read(cx).repositories().values() {
+ let repository = repository.read(cx);
+ let work_directory_abs_path = repository
+ .work_directory_abs_path
+ .to_string_lossy()
+ .into_owned();
+ if active_work_directory_abs_path.as_deref() == Some(work_directory_abs_path.as_str()) {
+ continue;
+ }
+ if let Some(buffer) = repository.commit_message_buffer() {
+ let text = buffer.read(cx).text();
+ if text.trim().is_empty() {
+ commit_messages.remove(&work_directory_abs_path);
+ } else {
+ commit_messages.insert(
+ work_directory_abs_path,
+ SerializedCommitMessage {
+ message: Some(text),
+ original_message: None,
+ amend_pending: false,
+ },
+ );
+ }
+ }
+ }
+ if let Some(work_directory_abs_path) = active_work_directory_abs_path {
+ let text = self.commit_message_buffer(cx).read(cx).text();
+ let message = (!text.trim().is_empty()).then_some(text);
+ let original_message = self.original_commit_message.clone();
+ let amend_pending = self.amend_pending;
+ if message.is_some() || original_message.is_some() || amend_pending {
+ commit_messages.insert(
+ work_directory_abs_path,
+ SerializedCommitMessage {
+ message,
+ original_message,
+ amend_pending,
+ },
+ );
+ } else {
+ commit_messages.remove(&work_directory_abs_path);
+ }
+ }
+ commit_messages
+ }
+
pub(crate) fn set_modal_open(&mut self, open: bool, cx: &mut Context<Self>) {
self.modal_open = open;
cx.notify();
@@ -2579,6 +2691,7 @@ impl GitPanel {
this.commit_editor
.update(cx, |editor, cx| editor.clear(window, cx));
this.original_commit_message = None;
+ this.serialize(cx);
}
}
Err(e) => this.show_error_toast("commit", e, cx),
@@ -3866,6 +3979,18 @@ impl GitPanel {
fn schedule_update(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let handle = cx.entity().downgrade();
+ let new_active_repository = self.project.read(cx).active_repository(cx);
+ let active_repository_changed = self.active_repository.as_ref().map(Entity::entity_id)
+ != new_active_repository.as_ref().map(Entity::entity_id);
+ if active_repository_changed && self.amend_pending {
+ // Leaving a repository with a pending amend: undo it so the amend
+ // state doesn't carry over to the newly active repository. The
+ // commit editor still holds the previous repository's buffer here
+ // (`reopen_commit_buffer` swaps it asynchronously below), so this
+ // restores the pre-amend draft into that repository's buffer.
+ self.set_amend_pending(false, cx);
+ }
+ self.active_repository = new_active_repository;
self.reopen_commit_buffer(window, cx);
self.preload_commit_history(cx);
if self.active_tab == GitPanelTab::History {
@@ -3885,8 +4010,14 @@ impl GitPanel {
fn reopen_commit_buffer(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let Some(active_repo) = self.active_repository.as_ref() else {
+ self.reopen_commit_buffer_task = Task::ready(());
return;
};
+ let active_repository_abs_path = active_repo
+ .read(cx)
+ .work_directory_abs_path
+ .to_string_lossy()
+ .into_owned();
let load_buffer = active_repo.update(cx, |active_repo, cx| {
let project = self.project.read(cx);
active_repo.open_commit_buffer(
@@ -3897,50 +4028,79 @@ impl GitPanel {
});
let load_template = self.load_commit_template(cx);
- cx.spawn_in(window, async move |git_panel, cx| {
- let buffer = load_buffer.await?;
- let template = load_template.await?;
+ self.reopen_commit_buffer_task = cx.spawn_in(window, async move |git_panel, cx| {
+ let result = async {
+ let buffer = load_buffer.await?;
+ let template = load_template.await?;
+
+ git_panel.update_in(cx, move |git_panel, window, cx| {
+ git_panel.commit_template = template;
+ let restored_commit_message = git_panel
+ .pending_commit_message_restores
+ .remove(&active_repository_abs_path);
+ if let Some(restored_commit_message) = restored_commit_message {
+ git_panel.amend_pending = restored_commit_message.amend_pending;
+ git_panel.original_commit_message =
+ restored_commit_message.original_message;
+ cx.notify();
+ if let Some(message) = restored_commit_message.message
+ && buffer.read(cx).text().trim().is_empty()
+ {
+ buffer.update(cx, |buffer, cx| {
+ let start = buffer.anchor_before(0);
+ let end = buffer.anchor_after(buffer.len());
+ buffer.edit([(start..end, message)], None, cx);
+ });
+ }
+ }
+ if buffer.read(cx).text().trim().is_empty() {
+ let template_text = git_panel
+ .commit_template
+ .as_ref()
+ .map(|t| t.template.clone())
+ .unwrap_or_default();
+ if !template_text.is_empty() {
+ buffer.update(cx, |buffer, cx| {
+ let start = buffer.anchor_before(0);
+ let end = buffer.anchor_after(buffer.len());
+ buffer.edit([(start..end, template_text)], None, cx);
+ });
+ }
+ }
- git_panel.update_in(cx, |git_panel, window, cx| {
- git_panel.commit_template = template;
- if buffer.read(cx).text().trim().is_empty() {
- let template_text = git_panel
- .commit_template
+ if git_panel
+ .commit_editor
+ .read(cx)
+ .buffer()
+ .read(cx)
+ .as_singleton()
.as_ref()
- .map(|t| t.template.clone())
- .unwrap_or_default();
- if !template_text.is_empty() {
- buffer.update(cx, |buffer, cx| {
- let start = buffer.anchor_before(0);
- let end = buffer.anchor_after(buffer.len());
- buffer.edit([(start..end, template_text)], None, cx);
+ != Some(&buffer)
+ {
+ git_panel.commit_editor = cx.new(|cx| {
+ commit_message_editor(
+ buffer.clone(),
+ git_panel.suggest_commit_message(cx).map(SharedString::from),
+ git_panel.project.clone(),
+ true,
+ window,
+ cx,
+ )
});
}
- }
- if git_panel
- .commit_editor
- .read(cx)
- .buffer()
- .read(cx)
- .as_singleton()
- .as_ref()
- != Some(&buffer)
- {
- git_panel.commit_editor = cx.new(|cx| {
- commit_message_editor(
- buffer,
- git_panel.suggest_commit_message(cx).map(SharedString::from),
- git_panel.project.clone(),
- true,
- window,
- cx,
- )
- });
- }
- })
- })
- .detach_and_log_err(cx);
+ git_panel._commit_message_buffer_subscription =
+ Some(cx.subscribe(&buffer, |this, _, event, cx| {
+ if matches!(event, BufferEvent::Edited { .. }) {
+ this.serialize(cx);
+ }
+ }));
+ })?;
+ anyhow::Ok(())
+ }
+ .await;
+ result.log_err();
+ });
}
fn update_visible_entries(&mut self, window: &mut Window, cx: &mut Context<Self>) {
@@ -6939,17 +7099,7 @@ impl GitPanel {
};
workspace.update_in(&mut cx, |workspace, window, cx| {
- let panel = GitPanel::new(workspace, window, cx);
-
- if let Some(serialized_panel) = serialized_panel {
- panel.update(cx, |panel, cx| {
- panel.amend_pending = serialized_panel.amend_pending;
- panel.signoff_enabled = serialized_panel.signoff_enabled;
- cx.notify();
- })
- }
-
- panel
+ GitPanel::new_with_serialized_panel(workspace, serialized_panel, window, cx)
})
}
@@ -7885,6 +8035,18 @@ mod tests {
});
}
+ fn register_git_commit_language(project: &Entity<Project>, cx: &mut VisualTestContext) {
+ project.read_with(cx, |project, _| {
+ project.languages().add(Arc::new(language::Language::new(
+ language::LanguageConfig {
+ name: "Git Commit".into(),
+ ..Default::default()
+ },
+ None,
+ )));
+ });
+ }
+
fn entry_index_for_repo_path(panel: &GitPanel, repo_path: &RepoPath) -> Option<usize> {
panel.entries.iter().position(|entry| {
entry
@@ -8879,6 +9041,283 @@ mod tests {
});
}
+ #[gpui::test]
+ async fn test_commit_message_restored_after_reconnect(cx: &mut TestAppContext) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.background_executor.clone());
+ fs.insert_tree(
+ "/root",
+ json!({
+ "project-a": {
+ ".git": {},
+ "src": {
+ "main.rs": "fn main() {}"
+ }
+ },
+ "project-b": {
+ ".git": {},
+ "src": {
+ "main.rs": "fn main() {}"
+ }
+ }
+ }),
+ )
+ .await;
+
+ fs.set_status_for_repo(
+ Path::new(path!("/root/project-a/.git")),
+ &[("src/main.rs", StatusCode::Modified.worktree())],
+ );
+ fs.set_status_for_repo(
+ Path::new(path!("/root/project-b/.git")),
+ &[("src/main.rs", StatusCode::Modified.worktree())],
+ );
+
+ let project = Project::test(
+ fs.clone(),
+ [
+ Path::new(path!("/root/project-a")),
+ Path::new(path!("/root/project-b")),
+ ],
+ cx,
+ )
+ .await;
+ let (repository_a, repository_b) = project.read_with(cx, |project, cx| {
+ let git_store = project.git_store().clone();
+ let mut repository_a = None;
+ let mut repository_b = None;
+ for repository in git_store.read(cx).repositories().values() {
+ let work_directory_abs_path = &repository.read(cx).work_directory_abs_path;
+ if work_directory_abs_path.as_ref() == Path::new(path!("/root/project-a")) {
+ repository_a = Some(repository.clone());
+ } else if work_directory_abs_path.as_ref() == Path::new(path!("/root/project-b")) {
+ repository_b = Some(repository.clone());
+ }
+ }
+ (
+ repository_a.expect("should have repository for project-a"),
+ repository_b.expect("should have repository for project-b"),
+ )
+ });
+ repository_a.update(cx, |repository, cx| repository.set_as_active_repository(cx));
+
+ 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);
+
+ register_git_commit_language(&project, cx);
+ let panel = workspace.update_in(cx, GitPanel::new);
+ cx.run_until_parked();
+
+ let message_a = "Restore repository A message";
+ panel.update(cx, |panel, cx| {
+ panel.commit_message_buffer(cx).update(cx, |buffer, cx| {
+ let start = buffer.anchor_before(0);
+ let end = buffer.anchor_after(buffer.len());
+ buffer.edit([(start..end, message_a)], None, cx);
+ });
+ });
+
+ repository_b.update(cx, |repository, cx| repository.set_as_active_repository(cx));
+ cx.run_until_parked();
+
+ let message_b = "Restore repository B message";
+ let serialized_panel = panel.update(cx, |panel, cx| {
+ panel.commit_message_buffer(cx).update(cx, |buffer, cx| {
+ let start = buffer.anchor_before(0);
+ let end = buffer.anchor_after(buffer.len());
+ buffer.edit([(start..end, message_b)], None, cx);
+ });
+
+ SerializedGitPanel {
+ signoff_enabled: false,
+ commit_messages: panel.serialized_commit_messages(cx),
+ }
+ });
+
+ for repository in [&repository_a, &repository_b] {
+ let buffer = repository.read_with(cx, |repository, _| {
+ repository
+ .commit_message_buffer()
+ .expect("repository commit message buffer should be open")
+ .clone()
+ });
+ buffer.update(cx, |buffer, cx| {
+ let start = buffer.anchor_before(0);
+ let end = buffer.anchor_after(buffer.len());
+ buffer.edit([(start..end, "")], None, cx);
+ });
+ }
+
+ let restored_panel = workspace.update_in(cx, |workspace, window, cx| {
+ GitPanel::new_with_serialized_panel(workspace, Some(serialized_panel), window, cx)
+ });
+ cx.run_until_parked();
+
+ restored_panel.read_with(cx, |panel, cx| {
+ assert_eq!(panel.commit_message_buffer(cx).read(cx).text(), message_b);
+ });
+
+ repository_a.update(cx, |repository, cx| repository.set_as_active_repository(cx));
+ cx.run_until_parked();
+
+ restored_panel.read_with(cx, |panel, cx| {
+ assert_eq!(panel.commit_message_buffer(cx).read(cx).text(), message_a);
+ });
+
+ restored_panel.update(cx, |panel, cx| {
+ panel.commit_message_buffer(cx).update(cx, |buffer, cx| {
+ let start = buffer.anchor_before(0);
+ let end = buffer.anchor_after(buffer.len());
+ buffer.edit([(start..end, "")], None, cx);
+ });
+ });
+
+ let mismatched_serialized_panel = SerializedGitPanel {
+ signoff_enabled: false,
+ commit_messages: BTreeMap::from_iter([(
+ path!("/root/other-project").to_string(),
+ SerializedCommitMessage {
+ message: Some(message_a.to_string()),
+ original_message: None,
+ ..Default::default()
+ },
+ )]),
+ };
+ let mismatched_panel = workspace.update_in(cx, |workspace, window, cx| {
+ GitPanel::new_with_serialized_panel(
+ workspace,
+ Some(mismatched_serialized_panel),
+ window,
+ cx,
+ )
+ });
+ cx.run_until_parked();
+
+ mismatched_panel.read_with(cx, |panel, cx| {
+ // The draft is not restored because the serialized work directory
+ // does not match the active repository, so it cannot leak across
+ // repositories.
+ assert_eq!(panel.commit_message_buffer(cx).read(cx).text(), "");
+ });
+ }
+
+ #[gpui::test]
+ async fn test_amend_state_is_per_repository(cx: &mut TestAppContext) {
+ init_test(cx);
+ let fs = FakeFs::new(cx.background_executor.clone());
+ fs.insert_tree(
+ "/root",
+ json!({
+ "project-a": {
+ ".git": {},
+ "src": {
+ "main.rs": "fn main() {}"
+ }
+ },
+ "project-b": {
+ ".git": {},
+ "src": {
+ "main.rs": "fn main() {}"
+ }
+ }
+ }),
+ )
+ .await;
+
+ fs.set_status_for_repo(
+ Path::new(path!("/root/project-a/.git")),
+ &[("src/main.rs", StatusCode::Modified.worktree())],
+ );
+ fs.set_status_for_repo(
+ Path::new(path!("/root/project-b/.git")),
+ &[("src/main.rs", StatusCode::Modified.worktree())],
+ );
+
+ let project = Project::test(
+ fs.clone(),
+ [
+ Path::new(path!("/root/project-a")),
+ Path::new(path!("/root/project-b")),
+ ],
+ cx,
+ )
+ .await;
+ let (repository_a, repository_b) = project.read_with(cx, |project, cx| {
+ let git_store = project.git_store().clone();
+ let mut repository_a = None;
+ let mut repository_b = None;
+ for repository in git_store.read(cx).repositories().values() {
+ let work_directory_abs_path = &repository.read(cx).work_directory_abs_path;
+ if work_directory_abs_path.as_ref() == Path::new(path!("/root/project-a")) {
+ repository_a = Some(repository.clone());
+ } else if work_directory_abs_path.as_ref() == Path::new(path!("/root/project-b")) {
+ repository_b = Some(repository.clone());
+ }
+ }
+ (
+ repository_a.expect("should have repository for project-a"),
+ repository_b.expect("should have repository for project-b"),
+ )
+ });
+ repository_a.update(cx, |repository, cx| repository.set_as_active_repository(cx));
+
+ 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);
+
+ register_git_commit_language(&project, cx);
+ let panel = workspace.update_in(cx, GitPanel::new);
+ cx.run_until_parked();
+
+ // Enter an amend on repository A, then simulate the amend flow loading
+ // the last commit message into the editor.
+ panel.update(cx, |panel, cx| {
+ panel.commit_message_buffer(cx).update(cx, |buffer, cx| {
+ let start = buffer.anchor_before(0);
+ let end = buffer.anchor_after(buffer.len());
+ buffer.edit([(start..end, "Draft for A")], None, cx);
+ });
+ panel.set_amend_pending(true, cx);
+ panel.commit_message_buffer(cx).update(cx, |buffer, cx| {
+ let start = buffer.anchor_before(0);
+ let end = buffer.anchor_after(buffer.len());
+ buffer.edit([(start..end, "Amended message")], None, cx);
+ });
+ assert!(panel.amend_pending());
+ });
+
+ // Switching the active repository away exits the amend state instead of
+ // carrying it over to repository B.
+ repository_b.update(cx, |repository, cx| repository.set_as_active_repository(cx));
+ cx.run_until_parked();
+
+ panel.update(cx, |panel, cx| {
+ assert!(!panel.amend_pending());
+ // Only the active repository may serialize a pending amend, and we
+ // just left repository A's amend, so nothing is left pending.
+ let serialized = panel.serialized_commit_messages(cx);
+ assert!(serialized.values().all(|message| !message.amend_pending));
+ });
+
+ // Repository A's pre-amend draft is restored, discarding the amend edit.
+ let buffer_a = repository_a.read_with(cx, |repository, _| {
+ repository
+ .commit_message_buffer()
+ .expect("repository commit message buffer should be open")
+ .clone()
+ });
+ buffer_a.read_with(cx, |buffer, _| {
+ assert_eq!(buffer.text(), "Draft for A");
+ });
+ }
+
#[gpui::test]
async fn test_amend(cx: &mut TestAppContext) {
init_test(cx);
diff --git a/crates/project/src/git_store.rs b/crates/project/src/git_store.rs
index 1266f6f87ea..1056e45f3bb 100644
--- a/crates/project/src/git_store.rs
+++ b/crates/project/src/git_store.rs
@@ -5421,6 +5421,10 @@ impl Repository {
.starts_with(&self.snapshot.work_directory_abs_path)
}
+ pub fn commit_message_buffer(&self) -> Option<&Entity<Buffer>> {
+ self.commit_message_buffer.as_ref()
+ }
+
pub fn open_commit_buffer(
&mut self,
languages: Option<Arc<LanguageRegistry>>,