@@ -9,6 +9,7 @@ use gix::{
9
9
sec:: { self , trust:: DefaultForLevel } ,
10
10
} ;
11
11
use log:: debug;
12
+ use num_enum:: IntoPrimitive ;
12
13
use std:: borrow:: Cow ;
13
14
use std:: env;
14
15
use std:: path:: Path ;
@@ -56,14 +57,24 @@ fn main() {
56
57
57
58
println ! ( "{progress_status}" ) ;
58
59
59
- let status = get_status ( & repo) ;
60
+ let status = get_status ( & repo) . into ( ) ;
60
61
61
62
exit ( status)
62
63
}
63
64
64
- fn get_status ( repo : & Repo ) -> i32 {
65
+ #[ derive( Debug , IntoPrimitive ) ]
66
+ #[ repr( i32 ) ]
67
+ enum Status {
68
+ Unchange = 5 ,
69
+ Change = 6 ,
70
+ Untracked = 7 ,
71
+ HasError = 8 ,
72
+ Disable = 9 ,
73
+ }
74
+
75
+ fn get_status ( repo : & Repo ) -> Status {
65
76
if env:: var ( "BASH_DISABLE_GIT_FILE_TRACKING" ) . is_ok ( ) {
66
- return 9 ;
77
+ return Status :: Disable ;
67
78
}
68
79
69
80
let repo = repo. repo . to_thread_local ( ) ;
@@ -76,7 +87,7 @@ fn get_status(repo: &Repo) -> i32 {
76
87
. status ( progress:: Discard )
77
88
. inspect_err ( |e| debug ! ( "{e}" ) )
78
89
else {
79
- return 8 ;
90
+ return Status :: HasError ;
80
91
} ;
81
92
82
93
let status = status. index_worktree_submodules ( Submodule :: AsConfigured { check_dirty : true } ) ;
@@ -105,14 +116,16 @@ fn get_status(repo: &Repo) -> i32 {
105
116
// This will start the status machinery, collecting status items in the background.
106
117
// Thus, we can do some work in this thread without blocking, before starting to count status items.
107
118
let Ok ( status) = status. into_iter ( None ) . inspect_err ( |e| debug ! ( "{e}" ) ) else {
108
- return 8 ;
119
+ return Status :: HasError ;
109
120
} ;
110
121
122
+ let mut is_untracked = false ;
123
+
111
124
for change in status. filter_map ( Result :: ok) {
112
125
use gix:: status;
113
126
match & change {
114
127
status:: Item :: TreeIndex ( _) => {
115
- return 6 ;
128
+ return Status :: Change ;
116
129
}
117
130
status:: Item :: IndexWorktree ( change) => {
118
131
use gix:: status:: index_worktree:: Item ;
@@ -122,13 +135,13 @@ fn get_status(repo: &Repo) -> i32 {
122
135
status : EntryStatus :: Conflict ( _) ,
123
136
..
124
137
} => {
125
- return 6 ;
138
+ return Status :: Change ;
126
139
}
127
140
Item :: Modification {
128
141
status : EntryStatus :: Change ( Change :: Removed ) ,
129
142
..
130
143
} => {
131
- return 6 ;
144
+ return Status :: Change ;
132
145
}
133
146
Item :: Modification {
134
147
status :
@@ -138,13 +151,13 @@ fn get_status(repo: &Repo) -> i32 {
138
151
) ,
139
152
..
140
153
} => {
141
- return 6 ;
154
+ return Status :: Change ;
142
155
}
143
156
Item :: Modification {
144
157
status : EntryStatus :: Change ( Change :: Type { .. } ) ,
145
158
..
146
159
} => {
147
- return 6 ;
160
+ return Status :: Change ;
148
161
}
149
162
Item :: DirectoryContents {
150
163
entry :
@@ -154,7 +167,7 @@ fn get_status(repo: &Repo) -> i32 {
154
167
} ,
155
168
..
156
169
} => {
157
- return 7 ;
170
+ is_untracked = true ;
158
171
}
159
172
Item :: Rewrite { .. } => {
160
173
unreachable ! (
@@ -167,16 +180,20 @@ fn get_status(repo: &Repo) -> i32 {
167
180
}
168
181
}
169
182
170
- 5
183
+ if is_untracked {
184
+ return Status :: Untracked ;
185
+ }
186
+
187
+ Status :: Unchange
171
188
}
172
189
173
- fn get_status_sparse ( ) -> i32 {
190
+ fn get_status_sparse ( ) -> Status {
174
191
let cmd = Command :: new ( "git" )
175
192
. arg ( "status" )
176
193
. arg ( "--porcelain" )
177
194
. output ( ) ;
178
195
179
- let mut status = 0 ;
196
+ let mut status = Status :: Unchange ;
180
197
181
198
if let Ok ( cmd) = cmd {
182
199
if cmd. status . success ( ) {
@@ -188,25 +205,23 @@ fn get_status_sparse() -> i32 {
188
205
. map ( |x| x. 0 ) ;
189
206
190
207
match out_iter. next ( ) {
191
- None => {
192
- status = 5 ;
193
- }
208
+ None => { }
194
209
Some ( x)
195
210
if MODIFY_STATUS . contains ( x)
196
211
|| MODIFY_STATUS . contains ( & x[ ..1 ] )
197
212
|| MODIFY_STATUS . contains ( & x[ 1 ..2 ] ) =>
198
213
{
199
- status = 6 ;
214
+ status = Status :: Change ;
200
215
}
201
216
Some ( "??" ) => {
202
- status = 7 ;
217
+ status = Status :: Untracked ;
203
218
}
204
219
_ => { }
205
220
}
206
221
207
222
debug ! ( "git status --porcelain output: {out}" ) ;
208
223
} else {
209
- status = 8 ;
224
+ status = Status :: HasError ;
210
225
}
211
226
}
212
227
0 commit comments