@@ -8,10 +8,17 @@ use crate::{
8
8
git:: Config ,
9
9
} ;
10
10
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
+ } )
15
22
}
16
23
17
24
/// Represents the git configuration options.
@@ -64,7 +71,7 @@ impl GitConfig {
64
71
diff_rename_limit : get_unsigned_integer ( git_config, "diff.renameLimit" , 200 ) ?,
65
72
diff_renames,
66
73
diff_copies,
67
- editor : get_string ( git_config, "core.editor" , editor_from_env ( ) . as_str ( ) ) ?,
74
+ editor : get_default_editor ( git_config) ?,
68
75
} )
69
76
}
70
77
}
@@ -153,18 +160,38 @@ mod tests {
153
160
#[ test]
154
161
fn git_editor_default_no_env ( ) {
155
162
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
+ ] ,
157
168
|| {
158
169
let config = GitConfig :: new_with_config ( None ) . unwrap ( ) ;
159
170
assert_eq ! ( config. editor, "vi" ) ;
160
171
} ,
161
172
) ;
162
173
}
163
174
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
+
164
190
#[ test]
165
191
fn git_editor_default_visual_env ( ) {
166
192
with_env_var (
167
193
& [
194
+ EnvVarAction :: Remove ( "GIT_EDITOR" ) ,
168
195
EnvVarAction :: Remove ( "EDITOR" ) ,
169
196
EnvVarAction :: Set ( "VISUAL" , String :: from ( "visual-editor" ) ) ,
170
197
] ,
@@ -179,6 +206,7 @@ mod tests {
179
206
fn git_editor_default_editor_env ( ) {
180
207
with_env_var (
181
208
& [
209
+ EnvVarAction :: Remove ( "GIT_EDITOR" ) ,
182
210
EnvVarAction :: Remove ( "VISUAL" ) ,
183
211
EnvVarAction :: Set ( "EDITOR" , String :: from ( "editor" ) ) ,
184
212
] ,
@@ -192,7 +220,11 @@ mod tests {
192
220
#[ test]
193
221
fn git_editor ( ) {
194
222
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
+ ] ,
196
228
|| {
197
229
with_git_config ( & [ "[core]" , "editor = custom" ] , |git_config| {
198
230
let config = GitConfig :: new_with_config ( Some ( & git_config) ) . unwrap ( ) ;
@@ -235,7 +267,11 @@ mod tests {
235
267
#[ test]
236
268
fn git_editor_invalid ( ) {
237
269
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
+ ] ,
239
275
|| {
240
276
with_git_config (
241
277
& [ "[core]" , format ! ( "editor = {}" , invalid_utf( ) ) . as_str ( ) ] ,
0 commit comments