blob: 8b9648281d7b53ad34a8dffca32ae2940b3a81c5 [file] [log] [blame]
Stephan Beyerd82f33e2008-07-25 18:28:41 +02001#include "cache.h"
Alban Gruin2aed0182018-08-10 18:51:30 +02002#include "config.h"
Stephan Beyerd82f33e2008-07-25 18:28:41 +02003#include "strbuf.h"
Ævar Arnfjörð Bjarmasonc7c4bde2021-11-25 23:52:24 +01004#include "strvec.h"
Stephan Beyerd82f33e2008-07-25 18:28:41 +02005#include "run-command.h"
Paul Fox913ef362012-11-30 17:41:26 -05006#include "sigchain.h"
Stephan Beyerd82f33e2008-07-25 18:28:41 +02007
Jonathan Nieder8f4b5762009-10-30 20:44:41 -05008#ifndef DEFAULT_EDITOR
9#define DEFAULT_EDITOR "vi"
10#endif
11
Lars Schneidera64f2132017-11-29 15:37:51 +010012int is_terminal_dumb(void)
13{
14 const char *terminal = getenv("TERM");
15 return !terminal || !strcmp(terminal, "dumb");
16}
17
Jonathan Nieder44fcb492009-11-11 18:01:27 -060018const char *git_editor(void)
Stephan Beyerd82f33e2008-07-25 18:28:41 +020019{
Jonathan Niederd33738d2009-11-11 17:56:07 -060020 const char *editor = getenv("GIT_EDITOR");
Lars Schneidera64f2132017-11-29 15:37:51 +010021 int terminal_is_dumb = is_terminal_dumb();
Stephan Beyerd82f33e2008-07-25 18:28:41 +020022
Stephan Beyerd82f33e2008-07-25 18:28:41 +020023 if (!editor && editor_program)
24 editor = editor_program;
Jonathan Niederd33738d2009-11-11 17:56:07 -060025 if (!editor && !terminal_is_dumb)
Stephan Beyerd82f33e2008-07-25 18:28:41 +020026 editor = getenv("VISUAL");
27 if (!editor)
28 editor = getenv("EDITOR");
29
Jonathan Niederd33738d2009-11-11 17:56:07 -060030 if (!editor && terminal_is_dumb)
Jonathan Nieder44fcb492009-11-11 18:01:27 -060031 return NULL;
Stephan Beyerd82f33e2008-07-25 18:28:41 +020032
33 if (!editor)
Jonathan Nieder8f4b5762009-10-30 20:44:41 -050034 editor = DEFAULT_EDITOR;
Stephan Beyerd82f33e2008-07-25 18:28:41 +020035
Jonathan Nieder44fcb492009-11-11 18:01:27 -060036 return editor;
37}
38
Alban Gruin2aed0182018-08-10 18:51:30 +020039const char *git_sequence_editor(void)
Jonathan Nieder44fcb492009-11-11 18:01:27 -060040{
Alban Gruin2aed0182018-08-10 18:51:30 +020041 const char *editor = getenv("GIT_SEQUENCE_EDITOR");
Jonathan Nieder44fcb492009-11-11 18:01:27 -060042
43 if (!editor)
Jeff Kingf1de9812020-08-14 12:17:36 -040044 git_config_get_string_tmp("sequence.editor", &editor);
Alban Gruin2aed0182018-08-10 18:51:30 +020045 if (!editor)
46 editor = git_editor();
47
48 return editor;
49}
50
51static int launch_specified_editor(const char *editor, const char *path,
52 struct strbuf *buffer, const char *const *env)
53{
54 if (!editor)
Jonathan Nieder44fcb492009-11-11 18:01:27 -060055 return error("Terminal is dumb, but EDITOR unset");
56
Stephan Beyerd82f33e2008-07-25 18:28:41 +020057 if (strcmp(editor, ":")) {
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +000058 struct strbuf realpath = STRBUF_INIT;
René Scharfed3180272014-08-19 21:09:35 +020059 struct child_process p = CHILD_PROCESS_INIT;
Jeff King12508572012-11-30 17:41:50 -050060 int ret, sig;
Ben Boeckeled9bff02021-08-23 12:44:00 +020061 int print_waiting_for_editor = advice_enabled(ADVICE_WAITING_FOR_EDITOR) && isatty(2);
Lars Schneiderabfb04d2017-12-07 16:16:41 +010062
63 if (print_waiting_for_editor) {
64 /*
65 * A dumb terminal cannot erase the line later on. Add a
66 * newline to separate the hint from subsequent output.
67 *
68 * Make sure that our message is separated with a whitespace
69 * from further cruft that may be written by the editor.
70 */
71 const char term = is_terminal_dumb() ? '\n' : ' ';
72
73 fprintf(stderr,
74 _("hint: Waiting for your editor to close the file...%c"),
75 term);
76 fflush(stderr);
77 }
Stephan Beyerd82f33e2008-07-25 18:28:41 +020078
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +000079 strbuf_realpath(&realpath, path, 1);
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +000080
Ævar Arnfjörð Bjarmason2b709892021-11-25 23:52:20 +010081 strvec_pushl(&p.args, editor, realpath.buf, NULL);
Ævar Arnfjörð Bjarmasonc7c4bde2021-11-25 23:52:24 +010082 if (env)
83 strvec_pushv(&p.env_array, (const char **)env);
Jeff Kingf42ca312012-11-30 17:41:04 -050084 p.use_shell = 1;
Jeff Hostetlereee73d12019-02-22 14:25:04 -080085 p.trace2_child_class = "editor";
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +000086 if (start_command(&p) < 0) {
87 strbuf_release(&realpath);
Jeff Kingf42ca312012-11-30 17:41:04 -050088 return error("unable to start editor '%s'", editor);
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +000089 }
Jeff Kingf42ca312012-11-30 17:41:04 -050090
Paul Fox913ef362012-11-30 17:41:26 -050091 sigchain_push(SIGINT, SIG_IGN);
92 sigchain_push(SIGQUIT, SIG_IGN);
93 ret = finish_command(&p);
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +000094 strbuf_release(&realpath);
Jeff King709ca732013-01-05 09:49:49 -050095 sig = ret - 128;
Paul Fox913ef362012-11-30 17:41:26 -050096 sigchain_pop(SIGINT);
97 sigchain_pop(SIGQUIT);
Jeff King12508572012-11-30 17:41:50 -050098 if (sig == SIGINT || sig == SIGQUIT)
99 raise(sig);
Paul Fox913ef362012-11-30 17:41:26 -0500100 if (ret)
Stephan Beyer71982032008-07-25 18:28:42 +0200101 return error("There was a problem with the editor '%s'.",
102 editor);
Lars Schneiderabfb04d2017-12-07 16:16:41 +0100103
104 if (print_waiting_for_editor && !is_terminal_dumb())
105 /*
SZEDER Gáborcd1096b2019-06-24 20:13:16 +0200106 * Erase the entire line to avoid wasting the
107 * vertical space.
Lars Schneiderabfb04d2017-12-07 16:16:41 +0100108 */
SZEDER Gáborcd1096b2019-06-24 20:13:16 +0200109 term_clear_line();
Stephan Beyerd82f33e2008-07-25 18:28:41 +0200110 }
111
112 if (!buffer)
Stephan Beyer71982032008-07-25 18:28:42 +0200113 return 0;
Stephan Beyerd82f33e2008-07-25 18:28:41 +0200114 if (strbuf_read_file(buffer, path, 0) < 0)
Nguyễn Thái Ngọc Duy9f9a5222016-05-08 16:47:43 +0700115 return error_errno("could not read file '%s'", path);
Stephan Beyer71982032008-07-25 18:28:42 +0200116 return 0;
Stephan Beyerd82f33e2008-07-25 18:28:41 +0200117}
Alban Gruin2aed0182018-08-10 18:51:30 +0200118
119int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
120{
121 return launch_specified_editor(git_editor(), path, buffer, env);
122}
123
124int launch_sequence_editor(const char *path, struct strbuf *buffer,
125 const char *const *env)
126{
127 return launch_specified_editor(git_sequence_editor(), path, buffer, env);
128}