Skip to content

Commit 166826f

Browse files
author
Stephan Dilly
committed
allow inspecting tag annotation
1 parent a1f3931 commit 166826f

File tree

6 files changed

+93
-7
lines changed

6 files changed

+93
-7
lines changed

asyncgit/src/sync/tags.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use crate::{
44
sync::{repository::repo, utils::bytes2string},
55
};
66
use scopetime::scope_time;
7-
use std::collections::{BTreeMap, HashMap, HashSet};
7+
use std::{
8+
collections::{BTreeMap, HashMap, HashSet},
9+
ops::Not,
10+
};
811

912
///
1013
#[derive(Clone, Hash, PartialEq, Debug)]
@@ -42,6 +45,8 @@ pub struct TagWithMetadata {
4245
pub message: String,
4346
///
4447
pub commit_id: CommitId,
48+
///
49+
pub annotation: Option<String>,
4550
}
4651

4752
static MAX_MESSAGE_WIDTH: usize = 100;
@@ -87,7 +92,12 @@ pub fn get_tags(repo_path: &RepoPath) -> Result<Tags> {
8792
.ok()
8893
.as_ref()
8994
.and_then(git2::Tag::message_bytes)
90-
.and_then(|msg| bytes2string(msg).ok());
95+
.and_then(|msg| {
96+
msg.is_empty()
97+
.not()
98+
.then(|| bytes2string(msg).ok())
99+
.flatten()
100+
});
91101

92102
if let Some(commit) = commit {
93103
adder(commit, Tag { name, annotation });
@@ -109,20 +119,26 @@ pub fn get_tags_with_metadata(
109119

110120
let tags_grouped_by_commit_id = get_tags(repo_path)?;
111121

112-
let tags_with_commit_id: Vec<(&str, &CommitId)> =
122+
let tags_with_commit_id: Vec<(&str, Option<&str>, &CommitId)> =
113123
tags_grouped_by_commit_id
114124
.iter()
115125
.flat_map(|(commit_id, tags)| {
116126
tags.iter()
117-
.map(|tag| (tag.name.as_ref(), commit_id))
118-
.collect::<Vec<(&str, &CommitId)>>()
127+
.map(|tag| {
128+
(
129+
tag.name.as_ref(),
130+
tag.annotation.as_deref(),
131+
commit_id,
132+
)
133+
})
134+
.collect::<Vec<_>>()
119135
})
120136
.collect();
121137

122138
let unique_commit_ids: HashSet<_> = tags_with_commit_id
123139
.iter()
124140
.copied()
125-
.map(|(_, &commit_id)| commit_id)
141+
.map(|(_, _, &commit_id)| commit_id)
126142
.collect();
127143
let mut commit_ids = Vec::with_capacity(unique_commit_ids.len());
128144
commit_ids.extend(unique_commit_ids);
@@ -136,14 +152,15 @@ pub fn get_tags_with_metadata(
136152

137153
let mut tags: Vec<TagWithMetadata> = tags_with_commit_id
138154
.into_iter()
139-
.filter_map(|(tag, commit_id)| {
155+
.filter_map(|(tag, annotation, commit_id)| {
140156
unique_commit_infos.get(commit_id).map(|commit_info| {
141157
TagWithMetadata {
142158
name: String::from(tag),
143159
author: commit_info.author.clone(),
144160
time: commit_info.time,
145161
message: commit_info.message.clone(),
146162
commit_id: *commit_id,
163+
annotation: annotation.map(String::from),
147164
}
148165
})
149166
})

src/app.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,11 @@ impl App {
674674
flags
675675
.insert(NeedsUpdate::ALL | NeedsUpdate::COMMANDS);
676676
}
677+
InternalEvent::ShowInfoMsg(msg) => {
678+
self.msg.show_info(msg.as_str())?;
679+
flags
680+
.insert(NeedsUpdate::ALL | NeedsUpdate::COMMANDS);
681+
}
677682
InternalEvent::Update(u) => flags.insert(u),
678683
InternalEvent::OpenCommit => self.commit.show()?,
679684
InternalEvent::PopupStashing(opts) => {

src/components/msg.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,13 @@ impl MsgComponent {
139139

140140
Ok(())
141141
}
142+
143+
///
144+
pub fn show_info(&mut self, msg: &str) -> Result<()> {
145+
self.title = strings::msg_title_info(&self.key_config);
146+
self.msg = msg.to_string();
147+
self.show()?;
148+
149+
Ok(())
150+
}
142151
}

src/components/taglist.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ impl DrawableComponent for TagListComponent {
8585
Constraint::Length(10),
8686
// author width
8787
Constraint::Length(19),
88+
// attachement
89+
Constraint::Length(1),
8890
// commit id
8991
Constraint::Percentage(100),
9092
];
@@ -171,6 +173,13 @@ impl Component for TagListComponent {
171173
true,
172174
true,
173175
));
176+
out.push(CommandInfo::new(
177+
strings::commands::show_tag_annotation(
178+
&self.key_config,
179+
),
180+
self.can_show_annotation(),
181+
true,
182+
));
174183
}
175184
visibility_blocking(self)
176185
}
@@ -196,6 +205,10 @@ impl Component for TagListComponent {
196205
self.move_selection(ScrollType::PageDown);
197206
} else if key == self.key_config.keys.page_up {
198207
self.move_selection(ScrollType::PageUp);
208+
} else if key == self.key_config.keys.move_right
209+
&& self.can_show_annotation()
210+
{
211+
self.show_annotation();
199212
} else if key == self.key_config.keys.delete_tag {
200213
return self.selected_tag().map_or(
201214
Ok(EventState::NotConsumed),
@@ -372,6 +385,22 @@ impl TagListComponent {
372385
needs_update
373386
}
374387

388+
fn show_annotation(&self) {
389+
if let Some(tag) = self.selected_tag() {
390+
if let Some(annotation) = &tag.annotation {
391+
self.queue.push(InternalEvent::ShowInfoMsg(
392+
annotation.clone(),
393+
));
394+
}
395+
}
396+
}
397+
398+
fn can_show_annotation(&self) -> bool {
399+
self.selected_tag()
400+
.and_then(|t| t.annotation.as_ref())
401+
.is_some()
402+
}
403+
375404
///
376405
fn get_rows(&self) -> Vec<Row> {
377406
self.tags.as_ref().map_or_else(Vec::new, |tags| {
@@ -382,6 +411,7 @@ impl TagListComponent {
382411
///
383412
fn get_row(&self, tag: &TagWithMetadata) -> Row {
384413
const UPSTREAM_SYMBOL: &str = "\u{2191}";
414+
const ATTACHEMENT_SYMBOL: &str = "!";
385415
const EMPTY_SYMBOL: &str = " ";
386416

387417
let is_tag_missing_on_remote = self
@@ -399,6 +429,12 @@ impl TagListComponent {
399429
EMPTY_SYMBOL
400430
};
401431

432+
let has_attachement_str = if tag.annotation.is_some() {
433+
ATTACHEMENT_SYMBOL
434+
} else {
435+
EMPTY_SYMBOL
436+
};
437+
402438
let cells: Vec<Cell> = vec![
403439
Cell::from(has_remote_str)
404440
.style(self.theme.commit_author(false)),
@@ -408,6 +444,8 @@ impl TagListComponent {
408444
.style(self.theme.commit_time(false)),
409445
Cell::from(tag.author.clone())
410446
.style(self.theme.commit_author(false)),
447+
Cell::from(has_attachement_str)
448+
.style(self.theme.commit_time(false)),
411449
Cell::from(tag.message.clone())
412450
.style(self.theme.text(true, false)),
413451
];

src/queue.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ pub enum InternalEvent {
5757
///
5858
ShowErrorMsg(String),
5959
///
60+
ShowInfoMsg(String),
61+
///
6062
Update(NeedsUpdate),
6163
///
6264
StatusLastFileMoved,

src/strings.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ pub fn msg_opening_editor(_key_config: &SharedKeyConfig) -> String {
8787
pub fn msg_title_error(_key_config: &SharedKeyConfig) -> String {
8888
"Error".to_string()
8989
}
90+
pub fn msg_title_info(_key_config: &SharedKeyConfig) -> String {
91+
"Info".to_string()
92+
}
9093
pub fn commit_title() -> String {
9194
"Commit".to_string()
9295
}
@@ -530,6 +533,18 @@ pub mod commands {
530533
CMD_GROUP_LOG,
531534
)
532535
}
536+
pub fn show_tag_annotation(
537+
key_config: &SharedKeyConfig,
538+
) -> CommandText {
539+
CommandText::new(
540+
format!(
541+
"Annotation [{}]",
542+
key_config.get_hint(key_config.keys.move_right),
543+
),
544+
"show tag annotation",
545+
CMD_GROUP_LOG,
546+
)
547+
}
533548
pub fn diff_home_end(
534549
key_config: &SharedKeyConfig,
535550
) -> CommandText {

0 commit comments

Comments
 (0)