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