Code

Merge branch 'jk/maint-config-param' into maint
authorJunio C Hamano <gitster@pobox.com>
Mon, 12 Sep 2011 04:53:13 +0000 (21:53 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 12 Sep 2011 04:53:13 +0000 (21:53 -0700)
* 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

config.c
strbuf.c
strbuf.h
t/t1300-repo-config.sh

index 113723bf3b069ed907cd71831ddff91a0ed113da..49e5250dc5384a012dc828d156cbf8686db9d0bb 100644 (file)
--- a/config.c
+++ b/config.c
@@ -42,10 +42,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]);
@@ -856,7 +856,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;
index 09c43ae59a7d4715c26f13d72ca37bc759d1c76d..1a7df12e8f233863cd931a960d453d54050f5584 100644 (file)
--- 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);
index 9e6d9fa53fc04156452bf850af1a9b2d3ba830f2..46a33f8c46985c4377071011d6ea48d6d3fe5331 100644 (file)
--- 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 -----*/
index 3db56267ee89e1b585a548f7bee13386e47395f4..3e140c18f4183c41c76aaab0834f1d23ce7bcd2d 100755 (executable)
@@ -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