summaryrefslogtreecommitdiff
path: root/editor.c
diff options
context:
space:
mode:
Diffstat (limited to 'editor.c')
-rw-r--r--editor.c35
1 files changed, 34 insertions, 1 deletions
diff --git a/editor.c b/editor.c
index d632d79066..38c5dbbb79 100644
--- a/editor.c
+++ b/editor.c
@@ -1,12 +1,17 @@
-#include "cache.h"
+#include "git-compat-util.h"
#include "abspath.h"
+#include "advice.h"
#include "config.h"
+#include "editor.h"
#include "environment.h"
#include "gettext.h"
+#include "pager.h"
+#include "path.h"
#include "strbuf.h"
#include "strvec.h"
#include "run-command.h"
#include "sigchain.h"
+#include "wrapper.h"
#ifndef DEFAULT_EDITOR
#define DEFAULT_EDITOR "vi"
@@ -129,3 +134,31 @@ int launch_sequence_editor(const char *path, struct strbuf *buffer,
{
return launch_specified_editor(git_sequence_editor(), path, buffer, env);
}
+
+int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
+ const char *const *env)
+{
+ char *path2 = NULL;
+ int fd, res = 0;
+
+ if (!is_absolute_path(path))
+ path = path2 = xstrdup(git_path("%s", path));
+
+ fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+ if (fd < 0)
+ res = error_errno(_("could not open '%s' for writing"), path);
+ else if (write_in_full(fd, buffer->buf, buffer->len) < 0) {
+ res = error_errno(_("could not write to '%s'"), path);
+ close(fd);
+ } else if (close(fd) < 0)
+ res = error_errno(_("could not close '%s'"), path);
+ else {
+ strbuf_reset(buffer);
+ if (launch_editor(path, buffer, env) < 0)
+ res = error_errno(_("could not edit '%s'"), path);
+ unlink(path);
+ }
+
+ free(path2);
+ return res;
+}