Skip to content

Commit 44fcb49

Browse files
jrngitster
authored andcommitted
Teach git var about GIT_EDITOR
Expose the command used by launch_editor() for scripts to use. This should allow one to avoid searching for a proper editor separately in each command. git_editor(void) uses the logic to decide which editor to use that used to live in launch_editor(). The function returns NULL if there is no suitable editor; the caller is expected to issue an error message when appropriate. launch_editor() uses git_editor() and gives the error message the same way as before when EDITOR is not set. "git var GIT_EDITOR" gives the editor name, or an error message when there is no appropriate one. "git var -l" gives GIT_EDITOR=name only if there is an appropriate editor. Originally-submitted-by: Johannes Sixt <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c27b392 commit 44fcb49

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

Documentation/git-var.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ 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+
3947
Diagnostics
4048
-----------
4149
You don't exist. Go away!::

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ extern const char *git_author_info(int);
750750
extern const char *git_committer_info(int);
751751
extern const char *fmt_ident(const char *name, const char *email, const char *date_str, int);
752752
extern const char *fmt_name(const char *name, const char *email);
753+
extern const char *git_editor(void);
753754

754755
struct checkout {
755756
const char *base_dir;

editor.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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+
const char *git_editor(void)
66
{
77
const char *editor = getenv("GIT_EDITOR");
88
const char *terminal = getenv("TERM");
@@ -16,11 +16,21 @@ int launch_editor(const char *path, struct strbuf *buffer, const char *const *en
1616
editor = getenv("EDITOR");
1717

1818
if (!editor && terminal_is_dumb)
19-
return error("terminal is dumb, but EDITOR unset");
19+
return NULL;
2020

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

24+
return editor;
25+
}
26+
27+
int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
28+
{
29+
const char *editor = git_editor();
30+
31+
if (!editor)
32+
return error("Terminal is dumb, but EDITOR unset");
33+
2434
if (strcmp(editor, ":")) {
2535
size_t len = strlen(editor);
2636
int i = 0;

var.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,35 @@
88

99
static const char var_usage[] = "git var [-l | <variable>]";
1010

11+
static const char *editor(int flag)
12+
{
13+
const char *pgm = git_editor();
14+
15+
if (!pgm && flag & IDENT_ERROR_ON_NO_NAME)
16+
die("Terminal is dumb, but EDITOR unset");
17+
18+
return pgm;
19+
}
20+
1121
struct git_var {
1222
const char *name;
1323
const char *(*read)(int);
1424
};
1525
static struct git_var git_vars[] = {
1626
{ "GIT_COMMITTER_IDENT", git_committer_info },
1727
{ "GIT_AUTHOR_IDENT", git_author_info },
28+
{ "GIT_EDITOR", editor },
1829
{ "", NULL },
1930
};
2031

2132
static void list_vars(void)
2233
{
2334
struct git_var *ptr;
35+
const char *val;
36+
2437
for (ptr = git_vars; ptr->read; ptr++)
25-
printf("%s=%s\n", ptr->name, ptr->read(0));
38+
if ((val = ptr->read(0)))
39+
printf("%s=%s\n", ptr->name, val);
2640
}
2741

2842
static const char *read_var(const char *var)

0 commit comments

Comments
 (0)