Do not use VISUAL editor on dumb terminals
Refuse to use $VISUAL and fall back to $EDITOR if TERM is unset or set to "dumb". Traditionally, VISUAL is set to a screen editor and EDITOR to a line-based editor, which should be more useful in that situation. vim, for example, is happy to assume a terminal supports ANSI sequences even if TERM is dumb (e.g., when running from a text editor like Acme). git already refuses to fall back to vi on a dumb terminal if GIT_EDITOR, core.editor, VISUAL, and EDITOR are unset, but without this patch, that check is suppressed by VISUAL=vi. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
Nov 11, 2009 at 17:56 UTC
d33738d7d3ed42a956c74d0125eb2b3abda451b7
4 files changed
+24
-14
editor.c
+6
-6
@@ -4,19 +4,19 @@
4
5
int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
6
{
7
- const char *editor, *terminal;
7
+ const char *editor = getenv("GIT_EDITOR");
8
+ const char *terminal = getenv("TERM");
9
+ int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb");
10
9
- editor = getenv("GIT_EDITOR");
11
if (!editor && editor_program)
12
editor = editor_program;
12
- if (!editor)
13
+ if (!editor && !terminal_is_dumb)
14
editor = getenv("VISUAL");
15
if (!editor)
16
editor = getenv("EDITOR");
17
17
- terminal = getenv("TERM");
18
- if (!editor && (!terminal || !strcmp(terminal, "dumb")))
19
- return error("Terminal is dumb but no VISUAL nor EDITOR defined.");
18
+ if (!editor && terminal_is_dumb)
19
+ return error("terminal is dumb, but EDITOR unset");
20
21
if (!editor)
22
editor = "vi";
t/t7005-editor.sh
+10
@@ -42,6 +42,16 @@ test_expect_success 'dumb should error out when falling back on vi' '
42
fi
43
'
44
45
+test_expect_success 'dumb should prefer EDITOR to VISUAL' '
46
+
47
+ EDITOR=./e-EDITOR.sh &&
48
+ VISUAL=./e-VISUAL.sh &&
49
+ export EDITOR VISUAL &&
50
+ git commit --amend &&
51
+ test "$(git show -s --format=%s)" = "Edited by EDITOR"
52
+
53
+'
54
+
55
TERM=vt100
56
export TERM
57
for i in vi EDITOR VISUAL core_editor GIT_EDITOR
t/t7501-commit.sh
+4
-4
@@ -86,7 +86,7 @@ chmod 755 editor
86
87
test_expect_success \
88
"amend commit" \
89
- "VISUAL=./editor git commit --amend"
89
+ "EDITOR=./editor git commit --amend"
90
91
test_expect_success \
92
"passing -m and -F" \
@@ -107,7 +107,7 @@ chmod 755 editor
107
test_expect_success \
108
"editing message from other commit" \
109
"echo 'hula hula' >file && \
110
- VISUAL=./editor git commit -c HEAD^ -a"
110
+ EDITOR=./editor git commit -c HEAD^ -a"
111
112
test_expect_success \
113
"message from stdin" \
@@ -141,10 +141,10 @@ EOF
141
test_expect_success \
142
'editor not invoked if -F is given' '
143
echo "moo" >file &&
144
- VISUAL=./editor git commit -a -F msg &&
144
+ EDITOR=./editor git commit -a -F msg &&
145
git show -s --pretty=format:"%s" | grep -q good &&
146
echo "quack" >file &&
147
- echo "Another good message." | VISUAL=./editor git commit -a -F - &&
147
+ echo "Another good message." | EDITOR=./editor git commit -a -F - &&
148
git show -s --pretty=format:"%s" | grep -q good
149
'
150
# We could just check the head sha1, but checking each commit makes it
t/test-lib.sh
+4
-4
@@ -30,7 +30,7 @@ TZ=UTC
30
TERM=dumb
31
export LANG LC_ALL PAGER TERM TZ
32
EDITOR=:
33
-VISUAL=:
33
+unset VISUAL
34
unset GIT_EDITOR
35
unset AUTHOR_DATE
36
unset AUTHOR_EMAIL
@@ -58,7 +58,7 @@ GIT_MERGE_VERBOSITY=5
58
export GIT_MERGE_VERBOSITY
59
export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
60
export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
61
-export EDITOR VISUAL
61
+export EDITOR
62
GIT_TEST_CMP=${GIT_TEST_CMP:-diff -u}
63
64
# Protect ourselves from common misconfiguration to export
@@ -207,8 +207,8 @@ trap 'die' EXIT
207
test_set_editor () {
208
FAKE_EDITOR="$1"
209
export FAKE_EDITOR
210
- VISUAL='"$FAKE_EDITOR"'
211
- export VISUAL
210
+ EDITOR='"$FAKE_EDITOR"'
211
+ export EDITOR
212
}
213
214
test_tick () {