From: Jeff King Date: Tue, 24 Feb 2026 09:02:07 GMT Subject: Re: [PATCH 2/3] format-patch: add ability to use alt cover format Message-ID: <20260224090207.GB986367@coredump.intra.peff.net> In-Reply-To: <20260224040400.751247-3-mroik@delayed.space> On Tue, Feb 24, 2026 at 05:03:57AM +0100, Mirko Faina wrote: > +static void generate_commit_list_cover(FILE *cover_file, const char *format, struct commit **list, int n) > +{ OK, so we're expecting "format" here to be the full format string here (so "%s" or whatever). But here... > + get_commit_format(format, &rev); ...this isn't quite the function you want to parse it. This function is more about parsing the --pretty option for git-log, etc. It allows named formats like "oneline", "medium", and so on, as well as "format:%s" (and just "%s", as we treat unknown names with a percent as if they had tformat: prepended). As a side effect, it sets up the global user_format variable. Which is horrible and subtle, but a result of historical function interfaces. I'll get to that at the bottom of this email. Back to your patch, I guess here: > + if (commit_format_is_empty(CMIT_FMT_USERFORMAT)) > + die(_("invalid format spec")); you are trying to check if we got a USERFORMAT, versus something else. A more direct way to check that would be: if (rev->commit_format != CMIT_FMT_USERFORMAT) But if we do not use get_commit_format() in the first place, we don't need to worry about that. And I guess you used it because of: > + pp_commit_easy(CMIT_FMT_USERFORMAT, list[i], &commit_line); that line, which is using the full-on pretty-print system that git-log, etc, use. It doesn't really work with CMIT_FMT_USERFORMAT because you have to set up the global user_format first (for those same historical reasons). So I think the entry point you want is repo_format_commit_message(), which does a one-off custom format into a strbuf without needing a rev_info, or touching the global user_format, etc. Something like this: diff --git a/builtin/log.c b/builtin/log.c index 5e9e337be4..370367a15a 100644 --- a/builtin/log.c +++ b/builtin/log.c @@ -1326,16 +1326,12 @@ static void get_notes_args(struct strvec *arg, struct rev_info *rev) static void generate_commit_list_cover(FILE *cover_file, const char *format, struct commit **list, int n) { struct strbuf commit_line = STRBUF_INIT; - struct rev_info rev = REV_INFO_INIT; - - strbuf_init(&commit_line, 0); - get_commit_format(format, &rev); - if (commit_format_is_empty(CMIT_FMT_USERFORMAT)) - die(_("invalid format spec")); + struct pretty_print_context ctx = {0}; for (int i = n - 1; i >= 0; i--) { strbuf_addf(&commit_line, "[%0*d/%d] ", decimal_width(n), n - i, n); - pp_commit_easy(CMIT_FMT_USERFORMAT, list[i], &commit_line); + repo_format_commit_message(the_repository, list[i], + format, &commit_line, &ctx); fprintf(cover_file, "%s\n", commit_line.buf); strbuf_reset(&commit_line); } I think that should make your series do what you want. Now...is the pretty-print code a horrible minefield of booby traps waiting to spring on the unwary developer? Yes, it is. ;) The global user_format thing is there because it was bolted onto the existing pretty-print code, which used a single enum to store the format. But that enum isn't enough for a user-format, because we also have an associated string. The "right" type is probably something like: struct commit_format { enum cmit_fmt fmt; const char *user_format; }; But C being what it is, switching all of the functions to that breaks all of the callers which just want to pass CMIT_FMT_ONELINE or whatever. So we have sort of a split world, where you can use pp_commit_easy() with the baked-in formats, and using the custom format code uses a totally different function. I think in the long run we probably do need to clean up the global user_format. Two pieces of code interleaving calls to the pretty-printer would stomp on each other's formats, for example. We've mostly gotten by because Git, being organized as a set of small programs, generally has one "main" output (for git-log, etc) and then any smaller outputs within a program are done by one-off calls to repo_format_commit_message(), etc. -Peff