mirror of
https://github.com/git/git
synced 2024-10-02 14:45:21 +00:00
Merge branch 'rs/strbuf-expand-bad-format'
Code clean-up. * rs/strbuf-expand-bad-format: cat-file: use strbuf_expand_bad_format() factor out strbuf_expand_bad_format()
This commit is contained in:
commit
4cc302e886
|
@ -314,7 +314,7 @@ static int is_atom(const char *atom, const char *s, int slen)
|
||||||
return alen == slen && !memcmp(atom, s, alen);
|
return alen == slen && !memcmp(atom, s, alen);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void expand_atom(struct strbuf *sb, const char *atom, int len,
|
static int expand_atom(struct strbuf *sb, const char *atom, int len,
|
||||||
struct expand_data *data)
|
struct expand_data *data)
|
||||||
{
|
{
|
||||||
if (is_atom("objectname", atom, len)) {
|
if (is_atom("objectname", atom, len)) {
|
||||||
|
@ -347,7 +347,8 @@ static void expand_atom(struct strbuf *sb, const char *atom, int len,
|
||||||
strbuf_addstr(sb,
|
strbuf_addstr(sb,
|
||||||
oid_to_hex(&data->delta_base_oid));
|
oid_to_hex(&data->delta_base_oid));
|
||||||
} else
|
} else
|
||||||
die("unknown format element: %.*s", len, atom);
|
return 0;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void expand_format(struct strbuf *sb, const char *start,
|
static void expand_format(struct strbuf *sb, const char *start,
|
||||||
|
@ -358,12 +359,11 @@ static void expand_format(struct strbuf *sb, const char *start,
|
||||||
|
|
||||||
if (skip_prefix(start, "%", &start) || *start != '(')
|
if (skip_prefix(start, "%", &start) || *start != '(')
|
||||||
strbuf_addch(sb, '%');
|
strbuf_addch(sb, '%');
|
||||||
else if (!(end = strchr(start + 1, ')')))
|
else if ((end = strchr(start + 1, ')')) &&
|
||||||
die("format element '%s' does not end in ')'", start);
|
expand_atom(sb, start + 1, end - start - 1, data))
|
||||||
else {
|
|
||||||
expand_atom(sb, start + 1, end - start - 1, data);
|
|
||||||
start = end + 1;
|
start = end + 1;
|
||||||
}
|
else
|
||||||
|
strbuf_expand_bad_format(start, "cat-file");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -266,7 +266,6 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
|
||||||
struct strbuf sb = STRBUF_INIT;
|
struct strbuf sb = STRBUF_INIT;
|
||||||
|
|
||||||
while (strbuf_expand_step(&sb, &format)) {
|
while (strbuf_expand_step(&sb, &format)) {
|
||||||
const char *end;
|
|
||||||
size_t len;
|
size_t len;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
|
@ -274,12 +273,6 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
|
||||||
strbuf_addch(&sb, '%');
|
strbuf_addch(&sb, '%');
|
||||||
else if ((len = strbuf_expand_literal(&sb, format)))
|
else if ((len = strbuf_expand_literal(&sb, format)))
|
||||||
format += len;
|
format += len;
|
||||||
else if (*format != '(')
|
|
||||||
die(_("bad ls-files format: element '%s' "
|
|
||||||
"does not start with '('"), format);
|
|
||||||
else if (!(end = strchr(format + 1, ')')))
|
|
||||||
die(_("bad ls-files format: element '%s' "
|
|
||||||
"does not end in ')'"), format);
|
|
||||||
else if (skip_prefix(format, "(objectmode)", &format))
|
else if (skip_prefix(format, "(objectmode)", &format))
|
||||||
strbuf_addf(&sb, "%06o", ce->ce_mode);
|
strbuf_addf(&sb, "%06o", ce->ce_mode);
|
||||||
else if (skip_prefix(format, "(objectname)", &format))
|
else if (skip_prefix(format, "(objectname)", &format))
|
||||||
|
@ -308,8 +301,7 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,
|
||||||
else if (skip_prefix(format, "(path)", &format))
|
else if (skip_prefix(format, "(path)", &format))
|
||||||
write_name_to_buf(&sb, fullname);
|
write_name_to_buf(&sb, fullname);
|
||||||
else
|
else
|
||||||
die(_("bad ls-files format: %%%.*s"),
|
strbuf_expand_bad_format(format, "ls-files");
|
||||||
(int)(end - format + 1), format);
|
|
||||||
}
|
}
|
||||||
strbuf_addch(&sb, line_terminator);
|
strbuf_addch(&sb, line_terminator);
|
||||||
fwrite(sb.buf, sb.len, 1, stdout);
|
fwrite(sb.buf, sb.len, 1, stdout);
|
||||||
|
|
|
@ -100,19 +100,12 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
while (strbuf_expand_step(&sb, &format)) {
|
while (strbuf_expand_step(&sb, &format)) {
|
||||||
const char *end;
|
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
if (skip_prefix(format, "%", &format))
|
if (skip_prefix(format, "%", &format))
|
||||||
strbuf_addch(&sb, '%');
|
strbuf_addch(&sb, '%');
|
||||||
else if ((len = strbuf_expand_literal(&sb, format)))
|
else if ((len = strbuf_expand_literal(&sb, format)))
|
||||||
format += len;
|
format += len;
|
||||||
else if (*format != '(')
|
|
||||||
die(_("bad ls-tree format: element '%s' "
|
|
||||||
"does not start with '('"), format);
|
|
||||||
else if (!(end = strchr(format + 1, ')')))
|
|
||||||
die(_("bad ls-tree format: element '%s' "
|
|
||||||
"does not end in ')'"), format);
|
|
||||||
else if (skip_prefix(format, "(objectmode)", &format))
|
else if (skip_prefix(format, "(objectmode)", &format))
|
||||||
strbuf_addf(&sb, "%06o", mode);
|
strbuf_addf(&sb, "%06o", mode);
|
||||||
else if (skip_prefix(format, "(objecttype)", &format))
|
else if (skip_prefix(format, "(objecttype)", &format))
|
||||||
|
@ -135,8 +128,7 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
|
||||||
strbuf_setlen(base, baselen);
|
strbuf_setlen(base, baselen);
|
||||||
strbuf_release(&sbuf);
|
strbuf_release(&sbuf);
|
||||||
} else
|
} else
|
||||||
die(_("bad ls-tree format: %%%.*s"),
|
strbuf_expand_bad_format(format, "ls-tree");
|
||||||
(int)(end - format + 1), format);
|
|
||||||
}
|
}
|
||||||
strbuf_addch(&sb, options->null_termination ? '\0' : '\n');
|
strbuf_addch(&sb, options->null_termination ? '\0' : '\n');
|
||||||
fwrite(sb.buf, sb.len, 1, stdout);
|
fwrite(sb.buf, sb.len, 1, stdout);
|
||||||
|
|
20
strbuf.c
20
strbuf.c
|
@ -442,6 +442,26 @@ size_t strbuf_expand_literal(struct strbuf *sb, const char *placeholder)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void strbuf_expand_bad_format(const char *format, const char *command)
|
||||||
|
{
|
||||||
|
const char *end;
|
||||||
|
|
||||||
|
if (*format != '(')
|
||||||
|
/* TRANSLATORS: The first %s is a command like "ls-tree". */
|
||||||
|
die(_("bad %s format: element '%s' does not start with '('"),
|
||||||
|
command, format);
|
||||||
|
|
||||||
|
end = strchr(format + 1, ')');
|
||||||
|
if (!end)
|
||||||
|
/* TRANSLATORS: The first %s is a command like "ls-tree". */
|
||||||
|
die(_("bad %s format: element '%s' does not end in ')'"),
|
||||||
|
command, format);
|
||||||
|
|
||||||
|
/* TRANSLATORS: %s is a command like "ls-tree". */
|
||||||
|
die(_("bad %s format: %%%.*s"),
|
||||||
|
command, (int)(end - format + 1), format);
|
||||||
|
}
|
||||||
|
|
||||||
void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
|
void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
|
||||||
{
|
{
|
||||||
size_t i, len = src->len;
|
size_t i, len = src->len;
|
||||||
|
|
5
strbuf.h
5
strbuf.h
|
@ -337,6 +337,11 @@ size_t strbuf_expand_literal(struct strbuf *sb, const char *placeholder);
|
||||||
*/
|
*/
|
||||||
int strbuf_expand_step(struct strbuf *sb, const char **formatp);
|
int strbuf_expand_step(struct strbuf *sb, const char **formatp);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used with `strbuf_expand_step` to report unknown placeholders.
|
||||||
|
*/
|
||||||
|
void strbuf_expand_bad_format(const char *format, const char *command);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Append the contents of one strbuf to another, quoting any
|
* Append the contents of one strbuf to another, quoting any
|
||||||
* percent signs ("%") into double-percents ("%%") in the
|
* percent signs ("%") into double-percents ("%%") in the
|
||||||
|
|
Loading…
Reference in a new issue