🌐 AI搜索 & 代理 主页
blob: 0abbd8dc3a0ec91acd0c143b6d9b5ad41ac90417 [file] [log] [blame]
Stephan Beyerd82f33e2008-07-25 16:28:411#include "cache.h"
2#include "strbuf.h"
3#include "run-command.h"
Paul Fox913ef362012-11-30 22:41:264#include "sigchain.h"
Stephan Beyerd82f33e2008-07-25 16:28:415
Jonathan Nieder8f4b5762009-10-31 01:44:416#ifndef DEFAULT_EDITOR
7#define DEFAULT_EDITOR "vi"
8#endif
9
Jonathan Nieder44fcb492009-11-12 00:01:2710const char *git_editor(void)
Stephan Beyerd82f33e2008-07-25 16:28:4111{
Jonathan Niederd33738d2009-11-11 23:56:0712 const char *editor = getenv("GIT_EDITOR");
13 const char *terminal = getenv("TERM");
14 int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb");
Stephan Beyerd82f33e2008-07-25 16:28:4115
Stephan Beyerd82f33e2008-07-25 16:28:4116 if (!editor && editor_program)
17 editor = editor_program;
Jonathan Niederd33738d2009-11-11 23:56:0718 if (!editor && !terminal_is_dumb)
Stephan Beyerd82f33e2008-07-25 16:28:4119 editor = getenv("VISUAL");
20 if (!editor)
21 editor = getenv("EDITOR");
22
Jonathan Niederd33738d2009-11-11 23:56:0723 if (!editor && terminal_is_dumb)
Jonathan Nieder44fcb492009-11-12 00:01:2724 return NULL;
Stephan Beyerd82f33e2008-07-25 16:28:4125
26 if (!editor)
Jonathan Nieder8f4b5762009-10-31 01:44:4127 editor = DEFAULT_EDITOR;
Stephan Beyerd82f33e2008-07-25 16:28:4128
Jonathan Nieder44fcb492009-11-12 00:01:2729 return editor;
30}
31
32int 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 Beyerd82f33e2008-07-25 16:28:4139 if (strcmp(editor, ":")) {
Ramkumar Ramachandra09c5ae52013-07-28 16:59:4240 const char *args[] = { editor, real_path(path), NULL };
Jeff Kingf42ca312012-11-30 22:41:0441 struct child_process p;
Jeff King12508572012-11-30 22:41:5042 int ret, sig;
Stephan Beyerd82f33e2008-07-25 16:28:4143
Jeff Kingf42ca312012-11-30 22:41:0444 memset(&p, 0, sizeof(p));
45 p.argv = args;
46 p.env = env;
47 p.use_shell = 1;
48 if (start_command(&p) < 0)
49 return error("unable to start editor '%s'", editor);
50
Paul Fox913ef362012-11-30 22:41:2651 sigchain_push(SIGINT, SIG_IGN);
52 sigchain_push(SIGQUIT, SIG_IGN);
53 ret = finish_command(&p);
Jeff King709ca732013-01-05 14:49:4954 sig = ret - 128;
Paul Fox913ef362012-11-30 22:41:2655 sigchain_pop(SIGINT);
56 sigchain_pop(SIGQUIT);
Jeff King12508572012-11-30 22:41:5057 if (sig == SIGINT || sig == SIGQUIT)
58 raise(sig);
Paul Fox913ef362012-11-30 22:41:2659 if (ret)
Stephan Beyer71982032008-07-25 16:28:4260 return error("There was a problem with the editor '%s'.",
61 editor);
Stephan Beyerd82f33e2008-07-25 16:28:4162 }
63
64 if (!buffer)
Stephan Beyer71982032008-07-25 16:28:4265 return 0;
Stephan Beyerd82f33e2008-07-25 16:28:4166 if (strbuf_read_file(buffer, path, 0) < 0)
Stephan Beyer71982032008-07-25 16:28:4267 return error("could not read file '%s': %s",
68 path, strerror(errno));
69 return 0;
Stephan Beyerd82f33e2008-07-25 16:28:4170}