Skip to content

Commit 14dff2b

Browse files
committed
Merge pull request #2402 from dscho/add-i-in-c-status-and-help-gfw
built-in add -i/-p: update to latest iteration
2 parents 815f7f4 + 115afa8 commit 14dff2b

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

strbuf.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,3 +1122,31 @@ int strbuf_normalize_path(struct strbuf *src)
11221122
strbuf_release(&dst);
11231123
return 0;
11241124
}
1125+
1126+
int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
1127+
const char *const *env)
1128+
{
1129+
char *path2 = NULL;
1130+
int fd, res = 0;
1131+
1132+
if (!is_absolute_path(path))
1133+
path = path2 = xstrdup(git_path("%s", path));
1134+
1135+
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
1136+
if (fd < 0)
1137+
res = error_errno(_("could not open '%s' for writing"), path);
1138+
else if (write_in_full(fd, buffer->buf, buffer->len) < 0) {
1139+
res = error_errno(_("could not write to '%s'"), path);
1140+
close(fd);
1141+
} else if (close(fd) < 0)
1142+
res = error_errno(_("could not close '%s'"), path);
1143+
else {
1144+
strbuf_reset(buffer);
1145+
if (launch_editor(path, buffer, env) < 0)
1146+
res = error_errno(_("could not edit '%s'"), path);
1147+
unlink(path);
1148+
}
1149+
1150+
free(path2);
1151+
return res;
1152+
}

strbuf.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,17 @@ int launch_editor(const char *path, struct strbuf *buffer,
621621
int launch_sequence_editor(const char *path, struct strbuf *buffer,
622622
const char *const *env);
623623

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+
624635
void strbuf_add_lines(struct strbuf *sb,
625636
const char *prefix,
626637
const char *buf,

0 commit comments

Comments
 (0)