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

Re: [PATCH v5 01/29] trailer: append trailers in-process and drop the fork to `interpret-trailers`

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Oct 23, 2025, 13:21 UTC
Message-ID
<7d12b046-365f-441c-af8e-8a39d61efbbd@gmail.com>
In-Reply-To
<20251022053951.602605-2-me@linux.beauty>
Hi Li
On 22/10/2025 06:39, Li Chen wrote:
Show 11 quoted lines
> From: Li Chen <chenl311@chinatelecom.cn>
> 
> Route all trailer insertion through trailer_process() and make
> builtin/interpret-trailers just do file I/O before calling into it.
> amend_file_with_trailers() now shares the same code path.
> 
> This removes the fork/exec and tempfile juggling, cutting overhead and
> simplifying error handling. No functional change. It also
> centralizes logic to prepare for follow-up rebase --trailer patch.
> 
> Signed-off-by: Li Chen <chenl311@chinatelecom.cn>
When I review v3 of this series I said
Show 7 quoted lines
>> As I said above reusing the existing code as you have done here is a
>> much better approach. However it would be much easier to review if
>> the code movement was separated from the refactoring. I'm also
>> struggling to see the benefit of a lot of the refactoring - I was
>> expecting the conversion to use an strubf would essentially look like
>> fwrite() being replaced with strbuf_add() and fprintf() being
>> replaced with strbuf_addf() etc. rather than reworking the logic.

Unfortunately this version has the same code changes as v3 that make it are virtually impossible to verify if the behavior is changed or not. The diff below shows what I was hoping to see as the first step. It refactors interpret_trailers() in place to factor out the code that processes the trailers into a separate function. That makes it easy to see that fwrite() is replaced with strbuf_add() etc. and so verify that the behavior is unchanged. If you view the diff with "--color-moved" you'll see that virtually all of the code in the new interpret_trailers() function is moved from the old one which in turn makes it easy to verify that there is no change in behavior. I would expect the next patch in the series to move the new function for processing trailers into trailer.c and the patch after that to refactor amend_file_with_trailers() to add and use amend_strbuf_with_trailers() and stop forking "git interpret-trailers". Then builtin/rebase.c can be modified to add support for trailers using amend_strbuf_with_trailers().

Thanks
Phillip
---- 8< ----
diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c
index 41b0750e5af..4c90580ffff 100644
--- a/builtin/interpret-trailers.c
+++ b/builtin/interpret-trailers.c
@@ -136,32 +136,21 @@ static void read_input_file(struct strbuf *sb, const char *file)
  	strbuf_complete_line(sb);
  }
  
-static void interpret_trailers(const struct process_trailer_options *opts,
-			       struct list_head *new_trailer_head,
-			       const char *file)
+static void process_trailers(const struct process_trailer_options *opts,
+			     struct list_head *new_trailer_head,
+			     struct strbuf *sb, struct strbuf *out)
  {
  	LIST_HEAD(head);
-	struct strbuf sb = STRBUF_INIT;
-	struct strbuf trailer_block_sb = STRBUF_INIT;
  	struct trailer_block *trailer_block;
-	FILE *outfile = stdout;
-
-	trailer_config_init();
-
-	read_input_file(&sb, file);
-
-	if (opts->in_place)
-		outfile = create_in_place_tempfile(file);
-
-	trailer_block = parse_trailers(opts, sb.buf, &head);
+
+	trailer_block = parse_trailers(opts, sb->buf, &head);
  
  	/* Print the lines before the trailer block */
  	if (!opts->only_trailers)
-		fwrite(sb.buf, 1, trailer_block_start(trailer_block), outfile);
+		strbuf_add(out, sb->buf, trailer_block_start(trailer_block));
  
  	if (!opts->only_trailers && !blank_line_before_trailer_block(trailer_block))
-		fprintf(outfile, "\n");
-
+		strbuf_addch(out, '\n');
  
  	if (!opts->only_input) {
  		LIST_HEAD(config_head);
@@ -173,22 +162,40 @@ static void interpret_trailers(const struct process_trailer_options *opts,
  	}
  
  	/* Print trailer block. */
-	format_trailers(opts, &head, &trailer_block_sb);
+	format_trailers(opts, &head, out);
  	free_trailers(&head);
-	fwrite(trailer_block_sb.buf, 1, trailer_block_sb.len, outfile);
-	strbuf_release(&trailer_block_sb);
  
  	/* Print the lines after the trailer block as is. */
  	if (!opts->only_trailers)
-		fwrite(sb.buf + trailer_block_end(trailer_block), 1,
-		       sb.len - trailer_block_end(trailer_block), outfile);
+		strbuf_add(out, sb->buf + trailer_block_end(trailer_block),
+			   sb->len - trailer_block_end(trailer_block));
  	trailer_block_release(trailer_block);
-
+}
+
+static void interpret_trailers(const struct process_trailer_options *opts,
+			       struct list_head *new_trailer_head,
+			       const char *file)
+{
+	struct strbuf sb = STRBUF_INIT;
+	struct strbuf out = STRBUF_INIT;
+	FILE *outfile = stdout;
+
+	trailer_config_init();
+
+	read_input_file(&sb, file);
+
+	if (opts->in_place)
+		outfile = create_in_place_tempfile(file);
+
+	process_trailers(opts, new_trailer_head, &sb, &out);
+
+	fwrite(out.buf, out.len, 1, outfile);
  	if (opts->in_place)
  		if (rename_tempfile(&trailers_tempfile, file))
  			die_errno(_("could not rename temporary file to %s"), file);
  
  	strbuf_release(&sb);
+	strbuf_release(&out);
  }
  
  int cmd_interpret_trailers(int argc,
Previous: Li ChenNext: Li Chen
Message 3 of 33 in “rebase: support --trailer”
  1. 00/29 rebase: support --trailerLi Chen, Oct 22, 2025
  2. 01/29 trailer: append trailers in-process and drop the fork to `interpret-trailers`Li Chen, Oct 22, 2025
  3. Phillip WoodOct 23, 2025
  4. Li ChenNov 4, 2025
  5. 02/29 trailer: restore interpret_trailers helperLi Chen, Oct 22, 2025
  6. 03/29 trailer: drop --trailer prefix handling in amend helperLi Chen, Oct 22, 2025
  7. 04/29 trailer: move config_head and arg_head to if storageLi Chen, Oct 22, 2025
  8. 05/29 trailer: use bool for had_trailer_beforeLi Chen, Oct 22, 2025
  9. 06/29 interpret-trailers: buffer stdout outputLi Chen, Oct 22, 2025
  10. 07/29 trailer: mirror interpret-trailers output flowLi Chen, Oct 22, 2025
  11. 08/29 trailer: handle trailer append failures gentlyLi Chen, Oct 22, 2025
  12. 09/29 rebase: support --trailerLi Chen, Oct 22, 2025
  13. Phillip WoodOct 23, 2025
  14. 10/29 rebase: inline trailer state pathsLi Chen, Oct 22, 2025
  15. 11/29 rebase: reuse buffer for trailer argsLi Chen, Oct 22, 2025
  16. 12/29 rebase: drop redundant strbuf_release callLi Chen, Oct 22, 2025
  17. 13/29 rebase: skip stripping of --trailer option prefixLi Chen, Oct 22, 2025
  18. 14/29 rebase: die on invalid trailer argsLi Chen, Oct 22, 2025
  19. 15/29 rebase: validate trailers with configured separatorsLi Chen, Oct 22, 2025
  20. 16/29 sequencer: add trailers to message before writing fileLi Chen, Oct 22, 2025
  21. 17/29 t3440: create expect files at point of useLi Chen, Oct 22, 2025
  22. 18/29 t3440: check apply backend error includes optionLi Chen, Oct 22, 2025
  23. 19/29 t3440: use test_commit_message for trailer checksLi Chen, Oct 22, 2025
  24. 20/29 t3440: drop redundant resets and pass branch to rebase where neededLi Chen, Oct 22, 2025
  25. 21/29 t3440: assert trailer on HEAD after conflict rebaseLi Chen, Oct 22, 2025
  26. 22/29 rebase: persist --trailer options across restartsLi Chen, Oct 22, 2025
  27. 23/29 t3440: remove redundant --keep-emptyLi Chen, Oct 22, 2025
  28. 24/29 t3440: use helper for trailer checksLi Chen, Oct 22, 2025
  29. 25/29 t3440: test --trailer without valuesLi Chen, Oct 22, 2025
  30. 26/29 t3440: convert ex.com to example.comLi Chen, Oct 22, 2025
  31. 27/29 t3440: ensure trailers persist after rebase continueLi Chen, Oct 22, 2025
  32. 28/29 t3440: exercise trailer config mappingLi Chen, Oct 22, 2025
  33. 29/29 sequencer: honor --trailer with fixup -CLi Chen, Oct 22, 2025

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.