Skip to content

Commit 376f39f

Browse files
committed
Merge branch 'jn/editor-pager'
* jn/editor-pager: Provide a build time default-pager setting Provide a build time default-editor setting am -i, git-svn: use "git var GIT_PAGER" add -i, send-email, svn, p4, etc: use "git var GIT_EDITOR" Teach git var about GIT_PAGER Teach git var about GIT_EDITOR Suppress warnings from "git var -l" Do not use VISUAL editor on dumb terminals Handle more shell metacharacters in editor names
2 parents 7a4383c + a3d023d commit 376f39f

19 files changed

+178
-69
lines changed

Documentation/config.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,7 @@ core.editor::
387387
Commands such as `commit` and `tag` that lets you edit
388388
messages by launching an editor uses the value of this
389389
variable when it is set, and the environment variable
390-
`GIT_EDITOR` is not set. The order of preference is
391-
`GIT_EDITOR` environment, `core.editor`, `VISUAL` and
392-
`EDITOR` environment variables and then finally `vi`.
390+
`GIT_EDITOR` is not set. See linkgit:git-var[1].
393391

394392
core.pager::
395393
The command that git will use to paginate output. Can

Documentation/git-commit.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ ENVIRONMENT AND CONFIGURATION VARIABLES
323323
The editor used to edit the commit log message will be chosen from the
324324
GIT_EDITOR environment variable, the core.editor configuration variable, the
325325
VISUAL environment variable, or the EDITOR environment variable (in that
326-
order).
326+
order). See linkgit:git-var[1] for details.
327327

328328
HOOKS
329329
-----

Documentation/git-send-email.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ The --bcc option must be repeated for each user you want on the bcc list.
6060
The --cc option must be repeated for each user you want on the cc list.
6161

6262
--compose::
63-
Use $GIT_EDITOR, core.editor, $VISUAL, or $EDITOR to edit an
64-
introductory message for the patch series.
63+
Invoke a text editor (see GIT_EDITOR in linkgit:git-var[1])
64+
to edit an introductory message for the patch series.
6565
+
6666
When '--compose' is used, git send-email will use the From, Subject, and
6767
In-Reply-To headers specified in the message. If the body of the message

Documentation/git-var.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ GIT_AUTHOR_IDENT::
3636
GIT_COMMITTER_IDENT::
3737
The person who put a piece of code into git.
3838

39+
GIT_EDITOR::
40+
Text editor for use by git commands. The value is meant to be
41+
interpreted by the shell when it is used. Examples: `~/bin/vi`,
42+
`$SOME_ENVIRONMENT_VARIABLE`, `"C:\Program Files\Vim\gvim.exe"
43+
--nofork`. The order of preference is the `$GIT_EDITOR`
44+
environment variable, then `core.editor` configuration, then
45+
`$VISUAL`, then `$EDITOR`, and then finally 'vi'.
46+
47+
GIT_PAGER::
48+
Text viewer for use by git commands (e.g., 'less'). The value
49+
is meant to be interpreted by the shell. The order of preference
50+
is the `$GIT_PAGER` environment variable, then `core.pager`
51+
configuration, then `$PAGER`, and then finally 'less'.
52+
3953
Diagnostics
4054
-----------
4155
You don't exist. Go away!::

Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ all::
204204
# memory allocators with the nedmalloc allocator written by Niall Douglas.
205205
#
206206
# Define NO_REGEX if you have no or inferior regex support in your C library.
207+
#
208+
# Define DEFAULT_PAGER to a sensible pager command (defaults to "less") if
209+
# you want to use something different. The value will be interpreted by the
210+
# shell at runtime when it is used.
211+
#
212+
# Define DEFAULT_EDITOR to a sensible editor command (defaults to "vi") if you
213+
# want to use something different. The value will be interpreted by the shell
214+
# if necessary when it is used. Examples:
215+
#
216+
# DEFAULT_EDITOR='~/bin/vi',
217+
# DEFAULT_EDITOR='$GIT_FALLBACK_EDITOR',
218+
# DEFAULT_EDITOR='"C:\Program Files\Vim\gvim.exe" --nofork'
207219

208220
GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE
209221
@$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -1374,6 +1386,22 @@ BASIC_CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER_SQ)' \
13741386
$(COMPAT_CFLAGS)
13751387
LIB_OBJS += $(COMPAT_OBJS)
13761388

1389+
# Quote for C
1390+
1391+
ifdef DEFAULT_EDITOR
1392+
DEFAULT_EDITOR_CQ = "$(subst ",\",$(subst \,\\,$(DEFAULT_EDITOR)))"
1393+
DEFAULT_EDITOR_CQ_SQ = $(subst ','\'',$(DEFAULT_EDITOR_CQ))
1394+
1395+
BASIC_CFLAGS += -DDEFAULT_EDITOR='$(DEFAULT_EDITOR_CQ_SQ)'
1396+
endif
1397+
1398+
ifdef DEFAULT_PAGER
1399+
DEFAULT_PAGER_CQ = "$(subst ",\",$(subst \,\\,$(DEFAULT_PAGER)))"
1400+
DEFAULT_PAGER_CQ_SQ = $(subst ','\'',$(DEFAULT_PAGER_CQ))
1401+
1402+
BASIC_CFLAGS += -DDEFAULT_PAGER='$(DEFAULT_PAGER_CQ_SQ)'
1403+
endif
1404+
13771405
ALL_CFLAGS += $(BASIC_CFLAGS)
13781406
ALL_LDFLAGS += $(BASIC_LDFLAGS)
13791407

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,8 @@ extern const char *git_author_info(int);
751751
extern const char *git_committer_info(int);
752752
extern const char *fmt_ident(const char *name, const char *email, const char *date_str, int);
753753
extern const char *fmt_name(const char *name, const char *email);
754+
extern const char *git_editor(void);
755+
extern const char *git_pager(void);
754756

755757
struct checkout {
756758
const char *base_dir;

contrib/fast-import/git-p4

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -729,13 +729,10 @@ class P4Submit(Command):
729729
tmpFile.write(submitTemplate + separatorLine + diff + newdiff)
730730
tmpFile.close()
731731
mtime = os.stat(fileName).st_mtime
732-
defaultEditor = "vi"
733-
if platform.system() == "Windows":
734-
defaultEditor = "notepad"
735732
if os.environ.has_key("P4EDITOR"):
736733
editor = os.environ.get("P4EDITOR")
737734
else:
738-
editor = os.environ.get("EDITOR", defaultEditor);
735+
editor = read_pipe("git var GIT_EDITOR")
739736
system(editor + " " + fileName)
740737

741738
response = "y"

editor.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,38 @@
22
#include "strbuf.h"
33
#include "run-command.h"
44

5-
int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
5+
#ifndef DEFAULT_EDITOR
6+
#define DEFAULT_EDITOR "vi"
7+
#endif
8+
9+
const char *git_editor(void)
610
{
7-
const char *editor, *terminal;
11+
const char *editor = getenv("GIT_EDITOR");
12+
const char *terminal = getenv("TERM");
13+
int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb");
814

9-
editor = getenv("GIT_EDITOR");
1015
if (!editor && editor_program)
1116
editor = editor_program;
12-
if (!editor)
17+
if (!editor && !terminal_is_dumb)
1318
editor = getenv("VISUAL");
1419
if (!editor)
1520
editor = getenv("EDITOR");
1621

17-
terminal = getenv("TERM");
18-
if (!editor && (!terminal || !strcmp(terminal, "dumb")))
19-
return error("Terminal is dumb but no VISUAL nor EDITOR defined.");
22+
if (!editor && terminal_is_dumb)
23+
return NULL;
24+
25+
if (!editor)
26+
editor = DEFAULT_EDITOR;
27+
28+
return editor;
29+
}
30+
31+
int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
32+
{
33+
const char *editor = git_editor();
2034

2135
if (!editor)
22-
editor = "vi";
36+
return error("Terminal is dumb, but EDITOR unset");
2337

2438
if (strcmp(editor, ":")) {
2539
size_t len = strlen(editor);
@@ -28,7 +42,7 @@ int launch_editor(const char *path, struct strbuf *buffer, const char *const *en
2842
const char *args[6];
2943
struct strbuf arg0 = STRBUF_INIT;
3044

31-
if (strcspn(editor, "$ \t'") != len) {
45+
if (strcspn(editor, "|&;<>()$`\\\"' \t\n*?[#~=%") != len) {
3246
/* there are specials */
3347
strbuf_addf(&arg0, "%s \"$@\"", editor);
3448
args[i++] = "sh";

git-add--interactive.perl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,8 +990,7 @@ sub edit_hunk_manually {
990990
EOF
991991
close $fh;
992992

993-
my $editor = $ENV{GIT_EDITOR} || $repo->config("core.editor")
994-
|| $ENV{VISUAL} || $ENV{EDITOR} || "vi";
993+
chomp(my $editor = run_cmd_pipe(qw(git var GIT_EDITOR)));
995994
system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
996995

997996
if ($? != 0) {

git-am.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,10 @@ do
649649
[eE]*) git_editor "$dotest/final-commit"
650650
action=again ;;
651651
[vV]*) action=again
652-
LESS=-S ${PAGER:-less} "$dotest/patch" ;;
652+
: ${GIT_PAGER=$(git var GIT_PAGER)}
653+
: ${LESS=-FRSX}
654+
export LESS
655+
$GIT_PAGER "$dotest/patch" ;;
653656
*) action=again ;;
654657
esac
655658
done

0 commit comments

Comments
 (0)