Skip to content

Commit 7198203

Browse files
sbeyergitster
authored andcommitted
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>
1 parent d82f33e commit 7198203

4 files changed

Lines changed: 23 additions & 15 deletions

File tree

builtin-commit.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,11 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
646646
char index[PATH_MAX];
647647
const char *env[2] = { index, NULL };
648648
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+
}
650654
}
651655

652656
if (!no_verify &&

builtin-tag.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,11 @@ static void create_tag(const unsigned char *object, const char *tag,
295295
write_or_die(fd, tag_template, strlen(tag_template));
296296
close(fd);
297297

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+
}
299303

300304
unlink(path);
301305
free(path);

editor.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "strbuf.h"
33
#include "run-command.h"
44

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)
66
{
77
const char *editor, *terminal;
88

@@ -15,19 +15,16 @@ void launch_editor(const char *path, struct strbuf *buffer, const char *const *e
1515
editor = getenv("EDITOR");
1616

1717
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.");
2420

2521
if (!editor)
2622
editor = "vi";
2723

2824
if (strcmp(editor, ":")) {
2925
size_t len = strlen(editor);
3026
int i = 0;
27+
int failed;
3128
const char *args[6];
3229
struct strbuf arg0;
3330

@@ -43,14 +40,17 @@ void launch_editor(const char *path, struct strbuf *buffer, const char *const *e
4340
args[i++] = path;
4441
args[i] = NULL;
4542

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);
4844
strbuf_release(&arg0);
45+
if (failed)
46+
return error("There was a problem with the editor '%s'.",
47+
editor);
4948
}
5049

5150
if (!buffer)
52-
return;
51+
return 0;
5352
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;
5656
}

strbuf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,6 @@ extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
123123
extern int strbuf_getline(struct strbuf *, FILE *, int);
124124

125125
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);
127127

128128
#endif /* STRBUF_H */

0 commit comments

Comments
 (0)