Skip to content

Commit 15ae82d

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 328c109 commit 15ae82d

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

Documentation/pretty-formats.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ The placeholders are:
208208
'%cs':: committer date, short format (`YYYY-MM-DD`)
209209
'%d':: ref names, like the --decorate option of linkgit:git-log[1]
210210
'%D':: ref names without the " (", ")" wrapping.
211+
'%(describe)':: human-readable name, like linkgit:git-describe[1];
212+
empty string for undescribable commits
211213
'%S':: ref name given on the command line by which the commit was reached
212214
(like `git log --source`), only works with `git log`
213215
'%e':: encoding

pretty.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "reflog-walk.h"
1313
#include "gpg-interface.h"
1414
#include "trailer.h"
15+
#include "run-command.h"
1516

1617
static char *user_format;
1718
static struct cmt_fmt_map {
@@ -1214,6 +1215,22 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
12141215
return parse_padding_placeholder(placeholder, c);
12151216
}
12161217

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+
12171234
/* these depend on the commit */
12181235
if (!commit->object.parsed)
12191236
parse_object(the_repository, &commit->object.oid);

t/t4205-log-pretty-formats.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,4 +962,14 @@ test_expect_success 'log --pretty=reference is colored appropriately' '
962962
test_cmp expect actual
963963
'
964964

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+
965975
test_done

0 commit comments

Comments
 (0)