git/list[1] front-page[2] threads[3] people[4] search[5] about
wed 2026-10-07 18:14 UTC

Re: [PATCH 2/2] name-rev: learn --format=<pretty>

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 14, 2026, 00:22 UTC
Message-ID
<xmqq8qbvz2dm.fsf@gitster.g>
In-Reply-To
<name-rev_--format.4af@msgid.xyz>
kristofferhaugsbakk@fastmail.com writes:
Show 11 quoted lines
> diff --git a/Documentation/git-name-rev.adoc b/Documentation/git-name-rev.adoc
> index d4f1c4d5945..8f050cd4763 100644
> --- a/Documentation/git-name-rev.adoc
> +++ b/Documentation/git-name-rev.adoc
> @@ -9,7 +9,7 @@ git-name-rev - Find symbolic names for given revs
>  SYNOPSIS
>  --------
>  [verse]
> -'git name-rev' [--tags] [--refs=<pattern>]
> +'git name-rev' [--tags] [--refs=<pattern>] [--format=<pretty>]
>  	       ( --all | --annotate-stdin | <commit-ish>... )

We acquired a new option. Do we need a matching change to the contents of name_rev_usage[] array?

Show 6 quoted lines
> +--format=<pretty>::
> +--no-format::
> +	Format revisions instead of outputting symbolic names. The
> +	default is `--no-format`.
> ++
> +Implies `--name-only`.
If it is implication, would 
    git name-rev --format=reference --no-name-only
do what is naturally expected?
Show 19 quoted lines
> @@ -462,6 +472,25 @@ static const char *get_rev_name(const struct object *o, struct strbuf *buf)
>  	if (o->type != OBJ_COMMIT)
>  		return get_exact_ref_match(o);
>  	c = (const struct commit *) o;
> +
> +	if (format_ctx) {
> +		strbuf_reset(buf);
> +
> +		if (format_ctx->want.notes) {
> +			struct strbuf notebuf = STRBUF_INIT;
> +
> +			format_display_notes(&c->object.oid, &notebuf,
> +					     get_log_output_encoding(),
> +					     format_ctx->ctx.fmt == CMIT_FMT_USERFORMAT);
> +			format_ctx->ctx.notes_message = strbuf_detach(&notebuf, NULL);
> +		}
> +
> +		pretty_print_commit(&format_ctx->ctx, c, buf);
> +		free(format_ctx->ctx.notes_message);

Is free() the expected thing to do here, or FREE_AND_NULL()? Unlike callers like log-tree.c:show_log() where a context is prepared, used once, and then discarded, format_pp is initialized in cmd_name_rev() once and then repeatedly used by show_name() potentially multiple times, so there may be a risk of getting confused by this leftover non-NULL pointer that points at an already free'd piece of memory.

Or there may not be---I did not check, but you as the author must have already checked, hence this question.

Show 51 quoted lines
> +		return buf->buf;
> +	}
> +
>  	n = get_commit_rev_name(c);
>  	if (!n)
>  		return NULL;
> @@ -479,6 +508,7 @@ static const char *get_rev_name(const struct object *o, struct strbuf *buf)
>  
>  static void show_name(const struct object *obj,
>  		      const char *caller_name,
> +		      struct pretty_format *format_ctx,
>  		      int always, int allow_undefined, int name_only)
>  {
>  	const char *name;
> @@ -487,7 +517,7 @@ static void show_name(const struct object *obj,
>  
>  	if (!name_only)
>  		printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
> -	name = get_rev_name(obj, &buf);
> +	name = get_rev_name(obj, format_ctx, &buf);
>  	if (name)
>  		printf("%s\n", name);
>  	else if (allow_undefined)
> @@ -507,7 +537,9 @@ static char const * const name_rev_usage[] = {
>  	NULL
>  };
>  
> -static void name_rev_line(char *p, struct name_ref_data *data)
> +static void name_rev_line(char *p,
> +			  struct name_ref_data *data,
> +			  struct pretty_format *format_ctx)
>  {
>  	struct strbuf buf = STRBUF_INIT;
>  	int counter = 0;
> @@ -532,7 +564,7 @@ static void name_rev_line(char *p, struct name_ref_data *data)
>  				struct object *o =
>  					lookup_object(the_repository, &oid);
>  				if (o)
> -					name = get_rev_name(o, &buf);
> +					name = get_rev_name(o, format_ctx, &buf);
>  			}
>  			*(p+1) = c;
>  
> @@ -567,6 +599,10 @@ int cmd_name_rev(int argc,
>  #endif
>  	int all = 0, annotate_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
>  	struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
> +	const char *format = NULL;
> +	struct rev_info format_rev = REV_INFO_INIT;
> +	struct pretty_format *format_ctx = NULL;
> +	struct pretty_format format_pp = {0};

Hmph, would we want to use the full init_revisions() instead of static REV_INFO_INIT that initialises a lot more members of the struct properly, most importantly the "repo" member that points at the repostiory to be used?

Show 12 quoted lines
> +	if (format) {
> +		struct pretty_print_context ctx = {0};
> +		struct userformat_want want = {0};
> +
> +		get_commit_format(format, &format_rev);
> +		ctx.rev = &format_rev;
> +		ctx.fmt = format_rev.commit_format;
> +		ctx.abbrev = format_rev.abbrev;
> +		ctx.date_mode_explicit = format_rev.date_mode_explicit;
> +		ctx.date_mode = format_rev.date_mode;
> +		ctx.color = GIT_COLOR_AUTO;
> +		format_pp.ctx = ctx;

Why does this code initialize and assign to a on-stack ctx first and then assign it to format_pp.ctx, instead of working on format_pp.ctx directly?

Show 10 quoted lines
> +		userformat_find_requirements(format, &want);
> +		if (want.notes)
> +			load_display_notes(NULL);
> +
> +		format_pp.want = want;
> +		format_ctx = &format_pp;
> +
> +		data.name_only = true;
> +	}
> +
Previous: Junio C HamanoNext: Kristoffer Haugsbakk
Message 5 of 45 in “name-rev: learn --format=<pretty>”
  1. 0/2 name-rev: learn --format=<pretty>kristofferhaugsbakk@fastmail.com, Mar 13, 2026
  2. 1/2 name-rev: wrap both blocks in braceskristofferhaugsbakk@fastmail.com, Mar 13, 2026
  3. 2/2 name-rev: learn --format=<pretty>kristofferhaugsbakk@fastmail.com, Mar 13, 2026
  4. Junio C HamanoMar 14, 2026
  5. Junio C HamanoMar 14, 2026
  6. Kristoffer HaugsbakkMar 17, 2026
  7. Kristoffer HaugsbakkMar 17, 2026
  8. Kristoffer HaugsbakkMar 18, 2026
  9. 0/2 name-rev: learn --format=<pretty>kristofferhaugsbakk@fastmail.com, Mar 20, 2026
  10. 1/2 name-rev: wrap both blocks in braceskristofferhaugsbakk@fastmail.com, Mar 20, 2026
  11. 2/2 name-rev: learn --format=<pretty>kristofferhaugsbakk@fastmail.com, Mar 20, 2026
  12. D. Ben KnobleMar 20, 2026
  13. Kristoffer HaugsbakkMar 23, 2026
  14. 0/5 format-rev: introduce builtin for on-demand pretty formattingkristofferhaugsbakk@fastmail.com, Apr 28, 2026
  15. 1/5 name-rev: wrap both blocks in braceskristofferhaugsbakk@fastmail.com, Apr 28, 2026
  16. 2/5 name-rev: run clang-format before factoring codekristofferhaugsbakk@fastmail.com, Apr 28, 2026
  17. 3/5 name-rev: factor code for sharing with a new commandkristofferhaugsbakk@fastmail.com, Apr 28, 2026
  18. 4/5 name-rev: make dedicated --annotate-stdin --name-only testkristofferhaugsbakk@fastmail.com, Apr 28, 2026
  19. 5/5 format-rev: introduce builtin for on-demand pretty formattingkristofferhaugsbakk@fastmail.com, Apr 28, 2026
  20. Kristoffer HaugsbakkApr 29, 2026
  21. Kristoffer HaugsbakkApr 30, 2026
  22. Kristoffer HaugsbakkApr 30, 2026
  23. Phillip WoodApr 30, 2026
  24. Phillip WoodMay 1, 2026
  25. kristofferhaugsbakk@fastmail.comMay 1, 2026
  26. kristofferhaugsbakk@fastmail.comMay 1, 2026
  27. Phillip WoodMay 2, 2026
  28. Phillip WoodMay 2, 2026
  29. Junio C HamanoMay 3, 2026
  30. Kristoffer HaugsbakkMay 5, 2026
  31. Kristoffer HaugsbakkMay 5, 2026
  32. 0/5 format-rev: introduce builtin for on-demand pretty formattingkristofferhaugsbakk@fastmail.com, May 7, 2026
  33. 1/5 name-rev: wrap both blocks in braceskristofferhaugsbakk@fastmail.com, May 7, 2026
  34. 2/5 name-rev: run clang-format before factoring codekristofferhaugsbakk@fastmail.com, May 7, 2026
  35. 3/5 name-rev: factor code for sharing with a new commandkristofferhaugsbakk@fastmail.com, May 7, 2026
  36. 4/5 name-rev: make dedicated --annotate-stdin --name-only testkristofferhaugsbakk@fastmail.com, May 7, 2026
  37. 5/5 format-rev: introduce builtin for on-demand pretty formattingkristofferhaugsbakk@fastmail.com, May 7, 2026
  38. Kristoffer HaugsbakkMay 8, 2026
  39. Kristoffer HaugsbakkMay 11, 2026
  40. 0/5 format-rev: introduce builtin for on-demand pretty formattingkristofferhaugsbakk@fastmail.com, May 11, 2026
  41. 1/5 name-rev: wrap both blocks in braceskristofferhaugsbakk@fastmail.com, May 11, 2026
  42. 2/5 name-rev: run clang-format before factoring codekristofferhaugsbakk@fastmail.com, May 11, 2026
  43. 3/5 name-rev: factor code for sharing with a new commandkristofferhaugsbakk@fastmail.com, May 11, 2026
  44. 4/5 name-rev: make dedicated --annotate-stdin --name-only testkristofferhaugsbakk@fastmail.com, May 11, 2026
  45. 5/5 format-rev: introduce builtin for on-demand pretty formattingkristofferhaugsbakk@fastmail.com, May 11, 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.