Skip to content

Commit

Permalink
Merge branch 'jk/maint-config-param'
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Junio C Hamano committed Jul 19, 2011
2 parents 20a80d0 + f77bcca commit fe01ef3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
8 changes: 4 additions & 4 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 9 additions & 6 deletions strbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 16 additions & 1 deletion strbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 -----*/
Expand Down
18 changes: 18 additions & 0 deletions t/t1300-repo-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fe01ef3

Please sign in to comment.