Elijah Newren | 77f091e | 2023-04-11 00:42:00 -0700 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Elijah Newren | 0b027f6 | 2023-03-21 06:25:58 +0000 | [diff] [blame] | 2 | #include "abspath.h" |
Elijah Newren | 6c6ddf9 | 2023-04-11 03:00:39 +0000 | [diff] [blame] | 3 | #include "advice.h" |
Alban Gruin | 2aed018 | 2018-08-10 18:51:30 +0200 | [diff] [blame] | 4 | #include "config.h" |
Elijah Newren | 4e12082 | 2023-04-11 00:41:57 -0700 | [diff] [blame] | 5 | #include "editor.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 6 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 7 | #include "gettext.h" |
Elijah Newren | ca4eed7 | 2023-04-11 00:41:59 -0700 | [diff] [blame] | 8 | #include "pager.h" |
Elijah Newren | d1cbe1e | 2023-04-22 20:17:20 +0000 | [diff] [blame] | 9 | #include "path.h" |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 10 | #include "strbuf.h" |
Ævar Arnfjörð Bjarmason | c7c4bde | 2021-11-25 23:52:24 +0100 | [diff] [blame] | 11 | #include "strvec.h" |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 12 | #include "run-command.h" |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 13 | #include "sigchain.h" |
Elijah Newren | 4e12082 | 2023-04-11 00:41:57 -0700 | [diff] [blame] | 14 | #include "wrapper.h" |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 15 | |
Jonathan Nieder | 8f4b576 | 2009-10-30 20:44:41 -0500 | [diff] [blame] | 16 | #ifndef DEFAULT_EDITOR |
| 17 | #define DEFAULT_EDITOR "vi" |
| 18 | #endif |
| 19 | |
Lars Schneider | a64f213 | 2017-11-29 15:37:51 +0100 | [diff] [blame] | 20 | int is_terminal_dumb(void) |
| 21 | { |
| 22 | const char *terminal = getenv("TERM"); |
| 23 | return !terminal || !strcmp(terminal, "dumb"); |
| 24 | } |
| 25 | |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 26 | const char *git_editor(void) |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 27 | { |
Jonathan Nieder | d33738d | 2009-11-11 17:56:07 -0600 | [diff] [blame] | 28 | const char *editor = getenv("GIT_EDITOR"); |
Lars Schneider | a64f213 | 2017-11-29 15:37:51 +0100 | [diff] [blame] | 29 | int terminal_is_dumb = is_terminal_dumb(); |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 30 | |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 31 | if (!editor && editor_program) |
| 32 | editor = editor_program; |
Jonathan Nieder | d33738d | 2009-11-11 17:56:07 -0600 | [diff] [blame] | 33 | if (!editor && !terminal_is_dumb) |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 34 | editor = getenv("VISUAL"); |
| 35 | if (!editor) |
| 36 | editor = getenv("EDITOR"); |
| 37 | |
Jonathan Nieder | d33738d | 2009-11-11 17:56:07 -0600 | [diff] [blame] | 38 | if (!editor && terminal_is_dumb) |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 39 | return NULL; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 40 | |
| 41 | if (!editor) |
Jonathan Nieder | 8f4b576 | 2009-10-30 20:44:41 -0500 | [diff] [blame] | 42 | editor = DEFAULT_EDITOR; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 43 | |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 44 | return editor; |
| 45 | } |
| 46 | |
Alban Gruin | 2aed018 | 2018-08-10 18:51:30 +0200 | [diff] [blame] | 47 | const char *git_sequence_editor(void) |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 48 | { |
Alban Gruin | 2aed018 | 2018-08-10 18:51:30 +0200 | [diff] [blame] | 49 | const char *editor = getenv("GIT_SEQUENCE_EDITOR"); |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 50 | |
| 51 | if (!editor) |
Jeff King | f1de981 | 2020-08-14 12:17:36 -0400 | [diff] [blame] | 52 | git_config_get_string_tmp("sequence.editor", &editor); |
Alban Gruin | 2aed018 | 2018-08-10 18:51:30 +0200 | [diff] [blame] | 53 | if (!editor) |
| 54 | editor = git_editor(); |
| 55 | |
| 56 | return editor; |
| 57 | } |
| 58 | |
| 59 | static int launch_specified_editor(const char *editor, const char *path, |
| 60 | struct strbuf *buffer, const char *const *env) |
| 61 | { |
| 62 | if (!editor) |
Jonathan Nieder | 44fcb49 | 2009-11-11 18:01:27 -0600 | [diff] [blame] | 63 | return error("Terminal is dumb, but EDITOR unset"); |
| 64 | |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 65 | if (strcmp(editor, ":")) { |
Alexandr Miloslavskiy | 3d7747e | 2020-03-10 13:11:22 +0000 | [diff] [blame] | 66 | struct strbuf realpath = STRBUF_INIT; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 67 | struct child_process p = CHILD_PROCESS_INIT; |
Jeff King | 1250857 | 2012-11-30 17:41:50 -0500 | [diff] [blame] | 68 | int ret, sig; |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 69 | int print_waiting_for_editor = advice_enabled(ADVICE_WAITING_FOR_EDITOR) && isatty(2); |
Lars Schneider | abfb04d | 2017-12-07 16:16:41 +0100 | [diff] [blame] | 70 | |
| 71 | if (print_waiting_for_editor) { |
| 72 | /* |
| 73 | * A dumb terminal cannot erase the line later on. Add a |
| 74 | * newline to separate the hint from subsequent output. |
| 75 | * |
| 76 | * Make sure that our message is separated with a whitespace |
| 77 | * from further cruft that may be written by the editor. |
| 78 | */ |
| 79 | const char term = is_terminal_dumb() ? '\n' : ' '; |
| 80 | |
| 81 | fprintf(stderr, |
| 82 | _("hint: Waiting for your editor to close the file...%c"), |
| 83 | term); |
| 84 | fflush(stderr); |
| 85 | } |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 86 | |
Alexandr Miloslavskiy | 3d7747e | 2020-03-10 13:11:22 +0000 | [diff] [blame] | 87 | strbuf_realpath(&realpath, path, 1); |
Alexandr Miloslavskiy | 3d7747e | 2020-03-10 13:11:22 +0000 | [diff] [blame] | 88 | |
Ævar Arnfjörð Bjarmason | 2b70989 | 2021-11-25 23:52:20 +0100 | [diff] [blame] | 89 | strvec_pushl(&p.args, editor, realpath.buf, NULL); |
Ævar Arnfjörð Bjarmason | c7c4bde | 2021-11-25 23:52:24 +0100 | [diff] [blame] | 90 | if (env) |
Ævar Arnfjörð Bjarmason | 29fda24 | 2022-06-02 11:09:50 +0200 | [diff] [blame] | 91 | strvec_pushv(&p.env, (const char **)env); |
Jeff King | f42ca31 | 2012-11-30 17:41:04 -0500 | [diff] [blame] | 92 | p.use_shell = 1; |
Jeff Hostetler | eee73d1 | 2019-02-22 14:25:04 -0800 | [diff] [blame] | 93 | p.trace2_child_class = "editor"; |
Alexandr Miloslavskiy | 3d7747e | 2020-03-10 13:11:22 +0000 | [diff] [blame] | 94 | if (start_command(&p) < 0) { |
| 95 | strbuf_release(&realpath); |
Jeff King | f42ca31 | 2012-11-30 17:41:04 -0500 | [diff] [blame] | 96 | return error("unable to start editor '%s'", editor); |
Alexandr Miloslavskiy | 3d7747e | 2020-03-10 13:11:22 +0000 | [diff] [blame] | 97 | } |
Jeff King | f42ca31 | 2012-11-30 17:41:04 -0500 | [diff] [blame] | 98 | |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 99 | sigchain_push(SIGINT, SIG_IGN); |
| 100 | sigchain_push(SIGQUIT, SIG_IGN); |
| 101 | ret = finish_command(&p); |
Alexandr Miloslavskiy | 3d7747e | 2020-03-10 13:11:22 +0000 | [diff] [blame] | 102 | strbuf_release(&realpath); |
Jeff King | 709ca73 | 2013-01-05 09:49:49 -0500 | [diff] [blame] | 103 | sig = ret - 128; |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 104 | sigchain_pop(SIGINT); |
| 105 | sigchain_pop(SIGQUIT); |
Jeff King | 1250857 | 2012-11-30 17:41:50 -0500 | [diff] [blame] | 106 | if (sig == SIGINT || sig == SIGQUIT) |
| 107 | raise(sig); |
Paul Fox | 913ef36 | 2012-11-30 17:41:26 -0500 | [diff] [blame] | 108 | if (ret) |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 109 | return error("There was a problem with the editor '%s'.", |
| 110 | editor); |
Lars Schneider | abfb04d | 2017-12-07 16:16:41 +0100 | [diff] [blame] | 111 | |
| 112 | if (print_waiting_for_editor && !is_terminal_dumb()) |
| 113 | /* |
SZEDER Gábor | cd1096b | 2019-06-24 20:13:16 +0200 | [diff] [blame] | 114 | * Erase the entire line to avoid wasting the |
| 115 | * vertical space. |
Lars Schneider | abfb04d | 2017-12-07 16:16:41 +0100 | [diff] [blame] | 116 | */ |
SZEDER Gábor | cd1096b | 2019-06-24 20:13:16 +0200 | [diff] [blame] | 117 | term_clear_line(); |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | if (!buffer) |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 121 | return 0; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 122 | 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] | 123 | return error_errno("could not read file '%s'", path); |
Stephan Beyer | 7198203 | 2008-07-25 18:28:42 +0200 | [diff] [blame] | 124 | return 0; |
Stephan Beyer | d82f33e | 2008-07-25 18:28:41 +0200 | [diff] [blame] | 125 | } |
Alban Gruin | 2aed018 | 2018-08-10 18:51:30 +0200 | [diff] [blame] | 126 | |
| 127 | int launch_editor(const char *path, struct strbuf *buffer, const char *const *env) |
| 128 | { |
| 129 | return launch_specified_editor(git_editor(), path, buffer, env); |
| 130 | } |
| 131 | |
| 132 | int launch_sequence_editor(const char *path, struct strbuf *buffer, |
| 133 | const char *const *env) |
| 134 | { |
| 135 | return launch_specified_editor(git_sequence_editor(), path, buffer, env); |
| 136 | } |
Elijah Newren | 4e12082 | 2023-04-11 00:41:57 -0700 | [diff] [blame] | 137 | |
| 138 | int strbuf_edit_interactively(struct strbuf *buffer, const char *path, |
| 139 | const char *const *env) |
| 140 | { |
| 141 | char *path2 = NULL; |
| 142 | int fd, res = 0; |
| 143 | |
| 144 | if (!is_absolute_path(path)) |
| 145 | path = path2 = xstrdup(git_path("%s", path)); |
| 146 | |
| 147 | fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 148 | if (fd < 0) |
| 149 | res = error_errno(_("could not open '%s' for writing"), path); |
| 150 | else if (write_in_full(fd, buffer->buf, buffer->len) < 0) { |
| 151 | res = error_errno(_("could not write to '%s'"), path); |
| 152 | close(fd); |
| 153 | } else if (close(fd) < 0) |
| 154 | res = error_errno(_("could not close '%s'"), path); |
| 155 | else { |
| 156 | strbuf_reset(buffer); |
| 157 | if (launch_editor(path, buffer, env) < 0) |
| 158 | res = error_errno(_("could not edit '%s'"), path); |
| 159 | unlink(path); |
| 160 | } |
| 161 | |
| 162 | free(path2); |
| 163 | return res; |
| 164 | } |