Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.kernel.org/pub/scm/git/git.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElijah Newren <newren@gmail.com>2023-04-11 10:41:57 +0300
committerJunio C Hamano <gitster@pobox.com>2023-04-11 18:52:10 +0300
commit4e120823a345cf348a052683d726c90bc4b256ca (patch)
tree6453f1bbf0b5fc757689b0b0060cb1fe3141a815 /editor.c
parentd812c3b6a0b41d48ce68f971d9cec50676048863 (diff)
editor: move editor-related functions and declarations into common file
cache.h and strbuf.[ch] had editor-related functions. Move these into editor.[ch]. Signed-off-by: Elijah Newren <newren@gmail.com> Acked-by: Calvin Wan <calvinwan@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'editor.c')
-rw-r--r--editor.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/editor.c b/editor.c
index 3bea3ef72f..12025dfec3 100644
--- a/editor.c
+++ b/editor.c
@@ -2,12 +2,14 @@
#include "abspath.h"
#include "advice.h"
#include "config.h"
+#include "editor.h"
#include "environment.h"
#include "gettext.h"
#include "strbuf.h"
#include "strvec.h"
#include "run-command.h"
#include "sigchain.h"
+#include "wrapper.h"
#ifndef DEFAULT_EDITOR
#define DEFAULT_EDITOR "vi"
@@ -130,3 +132,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;
+}