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

Re: [PATCH v8 2/6] interpret-trailers: refactor create_in_place_tempfile()

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 6, 2026, 21:05 UTC
Message-ID
<xmqqjyvozn13.fsf@gitster.g>
In-Reply-To
<5a4d03ab375bbba84436796ca6871204f47521eb.1772808594.git.phillip.wood@dunelm.org.uk>
Phillip Wood <phillip.wood123@gmail.com> writes:
Show 15 quoted lines
> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>
> Refactor create_in_place_tempfile() in preparation for moving it
> to tralier.c. Change the return type to return a `struct tempfile*`
> instead of a `FILE*` so that we can remove the file scope tempfile
> variable. Since 076aa2cbda5 (tempfile: auto-allocate tempfiles on
> heap, 2017-09-05) it has not been necessary to make tempfile varibales
> static so this is safe. Also use error() and return NULL in place of
> die() so the caller can exit gracefully and use find_last_dir_sep()
> rather than strchr() to find the parent directory.
>
> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
> ---
>  builtin/interpret-trailers.c | 51 ++++++++++++++++++++----------------
>  1 file changed, 29 insertions(+), 22 deletions(-)
Yes, this organization is much nicer.  Thanks for cleaning it up.
Show 91 quoted lines
>
> diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c
> index 69f9d67ec0e..033c2e46713 100644
> --- a/builtin/interpret-trailers.c
> +++ b/builtin/interpret-trailers.c
> @@ -93,35 +93,37 @@ static int parse_opt_parse(const struct option *opt, const char *arg,
>  	return 0;
>  }
>  
> -static struct tempfile *trailers_tempfile;
>  
> -static FILE *create_in_place_tempfile(const char *file)
> +static struct tempfile *create_in_place_tempfile(const char *file)
>  {
> +	struct tempfile *tempfile = NULL;
>  	struct stat st;
>  	struct strbuf filename_template = STRBUF_INIT;
>  	const char *tail;
> -	FILE *outfile;
> -
> -	if (stat(file, &st))
> -		die_errno(_("could not stat %s"), file);
> -	if (!S_ISREG(st.st_mode))
> -		die(_("file %s is not a regular file"), file);
> -	if (!(st.st_mode & S_IWUSR))
> -		die(_("file %s is not writable by user"), file);
>  
> +	if (stat(file, &st)) {
> +		error_errno(_("could not stat %s"), file);
> +		return NULL;
> +	}
> +	if (!S_ISREG(st.st_mode)) {
> +		error(_("file %s is not a regular file"), file);
> +		return NULL;
> +	}
> +	if (!(st.st_mode & S_IWUSR)) {
> +		error(_("file %s is not writable by user"), file);
> +		return NULL;
> +	}
>  	/* Create temporary file in the same directory as the original */
> -	tail = strrchr(file, '/');
> +	tail = find_last_dir_sep(file);
>  	if (tail)
>  		strbuf_add(&filename_template, file, tail - file + 1);
>  	strbuf_addstr(&filename_template, "git-interpret-trailers-XXXXXX");
>  
> -	trailers_tempfile = xmks_tempfile_m(filename_template.buf, st.st_mode);
> +	tempfile = mks_tempfile_m(filename_template.buf, st.st_mode);
> +
>  	strbuf_release(&filename_template);
> -	outfile = fdopen_tempfile(trailers_tempfile, "w");
> -	if (!outfile)
> -		die_errno(_("could not open temporary file"));
>  
> -	return outfile;
> +	return tempfile;
>  }
>  
>  static void read_input_file(struct strbuf *sb, const char *file)
> @@ -178,20 +180,25 @@ static void interpret_trailers(const struct process_trailer_options *opts,
>  {
>  	struct strbuf input = STRBUF_INIT;
>  	struct strbuf out = STRBUF_INIT;
> -	FILE *outfile = stdout;
> +	struct tempfile *tempfile = NULL;
> +	int fd = 1;
>  
>  	trailer_config_init();
>  
>  	read_input_file(&input, file);
>  
> -	if (opts->in_place)
> -		outfile = create_in_place_tempfile(file);
> -
> +	if (opts->in_place) {
> +		tempfile = create_in_place_tempfile(file);
> +		if (!tempfile)
> +			die(NULL);
> +		fd = tempfile->fd;
> +	}
>  	process_trailers(opts, new_trailer_head, &input, &out);
>  
> -	strbuf_write(&out, outfile);
> +	if (write_in_full(fd, out.buf, out.len) < 0)
> +		die_errno(_("could not write to temporary file '%s'"), file);
>  	if (opts->in_place)
> -		if (rename_tempfile(&trailers_tempfile, file))
> +		if (rename_tempfile(&tempfile, file))
>  			die_errno(_("could not rename temporary file to %s"), file);
>  
>  	strbuf_release(&input);
Previous: Phillip WoodNext: Phillip Wood
Message 28 of 32 in “rebase: support --trailer”
  1. 0/5 rebase: support --trailerLi Chen, Feb 24, 2026
  2. 1/5 interpret-trailers: factor trailer rewritingLi Chen, Feb 24, 2026
  3. Phillip WoodMar 2, 2026
  4. Li ChenMar 2, 2026
  5. 2/5 trailer: move process_trailers to trailer.hLi Chen, Feb 24, 2026
  6. phillip.wood123@gmail.comMar 2, 2026
  7. 3/5 trailer: append trailers without fork/execLi Chen, Feb 24, 2026
  8. Phillip WoodMar 2, 2026
  9. 4/5 commit, tag: parse --trailer with OPT_STRVECLi Chen, Feb 24, 2026
  10. Phillip WoodMar 2, 2026
  11. 5/5 rebase: support --trailerLi Chen, Feb 24, 2026
  12. Phillip WoodMar 3, 2026
  13. Kristoffer HaugsbakkMar 3, 2026
  14. Junio C HamanoMar 3, 2026
  15. Phillip WoodMar 4, 2026
  16. Junio C HamanoMar 4, 2026
  17. Junio C HamanoFeb 26, 2026
  18. Phillip WoodFeb 26, 2026
  19. Kristoffer HaugsbakkFeb 26, 2026
  20. Phillip WoodMar 4, 2026
  21. Li ChenMar 5, 2026
  22. Phillip WoodMar 6, 2026
  23. 0/6 rebase: support --trailerPhillip Wood, Mar 6, 2026
  24. 1/6 interpret-trailers: factor trailer rewritingPhillip Wood, Mar 6, 2026
  25. Junio C HamanoMar 6, 2026
  26. Phillip WoodMar 9, 2026
  27. 2/6 interpret-trailers: refactor create_in_place_tempfile()Phillip Wood, Mar 6, 2026
  28. Junio C HamanoMar 6, 2026
  29. 3/6 trailer: libify a couple of functionsPhillip Wood, Mar 6, 2026
  30. 4/6 trailer: append trailers without fork/execPhillip Wood, Mar 6, 2026
  31. 5/6 commit, tag: parse --trailer with OPT_STRVECPhillip Wood, Mar 6, 2026
  32. 6/6 rebase: support --trailerPhillip Wood, Mar 6, 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.