Skip to content

Commit

Permalink
Use GIT_EDITOR environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
injust authored and MitMaro committed Aug 12, 2024
1 parent a218aea commit e2ac211
Showing 1 changed file with 44 additions and 8 deletions.
52 changes: 44 additions & 8 deletions src/config/git_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ use crate::{
git::Config,
};

fn editor_from_env() -> String {
env::var("VISUAL")
.or_else(|_| env::var("EDITOR"))
.unwrap_or_else(|_| String::from("vi"))
fn get_default_editor(git_config: Option<&Config>) -> Result<String, ConfigError> {
env::var("GIT_EDITOR").or_else(|_| {
get_string(
git_config,
"core.editor",
(env::var("VISUAL")
.or_else(|_| env::var("EDITOR"))
.unwrap_or_else(|_| String::from("vi")))
.as_str(),
)
})
}

/// Represents the git configuration options.
Expand Down Expand Up @@ -64,7 +71,7 @@ impl GitConfig {
diff_rename_limit: get_unsigned_integer(git_config, "diff.renameLimit", 200)?,
diff_renames,
diff_copies,
editor: get_string(git_config, "core.editor", editor_from_env().as_str())?,
editor: get_default_editor(git_config)?,
})
}
}
Expand Down Expand Up @@ -153,18 +160,38 @@ mod tests {
#[test]
fn git_editor_default_no_env() {
with_env_var(
&[EnvVarAction::Remove("VISUAL"), EnvVarAction::Remove("EDITOR")],
&[
EnvVarAction::Remove("GIT_EDITOR"),
EnvVarAction::Remove("VISUAL"),
EnvVarAction::Remove("EDITOR"),
],
|| {
let config = GitConfig::new_with_config(None).unwrap();
assert_eq!(config.editor, "vi");
},
);
}

#[test]
fn git_editor_default_git_editor_env() {
with_env_var(
&[
EnvVarAction::Remove("VISUAL"),
EnvVarAction::Remove("EDITOR"),
EnvVarAction::Set("GIT_EDITOR", String::from("git-editor")),
],
|| {
let config = GitConfig::new_with_config(None).unwrap();
assert_eq!(config.editor, "git-editor");
},
);
}

#[test]
fn git_editor_default_visual_env() {
with_env_var(
&[
EnvVarAction::Remove("GIT_EDITOR"),
EnvVarAction::Remove("EDITOR"),
EnvVarAction::Set("VISUAL", String::from("visual-editor")),
],
Expand All @@ -179,6 +206,7 @@ mod tests {
fn git_editor_default_editor_env() {
with_env_var(
&[
EnvVarAction::Remove("GIT_EDITOR"),
EnvVarAction::Remove("VISUAL"),
EnvVarAction::Set("EDITOR", String::from("editor")),
],
Expand All @@ -192,7 +220,11 @@ mod tests {
#[test]
fn git_editor() {
with_env_var(
&[EnvVarAction::Remove("VISUAL"), EnvVarAction::Remove("EDITOR")],
&[
EnvVarAction::Remove("GIT_EDITOR"),
EnvVarAction::Remove("VISUAL"),
EnvVarAction::Remove("EDITOR"),
],
|| {
with_git_config(&["[core]", "editor = custom"], |git_config| {
let config = GitConfig::new_with_config(Some(&git_config)).unwrap();
Expand Down Expand Up @@ -235,7 +267,11 @@ mod tests {
#[test]
fn git_editor_invalid() {
with_env_var(
&[EnvVarAction::Remove("VISUAL"), EnvVarAction::Remove("EDITOR")],
&[
EnvVarAction::Remove("GIT_EDITOR"),
EnvVarAction::Remove("VISUAL"),
EnvVarAction::Remove("EDITOR"),
],
|| {
with_git_config(
&["[core]", format!("editor = {}", invalid_utf()).as_str()],
Expand Down

0 comments on commit e2ac211

Please sign in to comment.