2
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
4
5
+ use std:: error:: Error ;
5
6
use std:: process;
6
7
8
+ const PPERROR : & str = "Unexpected end of data while parsing Git output" ;
9
+
7
10
fn color ( c : i32 ) {
8
11
if c >= 0 {
9
12
print ! ( "\x1b [38;5;{}m" , c) ;
@@ -20,24 +23,18 @@ fn bold(b: bool) {
20
23
}
21
24
}
22
25
23
- fn main ( ) {
24
- let status = match process:: Command :: new ( "git" )
25
- . args ( & [ "status" , "--porcelain=v2" , "-z" , "--branch" , "--untracked-files=all" ] )
26
- . stdin ( process:: Stdio :: null ( ) )
27
- . stderr ( process:: Stdio :: null ( ) )
28
- . output ( ) {
29
- Err ( e) => {
30
- eprintln ! ( "git: {}" , e) ;
31
- process:: exit ( 1 ) ;
32
- } ,
33
- Ok ( output) => {
34
- if !output. status . success ( ) {
35
- // We're most likely not in a Git repo
36
- process:: exit ( 0 ) ;
37
- }
38
- String :: from_utf8 ( output. stdout ) . expect ( "Failed to decode output from Git" )
39
- } ,
40
- } ;
26
+ fn main ( ) -> Result < ( ) , Box < Error > > {
27
+ let output = process:: Command :: new ( "git" )
28
+ . args ( & [ "status" , "--porcelain=v2" , "-z" , "--branch" , "--untracked-files=all" ] )
29
+ . stdin ( process:: Stdio :: null ( ) )
30
+ . stderr ( process:: Stdio :: null ( ) )
31
+ . output ( ) ?;
32
+ if !output. status . success ( ) {
33
+ // We're most likely not in a Git repo
34
+ return Ok ( ( ) )
35
+ }
36
+ let status = String :: from_utf8 ( output. stdout )
37
+ . ok ( ) . ok_or ( "Invalid UTF-8 while decoding Git output" ) ?;
41
38
42
39
// Details on the current branch
43
40
let mut branch = None ;
@@ -57,27 +54,27 @@ fn main() {
57
54
match entry. next ( ) {
58
55
// Header lines
59
56
Some ( "#" ) => {
60
- match entry. next ( ) . unwrap ( ) {
57
+ match entry. next ( ) . ok_or ( PPERROR ) ? {
61
58
"branch.head" => {
62
- let head = entry. next ( ) . unwrap ( ) ;
59
+ let head = entry. next ( ) . ok_or ( PPERROR ) ? ;
63
60
if head != "(detached)" {
64
61
branch = Some ( head) ;
65
62
}
66
63
} ,
67
64
"branch.ab" => {
68
- let a = entry. next ( ) . unwrap ( ) ;
69
- let b = entry. next ( ) . unwrap ( ) ;
70
- ahead = a. parse :: < i64 > ( ) . unwrap ( ) . abs ( ) ;
71
- behind = b. parse :: < i64 > ( ) . unwrap ( ) . abs ( ) ;
65
+ let a = entry. next ( ) . ok_or ( PPERROR ) ? ;
66
+ let b = entry. next ( ) . ok_or ( PPERROR ) ? ;
67
+ ahead = a. parse :: < i64 > ( ) ? . abs ( ) ;
68
+ behind = b. parse :: < i64 > ( ) ? . abs ( ) ;
72
69
} ,
73
70
_ => { } ,
74
71
}
75
72
} ,
76
73
// File entries
77
74
Some ( "1" ) | Some ( "2" ) => {
78
- let mut xy = entry. next ( ) . unwrap ( ) . chars ( ) ;
79
- let x = xy. next ( ) . unwrap ( ) ;
80
- let y = xy. next ( ) . unwrap ( ) ;
75
+ let mut xy = entry. next ( ) . ok_or ( PPERROR ) ? . chars ( ) ;
76
+ let x = xy. next ( ) . ok_or ( PPERROR ) ? ;
77
+ let y = xy. next ( ) . ok_or ( PPERROR ) ? ;
81
78
if x != '.' {
82
79
staged += 1 ;
83
80
}
@@ -140,4 +137,6 @@ fn main() {
140
137
141
138
color ( -1 ) ;
142
139
print ! ( ")" ) ;
140
+
141
+ Ok ( ( ) )
143
142
}
0 commit comments