]> git.ipfire.org Git - thirdparty/git.git/blobdiff - strbuf.c
Sync with maint
[thirdparty/git.git] / strbuf.c
index aa48d179a9aec236069fc88501c5c26c3569d502..f19da55b0783dc2d1bf4cab0e0ce76be5711cc55 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -1125,3 +1125,31 @@ int strbuf_normalize_path(struct strbuf *src)
        strbuf_release(&dst);
        return 0;
 }
+
+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;
+}