Skip to content

Commit 25f9326

Browse files
committed
Merge branch 'rs/pretty-describe'
"git log --format='...'" learned "%(describe)" placeholder. * rs/pretty-describe: archive: expand only a single %(describe) per archive pretty: document multiple %(describe) being inconsistent t4205: assert %(describe) test coverage pretty: add merge and exclude options to %(describe) pretty: add %(describe)
2 parents f5c73f6 + 9609972 commit 25f9326

File tree

8 files changed

+145
-7
lines changed

8 files changed

+145
-7
lines changed

Documentation/gitattributes.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,8 @@ tag then no replacement will be done. The placeholders are the same
11741174
as those for the option `--pretty=format:` of linkgit:git-log[1],
11751175
except that they need to be wrapped like this: `$Format:PLACEHOLDERS$`
11761176
in the file. E.g. the string `$Format:%H$` will be replaced by the
1177-
commit hash.
1177+
commit hash. However, only one `%(describe)` placeholder is expanded
1178+
per archive to avoid denial-of-service attacks.
11781179

11791180

11801181
Packing objects

Documentation/pretty-formats.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,19 @@ 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[:options])':: human-readable name, like
212+
linkgit:git-describe[1]; empty string for
213+
undescribable commits. The `describe` string
214+
may be followed by a colon and zero or more
215+
comma-separated options. Descriptions can be
216+
inconsistent when tags are added or removed at
217+
the same time.
218+
+
219+
** 'match=<pattern>': Only consider tags matching the given
220+
`glob(7)` pattern, excluding the "refs/tags/" prefix.
221+
** 'exclude=<pattern>': Do not consider tags matching the given
222+
`glob(7)` pattern, excluding the "refs/tags/" prefix.
223+
211224
'%S':: ref name given on the command line by which the commit was reached
212225
(like `git log --source`), only works with `git log`
213226
'%e':: encoding

archive.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@ void init_archivers(void)
3737

3838
static void format_subst(const struct commit *commit,
3939
const char *src, size_t len,
40-
struct strbuf *buf)
40+
struct strbuf *buf, struct pretty_print_context *ctx)
4141
{
4242
char *to_free = NULL;
4343
struct strbuf fmt = STRBUF_INIT;
44-
struct pretty_print_context ctx = {0};
45-
ctx.date_mode.type = DATE_NORMAL;
46-
ctx.abbrev = DEFAULT_ABBREV;
4744

4845
if (src == buf->buf)
4946
to_free = strbuf_detach(buf, NULL);
@@ -61,7 +58,7 @@ static void format_subst(const struct commit *commit,
6158
strbuf_add(&fmt, b + 8, c - b - 8);
6259

6360
strbuf_add(buf, src, b - src);
64-
format_commit_message(commit, fmt.buf, buf, &ctx);
61+
format_commit_message(commit, fmt.buf, buf, ctx);
6562
len -= c + 1 - src;
6663
src = c + 1;
6764
}
@@ -94,7 +91,7 @@ static void *object_file_to_archive(const struct archiver_args *args,
9491
strbuf_attach(&buf, buffer, *sizep, *sizep + 1);
9592
convert_to_working_tree(args->repo->index, path, buf.buf, buf.len, &buf, &meta);
9693
if (commit)
97-
format_subst(commit, buf.buf, buf.len, &buf);
94+
format_subst(commit, buf.buf, buf.len, &buf, args->pretty_ctx);
9895
buffer = strbuf_detach(&buf, &size);
9996
*sizep = size;
10097
}
@@ -633,12 +630,19 @@ int write_archive(int argc, const char **argv, const char *prefix,
633630
const char *name_hint, int remote)
634631
{
635632
const struct archiver *ar = NULL;
633+
struct pretty_print_describe_status describe_status = {0};
634+
struct pretty_print_context ctx = {0};
636635
struct archiver_args args;
637636
int rc;
638637

639638
git_config_get_bool("uploadarchive.allowunreachable", &remote_allow_unreachable);
640639
git_config(git_default_config, NULL);
641640

641+
describe_status.max_invocations = 1;
642+
ctx.date_mode.type = DATE_NORMAL;
643+
ctx.abbrev = DEFAULT_ABBREV;
644+
ctx.describe_status = &describe_status;
645+
args.pretty_ctx = &ctx;
642646
args.repo = repo;
643647
args.prefix = prefix;
644648
string_list_init(&args.extra_files, 1);

archive.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "pathspec.h"
66

77
struct repository;
8+
struct pretty_print_context;
89

910
struct archiver_args {
1011
struct repository *repo;
@@ -22,6 +23,7 @@ struct archiver_args {
2223
unsigned int convert : 1;
2324
int compression_level;
2425
struct string_list extra_files;
26+
struct pretty_print_context *pretty_ctx;
2527
};
2628

2729
/* main api */

pretty.c

Lines changed: 64 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 {
@@ -1206,6 +1207,34 @@ int format_set_trailers_options(struct process_trailer_options *opts,
12061207
return 0;
12071208
}
12081209

1210+
static size_t parse_describe_args(const char *start, struct strvec *args)
1211+
{
1212+
const char *options[] = { "match", "exclude" };
1213+
const char *arg = start;
1214+
1215+
for (;;) {
1216+
const char *matched = NULL;
1217+
const char *argval;
1218+
size_t arglen = 0;
1219+
int i;
1220+
1221+
for (i = 0; i < ARRAY_SIZE(options); i++) {
1222+
if (match_placeholder_arg_value(arg, options[i], &arg,
1223+
&argval, &arglen)) {
1224+
matched = options[i];
1225+
break;
1226+
}
1227+
}
1228+
if (!matched)
1229+
break;
1230+
1231+
if (!arglen)
1232+
return 0;
1233+
strvec_pushf(args, "--%s=%.*s", matched, (int)arglen, argval);
1234+
}
1235+
return arg - start;
1236+
}
1237+
12091238
static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
12101239
const char *placeholder,
12111240
void *context)
@@ -1271,6 +1300,41 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
12711300
return parse_padding_placeholder(placeholder, c);
12721301
}
12731302

1303+
if (skip_prefix(placeholder, "(describe", &arg)) {
1304+
struct child_process cmd = CHILD_PROCESS_INIT;
1305+
struct strbuf out = STRBUF_INIT;
1306+
struct strbuf err = STRBUF_INIT;
1307+
struct pretty_print_describe_status *describe_status;
1308+
1309+
describe_status = c->pretty_ctx->describe_status;
1310+
if (describe_status) {
1311+
if (!describe_status->max_invocations)
1312+
return 0;
1313+
describe_status->max_invocations--;
1314+
}
1315+
1316+
cmd.git_cmd = 1;
1317+
strvec_push(&cmd.args, "describe");
1318+
1319+
if (*arg == ':') {
1320+
arg++;
1321+
arg += parse_describe_args(arg, &cmd.args);
1322+
}
1323+
1324+
if (*arg != ')') {
1325+
child_process_clear(&cmd);
1326+
return 0;
1327+
}
1328+
1329+
strvec_push(&cmd.args, oid_to_hex(&commit->object.oid));
1330+
pipe_command(&cmd, NULL, 0, &out, 0, &err, 0);
1331+
strbuf_rtrim(&out);
1332+
strbuf_addbuf(sb, &out);
1333+
strbuf_release(&out);
1334+
strbuf_release(&err);
1335+
return arg - placeholder + 1;
1336+
}
1337+
12741338
/* these depend on the commit */
12751339
if (!commit->object.parsed)
12761340
parse_object(the_repository, &commit->object.oid);

pretty.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ enum cmit_fmt {
2424
CMIT_FMT_UNSPECIFIED
2525
};
2626

27+
struct pretty_print_describe_status {
28+
unsigned int max_invocations;
29+
};
30+
2731
struct pretty_print_context {
2832
/*
2933
* Callers should tweak these to change the behavior of pp_* functions.
@@ -45,6 +49,7 @@ struct pretty_print_context {
4549
int color;
4650
struct ident_split *from_ident;
4751
unsigned encode_email_headers:1;
52+
struct pretty_print_describe_status *describe_status;
4853

4954
/*
5055
* Fields below here are manipulated internally by pp_* functions and

t/t4205-log-pretty-formats.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,4 +962,39 @@ 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+
if desc=$(git describe $hash)
969+
then
970+
: >expect-contains-good
971+
else
972+
: >expect-contains-bad
973+
fi &&
974+
echo "$hash $desc"
975+
done >expect &&
976+
test_path_exists expect-contains-good &&
977+
test_path_exists expect-contains-bad &&
978+
979+
git log --format="%H %(describe)" >actual 2>err &&
980+
test_cmp expect actual &&
981+
test_must_be_empty err
982+
'
983+
984+
test_expect_success '%(describe:match=...) vs git describe --match ...' '
985+
test_when_finished "git tag -d tag-match" &&
986+
git tag -a -m tagged tag-match&&
987+
git describe --match "*-match" >expect &&
988+
git log -1 --format="%(describe:match=*-match)" >actual &&
989+
test_cmp expect actual
990+
'
991+
992+
test_expect_success '%(describe:exclude=...) vs git describe --exclude ...' '
993+
test_when_finished "git tag -d tag-exclude" &&
994+
git tag -a -m tagged tag-exclude &&
995+
git describe --exclude "*-exclude" >expect &&
996+
git log -1 --format="%(describe:exclude=*-exclude)" >actual &&
997+
test_cmp expect actual
998+
'
999+
9651000
test_done

t/t5001-archive-attr.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,18 @@ test_expect_success 'export-subst' '
128128
test_cmp substfile2 archive/substfile2
129129
'
130130

131+
test_expect_success 'export-subst expands %(describe) once' '
132+
echo "\$Format:%(describe)\$" >substfile3 &&
133+
echo "\$Format:%(describe)\$" >>substfile3 &&
134+
echo "\$Format:%(describe)${LF}%(describe)\$" >substfile4 &&
135+
git add substfile[34] &&
136+
git commit -m export-subst-describe &&
137+
git tag -m export-subst-describe export-subst-describe &&
138+
git archive HEAD >archive-describe.tar &&
139+
extract_tar_to_dir archive-describe &&
140+
desc=$(git describe) &&
141+
grep -F "$desc" archive-describe/substfile[34] >substituted &&
142+
test_line_count = 1 substituted
143+
'
144+
131145
test_done

0 commit comments

Comments
 (0)