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" |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 4 | #include "sigchain.h" |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 5 | |
Jonathan Nieder | 8f4b576 | 2009-10-30 20:44:41 -0500 | [diff] [blame] | 6 | #ifndef DEFAULT_EDITOR |
| 7 | #define DEFAULT_EDITOR "vi" |
| 8 | #endif |
| 9 | |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 10 | const char *git_editor(void) |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 11 | { |
Jonathan Nieder | d33738d | 2009-11-11 17:56:07 -0600 | [diff] [blame] | 12 | const char *editor = getenv("GIT_EDITOR"); |
| 13 | const char *terminal = getenv("TERM"); |
| 14 | int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb"); |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 15 | |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 16 | if (!editor && editor_program) |
| 17 | editor = editor_program; |
Jonathan Nieder | d33738d | 2009-11-11 17:56:07 -0600 | [diff] [blame] | 18 | if (!editor && !terminal_is_dumb) |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 19 | editor = getenv("VISUAL"); |
| 20 | if (!editor) |
| 21 | editor = getenv("EDITOR"); |
| 22 | |
Jonathan Nieder | d33738d | 2009-11-11 17:56:07 -0600 | [diff] [blame] | 23 | if (!editor && terminal_is_dumb) |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 24 | return NULL; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 25 | |
| 26 | if (!editor) |
Jonathan Nieder | 8f4b576 | 2009-10-30 20:44:41 -0500 | [diff] [blame] | 27 | editor = DEFAULT_EDITOR; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 28 | |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 29 | return editor; |
| 30 | } |
| 31 | |
| 32 | int launch_editor(const char *path, struct strbuf *buffer, const char *const *env) |
| 33 | { |
| 34 | const char *editor = git_editor(); |
| 35 | |
| 36 | if (!editor) |
| 37 | return error("Terminal is dumb, but EDITOR unset"); |
| 38 | |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 39 | if (strcmp(editor, ":")) { |
Jeff King | bac8037 | 2009-12-30 05:56:16 -0500 | [diff] [blame] | 40 | const char *args[] = { editor, path, NULL }; |
Jeff King | f42ca31 | 2012-11-30 17:41:04 -0500 | [diff] [blame] | 41 | struct child_process p; |
Jeff King | 1250857 | 2012-11-30 17:41:50 -0500 | [diff] [blame] | 42 | int ret, sig; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 43 | |
Jeff King | f42ca31 | 2012-11-30 17:41:04 -0500 | [diff] [blame] | 44 | memset(&p, 0, sizeof(p)); |
| 45 | p.argv = args; |
| 46 | p.env = env; |
| 47 | p.use_shell = 1; |
| 48 | if (start_command(&p) < 0) |
| 49 | return error("unable to start editor '%s'", editor); |
| 50 | |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 51 | sigchain_push(SIGINT, SIG_IGN); |
| 52 | sigchain_push(SIGQUIT, SIG_IGN); |
| 53 | ret = finish_command(&p); |
Jeff King | 709ca73 | 2013-01-05 09:49:49 -0500 | [diff] [blame] | 54 | sig = ret - 128; |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 55 | sigchain_pop(SIGINT); |
| 56 | sigchain_pop(SIGQUIT); |
Jeff King | 1250857 | 2012-11-30 17:41:50 -0500 | [diff] [blame] | 57 | if (sig == SIGINT || sig == SIGQUIT) |
| 58 | raise(sig); |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 59 | if (ret) |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 60 | return error("There was a problem with the editor '%s'.", |
| 61 | editor); |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if (!buffer) |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 65 | return 0; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 66 | if (strbuf_read_file(buffer, path, 0) < 0) |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 67 | return error("could not read file '%s': %s", |
| 68 | path, strerror(errno)); |
| 69 | return 0; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 70 | } |