10028 lines · 364.3 KB · Rust
RawDownload
use crate::askpass_modal::AskPassModal;use crate::commit_modal::CommitModal;use crate::commit_tooltip::{CommitAvatar, CommitTooltip};use crate::commit_view::CommitView;use crate::git_panel_settings::GitPanelScrollbarAccessor;use crate::project_diff::{BranchDiff, Diff, ProjectDiff};use crate::remote_output::{self, RemoteAction, SuccessMessage};use crate::solo_diff_view::SoloDiffView;use crate::{branch_picker, picker_prompt, render_remote_button};use crate::{    git_panel_settings::GitPanelSettings, git_status_icon, repository_selector::RepositorySelector,};use agent_settings::{AgentSettings, UserAgentsMd};use anyhow::Context as _;use askpass::AskPassDelegate;use collections::{BTreeMap, HashMap, HashSet};use db::kvp::KeyValueStore;use editor::{Editor, EditorElement, EditorMode, MultiBuffer, MultiBufferOffset, SizingBehavior};use editor::{EditorStyle, RewrapOptions};use file_icons::FileIcons;use futures::StreamExt as _;use futures::channel::oneshot::Canceled;use git::Oid;use git::commit::ParsedCommitMessage;use git::repository::{    Branch, CommitData, CommitDetails, CommitOptions, CommitSummary, DiffType, FetchOptions,    GitCommitTemplate, GitCommitter, LogOrder, LogSource, PushOptions, Remote, RemoteCommandOutput,    ResetMode, Upstream, UpstreamTracking, UpstreamTrackingStatus, get_git_committer,};use git::stash::GitStash;use git::status::{DiffStat, StageStatus};use git::{Amend, Commit, Signoff, ToggleStaged, repository::RepoPath, status::FileStatus};use git::{    ExpandCommitEditor, GitHostingProviderRegistry, GitRemote, RestoreTrackedFiles, StageAll,    StashAll, StashApply, StashPop, ToggleFillCommitEditor, TrashUntrackedFiles, UnstageAll,    ViewFile, parse_git_remote_url,};use gpui::{    AbsoluteLength, Action, Anchor, AsyncApp, AsyncWindowContext, Bounds, ClickEvent, DismissEvent,    Empty, Entity, EventEmitter, FocusHandle, Focusable, KeyContext, MouseButton, MouseDownEvent,    Point, PromptLevel, ScrollStrategy, Subscription, Task, TaskExt, TextStyle,    UniformListScrollHandle, WeakEntity, actions, anchored, deferred, point, size, uniform_list,};use itertools::Itertools;use language::{Buffer, File};use language_model::{    CompletionIntent, ConfiguredModel, Event as LanguageModelEvent, LanguageModelRegistry,    LanguageModelRequest, LanguageModelRequestMessage, Role,};use menu;use multi_buffer::ExcerptBoundaryInfo;use notifications::status_toast::StatusToast;use panel::PanelHeader;use project::git_store::GitAccess;use project::{    Fs, Project, ProjectPath,    git_store::{        CommitDataState, GitStoreEvent, Repository, RepositoryEvent, RepositoryId, pending_op,    },    project_settings::{GitPathStyle, ProjectSettings},};use prompt_store::RULES_FILE_NAMES;use proto::RpcError;use serde::{Deserialize, Serialize};use settings::{    GitPanelClickBehavior, GitPanelGroupBy, GitPanelSortBy, Settings, SettingsStore, StatusStyle,    update_settings_file,};use smallvec::SmallVec;use std::cell::Cell;use std::future::Future;use std::ops::Range;use std::path::Path;use std::rc::Rc;use std::{sync::Arc, time::Duration, usize};use strum::{IntoEnumIterator, VariantNames};use theme_settings::ThemeSettings;use time::OffsetDateTime;use ui::{    ButtonLike, Checkbox, ContextMenu, ContextMenuEntry, Divider, ElevationIndex,    IndentGuideColors, KeyBinding, PopoverMenu, ProjectEmptyState, RenderedIndentGuide, ScrollAxes,    Scrollbars, SplitButton, Tab, TintColor, Tooltip, WithScrollbar, prelude::*,};use util::paths::PathStyle;use util::{ResultExt, TryFutureExt, markdown::MarkdownInlineCode, maybe, rel_path::RelPath};use workspace::SERIALIZATION_THROTTLE_TIME;use workspace::{    Item, Workspace,    dock::{DockPosition, Panel, PanelEvent},    notifications::{DetachAndPromptErr, NotificationId, NotifyTaskExt},};use zed_actions::{DecreaseBufferFontSize, IncreaseBufferFontSize, ResetBufferFontSize}; const GIT_PANEL_KEY: &str = "GitPanel";const UPDATE_DEBOUNCE: Duration = Duration::from_millis(50);// TODO: We should revise this part. It seems the indentation width is not aligned with the one in project panelconst TREE_INDENT: f32 = 16.0; actions!(    git_panel,    [        /// Closes the git panel.        Close,        /// Toggles the git panel.        Toggle,        /// Toggles focus on the git panel.        ToggleFocus,        /// Opens the git panel menu.        OpenMenu,        /// Focuses on the commit message editor.        FocusEditor,        /// Focuses on the changes list.        FocusChanges,        /// Select next git panel menu item, and show it in the diff view        NextEntry,        /// Select previous git panel menu item, and show it in the diff view        PreviousEntry,        /// Select first git panel menu item, and show it in the diff view        FirstEntry,        /// Select last git panel menu item, and show it in the diff view        LastEntry,        /// Toggles automatic co-author suggestions.        ToggleFillCoAuthors,        /// Sorts entries by path.        SetSortByPath,        /// Sorts entries by name.        SetSortByName,        /// Disables grouping entries by status.        SetGroupByNone,        /// Groups entries by status.        SetGroupByStatus,        /// Toggles showing entries in tree vs flat view.        ToggleTreeView,        /// Expands the selected entry to show its children.        ExpandSelectedEntry,        /// Collapses the selected entry to hide its children.        CollapseSelectedEntry,        /// Activates the Changes tab.        ActivateChangesTab,        /// Activates the History tab.        ActivateHistoryTab,    ]); actions!(    dev,    [        /// Shows the current git job queue debug state for the active repository.        ShowGitJobQueue,    ]); fn prompt<T>(    msg: &str,    detail: Option<&str>,    window: &mut Window,    cx: &mut App,) -> Task<anyhow::Result<T>>where    T: IntoEnumIterator + VariantNames + 'static,{    let rx = window.prompt(PromptLevel::Info, msg, detail, T::VARIANTS, cx);    cx.spawn(async move |_| Ok(T::iter().nth(rx.await?).unwrap()))} #[derive(strum::EnumIter, strum::VariantNames)]#[strum(serialize_all = "title_case")]enum TrashCancel {    Trash,    Cancel,} #[derive(Clone, Copy)]struct GitPanelViewOptionsMenuState {    sort_by: GitPanelSortBy,    group_by: GitPanelGroupBy,    tree_view: bool,} fn git_panel_context_menu(    has_tracked_changes: bool,    has_staged_changes: bool,    has_unstaged_changes: bool,    has_new_changes: bool,    has_stash_items: bool,    focus_handle: FocusHandle,    window: &mut Window,    cx: &mut App,) -> Entity<ContextMenu> {    ContextMenu::build(window, cx, |context_menu, _, _| {        context_menu            .context(focus_handle.clone())            .action_disabled_when(!has_unstaged_changes, "Stage All", StageAll.boxed_clone())            .action_disabled_when(!has_staged_changes, "Unstage All", UnstageAll.boxed_clone())            .separator()            .action_disabled_when(                !(has_new_changes || has_tracked_changes),                "Stash All",                StashAll.boxed_clone(),            )            .action_disabled_when(!has_stash_items, "Stash Pop", StashPop.boxed_clone())            .action("View Stash", zed_actions::git::ViewStash.boxed_clone())            .separator()            .action_disabled_when(                !has_tracked_changes,                "Discard Tracked Changes",                RestoreTrackedFiles.boxed_clone(),            )            .action_disabled_when(                !has_new_changes,                "Trash Untracked Files",                TrashUntrackedFiles.boxed_clone(),            )    })} fn git_panel_view_options_menu(    focus_handle: FocusHandle,    window: &mut Window,    cx: &mut App,) -> Entity<ContextMenu> {    let view_options_menu_state = Rc::new(Cell::new(GitPanelViewOptionsMenuState {        sort_by: GitPanelSettings::get_global(cx).sort_by,        group_by: GitPanelSettings::get_global(cx).group_by,        tree_view: GitPanelSettings::get_global(cx).tree_view,    }));     ContextMenu::build_persistent(window, cx, move |context_menu, _, _| {        let state = view_options_menu_state.get();         context_menu            .context(focus_handle.clone())            .header("View")            .item({                let view_options_menu_state = view_options_menu_state.clone();                ContextMenuEntry::new("List")                    .toggle(IconPosition::End, !state.tree_view)                    .handler(move |window, cx| {                        if state.tree_view {                            view_options_menu_state.set(GitPanelViewOptionsMenuState {                                tree_view: false,                                ..state                            });                            window.dispatch_action(Box::new(ToggleTreeView), cx);                        }                    })            })            .item({                let view_options_menu_state = view_options_menu_state.clone();                ContextMenuEntry::new("Tree")                    .toggle(IconPosition::End, state.tree_view)                    .handler(move |window, cx| {                        if !state.tree_view {                            view_options_menu_state.set(GitPanelViewOptionsMenuState {                                tree_view: true,                                ..state                            });                            window.dispatch_action(Box::new(ToggleTreeView), cx);                        }                    })            })            .when(!state.tree_view, |this| {                this.separator()                    .header("Sort By")                    .item({                        let view_options_menu_state = view_options_menu_state.clone();                        ContextMenuEntry::new("Path")                            .toggle(IconPosition::End, state.sort_by == GitPanelSortBy::Path)                            .handler(move |window, cx| {                                if !state.tree_view {                                    view_options_menu_state.set(GitPanelViewOptionsMenuState {                                        sort_by: GitPanelSortBy::Path,                                        ..state                                    });                                    window.dispatch_action(Box::new(SetSortByPath), cx);                                }                            })                    })                    .item({                        let view_options_menu_state = view_options_menu_state.clone();                        ContextMenuEntry::new("Name")                            .toggle(IconPosition::End, state.sort_by == GitPanelSortBy::Name)                            .handler(move |window, cx| {                                if !state.tree_view {                                    view_options_menu_state.set(GitPanelViewOptionsMenuState {                                        sort_by: GitPanelSortBy::Name,                                        ..state                                    });                                    window.dispatch_action(Box::new(SetSortByName), cx);                                }                            })                    })            })            .separator()            .header("Group By")            .item({                let view_options_menu_state = view_options_menu_state.clone();                ContextMenuEntry::new("None")                    .toggle(IconPosition::End, state.group_by == GitPanelGroupBy::None)                    .handler(move |window, cx| {                        if state.group_by != GitPanelGroupBy::None {                            view_options_menu_state.set(GitPanelViewOptionsMenuState {                                group_by: GitPanelGroupBy::None,                                ..state                            });                            window.dispatch_action(Box::new(SetGroupByNone), cx);                        }                    })            })            .item({                let view_options_menu_state = view_options_menu_state.clone();                ContextMenuEntry::new("Status")                    .toggle(IconPosition::End, state.group_by == GitPanelGroupBy::Status)                    .handler(move |window, cx| {                        if state.group_by != GitPanelGroupBy::Status {                            view_options_menu_state.set(GitPanelViewOptionsMenuState {                                group_by: GitPanelGroupBy::Status,                                ..state                            });                            window.dispatch_action(Box::new(SetGroupByStatus), cx);                        }                    })            })    })} // We only allow a single remote operation at a time to avoid concurrent// credential prompts and competing ref/working-tree updates.#[derive(Clone, Copy)]pub(crate) enum RemoteOperationKind {    Fetch,    Pull,    Push,} pub fn register(workspace: &mut Workspace) {    workspace.register_action(|workspace, _: &ToggleFocus, window, cx| {        workspace.toggle_panel_focus::<GitPanel>(window, cx);    });    workspace.register_action(|workspace, _: &Toggle, window, cx| {        if !workspace.toggle_panel_focus::<GitPanel>(window, cx) {            workspace.close_panel::<GitPanel>(window, cx);        }    });    workspace.register_action(|workspace, _: &ExpandCommitEditor, window, cx| {        CommitModal::toggle(workspace, None, window, cx)    });    workspace.register_action(|workspace, _: &ToggleFillCommitEditor, window, cx| {        if let Some(panel) = workspace.panel::<GitPanel>(cx) {            panel.update(cx, |panel, cx| {                panel.toggle_fill_commit_editor(&Default::default(), window, cx)            });        }    });    workspace.register_action(|workspace, _: &git::Init, window, cx| {        if let Some(panel) = workspace.panel::<GitPanel>(cx) {            panel.update(cx, |panel, cx| panel.git_init(window, cx));        }    });    workspace.register_action(|workspace, _: &ShowGitJobQueue, window, cx| {        if let Some(panel) = workspace.panel::<GitPanel>(cx) {            panel.update(cx, |panel, cx| {                panel.show_git_job_queue(window, cx);            });        }    });} #[derive(Debug, Clone)]pub enum Event {    Focus,} #[derive(Serialize, Deserialize)]struct SerializedGitPanel {    #[serde(default)]    amend_pending: bool,    #[serde(default)]    signoff_enabled: bool,} #[derive(Debug, PartialEq, Eq, Clone, Copy)]enum GitPanelTab {    Changes,    History,} #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]enum Section {    Conflict,    Tracked,    New,} #[derive(Debug, PartialEq, Eq, Clone)]struct GitHeaderEntry {    header: Section,} impl GitHeaderEntry {    pub fn contains(&self, status_entry: &GitStatusEntry, repo: &Repository) -> bool {        let this = &self.header;        let status = status_entry.status;        match this {            Section::Conflict => {                repo.had_conflict_on_last_merge_head_change(&status_entry.repo_path)            }            Section::Tracked => !status.is_created(),            Section::New => status.is_created(),        }    }    pub fn title(&self) -> &'static str {        match self.header {            Section::Conflict => "Conflicts",            Section::Tracked => "Tracked",            Section::New => "Untracked",        }    }} #[derive(Debug, PartialEq, Eq, Clone)]enum GitListEntry {    Status(GitStatusEntry),    TreeStatus(GitTreeStatusEntry),    Directory(GitTreeDirEntry),    Header(GitHeaderEntry),} impl GitListEntry {    fn status_entry(&self) -> Option<&GitStatusEntry> {        match self {            GitListEntry::Status(entry) => Some(entry),            GitListEntry::TreeStatus(entry) => Some(&entry.entry),            _ => None,        }    }     fn directory_entry(&self) -> Option<&GitTreeDirEntry> {        match self {            GitListEntry::Directory(entry) => Some(entry),            _ => None,        }    }     /// Returns the tree indentation depth for this entry.    fn depth(&self) -> usize {        match self {            GitListEntry::Directory(dir) => dir.depth,            GitListEntry::TreeStatus(status) => status.depth,            _ => 0,        }    }} enum GitPanelViewMode {    Flat,    Tree(TreeViewState),} impl GitPanelViewMode {    fn from_settings(cx: &App) -> Self {        if GitPanelSettings::get_global(cx).tree_view {            GitPanelViewMode::Tree(TreeViewState::default())        } else {            GitPanelViewMode::Flat        }    }     fn tree_state(&self) -> Option<&TreeViewState> {        match self {            GitPanelViewMode::Tree(state) => Some(state),            GitPanelViewMode::Flat => None,        }    }     fn tree_state_mut(&mut self) -> Option<&mut TreeViewState> {        match self {            GitPanelViewMode::Tree(state) => Some(state),            GitPanelViewMode::Flat => None,        }    }} #[derive(Default)]struct TreeViewState {    // Maps visible index to actual entry index.    // Length equals the number of visible entries.    // This is needed because some entries (like collapsed directories) may be hidden.    logical_indices: Vec<usize>,    expanded_dirs: HashMap<RepoPath, bool>,    directory_descendants: HashMap<TreeKey, Vec<GitStatusEntry>>,} impl TreeViewState {    fn build_tree_entries(        &mut self,        section: Section,        mut entries: Vec<GitStatusEntry>,        seen_directories: &mut HashSet<TreeKey>,    ) -> Vec<(GitListEntry, bool)> {

Lines 1–500 of 10028.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t