@@ -3,14 +3,21 @@ use crate::error::Result;
33use scopetime:: scope_time;
44use std:: collections:: HashMap ;
55
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 > > ;
88
99/// returns `Tags` type filled with all tags found in repo
1010pub fn get_tags ( repo_path : & str ) -> Result < Tags > {
1111 scope_time ! ( "get_tags" ) ;
1212
1313 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+ } ;
1421
1522 let repo = repo ( repo_path) ?;
1623
@@ -21,10 +28,50 @@ pub fn get_tags(repo_path: &str) -> Result<Tags> {
2128 if let Some ( tag) = obj. as_tag ( ) {
2229 let target_hash = tag. target_id ( ) . to_string ( ) ;
2330 let tag_name = String :: from ( name) ;
24- res . insert ( target_hash, tag_name) ;
31+ adder ( target_hash, tag_name) ;
2532 }
2633 }
2734 }
2835
2936 Ok ( res)
3037}
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