1use crate::askpass_modal::AskPassModal;
2use crate::commit_modal::CommitModal;
3use crate::commit_tooltip::CommitTooltip;
4use crate::commit_view::CommitView;
5use crate::git_panel_settings::StatusStyle;
6use crate::project_diff::{self, Diff, ProjectDiff};
7use crate::remote_output::{self, RemoteAction, SuccessMessage};
8use crate::{branch_picker, picker_prompt, render_remote_button};
9use crate::{
10 git_panel_settings::GitPanelSettings, git_status_icon, repository_selector::RepositorySelector,
11};
12use agent_settings::AgentSettings;
13use anyhow::Context as _;
14use askpass::AskPassDelegate;
15use client::DisableAiSettings;
16use db::kvp::KEY_VALUE_STORE;
17use editor::{
18 Editor, EditorElement, EditorMode, EditorSettings, MultiBuffer, ShowScrollbar,
19 scroll::ScrollbarAutoHide,
20};
21use futures::StreamExt as _;
22use git::blame::ParsedCommitMessage;
23use git::repository::{
24 Branch, CommitDetails, CommitOptions, CommitSummary, DiffType, FetchOptions, GitCommitter,
25 PushOptions, Remote, RemoteCommandOutput, ResetMode, Upstream, UpstreamTracking,
26 UpstreamTrackingStatus, get_git_committer,
27};
28use git::status::StageStatus;
29use git::{Amend, Signoff, ToggleStaged, repository::RepoPath, status::FileStatus};
30use git::{ExpandCommitEditor, RestoreTrackedFiles, StageAll, TrashUntrackedFiles, UnstageAll};
31use gpui::{
32 Action, Animation, AnimationExt as _, AsyncApp, AsyncWindowContext, Axis, ClickEvent, Corner,
33 DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, KeyContext,
34 ListHorizontalSizingBehavior, ListSizingBehavior, MouseButton, MouseDownEvent, Point,
35 PromptLevel, ScrollStrategy, Subscription, Task, Transformation, UniformListScrollHandle,
36 WeakEntity, actions, anchored, deferred, percentage, uniform_list,
37};
38use itertools::Itertools;
39use language::{Buffer, File};
40use language_model::{
41 ConfiguredModel, LanguageModel, LanguageModelRegistry, LanguageModelRequest,
42 LanguageModelRequestMessage, Role,
43};
44use menu::{Confirm, SecondaryConfirm, SelectFirst, SelectLast, SelectNext, SelectPrevious};
45use multi_buffer::ExcerptInfo;
46use notifications::status_toast::{StatusToast, ToastIcon};
47use panel::{
48 PanelHeader, panel_button, panel_editor_container, panel_editor_style, panel_filled_button,
49 panel_icon_button,
50};
51use project::git_store::{RepositoryEvent, RepositoryId};
52use project::{
53 Fs, Project, ProjectPath,
54 git_store::{GitStoreEvent, Repository},
55};
56use serde::{Deserialize, Serialize};
57use settings::{Settings, SettingsStore};
58use std::future::Future;
59use std::ops::Range;
60use std::path::{Path, PathBuf};
61use std::{collections::HashSet, sync::Arc, time::Duration, usize};
62use strum::{IntoEnumIterator, VariantNames};
63use time::OffsetDateTime;
64use ui::{
65 Checkbox, ContextMenu, ElevationIndex, IconPosition, Label, LabelSize, PopoverMenu, Scrollbar,
66 ScrollbarState, SplitButton, Tooltip, prelude::*,
67};
68use util::{ResultExt, TryFutureExt, maybe};
69use workspace::SERIALIZATION_THROTTLE_TIME;
70
71use workspace::{
72 Workspace,
73 dock::{DockPosition, Panel, PanelEvent},
74 notifications::{DetachAndPromptErr, ErrorMessagePrompt, NotificationId},
75};
76use zed_llm_client::CompletionIntent;
77
78actions!(
79 git_panel,
80 [
81 /// Closes the git panel.
82 Close,
83 /// Toggles focus on the git panel.
84 ToggleFocus,
85 /// Opens the git panel menu.
86 OpenMenu,
87 /// Focuses on the commit message editor.
88 FocusEditor,
89 /// Focuses on the changes list.
90 FocusChanges,
91 /// Toggles automatic co-author suggestions.
92 ToggleFillCoAuthors,
93 ]
94);
95
96fn prompt<T>(
97 msg: &str,
98 detail: Option<&str>,
99 window: &mut Window,
100 cx: &mut App,
101) -> Task<anyhow::Result<T>>
102where
103 T: IntoEnumIterator + VariantNames + 'static,
104{
105 let rx = window.prompt(PromptLevel::Info, msg, detail, &T::VARIANTS, cx);
106 cx.spawn(async move |_| Ok(T::iter().nth(rx.await?).unwrap()))
107}
108
109#[derive(strum::EnumIter, strum::VariantNames)]
110#[strum(serialize_all = "title_case")]
111enum TrashCancel {
112 Trash,
113 Cancel,
114}
115
116struct GitMenuState {
117 has_tracked_changes: bool,
118 has_staged_changes: bool,
119 has_unstaged_changes: bool,
120 has_new_changes: bool,
121}
122
123fn git_panel_context_menu(
124 focus_handle: FocusHandle,
125 state: GitMenuState,
126 window: &mut Window,
127 cx: &mut App,
128) -> Entity<ContextMenu> {
129 ContextMenu::build(window, cx, move |context_menu, _, _| {
130 context_menu
131 .context(focus_handle)
132 .action_disabled_when(
133 !state.has_unstaged_changes,
134 "Stage All",
135 StageAll.boxed_clone(),
136 )
137 .action_disabled_when(
138 !state.has_staged_changes,
139 "Unstage All",
140 UnstageAll.boxed_clone(),
141 )
142 .separator()
143 .action("Open Diff", project_diff::Diff.boxed_clone())
144 .separator()
145 .action_disabled_when(
146 !state.has_tracked_changes,
147 "Discard Tracked Changes",
148 RestoreTrackedFiles.boxed_clone(),
149 )
150 .action_disabled_when(
151 !state.has_new_changes,
152 "Trash Untracked Files",
153 TrashUntrackedFiles.boxed_clone(),
154 )
155 })
156}
157
158const GIT_PANEL_KEY: &str = "GitPanel";
159
160const UPDATE_DEBOUNCE: Duration = Duration::from_millis(50);
161
162pub fn register(workspace: &mut Workspace) {
163 workspace.register_action(|workspace, _: &ToggleFocus, window, cx| {
164 workspace.toggle_panel_focus::<GitPanel>(window, cx);
165 });
166 workspace.register_action(|workspace, _: &ExpandCommitEditor, window, cx| {
167 CommitModal::toggle(workspace, None, window, cx)
168 });
169}
170
171#[derive(Debug, Clone)]
172pub enum Event {
173 Focus,
174}
175
176#[derive(Serialize, Deserialize)]
177struct SerializedGitPanel {
178 width: Option<Pixels>,
179 #[serde(default)]
180 amend_pending: bool,
181 #[serde(default)]
182 signoff_enabled: bool,
183}
184
185#[derive(Debug, PartialEq, Eq, Clone, Copy)]
186enum Section {
187 Conflict,
188 Tracked,
189 New,
190}
191
192#[derive(Debug, PartialEq, Eq, Clone)]
193struct GitHeaderEntry {
194 header: Section,
195}
196
197impl GitHeaderEntry {
198 pub fn contains(&self, status_entry: &GitStatusEntry, repo: &Repository) -> bool {
199 let this = &self.header;
200 let status = status_entry.status;
201 match this {
202 Section::Conflict => {
203 repo.had_conflict_on_last_merge_head_change(&status_entry.repo_path)
204 }
205 Section::Tracked => !status.is_created(),
206 Section::New => status.is_created(),
207 }
208 }
209 pub fn title(&self) -> &'static str {
210 match self.header {
211 Section::Conflict => "Conflicts",
212 Section::Tracked => "Tracked",
213 Section::New => "Untracked",
214 }
215 }
216}
217
218#[derive(Debug, PartialEq, Eq, Clone)]
219enum GitListEntry {
220 Status(GitStatusEntry),
221 Header(GitHeaderEntry),
222}
223
224impl GitListEntry {
225 fn status_entry(&self) -> Option<&GitStatusEntry> {
226 match self {
227 GitListEntry::Status(entry) => Some(entry),
228 _ => None,
229 }
230 }
231}
232
233#[derive(Debug, PartialEq, Eq, Clone)]
234pub struct GitStatusEntry {
235 pub(crate) repo_path: RepoPath,
236 pub(crate) abs_path: PathBuf,
237 pub(crate) status: FileStatus,
238 pub(crate) staging: StageStatus,
239}
240
241impl GitStatusEntry {
242 fn display_name(&self) -> String {
243 self.repo_path
244 .file_name()
245 .map(|name| name.to_string_lossy().into_owned())
246 .unwrap_or_else(|| self.repo_path.to_string_lossy().into_owned())
247 }
248
249 fn parent_dir(&self) -> Option<String> {
250 self.repo_path
251 .parent()
252 .map(|parent| parent.to_string_lossy().into_owned())
253 }
254}
255
256#[derive(Clone, Copy, Debug, PartialEq, Eq)]
257enum TargetStatus {
258 Staged,
259 Unstaged,
260 Reverted,
261 Unchanged,
262}
263
264struct PendingOperation {
265 finished: bool,
266 target_status: TargetStatus,
267 entries: Vec<GitStatusEntry>,
268 op_id: usize,
269}
270
271// computed state related to how to render scrollbars
272// one per axis
273// on render we just read this off the panel
274// we update it when
275// - settings change
276// - on focus in, on focus out, on hover, etc.
277#[derive(Debug)]
278struct ScrollbarProperties {
279 axis: Axis,
280 show_scrollbar: bool,
281 show_track: bool,
282 auto_hide: bool,
283 hide_task: Option<Task<()>>,
284 state: ScrollbarState,
285}
286
287impl ScrollbarProperties {
288 // Shows the scrollbar and cancels any pending hide task
289 fn show(&mut self, cx: &mut Context<GitPanel>) {
290 if !self.auto_hide {
291 return;
292 }
293 self.show_scrollbar = true;
294 self.hide_task.take();
295 cx.notify();
296 }
297
298 fn hide(&mut self, window: &mut Window, cx: &mut Context<GitPanel>) {
299 const SCROLLBAR_SHOW_INTERVAL: Duration = Duration::from_secs(1);
300
301 if !self.auto_hide {
302 return;
303 }
304
305 let axis = self.axis;
306 self.hide_task = Some(cx.spawn_in(window, async move |panel, cx| {
307 cx.background_executor()
308 .timer(SCROLLBAR_SHOW_INTERVAL)
309 .await;
310
311 if let Some(panel) = panel.upgrade() {
312 panel
313 .update(cx, |panel, cx| {
314 match axis {
315 Axis::Vertical => panel.vertical_scrollbar.show_scrollbar = false,
316 Axis::Horizontal => panel.horizontal_scrollbar.show_scrollbar = false,
317 }
318 cx.notify();
319 })
320 .log_err();
321 }
322 }));
323 }
324}
325
326pub struct GitPanel {
327 pub(crate) active_repository: Option<Entity<Repository>>,
328 pub(crate) commit_editor: Entity<Editor>,
329 conflicted_count: usize,
330 conflicted_staged_count: usize,
331 add_coauthors: bool,
332 generate_commit_message_task: Option<Task<Option<()>>>,
333 entries: Vec<GitListEntry>,
334 single_staged_entry: Option<GitStatusEntry>,
335 single_tracked_entry: Option<GitStatusEntry>,
336 focus_handle: FocusHandle,
337 fs: Arc<dyn Fs>,
338 horizontal_scrollbar: ScrollbarProperties,
339 vertical_scrollbar: ScrollbarProperties,
340 new_count: usize,
341 entry_count: usize,
342 new_staged_count: usize,
343 pending: Vec<PendingOperation>,
344 pending_commit: Option<Task<()>>,
345 amend_pending: bool,
346 signoff_enabled: bool,
347 pending_serialization: Task<()>,
348 pub(crate) project: Entity<Project>,
349 scroll_handle: UniformListScrollHandle,
350 max_width_item_index: Option<usize>,
351 selected_entry: Option<usize>,
352 marked_entries: Vec<usize>,
353 tracked_count: usize,
354 tracked_staged_count: usize,
355 update_visible_entries_task: Task<()>,
356 width: Option<Pixels>,
357 workspace: WeakEntity<Workspace>,
358 context_menu: Option<(Entity<ContextMenu>, Point<Pixels>, Subscription)>,
359 modal_open: bool,
360 show_placeholders: bool,
361 local_committer: Option<GitCommitter>,
362 local_committer_task: Option<Task<()>>,
363 bulk_staging: Option<BulkStaging>,
364 _settings_subscription: Subscription,
365}
366
367#[derive(Clone, Debug, PartialEq, Eq)]
368struct BulkStaging {
369 repo_id: RepositoryId,
370 anchor: RepoPath,
371}
372
373const MAX_PANEL_EDITOR_LINES: usize = 6;
374
375pub(crate) fn commit_message_editor(
376 commit_message_buffer: Entity<Buffer>,
377 placeholder: Option<SharedString>,
378 project: Entity<Project>,
379 in_panel: bool,
380 window: &mut Window,
381 cx: &mut Context<Editor>,
382) -> Editor {
383 let buffer = cx.new(|cx| MultiBuffer::singleton(commit_message_buffer, cx));
384 let max_lines = if in_panel { MAX_PANEL_EDITOR_LINES } else { 18 };
385 let mut commit_editor = Editor::new(
386 EditorMode::AutoHeight {
387 min_lines: 1,
388 max_lines: Some(max_lines),
389 },
390 buffer,
391 None,
392 window,
393 cx,
394 );
395 commit_editor.set_collaboration_hub(Box::new(project));
396 commit_editor.set_use_autoclose(false);
397 commit_editor.set_show_gutter(false, cx);
398 commit_editor.set_use_modal_editing(true);
399 commit_editor.set_show_wrap_guides(false, cx);
400 commit_editor.set_show_indent_guides(false, cx);
401 let placeholder = placeholder.unwrap_or("Enter commit message".into());
402 commit_editor.set_placeholder_text(placeholder, cx);
403 commit_editor
404}
405
406impl GitPanel {
407 fn new(
408 workspace: &mut Workspace,
409 window: &mut Window,
410 cx: &mut Context<Workspace>,
411 ) -> Entity<Self> {
412 let project = workspace.project().clone();
413 let app_state = workspace.app_state().clone();
414 let fs = app_state.fs.clone();
415 let git_store = project.read(cx).git_store().clone();
416 let active_repository = project.read(cx).active_repository(cx);
417
418 let git_panel = cx.new(|cx| {
419 let focus_handle = cx.focus_handle();
420 cx.on_focus(&focus_handle, window, Self::focus_in).detach();
421 cx.on_focus_out(&focus_handle, window, |this, _, window, cx| {
422 this.hide_scrollbars(window, cx);
423 })
424 .detach();
425
426 let mut was_sort_by_path = GitPanelSettings::get_global(cx).sort_by_path;
427 cx.observe_global::<SettingsStore>(move |this, cx| {
428 let is_sort_by_path = GitPanelSettings::get_global(cx).sort_by_path;
429 if is_sort_by_path != was_sort_by_path {
430 this.update_visible_entries(cx);
431 }
432 was_sort_by_path = is_sort_by_path
433 })
434 .detach();
435
436 // just to let us render a placeholder editor.
437 // Once the active git repo is set, this buffer will be replaced.
438 let temporary_buffer = cx.new(|cx| Buffer::local("", cx));
439 let commit_editor = cx.new(|cx| {
440 commit_message_editor(temporary_buffer, None, project.clone(), true, window, cx)
441 });
442
443 commit_editor.update(cx, |editor, cx| {
444 editor.clear(window, cx);
445 });
446
447 let scroll_handle = UniformListScrollHandle::new();
448
449 let vertical_scrollbar = ScrollbarProperties {
450 axis: Axis::Vertical,
451 state: ScrollbarState::new(scroll_handle.clone()).parent_entity(&cx.entity()),
452 show_scrollbar: false,
453 show_track: false,
454 auto_hide: false,
455 hide_task: None,
456 };
457
458 let horizontal_scrollbar = ScrollbarProperties {
459 axis: Axis::Horizontal,
460 state: ScrollbarState::new(scroll_handle.clone()).parent_entity(&cx.entity()),
461 show_scrollbar: false,
462 show_track: false,
463 auto_hide: false,
464 hide_task: None,
465 };
466
467 let mut assistant_enabled = AgentSettings::get_global(cx).enabled;
468 let mut was_ai_disabled = DisableAiSettings::get_global(cx).disable_ai;
469 let _settings_subscription = cx.observe_global::<SettingsStore>(move |_, cx| {
470 let is_ai_disabled = DisableAiSettings::get_global(cx).disable_ai;
471 if assistant_enabled != AgentSettings::get_global(cx).enabled
472 || was_ai_disabled != is_ai_disabled
473 {
474 assistant_enabled = AgentSettings::get_global(cx).enabled;
475 was_ai_disabled = is_ai_disabled;
476 cx.notify();
477 }
478 });
479
480 cx.subscribe_in(
481 &git_store,
482 window,
483 move |this, _git_store, event, window, cx| match event {
484 GitStoreEvent::ActiveRepositoryChanged(_) => {
485 this.active_repository = this.project.read(cx).active_repository(cx);
486 this.schedule_update(true, window, cx);
487 }
488 GitStoreEvent::RepositoryUpdated(
489 _,
490 RepositoryEvent::Updated { full_scan, .. },
491 true,
492 ) => {
493 this.schedule_update(*full_scan, window, cx);
494 }
495
496 GitStoreEvent::RepositoryAdded(_) | GitStoreEvent::RepositoryRemoved(_) => {
497 this.schedule_update(false, window, cx);
498 }
499 GitStoreEvent::IndexWriteError(error) => {
500 this.workspace
501 .update(cx, |workspace, cx| {
502 workspace.show_error(error, cx);
503 })
504 .ok();
505 }
506 GitStoreEvent::RepositoryUpdated(_, _, _) => {}
507 GitStoreEvent::JobsUpdated | GitStoreEvent::ConflictsUpdated => {}
508 },
509 )
510 .detach();
511
512 let mut this = Self {
513 active_repository,
514 commit_editor,
515 conflicted_count: 0,
516 conflicted_staged_count: 0,
517 add_coauthors: true,
518 generate_commit_message_task: None,
519 entries: Vec::new(),
520 focus_handle: cx.focus_handle(),
521 fs,
522 new_count: 0,
523 new_staged_count: 0,
524 pending: Vec::new(),
525 pending_commit: None,
526 amend_pending: false,
527 signoff_enabled: false,
528 pending_serialization: Task::ready(()),
529 single_staged_entry: None,
530 single_tracked_entry: None,
531 project,
532 scroll_handle,
533 max_width_item_index: None,
534 selected_entry: None,
535 marked_entries: Vec::new(),
536 tracked_count: 0,
537 tracked_staged_count: 0,
538 update_visible_entries_task: Task::ready(()),
539 width: None,
540 show_placeholders: false,
541 local_committer: None,
542 local_committer_task: None,
543 context_menu: None,
544 workspace: workspace.weak_handle(),
545 modal_open: false,
546 entry_count: 0,
547 horizontal_scrollbar,
548 vertical_scrollbar,
549 bulk_staging: None,
550 _settings_subscription,
551 };
552
553 this.schedule_update(false, window, cx);
554 this
555 });
556
557 git_panel
558 }
559
560 fn hide_scrollbars(&mut self, window: &mut Window, cx: &mut Context<Self>) {
561 self.horizontal_scrollbar.hide(window, cx);
562 self.vertical_scrollbar.hide(window, cx);
563 }
564
565 fn update_scrollbar_properties(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
566 // TODO: This PR should have defined Editor's `scrollbar.axis`
567 // as an Option<ScrollbarAxis>, not a ScrollbarAxes as it would allow you to
568 // `.unwrap_or(EditorSettings::get_global(cx).scrollbar.show)`.
569 //
570 // Once this is fixed we can extend the GitPanelSettings with a `scrollbar.axis`
571 // so we can show each axis based on the settings.
572 //
573 // We should fix this. PR: https://github.com/zed-industries/zed/pull/19495
574
575 let show_setting = GitPanelSettings::get_global(cx)
576 .scrollbar
577 .show
578 .unwrap_or(EditorSettings::get_global(cx).scrollbar.show);
579
580 let scroll_handle = self.scroll_handle.0.borrow();
581
582 let autohide = |show: ShowScrollbar, cx: &mut Context<Self>| match show {
583 ShowScrollbar::Auto => true,
584 ShowScrollbar::System => cx
585 .try_global::<ScrollbarAutoHide>()
586 .map_or_else(|| cx.should_auto_hide_scrollbars(), |autohide| autohide.0),
587 ShowScrollbar::Always => false,
588 ShowScrollbar::Never => false,
589 };
590
591 let longest_item_width = scroll_handle.last_item_size.and_then(|size| {
592 (size.contents.width > size.item.width).then_some(size.contents.width)
593 });
594
595 // is there an item long enough that we should show a horizontal scrollbar?
596 let item_wider_than_container = if let Some(longest_item_width) = longest_item_width {
597 longest_item_width > px(scroll_handle.base_handle.bounds().size.width.0)
598 } else {
599 true
600 };
601
602 let show_horizontal = match (show_setting, item_wider_than_container) {
603 (_, false) => false,
604 (ShowScrollbar::Auto | ShowScrollbar::System | ShowScrollbar::Always, true) => true,
605 (ShowScrollbar::Never, true) => false,
606 };
607
608 let show_vertical = match show_setting {
609 ShowScrollbar::Auto | ShowScrollbar::System | ShowScrollbar::Always => true,
610 ShowScrollbar::Never => false,
611 };
612
613 let show_horizontal_track =
614 show_horizontal && matches!(show_setting, ShowScrollbar::Always);
615
616 // TODO: we probably should hide the scroll track when the list doesn't need to scroll
617 let show_vertical_track = show_vertical && matches!(show_setting, ShowScrollbar::Always);
618
619 self.vertical_scrollbar = ScrollbarProperties {
620 axis: self.vertical_scrollbar.axis,
621 state: self.vertical_scrollbar.state.clone(),
622 show_scrollbar: show_vertical,
623 show_track: show_vertical_track,
624 auto_hide: autohide(show_setting, cx),
625 hide_task: None,
626 };
627
628 self.horizontal_scrollbar = ScrollbarProperties {
629 axis: self.horizontal_scrollbar.axis,
630 state: self.horizontal_scrollbar.state.clone(),
631 show_scrollbar: show_horizontal,
632 show_track: show_horizontal_track,
633 auto_hide: autohide(show_setting, cx),
634 hide_task: None,
635 };
636
637 cx.notify();
638 }
639
640 pub fn entry_by_path(&self, path: &RepoPath, cx: &App) -> Option<usize> {
641 if GitPanelSettings::get_global(cx).sort_by_path {
642 return self
643 .entries
644 .binary_search_by(|entry| entry.status_entry().unwrap().repo_path.cmp(&path))
645 .ok();
646 }
647
648 if self.conflicted_count > 0 {
649 let conflicted_start = 1;
650 if let Ok(ix) = self.entries[conflicted_start..conflicted_start + self.conflicted_count]
651 .binary_search_by(|entry| entry.status_entry().unwrap().repo_path.cmp(&path))
652 {
653 return Some(conflicted_start + ix);
654 }
655 }
656 if self.tracked_count > 0 {
657 let tracked_start = if self.conflicted_count > 0 {
658 1 + self.conflicted_count
659 } else {
660 0
661 } + 1;
662 if let Ok(ix) = self.entries[tracked_start..tracked_start + self.tracked_count]
663 .binary_search_by(|entry| entry.status_entry().unwrap().repo_path.cmp(&path))
664 {
665 return Some(tracked_start + ix);
666 }
667 }
668 if self.new_count > 0 {
669 let untracked_start = if self.conflicted_count > 0 {
670 1 + self.conflicted_count
671 } else {
672 0
673 } + if self.tracked_count > 0 {
674 1 + self.tracked_count
675 } else {
676 0
677 } + 1;
678 if let Ok(ix) = self.entries[untracked_start..untracked_start + self.new_count]
679 .binary_search_by(|entry| entry.status_entry().unwrap().repo_path.cmp(&path))
680 {
681 return Some(untracked_start + ix);
682 }
683 }
684 None
685 }
686
687 pub fn select_entry_by_path(
688 &mut self,
689 path: ProjectPath,
690 _: &mut Window,
691 cx: &mut Context<Self>,
692 ) {
693 let Some(git_repo) = self.active_repository.as_ref() else {
694 return;
695 };
696 let Some(repo_path) = git_repo.read(cx).project_path_to_repo_path(&path, cx) else {
697 return;
698 };
699 let Some(ix) = self.entry_by_path(&repo_path, cx) else {
700 return;
701 };
702 self.selected_entry = Some(ix);
703 cx.notify();
704 }
705
706 fn serialization_key(workspace: &Workspace) -> Option<String> {
707 workspace
708 .database_id()
709 .map(|id| i64::from(id).to_string())
710 .or(workspace.session_id())
711 .map(|id| format!("{}-{:?}", GIT_PANEL_KEY, id))
712 }
713
714 fn serialize(&mut self, cx: &mut Context<Self>) {
715 let width = self.width;
716 let amend_pending = self.amend_pending;
717 let signoff_enabled = self.signoff_enabled;
718
719 self.pending_serialization = cx.spawn(async move |git_panel, cx| {
720 cx.background_executor()
721 .timer(SERIALIZATION_THROTTLE_TIME)
722 .await;
723 let Some(serialization_key) = git_panel
724 .update(cx, |git_panel, cx| {
725 git_panel
726 .workspace
727 .read_with(cx, |workspace, _| Self::serialization_key(workspace))
728 .ok()
729 .flatten()
730 })
731 .ok()
732 .flatten()
733 else {
734 return;
735 };
736 cx.background_spawn(
737 async move {
738 KEY_VALUE_STORE
739 .write_kvp(
740 serialization_key,
741 serde_json::to_string(&SerializedGitPanel {
742 width,
743 amend_pending,
744 signoff_enabled,
745 })?,
746 )
747 .await?;
748 anyhow::Ok(())
749 }
750 .log_err(),
751 )
752 .await;
753 });
754 }
755
756 pub(crate) fn set_modal_open(&mut self, open: bool, cx: &mut Context<Self>) {
757 self.modal_open = open;
758 cx.notify();
759 }
760
761 fn dispatch_context(&self, window: &mut Window, cx: &Context<Self>) -> KeyContext {
762 let mut dispatch_context = KeyContext::new_with_defaults();
763 dispatch_context.add("GitPanel");
764
765 if window
766 .focused(cx)
767 .map_or(false, |focused| self.focus_handle == focused)
768 {
769 dispatch_context.add("menu");
770 dispatch_context.add("ChangesList");
771 }
772
773 if self.commit_editor.read(cx).is_focused(window) {
774 dispatch_context.add("CommitEditor");
775 }
776
777 dispatch_context
778 }
779
780 fn close_panel(&mut self, _: &Close, _window: &mut Window, cx: &mut Context<Self>) {
781 cx.emit(PanelEvent::Close);
782 }
783
784 fn focus_in(&mut self, window: &mut Window, cx: &mut Context<Self>) {
785 if !self.focus_handle.contains_focused(window, cx) {
786 cx.emit(Event::Focus);
787 }
788 }
789
790 fn scroll_to_selected_entry(&mut self, cx: &mut Context<Self>) {
791 if let Some(selected_entry) = self.selected_entry {
792 self.scroll_handle
793 .scroll_to_item(selected_entry, ScrollStrategy::Center);
794 }
795
796 cx.notify();
797 }
798
799 fn select_first(&mut self, _: &SelectFirst, _window: &mut Window, cx: &mut Context<Self>) {
800 if !self.entries.is_empty() {
801 self.selected_entry = Some(1);
802 self.scroll_to_selected_entry(cx);
803 }
804 }
805
806 fn select_previous(
807 &mut self,
808 _: &SelectPrevious,
809 _window: &mut Window,
810 cx: &mut Context<Self>,
811 ) {
812 let item_count = self.entries.len();
813 if item_count == 0 {
814 return;
815 }
816
817 if let Some(selected_entry) = self.selected_entry {
818 let new_selected_entry = if selected_entry > 0 {
819 selected_entry - 1
820 } else {
821 selected_entry
822 };
823
824 if matches!(
825 self.entries.get(new_selected_entry),
826 Some(GitListEntry::Header(..))
827 ) {
828 if new_selected_entry > 0 {
829 self.selected_entry = Some(new_selected_entry - 1)
830 }
831 } else {
832 self.selected_entry = Some(new_selected_entry);
833 }
834
835 self.scroll_to_selected_entry(cx);
836 }
837
838 cx.notify();
839 }
840
841 fn select_next(&mut self, _: &SelectNext, _window: &mut Window, cx: &mut Context<Self>) {
842 let item_count = self.entries.len();
843 if item_count == 0 {
844 return;
845 }
846
847 if let Some(selected_entry) = self.selected_entry {
848 let new_selected_entry = if selected_entry < item_count - 1 {
849 selected_entry + 1
850 } else {
851 selected_entry
852 };
853 if matches!(
854 self.entries.get(new_selected_entry),
855 Some(GitListEntry::Header(..))
856 ) {
857 self.selected_entry = Some(new_selected_entry + 1);
858 } else {
859 self.selected_entry = Some(new_selected_entry);
860 }
861
862 self.scroll_to_selected_entry(cx);
863 }
864
865 cx.notify();
866 }
867
868 fn select_last(&mut self, _: &SelectLast, _window: &mut Window, cx: &mut Context<Self>) {
869 if self.entries.last().is_some() {
870 self.selected_entry = Some(self.entries.len() - 1);
871 self.scroll_to_selected_entry(cx);
872 }
873 }
874
875 fn focus_editor(&mut self, _: &FocusEditor, window: &mut Window, cx: &mut Context<Self>) {
876 self.commit_editor.update(cx, |editor, cx| {
877 window.focus(&editor.focus_handle(cx));
878 });
879 cx.notify();
880 }
881
882 fn select_first_entry_if_none(&mut self, cx: &mut Context<Self>) {
883 let have_entries = self
884 .active_repository
885 .as_ref()
886 .map_or(false, |active_repository| {
887 active_repository.read(cx).status_summary().count > 0
888 });
889 if have_entries && self.selected_entry.is_none() {
890 self.selected_entry = Some(1);
891 self.scroll_to_selected_entry(cx);
892 cx.notify();
893 }
894 }
895
896 fn focus_changes_list(
897 &mut self,
898 _: &FocusChanges,
899 window: &mut Window,
900 cx: &mut Context<Self>,
901 ) {
902 self.select_first_entry_if_none(cx);
903
904 cx.focus_self(window);
905 cx.notify();
906 }
907
908 fn get_selected_entry(&self) -> Option<&GitListEntry> {
909 self.selected_entry.and_then(|i| self.entries.get(i))
910 }
911
912 fn open_diff(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
913 maybe!({
914 let entry = self.entries.get(self.selected_entry?)?.status_entry()?;
915 let workspace = self.workspace.upgrade()?;
916 let git_repo = self.active_repository.as_ref()?;
917
918 if let Some(project_diff) = workspace.read(cx).active_item_as::<ProjectDiff>(cx) {
919 if let Some(project_path) = project_diff.read(cx).active_path(cx) {
920 if Some(&entry.repo_path)
921 == git_repo
922 .read(cx)
923 .project_path_to_repo_path(&project_path, cx)
924 .as_ref()
925 {
926 project_diff.focus_handle(cx).focus(window);
927 project_diff.update(cx, |project_diff, cx| project_diff.autoscroll(cx));
928 return None;
929 }
930 }
931 };
932
933 self.workspace
934 .update(cx, |workspace, cx| {
935 ProjectDiff::deploy_at(workspace, Some(entry.clone()), window, cx);
936 })
937 .ok();
938 self.focus_handle.focus(window);
939
940 Some(())
941 });
942 }
943
944 fn open_file(
945 &mut self,
946 _: &menu::SecondaryConfirm,
947 window: &mut Window,
948 cx: &mut Context<Self>,
949 ) {
950 maybe!({
951 let entry = self.entries.get(self.selected_entry?)?.status_entry()?;
952 let active_repo = self.active_repository.as_ref()?;
953 let path = active_repo
954 .read(cx)
955 .repo_path_to_project_path(&entry.repo_path, cx)?;
956 if entry.status.is_deleted() {
957 return None;
958 }
959
960 self.workspace
961 .update(cx, |workspace, cx| {
962 workspace
963 .open_path_preview(path, None, false, false, true, window, cx)
964 .detach_and_prompt_err("Failed to open file", window, cx, |e, _, _| {
965 Some(format!("{e}"))
966 });
967 })
968 .ok()
969 });
970 }
971
972 fn revert_selected(
973 &mut self,
974 action: &git::RestoreFile,
975 window: &mut Window,
976 cx: &mut Context<Self>,
977 ) {
978 maybe!({
979 let list_entry = self.entries.get(self.selected_entry?)?.clone();
980 let entry = list_entry.status_entry()?.to_owned();
981 let skip_prompt = action.skip_prompt || entry.status.is_created();
982
983 let prompt = if skip_prompt {
984 Task::ready(Ok(0))
985 } else {
986 let prompt = window.prompt(
987 PromptLevel::Warning,
988 &format!(
989 "Are you sure you want to restore {}?",
990 entry
991 .repo_path
992 .file_name()
993 .unwrap_or(entry.repo_path.as_os_str())
994 .to_string_lossy()
995 ),
996 None,
997 &["Restore", "Cancel"],
998 cx,
999 );
1000 cx.background_spawn(prompt)
1001 };
1002
1003 let this = cx.weak_entity();
1004 window
1005 .spawn(cx, async move |cx| {
1006 if prompt.await? != 0 {
1007 return anyhow::Ok(());
1008 }
1009
1010 this.update_in(cx, |this, window, cx| {
1011 this.revert_entry(&entry, window, cx);
1012 })?;
1013
1014 Ok(())
1015 })
1016 .detach();
1017 Some(())
1018 });
1019 }
1020
1021 fn revert_entry(
1022 &mut self,
1023 entry: &GitStatusEntry,
1024 window: &mut Window,
1025 cx: &mut Context<Self>,
1026 ) {
1027 maybe!({
1028 let active_repo = self.active_repository.clone()?;
1029 let path = active_repo
1030 .read(cx)
1031 .repo_path_to_project_path(&entry.repo_path, cx)?;
1032 let workspace = self.workspace.clone();
1033
1034 if entry.status.staging().has_staged() {
1035 self.change_file_stage(false, vec![entry.clone()], cx);
1036 }
1037 let filename = path.path.file_name()?.to_string_lossy();
1038
1039 if !entry.status.is_created() {
1040 self.perform_checkout(vec![entry.clone()], cx);
1041 } else {
1042 let prompt = prompt(&format!("Trash {}?", filename), None, window, cx);
1043 cx.spawn_in(window, async move |_, cx| {
1044 match prompt.await? {
1045 TrashCancel::Trash => {}
1046 TrashCancel::Cancel => return Ok(()),
1047 }
1048 let task = workspace.update(cx, |workspace, cx| {
1049 workspace
1050 .project()
1051 .update(cx, |project, cx| project.delete_file(path, true, cx))
1052 })?;
1053 if let Some(task) = task {
1054 task.await?;
1055 }
1056 Ok(())
1057 })
1058 .detach_and_prompt_err(
1059 "Failed to trash file",
1060 window,
1061 cx,
1062 |e, _, _| Some(format!("{e}")),
1063 );
1064 }
1065 Some(())
1066 });
1067 }
1068
1069 fn perform_checkout(&mut self, entries: Vec<GitStatusEntry>, cx: &mut Context<Self>) {
1070 let workspace = self.workspace.clone();
1071 let Some(active_repository) = self.active_repository.clone() else {
1072 return;
1073 };
1074
1075 let op_id = self.pending.iter().map(|p| p.op_id).max().unwrap_or(0) + 1;
1076 self.pending.push(PendingOperation {
1077 op_id,
1078 target_status: TargetStatus::Reverted,
1079 entries: entries.clone(),
1080 finished: false,
1081 });
1082 self.update_visible_entries(cx);
1083 let task = cx.spawn(async move |_, cx| {
1084 let tasks: Vec<_> = workspace.update(cx, |workspace, cx| {
1085 workspace.project().update(cx, |project, cx| {
1086 entries
1087 .iter()
1088 .filter_map(|entry| {
1089 let path = active_repository
1090 .read(cx)
1091 .repo_path_to_project_path(&entry.repo_path, cx)?;
1092 Some(project.open_buffer(path, cx))
1093 })
1094 .collect()
1095 })
1096 })?;
1097
1098 let buffers = futures::future::join_all(tasks).await;
1099
1100 active_repository
1101 .update(cx, |repo, cx| {
1102 repo.checkout_files(
1103 "HEAD",
1104 entries
1105 .into_iter()
1106 .map(|entries| entries.repo_path)
1107 .collect(),
1108 cx,
1109 )
1110 })?
1111 .await??;
1112
1113 let tasks: Vec<_> = cx.update(|cx| {
1114 buffers
1115 .iter()
1116 .filter_map(|buffer| {
1117 buffer.as_ref().ok()?.update(cx, |buffer, cx| {
1118 buffer.is_dirty().then(|| buffer.reload(cx))
1119 })
1120 })
1121 .collect()
1122 })?;
1123
1124 futures::future::join_all(tasks).await;
1125
1126 Ok(())
1127 });
1128
1129 cx.spawn(async move |this, cx| {
1130 let result = task.await;
1131
1132 this.update(cx, |this, cx| {
1133 for pending in this.pending.iter_mut() {
1134 if pending.op_id == op_id {
1135 pending.finished = true;
1136 if result.is_err() {
1137 pending.target_status = TargetStatus::Unchanged;
1138 this.update_visible_entries(cx);
1139 }
1140 break;
1141 }
1142 }
1143 result
1144 .map_err(|e| {
1145 this.show_error_toast("checkout", e, cx);
1146 })
1147 .ok();
1148 })
1149 .ok();
1150 })
1151 .detach();
1152 }
1153
1154 fn restore_tracked_files(
1155 &mut self,
1156 _: &RestoreTrackedFiles,
1157 window: &mut Window,
1158 cx: &mut Context<Self>,
1159 ) {
1160 let entries = self
1161 .entries
1162 .iter()
1163 .filter_map(|entry| entry.status_entry().cloned())
1164 .filter(|status_entry| !status_entry.status.is_created())
1165 .collect::<Vec<_>>();
1166
1167 match entries.len() {
1168 0 => return,
1169 1 => return self.revert_entry(&entries[0], window, cx),
1170 _ => {}
1171 }
1172 let mut details = entries
1173 .iter()
1174 .filter_map(|entry| entry.repo_path.0.file_name())
1175 .map(|filename| filename.to_string_lossy())
1176 .take(5)
1177 .join("\n");
1178 if entries.len() > 5 {
1179 details.push_str(&format!("\nand {} more…", entries.len() - 5))
1180 }
1181
1182 #[derive(strum::EnumIter, strum::VariantNames)]
1183 #[strum(serialize_all = "title_case")]
1184 enum RestoreCancel {
1185 RestoreTrackedFiles,
1186 Cancel,
1187 }
1188 let prompt = prompt(
1189 "Discard changes to these files?",
1190 Some(&details),
1191 window,
1192 cx,
1193 );
1194 cx.spawn(async move |this, cx| match prompt.await {
1195 Ok(RestoreCancel::RestoreTrackedFiles) => {
1196 this.update(cx, |this, cx| {
1197 this.perform_checkout(entries, cx);
1198 })
1199 .ok();
1200 }
1201 _ => {
1202 return;
1203 }
1204 })
1205 .detach();
1206 }
1207
1208 fn clean_all(&mut self, _: &TrashUntrackedFiles, window: &mut Window, cx: &mut Context<Self>) {
1209 let workspace = self.workspace.clone();
1210 let Some(active_repo) = self.active_repository.clone() else {
1211 return;
1212 };
1213 let to_delete = self
1214 .entries
1215 .iter()
1216 .filter_map(|entry| entry.status_entry())
1217 .filter(|status_entry| status_entry.status.is_created())
1218 .cloned()
1219 .collect::<Vec<_>>();
1220
1221 match to_delete.len() {
1222 0 => return,
1223 1 => return self.revert_entry(&to_delete[0], window, cx),
1224 _ => {}
1225 };
1226
1227 let mut details = to_delete
1228 .iter()
1229 .map(|entry| {
1230 entry
1231 .repo_path
1232 .0
1233 .file_name()
1234 .map(|f| f.to_string_lossy())
1235 .unwrap_or_default()
1236 })
1237 .take(5)
1238 .join("\n");
1239
1240 if to_delete.len() > 5 {
1241 details.push_str(&format!("\nand {} more…", to_delete.len() - 5))
1242 }
1243
1244 let prompt = prompt("Trash these files?", Some(&details), window, cx);
1245 cx.spawn_in(window, async move |this, cx| {
1246 match prompt.await? {
1247 TrashCancel::Trash => {}
1248 TrashCancel::Cancel => return Ok(()),
1249 }
1250 let tasks = workspace.update(cx, |workspace, cx| {
1251 to_delete
1252 .iter()
1253 .filter_map(|entry| {
1254 workspace.project().update(cx, |project, cx| {
1255 let project_path = active_repo
1256 .read(cx)
1257 .repo_path_to_project_path(&entry.repo_path, cx)?;
1258 project.delete_file(project_path, true, cx)
1259 })
1260 })
1261 .collect::<Vec<_>>()
1262 })?;
1263 let to_unstage = to_delete
1264 .into_iter()
1265 .filter(|entry| !entry.status.staging().is_fully_unstaged())
1266 .collect();
1267 this.update(cx, |this, cx| this.change_file_stage(false, to_unstage, cx))?;
1268 for task in tasks {
1269 task.await?;
1270 }
1271 Ok(())
1272 })
1273 .detach_and_prompt_err("Failed to trash files", window, cx, |e, _, _| {
1274 Some(format!("{e}"))
1275 });
1276 }
1277
1278 pub fn stage_all(&mut self, _: &StageAll, _window: &mut Window, cx: &mut Context<Self>) {
1279 let entries = self
1280 .entries
1281 .iter()
1282 .filter_map(|entry| entry.status_entry())
1283 .filter(|status_entry| status_entry.staging.has_unstaged())
1284 .cloned()
1285 .collect::<Vec<_>>();
1286 self.change_file_stage(true, entries, cx);
1287 }
1288
1289 pub fn unstage_all(&mut self, _: &UnstageAll, _window: &mut Window, cx: &mut Context<Self>) {
1290 let entries = self
1291 .entries
1292 .iter()
1293 .filter_map(|entry| entry.status_entry())
1294 .filter(|status_entry| status_entry.staging.has_staged())
1295 .cloned()
1296 .collect::<Vec<_>>();
1297 self.change_file_stage(false, entries, cx);
1298 }
1299
1300 fn toggle_staged_for_entry(
1301 &mut self,
1302 entry: &GitListEntry,
1303 _window: &mut Window,
1304 cx: &mut Context<Self>,
1305 ) {
1306 let Some(active_repository) = self.active_repository.as_ref() else {
1307 return;
1308 };
1309 let (stage, repo_paths) = match entry {
1310 GitListEntry::Status(status_entry) => {
1311 if status_entry.status.staging().is_fully_staged() {
1312 if let Some(op) = self.bulk_staging.clone()
1313 && op.anchor == status_entry.repo_path
1314 {
1315 self.bulk_staging = None;
1316 }
1317
1318 (false, vec![status_entry.clone()])
1319 } else {
1320 self.set_bulk_staging_anchor(status_entry.repo_path.clone(), cx);
1321
1322 (true, vec![status_entry.clone()])
1323 }
1324 }
1325 GitListEntry::Header(section) => {
1326 let goal_staged_state = !self.header_state(section.header).selected();
1327 let repository = active_repository.read(cx);
1328 let entries = self
1329 .entries
1330 .iter()
1331 .filter_map(|entry| entry.status_entry())
1332 .filter(|status_entry| {
1333 section.contains(&status_entry, repository)
1334 && status_entry.staging.as_bool() != Some(goal_staged_state)
1335 })
1336 .map(|status_entry| status_entry.clone())
1337 .collect::<Vec<_>>();
1338
1339 (goal_staged_state, entries)
1340 }
1341 };
1342 self.change_file_stage(stage, repo_paths, cx);
1343 }
1344
1345 fn change_file_stage(
1346 &mut self,
1347 stage: bool,
1348 entries: Vec<GitStatusEntry>,
1349 cx: &mut Context<Self>,
1350 ) {
1351 let Some(active_repository) = self.active_repository.clone() else {
1352 return;
1353 };
1354 let op_id = self.pending.iter().map(|p| p.op_id).max().unwrap_or(0) + 1;
1355 self.pending.push(PendingOperation {
1356 op_id,
1357 target_status: if stage {
1358 TargetStatus::Staged
1359 } else {
1360 TargetStatus::Unstaged
1361 },
1362 entries: entries.clone(),
1363 finished: false,
1364 });
1365 let repository = active_repository.read(cx);
1366 self.update_counts(repository);
1367 cx.notify();
1368
1369 cx.spawn({
1370 async move |this, cx| {
1371 let result = cx
1372 .update(|cx| {
1373 if stage {
1374 active_repository.update(cx, |repo, cx| {
1375 let repo_paths = entries
1376 .iter()
1377 .map(|entry| entry.repo_path.clone())
1378 .collect();
1379 repo.stage_entries(repo_paths, cx)
1380 })
1381 } else {
1382 active_repository.update(cx, |repo, cx| {
1383 let repo_paths = entries
1384 .iter()
1385 .map(|entry| entry.repo_path.clone())
1386 .collect();
1387 repo.unstage_entries(repo_paths, cx)
1388 })
1389 }
1390 })?
1391 .await;
1392
1393 this.update(cx, |this, cx| {
1394 for pending in this.pending.iter_mut() {
1395 if pending.op_id == op_id {
1396 pending.finished = true
1397 }
1398 }
1399 result
1400 .map_err(|e| {
1401 this.show_error_toast(if stage { "add" } else { "reset" }, e, cx);
1402 })
1403 .ok();
1404 cx.notify();
1405 })
1406 }
1407 })
1408 .detach();
1409 }
1410
1411 pub fn total_staged_count(&self) -> usize {
1412 self.tracked_staged_count + self.new_staged_count + self.conflicted_staged_count
1413 }
1414
1415 pub fn commit_message_buffer(&self, cx: &App) -> Entity<Buffer> {
1416 self.commit_editor
1417 .read(cx)
1418 .buffer()
1419 .read(cx)
1420 .as_singleton()
1421 .unwrap()
1422 .clone()
1423 }
1424
1425 fn toggle_staged_for_selected(
1426 &mut self,
1427 _: &git::ToggleStaged,
1428 window: &mut Window,
1429 cx: &mut Context<Self>,
1430 ) {
1431 if let Some(selected_entry) = self.get_selected_entry().cloned() {
1432 self.toggle_staged_for_entry(&selected_entry, window, cx);
1433 }
1434 }
1435
1436 fn stage_range(&mut self, _: &git::StageRange, _window: &mut Window, cx: &mut Context<Self>) {
1437 let Some(index) = self.selected_entry else {
1438 return;
1439 };
1440 self.stage_bulk(index, cx);
1441 }
1442
1443 fn stage_selected(&mut self, _: &git::StageFile, _window: &mut Window, cx: &mut Context<Self>) {
1444 let Some(selected_entry) = self.get_selected_entry() else {
1445 return;
1446 };
1447 let Some(status_entry) = selected_entry.status_entry() else {
1448 return;
1449 };
1450 if status_entry.staging != StageStatus::Staged {
1451 self.change_file_stage(true, vec![status_entry.clone()], cx);
1452 }
1453 }
1454
1455 fn unstage_selected(
1456 &mut self,
1457 _: &git::UnstageFile,
1458 _window: &mut Window,
1459 cx: &mut Context<Self>,
1460 ) {
1461 let Some(selected_entry) = self.get_selected_entry() else {
1462 return;
1463 };
1464 let Some(status_entry) = selected_entry.status_entry() else {
1465 return;
1466 };
1467 if status_entry.staging != StageStatus::Unstaged {
1468 self.change_file_stage(false, vec![status_entry.clone()], cx);
1469 }
1470 }
1471
1472 fn commit(&mut self, _: &git::Commit, window: &mut Window, cx: &mut Context<Self>) {
1473 if self.amend_pending {
1474 return;
1475 }
1476 if self
1477 .commit_editor
1478 .focus_handle(cx)
1479 .contains_focused(window, cx)
1480 {
1481 telemetry::event!("Git Committed", source = "Git Panel");
1482 self.commit_changes(
1483 CommitOptions {
1484 amend: false,
1485 signoff: self.signoff_enabled,
1486 },
1487 window,
1488 cx,
1489 )
1490 } else {
1491 cx.propagate();
1492 }
1493 }
1494
1495 fn amend(&mut self, _: &git::Amend, window: &mut Window, cx: &mut Context<Self>) {
1496 if self
1497 .commit_editor
1498 .focus_handle(cx)
1499 .contains_focused(window, cx)
1500 {
1501 if self.head_commit(cx).is_some() {
1502 if !self.amend_pending {
1503 self.set_amend_pending(true, cx);
1504 self.load_last_commit_message_if_empty(cx);
1505 } else {
1506 telemetry::event!("Git Amended", source = "Git Panel");
1507 self.set_amend_pending(false, cx);
1508 self.commit_changes(
1509 CommitOptions {
1510 amend: true,
1511 signoff: self.signoff_enabled,
1512 },
1513 window,
1514 cx,
1515 );
1516 }
1517 }
1518 } else {
1519 cx.propagate();
1520 }
1521 }
1522
1523 pub fn head_commit(&self, cx: &App) -> Option<CommitDetails> {
1524 self.active_repository
1525 .as_ref()
1526 .and_then(|repo| repo.read(cx).head_commit.as_ref())
1527 .cloned()
1528 }
1529
1530 pub fn load_last_commit_message_if_empty(&mut self, cx: &mut Context<Self>) {
1531 if !self.commit_editor.read(cx).is_empty(cx) {
1532 return;
1533 }
1534 let Some(head_commit) = self.head_commit(cx) else {
1535 return;
1536 };
1537 let recent_sha = head_commit.sha.to_string();
1538 let detail_task = self.load_commit_details(recent_sha, cx);
1539 cx.spawn(async move |this, cx| {
1540 if let Ok(message) = detail_task.await.map(|detail| detail.message) {
1541 this.update(cx, |this, cx| {
1542 this.commit_message_buffer(cx).update(cx, |buffer, cx| {
1543 let start = buffer.anchor_before(0);
1544 let end = buffer.anchor_after(buffer.len());
1545 buffer.edit([(start..end, message)], None, cx);
1546 });
1547 })
1548 .log_err();
1549 }
1550 })
1551 .detach();
1552 }
1553
1554 fn custom_or_suggested_commit_message(
1555 &self,
1556 window: &mut Window,
1557 cx: &mut Context<Self>,
1558 ) -> Option<String> {
1559 let git_commit_language = self.commit_editor.read(cx).language_at(0, cx);
1560 let message = self.commit_editor.read(cx).text(cx);
1561 if message.is_empty() {
1562 return self
1563 .suggest_commit_message(cx)
1564 .filter(|message| !message.trim().is_empty());
1565 } else if message.trim().is_empty() {
1566 return None;
1567 }
1568 let buffer = cx.new(|cx| {
1569 let mut buffer = Buffer::local(message, cx);
1570 buffer.set_language(git_commit_language, cx);
1571 buffer
1572 });
1573 let editor = cx.new(|cx| Editor::for_buffer(buffer, None, window, cx));
1574 let wrapped_message = editor.update(cx, |editor, cx| {
1575 editor.select_all(&Default::default(), window, cx);
1576 editor.rewrap(&Default::default(), window, cx);
1577 editor.text(cx)
1578 });
1579 if wrapped_message.trim().is_empty() {
1580 return None;
1581 }
1582 Some(wrapped_message)
1583 }
1584
1585 fn has_commit_message(&self, cx: &mut Context<Self>) -> bool {
1586 let text = self.commit_editor.read(cx).text(cx);
1587 if !text.trim().is_empty() {
1588 return true;
1589 } else if text.is_empty() {
1590 return self
1591 .suggest_commit_message(cx)
1592 .is_some_and(|text| !text.trim().is_empty());
1593 } else {
1594 return false;
1595 }
1596 }
1597
1598 pub(crate) fn commit_changes(
1599 &mut self,
1600 options: CommitOptions,
1601 window: &mut Window,
1602 cx: &mut Context<Self>,
1603 ) {
1604 let Some(active_repository) = self.active_repository.clone() else {
1605 return;
1606 };
1607 let error_spawn = |message, window: &mut Window, cx: &mut App| {
1608 let prompt = window.prompt(PromptLevel::Warning, message, None, &["Ok"], cx);
1609 cx.spawn(async move |_| {
1610 prompt.await.ok();
1611 })
1612 .detach();
1613 };
1614
1615 if self.has_unstaged_conflicts() {
1616 error_spawn(
1617 "There are still conflicts. You must stage these before committing",
1618 window,
1619 cx,
1620 );
1621 return;
1622 }
1623
1624 let commit_message = self.custom_or_suggested_commit_message(window, cx);
1625
1626 let Some(mut message) = commit_message else {
1627 self.commit_editor.read(cx).focus_handle(cx).focus(window);
1628 return;
1629 };
1630
1631 if self.add_coauthors {
1632 self.fill_co_authors(&mut message, cx);
1633 }
1634
1635 let task = if self.has_staged_changes() {
1636 // Repository serializes all git operations, so we can just send a commit immediately
1637 let commit_task = active_repository.update(cx, |repo, cx| {
1638 repo.commit(message.into(), None, options, cx)
1639 });
1640 cx.background_spawn(async move { commit_task.await? })
1641 } else {
1642 let changed_files = self
1643 .entries
1644 .iter()
1645 .filter_map(|entry| entry.status_entry())
1646 .filter(|status_entry| !status_entry.status.is_created())
1647 .map(|status_entry| status_entry.repo_path.clone())
1648 .collect::<Vec<_>>();
1649
1650 if changed_files.is_empty() {
1651 error_spawn("No changes to commit", window, cx);
1652 return;
1653 }
1654
1655 let stage_task =
1656 active_repository.update(cx, |repo, cx| repo.stage_entries(changed_files, cx));
1657 cx.spawn(async move |_, cx| {
1658 stage_task.await?;
1659 let commit_task = active_repository.update(cx, |repo, cx| {
1660 repo.commit(message.into(), None, options, cx)
1661 })?;
1662 commit_task.await?
1663 })
1664 };
1665 let task = cx.spawn_in(window, async move |this, cx| {
1666 let result = task.await;
1667 this.update_in(cx, |this, window, cx| {
1668 this.pending_commit.take();
1669 match result {
1670 Ok(()) => {
1671 this.commit_editor
1672 .update(cx, |editor, cx| editor.clear(window, cx));
1673 }
1674 Err(e) => this.show_error_toast("commit", e, cx),
1675 }
1676 })
1677 .ok();
1678 });
1679
1680 self.pending_commit = Some(task);
1681 }
1682
1683 fn uncommit(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1684 let Some(repo) = self.active_repository.clone() else {
1685 return;
1686 };
1687 telemetry::event!("Git Uncommitted");
1688
1689 let confirmation = self.check_for_pushed_commits(window, cx);
1690 let prior_head = self.load_commit_details("HEAD".to_string(), cx);
1691
1692 let task = cx.spawn_in(window, async move |this, cx| {
1693 let result = maybe!(async {
1694 if let Ok(true) = confirmation.await {
1695 let prior_head = prior_head.await?;
1696
1697 repo.update(cx, |repo, cx| {
1698 repo.reset("HEAD^".to_string(), ResetMode::Soft, cx)
1699 })?
1700 .await??;
1701
1702 Ok(Some(prior_head))
1703 } else {
1704 Ok(None)
1705 }
1706 })
1707 .await;
1708
1709 this.update_in(cx, |this, window, cx| {
1710 this.pending_commit.take();
1711 match result {
1712 Ok(None) => {}
1713 Ok(Some(prior_commit)) => {
1714 this.commit_editor.update(cx, |editor, cx| {
1715 editor.set_text(prior_commit.message, window, cx)
1716 });
1717 }
1718 Err(e) => this.show_error_toast("reset", e, cx),
1719 }
1720 })
1721 .ok();
1722 });
1723
1724 self.pending_commit = Some(task);
1725 }
1726
1727 fn check_for_pushed_commits(
1728 &mut self,
1729 window: &mut Window,
1730 cx: &mut Context<Self>,
1731 ) -> impl Future<Output = anyhow::Result<bool>> + use<> {
1732 let repo = self.active_repository.clone();
1733 let mut cx = window.to_async(cx);
1734
1735 async move {
1736 let repo = repo.context("No active repository")?;
1737
1738 let pushed_to: Vec<SharedString> = repo
1739 .update(&mut cx, |repo, _| repo.check_for_pushed_commits())?
1740 .await??;
1741
1742 if pushed_to.is_empty() {
1743 Ok(true)
1744 } else {
1745 #[derive(strum::EnumIter, strum::VariantNames)]
1746 #[strum(serialize_all = "title_case")]
1747 enum CancelUncommit {
1748 Uncommit,
1749 Cancel,
1750 }
1751 let detail = format!(
1752 "This commit was already pushed to {}.",
1753 pushed_to.into_iter().join(", ")
1754 );
1755 let result = cx
1756 .update(|window, cx| prompt("Are you sure?", Some(&detail), window, cx))?
1757 .await?;
1758
1759 match result {
1760 CancelUncommit::Cancel => Ok(false),
1761 CancelUncommit::Uncommit => Ok(true),
1762 }
1763 }
1764 }
1765 }
1766
1767 /// Suggests a commit message based on the changed files and their statuses
1768 pub fn suggest_commit_message(&self, cx: &App) -> Option<String> {
1769 if let Some(merge_message) = self
1770 .active_repository
1771 .as_ref()
1772 .and_then(|repo| repo.read(cx).merge.message.as_ref())
1773 {
1774 return Some(merge_message.to_string());
1775 }
1776
1777 let git_status_entry = if let Some(staged_entry) = &self.single_staged_entry {
1778 Some(staged_entry)
1779 } else if let Some(single_tracked_entry) = &self.single_tracked_entry {
1780 Some(single_tracked_entry)
1781 } else {
1782 None
1783 }?;
1784
1785 let action_text = if git_status_entry.status.is_deleted() {
1786 Some("Delete")
1787 } else if git_status_entry.status.is_created() {
1788 Some("Create")
1789 } else if git_status_entry.status.is_modified() {
1790 Some("Update")
1791 } else {
1792 None
1793 }?;
1794
1795 let file_name = git_status_entry
1796 .repo_path
1797 .file_name()
1798 .unwrap_or_default()
1799 .to_string_lossy();
1800
1801 Some(format!("{} {}", action_text, file_name))
1802 }
1803
1804 fn generate_commit_message_action(
1805 &mut self,
1806 _: &git::GenerateCommitMessage,
1807 _window: &mut Window,
1808 cx: &mut Context<Self>,
1809 ) {
1810 self.generate_commit_message(cx);
1811 }
1812
1813 /// Generates a commit message using an LLM.
1814 pub fn generate_commit_message(&mut self, cx: &mut Context<Self>) {
1815 if !self.can_commit() || DisableAiSettings::get_global(cx).disable_ai {
1816 return;
1817 }
1818
1819 let model = match current_language_model(cx) {
1820 Some(value) => value,
1821 None => return,
1822 };
1823
1824 let Some(repo) = self.active_repository.as_ref() else {
1825 return;
1826 };
1827
1828 telemetry::event!("Git Commit Message Generated");
1829
1830 let diff = repo.update(cx, |repo, cx| {
1831 if self.has_staged_changes() {
1832 repo.diff(DiffType::HeadToIndex, cx)
1833 } else {
1834 repo.diff(DiffType::HeadToWorktree, cx)
1835 }
1836 });
1837
1838 let temperature = AgentSettings::temperature_for_model(&model, cx);
1839
1840 self.generate_commit_message_task = Some(cx.spawn(async move |this, cx| {
1841 async move {
1842 let _defer = cx.on_drop(&this, |this, _cx| {
1843 this.generate_commit_message_task.take();
1844 });
1845
1846 let mut diff_text = match diff.await {
1847 Ok(result) => match result {
1848 Ok(text) => text,
1849 Err(e) => {
1850 Self::show_commit_message_error(&this, &e, cx);
1851 return anyhow::Ok(());
1852 }
1853 },
1854 Err(e) => {
1855 Self::show_commit_message_error(&this, &e, cx);
1856 return anyhow::Ok(());
1857 }
1858 };
1859
1860 const ONE_MB: usize = 1_000_000;
1861 if diff_text.len() > ONE_MB {
1862 diff_text = diff_text.chars().take(ONE_MB).collect()
1863 }
1864
1865 let subject = this.update(cx, |this, cx| {
1866 this.commit_editor.read(cx).text(cx).lines().next().map(ToOwned::to_owned).unwrap_or_default()
1867 })?;
1868
1869 let text_empty = subject.trim().is_empty();
1870
1871 let content = if text_empty {
1872 format!("{PROMPT}\nHere are the changes in this commit:\n{diff_text}")
1873 } else {
1874 format!("{PROMPT}\nHere is the user's subject line:\n{subject}\nHere are the changes in this commit:\n{diff_text}\n")
1875 };
1876
1877 const PROMPT: &str = include_str!("commit_message_prompt.txt");
1878
1879 let request = LanguageModelRequest {
1880 thread_id: None,
1881 prompt_id: None,
1882 intent: Some(CompletionIntent::GenerateGitCommitMessage),
1883 mode: None,
1884 messages: vec![LanguageModelRequestMessage {
1885 role: Role::User,
1886 content: vec![content.into()],
1887 cache: false,
1888 }],
1889 tools: Vec::new(),
1890 tool_choice: None,
1891 stop: Vec::new(),
1892 temperature,
1893 thinking_allowed: false,
1894 };
1895
1896 let stream = model.stream_completion_text(request, &cx);
1897 match stream.await {
1898 Ok(mut messages) => {
1899 if !text_empty {
1900 this.update(cx, |this, cx| {
1901 this.commit_message_buffer(cx).update(cx, |buffer, cx| {
1902 let insert_position = buffer.anchor_before(buffer.len());
1903 buffer.edit([(insert_position..insert_position, "\n")], None, cx)
1904 });
1905 })?;
1906 }
1907
1908 while let Some(message) = messages.stream.next().await {
1909 match message {
1910 Ok(text) => {
1911 this.update(cx, |this, cx| {
1912 this.commit_message_buffer(cx).update(cx, |buffer, cx| {
1913 let insert_position = buffer.anchor_before(buffer.len());
1914 buffer.edit([(insert_position..insert_position, text)], None, cx);
1915 });
1916 })?;
1917 }
1918 Err(e) => {
1919 Self::show_commit_message_error(&this, &e, cx);
1920 break;
1921 }
1922 }
1923 }
1924 }
1925 Err(e) => {
1926 Self::show_commit_message_error(&this, &e, cx);
1927 }
1928 }
1929
1930 anyhow::Ok(())
1931 }
1932 .log_err().await
1933 }));
1934 }
1935
1936 fn get_fetch_options(
1937 &self,
1938 window: &mut Window,
1939 cx: &mut Context<Self>,
1940 ) -> Task<Option<FetchOptions>> {
1941 let repo = self.active_repository.clone();
1942 let workspace = self.workspace.clone();
1943
1944 cx.spawn_in(window, async move |_, cx| {
1945 let repo = repo?;
1946 let remotes = repo
1947 .update(cx, |repo, _| repo.get_remotes(None))
1948 .ok()?
1949 .await
1950 .ok()?
1951 .log_err()?;
1952
1953 let mut remotes: Vec<_> = remotes.into_iter().map(FetchOptions::Remote).collect();
1954 if remotes.len() > 1 {
1955 remotes.push(FetchOptions::All);
1956 }
1957 let selection = cx
1958 .update(|window, cx| {
1959 picker_prompt::prompt(
1960 "Pick which remote to fetch",
1961 remotes.iter().map(|r| r.name()).collect(),
1962 workspace,
1963 window,
1964 cx,
1965 )
1966 })
1967 .ok()?
1968 .await?;
1969 remotes.get(selection).cloned()
1970 })
1971 }
1972
1973 pub(crate) fn fetch(
1974 &mut self,
1975 is_fetch_all: bool,
1976 window: &mut Window,
1977 cx: &mut Context<Self>,
1978 ) {
1979 if !self.can_push_and_pull(cx) {
1980 return;
1981 }
1982
1983 let Some(repo) = self.active_repository.clone() else {
1984 return;
1985 };
1986 telemetry::event!("Git Fetched");
1987 let askpass = self.askpass_delegate("git fetch", window, cx);
1988 let this = cx.weak_entity();
1989
1990 let fetch_options = if is_fetch_all {
1991 Task::ready(Some(FetchOptions::All))
1992 } else {
1993 self.get_fetch_options(window, cx)
1994 };
1995
1996 window
1997 .spawn(cx, async move |cx| {
1998 let Some(fetch_options) = fetch_options.await else {
1999 return Ok(());
2000 };
2001 let fetch = repo.update(cx, |repo, cx| {
2002 repo.fetch(fetch_options.clone(), askpass, cx)
2003 })?;
2004
2005 let remote_message = fetch.await?;
2006 this.update(cx, |this, cx| {
2007 let action = match fetch_options {
2008 FetchOptions::All => RemoteAction::Fetch(None),
2009 FetchOptions::Remote(remote) => RemoteAction::Fetch(Some(remote)),
2010 };
2011 match remote_message {
2012 Ok(remote_message) => this.show_remote_output(action, remote_message, cx),
2013 Err(e) => {
2014 log::error!("Error while fetching {:?}", e);
2015 this.show_error_toast(action.name(), e, cx)
2016 }
2017 }
2018
2019 anyhow::Ok(())
2020 })
2021 .ok();
2022 anyhow::Ok(())
2023 })
2024 .detach_and_log_err(cx);
2025 }
2026
2027 pub(crate) fn git_init(&mut self, window: &mut Window, cx: &mut Context<Self>) {
2028 let worktrees = self
2029 .project
2030 .read(cx)
2031 .visible_worktrees(cx)
2032 .collect::<Vec<_>>();
2033
2034 let worktree = if worktrees.len() == 1 {
2035 Task::ready(Some(worktrees.first().unwrap().clone()))
2036 } else if worktrees.len() == 0 {
2037 let result = window.prompt(
2038 PromptLevel::Warning,
2039 "Unable to initialize a git repository",
2040 Some("Open a directory first"),
2041 &["Ok"],
2042 cx,
2043 );
2044 cx.background_executor()
2045 .spawn(async move {
2046 result.await.ok();
2047 })
2048 .detach();
2049 return;
2050 } else {
2051 let worktree_directories = worktrees
2052 .iter()
2053 .map(|worktree| worktree.read(cx).abs_path())
2054 .map(|worktree_abs_path| {
2055 if let Ok(path) = worktree_abs_path.strip_prefix(util::paths::home_dir()) {
2056 Path::new("~")
2057 .join(path)
2058 .to_string_lossy()
2059 .to_string()
2060 .into()
2061 } else {
2062 worktree_abs_path.to_string_lossy().to_string().into()
2063 }
2064 })
2065 .collect_vec();
2066 let prompt = picker_prompt::prompt(
2067 "Where would you like to initialize this git repository?",
2068 worktree_directories,
2069 self.workspace.clone(),
2070 window,
2071 cx,
2072 );
2073
2074 cx.spawn(async move |_, _| prompt.await.map(|ix| worktrees[ix].clone()))
2075 };
2076
2077 cx.spawn_in(window, async move |this, cx| {
2078 let worktree = match worktree.await {
2079 Some(worktree) => worktree,
2080 None => {
2081 return;
2082 }
2083 };
2084
2085 let Ok(result) = this.update(cx, |this, cx| {
2086 let fallback_branch_name = GitPanelSettings::get_global(cx)
2087 .fallback_branch_name
2088 .clone();
2089 this.project.read(cx).git_init(
2090 worktree.read(cx).abs_path(),
2091 fallback_branch_name,
2092 cx,
2093 )
2094 }) else {
2095 return;
2096 };
2097
2098 let result = result.await;
2099
2100 this.update_in(cx, |this, _, cx| match result {
2101 Ok(()) => {}
2102 Err(e) => this.show_error_toast("init", e, cx),
2103 })
2104 .ok();
2105 })
2106 .detach();
2107 }
2108
2109 pub(crate) fn pull(&mut self, window: &mut Window, cx: &mut Context<Self>) {
2110 if !self.can_push_and_pull(cx) {
2111 return;
2112 }
2113 let Some(repo) = self.active_repository.clone() else {
2114 return;
2115 };
2116 let Some(branch) = repo.read(cx).branch.as_ref() else {
2117 return;
2118 };
2119 telemetry::event!("Git Pulled");
2120 let branch = branch.clone();
2121 let remote = self.get_remote(false, window, cx);
2122 cx.spawn_in(window, async move |this, cx| {
2123 let remote = match remote.await {
2124 Ok(Some(remote)) => remote,
2125 Ok(None) => {
2126 return Ok(());
2127 }
2128 Err(e) => {
2129 log::error!("Failed to get current remote: {}", e);
2130 this.update(cx, |this, cx| this.show_error_toast("pull", e, cx))
2131 .ok();
2132 return Ok(());
2133 }
2134 };
2135
2136 let askpass = this.update_in(cx, |this, window, cx| {
2137 this.askpass_delegate(format!("git pull {}", remote.name), window, cx)
2138 })?;
2139
2140 let pull = repo.update(cx, |repo, cx| {
2141 repo.pull(
2142 branch.name().to_owned().into(),
2143 remote.name.clone(),
2144 askpass,
2145 cx,
2146 )
2147 })?;
2148
2149 let remote_message = pull.await?;
2150
2151 let action = RemoteAction::Pull(remote);
2152 this.update(cx, |this, cx| match remote_message {
2153 Ok(remote_message) => this.show_remote_output(action, remote_message, cx),
2154 Err(e) => {
2155 log::error!("Error while pulling {:?}", e);
2156 this.show_error_toast(action.name(), e, cx)
2157 }
2158 })
2159 .ok();
2160
2161 anyhow::Ok(())
2162 })
2163 .detach_and_log_err(cx);
2164 }
2165
2166 pub(crate) fn push(
2167 &mut self,
2168 force_push: bool,
2169 select_remote: bool,
2170 window: &mut Window,
2171 cx: &mut Context<Self>,
2172 ) {
2173 if !self.can_push_and_pull(cx) {
2174 return;
2175 }
2176 let Some(repo) = self.active_repository.clone() else {
2177 return;
2178 };
2179 let Some(branch) = repo.read(cx).branch.as_ref() else {
2180 return;
2181 };
2182 telemetry::event!("Git Pushed");
2183 let branch = branch.clone();
2184
2185 let options = if force_push {
2186 Some(PushOptions::Force)
2187 } else {
2188 match branch.upstream {
2189 Some(Upstream {
2190 tracking: UpstreamTracking::Gone,
2191 ..
2192 })
2193 | None => Some(PushOptions::SetUpstream),
2194 _ => None,
2195 }
2196 };
2197 let remote = self.get_remote(select_remote, window, cx);
2198
2199 cx.spawn_in(window, async move |this, cx| {
2200 let remote = match remote.await {
2201 Ok(Some(remote)) => remote,
2202 Ok(None) => {
2203 return Ok(());
2204 }
2205 Err(e) => {
2206 log::error!("Failed to get current remote: {}", e);
2207 this.update(cx, |this, cx| this.show_error_toast("push", e, cx))
2208 .ok();
2209 return Ok(());
2210 }
2211 };
2212
2213 let askpass_delegate = this.update_in(cx, |this, window, cx| {
2214 this.askpass_delegate(format!("git push {}", remote.name), window, cx)
2215 })?;
2216
2217 let push = repo.update(cx, |repo, cx| {
2218 repo.push(
2219 branch.name().to_owned().into(),
2220 remote.name.clone(),
2221 options,
2222 askpass_delegate,
2223 cx,
2224 )
2225 })?;
2226
2227 let remote_output = push.await?;
2228
2229 let action = RemoteAction::Push(branch.name().to_owned().into(), remote);
2230 this.update(cx, |this, cx| match remote_output {
2231 Ok(remote_message) => this.show_remote_output(action, remote_message, cx),
2232 Err(e) => {
2233 log::error!("Error while pushing {:?}", e);
2234 this.show_error_toast(action.name(), e, cx)
2235 }
2236 })?;
2237
2238 anyhow::Ok(())
2239 })
2240 .detach_and_log_err(cx);
2241 }
2242
2243 fn askpass_delegate(
2244 &self,
2245 operation: impl Into<SharedString>,
2246 window: &mut Window,
2247 cx: &mut Context<Self>,
2248 ) -> AskPassDelegate {
2249 let this = cx.weak_entity();
2250 let operation = operation.into();
2251 let window = window.window_handle();
2252 AskPassDelegate::new(&mut cx.to_async(), move |prompt, tx, cx| {
2253 window
2254 .update(cx, |_, window, cx| {
2255 this.update(cx, |this, cx| {
2256 this.workspace.update(cx, |workspace, cx| {
2257 workspace.toggle_modal(window, cx, |window, cx| {
2258 AskPassModal::new(operation.clone(), prompt.into(), tx, window, cx)
2259 });
2260 })
2261 })
2262 })
2263 .ok();
2264 })
2265 }
2266
2267 fn can_push_and_pull(&self, cx: &App) -> bool {
2268 !self.project.read(cx).is_via_collab()
2269 }
2270
2271 fn get_remote(
2272 &mut self,
2273 always_select: bool,
2274 window: &mut Window,
2275 cx: &mut Context<Self>,
2276 ) -> impl Future<Output = anyhow::Result<Option<Remote>>> + use<> {
2277 let repo = self.active_repository.clone();
2278 let workspace = self.workspace.clone();
2279 let mut cx = window.to_async(cx);
2280
2281 async move {
2282 let repo = repo.context("No active repository")?;
2283 let current_remotes: Vec<Remote> = repo
2284 .update(&mut cx, |repo, _| {
2285 let current_branch = if always_select {
2286 None
2287 } else {
2288 let current_branch = repo.branch.as_ref().context("No active branch")?;
2289 Some(current_branch.name().to_string())
2290 };
2291 anyhow::Ok(repo.get_remotes(current_branch))
2292 })??
2293 .await??;
2294
2295 let current_remotes: Vec<_> = current_remotes
2296 .into_iter()
2297 .map(|remotes| remotes.name)
2298 .collect();
2299 let selection = cx
2300 .update(|window, cx| {
2301 picker_prompt::prompt(
2302 "Pick which remote to push to",
2303 current_remotes.clone(),
2304 workspace,
2305 window,
2306 cx,
2307 )
2308 })?
2309 .await;
2310
2311 Ok(selection.map(|selection| Remote {
2312 name: current_remotes[selection].clone(),
2313 }))
2314 }
2315 }
2316
2317 pub fn load_local_committer(&mut self, cx: &Context<Self>) {
2318 if self.local_committer_task.is_none() {
2319 self.local_committer_task = Some(cx.spawn(async move |this, cx| {
2320 let committer = get_git_committer(cx).await;
2321 this.update(cx, |this, cx| {
2322 this.local_committer = Some(committer);
2323 cx.notify()
2324 })
2325 .ok();
2326 }));
2327 }
2328 }
2329
2330 fn potential_co_authors(&self, cx: &App) -> Vec<(String, String)> {
2331 let mut new_co_authors = Vec::new();
2332 let project = self.project.read(cx);
2333
2334 let Some(room) = self
2335 .workspace
2336 .upgrade()
2337 .and_then(|workspace| workspace.read(cx).active_call()?.read(cx).room().cloned())
2338 else {
2339 return Vec::default();
2340 };
2341
2342 let room = room.read(cx);
2343
2344 for (peer_id, collaborator) in project.collaborators() {
2345 if collaborator.is_host {
2346 continue;
2347 }
2348
2349 let Some(participant) = room.remote_participant_for_peer_id(*peer_id) else {
2350 continue;
2351 };
2352 if !participant.can_write() {
2353 continue;
2354 }
2355 if let Some(email) = &collaborator.committer_email {
2356 let name = collaborator
2357 .committer_name
2358 .clone()
2359 .or_else(|| participant.user.name.clone())
2360 .unwrap_or_else(|| participant.user.github_login.clone());
2361 new_co_authors.push((name.clone(), email.clone()))
2362 }
2363 }
2364 if !project.is_local() && !project.is_read_only(cx) {
2365 if let Some(local_committer) = self.local_committer(room, cx) {
2366 new_co_authors.push(local_committer);
2367 }
2368 }
2369 new_co_authors
2370 }
2371
2372 fn local_committer(&self, room: &call::Room, cx: &App) -> Option<(String, String)> {
2373 let user = room.local_participant_user(cx)?;
2374 let committer = self.local_committer.as_ref()?;
2375 let email = committer.email.clone()?;
2376 let name = committer
2377 .name
2378 .clone()
2379 .or_else(|| user.name.clone())
2380 .unwrap_or_else(|| user.github_login.clone());
2381 Some((name, email))
2382 }
2383
2384 fn toggle_fill_co_authors(
2385 &mut self,
2386 _: &ToggleFillCoAuthors,
2387 _: &mut Window,
2388 cx: &mut Context<Self>,
2389 ) {
2390 self.add_coauthors = !self.add_coauthors;
2391 cx.notify();
2392 }
2393
2394 fn fill_co_authors(&mut self, message: &mut String, cx: &mut Context<Self>) {
2395 const CO_AUTHOR_PREFIX: &str = "Co-authored-by: ";
2396
2397 let existing_text = message.to_ascii_lowercase();
2398 let lowercase_co_author_prefix = CO_AUTHOR_PREFIX.to_lowercase();
2399 let mut ends_with_co_authors = false;
2400 let existing_co_authors = existing_text
2401 .lines()
2402 .filter_map(|line| {
2403 let line = line.trim();
2404 if line.starts_with(&lowercase_co_author_prefix) {
2405 ends_with_co_authors = true;
2406 Some(line)
2407 } else {
2408 ends_with_co_authors = false;
2409 None
2410 }
2411 })
2412 .collect::<HashSet<_>>();
2413
2414 let new_co_authors = self
2415 .potential_co_authors(cx)
2416 .into_iter()
2417 .filter(|(_, email)| {
2418 !existing_co_authors
2419 .iter()
2420 .any(|existing| existing.contains(email.as_str()))
2421 })
2422 .collect::<Vec<_>>();
2423
2424 if new_co_authors.is_empty() {
2425 return;
2426 }
2427
2428 if !ends_with_co_authors {
2429 message.push('\n');
2430 }
2431 for (name, email) in new_co_authors {
2432 message.push('\n');
2433 message.push_str(CO_AUTHOR_PREFIX);
2434 message.push_str(&name);
2435 message.push_str(" <");
2436 message.push_str(&email);
2437 message.push('>');
2438 }
2439 message.push('\n');
2440 }
2441
2442 fn schedule_update(
2443 &mut self,
2444 clear_pending: bool,
2445 window: &mut Window,
2446 cx: &mut Context<Self>,
2447 ) {
2448 let handle = cx.entity().downgrade();
2449 self.reopen_commit_buffer(window, cx);
2450 self.update_visible_entries_task = cx.spawn_in(window, async move |_, cx| {
2451 cx.background_executor().timer(UPDATE_DEBOUNCE).await;
2452 if let Some(git_panel) = handle.upgrade() {
2453 git_panel
2454 .update_in(cx, |git_panel, window, cx| {
2455 if clear_pending {
2456 git_panel.clear_pending();
2457 }
2458 git_panel.update_visible_entries(cx);
2459 git_panel.update_scrollbar_properties(window, cx);
2460 })
2461 .ok();
2462 }
2463 });
2464 }
2465
2466 fn reopen_commit_buffer(&mut self, window: &mut Window, cx: &mut Context<Self>) {
2467 let Some(active_repo) = self.active_repository.as_ref() else {
2468 return;
2469 };
2470 let load_buffer = active_repo.update(cx, |active_repo, cx| {
2471 let project = self.project.read(cx);
2472 active_repo.open_commit_buffer(
2473 Some(project.languages().clone()),
2474 project.buffer_store().clone(),
2475 cx,
2476 )
2477 });
2478
2479 cx.spawn_in(window, async move |git_panel, cx| {
2480 let buffer = load_buffer.await?;
2481 git_panel.update_in(cx, |git_panel, window, cx| {
2482 if git_panel
2483 .commit_editor
2484 .read(cx)
2485 .buffer()
2486 .read(cx)
2487 .as_singleton()
2488 .as_ref()
2489 != Some(&buffer)
2490 {
2491 git_panel.commit_editor = cx.new(|cx| {
2492 commit_message_editor(
2493 buffer,
2494 git_panel.suggest_commit_message(cx).map(SharedString::from),
2495 git_panel.project.clone(),
2496 true,
2497 window,
2498 cx,
2499 )
2500 });
2501 }
2502 })
2503 })
2504 .detach_and_log_err(cx);
2505 }
2506
2507 fn clear_pending(&mut self) {
2508 self.pending.retain(|v| !v.finished)
2509 }
2510
2511 fn update_visible_entries(&mut self, cx: &mut Context<Self>) {
2512 let bulk_staging = self.bulk_staging.take();
2513 let last_staged_path_prev_index = bulk_staging
2514 .as_ref()
2515 .and_then(|op| self.entry_by_path(&op.anchor, cx));
2516
2517 self.entries.clear();
2518 self.single_staged_entry.take();
2519 self.single_tracked_entry.take();
2520 self.conflicted_count = 0;
2521 self.conflicted_staged_count = 0;
2522 self.new_count = 0;
2523 self.tracked_count = 0;
2524 self.new_staged_count = 0;
2525 self.tracked_staged_count = 0;
2526 self.entry_count = 0;
2527
2528 let sort_by_path = GitPanelSettings::get_global(cx).sort_by_path;
2529
2530 let mut changed_entries = Vec::new();
2531 let mut new_entries = Vec::new();
2532 let mut conflict_entries = Vec::new();
2533 let mut single_staged_entry = None;
2534 let mut staged_count = 0;
2535 let mut max_width_item: Option<(RepoPath, usize)> = None;
2536
2537 let Some(repo) = self.active_repository.as_ref() else {
2538 // Just clear entries if no repository is active.
2539 cx.notify();
2540 return;
2541 };
2542
2543 let repo = repo.read(cx);
2544
2545 for entry in repo.cached_status() {
2546 let is_conflict = repo.had_conflict_on_last_merge_head_change(&entry.repo_path);
2547 let is_new = entry.status.is_created();
2548 let staging = entry.status.staging();
2549
2550 if self.pending.iter().any(|pending| {
2551 pending.target_status == TargetStatus::Reverted
2552 && !pending.finished
2553 && pending
2554 .entries
2555 .iter()
2556 .any(|pending| pending.repo_path == entry.repo_path)
2557 }) {
2558 continue;
2559 }
2560
2561 let abs_path = repo.work_directory_abs_path.join(&entry.repo_path.0);
2562 let entry = GitStatusEntry {
2563 repo_path: entry.repo_path.clone(),
2564 abs_path,
2565 status: entry.status,
2566 staging,
2567 };
2568
2569 if staging.has_staged() {
2570 staged_count += 1;
2571 single_staged_entry = Some(entry.clone());
2572 }
2573
2574 let width_estimate = Self::item_width_estimate(
2575 entry.parent_dir().map(|s| s.len()).unwrap_or(0),
2576 entry.display_name().len(),
2577 );
2578
2579 match max_width_item.as_mut() {
2580 Some((repo_path, estimate)) => {
2581 if width_estimate > *estimate {
2582 *repo_path = entry.repo_path.clone();
2583 *estimate = width_estimate;
2584 }
2585 }
2586 None => max_width_item = Some((entry.repo_path.clone(), width_estimate)),
2587 }
2588
2589 if sort_by_path {
2590 changed_entries.push(entry);
2591 } else if is_conflict {
2592 conflict_entries.push(entry);
2593 } else if is_new {
2594 new_entries.push(entry);
2595 } else {
2596 changed_entries.push(entry);
2597 }
2598 }
2599
2600 let mut pending_staged_count = 0;
2601 let mut last_pending_staged = None;
2602 let mut pending_status_for_single_staged = None;
2603 for pending in self.pending.iter() {
2604 if pending.target_status == TargetStatus::Staged {
2605 pending_staged_count += pending.entries.len();
2606 last_pending_staged = pending.entries.iter().next().cloned();
2607 }
2608 if let Some(single_staged) = &single_staged_entry {
2609 if pending
2610 .entries
2611 .iter()
2612 .any(|entry| entry.repo_path == single_staged.repo_path)
2613 {
2614 pending_status_for_single_staged = Some(pending.target_status);
2615 }
2616 }
2617 }
2618
2619 if conflict_entries.len() == 0 && staged_count == 1 && pending_staged_count == 0 {
2620 match pending_status_for_single_staged {
2621 Some(TargetStatus::Staged) | None => {
2622 self.single_staged_entry = single_staged_entry;
2623 }
2624 _ => {}
2625 }
2626 } else if conflict_entries.len() == 0 && pending_staged_count == 1 {
2627 self.single_staged_entry = last_pending_staged;
2628 }
2629
2630 if conflict_entries.len() == 0 && changed_entries.len() == 1 {
2631 self.single_tracked_entry = changed_entries.first().cloned();
2632 }
2633
2634 if conflict_entries.len() > 0 {
2635 self.entries.push(GitListEntry::Header(GitHeaderEntry {
2636 header: Section::Conflict,
2637 }));
2638 self.entries
2639 .extend(conflict_entries.into_iter().map(GitListEntry::Status));
2640 }
2641
2642 if changed_entries.len() > 0 {
2643 if !sort_by_path {
2644 self.entries.push(GitListEntry::Header(GitHeaderEntry {
2645 header: Section::Tracked,
2646 }));
2647 }
2648 self.entries
2649 .extend(changed_entries.into_iter().map(GitListEntry::Status));
2650 }
2651 if new_entries.len() > 0 {
2652 self.entries.push(GitListEntry::Header(GitHeaderEntry {
2653 header: Section::New,
2654 }));
2655 self.entries
2656 .extend(new_entries.into_iter().map(GitListEntry::Status));
2657 }
2658
2659 if let Some((repo_path, _)) = max_width_item {
2660 self.max_width_item_index = self.entries.iter().position(|entry| match entry {
2661 GitListEntry::Status(git_status_entry) => git_status_entry.repo_path == repo_path,
2662 GitListEntry::Header(_) => false,
2663 });
2664 }
2665
2666 self.update_counts(repo);
2667
2668 let bulk_staging_anchor_new_index = bulk_staging
2669 .as_ref()
2670 .filter(|op| op.repo_id == repo.id)
2671 .and_then(|op| self.entry_by_path(&op.anchor, cx));
2672 if bulk_staging_anchor_new_index == last_staged_path_prev_index
2673 && let Some(index) = bulk_staging_anchor_new_index
2674 && let Some(entry) = self.entries.get(index)
2675 && let Some(entry) = entry.status_entry()
2676 && self.entry_staging(entry) == StageStatus::Staged
2677 {
2678 self.bulk_staging = bulk_staging;
2679 }
2680
2681 self.select_first_entry_if_none(cx);
2682
2683 let suggested_commit_message = self.suggest_commit_message(cx);
2684 let placeholder_text = suggested_commit_message.unwrap_or("Enter commit message".into());
2685
2686 self.commit_editor.update(cx, |editor, cx| {
2687 editor.set_placeholder_text(Arc::from(placeholder_text), cx)
2688 });
2689
2690 cx.notify();
2691 }
2692
2693 fn header_state(&self, header_type: Section) -> ToggleState {
2694 let (staged_count, count) = match header_type {
2695 Section::New => (self.new_staged_count, self.new_count),
2696 Section::Tracked => (self.tracked_staged_count, self.tracked_count),
2697 Section::Conflict => (self.conflicted_staged_count, self.conflicted_count),
2698 };
2699 if staged_count == 0 {
2700 ToggleState::Unselected
2701 } else if count == staged_count {
2702 ToggleState::Selected
2703 } else {
2704 ToggleState::Indeterminate
2705 }
2706 }
2707
2708 fn update_counts(&mut self, repo: &Repository) {
2709 self.show_placeholders = false;
2710 self.conflicted_count = 0;
2711 self.conflicted_staged_count = 0;
2712 self.new_count = 0;
2713 self.tracked_count = 0;
2714 self.new_staged_count = 0;
2715 self.tracked_staged_count = 0;
2716 self.entry_count = 0;
2717 for entry in &self.entries {
2718 let Some(status_entry) = entry.status_entry() else {
2719 continue;
2720 };
2721 self.entry_count += 1;
2722 if repo.had_conflict_on_last_merge_head_change(&status_entry.repo_path) {
2723 self.conflicted_count += 1;
2724 if self.entry_staging(status_entry).has_staged() {
2725 self.conflicted_staged_count += 1;
2726 }
2727 } else if status_entry.status.is_created() {
2728 self.new_count += 1;
2729 if self.entry_staging(status_entry).has_staged() {
2730 self.new_staged_count += 1;
2731 }
2732 } else {
2733 self.tracked_count += 1;
2734 if self.entry_staging(status_entry).has_staged() {
2735 self.tracked_staged_count += 1;
2736 }
2737 }
2738 }
2739 }
2740
2741 fn entry_staging(&self, entry: &GitStatusEntry) -> StageStatus {
2742 for pending in self.pending.iter().rev() {
2743 if pending
2744 .entries
2745 .iter()
2746 .any(|pending_entry| pending_entry.repo_path == entry.repo_path)
2747 {
2748 match pending.target_status {
2749 TargetStatus::Staged => return StageStatus::Staged,
2750 TargetStatus::Unstaged => return StageStatus::Unstaged,
2751 TargetStatus::Reverted => continue,
2752 TargetStatus::Unchanged => continue,
2753 }
2754 }
2755 }
2756 entry.staging
2757 }
2758
2759 pub(crate) fn has_staged_changes(&self) -> bool {
2760 self.tracked_staged_count > 0
2761 || self.new_staged_count > 0
2762 || self.conflicted_staged_count > 0
2763 }
2764
2765 pub(crate) fn has_unstaged_changes(&self) -> bool {
2766 self.tracked_count > self.tracked_staged_count
2767 || self.new_count > self.new_staged_count
2768 || self.conflicted_count > self.conflicted_staged_count
2769 }
2770
2771 fn has_tracked_changes(&self) -> bool {
2772 self.tracked_count > 0
2773 }
2774
2775 pub fn has_unstaged_conflicts(&self) -> bool {
2776 self.conflicted_count > 0 && self.conflicted_count != self.conflicted_staged_count
2777 }
2778
2779 fn show_error_toast(&self, action: impl Into<SharedString>, e: anyhow::Error, cx: &mut App) {
2780 let action = action.into();
2781 let Some(workspace) = self.workspace.upgrade() else {
2782 return;
2783 };
2784
2785 let message = e.to_string().trim().to_string();
2786 if message
2787 .matches(git::repository::REMOTE_CANCELLED_BY_USER)
2788 .next()
2789 .is_some()
2790 {
2791 return; // Hide the cancelled by user message
2792 } else {
2793 workspace.update(cx, |workspace, cx| {
2794 let workspace_weak = cx.weak_entity();
2795 let toast = StatusToast::new(format!("git {} failed", action), cx, |this, _cx| {
2796 this.icon(ToastIcon::new(IconName::XCircle).color(Color::Error))
2797 .action("View Log", move |window, cx| {
2798 let message = message.clone();
2799 let action = action.clone();
2800 workspace_weak
2801 .update(cx, move |workspace, cx| {
2802 Self::open_output(action, workspace, &message, window, cx)
2803 })
2804 .ok();
2805 })
2806 });
2807 workspace.toggle_status_toast(toast, cx)
2808 });
2809 }
2810 }
2811
2812 fn show_commit_message_error<E>(weak_this: &WeakEntity<Self>, err: &E, cx: &mut AsyncApp)
2813 where
2814 E: std::fmt::Debug + std::fmt::Display,
2815 {
2816 if let Ok(Some(workspace)) = weak_this.update(cx, |this, _cx| this.workspace.upgrade()) {
2817 let _ = workspace.update(cx, |workspace, cx| {
2818 struct CommitMessageError;
2819 let notification_id = NotificationId::unique::<CommitMessageError>();
2820 workspace.show_notification(notification_id, cx, |cx| {
2821 cx.new(|cx| {
2822 ErrorMessagePrompt::new(
2823 format!("Failed to generate commit message: {err}"),
2824 cx,
2825 )
2826 })
2827 });
2828 });
2829 }
2830 }
2831
2832 fn show_remote_output(&self, action: RemoteAction, info: RemoteCommandOutput, cx: &mut App) {
2833 let Some(workspace) = self.workspace.upgrade() else {
2834 return;
2835 };
2836
2837 workspace.update(cx, |workspace, cx| {
2838 let SuccessMessage { message, style } = remote_output::format_output(&action, info);
2839 let workspace_weak = cx.weak_entity();
2840 let operation = action.name();
2841
2842 let status_toast = StatusToast::new(message, cx, move |this, _cx| {
2843 use remote_output::SuccessStyle::*;
2844 match style {
2845 Toast { .. } => this,
2846 ToastWithLog { output } => this
2847 .icon(ToastIcon::new(IconName::GitBranchSmall).color(Color::Muted))
2848 .action("View Log", move |window, cx| {
2849 let output = output.clone();
2850 let output =
2851 format!("stdout:\n{}\nstderr:\n{}", output.stdout, output.stderr);
2852 workspace_weak
2853 .update(cx, move |workspace, cx| {
2854 Self::open_output(operation, workspace, &output, window, cx)
2855 })
2856 .ok();
2857 }),
2858 PushPrLink { link } => this
2859 .icon(ToastIcon::new(IconName::GitBranchSmall).color(Color::Muted))
2860 .action("Open Pull Request", move |_, cx| cx.open_url(&link)),
2861 }
2862 });
2863 workspace.toggle_status_toast(status_toast, cx)
2864 });
2865 }
2866
2867 fn open_output(
2868 operation: impl Into<SharedString>,
2869 workspace: &mut Workspace,
2870 output: &str,
2871 window: &mut Window,
2872 cx: &mut Context<Workspace>,
2873 ) {
2874 let operation = operation.into();
2875 let buffer = cx.new(|cx| Buffer::local(output, cx));
2876 buffer.update(cx, |buffer, cx| {
2877 buffer.set_capability(language::Capability::ReadOnly, cx);
2878 });
2879 let editor = cx.new(|cx| {
2880 let mut editor = Editor::for_buffer(buffer, None, window, cx);
2881 editor.buffer().update(cx, |buffer, cx| {
2882 buffer.set_title(format!("Output from git {operation}"), cx);
2883 });
2884 editor.set_read_only(true);
2885 editor
2886 });
2887
2888 workspace.add_item_to_center(Box::new(editor), window, cx);
2889 }
2890
2891 pub fn can_commit(&self) -> bool {
2892 (self.has_staged_changes() || self.has_tracked_changes()) && !self.has_unstaged_conflicts()
2893 }
2894
2895 pub fn can_stage_all(&self) -> bool {
2896 self.has_unstaged_changes()
2897 }
2898
2899 pub fn can_unstage_all(&self) -> bool {
2900 self.has_staged_changes()
2901 }
2902
2903 // eventually we'll need to take depth into account here
2904 // if we add a tree view
2905 fn item_width_estimate(path: usize, file_name: usize) -> usize {
2906 path + file_name
2907 }
2908
2909 fn render_overflow_menu(&self, id: impl Into<ElementId>) -> impl IntoElement {
2910 let focus_handle = self.focus_handle.clone();
2911 let has_tracked_changes = self.has_tracked_changes();
2912 let has_staged_changes = self.has_staged_changes();
2913 let has_unstaged_changes = self.has_unstaged_changes();
2914 let has_new_changes = self.new_count > 0;
2915
2916 PopoverMenu::new(id.into())
2917 .trigger(
2918 IconButton::new("overflow-menu-trigger", IconName::Ellipsis)
2919 .icon_size(IconSize::Small)
2920 .icon_color(Color::Muted),
2921 )
2922 .menu(move |window, cx| {
2923 Some(git_panel_context_menu(
2924 focus_handle.clone(),
2925 GitMenuState {
2926 has_tracked_changes,
2927 has_staged_changes,
2928 has_unstaged_changes,
2929 has_new_changes,
2930 },
2931 window,
2932 cx,
2933 ))
2934 })
2935 .anchor(Corner::TopRight)
2936 }
2937
2938 pub(crate) fn render_generate_commit_message_button(
2939 &self,
2940 cx: &Context<Self>,
2941 ) -> Option<AnyElement> {
2942 current_language_model(cx).is_some().then(|| {
2943 if self.generate_commit_message_task.is_some() {
2944 return h_flex()
2945 .gap_1()
2946 .child(
2947 Icon::new(IconName::ArrowCircle)
2948 .size(IconSize::XSmall)
2949 .color(Color::Info)
2950 .with_animation(
2951 "arrow-circle",
2952 Animation::new(Duration::from_secs(2)).repeat(),
2953 |icon, delta| {
2954 icon.transform(Transformation::rotate(percentage(delta)))
2955 },
2956 ),
2957 )
2958 .child(
2959 Label::new("Generating Commit...")
2960 .size(LabelSize::Small)
2961 .color(Color::Muted),
2962 )
2963 .into_any_element();
2964 }
2965
2966 let can_commit = self.can_commit();
2967 let editor_focus_handle = self.commit_editor.focus_handle(cx);
2968 IconButton::new("generate-commit-message", IconName::AiEdit)
2969 .shape(ui::IconButtonShape::Square)
2970 .icon_color(Color::Muted)
2971 .tooltip(move |window, cx| {
2972 if can_commit {
2973 Tooltip::for_action_in(
2974 "Generate Commit Message",
2975 &git::GenerateCommitMessage,
2976 &editor_focus_handle,
2977 window,
2978 cx,
2979 )
2980 } else {
2981 Tooltip::simple("No changes to commit", cx)
2982 }
2983 })
2984 .disabled(!can_commit)
2985 .on_click(cx.listener(move |this, _event, _window, cx| {
2986 this.generate_commit_message(cx);
2987 }))
2988 .into_any_element()
2989 })
2990 }
2991
2992 pub(crate) fn render_co_authors(&self, cx: &Context<Self>) -> Option<AnyElement> {
2993 let potential_co_authors = self.potential_co_authors(cx);
2994
2995 let (tooltip_label, icon) = if self.add_coauthors {
2996 ("Remove co-authored-by", IconName::Person)
2997 } else {
2998 ("Add co-authored-by", IconName::UserCheck)
2999 };
3000
3001 if potential_co_authors.is_empty() {
3002 None
3003 } else {
3004 Some(
3005 IconButton::new("co-authors", icon)
3006 .shape(ui::IconButtonShape::Square)
3007 .icon_color(Color::Disabled)
3008 .selected_icon_color(Color::Selected)
3009 .toggle_state(self.add_coauthors)
3010 .tooltip(move |_, cx| {
3011 let title = format!(
3012 "{}:{}{}",
3013 tooltip_label,
3014 if potential_co_authors.len() == 1 {
3015 ""
3016 } else {
3017 "\n"
3018 },
3019 potential_co_authors
3020 .iter()
3021 .map(|(name, email)| format!(" {} <{}>", name, email))
3022 .join("\n")
3023 );
3024 Tooltip::simple(title, cx)
3025 })
3026 .on_click(cx.listener(|this, _, _, cx| {
3027 this.add_coauthors = !this.add_coauthors;
3028 cx.notify();
3029 }))
3030 .into_any_element(),
3031 )
3032 }
3033 }
3034
3035 fn render_git_commit_menu(
3036 &self,
3037 id: impl Into<ElementId>,
3038 keybinding_target: Option<FocusHandle>,
3039 cx: &mut Context<Self>,
3040 ) -> impl IntoElement {
3041 PopoverMenu::new(id.into())
3042 .trigger(
3043 ui::ButtonLike::new_rounded_right("commit-split-button-right")
3044 .layer(ui::ElevationIndex::ModalSurface)
3045 .size(ButtonSize::None)
3046 .child(
3047 h_flex()
3048 .px_1()
3049 .h_full()
3050 .justify_center()
3051 .border_l_1()
3052 .border_color(cx.theme().colors().border)
3053 .child(Icon::new(IconName::ChevronDownSmall).size(IconSize::XSmall)),
3054 ),
3055 )
3056 .menu({
3057 let git_panel = cx.entity();
3058 let has_previous_commit = self.head_commit(cx).is_some();
3059 let amend = self.amend_pending();
3060 let signoff = self.signoff_enabled;
3061
3062 move |window, cx| {
3063 Some(ContextMenu::build(window, cx, |context_menu, _, _| {
3064 context_menu
3065 .when_some(keybinding_target.clone(), |el, keybinding_target| {
3066 el.context(keybinding_target.clone())
3067 })
3068 .when(has_previous_commit, |this| {
3069 this.toggleable_entry(
3070 "Amend",
3071 amend,
3072 IconPosition::Start,
3073 Some(Box::new(Amend)),
3074 {
3075 let git_panel = git_panel.downgrade();
3076 move |_, cx| {
3077 git_panel
3078 .update(cx, |git_panel, cx| {
3079 git_panel.toggle_amend_pending(cx);
3080 })
3081 .ok();
3082 }
3083 },
3084 )
3085 })
3086 .toggleable_entry(
3087 "Signoff",
3088 signoff,
3089 IconPosition::Start,
3090 Some(Box::new(Signoff)),
3091 move |window, cx| window.dispatch_action(Box::new(Signoff), cx),
3092 )
3093 }))
3094 }
3095 })
3096 .anchor(Corner::TopRight)
3097 }
3098
3099 pub fn configure_commit_button(&self, cx: &mut Context<Self>) -> (bool, &'static str) {
3100 if self.has_unstaged_conflicts() {
3101 (false, "You must resolve conflicts before committing")
3102 } else if !self.has_staged_changes() && !self.has_tracked_changes() {
3103 (false, "No changes to commit")
3104 } else if self.pending_commit.is_some() {
3105 (false, "Commit in progress")
3106 } else if !self.has_commit_message(cx) {
3107 (false, "No commit message")
3108 } else if !self.has_write_access(cx) {
3109 (false, "You do not have write access to this project")
3110 } else {
3111 (true, self.commit_button_title())
3112 }
3113 }
3114
3115 pub fn commit_button_title(&self) -> &'static str {
3116 if self.amend_pending {
3117 if self.has_staged_changes() {
3118 "Amend"
3119 } else {
3120 "Amend Tracked"
3121 }
3122 } else {
3123 if self.has_staged_changes() {
3124 "Commit"
3125 } else {
3126 "Commit Tracked"
3127 }
3128 }
3129 }
3130
3131 fn expand_commit_editor(
3132 &mut self,
3133 _: &git::ExpandCommitEditor,
3134 window: &mut Window,
3135 cx: &mut Context<Self>,
3136 ) {
3137 let workspace = self.workspace.clone();
3138 window.defer(cx, move |window, cx| {
3139 workspace
3140 .update(cx, |workspace, cx| {
3141 CommitModal::toggle(workspace, None, window, cx)
3142 })
3143 .ok();
3144 })
3145 }
3146
3147 fn render_panel_header(
3148 &self,
3149 window: &mut Window,
3150 cx: &mut Context<Self>,
3151 ) -> Option<impl IntoElement> {
3152 self.active_repository.as_ref()?;
3153
3154 let text;
3155 let action;
3156 let tooltip;
3157 if self.total_staged_count() == self.entry_count && self.entry_count > 0 {
3158 text = "Unstage All";
3159 action = git::UnstageAll.boxed_clone();
3160 tooltip = "git reset";
3161 } else {
3162 text = "Stage All";
3163 action = git::StageAll.boxed_clone();
3164 tooltip = "git add --all ."
3165 }
3166
3167 let change_string = match self.entry_count {
3168 0 => "No Changes".to_string(),
3169 1 => "1 Change".to_string(),
3170 _ => format!("{} Changes", self.entry_count),
3171 };
3172
3173 Some(
3174 self.panel_header_container(window, cx)
3175 .px_2()
3176 .justify_between()
3177 .child(
3178 panel_button(change_string)
3179 .color(Color::Muted)
3180 .tooltip(Tooltip::for_action_title_in(
3181 "Open Diff",
3182 &Diff,
3183 &self.focus_handle,
3184 ))
3185 .on_click(|_, _, cx| {
3186 cx.defer(|cx| {
3187 cx.dispatch_action(&Diff);
3188 })
3189 }),
3190 )
3191 .child(
3192 h_flex()
3193 .gap_1()
3194 .child(self.render_overflow_menu("overflow_menu"))
3195 .child(
3196 panel_filled_button(text)
3197 .tooltip(Tooltip::for_action_title_in(
3198 tooltip,
3199 action.as_ref(),
3200 &self.focus_handle,
3201 ))
3202 .disabled(self.entry_count == 0)
3203 .on_click(move |_, _, cx| {
3204 let action = action.boxed_clone();
3205 cx.defer(move |cx| {
3206 cx.dispatch_action(action.as_ref());
3207 })
3208 }),
3209 ),
3210 ),
3211 )
3212 }
3213
3214 pub(crate) fn render_remote_button(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
3215 let branch = self.active_repository.as_ref()?.read(cx).branch.clone();
3216 if !self.can_push_and_pull(cx) {
3217 return None;
3218 }
3219 Some(
3220 h_flex()
3221 .gap_1()
3222 .flex_shrink_0()
3223 .when_some(branch, |this, branch| {
3224 let focus_handle = Some(self.focus_handle(cx));
3225
3226 this.children(render_remote_button(
3227 "remote-button",
3228 &branch,
3229 focus_handle,
3230 true,
3231 ))
3232 })
3233 .into_any_element(),
3234 )
3235 }
3236
3237 pub fn render_footer(
3238 &self,
3239 window: &mut Window,
3240 cx: &mut Context<Self>,
3241 ) -> Option<impl IntoElement> {
3242 let active_repository = self.active_repository.clone()?;
3243 let panel_editor_style = panel_editor_style(true, window, cx);
3244
3245 let enable_coauthors = self.render_co_authors(cx);
3246
3247 let editor_focus_handle = self.commit_editor.focus_handle(cx);
3248 let expand_tooltip_focus_handle = editor_focus_handle.clone();
3249
3250 let branch = active_repository.read(cx).branch.clone();
3251 let head_commit = active_repository.read(cx).head_commit.clone();
3252
3253 let footer_size = px(32.);
3254 let gap = px(9.0);
3255 let max_height = panel_editor_style
3256 .text
3257 .line_height_in_pixels(window.rem_size())
3258 * MAX_PANEL_EDITOR_LINES
3259 + gap;
3260
3261 let git_panel = cx.entity().clone();
3262 let display_name = SharedString::from(Arc::from(
3263 active_repository
3264 .read(cx)
3265 .display_name()
3266 .trim_end_matches("/"),
3267 ));
3268 let editor_is_long = self.commit_editor.update(cx, |editor, cx| {
3269 editor.max_point(cx).row().0 >= MAX_PANEL_EDITOR_LINES as u32
3270 });
3271
3272 let footer = v_flex()
3273 .child(PanelRepoFooter::new(
3274 display_name,
3275 branch,
3276 head_commit,
3277 Some(git_panel.clone()),
3278 ))
3279 .child(
3280 panel_editor_container(window, cx)
3281 .id("commit-editor-container")
3282 .relative()
3283 .w_full()
3284 .h(max_height + footer_size)
3285 .border_t_1()
3286 .border_color(cx.theme().colors().border)
3287 .cursor_text()
3288 .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| {
3289 window.focus(&this.commit_editor.focus_handle(cx));
3290 }))
3291 .child(
3292 h_flex()
3293 .id("commit-footer")
3294 .border_t_1()
3295 .when(editor_is_long, |el| {
3296 el.border_color(cx.theme().colors().border_variant)
3297 })
3298 .absolute()
3299 .bottom_0()
3300 .left_0()
3301 .w_full()
3302 .px_2()
3303 .h(footer_size)
3304 .flex_none()
3305 .justify_between()
3306 .child(
3307 self.render_generate_commit_message_button(cx)
3308 .unwrap_or_else(|| div().into_any_element()),
3309 )
3310 .child(
3311 h_flex()
3312 .gap_0p5()
3313 .children(enable_coauthors)
3314 .child(self.render_commit_button(cx)),
3315 ),
3316 )
3317 .child(
3318 div()
3319 .pr_2p5()
3320 .on_action(|&editor::actions::MoveUp, _, cx| {
3321 cx.stop_propagation();
3322 })
3323 .on_action(|&editor::actions::MoveDown, _, cx| {
3324 cx.stop_propagation();
3325 })
3326 .child(EditorElement::new(&self.commit_editor, panel_editor_style)),
3327 )
3328 .child(
3329 h_flex()
3330 .absolute()
3331 .top_2()
3332 .right_2()
3333 .opacity(0.5)
3334 .hover(|this| this.opacity(1.0))
3335 .child(
3336 panel_icon_button("expand-commit-editor", IconName::Maximize)
3337 .icon_size(IconSize::Small)
3338 .size(ui::ButtonSize::Default)
3339 .tooltip(move |window, cx| {
3340 Tooltip::for_action_in(
3341 "Open Commit Modal",
3342 &git::ExpandCommitEditor,
3343 &expand_tooltip_focus_handle,
3344 window,
3345 cx,
3346 )
3347 })
3348 .on_click(cx.listener({
3349 move |_, _, window, cx| {
3350 window.dispatch_action(
3351 git::ExpandCommitEditor.boxed_clone(),
3352 cx,
3353 )
3354 }
3355 })),
3356 ),
3357 ),
3358 );
3359
3360 Some(footer)
3361 }
3362
3363 fn render_commit_button(&self, cx: &mut Context<Self>) -> impl IntoElement {
3364 let (can_commit, tooltip) = self.configure_commit_button(cx);
3365 let title = self.commit_button_title();
3366 let commit_tooltip_focus_handle = self.commit_editor.focus_handle(cx);
3367 let amend = self.amend_pending();
3368 let signoff = self.signoff_enabled;
3369
3370 div()
3371 .id("commit-wrapper")
3372 .on_hover(cx.listener(move |this, hovered, _, cx| {
3373 this.show_placeholders =
3374 *hovered && !this.has_staged_changes() && !this.has_unstaged_conflicts();
3375 cx.notify()
3376 }))
3377 .child(SplitButton::new(
3378 ui::ButtonLike::new_rounded_left(ElementId::Name(
3379 format!("split-button-left-{}", title).into(),
3380 ))
3381 .layer(ui::ElevationIndex::ModalSurface)
3382 .size(ui::ButtonSize::Compact)
3383 .child(
3384 div()
3385 .child(Label::new(title).size(LabelSize::Small))
3386 .mr_0p5(),
3387 )
3388 .on_click({
3389 let git_panel = cx.weak_entity();
3390 move |_, window, cx| {
3391 telemetry::event!("Git Committed", source = "Git Panel");
3392 git_panel
3393 .update(cx, |git_panel, cx| {
3394 git_panel.set_amend_pending(false, cx);
3395 git_panel.commit_changes(
3396 CommitOptions { amend, signoff },
3397 window,
3398 cx,
3399 );
3400 })
3401 .ok();
3402 }
3403 })
3404 .disabled(!can_commit || self.modal_open)
3405 .tooltip({
3406 let handle = commit_tooltip_focus_handle.clone();
3407 move |window, cx| {
3408 if can_commit {
3409 Tooltip::with_meta_in(
3410 tooltip,
3411 Some(&git::Commit),
3412 format!(
3413 "git commit{}{}",
3414 if amend { " --amend" } else { "" },
3415 if signoff { " --signoff" } else { "" }
3416 ),
3417 &handle.clone(),
3418 window,
3419 cx,
3420 )
3421 } else {
3422 Tooltip::simple(tooltip, cx)
3423 }
3424 }
3425 }),
3426 self.render_git_commit_menu(
3427 ElementId::Name(format!("split-button-right-{}", title).into()),
3428 Some(commit_tooltip_focus_handle.clone()),
3429 cx,
3430 )
3431 .into_any_element(),
3432 ))
3433 }
3434
3435 fn render_pending_amend(&self, cx: &mut Context<Self>) -> impl IntoElement {
3436 h_flex()
3437 .py_1p5()
3438 .px_2()
3439 .gap_1p5()
3440 .justify_between()
3441 .border_t_1()
3442 .border_color(cx.theme().colors().border.opacity(0.8))
3443 .child(
3444 div()
3445 .flex_grow()
3446 .overflow_hidden()
3447 .max_w(relative(0.85))
3448 .child(
3449 Label::new("This will update your most recent commit.")
3450 .size(LabelSize::Small)
3451 .truncate(),
3452 ),
3453 )
3454 .child(
3455 panel_button("Cancel")
3456 .size(ButtonSize::Default)
3457 .on_click(cx.listener(|this, _, _, cx| this.set_amend_pending(false, cx))),
3458 )
3459 }
3460
3461 fn render_previous_commit(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> {
3462 let active_repository = self.active_repository.as_ref()?;
3463 let branch = active_repository.read(cx).branch.as_ref()?;
3464 let commit = branch.most_recent_commit.as_ref()?.clone();
3465 let workspace = self.workspace.clone();
3466 let this = cx.entity();
3467
3468 Some(
3469 h_flex()
3470 .py_1p5()
3471 .px_2()
3472 .gap_1p5()
3473 .justify_between()
3474 .border_t_1()
3475 .border_color(cx.theme().colors().border.opacity(0.8))
3476 .child(
3477 div()
3478 .flex_grow()
3479 .overflow_hidden()
3480 .max_w(relative(0.85))
3481 .child(
3482 Label::new(commit.subject.clone())
3483 .size(LabelSize::Small)
3484 .truncate(),
3485 )
3486 .id("commit-msg-hover")
3487 .on_click({
3488 let commit = commit.clone();
3489 let repo = active_repository.downgrade();
3490 move |_, window, cx| {
3491 CommitView::open(
3492 commit.clone(),
3493 repo.clone(),
3494 workspace.clone().clone(),
3495 window,
3496 cx,
3497 );
3498 }
3499 })
3500 .hoverable_tooltip({
3501 let repo = active_repository.clone();
3502 move |window, cx| {
3503 GitPanelMessageTooltip::new(
3504 this.clone(),
3505 commit.sha.clone(),
3506 repo.clone(),
3507 window,
3508 cx,
3509 )
3510 .into()
3511 }
3512 }),
3513 )
3514 .when(commit.has_parent, |this| {
3515 let has_unstaged = self.has_unstaged_changes();
3516 this.child(
3517 panel_icon_button("undo", IconName::Undo)
3518 .icon_size(IconSize::XSmall)
3519 .icon_color(Color::Muted)
3520 .tooltip(move |window, cx| {
3521 Tooltip::with_meta(
3522 "Uncommit",
3523 Some(&git::Uncommit),
3524 if has_unstaged {
3525 "git reset HEAD^ --soft"
3526 } else {
3527 "git reset HEAD^"
3528 },
3529 window,
3530 cx,
3531 )
3532 })
3533 .on_click(cx.listener(|this, _, window, cx| this.uncommit(window, cx))),
3534 )
3535 }),
3536 )
3537 }
3538
3539 fn render_empty_state(&self, cx: &mut Context<Self>) -> impl IntoElement {
3540 h_flex().h_full().flex_grow().justify_center().child(
3541 v_flex()
3542 .gap_2()
3543 .child(h_flex().w_full().justify_around().child(
3544 if self.active_repository.is_some() {
3545 "No changes to commit"
3546 } else {
3547 "No Git repositories"
3548 },
3549 ))
3550 .children({
3551 let worktree_count = self.project.read(cx).visible_worktrees(cx).count();
3552 (worktree_count > 0 && self.active_repository.is_none()).then(|| {
3553 h_flex().w_full().justify_around().child(
3554 panel_filled_button("Initialize Repository")
3555 .tooltip(Tooltip::for_action_title_in(
3556 "git init",
3557 &git::Init,
3558 &self.focus_handle,
3559 ))
3560 .on_click(move |_, _, cx| {
3561 cx.defer(move |cx| {
3562 cx.dispatch_action(&git::Init);
3563 })
3564 }),
3565 )
3566 })
3567 })
3568 .text_ui_sm(cx)
3569 .mx_auto()
3570 .text_color(Color::Placeholder.color(cx)),
3571 )
3572 }
3573
3574 fn render_vertical_scrollbar(
3575 &self,
3576 show_horizontal_scrollbar_container: bool,
3577 cx: &mut Context<Self>,
3578 ) -> impl IntoElement {
3579 div()
3580 .id("git-panel-vertical-scroll")
3581 .occlude()
3582 .flex_none()
3583 .h_full()
3584 .cursor_default()
3585 .absolute()
3586 .right_0()
3587 .top_0()
3588 .bottom_0()
3589 .w(px(12.))
3590 .when(show_horizontal_scrollbar_container, |this| {
3591 this.pb_neg_3p5()
3592 })
3593 .on_mouse_move(cx.listener(|_, _, _, cx| {
3594 cx.notify();
3595 cx.stop_propagation()
3596 }))
3597 .on_hover(|_, _, cx| {
3598 cx.stop_propagation();
3599 })
3600 .on_any_mouse_down(|_, _, cx| {
3601 cx.stop_propagation();
3602 })
3603 .on_mouse_up(
3604 MouseButton::Left,
3605 cx.listener(|this, _, window, cx| {
3606 if !this.vertical_scrollbar.state.is_dragging()
3607 && !this.focus_handle.contains_focused(window, cx)
3608 {
3609 this.vertical_scrollbar.hide(window, cx);
3610 cx.notify();
3611 }
3612
3613 cx.stop_propagation();
3614 }),
3615 )
3616 .on_scroll_wheel(cx.listener(|_, _, _, cx| {
3617 cx.notify();
3618 }))
3619 .children(Scrollbar::vertical(
3620 // percentage as f32..end_offset as f32,
3621 self.vertical_scrollbar.state.clone(),
3622 ))
3623 }
3624
3625 /// Renders the horizontal scrollbar.
3626 ///
3627 /// The right offset is used to determine how far to the right the
3628 /// scrollbar should extend to, useful for ensuring it doesn't collide
3629 /// with the vertical scrollbar when visible.
3630 fn render_horizontal_scrollbar(
3631 &self,
3632 right_offset: Pixels,
3633 cx: &mut Context<Self>,
3634 ) -> impl IntoElement {
3635 div()
3636 .id("git-panel-horizontal-scroll")
3637 .occlude()
3638 .flex_none()
3639 .w_full()
3640 .cursor_default()
3641 .absolute()
3642 .bottom_neg_px()
3643 .left_0()
3644 .right_0()
3645 .pr(right_offset)
3646 .on_mouse_move(cx.listener(|_, _, _, cx| {
3647 cx.notify();
3648 cx.stop_propagation()
3649 }))
3650 .on_hover(|_, _, cx| {
3651 cx.stop_propagation();
3652 })
3653 .on_any_mouse_down(|_, _, cx| {
3654 cx.stop_propagation();
3655 })
3656 .on_mouse_up(
3657 MouseButton::Left,
3658 cx.listener(|this, _, window, cx| {
3659 if !this.horizontal_scrollbar.state.is_dragging()
3660 && !this.focus_handle.contains_focused(window, cx)
3661 {
3662 this.horizontal_scrollbar.hide(window, cx);
3663 cx.notify();
3664 }
3665
3666 cx.stop_propagation();
3667 }),
3668 )
3669 .on_scroll_wheel(cx.listener(|_, _, _, cx| {
3670 cx.notify();
3671 }))
3672 .children(Scrollbar::horizontal(
3673 // percentage as f32..end_offset as f32,
3674 self.horizontal_scrollbar.state.clone(),
3675 ))
3676 }
3677
3678 fn render_buffer_header_controls(
3679 &self,
3680 entity: &Entity<Self>,
3681 file: &Arc<dyn File>,
3682 _: &Window,
3683 cx: &App,
3684 ) -> Option<AnyElement> {
3685 let repo = self.active_repository.as_ref()?.read(cx);
3686 let project_path = (file.worktree_id(cx), file.path()).into();
3687 let repo_path = repo.project_path_to_repo_path(&project_path, cx)?;
3688 let ix = self.entry_by_path(&repo_path, cx)?;
3689 let entry = self.entries.get(ix)?;
3690
3691 let entry_staging = self.entry_staging(entry.status_entry()?);
3692
3693 let checkbox = Checkbox::new("stage-file", entry_staging.as_bool().into())
3694 .disabled(!self.has_write_access(cx))
3695 .fill()
3696 .elevation(ElevationIndex::Surface)
3697 .on_click({
3698 let entry = entry.clone();
3699 let git_panel = entity.downgrade();
3700 move |_, window, cx| {
3701 git_panel
3702 .update(cx, |this, cx| {
3703 this.toggle_staged_for_entry(&entry, window, cx);
3704 cx.stop_propagation();
3705 })
3706 .ok();
3707 }
3708 });
3709 Some(
3710 h_flex()
3711 .id("start-slot")
3712 .text_lg()
3713 .child(checkbox)
3714 .on_mouse_down(MouseButton::Left, |_, _, cx| {
3715 // prevent the list item active state triggering when toggling checkbox
3716 cx.stop_propagation();
3717 })
3718 .into_any_element(),
3719 )
3720 }
3721
3722 fn render_entries(
3723 &self,
3724 has_write_access: bool,
3725 _: &Window,
3726 cx: &mut Context<Self>,
3727 ) -> impl IntoElement {
3728 let entry_count = self.entries.len();
3729
3730 let scroll_track_size = px(16.);
3731
3732 let h_scroll_offset = if self.vertical_scrollbar.show_scrollbar {
3733 // magic number
3734 px(3.)
3735 } else {
3736 px(0.)
3737 };
3738
3739 v_flex()
3740 .flex_1()
3741 .size_full()
3742 .overflow_hidden()
3743 .relative()
3744 // Show a border on the top and bottom of the container when
3745 // the vertical scrollbar container is visible so we don't have a
3746 // floating left border in the panel.
3747 .when(self.vertical_scrollbar.show_track, |this| {
3748 this.border_t_1()
3749 .border_b_1()
3750 .border_color(cx.theme().colors().border)
3751 })
3752 .child(
3753 h_flex()
3754 .flex_1()
3755 .size_full()
3756 .relative()
3757 .overflow_hidden()
3758 .child(
3759 uniform_list(
3760 "entries",
3761 entry_count,
3762 cx.processor(move |this, range: Range<usize>, window, cx| {
3763 let mut items = Vec::with_capacity(range.end - range.start);
3764
3765 for ix in range {
3766 match &this.entries.get(ix) {
3767 Some(GitListEntry::Status(entry)) => {
3768 items.push(this.render_entry(
3769 ix,
3770 entry,
3771 has_write_access,
3772 window,
3773 cx,
3774 ));
3775 }
3776 Some(GitListEntry::Header(header)) => {
3777 items.push(this.render_list_header(
3778 ix,
3779 header,
3780 has_write_access,
3781 window,
3782 cx,
3783 ));
3784 }
3785 None => {}
3786 }
3787 }
3788
3789 items
3790 }),
3791 )
3792 .when(
3793 !self.horizontal_scrollbar.show_track
3794 && self.horizontal_scrollbar.show_scrollbar,
3795 |this| {
3796 // when not showing the horizontal scrollbar track, make sure we don't
3797 // obscure the last entry
3798 this.pb(scroll_track_size)
3799 },
3800 )
3801 .size_full()
3802 .flex_grow()
3803 .with_sizing_behavior(ListSizingBehavior::Auto)
3804 .with_horizontal_sizing_behavior(
3805 ListHorizontalSizingBehavior::Unconstrained,
3806 )
3807 .with_width_from_item(self.max_width_item_index)
3808 .track_scroll(self.scroll_handle.clone()),
3809 )
3810 .on_mouse_down(
3811 MouseButton::Right,
3812 cx.listener(move |this, event: &MouseDownEvent, window, cx| {
3813 this.deploy_panel_context_menu(event.position, window, cx)
3814 }),
3815 )
3816 .when(self.vertical_scrollbar.show_track, |this| {
3817 this.child(
3818 v_flex()
3819 .h_full()
3820 .flex_none()
3821 .w(scroll_track_size)
3822 .bg(cx.theme().colors().panel_background)
3823 .child(
3824 div()
3825 .size_full()
3826 .flex_1()
3827 .border_l_1()
3828 .border_color(cx.theme().colors().border),
3829 ),
3830 )
3831 })
3832 .when(self.vertical_scrollbar.show_scrollbar, |this| {
3833 this.child(
3834 self.render_vertical_scrollbar(
3835 self.horizontal_scrollbar.show_track,
3836 cx,
3837 ),
3838 )
3839 }),
3840 )
3841 .when(self.horizontal_scrollbar.show_track, |this| {
3842 this.child(
3843 h_flex()
3844 .w_full()
3845 .h(scroll_track_size)
3846 .flex_none()
3847 .relative()
3848 .child(
3849 div()
3850 .w_full()
3851 .flex_1()
3852 // for some reason the horizontal scrollbar is 1px
3853 // taller than the vertical scrollbar??
3854 .h(scroll_track_size - px(1.))
3855 .bg(cx.theme().colors().panel_background)
3856 .border_t_1()
3857 .border_color(cx.theme().colors().border),
3858 )
3859 .when(self.vertical_scrollbar.show_track, |this| {
3860 this.child(
3861 div()
3862 .flex_none()
3863 // -1px prevents a missing pixel between the two container borders
3864 .w(scroll_track_size - px(1.))
3865 .h_full(),
3866 )
3867 .child(
3868 // HACK: Fill the missing 1px 🥲
3869 div()
3870 .absolute()
3871 .right(scroll_track_size - px(1.))
3872 .bottom(scroll_track_size - px(1.))
3873 .size_px()
3874 .bg(cx.theme().colors().border),
3875 )
3876 }),
3877 )
3878 })
3879 .when(self.horizontal_scrollbar.show_scrollbar, |this| {
3880 this.child(self.render_horizontal_scrollbar(h_scroll_offset, cx))
3881 })
3882 }
3883
3884 fn entry_label(&self, label: impl Into<SharedString>, color: Color) -> Label {
3885 Label::new(label.into()).color(color).single_line()
3886 }
3887
3888 fn list_item_height(&self) -> Rems {
3889 rems(1.75)
3890 }
3891
3892 fn render_list_header(
3893 &self,
3894 ix: usize,
3895 header: &GitHeaderEntry,
3896 _: bool,
3897 _: &Window,
3898 _: &Context<Self>,
3899 ) -> AnyElement {
3900 let id: ElementId = ElementId::Name(format!("header_{}", ix).into());
3901
3902 h_flex()
3903 .id(id)
3904 .h(self.list_item_height())
3905 .w_full()
3906 .items_end()
3907 .px(rems(0.75)) // ~12px
3908 .pb(rems(0.3125)) // ~ 5px
3909 .child(
3910 Label::new(header.title())
3911 .color(Color::Muted)
3912 .size(LabelSize::Small)
3913 .line_height_style(LineHeightStyle::UiLabel)
3914 .single_line(),
3915 )
3916 .into_any_element()
3917 }
3918
3919 pub fn load_commit_details(
3920 &self,
3921 sha: String,
3922 cx: &mut Context<Self>,
3923 ) -> Task<anyhow::Result<CommitDetails>> {
3924 let Some(repo) = self.active_repository.clone() else {
3925 return Task::ready(Err(anyhow::anyhow!("no active repo")));
3926 };
3927 repo.update(cx, |repo, cx| {
3928 let show = repo.show(sha);
3929 cx.spawn(async move |_, _| show.await?)
3930 })
3931 }
3932
3933 fn deploy_entry_context_menu(
3934 &mut self,
3935 position: Point<Pixels>,
3936 ix: usize,
3937 window: &mut Window,
3938 cx: &mut Context<Self>,
3939 ) {
3940 let Some(entry) = self.entries.get(ix).and_then(|e| e.status_entry()) else {
3941 return;
3942 };
3943 let stage_title = if entry.status.staging().is_fully_staged() {
3944 "Unstage File"
3945 } else {
3946 "Stage File"
3947 };
3948 let restore_title = if entry.status.is_created() {
3949 "Trash File"
3950 } else {
3951 "Restore File"
3952 };
3953 let context_menu = ContextMenu::build(window, cx, |context_menu, _, _| {
3954 context_menu
3955 .context(self.focus_handle.clone())
3956 .action(stage_title, ToggleStaged.boxed_clone())
3957 .action(restore_title, git::RestoreFile::default().boxed_clone())
3958 .separator()
3959 .action("Open Diff", Confirm.boxed_clone())
3960 .action("Open File", SecondaryConfirm.boxed_clone())
3961 });
3962 self.selected_entry = Some(ix);
3963 self.set_context_menu(context_menu, position, window, cx);
3964 }
3965
3966 fn deploy_panel_context_menu(
3967 &mut self,
3968 position: Point<Pixels>,
3969 window: &mut Window,
3970 cx: &mut Context<Self>,
3971 ) {
3972 let context_menu = git_panel_context_menu(
3973 self.focus_handle.clone(),
3974 GitMenuState {
3975 has_tracked_changes: self.has_tracked_changes(),
3976 has_staged_changes: self.has_staged_changes(),
3977 has_unstaged_changes: self.has_unstaged_changes(),
3978 has_new_changes: self.new_count > 0,
3979 },
3980 window,
3981 cx,
3982 );
3983 self.set_context_menu(context_menu, position, window, cx);
3984 }
3985
3986 fn set_context_menu(
3987 &mut self,
3988 context_menu: Entity<ContextMenu>,
3989 position: Point<Pixels>,
3990 window: &Window,
3991 cx: &mut Context<Self>,
3992 ) {
3993 let subscription = cx.subscribe_in(
3994 &context_menu,
3995 window,
3996 |this, _, _: &DismissEvent, window, cx| {
3997 if this.context_menu.as_ref().is_some_and(|context_menu| {
3998 context_menu.0.focus_handle(cx).contains_focused(window, cx)
3999 }) {
4000 cx.focus_self(window);
4001 }
4002 this.context_menu.take();
4003 cx.notify();
4004 },
4005 );
4006 self.context_menu = Some((context_menu, position, subscription));
4007 cx.notify();
4008 }
4009
4010 fn render_entry(
4011 &self,
4012 ix: usize,
4013 entry: &GitStatusEntry,
4014 has_write_access: bool,
4015 window: &Window,
4016 cx: &Context<Self>,
4017 ) -> AnyElement {
4018 let display_name = entry.display_name();
4019
4020 let selected = self.selected_entry == Some(ix);
4021 let marked = self.marked_entries.contains(&ix);
4022 let status_style = GitPanelSettings::get_global(cx).status_style;
4023 let status = entry.status;
4024
4025 let has_conflict = status.is_conflicted();
4026 let is_modified = status.is_modified();
4027 let is_deleted = status.is_deleted();
4028
4029 let label_color = if status_style == StatusStyle::LabelColor {
4030 if has_conflict {
4031 Color::VersionControlConflict
4032 } else if is_modified {
4033 Color::VersionControlModified
4034 } else if is_deleted {
4035 // We don't want a bunch of red labels in the list
4036 Color::Disabled
4037 } else {
4038 Color::VersionControlAdded
4039 }
4040 } else {
4041 Color::Default
4042 };
4043
4044 let path_color = if status.is_deleted() {
4045 Color::Disabled
4046 } else {
4047 Color::Muted
4048 };
4049
4050 let id: ElementId = ElementId::Name(format!("entry_{}_{}", display_name, ix).into());
4051 let checkbox_wrapper_id: ElementId =
4052 ElementId::Name(format!("entry_{}_{}_checkbox_wrapper", display_name, ix).into());
4053 let checkbox_id: ElementId =
4054 ElementId::Name(format!("entry_{}_{}_checkbox", display_name, ix).into());
4055
4056 let entry_staging = self.entry_staging(entry);
4057 let mut is_staged: ToggleState = self.entry_staging(entry).as_bool().into();
4058 if self.show_placeholders && !self.has_staged_changes() && !entry.status.is_created() {
4059 is_staged = ToggleState::Selected;
4060 }
4061
4062 let handle = cx.weak_entity();
4063
4064 let selected_bg_alpha = 0.08;
4065 let marked_bg_alpha = 0.12;
4066 let state_opacity_step = 0.04;
4067
4068 let base_bg = match (selected, marked) {
4069 (true, true) => cx
4070 .theme()
4071 .status()
4072 .info
4073 .alpha(selected_bg_alpha + marked_bg_alpha),
4074 (true, false) => cx.theme().status().info.alpha(selected_bg_alpha),
4075 (false, true) => cx.theme().status().info.alpha(marked_bg_alpha),
4076 _ => cx.theme().colors().ghost_element_background,
4077 };
4078
4079 let hover_bg = if selected {
4080 cx.theme()
4081 .status()
4082 .info
4083 .alpha(selected_bg_alpha + state_opacity_step)
4084 } else {
4085 cx.theme().colors().ghost_element_hover
4086 };
4087
4088 let active_bg = if selected {
4089 cx.theme()
4090 .status()
4091 .info
4092 .alpha(selected_bg_alpha + state_opacity_step * 2.0)
4093 } else {
4094 cx.theme().colors().ghost_element_active
4095 };
4096
4097 h_flex()
4098 .id(id)
4099 .h(self.list_item_height())
4100 .w_full()
4101 .items_center()
4102 .border_1()
4103 .when(selected && self.focus_handle.is_focused(window), |el| {
4104 el.border_color(cx.theme().colors().border_focused)
4105 })
4106 .px(rems(0.75)) // ~12px
4107 .overflow_hidden()
4108 .flex_none()
4109 .gap_1p5()
4110 .bg(base_bg)
4111 .hover(|this| this.bg(hover_bg))
4112 .active(|this| this.bg(active_bg))
4113 .on_click({
4114 cx.listener(move |this, event: &ClickEvent, window, cx| {
4115 this.selected_entry = Some(ix);
4116 cx.notify();
4117 if event.modifiers().secondary() {
4118 this.open_file(&Default::default(), window, cx)
4119 } else {
4120 this.open_diff(&Default::default(), window, cx);
4121 this.focus_handle.focus(window);
4122 }
4123 })
4124 })
4125 .on_mouse_down(
4126 MouseButton::Right,
4127 move |event: &MouseDownEvent, window, cx| {
4128 // why isn't this happening automatically? we are passing MouseButton::Right to `on_mouse_down`?
4129 if event.button != MouseButton::Right {
4130 return;
4131 }
4132
4133 let Some(this) = handle.upgrade() else {
4134 return;
4135 };
4136 this.update(cx, |this, cx| {
4137 this.deploy_entry_context_menu(event.position, ix, window, cx);
4138 });
4139 cx.stop_propagation();
4140 },
4141 )
4142 .child(
4143 div()
4144 .id(checkbox_wrapper_id)
4145 .flex_none()
4146 .occlude()
4147 .cursor_pointer()
4148 .child(
4149 Checkbox::new(checkbox_id, is_staged)
4150 .disabled(!has_write_access)
4151 .fill()
4152 .elevation(ElevationIndex::Surface)
4153 .on_click_ext({
4154 let entry = entry.clone();
4155 let this = cx.weak_entity();
4156 move |_, click, window, cx| {
4157 this.update(cx, |this, cx| {
4158 if !has_write_access {
4159 return;
4160 }
4161 if click.modifiers().shift {
4162 this.stage_bulk(ix, cx);
4163 } else {
4164 this.toggle_staged_for_entry(
4165 &GitListEntry::Status(entry.clone()),
4166 window,
4167 cx,
4168 );
4169 }
4170 cx.stop_propagation();
4171 })
4172 .ok();
4173 }
4174 })
4175 .tooltip(move |window, cx| {
4176 let is_staged = entry_staging.is_fully_staged();
4177
4178 let action = if is_staged { "Unstage" } else { "Stage" };
4179 let tooltip_name = action.to_string();
4180
4181 Tooltip::for_action(tooltip_name, &ToggleStaged, window, cx)
4182 }),
4183 ),
4184 )
4185 .child(git_status_icon(status))
4186 .child(
4187 h_flex()
4188 .items_center()
4189 .flex_1()
4190 // .overflow_hidden()
4191 .when_some(entry.parent_dir(), |this, parent| {
4192 if !parent.is_empty() {
4193 this.child(
4194 self.entry_label(format!("{}/", parent), path_color)
4195 .when(status.is_deleted(), |this| this.strikethrough()),
4196 )
4197 } else {
4198 this
4199 }
4200 })
4201 .child(
4202 self.entry_label(display_name.clone(), label_color)
4203 .when(status.is_deleted(), |this| this.strikethrough()),
4204 ),
4205 )
4206 .into_any_element()
4207 }
4208
4209 fn has_write_access(&self, cx: &App) -> bool {
4210 !self.project.read(cx).is_read_only(cx)
4211 }
4212
4213 pub fn amend_pending(&self) -> bool {
4214 self.amend_pending
4215 }
4216
4217 pub fn set_amend_pending(&mut self, value: bool, cx: &mut Context<Self>) {
4218 self.amend_pending = value;
4219 self.serialize(cx);
4220 cx.notify();
4221 }
4222
4223 pub fn signoff_enabled(&self) -> bool {
4224 self.signoff_enabled
4225 }
4226
4227 pub fn set_signoff_enabled(&mut self, value: bool, cx: &mut Context<Self>) {
4228 self.signoff_enabled = value;
4229 self.serialize(cx);
4230 cx.notify();
4231 }
4232
4233 pub fn toggle_signoff_enabled(
4234 &mut self,
4235 _: &Signoff,
4236 _window: &mut Window,
4237 cx: &mut Context<Self>,
4238 ) {
4239 self.set_signoff_enabled(!self.signoff_enabled, cx);
4240 }
4241
4242 pub async fn load(
4243 workspace: WeakEntity<Workspace>,
4244 mut cx: AsyncWindowContext,
4245 ) -> anyhow::Result<Entity<Self>> {
4246 let serialized_panel = match workspace
4247 .read_with(&cx, |workspace, _| Self::serialization_key(workspace))
4248 .ok()
4249 .flatten()
4250 {
4251 Some(serialization_key) => cx
4252 .background_spawn(async move { KEY_VALUE_STORE.read_kvp(&serialization_key) })
4253 .await
4254 .context("loading git panel")
4255 .log_err()
4256 .flatten()
4257 .map(|panel| serde_json::from_str::<SerializedGitPanel>(&panel))
4258 .transpose()
4259 .log_err()
4260 .flatten(),
4261 None => None,
4262 };
4263
4264 workspace.update_in(&mut cx, |workspace, window, cx| {
4265 let panel = GitPanel::new(workspace, window, cx);
4266
4267 if let Some(serialized_panel) = serialized_panel {
4268 panel.update(cx, |panel, cx| {
4269 panel.width = serialized_panel.width;
4270 panel.amend_pending = serialized_panel.amend_pending;
4271 panel.signoff_enabled = serialized_panel.signoff_enabled;
4272 cx.notify();
4273 })
4274 }
4275
4276 panel
4277 })
4278 }
4279
4280 fn stage_bulk(&mut self, mut index: usize, cx: &mut Context<'_, Self>) {
4281 let Some(op) = self.bulk_staging.as_ref() else {
4282 return;
4283 };
4284 let Some(mut anchor_index) = self.entry_by_path(&op.anchor, cx) else {
4285 return;
4286 };
4287 if let Some(entry) = self.entries.get(index)
4288 && let Some(entry) = entry.status_entry()
4289 {
4290 self.set_bulk_staging_anchor(entry.repo_path.clone(), cx);
4291 }
4292 if index < anchor_index {
4293 std::mem::swap(&mut index, &mut anchor_index);
4294 }
4295 let entries = self
4296 .entries
4297 .get(anchor_index..=index)
4298 .unwrap_or_default()
4299 .iter()
4300 .filter_map(|entry| entry.status_entry().cloned())
4301 .collect::<Vec<_>>();
4302 self.change_file_stage(true, entries, cx);
4303 }
4304
4305 fn set_bulk_staging_anchor(&mut self, path: RepoPath, cx: &mut Context<'_, GitPanel>) {
4306 let Some(repo) = self.active_repository.as_ref() else {
4307 return;
4308 };
4309 self.bulk_staging = Some(BulkStaging {
4310 repo_id: repo.read(cx).id,
4311 anchor: path,
4312 });
4313 }
4314
4315 pub(crate) fn toggle_amend_pending(&mut self, cx: &mut Context<Self>) {
4316 self.set_amend_pending(!self.amend_pending, cx);
4317 if self.amend_pending {
4318 self.load_last_commit_message_if_empty(cx);
4319 }
4320 }
4321}
4322
4323fn current_language_model(cx: &Context<'_, GitPanel>) -> Option<Arc<dyn LanguageModel>> {
4324 let is_enabled = agent_settings::AgentSettings::get_global(cx).enabled
4325 && !DisableAiSettings::get_global(cx).disable_ai;
4326
4327 is_enabled
4328 .then(|| {
4329 let ConfiguredModel { provider, model } =
4330 LanguageModelRegistry::read_global(cx).commit_message_model()?;
4331
4332 provider.is_authenticated(cx).then(|| model)
4333 })
4334 .flatten()
4335}
4336
4337impl Render for GitPanel {
4338 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
4339 let project = self.project.read(cx);
4340 let has_entries = self.entries.len() > 0;
4341 let room = self
4342 .workspace
4343 .upgrade()
4344 .and_then(|workspace| workspace.read(cx).active_call()?.read(cx).room().cloned());
4345
4346 let has_write_access = self.has_write_access(cx);
4347
4348 let has_co_authors = room.map_or(false, |room| {
4349 self.load_local_committer(cx);
4350 let room = room.read(cx);
4351 room.remote_participants()
4352 .values()
4353 .any(|remote_participant| remote_participant.can_write())
4354 });
4355
4356 v_flex()
4357 .id("git_panel")
4358 .key_context(self.dispatch_context(window, cx))
4359 .track_focus(&self.focus_handle)
4360 .when(has_write_access && !project.is_read_only(cx), |this| {
4361 this.on_action(cx.listener(Self::toggle_staged_for_selected))
4362 .on_action(cx.listener(Self::stage_range))
4363 .on_action(cx.listener(GitPanel::commit))
4364 .on_action(cx.listener(GitPanel::amend))
4365 .on_action(cx.listener(GitPanel::toggle_signoff_enabled))
4366 .on_action(cx.listener(Self::stage_all))
4367 .on_action(cx.listener(Self::unstage_all))
4368 .on_action(cx.listener(Self::stage_selected))
4369 .on_action(cx.listener(Self::unstage_selected))
4370 .on_action(cx.listener(Self::restore_tracked_files))
4371 .on_action(cx.listener(Self::revert_selected))
4372 .on_action(cx.listener(Self::clean_all))
4373 .on_action(cx.listener(Self::generate_commit_message_action))
4374 })
4375 .on_action(cx.listener(Self::select_first))
4376 .on_action(cx.listener(Self::select_next))
4377 .on_action(cx.listener(Self::select_previous))
4378 .on_action(cx.listener(Self::select_last))
4379 .on_action(cx.listener(Self::close_panel))
4380 .on_action(cx.listener(Self::open_diff))
4381 .on_action(cx.listener(Self::open_file))
4382 .on_action(cx.listener(Self::focus_changes_list))
4383 .on_action(cx.listener(Self::focus_editor))
4384 .on_action(cx.listener(Self::expand_commit_editor))
4385 .when(has_write_access && has_co_authors, |git_panel| {
4386 git_panel.on_action(cx.listener(Self::toggle_fill_co_authors))
4387 })
4388 .on_hover(cx.listener(move |this, hovered, window, cx| {
4389 if *hovered {
4390 this.horizontal_scrollbar.show(cx);
4391 this.vertical_scrollbar.show(cx);
4392 cx.notify();
4393 } else if !this.focus_handle.contains_focused(window, cx) {
4394 this.hide_scrollbars(window, cx);
4395 }
4396 }))
4397 .size_full()
4398 .overflow_hidden()
4399 .bg(cx.theme().colors().panel_background)
4400 .child(
4401 v_flex()
4402 .size_full()
4403 .children(self.render_panel_header(window, cx))
4404 .map(|this| {
4405 if has_entries {
4406 this.child(self.render_entries(has_write_access, window, cx))
4407 } else {
4408 this.child(self.render_empty_state(cx).into_any_element())
4409 }
4410 })
4411 .children(self.render_footer(window, cx))
4412 .when(self.amend_pending, |this| {
4413 this.child(self.render_pending_amend(cx))
4414 })
4415 .when(!self.amend_pending, |this| {
4416 this.children(self.render_previous_commit(cx))
4417 })
4418 .into_any_element(),
4419 )
4420 .children(self.context_menu.as_ref().map(|(menu, position, _)| {
4421 deferred(
4422 anchored()
4423 .position(*position)
4424 .anchor(Corner::TopLeft)
4425 .child(menu.clone()),
4426 )
4427 .with_priority(1)
4428 }))
4429 }
4430}
4431
4432impl Focusable for GitPanel {
4433 fn focus_handle(&self, cx: &App) -> gpui::FocusHandle {
4434 if self.entries.is_empty() {
4435 self.commit_editor.focus_handle(cx)
4436 } else {
4437 self.focus_handle.clone()
4438 }
4439 }
4440}
4441
4442impl EventEmitter<Event> for GitPanel {}
4443
4444impl EventEmitter<PanelEvent> for GitPanel {}
4445
4446pub(crate) struct GitPanelAddon {
4447 pub(crate) workspace: WeakEntity<Workspace>,
4448}
4449
4450impl editor::Addon for GitPanelAddon {
4451 fn to_any(&self) -> &dyn std::any::Any {
4452 self
4453 }
4454
4455 fn render_buffer_header_controls(
4456 &self,
4457 excerpt_info: &ExcerptInfo,
4458 window: &Window,
4459 cx: &App,
4460 ) -> Option<AnyElement> {
4461 let file = excerpt_info.buffer.file()?;
4462 let git_panel = self.workspace.upgrade()?.read(cx).panel::<GitPanel>(cx)?;
4463
4464 git_panel
4465 .read(cx)
4466 .render_buffer_header_controls(&git_panel, &file, window, cx)
4467 }
4468}
4469
4470impl Panel for GitPanel {
4471 fn persistent_name() -> &'static str {
4472 "GitPanel"
4473 }
4474
4475 fn position(&self, _: &Window, cx: &App) -> DockPosition {
4476 GitPanelSettings::get_global(cx).dock
4477 }
4478
4479 fn position_is_valid(&self, position: DockPosition) -> bool {
4480 matches!(position, DockPosition::Left | DockPosition::Right)
4481 }
4482
4483 fn set_position(&mut self, position: DockPosition, _: &mut Window, cx: &mut Context<Self>) {
4484 settings::update_settings_file::<GitPanelSettings>(
4485 self.fs.clone(),
4486 cx,
4487 move |settings, _| settings.dock = Some(position),
4488 );
4489 }
4490
4491 fn size(&self, _: &Window, cx: &App) -> Pixels {
4492 self.width
4493 .unwrap_or_else(|| GitPanelSettings::get_global(cx).default_width)
4494 }
4495
4496 fn set_size(&mut self, size: Option<Pixels>, _: &mut Window, cx: &mut Context<Self>) {
4497 self.width = size;
4498 self.serialize(cx);
4499 cx.notify();
4500 }
4501
4502 fn icon(&self, _: &Window, cx: &App) -> Option<ui::IconName> {
4503 Some(ui::IconName::GitBranchSmall).filter(|_| GitPanelSettings::get_global(cx).button)
4504 }
4505
4506 fn icon_tooltip(&self, _window: &Window, _cx: &App) -> Option<&'static str> {
4507 Some("Git Panel")
4508 }
4509
4510 fn toggle_action(&self) -> Box<dyn Action> {
4511 Box::new(ToggleFocus)
4512 }
4513
4514 fn activation_priority(&self) -> u32 {
4515 2
4516 }
4517}
4518
4519impl PanelHeader for GitPanel {}
4520
4521struct GitPanelMessageTooltip {
4522 commit_tooltip: Option<Entity<CommitTooltip>>,
4523}
4524
4525impl GitPanelMessageTooltip {
4526 fn new(
4527 git_panel: Entity<GitPanel>,
4528 sha: SharedString,
4529 repository: Entity<Repository>,
4530 window: &mut Window,
4531 cx: &mut App,
4532 ) -> Entity<Self> {
4533 cx.new(|cx| {
4534 cx.spawn_in(window, async move |this, cx| {
4535 let (details, workspace) = git_panel.update(cx, |git_panel, cx| {
4536 (
4537 git_panel.load_commit_details(sha.to_string(), cx),
4538 git_panel.workspace.clone(),
4539 )
4540 })?;
4541 let details = details.await?;
4542
4543 let commit_details = crate::commit_tooltip::CommitDetails {
4544 sha: details.sha.clone(),
4545 author_name: details.author_name.clone(),
4546 author_email: details.author_email.clone(),
4547 commit_time: OffsetDateTime::from_unix_timestamp(details.commit_timestamp)?,
4548 message: Some(ParsedCommitMessage {
4549 message: details.message.clone(),
4550 ..Default::default()
4551 }),
4552 };
4553
4554 this.update(cx, |this: &mut GitPanelMessageTooltip, cx| {
4555 this.commit_tooltip = Some(cx.new(move |cx| {
4556 CommitTooltip::new(commit_details, repository, workspace, cx)
4557 }));
4558 cx.notify();
4559 })
4560 })
4561 .detach();
4562
4563 Self {
4564 commit_tooltip: None,
4565 }
4566 })
4567 }
4568}
4569
4570impl Render for GitPanelMessageTooltip {
4571 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
4572 if let Some(commit_tooltip) = &self.commit_tooltip {
4573 commit_tooltip.clone().into_any_element()
4574 } else {
4575 gpui::Empty.into_any_element()
4576 }
4577 }
4578}
4579
4580#[derive(IntoElement, RegisterComponent)]
4581pub struct PanelRepoFooter {
4582 active_repository: SharedString,
4583 branch: Option<Branch>,
4584 head_commit: Option<CommitDetails>,
4585
4586 // Getting a GitPanel in previews will be difficult.
4587 //
4588 // For now just take an option here, and we won't bind handlers to buttons in previews.
4589 git_panel: Option<Entity<GitPanel>>,
4590}
4591
4592impl PanelRepoFooter {
4593 pub fn new(
4594 active_repository: SharedString,
4595 branch: Option<Branch>,
4596 head_commit: Option<CommitDetails>,
4597 git_panel: Option<Entity<GitPanel>>,
4598 ) -> Self {
4599 Self {
4600 active_repository,
4601 branch,
4602 head_commit,
4603 git_panel,
4604 }
4605 }
4606
4607 pub fn new_preview(active_repository: SharedString, branch: Option<Branch>) -> Self {
4608 Self {
4609 active_repository,
4610 branch,
4611 head_commit: None,
4612 git_panel: None,
4613 }
4614 }
4615}
4616
4617impl RenderOnce for PanelRepoFooter {
4618 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
4619 let project = self
4620 .git_panel
4621 .as_ref()
4622 .map(|panel| panel.read(cx).project.clone());
4623
4624 let repo = self
4625 .git_panel
4626 .as_ref()
4627 .and_then(|panel| panel.read(cx).active_repository.clone());
4628
4629 let single_repo = project
4630 .as_ref()
4631 .map(|project| project.read(cx).git_store().read(cx).repositories().len() == 1)
4632 .unwrap_or(true);
4633
4634 const MAX_BRANCH_LEN: usize = 16;
4635 const MAX_REPO_LEN: usize = 16;
4636 const LABEL_CHARACTER_BUDGET: usize = MAX_BRANCH_LEN + MAX_REPO_LEN;
4637 const MAX_SHORT_SHA_LEN: usize = 8;
4638
4639 let branch_name = self
4640 .branch
4641 .as_ref()
4642 .map(|branch| branch.name().to_owned())
4643 .or_else(|| {
4644 self.head_commit.as_ref().map(|commit| {
4645 commit
4646 .sha
4647 .chars()
4648 .take(MAX_SHORT_SHA_LEN)
4649 .collect::<String>()
4650 })
4651 })
4652 .unwrap_or_else(|| " (no branch)".to_owned());
4653 let show_separator = self.branch.is_some() || self.head_commit.is_some();
4654
4655 let active_repo_name = self.active_repository.clone();
4656
4657 let branch_actual_len = branch_name.len();
4658 let repo_actual_len = active_repo_name.len();
4659
4660 // ideally, show the whole branch and repo names but
4661 // when we can't, use a budget to allocate space between the two
4662 let (repo_display_len, branch_display_len) = if branch_actual_len + repo_actual_len
4663 <= LABEL_CHARACTER_BUDGET
4664 {
4665 (repo_actual_len, branch_actual_len)
4666 } else {
4667 if branch_actual_len <= MAX_BRANCH_LEN {
4668 let repo_space = (LABEL_CHARACTER_BUDGET - branch_actual_len).min(MAX_REPO_LEN);
4669 (repo_space, branch_actual_len)
4670 } else if repo_actual_len <= MAX_REPO_LEN {
4671 let branch_space = (LABEL_CHARACTER_BUDGET - repo_actual_len).min(MAX_BRANCH_LEN);
4672 (repo_actual_len, branch_space)
4673 } else {
4674 (MAX_REPO_LEN, MAX_BRANCH_LEN)
4675 }
4676 };
4677
4678 let truncated_repo_name = if repo_actual_len <= repo_display_len {
4679 active_repo_name.to_string()
4680 } else {
4681 util::truncate_and_trailoff(active_repo_name.trim_ascii(), repo_display_len)
4682 };
4683
4684 let truncated_branch_name = if branch_actual_len <= branch_display_len {
4685 branch_name.to_string()
4686 } else {
4687 util::truncate_and_trailoff(branch_name.trim_ascii(), branch_display_len)
4688 };
4689
4690 let repo_selector_trigger = Button::new("repo-selector", truncated_repo_name)
4691 .style(ButtonStyle::Transparent)
4692 .size(ButtonSize::None)
4693 .label_size(LabelSize::Small)
4694 .color(Color::Muted);
4695
4696 let repo_selector = PopoverMenu::new("repository-switcher")
4697 .menu({
4698 let project = project.clone();
4699 move |window, cx| {
4700 let project = project.clone()?;
4701 Some(cx.new(|cx| RepositorySelector::new(project, rems(16.), window, cx)))
4702 }
4703 })
4704 .trigger_with_tooltip(
4705 repo_selector_trigger.disabled(single_repo).truncate(true),
4706 Tooltip::text("Switch Active Repository"),
4707 )
4708 .anchor(Corner::BottomLeft)
4709 .into_any_element();
4710
4711 let branch_selector_button = Button::new("branch-selector", truncated_branch_name)
4712 .style(ButtonStyle::Transparent)
4713 .size(ButtonSize::None)
4714 .label_size(LabelSize::Small)
4715 .truncate(true)
4716 .tooltip(Tooltip::for_action_title(
4717 "Switch Branch",
4718 &zed_actions::git::Switch,
4719 ))
4720 .on_click(|_, window, cx| {
4721 window.dispatch_action(zed_actions::git::Switch.boxed_clone(), cx);
4722 });
4723
4724 let branch_selector = PopoverMenu::new("popover-button")
4725 .menu(move |window, cx| Some(branch_picker::popover(repo.clone(), window, cx)))
4726 .trigger_with_tooltip(
4727 branch_selector_button,
4728 Tooltip::for_action_title("Switch Branch", &zed_actions::git::Switch),
4729 )
4730 .anchor(Corner::BottomLeft)
4731 .offset(gpui::Point {
4732 x: px(0.0),
4733 y: px(-2.0),
4734 });
4735
4736 h_flex()
4737 .w_full()
4738 .px_2()
4739 .h(px(36.))
4740 .items_center()
4741 .justify_between()
4742 .gap_1()
4743 .child(
4744 h_flex()
4745 .flex_1()
4746 .overflow_hidden()
4747 .items_center()
4748 .child(
4749 div().child(
4750 Icon::new(IconName::GitBranchSmall)
4751 .size(IconSize::Small)
4752 .color(if single_repo {
4753 Color::Disabled
4754 } else {
4755 Color::Muted
4756 }),
4757 ),
4758 )
4759 .child(repo_selector)
4760 .when(show_separator, |this| {
4761 this.child(
4762 div()
4763 .text_color(cx.theme().colors().text_muted)
4764 .text_sm()
4765 .child("/"),
4766 )
4767 })
4768 .child(branch_selector),
4769 )
4770 .children(if let Some(git_panel) = self.git_panel {
4771 git_panel.update(cx, |git_panel, cx| git_panel.render_remote_button(cx))
4772 } else {
4773 None
4774 })
4775 }
4776}
4777
4778impl Component for PanelRepoFooter {
4779 fn scope() -> ComponentScope {
4780 ComponentScope::VersionControl
4781 }
4782
4783 fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement> {
4784 let unknown_upstream = None;
4785 let no_remote_upstream = Some(UpstreamTracking::Gone);
4786 let ahead_of_upstream = Some(
4787 UpstreamTrackingStatus {
4788 ahead: 2,
4789 behind: 0,
4790 }
4791 .into(),
4792 );
4793 let behind_upstream = Some(
4794 UpstreamTrackingStatus {
4795 ahead: 0,
4796 behind: 2,
4797 }
4798 .into(),
4799 );
4800 let ahead_and_behind_upstream = Some(
4801 UpstreamTrackingStatus {
4802 ahead: 3,
4803 behind: 1,
4804 }
4805 .into(),
4806 );
4807
4808 let not_ahead_or_behind_upstream = Some(
4809 UpstreamTrackingStatus {
4810 ahead: 0,
4811 behind: 0,
4812 }
4813 .into(),
4814 );
4815
4816 fn branch(upstream: Option<UpstreamTracking>) -> Branch {
4817 Branch {
4818 is_head: true,
4819 ref_name: "some-branch".into(),
4820 upstream: upstream.map(|tracking| Upstream {
4821 ref_name: "origin/some-branch".into(),
4822 tracking,
4823 }),
4824 most_recent_commit: Some(CommitSummary {
4825 sha: "abc123".into(),
4826 subject: "Modify stuff".into(),
4827 commit_timestamp: 1710932954,
4828 has_parent: true,
4829 }),
4830 }
4831 }
4832
4833 fn custom(branch_name: &str, upstream: Option<UpstreamTracking>) -> Branch {
4834 Branch {
4835 is_head: true,
4836 ref_name: branch_name.to_string().into(),
4837 upstream: upstream.map(|tracking| Upstream {
4838 ref_name: format!("zed/{}", branch_name).into(),
4839 tracking,
4840 }),
4841 most_recent_commit: Some(CommitSummary {
4842 sha: "abc123".into(),
4843 subject: "Modify stuff".into(),
4844 commit_timestamp: 1710932954,
4845 has_parent: true,
4846 }),
4847 }
4848 }
4849
4850 fn active_repository(id: usize) -> SharedString {
4851 format!("repo-{}", id).into()
4852 }
4853
4854 let example_width = px(340.);
4855 Some(
4856 v_flex()
4857 .gap_6()
4858 .w_full()
4859 .flex_none()
4860 .children(vec![
4861 example_group_with_title(
4862 "Action Button States",
4863 vec![
4864 single_example(
4865 "No Branch",
4866 div()
4867 .w(example_width)
4868 .overflow_hidden()
4869 .child(PanelRepoFooter::new_preview(
4870 active_repository(1).clone(),
4871 None,
4872 ))
4873 .into_any_element(),
4874 ),
4875 single_example(
4876 "Remote status unknown",
4877 div()
4878 .w(example_width)
4879 .overflow_hidden()
4880 .child(PanelRepoFooter::new_preview(
4881 active_repository(2).clone(),
4882 Some(branch(unknown_upstream)),
4883 ))
4884 .into_any_element(),
4885 ),
4886 single_example(
4887 "No Remote Upstream",
4888 div()
4889 .w(example_width)
4890 .overflow_hidden()
4891 .child(PanelRepoFooter::new_preview(
4892 active_repository(3).clone(),
4893 Some(branch(no_remote_upstream)),
4894 ))
4895 .into_any_element(),
4896 ),
4897 single_example(
4898 "Not Ahead or Behind",
4899 div()
4900 .w(example_width)
4901 .overflow_hidden()
4902 .child(PanelRepoFooter::new_preview(
4903 active_repository(4).clone(),
4904 Some(branch(not_ahead_or_behind_upstream)),
4905 ))
4906 .into_any_element(),
4907 ),
4908 single_example(
4909 "Behind remote",
4910 div()
4911 .w(example_width)
4912 .overflow_hidden()
4913 .child(PanelRepoFooter::new_preview(
4914 active_repository(5).clone(),
4915 Some(branch(behind_upstream)),
4916 ))
4917 .into_any_element(),
4918 ),
4919 single_example(
4920 "Ahead of remote",
4921 div()
4922 .w(example_width)
4923 .overflow_hidden()
4924 .child(PanelRepoFooter::new_preview(
4925 active_repository(6).clone(),
4926 Some(branch(ahead_of_upstream)),
4927 ))
4928 .into_any_element(),
4929 ),
4930 single_example(
4931 "Ahead and behind remote",
4932 div()
4933 .w(example_width)
4934 .overflow_hidden()
4935 .child(PanelRepoFooter::new_preview(
4936 active_repository(7).clone(),
4937 Some(branch(ahead_and_behind_upstream)),
4938 ))
4939 .into_any_element(),
4940 ),
4941 ],
4942 )
4943 .grow()
4944 .vertical(),
4945 ])
4946 .children(vec![
4947 example_group_with_title(
4948 "Labels",
4949 vec![
4950 single_example(
4951 "Short Branch & Repo",
4952 div()
4953 .w(example_width)
4954 .overflow_hidden()
4955 .child(PanelRepoFooter::new_preview(
4956 SharedString::from("zed"),
4957 Some(custom("main", behind_upstream)),
4958 ))
4959 .into_any_element(),
4960 ),
4961 single_example(
4962 "Long Branch",
4963 div()
4964 .w(example_width)
4965 .overflow_hidden()
4966 .child(PanelRepoFooter::new_preview(
4967 SharedString::from("zed"),
4968 Some(custom(
4969 "redesign-and-update-git-ui-list-entry-style",
4970 behind_upstream,
4971 )),
4972 ))
4973 .into_any_element(),
4974 ),
4975 single_example(
4976 "Long Repo",
4977 div()
4978 .w(example_width)
4979 .overflow_hidden()
4980 .child(PanelRepoFooter::new_preview(
4981 SharedString::from("zed-industries-community-examples"),
4982 Some(custom("gpui", ahead_of_upstream)),
4983 ))
4984 .into_any_element(),
4985 ),
4986 single_example(
4987 "Long Repo & Branch",
4988 div()
4989 .w(example_width)
4990 .overflow_hidden()
4991 .child(PanelRepoFooter::new_preview(
4992 SharedString::from("zed-industries-community-examples"),
4993 Some(custom(
4994 "redesign-and-update-git-ui-list-entry-style",
4995 behind_upstream,
4996 )),
4997 ))
4998 .into_any_element(),
4999 ),
5000 single_example(
5001 "Uppercase Repo",
5002 div()
5003 .w(example_width)
5004 .overflow_hidden()
5005 .child(PanelRepoFooter::new_preview(
5006 SharedString::from("LICENSES"),
5007 Some(custom("main", ahead_of_upstream)),
5008 ))
5009 .into_any_element(),
5010 ),
5011 single_example(
5012 "Uppercase Branch",
5013 div()
5014 .w(example_width)
5015 .overflow_hidden()
5016 .child(PanelRepoFooter::new_preview(
5017 SharedString::from("zed"),
5018 Some(custom("update-README", behind_upstream)),
5019 ))
5020 .into_any_element(),
5021 ),
5022 ],
5023 )
5024 .grow()
5025 .vertical(),
5026 ])
5027 .into_any_element(),
5028 )
5029 }
5030}
5031
5032#[cfg(test)]
5033mod tests {
5034 use git::status::{StatusCode, UnmergedStatus, UnmergedStatusCode};
5035 use gpui::{TestAppContext, VisualTestContext};
5036 use project::{FakeFs, WorktreeSettings};
5037 use serde_json::json;
5038 use settings::SettingsStore;
5039 use theme::LoadThemes;
5040 use util::path;
5041
5042 use super::*;
5043
5044 fn init_test(cx: &mut gpui::TestAppContext) {
5045 zlog::init_test();
5046
5047 cx.update(|cx| {
5048 let settings_store = SettingsStore::test(cx);
5049 cx.set_global(settings_store);
5050 AgentSettings::register(cx);
5051 WorktreeSettings::register(cx);
5052 workspace::init_settings(cx);
5053 theme::init(LoadThemes::JustBase, cx);
5054 language::init(cx);
5055 editor::init(cx);
5056 Project::init_settings(cx);
5057 client::DisableAiSettings::register(cx);
5058 crate::init(cx);
5059 });
5060 }
5061
5062 #[gpui::test]
5063 async fn test_entry_worktree_paths(cx: &mut TestAppContext) {
5064 init_test(cx);
5065 let fs = FakeFs::new(cx.background_executor.clone());
5066 fs.insert_tree(
5067 "/root",
5068 json!({
5069 "zed": {
5070 ".git": {},
5071 "crates": {
5072 "gpui": {
5073 "gpui.rs": "fn main() {}"
5074 },
5075 "util": {
5076 "util.rs": "fn do_it() {}"
5077 }
5078 }
5079 },
5080 }),
5081 )
5082 .await;
5083
5084 fs.set_status_for_repo(
5085 Path::new(path!("/root/zed/.git")),
5086 &[
5087 (
5088 Path::new("crates/gpui/gpui.rs"),
5089 StatusCode::Modified.worktree(),
5090 ),
5091 (
5092 Path::new("crates/util/util.rs"),
5093 StatusCode::Modified.worktree(),
5094 ),
5095 ],
5096 );
5097
5098 let project =
5099 Project::test(fs.clone(), [path!("/root/zed/crates/gpui").as_ref()], cx).await;
5100 let workspace =
5101 cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
5102 let cx = &mut VisualTestContext::from_window(*workspace, cx);
5103
5104 cx.read(|cx| {
5105 project
5106 .read(cx)
5107 .worktrees(cx)
5108 .nth(0)
5109 .unwrap()
5110 .read(cx)
5111 .as_local()
5112 .unwrap()
5113 .scan_complete()
5114 })
5115 .await;
5116
5117 cx.executor().run_until_parked();
5118
5119 let panel = workspace.update(cx, GitPanel::new).unwrap();
5120
5121 let handle = cx.update_window_entity(&panel, |panel, _, _| {
5122 std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
5123 });
5124 cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
5125 handle.await;
5126
5127 let entries = panel.read_with(cx, |panel, _| panel.entries.clone());
5128 pretty_assertions::assert_eq!(
5129 entries,
5130 [
5131 GitListEntry::Header(GitHeaderEntry {
5132 header: Section::Tracked
5133 }),
5134 GitListEntry::Status(GitStatusEntry {
5135 abs_path: path!("/root/zed/crates/gpui/gpui.rs").into(),
5136 repo_path: "crates/gpui/gpui.rs".into(),
5137 status: StatusCode::Modified.worktree(),
5138 staging: StageStatus::Unstaged,
5139 }),
5140 GitListEntry::Status(GitStatusEntry {
5141 abs_path: path!("/root/zed/crates/util/util.rs").into(),
5142 repo_path: "crates/util/util.rs".into(),
5143 status: StatusCode::Modified.worktree(),
5144 staging: StageStatus::Unstaged,
5145 },),
5146 ],
5147 );
5148
5149 let handle = cx.update_window_entity(&panel, |panel, _, _| {
5150 std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
5151 });
5152 cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
5153 handle.await;
5154 let entries = panel.read_with(cx, |panel, _| panel.entries.clone());
5155 pretty_assertions::assert_eq!(
5156 entries,
5157 [
5158 GitListEntry::Header(GitHeaderEntry {
5159 header: Section::Tracked
5160 }),
5161 GitListEntry::Status(GitStatusEntry {
5162 abs_path: path!("/root/zed/crates/gpui/gpui.rs").into(),
5163 repo_path: "crates/gpui/gpui.rs".into(),
5164 status: StatusCode::Modified.worktree(),
5165 staging: StageStatus::Unstaged,
5166 }),
5167 GitListEntry::Status(GitStatusEntry {
5168 abs_path: path!("/root/zed/crates/util/util.rs").into(),
5169 repo_path: "crates/util/util.rs".into(),
5170 status: StatusCode::Modified.worktree(),
5171 staging: StageStatus::Unstaged,
5172 },),
5173 ],
5174 );
5175 }
5176
5177 #[gpui::test]
5178 async fn test_bulk_staging(cx: &mut TestAppContext) {
5179 use GitListEntry::*;
5180
5181 init_test(cx);
5182 let fs = FakeFs::new(cx.background_executor.clone());
5183 fs.insert_tree(
5184 "/root",
5185 json!({
5186 "project": {
5187 ".git": {},
5188 "src": {
5189 "main.rs": "fn main() {}",
5190 "lib.rs": "pub fn hello() {}",
5191 "utils.rs": "pub fn util() {}"
5192 },
5193 "tests": {
5194 "test.rs": "fn test() {}"
5195 },
5196 "new_file.txt": "new content",
5197 "another_new.rs": "// new file",
5198 "conflict.txt": "conflicted content"
5199 }
5200 }),
5201 )
5202 .await;
5203
5204 fs.set_status_for_repo(
5205 Path::new(path!("/root/project/.git")),
5206 &[
5207 (Path::new("src/main.rs"), StatusCode::Modified.worktree()),
5208 (Path::new("src/lib.rs"), StatusCode::Modified.worktree()),
5209 (Path::new("tests/test.rs"), StatusCode::Modified.worktree()),
5210 (Path::new("new_file.txt"), FileStatus::Untracked),
5211 (Path::new("another_new.rs"), FileStatus::Untracked),
5212 (Path::new("src/utils.rs"), FileStatus::Untracked),
5213 (
5214 Path::new("conflict.txt"),
5215 UnmergedStatus {
5216 first_head: UnmergedStatusCode::Updated,
5217 second_head: UnmergedStatusCode::Updated,
5218 }
5219 .into(),
5220 ),
5221 ],
5222 );
5223
5224 let project = Project::test(fs.clone(), [Path::new(path!("/root/project"))], cx).await;
5225 let workspace =
5226 cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
5227 let cx = &mut VisualTestContext::from_window(*workspace, cx);
5228
5229 cx.read(|cx| {
5230 project
5231 .read(cx)
5232 .worktrees(cx)
5233 .nth(0)
5234 .unwrap()
5235 .read(cx)
5236 .as_local()
5237 .unwrap()
5238 .scan_complete()
5239 })
5240 .await;
5241
5242 cx.executor().run_until_parked();
5243
5244 let panel = workspace.update(cx, GitPanel::new).unwrap();
5245
5246 let handle = cx.update_window_entity(&panel, |panel, _, _| {
5247 std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
5248 });
5249 cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
5250 handle.await;
5251
5252 let entries = panel.read_with(cx, |panel, _| panel.entries.clone());
5253 #[rustfmt::skip]
5254 pretty_assertions::assert_matches!(
5255 entries.as_slice(),
5256 &[
5257 Header(GitHeaderEntry { header: Section::Conflict }),
5258 Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
5259 Header(GitHeaderEntry { header: Section::Tracked }),
5260 Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
5261 Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
5262 Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
5263 Header(GitHeaderEntry { header: Section::New }),
5264 Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
5265 Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
5266 Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
5267 ],
5268 );
5269
5270 let second_status_entry = entries[3].clone();
5271 panel.update_in(cx, |panel, window, cx| {
5272 panel.toggle_staged_for_entry(&second_status_entry, window, cx);
5273 });
5274
5275 panel.update_in(cx, |panel, window, cx| {
5276 panel.selected_entry = Some(7);
5277 panel.stage_range(&git::StageRange, window, cx);
5278 });
5279
5280 cx.read(|cx| {
5281 project
5282 .read(cx)
5283 .worktrees(cx)
5284 .nth(0)
5285 .unwrap()
5286 .read(cx)
5287 .as_local()
5288 .unwrap()
5289 .scan_complete()
5290 })
5291 .await;
5292
5293 cx.executor().run_until_parked();
5294
5295 let handle = cx.update_window_entity(&panel, |panel, _, _| {
5296 std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
5297 });
5298 cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
5299 handle.await;
5300
5301 let entries = panel.read_with(cx, |panel, _| panel.entries.clone());
5302 #[rustfmt::skip]
5303 pretty_assertions::assert_matches!(
5304 entries.as_slice(),
5305 &[
5306 Header(GitHeaderEntry { header: Section::Conflict }),
5307 Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
5308 Header(GitHeaderEntry { header: Section::Tracked }),
5309 Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
5310 Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
5311 Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
5312 Header(GitHeaderEntry { header: Section::New }),
5313 Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
5314 Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
5315 Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
5316 ],
5317 );
5318
5319 let third_status_entry = entries[4].clone();
5320 panel.update_in(cx, |panel, window, cx| {
5321 panel.toggle_staged_for_entry(&third_status_entry, window, cx);
5322 });
5323
5324 panel.update_in(cx, |panel, window, cx| {
5325 panel.selected_entry = Some(9);
5326 panel.stage_range(&git::StageRange, window, cx);
5327 });
5328
5329 cx.read(|cx| {
5330 project
5331 .read(cx)
5332 .worktrees(cx)
5333 .nth(0)
5334 .unwrap()
5335 .read(cx)
5336 .as_local()
5337 .unwrap()
5338 .scan_complete()
5339 })
5340 .await;
5341
5342 cx.executor().run_until_parked();
5343
5344 let handle = cx.update_window_entity(&panel, |panel, _, _| {
5345 std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
5346 });
5347 cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
5348 handle.await;
5349
5350 let entries = panel.read_with(cx, |panel, _| panel.entries.clone());
5351 #[rustfmt::skip]
5352 pretty_assertions::assert_matches!(
5353 entries.as_slice(),
5354 &[
5355 Header(GitHeaderEntry { header: Section::Conflict }),
5356 Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
5357 Header(GitHeaderEntry { header: Section::Tracked }),
5358 Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
5359 Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
5360 Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
5361 Header(GitHeaderEntry { header: Section::New }),
5362 Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
5363 Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
5364 Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
5365 ],
5366 );
5367 }
5368}