config: add support for --bool and --int while setting values
authorFrank Lichtenheld <frank@lichtenheld.de>
Mon, 25 Jun 2007 14:00:24 +0000 (16:00 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 27 Jun 2007 06:16:17 +0000 (23:16 -0700)
Signed-off-by: Frank Lichtenheld <frank@lichtenheld.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-config.txt
builtin-config.c
t/t1300-repo-config.sh
t/t9400-git-cvsserver-server.sh
index a44578166487d57ef8a17f687e27e5939c4bdc7a..5f66a7fcd5907b26ff6bdf94b53c2619c1eff509 100644 (file)
@@ -9,9 +9,9 @@ git-config - Get and set repository or global options
 SYNOPSIS
 --------
 [verse]
-'git-config' [--system | --global] [-z|--null] name [value [value_regex]]
-'git-config' [--system | --global] --add name value
-'git-config' [--system | --global] --replace-all name [value [value_regex]]
+'git-config' [--system | --global] [type] [-z|--null] name [value [value_regex]]
+'git-config' [--system | --global] [type] --add name value
+'git-config' [--system | --global] [type] --replace-all name [value [value_regex]]
 'git-config' [--system | --global] [type] [-z|--null] --get name [value_regex]
 'git-config' [--system | --global] [type] [-z|--null] --get-all name [value_regex]
 'git-config' [--system | --global] [type] [-z|--null] --get-regexp name_regex [value_regex]
@@ -37,8 +37,7 @@ prepend a single exclamation mark in front (see also <<EXAMPLES>>).
 The type specifier can be either '--int' or '--bool', which will make
 'git-config' ensure that the variable(s) are of the given type and
 convert the value to the canonical form (simple decimal number for int,
-a "true" or "false" string for bool).  Type specifiers currently only
-take effect for reading operations.  If no type specifier is passed,
+a "true" or "false" string for bool).  If no type specifier is passed,
 no checks or transformations are performed on the value.
 
 This command will fail if:
index b96c9aa74284a0c3435554312f65d880fce9e70f..3f7cab16d53718844a3964653a59c0feda730ad0 100644 (file)
@@ -138,9 +138,33 @@ static int get_value(const char* key_, const char* regex_)
        return ret;
 }
 
+char *normalize_value(const char *key, const char *value)
+{
+       char *normalized;
+
+       if (!value)
+               return NULL;
+
+       if (type == T_RAW)
+               normalized = xstrdup(value);
+       else {
+               normalized = xmalloc(64);
+               if (type == T_INT) {
+                       int v = git_config_int(key, value);
+                       sprintf(normalized, "%d", v);
+               }
+               else if (type == T_BOOL)
+                       sprintf(normalized, "%s",
+                               git_config_bool(key, value) ? "true" : "false");
+       }
+
+       return normalized;
+}
+
 int cmd_config(int argc, const char **argv, const char *prefix)
 {
        int nongit = 0;
+       char* value;
        setup_git_directory_gently(&nongit);
 
        while (1 < argc) {
@@ -217,9 +241,10 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                        use_key_regexp = 1;
                        do_all = 1;
                        return get_value(argv[2], NULL);
-               } else
-
-                       return git_config_set(argv[1], argv[2]);
+               } else {
+                       value = normalize_value(argv[1], argv[2]);
+                       return git_config_set(argv[1], value);
+               }
        case 4:
                if (!strcmp(argv[1], "--unset"))
                        return git_config_set_multivar(argv[2], NULL, argv[3], 0);
@@ -235,17 +260,21 @@ int cmd_config(int argc, const char **argv, const char *prefix)
                        use_key_regexp = 1;
                        do_all = 1;
                        return get_value(argv[2], argv[3]);
-               } else if (!strcmp(argv[1], "--add"))
-                       return git_config_set_multivar(argv[2], argv[3], "^$", 0);
-               else if (!strcmp(argv[1], "--replace-all"))
-
-                       return git_config_set_multivar(argv[2], argv[3], NULL, 1);
-               else
-
-                       return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
+               } else if (!strcmp(argv[1], "--add")) {
+                       value = normalize_value(argv[2], argv[3]);
+                       return git_config_set_multivar(argv[2], value, "^$", 0);
+               } else if (!strcmp(argv[1], "--replace-all")) {
+                       value = normalize_value(argv[2], argv[3]);
+                       return git_config_set_multivar(argv[2], value, NULL, 1);
+               } else {
+                       value = normalize_value(argv[1], argv[2]);
+                       return git_config_set_multivar(argv[1], value, argv[3], 0);
+               }
        case 5:
-               if (!strcmp(argv[1], "--replace-all"))
-                       return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
+               if (!strcmp(argv[1], "--replace-all")) {
+                       value = normalize_value(argv[2], argv[3]);
+                       return git_config_set_multivar(argv[2], value, argv[4], 1);
+               }
        case 1:
        default:
                usage(git_config_set_usage);
index 7a77bef4c0e7c24d263c87d59327bff1b8443aef..9443b875e2d802f05ce384dc8a230bce56ea1bcd 100755 (executable)
@@ -471,11 +471,57 @@ test_expect_success bool '
         done &&
        cmp expect result'
 
-test_expect_failure 'invalid bool' '
+test_expect_failure 'invalid bool (--get)' '
 
        git-config bool.nobool foobar &&
        git-config --bool --get bool.nobool'
 
+test_expect_failure 'invalid bool (set)' '
+
+       git-config --bool bool.nobool foobar'
+
+rm .git/config
+
+cat > expect <<\EOF
+[bool]
+       true1 = true
+       true2 = true
+       true3 = true
+       true4 = true
+       false1 = false
+       false2 = false
+       false3 = false
+       false4 = false
+EOF
+
+test_expect_success 'set --bool' '
+
+       git-config --bool bool.true1 01 &&
+       git-config --bool bool.true2 -1 &&
+       git-config --bool bool.true3 YeS &&
+       git-config --bool bool.true4 true &&
+       git-config --bool bool.false1 000 &&
+       git-config --bool bool.false2 "" &&
+       git-config --bool bool.false3 nO &&
+       git-config --bool bool.false4 FALSE &&
+       cmp expect .git/config'
+
+rm .git/config
+
+cat > expect <<\EOF
+[int]
+       val1 = 1
+       val2 = -1
+       val3 = 5242880
+EOF
+
+test_expect_success 'set --int' '
+
+       git-config --int int.val1 01 &&
+       git-config --int int.val2 -1 &&
+       git-config --int int.val3 5m &&
+       cmp expect .git/config'
+
 rm .git/config
 
 git-config quote.leading " test"
index 033177068670199aa8b5efc2bd970f8426b2f13e..641303e0a18c1c9958e4dae86d1fcdee644dbcff 100755 (executable)
@@ -38,7 +38,7 @@ echo >empty &&
   git commit -q -m "First Commit" &&
   git clone -q --local --bare "$WORKDIR/.git" "$SERVERDIR" >/dev/null 2>&1 &&
   GIT_DIR="$SERVERDIR" git config --bool gitcvs.enabled true &&
-  GIT_DIR="$SERVERDIR" git config --bool gitcvs.logfile "$SERVERDIR/gitcvs.log" ||
+  GIT_DIR="$SERVERDIR" git config gitcvs.logfile "$SERVERDIR/gitcvs.log" ||
   exit 1
 
 # note that cvs doesn't accept absolute pathnames
@@ -255,7 +255,7 @@ rm -fr "$SERVERDIR"
 cd "$WORKDIR" &&
 git clone -q --local --bare "$WORKDIR/.git" "$SERVERDIR" >/dev/null 2>&1 &&
 GIT_DIR="$SERVERDIR" git config --bool gitcvs.enabled true &&
-GIT_DIR="$SERVERDIR" git config --bool gitcvs.logfile "$SERVERDIR/gitcvs.log" ||
+GIT_DIR="$SERVERDIR" git config gitcvs.logfile "$SERVERDIR/gitcvs.log" ||
 exit 1
 
 test_expect_success 'cvs update (create new file)' \