@matheusbsbs / matheus-bagliano / commits / 44fcb4977c
Fork 0

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 <j6t@kdbg.org> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Nov 11, 2009 at 18:01 UTC 44fcb4977cbae67f4698306ccfe982420ceebcbf
4 files changed +36 -3
Documentation/git-var.txt
+8
@@ -36,6 +36,14 @@ GIT_AUTHOR_IDENT::
36 GIT_COMMITTER_IDENT::
37 The person who put a piece of code into git.
38
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 Diagnostics
48 -----------
49 You don't exist. Go away!::
cache.h
+1
@@ -750,6 +750,7 @@ extern const char *git_author_info(int);
750 extern const char *git_committer_info(int);
751 extern const char *fmt_ident(const char *name, const char *email, const char *date_str, int);
752 extern const char *fmt_name(const char *name, const char *email);
753 +extern const char *git_editor(void);
754
755 struct checkout {
756 const char *base_dir;
editor.c
+12 -2
@@ -2,7 +2,7 @@
2 #include "strbuf.h"
3 #include "run-command.h"
4
5 -int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
5 +const char *git_editor(void)
6 {
7 const char *editor = getenv("GIT_EDITOR");
8 const char *terminal = getenv("TERM");
@@ -16,11 +16,21 @@ int launch_editor(const char *path, struct strbuf *buffer, const char *const *en
16 editor = getenv("EDITOR");
17
18 if (!editor && terminal_is_dumb)
19 - return error("terminal is dumb, but EDITOR unset");
19 + return NULL;
20
21 if (!editor)
22 editor = "vi";
23
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 +
34 if (strcmp(editor, ":")) {
35 size_t len = strlen(editor);
36 int i = 0;
var.c
+15 -1
@@ -8,6 +8,16 @@
8
9 static const char var_usage[] = "git var [-l | <variable>]";
10
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 +
21 struct git_var {
22 const char *name;
23 const char *(*read)(int);
@@ -15,14 +25,18 @@ struct git_var {
25 static struct git_var git_vars[] = {
26 { "GIT_COMMITTER_IDENT", git_committer_info },
27 { "GIT_AUTHOR_IDENT", git_author_info },
28 + { "GIT_EDITOR", editor },
29 { "", NULL },
30 };
31
32 static void list_vars(void)
33 {
34 struct git_var *ptr;
35 + const char *val;
36 +
37 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);
40 }
41
42 static const char *read_var(const char *var)