Skip to content

Commit 8f4b576

Browse files
jrngitster
authored andcommitted
Provide a build time default-editor setting
Provide a DEFAULT_EDITOR knob to allow setting the fallback editor to use instead of vi (when VISUAL, EDITOR, and GIT_EDITOR are unset). The value can be set at build time according to a system’s policy. For example, on Debian systems, the default editor should be the 'editor' command. Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Ben Walton <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dec543e commit 8f4b576

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ all::
200200
# memory allocators with the nedmalloc allocator written by Niall Douglas.
201201
#
202202
# Define NO_REGEX if you have no or inferior regex support in your C library.
203+
#
204+
# Define DEFAULT_EDITOR to a sensible editor command (defaults to "vi") if you
205+
# want to use something different. The value will be interpreted by the shell
206+
# if necessary when it is used. Examples:
207+
#
208+
# DEFAULT_EDITOR='~/bin/vi',
209+
# DEFAULT_EDITOR='$GIT_FALLBACK_EDITOR',
210+
# DEFAULT_EDITOR='"C:\Program Files\Vim\gvim.exe" --nofork'
203211

204212
GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE
205213
@$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -1363,6 +1371,15 @@ BASIC_CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER_SQ)' \
13631371
$(COMPAT_CFLAGS)
13641372
LIB_OBJS += $(COMPAT_OBJS)
13651373

1374+
# Quote for C
1375+
1376+
ifdef DEFAULT_EDITOR
1377+
DEFAULT_EDITOR_CQ = "$(subst ",\",$(subst \,\\,$(DEFAULT_EDITOR)))"
1378+
DEFAULT_EDITOR_CQ_SQ = $(subst ','\'',$(DEFAULT_EDITOR_CQ))
1379+
1380+
BASIC_CFLAGS += -DDEFAULT_EDITOR='$(DEFAULT_EDITOR_CQ_SQ)'
1381+
endif
1382+
13661383
ALL_CFLAGS += $(BASIC_CFLAGS)
13671384
ALL_LDFLAGS += $(BASIC_LDFLAGS)
13681385

editor.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#include "strbuf.h"
33
#include "run-command.h"
44

5+
#ifndef DEFAULT_EDITOR
6+
#define DEFAULT_EDITOR "vi"
7+
#endif
8+
59
const char *git_editor(void)
610
{
711
const char *editor = getenv("GIT_EDITOR");
@@ -19,7 +23,7 @@ const char *git_editor(void)
1923
return NULL;
2024

2125
if (!editor)
22-
editor = "vi";
26+
editor = DEFAULT_EDITOR;
2327

2428
return editor;
2529
}

t/t7005-editor.sh

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,40 @@ test_description='GIT_EDITOR, core.editor, and stuff'
44

55
. ./test-lib.sh
66

7-
for i in GIT_EDITOR core_editor EDITOR VISUAL vi
7+
unset EDITOR VISUAL GIT_EDITOR
8+
9+
test_expect_success 'determine default editor' '
10+
11+
vi=$(TERM=vt100 git var GIT_EDITOR) &&
12+
test -n "$vi"
13+
14+
'
15+
16+
if ! expr "$vi" : '^[a-z]*$' >/dev/null
17+
then
18+
vi=
19+
fi
20+
21+
for i in GIT_EDITOR core_editor EDITOR VISUAL $vi
822
do
923
cat >e-$i.sh <<-EOF
1024
#!$SHELL_PATH
1125
echo "Edited by $i" >"\$1"
1226
EOF
1327
chmod +x e-$i.sh
1428
done
15-
unset vi
16-
mv e-vi.sh vi
17-
unset EDITOR VISUAL GIT_EDITOR
29+
30+
if ! test -z "$vi"
31+
then
32+
mv e-$vi.sh $vi
33+
fi
1834

1935
test_expect_success setup '
2036
21-
msg="Hand edited" &&
37+
msg="Hand-edited" &&
38+
test_commit "$msg" &&
2239
echo "$msg" >expect &&
23-
git add vi &&
24-
test_tick &&
25-
git commit -m "$msg" &&
26-
git show -s --pretty=oneline |
27-
sed -e "s/^[0-9a-f]* //" >actual &&
40+
git show -s --format=%s > actual &&
2841
diff actual expect
2942
3043
'
@@ -54,7 +67,7 @@ test_expect_success 'dumb should prefer EDITOR to VISUAL' '
5467

5568
TERM=vt100
5669
export TERM
57-
for i in vi EDITOR VISUAL core_editor GIT_EDITOR
70+
for i in $vi EDITOR VISUAL core_editor GIT_EDITOR
5871
do
5972
echo "Edited by $i" >expect
6073
unset EDITOR VISUAL GIT_EDITOR
@@ -78,7 +91,7 @@ done
7891

7992
unset EDITOR VISUAL GIT_EDITOR
8093
git config --unset-all core.editor
81-
for i in vi EDITOR VISUAL core_editor GIT_EDITOR
94+
for i in $vi EDITOR VISUAL core_editor GIT_EDITOR
8295
do
8396
echo "Edited by $i" >expect
8497
case "$i" in

0 commit comments

Comments
 (0)