Skip to content

Commit d33738d

Browse files
jrngitster
authored andcommitted
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>
1 parent eab58f1 commit d33738d

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

editor.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
66
{
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");
810

9-
editor = getenv("GIT_EDITOR");
1011
if (!editor && editor_program)
1112
editor = editor_program;
12-
if (!editor)
13+
if (!editor && !terminal_is_dumb)
1314
editor = getenv("VISUAL");
1415
if (!editor)
1516
editor = getenv("EDITOR");
1617

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");
2020

2121
if (!editor)
2222
editor = "vi";

t/t7005-editor.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ test_expect_success 'dumb should error out when falling back on vi' '
4242
fi
4343
'
4444

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+
4555
TERM=vt100
4656
export TERM
4757
for i in vi EDITOR VISUAL core_editor GIT_EDITOR

t/t7501-commit.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ chmod 755 editor
8686

8787
test_expect_success \
8888
"amend commit" \
89-
"VISUAL=./editor git commit --amend"
89+
"EDITOR=./editor git commit --amend"
9090

9191
test_expect_success \
9292
"passing -m and -F" \
@@ -107,7 +107,7 @@ chmod 755 editor
107107
test_expect_success \
108108
"editing message from other commit" \
109109
"echo 'hula hula' >file && \
110-
VISUAL=./editor git commit -c HEAD^ -a"
110+
EDITOR=./editor git commit -c HEAD^ -a"
111111

112112
test_expect_success \
113113
"message from stdin" \
@@ -141,10 +141,10 @@ EOF
141141
test_expect_success \
142142
'editor not invoked if -F is given' '
143143
echo "moo" >file &&
144-
VISUAL=./editor git commit -a -F msg &&
144+
EDITOR=./editor git commit -a -F msg &&
145145
git show -s --pretty=format:"%s" | grep -q good &&
146146
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 - &&
148148
git show -s --pretty=format:"%s" | grep -q good
149149
'
150150
# We could just check the head sha1, but checking each commit makes it

t/test-lib.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ TZ=UTC
3030
TERM=dumb
3131
export LANG LC_ALL PAGER TERM TZ
3232
EDITOR=:
33-
VISUAL=:
33+
unset VISUAL
3434
unset GIT_EDITOR
3535
unset AUTHOR_DATE
3636
unset AUTHOR_EMAIL
@@ -58,7 +58,7 @@ GIT_MERGE_VERBOSITY=5
5858
export GIT_MERGE_VERBOSITY
5959
export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
6060
export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
61-
export EDITOR VISUAL
61+
export EDITOR
6262
GIT_TEST_CMP=${GIT_TEST_CMP:-diff -u}
6363

6464
# Protect ourselves from common misconfiguration to export
@@ -207,8 +207,8 @@ trap 'die' EXIT
207207
test_set_editor () {
208208
FAKE_EDITOR="$1"
209209
export FAKE_EDITOR
210-
VISUAL='"$FAKE_EDITOR"'
211-
export VISUAL
210+
EDITOR='"$FAKE_EDITOR"'
211+
export EDITOR
212212
}
213213

214214
test_tick () {

0 commit comments

Comments
 (0)