1 #include "git-compat-util.h"
6 #include "environment.h"
12 #include "run-command.h"
15 #ifndef DEFAULT_EDITOR
16 #define DEFAULT_EDITOR "vi"
19 int is_terminal_dumb(void)
21 const char *terminal
= getenv("TERM");
22 return !terminal
|| !strcmp(terminal
, "dumb");
25 const char *git_editor(void)
27 const char *editor
= getenv("GIT_EDITOR");
28 int terminal_is_dumb
= is_terminal_dumb();
30 if (!editor
&& editor_program
)
31 editor
= editor_program
;
32 if (!editor
&& !terminal_is_dumb
)
33 editor
= getenv("VISUAL");
35 editor
= getenv("EDITOR");
37 if (!editor
&& terminal_is_dumb
)
41 editor
= DEFAULT_EDITOR
;
46 const char *git_sequence_editor(void)
48 const char *editor
= getenv("GIT_SEQUENCE_EDITOR");
51 git_config_get_string_tmp("sequence.editor", &editor
);
53 editor
= git_editor();
58 static int launch_specified_editor(const char *editor
, const char *path
,
59 struct strbuf
*buffer
, const char *const *env
)
62 return error("Terminal is dumb, but EDITOR unset");
64 if (strcmp(editor
, ":")) {
65 struct strbuf realpath
= STRBUF_INIT
;
66 struct child_process p
= CHILD_PROCESS_INIT
;
68 int print_waiting_for_editor
= advice_enabled(ADVICE_WAITING_FOR_EDITOR
) && isatty(2);
70 if (print_waiting_for_editor
) {
72 * A dumb terminal cannot erase the line later on. Add a
73 * newline to separate the hint from subsequent output.
75 * Make sure that our message is separated with a whitespace
76 * from further cruft that may be written by the editor.
78 const char term
= is_terminal_dumb() ? '\n' : ' ';
81 _("hint: Waiting for your editor to close the file...%c"),
86 strbuf_realpath(&realpath
, path
, 1);
88 strvec_pushl(&p
.args
, editor
, realpath
.buf
, NULL
);
90 strvec_pushv(&p
.env
, (const char **)env
);
92 p
.trace2_child_class
= "editor";
93 if (start_command(&p
) < 0) {
94 strbuf_release(&realpath
);
95 return error("unable to start editor '%s'", editor
);
98 sigchain_push(SIGINT
, SIG_IGN
);
99 sigchain_push(SIGQUIT
, SIG_IGN
);
100 ret
= finish_command(&p
);
101 strbuf_release(&realpath
);
103 sigchain_pop(SIGINT
);
104 sigchain_pop(SIGQUIT
);
105 if (sig
== SIGINT
|| sig
== SIGQUIT
)
107 if (print_waiting_for_editor
&& !is_terminal_dumb())
109 * Erase the entire line to avoid wasting the
114 return error("there was a problem with the editor '%s'",
120 if (strbuf_read_file(buffer
, path
, 0) < 0)
121 return error_errno("could not read file '%s'", path
);
125 int launch_editor(const char *path
, struct strbuf
*buffer
, const char *const *env
)
127 return launch_specified_editor(git_editor(), path
, buffer
, env
);
130 int launch_sequence_editor(const char *path
, struct strbuf
*buffer
,
131 const char *const *env
)
133 return launch_specified_editor(git_sequence_editor(), path
, buffer
, env
);
136 int strbuf_edit_interactively(struct strbuf
*buffer
, const char *path
,
137 const char *const *env
)
142 if (!is_absolute_path(path
))
143 path
= path2
= xstrdup(git_path("%s", path
));
145 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
147 res
= error_errno(_("could not open '%s' for writing"), path
);
148 else if (write_in_full(fd
, buffer
->buf
, buffer
->len
) < 0) {
149 res
= error_errno(_("could not write to '%s'"), path
);
151 } else if (close(fd
) < 0)
152 res
= error_errno(_("could not close '%s'"), path
);
154 strbuf_reset(buffer
);
155 if (launch_editor(path
, buffer
, env
) < 0)
156 res
= error_errno(_("could not edit '%s'"), path
);