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, ":")) { |
Ramkumar Ramachandra | 09c5ae5 | 2013-07-28 22:29:42 +0530 | [diff] [blame] | 40 | const char *args[] = { editor, real_path(path), NULL }; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 41 | struct child_process p = CHILD_PROCESS_INIT; |
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 | p.argv = args; |
| 45 | p.env = env; |
| 46 | p.use_shell = 1; |
| 47 | if (start_command(&p) < 0) |
| 48 | return error("unable to start editor '%s'", editor); |
| 49 | |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 50 | sigchain_push(SIGINT, SIG_IGN); |
| 51 | sigchain_push(SIGQUIT, SIG_IGN); |
| 52 | ret = finish_command(&p); |
Jeff King | 709ca73 | 2013-01-05 09:49:49 -0500 | [diff] [blame] | 53 | sig = ret - 128; |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 54 | sigchain_pop(SIGINT); |
| 55 | sigchain_pop(SIGQUIT); |
Jeff King | 1250857 | 2012-11-30 17:41:50 -0500 | [diff] [blame] | 56 | if (sig == SIGINT || sig == SIGQUIT) |
| 57 | raise(sig); |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 58 | if (ret) |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 59 | return error("There was a problem with the editor '%s'.", |
| 60 | editor); |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | if (!buffer) |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 64 | return 0; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 65 | if (strbuf_read_file(buffer, path, 0) < 0) |
Nguyễn Thái Ngọc Duy | 9f9a522 | 2016-05-08 16:47:43 +0700 | [diff] [blame] | 66 | return error_errno("could not read file '%s'", path); |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 67 | return 0; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 68 | } |