Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "strbuf.h" |
| 3 | #include "run-command.h" |
| 4 | |
Jonathan Nieder | 8f4b576 | 2009-10-30 20:44:41 -0500 | [diff] [blame] | 5 | #ifndef DEFAULT_EDITOR |
| 6 | #define DEFAULT_EDITOR "vi" |
| 7 | #endif |
| 8 | |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 9 | const char *git_editor(void) |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 10 | { |
Jonathan Nieder | d33738d | 2009-11-11 17:56:07 -0600 | [diff] [blame] | 11 | const char *editor = getenv("GIT_EDITOR"); |
| 12 | const char *terminal = getenv("TERM"); |
| 13 | int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb"); |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 14 | |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 15 | if (!editor && editor_program) |
| 16 | editor = editor_program; |
Jonathan Nieder | d33738d | 2009-11-11 17:56:07 -0600 | [diff] [blame] | 17 | if (!editor && !terminal_is_dumb) |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 18 | editor = getenv("VISUAL"); |
| 19 | if (!editor) |
| 20 | editor = getenv("EDITOR"); |
| 21 | |
Jonathan Nieder | d33738d | 2009-11-11 17:56:07 -0600 | [diff] [blame] | 22 | if (!editor && terminal_is_dumb) |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 23 | return NULL; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 24 | |
| 25 | if (!editor) |
Jonathan Nieder | 8f4b576 | 2009-10-30 20:44:41 -0500 | [diff] [blame] | 26 | editor = DEFAULT_EDITOR; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 27 | |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 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(); |
| 34 | |
| 35 | if (!editor) |
| 36 | return error("Terminal is dumb, but EDITOR unset"); |
| 37 | |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 38 | if (strcmp(editor, ":")) { |
Jeff King | bac8037 | 2009-12-30 05:56:16 -0500 | [diff] [blame] | 39 | const char *args[] = { editor, path, NULL }; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 40 | |
Jeff King | bac8037 | 2009-12-30 05:56:16 -0500 | [diff] [blame] | 41 | if (run_command_v_opt_cd_env(args, RUN_USING_SHELL, NULL, env)) |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 42 | return error("There was a problem with the editor '%s'.", |
| 43 | editor); |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | if (!buffer) |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 47 | return 0; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 48 | if (strbuf_read_file(buffer, path, 0) < 0) |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 49 | return error("could not read file '%s': %s", |
| 50 | path, strerror(errno)); |
| 51 | return 0; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 52 | } |