]> git.ipfire.org Git - thirdparty/git.git/blame - editor.c
fix compilation with NO_PTHREADS
[thirdparty/git.git] / editor.c
CommitLineData
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 10const 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
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
d82f33e2 39 if (strcmp(editor, ":")) {
bac80370 40 const char *args[] = { editor, path, NULL };
f42ca31d 41 struct child_process p;
1250857c 42 int ret, sig;
d82f33e2 43
f42ca31d
JK
44 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
913ef360
PF
51 sigchain_push(SIGINT, SIG_IGN);
52 sigchain_push(SIGQUIT, SIG_IGN);
53 ret = finish_command(&p);
1250857c 54 sig = ret + 128;
913ef360
PF
55 sigchain_pop(SIGINT);
56 sigchain_pop(SIGQUIT);
1250857c
JK
57 if (sig == SIGINT || sig == SIGQUIT)
58 raise(sig);
913ef360 59 if (ret)
7198203a
SB
60 return error("There was a problem with the editor '%s'.",
61 editor);
d82f33e2
SB
62 }
63
64 if (!buffer)
7198203a 65 return 0;
d82f33e2 66 if (strbuf_read_file(buffer, path, 0) < 0)
7198203a
SB
67 return error("could not read file '%s': %s",
68 path, strerror(errno));
69 return 0;
d82f33e2 70}