Skip to content

Commit eccd97d

Browse files
eli-schwartzgitster
authored andcommitted
pretty: add abbrev option to %(describe)
The %(describe) placeholder by default, like `git describe`, uses a seven-character abbreviated commit object name. This may not be sufficient to fully describe all commits in a given repository, resulting in a placeholder replacement changing its length because the repository grew in size. This could cause the output of git-archive to change. Add the --abbrev option to `git describe` to the placeholder interface in order to provide tools to the user for fine-tuning project defaults and ensure reproducible archives. One alternative would be to just always specify --abbrev=40 but this may be a bit too biased... Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 1d517ce commit eccd97d

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

Documentation/pretty-formats.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ The placeholders are:
222222
+
223223
** 'tags[=<bool>]': Instead of only considering annotated tags,
224224
consider lightweight tags as well.
225+
** 'abbrev=<number>': Instead of using the default number of hexadecimal digits
226+
(which will vary according to the number of objects in the repository with a
227+
default of 7) of the abbreviated object name, use <number> digits, or as many
228+
digits as needed to form a unique object name.
225229
** 'match=<pattern>': Only consider tags matching the given
226230
`glob(7)` pattern, excluding the "refs/tags/" prefix.
227231
** 'exclude=<pattern>': Do not consider tags matching the given

pretty.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,10 +1220,12 @@ static size_t parse_describe_args(const char *start, struct strvec *args)
12201220
char *name;
12211221
enum {
12221222
DESCRIBE_ARG_BOOL,
1223+
DESCRIBE_ARG_INTEGER,
12231224
DESCRIBE_ARG_STRING,
12241225
} type;
12251226
} option[] = {
12261227
{ "tags", DESCRIBE_ARG_BOOL},
1228+
{ "abbrev", DESCRIBE_ARG_INTEGER },
12271229
{ "exclude", DESCRIBE_ARG_STRING },
12281230
{ "match", DESCRIBE_ARG_STRING },
12291231
};
@@ -1247,6 +1249,19 @@ static size_t parse_describe_args(const char *start, struct strvec *args)
12471249
found = 1;
12481250
}
12491251
break;
1252+
case DESCRIBE_ARG_INTEGER:
1253+
if (match_placeholder_arg_value(arg, option[i].name, &arg,
1254+
&argval, &arglen)) {
1255+
char *endptr;
1256+
if (!arglen)
1257+
return 0;
1258+
strtol(argval, &endptr, 10);
1259+
if (endptr - argval != arglen)
1260+
return 0;
1261+
strvec_pushf(args, "--%s=%.*s", option[i].name, (int)arglen, argval);
1262+
found = 1;
1263+
}
1264+
break;
12501265
case DESCRIBE_ARG_STRING:
12511266
if (match_placeholder_arg_value(arg, option[i].name, &arg,
12521267
&argval, &arglen)) {

t/t4205-log-pretty-formats.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,4 +1010,12 @@ test_expect_success '%(describe:tags) vs git describe --tags' '
10101010
test_cmp expect actual
10111011
'
10121012

1013+
test_expect_success '%(describe:abbrev=...) vs git describe --abbrev=...' '
1014+
test_when_finished "git tag -d tagname" &&
1015+
git tag -a -m tagged tagname &&
1016+
git describe --abbrev=15 >expect &&
1017+
git log -1 --format="%(describe:abbrev=15)" >actual &&
1018+
test_cmp expect actual
1019+
'
1020+
10131021
test_done

0 commit comments

Comments
 (0)