git_ui: Render avatars in git blame gutter (#39168)

Release Notes:

- Added setting to render avatar in blame gutter
This commit is contained in:
Lukas Wirth
2025-09-30 06:55:09 +00:00
committed by GitHub
parent 5922f4adce
commit cc19387853
19 changed files with 267 additions and 230 deletions
+149 -165
View File
@@ -1,5 +1,5 @@
use crate::{
commit_tooltip::{CommitAvatar, CommitDetails, CommitTooltip},
commit_tooltip::{CommitAvatar, CommitTooltip},
commit_view::CommitView,
};
use editor::{BlameRenderer, Editor, hover_markdown_style};
@@ -17,7 +17,7 @@ use settings::Settings as _;
use theme::ThemeSettings;
use time::OffsetDateTime;
use time_format::format_local_timestamp;
use ui::{ContextMenu, Divider, IconButtonShape, prelude::*};
use ui::{ContextMenu, Divider, IconButtonShape, prelude::*, tooltip_container};
use workspace::Workspace;
const GIT_BLAME_MAX_AUTHOR_CHARS_DISPLAYED: usize = 20;
@@ -39,6 +39,7 @@ impl BlameRenderer for GitBlameRenderer {
editor: Entity<Editor>,
ix: usize,
sha_color: Hsla,
window: &mut Window,
cx: &mut App,
) -> Option<AnyElement> {
let relative_timestamp = blame_entry_relative_timestamp(&blame_entry);
@@ -46,6 +47,19 @@ impl BlameRenderer for GitBlameRenderer {
let author_name = blame_entry.author.as_deref().unwrap_or("<no name>");
let name = util::truncate_and_trailoff(author_name, GIT_BLAME_MAX_AUTHOR_CHARS_DISPLAYED);
let avatar = if ProjectSettings::get_global(cx)
.git
.inline_blame
.show_commit_summary
{
None
} else {
CommitAvatar::new(
&blame_entry.sha.to_string().into(),
details.as_ref().and_then(|it| it.remote.as_ref()),
)
.render(window, cx)
};
Some(
div()
.mr_2()
@@ -63,6 +77,7 @@ impl BlameRenderer for GitBlameRenderer {
.items_center()
.gap_2()
.child(div().text_color(sha_color).child(short_commit_id))
.children(avatar)
.child(name),
)
.child(relative_timestamp)
@@ -179,31 +194,22 @@ impl BlameRenderer for GitBlameRenderer {
.and_then(|t| OffsetDateTime::from_unix_timestamp(t).ok())
.unwrap_or(OffsetDateTime::now_utc());
let commit_details = CommitDetails {
sha: blame.sha.to_string().into(),
commit_time,
author_name: blame
.author
.clone()
.unwrap_or("<no name>".to_string())
.into(),
author_email: blame.author_mail.unwrap_or("".to_string()).into(),
message: details,
};
let sha = blame.sha.to_string().into();
let author: SharedString = blame
.author
.clone()
.unwrap_or("<no name>".to_string())
.into();
let author_email = blame.author_mail.as_deref().unwrap_or_default();
let avatar = CommitAvatar::new(&sha, details.as_ref().and_then(|it| it.remote.as_ref()))
.render(window, cx);
let avatar = CommitAvatar::new(&commit_details).render(window, cx);
let author = commit_details.author_name.clone();
let author_email = commit_details.author_email.clone();
let short_commit_id = commit_details
.sha
.get(0..8)
let short_commit_id = sha
.get(..8)
.map(|sha| sha.to_string().into())
.unwrap_or_else(|| commit_details.sha.clone());
let full_sha = commit_details.sha.to_string();
.unwrap_or_else(|| sha.clone());
let absolute_timestamp = format_local_timestamp(
commit_details.commit_time,
commit_time,
OffsetDateTime::now_utc(),
time_format::TimestampFormat::MediumAbsolute,
);
@@ -215,165 +221,143 @@ impl BlameRenderer for GitBlameRenderer {
style
};
let message = commit_details
.message
let message = details
.as_ref()
.map(|_| MarkdownElement::new(markdown.clone(), markdown_style).into_any())
.unwrap_or("<no commit message>".into_any());
let pull_request = commit_details
.message
let pull_request = details
.as_ref()
.and_then(|details| details.pull_request.clone());
let ui_font_size = ThemeSettings::get_global(cx).ui_font_size(cx);
let message_max_height = window.line_height() * 12 + (ui_font_size / 0.4);
let commit_summary = CommitSummary {
sha: commit_details.sha.clone(),
subject: commit_details
.message
sha: sha.clone(),
subject: details
.as_ref()
.map_or(Default::default(), |message| {
message
.message
.split('\n')
.next()
.unwrap()
.trim_end()
.to_string()
.into()
}),
commit_timestamp: commit_details.commit_time.unix_timestamp(),
author_name: commit_details.author_name.clone(),
.and_then(|details| {
Some(
details
.message
.split('\n')
.next()?
.trim_end()
.to_string()
.into(),
)
})
.unwrap_or_default(),
commit_timestamp: commit_time.unix_timestamp(),
author_name: author.clone(),
has_parent: false,
};
let ui_font = ThemeSettings::get_global(cx).ui_font.clone();
// padding to avoid tooltip appearing right below the mouse cursor
// TODO: use tooltip_container here
Some(
div()
.pl_2()
.pt_2p5()
.child(
v_flex()
.elevation_2(cx)
.font(ui_font)
.text_ui(cx)
.text_color(cx.theme().colors().text)
.py_1()
.px_2()
.map(|el| {
el.occlude()
.on_mouse_move(|_, _, cx| cx.stop_propagation())
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
.child(
v_flex()
.w(gpui::rems(30.))
.gap_4()
.child(
h_flex()
.pb_1p5()
.gap_x_2()
.overflow_x_hidden()
.flex_wrap()
.children(avatar)
.child(author)
.when(!author_email.is_empty(), |this| {
this.child(
div()
.text_color(
cx.theme().colors().text_muted,
)
.child(author_email),
)
})
.border_b_1()
.border_color(cx.theme().colors().border_variant),
)
.child(
tooltip_container(cx, |d, cx| {
d.occlude()
.on_mouse_move(|_, _, cx| cx.stop_propagation())
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
.child(
v_flex()
.w(gpui::rems(30.))
.gap_4()
.child(
h_flex()
.pb_1p5()
.gap_x_2()
.overflow_x_hidden()
.flex_wrap()
.children(avatar)
.child(author)
.when(!author_email.is_empty(), |this| {
this.child(
div()
.id("inline-blame-commit-message")
.child(message)
.max_h(message_max_height)
.overflow_y_scroll()
.track_scroll(&scroll_handle),
)
.child(
h_flex()
.text_color(cx.theme().colors().text_muted)
.w_full()
.justify_between()
.pt_1p5()
.border_t_1()
.border_color(cx.theme().colors().border_variant)
.child(absolute_timestamp)
.child(
h_flex()
.gap_1p5()
.when_some(pull_request, |this, pr| {
this.child(
Button::new(
"pull-request-button",
format!("#{}", pr.number),
)
.color(Color::Muted)
.icon(IconName::PullRequest)
.icon_color(Color::Muted)
.icon_position(IconPosition::Start)
.style(ButtonStyle::Subtle)
.on_click(move |_, _, cx| {
cx.stop_propagation();
cx.open_url(pr.url.as_str())
}),
)
})
.child(Divider::vertical())
.child(
Button::new(
"commit-sha-button",
short_commit_id.clone(),
)
.style(ButtonStyle::Subtle)
.color(Color::Muted)
.icon(IconName::FileGit)
.icon_color(Color::Muted)
.icon_position(IconPosition::Start)
.on_click(move |_, window, cx| {
CommitView::open(
commit_summary.clone(),
repository.downgrade(),
workspace.clone(),
window,
cx,
);
cx.stop_propagation();
}),
.child(author_email.to_owned()),
)
})
.border_b_1()
.border_color(cx.theme().colors().border_variant),
)
.child(
div()
.id("inline-blame-commit-message")
.child(message)
.max_h(message_max_height)
.overflow_y_scroll()
.track_scroll(&scroll_handle),
)
.child(
h_flex()
.text_color(cx.theme().colors().text_muted)
.w_full()
.justify_between()
.pt_1p5()
.border_t_1()
.border_color(cx.theme().colors().border_variant)
.child(absolute_timestamp)
.child(
h_flex()
.gap_1p5()
.when_some(pull_request, |this, pr| {
this.child(
Button::new(
"pull-request-button",
format!("#{}", pr.number),
)
.color(Color::Muted)
.icon(IconName::PullRequest)
.icon_color(Color::Muted)
.icon_position(IconPosition::Start)
.style(ButtonStyle::Subtle)
.on_click(move |_, _, cx| {
cx.stop_propagation();
cx.open_url(pr.url.as_str())
}),
)
})
.child(Divider::vertical())
.child(
Button::new(
"commit-sha-button",
short_commit_id.clone(),
)
.style(ButtonStyle::Subtle)
.color(Color::Muted)
.icon(IconName::FileGit)
.icon_color(Color::Muted)
.icon_position(IconPosition::Start)
.on_click(move |_, window, cx| {
CommitView::open(
commit_summary.clone(),
repository.downgrade(),
workspace.clone(),
window,
cx,
);
cx.stop_propagation();
}),
)
.child(
IconButton::new("copy-sha-button", IconName::Copy)
.shape(IconButtonShape::Square)
.icon_size(IconSize::Small)
.icon_color(Color::Muted)
.on_click(move |_, _, cx| {
cx.stop_propagation();
cx.write_to_clipboard(
ClipboardItem::new_string(
sha.to_string(),
),
)
.child(
IconButton::new(
"copy-sha-button",
IconName::Copy,
)
.shape(IconButtonShape::Square)
.icon_size(IconSize::Small)
.icon_color(Color::Muted)
.on_click(move |_, _, cx| {
cx.stop_propagation();
cx.write_to_clipboard(
ClipboardItem::new_string(
full_sha.clone(),
),
)
}),
),
),
),
)
}),
)
.into_any_element(),
}),
),
),
),
)
})
.into_any_element(),
)
}
+18 -10
View File
@@ -28,25 +28,33 @@ pub struct CommitDetails {
}
pub struct CommitAvatar<'a> {
commit: &'a CommitDetails,
sha: &'a SharedString,
remote: Option<&'a GitRemote>,
}
impl<'a> CommitAvatar<'a> {
pub fn new(details: &'a CommitDetails) -> Self {
Self { commit: details }
pub fn new(sha: &'a SharedString, remote: Option<&'a GitRemote>) -> Self {
Self { sha, remote }
}
pub fn from_commit_details(details: &'a CommitDetails) -> Self {
Self {
sha: &details.sha,
remote: details
.message
.as_ref()
.and_then(|details| details.remote.as_ref()),
}
}
}
impl<'a> CommitAvatar<'a> {
pub fn render(&'a self, window: &mut Window, cx: &mut App) -> Option<impl IntoElement + use<>> {
let remote = self
.commit
.message
.as_ref()
.and_then(|details| details.remote.clone())
.remote
.filter(|remote| remote.host_supports_avatars())?;
let avatar_url = CommitAvatarAsset::new(remote, self.commit.sha.clone());
let avatar_url = CommitAvatarAsset::new(remote.clone(), self.sha.clone());
let element = match window.use_asset::<CommitAvatarAsset>(&avatar_url, cx) {
// Loading or no avatar found
@@ -169,7 +177,7 @@ impl CommitTooltip {
impl Render for CommitTooltip {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let avatar = CommitAvatar::new(&self.commit).render(window, cx);
let avatar = CommitAvatar::from_commit_details(&self.commit).render(window, cx);
let author = self.commit.author_name.clone();
@@ -233,7 +241,7 @@ impl Render for CommitTooltip {
has_parent: false,
};
tooltip_container(window, cx, move |this, _, cx| {
tooltip_container(cx, move |this, cx| {
this.occlude()
.on_mouse_move(|_, _, cx| cx.stop_propagation())
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())