git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 4/9] Teach git var about GIT_EDITOR

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Nov 12, 2009, 00:01 UTC
Message-ID
<20091112000126.GE1140@progeny.tock>
In-Reply-To
<20091111235100.GA1140@progeny.tock>

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>
---
Changes from the version in pu:
 * unsquashed with the previous patch;
 * replaces Hannes’s sign-off with something more descriptive (see
<http://thread.gmane.org/gmane.comp.version-control.git/131471/focus=131851>);
 * nicer commit message based on Junio’s summary.
 Documentation/git-var.txt |    8 ++++++++
 cache.h                   |    1 +
 editor.c                  |   14 ++++++++++++--
 var.c                     |   16 +++++++++++++++-
 4 files changed, 36 insertions(+), 3 deletions(-)
diff --git a/Documentation/git-var.txt b/Documentation/git-var.txt
index e2f4c09..89e4b4f 100644
--- a/Documentation/git-var.txt
+++ b/Documentation/git-var.txt
@@ -36,6 +36,14 @@ GIT_AUTHOR_IDENT::
 GIT_COMMITTER_IDENT::
     The person who put a piece of code into git.
 
+GIT_EDITOR::
+    Text editor for use by git commands.  The value is meant to be
+    interpreted by the shell when it is used.  Examples: `~/bin/vi`,
+    `$SOME_ENVIRONMENT_VARIABLE`, `"C:\Program Files\Vim\gvim.exe"
+    --nofork`.  The order of preference is the `$GIT_EDITOR`
+    environment variable, then `core.editor` configuration, then
+    `$VISUAL`, then `$EDITOR`, and then finally 'vi'.
+
 Diagnostics
 -----------
 You don't exist. Go away!::
diff --git a/cache.h b/cache.h
index 96840c7..311cfe1 100644
--- a/cache.h
+++ b/cache.h
@@ -750,6 +750,7 @@ extern const char *git_author_info(int);
 extern const char *git_committer_info(int);
 extern const char *fmt_ident(const char *name, const char *email, const char *date_str, int);
 extern const char *fmt_name(const char *name, const char *email);
+extern const char *git_editor(void);
 
 struct checkout {
 	const char *base_dir;
diff --git a/editor.c b/editor.c
index 3f13751..70618f1 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;
diff --git a/var.c b/var.c
index dacbaab..b502487 100644
--- a/var.c
+++ b/var.c
@@ -8,6 +8,16 @@
 
 static const char var_usage[] = "git var [-l | <variable>]";
 
+static const char *editor(int flag)
+{
+	const char *pgm = git_editor();
+
+	if (!pgm && flag & IDENT_ERROR_ON_NO_NAME)
+		die("Terminal is dumb, but EDITOR unset");
+
+	return pgm;
+}
+
 struct git_var {
 	const char *name;
 	const char *(*read)(int);
@@ -15,14 +25,18 @@ struct git_var {
 static struct git_var git_vars[] = {
 	{ "GIT_COMMITTER_IDENT", git_committer_info },
 	{ "GIT_AUTHOR_IDENT",   git_author_info },
+	{ "GIT_EDITOR", editor },
 	{ "", NULL },
 };
 
 static void list_vars(void)
 {
 	struct git_var *ptr;
+	const char *val;
+
 	for (ptr = git_vars; ptr->read; ptr++)
-		printf("%s=%s\n", ptr->name, ptr->read(0));
+		if ((val = ptr->read(0)))
+			printf("%s=%s\n", ptr->name, val);
 }
 
 static const char *read_var(const char *var)
-- 
1.6.5.2
Previous: Jonathan NiederNext: Jonathan Nieder
Message 57 of 65 in “packaging vs default pager”
  1. Ben WaltonOct 28, 2009
  2. Junio C HamanoOct 28, 2009
  3. 0/2 Re: packaging vs default pagerJonathan Nieder, Oct 29, 2009
  4. 1/2 Provide a build time default-pager settingJonathan Nieder, Oct 29, 2009
  5. 2/2 Provide a build time default-editor settingJonathan Nieder, Oct 29, 2009
  6. David RoundyOct 29, 2009
  7. Johannes SixtOct 29, 2009
  8. Junio C HamanoOct 29, 2009
  9. Johannes SixtOct 29, 2009
  10. Junio C HamanoOct 29, 2009
  11. David RoundyOct 30, 2009
  12. Junio C HamanoOct 29, 2009
  13. 0/8 Default pager and editorJonathan Nieder, Oct 30, 2009
  14. 1/8 launch_editor: Longer error message when TERM=dumbJonathan Nieder, Oct 30, 2009
  15. 2/8 Handle more shell metacharacters in editor namesJonathan Nieder, Oct 30, 2009
  16. 3/8 Teach git var about GIT_EDITORJonathan Nieder, Oct 30, 2009
  17. Johannes SixtOct 30, 2009
  18. Jonathan NiederOct 30, 2009
  19. Junio C HamanoOct 30, 2009
  20. Jonathan NiederOct 31, 2009
  21. 4/8 Teach git var about GIT_PAGERJonathan Nieder, Oct 30, 2009
  22. 5/8 add -i, send-email, svn, p4, etc: use "git var GIT_EDITOR"Jonathan Nieder, Oct 30, 2009
  23. 6/8 am -i, git-svn: use "git var GIT_PAGER"Jonathan Nieder, Oct 30, 2009
  24. 7/8 Provide a build time default-editor settingJonathan Nieder, Oct 30, 2009
  25. Jonathan NiederOct 30, 2009
  26. 8/8 Provide a build time default-pager settingJonathan Nieder, Oct 30, 2009
  27. Junio C HamanoOct 30, 2009
  28. 9/8 Teach git var to run the editorJonathan Nieder, Oct 30, 2009
  29. 0/8 Default pager and editorJonathan Nieder, Oct 31, 2009
  30. 1/8 Handle more shell metacharacters in editor namesJonathan Nieder, Oct 31, 2009
  31. 2/8 Do not use VISUAL editor on dumb terminalsJonathan Nieder, Oct 31, 2009
  32. 2/8 Do not use VISUAL editor on dumb terminalsJonathan Nieder, Oct 31, 2009
  33. 3/8 Teach git var about GIT_EDITORJonathan Nieder, Oct 31, 2009
  34. Junio C HamanoOct 31, 2009
  35. Jonathan NiederOct 31, 2009
  36. Junio C HamanoOct 31, 2009
  37. Jonathan NiederOct 31, 2009
  38. Teach git var about GIT_EDITORJonathan Nieder, Oct 31, 2009
  39. Jonathan NiederOct 31, 2009
  40. Teach git var about GIT_EDITORJonathan Nieder, Oct 31, 2009
  41. Junio C HamanoNov 1, 2009
  42. Johannes SixtOct 31, 2009
  43. 4/8 Teach git var about GIT_PAGERJonathan Nieder, Oct 31, 2009
  44. 5/8 add -i, send-email, svn, p4, etc: use "git var GIT_EDITOR"Jonathan Nieder, Oct 31, 2009
  45. 6/8 am -i, git-svn: use "git var GIT_PAGER"Jonathan Nieder, Oct 31, 2009
  46. 7/8 Provide a build time default-editor settingJonathan Nieder, Oct 31, 2009
  47. Junio C HamanoOct 31, 2009
  48. Jonathan NiederOct 31, 2009
  49. Junio C HamanoOct 31, 2009
  50. Jonathan NiederOct 31, 2009
  51. Junio C HamanoNov 1, 2009
  52. 8/8 Provide a build time default-pager settingJonathan Nieder, Oct 31, 2009
  53. 0/9 Default pager and editorJonathan Nieder, Nov 11, 2009
  54. 1/9 Handle more shell metacharacters in editor namesJonathan Nieder, Nov 11, 2009
  55. 2/9 Do not use VISUAL editor on dumb terminalsJonathan Nieder, Nov 11, 2009
  56. 3/9 Suppress warnings from "git var -l"Jonathan Nieder, Nov 11, 2009
  57. 4/9 Teach git var about GIT_EDITORJonathan Nieder, Nov 12, 2009
  58. 5/9 Teach git var about GIT_PAGERJonathan Nieder, Nov 12, 2009
  59. 6/9 add -i, send-email, svn, p4, etc: use "git var GIT_EDITOR"Jonathan Nieder, Nov 12, 2009
  60. 7/9 am -i, git-svn: use "git var GIT_PAGER"Jonathan Nieder, Nov 12, 2009
  61. 8/9 Provide a build time default-editor settingJonathan Nieder, Nov 12, 2009
  62. 9/9 Provide a build time default-pager settingJonathan Nieder, Nov 12, 2009
  63. Junio C HamanoNov 15, 2009
  64. 0/2 Default Pager and Editor at build-timeBen Walton, Oct 29, 2009
  65. 1/2 Provide a build time default-pager settingBen Walton, Oct 29, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.