Skip to content

Commit af778c1

Browse files
committed
refactor: use Status enum to replace return code
1 parent bb538d6 commit af778c1

File tree

3 files changed

+100
-21
lines changed

3 files changed

+100
-21
lines changed

Cargo.lock

+64
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
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

+35-20
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!("this kind of rename tracking isn't enabled by default and specific to gitoxide")
@@ -164,16 +177,20 @@ fn get_status(repo: &Repo) -> i32 {
164177
}
165178
}
166179

167-
5
180+
if is_untracked {
181+
return Status::Untracked;
182+
}
183+
184+
Status::Unchange
168185
}
169186

170-
fn get_status_sparse() -> i32 {
187+
fn get_status_sparse() -> Status {
171188
let cmd = Command::new("git")
172189
.arg("status")
173190
.arg("--porcelain")
174191
.output();
175192

176-
let mut status = 0;
193+
let mut status = Status::Unchange;
177194

178195
if let Ok(cmd) = cmd {
179196
if cmd.status.success() {
@@ -185,25 +202,23 @@ fn get_status_sparse() -> i32 {
185202
.map(|x| x.0);
186203

187204
match out_iter.next() {
188-
None => {
189-
status = 5;
190-
}
205+
None => {}
191206
Some(x)
192207
if MODIFY_STATUS.contains(x)
193208
|| MODIFY_STATUS.contains(&x[..1])
194209
|| MODIFY_STATUS.contains(&x[1..2]) =>
195210
{
196-
status = 6;
211+
status = Status::Change;
197212
}
198213
Some("??") => {
199-
status = 7;
214+
status = Status::Untracked;
200215
}
201216
_ => {}
202217
}
203218

204219
debug!("git status --porcelain output: {out}");
205220
} else {
206-
status = 8;
221+
status = Status::HasError;
207222
}
208223
}
209224

0 commit comments

Comments
 (0)