Skip to content

Commit c5accad

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 c68aa1d + d0d883b commit c5accad

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

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)