name-rev: factor code for sharing with a new command

We are about to introduce a new command git-format-rev(1) to this file. Let’s factor some code so that we can share it with the new command. We want to be able to format commits found in freeform text, and git-name-rev(1) already has a function for that but for symbolic names. Let’s use a tagged union for the command-specific payload. No functional changes. Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kristoffer Haugsbakk committed May 11, 2026 at 17:45 UTC e2916329dbea62d033091a625340d3d79b686dc6
1 file changed +40 -13
builtin/name-rev.c
+40 -13
@@ -272,6 +272,24 @@ struct name_ref_data {
272 struct string_list exclude_filters;
273 };
274
275 +enum command_type {
276 + NAME_REV = 1,
277 +};
278 +
279 +struct command {
280 + enum command_type type;
281 + union {
282 + int name_only;
283 + } u;
284 +};
285 +
286 +static void init_name_rev_command(struct command *cmd,
287 + int name_only)
288 +{
289 + cmd->type = NAME_REV;
290 + cmd->u.name_only = name_only;
291 +}
292 +
293 static struct tip_table {
294 struct tip_table_entry {
295 struct object_id oid;
@@ -507,7 +525,7 @@ static char const * const name_rev_usage[] = {
525 NULL
526 };
527
510 -static void name_rev_line(char *p, struct name_ref_data *data)
528 +static void name_rev_line(char *p, struct command *cmd)
529 {
530 struct strbuf buf = STRBUF_INIT;
531 int counter = 0;
@@ -524,25 +542,32 @@ static void name_rev_line(char *p, struct name_ref_data *data)
542 const char *name = NULL;
543 char c = *(p + 1);
544 int p_len = p - p_start + 1;
545 + struct object *o = NULL;
546 + int oid_ret = 1;
547
548 counter = 0;
549
550 *(p + 1) = 0;
531 - if (!repo_get_oid(the_repository, p - (hexsz - 1), &oid)) {
532 - struct object *o =
533 - lookup_object(the_repository, &oid);
551 + oid_ret = repo_get_oid(the_repository, p - (hexsz - 1), &oid);
552 + *(p + 1) = c;
553 +
554 + switch (cmd->type) {
555 + case NAME_REV:
556 + if (!oid_ret)
557 + o = lookup_object(the_repository, &oid);
558 if (o)
559 name = get_rev_name(o, &buf);
560 + if (!name)
561 + continue;
562 + if (cmd->u.name_only)
563 + printf("%.*s%s", p_len - hexsz, p_start, name);
564 + else
565 + printf("%.*s (%s)", p_len, p_start, name);
566 + break;
567 + default:
568 + BUG("uncovered case: %d", cmd->type);
569 }
537 - *(p + 1) = c;
538 -
539 - if (!name)
540 - continue;
570
542 - if (data->name_only)
543 - printf("%.*s%s", p_len - hexsz, p_start, name);
544 - else
545 - printf("%.*s (%s)", p_len, p_start, name);
571 p_start = p + 1;
572 }
573 }
@@ -567,6 +592,7 @@ int cmd_name_rev(int argc,
592 #endif
593 int all = 0, annotate_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
594 struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
595 + struct command cmd;
596 struct option opts[] = {
597 OPT_BOOL(0, "name-only", &data.name_only, N_("print only ref-based names (no object names)")),
598 OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
@@ -596,6 +622,7 @@ int cmd_name_rev(int argc,
622 init_commit_rev_name(&rev_names);
623 repo_config(the_repository, git_default_config, NULL);
624 argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
625 + init_name_rev_command(&cmd, data.name_only);
626
627 #ifndef WITH_BREAKING_CHANGES
628 if (transform_stdin) {
@@ -663,7 +690,7 @@ int cmd_name_rev(int argc,
690
691 while (strbuf_getline(&sb, stdin) != EOF) {
692 strbuf_addch(&sb, '\n');
666 - name_rev_line(sb.buf, &data);
693 + name_rev_line(sb.buf, &cmd);
694 }
695 strbuf_release(&sb);
696 } else if (all) {