Skip to content

Commit d0ae6eb

Browse files
committed
refactor: use Status enum to replace return code
1 parent 48de2d0 commit d0ae6eb

File tree

3 files changed

+100
-21
lines changed

3 files changed

+100
-21
lines changed

Cargo.lock

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ anyhow = "1.0"
1111
env_logger = "0.11"
1212
log = "0.4"
1313
rustix = { version = "1", features = ["process"] }
14-
14+
num_enum = "0.7.3"
1515

1616
[profile.release]
1717
lto = "thin"

src/main.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use gix::{
88
sec::{self, trust::DefaultForLevel},
99
};
1010
use log::debug;
11+
use num_enum::IntoPrimitive;
1112
use std::borrow::Cow;
1213
use std::env;
1314
use std::path::Path;
@@ -55,14 +56,24 @@ fn main() {
5556

5657
println!("{progress_status}");
5758

58-
let status = get_status(&repo);
59+
let status = get_status(&repo).into();
5960

6061
exit(status)
6162
}
6263

63-
fn get_status(repo: &Repo) -> i32 {
64+
#[derive(Debug, IntoPrimitive)]
65+
#[repr(i32)]
66+
enum Status {
67+
Unchange = 5,
68+
Change = 6,
69+
Untracked = 7,
70+
HasError = 8,
71+
Disable = 9,
72+
}
73+
74+
fn get_status(repo: &Repo) -> Status {
6475
if env::var("BASH_DISABLE_GIT_FILE_TRACKING").is_ok() {
65-
return 9;
76+
return Status::Disable;
6677
}
6778

6879
let repo = repo.repo.to_thread_local();
@@ -75,7 +86,7 @@ fn get_status(repo: &Repo) -> i32 {
7586
.status(progress::Discard)
7687
.inspect_err(|e| debug!("{e}"))
7788
else {
78-
return 8;
89+
return Status::HasError;
7990
};
8091

8192
let status = status.index_worktree_submodules(Submodule::AsConfigured { check_dirty: true });
@@ -104,14 +115,16 @@ fn get_status(repo: &Repo) -> i32 {
104115
// This will start the status machinery, collecting status items in the background.
105116
// Thus, we can do some work in this thread without blocking, before starting to count status items.
106117
let Ok(status) = status.into_iter(None).inspect_err(|e| debug!("{e}")) else {
107-
return 8;
118+
return Status::HasError;
108119
};
109120

121+
let mut is_untracked = false;
122+
110123
for change in status.filter_map(Result::ok) {
111124
use gix::status;
112125
match &change {
113126
status::Item::TreeIndex(_) => {
114-
return 6;
127+
return Status::Change;
115128
}
116129
status::Item::IndexWorktree(change) => {
117130
use gix::status::index_worktree::Item;
@@ -121,13 +134,13 @@ fn get_status(repo: &Repo) -> i32 {
121134
status: EntryStatus::Conflict(_),
122135
..
123136
} => {
124-
return 6;
137+
return Status::Change;
125138
}
126139
Item::Modification {
127140
status: EntryStatus::Change(Change::Removed),
128141
..
129142
} => {
130-
return 6;
143+
return Status::Change;
131144
}
132145
Item::Modification {
133146
status:
@@ -137,13 +150,13 @@ fn get_status(repo: &Repo) -> i32 {
137150
),
138151
..
139152
} => {
140-
return 6;
153+
return Status::Change;
141154
}
142155
Item::Modification {
143156
status: EntryStatus::Change(Change::Type { .. }),
144157
..
145158
} => {
146-
return 6;
159+
return Status::Change;
147160
}
148161
Item::DirectoryContents {
149162
entry:
@@ -153,7 +166,7 @@ fn get_status(repo: &Repo) -> i32 {
153166
},
154167
..
155168
} => {
156-
return 7;
169+
is_untracked = true;
157170
}
158171
Item::Rewrite { .. } => {
159172
unreachable!(
@@ -166,16 +179,20 @@ fn get_status(repo: &Repo) -> i32 {
166179
}
167180
}
168181

169-
5
182+
if is_untracked {
183+
return Status::Untracked;
184+
}
185+
186+
Status::Unchange
170187
}
171188

172-
fn get_status_sparse() -> i32 {
189+
fn get_status_sparse() -> Status {
173190
let cmd = Command::new("git")
174191
.arg("status")
175192
.arg("--porcelain")
176193
.output();
177194

178-
let mut status = 0;
195+
let mut status = Status::Unchange;
179196

180197
if let Ok(cmd) = cmd {
181198
if cmd.status.success() {
@@ -187,25 +204,23 @@ fn get_status_sparse() -> i32 {
187204
.map(|x| x.0);
188205

189206
match out_iter.next() {
190-
None => {
191-
status = 5;
192-
}
207+
None => {}
193208
Some(x)
194209
if MODIFY_STATUS.contains(x)
195210
|| MODIFY_STATUS.contains(&x[..1])
196211
|| MODIFY_STATUS.contains(&x[1..2]) =>
197212
{
198-
status = 6;
213+
status = Status::Change;
199214
}
200215
Some("??") => {
201-
status = 7;
216+
status = Status::Untracked;
202217
}
203218
_ => {}
204219
}
205220

206221
debug!("git status --porcelain output: {out}");
207222
} else {
208-
status = 8;
223+
status = Status::HasError;
209224
}
210225
}
211226

0 commit comments

Comments
 (0)