Skip to content

Commit ebb7eec

Browse files
committed
refactor: Stupid.setup_git_env() helper
1 parent 846fe0b commit ebb7eec

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/stupid/mod.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ impl<'repo, 'index> StupidContext<'repo, 'index> {
8787

8888
fn git(&self) -> Command {
8989
let mut command = Command::new("git");
90-
self.git_dir.map(|p| command.env("GIT_DIR", p));
91-
self.work_dir.map(|p| command.env("GIT_WORK_TREE", p));
92-
self.index_path.map(|p| command.env("GIT_INDEX_FILE", p));
90+
self.setup_git_env(&mut command);
9391
command
9492
}
9593

@@ -112,6 +110,14 @@ impl<'repo, 'index> StupidContext<'repo, 'index> {
112110
Ok(command)
113111
}
114112

113+
fn setup_git_env(&self, command: &mut Command) {
114+
self.git_dir.map(|git_dir| command.env("GIT_DIR", git_dir));
115+
self.work_dir
116+
.map(|work_dir| command.env("GIT_WORK_TREE", work_dir));
117+
self.index_path
118+
.map(|index_path| command.env("GIT_INDEX_FILE", index_path));
119+
}
120+
115121
fn at_least_version(&self, version: &StupidVersion) -> Result<bool> {
116122
let mut git_version = self.git_version.borrow_mut();
117123
if let Some(git_version) = git_version.as_ref() {
@@ -683,9 +689,7 @@ impl<'repo, 'index> StupidContext<'repo, 'index> {
683689
SpecArg: AsRef<OsStr>,
684690
{
685691
let mut command = Command::new("gitk");
686-
self.git_dir.map(|p| command.env("GIT_DIR", p));
687-
self.work_dir.map(|p| command.env("GIT_WORK_TREE", p));
688-
self.index_path.map(|p| command.env("GIT_INDEX_FILE", p));
692+
self.setup_git_env(&mut command);
689693
command.arg(commit_id.to_string());
690694
if let Some(pathspecs) = pathspecs {
691695
command.arg("--");
@@ -1325,9 +1329,7 @@ impl<'repo, 'index> StupidContext<'repo, 'index> {
13251329
let mut args = user_cmd_str.split(|c: char| c.is_ascii_whitespace());
13261330
if let Some(command_name) = args.next() {
13271331
let mut command = Command::new(command_name);
1328-
self.git_dir.map(|p| command.env("GIT_DIR", p));
1329-
self.work_dir.map(|p| command.env("GIT_WORK_TREE", p));
1330-
self.index_path.map(|p| command.env("GIT_INDEX_FILE", p));
1332+
self.setup_git_env(&mut command);
13311333
let status = command
13321334
.args(args)
13331335
.arg(remote_name)
@@ -1357,9 +1359,7 @@ impl<'repo, 'index> StupidContext<'repo, 'index> {
13571359
let mut args = user_cmd_str.split(|c: char| c.is_ascii_whitespace());
13581360
if let Some(command_name) = args.next() {
13591361
let mut command = Command::new(command_name);
1360-
self.git_dir.map(|p| command.env("GIT_DIR", p));
1361-
self.work_dir.map(|p| command.env("GIT_WORK_TREE", p));
1362-
self.index_path.map(|p| command.env("GIT_INDEX_FILE", p));
1362+
self.setup_git_env(&mut command);
13631363
let status = command
13641364
.args(args)
13651365
.arg(remote_name)
@@ -1388,9 +1388,7 @@ impl<'repo, 'index> StupidContext<'repo, 'index> {
13881388
let mut args = user_cmd_str.split(|c: char| c.is_ascii_whitespace());
13891389
if let Some(command_name) = args.next() {
13901390
let mut command = Command::new(command_name);
1391-
self.git_dir.map(|p| command.env("GIT_DIR", p));
1392-
self.work_dir.map(|p| command.env("GIT_WORK_TREE", p));
1393-
self.index_path.map(|p| command.env("GIT_INDEX_FILE", p));
1391+
self.setup_git_env(&mut command);
13941392
let status = command
13951393
.args(args)
13961394
.arg(target.to_string())

0 commit comments

Comments
 (0)