pretty: add %(describe)

Add a format placeholder for describe output. Implement it by actually calling git describe, which is simple and guarantees correctness. It's intended to be used with $Format:...$ in files with the attribute export-subst and git archive. It can also be used with git log etc., even though that's going to be slow due to the fork for each commit. Suggested-by: Eli Schwartz <eschwartz@archlinux.org> Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Feb 14, 2021 at 11:04 UTC 15ae82d5d6ccdcad00aeb456ff512d3031f03039
3 files changed +29
Documentation/pretty-formats.txt
+2
@@ -208,6 +208,8 @@ The placeholders are:
208 '%cs':: committer date, short format (`YYYY-MM-DD`)
209 '%d':: ref names, like the --decorate option of linkgit:git-log[1]
210 '%D':: ref names without the " (", ")" wrapping.
211 +'%(describe)':: human-readable name, like linkgit:git-describe[1];
212 + empty string for undescribable commits
213 '%S':: ref name given on the command line by which the commit was reached
214 (like `git log --source`), only works with `git log`
215 '%e':: encoding
pretty.c
+17
@@ -12,6 +12,7 @@
12 #include "reflog-walk.h"
13 #include "gpg-interface.h"
14 #include "trailer.h"
15 +#include "run-command.h"
16
17 static char *user_format;
18 static struct cmt_fmt_map {
@@ -1214,6 +1215,22 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1215 return parse_padding_placeholder(placeholder, c);
1216 }
1217
1218 + if (skip_prefix(placeholder, "(describe)", &arg)) {
1219 + struct child_process cmd = CHILD_PROCESS_INIT;
1220 + struct strbuf out = STRBUF_INIT;
1221 + struct strbuf err = STRBUF_INIT;
1222 +
1223 + cmd.git_cmd = 1;
1224 + strvec_push(&cmd.args, "describe");
1225 + strvec_push(&cmd.args, oid_to_hex(&commit->object.oid));
1226 + pipe_command(&cmd, NULL, 0, &out, 0, &err, 0);
1227 + strbuf_rtrim(&out);
1228 + strbuf_addbuf(sb, &out);
1229 + strbuf_release(&out);
1230 + strbuf_release(&err);
1231 + return arg - placeholder;
1232 + }
1233 +
1234 /* these depend on the commit */
1235 if (!commit->object.parsed)
1236 parse_object(the_repository, &commit->object.oid);
t/t4205-log-pretty-formats.sh
+10
@@ -962,4 +962,14 @@ test_expect_success 'log --pretty=reference is colored appropriately' '
962 test_cmp expect actual
963 '
964
965 +test_expect_success '%(describe) vs git describe' '
966 + git log --format="%H" | while read hash
967 + do
968 + echo "$hash $(git describe $hash)"
969 + done >expect &&
970 + git log --format="%H %(describe)" >actual 2>err &&
971 + test_cmp expect actual &&
972 + test_must_be_empty err
973 +'
974 +
975 test_done