Skip to content

Commit

Permalink
Provide a build time default-editor setting
Browse files Browse the repository at this point in the history
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 <jrnieder@gmail.com>
Signed-off-by: Ben Walton <bwalton@artsci.utoronto.ca>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jonathan Nieder authored and Junio C Hamano committed Nov 13, 2009
1 parent dec543e commit 8f4b576
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ all::
# memory allocators with the nedmalloc allocator written by Niall Douglas.
#
# Define NO_REGEX if you have no or inferior regex support in your C library.
#
# Define DEFAULT_EDITOR to a sensible editor command (defaults to "vi") if you
# want to use something different. The value will be interpreted by the shell
# if necessary when it is used. Examples:
#
# DEFAULT_EDITOR='~/bin/vi',
# DEFAULT_EDITOR='$GIT_FALLBACK_EDITOR',
# DEFAULT_EDITOR='"C:\Program Files\Vim\gvim.exe" --nofork'

GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE
@$(SHELL_PATH) ./GIT-VERSION-GEN
Expand Down Expand Up @@ -1363,6 +1371,15 @@ BASIC_CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER_SQ)' \
$(COMPAT_CFLAGS)
LIB_OBJS += $(COMPAT_OBJS)

# Quote for C

ifdef DEFAULT_EDITOR
DEFAULT_EDITOR_CQ = "$(subst ",\",$(subst \,\\,$(DEFAULT_EDITOR)))"
DEFAULT_EDITOR_CQ_SQ = $(subst ','\'',$(DEFAULT_EDITOR_CQ))

BASIC_CFLAGS += -DDEFAULT_EDITOR='$(DEFAULT_EDITOR_CQ_SQ)'
endif

ALL_CFLAGS += $(BASIC_CFLAGS)
ALL_LDFLAGS += $(BASIC_LDFLAGS)

Expand Down
6 changes: 5 additions & 1 deletion editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include "strbuf.h"
#include "run-command.h"

#ifndef DEFAULT_EDITOR
#define DEFAULT_EDITOR "vi"
#endif

const char *git_editor(void)
{
const char *editor = getenv("GIT_EDITOR");
Expand All @@ -19,7 +23,7 @@ const char *git_editor(void)
return NULL;

if (!editor)
editor = "vi";
editor = DEFAULT_EDITOR;

return editor;
}
Expand Down
37 changes: 25 additions & 12 deletions t/t7005-editor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,40 @@ test_description='GIT_EDITOR, core.editor, and stuff'

. ./test-lib.sh

for i in GIT_EDITOR core_editor EDITOR VISUAL vi
unset EDITOR VISUAL GIT_EDITOR

test_expect_success 'determine default editor' '
vi=$(TERM=vt100 git var GIT_EDITOR) &&
test -n "$vi"
'

if ! expr "$vi" : '^[a-z]*$' >/dev/null
then
vi=
fi

for i in GIT_EDITOR core_editor EDITOR VISUAL $vi
do
cat >e-$i.sh <<-EOF
#!$SHELL_PATH
echo "Edited by $i" >"\$1"
EOF
chmod +x e-$i.sh
done
unset vi
mv e-vi.sh vi
unset EDITOR VISUAL GIT_EDITOR

if ! test -z "$vi"
then
mv e-$vi.sh $vi
fi

test_expect_success setup '
msg="Hand edited" &&
msg="Hand-edited" &&
test_commit "$msg" &&
echo "$msg" >expect &&
git add vi &&
test_tick &&
git commit -m "$msg" &&
git show -s --pretty=oneline |
sed -e "s/^[0-9a-f]* //" >actual &&
git show -s --format=%s > actual &&
diff actual expect
'
Expand Down Expand Up @@ -54,7 +67,7 @@ test_expect_success 'dumb should prefer EDITOR to VISUAL' '

TERM=vt100
export TERM
for i in vi EDITOR VISUAL core_editor GIT_EDITOR
for i in $vi EDITOR VISUAL core_editor GIT_EDITOR
do
echo "Edited by $i" >expect
unset EDITOR VISUAL GIT_EDITOR
Expand All @@ -78,7 +91,7 @@ done

unset EDITOR VISUAL GIT_EDITOR
git config --unset-all core.editor
for i in vi EDITOR VISUAL core_editor GIT_EDITOR
for i in $vi EDITOR VISUAL core_editor GIT_EDITOR
do
echo "Edited by $i" >expect
case "$i" in
Expand Down

0 comments on commit 8f4b576

Please sign in to comment.