summaryrefslogtreecommitdiff
path: root/strbuf.c
diff options
context:
space:
mode:
authorJohannes Schindelin <johannes.schindelin@gmx.de>2019-12-13 08:08:00 (GMT)
committerJunio C Hamano <gitster@pobox.com>2019-12-13 20:37:14 (GMT)
commitb38dd9e7151f118b989f8c9851bc38a8a8733eaf (patch)
tree442c629074f7e0231d18efde47d214697d021a37 /strbuf.c
parent11f2c0dae8f8889b533455d700121d437f4be19f (diff)
downloadgit-b38dd9e7151f118b989f8c9851bc38a8a8733eaf.zip
git-b38dd9e7151f118b989f8c9851bc38a8a8733eaf.tar.gz
git-b38dd9e7151f118b989f8c9851bc38a8a8733eaf.tar.bz2
strbuf: add a helper function to call the editor "on an strbuf"
This helper supports the scenario where Git has a populated `strbuf` and wants to let the user edit it interactively. In `git add -p`, we will use this to allow interactive hunk editing: the diff hunks are already in memory, but we need to write them out to a file so that an editor can be launched, then read everything back once the user is done editing. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'strbuf.c')
-rw-r--r--strbuf.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/strbuf.c b/strbuf.c
index aa48d17..f19da55 100644
--- 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;
+}