@@ -13,72 +13,49 @@ use anyhow::Error;
13
13
14
14
#[ derive( Debug ) ]
15
15
pub struct Commit {
16
- hash : String ,
17
- short_hash : String ,
18
- timestamp : String ,
16
+ info : String ,
17
+ splits : [ usize ; 2 ] ,
19
18
}
20
19
21
20
impl Commit {
22
21
pub fn rev_parse ( what : & str ) -> Result < Self , Error > {
23
22
let output = Command :: new ( "git" )
24
- . args ( [ "rev-parse " , what] )
23
+ . args ( [ "show" , "-s" , "--format=%H%x00%h%x00%ci ", what] )
25
24
. stderr ( Stdio :: inherit ( ) )
26
25
. stdin ( Stdio :: null ( ) )
27
26
. output ( )
28
- . expect ( "Failed to get commit hash " ) ;
27
+ . expect ( "Failed to get commit info " ) ;
29
28
if !output. status . success ( ) {
30
29
return Err ( Error :: msg ( format ! (
31
- "Git exited with {} while getting commit hash " ,
30
+ "Git exited with status {} while getting commit info " ,
32
31
output. status
33
32
) ) ) ;
34
33
}
35
- let hash = String :: from_utf8 ( output. stdout ) . expect ( "Commit hash is not valid UTF-8??" ) ;
34
+ let info = String :: from_utf8 ( output. stdout ) . expect ( "Commit info is not valid UTF-8??" ) ;
36
35
37
- let output = Command :: new ( "git" )
38
- . args ( [ "rev-parse" , "--short" , what] )
39
- . stderr ( Stdio :: inherit ( ) )
40
- . stdin ( Stdio :: null ( ) )
41
- . output ( )
42
- . expect ( "Failed to get short commit hash" ) ;
43
- if !output. status . success ( ) {
44
- return Err ( Error :: msg ( format ! (
45
- "Git exited with status {} while getting commit short hash" ,
46
- output. status
47
- ) ) ) ;
48
- }
49
- let short_hash =
50
- String :: from_utf8 ( output. stdout ) . expect ( "Commit hash is not valid UTF-8??" ) ;
51
-
52
- let output = Command :: new ( "git" )
53
- . args ( [ "show" , "-s" , "--format=%ci" , what] )
54
- . stderr ( Stdio :: inherit ( ) )
55
- . stdin ( Stdio :: null ( ) )
56
- . output ( )
57
- . expect ( "Failed to get timestamp" ) ;
58
- if !output. status . success ( ) {
59
- return Err ( Error :: msg ( format ! (
60
- "Git exited with status {} while getting timestamp" ,
61
- output. status
62
- ) ) ) ;
63
- }
64
- let timestamp = String :: from_utf8 ( output. stdout ) . expect ( "Commit hash is not valid UTF-8??" ) ;
36
+ let first_split = info
37
+ . find ( '\0' )
38
+ . expect ( "Failed to split hash and short hash" ) ;
39
+ let second_split = info[ first_split + 1 ..]
40
+ . find ( '\0' )
41
+ . expect ( "Failed to split short hash and timestamp" )
42
+ + first_split;
65
43
66
44
Ok ( Self {
67
- hash,
68
- short_hash,
69
- timestamp,
45
+ info,
46
+ splits : [ first_split, second_split] ,
70
47
} )
71
48
}
72
49
73
50
pub fn hash ( & self ) -> & str {
74
- self . hash . trim ( )
51
+ & self . info [ .. self . splits [ 0 ] ]
75
52
}
76
53
77
54
pub fn short_hash ( & self ) -> & str {
78
- self . short_hash . trim ( )
55
+ & self . info [ self . splits [ 0 ] + 1 .. self . splits [ 1 ] ]
79
56
}
80
57
81
58
pub fn timestamp ( & self ) -> & str {
82
- self . timestamp . trim ( )
59
+ & self . info [ self . splits [ 1 ] + 1 .. ]
83
60
}
84
61
}
0 commit comments