🌐 AI搜索 & 代理 主页
blob: 7519edecdc297a0c0fdbbb2f3c68cb55abf2e8b2 [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 };
René Scharfed3180272014-08-19 19:09:3541 struct child_process p = CHILD_PROCESS_INIT;
Jeff King12508572012-11-30 22:41:5042 int ret, sig;
Stephan Beyerd82f33e2008-07-25 16:28:4143
Jeff Kingf42ca312012-11-30 22:41:0444 p.argv = args;
45 p.env = env;
46 p.use_shell = 1;
47 if (start_command(&p) < 0)
48 return error("unable to start editor '%s'", editor);
49
Paul Fox913ef362012-11-30 22:41:2650 sigchain_push(SIGINT, SIG_IGN);
51 sigchain_push(SIGQUIT, SIG_IGN);
52 ret = finish_command(&p);
Jeff King709ca732013-01-05 14:49:4953 sig = ret - 128;
Paul Fox913ef362012-11-30 22:41:2654 sigchain_pop(SIGINT);
55 sigchain_pop(SIGQUIT);
Jeff King12508572012-11-30 22:41:5056 if (sig == SIGINT || sig == SIGQUIT)
57 raise(sig);
Paul Fox913ef362012-11-30 22:41:2658 if (ret)
Stephan Beyer71982032008-07-25 16:28:4259 return error("There was a problem with the editor '%s'.",
60 editor);
Stephan Beyerd82f33e2008-07-25 16:28:4161 }
62
63 if (!buffer)
Stephan Beyer71982032008-07-25 16:28:4264 return 0;
Stephan Beyerd82f33e2008-07-25 16:28:4165 if (strbuf_read_file(buffer, path, 0) < 0)
Nguyễn Thái Ngọc Duy9f9a5222016-05-08 09:47:4366 return error_errno("could not read file '%s'", path);
Stephan Beyer71982032008-07-25 16:28:4267 return 0;
Stephan Beyerd82f33e2008-07-25 16:28:4168}