1
1
mod find {
2
- use std:: convert:: TryInto ;
2
+ use std:: convert:: { TryFrom , TryInto } ;
3
3
4
4
use git_ref as refs;
5
5
use git_repository:: prelude:: ReferenceAccessExt ;
@@ -9,35 +9,72 @@ mod find {
9
9
crate :: repo ( "make_references_repo.sh" ) . map ( Into :: into)
10
10
}
11
11
12
+ /// Gain an understanding how uses might want to call this function, and see what happens
12
13
#[ test]
13
- fn and_peel ( ) {
14
- let repo = repo ( ) . unwrap ( ) ;
15
- let mut packed_tag_ref = repo. try_find_reference ( "dt1" ) . unwrap ( ) . expect ( "tag to exist" ) ;
16
- assert_eq ! ( packed_tag_ref. name( ) , "refs/tags/dt1" . try_into( ) . unwrap( ) ) ;
14
+ fn possible_inputs ( ) -> crate :: Result {
15
+ let repo = repo ( ) ?;
16
+ repo. find_reference ( "dt1" ) ?;
17
+ repo. find_reference ( & String :: from ( "dt1" ) ) ?; // Owned Strings don't have an impl for PartialName
18
+
19
+ struct CustomType ( String ) ;
20
+ impl < ' a > TryFrom < & ' a CustomType > for refs:: PartialNameRef < ' a > {
21
+ type Error = refs:: name:: Error ;
22
+
23
+ fn try_from ( value : & ' a CustomType ) -> Result < Self , Self :: Error > {
24
+ refs:: PartialNameRef :: try_from ( & value. 0 )
25
+ }
26
+ }
27
+ repo. find_reference ( & CustomType ( "dt1" . into ( ) ) ) ?;
28
+
29
+ struct CustomName {
30
+ remote : & ' static str ,
31
+ branch : & ' static str ,
32
+ }
33
+
34
+ impl CustomName {
35
+ fn to_partial_name ( & self ) -> String {
36
+ format ! ( "{}/{}" , self . remote, self . branch)
37
+ }
38
+ }
39
+ let name = CustomName {
40
+ remote : "origin" ,
41
+ branch : "main" ,
42
+ } ;
43
+ repo. find_reference ( & name. to_partial_name ( ) ) ?;
44
+
45
+ Ok ( ( ) )
46
+ }
47
+
48
+ #[ test]
49
+ fn and_peel ( ) -> crate :: Result {
50
+ let repo = repo ( ) ?;
51
+ let mut packed_tag_ref = repo. try_find_reference ( "dt1" ) ?. expect ( "tag to exist" ) ;
52
+ assert_eq ! ( packed_tag_ref. name( ) , "refs/tags/dt1" . try_into( ) ?) ;
17
53
18
54
assert_eq ! (
19
55
packed_tag_ref. inner. target,
20
56
refs:: Target :: Peeled ( hex_to_id( "4c3f4cce493d7beb45012e478021b5f65295e5a3" ) ) ,
21
57
"it points to a tag object"
22
58
) ;
23
59
24
- let object = packed_tag_ref. peel_to_id_in_place ( ) . unwrap ( ) ;
60
+ let object = packed_tag_ref. peel_to_id_in_place ( ) ? ;
25
61
let the_commit = hex_to_id ( "134385f6d781b7e97062102c6a483440bfda2a03" ) ;
26
62
assert_eq ! ( object, the_commit, "it is assumed to be fully peeled" ) ;
27
63
assert_eq ! (
28
64
object,
29
- packed_tag_ref. peel_to_id_in_place( ) . unwrap ( ) ,
65
+ packed_tag_ref. peel_to_id_in_place( ) ? ,
30
66
"peeling again yields the same object"
31
67
) ;
32
68
33
- let mut symbolic_ref = repo. find_reference ( "multi-link-target1" ) . unwrap ( ) ;
34
- assert_eq ! ( symbolic_ref. name( ) , "refs/heads/multi-link-target1" . try_into( ) . unwrap ( ) ) ;
35
- assert_eq ! ( symbolic_ref. peel_to_id_in_place( ) . unwrap ( ) , the_commit) ;
69
+ let mut symbolic_ref = repo. find_reference ( "multi-link-target1" ) ? ;
70
+ assert_eq ! ( symbolic_ref. name( ) , "refs/heads/multi-link-target1" . try_into( ) ? ) ;
71
+ assert_eq ! ( symbolic_ref. peel_to_id_in_place( ) ? , the_commit) ;
36
72
assert_eq ! (
37
73
symbolic_ref. name( ) ,
38
- "refs/remotes/origin/multi-link-target3" . try_into( ) . unwrap ( ) ,
74
+ "refs/remotes/origin/multi-link-target3" . try_into( ) ? ,
39
75
"it follows symbolic refs, too"
40
76
) ;
41
- assert_eq ! ( symbolic_ref. into_fully_peeled_id( ) . unwrap( ) , the_commit, "idempotency" ) ;
77
+ assert_eq ! ( symbolic_ref. into_fully_peeled_id( ) ?, the_commit, "idempotency" ) ;
78
+ Ok ( ( ) )
42
79
}
43
80
}
0 commit comments