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:
Junio C Hamano 2011-07-19 09:45:21 -07:00
Родитель 20a80d04a4 f77bccaeba
Коммит fe01ef31b7
4 изменённых файлов: 47 добавлений и 11 удалений

Просмотреть файл

@ -50,10 +50,10 @@ void git_config_push_parameter(const char *text)
static int git_config_parse_parameter(const char *text,
config_fn_t fn, void *data)
{
struct strbuf tmp = STRBUF_INIT;
struct strbuf **pair;
strbuf_addstr(&tmp, text);
pair = strbuf_split(&tmp, '=');
pair = strbuf_split_str(text, '=', 2);
if (!pair[0])
return error("bogus config parameter: %s", text);
if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=')
strbuf_setlen(pair[0], pair[0]->len - 1);
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)) {
case -1: /* error */
ret--;
die("unable to parse command-line config");
break;
case 0: /* found nothing */
break;

Просмотреть файл

@ -103,24 +103,27 @@ void strbuf_ltrim(struct strbuf *sb)
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;
char *n, *p;
const char *n, *p;
struct strbuf **ret;
struct strbuf *t;
ret = xcalloc(alloc, sizeof(struct strbuf *));
p = n = sb->buf;
while (n < sb->buf + sb->len) {
p = n = str;
while (n < str + slen) {
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) {
alloc = alloc * 2;
ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
}
if (!n)
n = sb->buf + sb->len - 1;
n = str + slen - 1;
len = n - p + 1;
t = xmalloc(sizeof(struct strbuf));
strbuf_init(t, len);

Просмотреть файл

@ -44,7 +44,22 @@ extern void strbuf_rtrim(struct strbuf *);
extern void strbuf_ltrim(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 **);
/*----- add data in your buffer -----*/

Просмотреть файл

@ -904,4 +904,22 @@ test_expect_success 'git -c works with aliases of builtins' '
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