git_panel.rs

   1use crate::askpass_modal::AskPassModal;
   2use crate::commit_modal::CommitModal;
   3use crate::commit_tooltip::CommitTooltip;
   4use crate::commit_view::CommitView;
   5use crate::git_panel_settings::GitPanelScrollbarAccessor;
   6use crate::project_diff::{self, BranchDiff, Diff, ProjectDiff};
   7use crate::remote_output::{self, RemoteAction, SuccessMessage};
   8use crate::{branch_picker, picker_prompt, render_remote_button};
   9use crate::{
  10    file_history_view::FileHistoryView, git_panel_settings::GitPanelSettings, git_status_icon,
  11    repository_selector::RepositorySelector,
  12};
  13use agent_settings::AgentSettings;
  14use alacritty_terminal::vte::ansi;
  15use anyhow::Context as _;
  16use askpass::AskPassDelegate;
  17use collections::{BTreeMap, HashMap, HashSet};
  18use db::kvp::KeyValueStore;
  19use editor::{
  20    Direction, Editor, EditorElement, EditorMode, MultiBuffer, MultiBufferOffset,
  21    actions::ExpandAllDiffHunks,
  22};
  23use editor::{EditorStyle, RewrapOptions};
  24use file_icons::FileIcons;
  25use futures::StreamExt as _;
  26use git::commit::ParsedCommitMessage;
  27use git::repository::{
  28    Branch, CommitDetails, CommitOptions, CommitSummary, DiffType, FetchOptions, GitCommitTemplate,
  29    GitCommitter, PushOptions, Remote, RemoteCommandOutput, ResetMode, Upstream, UpstreamTracking,
  30    UpstreamTrackingStatus, get_git_committer,
  31};
  32use git::stash::GitStash;
  33use git::status::{DiffStat, StageStatus};
  34use git::{Amend, Signoff, ToggleStaged, repository::RepoPath, status::FileStatus};
  35use git::{
  36    ExpandCommitEditor, GitHostingProviderRegistry, RestoreTrackedFiles, StageAll, StashAll,
  37    StashApply, StashPop, TrashUntrackedFiles, UnstageAll,
  38};
  39use gpui::{
  40    Action, AsyncApp, AsyncWindowContext, Bounds, ClickEvent, Corner, DismissEvent, Empty, Entity,
  41    EventEmitter, FocusHandle, Focusable, KeyContext, MouseButton, MouseDownEvent, Point,
  42    PromptLevel, ScrollStrategy, Subscription, Task, TextStyle, UniformListScrollHandle,
  43    WeakEntity, actions, anchored, deferred, point, size, uniform_list,
  44};
  45use itertools::Itertools;
  46use language::{Buffer, File};
  47use language_model::{
  48    CompletionIntent, ConfiguredModel, LanguageModelRegistry, LanguageModelRequest,
  49    LanguageModelRequestMessage, Role,
  50};
  51use menu;
  52use multi_buffer::ExcerptBoundaryInfo;
  53use notifications::status_toast::StatusToast;
  54use panel::{PanelHeader, panel_button, panel_filled_button, panel_icon_button};
  55use project::{
  56    Fs, Project, ProjectPath,
  57    git_store::{GitStoreEvent, Repository, RepositoryEvent, RepositoryId, pending_op},
  58    project_settings::{GitPathStyle, ProjectSettings},
  59};
  60use prompt_store::{BuiltInPrompt, PromptId, PromptStore, RULES_FILE_NAMES};
  61use proto::RpcError;
  62use serde::{Deserialize, Serialize};
  63use settings::{Settings, SettingsStore, StatusStyle};
  64use smallvec::SmallVec;
  65use std::future::Future;
  66use std::ops::Range;
  67use std::path::Path;
  68use std::{sync::Arc, time::Duration, usize};
  69use strum::{IntoEnumIterator, VariantNames};
  70use theme_settings::ThemeSettings;
  71use time::OffsetDateTime;
  72use ui::{
  73    ButtonLike, Checkbox, CommonAnimationExt, ContextMenu, ElevationIndex, IndentGuideColors,
  74    PopoverMenu, RenderedIndentGuide, ScrollAxes, Scrollbars, SplitButton, Tooltip, WithScrollbar,
  75    prelude::*,
  76};
  77use util::paths::PathStyle;
  78use util::{ResultExt, TryFutureExt, maybe, rel_path::RelPath};
  79use workspace::SERIALIZATION_THROTTLE_TIME;
  80use workspace::{
  81    Workspace,
  82    dock::{DockPosition, Panel, PanelEvent},
  83    notifications::{DetachAndPromptErr, ErrorMessagePrompt, NotificationId, NotifyResultExt},
  84};
  85
  86actions!(
  87    git_panel,
  88    [
  89        /// Closes the git panel.
  90        Close,
  91        /// Toggles the git panel.
  92        Toggle,
  93        /// Toggles focus on the git panel.
  94        ToggleFocus,
  95        /// Opens the git panel menu.
  96        OpenMenu,
  97        /// Focuses on the commit message editor.
  98        FocusEditor,
  99        /// Focuses on the changes list.
 100        FocusChanges,
 101        /// Select next git panel menu item, and show it in the diff view
 102        NextEntry,
 103        /// Select previous git panel menu item, and show it in the diff view
 104        PreviousEntry,
 105        /// Select first git panel menu item, and show it in the diff view
 106        FirstEntry,
 107        /// Select last git panel menu item, and show it in the diff view
 108        LastEntry,
 109        /// Toggles automatic co-author suggestions.
 110        ToggleFillCoAuthors,
 111        /// Toggles sorting entries by path vs status.
 112        ToggleSortByPath,
 113        /// Toggles showing entries in tree vs flat view.
 114        ToggleTreeView,
 115        /// Expands the selected entry to show its children.
 116        ExpandSelectedEntry,
 117        /// Collapses the selected entry to hide its children.
 118        CollapseSelectedEntry,
 119    ]
 120);
 121
 122actions!(
 123    git_graph,
 124    [
 125        /// Opens the Git Graph Tab.
 126        Open,
 127    ]
 128);
 129
 130/// Opens the Git Graph Tab at a specific commit.
 131#[derive(Clone, PartialEq, serde::Deserialize, schemars::JsonSchema, gpui::Action)]
 132#[action(namespace = git_graph)]
 133pub struct OpenAtCommit {
 134    pub sha: String,
 135}
 136
 137fn prompt<T>(
 138    msg: &str,
 139    detail: Option<&str>,
 140    window: &mut Window,
 141    cx: &mut App,
 142) -> Task<anyhow::Result<T>>
 143where
 144    T: IntoEnumIterator + VariantNames + 'static,
 145{
 146    let rx = window.prompt(PromptLevel::Info, msg, detail, T::VARIANTS, cx);
 147    cx.spawn(async move |_| Ok(T::iter().nth(rx.await?).unwrap()))
 148}
 149
 150#[derive(strum::EnumIter, strum::VariantNames)]
 151#[strum(serialize_all = "title_case")]
 152enum TrashCancel {
 153    Trash,
 154    Cancel,
 155}
 156
 157struct GitMenuState {
 158    has_tracked_changes: bool,
 159    has_staged_changes: bool,
 160    has_unstaged_changes: bool,
 161    has_new_changes: bool,
 162    sort_by_path: bool,
 163    has_stash_items: bool,
 164    tree_view: bool,
 165}
 166
 167fn git_panel_context_menu(
 168    focus_handle: FocusHandle,
 169    state: GitMenuState,
 170    window: &mut Window,
 171    cx: &mut App,
 172) -> Entity<ContextMenu> {
 173    ContextMenu::build(window, cx, move |context_menu, _, _| {
 174        context_menu
 175            .context(focus_handle)
 176            .action_disabled_when(
 177                !state.has_unstaged_changes,
 178                "Stage All",
 179                StageAll.boxed_clone(),
 180            )
 181            .action_disabled_when(
 182                !state.has_staged_changes,
 183                "Unstage All",
 184                UnstageAll.boxed_clone(),
 185            )
 186            .separator()
 187            .action_disabled_when(
 188                !(state.has_new_changes || state.has_tracked_changes),
 189                "Stash All",
 190                StashAll.boxed_clone(),
 191            )
 192            .action_disabled_when(!state.has_stash_items, "Stash Pop", StashPop.boxed_clone())
 193            .action("View Stash", zed_actions::git::ViewStash.boxed_clone())
 194            .separator()
 195            .action("Open Diff", project_diff::Diff.boxed_clone())
 196            .separator()
 197            .action_disabled_when(
 198                !state.has_tracked_changes,
 199                "Discard Tracked Changes",
 200                RestoreTrackedFiles.boxed_clone(),
 201            )
 202            .action_disabled_when(
 203                !state.has_new_changes,
 204                "Trash Untracked Files",
 205                TrashUntrackedFiles.boxed_clone(),
 206            )
 207            .separator()
 208            .entry(
 209                if state.tree_view {
 210                    "Flat View"
 211                } else {
 212                    "Tree View"
 213                },
 214                Some(Box::new(ToggleTreeView)),
 215                move |window, cx| window.dispatch_action(Box::new(ToggleTreeView), cx),
 216            )
 217            .when(!state.tree_view, |this| {
 218                this.entry(
 219                    if state.sort_by_path {
 220                        "Sort by Status"
 221                    } else {
 222                        "Sort by Path"
 223                    },
 224                    Some(Box::new(ToggleSortByPath)),
 225                    move |window, cx| window.dispatch_action(Box::new(ToggleSortByPath), cx),
 226                )
 227            })
 228    })
 229}
 230
 231const GIT_PANEL_KEY: &str = "GitPanel";
 232
 233const UPDATE_DEBOUNCE: Duration = Duration::from_millis(50);
 234// TODO: We should revise this part. It seems the indentation width is not aligned with the one in project panel
 235const TREE_INDENT: f32 = 16.0;
 236
 237pub fn register(workspace: &mut Workspace) {
 238    workspace.register_action(|workspace, _: &ToggleFocus, window, cx| {
 239        workspace.toggle_panel_focus::<GitPanel>(window, cx);
 240    });
 241    workspace.register_action(|workspace, _: &Toggle, window, cx| {
 242        if !workspace.toggle_panel_focus::<GitPanel>(window, cx) {
 243            workspace.close_panel::<GitPanel>(window, cx);
 244        }
 245    });
 246    workspace.register_action(|workspace, _: &ExpandCommitEditor, window, cx| {
 247        CommitModal::toggle(workspace, None, window, cx)
 248    });
 249    workspace.register_action(|workspace, _: &git::Init, window, cx| {
 250        if let Some(panel) = workspace.panel::<GitPanel>(cx) {
 251            panel.update(cx, |panel, cx| panel.git_init(window, cx));
 252        }
 253    });
 254}
 255
 256#[derive(Debug, Clone)]
 257pub enum Event {
 258    Focus,
 259}
 260
 261#[derive(Serialize, Deserialize)]
 262struct SerializedGitPanel {
 263    #[serde(default)]
 264    amend_pending: bool,
 265    #[serde(default)]
 266    signoff_enabled: bool,
 267}
 268
 269#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
 270enum Section {
 271    Conflict,
 272    Tracked,
 273    New,
 274}
 275
 276#[derive(Debug, PartialEq, Eq, Clone)]
 277struct GitHeaderEntry {
 278    header: Section,
 279}
 280
 281impl GitHeaderEntry {
 282    pub fn contains(&self, status_entry: &GitStatusEntry, repo: &Repository) -> bool {
 283        let this = &self.header;
 284        let status = status_entry.status;
 285        match this {
 286            Section::Conflict => {
 287                repo.had_conflict_on_last_merge_head_change(&status_entry.repo_path)
 288            }
 289            Section::Tracked => !status.is_created(),
 290            Section::New => status.is_created(),
 291        }
 292    }
 293    pub fn title(&self) -> &'static str {
 294        match self.header {
 295            Section::Conflict => "Conflicts",
 296            Section::Tracked => "Tracked",
 297            Section::New => "Untracked",
 298        }
 299    }
 300}
 301
 302#[derive(Debug, PartialEq, Eq, Clone)]
 303enum GitListEntry {
 304    Status(GitStatusEntry),
 305    TreeStatus(GitTreeStatusEntry),
 306    Directory(GitTreeDirEntry),
 307    Header(GitHeaderEntry),
 308}
 309
 310impl GitListEntry {
 311    fn status_entry(&self) -> Option<&GitStatusEntry> {
 312        match self {
 313            GitListEntry::Status(entry) => Some(entry),
 314            GitListEntry::TreeStatus(entry) => Some(&entry.entry),
 315            _ => None,
 316        }
 317    }
 318
 319    fn directory_entry(&self) -> Option<&GitTreeDirEntry> {
 320        match self {
 321            GitListEntry::Directory(entry) => Some(entry),
 322            _ => None,
 323        }
 324    }
 325
 326    /// Returns the tree indentation depth for this entry.
 327    fn depth(&self) -> usize {
 328        match self {
 329            GitListEntry::Directory(dir) => dir.depth,
 330            GitListEntry::TreeStatus(status) => status.depth,
 331            _ => 0,
 332        }
 333    }
 334}
 335
 336enum GitPanelViewMode {
 337    Flat,
 338    Tree(TreeViewState),
 339}
 340
 341impl GitPanelViewMode {
 342    fn from_settings(cx: &App) -> Self {
 343        if GitPanelSettings::get_global(cx).tree_view {
 344            GitPanelViewMode::Tree(TreeViewState::default())
 345        } else {
 346            GitPanelViewMode::Flat
 347        }
 348    }
 349
 350    fn tree_state(&self) -> Option<&TreeViewState> {
 351        match self {
 352            GitPanelViewMode::Tree(state) => Some(state),
 353            GitPanelViewMode::Flat => None,
 354        }
 355    }
 356
 357    fn tree_state_mut(&mut self) -> Option<&mut TreeViewState> {
 358        match self {
 359            GitPanelViewMode::Tree(state) => Some(state),
 360            GitPanelViewMode::Flat => None,
 361        }
 362    }
 363}
 364
 365#[derive(Default)]
 366struct TreeViewState {
 367    // Maps visible index to actual entry index.
 368    // Length equals the number of visible entries.
 369    // This is needed because some entries (like collapsed directories) may be hidden.
 370    logical_indices: Vec<usize>,
 371    expanded_dirs: HashMap<TreeKey, bool>,
 372    directory_descendants: HashMap<TreeKey, Vec<GitStatusEntry>>,
 373}
 374
 375impl TreeViewState {
 376    fn build_tree_entries(
 377        &mut self,
 378        section: Section,
 379        mut entries: Vec<GitStatusEntry>,
 380        seen_directories: &mut HashSet<TreeKey>,
 381    ) -> Vec<(GitListEntry, bool)> {
 382        if entries.is_empty() {
 383            return Vec::new();
 384        }
 385
 386        entries.sort_by(|a, b| a.repo_path.cmp(&b.repo_path));
 387
 388        let mut root = TreeNode::default();
 389        for entry in entries {
 390            let components: Vec<&str> = entry.repo_path.components().collect();
 391            if components.is_empty() {
 392                root.files.push(entry);
 393                continue;
 394            }
 395
 396            let mut current = &mut root;
 397            let mut current_path = String::new();
 398
 399            for (ix, component) in components.iter().enumerate() {
 400                if ix == components.len() - 1 {
 401                    current.files.push(entry.clone());
 402                } else {
 403                    if !current_path.is_empty() {
 404                        current_path.push('/');
 405                    }
 406                    current_path.push_str(component);
 407                    let dir_path = RepoPath::new(&current_path)
 408                        .expect("repo path from status entry component");
 409
 410                    let component = SharedString::from(component.to_string());
 411
 412                    current = current
 413                        .children
 414                        .entry(component.clone())
 415                        .or_insert_with(|| TreeNode {
 416                            name: component,
 417                            path: Some(dir_path),
 418                            ..Default::default()
 419                        });
 420                }
 421            }
 422        }
 423
 424        let (flattened, _) = self.flatten_tree(&root, section, 0, seen_directories);
 425        flattened
 426    }
 427
 428    fn flatten_tree(
 429        &mut self,
 430        node: &TreeNode,
 431        section: Section,
 432        depth: usize,
 433        seen_directories: &mut HashSet<TreeKey>,
 434    ) -> (Vec<(GitListEntry, bool)>, Vec<GitStatusEntry>) {
 435        let mut all_statuses = Vec::new();
 436        let mut flattened = Vec::new();
 437
 438        for child in node.children.values() {
 439            let (terminal, name) = Self::compact_directory_chain(child);
 440            let Some(path) = terminal.path.clone().or_else(|| child.path.clone()) else {
 441                continue;
 442            };
 443            let (child_flattened, mut child_statuses) =
 444                self.flatten_tree(terminal, section, depth + 1, seen_directories);
 445            let key = TreeKey { section, path };
 446            let expanded = *self.expanded_dirs.get(&key).unwrap_or(&true);
 447            self.expanded_dirs.entry(key.clone()).or_insert(true);
 448            seen_directories.insert(key.clone());
 449
 450            self.directory_descendants
 451                .insert(key.clone(), child_statuses.clone());
 452
 453            flattened.push((
 454                GitListEntry::Directory(GitTreeDirEntry {
 455                    key,
 456                    name,
 457                    depth,
 458                    expanded,
 459                }),
 460                true,
 461            ));
 462
 463            if expanded {
 464                flattened.extend(child_flattened);
 465            } else {
 466                flattened.extend(child_flattened.into_iter().map(|(child, _)| (child, false)));
 467            }
 468
 469            all_statuses.append(&mut child_statuses);
 470        }
 471
 472        for file in &node.files {
 473            all_statuses.push(file.clone());
 474            flattened.push((
 475                GitListEntry::TreeStatus(GitTreeStatusEntry {
 476                    entry: file.clone(),
 477                    depth,
 478                }),
 479                true,
 480            ));
 481        }
 482
 483        (flattened, all_statuses)
 484    }
 485
 486    fn compact_directory_chain(mut node: &TreeNode) -> (&TreeNode, SharedString) {
 487        let mut parts = vec![node.name.clone()];
 488        while node.files.is_empty() && node.children.len() == 1 {
 489            let Some(child) = node.children.values().next() else {
 490                continue;
 491            };
 492            if child.path.is_none() {
 493                break;
 494            }
 495            parts.push(child.name.clone());
 496            node = child;
 497        }
 498        let name = parts.join("/");
 499        (node, SharedString::from(name))
 500    }
 501}
 502
 503#[derive(Debug, PartialEq, Eq, Clone)]
 504struct GitTreeStatusEntry {
 505    entry: GitStatusEntry,
 506    depth: usize,
 507}
 508
 509#[derive(Debug, PartialEq, Eq, Clone, Hash)]
 510struct TreeKey {
 511    section: Section,
 512    path: RepoPath,
 513}
 514
 515#[derive(Debug, PartialEq, Eq, Clone)]
 516struct GitTreeDirEntry {
 517    key: TreeKey,
 518    name: SharedString,
 519    depth: usize,
 520    // staged_state: ToggleState,
 521    expanded: bool,
 522}
 523
 524#[derive(Default)]
 525struct TreeNode {
 526    name: SharedString,
 527    path: Option<RepoPath>,
 528    children: BTreeMap<SharedString, TreeNode>,
 529    files: Vec<GitStatusEntry>,
 530}
 531
 532#[derive(Debug, PartialEq, Eq, Clone)]
 533pub struct GitStatusEntry {
 534    pub(crate) repo_path: RepoPath,
 535    pub(crate) status: FileStatus,
 536    pub(crate) staging: StageStatus,
 537    pub(crate) diff_stat: Option<DiffStat>,
 538}
 539
 540impl GitStatusEntry {
 541    fn display_name(&self, path_style: PathStyle) -> String {
 542        self.repo_path
 543            .file_name()
 544            .map(|name| name.to_owned())
 545            .unwrap_or_else(|| self.repo_path.display(path_style).to_string())
 546    }
 547
 548    fn parent_dir(&self, path_style: PathStyle) -> Option<String> {
 549        self.repo_path
 550            .parent()
 551            .map(|parent| parent.display(path_style).to_string())
 552    }
 553}
 554
 555struct TruncatedPatch {
 556    header: String,
 557    hunks: Vec<String>,
 558    hunks_to_keep: usize,
 559}
 560
 561impl TruncatedPatch {
 562    fn from_unified_diff(patch_str: &str) -> Option<Self> {
 563        let lines: Vec<&str> = patch_str.lines().collect();
 564        if lines.len() < 2 {
 565            return None;
 566        }
 567        let header = format!("{}\n{}\n", lines[0], lines[1]);
 568        let mut hunks = Vec::new();
 569        let mut current_hunk = String::new();
 570        for line in &lines[2..] {
 571            if line.starts_with("@@") {
 572                if !current_hunk.is_empty() {
 573                    hunks.push(current_hunk);
 574                }
 575                current_hunk = format!("{}\n", line);
 576            } else if !current_hunk.is_empty() {
 577                current_hunk.push_str(line);
 578                current_hunk.push('\n');
 579            }
 580        }
 581        if !current_hunk.is_empty() {
 582            hunks.push(current_hunk);
 583        }
 584        if hunks.is_empty() {
 585            return None;
 586        }
 587        let hunks_to_keep = hunks.len();
 588        Some(TruncatedPatch {
 589            header,
 590            hunks,
 591            hunks_to_keep,
 592        })
 593    }
 594    fn calculate_size(&self) -> usize {
 595        let mut size = self.header.len();
 596        for (i, hunk) in self.hunks.iter().enumerate() {
 597            if i < self.hunks_to_keep {
 598                size += hunk.len();
 599            }
 600        }
 601        size
 602    }
 603    fn to_string(&self) -> String {
 604        let mut out = self.header.clone();
 605        for (i, hunk) in self.hunks.iter().enumerate() {
 606            if i < self.hunks_to_keep {
 607                out.push_str(hunk);
 608            }
 609        }
 610        let skipped_hunks = self.hunks.len() - self.hunks_to_keep;
 611        if skipped_hunks > 0 {
 612            out.push_str(&format!("[...skipped {} hunks...]\n", skipped_hunks));
 613        }
 614        out
 615    }
 616}
 617
 618pub struct GitPanel {
 619    pub(crate) active_repository: Option<Entity<Repository>>,
 620    pub(crate) commit_editor: Entity<Editor>,
 621    conflicted_count: usize,
 622    conflicted_staged_count: usize,
 623    add_coauthors: bool,
 624    generate_commit_message_task: Option<Task<Option<()>>>,
 625    entries: Vec<GitListEntry>,
 626    view_mode: GitPanelViewMode,
 627    entries_indices: HashMap<RepoPath, usize>,
 628    single_staged_entry: Option<GitStatusEntry>,
 629    single_tracked_entry: Option<GitStatusEntry>,
 630    focus_handle: FocusHandle,
 631    fs: Arc<dyn Fs>,
 632    new_count: usize,
 633    entry_count: usize,
 634    changes_count: usize,
 635    new_staged_count: usize,
 636    pending_commit: Option<Task<()>>,
 637    amend_pending: bool,
 638    original_commit_message: Option<String>,
 639    signoff_enabled: bool,
 640    pending_serialization: Task<()>,
 641    pub(crate) project: Entity<Project>,
 642    scroll_handle: UniformListScrollHandle,
 643    max_width_item_index: Option<usize>,
 644    selected_entry: Option<usize>,
 645    marked_entries: Vec<usize>,
 646    tracked_count: usize,
 647    tracked_staged_count: usize,
 648    update_visible_entries_task: Task<()>,
 649    pub(crate) workspace: WeakEntity<Workspace>,
 650    context_menu: Option<(Entity<ContextMenu>, Point<Pixels>, Subscription)>,
 651    modal_open: bool,
 652    show_placeholders: bool,
 653    local_committer: Option<GitCommitter>,
 654    local_committer_task: Option<Task<()>>,
 655    commit_template: Option<GitCommitTemplate>,
 656    bulk_staging: Option<BulkStaging>,
 657    stash_entries: GitStash,
 658
 659    _settings_subscription: Subscription,
 660}
 661
 662#[derive(Clone, Debug, PartialEq, Eq)]
 663struct BulkStaging {
 664    repo_id: RepositoryId,
 665    anchor: RepoPath,
 666}
 667
 668const MAX_PANEL_EDITOR_LINES: usize = 6;
 669
 670pub(crate) fn commit_message_editor(
 671    commit_message_buffer: Entity<Buffer>,
 672    placeholder: Option<SharedString>,
 673    project: Entity<Project>,
 674    in_panel: bool,
 675    window: &mut Window,
 676    cx: &mut Context<Editor>,
 677) -> Editor {
 678    let buffer = cx.new(|cx| MultiBuffer::singleton(commit_message_buffer, cx));
 679    let max_lines = if in_panel { MAX_PANEL_EDITOR_LINES } else { 18 };
 680    let mut commit_editor = Editor::new(
 681        EditorMode::AutoHeight {
 682            min_lines: max_lines,
 683            max_lines: Some(max_lines),
 684        },
 685        buffer,
 686        None,
 687        window,
 688        cx,
 689    );
 690    commit_editor.set_collaboration_hub(Box::new(project));
 691    commit_editor.set_use_autoclose(false);
 692    commit_editor.set_show_gutter(false, cx);
 693    commit_editor.set_use_modal_editing(true);
 694    commit_editor.set_show_wrap_guides(false, cx);
 695    commit_editor.set_show_indent_guides(false, cx);
 696    let placeholder = placeholder.unwrap_or("Enter commit message".into());
 697    commit_editor.set_placeholder_text(&placeholder, window, cx);
 698    commit_editor
 699}
 700
 701impl GitPanel {
 702    fn new(
 703        workspace: &mut Workspace,
 704        window: &mut Window,
 705        cx: &mut Context<Workspace>,
 706    ) -> Entity<Self> {
 707        let project = workspace.project().clone();
 708        let app_state = workspace.app_state().clone();
 709        let fs = app_state.fs.clone();
 710        let git_store = project.read(cx).git_store().clone();
 711        let active_repository = project.read(cx).active_repository(cx);
 712
 713        cx.new(|cx| {
 714            let focus_handle = cx.focus_handle();
 715            cx.on_focus(&focus_handle, window, Self::focus_in).detach();
 716
 717            let mut was_sort_by_path = GitPanelSettings::get_global(cx).sort_by_path;
 718            let mut was_tree_view = GitPanelSettings::get_global(cx).tree_view;
 719            let mut was_file_icons = GitPanelSettings::get_global(cx).file_icons;
 720            let mut was_folder_icons = GitPanelSettings::get_global(cx).folder_icons;
 721            let mut was_diff_stats = GitPanelSettings::get_global(cx).diff_stats;
 722            cx.observe_global_in::<SettingsStore>(window, move |this, window, cx| {
 723                let settings = GitPanelSettings::get_global(cx);
 724                let sort_by_path = settings.sort_by_path;
 725                let tree_view = settings.tree_view;
 726                let file_icons = settings.file_icons;
 727                let folder_icons = settings.folder_icons;
 728                let diff_stats = settings.diff_stats;
 729                if tree_view != was_tree_view {
 730                    this.view_mode = GitPanelViewMode::from_settings(cx);
 731                }
 732
 733                let mut update_entries = false;
 734                if sort_by_path != was_sort_by_path || tree_view != was_tree_view {
 735                    this.bulk_staging.take();
 736                    update_entries = true;
 737                }
 738                if (diff_stats != was_diff_stats) || update_entries {
 739                    this.update_visible_entries(window, cx);
 740                }
 741                if file_icons != was_file_icons || folder_icons != was_folder_icons {
 742                    cx.notify();
 743                }
 744                was_sort_by_path = sort_by_path;
 745                was_tree_view = tree_view;
 746                was_file_icons = file_icons;
 747                was_folder_icons = folder_icons;
 748                was_diff_stats = diff_stats;
 749            })
 750            .detach();
 751
 752            cx.observe_global::<FileIcons>(|_, cx| {
 753                cx.notify();
 754            })
 755            .detach();
 756
 757            // just to let us render a placeholder editor.
 758            // Once the active git repo is set, this buffer will be replaced.
 759            let temporary_buffer = cx.new(|cx| Buffer::local("", cx));
 760            let commit_editor = cx.new(|cx| {
 761                commit_message_editor(temporary_buffer, None, project.clone(), true, window, cx)
 762            });
 763
 764            commit_editor.update(cx, |editor, cx| {
 765                editor.clear(window, cx);
 766            });
 767
 768            let scroll_handle = UniformListScrollHandle::new();
 769
 770            let mut was_ai_enabled = AgentSettings::get_global(cx).enabled(cx);
 771            let _settings_subscription = cx.observe_global::<SettingsStore>(move |_, cx| {
 772                let is_ai_enabled = AgentSettings::get_global(cx).enabled(cx);
 773                if was_ai_enabled != is_ai_enabled {
 774                    was_ai_enabled = is_ai_enabled;
 775                    cx.notify();
 776                }
 777            });
 778
 779            cx.subscribe_in(
 780                &git_store,
 781                window,
 782                move |this, _git_store, event, window, cx| match event {
 783                    GitStoreEvent::RepositoryUpdated(
 784                        _,
 785                        RepositoryEvent::StatusesChanged | RepositoryEvent::HeadChanged,
 786                        true,
 787                    )
 788                    | GitStoreEvent::RepositoryAdded
 789                    | GitStoreEvent::RepositoryRemoved(_)
 790                    | GitStoreEvent::ActiveRepositoryChanged(_) => {
 791                        this.schedule_update(window, cx);
 792                    }
 793                    GitStoreEvent::IndexWriteError(error) => {
 794                        this.workspace
 795                            .update(cx, |workspace, cx| {
 796                                workspace.show_error(error, cx);
 797                            })
 798                            .ok();
 799                    }
 800                    GitStoreEvent::RepositoryUpdated(_, _, _) => {}
 801                    GitStoreEvent::JobsUpdated | GitStoreEvent::ConflictsUpdated => {}
 802                },
 803            )
 804            .detach();
 805
 806            let mut this = Self {
 807                active_repository,
 808                commit_editor,
 809                conflicted_count: 0,
 810                conflicted_staged_count: 0,
 811                add_coauthors: true,
 812                generate_commit_message_task: None,
 813                entries: Vec::new(),
 814                view_mode: GitPanelViewMode::from_settings(cx),
 815                entries_indices: HashMap::default(),
 816                focus_handle: cx.focus_handle(),
 817                fs,
 818                new_count: 0,
 819                new_staged_count: 0,
 820                changes_count: 0,
 821                pending_commit: None,
 822                amend_pending: false,
 823                original_commit_message: None,
 824                signoff_enabled: false,
 825                pending_serialization: Task::ready(()),
 826                single_staged_entry: None,
 827                single_tracked_entry: None,
 828                project,
 829                scroll_handle,
 830                max_width_item_index: None,
 831                selected_entry: None,
 832                marked_entries: Vec::new(),
 833                tracked_count: 0,
 834                tracked_staged_count: 0,
 835                update_visible_entries_task: Task::ready(()),
 836                show_placeholders: false,
 837                local_committer: None,
 838                local_committer_task: None,
 839                commit_template: None,
 840                context_menu: None,
 841                workspace: workspace.weak_handle(),
 842                modal_open: false,
 843                entry_count: 0,
 844                bulk_staging: None,
 845                stash_entries: Default::default(),
 846                _settings_subscription,
 847            };
 848
 849            this.schedule_update(window, cx);
 850            this
 851        })
 852    }
 853
 854    pub fn entry_by_path(&self, path: &RepoPath) -> Option<usize> {
 855        self.entries_indices.get(path).copied()
 856    }
 857
 858    pub fn select_entry_by_path(
 859        &mut self,
 860        path: ProjectPath,
 861        window: &mut Window,
 862        cx: &mut Context<Self>,
 863    ) {
 864        let Some(git_repo) = self.active_repository.as_ref() else {
 865            return;
 866        };
 867
 868        let (repo_path, section) = {
 869            let repo = git_repo.read(cx);
 870            let Some(repo_path) = repo.project_path_to_repo_path(&path, cx) else {
 871                return;
 872            };
 873
 874            let section = repo
 875                .status_for_path(&repo_path)
 876                .map(|status| status.status)
 877                .map(|status| {
 878                    if repo.had_conflict_on_last_merge_head_change(&repo_path) {
 879                        Section::Conflict
 880                    } else if status.is_created() {
 881                        Section::New
 882                    } else {
 883                        Section::Tracked
 884                    }
 885                });
 886
 887            (repo_path, section)
 888        };
 889
 890        let mut needs_rebuild = false;
 891        if let (Some(section), Some(tree_state)) = (section, self.view_mode.tree_state_mut()) {
 892            let mut current_dir = repo_path.parent();
 893            while let Some(dir) = current_dir {
 894                let key = TreeKey {
 895                    section,
 896                    path: RepoPath::from_rel_path(dir),
 897                };
 898
 899                if tree_state.expanded_dirs.get(&key) == Some(&false) {
 900                    tree_state.expanded_dirs.insert(key, true);
 901                    needs_rebuild = true;
 902                }
 903
 904                current_dir = dir.parent();
 905            }
 906        }
 907
 908        if needs_rebuild {
 909            self.update_visible_entries(window, cx);
 910        }
 911
 912        let Some(ix) = self.entry_by_path(&repo_path) else {
 913            return;
 914        };
 915
 916        self.selected_entry = Some(ix);
 917        self.scroll_to_selected_entry(cx);
 918    }
 919
 920    fn serialization_key(workspace: &Workspace) -> Option<String> {
 921        workspace
 922            .database_id()
 923            .map(|id| i64::from(id).to_string())
 924            .or(workspace.session_id())
 925            .map(|id| format!("{}-{:?}", GIT_PANEL_KEY, id))
 926    }
 927
 928    fn serialize(&mut self, cx: &mut Context<Self>) {
 929        let amend_pending = self.amend_pending;
 930        let signoff_enabled = self.signoff_enabled;
 931        let kvp = KeyValueStore::global(cx);
 932
 933        self.pending_serialization = cx.spawn(async move |git_panel, cx| {
 934            cx.background_executor()
 935                .timer(SERIALIZATION_THROTTLE_TIME)
 936                .await;
 937            let Some(serialization_key) = git_panel
 938                .update(cx, |git_panel, cx| {
 939                    git_panel
 940                        .workspace
 941                        .read_with(cx, |workspace, _| Self::serialization_key(workspace))
 942                        .ok()
 943                        .flatten()
 944                })
 945                .ok()
 946                .flatten()
 947            else {
 948                return;
 949            };
 950            cx.background_spawn(
 951                async move {
 952                    kvp.write_kvp(
 953                        serialization_key,
 954                        serde_json::to_string(&SerializedGitPanel {
 955                            amend_pending,
 956                            signoff_enabled,
 957                        })?,
 958                    )
 959                    .await?;
 960                    anyhow::Ok(())
 961                }
 962                .log_err(),
 963            )
 964            .await;
 965        });
 966    }
 967
 968    pub(crate) fn set_modal_open(&mut self, open: bool, cx: &mut Context<Self>) {
 969        self.modal_open = open;
 970        cx.notify();
 971    }
 972
 973    fn dispatch_context(&self, window: &mut Window, cx: &Context<Self>) -> KeyContext {
 974        let mut dispatch_context = KeyContext::new_with_defaults();
 975        dispatch_context.add("GitPanel");
 976
 977        if self.commit_editor.read(cx).is_focused(window) {
 978            dispatch_context.add("CommitEditor");
 979        } else if self.focus_handle.contains_focused(window, cx) {
 980            dispatch_context.add("menu");
 981            dispatch_context.add("ChangesList");
 982        }
 983
 984        dispatch_context
 985    }
 986
 987    fn close_panel(&mut self, _: &Close, _window: &mut Window, cx: &mut Context<Self>) {
 988        cx.emit(PanelEvent::Close);
 989    }
 990
 991    fn focus_in(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 992        if !self.focus_handle.contains_focused(window, cx) {
 993            cx.emit(Event::Focus);
 994        }
 995    }
 996
 997    fn scroll_to_selected_entry(&mut self, cx: &mut Context<Self>) {
 998        let Some(selected_entry) = self.selected_entry else {
 999            cx.notify();
1000            return;
1001        };
1002
1003        let visible_index = match &self.view_mode {
1004            GitPanelViewMode::Flat => Some(selected_entry),
1005            GitPanelViewMode::Tree(state) => state
1006                .logical_indices
1007                .iter()
1008                .position(|&ix| ix == selected_entry),
1009        };
1010
1011        if let Some(visible_index) = visible_index {
1012            self.scroll_handle
1013                .scroll_to_item(visible_index, ScrollStrategy::Center);
1014        }
1015
1016        cx.notify();
1017    }
1018
1019    fn expand_selected_entry(
1020        &mut self,
1021        _: &ExpandSelectedEntry,
1022        window: &mut Window,
1023        cx: &mut Context<Self>,
1024    ) {
1025        let Some(entry) = self.get_selected_entry().cloned() else {
1026            return;
1027        };
1028
1029        if let GitListEntry::Directory(dir_entry) = entry {
1030            if dir_entry.expanded {
1031                self.select_next(&menu::SelectNext, window, cx);
1032            } else {
1033                self.toggle_directory(&dir_entry.key, window, cx);
1034            }
1035        } else {
1036            self.select_next(&menu::SelectNext, window, cx);
1037        }
1038    }
1039
1040    fn collapse_selected_entry(
1041        &mut self,
1042        _: &CollapseSelectedEntry,
1043        window: &mut Window,
1044        cx: &mut Context<Self>,
1045    ) {
1046        let Some(entry) = self.get_selected_entry().cloned() else {
1047            return;
1048        };
1049
1050        if let GitListEntry::Directory(dir_entry) = entry {
1051            if dir_entry.expanded {
1052                self.toggle_directory(&dir_entry.key, window, cx);
1053            } else {
1054                self.select_previous(&menu::SelectPrevious, window, cx);
1055            }
1056        } else {
1057            self.select_previous(&menu::SelectPrevious, window, cx);
1058        }
1059    }
1060
1061    fn select_first(
1062        &mut self,
1063        _: &menu::SelectFirst,
1064        _window: &mut Window,
1065        cx: &mut Context<Self>,
1066    ) {
1067        let first_entry = match &self.view_mode {
1068            GitPanelViewMode::Flat => self
1069                .entries
1070                .iter()
1071                .position(|entry| entry.status_entry().is_some()),
1072            GitPanelViewMode::Tree(state) => {
1073                let index = self.entries.iter().position(|entry| {
1074                    entry.status_entry().is_some() || entry.directory_entry().is_some()
1075                });
1076
1077                index.map(|index| state.logical_indices[index])
1078            }
1079        };
1080
1081        if let Some(first_entry) = first_entry {
1082            self.selected_entry = Some(first_entry);
1083            self.scroll_to_selected_entry(cx);
1084        }
1085    }
1086
1087    fn select_previous(
1088        &mut self,
1089        _: &menu::SelectPrevious,
1090        _window: &mut Window,
1091        cx: &mut Context<Self>,
1092    ) {
1093        let item_count = self.entries.len();
1094        if item_count == 0 {
1095            return;
1096        }
1097
1098        let Some(selected_entry) = self.selected_entry else {
1099            return;
1100        };
1101
1102        let new_index = match &self.view_mode {
1103            GitPanelViewMode::Flat => selected_entry.saturating_sub(1),
1104            GitPanelViewMode::Tree(state) => {
1105                let Some(current_logical_index) = state
1106                    .logical_indices
1107                    .iter()
1108                    .position(|&i| i == selected_entry)
1109                else {
1110                    return;
1111                };
1112
1113                state.logical_indices[current_logical_index.saturating_sub(1)]
1114            }
1115        };
1116
1117        if selected_entry == 0 && new_index == 0 {
1118            return;
1119        }
1120
1121        if matches!(
1122            self.entries.get(new_index.saturating_sub(1)),
1123            Some(GitListEntry::Header(..))
1124        ) && new_index == 0
1125        {
1126            return;
1127        }
1128
1129        if matches!(self.entries.get(new_index), Some(GitListEntry::Header(..))) {
1130            self.selected_entry = match &self.view_mode {
1131                GitPanelViewMode::Flat => Some(new_index.saturating_sub(1)),
1132                GitPanelViewMode::Tree(tree_view_state) => {
1133                    maybe!({
1134                        let current_logical_index = tree_view_state
1135                            .logical_indices
1136                            .iter()
1137                            .position(|&i| i == new_index)?;
1138
1139                        tree_view_state
1140                            .logical_indices
1141                            .get(current_logical_index.saturating_sub(1))
1142                            .copied()
1143                    })
1144                }
1145            };
1146        } else {
1147            self.selected_entry = Some(new_index);
1148        }
1149
1150        self.scroll_to_selected_entry(cx);
1151    }
1152
1153    fn select_next(&mut self, _: &menu::SelectNext, _window: &mut Window, cx: &mut Context<Self>) {
1154        let item_count = self.entries.len();
1155        if item_count == 0 {
1156            return;
1157        }
1158
1159        let Some(selected_entry) = self.selected_entry else {
1160            return;
1161        };
1162
1163        let new_index = match &self.view_mode {
1164            GitPanelViewMode::Flat => {
1165                if selected_entry >= item_count.saturating_sub(1) {
1166                    return;
1167                }
1168
1169                selected_entry.saturating_add(1)
1170            }
1171            GitPanelViewMode::Tree(state) => {
1172                let Some(current_logical_index) = state
1173                    .logical_indices
1174                    .iter()
1175                    .position(|&i| i == selected_entry)
1176                else {
1177                    return;
1178                };
1179
1180                let Some(new_index) = state
1181                    .logical_indices
1182                    .get(current_logical_index.saturating_add(1))
1183                    .copied()
1184                else {
1185                    return;
1186                };
1187
1188                new_index
1189            }
1190        };
1191
1192        if matches!(self.entries.get(new_index), Some(GitListEntry::Header(..))) {
1193            self.selected_entry = Some(new_index.saturating_add(1));
1194        } else {
1195            self.selected_entry = Some(new_index);
1196        }
1197
1198        self.scroll_to_selected_entry(cx);
1199    }
1200
1201    fn select_last(&mut self, _: &menu::SelectLast, _window: &mut Window, cx: &mut Context<Self>) {
1202        if self.entries.last().is_some() {
1203            self.selected_entry = Some(self.entries.len() - 1);
1204            self.scroll_to_selected_entry(cx);
1205        }
1206    }
1207
1208    /// Show diff view at selected entry, only if the diff view is open
1209    fn move_diff_to_entry(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1210        maybe!({
1211            let workspace = self.workspace.upgrade()?;
1212
1213            if let Some(project_diff) = workspace.read(cx).item_of_type::<ProjectDiff>(cx) {
1214                let entry = self.entries.get(self.selected_entry?)?.status_entry()?;
1215
1216                project_diff.update(cx, |project_diff, cx| {
1217                    project_diff.move_to_entry(entry.clone(), window, cx);
1218                });
1219            }
1220
1221            Some(())
1222        });
1223    }
1224
1225    fn first_entry(&mut self, _: &FirstEntry, window: &mut Window, cx: &mut Context<Self>) {
1226        self.select_first(&menu::SelectFirst, window, cx);
1227        self.move_diff_to_entry(window, cx);
1228    }
1229
1230    fn last_entry(&mut self, _: &LastEntry, window: &mut Window, cx: &mut Context<Self>) {
1231        self.select_last(&menu::SelectLast, window, cx);
1232        self.move_diff_to_entry(window, cx);
1233    }
1234
1235    fn next_entry(&mut self, _: &NextEntry, window: &mut Window, cx: &mut Context<Self>) {
1236        self.select_next(&menu::SelectNext, window, cx);
1237        self.move_diff_to_entry(window, cx);
1238    }
1239
1240    fn previous_entry(&mut self, _: &PreviousEntry, window: &mut Window, cx: &mut Context<Self>) {
1241        self.select_previous(&menu::SelectPrevious, window, cx);
1242        self.move_diff_to_entry(window, cx);
1243    }
1244
1245    fn focus_editor(&mut self, _: &FocusEditor, window: &mut Window, cx: &mut Context<Self>) {
1246        self.commit_editor.update(cx, |editor, cx| {
1247            window.focus(&editor.focus_handle(cx), cx);
1248        });
1249        cx.notify();
1250    }
1251
1252    fn select_first_entry_if_none(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1253        let have_entries = self
1254            .active_repository
1255            .as_ref()
1256            .is_some_and(|active_repository| active_repository.read(cx).status_summary().count > 0);
1257        if have_entries && self.selected_entry.is_none() {
1258            self.select_first(&menu::SelectFirst, window, cx);
1259        }
1260    }
1261
1262    fn focus_changes_list(
1263        &mut self,
1264        _: &FocusChanges,
1265        window: &mut Window,
1266        cx: &mut Context<Self>,
1267    ) {
1268        self.focus_handle.focus(window, cx);
1269        self.select_first_entry_if_none(window, cx);
1270    }
1271
1272    fn get_selected_entry(&self) -> Option<&GitListEntry> {
1273        self.selected_entry.and_then(|i| self.entries.get(i))
1274    }
1275
1276    fn open_diff(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
1277        if let Some(GitListEntry::Directory(dir_entry)) = self
1278            .selected_entry
1279            .and_then(|i| self.entries.get(i))
1280            .cloned()
1281        {
1282            self.toggle_directory(&dir_entry.key, window, cx);
1283            return;
1284        }
1285        maybe!({
1286            let entry = self.entries.get(self.selected_entry?)?.status_entry()?;
1287            let workspace = self.workspace.upgrade()?;
1288            let git_repo = self.active_repository.as_ref()?;
1289
1290            if let Some(project_diff) = workspace.read(cx).active_item_as::<ProjectDiff>(cx)
1291                && let Some(project_path) = project_diff.read(cx).active_path(cx)
1292                && Some(&entry.repo_path)
1293                    == git_repo
1294                        .read(cx)
1295                        .project_path_to_repo_path(&project_path, cx)
1296                        .as_ref()
1297            {
1298                project_diff.focus_handle(cx).focus(window, cx);
1299                project_diff.update(cx, |project_diff, cx| project_diff.autoscroll(cx));
1300                return None;
1301            };
1302
1303            self.workspace
1304                .update(cx, |workspace, cx| {
1305                    ProjectDiff::deploy_at(workspace, Some(entry.clone()), window, cx);
1306                })
1307                .ok();
1308            self.focus_handle.focus(window, cx);
1309
1310            Some(())
1311        });
1312    }
1313
1314    fn file_history(&mut self, _: &git::FileHistory, window: &mut Window, cx: &mut Context<Self>) {
1315        maybe!({
1316            let entry = self.entries.get(self.selected_entry?)?.status_entry()?;
1317            let active_repo = self.active_repository.as_ref()?;
1318            let repo_path = entry.repo_path.clone();
1319            let git_store = self.project.read(cx).git_store();
1320
1321            FileHistoryView::open(
1322                repo_path,
1323                git_store.downgrade(),
1324                active_repo.downgrade(),
1325                self.workspace.clone(),
1326                window,
1327                cx,
1328            );
1329
1330            Some(())
1331        });
1332    }
1333
1334    fn open_file(
1335        &mut self,
1336        _: &menu::SecondaryConfirm,
1337        window: &mut Window,
1338        cx: &mut Context<Self>,
1339    ) {
1340        maybe!({
1341            let entry = self.entries.get(self.selected_entry?)?.status_entry()?;
1342            let active_repo = self.active_repository.as_ref()?;
1343            let path = active_repo
1344                .read(cx)
1345                .repo_path_to_project_path(&entry.repo_path, cx)?;
1346            if entry.status.is_deleted() {
1347                return None;
1348            }
1349
1350            let open_task = self
1351                .workspace
1352                .update(cx, |workspace, cx| {
1353                    workspace.open_path_preview(path, None, false, false, true, window, cx)
1354                })
1355                .ok()?;
1356
1357            let workspace = self.workspace.clone();
1358            cx.spawn_in(window, async move |_, mut cx| {
1359                let item = open_task
1360                    .await
1361                    .notify_workspace_async_err(workspace, &mut cx)
1362                    .ok_or_else(|| anyhow::anyhow!("Failed to open file"))?;
1363                if let Some(active_editor) = item.downcast::<Editor>() {
1364                    if let Some(diff_task) =
1365                        active_editor.update(cx, |editor, _cx| editor.wait_for_diff_to_load())
1366                    {
1367                        diff_task.await;
1368                    }
1369
1370                    cx.update(|window, cx| {
1371                        active_editor.update(cx, |editor, cx| {
1372                            editor.expand_all_diff_hunks(&ExpandAllDiffHunks, window, cx);
1373
1374                            let snapshot = editor.snapshot(window, cx);
1375                            editor.go_to_hunk_before_or_after_position(
1376                                &snapshot,
1377                                language::Point::new(0, 0),
1378                                Direction::Next,
1379                                true,
1380                                window,
1381                                cx,
1382                            );
1383                        })
1384                    })
1385                    .log_err();
1386                }
1387
1388                anyhow::Ok(())
1389            })
1390            .detach();
1391
1392            Some(())
1393        });
1394    }
1395
1396    fn revert_selected(
1397        &mut self,
1398        action: &git::RestoreFile,
1399        window: &mut Window,
1400        cx: &mut Context<Self>,
1401    ) {
1402        let path_style = self.project.read(cx).path_style(cx);
1403        maybe!({
1404            let list_entry = self.entries.get(self.selected_entry?)?.clone();
1405            let entry = list_entry.status_entry()?.to_owned();
1406            let skip_prompt = action.skip_prompt || entry.status.is_created();
1407
1408            let prompt = if skip_prompt {
1409                Task::ready(Ok(0))
1410            } else {
1411                let prompt = window.prompt(
1412                    PromptLevel::Warning,
1413                    &format!(
1414                        "Are you sure you want to discard changes to {}?",
1415                        entry
1416                            .repo_path
1417                            .file_name()
1418                            .unwrap_or(entry.repo_path.display(path_style).as_ref()),
1419                    ),
1420                    None,
1421                    &["Discard Changes", "Cancel"],
1422                    cx,
1423                );
1424                cx.background_spawn(prompt)
1425            };
1426
1427            let this = cx.weak_entity();
1428            window
1429                .spawn(cx, async move |cx| {
1430                    if prompt.await? != 0 {
1431                        return anyhow::Ok(());
1432                    }
1433
1434                    this.update_in(cx, |this, window, cx| {
1435                        this.revert_entry(&entry, window, cx);
1436                    })?;
1437
1438                    Ok(())
1439                })
1440                .detach();
1441            Some(())
1442        });
1443    }
1444
1445    fn add_to_gitignore(
1446        &mut self,
1447        _: &git::AddToGitignore,
1448        _window: &mut Window,
1449        cx: &mut Context<Self>,
1450    ) {
1451        maybe!({
1452            let list_entry = self.entries.get(self.selected_entry?)?.clone();
1453            let entry = list_entry.status_entry()?.to_owned();
1454
1455            if !entry.status.is_created() {
1456                return Some(());
1457            }
1458
1459            let project = self.project.downgrade();
1460            let repo_path = entry.repo_path;
1461            let active_repository = self.active_repository.as_ref()?.downgrade();
1462
1463            cx.spawn(async move |_, cx| {
1464                let file_path_str = repo_path.as_ref().display(PathStyle::Posix);
1465
1466                let repo_root = active_repository.read_with(cx, |repository, _| {
1467                    repository.snapshot().work_directory_abs_path
1468                })?;
1469
1470                let gitignore_abs_path = repo_root.join(".gitignore");
1471
1472                let buffer: Entity<Buffer> = project
1473                    .update(cx, |project, cx| {
1474                        project.open_local_buffer(gitignore_abs_path, cx)
1475                    })?
1476                    .await?;
1477
1478                let mut should_save = false;
1479                buffer.update(cx, |buffer, cx| {
1480                    let existing_content = buffer.text();
1481
1482                    if existing_content
1483                        .lines()
1484                        .any(|line: &str| line.trim() == file_path_str)
1485                    {
1486                        return;
1487                    }
1488
1489                    let insert_position = existing_content.len();
1490                    let new_entry = if existing_content.is_empty() {
1491                        format!("{}\n", file_path_str)
1492                    } else if existing_content.ends_with('\n') {
1493                        format!("{}\n", file_path_str)
1494                    } else {
1495                        format!("\n{}\n", file_path_str)
1496                    };
1497
1498                    buffer.edit([(insert_position..insert_position, new_entry)], None, cx);
1499                    should_save = true;
1500                });
1501
1502                if should_save {
1503                    project
1504                        .update(cx, |project, cx| project.save_buffer(buffer, cx))?
1505                        .await?;
1506                }
1507
1508                anyhow::Ok(())
1509            })
1510            .detach_and_log_err(cx);
1511
1512            Some(())
1513        });
1514    }
1515
1516    fn revert_entry(
1517        &mut self,
1518        entry: &GitStatusEntry,
1519        window: &mut Window,
1520        cx: &mut Context<Self>,
1521    ) {
1522        maybe!({
1523            let active_repo = self.active_repository.clone()?;
1524            let path = active_repo
1525                .read(cx)
1526                .repo_path_to_project_path(&entry.repo_path, cx)?;
1527            let workspace = self.workspace.clone();
1528
1529            if entry.status.staging().has_staged() {
1530                self.change_file_stage(false, vec![entry.clone()], cx);
1531            }
1532            let filename = path.path.file_name()?.to_string();
1533
1534            if !entry.status.is_created() {
1535                self.perform_checkout(vec![entry.clone()], window, cx);
1536            } else {
1537                let prompt = prompt(&format!("Trash {}?", filename), None, window, cx);
1538                cx.spawn_in(window, async move |_, cx| {
1539                    match prompt.await? {
1540                        TrashCancel::Trash => {}
1541                        TrashCancel::Cancel => return Ok(()),
1542                    }
1543                    let task = workspace.update(cx, |workspace, cx| {
1544                        workspace
1545                            .project()
1546                            .update(cx, |project, cx| project.delete_file(path, true, cx))
1547                    })?;
1548                    if let Some(task) = task {
1549                        task.await?;
1550                    }
1551                    Ok(())
1552                })
1553                .detach_and_prompt_err(
1554                    "Failed to trash file",
1555                    window,
1556                    cx,
1557                    |e, _, _| Some(format!("{e}")),
1558                );
1559            }
1560            Some(())
1561        });
1562    }
1563
1564    fn perform_checkout(
1565        &mut self,
1566        entries: Vec<GitStatusEntry>,
1567        window: &mut Window,
1568        cx: &mut Context<Self>,
1569    ) {
1570        let workspace = self.workspace.clone();
1571        let Some(active_repository) = self.active_repository.clone() else {
1572            return;
1573        };
1574
1575        let task = cx.spawn_in(window, async move |this, cx| {
1576            let tasks: Vec<_> = workspace.update(cx, |workspace, cx| {
1577                workspace.project().update(cx, |project, cx| {
1578                    entries
1579                        .iter()
1580                        .filter_map(|entry| {
1581                            let path = active_repository
1582                                .read(cx)
1583                                .repo_path_to_project_path(&entry.repo_path, cx)?;
1584                            Some(project.open_buffer(path, cx))
1585                        })
1586                        .collect()
1587                })
1588            })?;
1589
1590            let buffers = futures::future::join_all(tasks).await;
1591
1592            this.update_in(cx, |this, window, cx| {
1593                let task = active_repository.update(cx, |repo, cx| {
1594                    repo.checkout_files(
1595                        "HEAD",
1596                        entries
1597                            .into_iter()
1598                            .map(|entries| entries.repo_path)
1599                            .collect(),
1600                        cx,
1601                    )
1602                });
1603                this.update_visible_entries(window, cx);
1604                cx.notify();
1605                task
1606            })?
1607            .await?;
1608
1609            let tasks: Vec<_> = cx.update(|_, cx| {
1610                buffers
1611                    .iter()
1612                    .filter_map(|buffer| {
1613                        buffer.as_ref().ok()?.update(cx, |buffer, cx| {
1614                            buffer.is_dirty().then(|| buffer.reload(cx))
1615                        })
1616                    })
1617                    .collect()
1618            })?;
1619
1620            futures::future::join_all(tasks).await;
1621
1622            Ok(())
1623        });
1624
1625        cx.spawn_in(window, async move |this, cx| {
1626            let result = task.await;
1627
1628            this.update_in(cx, |this, window, cx| {
1629                if let Err(err) = result {
1630                    this.update_visible_entries(window, cx);
1631                    this.show_error_toast("checkout", err, cx);
1632                }
1633            })
1634            .ok();
1635        })
1636        .detach();
1637    }
1638
1639    fn restore_tracked_files(
1640        &mut self,
1641        _: &RestoreTrackedFiles,
1642        window: &mut Window,
1643        cx: &mut Context<Self>,
1644    ) {
1645        let entries = self
1646            .entries
1647            .iter()
1648            .filter_map(|entry| entry.status_entry().cloned())
1649            .filter(|status_entry| !status_entry.status.is_created())
1650            .collect::<Vec<_>>();
1651
1652        match entries.len() {
1653            0 => return,
1654            1 => return self.revert_entry(&entries[0], window, cx),
1655            _ => {}
1656        }
1657        let mut details = entries
1658            .iter()
1659            .filter_map(|entry| entry.repo_path.as_ref().file_name())
1660            .map(|filename| filename.to_string())
1661            .take(5)
1662            .join("\n");
1663        if entries.len() > 5 {
1664            details.push_str(&format!("\nand {} more…", entries.len() - 5))
1665        }
1666
1667        #[derive(strum::EnumIter, strum::VariantNames)]
1668        #[strum(serialize_all = "title_case")]
1669        enum RestoreCancel {
1670            RestoreTrackedFiles,
1671            Cancel,
1672        }
1673        let prompt = prompt(
1674            "Discard changes to these files?",
1675            Some(&details),
1676            window,
1677            cx,
1678        );
1679        cx.spawn_in(window, async move |this, cx| {
1680            if let Ok(RestoreCancel::RestoreTrackedFiles) = prompt.await {
1681                this.update_in(cx, |this, window, cx| {
1682                    this.perform_checkout(entries, window, cx);
1683                })
1684                .ok();
1685            }
1686        })
1687        .detach();
1688    }
1689
1690    fn clean_all(&mut self, _: &TrashUntrackedFiles, window: &mut Window, cx: &mut Context<Self>) {
1691        let workspace = self.workspace.clone();
1692        let Some(active_repo) = self.active_repository.clone() else {
1693            return;
1694        };
1695        let to_delete = self
1696            .entries
1697            .iter()
1698            .filter_map(|entry| entry.status_entry())
1699            .filter(|status_entry| status_entry.status.is_created())
1700            .cloned()
1701            .collect::<Vec<_>>();
1702
1703        match to_delete.len() {
1704            0 => return,
1705            1 => return self.revert_entry(&to_delete[0], window, cx),
1706            _ => {}
1707        };
1708
1709        let mut details = to_delete
1710            .iter()
1711            .map(|entry| {
1712                entry
1713                    .repo_path
1714                    .as_ref()
1715                    .file_name()
1716                    .map(|f| f.to_string())
1717                    .unwrap_or_default()
1718            })
1719            .take(5)
1720            .join("\n");
1721
1722        if to_delete.len() > 5 {
1723            details.push_str(&format!("\nand {} more…", to_delete.len() - 5))
1724        }
1725
1726        let prompt = prompt("Trash these files?", Some(&details), window, cx);
1727        cx.spawn_in(window, async move |this, cx| {
1728            match prompt.await? {
1729                TrashCancel::Trash => {}
1730                TrashCancel::Cancel => return Ok(()),
1731            }
1732            let tasks = workspace.update(cx, |workspace, cx| {
1733                to_delete
1734                    .iter()
1735                    .filter_map(|entry| {
1736                        workspace.project().update(cx, |project, cx| {
1737                            let project_path = active_repo
1738                                .read(cx)
1739                                .repo_path_to_project_path(&entry.repo_path, cx)?;
1740                            project.delete_file(project_path, true, cx)
1741                        })
1742                    })
1743                    .collect::<Vec<_>>()
1744            })?;
1745            let to_unstage = to_delete
1746                .into_iter()
1747                .filter(|entry| !entry.status.staging().is_fully_unstaged())
1748                .collect();
1749            this.update(cx, |this, cx| this.change_file_stage(false, to_unstage, cx))?;
1750            for task in tasks {
1751                task.await?;
1752            }
1753            Ok(())
1754        })
1755        .detach_and_prompt_err("Failed to trash files", window, cx, |e, _, _| {
1756            Some(format!("{e}"))
1757        });
1758    }
1759
1760    fn change_all_files_stage(&mut self, stage: bool, cx: &mut Context<Self>) {
1761        let Some(active_repository) = self.active_repository.clone() else {
1762            return;
1763        };
1764        cx.spawn({
1765            async move |this, cx| {
1766                let result = this
1767                    .update(cx, |this, cx| {
1768                        let task = active_repository.update(cx, |repo, cx| {
1769                            if stage {
1770                                repo.stage_all(cx)
1771                            } else {
1772                                repo.unstage_all(cx)
1773                            }
1774                        });
1775                        this.update_counts(active_repository.read(cx));
1776                        cx.notify();
1777                        task
1778                    })?
1779                    .await;
1780
1781                this.update(cx, |this, cx| {
1782                    if let Err(err) = result {
1783                        this.show_error_toast(if stage { "add" } else { "reset" }, err, cx);
1784                    }
1785                    cx.notify()
1786                })
1787            }
1788        })
1789        .detach();
1790    }
1791
1792    fn stage_status_for_entry(entry: &GitStatusEntry, repo: &Repository) -> StageStatus {
1793        // Checking for current staged/unstaged file status is a chained operation:
1794        // 1. first, we check for any pending operation recorded in repository
1795        // 2. if there are no pending ops either running or finished, we then ask the repository
1796        //    for the most up-to-date file status read from disk - we do this since `entry` arg to this function `render_entry`
1797        //    is likely to be staled, and may lead to weird artifacts in the form of subsecond auto-uncheck/check on
1798        //    the checkbox's state (or flickering) which is undesirable.
1799        // 3. finally, if there is no info about this `entry` in the repo, we fall back to whatever status is encoded
1800        //    in `entry` arg.
1801        repo.pending_ops_for_path(&entry.repo_path)
1802            .map(|ops| {
1803                if ops.staging() || ops.staged() {
1804                    StageStatus::Staged
1805                } else {
1806                    StageStatus::Unstaged
1807                }
1808            })
1809            .or_else(|| {
1810                repo.status_for_path(&entry.repo_path)
1811                    .map(|status| status.status.staging())
1812            })
1813            .unwrap_or(entry.staging)
1814    }
1815
1816    fn stage_status_for_directory(
1817        &self,
1818        entry: &GitTreeDirEntry,
1819        repo: &Repository,
1820    ) -> StageStatus {
1821        let GitPanelViewMode::Tree(tree_state) = &self.view_mode else {
1822            util::debug_panic!("We should never render a directory entry while in flat view mode");
1823            return StageStatus::Unstaged;
1824        };
1825
1826        let Some(descendants) = tree_state.directory_descendants.get(&entry.key) else {
1827            return StageStatus::Unstaged;
1828        };
1829
1830        let show_placeholders = self.show_placeholders && !self.has_staged_changes();
1831        let mut fully_staged_count = 0usize;
1832        let mut any_staged_or_partially_staged = false;
1833
1834        for descendant in descendants {
1835            if show_placeholders && !descendant.status.is_created() {
1836                fully_staged_count += 1;
1837                any_staged_or_partially_staged = true;
1838            } else {
1839                match GitPanel::stage_status_for_entry(descendant, repo) {
1840                    StageStatus::Staged => {
1841                        fully_staged_count += 1;
1842                        any_staged_or_partially_staged = true;
1843                    }
1844                    StageStatus::PartiallyStaged => {
1845                        any_staged_or_partially_staged = true;
1846                    }
1847                    StageStatus::Unstaged => {}
1848                }
1849            }
1850        }
1851
1852        if descendants.is_empty() {
1853            StageStatus::Unstaged
1854        } else if fully_staged_count == descendants.len() {
1855            StageStatus::Staged
1856        } else if any_staged_or_partially_staged {
1857            StageStatus::PartiallyStaged
1858        } else {
1859            StageStatus::Unstaged
1860        }
1861    }
1862
1863    pub fn stage_all(&mut self, _: &StageAll, _window: &mut Window, cx: &mut Context<Self>) {
1864        self.change_all_files_stage(true, cx);
1865    }
1866
1867    pub fn unstage_all(&mut self, _: &UnstageAll, _window: &mut Window, cx: &mut Context<Self>) {
1868        self.change_all_files_stage(false, cx);
1869    }
1870
1871    fn toggle_staged_for_entry(
1872        &mut self,
1873        entry: &GitListEntry,
1874        _window: &mut Window,
1875        cx: &mut Context<Self>,
1876    ) {
1877        let Some(active_repository) = self.active_repository.clone() else {
1878            return;
1879        };
1880        let mut set_anchor: Option<RepoPath> = None;
1881        let mut clear_anchor = None;
1882
1883        let (stage, repo_paths) = {
1884            let repo = active_repository.read(cx);
1885            match entry {
1886                GitListEntry::Status(status_entry) => {
1887                    let repo_paths = vec![status_entry.clone()];
1888                    let stage = match GitPanel::stage_status_for_entry(status_entry, &repo) {
1889                        StageStatus::Staged => {
1890                            if let Some(op) = self.bulk_staging.clone()
1891                                && op.anchor == status_entry.repo_path
1892                            {
1893                                clear_anchor = Some(op.anchor);
1894                            }
1895                            false
1896                        }
1897                        StageStatus::Unstaged | StageStatus::PartiallyStaged => {
1898                            set_anchor = Some(status_entry.repo_path.clone());
1899                            true
1900                        }
1901                    };
1902                    (stage, repo_paths)
1903                }
1904                GitListEntry::TreeStatus(status_entry) => {
1905                    let repo_paths = vec![status_entry.entry.clone()];
1906                    let stage = match GitPanel::stage_status_for_entry(&status_entry.entry, &repo) {
1907                        StageStatus::Staged => {
1908                            if let Some(op) = self.bulk_staging.clone()
1909                                && op.anchor == status_entry.entry.repo_path
1910                            {
1911                                clear_anchor = Some(op.anchor);
1912                            }
1913                            false
1914                        }
1915                        StageStatus::Unstaged | StageStatus::PartiallyStaged => {
1916                            set_anchor = Some(status_entry.entry.repo_path.clone());
1917                            true
1918                        }
1919                    };
1920                    (stage, repo_paths)
1921                }
1922                GitListEntry::Header(section) => {
1923                    let goal_staged_state = !self.header_state(section.header).selected();
1924                    let entries = self
1925                        .entries
1926                        .iter()
1927                        .filter_map(|entry| entry.status_entry())
1928                        .filter(|status_entry| {
1929                            section.contains(status_entry, &repo)
1930                                && GitPanel::stage_status_for_entry(status_entry, &repo).as_bool()
1931                                    != Some(goal_staged_state)
1932                        })
1933                        .cloned()
1934                        .collect::<Vec<_>>();
1935
1936                    (goal_staged_state, entries)
1937                }
1938                GitListEntry::Directory(entry) => {
1939                    let goal_staged_state = match self.stage_status_for_directory(entry, repo) {
1940                        StageStatus::Staged => StageStatus::Unstaged,
1941                        StageStatus::Unstaged | StageStatus::PartiallyStaged => StageStatus::Staged,
1942                    };
1943                    let goal_stage = goal_staged_state == StageStatus::Staged;
1944
1945                    let entries = self
1946                        .view_mode
1947                        .tree_state()
1948                        .and_then(|state| state.directory_descendants.get(&entry.key))
1949                        .cloned()
1950                        .unwrap_or_default()
1951                        .into_iter()
1952                        .filter(|status_entry| {
1953                            GitPanel::stage_status_for_entry(status_entry, &repo)
1954                                != goal_staged_state
1955                        })
1956                        .collect::<Vec<_>>();
1957                    (goal_stage, entries)
1958                }
1959            }
1960        };
1961        if let Some(anchor) = clear_anchor {
1962            if let Some(op) = self.bulk_staging.clone()
1963                && op.anchor == anchor
1964            {
1965                self.bulk_staging = None;
1966            }
1967        }
1968        if let Some(anchor) = set_anchor {
1969            self.set_bulk_staging_anchor(anchor, cx);
1970        }
1971
1972        self.change_file_stage(stage, repo_paths, cx);
1973    }
1974
1975    fn change_file_stage(
1976        &mut self,
1977        stage: bool,
1978        entries: Vec<GitStatusEntry>,
1979        cx: &mut Context<Self>,
1980    ) {
1981        let Some(active_repository) = self.active_repository.clone() else {
1982            return;
1983        };
1984        cx.spawn({
1985            async move |this, cx| {
1986                let result = this
1987                    .update(cx, |this, cx| {
1988                        let task = active_repository.update(cx, |repo, cx| {
1989                            let repo_paths = entries
1990                                .iter()
1991                                .map(|entry| entry.repo_path.clone())
1992                                .collect();
1993                            if stage {
1994                                repo.stage_entries(repo_paths, cx)
1995                            } else {
1996                                repo.unstage_entries(repo_paths, cx)
1997                            }
1998                        });
1999                        this.update_counts(active_repository.read(cx));
2000                        cx.notify();
2001                        task
2002                    })?
2003                    .await;
2004
2005                this.update(cx, |this, cx| {
2006                    if let Err(err) = result {
2007                        this.show_error_toast(if stage { "add" } else { "reset" }, err, cx);
2008                    }
2009                    cx.notify();
2010                })
2011            }
2012        })
2013        .detach();
2014    }
2015
2016    pub fn total_staged_count(&self) -> usize {
2017        self.tracked_staged_count + self.new_staged_count + self.conflicted_staged_count
2018    }
2019
2020    pub fn stash_pop(&mut self, _: &StashPop, _window: &mut Window, cx: &mut Context<Self>) {
2021        let Some(active_repository) = self.active_repository.clone() else {
2022            return;
2023        };
2024
2025        cx.spawn({
2026            async move |this, cx| {
2027                let stash_task = active_repository
2028                    .update(cx, |repo, cx| repo.stash_pop(None, cx))
2029                    .await;
2030                this.update(cx, |this, cx| {
2031                    stash_task
2032                        .map_err(|e| {
2033                            this.show_error_toast("stash pop", e, cx);
2034                        })
2035                        .ok();
2036                    cx.notify();
2037                })
2038            }
2039        })
2040        .detach();
2041    }
2042
2043    pub fn stash_apply(&mut self, _: &StashApply, _window: &mut Window, cx: &mut Context<Self>) {
2044        let Some(active_repository) = self.active_repository.clone() else {
2045            return;
2046        };
2047
2048        cx.spawn({
2049            async move |this, cx| {
2050                let stash_task = active_repository
2051                    .update(cx, |repo, cx| repo.stash_apply(None, cx))
2052                    .await;
2053                this.update(cx, |this, cx| {
2054                    stash_task
2055                        .map_err(|e| {
2056                            this.show_error_toast("stash apply", e, cx);
2057                        })
2058                        .ok();
2059                    cx.notify();
2060                })
2061            }
2062        })
2063        .detach();
2064    }
2065
2066    pub fn stash_all(&mut self, _: &StashAll, _window: &mut Window, cx: &mut Context<Self>) {
2067        let Some(active_repository) = self.active_repository.clone() else {
2068            return;
2069        };
2070
2071        cx.spawn({
2072            async move |this, cx| {
2073                let stash_task = active_repository
2074                    .update(cx, |repo, cx| repo.stash_all(cx))
2075                    .await;
2076                this.update(cx, |this, cx| {
2077                    stash_task
2078                        .map_err(|e| {
2079                            this.show_error_toast("stash", e, cx);
2080                        })
2081                        .ok();
2082                    cx.notify();
2083                })
2084            }
2085        })
2086        .detach();
2087    }
2088
2089    pub fn commit_message_buffer(&self, cx: &App) -> Entity<Buffer> {
2090        self.commit_editor
2091            .read(cx)
2092            .buffer()
2093            .read(cx)
2094            .as_singleton()
2095            .unwrap()
2096    }
2097
2098    fn toggle_staged_for_selected(
2099        &mut self,
2100        _: &git::ToggleStaged,
2101        window: &mut Window,
2102        cx: &mut Context<Self>,
2103    ) {
2104        if let Some(selected_entry) = self.get_selected_entry().cloned() {
2105            self.toggle_staged_for_entry(&selected_entry, window, cx);
2106        }
2107    }
2108
2109    fn stage_range(&mut self, _: &git::StageRange, _window: &mut Window, cx: &mut Context<Self>) {
2110        let Some(index) = self.selected_entry else {
2111            return;
2112        };
2113        self.stage_bulk(index, cx);
2114    }
2115
2116    fn stage_selected(&mut self, _: &git::StageFile, _window: &mut Window, cx: &mut Context<Self>) {
2117        let Some(selected_entry) = self.get_selected_entry() else {
2118            return;
2119        };
2120        let Some(status_entry) = selected_entry.status_entry() else {
2121            return;
2122        };
2123        if status_entry.staging != StageStatus::Staged {
2124            self.change_file_stage(true, vec![status_entry.clone()], cx);
2125        }
2126    }
2127
2128    fn unstage_selected(
2129        &mut self,
2130        _: &git::UnstageFile,
2131        _window: &mut Window,
2132        cx: &mut Context<Self>,
2133    ) {
2134        let Some(selected_entry) = self.get_selected_entry() else {
2135            return;
2136        };
2137        let Some(status_entry) = selected_entry.status_entry() else {
2138            return;
2139        };
2140        if status_entry.staging != StageStatus::Unstaged {
2141            self.change_file_stage(false, vec![status_entry.clone()], cx);
2142        }
2143    }
2144
2145    fn on_commit(&mut self, _: &git::Commit, window: &mut Window, cx: &mut Context<Self>) {
2146        if self.commit(&self.commit_editor.focus_handle(cx), window, cx) {
2147            telemetry::event!("Git Committed", source = "Git Panel");
2148        }
2149    }
2150
2151    /// Commits staged changes with the current commit message.
2152    ///
2153    /// Returns `true` if the commit was executed, `false` otherwise.
2154    pub(crate) fn commit(
2155        &mut self,
2156        commit_editor_focus_handle: &FocusHandle,
2157        window: &mut Window,
2158        cx: &mut Context<Self>,
2159    ) -> bool {
2160        if self.amend_pending {
2161            return false;
2162        }
2163
2164        if commit_editor_focus_handle.contains_focused(window, cx) {
2165            self.commit_changes(
2166                CommitOptions {
2167                    amend: false,
2168                    signoff: self.signoff_enabled,
2169                    allow_empty: false,
2170                },
2171                window,
2172                cx,
2173            );
2174            true
2175        } else {
2176            cx.propagate();
2177            false
2178        }
2179    }
2180
2181    fn on_amend(&mut self, _: &git::Amend, window: &mut Window, cx: &mut Context<Self>) {
2182        if self.amend(&self.commit_editor.focus_handle(cx), window, cx) {
2183            telemetry::event!("Git Amended", source = "Git Panel");
2184        }
2185    }
2186
2187    /// Amends the most recent commit with staged changes and/or an updated commit message.
2188    ///
2189    /// Uses a two-stage workflow where the first invocation loads the commit
2190    /// message for editing, second invocation performs the amend. Returns
2191    /// `true` if the amend was executed, `false` otherwise.
2192    pub(crate) fn amend(
2193        &mut self,
2194        commit_editor_focus_handle: &FocusHandle,
2195        window: &mut Window,
2196        cx: &mut Context<Self>,
2197    ) -> bool {
2198        if commit_editor_focus_handle.contains_focused(window, cx) {
2199            if self.head_commit(cx).is_some() {
2200                if !self.amend_pending {
2201                    self.set_amend_pending(true, cx);
2202                    self.load_last_commit_message(cx);
2203
2204                    return false;
2205                } else {
2206                    self.commit_changes(
2207                        CommitOptions {
2208                            amend: true,
2209                            signoff: self.signoff_enabled,
2210                            allow_empty: false,
2211                        },
2212                        window,
2213                        cx,
2214                    );
2215
2216                    return true;
2217                }
2218            }
2219            return false;
2220        } else {
2221            cx.propagate();
2222            return false;
2223        }
2224    }
2225    pub fn head_commit(&self, cx: &App) -> Option<CommitDetails> {
2226        self.active_repository
2227            .as_ref()
2228            .and_then(|repo| repo.read(cx).head_commit.as_ref())
2229            .cloned()
2230    }
2231
2232    pub fn load_last_commit_message(&mut self, cx: &mut Context<Self>) {
2233        let Some(head_commit) = self.head_commit(cx) else {
2234            return;
2235        };
2236
2237        let recent_sha = head_commit.sha.to_string();
2238        let detail_task = self.load_commit_details(recent_sha, cx);
2239        cx.spawn(async move |this, cx| {
2240            if let Ok(message) = detail_task.await.map(|detail| detail.message) {
2241                this.update(cx, |this, cx| {
2242                    this.commit_message_buffer(cx).update(cx, |buffer, cx| {
2243                        let start = buffer.anchor_before(0);
2244                        let end = buffer.anchor_after(buffer.len());
2245                        buffer.edit([(start..end, message)], None, cx);
2246                    });
2247                })
2248                .log_err();
2249            }
2250        })
2251        .detach();
2252    }
2253
2254    fn custom_or_suggested_commit_message(
2255        &self,
2256        window: &mut Window,
2257        cx: &mut Context<Self>,
2258    ) -> Option<String> {
2259        let git_commit_language = self
2260            .commit_editor
2261            .read(cx)
2262            .language_at(MultiBufferOffset(0), cx);
2263        let message = self.commit_editor.read(cx).text(cx);
2264        if message.is_empty() {
2265            return self
2266                .suggest_commit_message(cx)
2267                .filter(|message| !message.trim().is_empty());
2268        } else if message.trim().is_empty() {
2269            return None;
2270        }
2271        let buffer = cx.new(|cx| {
2272            let mut buffer = Buffer::local(message, cx);
2273            buffer.set_language(git_commit_language, cx);
2274            buffer
2275        });
2276        let editor = cx.new(|cx| Editor::for_buffer(buffer, None, window, cx));
2277        let wrapped_message = editor.update(cx, |editor, cx| {
2278            editor.select_all(&Default::default(), window, cx);
2279            editor.rewrap_impl(
2280                RewrapOptions {
2281                    override_language_settings: false,
2282                    preserve_existing_whitespace: true,
2283                    line_length: None,
2284                },
2285                cx,
2286            );
2287            editor.text(cx)
2288        });
2289        if wrapped_message.trim().is_empty() {
2290            return None;
2291        }
2292        Some(wrapped_message)
2293    }
2294
2295    fn has_commit_message(&self, cx: &mut Context<Self>) -> bool {
2296        let text = self.commit_editor.read(cx).text(cx);
2297        if !text.trim().is_empty() {
2298            true
2299        } else if text.is_empty() {
2300            self.suggest_commit_message(cx)
2301                .is_some_and(|text| !text.trim().is_empty())
2302        } else {
2303            false
2304        }
2305    }
2306
2307    pub(crate) fn commit_changes(
2308        &mut self,
2309        options: CommitOptions,
2310        window: &mut Window,
2311        cx: &mut Context<Self>,
2312    ) {
2313        let Some(active_repository) = self.active_repository.clone() else {
2314            return;
2315        };
2316        let error_spawn = |message, window: &mut Window, cx: &mut App| {
2317            let prompt = window.prompt(PromptLevel::Warning, message, None, &["Ok"], cx);
2318            cx.spawn(async move |_| {
2319                prompt.await.ok();
2320            })
2321            .detach();
2322        };
2323
2324        if self.has_unstaged_conflicts() {
2325            error_spawn(
2326                "There are still conflicts. You must stage these before committing",
2327                window,
2328                cx,
2329            );
2330            return;
2331        }
2332
2333        let askpass = self.askpass_delegate("git commit", window, cx);
2334        let commit_message = self.custom_or_suggested_commit_message(window, cx);
2335
2336        let Some(mut message) = commit_message else {
2337            self.commit_editor
2338                .read(cx)
2339                .focus_handle(cx)
2340                .focus(window, cx);
2341            return;
2342        };
2343
2344        if self.add_coauthors {
2345            self.fill_co_authors(&mut message, cx);
2346        }
2347
2348        let task = if self.has_staged_changes() {
2349            // Repository serializes all git operations, so we can just send a commit immediately
2350            let commit_task = active_repository.update(cx, |repo, cx| {
2351                repo.commit(message.into(), None, options, askpass, cx)
2352            });
2353            cx.background_spawn(async move { commit_task.await? })
2354        } else {
2355            let changed_files = self
2356                .entries
2357                .iter()
2358                .filter_map(|entry| entry.status_entry())
2359                .filter(|status_entry| !status_entry.status.is_created())
2360                .map(|status_entry| status_entry.repo_path.clone())
2361                .collect::<Vec<_>>();
2362
2363            if changed_files.is_empty() && !options.amend {
2364                error_spawn("No changes to commit", window, cx);
2365                return;
2366            }
2367
2368            let stage_task =
2369                active_repository.update(cx, |repo, cx| repo.stage_entries(changed_files, cx));
2370            cx.spawn(async move |_, cx| {
2371                stage_task.await?;
2372                let commit_task = active_repository.update(cx, |repo, cx| {
2373                    repo.commit(message.into(), None, options, askpass, cx)
2374                });
2375                commit_task.await?
2376            })
2377        };
2378        let task = cx.spawn_in(window, async move |this, cx| {
2379            let result = task.await;
2380            this.update_in(cx, |this, window, cx| {
2381                this.pending_commit.take();
2382
2383                match result {
2384                    Ok(()) => {
2385                        if options.amend {
2386                            this.set_amend_pending(false, cx);
2387                        } else {
2388                            this.commit_editor
2389                                .update(cx, |editor, cx| editor.clear(window, cx));
2390                            this.original_commit_message = None;
2391                        }
2392                    }
2393                    Err(e) => this.show_error_toast("commit", e, cx),
2394                }
2395            })
2396            .ok();
2397        });
2398
2399        self.pending_commit = Some(task);
2400    }
2401
2402    pub(crate) fn uncommit(&mut self, window: &mut Window, cx: &mut Context<Self>) {
2403        let Some(repo) = self.active_repository.clone() else {
2404            return;
2405        };
2406        telemetry::event!("Git Uncommitted");
2407
2408        let confirmation = self.check_for_pushed_commits(window, cx);
2409        let prior_head = self.load_commit_details("HEAD".to_string(), cx);
2410
2411        let task = cx.spawn_in(window, async move |this, cx| {
2412            let result = maybe!(async {
2413                if let Ok(true) = confirmation.await {
2414                    let prior_head = prior_head.await?;
2415
2416                    repo.update(cx, |repo, cx| {
2417                        repo.reset("HEAD^".to_string(), ResetMode::Soft, cx)
2418                    })
2419                    .await??;
2420
2421                    Ok(Some(prior_head))
2422                } else {
2423                    Ok(None)
2424                }
2425            })
2426            .await;
2427
2428            this.update_in(cx, |this, window, cx| {
2429                this.pending_commit.take();
2430                match result {
2431                    Ok(None) => {}
2432                    Ok(Some(prior_commit)) => {
2433                        this.commit_editor.update(cx, |editor, cx| {
2434                            editor.set_text(prior_commit.message, window, cx)
2435                        });
2436                    }
2437                    Err(e) => this.show_error_toast("reset", e, cx),
2438                }
2439            })
2440            .ok();
2441        });
2442
2443        self.pending_commit = Some(task);
2444    }
2445
2446    fn check_for_pushed_commits(
2447        &mut self,
2448        window: &mut Window,
2449        cx: &mut Context<Self>,
2450    ) -> impl Future<Output = anyhow::Result<bool>> + use<> {
2451        let repo = self.active_repository.clone();
2452        let mut cx = window.to_async(cx);
2453
2454        async move {
2455            let repo = repo.context("No active repository")?;
2456
2457            let pushed_to: Vec<SharedString> = repo
2458                .update(&mut cx, |repo, _| repo.check_for_pushed_commits())
2459                .await??;
2460
2461            if pushed_to.is_empty() {
2462                Ok(true)
2463            } else {
2464                #[derive(strum::EnumIter, strum::VariantNames)]
2465                #[strum(serialize_all = "title_case")]
2466                enum CancelUncommit {
2467                    Uncommit,
2468                    Cancel,
2469                }
2470                let detail = format!(
2471                    "This commit was already pushed to {}.",
2472                    pushed_to.into_iter().join(", ")
2473                );
2474                let result = cx
2475                    .update(|window, cx| prompt("Are you sure?", Some(&detail), window, cx))?
2476                    .await?;
2477
2478                match result {
2479                    CancelUncommit::Cancel => Ok(false),
2480                    CancelUncommit::Uncommit => Ok(true),
2481                }
2482            }
2483        }
2484    }
2485
2486    /// Suggests a commit message based on the changed files and their statuses
2487    pub fn suggest_commit_message(&self, cx: &App) -> Option<String> {
2488        if let Some(merge_message) = self
2489            .active_repository
2490            .as_ref()
2491            .and_then(|repo| repo.read(cx).merge.message.as_ref())
2492        {
2493            return Some(merge_message.to_string());
2494        }
2495
2496        let git_status_entry = if let Some(staged_entry) = &self.single_staged_entry {
2497            Some(staged_entry)
2498        } else if self.total_staged_count() == 0
2499            && let Some(single_tracked_entry) = &self.single_tracked_entry
2500        {
2501            Some(single_tracked_entry)
2502        } else {
2503            None
2504        }?;
2505
2506        let action_text = if git_status_entry.status.is_deleted() {
2507            Some("Delete")
2508        } else if git_status_entry.status.is_created() {
2509            Some("Create")
2510        } else if git_status_entry.status.is_modified() {
2511            Some("Update")
2512        } else {
2513            None
2514        }?;
2515
2516        let file_name = git_status_entry
2517            .repo_path
2518            .file_name()
2519            .unwrap_or_default()
2520            .to_string();
2521
2522        Some(format!("{} {}", action_text, file_name))
2523    }
2524
2525    fn generate_commit_message_action(
2526        &mut self,
2527        _: &git::GenerateCommitMessage,
2528        _window: &mut Window,
2529        cx: &mut Context<Self>,
2530    ) {
2531        self.generate_commit_message(cx);
2532    }
2533
2534    fn split_patch(patch: &str) -> Vec<String> {
2535        let mut result = Vec::new();
2536        let mut current_patch = String::new();
2537
2538        for line in patch.lines() {
2539            if line.starts_with("---") && !current_patch.is_empty() {
2540                result.push(current_patch.trim_end_matches('\n').into());
2541                current_patch = String::new();
2542            }
2543            current_patch.push_str(line);
2544            current_patch.push('\n');
2545        }
2546
2547        if !current_patch.is_empty() {
2548            result.push(current_patch.trim_end_matches('\n').into());
2549        }
2550
2551        result
2552    }
2553    fn truncate_iteratively(patch: &str, max_bytes: usize) -> String {
2554        let mut current_size = patch.len();
2555        if current_size <= max_bytes {
2556            return patch.to_string();
2557        }
2558        let file_patches = Self::split_patch(patch);
2559        let mut file_infos: Vec<TruncatedPatch> = file_patches
2560            .iter()
2561            .filter_map(|patch| TruncatedPatch::from_unified_diff(patch))
2562            .collect();
2563
2564        if file_infos.is_empty() {
2565            return patch.to_string();
2566        }
2567
2568        current_size = file_infos.iter().map(|f| f.calculate_size()).sum::<usize>();
2569        while current_size > max_bytes {
2570            let file_idx = file_infos
2571                .iter()
2572                .enumerate()
2573                .filter(|(_, f)| f.hunks_to_keep > 1)
2574                .max_by_key(|(_, f)| f.hunks_to_keep)
2575                .map(|(idx, _)| idx);
2576            match file_idx {
2577                Some(idx) => {
2578                    let file = &mut file_infos[idx];
2579                    let size_before = file.calculate_size();
2580                    file.hunks_to_keep -= 1;
2581                    let size_after = file.calculate_size();
2582                    let saved = size_before.saturating_sub(size_after);
2583                    current_size = current_size.saturating_sub(saved);
2584                }
2585                None => {
2586                    break;
2587                }
2588            }
2589        }
2590
2591        file_infos
2592            .iter()
2593            .map(|info| info.to_string())
2594            .collect::<Vec<_>>()
2595            .join("\n")
2596    }
2597
2598    pub fn compress_commit_diff(diff_text: &str, max_bytes: usize) -> String {
2599        if diff_text.len() <= max_bytes {
2600            return diff_text.to_string();
2601        }
2602
2603        let mut compressed = diff_text
2604            .lines()
2605            .map(|line| {
2606                if line.len() > 256 {
2607                    format!("{}...[truncated]\n", &line[..line.floor_char_boundary(256)])
2608                } else {
2609                    format!("{}\n", line)
2610                }
2611            })
2612            .collect::<Vec<_>>()
2613            .join("");
2614
2615        if compressed.len() <= max_bytes {
2616            return compressed;
2617        }
2618
2619        compressed = Self::truncate_iteratively(&compressed, max_bytes);
2620
2621        compressed
2622    }
2623
2624    async fn load_project_rules(
2625        project: &Entity<Project>,
2626        repo_work_dir: &Arc<Path>,
2627        cx: &mut AsyncApp,
2628    ) -> Option<String> {
2629        let rules_path = cx.update(|cx| {
2630            for worktree in project.read(cx).worktrees(cx) {
2631                let worktree_abs_path = worktree.read(cx).abs_path();
2632                if !worktree_abs_path.starts_with(&repo_work_dir) {
2633                    continue;
2634                }
2635
2636                let worktree_snapshot = worktree.read(cx).snapshot();
2637                for rules_name in RULES_FILE_NAMES {
2638                    if let Ok(rel_path) = RelPath::unix(rules_name) {
2639                        if let Some(entry) = worktree_snapshot.entry_for_path(rel_path) {
2640                            if entry.is_file() {
2641                                return Some(ProjectPath {
2642                                    worktree_id: worktree.read(cx).id(),
2643                                    path: entry.path.clone(),
2644                                });
2645                            }
2646                        }
2647                    }
2648                }
2649            }
2650            None
2651        })?;
2652
2653        let buffer = project
2654            .update(cx, |project, cx| project.open_buffer(rules_path, cx))
2655            .await
2656            .ok()?;
2657
2658        let content = buffer
2659            .read_with(cx, |buffer, _| buffer.text())
2660            .trim()
2661            .to_string();
2662
2663        if content.is_empty() {
2664            None
2665        } else {
2666            Some(content)
2667        }
2668    }
2669
2670    async fn load_commit_message_prompt(cx: &mut AsyncApp) -> String {
2671        let load = async {
2672            let store = cx.update(|cx| PromptStore::global(cx)).await.ok()?;
2673            store
2674                .update(cx, |s, cx| {
2675                    s.load(PromptId::BuiltIn(BuiltInPrompt::CommitMessage), cx)
2676                })
2677                .await
2678                .ok()
2679        };
2680        load.await
2681            .unwrap_or_else(|| BuiltInPrompt::CommitMessage.default_content().to_string())
2682    }
2683
2684    /// Generates a commit message using an LLM.
2685    pub fn generate_commit_message(&mut self, cx: &mut Context<Self>) {
2686        if !self.can_commit() || !AgentSettings::get_global(cx).enabled(cx) {
2687            return;
2688        }
2689
2690        let Some(ConfiguredModel { provider, model }) =
2691            LanguageModelRegistry::read_global(cx).commit_message_model()
2692        else {
2693            return;
2694        };
2695
2696        let Some(repo) = self.active_repository.as_ref() else {
2697            return;
2698        };
2699
2700        telemetry::event!("Git Commit Message Generated");
2701
2702        let diff = repo.update(cx, |repo, cx| {
2703            if self.has_staged_changes() {
2704                repo.diff(DiffType::HeadToIndex, cx)
2705            } else {
2706                repo.diff(DiffType::HeadToWorktree, cx)
2707            }
2708        });
2709
2710        let temperature = AgentSettings::temperature_for_model(&model, cx);
2711        let project = self.project.clone();
2712        let repo_work_dir = repo.read(cx).work_directory_abs_path.clone();
2713
2714        self.generate_commit_message_task = Some(cx.spawn(async move |this, mut cx| {
2715             async move {
2716                let _defer = cx.on_drop(&this, |this, _cx| {
2717                    this.generate_commit_message_task.take();
2718                });
2719
2720                if let Some(task) = cx.update(|cx| {
2721                    if !provider.is_authenticated(cx) {
2722                        Some(provider.authenticate(cx))
2723                    } else {
2724                        None
2725                    }
2726                }) {
2727                    task.await.log_err();
2728                }
2729
2730                let mut diff_text = match diff.await {
2731                    Ok(result) => match result {
2732                        Ok(text) => text,
2733                        Err(e) => {
2734                            Self::show_commit_message_error(&this, &e, cx);
2735                            return anyhow::Ok(());
2736                        }
2737                    },
2738                    Err(e) => {
2739                        Self::show_commit_message_error(&this, &e, cx);
2740                        return anyhow::Ok(());
2741                    }
2742                };
2743
2744                const MAX_DIFF_BYTES: usize = 20_000;
2745                diff_text = Self::compress_commit_diff(&diff_text, MAX_DIFF_BYTES);
2746
2747                let rules_content = Self::load_project_rules(&project, &repo_work_dir, &mut cx).await;
2748
2749                let prompt = Self::load_commit_message_prompt(&mut cx).await;
2750
2751                let subject = this.update(cx, |this, cx| {
2752                    this.commit_editor.read(cx).text(cx).lines().next().map(ToOwned::to_owned).unwrap_or_default()
2753                })?;
2754
2755                let text_empty = subject.trim().is_empty();
2756
2757                let rules_section = match &rules_content {
2758                    Some(rules) => format!(
2759                        "\n\nThe user has provided the following project rules that you should follow when writing the commit message:\n\
2760                        <project_rules>\n{rules}\n</project_rules>\n"
2761                    ),
2762                    None => String::new(),
2763                };
2764
2765                let subject_section = if text_empty {
2766                    String::new()
2767                } else {
2768                    format!("\nHere is the user's subject line:\n{subject}")
2769                };
2770
2771                let content = format!(
2772                    "{prompt}{rules_section}{subject_section}\nHere are the changes in this commit:\n{diff_text}"
2773                );
2774
2775                let request = LanguageModelRequest {
2776                    thread_id: None,
2777                    prompt_id: None,
2778                    intent: Some(CompletionIntent::GenerateGitCommitMessage),
2779                    messages: vec![LanguageModelRequestMessage {
2780                        role: Role::User,
2781                        content: vec![content.into()],
2782                        cache: false,
2783                        reasoning_details: None,
2784                    }],
2785                    tools: Vec::new(),
2786                    tool_choice: None,
2787                    stop: Vec::new(),
2788                    temperature,
2789                    thinking_allowed: false,
2790                    thinking_effort: None,
2791                    speed: None,
2792                };
2793
2794                let stream = model.stream_completion_text(request, cx);
2795                match stream.await {
2796                    Ok(mut messages) => {
2797                        if !text_empty {
2798                            this.update(cx, |this, cx| {
2799                                this.commit_message_buffer(cx).update(cx, |buffer, cx| {
2800                                    let insert_position = buffer.anchor_before(buffer.len());
2801                                    buffer.edit([(insert_position..insert_position, "\n")], None, cx)
2802                                });
2803                            })?;
2804                        }
2805
2806                        while let Some(message) = messages.stream.next().await {
2807                            match message {
2808                                Ok(text) => {
2809                                    this.update(cx, |this, cx| {
2810                                        this.commit_message_buffer(cx).update(cx, |buffer, cx| {
2811                                            let insert_position = buffer.anchor_before(buffer.len());
2812                                            buffer.edit([(insert_position..insert_position, text)], None, cx);
2813                                        });
2814                                    })?;
2815                                }
2816                                Err(e) => {
2817                                    Self::show_commit_message_error(&this, &e, cx);
2818                                    break;
2819                                }
2820                            }
2821                        }
2822                    }
2823                    Err(e) => {
2824                        Self::show_commit_message_error(&this, &e, cx);
2825                    }
2826                }
2827
2828                anyhow::Ok(())
2829            }
2830            .log_err().await
2831        }));
2832    }
2833
2834    fn get_fetch_options(
2835        &self,
2836        window: &mut Window,
2837        cx: &mut Context<Self>,
2838    ) -> Task<Option<FetchOptions>> {
2839        let repo = self.active_repository.clone();
2840        let workspace = self.workspace.clone();
2841
2842        cx.spawn_in(window, async move |_, cx| {
2843            let repo = repo?;
2844            let remotes = repo
2845                .update(cx, |repo, _| repo.get_remotes(None, false))
2846                .await
2847                .ok()?
2848                .log_err()?;
2849
2850            let mut remotes: Vec<_> = remotes.into_iter().map(FetchOptions::Remote).collect();
2851            if remotes.len() > 1 {
2852                remotes.push(FetchOptions::All);
2853            }
2854            let selection = cx
2855                .update(|window, cx| {
2856                    picker_prompt::prompt(
2857                        "Pick which remote to fetch",
2858                        remotes.iter().map(|r| r.name()).collect(),
2859                        workspace,
2860                        window,
2861                        cx,
2862                    )
2863                })
2864                .ok()?
2865                .await?;
2866            remotes.get(selection).cloned()
2867        })
2868    }
2869
2870    pub(crate) fn fetch(
2871        &mut self,
2872        is_fetch_all: bool,
2873        window: &mut Window,
2874        cx: &mut Context<Self>,
2875    ) {
2876        if !self.can_push_and_pull(cx) {
2877            return;
2878        }
2879
2880        let Some(repo) = self.active_repository.clone() else {
2881            return;
2882        };
2883        telemetry::event!("Git Fetched");
2884        let askpass = self.askpass_delegate("git fetch", window, cx);
2885        let this = cx.weak_entity();
2886
2887        let fetch_options = if is_fetch_all {
2888            Task::ready(Some(FetchOptions::All))
2889        } else {
2890            self.get_fetch_options(window, cx)
2891        };
2892
2893        window
2894            .spawn(cx, async move |cx| {
2895                let Some(fetch_options) = fetch_options.await else {
2896                    return Ok(());
2897                };
2898                let fetch = repo.update(cx, |repo, cx| {
2899                    repo.fetch(fetch_options.clone(), askpass, cx)
2900                });
2901
2902                let remote_message = fetch.await?;
2903                this.update(cx, |this, cx| {
2904                    let action = match fetch_options {
2905                        FetchOptions::All => RemoteAction::Fetch(None),
2906                        FetchOptions::Remote(remote) => RemoteAction::Fetch(Some(remote)),
2907                    };
2908                    match remote_message {
2909                        Ok(remote_message) => this.show_remote_output(action, remote_message, cx),
2910                        Err(e) => {
2911                            log::error!("Error while fetching {:?}", e);
2912                            this.show_error_toast(action.name(), e, cx)
2913                        }
2914                    }
2915
2916                    anyhow::Ok(())
2917                })
2918                .ok();
2919                anyhow::Ok(())
2920            })
2921            .detach_and_log_err(cx);
2922    }
2923
2924    pub(crate) fn git_clone(&mut self, repo: String, window: &mut Window, cx: &mut Context<Self>) {
2925        let workspace = self.workspace.clone();
2926
2927        crate::clone::clone_and_open(
2928            repo.into(),
2929            workspace,
2930            window,
2931            cx,
2932            Arc::new(|_workspace: &mut workspace::Workspace, _window, _cx| {}),
2933        );
2934    }
2935
2936    pub(crate) fn git_init(&mut self, window: &mut Window, cx: &mut Context<Self>) {
2937        let worktrees = self
2938            .project
2939            .read(cx)
2940            .visible_worktrees(cx)
2941            .collect::<Vec<_>>();
2942
2943        let worktree = if worktrees.len() == 1 {
2944            Task::ready(Some(worktrees.first().unwrap().clone()))
2945        } else if worktrees.is_empty() {
2946            let result = window.prompt(
2947                PromptLevel::Warning,
2948                "Unable to initialize a git repository",
2949                Some("Open a directory first"),
2950                &["Ok"],
2951                cx,
2952            );
2953            cx.background_executor()
2954                .spawn(async move {
2955                    result.await.ok();
2956                })
2957                .detach();
2958            return;
2959        } else {
2960            let worktree_directories = worktrees
2961                .iter()
2962                .map(|worktree| worktree.read(cx).abs_path())
2963                .map(|worktree_abs_path| {
2964                    if let Ok(path) = worktree_abs_path.strip_prefix(util::paths::home_dir()) {
2965                        Path::new("~")
2966                            .join(path)
2967                            .to_string_lossy()
2968                            .to_string()
2969                            .into()
2970                    } else {
2971                        worktree_abs_path.to_string_lossy().into_owned().into()
2972                    }
2973                })
2974                .collect_vec();
2975            let prompt = picker_prompt::prompt(
2976                "Where would you like to initialize this git repository?",
2977                worktree_directories,
2978                self.workspace.clone(),
2979                window,
2980                cx,
2981            );
2982
2983            cx.spawn(async move |_, _| prompt.await.map(|ix| worktrees[ix].clone()))
2984        };
2985
2986        cx.spawn_in(window, async move |this, cx| {
2987            let worktree = match worktree.await {
2988                Some(worktree) => worktree,
2989                None => {
2990                    return;
2991                }
2992            };
2993
2994            let Ok(result) = this.update(cx, |this, cx| {
2995                let fallback_branch_name = GitPanelSettings::get_global(cx)
2996                    .fallback_branch_name
2997                    .clone();
2998                this.project.read(cx).git_init(
2999                    worktree.read(cx).abs_path(),
3000                    fallback_branch_name,
3001                    cx,
3002                )
3003            }) else {
3004                return;
3005            };
3006
3007            let result = result.await;
3008
3009            this.update_in(cx, |this, _, cx| match result {
3010                Ok(()) => {}
3011                Err(e) => this.show_error_toast("init", e, cx),
3012            })
3013            .ok();
3014        })
3015        .detach();
3016    }
3017
3018    pub(crate) fn pull(&mut self, rebase: bool, window: &mut Window, cx: &mut Context<Self>) {
3019        if !self.can_push_and_pull(cx) {
3020            return;
3021        }
3022        let Some(repo) = self.active_repository.clone() else {
3023            return;
3024        };
3025        let Some(branch) = repo.read(cx).branch.as_ref() else {
3026            return;
3027        };
3028        telemetry::event!("Git Pulled");
3029        let branch = branch.clone();
3030        let remote = self.get_remote(false, false, window, cx);
3031        cx.spawn_in(window, async move |this, cx| {
3032            let remote = match remote.await {
3033                Ok(Some(remote)) => remote,
3034                Ok(None) => {
3035                    return Ok(());
3036                }
3037                Err(e) => {
3038                    log::error!("Failed to get current remote: {}", e);
3039                    this.update(cx, |this, cx| this.show_error_toast("pull", e, cx))
3040                        .ok();
3041                    return Ok(());
3042                }
3043            };
3044
3045            let askpass = this.update_in(cx, |this, window, cx| {
3046                this.askpass_delegate(format!("git pull {}", remote.name), window, cx)
3047            })?;
3048
3049            let branch_name = branch
3050                .upstream
3051                .is_none()
3052                .then(|| branch.name().to_owned().into());
3053
3054            let pull = repo.update(cx, |repo, cx| {
3055                repo.pull(branch_name, remote.name.clone(), rebase, askpass, cx)
3056            });
3057
3058            let remote_message = pull.await?;
3059
3060            let action = RemoteAction::Pull(remote);
3061            this.update(cx, |this, cx| match remote_message {
3062                Ok(remote_message) => this.show_remote_output(action, remote_message, cx),
3063                Err(e) => {
3064                    log::error!("Error while pulling {:?}", e);
3065                    this.show_error_toast(action.name(), e, cx)
3066                }
3067            })
3068            .ok();
3069
3070            anyhow::Ok(())
3071        })
3072        .detach_and_log_err(cx);
3073    }
3074
3075    pub(crate) fn push(
3076        &mut self,
3077        force_push: bool,
3078        select_remote: bool,
3079        window: &mut Window,
3080        cx: &mut Context<Self>,
3081    ) {
3082        if !self.can_push_and_pull(cx) {
3083            return;
3084        }
3085        let Some(repo) = self.active_repository.clone() else {
3086            return;
3087        };
3088        let Some(branch) = repo.read(cx).branch.as_ref() else {
3089            return;
3090        };
3091        telemetry::event!("Git Pushed");
3092        let branch = branch.clone();
3093
3094        let options = if force_push {
3095            Some(PushOptions::Force)
3096        } else {
3097            match branch.upstream {
3098                Some(Upstream {
3099                    tracking: UpstreamTracking::Gone,
3100                    ..
3101                })
3102                | None => Some(PushOptions::SetUpstream),
3103                _ => None,
3104            }
3105        };
3106        let remote = self.get_remote(select_remote, true, window, cx);
3107
3108        cx.spawn_in(window, async move |this, cx| {
3109            let remote = match remote.await {
3110                Ok(Some(remote)) => remote,
3111                Ok(None) => {
3112                    return Ok(());
3113                }
3114                Err(e) => {
3115                    log::error!("Failed to get current remote: {}", e);
3116                    this.update(cx, |this, cx| this.show_error_toast("push", e, cx))
3117                        .ok();
3118                    return Ok(());
3119                }
3120            };
3121
3122            let askpass_delegate = this.update_in(cx, |this, window, cx| {
3123                this.askpass_delegate(format!("git push {}", remote.name), window, cx)
3124            })?;
3125
3126            let push = repo.update(cx, |repo, cx| {
3127                repo.push(
3128                    branch.name().to_owned().into(),
3129                    branch
3130                        .upstream
3131                        .as_ref()
3132                        .filter(|u| matches!(u.tracking, UpstreamTracking::Tracked(_)))
3133                        .and_then(|u| u.branch_name())
3134                        .unwrap_or_else(|| branch.name())
3135                        .to_owned()
3136                        .into(),
3137                    remote.name.clone(),
3138                    options,
3139                    askpass_delegate,
3140                    cx,
3141                )
3142            });
3143
3144            let remote_output = push.await?;
3145
3146            let action = RemoteAction::Push(branch.name().to_owned().into(), remote);
3147            this.update(cx, |this, cx| match remote_output {
3148                Ok(remote_message) => this.show_remote_output(action, remote_message, cx),
3149                Err(e) => {
3150                    log::error!("Error while pushing {:?}", e);
3151                    this.show_error_toast(action.name(), e, cx)
3152                }
3153            })?;
3154
3155            anyhow::Ok(())
3156        })
3157        .detach_and_log_err(cx);
3158    }
3159
3160    pub fn create_pull_request(&self, window: &mut Window, cx: &mut Context<Self>) {
3161        let result = (|| -> anyhow::Result<()> {
3162            let repo = self
3163                .active_repository
3164                .clone()
3165                .ok_or_else(|| anyhow::anyhow!("No active repository"))?;
3166
3167            let (branch, remote_origin, remote_upstream) = {
3168                let repository = repo.read(cx);
3169                (
3170                    repository.branch.clone(),
3171                    repository.remote_origin_url.clone(),
3172                    repository.remote_upstream_url.clone(),
3173                )
3174            };
3175
3176            let branch = branch.ok_or_else(|| anyhow::anyhow!("No active branch"))?;
3177            let source_branch = branch
3178                .upstream
3179                .as_ref()
3180                .filter(|upstream| matches!(upstream.tracking, UpstreamTracking::Tracked(_)))
3181                .and_then(|upstream| upstream.branch_name())
3182                .ok_or_else(|| anyhow::anyhow!("No remote configured for repository"))?;
3183            let source_branch = source_branch.to_string();
3184
3185            let remote_url = branch
3186                .upstream
3187                .as_ref()
3188                .and_then(|upstream| match upstream.remote_name() {
3189                    Some("upstream") => remote_upstream.as_deref(),
3190                    Some(_) => remote_origin.as_deref(),
3191                    None => None,
3192                })
3193                .or(remote_origin.as_deref())
3194                .or(remote_upstream.as_deref())
3195                .ok_or_else(|| anyhow::anyhow!("No remote configured for repository"))?;
3196            let remote_url = remote_url.to_string();
3197
3198            let provider_registry = GitHostingProviderRegistry::global(cx);
3199            let Some((provider, parsed_remote)) =
3200                git::parse_git_remote_url(provider_registry, &remote_url)
3201            else {
3202                return Err(anyhow::anyhow!("Unsupported remote URL: {}", remote_url));
3203            };
3204
3205            let Some(url) = provider.build_create_pull_request_url(&parsed_remote, &source_branch)
3206            else {
3207                return Err(anyhow::anyhow!("Unable to construct pull request URL"));
3208            };
3209
3210            cx.open_url(url.as_str());
3211            Ok(())
3212        })();
3213
3214        if let Err(err) = result {
3215            log::error!("Error while creating pull request {:?}", err);
3216            cx.defer_in(window, |panel, _window, cx| {
3217                panel.show_error_toast("create pull request", err, cx);
3218            });
3219        }
3220    }
3221
3222    fn askpass_delegate(
3223        &self,
3224        operation: impl Into<SharedString>,
3225        window: &mut Window,
3226        cx: &mut Context<Self>,
3227    ) -> AskPassDelegate {
3228        let workspace = self.workspace.clone();
3229        let operation = operation.into();
3230        let window = window.window_handle();
3231        AskPassDelegate::new(&mut cx.to_async(), move |prompt, tx, cx| {
3232            window
3233                .update(cx, |_, window, cx| {
3234                    workspace.update(cx, |workspace, cx| {
3235                        workspace.toggle_modal(window, cx, |window, cx| {
3236                            AskPassModal::new(operation.clone(), prompt.into(), tx, window, cx)
3237                        });
3238                    })
3239                })
3240                .ok();
3241        })
3242    }
3243
3244    fn can_push_and_pull(&self, cx: &App) -> bool {
3245        !self.project.read(cx).is_via_collab()
3246    }
3247
3248    fn get_remote(
3249        &mut self,
3250        always_select: bool,
3251        is_push: bool,
3252        window: &mut Window,
3253        cx: &mut Context<Self>,
3254    ) -> impl Future<Output = anyhow::Result<Option<Remote>>> + use<> {
3255        let repo = self.active_repository.clone();
3256        let workspace = self.workspace.clone();
3257        let mut cx = window.to_async(cx);
3258
3259        async move {
3260            let repo = repo.context("No active repository")?;
3261            let current_remotes: Vec<Remote> = repo
3262                .update(&mut cx, |repo, _| {
3263                    let current_branch = if always_select {
3264                        None
3265                    } else {
3266                        let current_branch = repo.branch.as_ref().context("No active branch")?;
3267                        Some(current_branch.name().to_string())
3268                    };
3269                    anyhow::Ok(repo.get_remotes(current_branch, is_push))
3270                })?
3271                .await??;
3272
3273            let current_remotes: Vec<_> = current_remotes
3274                .into_iter()
3275                .map(|remotes| remotes.name)
3276                .collect();
3277            let selection = cx
3278                .update(|window, cx| {
3279                    picker_prompt::prompt(
3280                        "Pick which remote to push to",
3281                        current_remotes.clone(),
3282                        workspace,
3283                        window,
3284                        cx,
3285                    )
3286                })?
3287                .await;
3288
3289            Ok(selection.map(|selection| Remote {
3290                name: current_remotes[selection].clone(),
3291            }))
3292        }
3293    }
3294
3295    pub fn load_local_committer(&mut self, cx: &Context<Self>) {
3296        if self.local_committer_task.is_none() {
3297            self.local_committer_task = Some(cx.spawn(async move |this, cx| {
3298                let committer = get_git_committer(cx).await;
3299                this.update(cx, |this, cx| {
3300                    this.local_committer = Some(committer);
3301                    cx.notify()
3302                })
3303                .ok();
3304            }));
3305        }
3306    }
3307
3308    fn potential_co_authors(&self, cx: &App) -> Vec<(String, String)> {
3309        let mut new_co_authors = Vec::new();
3310        let project = self.project.read(cx);
3311
3312        let Some(room) =
3313            call::ActiveCall::try_global(cx).and_then(|call| call.read(cx).room().cloned())
3314        else {
3315            return Vec::default();
3316        };
3317
3318        let room = room.read(cx);
3319
3320        for (peer_id, collaborator) in project.collaborators() {
3321            if collaborator.is_host {
3322                continue;
3323            }
3324
3325            let Some(participant) = room.remote_participant_for_peer_id(*peer_id) else {
3326                continue;
3327            };
3328            if !participant.can_write() {
3329                continue;
3330            }
3331            if let Some(email) = &collaborator.committer_email {
3332                let name = collaborator
3333                    .committer_name
3334                    .clone()
3335                    .or_else(|| participant.user.name.clone())
3336                    .unwrap_or_else(|| participant.user.github_login.clone().to_string());
3337                new_co_authors.push((name.clone(), email.clone()))
3338            }
3339        }
3340        if !project.is_local()
3341            && !project.is_read_only(cx)
3342            && let Some(local_committer) = self.local_committer(room, cx)
3343        {
3344            new_co_authors.push(local_committer);
3345        }
3346        new_co_authors
3347    }
3348
3349    fn local_committer(&self, room: &call::Room, cx: &App) -> Option<(String, String)> {
3350        let user = room.local_participant_user(cx)?;
3351        let committer = self.local_committer.as_ref()?;
3352        let email = committer.email.clone()?;
3353        let name = committer
3354            .name
3355            .clone()
3356            .or_else(|| user.name.clone())
3357            .unwrap_or_else(|| user.github_login.clone().to_string());
3358        Some((name, email))
3359    }
3360
3361    fn toggle_fill_co_authors(
3362        &mut self,
3363        _: &ToggleFillCoAuthors,
3364        _: &mut Window,
3365        cx: &mut Context<Self>,
3366    ) {
3367        self.add_coauthors = !self.add_coauthors;
3368        cx.notify();
3369    }
3370
3371    fn toggle_sort_by_path(
3372        &mut self,
3373        _: &ToggleSortByPath,
3374        _: &mut Window,
3375        cx: &mut Context<Self>,
3376    ) {
3377        let current_setting = GitPanelSettings::get_global(cx).sort_by_path;
3378        if let Some(workspace) = self.workspace.upgrade() {
3379            let workspace = workspace.read(cx);
3380            let fs = workspace.app_state().fs.clone();
3381            cx.update_global::<SettingsStore, _>(|store, _cx| {
3382                store.update_settings_file(fs, move |settings, _cx| {
3383                    settings.git_panel.get_or_insert_default().sort_by_path =
3384                        Some(!current_setting);
3385                });
3386            });
3387        }
3388    }
3389
3390    fn toggle_tree_view(&mut self, _: &ToggleTreeView, _: &mut Window, cx: &mut Context<Self>) {
3391        let current_setting = GitPanelSettings::get_global(cx).tree_view;
3392        if let Some(workspace) = self.workspace.upgrade() {
3393            let workspace = workspace.read(cx);
3394            let fs = workspace.app_state().fs.clone();
3395            cx.update_global::<SettingsStore, _>(|store, _cx| {
3396                store.update_settings_file(fs, move |settings, _cx| {
3397                    settings.git_panel.get_or_insert_default().tree_view = Some(!current_setting);
3398                });
3399            })
3400        }
3401    }
3402
3403    fn toggle_directory(&mut self, key: &TreeKey, window: &mut Window, cx: &mut Context<Self>) {
3404        if let Some(state) = self.view_mode.tree_state_mut() {
3405            let expanded = state.expanded_dirs.entry(key.clone()).or_insert(true);
3406            *expanded = !*expanded;
3407            self.update_visible_entries(window, cx);
3408        } else {
3409            util::debug_panic!("Attempted to toggle directory in flat Git Panel state");
3410        }
3411    }
3412
3413    fn fill_co_authors(&mut self, message: &mut String, cx: &mut Context<Self>) {
3414        const CO_AUTHOR_PREFIX: &str = "Co-authored-by: ";
3415
3416        let existing_text = message.to_ascii_lowercase();
3417        let lowercase_co_author_prefix = CO_AUTHOR_PREFIX.to_lowercase();
3418        let mut ends_with_co_authors = false;
3419        let existing_co_authors = existing_text
3420            .lines()
3421            .filter_map(|line| {
3422                let line = line.trim();
3423                if line.starts_with(&lowercase_co_author_prefix) {
3424                    ends_with_co_authors = true;
3425                    Some(line)
3426                } else {
3427                    ends_with_co_authors = false;
3428                    None
3429                }
3430            })
3431            .collect::<HashSet<_>>();
3432
3433        let new_co_authors = self
3434            .potential_co_authors(cx)
3435            .into_iter()
3436            .filter(|(_, email)| {
3437                !existing_co_authors
3438                    .iter()
3439                    .any(|existing| existing.contains(email.as_str()))
3440            })
3441            .collect::<Vec<_>>();
3442
3443        if new_co_authors.is_empty() {
3444            return;
3445        }
3446
3447        if !ends_with_co_authors {
3448            message.push('\n');
3449        }
3450        for (name, email) in new_co_authors {
3451            message.push('\n');
3452            message.push_str(CO_AUTHOR_PREFIX);
3453            message.push_str(&name);
3454            message.push_str(" <");
3455            message.push_str(&email);
3456            message.push('>');
3457        }
3458        message.push('\n');
3459    }
3460
3461    fn schedule_update(&mut self, window: &mut Window, cx: &mut Context<Self>) {
3462        let handle = cx.entity().downgrade();
3463        self.reopen_commit_buffer(window, cx);
3464        self.update_visible_entries_task = cx.spawn_in(window, async move |_, cx| {
3465            cx.background_executor().timer(UPDATE_DEBOUNCE).await;
3466            if let Some(git_panel) = handle.upgrade() {
3467                git_panel
3468                    .update_in(cx, |git_panel, window, cx| {
3469                        git_panel.update_visible_entries(window, cx);
3470                    })
3471                    .ok();
3472            }
3473        });
3474    }
3475
3476    fn reopen_commit_buffer(&mut self, window: &mut Window, cx: &mut Context<Self>) {
3477        let Some(active_repo) = self.active_repository.as_ref() else {
3478            return;
3479        };
3480        let load_buffer = active_repo.update(cx, |active_repo, cx| {
3481            let project = self.project.read(cx);
3482            active_repo.open_commit_buffer(
3483                Some(project.languages().clone()),
3484                project.buffer_store().clone(),
3485                cx,
3486            )
3487        });
3488        let load_template = self.load_commit_template(cx);
3489
3490        cx.spawn_in(window, async move |git_panel, cx| {
3491            let buffer = load_buffer.await?;
3492            let template = load_template.await?;
3493
3494            git_panel.update_in(cx, |git_panel, window, cx| {
3495                git_panel.commit_template = template;
3496                if buffer.read(cx).text().trim().is_empty() {
3497                    let template_text = git_panel
3498                        .commit_template
3499                        .as_ref()
3500                        .map(|t| t.template.clone())
3501                        .unwrap_or_default();
3502                    if !template_text.is_empty() {
3503                        buffer.update(cx, |buffer, cx| {
3504                            let start = buffer.anchor_before(0);
3505                            let end = buffer.anchor_after(buffer.len());
3506                            buffer.edit([(start..end, template_text)], None, cx);
3507                        });
3508                    }
3509                }
3510
3511                if git_panel
3512                    .commit_editor
3513                    .read(cx)
3514                    .buffer()
3515                    .read(cx)
3516                    .as_singleton()
3517                    .as_ref()
3518                    != Some(&buffer)
3519                {
3520                    git_panel.commit_editor = cx.new(|cx| {
3521                        commit_message_editor(
3522                            buffer,
3523                            git_panel.suggest_commit_message(cx).map(SharedString::from),
3524                            git_panel.project.clone(),
3525                            true,
3526                            window,
3527                            cx,
3528                        )
3529                    });
3530                }
3531            })
3532        })
3533        .detach_and_log_err(cx);
3534    }
3535
3536    fn update_visible_entries(&mut self, window: &mut Window, cx: &mut Context<Self>) {
3537        let path_style = self.project.read(cx).path_style(cx);
3538        let bulk_staging = self.bulk_staging.take();
3539        let last_staged_path_prev_index = bulk_staging
3540            .as_ref()
3541            .and_then(|op| self.entry_by_path(&op.anchor));
3542
3543        self.active_repository = self.project.read(cx).active_repository(cx);
3544        self.entries.clear();
3545        self.entries_indices.clear();
3546        self.single_staged_entry.take();
3547        self.single_tracked_entry.take();
3548        self.conflicted_count = 0;
3549        self.conflicted_staged_count = 0;
3550        self.changes_count = 0;
3551        self.new_count = 0;
3552        self.tracked_count = 0;
3553        self.new_staged_count = 0;
3554        self.tracked_staged_count = 0;
3555        self.entry_count = 0;
3556        self.max_width_item_index = None;
3557
3558        let sort_by_path = GitPanelSettings::get_global(cx).sort_by_path;
3559        let is_tree_view = matches!(self.view_mode, GitPanelViewMode::Tree(_));
3560        let group_by_status = is_tree_view || !sort_by_path;
3561
3562        let mut changed_entries = Vec::new();
3563        let mut new_entries = Vec::new();
3564        let mut conflict_entries = Vec::new();
3565        let mut single_staged_entry = None;
3566        let mut staged_count = 0;
3567        let mut seen_directories = HashSet::default();
3568        let mut max_width_estimate = 0usize;
3569        let mut max_width_item_index = None;
3570
3571        let Some(repo) = self.active_repository.as_ref() else {
3572            // Just clear entries if no repository is active.
3573            cx.notify();
3574            return;
3575        };
3576
3577        let repo = repo.read(cx);
3578
3579        self.stash_entries = repo.cached_stash();
3580
3581        for entry in repo.cached_status() {
3582            self.changes_count += 1;
3583            let is_conflict = repo.had_conflict_on_last_merge_head_change(&entry.repo_path);
3584            let is_new = entry.status.is_created();
3585            let staging = entry.status.staging();
3586
3587            if let Some(pending) = repo.pending_ops_for_path(&entry.repo_path)
3588                && pending
3589                    .ops
3590                    .iter()
3591                    .any(|op| op.git_status == pending_op::GitStatus::Reverted && op.finished())
3592            {
3593                continue;
3594            }
3595
3596            let entry = GitStatusEntry {
3597                repo_path: entry.repo_path.clone(),
3598                status: entry.status,
3599                staging,
3600                diff_stat: entry.diff_stat,
3601            };
3602
3603            if staging.has_staged() {
3604                staged_count += 1;
3605                single_staged_entry = Some(entry.clone());
3606            }
3607
3608            if group_by_status && is_conflict {
3609                conflict_entries.push(entry);
3610            } else if group_by_status && is_new {
3611                new_entries.push(entry);
3612            } else {
3613                changed_entries.push(entry);
3614            }
3615        }
3616
3617        if conflict_entries.is_empty() {
3618            if staged_count == 1
3619                && let Some(entry) = single_staged_entry.as_ref()
3620            {
3621                if let Some(ops) = repo.pending_ops_for_path(&entry.repo_path) {
3622                    if ops.staged() {
3623                        self.single_staged_entry = single_staged_entry;
3624                    }
3625                } else {
3626                    self.single_staged_entry = single_staged_entry;
3627                }
3628            } else if repo.pending_ops_summary().item_summary.staging_count == 1
3629                && let Some(ops) = repo.pending_ops().find(|ops| ops.staging())
3630            {
3631                self.single_staged_entry =
3632                    repo.status_for_path(&ops.repo_path)
3633                        .map(|status| GitStatusEntry {
3634                            repo_path: ops.repo_path.clone(),
3635                            status: status.status,
3636                            staging: StageStatus::Staged,
3637                            diff_stat: status.diff_stat,
3638                        });
3639            }
3640        }
3641
3642        if conflict_entries.is_empty() && changed_entries.len() == 1 {
3643            self.single_tracked_entry = changed_entries.first().cloned();
3644        }
3645
3646        let mut push_entry =
3647            |this: &mut Self,
3648             entry: GitListEntry,
3649             is_visible: bool,
3650             logical_indices: Option<&mut Vec<usize>>| {
3651                if let Some(estimate) =
3652                    this.width_estimate_for_list_entry(is_tree_view, &entry, path_style)
3653                {
3654                    if estimate > max_width_estimate {
3655                        max_width_estimate = estimate;
3656                        max_width_item_index = Some(this.entries.len());
3657                    }
3658                }
3659
3660                if let Some(repo_path) = entry.status_entry().map(|status| status.repo_path.clone())
3661                {
3662                    this.entries_indices.insert(repo_path, this.entries.len());
3663                }
3664
3665                if let (Some(indices), true) = (logical_indices, is_visible) {
3666                    indices.push(this.entries.len());
3667                }
3668
3669                this.entries.push(entry);
3670            };
3671
3672        macro_rules! take_section_entries {
3673            () => {
3674                [
3675                    (Section::Conflict, std::mem::take(&mut conflict_entries)),
3676                    (Section::Tracked, std::mem::take(&mut changed_entries)),
3677                    (Section::New, std::mem::take(&mut new_entries)),
3678                ]
3679            };
3680        }
3681
3682        match &mut self.view_mode {
3683            GitPanelViewMode::Tree(tree_state) => {
3684                tree_state.logical_indices.clear();
3685                tree_state.directory_descendants.clear();
3686
3687                // This is just to get around the borrow checker
3688                // because push_entry mutably borrows self
3689                let mut tree_state = std::mem::take(tree_state);
3690
3691                for (section, entries) in take_section_entries!() {
3692                    if entries.is_empty() {
3693                        continue;
3694                    }
3695
3696                    push_entry(
3697                        self,
3698                        GitListEntry::Header(GitHeaderEntry { header: section }),
3699                        true,
3700                        Some(&mut tree_state.logical_indices),
3701                    );
3702
3703                    for (entry, is_visible) in
3704                        tree_state.build_tree_entries(section, entries, &mut seen_directories)
3705                    {
3706                        push_entry(
3707                            self,
3708                            entry,
3709                            is_visible,
3710                            Some(&mut tree_state.logical_indices),
3711                        );
3712                    }
3713                }
3714
3715                tree_state
3716                    .expanded_dirs
3717                    .retain(|key, _| seen_directories.contains(key));
3718                self.view_mode = GitPanelViewMode::Tree(tree_state);
3719            }
3720            GitPanelViewMode::Flat => {
3721                for (section, entries) in take_section_entries!() {
3722                    if entries.is_empty() {
3723                        continue;
3724                    }
3725
3726                    if section != Section::Tracked || !sort_by_path {
3727                        push_entry(
3728                            self,
3729                            GitListEntry::Header(GitHeaderEntry { header: section }),
3730                            true,
3731                            None,
3732                        );
3733                    }
3734
3735                    for entry in entries {
3736                        push_entry(self, GitListEntry::Status(entry), true, None);
3737                    }
3738                }
3739            }
3740        }
3741
3742        self.max_width_item_index = max_width_item_index;
3743
3744        self.update_counts(repo);
3745
3746        let bulk_staging_anchor_new_index = bulk_staging
3747            .as_ref()
3748            .filter(|op| op.repo_id == repo.id)
3749            .and_then(|op| self.entry_by_path(&op.anchor));
3750        if bulk_staging_anchor_new_index == last_staged_path_prev_index
3751            && let Some(index) = bulk_staging_anchor_new_index
3752            && let Some(entry) = self.entries.get(index)
3753            && let Some(entry) = entry.status_entry()
3754            && GitPanel::stage_status_for_entry(entry, &repo)
3755                .as_bool()
3756                .unwrap_or(false)
3757        {
3758            self.bulk_staging = bulk_staging;
3759        }
3760
3761        self.select_first_entry_if_none(window, cx);
3762
3763        let suggested_commit_message = self.suggest_commit_message(cx);
3764        let placeholder_text = suggested_commit_message.unwrap_or("Enter commit message".into());
3765
3766        self.commit_editor.update(cx, |editor, cx| {
3767            editor.set_placeholder_text(&placeholder_text, window, cx)
3768        });
3769
3770        cx.notify();
3771    }
3772
3773    fn header_state(&self, header_type: Section) -> ToggleState {
3774        let (staged_count, count) = match header_type {
3775            Section::New => (self.new_staged_count, self.new_count),
3776            Section::Tracked => (self.tracked_staged_count, self.tracked_count),
3777            Section::Conflict => (self.conflicted_staged_count, self.conflicted_count),
3778        };
3779        if staged_count == 0 {
3780            ToggleState::Unselected
3781        } else if count == staged_count {
3782            ToggleState::Selected
3783        } else {
3784            ToggleState::Indeterminate
3785        }
3786    }
3787
3788    fn update_counts(&mut self, repo: &Repository) {
3789        self.show_placeholders = false;
3790        self.conflicted_count = 0;
3791        self.conflicted_staged_count = 0;
3792        self.new_count = 0;
3793        self.tracked_count = 0;
3794        self.new_staged_count = 0;
3795        self.tracked_staged_count = 0;
3796        self.entry_count = 0;
3797
3798        for status_entry in self.entries.iter().filter_map(|entry| entry.status_entry()) {
3799            self.entry_count += 1;
3800            let is_staging_or_staged = GitPanel::stage_status_for_entry(status_entry, repo)
3801                .as_bool()
3802                .unwrap_or(true);
3803
3804            if repo.had_conflict_on_last_merge_head_change(&status_entry.repo_path) {
3805                self.conflicted_count += 1;
3806                if is_staging_or_staged {
3807                    self.conflicted_staged_count += 1;
3808                }
3809            } else if status_entry.status.is_created() {
3810                self.new_count += 1;
3811                if is_staging_or_staged {
3812                    self.new_staged_count += 1;
3813                }
3814            } else {
3815                self.tracked_count += 1;
3816                if is_staging_or_staged {
3817                    self.tracked_staged_count += 1;
3818                }
3819            }
3820        }
3821    }
3822
3823    pub(crate) fn has_staged_changes(&self) -> bool {
3824        self.tracked_staged_count > 0
3825            || self.new_staged_count > 0
3826            || self.conflicted_staged_count > 0
3827    }
3828
3829    pub(crate) fn has_unstaged_changes(&self) -> bool {
3830        self.tracked_count > self.tracked_staged_count
3831            || self.new_count > self.new_staged_count
3832            || self.conflicted_count > self.conflicted_staged_count
3833    }
3834
3835    fn has_tracked_changes(&self) -> bool {
3836        self.tracked_count > 0
3837    }
3838
3839    pub fn has_unstaged_conflicts(&self) -> bool {
3840        self.conflicted_count > 0 && self.conflicted_count != self.conflicted_staged_count
3841    }
3842
3843    fn show_error_toast(&self, action: impl Into<SharedString>, e: anyhow::Error, cx: &mut App) {
3844        let Some(workspace) = self.workspace.upgrade() else {
3845            return;
3846        };
3847        show_error_toast(workspace, action, e, cx)
3848    }
3849
3850    fn show_commit_message_error<E>(weak_this: &WeakEntity<Self>, err: &E, cx: &mut AsyncApp)
3851    where
3852        E: std::fmt::Debug + std::fmt::Display,
3853    {
3854        if let Ok(Some(workspace)) = weak_this.update(cx, |this, _cx| this.workspace.upgrade()) {
3855            let _ = workspace.update(cx, |workspace, cx| {
3856                struct CommitMessageError;
3857                let notification_id = NotificationId::unique::<CommitMessageError>();
3858                workspace.show_notification(notification_id, cx, |cx| {
3859                    cx.new(|cx| {
3860                        ErrorMessagePrompt::new(
3861                            format!("Failed to generate commit message: {err}"),
3862                            cx,
3863                        )
3864                    })
3865                });
3866            });
3867        }
3868    }
3869
3870    fn show_remote_output(
3871        &mut self,
3872        action: RemoteAction,
3873        info: RemoteCommandOutput,
3874        cx: &mut Context<Self>,
3875    ) {
3876        let Some(workspace) = self.workspace.upgrade() else {
3877            return;
3878        };
3879
3880        workspace.update(cx, |workspace, cx| {
3881            let SuccessMessage { message, style } = remote_output::format_output(&action, info);
3882            let workspace_weak = cx.weak_entity();
3883            let operation = action.name();
3884
3885            let status_toast = StatusToast::new(message, cx, move |this, _cx| {
3886                use remote_output::SuccessStyle::*;
3887                match style {
3888                    Toast => this.icon(
3889                        Icon::new(IconName::GitBranch)
3890                            .size(IconSize::Small)
3891                            .color(Color::Muted),
3892                    ),
3893                    ToastWithLog { output } => this
3894                        .icon(
3895                            Icon::new(IconName::GitBranch)
3896                                .size(IconSize::Small)
3897                                .color(Color::Muted),
3898                        )
3899                        .action("View Log", move |window, cx| {
3900                            let output = output.clone();
3901                            let output =
3902                                format!("stdout:\n{}\nstderr:\n{}", output.stdout, output.stderr);
3903                            workspace_weak
3904                                .update(cx, move |workspace, cx| {
3905                                    open_output(operation, workspace, &output, window, cx)
3906                                })
3907                                .ok();
3908                        }),
3909                    PushPrLink { text, link } => this
3910                        .icon(
3911                            Icon::new(IconName::GitBranch)
3912                                .size(IconSize::Small)
3913                                .color(Color::Muted),
3914                        )
3915                        .action(text, move |_, cx| cx.open_url(&link)),
3916                }
3917                .dismiss_button(true)
3918            });
3919            workspace.toggle_status_toast(status_toast, cx)
3920        });
3921    }
3922
3923    pub fn can_commit(&self) -> bool {
3924        (self.has_staged_changes() || self.has_tracked_changes()) && !self.has_unstaged_conflicts()
3925    }
3926
3927    pub fn can_stage_all(&self) -> bool {
3928        self.has_unstaged_changes()
3929    }
3930
3931    pub fn can_unstage_all(&self) -> bool {
3932        self.has_staged_changes()
3933    }
3934
3935    /// Computes tree indentation depths for visible entries in the given range.
3936    /// Used by indent guides to render vertical connector lines in tree view.
3937    fn compute_visible_depths(&self, range: Range<usize>) -> SmallVec<[usize; 64]> {
3938        let GitPanelViewMode::Tree(state) = &self.view_mode else {
3939            return SmallVec::new();
3940        };
3941
3942        range
3943            .map(|ix| {
3944                state
3945                    .logical_indices
3946                    .get(ix)
3947                    .and_then(|&entry_ix| self.entries.get(entry_ix))
3948                    .map_or(0, |entry| entry.depth())
3949            })
3950            .collect()
3951    }
3952
3953    fn status_width_estimate(
3954        tree_view: bool,
3955        entry: &GitStatusEntry,
3956        path_style: PathStyle,
3957        depth: usize,
3958    ) -> usize {
3959        if tree_view {
3960            Self::item_width_estimate(0, entry.display_name(path_style).len(), depth)
3961        } else {
3962            Self::item_width_estimate(
3963                entry.parent_dir(path_style).map(|s| s.len()).unwrap_or(0),
3964                entry.display_name(path_style).len(),
3965                0,
3966            )
3967        }
3968    }
3969
3970    fn width_estimate_for_list_entry(
3971        &self,
3972        tree_view: bool,
3973        entry: &GitListEntry,
3974        path_style: PathStyle,
3975    ) -> Option<usize> {
3976        match entry {
3977            GitListEntry::Status(status) => Some(Self::status_width_estimate(
3978                tree_view, status, path_style, 0,
3979            )),
3980            GitListEntry::TreeStatus(status) => Some(Self::status_width_estimate(
3981                tree_view,
3982                &status.entry,
3983                path_style,
3984                status.depth,
3985            )),
3986            GitListEntry::Directory(dir) => {
3987                Some(Self::item_width_estimate(0, dir.name.len(), dir.depth))
3988            }
3989            GitListEntry::Header(_) => None,
3990        }
3991    }
3992
3993    fn item_width_estimate(path: usize, file_name: usize, depth: usize) -> usize {
3994        path + file_name + depth * 2
3995    }
3996
3997    fn render_overflow_menu(&self, id: impl Into<ElementId>) -> impl IntoElement {
3998        let focus_handle = self.focus_handle.clone();
3999        let has_tracked_changes = self.has_tracked_changes();
4000        let has_staged_changes = self.has_staged_changes();
4001        let has_unstaged_changes = self.has_unstaged_changes();
4002        let has_new_changes = self.new_count > 0;
4003        let has_stash_items = self.stash_entries.entries.len() > 0;
4004
4005        PopoverMenu::new(id.into())
4006            .trigger(
4007                IconButton::new("overflow-menu-trigger", IconName::Ellipsis)
4008                    .icon_size(IconSize::Small)
4009                    .icon_color(Color::Muted),
4010            )
4011            .menu(move |window, cx| {
4012                Some(git_panel_context_menu(
4013                    focus_handle.clone(),
4014                    GitMenuState {
4015                        has_tracked_changes,
4016                        has_staged_changes,
4017                        has_unstaged_changes,
4018                        has_new_changes,
4019                        sort_by_path: GitPanelSettings::get_global(cx).sort_by_path,
4020                        has_stash_items,
4021                        tree_view: GitPanelSettings::get_global(cx).tree_view,
4022                    },
4023                    window,
4024                    cx,
4025                ))
4026            })
4027            .anchor(Corner::TopRight)
4028    }
4029
4030    pub(crate) fn render_generate_commit_message_button(
4031        &self,
4032        cx: &Context<Self>,
4033    ) -> Option<AnyElement> {
4034        if !agent_settings::AgentSettings::get_global(cx).enabled(cx) {
4035            return None;
4036        }
4037
4038        if self.generate_commit_message_task.is_some() {
4039            return Some(
4040                h_flex()
4041                    .gap_1()
4042                    .child(
4043                        Icon::new(IconName::ArrowCircle)
4044                            .size(IconSize::XSmall)
4045                            .color(Color::Info)
4046                            .with_rotate_animation(2),
4047                    )
4048                    .child(
4049                        Label::new("Generating Commit…")
4050                            .size(LabelSize::Small)
4051                            .color(Color::Muted),
4052                    )
4053                    .into_any_element(),
4054            );
4055        }
4056
4057        let model_registry = LanguageModelRegistry::read_global(cx);
4058        let has_commit_model_configuration_error = model_registry
4059            .configuration_error(model_registry.commit_message_model(), cx)
4060            .is_some();
4061        let can_commit = self.can_commit();
4062
4063        let editor_focus_handle = self.commit_editor.focus_handle(cx);
4064
4065        Some(
4066            IconButton::new("generate-commit-message", IconName::AiEdit)
4067                .shape(ui::IconButtonShape::Square)
4068                .icon_color(if has_commit_model_configuration_error {
4069                    Color::Disabled
4070                } else {
4071                    Color::Muted
4072                })
4073                .tooltip(move |_window, cx| {
4074                    if !can_commit {
4075                        Tooltip::simple("No Changes to Commit", cx)
4076                    } else if has_commit_model_configuration_error {
4077                        Tooltip::simple("Configure an LLM provider to generate commit messages", cx)
4078                    } else {
4079                        Tooltip::for_action_in(
4080                            "Generate Commit Message",
4081                            &git::GenerateCommitMessage,
4082                            &editor_focus_handle,
4083                            cx,
4084                        )
4085                    }
4086                })
4087                .disabled(!can_commit || has_commit_model_configuration_error)
4088                .on_click(cx.listener(move |this, _event, _window, cx| {
4089                    this.generate_commit_message(cx);
4090                }))
4091                .into_any_element(),
4092        )
4093    }
4094
4095    pub(crate) fn render_co_authors(&self, cx: &Context<Self>) -> Option<AnyElement> {
4096        let potential_co_authors = self.potential_co_authors(cx);
4097
4098        let (tooltip_label, icon) = if self.add_coauthors {
4099            ("Remove co-authored-by", IconName::Person)
4100        } else {
4101            ("Add co-authored-by", IconName::UserCheck)
4102        };
4103
4104        if potential_co_authors.is_empty() {
4105            None
4106        } else {
4107            Some(
4108                IconButton::new("co-authors", icon)
4109                    .shape(ui::IconButtonShape::Square)
4110                    .icon_color(Color::Disabled)
4111                    .selected_icon_color(Color::Selected)
4112                    .toggle_state(self.add_coauthors)
4113                    .tooltip(move |_, cx| {
4114                        let title = format!(
4115                            "{}:{}{}",
4116                            tooltip_label,
4117                            if potential_co_authors.len() == 1 {
4118                                ""
4119                            } else {
4120                                "\n"
4121                            },
4122                            potential_co_authors
4123                                .iter()
4124                                .map(|(name, email)| format!(" {} <{}>", name, email))
4125                                .join("\n")
4126                        );
4127                        Tooltip::simple(title, cx)
4128                    })
4129                    .on_click(cx.listener(|this, _, _, cx| {
4130                        this.add_coauthors = !this.add_coauthors;
4131                        cx.notify();
4132                    }))
4133                    .into_any_element(),
4134            )
4135        }
4136    }
4137
4138    fn render_git_commit_menu(
4139        &self,
4140        id: impl Into<ElementId>,
4141        keybinding_target: Option<FocusHandle>,
4142        cx: &mut Context<Self>,
4143    ) -> impl IntoElement {
4144        PopoverMenu::new(id.into())
4145            .trigger(
4146                ui::ButtonLike::new_rounded_right("commit-split-button-right")
4147                    .layer(ui::ElevationIndex::ModalSurface)
4148                    .size(ButtonSize::None)
4149                    .child(
4150                        h_flex()
4151                            .px_1()
4152                            .h_full()
4153                            .justify_center()
4154                            .border_l_1()
4155                            .border_color(cx.theme().colors().border)
4156                            .child(Icon::new(IconName::ChevronDown).size(IconSize::XSmall)),
4157                    ),
4158            )
4159            .menu({
4160                let git_panel = cx.entity();
4161                let has_previous_commit = self.head_commit(cx).is_some();
4162                let amend = self.amend_pending();
4163                let signoff = self.signoff_enabled;
4164
4165                move |window, cx| {
4166                    Some(ContextMenu::build(window, cx, |context_menu, _, _| {
4167                        context_menu
4168                            .when_some(keybinding_target.clone(), |el, keybinding_target| {
4169                                el.context(keybinding_target)
4170                            })
4171                            .when(has_previous_commit, |this| {
4172                                this.toggleable_entry(
4173                                    "Amend",
4174                                    amend,
4175                                    IconPosition::Start,
4176                                    Some(Box::new(Amend)),
4177                                    {
4178                                        let git_panel = git_panel.downgrade();
4179                                        move |_, cx| {
4180                                            git_panel
4181                                                .update(cx, |git_panel, cx| {
4182                                                    git_panel.toggle_amend_pending(cx);
4183                                                })
4184                                                .ok();
4185                                        }
4186                                    },
4187                                )
4188                            })
4189                            .toggleable_entry(
4190                                "Signoff",
4191                                signoff,
4192                                IconPosition::Start,
4193                                Some(Box::new(Signoff)),
4194                                move |window, cx| window.dispatch_action(Box::new(Signoff), cx),
4195                            )
4196                    }))
4197                }
4198            })
4199            .anchor(Corner::TopRight)
4200    }
4201
4202    pub fn configure_commit_button(&self, cx: &mut Context<Self>) -> (bool, &'static str) {
4203        if self.has_unstaged_conflicts() {
4204            (false, "You must resolve conflicts before committing")
4205        } else if !self.has_staged_changes() && !self.has_tracked_changes() && !self.amend_pending {
4206            (false, "No changes to commit")
4207        } else if self.pending_commit.is_some() {
4208            (false, "Commit in progress")
4209        } else if !self.has_commit_message(cx) {
4210            (false, "No commit message")
4211        } else if !self.has_write_access(cx) {
4212            (false, "You do not have write access to this project")
4213        } else {
4214            (true, self.commit_button_title())
4215        }
4216    }
4217
4218    pub fn commit_button_title(&self) -> &'static str {
4219        if self.amend_pending {
4220            if self.has_staged_changes() {
4221                "Amend"
4222            } else if self.has_tracked_changes() {
4223                "Amend Tracked"
4224            } else {
4225                "Amend"
4226            }
4227        } else if self.has_staged_changes() {
4228            "Commit"
4229        } else {
4230            "Commit Tracked"
4231        }
4232    }
4233
4234    fn expand_commit_editor(
4235        &mut self,
4236        _: &git::ExpandCommitEditor,
4237        window: &mut Window,
4238        cx: &mut Context<Self>,
4239    ) {
4240        let workspace = self.workspace.clone();
4241        window.defer(cx, move |window, cx| {
4242            workspace
4243                .update(cx, |workspace, cx| {
4244                    CommitModal::toggle(workspace, None, window, cx)
4245                })
4246                .ok();
4247        })
4248    }
4249
4250    fn render_panel_header(
4251        &self,
4252        window: &mut Window,
4253        cx: &mut Context<Self>,
4254    ) -> Option<impl IntoElement> {
4255        self.active_repository.as_ref()?;
4256
4257        let (text, action, stage, tooltip) =
4258            if self.total_staged_count() == self.entry_count && self.entry_count > 0 {
4259                ("Unstage All", UnstageAll.boxed_clone(), false, "git reset")
4260            } else {
4261                ("Stage All", StageAll.boxed_clone(), true, "git add --all")
4262            };
4263
4264        let change_string = match self.changes_count {
4265            0 => "No Changes".to_string(),
4266            1 => "1 Change".to_string(),
4267            count => format!("{} Changes", count),
4268        };
4269
4270        Some(
4271            self.panel_header_container(window, cx)
4272                .px_2()
4273                .justify_between()
4274                .child(
4275                    panel_button(change_string)
4276                        .color(Color::Muted)
4277                        .tooltip(Tooltip::for_action_title_in(
4278                            "Open Diff",
4279                            &Diff,
4280                            &self.focus_handle,
4281                        ))
4282                        .on_click(|_, _, cx| {
4283                            cx.defer(|cx| {
4284                                cx.dispatch_action(&Diff);
4285                            })
4286                        }),
4287                )
4288                .child(
4289                    h_flex()
4290                        .gap_1()
4291                        .child(self.render_overflow_menu("overflow_menu"))
4292                        .child(
4293                            panel_filled_button(text)
4294                                .tooltip(Tooltip::for_action_title_in(
4295                                    tooltip,
4296                                    action.as_ref(),
4297                                    &self.focus_handle,
4298                                ))
4299                                .disabled(self.entry_count == 0)
4300                                .on_click({
4301                                    let git_panel = cx.weak_entity();
4302                                    move |_, _, cx| {
4303                                        git_panel
4304                                            .update(cx, |git_panel, cx| {
4305                                                git_panel.change_all_files_stage(stage, cx);
4306                                            })
4307                                            .ok();
4308                                    }
4309                                }),
4310                        ),
4311                ),
4312        )
4313    }
4314
4315    pub(crate) fn render_remote_button(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
4316        let branch = self.active_repository.as_ref()?.read(cx).branch.clone();
4317        if !self.can_push_and_pull(cx) {
4318            return None;
4319        }
4320        Some(
4321            h_flex()
4322                .gap_1()
4323                .flex_shrink_0()
4324                .when_some(branch, |this, branch| {
4325                    let focus_handle = Some(self.focus_handle(cx));
4326
4327                    this.children(render_remote_button(
4328                        "remote-button",
4329                        &branch,
4330                        focus_handle,
4331                        true,
4332                    ))
4333                })
4334                .into_any_element(),
4335        )
4336    }
4337
4338    pub fn render_footer(
4339        &self,
4340        window: &mut Window,
4341        cx: &mut Context<Self>,
4342    ) -> Option<impl IntoElement> {
4343        let active_repository = self.active_repository.clone()?;
4344        let panel_editor_style = panel_editor_style(true, window, cx);
4345        let enable_coauthors = self.render_co_authors(cx);
4346
4347        let editor_focus_handle = self.commit_editor.focus_handle(cx);
4348        let expand_tooltip_focus_handle = editor_focus_handle;
4349
4350        let branch = active_repository.read(cx).branch.clone();
4351        let head_commit = active_repository.read(cx).head_commit.clone();
4352
4353        let footer_size = px(32.);
4354        let gap = px(9.0);
4355        let max_height = panel_editor_style
4356            .text
4357            .line_height_in_pixels(window.rem_size())
4358            * MAX_PANEL_EDITOR_LINES
4359            + gap;
4360
4361        let git_panel = cx.entity();
4362        let display_name = SharedString::from(Arc::from(
4363            active_repository
4364                .read(cx)
4365                .display_name()
4366                .trim_end_matches("/"),
4367        ));
4368        let editor_is_long = self.commit_editor.update(cx, |editor, cx| {
4369            editor.max_point(cx).row().0 >= MAX_PANEL_EDITOR_LINES as u32
4370        });
4371
4372        let footer = v_flex()
4373            .child(PanelRepoFooter::new(
4374                display_name,
4375                branch,
4376                head_commit,
4377                Some(git_panel),
4378            ))
4379            .child(
4380                panel_editor_container(window, cx)
4381                    .id("commit-editor-container")
4382                    .relative()
4383                    .w_full()
4384                    .h(max_height + footer_size)
4385                    .border_t_1()
4386                    .border_color(cx.theme().colors().border)
4387                    .cursor_text()
4388                    .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| {
4389                        window.focus(&this.commit_editor.focus_handle(cx), cx);
4390                    }))
4391                    .child(
4392                        h_flex()
4393                            .id("commit-footer")
4394                            .border_t_1()
4395                            .when(editor_is_long, |el| {
4396                                el.border_color(cx.theme().colors().border_variant)
4397                            })
4398                            .absolute()
4399                            .bottom_0()
4400                            .left_0()
4401                            .w_full()
4402                            .px_2()
4403                            .h(footer_size)
4404                            .flex_none()
4405                            .justify_between()
4406                            .child(
4407                                self.render_generate_commit_message_button(cx)
4408                                    .unwrap_or_else(|| div().into_any_element()),
4409                            )
4410                            .child(
4411                                h_flex()
4412                                    .gap_0p5()
4413                                    .children(enable_coauthors)
4414                                    .child(self.render_commit_button(cx)),
4415                            ),
4416                    )
4417                    .child(
4418                        div()
4419                            .pr_2p5()
4420                            .on_action(|&zed_actions::editor::MoveUp, _, cx| {
4421                                cx.stop_propagation();
4422                            })
4423                            .on_action(|&zed_actions::editor::MoveDown, _, cx| {
4424                                cx.stop_propagation();
4425                            })
4426                            .child(EditorElement::new(&self.commit_editor, panel_editor_style)),
4427                    )
4428                    .child(
4429                        h_flex()
4430                            .absolute()
4431                            .top_2()
4432                            .right_2()
4433                            .opacity(0.5)
4434                            .hover(|this| this.opacity(1.0))
4435                            .child(
4436                                panel_icon_button("expand-commit-editor", IconName::Maximize)
4437                                    .icon_size(IconSize::Small)
4438                                    .size(ui::ButtonSize::Default)
4439                                    .tooltip(move |_window, cx| {
4440                                        Tooltip::for_action_in(
4441                                            "Open Commit Modal",
4442                                            &git::ExpandCommitEditor,
4443                                            &expand_tooltip_focus_handle,
4444                                            cx,
4445                                        )
4446                                    })
4447                                    .on_click(cx.listener({
4448                                        move |_, _, window, cx| {
4449                                            window.dispatch_action(
4450                                                git::ExpandCommitEditor.boxed_clone(),
4451                                                cx,
4452                                            )
4453                                        }
4454                                    })),
4455                            ),
4456                    ),
4457            );
4458
4459        Some(footer)
4460    }
4461
4462    fn render_commit_button(&self, cx: &mut Context<Self>) -> impl IntoElement {
4463        let (can_commit, tooltip) = self.configure_commit_button(cx);
4464        let title = self.commit_button_title();
4465        let commit_tooltip_focus_handle = self.commit_editor.focus_handle(cx);
4466        let amend = self.amend_pending();
4467        let signoff = self.signoff_enabled;
4468
4469        let label_color = if self.pending_commit.is_some() {
4470            Color::Disabled
4471        } else {
4472            Color::Default
4473        };
4474
4475        div()
4476            .id("commit-wrapper")
4477            .on_hover(cx.listener(move |this, hovered, _, cx| {
4478                this.show_placeholders =
4479                    *hovered && !this.has_staged_changes() && !this.has_unstaged_conflicts();
4480                cx.notify()
4481            }))
4482            .child(SplitButton::new(
4483                ButtonLike::new_rounded_left(ElementId::Name(
4484                    format!("split-button-left-{}", title).into(),
4485                ))
4486                .layer(ElevationIndex::ModalSurface)
4487                .size(ButtonSize::Compact)
4488                .child(
4489                    Label::new(title)
4490                        .size(LabelSize::Small)
4491                        .color(label_color)
4492                        .mr_0p5(),
4493                )
4494                .on_click({
4495                    let git_panel = cx.weak_entity();
4496                    move |_, window, cx| {
4497                        telemetry::event!("Git Committed", source = "Git Panel");
4498                        git_panel
4499                            .update(cx, |git_panel, cx| {
4500                                git_panel.commit_changes(
4501                                    CommitOptions {
4502                                        amend,
4503                                        signoff,
4504                                        allow_empty: false,
4505                                    },
4506                                    window,
4507                                    cx,
4508                                );
4509                            })
4510                            .ok();
4511                    }
4512                })
4513                .disabled(!can_commit || self.modal_open)
4514                .tooltip({
4515                    let handle = commit_tooltip_focus_handle.clone();
4516                    move |_window, cx| {
4517                        if can_commit {
4518                            Tooltip::with_meta_in(
4519                                tooltip,
4520                                Some(if amend { &git::Amend } else { &git::Commit }),
4521                                format!(
4522                                    "git commit{}{}",
4523                                    if amend { " --amend" } else { "" },
4524                                    if signoff { " --signoff" } else { "" }
4525                                ),
4526                                &handle.clone(),
4527                                cx,
4528                            )
4529                        } else {
4530                            Tooltip::simple(tooltip, cx)
4531                        }
4532                    }
4533                }),
4534                self.render_git_commit_menu(
4535                    ElementId::Name(format!("split-button-right-{}", title).into()),
4536                    Some(commit_tooltip_focus_handle),
4537                    cx,
4538                )
4539                .into_any_element(),
4540            ))
4541    }
4542
4543    fn render_pending_amend(&self, cx: &mut Context<Self>) -> impl IntoElement {
4544        h_flex()
4545            .py_1p5()
4546            .px_2()
4547            .gap_1p5()
4548            .justify_between()
4549            .border_t_1()
4550            .border_color(cx.theme().colors().border.opacity(0.8))
4551            .child(
4552                div()
4553                    .flex_grow()
4554                    .overflow_hidden()
4555                    .max_w(relative(0.85))
4556                    .child(
4557                        Label::new("This will update your most recent commit.")
4558                            .size(LabelSize::Small)
4559                            .truncate(),
4560                    ),
4561            )
4562            .child(
4563                panel_button("Cancel")
4564                    .size(ButtonSize::Default)
4565                    .on_click(cx.listener(|this, _, _, cx| this.set_amend_pending(false, cx))),
4566            )
4567    }
4568
4569    fn render_previous_commit(
4570        &self,
4571        _window: &mut Window,
4572        cx: &mut Context<Self>,
4573    ) -> Option<impl IntoElement> {
4574        let active_repository = self.active_repository.as_ref()?;
4575        let branch = active_repository.read(cx).branch.as_ref()?;
4576        let commit = branch.most_recent_commit.as_ref()?.clone();
4577        let workspace = self.workspace.clone();
4578        let this = cx.entity();
4579
4580        Some(
4581            h_flex()
4582                .p_1p5()
4583                .gap_1p5()
4584                .justify_between()
4585                .border_t_1()
4586                .border_color(cx.theme().colors().border.opacity(0.8))
4587                .child(
4588                    div()
4589                        .id("commit-msg-hover")
4590                        .cursor_pointer()
4591                        .px_1()
4592                        .rounded_sm()
4593                        .line_clamp(1)
4594                        .hover(|s| s.bg(cx.theme().colors().element_hover))
4595                        .child(
4596                            Label::new(commit.subject.clone())
4597                                .size(LabelSize::Small)
4598                                .truncate(),
4599                        )
4600                        .on_click({
4601                            let commit = commit.clone();
4602                            let repo = active_repository.downgrade();
4603                            move |_, window, cx| {
4604                                CommitView::open(
4605                                    commit.sha.to_string(),
4606                                    repo.clone(),
4607                                    workspace.clone(),
4608                                    None,
4609                                    None,
4610                                    window,
4611                                    cx,
4612                                );
4613                            }
4614                        })
4615                        .hoverable_tooltip({
4616                            let repo = active_repository.clone();
4617                            move |window, cx| {
4618                                GitPanelMessageTooltip::new(
4619                                    this.clone(),
4620                                    commit.sha.clone(),
4621                                    repo.clone(),
4622                                    window,
4623                                    cx,
4624                                )
4625                                .into()
4626                            }
4627                        }),
4628                )
4629                .child(
4630                    h_flex()
4631                        .gap_0p5()
4632                        .when(commit.has_parent, |this| {
4633                            let has_unstaged = self.has_unstaged_changes();
4634                            this.child(
4635                                panel_icon_button("undo", IconName::Undo)
4636                                    .icon_size(IconSize::Small)
4637                                    .tooltip(move |_window, cx| {
4638                                        Tooltip::with_meta(
4639                                            "Uncommit",
4640                                            Some(&git::Uncommit),
4641                                            if has_unstaged {
4642                                                "git reset HEAD^ --soft"
4643                                            } else {
4644                                                "git reset HEAD^"
4645                                            },
4646                                            cx,
4647                                        )
4648                                    })
4649                                    .on_click(
4650                                        cx.listener(|this, _, window, cx| {
4651                                            this.uncommit(window, cx)
4652                                        }),
4653                                    ),
4654                            )
4655                        })
4656                        .child(
4657                            panel_icon_button("git-graph-button", IconName::GitGraph)
4658                                .icon_size(IconSize::Small)
4659                                .tooltip(|_window, cx| {
4660                                    Tooltip::for_action("Open Git Graph", &Open, cx)
4661                                })
4662                                .on_click(|_, window, cx| {
4663                                    window.dispatch_action(Open.boxed_clone(), cx)
4664                                }),
4665                        ),
4666                ),
4667        )
4668    }
4669
4670    fn render_empty_state(&self, cx: &mut Context<Self>) -> impl IntoElement {
4671        let has_repo = self.active_repository.is_some();
4672        let has_no_repo = self.active_repository.is_none();
4673        let worktree_count = self.project.read(cx).visible_worktrees(cx).count();
4674
4675        let should_show_branch_diff =
4676            has_repo && self.changes_count == 0 && !self.is_on_main_branch(cx);
4677
4678        let label = if has_repo {
4679            "No changes to commit"
4680        } else {
4681            "No Git repositories"
4682        };
4683
4684        v_flex()
4685            .gap_1p5()
4686            .flex_1()
4687            .items_center()
4688            .justify_center()
4689            .child(Label::new(label).size(LabelSize::Small).color(Color::Muted))
4690            .when(has_no_repo && worktree_count > 0, |this| {
4691                this.child(
4692                    panel_filled_button("Initialize Repository")
4693                        .tooltip(Tooltip::for_action_title_in(
4694                            "git init",
4695                            &git::Init,
4696                            &self.focus_handle,
4697                        ))
4698                        .on_click(move |_, _, cx| {
4699                            cx.defer(move |cx| {
4700                                cx.dispatch_action(&git::Init);
4701                            })
4702                        }),
4703                )
4704            })
4705            .when(should_show_branch_diff, |this| {
4706                this.child(
4707                    panel_filled_button("View Branch Diff")
4708                        .tooltip(move |_, cx| {
4709                            Tooltip::with_meta(
4710                                "Branch Diff",
4711                                Some(&BranchDiff),
4712                                "Show diff between working directory and default branch",
4713                                cx,
4714                            )
4715                        })
4716                        .on_click(move |_, _, cx| {
4717                            cx.defer(move |cx| {
4718                                cx.dispatch_action(&BranchDiff);
4719                            })
4720                        }),
4721                )
4722            })
4723    }
4724
4725    fn is_on_main_branch(&self, cx: &Context<Self>) -> bool {
4726        let Some(repo) = self.active_repository.as_ref() else {
4727            return false;
4728        };
4729
4730        let Some(branch) = repo.read(cx).branch.as_ref() else {
4731            return false;
4732        };
4733
4734        let branch_name = branch.name();
4735        matches!(branch_name, "main" | "master")
4736    }
4737
4738    fn render_buffer_header_controls(
4739        &self,
4740        entity: &Entity<Self>,
4741        file: &Arc<dyn File>,
4742        _: &Window,
4743        cx: &App,
4744    ) -> Option<AnyElement> {
4745        let repo = self.active_repository.as_ref()?.read(cx);
4746        let project_path = (file.worktree_id(cx), file.path().clone()).into();
4747        let repo_path = repo.project_path_to_repo_path(&project_path, cx)?;
4748        let ix = self.entry_by_path(&repo_path)?;
4749        let entry = self.entries.get(ix)?;
4750
4751        let is_staging_or_staged = repo
4752            .pending_ops_for_path(&repo_path)
4753            .map(|ops| ops.staging() || ops.staged())
4754            .or_else(|| {
4755                repo.status_for_path(&repo_path)
4756                    .and_then(|status| status.status.staging().as_bool())
4757            })
4758            .or_else(|| {
4759                entry
4760                    .status_entry()
4761                    .and_then(|entry| entry.staging.as_bool())
4762            });
4763
4764        let checkbox = Checkbox::new("stage-file", is_staging_or_staged.into())
4765            .disabled(!self.has_write_access(cx))
4766            .fill()
4767            .elevation(ElevationIndex::Surface)
4768            .on_click({
4769                let entry = entry.clone();
4770                let git_panel = entity.downgrade();
4771                move |_, window, cx| {
4772                    git_panel
4773                        .update(cx, |this, cx| {
4774                            this.toggle_staged_for_entry(&entry, window, cx);
4775                            cx.stop_propagation();
4776                        })
4777                        .ok();
4778                }
4779            });
4780        Some(
4781            h_flex()
4782                .id("start-slot")
4783                .text_lg()
4784                .child(checkbox)
4785                .on_mouse_down(MouseButton::Left, |_, _, cx| {
4786                    // prevent the list item active state triggering when toggling checkbox
4787                    cx.stop_propagation();
4788                })
4789                .into_any_element(),
4790        )
4791    }
4792
4793    fn render_entries(
4794        &self,
4795        has_write_access: bool,
4796        repo: Entity<Repository>,
4797        window: &mut Window,
4798        cx: &mut Context<Self>,
4799    ) -> impl IntoElement {
4800        let (is_tree_view, entry_count) = match &self.view_mode {
4801            GitPanelViewMode::Tree(state) => (true, state.logical_indices.len()),
4802            GitPanelViewMode::Flat => (false, self.entries.len()),
4803        };
4804        let repo = repo.downgrade();
4805
4806        v_flex()
4807            .flex_1()
4808            .size_full()
4809            .overflow_hidden()
4810            .relative()
4811            .child(
4812                h_flex()
4813                    .flex_1()
4814                    .size_full()
4815                    .relative()
4816                    .overflow_hidden()
4817                    .child(
4818                        uniform_list(
4819                            "entries",
4820                            entry_count,
4821                            cx.processor(move |this, range: Range<usize>, window, cx| {
4822                                let Some(repo) = repo.upgrade() else {
4823                                    return Vec::new();
4824                                };
4825                                let repo = repo.read(cx);
4826
4827                                let mut items = Vec::with_capacity(range.end - range.start);
4828
4829                                for ix in range.into_iter().map(|ix| match &this.view_mode {
4830                                    GitPanelViewMode::Tree(state) => state.logical_indices[ix],
4831                                    GitPanelViewMode::Flat => ix,
4832                                }) {
4833                                    match &this.entries.get(ix) {
4834                                        Some(GitListEntry::Status(entry)) => {
4835                                            items.push(this.render_status_entry(
4836                                                ix,
4837                                                entry,
4838                                                0,
4839                                                has_write_access,
4840                                                repo,
4841                                                window,
4842                                                cx,
4843                                            ));
4844                                        }
4845                                        Some(GitListEntry::TreeStatus(entry)) => {
4846                                            items.push(this.render_status_entry(
4847                                                ix,
4848                                                &entry.entry,
4849                                                entry.depth,
4850                                                has_write_access,
4851                                                repo,
4852                                                window,
4853                                                cx,
4854                                            ));
4855                                        }
4856                                        Some(GitListEntry::Directory(entry)) => {
4857                                            items.push(this.render_directory_entry(
4858                                                ix,
4859                                                entry,
4860                                                has_write_access,
4861                                                window,
4862                                                cx,
4863                                            ));
4864                                        }
4865                                        Some(GitListEntry::Header(header)) => {
4866                                            items.push(this.render_list_header(
4867                                                ix,
4868                                                header,
4869                                                has_write_access,
4870                                                window,
4871                                                cx,
4872                                            ));
4873                                        }
4874                                        None => {}
4875                                    }
4876                                }
4877
4878                                items
4879                            }),
4880                        )
4881                        .when(is_tree_view, |list| {
4882                            let indent_size = px(TREE_INDENT);
4883                            list.with_decoration(
4884                                ui::indent_guides(indent_size, IndentGuideColors::panel(cx))
4885                                    .with_compute_indents_fn(
4886                                        cx.entity(),
4887                                        |this, range, _window, _cx| {
4888                                            this.compute_visible_depths(range)
4889                                        },
4890                                    )
4891                                    .with_render_fn(cx.entity(), |_, params, _, _| {
4892                                        // Magic number to align the tree item is 3 here
4893                                        // because we're using 12px as the left-side padding
4894                                        // and 3 makes the alignment work with the bounding box of the icon
4895                                        let left_offset = px(TREE_INDENT + 3_f32);
4896                                        let indent_size = params.indent_size;
4897                                        let item_height = params.item_height;
4898
4899                                        params
4900                                            .indent_guides
4901                                            .into_iter()
4902                                            .map(|layout| {
4903                                                let bounds = Bounds::new(
4904                                                    point(
4905                                                        layout.offset.x * indent_size + left_offset,
4906                                                        layout.offset.y * item_height,
4907                                                    ),
4908                                                    size(px(1.), layout.length * item_height),
4909                                                );
4910                                                RenderedIndentGuide {
4911                                                    bounds,
4912                                                    layout,
4913                                                    is_active: false,
4914                                                    hitbox: None,
4915                                                }
4916                                            })
4917                                            .collect()
4918                                    }),
4919                            )
4920                        })
4921                        .size_full()
4922                        .flex_grow()
4923                        .with_width_from_item(self.max_width_item_index)
4924                        .track_scroll(&self.scroll_handle),
4925                    )
4926                    .on_mouse_down(
4927                        MouseButton::Right,
4928                        cx.listener(move |this, event: &MouseDownEvent, window, cx| {
4929                            this.deploy_panel_context_menu(event.position, window, cx)
4930                        }),
4931                    )
4932                    .custom_scrollbars(
4933                        Scrollbars::for_settings::<GitPanelScrollbarAccessor>()
4934                            .tracked_scroll_handle(&self.scroll_handle)
4935                            .with_track_along(
4936                                ScrollAxes::Horizontal,
4937                                cx.theme().colors().panel_background,
4938                            ),
4939                        window,
4940                        cx,
4941                    ),
4942            )
4943    }
4944
4945    fn entry_label(&self, label: impl Into<SharedString>, color: Color) -> Label {
4946        Label::new(label.into()).color(color)
4947    }
4948
4949    fn list_item_height(&self) -> Rems {
4950        rems(1.75)
4951    }
4952
4953    fn render_list_header(
4954        &self,
4955        ix: usize,
4956        header: &GitHeaderEntry,
4957        _: bool,
4958        _: &Window,
4959        _: &Context<Self>,
4960    ) -> AnyElement {
4961        let id: ElementId = ElementId::Name(format!("header_{}", ix).into());
4962
4963        h_flex()
4964            .id(id)
4965            .h(self.list_item_height())
4966            .w_full()
4967            .items_end()
4968            .px_3()
4969            .pb_1()
4970            .child(
4971                Label::new(header.title())
4972                    .color(Color::Muted)
4973                    .size(LabelSize::Small)
4974                    .line_height_style(LineHeightStyle::UiLabel)
4975                    .single_line(),
4976            )
4977            .into_any_element()
4978    }
4979
4980    pub fn load_commit_details(
4981        &self,
4982        sha: String,
4983        cx: &mut Context<Self>,
4984    ) -> Task<anyhow::Result<CommitDetails>> {
4985        let Some(repo) = self.active_repository.clone() else {
4986            return Task::ready(Err(anyhow::anyhow!("no active repo")));
4987        };
4988        repo.update(cx, |repo, cx| {
4989            let show = repo.show(sha);
4990            cx.spawn(async move |_, _| show.await?)
4991        })
4992    }
4993
4994    fn deploy_entry_context_menu(
4995        &mut self,
4996        position: Point<Pixels>,
4997        ix: usize,
4998        window: &mut Window,
4999        cx: &mut Context<Self>,
5000    ) {
5001        let Some(entry) = self.entries.get(ix).and_then(|e| e.status_entry()) else {
5002            return;
5003        };
5004        let stage_title = if entry.status.staging().is_fully_staged() {
5005            "Unstage File"
5006        } else {
5007            "Stage File"
5008        };
5009        let restore_title = if entry.status.is_created() {
5010            "Trash File"
5011        } else {
5012            "Discard Changes"
5013        };
5014        let context_menu = ContextMenu::build(window, cx, |context_menu, _, _| {
5015            let is_created = entry.status.is_created();
5016            context_menu
5017                .context(self.focus_handle.clone())
5018                .action(stage_title, ToggleStaged.boxed_clone())
5019                .action(restore_title, git::RestoreFile::default().boxed_clone())
5020                .action_disabled_when(
5021                    !is_created,
5022                    "Add to .gitignore",
5023                    git::AddToGitignore.boxed_clone(),
5024                )
5025                .separator()
5026                .action("Open Diff", menu::Confirm.boxed_clone())
5027                .action("Open File", menu::SecondaryConfirm.boxed_clone())
5028                .separator()
5029                .action_disabled_when(is_created, "View File History", Box::new(git::FileHistory))
5030        });
5031        self.selected_entry = Some(ix);
5032        self.set_context_menu(context_menu, position, window, cx);
5033    }
5034
5035    fn deploy_panel_context_menu(
5036        &mut self,
5037        position: Point<Pixels>,
5038        window: &mut Window,
5039        cx: &mut Context<Self>,
5040    ) {
5041        let context_menu = git_panel_context_menu(
5042            self.focus_handle.clone(),
5043            GitMenuState {
5044                has_tracked_changes: self.has_tracked_changes(),
5045                has_staged_changes: self.has_staged_changes(),
5046                has_unstaged_changes: self.has_unstaged_changes(),
5047                has_new_changes: self.new_count > 0,
5048                sort_by_path: GitPanelSettings::get_global(cx).sort_by_path,
5049                has_stash_items: self.stash_entries.entries.len() > 0,
5050                tree_view: GitPanelSettings::get_global(cx).tree_view,
5051            },
5052            window,
5053            cx,
5054        );
5055        self.set_context_menu(context_menu, position, window, cx);
5056    }
5057
5058    fn set_context_menu(
5059        &mut self,
5060        context_menu: Entity<ContextMenu>,
5061        position: Point<Pixels>,
5062        window: &Window,
5063        cx: &mut Context<Self>,
5064    ) {
5065        let subscription = cx.subscribe_in(
5066            &context_menu,
5067            window,
5068            |this, _, _: &DismissEvent, window, cx| {
5069                if this.context_menu.as_ref().is_some_and(|context_menu| {
5070                    context_menu.0.focus_handle(cx).contains_focused(window, cx)
5071                }) {
5072                    cx.focus_self(window);
5073                }
5074                this.context_menu.take();
5075                cx.notify();
5076            },
5077        );
5078        self.context_menu = Some((context_menu, position, subscription));
5079        cx.notify();
5080    }
5081
5082    fn render_status_entry(
5083        &self,
5084        ix: usize,
5085        entry: &GitStatusEntry,
5086        depth: usize,
5087        has_write_access: bool,
5088        repo: &Repository,
5089        window: &Window,
5090        cx: &Context<Self>,
5091    ) -> AnyElement {
5092        let settings = GitPanelSettings::get_global(cx);
5093        let tree_view = settings.tree_view;
5094        let path_style = self.project.read(cx).path_style(cx);
5095        let git_path_style = ProjectSettings::get_global(cx).git.path_style;
5096        let display_name = entry.display_name(path_style);
5097
5098        let selected = self.selected_entry == Some(ix);
5099        let marked = self.marked_entries.contains(&ix);
5100        let status_style = settings.status_style;
5101        let status = entry.status;
5102        let file_icon = if settings.file_icons {
5103            FileIcons::get_icon(entry.repo_path.as_std_path(), cx)
5104        } else {
5105            None
5106        };
5107
5108        let has_conflict = status.is_conflicted();
5109        let is_modified = status.is_modified();
5110        let is_deleted = status.is_deleted();
5111        let is_created = status.is_created();
5112
5113        let label_color = if status_style == StatusStyle::LabelColor {
5114            if has_conflict {
5115                Color::VersionControlConflict
5116            } else if is_created {
5117                Color::VersionControlAdded
5118            } else if is_modified {
5119                Color::VersionControlModified
5120            } else if is_deleted {
5121                // We don't want a bunch of red labels in the list
5122                Color::Disabled
5123            } else {
5124                Color::VersionControlAdded
5125            }
5126        } else {
5127            Color::Default
5128        };
5129
5130        let path_color = if status.is_deleted() {
5131            Color::Disabled
5132        } else {
5133            Color::Muted
5134        };
5135
5136        let id: ElementId = ElementId::Name(format!("entry_{}_{}", display_name, ix).into());
5137        let checkbox_wrapper_id: ElementId =
5138            ElementId::Name(format!("entry_{}_{}_checkbox_wrapper", display_name, ix).into());
5139        let checkbox_id: ElementId =
5140            ElementId::Name(format!("entry_{}_{}_checkbox", display_name, ix).into());
5141
5142        let stage_status = GitPanel::stage_status_for_entry(entry, &repo);
5143        let mut is_staged: ToggleState = match stage_status {
5144            StageStatus::Staged => ToggleState::Selected,
5145            StageStatus::Unstaged => ToggleState::Unselected,
5146            StageStatus::PartiallyStaged => ToggleState::Indeterminate,
5147        };
5148        if self.show_placeholders && !self.has_staged_changes() && !entry.status.is_created() {
5149            is_staged = ToggleState::Selected;
5150        }
5151
5152        let handle = cx.weak_entity();
5153
5154        let selected_bg_alpha = 0.08;
5155        let marked_bg_alpha = 0.12;
5156        let state_opacity_step = 0.04;
5157
5158        let info_color = cx.theme().status().info;
5159
5160        let base_bg = match (selected, marked) {
5161            (true, true) => info_color.alpha(selected_bg_alpha + marked_bg_alpha),
5162            (true, false) => info_color.alpha(selected_bg_alpha),
5163            (false, true) => info_color.alpha(marked_bg_alpha),
5164            _ => cx.theme().colors().ghost_element_background,
5165        };
5166
5167        let (hover_bg, active_bg) = if selected {
5168            (
5169                info_color.alpha(selected_bg_alpha + state_opacity_step),
5170                info_color.alpha(selected_bg_alpha + state_opacity_step * 2.0),
5171            )
5172        } else {
5173            (
5174                cx.theme().colors().ghost_element_hover,
5175                cx.theme().colors().ghost_element_active,
5176            )
5177        };
5178
5179        let name_row = h_flex()
5180            .min_w_0()
5181            .flex_1()
5182            .gap_1()
5183            .when(settings.file_icons, |this| {
5184                this.child(
5185                    file_icon
5186                        .map(|file_icon| {
5187                            Icon::from_path(file_icon)
5188                                .size(IconSize::Small)
5189                                .color(Color::Muted)
5190                        })
5191                        .unwrap_or_else(|| {
5192                            Icon::new(IconName::File)
5193                                .size(IconSize::Small)
5194                                .color(Color::Muted)
5195                        }),
5196                )
5197            })
5198            .when(status_style != StatusStyle::LabelColor, |el| {
5199                el.child(git_status_icon(status))
5200            })
5201            .map(|this| {
5202                if tree_view {
5203                    this.pl(px(depth as f32 * TREE_INDENT)).child(
5204                        self.entry_label(display_name, label_color)
5205                            .when(status.is_deleted(), Label::strikethrough)
5206                            .truncate(),
5207                    )
5208                } else {
5209                    this.child(self.path_formatted(
5210                        entry.parent_dir(path_style),
5211                        path_color,
5212                        display_name,
5213                        label_color,
5214                        path_style,
5215                        git_path_style,
5216                        status.is_deleted(),
5217                    ))
5218                }
5219            });
5220
5221        let id_for_diff_stat = id.clone();
5222
5223        h_flex()
5224            .id(id)
5225            .h(self.list_item_height())
5226            .w_full()
5227            .pl_3()
5228            .pr_1()
5229            .gap_1p5()
5230            .border_1()
5231            .border_r_2()
5232            .when(selected && self.focus_handle.is_focused(window), |el| {
5233                el.border_color(cx.theme().colors().panel_focused_border)
5234            })
5235            .bg(base_bg)
5236            .hover(|s| s.bg(hover_bg))
5237            .active(|s| s.bg(active_bg))
5238            .child(name_row)
5239            .when(GitPanelSettings::get_global(cx).diff_stats, |el| {
5240                el.when_some(entry.diff_stat, move |this, stat| {
5241                    let id = format!("diff-stat-{}", id_for_diff_stat);
5242                    this.child(ui::DiffStat::new(
5243                        id,
5244                        stat.added as usize,
5245                        stat.deleted as usize,
5246                    ))
5247                })
5248            })
5249            .child(
5250                div()
5251                    .id(checkbox_wrapper_id)
5252                    .flex_none()
5253                    .occlude()
5254                    .cursor_pointer()
5255                    .child(
5256                        Checkbox::new(checkbox_id, is_staged)
5257                            .disabled(!has_write_access)
5258                            .fill()
5259                            .elevation(ElevationIndex::Surface)
5260                            .on_click_ext({
5261                                let entry = entry.clone();
5262                                let this = cx.weak_entity();
5263                                move |_, click, window, cx| {
5264                                    this.update(cx, |this, cx| {
5265                                        if !has_write_access {
5266                                            return;
5267                                        }
5268                                        if click.modifiers().shift {
5269                                            this.stage_bulk(ix, cx);
5270                                        } else {
5271                                            let list_entry =
5272                                                if GitPanelSettings::get_global(cx).tree_view {
5273                                                    GitListEntry::TreeStatus(GitTreeStatusEntry {
5274                                                        entry: entry.clone(),
5275                                                        depth,
5276                                                    })
5277                                                } else {
5278                                                    GitListEntry::Status(entry.clone())
5279                                                };
5280                                            this.toggle_staged_for_entry(&list_entry, window, cx);
5281                                        }
5282                                        cx.stop_propagation();
5283                                    })
5284                                    .ok();
5285                                }
5286                            })
5287                            .tooltip(move |_window, cx| {
5288                                let action = match stage_status {
5289                                    StageStatus::Staged => "Unstage",
5290                                    StageStatus::Unstaged | StageStatus::PartiallyStaged => "Stage",
5291                                };
5292                                let tooltip_name = action.to_string();
5293
5294                                Tooltip::for_action(tooltip_name, &ToggleStaged, cx)
5295                            }),
5296                    ),
5297            )
5298            .on_click({
5299                cx.listener(move |this, event: &ClickEvent, window, cx| {
5300                    this.selected_entry = Some(ix);
5301                    cx.notify();
5302                    if event.click_count() > 1 || event.modifiers().secondary() {
5303                        this.open_file(&Default::default(), window, cx)
5304                    } else {
5305                        this.open_diff(&Default::default(), window, cx);
5306                        this.focus_handle.focus(window, cx);
5307                    }
5308                })
5309            })
5310            .on_mouse_down(
5311                MouseButton::Right,
5312                move |event: &MouseDownEvent, window, cx| {
5313                    // why isn't this happening automatically? we are passing MouseButton::Right to `on_mouse_down`?
5314                    if event.button != MouseButton::Right {
5315                        return;
5316                    }
5317
5318                    let Some(this) = handle.upgrade() else {
5319                        return;
5320                    };
5321                    this.update(cx, |this, cx| {
5322                        this.deploy_entry_context_menu(event.position, ix, window, cx);
5323                    });
5324                    cx.stop_propagation();
5325                },
5326            )
5327            .into_any_element()
5328    }
5329
5330    fn render_directory_entry(
5331        &self,
5332        ix: usize,
5333        entry: &GitTreeDirEntry,
5334        has_write_access: bool,
5335        window: &Window,
5336        cx: &Context<Self>,
5337    ) -> AnyElement {
5338        // TODO: Have not yet plugin the self.marked_entries. Not sure when and why we need that
5339        let selected = self.selected_entry == Some(ix);
5340        let label_color = Color::Muted;
5341
5342        let id: ElementId = ElementId::Name(format!("dir_{}_{}", entry.name, ix).into());
5343        let checkbox_id: ElementId =
5344            ElementId::Name(format!("dir_checkbox_{}_{}", entry.name, ix).into());
5345        let checkbox_wrapper_id: ElementId =
5346            ElementId::Name(format!("dir_checkbox_wrapper_{}_{}", entry.name, ix).into());
5347
5348        let selected_bg_alpha = 0.08;
5349        let state_opacity_step = 0.04;
5350
5351        let info_color = cx.theme().status().info;
5352        let colors = cx.theme().colors();
5353
5354        let (base_bg, hover_bg, active_bg) = if selected {
5355            (
5356                info_color.alpha(selected_bg_alpha),
5357                info_color.alpha(selected_bg_alpha + state_opacity_step),
5358                info_color.alpha(selected_bg_alpha + state_opacity_step * 2.0),
5359            )
5360        } else {
5361            (
5362                colors.ghost_element_background,
5363                colors.ghost_element_hover,
5364                colors.ghost_element_active,
5365            )
5366        };
5367
5368        let settings = GitPanelSettings::get_global(cx);
5369        let folder_icon = if settings.folder_icons {
5370            FileIcons::get_folder_icon(entry.expanded, entry.key.path.as_std_path(), cx)
5371        } else {
5372            FileIcons::get_chevron_icon(entry.expanded, cx)
5373        };
5374        let fallback_folder_icon = if settings.folder_icons {
5375            if entry.expanded {
5376                IconName::FolderOpen
5377            } else {
5378                IconName::Folder
5379            }
5380        } else {
5381            if entry.expanded {
5382                IconName::ChevronDown
5383            } else {
5384                IconName::ChevronRight
5385            }
5386        };
5387
5388        let stage_status = if let Some(repo) = &self.active_repository {
5389            self.stage_status_for_directory(entry, repo.read(cx))
5390        } else {
5391            util::debug_panic!(
5392                "Won't have entries to render without an active repository in Git Panel"
5393            );
5394            StageStatus::PartiallyStaged
5395        };
5396
5397        let toggle_state: ToggleState = match stage_status {
5398            StageStatus::Staged => ToggleState::Selected,
5399            StageStatus::Unstaged => ToggleState::Unselected,
5400            StageStatus::PartiallyStaged => ToggleState::Indeterminate,
5401        };
5402
5403        let name_row = h_flex()
5404            .min_w_0()
5405            .gap_1()
5406            .pl(px(entry.depth as f32 * TREE_INDENT))
5407            .child(
5408                folder_icon
5409                    .map(|folder_icon| {
5410                        Icon::from_path(folder_icon)
5411                            .size(IconSize::Small)
5412                            .color(Color::Muted)
5413                    })
5414                    .unwrap_or_else(|| {
5415                        Icon::new(fallback_folder_icon)
5416                            .size(IconSize::Small)
5417                            .color(Color::Muted)
5418                    }),
5419            )
5420            .child(self.entry_label(entry.name.clone(), label_color).truncate());
5421
5422        h_flex()
5423            .id(id)
5424            .h(self.list_item_height())
5425            .min_w_0()
5426            .w_full()
5427            .pl_3()
5428            .pr_1()
5429            .gap_1p5()
5430            .justify_between()
5431            .border_1()
5432            .border_r_2()
5433            .when(selected && self.focus_handle.is_focused(window), |el| {
5434                el.border_color(cx.theme().colors().panel_focused_border)
5435            })
5436            .bg(base_bg)
5437            .hover(|s| s.bg(hover_bg))
5438            .active(|s| s.bg(active_bg))
5439            .child(name_row)
5440            .child(
5441                div()
5442                    .id(checkbox_wrapper_id)
5443                    .flex_none()
5444                    .occlude()
5445                    .cursor_pointer()
5446                    .child(
5447                        Checkbox::new(checkbox_id, toggle_state)
5448                            .disabled(!has_write_access)
5449                            .fill()
5450                            .elevation(ElevationIndex::Surface)
5451                            .on_click({
5452                                let entry = entry.clone();
5453                                let this = cx.weak_entity();
5454                                move |_, window, cx| {
5455                                    this.update(cx, |this, cx| {
5456                                        if !has_write_access {
5457                                            return;
5458                                        }
5459                                        this.toggle_staged_for_entry(
5460                                            &GitListEntry::Directory(entry.clone()),
5461                                            window,
5462                                            cx,
5463                                        );
5464                                        cx.stop_propagation();
5465                                    })
5466                                    .ok();
5467                                }
5468                            })
5469                            .tooltip(move |_window, cx| {
5470                                let action = match stage_status {
5471                                    StageStatus::Staged => "Unstage",
5472                                    StageStatus::Unstaged | StageStatus::PartiallyStaged => "Stage",
5473                                };
5474                                Tooltip::simple(format!("{action} folder"), cx)
5475                            }),
5476                    ),
5477            )
5478            .on_click({
5479                let key = entry.key.clone();
5480                cx.listener(move |this, _event: &ClickEvent, window, cx| {
5481                    this.selected_entry = Some(ix);
5482                    this.toggle_directory(&key, window, cx);
5483                })
5484            })
5485            .into_any_element()
5486    }
5487
5488    fn path_formatted(
5489        &self,
5490        directory: Option<String>,
5491        path_color: Color,
5492        file_name: String,
5493        label_color: Color,
5494        path_style: PathStyle,
5495        git_path_style: GitPathStyle,
5496        strikethrough: bool,
5497    ) -> Div {
5498        let file_name_first = git_path_style == GitPathStyle::FileNameFirst;
5499        let file_path_first = git_path_style == GitPathStyle::FilePathFirst;
5500
5501        let file_name = format!("{} ", file_name);
5502
5503        h_flex()
5504            .min_w_0()
5505            .overflow_hidden()
5506            .when(file_path_first, |this| this.flex_row_reverse())
5507            .child(
5508                div().flex_none().child(
5509                    self.entry_label(file_name, label_color)
5510                        .when(strikethrough, Label::strikethrough),
5511                ),
5512            )
5513            .when_some(directory, |this, dir| {
5514                let path_name = if file_name_first {
5515                    dir
5516                } else {
5517                    format!("{dir}{}", path_style.primary_separator())
5518                };
5519
5520                this.child(
5521                    self.entry_label(path_name, path_color)
5522                        .truncate_start()
5523                        .when(strikethrough, Label::strikethrough),
5524                )
5525            })
5526    }
5527
5528    fn has_write_access(&self, cx: &App) -> bool {
5529        !self.project.read(cx).is_read_only(cx)
5530    }
5531
5532    pub fn load_commit_template(
5533        &self,
5534        cx: &mut Context<Self>,
5535    ) -> Task<anyhow::Result<Option<GitCommitTemplate>>> {
5536        let Some(repo) = self.active_repository.clone() else {
5537            return Task::ready(Err(anyhow::anyhow!("no active repo")));
5538        };
5539        repo.update(cx, |repo, cx| {
5540            let rx = repo.load_commit_template_text();
5541            cx.spawn(async move |_, _| rx.await?)
5542        })
5543    }
5544
5545    pub fn amend_pending(&self) -> bool {
5546        self.amend_pending
5547    }
5548
5549    /// Sets the pending amend state, ensuring that the original commit message
5550    /// is either saved, when `value` is `true` and there's no pending amend, or
5551    /// restored, when `value` is `false` and there's a pending amend.
5552    pub fn set_amend_pending(&mut self, value: bool, cx: &mut Context<Self>) {
5553        if value && !self.amend_pending {
5554            let current_message = self.commit_message_buffer(cx).read(cx).text();
5555            self.original_commit_message = if current_message.trim().is_empty() {
5556                None
5557            } else {
5558                Some(current_message)
5559            };
5560        } else if !value && self.amend_pending {
5561            let message = self.original_commit_message.take().unwrap_or_default();
5562            self.commit_message_buffer(cx).update(cx, |buffer, cx| {
5563                let start = buffer.anchor_before(0);
5564                let end = buffer.anchor_after(buffer.len());
5565                buffer.edit([(start..end, message)], None, cx);
5566            });
5567        }
5568
5569        self.amend_pending = value;
5570        self.serialize(cx);
5571        cx.notify();
5572    }
5573
5574    pub fn signoff_enabled(&self) -> bool {
5575        self.signoff_enabled
5576    }
5577
5578    pub fn set_signoff_enabled(&mut self, value: bool, cx: &mut Context<Self>) {
5579        self.signoff_enabled = value;
5580        self.serialize(cx);
5581        cx.notify();
5582    }
5583
5584    pub fn toggle_signoff_enabled(
5585        &mut self,
5586        _: &Signoff,
5587        _window: &mut Window,
5588        cx: &mut Context<Self>,
5589    ) {
5590        self.set_signoff_enabled(!self.signoff_enabled, cx);
5591    }
5592
5593    pub async fn load(
5594        workspace: WeakEntity<Workspace>,
5595        mut cx: AsyncWindowContext,
5596    ) -> anyhow::Result<Entity<Self>> {
5597        let serialized_panel = match workspace
5598            .read_with(&cx, |workspace, cx| {
5599                Self::serialization_key(workspace).map(|key| (key, KeyValueStore::global(cx)))
5600            })
5601            .ok()
5602            .flatten()
5603        {
5604            Some((serialization_key, kvp)) => cx
5605                .background_spawn(async move { kvp.read_kvp(&serialization_key) })
5606                .await
5607                .context("loading git panel")
5608                .log_err()
5609                .flatten()
5610                .map(|panel| serde_json::from_str::<SerializedGitPanel>(&panel))
5611                .transpose()
5612                .log_err()
5613                .flatten(),
5614            None => None,
5615        };
5616
5617        workspace.update_in(&mut cx, |workspace, window, cx| {
5618            let panel = GitPanel::new(workspace, window, cx);
5619
5620            if let Some(serialized_panel) = serialized_panel {
5621                panel.update(cx, |panel, cx| {
5622                    panel.amend_pending = serialized_panel.amend_pending;
5623                    panel.signoff_enabled = serialized_panel.signoff_enabled;
5624                    cx.notify();
5625                })
5626            }
5627
5628            panel
5629        })
5630    }
5631
5632    fn stage_bulk(&mut self, mut index: usize, cx: &mut Context<'_, Self>) {
5633        let Some(op) = self.bulk_staging.as_ref() else {
5634            return;
5635        };
5636        let Some(mut anchor_index) = self.entry_by_path(&op.anchor) else {
5637            return;
5638        };
5639        if let Some(entry) = self.entries.get(index)
5640            && let Some(entry) = entry.status_entry()
5641        {
5642            self.set_bulk_staging_anchor(entry.repo_path.clone(), cx);
5643        }
5644        if index < anchor_index {
5645            std::mem::swap(&mut index, &mut anchor_index);
5646        }
5647        let entries = self
5648            .entries
5649            .get(anchor_index..=index)
5650            .unwrap_or_default()
5651            .iter()
5652            .filter_map(|entry| entry.status_entry().cloned())
5653            .collect::<Vec<_>>();
5654        self.change_file_stage(true, entries, cx);
5655    }
5656
5657    fn set_bulk_staging_anchor(&mut self, path: RepoPath, cx: &mut Context<'_, GitPanel>) {
5658        let Some(repo) = self.active_repository.as_ref() else {
5659            return;
5660        };
5661        self.bulk_staging = Some(BulkStaging {
5662            repo_id: repo.read(cx).id,
5663            anchor: path,
5664        });
5665    }
5666
5667    pub(crate) fn toggle_amend_pending(&mut self, cx: &mut Context<Self>) {
5668        self.set_amend_pending(!self.amend_pending, cx);
5669        if self.amend_pending {
5670            self.load_last_commit_message(cx);
5671        }
5672    }
5673}
5674
5675#[cfg(any(test, feature = "test-support"))]
5676impl GitPanel {
5677    pub fn new_test(
5678        workspace: &mut Workspace,
5679        window: &mut Window,
5680        cx: &mut Context<Workspace>,
5681    ) -> Entity<Self> {
5682        Self::new(workspace, window, cx)
5683    }
5684
5685    pub fn active_repository(&self) -> Option<&Entity<Repository>> {
5686        self.active_repository.as_ref()
5687    }
5688}
5689
5690impl Render for GitPanel {
5691    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
5692        let project = self.project.read(cx);
5693        let has_entries = !self.entries.is_empty();
5694        let room = self.workspace.upgrade().and_then(|_workspace| {
5695            call::ActiveCall::try_global(cx).and_then(|call| call.read(cx).room().cloned())
5696        });
5697
5698        let has_write_access = self.has_write_access(cx);
5699
5700        let has_co_authors = room.is_some_and(|room| {
5701            self.load_local_committer(cx);
5702            let room = room.read(cx);
5703            room.remote_participants()
5704                .values()
5705                .any(|remote_participant| remote_participant.can_write())
5706        });
5707
5708        v_flex()
5709            .id("git_panel")
5710            .key_context(self.dispatch_context(window, cx))
5711            .track_focus(&self.focus_handle)
5712            .when(has_write_access && !project.is_read_only(cx), |this| {
5713                this.on_action(cx.listener(Self::toggle_staged_for_selected))
5714                    .on_action(cx.listener(Self::stage_range))
5715                    .on_action(cx.listener(GitPanel::on_commit))
5716                    .on_action(cx.listener(GitPanel::on_amend))
5717                    .on_action(cx.listener(GitPanel::toggle_signoff_enabled))
5718                    .on_action(cx.listener(Self::stage_all))
5719                    .on_action(cx.listener(Self::unstage_all))
5720                    .on_action(cx.listener(Self::stage_selected))
5721                    .on_action(cx.listener(Self::unstage_selected))
5722                    .on_action(cx.listener(Self::restore_tracked_files))
5723                    .on_action(cx.listener(Self::revert_selected))
5724                    .on_action(cx.listener(Self::add_to_gitignore))
5725                    .on_action(cx.listener(Self::clean_all))
5726                    .on_action(cx.listener(Self::generate_commit_message_action))
5727                    .on_action(cx.listener(Self::stash_all))
5728                    .on_action(cx.listener(Self::stash_pop))
5729            })
5730            .on_action(cx.listener(Self::collapse_selected_entry))
5731            .on_action(cx.listener(Self::expand_selected_entry))
5732            .on_action(cx.listener(Self::select_first))
5733            .on_action(cx.listener(Self::select_next))
5734            .on_action(cx.listener(Self::select_previous))
5735            .on_action(cx.listener(Self::select_last))
5736            .on_action(cx.listener(Self::first_entry))
5737            .on_action(cx.listener(Self::next_entry))
5738            .on_action(cx.listener(Self::previous_entry))
5739            .on_action(cx.listener(Self::last_entry))
5740            .on_action(cx.listener(Self::close_panel))
5741            .on_action(cx.listener(Self::open_diff))
5742            .on_action(cx.listener(Self::open_file))
5743            .on_action(cx.listener(Self::file_history))
5744            .on_action(cx.listener(Self::focus_changes_list))
5745            .on_action(cx.listener(Self::focus_editor))
5746            .on_action(cx.listener(Self::expand_commit_editor))
5747            .when(has_write_access && has_co_authors, |git_panel| {
5748                git_panel.on_action(cx.listener(Self::toggle_fill_co_authors))
5749            })
5750            .on_action(cx.listener(Self::toggle_sort_by_path))
5751            .on_action(cx.listener(Self::toggle_tree_view))
5752            .size_full()
5753            .overflow_hidden()
5754            .bg(cx.theme().colors().panel_background)
5755            .child(
5756                v_flex()
5757                    .size_full()
5758                    .children(self.render_panel_header(window, cx))
5759                    .map(|this| {
5760                        if let Some(repo) = self.active_repository.clone()
5761                            && has_entries
5762                        {
5763                            this.child(self.render_entries(has_write_access, repo, window, cx))
5764                        } else {
5765                            this.child(self.render_empty_state(cx).into_any_element())
5766                        }
5767                    })
5768                    .children(self.render_footer(window, cx))
5769                    .when(self.amend_pending, |this| {
5770                        this.child(self.render_pending_amend(cx))
5771                    })
5772                    .when(!self.amend_pending, |this| {
5773                        this.children(self.render_previous_commit(window, cx))
5774                    })
5775                    .into_any_element(),
5776            )
5777            .children(self.context_menu.as_ref().map(|(menu, position, _)| {
5778                deferred(
5779                    anchored()
5780                        .position(*position)
5781                        .anchor(Corner::TopLeft)
5782                        .child(menu.clone()),
5783                )
5784                .with_priority(1)
5785            }))
5786    }
5787}
5788
5789impl Focusable for GitPanel {
5790    fn focus_handle(&self, cx: &App) -> gpui::FocusHandle {
5791        if self.entries.is_empty() {
5792            self.commit_editor.focus_handle(cx)
5793        } else {
5794            self.focus_handle.clone()
5795        }
5796    }
5797}
5798
5799impl EventEmitter<Event> for GitPanel {}
5800
5801impl EventEmitter<PanelEvent> for GitPanel {}
5802
5803pub(crate) struct GitPanelAddon {
5804    pub(crate) workspace: WeakEntity<Workspace>,
5805}
5806
5807impl editor::Addon for GitPanelAddon {
5808    fn to_any(&self) -> &dyn std::any::Any {
5809        self
5810    }
5811
5812    fn render_buffer_header_controls(
5813        &self,
5814        _excerpt_info: &ExcerptBoundaryInfo,
5815        buffer: &language::BufferSnapshot,
5816        window: &Window,
5817        cx: &App,
5818    ) -> Option<AnyElement> {
5819        let file = buffer.file()?;
5820        let git_panel = self.workspace.upgrade()?.read(cx).panel::<GitPanel>(cx)?;
5821
5822        git_panel
5823            .read(cx)
5824            .render_buffer_header_controls(&git_panel, file, window, cx)
5825    }
5826}
5827
5828impl Panel for GitPanel {
5829    fn persistent_name() -> &'static str {
5830        "GitPanel"
5831    }
5832
5833    fn panel_key() -> &'static str {
5834        GIT_PANEL_KEY
5835    }
5836
5837    fn position(&self, _: &Window, cx: &App) -> DockPosition {
5838        GitPanelSettings::get_global(cx).dock
5839    }
5840
5841    fn position_is_valid(&self, position: DockPosition) -> bool {
5842        matches!(position, DockPosition::Left | DockPosition::Right)
5843    }
5844
5845    fn set_position(&mut self, position: DockPosition, _: &mut Window, cx: &mut Context<Self>) {
5846        settings::update_settings_file(self.fs.clone(), cx, move |settings, _| {
5847            settings.git_panel.get_or_insert_default().dock = Some(position.into())
5848        });
5849    }
5850
5851    fn default_size(&self, _: &Window, cx: &App) -> Pixels {
5852        GitPanelSettings::get_global(cx).default_width
5853    }
5854
5855    fn icon(&self, _: &Window, cx: &App) -> Option<ui::IconName> {
5856        Some(ui::IconName::GitBranch).filter(|_| GitPanelSettings::get_global(cx).button)
5857    }
5858
5859    fn icon_tooltip(&self, _window: &Window, _cx: &App) -> Option<&'static str> {
5860        Some("Git Panel")
5861    }
5862
5863    fn icon_label(&self, _: &Window, cx: &App) -> Option<String> {
5864        if !GitPanelSettings::get_global(cx).show_count_badge {
5865            return None;
5866        }
5867        let total = self.changes_count;
5868        (total > 0).then(|| total.to_string())
5869    }
5870
5871    fn toggle_action(&self) -> Box<dyn Action> {
5872        Box::new(ToggleFocus)
5873    }
5874
5875    fn starts_open(&self, _: &Window, cx: &App) -> bool {
5876        GitPanelSettings::get_global(cx).starts_open
5877    }
5878
5879    fn activation_priority(&self) -> u32 {
5880        3
5881    }
5882}
5883
5884impl PanelHeader for GitPanel {}
5885
5886pub fn panel_editor_container(_window: &mut Window, cx: &mut App) -> Div {
5887    v_flex()
5888        .size_full()
5889        .gap(px(8.))
5890        .p_2()
5891        .bg(cx.theme().colors().editor_background)
5892}
5893
5894pub(crate) fn panel_editor_style(monospace: bool, window: &Window, cx: &App) -> EditorStyle {
5895    let settings = ThemeSettings::get_global(cx);
5896
5897    let font_size = TextSize::Small.rems(cx).to_pixels(window.rem_size());
5898
5899    let (font_family, font_fallbacks, font_features, font_weight, line_height) = if monospace {
5900        (
5901            settings.buffer_font.family.clone(),
5902            settings.buffer_font.fallbacks.clone(),
5903            settings.buffer_font.features.clone(),
5904            settings.buffer_font.weight,
5905            font_size * settings.buffer_line_height.value(),
5906        )
5907    } else {
5908        (
5909            settings.ui_font.family.clone(),
5910            settings.ui_font.fallbacks.clone(),
5911            settings.ui_font.features.clone(),
5912            settings.ui_font.weight,
5913            window.line_height(),
5914        )
5915    };
5916
5917    EditorStyle {
5918        background: cx.theme().colors().editor_background,
5919        local_player: cx.theme().players().local(),
5920        text: TextStyle {
5921            color: cx.theme().colors().text,
5922            font_family,
5923            font_fallbacks,
5924            font_features,
5925            font_size: TextSize::Small.rems(cx).into(),
5926            font_weight,
5927            line_height: line_height.into(),
5928            ..Default::default()
5929        },
5930        syntax: cx.theme().syntax().clone(),
5931        ..Default::default()
5932    }
5933}
5934
5935struct GitPanelMessageTooltip {
5936    commit_tooltip: Option<Entity<CommitTooltip>>,
5937}
5938
5939impl GitPanelMessageTooltip {
5940    fn new(
5941        git_panel: Entity<GitPanel>,
5942        sha: SharedString,
5943        repository: Entity<Repository>,
5944        window: &mut Window,
5945        cx: &mut App,
5946    ) -> Entity<Self> {
5947        let remote_url = repository.read(cx).default_remote_url();
5948        cx.new(|cx| {
5949            cx.spawn_in(window, async move |this, cx| {
5950                let (details, workspace) = git_panel.update(cx, |git_panel, cx| {
5951                    (
5952                        git_panel.load_commit_details(sha.to_string(), cx),
5953                        git_panel.workspace.clone(),
5954                    )
5955                });
5956                let details = details.await?;
5957                let provider_registry = cx
5958                    .update(|_, app| GitHostingProviderRegistry::default_global(app))
5959                    .ok();
5960
5961                let commit_details = crate::commit_tooltip::CommitDetails {
5962                    sha: details.sha.clone(),
5963                    author_name: details.author_name.clone(),
5964                    author_email: details.author_email.clone(),
5965                    commit_time: OffsetDateTime::from_unix_timestamp(details.commit_timestamp)?,
5966                    message: Some(ParsedCommitMessage::parse(
5967                        details.sha.to_string(),
5968                        details.message.to_string(),
5969                        remote_url.as_deref(),
5970                        provider_registry,
5971                    )),
5972                };
5973
5974                this.update(cx, |this: &mut GitPanelMessageTooltip, cx| {
5975                    this.commit_tooltip = Some(cx.new(move |cx| {
5976                        CommitTooltip::new(commit_details, repository, workspace, cx)
5977                    }));
5978                    cx.notify();
5979                })
5980            })
5981            .detach();
5982
5983            Self {
5984                commit_tooltip: None,
5985            }
5986        })
5987    }
5988}
5989
5990impl Render for GitPanelMessageTooltip {
5991    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
5992        if let Some(commit_tooltip) = &self.commit_tooltip {
5993            commit_tooltip.clone().into_any_element()
5994        } else {
5995            gpui::Empty.into_any_element()
5996        }
5997    }
5998}
5999
6000#[derive(IntoElement, RegisterComponent)]
6001pub struct PanelRepoFooter {
6002    active_repository: SharedString,
6003    branch: Option<Branch>,
6004    head_commit: Option<CommitDetails>,
6005
6006    // Getting a GitPanel in previews will be difficult.
6007    //
6008    // For now just take an option here, and we won't bind handlers to buttons in previews.
6009    git_panel: Option<Entity<GitPanel>>,
6010}
6011
6012impl PanelRepoFooter {
6013    pub fn new(
6014        active_repository: SharedString,
6015        branch: Option<Branch>,
6016        head_commit: Option<CommitDetails>,
6017        git_panel: Option<Entity<GitPanel>>,
6018    ) -> Self {
6019        Self {
6020            active_repository,
6021            branch,
6022            head_commit,
6023            git_panel,
6024        }
6025    }
6026
6027    pub fn new_preview(active_repository: SharedString, branch: Option<Branch>) -> Self {
6028        Self {
6029            active_repository,
6030            branch,
6031            head_commit: None,
6032            git_panel: None,
6033        }
6034    }
6035}
6036
6037impl RenderOnce for PanelRepoFooter {
6038    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
6039        let project = self
6040            .git_panel
6041            .as_ref()
6042            .map(|panel| panel.read(cx).project.clone());
6043
6044        let (workspace, repo) = self
6045            .git_panel
6046            .as_ref()
6047            .map(|panel| {
6048                let panel = panel.read(cx);
6049                (panel.workspace.clone(), panel.active_repository.clone())
6050            })
6051            .unzip();
6052
6053        let single_repo = project
6054            .as_ref()
6055            .map(|project| project.read(cx).git_store().read(cx).repositories().len() == 1)
6056            .unwrap_or(true);
6057
6058        const MAX_BRANCH_LEN: usize = 16;
6059        const MAX_REPO_LEN: usize = 16;
6060        const LABEL_CHARACTER_BUDGET: usize = MAX_BRANCH_LEN + MAX_REPO_LEN;
6061        const MAX_SHORT_SHA_LEN: usize = 8;
6062        let branch_name = self
6063            .branch
6064            .as_ref()
6065            .map(|branch| branch.name().to_owned())
6066            .or_else(|| {
6067                self.head_commit.as_ref().map(|commit| {
6068                    commit
6069                        .sha
6070                        .chars()
6071                        .take(MAX_SHORT_SHA_LEN)
6072                        .collect::<String>()
6073                })
6074            })
6075            .unwrap_or_else(|| " (no branch)".to_owned());
6076        let show_separator = self.branch.is_some() || self.head_commit.is_some();
6077
6078        let active_repo_name = self.active_repository.clone();
6079
6080        let branch_actual_len = branch_name.len();
6081        let repo_actual_len = active_repo_name.len();
6082
6083        // ideally, show the whole branch and repo names but
6084        // when we can't, use a budget to allocate space between the two
6085        let (repo_display_len, branch_display_len) =
6086            if branch_actual_len + repo_actual_len <= LABEL_CHARACTER_BUDGET {
6087                (repo_actual_len, branch_actual_len)
6088            } else if branch_actual_len <= MAX_BRANCH_LEN {
6089                let repo_space = (LABEL_CHARACTER_BUDGET - branch_actual_len).min(MAX_REPO_LEN);
6090                (repo_space, branch_actual_len)
6091            } else if repo_actual_len <= MAX_REPO_LEN {
6092                let branch_space = (LABEL_CHARACTER_BUDGET - repo_actual_len).min(MAX_BRANCH_LEN);
6093                (repo_actual_len, branch_space)
6094            } else {
6095                (MAX_REPO_LEN, MAX_BRANCH_LEN)
6096            };
6097
6098        let truncated_repo_name = if repo_actual_len <= repo_display_len {
6099            active_repo_name.to_string()
6100        } else {
6101            util::truncate_and_trailoff(active_repo_name.trim_ascii(), repo_display_len)
6102        };
6103
6104        let truncated_branch_name = if branch_actual_len <= branch_display_len {
6105            branch_name
6106        } else {
6107            util::truncate_and_trailoff(branch_name.trim_ascii(), branch_display_len)
6108        };
6109
6110        let repo_selector_trigger = Button::new("repo-selector", truncated_repo_name)
6111            .size(ButtonSize::None)
6112            .label_size(LabelSize::Small);
6113
6114        let repo_selector = PopoverMenu::new("repository-switcher")
6115            .menu({
6116                let project = project;
6117                move |window, cx| {
6118                    let project = project.clone()?;
6119                    Some(cx.new(|cx| RepositorySelector::new(project, rems(20.), window, cx)))
6120                }
6121            })
6122            .trigger_with_tooltip(
6123                repo_selector_trigger
6124                    .when(single_repo, |this| this.disabled(true).color(Color::Muted))
6125                    .truncate(true),
6126                move |_, cx| {
6127                    if single_repo {
6128                        cx.new(|_| Empty).into()
6129                    } else {
6130                        Tooltip::simple("Switch Active Repository", cx)
6131                    }
6132                },
6133            )
6134            .anchor(Corner::BottomLeft)
6135            .offset(gpui::Point {
6136                x: px(0.0),
6137                y: px(-2.0),
6138            })
6139            .into_any_element();
6140
6141        let branch_selector_button = Button::new("branch-selector", truncated_branch_name)
6142            .size(ButtonSize::None)
6143            .label_size(LabelSize::Small)
6144            .truncate(true)
6145            .on_click(|_, window, cx| {
6146                window.dispatch_action(zed_actions::git::Switch.boxed_clone(), cx);
6147            });
6148
6149        let branch_selector = PopoverMenu::new("popover-button")
6150            .menu(move |window, cx| {
6151                let workspace = workspace.clone()?;
6152                let repo = repo.clone().flatten();
6153                Some(branch_picker::popover(workspace, false, repo, window, cx))
6154            })
6155            .trigger_with_tooltip(
6156                branch_selector_button,
6157                Tooltip::for_action_title("Switch Branch", &zed_actions::git::Switch),
6158            )
6159            .anchor(Corner::BottomLeft)
6160            .offset(gpui::Point {
6161                x: px(0.0),
6162                y: px(-2.0),
6163            });
6164
6165        h_flex()
6166            .h(px(36.))
6167            .w_full()
6168            .px_2()
6169            .justify_between()
6170            .gap_1()
6171            .child(
6172                h_flex()
6173                    .flex_1()
6174                    .overflow_hidden()
6175                    .gap_px()
6176                    .child(Icon::new(IconName::GitBranch).size(IconSize::Small).color(
6177                        if single_repo {
6178                            Color::Disabled
6179                        } else {
6180                            Color::Muted
6181                        },
6182                    ))
6183                    .child(repo_selector)
6184                    .when(show_separator, |this| {
6185                        this.child(
6186                            div()
6187                                .text_sm()
6188                                .text_color(cx.theme().colors().icon_muted.opacity(0.5))
6189                                .child("/"),
6190                        )
6191                    })
6192                    .child(branch_selector),
6193            )
6194            .children(if let Some(git_panel) = self.git_panel {
6195                git_panel.update(cx, |git_panel, cx| git_panel.render_remote_button(cx))
6196            } else {
6197                None
6198            })
6199    }
6200}
6201
6202impl Component for PanelRepoFooter {
6203    fn scope() -> ComponentScope {
6204        ComponentScope::VersionControl
6205    }
6206
6207    fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement> {
6208        let unknown_upstream = None;
6209        let no_remote_upstream = Some(UpstreamTracking::Gone);
6210        let ahead_of_upstream = Some(
6211            UpstreamTrackingStatus {
6212                ahead: 2,
6213                behind: 0,
6214            }
6215            .into(),
6216        );
6217        let behind_upstream = Some(
6218            UpstreamTrackingStatus {
6219                ahead: 0,
6220                behind: 2,
6221            }
6222            .into(),
6223        );
6224        let ahead_and_behind_upstream = Some(
6225            UpstreamTrackingStatus {
6226                ahead: 3,
6227                behind: 1,
6228            }
6229            .into(),
6230        );
6231
6232        let not_ahead_or_behind_upstream = Some(
6233            UpstreamTrackingStatus {
6234                ahead: 0,
6235                behind: 0,
6236            }
6237            .into(),
6238        );
6239
6240        fn branch(upstream: Option<UpstreamTracking>) -> Branch {
6241            Branch {
6242                is_head: true,
6243                ref_name: "some-branch".into(),
6244                upstream: upstream.map(|tracking| Upstream {
6245                    ref_name: "origin/some-branch".into(),
6246                    tracking,
6247                }),
6248                most_recent_commit: Some(CommitSummary {
6249                    sha: "abc123".into(),
6250                    subject: "Modify stuff".into(),
6251                    commit_timestamp: 1710932954,
6252                    author_name: "John Doe".into(),
6253                    has_parent: true,
6254                }),
6255            }
6256        }
6257
6258        fn custom(branch_name: &str, upstream: Option<UpstreamTracking>) -> Branch {
6259            Branch {
6260                is_head: true,
6261                ref_name: branch_name.to_string().into(),
6262                upstream: upstream.map(|tracking| Upstream {
6263                    ref_name: format!("zed/{}", branch_name).into(),
6264                    tracking,
6265                }),
6266                most_recent_commit: Some(CommitSummary {
6267                    sha: "abc123".into(),
6268                    subject: "Modify stuff".into(),
6269                    commit_timestamp: 1710932954,
6270                    author_name: "John Doe".into(),
6271                    has_parent: true,
6272                }),
6273            }
6274        }
6275
6276        fn active_repository(id: usize) -> SharedString {
6277            format!("repo-{}", id).into()
6278        }
6279
6280        let example_width = px(340.);
6281        Some(
6282            v_flex()
6283                .gap_6()
6284                .w_full()
6285                .flex_none()
6286                .children(vec![
6287                    example_group_with_title(
6288                        "Action Button States",
6289                        vec![
6290                            single_example(
6291                                "No Branch",
6292                                div()
6293                                    .w(example_width)
6294                                    .overflow_hidden()
6295                                    .child(PanelRepoFooter::new_preview(active_repository(1), None))
6296                                    .into_any_element(),
6297                            ),
6298                            single_example(
6299                                "Remote status unknown",
6300                                div()
6301                                    .w(example_width)
6302                                    .overflow_hidden()
6303                                    .child(PanelRepoFooter::new_preview(
6304                                        active_repository(2),
6305                                        Some(branch(unknown_upstream)),
6306                                    ))
6307                                    .into_any_element(),
6308                            ),
6309                            single_example(
6310                                "No Remote Upstream",
6311                                div()
6312                                    .w(example_width)
6313                                    .overflow_hidden()
6314                                    .child(PanelRepoFooter::new_preview(
6315                                        active_repository(3),
6316                                        Some(branch(no_remote_upstream)),
6317                                    ))
6318                                    .into_any_element(),
6319                            ),
6320                            single_example(
6321                                "Not Ahead or Behind",
6322                                div()
6323                                    .w(example_width)
6324                                    .overflow_hidden()
6325                                    .child(PanelRepoFooter::new_preview(
6326                                        active_repository(4),
6327                                        Some(branch(not_ahead_or_behind_upstream)),
6328                                    ))
6329                                    .into_any_element(),
6330                            ),
6331                            single_example(
6332                                "Behind remote",
6333                                div()
6334                                    .w(example_width)
6335                                    .overflow_hidden()
6336                                    .child(PanelRepoFooter::new_preview(
6337                                        active_repository(5),
6338                                        Some(branch(behind_upstream)),
6339                                    ))
6340                                    .into_any_element(),
6341                            ),
6342                            single_example(
6343                                "Ahead of remote",
6344                                div()
6345                                    .w(example_width)
6346                                    .overflow_hidden()
6347                                    .child(PanelRepoFooter::new_preview(
6348                                        active_repository(6),
6349                                        Some(branch(ahead_of_upstream)),
6350                                    ))
6351                                    .into_any_element(),
6352                            ),
6353                            single_example(
6354                                "Ahead and behind remote",
6355                                div()
6356                                    .w(example_width)
6357                                    .overflow_hidden()
6358                                    .child(PanelRepoFooter::new_preview(
6359                                        active_repository(7),
6360                                        Some(branch(ahead_and_behind_upstream)),
6361                                    ))
6362                                    .into_any_element(),
6363                            ),
6364                        ],
6365                    )
6366                    .grow()
6367                    .vertical(),
6368                ])
6369                .children(vec![
6370                    example_group_with_title(
6371                        "Labels",
6372                        vec![
6373                            single_example(
6374                                "Short Branch & Repo",
6375                                div()
6376                                    .w(example_width)
6377                                    .overflow_hidden()
6378                                    .child(PanelRepoFooter::new_preview(
6379                                        SharedString::from("zed"),
6380                                        Some(custom("main", behind_upstream)),
6381                                    ))
6382                                    .into_any_element(),
6383                            ),
6384                            single_example(
6385                                "Long Branch",
6386                                div()
6387                                    .w(example_width)
6388                                    .overflow_hidden()
6389                                    .child(PanelRepoFooter::new_preview(
6390                                        SharedString::from("zed"),
6391                                        Some(custom(
6392                                            "redesign-and-update-git-ui-list-entry-style",
6393                                            behind_upstream,
6394                                        )),
6395                                    ))
6396                                    .into_any_element(),
6397                            ),
6398                            single_example(
6399                                "Long Repo",
6400                                div()
6401                                    .w(example_width)
6402                                    .overflow_hidden()
6403                                    .child(PanelRepoFooter::new_preview(
6404                                        SharedString::from("zed-industries-community-examples"),
6405                                        Some(custom("gpui", ahead_of_upstream)),
6406                                    ))
6407                                    .into_any_element(),
6408                            ),
6409                            single_example(
6410                                "Long Repo & Branch",
6411                                div()
6412                                    .w(example_width)
6413                                    .overflow_hidden()
6414                                    .child(PanelRepoFooter::new_preview(
6415                                        SharedString::from("zed-industries-community-examples"),
6416                                        Some(custom(
6417                                            "redesign-and-update-git-ui-list-entry-style",
6418                                            behind_upstream,
6419                                        )),
6420                                    ))
6421                                    .into_any_element(),
6422                            ),
6423                            single_example(
6424                                "Uppercase Repo",
6425                                div()
6426                                    .w(example_width)
6427                                    .overflow_hidden()
6428                                    .child(PanelRepoFooter::new_preview(
6429                                        SharedString::from("LICENSES"),
6430                                        Some(custom("main", ahead_of_upstream)),
6431                                    ))
6432                                    .into_any_element(),
6433                            ),
6434                            single_example(
6435                                "Uppercase Branch",
6436                                div()
6437                                    .w(example_width)
6438                                    .overflow_hidden()
6439                                    .child(PanelRepoFooter::new_preview(
6440                                        SharedString::from("zed"),
6441                                        Some(custom("update-README", behind_upstream)),
6442                                    ))
6443                                    .into_any_element(),
6444                            ),
6445                        ],
6446                    )
6447                    .grow()
6448                    .vertical(),
6449                ])
6450                .into_any_element(),
6451        )
6452    }
6453}
6454
6455fn open_output(
6456    operation: impl Into<SharedString>,
6457    workspace: &mut Workspace,
6458    output: &str,
6459    window: &mut Window,
6460    cx: &mut Context<Workspace>,
6461) {
6462    let operation = operation.into();
6463
6464    let mut handler = GitOutputHandler::default();
6465    let mut processor = ansi::Processor::<ansi::StdSyncHandler>::default();
6466    processor.advance(&mut handler, output.as_bytes());
6467    let plain_text = handler.output;
6468
6469    let buffer = cx.new(|cx| Buffer::local(plain_text.as_str(), cx));
6470    buffer.update(cx, |buffer, cx| {
6471        buffer.set_capability(language::Capability::ReadOnly, cx);
6472    });
6473    let editor = cx.new(|cx| {
6474        let mut editor = Editor::for_buffer(buffer, None, window, cx);
6475        editor.buffer().update(cx, |buffer, cx| {
6476            buffer.set_title(format!("Output from git {operation}"), cx);
6477        });
6478        editor.set_read_only(true);
6479        editor
6480    });
6481
6482    workspace.add_item_to_center(Box::new(editor), window, cx);
6483}
6484
6485#[derive(Default)]
6486struct GitOutputHandler {
6487    output: String,
6488    line_start: usize,
6489}
6490
6491impl ansi::Handler for GitOutputHandler {
6492    fn input(&mut self, c: char) {
6493        self.output.push(c);
6494    }
6495
6496    fn linefeed(&mut self) {
6497        self.output.push('\n');
6498        self.line_start = self.output.len();
6499    }
6500
6501    fn carriage_return(&mut self) {
6502        self.output.truncate(self.line_start);
6503    }
6504
6505    fn put_tab(&mut self, count: u16) {
6506        self.output
6507            .extend(std::iter::repeat_n('\t', count as usize));
6508    }
6509}
6510
6511pub(crate) fn show_error_toast(
6512    workspace: Entity<Workspace>,
6513    action: impl Into<SharedString>,
6514    e: anyhow::Error,
6515    cx: &mut App,
6516) {
6517    let action = action.into();
6518    let message = format_git_error_toast_message(&e);
6519    if message
6520        .matches(git::repository::REMOTE_CANCELLED_BY_USER)
6521        .next()
6522        .is_some()
6523    { // Hide the cancelled by user message
6524    } else {
6525        workspace.update(cx, |workspace, cx| {
6526            let workspace_weak = cx.weak_entity();
6527            let toast = StatusToast::new(format!("git {} failed", action), cx, |this, _cx| {
6528                this.icon(
6529                    Icon::new(IconName::XCircle)
6530                        .size(IconSize::Small)
6531                        .color(Color::Error),
6532                )
6533                .action("View Log", move |window, cx| {
6534                    let message = message.clone();
6535                    let action = action.clone();
6536                    workspace_weak
6537                        .update(cx, move |workspace, cx| {
6538                            open_output(action, workspace, &message, window, cx)
6539                        })
6540                        .ok();
6541                })
6542            });
6543            workspace.toggle_status_toast(toast, cx)
6544        });
6545    }
6546}
6547
6548fn rpc_error_raw_message_from_chain(error: &anyhow::Error) -> Option<&str> {
6549    error
6550        .chain()
6551        .find_map(|cause| cause.downcast_ref::<RpcError>().map(RpcError::raw_message))
6552}
6553
6554fn format_git_error_toast_message(error: &anyhow::Error) -> String {
6555    if let Some(message) = rpc_error_raw_message_from_chain(error) {
6556        message.trim().to_string()
6557    } else {
6558        error.to_string().trim().to_string()
6559    }
6560}
6561
6562#[cfg(test)]
6563mod tests {
6564    use git::{
6565        repository::repo_path,
6566        status::{StatusCode, UnmergedStatus, UnmergedStatusCode},
6567    };
6568    use gpui::{TestAppContext, UpdateGlobal, VisualTestContext, px};
6569    use indoc::indoc;
6570    use project::FakeFs;
6571    use serde_json::json;
6572    use settings::SettingsStore;
6573    use theme::LoadThemes;
6574    use util::path;
6575    use util::rel_path::rel_path;
6576
6577    use workspace::MultiWorkspace;
6578
6579    use super::*;
6580
6581    fn init_test(cx: &mut gpui::TestAppContext) {
6582        zlog::init_test();
6583
6584        cx.update(|cx| {
6585            let settings_store = SettingsStore::test(cx);
6586            cx.set_global(settings_store);
6587            theme_settings::init(LoadThemes::JustBase, cx);
6588            editor::init(cx);
6589            crate::init(cx);
6590        });
6591    }
6592
6593    #[test]
6594    fn test_format_git_error_toast_message_prefers_raw_rpc_message() {
6595        let rpc_error = RpcError::from_proto(
6596            &proto::Error {
6597                message:
6598                    "Your local changes to the following files would be overwritten by merge\n"
6599                        .to_string(),
6600                code: proto::ErrorCode::Internal as i32,
6601                tags: Default::default(),
6602            },
6603            "Pull",
6604        );
6605
6606        let message = format_git_error_toast_message(&rpc_error);
6607        assert_eq!(
6608            message,
6609            "Your local changes to the following files would be overwritten by merge"
6610        );
6611    }
6612
6613    #[test]
6614    fn test_format_git_error_toast_message_prefers_raw_rpc_message_when_wrapped() {
6615        let rpc_error = RpcError::from_proto(
6616            &proto::Error {
6617                message:
6618                    "Your local changes to the following files would be overwritten by merge\n"
6619                        .to_string(),
6620                code: proto::ErrorCode::Internal as i32,
6621                tags: Default::default(),
6622            },
6623            "Pull",
6624        );
6625        let wrapped = rpc_error.context("sending pull request");
6626
6627        let message = format_git_error_toast_message(&wrapped);
6628        assert_eq!(
6629            message,
6630            "Your local changes to the following files would be overwritten by merge"
6631        );
6632    }
6633
6634    #[gpui::test]
6635    async fn test_entry_worktree_paths(cx: &mut TestAppContext) {
6636        init_test(cx);
6637        let fs = FakeFs::new(cx.background_executor.clone());
6638        fs.insert_tree(
6639            "/root",
6640            json!({
6641                "zed": {
6642                    ".git": {},
6643                    "crates": {
6644                        "gpui": {
6645                            "gpui.rs": "fn main() {}"
6646                        },
6647                        "util": {
6648                            "util.rs": "fn do_it() {}"
6649                        }
6650                    }
6651                },
6652            }),
6653        )
6654        .await;
6655
6656        fs.set_status_for_repo(
6657            Path::new(path!("/root/zed/.git")),
6658            &[
6659                ("crates/gpui/gpui.rs", StatusCode::Modified.worktree()),
6660                ("crates/util/util.rs", StatusCode::Modified.worktree()),
6661            ],
6662        );
6663
6664        let project =
6665            Project::test(fs.clone(), [path!("/root/zed/crates/gpui").as_ref()], cx).await;
6666        let window_handle =
6667            cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
6668        let workspace = window_handle
6669            .read_with(cx, |mw, _| mw.workspace().clone())
6670            .unwrap();
6671        let cx = &mut VisualTestContext::from_window(window_handle.into(), cx);
6672
6673        cx.read(|cx| {
6674            project
6675                .read(cx)
6676                .worktrees(cx)
6677                .next()
6678                .unwrap()
6679                .read(cx)
6680                .as_local()
6681                .unwrap()
6682                .scan_complete()
6683        })
6684        .await;
6685
6686        cx.executor().run_until_parked();
6687
6688        let panel = workspace.update_in(cx, GitPanel::new);
6689
6690        let handle = cx.update_window_entity(&panel, |panel, _, _| {
6691            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
6692        });
6693        cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
6694        handle.await;
6695
6696        let entries = panel.read_with(cx, |panel, _| panel.entries.clone());
6697        pretty_assertions::assert_eq!(
6698            entries,
6699            [
6700                GitListEntry::Header(GitHeaderEntry {
6701                    header: Section::Tracked
6702                }),
6703                GitListEntry::Status(GitStatusEntry {
6704                    repo_path: repo_path("crates/gpui/gpui.rs"),
6705                    status: StatusCode::Modified.worktree(),
6706                    staging: StageStatus::Unstaged,
6707                    diff_stat: Some(DiffStat {
6708                        added: 1,
6709                        deleted: 1,
6710                    }),
6711                }),
6712                GitListEntry::Status(GitStatusEntry {
6713                    repo_path: repo_path("crates/util/util.rs"),
6714                    status: StatusCode::Modified.worktree(),
6715                    staging: StageStatus::Unstaged,
6716                    diff_stat: Some(DiffStat {
6717                        added: 1,
6718                        deleted: 1,
6719                    }),
6720                },),
6721            ],
6722        );
6723
6724        let handle = cx.update_window_entity(&panel, |panel, _, _| {
6725            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
6726        });
6727        cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
6728        handle.await;
6729        let entries = panel.read_with(cx, |panel, _| panel.entries.clone());
6730        pretty_assertions::assert_eq!(
6731            entries,
6732            [
6733                GitListEntry::Header(GitHeaderEntry {
6734                    header: Section::Tracked
6735                }),
6736                GitListEntry::Status(GitStatusEntry {
6737                    repo_path: repo_path("crates/gpui/gpui.rs"),
6738                    status: StatusCode::Modified.worktree(),
6739                    staging: StageStatus::Unstaged,
6740                    diff_stat: Some(DiffStat {
6741                        added: 1,
6742                        deleted: 1,
6743                    }),
6744                }),
6745                GitListEntry::Status(GitStatusEntry {
6746                    repo_path: repo_path("crates/util/util.rs"),
6747                    status: StatusCode::Modified.worktree(),
6748                    staging: StageStatus::Unstaged,
6749                    diff_stat: Some(DiffStat {
6750                        added: 1,
6751                        deleted: 1,
6752                    }),
6753                },),
6754            ],
6755        );
6756    }
6757
6758    #[gpui::test]
6759    async fn test_bulk_staging(cx: &mut TestAppContext) {
6760        use GitListEntry::*;
6761
6762        init_test(cx);
6763        let fs = FakeFs::new(cx.background_executor.clone());
6764        fs.insert_tree(
6765            "/root",
6766            json!({
6767                "project": {
6768                    ".git": {},
6769                    "src": {
6770                        "main.rs": "fn main() {}",
6771                        "lib.rs": "pub fn hello() {}",
6772                        "utils.rs": "pub fn util() {}"
6773                    },
6774                    "tests": {
6775                        "test.rs": "fn test() {}"
6776                    },
6777                    "new_file.txt": "new content",
6778                    "another_new.rs": "// new file",
6779                    "conflict.txt": "conflicted content"
6780                }
6781            }),
6782        )
6783        .await;
6784
6785        fs.set_status_for_repo(
6786            Path::new(path!("/root/project/.git")),
6787            &[
6788                ("src/main.rs", StatusCode::Modified.worktree()),
6789                ("src/lib.rs", StatusCode::Modified.worktree()),
6790                ("tests/test.rs", StatusCode::Modified.worktree()),
6791                ("new_file.txt", FileStatus::Untracked),
6792                ("another_new.rs", FileStatus::Untracked),
6793                ("src/utils.rs", FileStatus::Untracked),
6794                (
6795                    "conflict.txt",
6796                    UnmergedStatus {
6797                        first_head: UnmergedStatusCode::Updated,
6798                        second_head: UnmergedStatusCode::Updated,
6799                    }
6800                    .into(),
6801                ),
6802            ],
6803        );
6804
6805        let project = Project::test(fs.clone(), [Path::new(path!("/root/project"))], cx).await;
6806        let window_handle =
6807            cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
6808        let workspace = window_handle
6809            .read_with(cx, |mw, _| mw.workspace().clone())
6810            .unwrap();
6811        let cx = &mut VisualTestContext::from_window(window_handle.into(), cx);
6812
6813        cx.read(|cx| {
6814            project
6815                .read(cx)
6816                .worktrees(cx)
6817                .next()
6818                .unwrap()
6819                .read(cx)
6820                .as_local()
6821                .unwrap()
6822                .scan_complete()
6823        })
6824        .await;
6825
6826        cx.executor().run_until_parked();
6827
6828        let panel = workspace.update_in(cx, GitPanel::new);
6829
6830        let handle = cx.update_window_entity(&panel, |panel, _, _| {
6831            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
6832        });
6833        cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
6834        handle.await;
6835
6836        let entries = panel.read_with(cx, |panel, _| panel.entries.clone());
6837        #[rustfmt::skip]
6838        pretty_assertions::assert_matches!(
6839            entries.as_slice(),
6840            &[
6841                Header(GitHeaderEntry { header: Section::Conflict }),
6842                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
6843                Header(GitHeaderEntry { header: Section::Tracked }),
6844                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
6845                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
6846                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
6847                Header(GitHeaderEntry { header: Section::New }),
6848                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
6849                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
6850                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
6851            ],
6852        );
6853
6854        let second_status_entry = entries[3].clone();
6855        panel.update_in(cx, |panel, window, cx| {
6856            panel.toggle_staged_for_entry(&second_status_entry, window, cx);
6857        });
6858
6859        panel.update_in(cx, |panel, window, cx| {
6860            panel.selected_entry = Some(7);
6861            panel.stage_range(&git::StageRange, window, cx);
6862        });
6863
6864        cx.read(|cx| {
6865            project
6866                .read(cx)
6867                .worktrees(cx)
6868                .next()
6869                .unwrap()
6870                .read(cx)
6871                .as_local()
6872                .unwrap()
6873                .scan_complete()
6874        })
6875        .await;
6876
6877        cx.executor().run_until_parked();
6878
6879        let handle = cx.update_window_entity(&panel, |panel, _, _| {
6880            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
6881        });
6882        cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
6883        handle.await;
6884
6885        let entries = panel.read_with(cx, |panel, _| panel.entries.clone());
6886        #[rustfmt::skip]
6887        pretty_assertions::assert_matches!(
6888            entries.as_slice(),
6889            &[
6890                Header(GitHeaderEntry { header: Section::Conflict }),
6891                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
6892                Header(GitHeaderEntry { header: Section::Tracked }),
6893                Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
6894                Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
6895                Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
6896                Header(GitHeaderEntry { header: Section::New }),
6897                Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
6898                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
6899                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
6900            ],
6901        );
6902
6903        let third_status_entry = entries[4].clone();
6904        panel.update_in(cx, |panel, window, cx| {
6905            panel.toggle_staged_for_entry(&third_status_entry, window, cx);
6906        });
6907
6908        panel.update_in(cx, |panel, window, cx| {
6909            panel.selected_entry = Some(9);
6910            panel.stage_range(&git::StageRange, window, cx);
6911        });
6912
6913        cx.read(|cx| {
6914            project
6915                .read(cx)
6916                .worktrees(cx)
6917                .next()
6918                .unwrap()
6919                .read(cx)
6920                .as_local()
6921                .unwrap()
6922                .scan_complete()
6923        })
6924        .await;
6925
6926        cx.executor().run_until_parked();
6927
6928        let handle = cx.update_window_entity(&panel, |panel, _, _| {
6929            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
6930        });
6931        cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
6932        handle.await;
6933
6934        let entries = panel.read_with(cx, |panel, _| panel.entries.clone());
6935        #[rustfmt::skip]
6936        pretty_assertions::assert_matches!(
6937            entries.as_slice(),
6938            &[
6939                Header(GitHeaderEntry { header: Section::Conflict }),
6940                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
6941                Header(GitHeaderEntry { header: Section::Tracked }),
6942                Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
6943                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
6944                Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
6945                Header(GitHeaderEntry { header: Section::New }),
6946                Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
6947                Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
6948                Status(GitStatusEntry { staging: StageStatus::Staged, .. }),
6949            ],
6950        );
6951    }
6952
6953    #[gpui::test]
6954    async fn test_bulk_staging_with_sort_by_paths(cx: &mut TestAppContext) {
6955        use GitListEntry::*;
6956
6957        init_test(cx);
6958        let fs = FakeFs::new(cx.background_executor.clone());
6959        fs.insert_tree(
6960            "/root",
6961            json!({
6962                "project": {
6963                    ".git": {},
6964                    "src": {
6965                        "main.rs": "fn main() {}",
6966                        "lib.rs": "pub fn hello() {}",
6967                        "utils.rs": "pub fn util() {}"
6968                    },
6969                    "tests": {
6970                        "test.rs": "fn test() {}"
6971                    },
6972                    "new_file.txt": "new content",
6973                    "another_new.rs": "// new file",
6974                    "conflict.txt": "conflicted content"
6975                }
6976            }),
6977        )
6978        .await;
6979
6980        fs.set_status_for_repo(
6981            Path::new(path!("/root/project/.git")),
6982            &[
6983                ("src/main.rs", StatusCode::Modified.worktree()),
6984                ("src/lib.rs", StatusCode::Modified.worktree()),
6985                ("tests/test.rs", StatusCode::Modified.worktree()),
6986                ("new_file.txt", FileStatus::Untracked),
6987                ("another_new.rs", FileStatus::Untracked),
6988                ("src/utils.rs", FileStatus::Untracked),
6989                (
6990                    "conflict.txt",
6991                    UnmergedStatus {
6992                        first_head: UnmergedStatusCode::Updated,
6993                        second_head: UnmergedStatusCode::Updated,
6994                    }
6995                    .into(),
6996                ),
6997            ],
6998        );
6999
7000        let project = Project::test(fs.clone(), [Path::new(path!("/root/project"))], cx).await;
7001        let window_handle =
7002            cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
7003        let workspace = window_handle
7004            .read_with(cx, |mw, _| mw.workspace().clone())
7005            .unwrap();
7006        let cx = &mut VisualTestContext::from_window(window_handle.into(), cx);
7007
7008        cx.read(|cx| {
7009            project
7010                .read(cx)
7011                .worktrees(cx)
7012                .next()
7013                .unwrap()
7014                .read(cx)
7015                .as_local()
7016                .unwrap()
7017                .scan_complete()
7018        })
7019        .await;
7020
7021        cx.executor().run_until_parked();
7022
7023        let panel = workspace.update_in(cx, GitPanel::new);
7024
7025        let handle = cx.update_window_entity(&panel, |panel, _, _| {
7026            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
7027        });
7028        cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
7029        handle.await;
7030
7031        let entries = panel.read_with(cx, |panel, _| panel.entries.clone());
7032        #[rustfmt::skip]
7033        pretty_assertions::assert_matches!(
7034            entries.as_slice(),
7035            &[
7036                Header(GitHeaderEntry { header: Section::Conflict }),
7037                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
7038                Header(GitHeaderEntry { header: Section::Tracked }),
7039                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
7040                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
7041                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
7042                Header(GitHeaderEntry { header: Section::New }),
7043                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
7044                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
7045                Status(GitStatusEntry { staging: StageStatus::Unstaged, .. }),
7046            ],
7047        );
7048
7049        assert_entry_paths(
7050            &entries,
7051            &[
7052                None,
7053                Some("conflict.txt"),
7054                None,
7055                Some("src/lib.rs"),
7056                Some("src/main.rs"),
7057                Some("tests/test.rs"),
7058                None,
7059                Some("another_new.rs"),
7060                Some("new_file.txt"),
7061                Some("src/utils.rs"),
7062            ],
7063        );
7064
7065        let second_status_entry = entries[3].clone();
7066        panel.update_in(cx, |panel, window, cx| {
7067            panel.toggle_staged_for_entry(&second_status_entry, window, cx);
7068        });
7069
7070        cx.update(|_window, cx| {
7071            SettingsStore::update_global(cx, |store, cx| {
7072                store.update_user_settings(cx, |settings| {
7073                    settings.git_panel.get_or_insert_default().sort_by_path = Some(true);
7074                })
7075            });
7076        });
7077
7078        panel.update_in(cx, |panel, window, cx| {
7079            panel.selected_entry = Some(7);
7080            panel.stage_range(&git::StageRange, window, cx);
7081        });
7082
7083        cx.read(|cx| {
7084            project
7085                .read(cx)
7086                .worktrees(cx)
7087                .next()
7088                .unwrap()
7089                .read(cx)
7090                .as_local()
7091                .unwrap()
7092                .scan_complete()
7093        })
7094        .await;
7095
7096        cx.executor().run_until_parked();
7097
7098        let handle = cx.update_window_entity(&panel, |panel, _, _| {
7099            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
7100        });
7101        cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
7102        handle.await;
7103
7104        let entries = panel.read_with(cx, |panel, _| panel.entries.clone());
7105        #[rustfmt::skip]
7106        pretty_assertions::assert_matches!(
7107            entries.as_slice(),
7108            &[
7109                Status(GitStatusEntry { status: FileStatus::Untracked, staging: StageStatus::Unstaged, .. }),
7110                Status(GitStatusEntry { status: FileStatus::Unmerged(..), staging: StageStatus::Unstaged, .. }),
7111                Status(GitStatusEntry { status: FileStatus::Untracked, staging: StageStatus::Unstaged, .. }),
7112                Status(GitStatusEntry { status: FileStatus::Tracked(..), staging: StageStatus::Staged, .. }),
7113                Status(GitStatusEntry { status: FileStatus::Tracked(..), staging: StageStatus::Unstaged, .. }),
7114                Status(GitStatusEntry { status: FileStatus::Untracked, staging: StageStatus::Unstaged, .. }),
7115                Status(GitStatusEntry { status: FileStatus::Tracked(..), staging: StageStatus::Unstaged, .. }),
7116            ],
7117        );
7118
7119        assert_entry_paths(
7120            &entries,
7121            &[
7122                Some("another_new.rs"),
7123                Some("conflict.txt"),
7124                Some("new_file.txt"),
7125                Some("src/lib.rs"),
7126                Some("src/main.rs"),
7127                Some("src/utils.rs"),
7128                Some("tests/test.rs"),
7129            ],
7130        );
7131
7132        let third_status_entry = entries[4].clone();
7133        panel.update_in(cx, |panel, window, cx| {
7134            panel.toggle_staged_for_entry(&third_status_entry, window, cx);
7135        });
7136
7137        panel.update_in(cx, |panel, window, cx| {
7138            panel.selected_entry = Some(9);
7139            panel.stage_range(&git::StageRange, window, cx);
7140        });
7141
7142        cx.read(|cx| {
7143            project
7144                .read(cx)
7145                .worktrees(cx)
7146                .next()
7147                .unwrap()
7148                .read(cx)
7149                .as_local()
7150                .unwrap()
7151                .scan_complete()
7152        })
7153        .await;
7154
7155        cx.executor().run_until_parked();
7156
7157        let handle = cx.update_window_entity(&panel, |panel, _, _| {
7158            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
7159        });
7160        cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
7161        handle.await;
7162
7163        let entries = panel.read_with(cx, |panel, _| panel.entries.clone());
7164        #[rustfmt::skip]
7165        pretty_assertions::assert_matches!(
7166            entries.as_slice(),
7167            &[
7168                Status(GitStatusEntry { status: FileStatus::Untracked, staging: StageStatus::Unstaged, .. }),
7169                Status(GitStatusEntry { status: FileStatus::Unmerged(..), staging: StageStatus::Unstaged, .. }),
7170                Status(GitStatusEntry { status: FileStatus::Untracked, staging: StageStatus::Unstaged, .. }),
7171                Status(GitStatusEntry { status: FileStatus::Tracked(..), staging: StageStatus::Staged, .. }),
7172                Status(GitStatusEntry { status: FileStatus::Tracked(..), staging: StageStatus::Staged, .. }),
7173                Status(GitStatusEntry { status: FileStatus::Untracked, staging: StageStatus::Unstaged, .. }),
7174                Status(GitStatusEntry { status: FileStatus::Tracked(..), staging: StageStatus::Unstaged, .. }),
7175            ],
7176        );
7177
7178        assert_entry_paths(
7179            &entries,
7180            &[
7181                Some("another_new.rs"),
7182                Some("conflict.txt"),
7183                Some("new_file.txt"),
7184                Some("src/lib.rs"),
7185                Some("src/main.rs"),
7186                Some("src/utils.rs"),
7187                Some("tests/test.rs"),
7188            ],
7189        );
7190    }
7191
7192    #[gpui::test]
7193    async fn test_amend_commit_message_handling(cx: &mut TestAppContext) {
7194        init_test(cx);
7195        let fs = FakeFs::new(cx.background_executor.clone());
7196        fs.insert_tree(
7197            "/root",
7198            json!({
7199                "project": {
7200                    ".git": {},
7201                    "src": {
7202                        "main.rs": "fn main() {}"
7203                    }
7204                }
7205            }),
7206        )
7207        .await;
7208
7209        fs.set_status_for_repo(
7210            Path::new(path!("/root/project/.git")),
7211            &[("src/main.rs", StatusCode::Modified.worktree())],
7212        );
7213
7214        let project = Project::test(fs.clone(), [Path::new(path!("/root/project"))], cx).await;
7215        let window_handle =
7216            cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
7217        let workspace = window_handle
7218            .read_with(cx, |mw, _| mw.workspace().clone())
7219            .unwrap();
7220        let cx = &mut VisualTestContext::from_window(window_handle.into(), cx);
7221
7222        let panel = workspace.update_in(cx, GitPanel::new);
7223
7224        // Test: User has commit message, enables amend (saves message), then disables (restores message)
7225        panel.update(cx, |panel, cx| {
7226            panel.commit_message_buffer(cx).update(cx, |buffer, cx| {
7227                let start = buffer.anchor_before(0);
7228                let end = buffer.anchor_after(buffer.len());
7229                buffer.edit([(start..end, "Initial commit message")], None, cx);
7230            });
7231
7232            panel.set_amend_pending(true, cx);
7233            assert!(panel.original_commit_message.is_some());
7234
7235            panel.set_amend_pending(false, cx);
7236            let current_message = panel.commit_message_buffer(cx).read(cx).text();
7237            assert_eq!(current_message, "Initial commit message");
7238            assert!(panel.original_commit_message.is_none());
7239        });
7240
7241        // Test: User has empty commit message, enables amend, then disables (clears message)
7242        panel.update(cx, |panel, cx| {
7243            panel.commit_message_buffer(cx).update(cx, |buffer, cx| {
7244                let start = buffer.anchor_before(0);
7245                let end = buffer.anchor_after(buffer.len());
7246                buffer.edit([(start..end, "")], None, cx);
7247            });
7248
7249            panel.set_amend_pending(true, cx);
7250            assert!(panel.original_commit_message.is_none());
7251
7252            panel.commit_message_buffer(cx).update(cx, |buffer, cx| {
7253                let start = buffer.anchor_before(0);
7254                let end = buffer.anchor_after(buffer.len());
7255                buffer.edit([(start..end, "Previous commit message")], None, cx);
7256            });
7257
7258            panel.set_amend_pending(false, cx);
7259            let current_message = panel.commit_message_buffer(cx).read(cx).text();
7260            assert_eq!(current_message, "");
7261        });
7262    }
7263
7264    #[gpui::test]
7265    async fn test_amend(cx: &mut TestAppContext) {
7266        init_test(cx);
7267        let fs = FakeFs::new(cx.background_executor.clone());
7268        fs.insert_tree(
7269            "/root",
7270            json!({
7271                "project": {
7272                    ".git": {},
7273                    "src": {
7274                        "main.rs": "fn main() {}"
7275                    }
7276                }
7277            }),
7278        )
7279        .await;
7280
7281        fs.set_status_for_repo(
7282            Path::new(path!("/root/project/.git")),
7283            &[("src/main.rs", StatusCode::Modified.worktree())],
7284        );
7285
7286        let project = Project::test(fs.clone(), [Path::new(path!("/root/project"))], cx).await;
7287        let window_handle =
7288            cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
7289        let workspace = window_handle
7290            .read_with(cx, |mw, _| mw.workspace().clone())
7291            .unwrap();
7292        let cx = &mut VisualTestContext::from_window(window_handle.into(), cx);
7293
7294        // Wait for the project scanning to finish so that `head_commit(cx)` is
7295        // actually set, otherwise no head commit would be available from which
7296        // to fetch the latest commit message from.
7297        cx.executor().run_until_parked();
7298
7299        let panel = workspace.update_in(cx, GitPanel::new);
7300        panel.read_with(cx, |panel, cx| {
7301            assert!(panel.active_repository.is_some());
7302            assert!(panel.head_commit(cx).is_some());
7303        });
7304
7305        panel.update_in(cx, |panel, window, cx| {
7306            // Update the commit editor's message to ensure that its contents
7307            // are later restored, after amending is finished.
7308            panel.commit_message_buffer(cx).update(cx, |buffer, cx| {
7309                buffer.set_text("refactor: update main.rs", cx);
7310            });
7311
7312            // Start amending the previous commit.
7313            panel.focus_editor(&Default::default(), window, cx);
7314            panel.on_amend(&Amend, window, cx);
7315        });
7316
7317        // Since `GitPanel.amend` attempts to fetch the latest commit message in
7318        // a background task, we need to wait for it to complete before being
7319        // able to assert that the commit message editor's state has been
7320        // updated.
7321        cx.run_until_parked();
7322
7323        panel.update_in(cx, |panel, window, cx| {
7324            assert_eq!(
7325                panel.commit_message_buffer(cx).read(cx).text(),
7326                "initial commit"
7327            );
7328            assert_eq!(
7329                panel.original_commit_message,
7330                Some("refactor: update main.rs".to_string())
7331            );
7332
7333            // Finish amending the previous commit.
7334            panel.focus_editor(&Default::default(), window, cx);
7335            panel.on_amend(&Amend, window, cx);
7336        });
7337
7338        // Since the actual commit logic is run in a background task, we need to
7339        // await its completion to actually ensure that the commit message
7340        // editor's contents are set to the original message and haven't been
7341        // cleared.
7342        cx.run_until_parked();
7343
7344        panel.update_in(cx, |panel, _window, cx| {
7345            // After amending, the commit editor's message should be restored to
7346            // the original message.
7347            assert_eq!(
7348                panel.commit_message_buffer(cx).read(cx).text(),
7349                "refactor: update main.rs"
7350            );
7351            assert!(panel.original_commit_message.is_none());
7352        });
7353    }
7354
7355    #[gpui::test]
7356    async fn test_open_diff(cx: &mut TestAppContext) {
7357        init_test(cx);
7358
7359        let fs = FakeFs::new(cx.background_executor.clone());
7360        fs.insert_tree(
7361            path!("/project"),
7362            json!({
7363                ".git": {},
7364                "tracked": "tracked\n",
7365                "untracked": "\n",
7366            }),
7367        )
7368        .await;
7369
7370        fs.set_head_and_index_for_repo(
7371            path!("/project/.git").as_ref(),
7372            &[("tracked", "old tracked\n".into())],
7373        );
7374
7375        let project = Project::test(fs.clone(), [Path::new(path!("/project"))], cx).await;
7376        let window_handle =
7377            cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
7378        let workspace = window_handle
7379            .read_with(cx, |mw, _| mw.workspace().clone())
7380            .unwrap();
7381        let cx = &mut VisualTestContext::from_window(window_handle.into(), cx);
7382        let panel = workspace.update_in(cx, GitPanel::new);
7383
7384        // Enable the `sort_by_path` setting and wait for entries to be updated,
7385        // as there should no longer be separators between Tracked and Untracked
7386        // files.
7387        cx.update(|_window, cx| {
7388            SettingsStore::update_global(cx, |store, cx| {
7389                store.update_user_settings(cx, |settings| {
7390                    settings.git_panel.get_or_insert_default().sort_by_path = Some(true);
7391                })
7392            });
7393        });
7394
7395        cx.update_window_entity(&panel, |panel, _, _| {
7396            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
7397        })
7398        .await;
7399
7400        // Confirm that `Open Diff` still works for the untracked file, updating
7401        // the Project Diff's active path.
7402        panel.update_in(cx, |panel, window, cx| {
7403            panel.selected_entry = Some(1);
7404            panel.open_diff(&menu::Confirm, window, cx);
7405        });
7406        cx.run_until_parked();
7407
7408        workspace.update_in(cx, |workspace, _window, cx| {
7409            let active_path = workspace
7410                .item_of_type::<ProjectDiff>(cx)
7411                .expect("ProjectDiff should exist")
7412                .read(cx)
7413                .active_path(cx)
7414                .expect("active_path should exist");
7415
7416            assert_eq!(active_path.path, rel_path("untracked").into_arc());
7417        });
7418    }
7419
7420    #[gpui::test]
7421    async fn test_tree_view_reveals_collapsed_parent_on_select_entry_by_path(
7422        cx: &mut TestAppContext,
7423    ) {
7424        init_test(cx);
7425
7426        let fs = FakeFs::new(cx.background_executor.clone());
7427        fs.insert_tree(
7428            path!("/project"),
7429            json!({
7430                ".git": {},
7431                "src": {
7432                    "a": {
7433                        "foo.rs": "fn foo() {}",
7434                    },
7435                    "b": {
7436                        "bar.rs": "fn bar() {}",
7437                    },
7438                },
7439            }),
7440        )
7441        .await;
7442
7443        fs.set_status_for_repo(
7444            path!("/project/.git").as_ref(),
7445            &[
7446                ("src/a/foo.rs", StatusCode::Modified.worktree()),
7447                ("src/b/bar.rs", StatusCode::Modified.worktree()),
7448            ],
7449        );
7450
7451        let project = Project::test(fs.clone(), [Path::new(path!("/project"))], cx).await;
7452        let window_handle =
7453            cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
7454        let workspace = window_handle
7455            .read_with(cx, |mw, _| mw.workspace().clone())
7456            .unwrap();
7457        let cx = &mut VisualTestContext::from_window(window_handle.into(), cx);
7458
7459        cx.read(|cx| {
7460            project
7461                .read(cx)
7462                .worktrees(cx)
7463                .next()
7464                .unwrap()
7465                .read(cx)
7466                .as_local()
7467                .unwrap()
7468                .scan_complete()
7469        })
7470        .await;
7471
7472        cx.executor().run_until_parked();
7473
7474        cx.update(|_window, cx| {
7475            SettingsStore::update_global(cx, |store, cx| {
7476                store.update_user_settings(cx, |settings| {
7477                    settings.git_panel.get_or_insert_default().tree_view = Some(true);
7478                })
7479            });
7480        });
7481
7482        let panel = workspace.update_in(cx, GitPanel::new);
7483
7484        let handle = cx.update_window_entity(&panel, |panel, _, _| {
7485            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
7486        });
7487        cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
7488        handle.await;
7489
7490        let src_key = panel.read_with(cx, |panel, _| {
7491            panel
7492                .entries
7493                .iter()
7494                .find_map(|entry| match entry {
7495                    GitListEntry::Directory(dir) if dir.key.path == repo_path("src") => {
7496                        Some(dir.key.clone())
7497                    }
7498                    _ => None,
7499                })
7500                .expect("src directory should exist in tree view")
7501        });
7502
7503        panel.update_in(cx, |panel, window, cx| {
7504            panel.toggle_directory(&src_key, window, cx);
7505        });
7506
7507        panel.read_with(cx, |panel, _| {
7508            let state = panel
7509                .view_mode
7510                .tree_state()
7511                .expect("tree view state should exist");
7512            assert_eq!(state.expanded_dirs.get(&src_key).copied(), Some(false));
7513        });
7514
7515        let worktree_id =
7516            cx.read(|cx| project.read(cx).worktrees(cx).next().unwrap().read(cx).id());
7517        let project_path = ProjectPath {
7518            worktree_id,
7519            path: RelPath::unix("src/a/foo.rs").unwrap().into_arc(),
7520        };
7521
7522        panel.update_in(cx, |panel, window, cx| {
7523            panel.select_entry_by_path(project_path, window, cx);
7524        });
7525
7526        panel.read_with(cx, |panel, _| {
7527            let state = panel
7528                .view_mode
7529                .tree_state()
7530                .expect("tree view state should exist");
7531            assert_eq!(state.expanded_dirs.get(&src_key).copied(), Some(true));
7532
7533            let selected_ix = panel.selected_entry.expect("selection should be set");
7534            assert!(state.logical_indices.contains(&selected_ix));
7535
7536            let selected_entry = panel
7537                .entries
7538                .get(selected_ix)
7539                .and_then(|entry| entry.status_entry())
7540                .expect("selected entry should be a status entry");
7541            assert_eq!(selected_entry.repo_path, repo_path("src/a/foo.rs"));
7542        });
7543    }
7544
7545    #[gpui::test]
7546    async fn test_tree_view_select_next_at_last_visible_collapsed_directory(
7547        cx: &mut TestAppContext,
7548    ) {
7549        init_test(cx);
7550
7551        let fs = FakeFs::new(cx.background_executor.clone());
7552        fs.insert_tree(
7553            path!("/project"),
7554            json!({
7555                ".git": {},
7556                "bar": {
7557                    "bar1.py": "print('bar1')",
7558                    "bar2.py": "print('bar2')",
7559                },
7560                "foo": {
7561                    "foo1.py": "print('foo1')",
7562                    "foo2.py": "print('foo2')",
7563                },
7564                "foobar.py": "print('foobar')",
7565            }),
7566        )
7567        .await;
7568
7569        fs.set_status_for_repo(
7570            path!("/project/.git").as_ref(),
7571            &[
7572                ("bar/bar1.py", StatusCode::Modified.worktree()),
7573                ("bar/bar2.py", StatusCode::Modified.worktree()),
7574                ("foo/foo1.py", StatusCode::Modified.worktree()),
7575                ("foo/foo2.py", StatusCode::Modified.worktree()),
7576                ("foobar.py", FileStatus::Untracked),
7577            ],
7578        );
7579
7580        let project = Project::test(fs.clone(), [Path::new(path!("/project"))], cx).await;
7581        let window_handle =
7582            cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
7583        let workspace = window_handle
7584            .read_with(cx, |mw, _| mw.workspace().clone())
7585            .unwrap();
7586        let cx = &mut VisualTestContext::from_window(window_handle.into(), cx);
7587
7588        cx.read(|cx| {
7589            project
7590                .read(cx)
7591                .worktrees(cx)
7592                .next()
7593                .unwrap()
7594                .read(cx)
7595                .as_local()
7596                .unwrap()
7597                .scan_complete()
7598        })
7599        .await;
7600
7601        cx.executor().run_until_parked();
7602        cx.update(|_window, cx| {
7603            SettingsStore::update_global(cx, |store, cx| {
7604                store.update_user_settings(cx, |settings| {
7605                    settings.git_panel.get_or_insert_default().tree_view = Some(true);
7606                })
7607            });
7608        });
7609
7610        let panel = workspace.update_in(cx, GitPanel::new);
7611        let handle = cx.update_window_entity(&panel, |panel, _, _| {
7612            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
7613        });
7614
7615        cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
7616        handle.await;
7617
7618        let foo_key = panel.read_with(cx, |panel, _| {
7619            panel
7620                .entries
7621                .iter()
7622                .find_map(|entry| match entry {
7623                    GitListEntry::Directory(dir) if dir.key.path == repo_path("foo") => {
7624                        Some(dir.key.clone())
7625                    }
7626                    _ => None,
7627                })
7628                .expect("foo directory should exist in tree view")
7629        });
7630
7631        panel.update_in(cx, |panel, window, cx| {
7632            panel.toggle_directory(&foo_key, window, cx);
7633        });
7634
7635        let foo_idx = panel.read_with(cx, |panel, _| {
7636            let state = panel
7637                .view_mode
7638                .tree_state()
7639                .expect("tree view state should exist");
7640            assert_eq!(state.expanded_dirs.get(&foo_key).copied(), Some(false));
7641
7642            let foo_idx = panel
7643                .entries
7644                .iter()
7645                .enumerate()
7646                .find_map(|(index, entry)| match entry {
7647                    GitListEntry::Directory(dir) if dir.key.path == repo_path("foo") => Some(index),
7648                    _ => None,
7649                })
7650                .expect("foo directory should exist in tree view");
7651
7652            let foo_logical_idx = state
7653                .logical_indices
7654                .iter()
7655                .position(|&index| index == foo_idx)
7656                .expect("foo directory should be visible");
7657            let next_logical_idx = state.logical_indices[foo_logical_idx + 1];
7658            assert!(matches!(
7659                panel.entries.get(next_logical_idx),
7660                Some(GitListEntry::Header(GitHeaderEntry {
7661                    header: Section::New
7662                }))
7663            ));
7664
7665            foo_idx
7666        });
7667
7668        panel.update_in(cx, |panel, window, cx| {
7669            panel.selected_entry = Some(foo_idx);
7670            panel.select_next(&menu::SelectNext, window, cx);
7671        });
7672
7673        panel.read_with(cx, |panel, _| {
7674            let selected_idx = panel.selected_entry.expect("selection should be set");
7675            let selected_entry = panel
7676                .entries
7677                .get(selected_idx)
7678                .and_then(|entry| entry.status_entry())
7679                .expect("selected entry should be a status entry");
7680            assert_eq!(selected_entry.repo_path, repo_path("foobar.py"));
7681        });
7682    }
7683
7684    fn assert_entry_paths(entries: &[GitListEntry], expected_paths: &[Option<&str>]) {
7685        assert_eq!(entries.len(), expected_paths.len());
7686        for (entry, expected_path) in entries.iter().zip(expected_paths) {
7687            assert_eq!(
7688                entry.status_entry().map(|status| status
7689                    .repo_path
7690                    .as_ref()
7691                    .as_std_path()
7692                    .to_string_lossy()
7693                    .to_string()),
7694                expected_path.map(|s| s.to_string())
7695            );
7696        }
7697    }
7698
7699    #[test]
7700    fn test_compress_diff_no_truncation() {
7701        let diff = indoc! {"
7702            --- a/file.txt
7703            +++ b/file.txt
7704            @@ -1,2 +1,2 @@
7705            -old
7706            +new
7707        "};
7708        let result = GitPanel::compress_commit_diff(diff, 1000);
7709        assert_eq!(result, diff);
7710    }
7711
7712    #[test]
7713    fn test_compress_diff_truncate_long_lines() {
7714        let long_line = "🦀".repeat(300);
7715        let diff = indoc::formatdoc! {"
7716            --- a/file.txt
7717            +++ b/file.txt
7718            @@ -1,2 +1,3 @@
7719             context
7720            +{}
7721             more context
7722        ", long_line};
7723        let result = GitPanel::compress_commit_diff(&diff, 100);
7724        assert!(result.contains("...[truncated]"));
7725        assert!(result.len() < diff.len());
7726    }
7727
7728    #[test]
7729    fn test_compress_diff_truncate_hunks() {
7730        let diff = indoc! {"
7731            --- a/file.txt
7732            +++ b/file.txt
7733            @@ -1,2 +1,2 @@
7734             context
7735            -old1
7736            +new1
7737            @@ -5,2 +5,2 @@
7738             context 2
7739            -old2
7740            +new2
7741            @@ -10,2 +10,2 @@
7742             context 3
7743            -old3
7744            +new3
7745        "};
7746        let result = GitPanel::compress_commit_diff(diff, 100);
7747        let expected = indoc! {"
7748            --- a/file.txt
7749            +++ b/file.txt
7750            @@ -1,2 +1,2 @@
7751             context
7752            -old1
7753            +new1
7754            [...skipped 2 hunks...]
7755        "};
7756        assert_eq!(result, expected);
7757    }
7758
7759    #[gpui::test]
7760    async fn test_suggest_commit_message(cx: &mut TestAppContext) {
7761        init_test(cx);
7762
7763        let fs = FakeFs::new(cx.background_executor.clone());
7764        fs.insert_tree(
7765            path!("/project"),
7766            json!({
7767                ".git": {},
7768                "tracked": "tracked\n",
7769                "untracked": "\n",
7770            }),
7771        )
7772        .await;
7773
7774        fs.set_head_and_index_for_repo(
7775            path!("/project/.git").as_ref(),
7776            &[("tracked", "old tracked\n".into())],
7777        );
7778
7779        let project = Project::test(fs.clone(), [Path::new(path!("/project"))], cx).await;
7780        let window_handle =
7781            cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
7782        let workspace = window_handle
7783            .read_with(cx, |mw, _| mw.workspace().clone())
7784            .unwrap();
7785        let cx = &mut VisualTestContext::from_window(window_handle.into(), cx);
7786        let panel = workspace.update_in(cx, GitPanel::new);
7787
7788        let handle = cx.update_window_entity(&panel, |panel, _, _| {
7789            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
7790        });
7791        cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
7792        handle.await;
7793
7794        let entries = panel.read_with(cx, |panel, _| panel.entries.clone());
7795
7796        // GitPanel
7797        // - Tracked:
7798        // - [] tracked
7799        // - Untracked
7800        // - [] untracked
7801        //
7802        // The commit message should now read:
7803        // "Update tracked"
7804        let message = panel.update(cx, |panel, cx| panel.suggest_commit_message(cx));
7805        assert_eq!(message, Some("Update tracked".to_string()));
7806
7807        let first_status_entry = entries[1].clone();
7808        panel.update_in(cx, |panel, window, cx| {
7809            panel.toggle_staged_for_entry(&first_status_entry, window, cx);
7810        });
7811
7812        cx.read(|cx| {
7813            project
7814                .read(cx)
7815                .worktrees(cx)
7816                .next()
7817                .unwrap()
7818                .read(cx)
7819                .as_local()
7820                .unwrap()
7821                .scan_complete()
7822        })
7823        .await;
7824
7825        cx.executor().run_until_parked();
7826
7827        let handle = cx.update_window_entity(&panel, |panel, _, _| {
7828            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
7829        });
7830        cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
7831        handle.await;
7832
7833        // GitPanel
7834        // - Tracked:
7835        // - [x] tracked
7836        // - Untracked
7837        // - [] untracked
7838        //
7839        // The commit message should still read:
7840        // "Update tracked"
7841        let message = panel.update(cx, |panel, cx| panel.suggest_commit_message(cx));
7842        assert_eq!(message, Some("Update tracked".to_string()));
7843
7844        let second_status_entry = entries[3].clone();
7845        panel.update_in(cx, |panel, window, cx| {
7846            panel.toggle_staged_for_entry(&second_status_entry, window, cx);
7847        });
7848
7849        cx.read(|cx| {
7850            project
7851                .read(cx)
7852                .worktrees(cx)
7853                .next()
7854                .unwrap()
7855                .read(cx)
7856                .as_local()
7857                .unwrap()
7858                .scan_complete()
7859        })
7860        .await;
7861
7862        cx.executor().run_until_parked();
7863
7864        let handle = cx.update_window_entity(&panel, |panel, _, _| {
7865            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
7866        });
7867        cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
7868        handle.await;
7869
7870        // GitPanel
7871        // - Tracked:
7872        // - [x] tracked
7873        // - Untracked
7874        // - [x] untracked
7875        //
7876        // The commit message should now read:
7877        // "Enter commit message"
7878        // (which means we should see None returned).
7879        let message = panel.update(cx, |panel, cx| panel.suggest_commit_message(cx));
7880        assert!(message.is_none());
7881
7882        panel.update_in(cx, |panel, window, cx| {
7883            panel.toggle_staged_for_entry(&first_status_entry, window, cx);
7884        });
7885
7886        cx.read(|cx| {
7887            project
7888                .read(cx)
7889                .worktrees(cx)
7890                .next()
7891                .unwrap()
7892                .read(cx)
7893                .as_local()
7894                .unwrap()
7895                .scan_complete()
7896        })
7897        .await;
7898
7899        cx.executor().run_until_parked();
7900
7901        let handle = cx.update_window_entity(&panel, |panel, _, _| {
7902            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
7903        });
7904        cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
7905        handle.await;
7906
7907        // GitPanel
7908        // - Tracked:
7909        // - [] tracked
7910        // - Untracked
7911        // - [x] untracked
7912        //
7913        // The commit message should now read:
7914        // "Update untracked"
7915        let message = panel.update(cx, |panel, cx| panel.suggest_commit_message(cx));
7916        assert_eq!(message, Some("Create untracked".to_string()));
7917
7918        panel.update_in(cx, |panel, window, cx| {
7919            panel.toggle_staged_for_entry(&second_status_entry, window, cx);
7920        });
7921
7922        cx.read(|cx| {
7923            project
7924                .read(cx)
7925                .worktrees(cx)
7926                .next()
7927                .unwrap()
7928                .read(cx)
7929                .as_local()
7930                .unwrap()
7931                .scan_complete()
7932        })
7933        .await;
7934
7935        cx.executor().run_until_parked();
7936
7937        let handle = cx.update_window_entity(&panel, |panel, _, _| {
7938            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
7939        });
7940        cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
7941        handle.await;
7942
7943        // GitPanel
7944        // - Tracked:
7945        // - [] tracked
7946        // - Untracked
7947        // - [] untracked
7948        //
7949        // The commit message should now read:
7950        // "Update tracked"
7951        let message = panel.update(cx, |panel, cx| panel.suggest_commit_message(cx));
7952        assert_eq!(message, Some("Update tracked".to_string()));
7953    }
7954
7955    #[test]
7956    fn test_git_output_handler_strips_ansi_codes() {
7957        use alacritty_terminal::vte::ansi;
7958
7959        let cases = [
7960            ("no escape codes here\n", "no escape codes here\n"),
7961            ("\x1b[31mhello\x1b[0m", "hello"),
7962            ("\x1b[1;32mfoo\x1b[0m bar", "foo bar"),
7963            ("progress 10%\rprogress 100%\n", "progress 100%\n"),
7964        ];
7965
7966        for (input, expected) in cases {
7967            let mut handler = GitOutputHandler::default();
7968            let mut processor = ansi::Processor::<ansi::StdSyncHandler>::default();
7969            processor.advance(&mut handler, input.as_bytes());
7970            assert_eq!(handler.output, expected);
7971        }
7972    }
7973
7974    #[gpui::test]
7975    async fn test_dispatch_context_with_focus_states(cx: &mut TestAppContext) {
7976        init_test(cx);
7977
7978        let fs = FakeFs::new(cx.background_executor.clone());
7979        fs.insert_tree(
7980            path!("/project"),
7981            json!({
7982                ".git": {},
7983                "tracked": "tracked\n",
7984            }),
7985        )
7986        .await;
7987
7988        fs.set_head_and_index_for_repo(
7989            path!("/project/.git").as_ref(),
7990            &[("tracked", "old tracked\n".into())],
7991        );
7992
7993        let project = Project::test(fs.clone(), [Path::new(path!("/project"))], cx).await;
7994        let window_handle =
7995            cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
7996        let workspace = window_handle
7997            .read_with(cx, |mw, _| mw.workspace().clone())
7998            .unwrap();
7999        let cx = &mut VisualTestContext::from_window(window_handle.into(), cx);
8000        let panel = workspace.update_in(cx, GitPanel::new);
8001
8002        let handle = cx.update_window_entity(&panel, |panel, _, _| {
8003            std::mem::replace(&mut panel.update_visible_entries_task, Task::ready(()))
8004        });
8005        cx.executor().advance_clock(2 * UPDATE_DEBOUNCE);
8006        handle.await;
8007
8008        // Case 1: Focus the commit editor — should have "CommitEditor" but NOT "menu"/"ChangesList"
8009        panel.update_in(cx, |panel, window, cx| {
8010            panel.focus_editor(&FocusEditor, window, cx);
8011            let editor_is_focused = panel.commit_editor.read(cx).is_focused(window);
8012            assert!(
8013                editor_is_focused,
8014                "commit editor should be focused after focus_editor action"
8015            );
8016            let context = panel.dispatch_context(window, cx);
8017            assert!(
8018                context.contains("GitPanel"),
8019                "should always have GitPanel context"
8020            );
8021            assert!(
8022                context.contains("CommitEditor"),
8023                "should have CommitEditor context when commit editor is focused"
8024            );
8025            assert!(
8026                !context.contains("menu"),
8027                "should not have menu context when commit editor is focused"
8028            );
8029            assert!(
8030                !context.contains("ChangesList"),
8031                "should not have ChangesList context when commit editor is focused"
8032            );
8033        });
8034
8035        // Case 2: Focus the panel's focus handle directly — should have "menu" and "ChangesList".
8036        // We force a draw via simulate_resize to ensure the dispatch tree is populated,
8037        // since contains_focused() depends on the rendered dispatch tree.
8038        panel.update_in(cx, |panel, window, cx| {
8039            panel.focus_handle.focus(window, cx);
8040        });
8041        cx.simulate_resize(gpui::size(px(800.), px(600.)));
8042
8043        panel.update_in(cx, |panel, window, cx| {
8044            let context = panel.dispatch_context(window, cx);
8045            assert!(
8046                context.contains("GitPanel"),
8047                "should always have GitPanel context"
8048            );
8049            assert!(
8050                context.contains("menu"),
8051                "should have menu context when changes list is focused"
8052            );
8053            assert!(
8054                context.contains("ChangesList"),
8055                "should have ChangesList context when changes list is focused"
8056            );
8057            assert!(
8058                !context.contains("CommitEditor"),
8059                "should not have CommitEditor context when changes list is focused"
8060            );
8061        });
8062
8063        // Case 3: Switch back to commit editor and verify context switches correctly
8064        panel.update_in(cx, |panel, window, cx| {
8065            panel.focus_editor(&FocusEditor, window, cx);
8066        });
8067
8068        panel.update_in(cx, |panel, window, cx| {
8069            let context = panel.dispatch_context(window, cx);
8070            assert!(
8071                context.contains("CommitEditor"),
8072                "should have CommitEditor after switching focus back to editor"
8073            );
8074            assert!(
8075                !context.contains("menu"),
8076                "should not have menu after switching focus back to editor"
8077            );
8078        });
8079
8080        // Case 4: Re-focus changes list and verify it transitions back correctly
8081        panel.update_in(cx, |panel, window, cx| {
8082            panel.focus_handle.focus(window, cx);
8083        });
8084        cx.simulate_resize(gpui::size(px(800.), px(600.)));
8085
8086        panel.update_in(cx, |panel, window, cx| {
8087            assert!(
8088                panel.focus_handle.contains_focused(window, cx),
8089                "panel focus handle should report contains_focused when directly focused"
8090            );
8091            let context = panel.dispatch_context(window, cx);
8092            assert!(
8093                context.contains("menu"),
8094                "should have menu context after re-focusing changes list"
8095            );
8096            assert!(
8097                context.contains("ChangesList"),
8098                "should have ChangesList context after re-focusing changes list"
8099            );
8100        });
8101    }
8102}