git_panel.rs

   1use crate::git_panel_settings::StatusStyle;
   2use crate::repository_selector::RepositorySelectorPopoverMenu;
   3use crate::{
   4    git_panel_settings::GitPanelSettings, git_status_icon, repository_selector::RepositorySelector,
   5};
   6use crate::{project_diff, ProjectDiff};
   7use collections::HashMap;
   8use db::kvp::KEY_VALUE_STORE;
   9use editor::commit_tooltip::CommitTooltip;
  10use editor::{
  11    scroll::ScrollbarAutoHide, Editor, EditorElement, EditorMode, EditorSettings, MultiBuffer,
  12    ShowScrollbar,
  13};
  14use git::repository::{CommitDetails, ResetMode};
  15use git::{repository::RepoPath, status::FileStatus, Commit, ToggleStaged};
  16use git::{DiscardTrackedChanges, StageAll, TrashUntrackedFiles, UnstageAll};
  17use gpui::*;
  18use itertools::Itertools;
  19use language::{markdown, Buffer, File, ParsedMarkdown};
  20use menu::{Confirm, SecondaryConfirm, SelectFirst, SelectLast, SelectNext, SelectPrev};
  21use multi_buffer::ExcerptInfo;
  22use panel::{panel_editor_container, panel_editor_style, panel_filled_button, PanelHeader};
  23use project::{
  24    git::{GitEvent, Repository},
  25    Fs, Project, ProjectPath,
  26};
  27use serde::{Deserialize, Serialize};
  28use settings::Settings as _;
  29use std::{collections::HashSet, path::PathBuf, sync::Arc, time::Duration, usize};
  30use strum::{IntoEnumIterator, VariantNames};
  31use time::OffsetDateTime;
  32use ui::{
  33    prelude::*, ButtonLike, Checkbox, ContextMenu, Divider, DividerColor, ElevationIndex, ListItem,
  34    ListItemSpacing, Scrollbar, ScrollbarState, Tooltip,
  35};
  36use util::{maybe, ResultExt, TryFutureExt};
  37use workspace::{
  38    dock::{DockPosition, Panel, PanelEvent},
  39    notifications::{DetachAndPromptErr, NotificationId},
  40    Toast, Workspace,
  41};
  42
  43actions!(
  44    git_panel,
  45    [
  46        Close,
  47        ToggleFocus,
  48        OpenMenu,
  49        FocusEditor,
  50        FocusChanges,
  51        ToggleFillCoAuthors,
  52    ]
  53);
  54
  55fn prompt<T>(msg: &str, detail: Option<&str>, window: &mut Window, cx: &mut App) -> Task<Result<T>>
  56where
  57    T: IntoEnumIterator + VariantNames + 'static,
  58{
  59    let rx = window.prompt(PromptLevel::Info, msg, detail, &T::VARIANTS, cx);
  60    cx.spawn(|_| async move { Ok(T::iter().nth(rx.await?).unwrap()) })
  61}
  62
  63#[derive(strum::EnumIter, strum::VariantNames)]
  64#[strum(serialize_all = "title_case")]
  65enum TrashCancel {
  66    Trash,
  67    Cancel,
  68}
  69
  70const GIT_PANEL_KEY: &str = "GitPanel";
  71
  72const UPDATE_DEBOUNCE: Duration = Duration::from_millis(50);
  73
  74pub fn init(cx: &mut App) {
  75    cx.observe_new(
  76        |workspace: &mut Workspace, _window, _cx: &mut Context<Workspace>| {
  77            workspace.register_action(|workspace, _: &ToggleFocus, window, cx| {
  78                workspace.toggle_panel_focus::<GitPanel>(window, cx);
  79            });
  80
  81            workspace.register_action(|workspace, _: &Commit, window, cx| {
  82                workspace.open_panel::<GitPanel>(window, cx);
  83                if let Some(git_panel) = workspace.panel::<GitPanel>(cx) {
  84                    git_panel
  85                        .read(cx)
  86                        .commit_editor
  87                        .focus_handle(cx)
  88                        .focus(window);
  89                }
  90            });
  91        },
  92    )
  93    .detach();
  94}
  95
  96#[derive(Debug, Clone)]
  97pub enum Event {
  98    Focus,
  99}
 100
 101#[derive(Serialize, Deserialize)]
 102struct SerializedGitPanel {
 103    width: Option<Pixels>,
 104}
 105
 106#[derive(Debug, PartialEq, Eq, Clone, Copy)]
 107enum Section {
 108    Conflict,
 109    Tracked,
 110    New,
 111}
 112
 113#[derive(Debug, PartialEq, Eq, Clone)]
 114struct GitHeaderEntry {
 115    header: Section,
 116}
 117
 118impl GitHeaderEntry {
 119    pub fn contains(&self, status_entry: &GitStatusEntry, repo: &Repository) -> bool {
 120        let this = &self.header;
 121        let status = status_entry.status;
 122        match this {
 123            Section::Conflict => repo.has_conflict(&status_entry.repo_path),
 124            Section::Tracked => !status.is_created(),
 125            Section::New => status.is_created(),
 126        }
 127    }
 128    pub fn title(&self) -> &'static str {
 129        match self.header {
 130            Section::Conflict => "Conflicts",
 131            Section::Tracked => "Tracked",
 132            Section::New => "Untracked",
 133        }
 134    }
 135}
 136
 137#[derive(Debug, PartialEq, Eq, Clone)]
 138enum GitListEntry {
 139    GitStatusEntry(GitStatusEntry),
 140    Header(GitHeaderEntry),
 141}
 142
 143impl GitListEntry {
 144    fn status_entry(&self) -> Option<&GitStatusEntry> {
 145        match self {
 146            GitListEntry::GitStatusEntry(entry) => Some(entry),
 147            _ => None,
 148        }
 149    }
 150}
 151
 152#[derive(Debug, PartialEq, Eq, Clone)]
 153pub struct GitStatusEntry {
 154    pub(crate) depth: usize,
 155    pub(crate) display_name: String,
 156    pub(crate) repo_path: RepoPath,
 157    pub(crate) status: FileStatus,
 158    pub(crate) is_staged: Option<bool>,
 159}
 160
 161#[derive(Clone, Copy, Debug, PartialEq, Eq)]
 162enum TargetStatus {
 163    Staged,
 164    Unstaged,
 165    Reverted,
 166    Unchanged,
 167}
 168
 169struct PendingOperation {
 170    finished: bool,
 171    target_status: TargetStatus,
 172    repo_paths: HashSet<RepoPath>,
 173    op_id: usize,
 174}
 175
 176pub struct GitPanel {
 177    active_repository: Option<Entity<Repository>>,
 178    commit_editor: Entity<Editor>,
 179    conflicted_count: usize,
 180    conflicted_staged_count: usize,
 181    current_modifiers: Modifiers,
 182    add_coauthors: bool,
 183    entries: Vec<GitListEntry>,
 184    entries_by_path: collections::HashMap<RepoPath, usize>,
 185    focus_handle: FocusHandle,
 186    fs: Arc<dyn Fs>,
 187    hide_scrollbar_task: Option<Task<()>>,
 188    new_count: usize,
 189    new_staged_count: usize,
 190    pending: Vec<PendingOperation>,
 191    pending_commit: Option<Task<()>>,
 192    pending_serialization: Task<Option<()>>,
 193    project: Entity<Project>,
 194    repository_selector: Entity<RepositorySelector>,
 195    scroll_handle: UniformListScrollHandle,
 196    scrollbar_state: ScrollbarState,
 197    selected_entry: Option<usize>,
 198    show_scrollbar: bool,
 199    tracked_count: usize,
 200    tracked_staged_count: usize,
 201    update_visible_entries_task: Task<()>,
 202    width: Option<Pixels>,
 203    workspace: WeakEntity<Workspace>,
 204    context_menu: Option<(Entity<ContextMenu>, Point<Pixels>, Subscription)>,
 205}
 206
 207fn commit_message_editor(
 208    commit_message_buffer: Entity<Buffer>,
 209    project: Entity<Project>,
 210    window: &mut Window,
 211    cx: &mut Context<'_, Editor>,
 212) -> Editor {
 213    let buffer = cx.new(|cx| MultiBuffer::singleton(commit_message_buffer, cx));
 214    let mut commit_editor = Editor::new(
 215        EditorMode::AutoHeight { max_lines: 6 },
 216        buffer,
 217        None,
 218        false,
 219        window,
 220        cx,
 221    );
 222    commit_editor.set_collaboration_hub(Box::new(project));
 223    commit_editor.set_use_autoclose(false);
 224    commit_editor.set_show_gutter(false, cx);
 225    commit_editor.set_show_wrap_guides(false, cx);
 226    commit_editor.set_show_indent_guides(false, cx);
 227    commit_editor.set_placeholder_text("Enter commit message", cx);
 228    commit_editor
 229}
 230
 231impl GitPanel {
 232    pub fn new(
 233        workspace: &mut Workspace,
 234        window: &mut Window,
 235        cx: &mut Context<Workspace>,
 236    ) -> Entity<Self> {
 237        let fs = workspace.app_state().fs.clone();
 238        let project = workspace.project().clone();
 239        let git_store = project.read(cx).git_store().clone();
 240        let active_repository = project.read(cx).active_repository(cx);
 241        let workspace = cx.entity().downgrade();
 242
 243        cx.new(|cx| {
 244            let focus_handle = cx.focus_handle();
 245            cx.on_focus(&focus_handle, window, Self::focus_in).detach();
 246            cx.on_focus_out(&focus_handle, window, |this, _, window, cx| {
 247                this.hide_scrollbar(window, cx);
 248            })
 249            .detach();
 250
 251            // just to let us render a placeholder editor.
 252            // Once the active git repo is set, this buffer will be replaced.
 253            let temporary_buffer = cx.new(|cx| Buffer::local("", cx));
 254            let commit_editor =
 255                cx.new(|cx| commit_message_editor(temporary_buffer, project.clone(), window, cx));
 256            commit_editor.update(cx, |editor, cx| {
 257                editor.clear(window, cx);
 258            });
 259
 260            let scroll_handle = UniformListScrollHandle::new();
 261
 262            cx.subscribe_in(
 263                &git_store,
 264                window,
 265                move |this, git_store, event, window, cx| match event {
 266                    GitEvent::FileSystemUpdated => {
 267                        this.schedule_update(false, window, cx);
 268                    }
 269                    GitEvent::ActiveRepositoryChanged | GitEvent::GitStateUpdated => {
 270                        this.active_repository = git_store.read(cx).active_repository();
 271                        this.schedule_update(true, window, cx);
 272                    }
 273                },
 274            )
 275            .detach();
 276
 277            let scrollbar_state =
 278                ScrollbarState::new(scroll_handle.clone()).parent_entity(&cx.entity());
 279
 280            let repository_selector =
 281                cx.new(|cx| RepositorySelector::new(project.clone(), window, cx));
 282
 283            let mut git_panel = Self {
 284                active_repository,
 285                commit_editor,
 286                conflicted_count: 0,
 287                conflicted_staged_count: 0,
 288                current_modifiers: window.modifiers(),
 289                add_coauthors: true,
 290                entries: Vec::new(),
 291                entries_by_path: HashMap::default(),
 292                focus_handle: cx.focus_handle(),
 293                fs,
 294                hide_scrollbar_task: None,
 295                new_count: 0,
 296                new_staged_count: 0,
 297                pending: Vec::new(),
 298                pending_commit: None,
 299                pending_serialization: Task::ready(None),
 300                project,
 301                repository_selector,
 302                scroll_handle,
 303                scrollbar_state,
 304                selected_entry: None,
 305                show_scrollbar: false,
 306                tracked_count: 0,
 307                tracked_staged_count: 0,
 308                update_visible_entries_task: Task::ready(()),
 309                width: Some(px(360.)),
 310                context_menu: None,
 311                workspace,
 312            };
 313            git_panel.schedule_update(false, window, cx);
 314            git_panel.show_scrollbar = git_panel.should_show_scrollbar(cx);
 315            git_panel
 316        })
 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(1);
 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            if matches!(
 498                self.entries.get(new_selected_entry),
 499                Some(GitListEntry::Header(..))
 500            ) {
 501                if new_selected_entry > 0 {
 502                    self.selected_entry = Some(new_selected_entry - 1)
 503                }
 504            } else {
 505                self.selected_entry = Some(new_selected_entry);
 506            }
 507
 508            self.scroll_to_selected_entry(cx);
 509        }
 510
 511        cx.notify();
 512    }
 513
 514    fn select_next(&mut self, _: &SelectNext, _window: &mut Window, cx: &mut Context<Self>) {
 515        let item_count = self.entries.len();
 516        if item_count == 0 {
 517            return;
 518        }
 519
 520        if let Some(selected_entry) = self.selected_entry {
 521            let new_selected_entry = if selected_entry < item_count - 1 {
 522                selected_entry + 1
 523            } else {
 524                selected_entry
 525            };
 526            if matches!(
 527                self.entries.get(new_selected_entry),
 528                Some(GitListEntry::Header(..))
 529            ) {
 530                self.selected_entry = Some(new_selected_entry + 1);
 531            } else {
 532                self.selected_entry = Some(new_selected_entry);
 533            }
 534
 535            self.scroll_to_selected_entry(cx);
 536        }
 537
 538        cx.notify();
 539    }
 540
 541    fn select_last(&mut self, _: &SelectLast, _window: &mut Window, cx: &mut Context<Self>) {
 542        if self.entries.last().is_some() {
 543            self.selected_entry = Some(self.entries.len() - 1);
 544            self.scroll_to_selected_entry(cx);
 545        }
 546    }
 547
 548    fn focus_editor(&mut self, _: &FocusEditor, window: &mut Window, cx: &mut Context<Self>) {
 549        self.commit_editor.update(cx, |editor, cx| {
 550            window.focus(&editor.focus_handle(cx));
 551        });
 552        cx.notify();
 553    }
 554
 555    fn select_first_entry_if_none(&mut self, cx: &mut Context<Self>) {
 556        let have_entries = self
 557            .active_repository
 558            .as_ref()
 559            .map_or(false, |active_repository| {
 560                active_repository.read(cx).entry_count() > 0
 561            });
 562        if have_entries && self.selected_entry.is_none() {
 563            self.selected_entry = Some(1);
 564            self.scroll_to_selected_entry(cx);
 565            cx.notify();
 566        }
 567    }
 568
 569    fn focus_changes_list(
 570        &mut self,
 571        _: &FocusChanges,
 572        window: &mut Window,
 573        cx: &mut Context<Self>,
 574    ) {
 575        self.select_first_entry_if_none(cx);
 576
 577        cx.focus_self(window);
 578        cx.notify();
 579    }
 580
 581    fn get_selected_entry(&self) -> Option<&GitListEntry> {
 582        self.selected_entry.and_then(|i| self.entries.get(i))
 583    }
 584
 585    fn open_diff(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
 586        maybe!({
 587            let entry = self.entries.get(self.selected_entry?)?.status_entry()?;
 588
 589            self.workspace
 590                .update(cx, |workspace, cx| {
 591                    ProjectDiff::deploy_at(workspace, Some(entry.clone()), window, cx);
 592                })
 593                .ok()
 594        });
 595        self.focus_handle.focus(window);
 596    }
 597
 598    fn open_file(
 599        &mut self,
 600        _: &menu::SecondaryConfirm,
 601        window: &mut Window,
 602        cx: &mut Context<Self>,
 603    ) {
 604        maybe!({
 605            let entry = self.entries.get(self.selected_entry?)?.status_entry()?;
 606            let active_repo = self.active_repository.as_ref()?;
 607            let path = active_repo
 608                .read(cx)
 609                .repo_path_to_project_path(&entry.repo_path)?;
 610            if entry.status.is_deleted() {
 611                return None;
 612            }
 613
 614            self.workspace
 615                .update(cx, |workspace, cx| {
 616                    workspace
 617                        .open_path_preview(path, None, false, false, window, cx)
 618                        .detach_and_prompt_err("Failed to open file", window, cx, |e, _, _| {
 619                            Some(format!("{e}"))
 620                        });
 621                })
 622                .ok()
 623        });
 624    }
 625
 626    fn revert_selected(
 627        &mut self,
 628        _: &editor::actions::RevertFile,
 629        window: &mut Window,
 630        cx: &mut Context<Self>,
 631    ) {
 632        maybe!({
 633            let list_entry = self.entries.get(self.selected_entry?)?.clone();
 634            let entry = list_entry.status_entry()?;
 635            self.revert_entry(&entry, window, cx);
 636            Some(())
 637        });
 638    }
 639
 640    fn revert_entry(
 641        &mut self,
 642        entry: &GitStatusEntry,
 643        window: &mut Window,
 644        cx: &mut Context<Self>,
 645    ) {
 646        maybe!({
 647            let active_repo = self.active_repository.clone()?;
 648            let path = active_repo
 649                .read(cx)
 650                .repo_path_to_project_path(&entry.repo_path)?;
 651            let workspace = self.workspace.clone();
 652
 653            if entry.status.is_staged() != Some(false) {
 654                self.perform_stage(false, vec![entry.repo_path.clone()], cx);
 655            }
 656            let filename = path.path.file_name()?.to_string_lossy();
 657
 658            if !entry.status.is_created() {
 659                self.perform_checkout(vec![entry.repo_path.clone()], cx);
 660            } else {
 661                let prompt = prompt(&format!("Trash {}?", filename), None, window, cx);
 662                cx.spawn_in(window, |_, mut cx| async move {
 663                    match prompt.await? {
 664                        TrashCancel::Trash => {}
 665                        TrashCancel::Cancel => return Ok(()),
 666                    }
 667                    let task = workspace.update(&mut cx, |workspace, cx| {
 668                        workspace
 669                            .project()
 670                            .update(cx, |project, cx| project.delete_file(path, true, cx))
 671                    })?;
 672                    if let Some(task) = task {
 673                        task.await?;
 674                    }
 675                    Ok(())
 676                })
 677                .detach_and_prompt_err(
 678                    "Failed to trash file",
 679                    window,
 680                    cx,
 681                    |e, _, _| Some(format!("{e}")),
 682                );
 683            }
 684            Some(())
 685        });
 686    }
 687
 688    fn perform_checkout(&mut self, repo_paths: Vec<RepoPath>, cx: &mut Context<Self>) {
 689        let workspace = self.workspace.clone();
 690        let Some(active_repository) = self.active_repository.clone() else {
 691            return;
 692        };
 693
 694        let op_id = self.pending.iter().map(|p| p.op_id).max().unwrap_or(0) + 1;
 695        self.pending.push(PendingOperation {
 696            op_id,
 697            target_status: TargetStatus::Reverted,
 698            repo_paths: repo_paths.iter().cloned().collect(),
 699            finished: false,
 700        });
 701        self.update_visible_entries(cx);
 702        let task = cx.spawn(|_, mut cx| async move {
 703            let tasks: Vec<_> = workspace.update(&mut cx, |workspace, cx| {
 704                workspace.project().update(cx, |project, cx| {
 705                    repo_paths
 706                        .iter()
 707                        .filter_map(|repo_path| {
 708                            let path = active_repository
 709                                .read(cx)
 710                                .repo_path_to_project_path(&repo_path)?;
 711                            Some(project.open_buffer(path, cx))
 712                        })
 713                        .collect()
 714                })
 715            })?;
 716
 717            let buffers = futures::future::join_all(tasks).await;
 718
 719            active_repository
 720                .update(&mut cx, |repo, _| repo.checkout_files("HEAD", repo_paths))?
 721                .await??;
 722
 723            let tasks: Vec<_> = cx.update(|cx| {
 724                buffers
 725                    .iter()
 726                    .filter_map(|buffer| {
 727                        buffer.as_ref().ok()?.update(cx, |buffer, cx| {
 728                            buffer.is_dirty().then(|| buffer.reload(cx))
 729                        })
 730                    })
 731                    .collect()
 732            })?;
 733
 734            futures::future::join_all(tasks).await;
 735
 736            Ok(())
 737        });
 738
 739        cx.spawn(|this, mut cx| async move {
 740            let result = task.await;
 741
 742            this.update(&mut cx, |this, cx| {
 743                for pending in this.pending.iter_mut() {
 744                    if pending.op_id == op_id {
 745                        pending.finished = true;
 746                        if result.is_err() {
 747                            pending.target_status = TargetStatus::Unchanged;
 748                            this.update_visible_entries(cx);
 749                        }
 750                        break;
 751                    }
 752                }
 753                result
 754                    .map_err(|e| {
 755                        this.show_err_toast(e, cx);
 756                    })
 757                    .ok();
 758            })
 759            .ok();
 760        })
 761        .detach();
 762    }
 763
 764    fn discard_tracked_changes(
 765        &mut self,
 766        _: &DiscardTrackedChanges,
 767        window: &mut Window,
 768        cx: &mut Context<Self>,
 769    ) {
 770        let entries = self
 771            .entries
 772            .iter()
 773            .filter_map(|entry| entry.status_entry().cloned())
 774            .filter(|status_entry| !status_entry.status.is_created())
 775            .collect::<Vec<_>>();
 776
 777        match entries.len() {
 778            0 => return,
 779            1 => return self.revert_entry(&entries[0], window, cx),
 780            _ => {}
 781        }
 782        let details = entries
 783            .iter()
 784            .filter_map(|entry| entry.repo_path.0.file_name())
 785            .map(|filename| filename.to_string_lossy())
 786            .join("\n");
 787
 788        #[derive(strum::EnumIter, strum::VariantNames)]
 789        #[strum(serialize_all = "title_case")]
 790        enum DiscardCancel {
 791            DiscardTrackedChanges,
 792            Cancel,
 793        }
 794        let prompt = prompt(
 795            "Discard changes to these files?",
 796            Some(&details),
 797            window,
 798            cx,
 799        );
 800        cx.spawn(|this, mut cx| async move {
 801            match prompt.await {
 802                Ok(DiscardCancel::DiscardTrackedChanges) => {
 803                    this.update(&mut cx, |this, cx| {
 804                        let repo_paths = entries.into_iter().map(|entry| entry.repo_path).collect();
 805                        this.perform_checkout(repo_paths, cx);
 806                    })
 807                    .ok();
 808                }
 809                _ => {
 810                    return;
 811                }
 812            }
 813        })
 814        .detach();
 815    }
 816
 817    fn clean_all(&mut self, _: &TrashUntrackedFiles, window: &mut Window, cx: &mut Context<Self>) {
 818        let workspace = self.workspace.clone();
 819        let Some(active_repo) = self.active_repository.clone() else {
 820            return;
 821        };
 822        let to_delete = self
 823            .entries
 824            .iter()
 825            .filter_map(|entry| entry.status_entry())
 826            .filter(|status_entry| status_entry.status.is_created())
 827            .cloned()
 828            .collect::<Vec<_>>();
 829
 830        match to_delete.len() {
 831            0 => return,
 832            1 => return self.revert_entry(&to_delete[0], window, cx),
 833            _ => {}
 834        };
 835
 836        let details = to_delete
 837            .iter()
 838            .map(|entry| {
 839                entry
 840                    .repo_path
 841                    .0
 842                    .file_name()
 843                    .map(|f| f.to_string_lossy())
 844                    .unwrap_or_default()
 845            })
 846            .join("\n");
 847
 848        let prompt = prompt("Trash these files?", Some(&details), window, cx);
 849        cx.spawn_in(window, |this, mut cx| async move {
 850            match prompt.await? {
 851                TrashCancel::Trash => {}
 852                TrashCancel::Cancel => return Ok(()),
 853            }
 854            let tasks = workspace.update(&mut cx, |workspace, cx| {
 855                to_delete
 856                    .iter()
 857                    .filter_map(|entry| {
 858                        workspace.project().update(cx, |project, cx| {
 859                            let project_path = active_repo
 860                                .read(cx)
 861                                .repo_path_to_project_path(&entry.repo_path)?;
 862                            project.delete_file(project_path, true, cx)
 863                        })
 864                    })
 865                    .collect::<Vec<_>>()
 866            })?;
 867            let to_unstage = to_delete
 868                .into_iter()
 869                .filter_map(|entry| {
 870                    if entry.status.is_staged() != Some(false) {
 871                        Some(entry.repo_path.clone())
 872                    } else {
 873                        None
 874                    }
 875                })
 876                .collect();
 877            this.update(&mut cx, |this, cx| {
 878                this.perform_stage(false, to_unstage, cx)
 879            })?;
 880            for task in tasks {
 881                task.await?;
 882            }
 883            Ok(())
 884        })
 885        .detach_and_prompt_err("Failed to trash files", window, cx, |e, _, _| {
 886            Some(format!("{e}"))
 887        });
 888    }
 889
 890    fn stage_all(&mut self, _: &StageAll, _window: &mut Window, cx: &mut Context<Self>) {
 891        let repo_paths = self
 892            .entries
 893            .iter()
 894            .filter_map(|entry| entry.status_entry())
 895            .filter(|status_entry| status_entry.is_staged != Some(true))
 896            .map(|status_entry| status_entry.repo_path.clone())
 897            .collect::<Vec<_>>();
 898        self.perform_stage(true, repo_paths, cx);
 899    }
 900
 901    fn unstage_all(&mut self, _: &UnstageAll, _window: &mut Window, cx: &mut Context<Self>) {
 902        let repo_paths = self
 903            .entries
 904            .iter()
 905            .filter_map(|entry| entry.status_entry())
 906            .filter(|status_entry| status_entry.is_staged != Some(false))
 907            .map(|status_entry| status_entry.repo_path.clone())
 908            .collect::<Vec<_>>();
 909        self.perform_stage(false, repo_paths, cx);
 910    }
 911
 912    fn toggle_staged_for_entry(
 913        &mut self,
 914        entry: &GitListEntry,
 915        _window: &mut Window,
 916        cx: &mut Context<Self>,
 917    ) {
 918        let Some(active_repository) = self.active_repository.as_ref() else {
 919            return;
 920        };
 921        let (stage, repo_paths) = match entry {
 922            GitListEntry::GitStatusEntry(status_entry) => {
 923                if status_entry.status.is_staged().unwrap_or(false) {
 924                    (false, vec![status_entry.repo_path.clone()])
 925                } else {
 926                    (true, vec![status_entry.repo_path.clone()])
 927                }
 928            }
 929            GitListEntry::Header(section) => {
 930                let goal_staged_state = !self.header_state(section.header).selected();
 931                let repository = active_repository.read(cx);
 932                let entries = self
 933                    .entries
 934                    .iter()
 935                    .filter_map(|entry| entry.status_entry())
 936                    .filter(|status_entry| {
 937                        section.contains(&status_entry, repository)
 938                            && status_entry.is_staged != Some(goal_staged_state)
 939                    })
 940                    .map(|status_entry| status_entry.repo_path.clone())
 941                    .collect::<Vec<_>>();
 942
 943                (goal_staged_state, entries)
 944            }
 945        };
 946        self.perform_stage(stage, repo_paths, cx);
 947    }
 948
 949    fn perform_stage(&mut self, stage: bool, repo_paths: Vec<RepoPath>, cx: &mut Context<Self>) {
 950        let Some(active_repository) = self.active_repository.clone() else {
 951            return;
 952        };
 953        let op_id = self.pending.iter().map(|p| p.op_id).max().unwrap_or(0) + 1;
 954        self.pending.push(PendingOperation {
 955            op_id,
 956            target_status: if stage {
 957                TargetStatus::Staged
 958            } else {
 959                TargetStatus::Unstaged
 960            },
 961            repo_paths: repo_paths.iter().cloned().collect(),
 962            finished: false,
 963        });
 964        let repo_paths = repo_paths.clone();
 965        let repository = active_repository.read(cx);
 966        self.update_counts(repository);
 967        cx.notify();
 968
 969        cx.spawn({
 970            |this, mut cx| async move {
 971                let result = cx
 972                    .update(|cx| {
 973                        if stage {
 974                            active_repository
 975                                .update(cx, |repo, cx| repo.stage_entries(repo_paths.clone(), cx))
 976                        } else {
 977                            active_repository
 978                                .update(cx, |repo, cx| repo.unstage_entries(repo_paths.clone(), cx))
 979                        }
 980                    })?
 981                    .await;
 982
 983                this.update(&mut cx, |this, cx| {
 984                    for pending in this.pending.iter_mut() {
 985                        if pending.op_id == op_id {
 986                            pending.finished = true
 987                        }
 988                    }
 989                    result
 990                        .map_err(|e| {
 991                            this.show_err_toast(e, cx);
 992                        })
 993                        .ok();
 994                    cx.notify();
 995                })
 996            }
 997        })
 998        .detach();
 999    }
1000
1001    fn toggle_staged_for_selected(
1002        &mut self,
1003        _: &git::ToggleStaged,
1004        window: &mut Window,
1005        cx: &mut Context<Self>,
1006    ) {
1007        if let Some(selected_entry) = self.get_selected_entry().cloned() {
1008            self.toggle_staged_for_entry(&selected_entry, window, cx);
1009        }
1010    }
1011
1012    /// Commit all staged changes
1013    fn commit(&mut self, _: &git::Commit, window: &mut Window, cx: &mut Context<Self>) {
1014        let editor = self.commit_editor.read(cx);
1015        if editor.is_empty(cx) {
1016            if !editor.focus_handle(cx).contains_focused(window, cx) {
1017                editor.focus_handle(cx).focus(window);
1018                return;
1019            }
1020        }
1021
1022        self.commit_changes(window, cx)
1023    }
1024
1025    fn commit_changes(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1026        let Some(active_repository) = self.active_repository.clone() else {
1027            return;
1028        };
1029        let error_spawn = |message, window: &mut Window, cx: &mut App| {
1030            let prompt = window.prompt(PromptLevel::Warning, message, None, &["Ok"], cx);
1031            cx.spawn(|_| async move {
1032                prompt.await.ok();
1033            })
1034            .detach();
1035        };
1036
1037        if self.has_unstaged_conflicts() {
1038            error_spawn(
1039                "There are still conflicts. You must stage these before committing",
1040                window,
1041                cx,
1042            );
1043            return;
1044        }
1045
1046        let mut message = self.commit_editor.read(cx).text(cx);
1047        if message.trim().is_empty() {
1048            self.commit_editor.read(cx).focus_handle(cx).focus(window);
1049            return;
1050        }
1051        if self.add_coauthors {
1052            self.fill_co_authors(&mut message, cx);
1053        }
1054
1055        let task = if self.has_staged_changes() {
1056            // Repository serializes all git operations, so we can just send a commit immediately
1057            let commit_task = active_repository.read(cx).commit(message.into(), None);
1058            cx.background_executor()
1059                .spawn(async move { commit_task.await? })
1060        } else {
1061            let changed_files = self
1062                .entries
1063                .iter()
1064                .filter_map(|entry| entry.status_entry())
1065                .filter(|status_entry| !status_entry.status.is_created())
1066                .map(|status_entry| status_entry.repo_path.clone())
1067                .collect::<Vec<_>>();
1068
1069            if changed_files.is_empty() {
1070                error_spawn("No changes to commit", window, cx);
1071                return;
1072            }
1073
1074            let stage_task =
1075                active_repository.update(cx, |repo, cx| repo.stage_entries(changed_files, cx));
1076            cx.spawn(|_, mut cx| async move {
1077                stage_task.await?;
1078                let commit_task = active_repository
1079                    .update(&mut cx, |repo, _| repo.commit(message.into(), None))?;
1080                commit_task.await?
1081            })
1082        };
1083        let task = cx.spawn_in(window, |this, mut cx| async move {
1084            let result = task.await;
1085            this.update_in(&mut cx, |this, window, cx| {
1086                this.pending_commit.take();
1087                match result {
1088                    Ok(()) => {
1089                        this.commit_editor
1090                            .update(cx, |editor, cx| editor.clear(window, cx));
1091                    }
1092                    Err(e) => this.show_err_toast(e, cx),
1093                }
1094            })
1095            .ok();
1096        });
1097
1098        self.pending_commit = Some(task);
1099    }
1100
1101    fn uncommit(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1102        let Some(repo) = self.active_repository.clone() else {
1103            return;
1104        };
1105        let prior_head = self.load_commit_details("HEAD", cx);
1106
1107        let task = cx.spawn(|_, mut cx| async move {
1108            let prior_head = prior_head.await?;
1109
1110            repo.update(&mut cx, |repo, _| repo.reset("HEAD^", ResetMode::Soft))?
1111                .await??;
1112
1113            Ok(prior_head)
1114        });
1115
1116        let task = cx.spawn_in(window, |this, mut cx| async move {
1117            let result = task.await;
1118            this.update_in(&mut cx, |this, window, cx| {
1119                this.pending_commit.take();
1120                match result {
1121                    Ok(prior_commit) => {
1122                        this.commit_editor.update(cx, |editor, cx| {
1123                            editor.set_text(prior_commit.message, window, cx)
1124                        });
1125                    }
1126                    Err(e) => this.show_err_toast(e, cx),
1127                }
1128            })
1129            .ok();
1130        });
1131
1132        self.pending_commit = Some(task);
1133    }
1134
1135    fn potential_co_authors(&self, cx: &App) -> Vec<(String, String)> {
1136        let mut new_co_authors = Vec::new();
1137        let project = self.project.read(cx);
1138
1139        let Some(room) = self
1140            .workspace
1141            .upgrade()
1142            .and_then(|workspace| workspace.read(cx).active_call()?.read(cx).room().cloned())
1143        else {
1144            return Vec::default();
1145        };
1146
1147        let room = room.read(cx);
1148
1149        for (peer_id, collaborator) in project.collaborators() {
1150            if collaborator.is_host {
1151                continue;
1152            }
1153
1154            let Some(participant) = room.remote_participant_for_peer_id(*peer_id) else {
1155                continue;
1156            };
1157            if participant.can_write() && participant.user.email.is_some() {
1158                let email = participant.user.email.clone().unwrap();
1159
1160                new_co_authors.push((
1161                    participant
1162                        .user
1163                        .name
1164                        .clone()
1165                        .unwrap_or_else(|| participant.user.github_login.clone()),
1166                    email,
1167                ))
1168            }
1169        }
1170        if !project.is_local() && !project.is_read_only(cx) {
1171            if let Some(user) = room.local_participant_user(cx) {
1172                if let Some(email) = user.email.clone() {
1173                    new_co_authors.push((
1174                        user.name
1175                            .clone()
1176                            .unwrap_or_else(|| user.github_login.clone()),
1177                        email.clone(),
1178                    ))
1179                }
1180            }
1181        }
1182        new_co_authors
1183    }
1184
1185    fn toggle_fill_co_authors(
1186        &mut self,
1187        _: &ToggleFillCoAuthors,
1188        _: &mut Window,
1189        cx: &mut Context<Self>,
1190    ) {
1191        self.add_coauthors = !self.add_coauthors;
1192        cx.notify();
1193    }
1194
1195    fn fill_co_authors(&mut self, message: &mut String, cx: &mut Context<Self>) {
1196        const CO_AUTHOR_PREFIX: &str = "Co-authored-by: ";
1197
1198        let existing_text = message.to_ascii_lowercase();
1199        let lowercase_co_author_prefix = CO_AUTHOR_PREFIX.to_lowercase();
1200        let mut ends_with_co_authors = false;
1201        let existing_co_authors = existing_text
1202            .lines()
1203            .filter_map(|line| {
1204                let line = line.trim();
1205                if line.starts_with(&lowercase_co_author_prefix) {
1206                    ends_with_co_authors = true;
1207                    Some(line)
1208                } else {
1209                    ends_with_co_authors = false;
1210                    None
1211                }
1212            })
1213            .collect::<HashSet<_>>();
1214
1215        let new_co_authors = self
1216            .potential_co_authors(cx)
1217            .into_iter()
1218            .filter(|(_, email)| {
1219                !existing_co_authors
1220                    .iter()
1221                    .any(|existing| existing.contains(email.as_str()))
1222            })
1223            .collect::<Vec<_>>();
1224
1225        if new_co_authors.is_empty() {
1226            return;
1227        }
1228
1229        if !ends_with_co_authors {
1230            message.push('\n');
1231        }
1232        for (name, email) in new_co_authors {
1233            message.push('\n');
1234            message.push_str(CO_AUTHOR_PREFIX);
1235            message.push_str(&name);
1236            message.push_str(" <");
1237            message.push_str(&email);
1238            message.push('>');
1239        }
1240        message.push('\n');
1241    }
1242
1243    fn schedule_update(
1244        &mut self,
1245        clear_pending: bool,
1246        window: &mut Window,
1247        cx: &mut Context<Self>,
1248    ) {
1249        let handle = cx.entity().downgrade();
1250        self.reopen_commit_buffer(window, cx);
1251        self.update_visible_entries_task = cx.spawn_in(window, |_, mut cx| async move {
1252            cx.background_executor().timer(UPDATE_DEBOUNCE).await;
1253            if let Some(git_panel) = handle.upgrade() {
1254                git_panel
1255                    .update_in(&mut cx, |git_panel, _, cx| {
1256                        if clear_pending {
1257                            git_panel.clear_pending();
1258                        }
1259                        git_panel.update_visible_entries(cx);
1260                    })
1261                    .ok();
1262            }
1263        });
1264    }
1265
1266    fn reopen_commit_buffer(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1267        let Some(active_repo) = self.active_repository.as_ref() else {
1268            return;
1269        };
1270        let load_buffer = active_repo.update(cx, |active_repo, cx| {
1271            let project = self.project.read(cx);
1272            active_repo.open_commit_buffer(
1273                Some(project.languages().clone()),
1274                project.buffer_store().clone(),
1275                cx,
1276            )
1277        });
1278
1279        cx.spawn_in(window, |git_panel, mut cx| async move {
1280            let buffer = load_buffer.await?;
1281            git_panel.update_in(&mut cx, |git_panel, window, cx| {
1282                if git_panel
1283                    .commit_editor
1284                    .read(cx)
1285                    .buffer()
1286                    .read(cx)
1287                    .as_singleton()
1288                    .as_ref()
1289                    != Some(&buffer)
1290                {
1291                    git_panel.commit_editor = cx.new(|cx| {
1292                        commit_message_editor(buffer, git_panel.project.clone(), window, cx)
1293                    });
1294                }
1295            })
1296        })
1297        .detach_and_log_err(cx);
1298    }
1299
1300    fn clear_pending(&mut self) {
1301        self.pending.retain(|v| !v.finished)
1302    }
1303
1304    fn update_visible_entries(&mut self, cx: &mut Context<Self>) {
1305        self.entries.clear();
1306        self.entries_by_path.clear();
1307        let mut changed_entries = Vec::new();
1308        let mut new_entries = Vec::new();
1309        let mut conflict_entries = Vec::new();
1310
1311        let Some(repo) = self.active_repository.as_ref() else {
1312            // Just clear entries if no repository is active.
1313            cx.notify();
1314            return;
1315        };
1316
1317        // First pass - collect all paths
1318        let repo = repo.read(cx);
1319        let path_set = HashSet::from_iter(repo.status().map(|entry| entry.repo_path));
1320
1321        // Second pass - create entries with proper depth calculation
1322        for entry in repo.status() {
1323            let (depth, difference) =
1324                Self::calculate_depth_and_difference(&entry.repo_path, &path_set);
1325
1326            let is_conflict = repo.has_conflict(&entry.repo_path);
1327            let is_new = entry.status.is_created();
1328            let is_staged = entry.status.is_staged();
1329
1330            if self.pending.iter().any(|pending| {
1331                pending.target_status == TargetStatus::Reverted
1332                    && !pending.finished
1333                    && pending.repo_paths.contains(&entry.repo_path)
1334            }) {
1335                continue;
1336            }
1337
1338            let display_name = if difference > 1 {
1339                // Show partial path for deeply nested files
1340                entry
1341                    .repo_path
1342                    .as_ref()
1343                    .iter()
1344                    .skip(entry.repo_path.components().count() - difference)
1345                    .collect::<PathBuf>()
1346                    .to_string_lossy()
1347                    .into_owned()
1348            } else {
1349                // Just show filename
1350                entry
1351                    .repo_path
1352                    .file_name()
1353                    .map(|name| name.to_string_lossy().into_owned())
1354                    .unwrap_or_default()
1355            };
1356
1357            let entry = GitStatusEntry {
1358                depth,
1359                display_name,
1360                repo_path: entry.repo_path.clone(),
1361                status: entry.status,
1362                is_staged,
1363            };
1364
1365            if is_conflict {
1366                conflict_entries.push(entry);
1367            } else if is_new {
1368                new_entries.push(entry);
1369            } else {
1370                changed_entries.push(entry);
1371            }
1372        }
1373
1374        // Sort entries by path to maintain consistent order
1375        conflict_entries.sort_by(|a, b| a.repo_path.cmp(&b.repo_path));
1376        changed_entries.sort_by(|a, b| a.repo_path.cmp(&b.repo_path));
1377        new_entries.sort_by(|a, b| a.repo_path.cmp(&b.repo_path));
1378
1379        if conflict_entries.len() > 0 {
1380            self.entries.push(GitListEntry::Header(GitHeaderEntry {
1381                header: Section::Conflict,
1382            }));
1383            self.entries.extend(
1384                conflict_entries
1385                    .into_iter()
1386                    .map(GitListEntry::GitStatusEntry),
1387            );
1388        }
1389
1390        if changed_entries.len() > 0 {
1391            self.entries.push(GitListEntry::Header(GitHeaderEntry {
1392                header: Section::Tracked,
1393            }));
1394            self.entries.extend(
1395                changed_entries
1396                    .into_iter()
1397                    .map(GitListEntry::GitStatusEntry),
1398            );
1399        }
1400        if new_entries.len() > 0 {
1401            self.entries.push(GitListEntry::Header(GitHeaderEntry {
1402                header: Section::New,
1403            }));
1404            self.entries
1405                .extend(new_entries.into_iter().map(GitListEntry::GitStatusEntry));
1406        }
1407
1408        for (ix, entry) in self.entries.iter().enumerate() {
1409            if let Some(status_entry) = entry.status_entry() {
1410                self.entries_by_path
1411                    .insert(status_entry.repo_path.clone(), ix);
1412            }
1413        }
1414        self.update_counts(repo);
1415
1416        self.select_first_entry_if_none(cx);
1417
1418        cx.notify();
1419    }
1420
1421    fn header_state(&self, header_type: Section) -> ToggleState {
1422        let (staged_count, count) = match header_type {
1423            Section::New => (self.new_staged_count, self.new_count),
1424            Section::Tracked => (self.tracked_staged_count, self.tracked_count),
1425            Section::Conflict => (self.conflicted_staged_count, self.conflicted_count),
1426        };
1427        if staged_count == 0 {
1428            ToggleState::Unselected
1429        } else if count == staged_count {
1430            ToggleState::Selected
1431        } else {
1432            ToggleState::Indeterminate
1433        }
1434    }
1435
1436    fn update_counts(&mut self, repo: &Repository) {
1437        self.conflicted_count = 0;
1438        self.conflicted_staged_count = 0;
1439        self.new_count = 0;
1440        self.tracked_count = 0;
1441        self.new_staged_count = 0;
1442        self.tracked_staged_count = 0;
1443        for entry in &self.entries {
1444            let Some(status_entry) = entry.status_entry() else {
1445                continue;
1446            };
1447            if repo.has_conflict(&status_entry.repo_path) {
1448                self.conflicted_count += 1;
1449                if self.entry_is_staged(status_entry) != Some(false) {
1450                    self.conflicted_staged_count += 1;
1451                }
1452            } else if status_entry.status.is_created() {
1453                self.new_count += 1;
1454                if self.entry_is_staged(status_entry) != Some(false) {
1455                    self.new_staged_count += 1;
1456                }
1457            } else {
1458                self.tracked_count += 1;
1459                if self.entry_is_staged(status_entry) != Some(false) {
1460                    self.tracked_staged_count += 1;
1461                }
1462            }
1463        }
1464    }
1465
1466    fn entry_is_staged(&self, entry: &GitStatusEntry) -> Option<bool> {
1467        for pending in self.pending.iter().rev() {
1468            if pending.repo_paths.contains(&entry.repo_path) {
1469                match pending.target_status {
1470                    TargetStatus::Staged => return Some(true),
1471                    TargetStatus::Unstaged => return Some(false),
1472                    TargetStatus::Reverted => continue,
1473                    TargetStatus::Unchanged => continue,
1474                }
1475            }
1476        }
1477        entry.is_staged
1478    }
1479
1480    fn has_staged_changes(&self) -> bool {
1481        self.tracked_staged_count > 0
1482            || self.new_staged_count > 0
1483            || self.conflicted_staged_count > 0
1484    }
1485
1486    fn has_conflicts(&self) -> bool {
1487        self.conflicted_count > 0
1488    }
1489
1490    fn has_tracked_changes(&self) -> bool {
1491        self.tracked_count > 0
1492    }
1493
1494    fn has_unstaged_conflicts(&self) -> bool {
1495        self.conflicted_count > 0 && self.conflicted_count != self.conflicted_staged_count
1496    }
1497
1498    fn show_err_toast(&self, e: anyhow::Error, cx: &mut App) {
1499        let Some(workspace) = self.workspace.upgrade() else {
1500            return;
1501        };
1502        let notif_id = NotificationId::Named("git-operation-error".into());
1503
1504        let message = e.to_string();
1505        workspace.update(cx, |workspace, cx| {
1506            let toast = Toast::new(notif_id, message).on_click("Open Zed Log", |window, cx| {
1507                window.dispatch_action(workspace::OpenLog.boxed_clone(), cx);
1508            });
1509            workspace.show_toast(toast, cx);
1510        });
1511    }
1512
1513    pub fn panel_button(
1514        &self,
1515        id: impl Into<SharedString>,
1516        label: impl Into<SharedString>,
1517    ) -> Button {
1518        let id = id.into().clone();
1519        let label = label.into().clone();
1520
1521        Button::new(id, label)
1522            .label_size(LabelSize::Small)
1523            .layer(ElevationIndex::ElevatedSurface)
1524            .size(ButtonSize::Compact)
1525            .style(ButtonStyle::Filled)
1526    }
1527
1528    pub fn indent_size(&self, window: &Window, cx: &mut Context<Self>) -> Pixels {
1529        Checkbox::container_size(cx).to_pixels(window.rem_size())
1530    }
1531
1532    pub fn render_divider(&self, _cx: &mut Context<Self>) -> impl IntoElement {
1533        h_flex()
1534            .items_center()
1535            .h(px(8.))
1536            .child(Divider::horizontal_dashed().color(DividerColor::Border))
1537    }
1538
1539    pub fn render_panel_header(
1540        &self,
1541        window: &mut Window,
1542        cx: &mut Context<Self>,
1543    ) -> impl IntoElement {
1544        let all_repositories = self
1545            .project
1546            .read(cx)
1547            .git_store()
1548            .read(cx)
1549            .all_repositories();
1550
1551        let has_repo_above = all_repositories.iter().any(|repo| {
1552            repo.read(cx)
1553                .repository_entry
1554                .work_directory
1555                .is_above_project()
1556        });
1557
1558        self.panel_header_container(window, cx).when(
1559            all_repositories.len() > 1 || has_repo_above,
1560            |el| {
1561                el.child(
1562                    Label::new("Repository")
1563                        .size(LabelSize::Small)
1564                        .color(Color::Muted),
1565                )
1566                .child(self.render_repository_selector(cx))
1567            },
1568        )
1569    }
1570
1571    pub fn render_repository_selector(&self, cx: &mut Context<Self>) -> impl IntoElement {
1572        let active_repository = self.project.read(cx).active_repository(cx);
1573        let repository_display_name = active_repository
1574            .as_ref()
1575            .map(|repo| repo.read(cx).display_name(self.project.read(cx), cx))
1576            .unwrap_or_default();
1577
1578        RepositorySelectorPopoverMenu::new(
1579            self.repository_selector.clone(),
1580            ButtonLike::new("active-repository")
1581                .style(ButtonStyle::Subtle)
1582                .child(Label::new(repository_display_name).size(LabelSize::Small)),
1583            Tooltip::text("Select a repository"),
1584        )
1585    }
1586
1587    pub fn render_commit_editor(
1588        &self,
1589        window: &mut Window,
1590        cx: &mut Context<Self>,
1591    ) -> impl IntoElement {
1592        let editor = self.commit_editor.clone();
1593        let can_commit = (self.has_staged_changes() || self.has_tracked_changes())
1594            && self.pending_commit.is_none()
1595            && !editor.read(cx).is_empty(cx)
1596            && !self.has_unstaged_conflicts()
1597            && self.has_write_access(cx);
1598
1599        // let can_commit_all =
1600        //     !self.commit_pending && self.can_commit_all && !editor.read(cx).is_empty(cx);
1601        let panel_editor_style = panel_editor_style(true, window, cx);
1602
1603        let editor_focus_handle = editor.read(cx).focus_handle(cx).clone();
1604
1605        let focus_handle_1 = self.focus_handle(cx).clone();
1606        let tooltip = if self.has_staged_changes() {
1607            "Commit staged changes"
1608        } else {
1609            "Commit changes to tracked files"
1610        };
1611        let title = if self.has_staged_changes() {
1612            "Commit"
1613        } else {
1614            "Commit All"
1615        };
1616
1617        let commit_button = panel_filled_button(title)
1618            .tooltip(move |window, cx| {
1619                let focus_handle = focus_handle_1.clone();
1620                Tooltip::for_action_in(tooltip, &Commit, &focus_handle, window, cx)
1621            })
1622            .disabled(!can_commit)
1623            .on_click({
1624                cx.listener(move |this, _: &ClickEvent, window, cx| this.commit_changes(window, cx))
1625            });
1626
1627        let potential_co_authors = self.potential_co_authors(cx);
1628        let enable_coauthors = if potential_co_authors.is_empty() {
1629            None
1630        } else {
1631            Some(
1632                IconButton::new("co-authors", IconName::Person)
1633                    .icon_color(Color::Disabled)
1634                    .selected_icon_color(Color::Selected)
1635                    .toggle_state(self.add_coauthors)
1636                    .tooltip(move |_, cx| {
1637                        let title = format!(
1638                            "Add co-authored-by:{}{}",
1639                            if potential_co_authors.len() == 1 {
1640                                ""
1641                            } else {
1642                                "\n"
1643                            },
1644                            potential_co_authors
1645                                .iter()
1646                                .map(|(name, email)| format!(" {} <{}>", name, email))
1647                                .join("\n")
1648                        );
1649                        Tooltip::simple(title, cx)
1650                    })
1651                    .on_click(cx.listener(|this, _, _, cx| {
1652                        this.add_coauthors = !this.add_coauthors;
1653                        cx.notify();
1654                    })),
1655            )
1656        };
1657
1658        let branch = self
1659            .active_repository
1660            .as_ref()
1661            .and_then(|repo| repo.read(cx).branch().map(|b| b.name.clone()))
1662            .unwrap_or_else(|| "<no branch>".into());
1663
1664        let branch_selector = Button::new("branch-selector", branch)
1665            .color(Color::Muted)
1666            .style(ButtonStyle::Subtle)
1667            .icon(IconName::GitBranch)
1668            .icon_size(IconSize::Small)
1669            .icon_color(Color::Muted)
1670            .size(ButtonSize::Compact)
1671            .icon_position(IconPosition::Start)
1672            .tooltip(Tooltip::for_action_title(
1673                "Switch Branch",
1674                &zed_actions::git::Branch,
1675            ))
1676            .on_click(cx.listener(|_, _, window, cx| {
1677                window.dispatch_action(zed_actions::git::Branch.boxed_clone(), cx);
1678            }))
1679            .style(ButtonStyle::Transparent);
1680
1681        let footer_size = px(32.);
1682        let gap = px(16.0);
1683
1684        let max_height = window.line_height() * 6. + gap + footer_size;
1685
1686        panel_editor_container(window, cx)
1687            .id("commit-editor-container")
1688            .relative()
1689            .h(max_height)
1690            .w_full()
1691            .border_t_1()
1692            .border_color(cx.theme().colors().border)
1693            .bg(cx.theme().colors().editor_background)
1694            .on_click(cx.listener(move |_, _: &ClickEvent, window, _cx| {
1695                window.focus(&editor_focus_handle);
1696            }))
1697            .child(EditorElement::new(&self.commit_editor, panel_editor_style))
1698            .child(
1699                h_flex()
1700                    .absolute()
1701                    .bottom_0()
1702                    .left_2()
1703                    .h(footer_size)
1704                    .flex_none()
1705                    .child(branch_selector),
1706            )
1707            .child(
1708                h_flex()
1709                    .absolute()
1710                    .bottom_0()
1711                    .right_2()
1712                    .h(footer_size)
1713                    .flex_none()
1714                    .children(enable_coauthors)
1715                    .child(commit_button),
1716            )
1717    }
1718
1719    fn render_previous_commit(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> {
1720        let active_repository = self.active_repository.as_ref()?;
1721        let branch = active_repository.read(cx).branch()?;
1722        let commit = branch.most_recent_commit.as_ref()?.clone();
1723
1724        if branch.upstream.as_ref().is_some_and(|upstream| {
1725            if let Some(tracking) = &upstream.tracking {
1726                tracking.ahead == 0
1727            } else {
1728                true
1729            }
1730        }) {
1731            return None;
1732        }
1733        let tooltip = if self.has_staged_changes() {
1734            "git reset HEAD^ --soft"
1735        } else {
1736            "git reset HEAD^"
1737        };
1738
1739        let this = cx.entity();
1740        Some(
1741            h_flex()
1742                .items_center()
1743                .py_1p5()
1744                .px(px(8.))
1745                .bg(cx.theme().colors().background)
1746                .border_t_1()
1747                .border_color(cx.theme().colors().border)
1748                .gap_1p5()
1749                .child(
1750                    div()
1751                        .flex_grow()
1752                        .overflow_hidden()
1753                        .max_w(relative(0.6))
1754                        .h_full()
1755                        .child(
1756                            Label::new(commit.subject.clone())
1757                                .size(LabelSize::Small)
1758                                .text_ellipsis(),
1759                        )
1760                        .id("commit-msg-hover")
1761                        .hoverable_tooltip(move |window, cx| {
1762                            GitPanelMessageTooltip::new(
1763                                this.clone(),
1764                                commit.sha.clone(),
1765                                window,
1766                                cx,
1767                            )
1768                            .into()
1769                        }),
1770                )
1771                .child(div().flex_1())
1772                .child(
1773                    panel_filled_button("Uncommit")
1774                        .icon(IconName::Undo)
1775                        .icon_size(IconSize::Small)
1776                        .icon_color(Color::Muted)
1777                        .icon_position(IconPosition::Start)
1778                        .tooltip(Tooltip::for_action_title(tooltip, &git::Uncommit))
1779                        .on_click(cx.listener(|this, _, window, cx| this.uncommit(window, cx))),
1780                ),
1781        )
1782    }
1783
1784    fn render_empty_state(&self, cx: &mut Context<Self>) -> impl IntoElement {
1785        h_flex()
1786            .h_full()
1787            .flex_grow()
1788            .justify_center()
1789            .items_center()
1790            .child(
1791                v_flex()
1792                    .gap_3()
1793                    .child(if self.active_repository.is_some() {
1794                        "No changes to commit"
1795                    } else {
1796                        "No Git repositories"
1797                    })
1798                    .text_ui_sm(cx)
1799                    .mx_auto()
1800                    .text_color(Color::Placeholder.color(cx)),
1801            )
1802    }
1803
1804    fn render_scrollbar(&self, cx: &mut Context<Self>) -> Option<Stateful<Div>> {
1805        let scroll_bar_style = self.show_scrollbar(cx);
1806        let show_container = matches!(scroll_bar_style, ShowScrollbar::Always);
1807
1808        if !self.should_show_scrollbar(cx)
1809            || !(self.show_scrollbar || self.scrollbar_state.is_dragging())
1810        {
1811            return None;
1812        }
1813
1814        Some(
1815            div()
1816                .id("git-panel-vertical-scroll")
1817                .occlude()
1818                .flex_none()
1819                .h_full()
1820                .cursor_default()
1821                .when(show_container, |this| this.pl_1().px_1p5())
1822                .when(!show_container, |this| {
1823                    this.absolute().right_1().top_1().bottom_1().w(px(12.))
1824                })
1825                .on_mouse_move(cx.listener(|_, _, _, cx| {
1826                    cx.notify();
1827                    cx.stop_propagation()
1828                }))
1829                .on_hover(|_, _, cx| {
1830                    cx.stop_propagation();
1831                })
1832                .on_any_mouse_down(|_, _, cx| {
1833                    cx.stop_propagation();
1834                })
1835                .on_mouse_up(
1836                    MouseButton::Left,
1837                    cx.listener(|this, _, window, cx| {
1838                        if !this.scrollbar_state.is_dragging()
1839                            && !this.focus_handle.contains_focused(window, cx)
1840                        {
1841                            this.hide_scrollbar(window, cx);
1842                            cx.notify();
1843                        }
1844
1845                        cx.stop_propagation();
1846                    }),
1847                )
1848                .on_scroll_wheel(cx.listener(|_, _, _, cx| {
1849                    cx.notify();
1850                }))
1851                .children(Scrollbar::vertical(
1852                    // percentage as f32..end_offset as f32,
1853                    self.scrollbar_state.clone(),
1854                )),
1855        )
1856    }
1857
1858    pub fn render_buffer_header_controls(
1859        &self,
1860        entity: &Entity<Self>,
1861        file: &Arc<dyn File>,
1862        _: &Window,
1863        cx: &App,
1864    ) -> Option<AnyElement> {
1865        let repo = self.active_repository.as_ref()?.read(cx);
1866        let repo_path = repo.worktree_id_path_to_repo_path(file.worktree_id(cx), file.path())?;
1867        let ix = self.entries_by_path.get(&repo_path)?;
1868        let entry = self.entries.get(*ix)?;
1869
1870        let is_staged = self.entry_is_staged(entry.status_entry()?);
1871
1872        let checkbox = Checkbox::new("stage-file", is_staged.into())
1873            .disabled(!self.has_write_access(cx))
1874            .fill()
1875            .elevation(ElevationIndex::Surface)
1876            .on_click({
1877                let entry = entry.clone();
1878                let git_panel = entity.downgrade();
1879                move |_, window, cx| {
1880                    git_panel
1881                        .update(cx, |this, cx| {
1882                            this.toggle_staged_for_entry(&entry, window, cx);
1883                            cx.stop_propagation();
1884                        })
1885                        .ok();
1886                }
1887            });
1888        Some(
1889            h_flex()
1890                .id("start-slot")
1891                .child(checkbox)
1892                .child(git_status_icon(entry.status_entry()?.status, cx))
1893                .on_mouse_down(MouseButton::Left, |_, _, cx| {
1894                    // prevent the list item active state triggering when toggling checkbox
1895                    cx.stop_propagation();
1896                })
1897                .into_any_element(),
1898        )
1899    }
1900
1901    fn render_entries(
1902        &self,
1903        has_write_access: bool,
1904        _: &Window,
1905        cx: &mut Context<Self>,
1906    ) -> impl IntoElement {
1907        let entry_count = self.entries.len();
1908
1909        v_flex()
1910            .size_full()
1911            .flex_grow()
1912            .overflow_hidden()
1913            .child(
1914                uniform_list(cx.entity().clone(), "entries", entry_count, {
1915                    move |this, range, window, cx| {
1916                        let mut items = Vec::with_capacity(range.end - range.start);
1917
1918                        for ix in range {
1919                            match &this.entries.get(ix) {
1920                                Some(GitListEntry::GitStatusEntry(entry)) => {
1921                                    items.push(this.render_entry(
1922                                        ix,
1923                                        entry,
1924                                        has_write_access,
1925                                        window,
1926                                        cx,
1927                                    ));
1928                                }
1929                                Some(GitListEntry::Header(header)) => {
1930                                    items.push(this.render_list_header(
1931                                        ix,
1932                                        header,
1933                                        has_write_access,
1934                                        window,
1935                                        cx,
1936                                    ));
1937                                }
1938                                None => {}
1939                            }
1940                        }
1941
1942                        items
1943                    }
1944                })
1945                .size_full()
1946                .with_sizing_behavior(ListSizingBehavior::Infer)
1947                .with_horizontal_sizing_behavior(ListHorizontalSizingBehavior::Unconstrained)
1948                .track_scroll(self.scroll_handle.clone()),
1949            )
1950            .on_mouse_down(
1951                MouseButton::Right,
1952                cx.listener(move |this, event: &MouseDownEvent, window, cx| {
1953                    this.deploy_panel_context_menu(event.position, window, cx)
1954                }),
1955            )
1956            .children(self.render_scrollbar(cx))
1957    }
1958
1959    fn entry_label(&self, label: impl Into<SharedString>, color: Color) -> Label {
1960        Label::new(label.into()).color(color).single_line()
1961    }
1962
1963    fn render_list_header(
1964        &self,
1965        ix: usize,
1966        header: &GitHeaderEntry,
1967        _: bool,
1968        _: &Window,
1969        _: &Context<Self>,
1970    ) -> AnyElement {
1971        div()
1972            .w_full()
1973            .child(
1974                ListItem::new(ix)
1975                    .spacing(ListItemSpacing::Sparse)
1976                    .disabled(true)
1977                    .child(
1978                        Label::new(header.title())
1979                            .color(Color::Muted)
1980                            .size(LabelSize::Small)
1981                            .single_line(),
1982                    ),
1983            )
1984            .into_any_element()
1985    }
1986
1987    fn load_commit_details(
1988        &self,
1989        sha: &str,
1990        cx: &mut Context<Self>,
1991    ) -> Task<Result<CommitDetails>> {
1992        let Some(repo) = self.active_repository.clone() else {
1993            return Task::ready(Err(anyhow::anyhow!("no active repo")));
1994        };
1995        repo.update(cx, |repo, cx| repo.show(sha, cx))
1996    }
1997
1998    fn deploy_entry_context_menu(
1999        &mut self,
2000        position: Point<Pixels>,
2001        ix: usize,
2002        window: &mut Window,
2003        cx: &mut Context<Self>,
2004    ) {
2005        let Some(entry) = self.entries.get(ix).and_then(|e| e.status_entry()) else {
2006            return;
2007        };
2008        let revert_title = if entry.status.is_deleted() {
2009            "Restore file"
2010        } else if entry.status.is_created() {
2011            "Trash file"
2012        } else {
2013            "Discard changes"
2014        };
2015        let context_menu = ContextMenu::build(window, cx, |context_menu, _, _| {
2016            context_menu
2017                .action("Stage File", ToggleStaged.boxed_clone())
2018                .action(revert_title, editor::actions::RevertFile.boxed_clone())
2019                .separator()
2020                .action("Open Diff", Confirm.boxed_clone())
2021                .action("Open File", SecondaryConfirm.boxed_clone())
2022        });
2023        self.selected_entry = Some(ix);
2024        self.set_context_menu(context_menu, position, window, cx);
2025    }
2026
2027    fn deploy_panel_context_menu(
2028        &mut self,
2029        position: Point<Pixels>,
2030        window: &mut Window,
2031        cx: &mut Context<Self>,
2032    ) {
2033        let context_menu = ContextMenu::build(window, cx, |context_menu, _, _| {
2034            context_menu
2035                .action("Stage All", StageAll.boxed_clone())
2036                .action("Unstage All", UnstageAll.boxed_clone())
2037                .action("Open Diff", project_diff::Diff.boxed_clone())
2038                .separator()
2039                .action(
2040                    "Discard Tracked Changes",
2041                    DiscardTrackedChanges.boxed_clone(),
2042                )
2043                .action("Trash Untracked Files", TrashUntrackedFiles.boxed_clone())
2044        });
2045        self.set_context_menu(context_menu, position, window, cx);
2046    }
2047
2048    fn set_context_menu(
2049        &mut self,
2050        context_menu: Entity<ContextMenu>,
2051        position: Point<Pixels>,
2052        window: &Window,
2053        cx: &mut Context<Self>,
2054    ) {
2055        let subscription = cx.subscribe_in(
2056            &context_menu,
2057            window,
2058            |this, _, _: &DismissEvent, window, cx| {
2059                if this.context_menu.as_ref().is_some_and(|context_menu| {
2060                    context_menu.0.focus_handle(cx).contains_focused(window, cx)
2061                }) {
2062                    cx.focus_self(window);
2063                }
2064                this.context_menu.take();
2065                cx.notify();
2066            },
2067        );
2068        self.context_menu = Some((context_menu, position, subscription));
2069        cx.notify();
2070    }
2071
2072    fn render_entry(
2073        &self,
2074        ix: usize,
2075        entry: &GitStatusEntry,
2076        has_write_access: bool,
2077        window: &Window,
2078        cx: &Context<Self>,
2079    ) -> AnyElement {
2080        let display_name = entry
2081            .repo_path
2082            .file_name()
2083            .map(|name| name.to_string_lossy().into_owned())
2084            .unwrap_or_else(|| entry.repo_path.to_string_lossy().into_owned());
2085
2086        let repo_path = entry.repo_path.clone();
2087        let selected = self.selected_entry == Some(ix);
2088        let status_style = GitPanelSettings::get_global(cx).status_style;
2089        let status = entry.status;
2090        let has_conflict = status.is_conflicted();
2091        let is_modified = status.is_modified();
2092        let is_deleted = status.is_deleted();
2093
2094        let label_color = if status_style == StatusStyle::LabelColor {
2095            if has_conflict {
2096                Color::Conflict
2097            } else if is_modified {
2098                Color::Modified
2099            } else if is_deleted {
2100                // We don't want a bunch of red labels in the list
2101                Color::Disabled
2102            } else {
2103                Color::Created
2104            }
2105        } else {
2106            Color::Default
2107        };
2108
2109        let path_color = if status.is_deleted() {
2110            Color::Disabled
2111        } else {
2112            Color::Muted
2113        };
2114
2115        let id: ElementId = ElementId::Name(format!("entry_{}", display_name).into());
2116
2117        let mut is_staged: ToggleState = self.entry_is_staged(entry).into();
2118
2119        if !self.has_staged_changes() && !self.has_conflicts() && !entry.status.is_created() {
2120            is_staged = ToggleState::Selected;
2121        }
2122
2123        let checkbox = Checkbox::new(id, is_staged)
2124            .disabled(!has_write_access)
2125            .fill()
2126            .placeholder(!self.has_staged_changes() && !self.has_conflicts())
2127            .elevation(ElevationIndex::Surface)
2128            .on_click({
2129                let entry = entry.clone();
2130                cx.listener(move |this, _, window, cx| {
2131                    this.toggle_staged_for_entry(
2132                        &GitListEntry::GitStatusEntry(entry.clone()),
2133                        window,
2134                        cx,
2135                    );
2136                    cx.stop_propagation();
2137                })
2138            });
2139
2140        let start_slot = h_flex()
2141            .id(("start-slot", ix))
2142            .gap(DynamicSpacing::Base04.rems(cx))
2143            .child(checkbox)
2144            .tooltip(|window, cx| Tooltip::for_action("Stage File", &ToggleStaged, window, cx))
2145            .child(git_status_icon(status, cx))
2146            .on_mouse_down(MouseButton::Left, |_, _, cx| {
2147                // prevent the list item active state triggering when toggling checkbox
2148                cx.stop_propagation();
2149            });
2150
2151        div()
2152            .w_full()
2153            .child(
2154                ListItem::new(ix)
2155                    .spacing(ListItemSpacing::Sparse)
2156                    .start_slot(start_slot)
2157                    .toggle_state(selected)
2158                    .focused(selected && self.focus_handle(cx).is_focused(window))
2159                    .disabled(!has_write_access)
2160                    .on_click({
2161                        cx.listener(move |this, event: &ClickEvent, window, cx| {
2162                            this.selected_entry = Some(ix);
2163                            cx.notify();
2164                            if event.modifiers().secondary() {
2165                                this.open_file(&Default::default(), window, cx)
2166                            } else {
2167                                this.open_diff(&Default::default(), window, cx);
2168                            }
2169                        })
2170                    })
2171                    .on_secondary_mouse_down(cx.listener(
2172                        move |this, event: &MouseDownEvent, window, cx| {
2173                            this.deploy_entry_context_menu(event.position, ix, window, cx);
2174                            cx.stop_propagation();
2175                        },
2176                    ))
2177                    .child(
2178                        h_flex()
2179                            .when_some(repo_path.parent(), |this, parent| {
2180                                let parent_str = parent.to_string_lossy();
2181                                if !parent_str.is_empty() {
2182                                    this.child(
2183                                        self.entry_label(format!("{}/", parent_str), path_color)
2184                                            .when(status.is_deleted(), |this| this.strikethrough()),
2185                                    )
2186                                } else {
2187                                    this
2188                                }
2189                            })
2190                            .child(
2191                                self.entry_label(display_name.clone(), label_color)
2192                                    .when(status.is_deleted(), |this| this.strikethrough()),
2193                            ),
2194                    ),
2195            )
2196            .into_any_element()
2197    }
2198
2199    fn has_write_access(&self, cx: &App) -> bool {
2200        !self.project.read(cx).is_read_only(cx)
2201    }
2202}
2203
2204impl Render for GitPanel {
2205    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2206        let project = self.project.read(cx);
2207        let has_entries = self.entries.len() > 0;
2208        let room = self
2209            .workspace
2210            .upgrade()
2211            .and_then(|workspace| workspace.read(cx).active_call()?.read(cx).room().cloned());
2212
2213        let has_write_access = self.has_write_access(cx);
2214
2215        let has_co_authors = room.map_or(false, |room| {
2216            room.read(cx)
2217                .remote_participants()
2218                .values()
2219                .any(|remote_participant| remote_participant.can_write())
2220        });
2221
2222        v_flex()
2223            .id("git_panel")
2224            .key_context(self.dispatch_context(window, cx))
2225            .track_focus(&self.focus_handle)
2226            .on_modifiers_changed(cx.listener(Self::handle_modifiers_changed))
2227            .when(has_write_access && !project.is_read_only(cx), |this| {
2228                this.on_action(cx.listener(|this, &ToggleStaged, window, cx| {
2229                    this.toggle_staged_for_selected(&ToggleStaged, window, cx)
2230                }))
2231                .on_action(cx.listener(GitPanel::commit))
2232            })
2233            .on_action(cx.listener(Self::select_first))
2234            .on_action(cx.listener(Self::select_next))
2235            .on_action(cx.listener(Self::select_prev))
2236            .on_action(cx.listener(Self::select_last))
2237            .on_action(cx.listener(Self::close_panel))
2238            .on_action(cx.listener(Self::open_diff))
2239            .on_action(cx.listener(Self::open_file))
2240            .on_action(cx.listener(Self::revert_selected))
2241            .on_action(cx.listener(Self::focus_changes_list))
2242            .on_action(cx.listener(Self::focus_editor))
2243            .on_action(cx.listener(Self::toggle_staged_for_selected))
2244            .on_action(cx.listener(Self::stage_all))
2245            .on_action(cx.listener(Self::unstage_all))
2246            .on_action(cx.listener(Self::discard_tracked_changes))
2247            .on_action(cx.listener(Self::clean_all))
2248            .when(has_write_access && has_co_authors, |git_panel| {
2249                git_panel.on_action(cx.listener(Self::toggle_fill_co_authors))
2250            })
2251            // .on_action(cx.listener(|this, &OpenSelected, cx| this.open_selected(&OpenSelected, cx)))
2252            .on_hover(cx.listener(|this, hovered, window, cx| {
2253                if *hovered {
2254                    this.show_scrollbar = true;
2255                    this.hide_scrollbar_task.take();
2256                    cx.notify();
2257                } else if !this.focus_handle.contains_focused(window, cx) {
2258                    this.hide_scrollbar(window, cx);
2259                }
2260            }))
2261            .size_full()
2262            .overflow_hidden()
2263            .bg(ElevationIndex::Surface.bg(cx))
2264            .child(self.render_panel_header(window, cx))
2265            .child(if has_entries {
2266                self.render_entries(has_write_access, window, cx)
2267                    .into_any_element()
2268            } else {
2269                self.render_empty_state(cx).into_any_element()
2270            })
2271            .children(self.render_previous_commit(cx))
2272            .child(self.render_commit_editor(window, cx))
2273            .children(self.context_menu.as_ref().map(|(menu, position, _)| {
2274                deferred(
2275                    anchored()
2276                        .position(*position)
2277                        .anchor(gpui::Corner::TopLeft)
2278                        .child(menu.clone()),
2279                )
2280                .with_priority(1)
2281            }))
2282    }
2283}
2284
2285impl Focusable for GitPanel {
2286    fn focus_handle(&self, _: &App) -> gpui::FocusHandle {
2287        self.focus_handle.clone()
2288    }
2289}
2290
2291impl EventEmitter<Event> for GitPanel {}
2292
2293impl EventEmitter<PanelEvent> for GitPanel {}
2294
2295pub(crate) struct GitPanelAddon {
2296    pub(crate) git_panel: Entity<GitPanel>,
2297}
2298
2299impl editor::Addon for GitPanelAddon {
2300    fn to_any(&self) -> &dyn std::any::Any {
2301        self
2302    }
2303
2304    fn render_buffer_header_controls(
2305        &self,
2306        excerpt_info: &ExcerptInfo,
2307        window: &Window,
2308        cx: &App,
2309    ) -> Option<AnyElement> {
2310        let file = excerpt_info.buffer.file()?;
2311        let git_panel = self.git_panel.read(cx);
2312
2313        git_panel.render_buffer_header_controls(&self.git_panel, &file, window, cx)
2314    }
2315}
2316
2317impl Panel for GitPanel {
2318    fn persistent_name() -> &'static str {
2319        "GitPanel"
2320    }
2321
2322    fn position(&self, _: &Window, cx: &App) -> DockPosition {
2323        GitPanelSettings::get_global(cx).dock
2324    }
2325
2326    fn position_is_valid(&self, position: DockPosition) -> bool {
2327        matches!(position, DockPosition::Left | DockPosition::Right)
2328    }
2329
2330    fn set_position(&mut self, position: DockPosition, _: &mut Window, cx: &mut Context<Self>) {
2331        settings::update_settings_file::<GitPanelSettings>(
2332            self.fs.clone(),
2333            cx,
2334            move |settings, _| settings.dock = Some(position),
2335        );
2336    }
2337
2338    fn size(&self, _: &Window, cx: &App) -> Pixels {
2339        self.width
2340            .unwrap_or_else(|| GitPanelSettings::get_global(cx).default_width)
2341    }
2342
2343    fn set_size(&mut self, size: Option<Pixels>, _: &mut Window, cx: &mut Context<Self>) {
2344        self.width = size;
2345        self.serialize(cx);
2346        cx.notify();
2347    }
2348
2349    fn icon(&self, _: &Window, cx: &App) -> Option<ui::IconName> {
2350        Some(ui::IconName::GitBranch).filter(|_| GitPanelSettings::get_global(cx).button)
2351    }
2352
2353    fn icon_tooltip(&self, _window: &Window, _cx: &App) -> Option<&'static str> {
2354        Some("Git Panel")
2355    }
2356
2357    fn toggle_action(&self) -> Box<dyn Action> {
2358        Box::new(ToggleFocus)
2359    }
2360
2361    fn activation_priority(&self) -> u32 {
2362        2
2363    }
2364}
2365
2366impl PanelHeader for GitPanel {}
2367
2368struct GitPanelMessageTooltip {
2369    commit_tooltip: Option<Entity<CommitTooltip>>,
2370}
2371
2372impl GitPanelMessageTooltip {
2373    fn new(
2374        git_panel: Entity<GitPanel>,
2375        sha: SharedString,
2376        window: &mut Window,
2377        cx: &mut App,
2378    ) -> Entity<Self> {
2379        let workspace = git_panel.read(cx).workspace.clone();
2380        cx.new(|cx| {
2381            cx.spawn_in(window, |this, mut cx| async move {
2382                let language_registry = workspace.update(&mut cx, |workspace, _cx| {
2383                    workspace.app_state().languages.clone()
2384                })?;
2385
2386                let details = git_panel
2387                    .update(&mut cx, |git_panel, cx| {
2388                        git_panel.load_commit_details(&sha, cx)
2389                    })?
2390                    .await?;
2391
2392                let mut parsed_message = ParsedMarkdown::default();
2393                markdown::parse_markdown_block(
2394                    &details.message,
2395                    Some(&language_registry),
2396                    None,
2397                    &mut parsed_message.text,
2398                    &mut parsed_message.highlights,
2399                    &mut parsed_message.region_ranges,
2400                    &mut parsed_message.regions,
2401                )
2402                .await;
2403
2404                let commit_details = editor::commit_tooltip::CommitDetails {
2405                    sha: details.sha.clone(),
2406                    committer_name: details.committer_name.clone(),
2407                    committer_email: details.committer_email.clone(),
2408                    commit_time: OffsetDateTime::from_unix_timestamp(details.commit_timestamp)?,
2409                    message: Some(editor::commit_tooltip::ParsedCommitMessage {
2410                        message: details.message.clone(),
2411                        parsed_message,
2412                        ..Default::default()
2413                    }),
2414                };
2415
2416                this.update_in(&mut cx, |this: &mut GitPanelMessageTooltip, window, cx| {
2417                    this.commit_tooltip = Some(cx.new(move |cx| {
2418                        CommitTooltip::new(
2419                            commit_details,
2420                            panel_editor_style(true, window, cx),
2421                            Some(workspace),
2422                        )
2423                    }));
2424                    cx.notify();
2425                })
2426            })
2427            .detach();
2428
2429            Self {
2430                commit_tooltip: None,
2431            }
2432        })
2433    }
2434}
2435
2436impl Render for GitPanelMessageTooltip {
2437    fn render(&mut self, _window: &mut Window, _cx: &mut Context<'_, Self>) -> impl IntoElement {
2438        if let Some(commit_tooltip) = &self.commit_tooltip {
2439            commit_tooltip.clone().into_any_element()
2440        } else {
2441            gpui::Empty.into_any_element()
2442        }
2443    }
2444}