]>
Commit | Line | Data |
---|---|---|
d82f33e2 SB |
1 | #include "cache.h" |
2 | #include "strbuf.h" | |
3 | #include "run-command.h" | |
913ef360 | 4 | #include "sigchain.h" |
d82f33e2 | 5 | |
8f4b576a JN |
6 | #ifndef DEFAULT_EDITOR |
7 | #define DEFAULT_EDITOR "vi" | |
8 | #endif | |
9 | ||
44fcb497 | 10 | const char *git_editor(void) |
d82f33e2 | 11 | { |
d33738d7 JN |
12 | const char *editor = getenv("GIT_EDITOR"); |
13 | const char *terminal = getenv("TERM"); | |
14 | int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb"); | |
d82f33e2 | 15 | |
d82f33e2 SB |
16 | if (!editor && editor_program) |
17 | editor = editor_program; | |
d33738d7 | 18 | if (!editor && !terminal_is_dumb) |
d82f33e2 SB |
19 | editor = getenv("VISUAL"); |
20 | if (!editor) | |
21 | editor = getenv("EDITOR"); | |
22 | ||
d33738d7 | 23 | if (!editor && terminal_is_dumb) |
44fcb497 | 24 | return NULL; |
d82f33e2 SB |
25 | |
26 | if (!editor) | |
8f4b576a | 27 | editor = DEFAULT_EDITOR; |
d82f33e2 | 28 | |
44fcb497 JN |
29 | return editor; |
30 | } | |
31 | ||
32 | int 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 | ||
d82f33e2 | 39 | if (strcmp(editor, ":")) { |
09c5ae5a | 40 | const char *args[] = { editor, real_path(path), NULL }; |
d3180279 | 41 | struct child_process p = CHILD_PROCESS_INIT; |
1250857c | 42 | int ret, sig; |
d82f33e2 | 43 | |
f42ca31d JK |
44 | 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 | ||
913ef360 PF |
50 | sigchain_push(SIGINT, SIG_IGN); |
51 | sigchain_push(SIGQUIT, SIG_IGN); | |
52 | ret = finish_command(&p); | |
709ca730 | 53 | sig = ret - 128; |
913ef360 PF |
54 | sigchain_pop(SIGINT); |
55 | sigchain_pop(SIGQUIT); | |
1250857c JK |
56 | if (sig == SIGINT || sig == SIGQUIT) |
57 | raise(sig); | |
913ef360 | 58 | if (ret) |
7198203a SB |
59 | return error("There was a problem with the editor '%s'.", |
60 | editor); | |
d82f33e2 SB |
61 | } |
62 | ||
63 | if (!buffer) | |
7198203a | 64 | return 0; |
d82f33e2 | 65 | if (strbuf_read_file(buffer, path, 0) < 0) |
9f9a522c | 66 | return error_errno("could not read file '%s'", path); |
7198203a | 67 | return 0; |
d82f33e2 | 68 | } |