project: Show detached head commit SHA in branch pickers (#29007)

When Git is in a detached HEAD state, the branch is `None`, and we can't
get any meaningful information from it. This PR adds a `head_commit`
field to the snapshot, which is always populated with the HEAD details,
even when the branch is `None`.

This also pave path to fix:
https://github.com/zed-industries/zed/issues/28736

git panel branch picker (before, after):
<img width="197" alt="image"
src="https://github.com/user-attachments/assets/0b6abbba-2988-4890-a708-bcd8aad84f26"
/> <img width="198" alt="image"
src="https://github.com/user-attachments/assets/4b08b1a8-5e79-4aa3-a44e-932249602c18"
/>

title bar branch picker (before, after):
<img width="183" alt="image"
src="https://github.com/user-attachments/assets/d94357f8-a4da-4d60-8ddd-fdd978b99fdf"
/> <img width="228" alt="image"
src="https://github.com/user-attachments/assets/d20824a1-9279-44d6-afd1-bf9319fc50e4"
/>

Release Notes:

- Added head commit SHA information to the Git branch picker in the
title bar and Git panel.
This commit is contained in:
Smit Barmase 2025-04-18 04:23:56 +05:30 committed by GitHub
parent c2cd4fd7a1
commit ba7f886c62
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 109 additions and 7 deletions

View file

@ -2938,6 +2938,7 @@ impl GitPanel {
let expand_tooltip_focus_handle = editor_focus_handle.clone();
let branch = active_repository.read(cx).branch.clone();
let head_commit = active_repository.read(cx).head_commit.clone();
let footer_size = px(32.);
let gap = px(9.0);
@ -2966,6 +2967,7 @@ impl GitPanel {
.child(PanelRepoFooter::new(
display_name,
branch,
head_commit,
Some(git_panel.clone()),
))
.child(
@ -4291,6 +4293,8 @@ impl Render for GitPanelMessageTooltip {
pub struct PanelRepoFooter {
active_repository: SharedString,
branch: Option<Branch>,
head_commit: Option<CommitDetails>,
// Getting a GitPanel in previews will be difficult.
//
// For now just take an option here, and we won't bind handlers to buttons in previews.
@ -4301,11 +4305,13 @@ impl PanelRepoFooter {
pub fn new(
active_repository: SharedString,
branch: Option<Branch>,
head_commit: Option<CommitDetails>,
git_panel: Option<Entity<GitPanel>>,
) -> Self {
Self {
active_repository,
branch,
head_commit,
git_panel,
}
}
@ -4314,6 +4320,7 @@ impl PanelRepoFooter {
Self {
active_repository,
branch,
head_commit: None,
git_panel: None,
}
}
@ -4339,11 +4346,26 @@ impl RenderOnce for PanelRepoFooter {
const MAX_BRANCH_LEN: usize = 16;
const MAX_REPO_LEN: usize = 16;
const LABEL_CHARACTER_BUDGET: usize = MAX_BRANCH_LEN + MAX_REPO_LEN;
const MAX_SHORT_SHA_LEN: usize = 8;
let branch = self.branch.clone();
let branch_name = branch
let branch_name = self
.branch
.as_ref()
.map_or(" (no branch)".into(), |branch| branch.name.clone());
.map(|branch| branch.name.clone())
.or_else(|| {
self.head_commit.as_ref().map(|commit| {
SharedString::from(
commit
.sha
.chars()
.take(MAX_SHORT_SHA_LEN)
.collect::<String>(),
)
})
})
.unwrap_or_else(|| SharedString::from(" (no branch)"));
let show_separator = self.branch.is_some() || self.head_commit.is_some();
let active_repo_name = self.active_repository.clone();
let branch_actual_len = branch_name.len();
@ -4449,7 +4471,7 @@ impl RenderOnce for PanelRepoFooter {
),
)
.child(repo_selector)
.when_some(branch.clone(), |this, _| {
.when(show_separator, |this| {
this.child(
div()
.text_color(cx.theme().colors().text_muted)