1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use super::{get_commits_info, CommitId, RepoPath};
use crate::{
error::Result,
sync::{repository::repo, utils::bytes2string},
};
use scopetime::scope_time;
use std::{
collections::{BTreeMap, HashMap, HashSet},
ops::Not,
};
#[derive(Clone, Hash, PartialEq, Eq, Debug)]
pub struct Tag {
pub name: String,
pub annotation: Option<String>,
}
impl Tag {
#[allow(clippy::missing_const_for_fn)]
pub fn new(name: &str) -> Self {
Self {
name: name.into(),
annotation: None,
}
}
}
pub type CommitTags = Vec<Tag>;
pub type Tags = BTreeMap<CommitId, CommitTags>;
pub struct TagWithMetadata {
pub name: String,
pub author: String,
pub time: i64,
pub message: String,
pub commit_id: CommitId,
pub annotation: Option<String>,
}
static MAX_MESSAGE_WIDTH: usize = 100;
pub fn get_tags(repo_path: &RepoPath) -> Result<Tags> {
scope_time!("get_tags");
let mut res = Tags::new();
let mut adder = |key, value: Tag| {
if let Some(key) = res.get_mut(&key) {
key.push(value);
} else {
res.insert(key, vec![value]);
}
};
let repo = repo(repo_path)?;
repo.tag_foreach(|id, name| {
if let Ok(name) =
String::from_utf8(name[10..name.len()].into())
{
let commit = repo
.find_tag(id)
.and_then(|tag| tag.target())
.and_then(|target| target.peel_to_commit())
.map_or_else(
|_| {
if repo.find_commit(id).is_ok() {
Some(CommitId::new(id))
} else {
None
}
},
|commit| Some(CommitId::new(commit.id())),
);
let annotation = repo
.find_tag(id)
.ok()
.as_ref()
.and_then(git2::Tag::message_bytes)
.and_then(|msg| {
msg.is_empty()
.not()
.then(|| bytes2string(msg).ok())
.flatten()
});
if let Some(commit) = commit {
adder(commit, Tag { name, annotation });
}
return true;
}
false
})?;
Ok(res)
}
pub fn get_tags_with_metadata(
repo_path: &RepoPath,
) -> Result<Vec<TagWithMetadata>> {
scope_time!("get_tags_with_metadata");
let tags_grouped_by_commit_id = get_tags(repo_path)?;
let tags_with_commit_id: Vec<(&str, Option<&str>, &CommitId)> =
tags_grouped_by_commit_id
.iter()
.flat_map(|(commit_id, tags)| {
tags.iter()
.map(|tag| {
(
tag.name.as_ref(),
tag.annotation.as_deref(),
commit_id,
)
})
.collect::<Vec<_>>()
})
.collect();
let unique_commit_ids: HashSet<_> = tags_with_commit_id
.iter()
.copied()
.map(|(_, _, &commit_id)| commit_id)
.collect();
let mut commit_ids = Vec::with_capacity(unique_commit_ids.len());
commit_ids.extend(unique_commit_ids);
let commit_infos =
get_commits_info(repo_path, &commit_ids, MAX_MESSAGE_WIDTH)?;
let unique_commit_infos: HashMap<_, _> = commit_infos
.iter()
.map(|commit_info| (commit_info.id, commit_info))
.collect();
let mut tags: Vec<TagWithMetadata> = tags_with_commit_id
.into_iter()
.filter_map(|(tag, annotation, commit_id)| {
unique_commit_infos.get(commit_id).map(|commit_info| {
TagWithMetadata {
name: String::from(tag),
author: commit_info.author.clone(),
time: commit_info.time,
message: commit_info.message.clone(),
commit_id: *commit_id,
annotation: annotation.map(String::from),
}
})
})
.collect();
tags.sort_unstable_by(|a, b| b.time.cmp(&a.time));
Ok(tags)
}
pub fn delete_tag(
repo_path: &RepoPath,
tag_name: &str,
) -> Result<()> {
scope_time!("delete_tag");
let repo = repo(repo_path)?;
repo.tag_delete(tag_name)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sync::tests::repo_init;
use git2::ObjectType;
#[test]
fn test_smoke() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path: &RepoPath =
&root.as_os_str().to_str().unwrap().into();
assert_eq!(get_tags(repo_path).unwrap().is_empty(), true);
}
#[test]
fn test_multitags() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path: &RepoPath =
&root.as_os_str().to_str().unwrap().into();
let sig = repo.signature().unwrap();
let head_id = repo.head().unwrap().target().unwrap();
let target = repo
.find_object(
repo.head().unwrap().target().unwrap(),
Some(ObjectType::Commit),
)
.unwrap();
repo.tag("a", &target, &sig, "", false).unwrap();
repo.tag("b", &target, &sig, "", false).unwrap();
assert_eq!(
get_tags(repo_path).unwrap()[&CommitId::new(head_id)]
.iter()
.map(|t| &t.name)
.collect::<Vec<_>>(),
vec!["a", "b"]
);
let tags = get_tags_with_metadata(repo_path).unwrap();
assert_eq!(tags.len(), 2);
assert_eq!(tags[0].name, "a");
assert_eq!(tags[0].message, "initial");
assert_eq!(tags[1].name, "b");
assert_eq!(tags[1].message, "initial");
assert_eq!(tags[0].commit_id, tags[1].commit_id);
delete_tag(repo_path, "a").unwrap();
let tags = get_tags(repo_path).unwrap();
assert_eq!(tags.len(), 1);
delete_tag(repo_path, "b").unwrap();
let tags = get_tags(repo_path).unwrap();
assert_eq!(tags.len(), 0);
}
}