diff --git a/config.c b/config.c index 1fc063b256..9340dfe5a8 100644 --- a/config.c +++ b/config.c @@ -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; diff --git a/strbuf.c b/strbuf.c index 09c43ae59a..1a7df12e8f 100644 --- a/strbuf.c +++ b/strbuf.c @@ -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); diff --git a/strbuf.h b/strbuf.h index 9e6d9fa53f..46a33f8c46 100644 --- a/strbuf.h +++ b/strbuf.h @@ -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 -----*/ diff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh index 3db56267ee..3e140c18f4 100755 --- a/t/t1300-repo-config.sh +++ b/t/t1300-repo-config.sh @@ -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