Merge branch 'mm/maint-config-explicit-bool-display'
authorJunio C Hamano <gitster@pobox.com>
Tue, 18 Oct 2011 04:37:12 +0000 (21:37 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 18 Oct 2011 04:37:12 +0000 (21:37 -0700)
* mm/maint-config-explicit-bool-display:
  config: display key_delim for config --bool --get-regexp

1  2 
builtin/config.c
t/t1300-repo-config.sh

diff --combined builtin/config.c
index 0b4ecac855dce9b70878de4d15f4d317254d9ef3,3f5e9d26ab1fcfbce39d01938575a0b7e36e7b9a..0315ad76f881aab9a64084f8d2fdd2019907e4cb
@@@ -52,7 -52,7 +52,7 @@@ static struct option builtin_config_opt
        OPT_BOOLEAN(0, "global", &use_global_config, "use global config file"),
        OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"),
        OPT_BOOLEAN(0, "local", &use_local_config, "use repository config file"),
 -      OPT_STRING('f', "file", &given_config_file, "FILE", "use given config file"),
 +      OPT_STRING('f', "file", &given_config_file, "file", "use given config file"),
        OPT_GROUP("Action"),
        OPT_BIT(0, "get", &actions, "get value: name [value-regex]", ACTION_GET),
        OPT_BIT(0, "get-all", &actions, "get all values: key [value-regex]", ACTION_GET_ALL),
@@@ -99,6 -99,7 +99,7 @@@ static int show_config(const char *key_
        const char *vptr = value;
        int must_free_vptr = 0;
        int dup_error = 0;
+       int must_print_delim = 0;
  
        if (!use_key_regexp && strcmp(key_, key))
                return 0;
                return 0;
  
        if (show_keys) {
-               if (value_)
-                       printf("%s%c", key_, key_delim);
-               else
-                       printf("%s", key_);
+               printf("%s", key_);
+               must_print_delim = 1;
        }
        if (seen && !do_all)
                dup_error = 1;
        } else if (types == TYPE_PATH) {
                git_config_pathname(&vptr, key_, value_);
                must_free_vptr = 1;
+       } else if (value_) {
+               vptr = value_;
+       } else {
+               /* Just show the key name */
+               vptr = "";
+               must_print_delim = 0;
        }
-       else
-               vptr = value_?value_:"";
        seen++;
        if (dup_error) {
                error("More than one value for the key %s: %s",
                                key_, vptr);
        }
-       else
+       else {
+               if (must_print_delim)
+                       printf("%c", key_delim);
                printf("%s%c", vptr, term);
+       }
        if (must_free_vptr)
                /* If vptr must be freed, it's a pointer to a
                 * dynamically allocated buffer, it's safe to cast to
  static int get_value(const char *key_, const char *regex_)
  {
        int ret = -1;
 -      char *tl;
        char *global = NULL, *repo_config = NULL;
        const char *system_wide = NULL, *local;
  
        if (!local) {
                const char *home = getenv("HOME");
                local = repo_config = git_pathdup("config");
 -              if (git_config_global() && home)
 +              if (home)
                        global = xstrdup(mkpath("%s/.gitconfig", home));
                if (git_config_system())
                        system_wide = git_etc_gitconfig();
        }
  
 -      key = xstrdup(key_);
 -      for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
 -              *tl = tolower(*tl);
 -      for (tl=key; *tl && *tl != '.'; ++tl)
 -              *tl = tolower(*tl);
 -
        if (use_key_regexp) {
 +              char *tl;
 +
 +              /*
 +               * NEEDSWORK: this naive pattern lowercasing obviously does not
 +               * work for more complex patterns like "^[^.]*Foo.*bar".
 +               * Perhaps we should deprecate this altogether someday.
 +               */
 +
 +              key = xstrdup(key_);
 +              for (tl = key + strlen(key) - 1;
 +                   tl >= key && *tl != '.';
 +                   tl--)
 +                      *tl = tolower(*tl);
 +              for (tl = key; *tl && *tl != '.'; tl++)
 +                      *tl = tolower(*tl);
 +
                key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
                if (regcomp(key_regexp, key, REG_EXTENDED)) {
                        fprintf(stderr, "Invalid key pattern: %s\n", key_);
 +                      free(key);
                        goto free_strings;
                }
 +      } else {
 +              if (git_config_parse_key(key_, &key, NULL))
 +                      goto free_strings;
        }
  
        if (regex_) {
@@@ -303,18 -296,24 +309,18 @@@ static void get_color(const char *def_c
        fputs(parsed_color, stdout);
  }
  
 -static int stdout_is_tty;
  static int get_colorbool_found;
  static int get_diff_color_found;
 +static int get_color_ui_found;
  static int git_get_colorbool_config(const char *var, const char *value,
                void *cb)
  {
 -      if (!strcmp(var, get_colorbool_slot)) {
 -              get_colorbool_found =
 -                      git_config_colorbool(var, value, stdout_is_tty);
 -      }
 -      if (!strcmp(var, "diff.color")) {
 -              get_diff_color_found =
 -                      git_config_colorbool(var, value, stdout_is_tty);
 -      }
 -      if (!strcmp(var, "color.ui")) {
 -              git_use_color_default = git_config_colorbool(var, value, stdout_is_tty);
 -              return 0;
 -      }
 +      if (!strcmp(var, get_colorbool_slot))
 +              get_colorbool_found = git_config_colorbool(var, value);
 +      else if (!strcmp(var, "diff.color"))
 +              get_diff_color_found = git_config_colorbool(var, value);
 +      else if (!strcmp(var, "color.ui"))
 +              get_color_ui_found = git_config_colorbool(var, value);
        return 0;
  }
  
@@@ -328,11 -327,9 +334,11 @@@ static int get_colorbool(int print
                if (!strcmp(get_colorbool_slot, "color.diff"))
                        get_colorbool_found = get_diff_color_found;
                if (get_colorbool_found < 0)
 -                      get_colorbool_found = git_use_color_default;
 +                      get_colorbool_found = get_color_ui_found;
        }
  
 +      get_colorbool_found = want_color(get_colorbool_found);
 +
        if (print) {
                printf("%s\n", get_colorbool_found ? "true" : "false");
                return 0;
@@@ -432,14 -429,9 +438,14 @@@ int cmd_config(int argc, const char **a
                              NULL, NULL);
        }
        else if (actions == ACTION_SET) {
 +              int ret;
                check_argc(argc, 2, 2);
                value = normalize_value(argv[0], argv[1]);
 -              return git_config_set(argv[0], value);
 +              ret = git_config_set(argv[0], value);
 +              if (ret == CONFIG_NOTHING_SET)
 +                      error("cannot overwrite multiple values with a single value\n"
 +                      "       Use a regexp, --add or --set-all to change %s.", argv[0]);
 +              return ret;
        }
        else if (actions == ACTION_SET_ALL) {
                check_argc(argc, 2, 3);
        }
        else if (actions == ACTION_GET_COLORBOOL) {
                if (argc == 1)
 -                      stdout_is_tty = git_config_bool("command line", argv[0]);
 -              else if (argc == 0)
 -                      stdout_is_tty = isatty(1);
 +                      color_stdout_is_tty = git_config_bool("command line", argv[0]);
                return get_colorbool(argc != 0);
        }
  
        return 0;
  }
 +
 +int cmd_repo_config(int argc, const char **argv, const char *prefix)
 +{
 +      fprintf(stderr, "WARNING: git repo-config is deprecated in favor of git config.\n");
 +      return cmd_config(argc, argv, prefix);
 +}
diff --combined t/t1300-repo-config.sh
index 3e140c18f4183c41c76aaab0834f1d23ce7bcd2d,cce601f0a63ae64ae795a815031623a776d89931..dffccf84f8f67ae8dc13eae4547eb4c7c687de1d
@@@ -333,6 -333,12 +333,12 @@@ test_expect_success 'get-regexp variabl
        'git config --get-regexp novalue > output &&
         cmp output expect'
  
+ echo 'novalue.variable true' > expect
+ test_expect_success 'get-regexp --bool variable with no value' \
+       'git config --bool --get-regexp novalue > output &&
+        cmp output expect'
  echo 'emptyvalue.variable ' > expect
  
  test_expect_success 'get-regexp variable with empty value' \
@@@ -876,50 -882,11 +882,50 @@@ test_expect_success 'check split_cmdlin
        "
  
  test_expect_success 'git -c "key=value" support' '
 -      test "z$(git -c name=value config name)" = zvalue &&
        test "z$(git -c core.name=value config core.name)" = zvalue &&
 -      test "z$(git -c CamelCase=value config camelcase)" = zvalue &&
 -      test "z$(git -c flag config --bool flag)" = ztrue &&
 -      test_must_fail git -c core.name=value config name
 +      test "z$(git -c foo.CamelCase=value config foo.camelcase)" = zvalue &&
 +      test "z$(git -c foo.flag config --bool foo.flag)" = ztrue &&
 +      test_must_fail git -c name=value config core.name
 +'
 +
 +test_expect_success 'key sanity-checking' '
 +      test_must_fail git config foo=bar &&
 +      test_must_fail git config foo=.bar &&
 +      test_must_fail git config foo.ba=r &&
 +      test_must_fail git config foo.1bar &&
 +      test_must_fail git config foo."ba
 +                              z".bar &&
 +      test_must_fail git config . false &&
 +      test_must_fail git config .foo false &&
 +      test_must_fail git config foo. false &&
 +      test_must_fail git config .foo. false &&
 +      git config foo.bar true &&
 +      git config foo."ba =z".bar false
 +'
 +
 +test_expect_success 'git -c works with aliases of builtins' '
 +      git config alias.checkconfig "-c foo.check=bar config foo.check" &&
 +      echo bar >expect &&
 +      git checkconfig >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