@matheusbsbs / matheus-bagliano / commits / 7198203ae3
Fork 0

editor.c: Libify launch_editor()

This patch removes exit()/die() calls and builtin-specific messages from launch_editor(), so that it can be used as a general libgit.a function to launch an editor. Signed-off-by: Stephan Beyer <s-beyer@gmx.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stephan Beyer committed Jul 25, 2008 at 18:28 UTC 7198203ae37c11327c0d01f1e37f3e74381755a9
4 files changed +23 -15
builtin-commit.c
+5 -1
@@ -646,7 +646,11 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
646 char index[PATH_MAX];
647 const char *env[2] = { index, NULL };
648 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
649 - launch_editor(git_path(commit_editmsg), NULL, env);
649 + if (launch_editor(git_path(commit_editmsg), NULL, env)) {
650 + fprintf(stderr,
651 + "Please supply the message using either -m or -F option.\n");
652 + exit(1);
653 + }
654 }
655
656 if (!no_verify &&
builtin-tag.c
+5 -1
@@ -295,7 +295,11 @@ static void create_tag(const unsigned char *object, const char *tag,
295 write_or_die(fd, tag_template, strlen(tag_template));
296 close(fd);
297
298 - launch_editor(path, buf, NULL);
298 + if (launch_editor(path, buf, NULL)) {
299 + fprintf(stderr,
300 + "Please supply the message using either -m or -F option.\n");
301 + exit(1);
302 + }
303
304 unlink(path);
305 free(path);
editor.c
+12 -12
@@ -2,7 +2,7 @@
2 #include "strbuf.h"
3 #include "run-command.h"
4
5 -void launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
5 +int launch_editor(const char *path, struct strbuf *buffer, const char *const *env)
6 {
7 const char *editor, *terminal;
8
@@ -15,12 +15,8 @@ void launch_editor(const char *path, struct strbuf *buffer, const char *const *e
15 editor = getenv("EDITOR");
16
17 terminal = getenv("TERM");
18 - if (!editor && (!terminal || !strcmp(terminal, "dumb"))) {
19 - fprintf(stderr,
20 - "Terminal is dumb but no VISUAL nor EDITOR defined.\n"
21 - "Please supply the message using either -m or -F option.\n");
22 - exit(1);
23 - }
18 + if (!editor && (!terminal || !strcmp(terminal, "dumb")))
19 + return error("Terminal is dumb but no VISUAL nor EDITOR defined.");
20
21 if (!editor)
22 editor = "vi";
@@ -28,6 +24,7 @@ void launch_editor(const char *path, struct strbuf *buffer, const char *const *e
24 if (strcmp(editor, ":")) {
25 size_t len = strlen(editor);
26 int i = 0;
27 + int failed;
28 const char *args[6];
29 struct strbuf arg0;
30
@@ -43,14 +40,17 @@ void launch_editor(const char *path, struct strbuf *buffer, const char *const *e
40 args[i++] = path;
41 args[i] = NULL;
42
46 - if (run_command_v_opt_cd_env(args, 0, NULL, env))
47 - die("There was a problem with the editor %s.", editor);
43 + failed = run_command_v_opt_cd_env(args, 0, NULL, env);
44 strbuf_release(&arg0);
45 + if (failed)
46 + return error("There was a problem with the editor '%s'.",
47 + editor);
48 }
49
50 if (!buffer)
52 - return;
51 + return 0;
52 if (strbuf_read_file(buffer, path, 0) < 0)
54 - die("could not read message file '%s': %s",
55 - path, strerror(errno));
53 + return error("could not read file '%s': %s",
54 + path, strerror(errno));
55 + return 0;
56 }
strbuf.h
+1 -1
@@ -123,6 +123,6 @@ extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
123 extern int strbuf_getline(struct strbuf *, FILE *, int);
124
125 extern void stripspace(struct strbuf *buf, int skip_comments);
126 -extern void launch_editor(const char *path, struct strbuf *buffer, const char *const *env);
126 +extern int launch_editor(const char *path, struct strbuf *buffer, const char *const *env);
127
128 #endif /* STRBUF_H */