added support for displaying tags behind branch name

This commit is contained in:
MasterGordon 2024-08-11 16:15:00 +02:00
parent c8803ad0ae
commit 5b6d1e3795
3 changed files with 24 additions and 4 deletions

2
Cargo.lock generated
View File

@ -35,7 +35,7 @@ checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125"
[[package]] [[package]]
name = "fast-git-prompt" name = "fast-git-prompt"
version = "0.1.0" version = "0.2.0"
dependencies = [ dependencies = [
"git2", "git2",
"regex", "regex",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "fast-git-prompt" name = "fast-git-prompt"
version = "0.1.1" version = "0.2.0"
description = "A fast git prompt for zsh and bash." description = "A fast git prompt for zsh and bash."
license = "MIT" license = "MIT"
repository = "https://github.com/MasterGordon/fast-git-prompt" repository = "https://github.com/MasterGordon/fast-git-prompt"

View File

@ -1,3 +1,4 @@
use crate::colors::{color, Color};
use crate::prompt_parts::prompt_part::RenderablePromptPart; use crate::prompt_parts::prompt_part::RenderablePromptPart;
use git2::Repository; use git2::Repository;
use schemars::JsonSchema; use schemars::JsonSchema;
@ -5,7 +6,7 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct BranchName { pub struct BranchName {
pub color: Option<String>, pub color: Option<Color>,
} }
impl RenderablePromptPart for BranchName { impl RenderablePromptPart for BranchName {
@ -16,6 +17,25 @@ impl RenderablePromptPart for BranchName {
}?; }?;
let name = head.name()?; let name = head.name()?;
let last = name.split('/').last()?; let last = name.split('/').last()?;
return Some(format!("{}{}", self.color.unwrap_or("".to_string()), last)); let head_commit = head.peel_to_commit().unwrap();
let mut current_tags: Vec<String> = vec![];
let tag_names = repo.tag_names(None);
for tag_name in tag_names.iter().flatten() {
let tag_object = repo.revparse_single(&format!("refs/tags/{}", tag_name?));
if let Ok(tag_commit) = tag_object.unwrap().peel_to_commit() {
if head_commit.id() == tag_commit.id() {
if let Some(tag) = tag_name {
current_tags.push(tag.to_string());
}
}
}
}
let last_tag = current_tags.last();
if let Some(tag) = last_tag {
return Some(format!("{}{} ({})", color(self.color), last, tag));
} else {
return Some(format!("{}{}", color(self.color), last));
}
} }
} }