@@ -3,14 +3,21 @@ use crate::error::Result;
3
3
use scopetime:: scope_time;
4
4
use std:: collections:: HashMap ;
5
5
6
- /// hashmap of tag target commit hash to tag name
7
- pub type Tags = HashMap < String , String > ;
6
+ /// hashmap of tag target commit hash to tag names
7
+ pub type Tags = HashMap < String , Vec < String > > ;
8
8
9
9
/// returns `Tags` type filled with all tags found in repo
10
10
pub fn get_tags ( repo_path : & str ) -> Result < Tags > {
11
11
scope_time ! ( "get_tags" ) ;
12
12
13
13
let mut res = Tags :: new ( ) ;
14
+ let mut adder = |key : String , value : String | {
15
+ if let Some ( key) = res. get_mut ( & key) {
16
+ key. push ( value)
17
+ } else {
18
+ res. insert ( key, vec ! [ value] ) ;
19
+ }
20
+ } ;
14
21
15
22
let repo = repo ( repo_path) ?;
16
23
@@ -21,10 +28,50 @@ pub fn get_tags(repo_path: &str) -> Result<Tags> {
21
28
if let Some ( tag) = obj. as_tag ( ) {
22
29
let target_hash = tag. target_id ( ) . to_string ( ) ;
23
30
let tag_name = String :: from ( name) ;
24
- res . insert ( target_hash, tag_name) ;
31
+ adder ( target_hash, tag_name) ;
25
32
}
26
33
}
27
34
}
28
35
29
36
Ok ( res)
30
37
}
38
+
39
+ #[ cfg( test) ]
40
+ mod tests {
41
+ use super :: * ;
42
+ use crate :: sync:: tests:: repo_init;
43
+ use git2:: ObjectType ;
44
+
45
+ #[ test]
46
+ fn test_smoke ( ) {
47
+ let ( _td, repo) = repo_init ( ) . unwrap ( ) ;
48
+ let root = repo. path ( ) . parent ( ) . unwrap ( ) ;
49
+ let repo_path = root. as_os_str ( ) . to_str ( ) . unwrap ( ) ;
50
+
51
+ assert_eq ! ( get_tags( repo_path) . unwrap( ) . is_empty( ) , true ) ;
52
+ }
53
+
54
+ #[ test]
55
+ fn test_multitags ( ) {
56
+ let ( _td, repo) = repo_init ( ) . unwrap ( ) ;
57
+ let root = repo. path ( ) . parent ( ) . unwrap ( ) ;
58
+ let repo_path = root. as_os_str ( ) . to_str ( ) . unwrap ( ) ;
59
+
60
+ let sig = repo. signature ( ) . unwrap ( ) ;
61
+ let head_id = repo. head ( ) . unwrap ( ) . target ( ) . unwrap ( ) ;
62
+ let target = repo
63
+ . find_object (
64
+ repo. head ( ) . unwrap ( ) . target ( ) . unwrap ( ) ,
65
+ Some ( ObjectType :: Commit ) ,
66
+ )
67
+ . unwrap ( ) ;
68
+
69
+ repo. tag ( "a" , & target, & sig, "" , false ) . unwrap ( ) ;
70
+ repo. tag ( "b" , & target, & sig, "" , false ) . unwrap ( ) ;
71
+
72
+ assert_eq ! (
73
+ get_tags( repo_path) . unwrap( ) [ & head_id. to_string( ) ] ,
74
+ vec![ "a" , "b" ]
75
+ ) ;
76
+ }
77
+ }
0 commit comments