git
/
editor.c
167 строк · 4.0 Кб
1#define USE_THE_REPOSITORY_VARIABLE2
3#include "git-compat-util.h"4#include "abspath.h"5#include "advice.h"6#include "config.h"7#include "editor.h"8#include "environment.h"9#include "gettext.h"10#include "pager.h"11#include "path.h"12#include "strbuf.h"13#include "strvec.h"14#include "run-command.h"15#include "sigchain.h"16
17#ifndef DEFAULT_EDITOR18#define DEFAULT_EDITOR "vi"19#endif20
21int is_terminal_dumb(void)22{
23const char *terminal = getenv("TERM");24return !terminal || !strcmp(terminal, "dumb");25}
26
27const char *git_editor(void)28{
29const char *editor = getenv("GIT_EDITOR");30int terminal_is_dumb = is_terminal_dumb();31
32if (!editor && editor_program)33editor = editor_program;34if (!editor && !terminal_is_dumb)35editor = getenv("VISUAL");36if (!editor)37editor = getenv("EDITOR");38
39if (!editor && terminal_is_dumb)40return NULL;41
42if (!editor)43editor = DEFAULT_EDITOR;44
45return editor;46}
47
48const char *git_sequence_editor(void)49{
50const char *editor = getenv("GIT_SEQUENCE_EDITOR");51
52if (!editor)53git_config_get_string_tmp("sequence.editor", &editor);54if (!editor)55editor = git_editor();56
57return editor;58}
59
60static int launch_specified_editor(const char *editor, const char *path,61struct strbuf *buffer, const char *const *env)62{
63if (!editor)64return error("Terminal is dumb, but EDITOR unset");65
66if (strcmp(editor, ":")) {67struct strbuf realpath = STRBUF_INIT;68struct child_process p = CHILD_PROCESS_INIT;69int ret, sig;70int print_waiting_for_editor = advice_enabled(ADVICE_WAITING_FOR_EDITOR) && isatty(2);71
72if (print_waiting_for_editor) {73/*74* A dumb terminal cannot erase the line later on. Add a
75* newline to separate the hint from subsequent output.
76*
77* Make sure that our message is separated with a whitespace
78* from further cruft that may be written by the editor.
79*/
80const char term = is_terminal_dumb() ? '\n' : ' ';81
82fprintf(stderr,83_("hint: Waiting for your editor to close the file...%c"),84term);85fflush(stderr);86}87
88strbuf_realpath(&realpath, path, 1);89
90strvec_pushl(&p.args, editor, realpath.buf, NULL);91if (env)92strvec_pushv(&p.env, (const char **)env);93p.use_shell = 1;94p.trace2_child_class = "editor";95if (start_command(&p) < 0) {96strbuf_release(&realpath);97return error("unable to start editor '%s'", editor);98}99
100sigchain_push(SIGINT, SIG_IGN);101sigchain_push(SIGQUIT, SIG_IGN);102ret = finish_command(&p);103strbuf_release(&realpath);104sig = ret - 128;105sigchain_pop(SIGINT);106sigchain_pop(SIGQUIT);107if (sig == SIGINT || sig == SIGQUIT)108raise(sig);109if (print_waiting_for_editor && !is_terminal_dumb())110/*111* Erase the entire line to avoid wasting the
112* vertical space.
113*/
114term_clear_line();115if (ret)116return error("there was a problem with the editor '%s'",117editor);118}119
120if (!buffer)121return 0;122if (strbuf_read_file(buffer, path, 0) < 0)123return error_errno("could not read file '%s'", path);124return 0;125}
126
127int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)128{
129return launch_specified_editor(git_editor(), path, buffer, env);130}
131
132int launch_sequence_editor(const char *path, struct strbuf *buffer,133const char *const *env)134{
135return launch_specified_editor(git_sequence_editor(), path, buffer, env);136}
137
138int strbuf_edit_interactively(struct repository *r,139struct strbuf *buffer, const char *path,140const char *const *env)141{
142struct strbuf sb = STRBUF_INIT;143int fd, res = 0;144
145if (!is_absolute_path(path)) {146strbuf_repo_git_path(&sb, r, "%s", path);147path = sb.buf;148}149
150fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);151if (fd < 0)152res = error_errno(_("could not open '%s' for writing"), path);153else if (write_in_full(fd, buffer->buf, buffer->len) < 0) {154res = error_errno(_("could not write to '%s'"), path);155close(fd);156} else if (close(fd) < 0)157res = error_errno(_("could not close '%s'"), path);158else {159strbuf_reset(buffer);160if (launch_editor(path, buffer, env) < 0)161res = error_errno(_("could not edit '%s'"), path);162unlink(path);163}164
165strbuf_release(&sb);166return res;167}
168