Skip to content

Commit 7a65317

Browse files
committed
refactor: use Status enum to replace return code
1 parent cfe7eb4 commit 7a65317

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
@@ -9,6 +9,7 @@ use gix::{
99
sec::{self, trust::DefaultForLevel},
1010
};
1111
use log::debug;
12+
use num_enum::IntoPrimitive;
1213
use std::borrow::Cow;
1314
use std::env;
1415
use std::path::Path;
@@ -56,14 +57,24 @@ fn main() {
5657

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

59-
let status = get_status(&repo);
60+
let status = get_status(&repo).into();
6061

6162
exit(status)
6263
}
6364

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 {
6576
if env::var("BASH_DISABLE_GIT_FILE_TRACKING").is_ok() {
66-
return 9;
77+
return Status::Disable;
6778
}
6879

6980
let repo = repo.repo.to_thread_local();
@@ -76,7 +87,7 @@ fn get_status(repo: &Repo) -> i32 {
7687
.status(progress::Discard)
7788
.inspect_err(|e| debug!("{e}"))
7889
else {
79-
return 8;
90+
return Status::HasError;
8091
};
8192

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

122+
let mut is_untracked = false;
123+
111124
for change in status.filter_map(Result::ok) {
112125
use gix::status;
113126
match &change {
114127
status::Item::TreeIndex(_) => {
115-
return 6;
128+
return Status::Change;
116129
}
117130
status::Item::IndexWorktree(change) => {
118131
use gix::status::index_worktree::Item;
@@ -122,13 +135,13 @@ fn get_status(repo: &Repo) -> i32 {
122135
status: EntryStatus::Conflict(_),
123136
..
124137
} => {
125-
return 6;
138+
return Status::Change;
126139
}
127140
Item::Modification {
128141
status: EntryStatus::Change(Change::Removed),
129142
..
130143
} => {
131-
return 6;
144+
return Status::Change;
132145
}
133146
Item::Modification {
134147
status:
@@ -138,13 +151,13 @@ fn get_status(repo: &Repo) -> i32 {
138151
),
139152
..
140153
} => {
141-
return 6;
154+
return Status::Change;
142155
}
143156
Item::Modification {
144157
status: EntryStatus::Change(Change::Type { .. }),
145158
..
146159
} => {
147-
return 6;
160+
return Status::Change;
148161
}
149162
Item::DirectoryContents {
150163
entry:
@@ -154,7 +167,7 @@ fn get_status(repo: &Repo) -> i32 {
154167
},
155168
..
156169
} => {
157-
return 7;
170+
is_untracked = true;
158171
}
159172
Item::Rewrite { .. } => {
160173
unreachable!(
@@ -167,16 +180,20 @@ fn get_status(repo: &Repo) -> i32 {
167180
}
168181
}
169182

170-
5
183+
if is_untracked {
184+
return Status::Untracked;
185+
}
186+
187+
Status::Unchange
171188
}
172189

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

179-
let mut status = 0;
196+
let mut status = Status::Unchange;
180197

181198
if let Ok(cmd) = cmd {
182199
if cmd.status.success() {
@@ -188,25 +205,23 @@ fn get_status_sparse() -> i32 {
188205
.map(|x| x.0);
189206

190207
match out_iter.next() {
191-
None => {
192-
status = 5;
193-
}
208+
None => {}
194209
Some(x)
195210
if MODIFY_STATUS.contains(x)
196211
|| MODIFY_STATUS.contains(&x[..1])
197212
|| MODIFY_STATUS.contains(&x[1..2]) =>
198213
{
199-
status = 6;
214+
status = Status::Change;
200215
}
201216
Some("??") => {
202-
status = 7;
217+
status = Status::Untracked;
203218
}
204219
_ => {}
205220
}
206221

207222
debug!("git status --porcelain output: {out}");
208223
} else {
209-
status = 8;
224+
status = Status::HasError;
210225
}
211226
}
212227

0 commit comments

Comments
 (0)