1use crate::git_panel_settings::StatusStyle;
2use crate::repository_selector::RepositorySelectorPopoverMenu;
3use crate::ProjectDiff;
4use crate::{
5 git_panel_settings::GitPanelSettings, git_status_icon, repository_selector::RepositorySelector,
6};
7use collections::HashMap;
8use db::kvp::KEY_VALUE_STORE;
9use editor::actions::MoveToEnd;
10use editor::scroll::ScrollbarAutoHide;
11use editor::{Editor, EditorMode, EditorSettings, MultiBuffer, ShowScrollbar};
12use git::repository::RepoPath;
13use git::status::FileStatus;
14use git::{Commit, ToggleStaged};
15use gpui::*;
16use language::{Buffer, File};
17use menu::{SelectFirst, SelectLast, SelectNext, SelectPrev};
18use multi_buffer::ExcerptInfo;
19use panel::PanelHeader;
20use project::git::{GitEvent, Repository};
21use project::{Fs, Project, ProjectPath};
22use serde::{Deserialize, Serialize};
23use settings::Settings as _;
24use std::{collections::HashSet, path::PathBuf, sync::Arc, time::Duration, usize};
25use theme::ThemeSettings;
26use ui::{
27 prelude::*, ButtonLike, Checkbox, Divider, DividerColor, ElevationIndex, IndentGuideColors,
28 ListItem, ListItemSpacing, Scrollbar, ScrollbarState, Tooltip,
29};
30use util::{maybe, ResultExt, TryFutureExt};
31use workspace::notifications::{DetachAndPromptErr, NotificationId};
32use workspace::Toast;
33use workspace::{
34 dock::{DockPosition, Panel, PanelEvent},
35 Workspace,
36};
37
38actions!(
39 git_panel,
40 [
41 Close,
42 ToggleFocus,
43 OpenMenu,
44 FocusEditor,
45 FocusChanges,
46 FillCoAuthors,
47 ]
48);
49
50const GIT_PANEL_KEY: &str = "GitPanel";
51
52const UPDATE_DEBOUNCE: Duration = Duration::from_millis(50);
53
54pub fn init(cx: &mut App) {
55 cx.observe_new(
56 |workspace: &mut Workspace, _window, _cx: &mut Context<Workspace>| {
57 workspace.register_action(|workspace, _: &ToggleFocus, window, cx| {
58 workspace.toggle_panel_focus::<GitPanel>(window, cx);
59 });
60
61 workspace.register_action(|workspace, _: &Commit, window, cx| {
62 workspace.open_panel::<GitPanel>(window, cx);
63 if let Some(git_panel) = workspace.panel::<GitPanel>(cx) {
64 git_panel
65 .read(cx)
66 .commit_editor
67 .focus_handle(cx)
68 .focus(window);
69 }
70 });
71 },
72 )
73 .detach();
74}
75
76#[derive(Debug, Clone)]
77pub enum Event {
78 Focus,
79 OpenedEntry { path: ProjectPath },
80}
81
82#[derive(Serialize, Deserialize)]
83struct SerializedGitPanel {
84 width: Option<Pixels>,
85}
86
87#[derive(Debug, PartialEq, Eq, Clone, Copy)]
88enum Section {
89 Conflict,
90 Tracked,
91 New,
92}
93
94#[derive(Debug, PartialEq, Eq, Clone)]
95struct GitHeaderEntry {
96 header: Section,
97}
98
99impl GitHeaderEntry {
100 pub fn contains(&self, status_entry: &GitStatusEntry, repo: &Repository) -> bool {
101 let this = &self.header;
102 let status = status_entry.status;
103 match this {
104 Section::Conflict => repo.has_conflict(&status_entry.repo_path),
105 Section::Tracked => !status.is_created(),
106 Section::New => status.is_created(),
107 }
108 }
109 pub fn title(&self) -> &'static str {
110 match self.header {
111 Section::Conflict => "Conflicts",
112 Section::Tracked => "Changed",
113 Section::New => "New",
114 }
115 }
116}
117
118#[derive(Debug, PartialEq, Eq, Clone)]
119enum GitListEntry {
120 GitStatusEntry(GitStatusEntry),
121 Header(GitHeaderEntry),
122}
123
124impl GitListEntry {
125 fn status_entry(&self) -> Option<&GitStatusEntry> {
126 match self {
127 GitListEntry::GitStatusEntry(entry) => Some(entry),
128 _ => None,
129 }
130 }
131}
132
133#[derive(Debug, PartialEq, Eq, Clone)]
134pub struct GitStatusEntry {
135 pub(crate) depth: usize,
136 pub(crate) display_name: String,
137 pub(crate) repo_path: RepoPath,
138 pub(crate) status: FileStatus,
139 pub(crate) is_staged: Option<bool>,
140}
141
142struct PendingOperation {
143 finished: bool,
144 will_become_staged: bool,
145 repo_paths: HashSet<RepoPath>,
146 op_id: usize,
147}
148
149pub struct GitPanel {
150 current_modifiers: Modifiers,
151 focus_handle: FocusHandle,
152 fs: Arc<dyn Fs>,
153 hide_scrollbar_task: Option<Task<()>>,
154 pending_serialization: Task<Option<()>>,
155 workspace: WeakEntity<Workspace>,
156 project: Entity<Project>,
157 active_repository: Option<Entity<Repository>>,
158 scroll_handle: UniformListScrollHandle,
159 scrollbar_state: ScrollbarState,
160 selected_entry: Option<usize>,
161 show_scrollbar: bool,
162 update_visible_entries_task: Task<()>,
163 repository_selector: Entity<RepositorySelector>,
164 commit_editor: Entity<Editor>,
165 entries: Vec<GitListEntry>,
166 entries_by_path: collections::HashMap<RepoPath, usize>,
167 width: Option<Pixels>,
168 pending: Vec<PendingOperation>,
169 pending_commit: Option<Task<()>>,
170
171 conflicted_staged_count: usize,
172 conflicted_count: usize,
173 tracked_staged_count: usize,
174 tracked_count: usize,
175 new_staged_count: usize,
176 new_count: usize,
177}
178
179fn commit_message_editor(
180 commit_message_buffer: Option<Entity<Buffer>>,
181 window: &mut Window,
182 cx: &mut Context<'_, Editor>,
183) -> Editor {
184 let theme = ThemeSettings::get_global(cx);
185
186 let mut text_style = window.text_style();
187 let refinement = TextStyleRefinement {
188 font_family: Some(theme.buffer_font.family.clone()),
189 font_features: Some(FontFeatures::disable_ligatures()),
190 font_size: Some(px(12.).into()),
191 color: Some(cx.theme().colors().editor_foreground),
192 background_color: Some(gpui::transparent_black()),
193 ..Default::default()
194 };
195 text_style.refine(&refinement);
196
197 let mut commit_editor = if let Some(commit_message_buffer) = commit_message_buffer {
198 let buffer = cx.new(|cx| MultiBuffer::singleton(commit_message_buffer, cx));
199 Editor::new(
200 EditorMode::AutoHeight { max_lines: 10 },
201 buffer,
202 None,
203 false,
204 window,
205 cx,
206 )
207 } else {
208 Editor::auto_height(10, window, cx)
209 };
210 commit_editor.set_use_autoclose(false);
211 commit_editor.set_show_gutter(false, cx);
212 commit_editor.set_show_wrap_guides(false, cx);
213 commit_editor.set_show_indent_guides(false, cx);
214 commit_editor.set_text_style_refinement(refinement);
215 commit_editor.set_placeholder_text("Enter commit message", cx);
216 commit_editor
217}
218
219impl GitPanel {
220 pub fn new(
221 workspace: &mut Workspace,
222 window: &mut Window,
223 commit_message_buffer: Option<Entity<Buffer>>,
224 cx: &mut Context<Workspace>,
225 ) -> Entity<Self> {
226 let fs = workspace.app_state().fs.clone();
227 let project = workspace.project().clone();
228 let git_state = project.read(cx).git_state().clone();
229 let active_repository = project.read(cx).active_repository(cx);
230 let workspace = cx.entity().downgrade();
231
232 let git_panel = cx.new(|cx| {
233 let focus_handle = cx.focus_handle();
234 cx.on_focus(&focus_handle, window, Self::focus_in).detach();
235 cx.on_focus_out(&focus_handle, window, |this, _, window, cx| {
236 this.hide_scrollbar(window, cx);
237 })
238 .detach();
239
240 let commit_editor =
241 cx.new(|cx| commit_message_editor(commit_message_buffer, window, cx));
242 commit_editor.update(cx, |editor, cx| {
243 editor.clear(window, cx);
244 });
245
246 let scroll_handle = UniformListScrollHandle::new();
247
248 cx.subscribe_in(
249 &git_state,
250 window,
251 move |this, git_state, event, window, cx| match event {
252 GitEvent::FileSystemUpdated => {
253 this.schedule_update(false, window, cx);
254 }
255 GitEvent::ActiveRepositoryChanged | GitEvent::GitStateUpdated => {
256 this.active_repository = git_state.read(cx).active_repository();
257 this.schedule_update(true, window, cx);
258 }
259 },
260 )
261 .detach();
262
263 let repository_selector =
264 cx.new(|cx| RepositorySelector::new(project.clone(), window, cx));
265
266 let mut git_panel = Self {
267 focus_handle: cx.focus_handle(),
268 pending_serialization: Task::ready(None),
269 entries: Vec::new(),
270 entries_by_path: HashMap::default(),
271 pending: Vec::new(),
272 current_modifiers: window.modifiers(),
273 width: Some(px(360.)),
274 scrollbar_state: ScrollbarState::new(scroll_handle.clone())
275 .parent_entity(&cx.entity()),
276 repository_selector,
277 selected_entry: None,
278 show_scrollbar: false,
279 hide_scrollbar_task: None,
280 update_visible_entries_task: Task::ready(()),
281 pending_commit: None,
282 active_repository,
283 scroll_handle,
284 fs,
285 commit_editor,
286 project,
287 workspace,
288 conflicted_count: 0,
289 conflicted_staged_count: 0,
290 tracked_staged_count: 0,
291 tracked_count: 0,
292 new_staged_count: 0,
293 new_count: 0,
294 };
295 git_panel.schedule_update(false, window, cx);
296 git_panel.show_scrollbar = git_panel.should_show_scrollbar(cx);
297 git_panel
298 });
299
300 cx.subscribe_in(
301 &git_panel,
302 window,
303 move |workspace, _, event: &Event, window, cx| match event.clone() {
304 Event::OpenedEntry { path } => {
305 workspace
306 .open_path_preview(path, None, false, false, window, cx)
307 .detach_and_prompt_err("Failed to open file", window, cx, |e, _, _| {
308 Some(format!("{e}"))
309 });
310 }
311 Event::Focus => { /* TODO */ }
312 },
313 )
314 .detach();
315
316 git_panel
317 }
318
319 pub fn select_entry_by_path(
320 &mut self,
321 path: ProjectPath,
322 _: &mut Window,
323 cx: &mut Context<Self>,
324 ) {
325 let Some(git_repo) = self.active_repository.as_ref() else {
326 return;
327 };
328 let Some(repo_path) = git_repo.read(cx).project_path_to_repo_path(&path) else {
329 return;
330 };
331 let Some(ix) = self.entries_by_path.get(&repo_path) else {
332 return;
333 };
334 self.selected_entry = Some(*ix);
335 cx.notify();
336 }
337
338 fn serialize(&mut self, cx: &mut Context<Self>) {
339 let width = self.width;
340 self.pending_serialization = cx.background_executor().spawn(
341 async move {
342 KEY_VALUE_STORE
343 .write_kvp(
344 GIT_PANEL_KEY.into(),
345 serde_json::to_string(&SerializedGitPanel { width })?,
346 )
347 .await?;
348 anyhow::Ok(())
349 }
350 .log_err(),
351 );
352 }
353
354 fn dispatch_context(&self, window: &mut Window, cx: &Context<Self>) -> KeyContext {
355 let mut dispatch_context = KeyContext::new_with_defaults();
356 dispatch_context.add("GitPanel");
357
358 if self.is_focused(window, cx) {
359 dispatch_context.add("menu");
360 dispatch_context.add("ChangesList");
361 }
362
363 if self.commit_editor.read(cx).is_focused(window) {
364 dispatch_context.add("CommitEditor");
365 }
366
367 dispatch_context
368 }
369
370 fn is_focused(&self, window: &Window, cx: &Context<Self>) -> bool {
371 window
372 .focused(cx)
373 .map_or(false, |focused| self.focus_handle == focused)
374 }
375
376 fn close_panel(&mut self, _: &Close, _window: &mut Window, cx: &mut Context<Self>) {
377 cx.emit(PanelEvent::Close);
378 }
379
380 fn focus_in(&mut self, window: &mut Window, cx: &mut Context<Self>) {
381 if !self.focus_handle.contains_focused(window, cx) {
382 cx.emit(Event::Focus);
383 }
384 }
385
386 fn show_scrollbar(&self, cx: &mut Context<Self>) -> ShowScrollbar {
387 GitPanelSettings::get_global(cx)
388 .scrollbar
389 .show
390 .unwrap_or_else(|| EditorSettings::get_global(cx).scrollbar.show)
391 }
392
393 fn should_show_scrollbar(&self, cx: &mut Context<Self>) -> bool {
394 let show = self.show_scrollbar(cx);
395 match show {
396 ShowScrollbar::Auto => true,
397 ShowScrollbar::System => true,
398 ShowScrollbar::Always => true,
399 ShowScrollbar::Never => false,
400 }
401 }
402
403 fn should_autohide_scrollbar(&self, cx: &mut Context<Self>) -> bool {
404 let show = self.show_scrollbar(cx);
405 match show {
406 ShowScrollbar::Auto => true,
407 ShowScrollbar::System => cx
408 .try_global::<ScrollbarAutoHide>()
409 .map_or_else(|| cx.should_auto_hide_scrollbars(), |autohide| autohide.0),
410 ShowScrollbar::Always => false,
411 ShowScrollbar::Never => true,
412 }
413 }
414
415 fn hide_scrollbar(&mut self, window: &mut Window, cx: &mut Context<Self>) {
416 const SCROLLBAR_SHOW_INTERVAL: Duration = Duration::from_secs(1);
417 if !self.should_autohide_scrollbar(cx) {
418 return;
419 }
420 self.hide_scrollbar_task = Some(cx.spawn_in(window, |panel, mut cx| async move {
421 cx.background_executor()
422 .timer(SCROLLBAR_SHOW_INTERVAL)
423 .await;
424 panel
425 .update(&mut cx, |panel, cx| {
426 panel.show_scrollbar = false;
427 cx.notify();
428 })
429 .log_err();
430 }))
431 }
432
433 fn handle_modifiers_changed(
434 &mut self,
435 event: &ModifiersChangedEvent,
436 _: &mut Window,
437 cx: &mut Context<Self>,
438 ) {
439 self.current_modifiers = event.modifiers;
440 cx.notify();
441 }
442
443 fn calculate_depth_and_difference(
444 repo_path: &RepoPath,
445 visible_entries: &HashSet<RepoPath>,
446 ) -> (usize, usize) {
447 let ancestors = repo_path.ancestors().skip(1);
448 for ancestor in ancestors {
449 if let Some(parent_entry) = visible_entries.get(ancestor) {
450 let entry_component_count = repo_path.components().count();
451 let parent_component_count = parent_entry.components().count();
452
453 let difference = entry_component_count - parent_component_count;
454
455 let parent_depth = parent_entry
456 .ancestors()
457 .skip(1) // Skip the parent itself
458 .filter(|ancestor| visible_entries.contains(*ancestor))
459 .count();
460
461 return (parent_depth + 1, difference);
462 }
463 }
464
465 (0, 0)
466 }
467
468 fn scroll_to_selected_entry(&mut self, cx: &mut Context<Self>) {
469 if let Some(selected_entry) = self.selected_entry {
470 self.scroll_handle
471 .scroll_to_item(selected_entry, ScrollStrategy::Center);
472 }
473
474 cx.notify();
475 }
476
477 fn select_first(&mut self, _: &SelectFirst, _window: &mut Window, cx: &mut Context<Self>) {
478 if self.entries.first().is_some() {
479 self.selected_entry = Some(0);
480 self.scroll_to_selected_entry(cx);
481 }
482 }
483
484 fn select_prev(&mut self, _: &SelectPrev, _window: &mut Window, cx: &mut Context<Self>) {
485 let item_count = self.entries.len();
486 if item_count == 0 {
487 return;
488 }
489
490 if let Some(selected_entry) = self.selected_entry {
491 let new_selected_entry = if selected_entry > 0 {
492 selected_entry - 1
493 } else {
494 selected_entry
495 };
496
497 self.selected_entry = Some(new_selected_entry);
498
499 self.scroll_to_selected_entry(cx);
500 }
501
502 cx.notify();
503 }
504
505 fn select_next(&mut self, _: &SelectNext, _window: &mut Window, cx: &mut Context<Self>) {
506 let item_count = self.entries.len();
507 if item_count == 0 {
508 return;
509 }
510
511 if let Some(selected_entry) = self.selected_entry {
512 let new_selected_entry = if selected_entry < item_count - 1 {
513 selected_entry + 1
514 } else {
515 selected_entry
516 };
517
518 self.selected_entry = Some(new_selected_entry);
519
520 self.scroll_to_selected_entry(cx);
521 }
522
523 cx.notify();
524 }
525
526 fn select_last(&mut self, _: &SelectLast, _window: &mut Window, cx: &mut Context<Self>) {
527 if self.entries.last().is_some() {
528 self.selected_entry = Some(self.entries.len() - 1);
529 self.scroll_to_selected_entry(cx);
530 }
531 }
532
533 fn focus_editor(&mut self, _: &FocusEditor, window: &mut Window, cx: &mut Context<Self>) {
534 self.commit_editor.update(cx, |editor, cx| {
535 window.focus(&editor.focus_handle(cx));
536 });
537 cx.notify();
538 }
539
540 fn select_first_entry_if_none(&mut self, cx: &mut Context<Self>) {
541 let have_entries = self
542 .active_repository
543 .as_ref()
544 .map_or(false, |active_repository| {
545 active_repository.read(cx).entry_count() > 0
546 });
547 if have_entries && self.selected_entry.is_none() {
548 self.selected_entry = Some(0);
549 self.scroll_to_selected_entry(cx);
550 cx.notify();
551 }
552 }
553
554 fn focus_changes_list(
555 &mut self,
556 _: &FocusChanges,
557 window: &mut Window,
558 cx: &mut Context<Self>,
559 ) {
560 self.select_first_entry_if_none(cx);
561
562 cx.focus_self(window);
563 cx.notify();
564 }
565
566 fn get_selected_entry(&self) -> Option<&GitListEntry> {
567 self.selected_entry.and_then(|i| self.entries.get(i))
568 }
569
570 fn open_selected(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
571 maybe!({
572 let entry = self.entries.get(self.selected_entry?)?.status_entry()?;
573
574 self.workspace
575 .update(cx, |workspace, cx| {
576 ProjectDiff::deploy_at(workspace, Some(entry.clone()), window, cx);
577 })
578 .ok()
579 });
580 self.focus_handle.focus(window);
581 }
582
583 fn toggle_staged_for_entry(
584 &mut self,
585 entry: &GitListEntry,
586 _window: &mut Window,
587 cx: &mut Context<Self>,
588 ) {
589 let Some(active_repository) = self.active_repository.as_ref() else {
590 return;
591 };
592 let (stage, repo_paths) = match entry {
593 GitListEntry::GitStatusEntry(status_entry) => {
594 if status_entry.status.is_staged().unwrap_or(false) {
595 (false, vec![status_entry.repo_path.clone()])
596 } else {
597 (true, vec![status_entry.repo_path.clone()])
598 }
599 }
600 GitListEntry::Header(section) => {
601 let goal_staged_state = !self.header_state(section.header).selected();
602 let repository = active_repository.read(cx);
603 let entries = self
604 .entries
605 .iter()
606 .filter_map(|entry| entry.status_entry())
607 .filter(|status_entry| {
608 section.contains(&status_entry, repository)
609 && status_entry.is_staged != Some(goal_staged_state)
610 })
611 .map(|status_entry| status_entry.repo_path.clone())
612 .collect::<Vec<_>>();
613
614 (goal_staged_state, entries)
615 }
616 };
617
618 let op_id = self.pending.iter().map(|p| p.op_id).max().unwrap_or(0) + 1;
619 self.pending.push(PendingOperation {
620 op_id,
621 will_become_staged: stage,
622 repo_paths: repo_paths.iter().cloned().collect(),
623 finished: false,
624 });
625 let repo_paths = repo_paths.clone();
626 let active_repository = active_repository.clone();
627 let repository = active_repository.read(cx);
628 self.update_counts(repository);
629 cx.notify();
630
631 cx.spawn({
632 |this, mut cx| async move {
633 let result = cx
634 .update(|cx| {
635 if stage {
636 active_repository.read(cx).stage_entries(repo_paths.clone())
637 } else {
638 active_repository
639 .read(cx)
640 .unstage_entries(repo_paths.clone())
641 }
642 })?
643 .await?;
644
645 this.update(&mut cx, |this, cx| {
646 for pending in this.pending.iter_mut() {
647 if pending.op_id == op_id {
648 pending.finished = true
649 }
650 }
651 result
652 .map_err(|e| {
653 this.show_err_toast(e, cx);
654 })
655 .ok();
656 cx.notify();
657 })
658 }
659 })
660 .detach();
661 }
662
663 fn toggle_staged_for_selected(
664 &mut self,
665 _: &git::ToggleStaged,
666 window: &mut Window,
667 cx: &mut Context<Self>,
668 ) {
669 if let Some(selected_entry) = self.get_selected_entry().cloned() {
670 self.toggle_staged_for_entry(&selected_entry, window, cx);
671 }
672 }
673
674 /// Commit all staged changes
675 fn commit(&mut self, _: &git::Commit, window: &mut Window, cx: &mut Context<Self>) {
676 let editor = self.commit_editor.read(cx);
677 if editor.is_empty(cx) {
678 if !editor.focus_handle(cx).contains_focused(window, cx) {
679 editor.focus_handle(cx).focus(window);
680 return;
681 }
682 }
683
684 self.commit_changes(window, cx)
685 }
686
687 fn commit_changes(&mut self, window: &mut Window, cx: &mut Context<Self>) {
688 let Some(active_repository) = self.active_repository.clone() else {
689 return;
690 };
691 let error_spawn = |message, window: &mut Window, cx: &mut App| {
692 let prompt = window.prompt(PromptLevel::Warning, message, None, &["Ok"], cx);
693 cx.spawn(|_| async move {
694 prompt.await.ok();
695 })
696 .detach();
697 };
698
699 if self.has_unstaged_conflicts() {
700 error_spawn(
701 "There are still conflicts. You must stage these before committing",
702 window,
703 cx,
704 );
705 return;
706 }
707
708 let message = self.commit_editor.read(cx).text(cx);
709 if message.trim().is_empty() {
710 self.commit_editor.read(cx).focus_handle(cx).focus(window);
711 return;
712 }
713
714 let task = if self.has_staged_changes() {
715 // Repository serializes all git operations, so we can just send a commit immediately
716 let commit_task = active_repository.read(cx).commit(message.into(), None);
717 cx.background_executor()
718 .spawn(async move { commit_task.await? })
719 } else {
720 let changed_files = self
721 .entries
722 .iter()
723 .filter_map(|entry| entry.status_entry())
724 .filter(|status_entry| !status_entry.status.is_created())
725 .map(|status_entry| status_entry.repo_path.clone())
726 .collect::<Vec<_>>();
727
728 if changed_files.is_empty() {
729 error_spawn("No changes to commit", window, cx);
730 return;
731 }
732
733 let stage_task = active_repository.read(cx).stage_entries(changed_files);
734 cx.spawn(|_, mut cx| async move {
735 stage_task.await??;
736 let commit_task = active_repository
737 .update(&mut cx, |repo, _| repo.commit(message.into(), None))?;
738 commit_task.await?
739 })
740 };
741 let task = cx.spawn_in(window, |this, mut cx| async move {
742 let result = task.await;
743 this.update_in(&mut cx, |this, window, cx| {
744 this.pending_commit.take();
745 match result {
746 Ok(()) => {
747 this.commit_editor
748 .update(cx, |editor, cx| editor.clear(window, cx));
749 }
750 Err(e) => this.show_err_toast(e, cx),
751 }
752 })
753 .ok();
754 });
755
756 self.pending_commit = Some(task);
757 }
758
759 fn fill_co_authors(&mut self, _: &FillCoAuthors, window: &mut Window, cx: &mut Context<Self>) {
760 const CO_AUTHOR_PREFIX: &str = "Co-authored-by: ";
761
762 let Some(room) = self
763 .workspace
764 .upgrade()
765 .and_then(|workspace| workspace.read(cx).active_call()?.read(cx).room().cloned())
766 else {
767 return;
768 };
769
770 let mut existing_text = self.commit_editor.read(cx).text(cx);
771 existing_text.make_ascii_lowercase();
772 let lowercase_co_author_prefix = CO_AUTHOR_PREFIX.to_lowercase();
773 let mut ends_with_co_authors = false;
774 let existing_co_authors = existing_text
775 .lines()
776 .filter_map(|line| {
777 let line = line.trim();
778 if line.starts_with(&lowercase_co_author_prefix) {
779 ends_with_co_authors = true;
780 Some(line)
781 } else {
782 ends_with_co_authors = false;
783 None
784 }
785 })
786 .collect::<HashSet<_>>();
787
788 let new_co_authors = room
789 .read(cx)
790 .remote_participants()
791 .values()
792 .filter(|participant| participant.can_write())
793 .map(|participant| participant.user.as_ref())
794 .filter_map(|user| {
795 let email = user.email.as_deref()?;
796 let name = user.name.as_deref().unwrap_or(&user.github_login);
797 Some(format!("{CO_AUTHOR_PREFIX}{name} <{email}>"))
798 })
799 .filter(|co_author| {
800 !existing_co_authors.contains(co_author.to_ascii_lowercase().as_str())
801 })
802 .collect::<Vec<_>>();
803 if new_co_authors.is_empty() {
804 return;
805 }
806
807 self.commit_editor.update(cx, |editor, cx| {
808 let editor_end = editor.buffer().read(cx).read(cx).len();
809 let mut edit = String::new();
810 if !ends_with_co_authors {
811 edit.push('\n');
812 }
813 for co_author in new_co_authors {
814 edit.push('\n');
815 edit.push_str(&co_author);
816 }
817
818 editor.edit(Some((editor_end..editor_end, edit)), cx);
819 editor.move_to_end(&MoveToEnd, window, cx);
820 editor.focus_handle(cx).focus(window);
821 });
822 }
823
824 fn schedule_update(
825 &mut self,
826 clear_pending: bool,
827 window: &mut Window,
828 cx: &mut Context<Self>,
829 ) {
830 let handle = cx.entity().downgrade();
831 self.reopen_commit_buffer(window, cx);
832 self.update_visible_entries_task = cx.spawn_in(window, |_, mut cx| async move {
833 cx.background_executor().timer(UPDATE_DEBOUNCE).await;
834 if let Some(git_panel) = handle.upgrade() {
835 git_panel
836 .update_in(&mut cx, |git_panel, _, cx| {
837 if clear_pending {
838 git_panel.clear_pending();
839 }
840 git_panel.update_visible_entries(cx);
841 })
842 .ok();
843 }
844 });
845 }
846
847 fn reopen_commit_buffer(&mut self, window: &mut Window, cx: &mut Context<Self>) {
848 let Some(active_repo) = self.active_repository.as_ref() else {
849 return;
850 };
851 let load_buffer = active_repo.update(cx, |active_repo, cx| {
852 let project = self.project.read(cx);
853 active_repo.open_commit_buffer(
854 Some(project.languages().clone()),
855 project.buffer_store().clone(),
856 cx,
857 )
858 });
859
860 cx.spawn_in(window, |git_panel, mut cx| async move {
861 let buffer = load_buffer.await?;
862 git_panel.update_in(&mut cx, |git_panel, window, cx| {
863 if git_panel
864 .commit_editor
865 .read(cx)
866 .buffer()
867 .read(cx)
868 .as_singleton()
869 .as_ref()
870 != Some(&buffer)
871 {
872 git_panel.commit_editor =
873 cx.new(|cx| commit_message_editor(Some(buffer), window, cx));
874 }
875 })
876 })
877 .detach_and_log_err(cx);
878 }
879
880 fn clear_pending(&mut self) {
881 self.pending.retain(|v| !v.finished)
882 }
883
884 fn update_visible_entries(&mut self, cx: &mut Context<Self>) {
885 self.entries.clear();
886 self.entries_by_path.clear();
887 let mut changed_entries = Vec::new();
888 let mut new_entries = Vec::new();
889 let mut conflict_entries = Vec::new();
890
891 let Some(repo) = self.active_repository.as_ref() else {
892 // Just clear entries if no repository is active.
893 cx.notify();
894 return;
895 };
896
897 // First pass - collect all paths
898 let repo = repo.read(cx);
899 let path_set = HashSet::from_iter(repo.status().map(|entry| entry.repo_path));
900
901 // Second pass - create entries with proper depth calculation
902 for entry in repo.status() {
903 let (depth, difference) =
904 Self::calculate_depth_and_difference(&entry.repo_path, &path_set);
905
906 let is_conflict = repo.has_conflict(&entry.repo_path);
907 let is_new = entry.status.is_created();
908 let is_staged = entry.status.is_staged();
909
910 let display_name = if difference > 1 {
911 // Show partial path for deeply nested files
912 entry
913 .repo_path
914 .as_ref()
915 .iter()
916 .skip(entry.repo_path.components().count() - difference)
917 .collect::<PathBuf>()
918 .to_string_lossy()
919 .into_owned()
920 } else {
921 // Just show filename
922 entry
923 .repo_path
924 .file_name()
925 .map(|name| name.to_string_lossy().into_owned())
926 .unwrap_or_default()
927 };
928
929 let entry = GitStatusEntry {
930 depth,
931 display_name,
932 repo_path: entry.repo_path.clone(),
933 status: entry.status,
934 is_staged,
935 };
936
937 if is_conflict {
938 conflict_entries.push(entry);
939 } else if is_new {
940 new_entries.push(entry);
941 } else {
942 changed_entries.push(entry);
943 }
944 }
945
946 // Sort entries by path to maintain consistent order
947 conflict_entries.sort_by(|a, b| a.repo_path.cmp(&b.repo_path));
948 changed_entries.sort_by(|a, b| a.repo_path.cmp(&b.repo_path));
949 new_entries.sort_by(|a, b| a.repo_path.cmp(&b.repo_path));
950
951 if conflict_entries.len() > 0 {
952 self.entries.push(GitListEntry::Header(GitHeaderEntry {
953 header: Section::Conflict,
954 }));
955 self.entries.extend(
956 conflict_entries
957 .into_iter()
958 .map(GitListEntry::GitStatusEntry),
959 );
960 }
961
962 if changed_entries.len() > 0 {
963 self.entries.push(GitListEntry::Header(GitHeaderEntry {
964 header: Section::Tracked,
965 }));
966 self.entries.extend(
967 changed_entries
968 .into_iter()
969 .map(GitListEntry::GitStatusEntry),
970 );
971 }
972 if new_entries.len() > 0 {
973 self.entries.push(GitListEntry::Header(GitHeaderEntry {
974 header: Section::New,
975 }));
976 self.entries
977 .extend(new_entries.into_iter().map(GitListEntry::GitStatusEntry));
978 }
979
980 for (ix, entry) in self.entries.iter().enumerate() {
981 if let Some(status_entry) = entry.status_entry() {
982 self.entries_by_path
983 .insert(status_entry.repo_path.clone(), ix);
984 }
985 }
986 self.update_counts(repo);
987
988 self.select_first_entry_if_none(cx);
989
990 cx.notify();
991 }
992
993 fn update_counts(&mut self, repo: &Repository) {
994 self.conflicted_count = 0;
995 self.conflicted_staged_count = 0;
996 self.new_count = 0;
997 self.tracked_count = 0;
998 self.new_staged_count = 0;
999 self.tracked_staged_count = 0;
1000 for entry in &self.entries {
1001 let Some(status_entry) = entry.status_entry() else {
1002 continue;
1003 };
1004 if repo.has_conflict(&status_entry.repo_path) {
1005 self.conflicted_count += 1;
1006 if self.entry_is_staged(status_entry) != Some(false) {
1007 self.conflicted_staged_count += 1;
1008 }
1009 } else if status_entry.status.is_created() {
1010 self.new_count += 1;
1011 if self.entry_is_staged(status_entry) != Some(false) {
1012 self.new_staged_count += 1;
1013 }
1014 } else {
1015 self.tracked_count += 1;
1016 if self.entry_is_staged(status_entry) != Some(false) {
1017 self.tracked_staged_count += 1;
1018 }
1019 }
1020 }
1021 }
1022
1023 fn entry_is_staged(&self, entry: &GitStatusEntry) -> Option<bool> {
1024 for pending in self.pending.iter().rev() {
1025 if pending.repo_paths.contains(&entry.repo_path) {
1026 return Some(pending.will_become_staged);
1027 }
1028 }
1029 entry.is_staged
1030 }
1031
1032 fn has_staged_changes(&self) -> bool {
1033 self.tracked_staged_count > 0
1034 || self.new_staged_count > 0
1035 || self.conflicted_staged_count > 0
1036 }
1037
1038 fn has_tracked_changes(&self) -> bool {
1039 self.tracked_count > 0
1040 }
1041
1042 fn has_unstaged_conflicts(&self) -> bool {
1043 self.conflicted_count > 0 && self.conflicted_count != self.conflicted_staged_count
1044 }
1045
1046 fn header_state(&self, header_type: Section) -> ToggleState {
1047 let (staged_count, count) = match header_type {
1048 Section::New => (self.new_staged_count, self.new_count),
1049 Section::Tracked => (self.tracked_staged_count, self.tracked_count),
1050 Section::Conflict => (self.conflicted_staged_count, self.conflicted_count),
1051 };
1052 if staged_count == 0 {
1053 ToggleState::Unselected
1054 } else if count == staged_count {
1055 ToggleState::Selected
1056 } else {
1057 ToggleState::Indeterminate
1058 }
1059 }
1060
1061 fn show_err_toast(&self, e: anyhow::Error, cx: &mut App) {
1062 let Some(workspace) = self.workspace.upgrade() else {
1063 return;
1064 };
1065 let notif_id = NotificationId::Named("git-operation-error".into());
1066
1067 let message = e.to_string();
1068 workspace.update(cx, |workspace, cx| {
1069 let toast = Toast::new(notif_id, message).on_click("Open Zed Log", |window, cx| {
1070 window.dispatch_action(workspace::OpenLog.boxed_clone(), cx);
1071 });
1072 workspace.show_toast(toast, cx);
1073 });
1074 }
1075
1076 pub fn panel_button(
1077 &self,
1078 id: impl Into<SharedString>,
1079 label: impl Into<SharedString>,
1080 ) -> Button {
1081 let id = id.into().clone();
1082 let label = label.into().clone();
1083
1084 Button::new(id, label)
1085 .label_size(LabelSize::Small)
1086 .layer(ElevationIndex::ElevatedSurface)
1087 .size(ButtonSize::Compact)
1088 .style(ButtonStyle::Filled)
1089 }
1090
1091 pub fn indent_size(&self, window: &Window, cx: &mut Context<Self>) -> Pixels {
1092 Checkbox::container_size(cx).to_pixels(window.rem_size())
1093 }
1094
1095 pub fn render_divider(&self, _cx: &mut Context<Self>) -> impl IntoElement {
1096 h_flex()
1097 .items_center()
1098 .h(px(8.))
1099 .child(Divider::horizontal_dashed().color(DividerColor::Border))
1100 }
1101
1102 pub fn render_panel_header(
1103 &self,
1104 window: &mut Window,
1105 cx: &mut Context<Self>,
1106 ) -> impl IntoElement {
1107 let all_repositories = self
1108 .project
1109 .read(cx)
1110 .git_state()
1111 .read(cx)
1112 .all_repositories();
1113 let entry_count = self
1114 .active_repository
1115 .as_ref()
1116 .map_or(0, |repo| repo.read(cx).entry_count());
1117
1118 let changes_string = match entry_count {
1119 0 => "No changes".to_string(),
1120 1 => "1 change".to_string(),
1121 n => format!("{} changes", n),
1122 };
1123
1124 self.panel_header_container(window, cx)
1125 .child(h_flex().gap_2().child(if all_repositories.len() <= 1 {
1126 div()
1127 .id("changes-label")
1128 .text_buffer(cx)
1129 .text_ui_sm(cx)
1130 .child(
1131 Label::new(changes_string)
1132 .single_line()
1133 .size(LabelSize::Small),
1134 )
1135 .into_any_element()
1136 } else {
1137 self.render_repository_selector(cx).into_any_element()
1138 }))
1139 .child(div().flex_grow())
1140 }
1141
1142 pub fn render_repository_selector(&self, cx: &mut Context<Self>) -> impl IntoElement {
1143 let active_repository = self.project.read(cx).active_repository(cx);
1144 let repository_display_name = active_repository
1145 .as_ref()
1146 .map(|repo| repo.read(cx).display_name(self.project.read(cx), cx))
1147 .unwrap_or_default();
1148
1149 let entry_count = self.entries.len();
1150
1151 RepositorySelectorPopoverMenu::new(
1152 self.repository_selector.clone(),
1153 ButtonLike::new("active-repository")
1154 .style(ButtonStyle::Subtle)
1155 .child(
1156 h_flex().w_full().gap_0p5().child(
1157 div()
1158 .overflow_x_hidden()
1159 .flex_grow()
1160 .whitespace_nowrap()
1161 .child(
1162 h_flex()
1163 .gap_1()
1164 .child(
1165 Label::new(repository_display_name).size(LabelSize::Small),
1166 )
1167 .when(entry_count > 0, |flex| {
1168 flex.child(
1169 Label::new(format!("({})", entry_count))
1170 .size(LabelSize::Small)
1171 .color(Color::Muted),
1172 )
1173 })
1174 .into_any_element(),
1175 ),
1176 ),
1177 ),
1178 )
1179 }
1180
1181 pub fn render_commit_editor(&self, cx: &Context<Self>) -> impl IntoElement {
1182 let editor = self.commit_editor.clone();
1183 let can_commit = (self.has_staged_changes() || self.has_tracked_changes())
1184 && self.pending_commit.is_none()
1185 && !editor.read(cx).is_empty(cx)
1186 && !self.has_unstaged_conflicts()
1187 && self.has_write_access(cx);
1188 let editor_focus_handle = editor.read(cx).focus_handle(cx).clone();
1189
1190 let focus_handle_1 = self.focus_handle(cx).clone();
1191 let tooltip = if self.has_staged_changes() {
1192 "Commit staged changes"
1193 } else {
1194 "Commit changes to tracked files"
1195 };
1196 let title = if self.has_staged_changes() {
1197 "Commit"
1198 } else {
1199 "Commit All"
1200 };
1201
1202 let commit_button = self
1203 .panel_button("commit-changes", title)
1204 .tooltip(move |window, cx| {
1205 let focus_handle = focus_handle_1.clone();
1206 Tooltip::for_action_in(tooltip, &Commit, &focus_handle, window, cx)
1207 })
1208 .disabled(!can_commit)
1209 .on_click({
1210 cx.listener(move |this, _: &ClickEvent, window, cx| this.commit_changes(window, cx))
1211 });
1212
1213 div().w_full().h(px(140.)).px_2().pt_1().pb_2().child(
1214 v_flex()
1215 .id("commit-editor-container")
1216 .relative()
1217 .h_full()
1218 .py_2p5()
1219 .px_3()
1220 .bg(cx.theme().colors().editor_background)
1221 .on_click(cx.listener(move |_, _: &ClickEvent, window, _cx| {
1222 window.focus(&editor_focus_handle);
1223 }))
1224 .child(self.commit_editor.clone())
1225 .child(
1226 h_flex()
1227 .absolute()
1228 .bottom_2p5()
1229 .right_3()
1230 .gap_1p5()
1231 .child(div().gap_1().flex_grow())
1232 .child(commit_button),
1233 ),
1234 )
1235 }
1236
1237 fn render_empty_state(&self, cx: &mut Context<Self>) -> impl IntoElement {
1238 h_flex()
1239 .h_full()
1240 .flex_1()
1241 .justify_center()
1242 .items_center()
1243 .child(
1244 v_flex()
1245 .gap_3()
1246 .child("No changes to commit")
1247 .text_ui_sm(cx)
1248 .mx_auto()
1249 .text_color(Color::Placeholder.color(cx)),
1250 )
1251 }
1252
1253 fn render_scrollbar(&self, cx: &mut Context<Self>) -> Option<Stateful<Div>> {
1254 let scroll_bar_style = self.show_scrollbar(cx);
1255 let show_container = matches!(scroll_bar_style, ShowScrollbar::Always);
1256
1257 if !self.should_show_scrollbar(cx)
1258 || !(self.show_scrollbar || self.scrollbar_state.is_dragging())
1259 {
1260 return None;
1261 }
1262
1263 Some(
1264 div()
1265 .id("git-panel-vertical-scroll")
1266 .occlude()
1267 .flex_none()
1268 .h_full()
1269 .cursor_default()
1270 .when(show_container, |this| this.pl_1().px_1p5())
1271 .when(!show_container, |this| {
1272 this.absolute().right_1().top_1().bottom_1().w(px(12.))
1273 })
1274 .on_mouse_move(cx.listener(|_, _, _, cx| {
1275 cx.notify();
1276 cx.stop_propagation()
1277 }))
1278 .on_hover(|_, _, cx| {
1279 cx.stop_propagation();
1280 })
1281 .on_any_mouse_down(|_, _, cx| {
1282 cx.stop_propagation();
1283 })
1284 .on_mouse_up(
1285 MouseButton::Left,
1286 cx.listener(|this, _, window, cx| {
1287 if !this.scrollbar_state.is_dragging()
1288 && !this.focus_handle.contains_focused(window, cx)
1289 {
1290 this.hide_scrollbar(window, cx);
1291 cx.notify();
1292 }
1293
1294 cx.stop_propagation();
1295 }),
1296 )
1297 .on_scroll_wheel(cx.listener(|_, _, _, cx| {
1298 cx.notify();
1299 }))
1300 .children(Scrollbar::vertical(
1301 // percentage as f32..end_offset as f32,
1302 self.scrollbar_state.clone(),
1303 )),
1304 )
1305 }
1306
1307 pub fn render_buffer_header_controls(
1308 &self,
1309 entity: &Entity<Self>,
1310 file: &Arc<dyn File>,
1311 _: &Window,
1312 cx: &App,
1313 ) -> Option<AnyElement> {
1314 let repo = self.active_repository.as_ref()?.read(cx);
1315 let repo_path = repo.worktree_id_path_to_repo_path(file.worktree_id(cx), file.path())?;
1316 let ix = self.entries_by_path.get(&repo_path)?;
1317 let entry = self.entries.get(*ix)?;
1318
1319 let is_staged = self.entry_is_staged(entry.status_entry()?);
1320
1321 let checkbox = Checkbox::new("stage-file", is_staged.into())
1322 .disabled(!self.has_write_access(cx))
1323 .fill()
1324 .elevation(ElevationIndex::Surface)
1325 .on_click({
1326 let entry = entry.clone();
1327 let git_panel = entity.downgrade();
1328 move |_, window, cx| {
1329 git_panel
1330 .update(cx, |this, cx| {
1331 this.toggle_staged_for_entry(&entry, window, cx);
1332 cx.stop_propagation();
1333 })
1334 .ok();
1335 }
1336 });
1337 Some(
1338 h_flex()
1339 .id("start-slot")
1340 .child(checkbox)
1341 .child(git_status_icon(entry.status_entry()?.status, cx))
1342 .on_mouse_down(MouseButton::Left, |_, _, cx| {
1343 // prevent the list item active state triggering when toggling checkbox
1344 cx.stop_propagation();
1345 })
1346 .into_any_element(),
1347 )
1348 }
1349
1350 fn render_entries(
1351 &self,
1352 has_write_access: bool,
1353 window: &Window,
1354 cx: &mut Context<Self>,
1355 ) -> impl IntoElement {
1356 let entry_count = self.entries.len();
1357
1358 v_flex()
1359 .size_full()
1360 .overflow_hidden()
1361 .child(
1362 uniform_list(cx.entity().clone(), "entries", entry_count, {
1363 move |this, range, window, cx| {
1364 let mut items = Vec::with_capacity(range.end - range.start);
1365
1366 for ix in range {
1367 match &this.entries.get(ix) {
1368 Some(GitListEntry::GitStatusEntry(entry)) => {
1369 items.push(this.render_entry(
1370 ix,
1371 entry,
1372 has_write_access,
1373 window,
1374 cx,
1375 ));
1376 }
1377 Some(GitListEntry::Header(header)) => {
1378 items.push(this.render_list_header(
1379 ix,
1380 header,
1381 has_write_access,
1382 window,
1383 cx,
1384 ));
1385 }
1386 None => {}
1387 }
1388 }
1389
1390 items
1391 }
1392 })
1393 .with_decoration(
1394 ui::indent_guides(
1395 cx.entity().clone(),
1396 self.indent_size(window, cx),
1397 IndentGuideColors::panel(cx),
1398 |this, range, _windows, _cx| {
1399 this.entries
1400 .iter()
1401 .skip(range.start)
1402 .map(|entry| match entry {
1403 GitListEntry::GitStatusEntry(_) => 1,
1404 GitListEntry::Header(_) => 0,
1405 })
1406 .collect()
1407 },
1408 )
1409 .with_render_fn(
1410 cx.entity().clone(),
1411 move |_, params, _, _| {
1412 let indent_size = params.indent_size;
1413 let left_offset = indent_size - px(3.0);
1414 let item_height = params.item_height;
1415
1416 params
1417 .indent_guides
1418 .into_iter()
1419 .enumerate()
1420 .map(|(_, layout)| {
1421 let offset = if layout.continues_offscreen {
1422 px(0.)
1423 } else {
1424 px(4.0)
1425 };
1426 let bounds = Bounds::new(
1427 point(
1428 px(layout.offset.x as f32) * indent_size + left_offset,
1429 px(layout.offset.y as f32) * item_height + offset,
1430 ),
1431 size(
1432 px(1.),
1433 px(layout.length as f32) * item_height
1434 - px(offset.0 * 2.),
1435 ),
1436 );
1437 ui::RenderedIndentGuide {
1438 bounds,
1439 layout,
1440 is_active: false,
1441 hitbox: None,
1442 }
1443 })
1444 .collect()
1445 },
1446 ),
1447 )
1448 .size_full()
1449 .with_sizing_behavior(ListSizingBehavior::Infer)
1450 .with_horizontal_sizing_behavior(ListHorizontalSizingBehavior::Unconstrained)
1451 .track_scroll(self.scroll_handle.clone()),
1452 )
1453 .children(self.render_scrollbar(cx))
1454 }
1455
1456 fn entry_label(&self, label: impl Into<SharedString>, color: Color) -> Label {
1457 Label::new(label.into()).color(color).single_line()
1458 }
1459
1460 fn render_list_header(
1461 &self,
1462 ix: usize,
1463 header: &GitHeaderEntry,
1464 has_write_access: bool,
1465 window: &Window,
1466 cx: &Context<Self>,
1467 ) -> AnyElement {
1468 let selected = self.selected_entry == Some(ix);
1469 let header_state = if self.has_staged_changes() {
1470 self.header_state(header.header)
1471 } else {
1472 match header.header {
1473 Section::Tracked | Section::Conflict => ToggleState::Selected,
1474 Section::New => ToggleState::Unselected,
1475 }
1476 };
1477
1478 let checkbox = Checkbox::new(("checkbox", ix), header_state)
1479 .disabled(!has_write_access)
1480 .fill()
1481 .placeholder(!self.has_staged_changes())
1482 .elevation(ElevationIndex::Surface)
1483 .on_click({
1484 let header = header.clone();
1485 cx.listener(move |this, _, window, cx| {
1486 this.toggle_staged_for_entry(&GitListEntry::Header(header.clone()), window, cx);
1487 cx.stop_propagation();
1488 })
1489 });
1490
1491 let start_slot = h_flex()
1492 .id(("start-slot", ix))
1493 .gap(DynamicSpacing::Base04.rems(cx))
1494 .child(checkbox)
1495 .tooltip(|window, cx| Tooltip::for_action("Stage File", &ToggleStaged, window, cx))
1496 .on_mouse_down(MouseButton::Left, |_, _, cx| {
1497 // prevent the list item active state triggering when toggling checkbox
1498 cx.stop_propagation();
1499 });
1500
1501 div()
1502 .w_full()
1503 .child(
1504 ListItem::new(ix)
1505 .spacing(ListItemSpacing::Sparse)
1506 .start_slot(start_slot)
1507 .toggle_state(selected)
1508 .focused(selected && self.focus_handle.is_focused(window))
1509 .disabled(!has_write_access)
1510 .on_click({
1511 cx.listener(move |this, _, _, cx| {
1512 this.selected_entry = Some(ix);
1513 cx.notify();
1514 })
1515 })
1516 .child(h_flex().child(self.entry_label(header.title(), Color::Muted))),
1517 )
1518 .into_any_element()
1519 }
1520
1521 fn render_entry(
1522 &self,
1523 ix: usize,
1524 entry: &GitStatusEntry,
1525 has_write_access: bool,
1526 window: &Window,
1527 cx: &Context<Self>,
1528 ) -> AnyElement {
1529 let display_name = entry
1530 .repo_path
1531 .file_name()
1532 .map(|name| name.to_string_lossy().into_owned())
1533 .unwrap_or_else(|| entry.repo_path.to_string_lossy().into_owned());
1534
1535 let repo_path = entry.repo_path.clone();
1536 let selected = self.selected_entry == Some(ix);
1537 let status_style = GitPanelSettings::get_global(cx).status_style;
1538 let status = entry.status;
1539 let has_conflict = status.is_conflicted();
1540 let is_modified = status.is_modified();
1541 let is_deleted = status.is_deleted();
1542
1543 let label_color = if status_style == StatusStyle::LabelColor {
1544 if has_conflict {
1545 Color::Conflict
1546 } else if is_modified {
1547 Color::Modified
1548 } else if is_deleted {
1549 // We don't want a bunch of red labels in the list
1550 Color::Disabled
1551 } else {
1552 Color::Created
1553 }
1554 } else {
1555 Color::Default
1556 };
1557
1558 let path_color = if status.is_deleted() {
1559 Color::Disabled
1560 } else {
1561 Color::Muted
1562 };
1563
1564 let id: ElementId = ElementId::Name(format!("entry_{}", display_name).into());
1565
1566 let mut is_staged: ToggleState = self.entry_is_staged(entry).into();
1567
1568 if !self.has_staged_changes() && !entry.status.is_created() {
1569 is_staged = ToggleState::Selected;
1570 }
1571
1572 let checkbox = Checkbox::new(id, is_staged)
1573 .disabled(!has_write_access)
1574 .fill()
1575 .placeholder(!self.has_staged_changes())
1576 .elevation(ElevationIndex::Surface)
1577 .on_click({
1578 let entry = entry.clone();
1579 cx.listener(move |this, _, window, cx| {
1580 this.toggle_staged_for_entry(
1581 &GitListEntry::GitStatusEntry(entry.clone()),
1582 window,
1583 cx,
1584 );
1585 cx.stop_propagation();
1586 })
1587 });
1588
1589 let start_slot = h_flex()
1590 .id(("start-slot", ix))
1591 .gap(DynamicSpacing::Base04.rems(cx))
1592 .child(checkbox)
1593 .tooltip(|window, cx| Tooltip::for_action("Stage File", &ToggleStaged, window, cx))
1594 .child(git_status_icon(status, cx))
1595 .on_mouse_down(MouseButton::Left, |_, _, cx| {
1596 // prevent the list item active state triggering when toggling checkbox
1597 cx.stop_propagation();
1598 });
1599
1600 let id = ElementId::Name(format!("entry_{}", display_name).into());
1601
1602 div()
1603 .w_full()
1604 .child(
1605 ListItem::new(id)
1606 .indent_level(1)
1607 .indent_step_size(Checkbox::container_size(cx).to_pixels(window.rem_size()))
1608 .spacing(ListItemSpacing::Sparse)
1609 .start_slot(start_slot)
1610 .toggle_state(selected)
1611 .focused(selected && self.focus_handle.is_focused(window))
1612 .disabled(!has_write_access)
1613 .on_click({
1614 cx.listener(move |this, _, window, cx| {
1615 this.selected_entry = Some(ix);
1616 cx.notify();
1617 this.open_selected(&Default::default(), window, cx);
1618 })
1619 })
1620 .child(
1621 h_flex()
1622 .when_some(repo_path.parent(), |this, parent| {
1623 let parent_str = parent.to_string_lossy();
1624 if !parent_str.is_empty() {
1625 this.child(
1626 self.entry_label(format!("{}/", parent_str), path_color)
1627 .when(status.is_deleted(), |this| {
1628 this.strikethrough(true)
1629 }),
1630 )
1631 } else {
1632 this
1633 }
1634 })
1635 .child(
1636 self.entry_label(display_name.clone(), label_color)
1637 .when(status.is_deleted(), |this| this.strikethrough(true)),
1638 ),
1639 ),
1640 )
1641 .into_any_element()
1642 }
1643
1644 fn has_write_access(&self, cx: &App) -> bool {
1645 !self.project.read(cx).is_read_only(cx)
1646 }
1647}
1648
1649impl Render for GitPanel {
1650 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1651 let project = self.project.read(cx);
1652 let has_entries = self
1653 .active_repository
1654 .as_ref()
1655 .map_or(false, |active_repository| {
1656 active_repository.read(cx).entry_count() > 0
1657 });
1658 let room = self
1659 .workspace
1660 .upgrade()
1661 .and_then(|workspace| workspace.read(cx).active_call()?.read(cx).room().cloned());
1662
1663 let has_write_access = self.has_write_access(cx);
1664
1665 let has_co_authors = room.map_or(false, |room| {
1666 room.read(cx)
1667 .remote_participants()
1668 .values()
1669 .any(|remote_participant| remote_participant.can_write())
1670 });
1671
1672 v_flex()
1673 .id("git_panel")
1674 .key_context(self.dispatch_context(window, cx))
1675 .track_focus(&self.focus_handle)
1676 .on_modifiers_changed(cx.listener(Self::handle_modifiers_changed))
1677 .when(has_write_access && !project.is_read_only(cx), |this| {
1678 this.on_action(cx.listener(|this, &ToggleStaged, window, cx| {
1679 this.toggle_staged_for_selected(&ToggleStaged, window, cx)
1680 }))
1681 .on_action(cx.listener(GitPanel::commit))
1682 })
1683 .when(self.is_focused(window, cx), |this| {
1684 this.on_action(cx.listener(Self::select_first))
1685 .on_action(cx.listener(Self::select_next))
1686 .on_action(cx.listener(Self::select_prev))
1687 .on_action(cx.listener(Self::select_last))
1688 .on_action(cx.listener(Self::close_panel))
1689 })
1690 .on_action(cx.listener(Self::open_selected))
1691 .on_action(cx.listener(Self::focus_changes_list))
1692 .on_action(cx.listener(Self::focus_editor))
1693 .on_action(cx.listener(Self::toggle_staged_for_selected))
1694 .when(has_write_access && has_co_authors, |git_panel| {
1695 git_panel.on_action(cx.listener(Self::fill_co_authors))
1696 })
1697 // .on_action(cx.listener(|this, &OpenSelected, cx| this.open_selected(&OpenSelected, cx)))
1698 .on_hover(cx.listener(|this, hovered, window, cx| {
1699 if *hovered {
1700 this.show_scrollbar = true;
1701 this.hide_scrollbar_task.take();
1702 cx.notify();
1703 } else if !this.focus_handle.contains_focused(window, cx) {
1704 this.hide_scrollbar(window, cx);
1705 }
1706 }))
1707 .size_full()
1708 .overflow_hidden()
1709 .bg(ElevationIndex::Surface.bg(cx))
1710 .child(self.render_panel_header(window, cx))
1711 .child(if has_entries {
1712 self.render_entries(has_write_access, window, cx)
1713 .into_any_element()
1714 } else {
1715 self.render_empty_state(cx).into_any_element()
1716 })
1717 .child(self.render_commit_editor(cx))
1718 }
1719}
1720
1721impl Focusable for GitPanel {
1722 fn focus_handle(&self, _: &App) -> gpui::FocusHandle {
1723 self.focus_handle.clone()
1724 }
1725}
1726
1727impl EventEmitter<Event> for GitPanel {}
1728
1729impl EventEmitter<PanelEvent> for GitPanel {}
1730
1731pub(crate) struct GitPanelAddon {
1732 pub(crate) git_panel: Entity<GitPanel>,
1733}
1734
1735impl editor::Addon for GitPanelAddon {
1736 fn to_any(&self) -> &dyn std::any::Any {
1737 self
1738 }
1739
1740 fn render_buffer_header_controls(
1741 &self,
1742 excerpt_info: &ExcerptInfo,
1743 window: &Window,
1744 cx: &App,
1745 ) -> Option<AnyElement> {
1746 let file = excerpt_info.buffer.file()?;
1747 let git_panel = self.git_panel.read(cx);
1748
1749 git_panel.render_buffer_header_controls(&self.git_panel, &file, window, cx)
1750 }
1751}
1752
1753impl Panel for GitPanel {
1754 fn persistent_name() -> &'static str {
1755 "GitPanel"
1756 }
1757
1758 fn position(&self, _: &Window, cx: &App) -> DockPosition {
1759 GitPanelSettings::get_global(cx).dock
1760 }
1761
1762 fn position_is_valid(&self, position: DockPosition) -> bool {
1763 matches!(position, DockPosition::Left | DockPosition::Right)
1764 }
1765
1766 fn set_position(&mut self, position: DockPosition, _: &mut Window, cx: &mut Context<Self>) {
1767 settings::update_settings_file::<GitPanelSettings>(
1768 self.fs.clone(),
1769 cx,
1770 move |settings, _| settings.dock = Some(position),
1771 );
1772 }
1773
1774 fn size(&self, _: &Window, cx: &App) -> Pixels {
1775 self.width
1776 .unwrap_or_else(|| GitPanelSettings::get_global(cx).default_width)
1777 }
1778
1779 fn set_size(&mut self, size: Option<Pixels>, _: &mut Window, cx: &mut Context<Self>) {
1780 self.width = size;
1781 self.serialize(cx);
1782 cx.notify();
1783 }
1784
1785 fn icon(&self, _: &Window, cx: &App) -> Option<ui::IconName> {
1786 Some(ui::IconName::GitBranch).filter(|_| GitPanelSettings::get_global(cx).button)
1787 }
1788
1789 fn icon_tooltip(&self, _window: &Window, _cx: &App) -> Option<&'static str> {
1790 Some("Git Panel")
1791 }
1792
1793 fn toggle_action(&self) -> Box<dyn Action> {
1794 Box::new(ToggleFocus)
1795 }
1796
1797 fn activation_priority(&self) -> u32 {
1798 2
1799 }
1800}
1801
1802impl PanelHeader for GitPanel {}