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

[PATCH v2 1/3] merge-ll: use strbuf to read back external merge result

From
Jeff King <peff@peff.net>
Date
Sep 11, 2026, 17:11 UTC
Message-ID
<20260911171124.GA1610200@coredump.intra.peff.net>
In-Reply-To
<20260911171044.GA1609692@coredump.intra.peff.net>

After the external merge runs, we read the file back into a heap buffer. This ancient code does it by hand, but these days we can make the code shorter and less error prone by using strbuf_read_file().

It's not quite a one-liner replacement, because we have to copy the pointer and size into an mmbuffer_t. Two things to note there:

  1. We can't just pass result->size to strbuf_detach(), since the
     former uses long instead of size_t (something that we'd ideally fix
     in the long run, but is way out of scope here).
  2. We can leave result untouched on error; we zero it at the top of
     the function (confusingly we may still return LL_MERGE_OK and a
     NULL result if we hit an I/O error, but that is how the function
     has always behaved, and callers know to check for NULL).
Signed-off-by: Jeff King <peff@peff.net>
---
Not strictly needed for the rest of the series, but it felt like a
cleanup worth doing, and it conflicts textually.
 merge-ll.c | 22 +++++++---------------
 1 file changed, 7 insertions(+), 15 deletions(-)
diff --git a/merge-ll.c b/merge-ll.c
index ef5287dee8..5b6af15e23 100644
--- a/merge-ll.c
+++ b/merge-ll.c
@@ -201,8 +201,8 @@ static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn,
 	struct strbuf cmd = STRBUF_INIT;
 	const char *format = fn->cmdline;
 	struct child_process child = CHILD_PROCESS_INIT;
-	int status, fd, i;
-	struct stat st;
+	int status, i;
+	struct strbuf result_buf = STRBUF_INIT;
 	enum ll_merge_result ret;
 	assert(opts);
 
@@ -241,20 +241,12 @@ static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn,
 	child.use_shell = 1;
 	strvec_push(&child.args, cmd.buf);
 	status = run_command(&child);
-	fd = open(temp[1], O_RDONLY);
-	if (fd < 0)
-		goto bad;
-	if (fstat(fd, &st))
-		goto close_bad;
-	result->size = st.st_size;
-	result->ptr = xmallocz(result->size);
-	if (read_in_full(fd, result->ptr, result->size) != result->size) {
-		FREE_AND_NULL(result->ptr);
-		result->size = 0;
+
+	if (strbuf_read_file(&result_buf, temp[1], 0) >= 0) {
+		result->size = result_buf.len;
+		result->ptr = strbuf_detach(&result_buf, NULL);
 	}
- close_bad:
-	close(fd);
- bad:
+
 	for (i = 0; i < 3; i++)
 		unlink_or_warn(temp[i]);
 	strbuf_release(&cmd);
-- 
2.56.0.rc0.314.g7a874b6915
Previous: Jeff KingNext: Elijah Newren
Message 5 of 22 in “merge-ll: Cleanup merge driver temporaries after interrupt”
  1. merge-ll: Cleanup merge driver temporaries after interruptMichal Koutný, Sep 10, 2026
  2. Jeff KingSep 10, 2026
  3. Michal KoutnýSep 11, 2026
  4. 0/3 merge-ll: Cleanup merge driver temporaries afterJeff King, Sep 11, 2026
  5. 1/3 merge-ll: use strbuf to read back external merge resultJeff King, Sep 11, 2026
  6. Elijah NewrenSep 11, 2026
  7. Junio C HamanoSep 11, 2026
  8. Jeff KingSep 14, 2026
  9. 2/3 merge-ll: catch close() errors when writing external tempfilesJeff King, Sep 11, 2026
  10. Elijah NewrenSep 11, 2026
  11. Jeff KingSep 14, 2026
  12. 3/3 merge-ll: use tempfile API for external driver filesJeff King, Sep 11, 2026
  13. Elijah NewrenSep 11, 2026
  14. Michal KoutnýSep 14, 2026
  15. Jeff KingSep 14, 2026
  16. Michal KoutnýSep 14, 2026
  17. Jeff KingSep 14, 2026
  18. 0/2 merge-ll: Cleanup merge driver temporaries after signalJeff King, Sep 29, 2026
  19. 1/2 merge-ll: catch close() errors when writing external tempfilesJeff King, Sep 29, 2026
  20. 2/2 merge-ll: use tempfile API for external driver filesJeff King, Sep 29, 2026
  21. Junio C HamanoSep 29, 2026
  22. Jeff KingSep 29, 2026

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.