1
1
//!
2
+ use std:: convert:: TryInto ;
2
3
use std:: ops:: Deref ;
3
4
4
5
use git_hash:: { oid, ObjectId } ;
@@ -28,11 +29,22 @@ impl<'repo> Id<'repo> {
28
29
29
30
/// Turn this object id into a shortened id with a length in hex as configured by `core.abbrev`.
30
31
pub fn prefix ( & self ) -> Result < git_hash:: Prefix , prefix:: Error > {
31
- // let hex_len = self.handle.config.get_int("core.abbrev")?;
32
+ let hex_len = self . repo . config_int ( "core.abbrev" , 8 ) ;
33
+ let hex_len = hex_len. try_into ( ) . map_err ( |_| prefix:: Error :: ConfigValue {
34
+ actual : hex_len,
35
+ max_range : self . inner . kind ( ) . len_in_hex ( ) ,
36
+ err : None ,
37
+ } ) ?;
38
+ let prefix =
39
+ git_odb:: find:: PotentialPrefix :: new ( self . inner , hex_len) . map_err ( |err| prefix:: Error :: ConfigValue {
40
+ actual : hex_len as i64 ,
41
+ max_range : self . inner . kind ( ) . len_in_hex ( ) ,
42
+ err : Some ( err) ,
43
+ } ) ?;
32
44
Ok ( self
33
45
. repo
34
46
. objects
35
- . disambiguate_prefix ( git_odb :: find :: PotentialPrefix :: new ( self . inner , 7 ) ? )
47
+ . disambiguate_prefix ( prefix )
36
48
. map_err ( crate :: object:: find:: existing:: OdbError :: Find ) ?
37
49
. ok_or ( crate :: object:: find:: existing:: OdbError :: NotFound { oid : self . inner } ) ?)
38
50
}
@@ -46,10 +58,13 @@ mod prefix {
46
58
pub enum Error {
47
59
#[ error( transparent) ]
48
60
FindExisting ( #[ from] crate :: object:: find:: existing:: OdbError ) ,
49
- #[ error( transparent) ]
50
- Config ( #[ from] crate :: config:: open:: Error ) ,
51
- #[ error( transparent) ]
52
- Prefix ( #[ from] git_hash:: prefix:: Error ) ,
61
+ #[ error( "core.abbrev length was {}, but needs to be between 4 and {}" , . actual, . max_range) ]
62
+ ConfigValue {
63
+ #[ source]
64
+ err : Option < git_hash:: prefix:: Error > ,
65
+ actual : i64 ,
66
+ max_range : usize ,
67
+ } ,
53
68
}
54
69
}
55
70
0 commit comments