998 lines · 33.3 KB · Rust
RawDownload
use anyhow::{Context as _, Result};use buffer_diff::{BufferDiff, BufferDiffSnapshot};use editor::display_map::{BlockPlacement, BlockProperties, BlockStyle};use editor::{Editor, EditorEvent, ExcerptRange, MultiBuffer, multibuffer_context_lines};use git::repository::{CommitDetails, CommitDiff, RepoPath};use git::{    BuildCommitPermalinkParams, GitHostingProviderRegistry, GitRemote, ParsedGitRemote,    parse_git_remote_url,};use gpui::{    AnyElement, App, AppContext as _, AsyncApp, AsyncWindowContext, Context, Element, Entity,    EventEmitter, FocusHandle, Focusable, InteractiveElement, IntoElement, ParentElement,    PromptLevel, Render, Styled, Task, WeakEntity, Window, actions,};use language::{    Anchor, Buffer, Capability, DiskState, File, LanguageRegistry, LineEnding, OffsetRangeExt as _,    Point, ReplicaId, Rope, TextBuffer,};use multi_buffer::PathKey;use project::{Project, WorktreeId, git_store::Repository};use std::{    any::{Any, TypeId},    path::PathBuf,    sync::Arc,};use theme::ActiveTheme;use ui::{DiffStat, Tooltip, prelude::*};use util::{ResultExt, paths::PathStyle, rel_path::RelPath, truncate_and_trailoff};use workspace::item::TabTooltipContent;use workspace::{    Item, ItemHandle, ItemNavHistory, ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView,    Workspace,    item::{BreadcrumbText, ItemEvent, TabContentParams},    notifications::NotifyTaskExt,    pane::SaveIntent,    searchable::SearchableItemHandle,}; use crate::commit_tooltip::CommitAvatar;use crate::git_panel::GitPanel; actions!(git, [ApplyCurrentStash, PopCurrentStash, DropCurrentStash,]); pub fn init(cx: &mut App) {    cx.observe_new(|workspace: &mut Workspace, _window, _cx| {        workspace.register_action(|workspace, _: &ApplyCurrentStash, window, cx| {            CommitView::apply_stash(workspace, window, cx);        });        workspace.register_action(|workspace, _: &DropCurrentStash, window, cx| {            CommitView::remove_stash(workspace, window, cx);        });        workspace.register_action(|workspace, _: &PopCurrentStash, window, cx| {            CommitView::pop_stash(workspace, window, cx);        });    })    .detach();} pub struct CommitView {    commit: CommitDetails,    editor: Entity<Editor>,    stash: Option<usize>,    multibuffer: Entity<MultiBuffer>,    repository: Entity<Repository>,    remote: Option<GitRemote>,} struct GitBlob {    path: RepoPath,    worktree_id: WorktreeId,    is_deleted: bool,    display_name: String,} const COMMIT_MESSAGE_SORT_PREFIX: u64 = 0;const FILE_NAMESPACE_SORT_PREFIX: u64 = 1; impl CommitView {    pub fn open(        commit_sha: String,        repo: WeakEntity<Repository>,        workspace: WeakEntity<Workspace>,        stash: Option<usize>,        file_filter: Option<RepoPath>,        window: &mut Window,        cx: &mut App,    ) {        let commit_diff = repo            .update(cx, |repo, _| repo.load_commit_diff(commit_sha.clone()))            .ok();        let commit_details = repo            .update(cx, |repo, _| repo.show(commit_sha.clone()))            .ok();         window            .spawn(cx, async move |cx| {                let (commit_diff, commit_details) = futures::join!(commit_diff?, commit_details?);                let mut commit_diff = commit_diff.log_err()?.log_err()?;                let commit_details = commit_details.log_err()?.log_err()?;                 // Filter to specific file if requested                if let Some(ref filter_path) = file_filter {                    commit_diff.files.retain(|f| &f.path == filter_path);                }                 let repo = repo.upgrade()?;                 workspace                    .update_in(cx, |workspace, window, cx| {                        let project = workspace.project();                        let commit_view = cx.new(|cx| {                            CommitView::new(                                commit_details,                                commit_diff,                                repo,                                project.clone(),                                stash,                                window,                                cx,                            )                        });                         let pane = workspace.active_pane();                        pane.update(cx, |pane, cx| {                            let ix = pane.items().position(|item| {                                let commit_view = item.downcast::<CommitView>();                                commit_view                                    .is_some_and(|view| view.read(cx).commit.sha == commit_sha)                            });                            if let Some(ix) = ix {                                pane.activate_item(ix, true, true, window, cx);                            } else {                                pane.add_item(Box::new(commit_view), true, true, None, window, cx);                            }                        })                    })                    .log_err()            })            .detach();    }     fn new(        commit: CommitDetails,        commit_diff: CommitDiff,        repository: Entity<Repository>,        project: Entity<Project>,        stash: Option<usize>,        window: &mut Window,        cx: &mut Context<Self>,    ) -> Self {        let language_registry = project.read(cx).languages().clone();        let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadOnly));         let message_buffer = cx.new(|cx| {            let mut buffer = Buffer::local(commit.message.clone(), cx);            buffer.set_capability(Capability::ReadOnly, cx);            buffer        });         multibuffer.update(cx, |multibuffer, cx| {            let snapshot = message_buffer.read(cx).snapshot();            let full_range = Point::zero()..snapshot.max_point();            let range = ExcerptRange {                context: full_range.clone(),                primary: full_range,            };            multibuffer.set_excerpt_ranges_for_path(                PathKey::with_sort_prefix(                    COMMIT_MESSAGE_SORT_PREFIX,                    RelPath::unix("commit message").unwrap().into(),                ),                message_buffer.clone(),                &snapshot,                vec![range],                cx,            )        });         let editor = cx.new(|cx| {            let mut editor =                Editor::for_multibuffer(multibuffer.clone(), Some(project.clone()), window, cx);             editor.disable_inline_diagnostics();            editor.set_show_breakpoints(false, cx);            editor.set_expand_all_diff_hunks(cx);            editor.disable_header_for_buffer(message_buffer.read(cx).remote_id(), cx);            editor.disable_indent_guides_for_buffer(message_buffer.read(cx).remote_id(), cx);             editor.insert_blocks(                [BlockProperties {                    placement: BlockPlacement::Above(editor::Anchor::min()),                    height: Some(1),                    style: BlockStyle::Sticky,                    render: Arc::new(|_| gpui::Empty.into_any_element()),                    priority: 0,                }]                .into_iter()                .chain(                    editor                        .buffer()                        .read(cx)                        .buffer_anchor_to_anchor(&message_buffer, Anchor::MAX, cx)                        .map(|anchor| BlockProperties {                            placement: BlockPlacement::Below(anchor),                            height: Some(1),                            style: BlockStyle::Sticky,                            render: Arc::new(|_| gpui::Empty.into_any_element()),                            priority: 0,                        }),                ),                None,                cx,            );             editor        });         let commit_sha = Arc::<str>::from(commit.sha.as_ref());         let first_worktree_id = project            .read(cx)            .worktrees(cx)            .next()            .map(|worktree| worktree.read(cx).id());         let repository_clone = repository.clone();         cx.spawn(async move |this, cx| {            for file in commit_diff.files {                let is_deleted = file.new_text.is_none();                let new_text = file.new_text.unwrap_or_default();                let old_text = file.old_text;                let worktree_id = repository_clone                    .update(cx, |repository, cx| {                        repository                            .repo_path_to_project_path(&file.path, cx)                            .map(|path| path.worktree_id)                            .or(first_worktree_id)                    })?                    .context("project has no worktrees")?;                let short_sha = commit_sha.get(0..7).unwrap_or(&commit_sha);                let file_name = file                    .path                    .file_name()                    .map(|name| name.to_string())                    .unwrap_or_else(|| file.path.display(PathStyle::local()).to_string());                let display_name = format!("{short_sha} - {file_name}");                 let file = Arc::new(GitBlob {                    path: file.path.clone(),                    is_deleted,                    worktree_id,                    display_name,                }) as Arc<dyn language::File>;                 let buffer = build_buffer(new_text, file, &language_registry, cx).await?;                let buffer_diff =                    build_buffer_diff(old_text, &buffer, &language_registry, cx).await?;                 this.update(cx, |this, cx| {                    this.multibuffer.update(cx, |multibuffer, cx| {                        let snapshot = buffer.read(cx).snapshot();                        let path = snapshot.file().unwrap().path().clone();                        let excerpt_ranges = {                            let mut hunks = buffer_diff.read(cx).hunks(&snapshot, cx).peekable();                            if hunks.peek().is_none() {                                vec![language::Point::zero()..snapshot.max_point()]                            } else {                                hunks                                    .map(|hunk| hunk.buffer_range.to_point(&snapshot))                                    .collect::<Vec<_>>()                            }                        };                         let _is_newly_added = multibuffer.set_excerpts_for_path(                            PathKey::with_sort_prefix(FILE_NAMESPACE_SORT_PREFIX, path),                            buffer,                            excerpt_ranges,                            multibuffer_context_lines(cx),                            cx,                        );                        multibuffer.add_diff(buffer_diff, cx);                    });                })?;            }             anyhow::Ok(())        })        .detach();         let snapshot = repository.read(cx).snapshot();        let remote_url = snapshot            .remote_upstream_url            .as_ref()            .or(snapshot.remote_origin_url.as_ref());         let remote = remote_url.and_then(|url| {            let provider_registry = GitHostingProviderRegistry::default_global(cx);            parse_git_remote_url(provider_registry, url).map(|(host, parsed)| GitRemote {                host,                owner: parsed.owner.into(),                repo: parsed.repo.into(),            })        });         Self {            commit,            editor,            multibuffer,            stash,            repository,            remote,        }    }     fn render_commit_avatar(        &self,        sha: &SharedString,        size: impl Into<gpui::AbsoluteLength>,        window: &mut Window,        cx: &mut App,    ) -> AnyElement {        let size = size.into();        let avatar = CommitAvatar::new(sha, self.remote.as_ref());         v_flex()            .w(size)            .h(size)            .border_1()            .border_color(cx.theme().colors().border)            .rounded_full()            .justify_center()            .items_center()            .child(                avatar                    .avatar(window, cx)                    .map(|a| a.size(size).into_any_element())                    .unwrap_or_else(|| {                        Icon::new(IconName::Person)                            .color(Color::Muted)                            .size(IconSize::Medium)                            .into_any_element()                    }),            )            .into_any()    }     fn calculate_changed_lines(&self, cx: &App) -> (u32, u32) {        let snapshot = self.multibuffer.read(cx).snapshot(cx);        let mut total_additions = 0u32;        let mut total_deletions = 0u32;         let mut seen_buffers = std::collections::HashSet::new();        for (_, buffer, _) in snapshot.excerpts() {            let buffer_id = buffer.remote_id();            if !seen_buffers.insert(buffer_id) {                continue;            }             let Some(diff) = snapshot.diff_for_buffer_id(buffer_id) else {                continue;            };             let base_text = diff.base_text();             for hunk in diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, buffer) {                let added_rows = hunk.range.end.row.saturating_sub(hunk.range.start.row);                total_additions += added_rows;                 let base_start = base_text                    .offset_to_point(hunk.diff_base_byte_range.start)                    .row;                let base_end = base_text.offset_to_point(hunk.diff_base_byte_range.end).row;                let deleted_rows = base_end.saturating_sub(base_start);                 total_deletions += deleted_rows;            }        }         (total_additions, total_deletions)    }     fn render_header(&self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {        let commit = &self.commit;        let author_name = commit.author_name.clone();        let commit_date = time::OffsetDateTime::from_unix_timestamp(commit.commit_timestamp)            .unwrap_or_else(|_| time::OffsetDateTime::now_utc());        let local_offset = time::UtcOffset::current_local_offset().unwrap_or(time::UtcOffset::UTC);        let date_string = time_format::format_localized_timestamp(            commit_date,            time::OffsetDateTime::now_utc(),            local_offset,            time_format::TimestampFormat::MediumAbsolute,        );         let remote_info = self.remote.as_ref().map(|remote| {            let provider = remote.host.name();            let parsed_remote = ParsedGitRemote {                owner: remote.owner.as_ref().into(),                repo: remote.repo.as_ref().into(),            };            let params = BuildCommitPermalinkParams { sha: &commit.sha };            let url = remote                .host                .build_commit_permalink(&parsed_remote, params)                .to_string();            (provider, url)        });         let (additions, deletions) = self.calculate_changed_lines(cx);         let commit_diff_stat = if additions > 0 || deletions > 0 {            Some(DiffStat::new(                "commit-diff-stat",                additions as usize,                deletions as usize,            ))        } else {            None        };         let gutter_width = self.editor.update(cx, |editor, cx| {            let snapshot = editor.snapshot(window, cx);            let style = editor.style(cx);            let font_id = window.text_system().resolve_font(&style.text.font());            let font_size = style.text.font_size.to_pixels(window.rem_size());            snapshot                .gutter_dimensions(font_id, font_size, style, window, cx)                .full_width()        });         h_flex()            .border_b_1()            .border_color(cx.theme().colors().border_variant)            .w_full()            .child(                h_flex()                    .w(gutter_width)                    .justify_center()                    .child(self.render_commit_avatar(&commit.sha, rems_from_px(48.), window, cx)),            )            .child(                h_flex()                    .py_4()                    .pl_1()                    .pr_4()                    .w_full()                    .items_start()                    .justify_between()                    .flex_wrap()                    .child(                        v_flex()                            .child(                                h_flex()                                    .gap_1()                                    .child(Label::new(author_name).color(Color::Default))                                    .child(                                        Label::new(format!("Commit:{}", commit.sha))                                            .color(Color::Muted)                                            .size(LabelSize::Small)                                            .truncate()                                            .buffer_font(cx),                                    ),                            )                            .child(                                h_flex()                                    .gap_1p5()                                    .child(                                        Label::new(date_string)                                            .color(Color::Muted)                                            .size(LabelSize::Small),                                    )                                    .child(                                        Label::new("•")                                            .color(Color::Ignored)                                            .size(LabelSize::Small),                                    )                                    .children(commit_diff_stat),                            ),                    )                    .children(remote_info.map(|(provider_name, url)| {                        let icon = match provider_name.as_str() {                            "GitHub" => IconName::Github,                            _ => IconName::Link,                        };                         Button::new("view_on_provider", format!("View on {}", provider_name))                            .icon(icon)                            .icon_color(Color::Muted)                            .icon_size(IconSize::Small)                            .icon_position(IconPosition::Start)                            .on_click(move |_, _, cx| cx.open_url(&url))                    })),            )    }     fn apply_stash(workspace: &mut Workspace, window: &mut Window, cx: &mut App) {        Self::stash_action(            workspace,            "Apply",

Lines 1–500 of 998.