From eab58f1e8e5ef86b5075ce6dfcd6d3f1b3b888b3 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Fri, 30 Oct 2009 20:24:04 -0500 Subject: Handle more shell metacharacters in editor names Pass the editor name to the shell if it contains any susv3 shell special character (globs, redirections, variable substitutions, escapes, etc). This way, the meaning of some characters will not meaninglessly change when others are added, and git commands implemented in C and in shell scripts will interpret editor names in the same way. This does not make the GIT_EDITOR setting any more expressive, since one could always use single quotes to force the editor to be passed to the shell. Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- editor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'editor.c') diff --git a/editor.c b/editor.c index 4d469d076..941c0b2ab 100644 --- a/editor.c +++ b/editor.c @@ -28,7 +28,7 @@ int launch_editor(const char *path, struct strbuf *buffer, const char *const *en const char *args[6]; struct strbuf arg0 = STRBUF_INIT; - if (strcspn(editor, "$ \t'") != len) { + if (strcspn(editor, "|&;<>()$`\\\"' \t\n*?[#~=%") != len) { /* there are specials */ strbuf_addf(&arg0, "%s \"$@\"", editor); args[i++] = "sh"; -- cgit v1.2.1 From d33738d7d3ed42a956c74d0125eb2b3abda451b7 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Wed, 11 Nov 2009 17:56:07 -0600 Subject: Do not use VISUAL editor on dumb terminals Refuse to use $VISUAL and fall back to $EDITOR if TERM is unset or set to "dumb". Traditionally, VISUAL is set to a screen editor and EDITOR to a line-based editor, which should be more useful in that situation. vim, for example, is happy to assume a terminal supports ANSI sequences even if TERM is dumb (e.g., when running from a text editor like Acme). git already refuses to fall back to vi on a dumb terminal if GIT_EDITOR, core.editor, VISUAL, and EDITOR are unset, but without this patch, that check is suppressed by VISUAL=vi. Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- editor.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'editor.c') diff --git a/editor.c b/editor.c index 941c0b2ab..3f1375114 100644 --- a/editor.c +++ b/editor.c @@ -4,19 +4,19 @@ int launch_editor(const char *path, struct strbuf *buffer, const char *const *env) { - const char *editor, *terminal; + const char *editor = getenv("GIT_EDITOR"); + const char *terminal = getenv("TERM"); + int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb"); - editor = getenv("GIT_EDITOR"); if (!editor && editor_program) editor = editor_program; - if (!editor) + if (!editor && !terminal_is_dumb) editor = getenv("VISUAL"); if (!editor) editor = getenv("EDITOR"); - terminal = getenv("TERM"); - if (!editor && (!terminal || !strcmp(terminal, "dumb"))) - return error("Terminal is dumb but no VISUAL nor EDITOR defined."); + if (!editor && terminal_is_dumb) + return error("terminal is dumb, but EDITOR unset"); if (!editor) editor = "vi"; -- cgit v1.2.1 From 44fcb4977cbae67f4698306ccfe982420ceebcbf Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Wed, 11 Nov 2009 18:01:27 -0600 Subject: 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 Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- editor.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'editor.c') diff --git a/editor.c b/editor.c index 3f1375114..70618f106 100644 --- a/editor.c +++ b/editor.c @@ -2,7 +2,7 @@ #include "strbuf.h" #include "run-command.h" -int launch_editor(const char *path, struct strbuf *buffer, const char *const *env) +const char *git_editor(void) { const char *editor = getenv("GIT_EDITOR"); const char *terminal = getenv("TERM"); @@ -16,11 +16,21 @@ int launch_editor(const char *path, struct strbuf *buffer, const char *const *en editor = getenv("EDITOR"); if (!editor && terminal_is_dumb) - return error("terminal is dumb, but EDITOR unset"); + return NULL; if (!editor) editor = "vi"; + return editor; +} + +int launch_editor(const char *path, struct strbuf *buffer, const char *const *env) +{ + const char *editor = git_editor(); + + if (!editor) + return error("Terminal is dumb, but EDITOR unset"); + if (strcmp(editor, ":")) { size_t len = strlen(editor); int i = 0; -- cgit v1.2.1 From 8f4b576ad14943a7b14cb8937eb321b7bfd91ee7 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Fri, 30 Oct 2009 20:44:41 -0500 Subject: Provide a build time default-editor setting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Ben Walton Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- editor.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'editor.c') diff --git a/editor.c b/editor.c index 70618f106..615f5754d 100644 --- a/editor.c +++ b/editor.c @@ -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"); @@ -19,7 +23,7 @@ const char *git_editor(void) return NULL; if (!editor) - editor = "vi"; + editor = DEFAULT_EDITOR; return editor; } -- cgit v1.2.1