git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/1] add --edit: truncate the patch file

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Jan 15, 2019, 15:42 UTC
Message-ID
<52f74c5d356325660ca1b494e78fb99a3c6c3861.1547566970.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.107.git.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

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>
---
 builtin/add.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/builtin/add.c b/builtin/add.c
index f65c172299..53c18ea429 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -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");
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: Junio C Hamano
Message 2 of 5 in “Be careful about left-over files from git add --edit runs”
  1. 0/1 Be careful about left-over files from git add --edit runsJohannes Schindelin via GitGitGadget, Jan 15, 2019
  2. 1/1 add --edit: truncate the patch fileJohannes Schindelin via GitGitGadget, Jan 15, 2019
  3. Junio C HamanoJan 15, 2019
  4. Junio C HamanoJan 15, 2019
  5. Johannes SchindelinJan 16, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.