add --edit: truncate the patch file
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Tue, 15 Jan 2019 15:42:52 +0000 (07:42 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 15 Jan 2019 18:51:21 +0000 (10:51 -0800)
If there is already a .git/ADD_EDIT.patch file, we fail to truncate it
properly, which could result in very funny errors.

Of course, this file should not be left lying around. But at least in
one case, there was a stale copy, larger than the current diff. So the
result was a corrupt diff.

Let's just truncate the file when we write it and not worry about it too
much.

Reported by J Wyman.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/add.c
index 8a155dd41eccd8004a55b1302111293ae59ea542..160a4944fc088f04a644e6fe450132f13ac72e08 100644 (file)
@@ -239,7 +239,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
        rev.diffopt.output_format = DIFF_FORMAT_PATCH;
        rev.diffopt.use_color = 0;
        rev.diffopt.flags.ignore_dirty_submodules = 1;
-       out = open(file, O_CREAT | O_WRONLY, 0666);
+       out = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
        if (out < 0)
                die(_("Could not open '%s' for writing."), file);
        rev.diffopt.file = xfdopen(out, "w");