Skip to content

Commit e2ac211

Browse files
injustMitMaro
authored andcommitted
Use GIT_EDITOR environment variable
1 parent a218aea commit e2ac211

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

src/config/git_config.rs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@ use crate::{
88
git::Config,
99
};
1010

11-
fn editor_from_env() -> String {
12-
env::var("VISUAL")
13-
.or_else(|_| env::var("EDITOR"))
14-
.unwrap_or_else(|_| String::from("vi"))
11+
fn get_default_editor(git_config: Option<&Config>) -> Result<String, ConfigError> {
12+
env::var("GIT_EDITOR").or_else(|_| {
13+
get_string(
14+
git_config,
15+
"core.editor",
16+
(env::var("VISUAL")
17+
.or_else(|_| env::var("EDITOR"))
18+
.unwrap_or_else(|_| String::from("vi")))
19+
.as_str(),
20+
)
21+
})
1522
}
1623

1724
/// Represents the git configuration options.
@@ -64,7 +71,7 @@ impl GitConfig {
6471
diff_rename_limit: get_unsigned_integer(git_config, "diff.renameLimit", 200)?,
6572
diff_renames,
6673
diff_copies,
67-
editor: get_string(git_config, "core.editor", editor_from_env().as_str())?,
74+
editor: get_default_editor(git_config)?,
6875
})
6976
}
7077
}
@@ -153,18 +160,38 @@ mod tests {
153160
#[test]
154161
fn git_editor_default_no_env() {
155162
with_env_var(
156-
&[EnvVarAction::Remove("VISUAL"), EnvVarAction::Remove("EDITOR")],
163+
&[
164+
EnvVarAction::Remove("GIT_EDITOR"),
165+
EnvVarAction::Remove("VISUAL"),
166+
EnvVarAction::Remove("EDITOR"),
167+
],
157168
|| {
158169
let config = GitConfig::new_with_config(None).unwrap();
159170
assert_eq!(config.editor, "vi");
160171
},
161172
);
162173
}
163174

175+
#[test]
176+
fn git_editor_default_git_editor_env() {
177+
with_env_var(
178+
&[
179+
EnvVarAction::Remove("VISUAL"),
180+
EnvVarAction::Remove("EDITOR"),
181+
EnvVarAction::Set("GIT_EDITOR", String::from("git-editor")),
182+
],
183+
|| {
184+
let config = GitConfig::new_with_config(None).unwrap();
185+
assert_eq!(config.editor, "git-editor");
186+
},
187+
);
188+
}
189+
164190
#[test]
165191
fn git_editor_default_visual_env() {
166192
with_env_var(
167193
&[
194+
EnvVarAction::Remove("GIT_EDITOR"),
168195
EnvVarAction::Remove("EDITOR"),
169196
EnvVarAction::Set("VISUAL", String::from("visual-editor")),
170197
],
@@ -179,6 +206,7 @@ mod tests {
179206
fn git_editor_default_editor_env() {
180207
with_env_var(
181208
&[
209+
EnvVarAction::Remove("GIT_EDITOR"),
182210
EnvVarAction::Remove("VISUAL"),
183211
EnvVarAction::Set("EDITOR", String::from("editor")),
184212
],
@@ -192,7 +220,11 @@ mod tests {
192220
#[test]
193221
fn git_editor() {
194222
with_env_var(
195-
&[EnvVarAction::Remove("VISUAL"), EnvVarAction::Remove("EDITOR")],
223+
&[
224+
EnvVarAction::Remove("GIT_EDITOR"),
225+
EnvVarAction::Remove("VISUAL"),
226+
EnvVarAction::Remove("EDITOR"),
227+
],
196228
|| {
197229
with_git_config(&["[core]", "editor = custom"], |git_config| {
198230
let config = GitConfig::new_with_config(Some(&git_config)).unwrap();
@@ -235,7 +267,11 @@ mod tests {
235267
#[test]
236268
fn git_editor_invalid() {
237269
with_env_var(
238-
&[EnvVarAction::Remove("VISUAL"), EnvVarAction::Remove("EDITOR")],
270+
&[
271+
EnvVarAction::Remove("GIT_EDITOR"),
272+
EnvVarAction::Remove("VISUAL"),
273+
EnvVarAction::Remove("EDITOR"),
274+
],
239275
|| {
240276
with_git_config(
241277
&["[core]", format!("editor = {}", invalid_utf()).as_str()],

0 commit comments

Comments
 (0)