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

Re: [PATCHv3 1/6] pretty.c: add %f format specifier to format_commit_message()

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
Mar 31, 2009, 22:17 UTC
Message-ID
<49D2968D.6010108@lsrfire.ath.cx>
In-Reply-To
<baa327d88b54efae40b6d9df09a2fe482672101e.1237773604.git.bebarino@gmail.com>
Stephen Boyd schrieb:
Show 36 quoted lines
> This specifier represents the sanitized and filename friendly subject
> line of a commit. No checks are made against the length of the string,
> so users may need to trim the result to the desired length if using as a
> filename. This is commonly used by format-patch to massage commit
> subjects into filenames and output patches to files.
> 
> Signed-off-by: Stephen Boyd <bebarino@gmail.com>
> ---
>  Documentation/pretty-formats.txt |    1 +
>  pretty.c                         |   38 ++++++++++++++++++++++++++++++++++++++
>  2 files changed, 39 insertions(+), 0 deletions(-)
> 
> diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
> index 5c6e678..2a845b1 100644
> --- a/Documentation/pretty-formats.txt
> +++ b/Documentation/pretty-formats.txt
> @@ -121,6 +121,7 @@ The placeholders are:
>  - '%d': ref names, like the --decorate option of linkgit:git-log[1]
>  - '%e': encoding
>  - '%s': subject
> +- '%f': sanitized subject line, suitable for a filename
>  - '%b': body
>  - '%Cred': switch color to red
>  - '%Cgreen': switch color to green
> diff --git a/pretty.c b/pretty.c
> index efa7024..97de415 100644
> --- a/pretty.c
> +++ b/pretty.c
> @@ -493,6 +493,41 @@ static void parse_commit_header(struct format_commit_context *context)
>  	context->commit_header_parsed = 1;
>  }
>  
> +static int istitlechar(char c)
> +{
> +	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
> +		(c >= '0' && c <= '9') || c == '.' || c == '_';
How about this?
	return isalnum(c) || c == '.' || c == '_';
Show 23 quoted lines
> +}
> +
> +static void format_sanitized_subject(struct strbuf *sb, const char *msg)
> +{
> +	size_t trimlen;
> +	int space = 0;
> +
> +	for (; *msg && *msg != '\n'; msg++) {
> +		if (istitlechar(*msg))
> +		{
> +		    if (space) {
> +			strbuf_addch(sb, '-');
> +			space = 0;
> +		    }
> +		    strbuf_addch(sb, *msg);
> +		    if (*msg == '.')
> +			while (*(msg+1) == '.')
> +				msg++;
> +		}
> +		else
> +			space = 1;
> +	}
> +
Show 6 quoted lines
> +	// trim any trailing '.' or '-' characters
> +	trimlen = 0;
> +	while (sb->buf[sb->len - 1 - trimlen] == '.'
> +		|| sb->buf[sb->len - 1 - trimlen] == '-')
> +		trimlen++;
> +	strbuf_remove(sb, sb->len - trimlen, trimlen);

You need to make sure that trimming stops as soon as the strbuf has been shortened to its original length. E.g. for a subject line of "..." you'd access the char before the first dot currently, or sb->buf[-1] if the strbuf was empty initially.

(One could also check for sb->len > 0 to just prevent the buffer underrun, but %f sometimes eating preceding dots and dashes is counter-intuitive to me.)

Show 15 quoted lines
> +}
> +
>  const char *format_subject(struct strbuf *sb, const char *msg,
>  			   const char *line_separator)
>  {
> @@ -683,6 +718,9 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
>  	case 's':	/* subject */
>  		format_subject(sb, msg + c->subject_off, " ");
>  		return 1;
> +	case 'f':	/* sanitized subject */
> +		format_sanitized_subject(sb, msg + c->subject_off);
> +		return 1;
>  	case 'b':	/* body */
>  		strbuf_addstr(sb, msg + c->body_off);
>  		return 1;
Previous: Stephen BoydNext: Stephen Boyd
Message 19 of 20 in “[PATCHv2 0/3] format-patch --attach/--inline use filename instead of SHA1”
  1. Stephen BoydMar 22, 2009
  2. 1/3 format-patch: create patch filename in one functionStephen Boyd, Mar 22, 2009
  3. 2/3 format-patch: --attach/inline uses filename instead of SHA1Stephen Boyd, Mar 22, 2009
  4. 3/3 format-patch: --numbered-files and --stdout aren't mutually exclusiveStephen Boyd, Mar 22, 2009
  5. Junio C HamanoMar 22, 2009
  6. Junio C HamanoMar 22, 2009
  7. Junio C HamanoMar 22, 2009
  8. Stephen BoydMar 22, 2009
  9. Junio C HamanoMar 22, 2009
  10. Stephen BoydMar 22, 2009
  11. Junio C HamanoMar 22, 2009
  12. 0/6 format-patch --attach/--inline uses filename not SHA1Stephen Boyd, Mar 23, 2009
  13. 1/6 pretty.c: add %f format specifier to format_commit_message()Stephen Boyd, Mar 23, 2009
  14. 2/6 format-patch: construct patch filename in one functionStephen Boyd, Mar 23, 2009
  15. 3/6 format-patch: pass a commit to reopen_stdout()Stephen Boyd, Mar 23, 2009
  16. 4/6 format-patch: move get_patch_filename() into log-treeStephen Boyd, Mar 23, 2009
  17. 5/6 format-patch: --attach/inline uses filename instead of SHA1Stephen Boyd, Mar 23, 2009
  18. 6/6 format-patch: --numbered-files and --stdout aren't mutually exclusiveStephen Boyd, Mar 23, 2009
  19. René ScharfeMar 31, 2009
  20. format_sanitized_subject: Don't trim past initial length of strbufStephen Boyd, Mar 31, 2009

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.