Merge branch 'jk/maint-config-param'
* jk/maint-config-param: config: use strbuf_split_str instead of a temporary strbuf strbuf: allow strbuf_split to work on non-strbufs config: avoid segfault when parsing command-line config config: die on error in command-line config fix "git -c" parsing of values with equals signs strbuf_split: add a max parameter
This commit is contained in:
commit
fe01ef31b7
8
config.c
8
config.c
@ -50,10 +50,10 @@ void git_config_push_parameter(const char *text)
|
|||||||
static int git_config_parse_parameter(const char *text,
|
static int git_config_parse_parameter(const char *text,
|
||||||
config_fn_t fn, void *data)
|
config_fn_t fn, void *data)
|
||||||
{
|
{
|
||||||
struct strbuf tmp = STRBUF_INIT;
|
|
||||||
struct strbuf **pair;
|
struct strbuf **pair;
|
||||||
strbuf_addstr(&tmp, text);
|
pair = strbuf_split_str(text, '=', 2);
|
||||||
pair = strbuf_split(&tmp, '=');
|
if (!pair[0])
|
||||||
|
return error("bogus config parameter: %s", text);
|
||||||
if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=')
|
if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=')
|
||||||
strbuf_setlen(pair[0], pair[0]->len - 1);
|
strbuf_setlen(pair[0], pair[0]->len - 1);
|
||||||
strbuf_trim(pair[0]);
|
strbuf_trim(pair[0]);
|
||||||
@ -874,7 +874,7 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
|
|||||||
|
|
||||||
switch (git_config_from_parameters(fn, data)) {
|
switch (git_config_from_parameters(fn, data)) {
|
||||||
case -1: /* error */
|
case -1: /* error */
|
||||||
ret--;
|
die("unable to parse command-line config");
|
||||||
break;
|
break;
|
||||||
case 0: /* found nothing */
|
case 0: /* found nothing */
|
||||||
break;
|
break;
|
||||||
|
15
strbuf.c
15
strbuf.c
@ -103,24 +103,27 @@ void strbuf_ltrim(struct strbuf *sb)
|
|||||||
sb->buf[sb->len] = '\0';
|
sb->buf[sb->len] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
|
struct strbuf **strbuf_split_buf(const char *str, size_t slen, int delim, int max)
|
||||||
{
|
{
|
||||||
int alloc = 2, pos = 0;
|
int alloc = 2, pos = 0;
|
||||||
char *n, *p;
|
const char *n, *p;
|
||||||
struct strbuf **ret;
|
struct strbuf **ret;
|
||||||
struct strbuf *t;
|
struct strbuf *t;
|
||||||
|
|
||||||
ret = xcalloc(alloc, sizeof(struct strbuf *));
|
ret = xcalloc(alloc, sizeof(struct strbuf *));
|
||||||
p = n = sb->buf;
|
p = n = str;
|
||||||
while (n < sb->buf + sb->len) {
|
while (n < str + slen) {
|
||||||
int len;
|
int len;
|
||||||
n = memchr(n, delim, sb->len - (n - sb->buf));
|
if (max <= 0 || pos + 1 < max)
|
||||||
|
n = memchr(n, delim, slen - (n - str));
|
||||||
|
else
|
||||||
|
n = NULL;
|
||||||
if (pos + 1 >= alloc) {
|
if (pos + 1 >= alloc) {
|
||||||
alloc = alloc * 2;
|
alloc = alloc * 2;
|
||||||
ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
|
ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
|
||||||
}
|
}
|
||||||
if (!n)
|
if (!n)
|
||||||
n = sb->buf + sb->len - 1;
|
n = str + slen - 1;
|
||||||
len = n - p + 1;
|
len = n - p + 1;
|
||||||
t = xmalloc(sizeof(struct strbuf));
|
t = xmalloc(sizeof(struct strbuf));
|
||||||
strbuf_init(t, len);
|
strbuf_init(t, len);
|
||||||
|
17
strbuf.h
17
strbuf.h
@ -44,7 +44,22 @@ extern void strbuf_rtrim(struct strbuf *);
|
|||||||
extern void strbuf_ltrim(struct strbuf *);
|
extern void strbuf_ltrim(struct strbuf *);
|
||||||
extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
|
extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
|
||||||
|
|
||||||
extern struct strbuf **strbuf_split(const struct strbuf *, int delim);
|
extern struct strbuf **strbuf_split_buf(const char *, size_t,
|
||||||
|
int delim, int max);
|
||||||
|
static inline struct strbuf **strbuf_split_str(const char *str,
|
||||||
|
int delim, int max)
|
||||||
|
{
|
||||||
|
return strbuf_split_buf(str, strlen(str), delim, max);
|
||||||
|
}
|
||||||
|
static inline struct strbuf **strbuf_split_max(const struct strbuf *sb,
|
||||||
|
int delim, int max)
|
||||||
|
{
|
||||||
|
return strbuf_split_buf(sb->buf, sb->len, delim, max);
|
||||||
|
}
|
||||||
|
static inline struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
|
||||||
|
{
|
||||||
|
return strbuf_split_max(sb, delim, 0);
|
||||||
|
}
|
||||||
extern void strbuf_list_free(struct strbuf **);
|
extern void strbuf_list_free(struct strbuf **);
|
||||||
|
|
||||||
/*----- add data in your buffer -----*/
|
/*----- add data in your buffer -----*/
|
||||||
|
@ -904,4 +904,22 @@ test_expect_success 'git -c works with aliases of builtins' '
|
|||||||
test_cmp expect actual
|
test_cmp expect actual
|
||||||
'
|
'
|
||||||
|
|
||||||
|
test_expect_success 'git -c does not split values on equals' '
|
||||||
|
echo "value with = in it" >expect &&
|
||||||
|
git -c core.foo="value with = in it" config core.foo >actual &&
|
||||||
|
test_cmp expect actual
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'git -c dies on bogus config' '
|
||||||
|
test_must_fail git -c core.bare=foo rev-parse
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'git -c complains about empty key' '
|
||||||
|
test_must_fail git -c "=foo" rev-parse
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'git -c complains about empty key and value' '
|
||||||
|
test_must_fail git -c "" rev-parse
|
||||||
|
'
|
||||||
|
|
||||||
test_done
|
test_done
|
||||||
|
Loading…
Reference in New Issue
Block a user