Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.kernel.org/pub/scm/git/git.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2023-07-06 21:54:45 +0300
committerJunio C Hamano <gitster@pobox.com>2023-07-06 21:54:45 +0300
commitda269af9207e38e820daad8aa993caa7d2fad76c (patch)
tree28567788ed00e6c037cb93684e95a473e3d2f412 /builtin/cat-file.c
parenta646b86cd10282de2ceb64ef33b5412e4fb2a54c (diff)
parent4416b86c6b34dad64b556bb1eb6711d5e6595a48 (diff)
Merge branch 'rs/strbuf-expand-step'
Code clean-up around strbuf_expand() API. * rs/strbuf-expand-step: strbuf: simplify strbuf_expand_literal_cb() replace strbuf_expand() with strbuf_expand_step() replace strbuf_expand_dict_cb() with strbuf_expand_step() strbuf: factor out strbuf_expand_step() pretty: factor out expand_separator()
Diffstat (limited to 'builtin/cat-file.c')
-rw-r--r--builtin/cat-file.c35
1 files changed, 17 insertions, 18 deletions
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index ab8ac105e3..fab04310fb 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -309,10 +309,8 @@ static int is_atom(const char *atom, const char *s, int slen)
}
static void expand_atom(struct strbuf *sb, const char *atom, int len,
- void *vdata)
+ struct expand_data *data)
{
- struct expand_data *data = vdata;
-
if (is_atom("objectname", atom, len)) {
if (!data->mark_query)
strbuf_addstr(sb, oid_to_hex(&data->oid));
@@ -346,19 +344,21 @@ static void expand_atom(struct strbuf *sb, const char *atom, int len,
die("unknown format element: %.*s", len, atom);
}
-static size_t expand_format(struct strbuf *sb, const char *start, void *data)
+static void expand_format(struct strbuf *sb, const char *start,
+ struct expand_data *data)
{
- const char *end;
-
- if (*start != '(')
- return 0;
- end = strchr(start + 1, ')');
- if (!end)
- die("format element '%s' does not end in ')'", start);
-
- expand_atom(sb, start + 1, end - start - 1, data);
-
- return end - start + 1;
+ while (strbuf_expand_step(sb, &start)) {
+ const char *end;
+
+ if (skip_prefix(start, "%", &start) || *start != '(')
+ strbuf_addch(sb, '%');
+ else if (!(end = strchr(start + 1, ')')))
+ die("format element '%s' does not end in ')'", start);
+ else {
+ expand_atom(sb, start + 1, end - start - 1, data);
+ start = end + 1;
+ }
+ }
}
static void batch_write(struct batch_options *opt, const void *data, int len)
@@ -495,7 +495,7 @@ static void batch_object_write(const char *obj_name,
if (!opt->format) {
print_default_format(scratch, data, opt);
} else {
- strbuf_expand(scratch, opt->format, expand_format, data);
+ expand_format(scratch, opt->format, data);
strbuf_addch(scratch, opt->output_delim);
}
@@ -773,9 +773,8 @@ static int batch_objects(struct batch_options *opt)
*/
memset(&data, 0, sizeof(data));
data.mark_query = 1;
- strbuf_expand(&output,
+ expand_format(&output,
opt->format ? opt->format : DEFAULT_FORMAT,
- expand_format,
&data);
data.mark_query = 0;
strbuf_release(&output);