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>
Johannes Schindelin committed
Dec 13, 2019 at 08:08 UTC
b38dd9e7151f118b989f8c9851bc38a8a8733eaf
2 files changed
+39
strbuf.c
+28
@@ -1125,3 +1125,31 @@ int strbuf_normalize_path(struct strbuf *src)
1125
strbuf_release(&dst);
1126
return 0;
1127
}
1128
+
1129
+int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
1130
+ const char *const *env)
1131
+{
1132
+ char *path2 = NULL;
1133
+ int fd, res = 0;
1134
+
1135
+ if (!is_absolute_path(path))
1136
+ path = path2 = xstrdup(git_path("%s", path));
1137
+
1138
+ fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
1139
+ if (fd < 0)
1140
+ res = error_errno(_("could not open '%s' for writing"), path);
1141
+ else if (write_in_full(fd, buffer->buf, buffer->len) < 0) {
1142
+ res = error_errno(_("could not write to '%s'"), path);
1143
+ close(fd);
1144
+ } else if (close(fd) < 0)
1145
+ res = error_errno(_("could not close '%s'"), path);
1146
+ else {
1147
+ strbuf_reset(buffer);
1148
+ if (launch_editor(path, buffer, env) < 0)
1149
+ res = error_errno(_("could not edit '%s'"), path);
1150
+ unlink(path);
1151
+ }
1152
+
1153
+ free(path2);
1154
+ return res;
1155
+}
strbuf.h
+11
@@ -621,6 +621,17 @@ int launch_editor(const char *path, struct strbuf *buffer,
621
int launch_sequence_editor(const char *path, struct strbuf *buffer,
622
const char *const *env);
623
624
+/*
625
+ * In contrast to `launch_editor()`, this function writes out the contents
626
+ * of the specified file first, then clears the `buffer`, then launches
627
+ * the editor and reads back in the file contents into the `buffer`.
628
+ * Finally, it deletes the temporary file.
629
+ *
630
+ * If `path` is relative, it refers to a file in the `.git` directory.
631
+ */
632
+int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
633
+ const char *const *env);
634
+
635
void strbuf_add_lines(struct strbuf *sb,
636
const char *prefix,
637
const char *buf,