repo-config: fix segfault with no argument.
[git.git] / repo-config.c
index b2569b7..e350630 100644 (file)
@@ -2,21 +2,34 @@
 #include <regex.h>
 
 static const char git_config_set_usage[] =
-"git-repo-config [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]]";
+"git-repo-config [ --bool | --int ] [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]] | --list";
 
 static char* key = NULL;
 static char* value = NULL;
-static regex_t* regex = NULL;
+static regex_t* regexp = NULL;
 static int do_all = 0;
 static int do_not_match = 0;
 static int seen = 0;
+static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
+
+static int show_all_config(const char *key_, const char *value_)
+{
+       if (value_)
+               printf("%s=%s\n", key_, value_);
+       else
+               printf("%s\n", key_);
+       return 0;
+}
 
 static int show_config(const char* key_, const char* value_)
 {
+       if (value_ == NULL)
+               value_ = "";
+
        if (!strcmp(key_, key) &&
-                       (regex == NULL ||
+                       (regexp == NULL ||
                         (do_not_match ^
-                         !regexec(regex, value_, 0, NULL, 0)))) {
+                         !regexec(regexp, value_, 0, NULL, 0)))) {
                if (do_all) {
                        printf("%s\n", value_);
                        return 0;
@@ -25,7 +38,17 @@ static int show_config(const char* key_, const char* value_)
                        fprintf(stderr, "More than one value: %s\n", value);
                        free(value);
                }
-               value = strdup(value_);
+
+               if (type == T_INT) {
+                       value = malloc(256);
+                       sprintf(value, "%d", git_config_int(key_, value_));
+               } else if (type == T_BOOL) {
+                       value = malloc(256);
+                       sprintf(value, "%s", git_config_bool(key_, value_)
+                                            ? "true" : "false");
+               } else {
+                       value = strdup(value_);
+               }
                seen++;
        }
        return 0;
@@ -46,22 +69,22 @@ static int get_value(const char* key_, const char* regex_)
                        regex_++;
                }
 
-               regex = (regex_t*)malloc(sizeof(regex_t));
-               if (regcomp(regex, regex_, REG_EXTENDED)) {
+               regexp = (regex_t*)malloc(sizeof(regex_t));
+               if (regcomp(regexp, regex_, REG_EXTENDED)) {
                        fprintf(stderr, "Invalid pattern: %s\n", regex_);
                        return -1;
                }
        }
 
-       i = git_config(show_config);
+       git_config(show_config);
        if (value) {
                printf("%s\n", value);
                free(value);
        }
        free(key);
-       if (regex) {
-               regfree(regex);
-               free(regex);
+       if (regexp) {
+               regfree(regexp);
+               free(regexp);
        }
 
        if (do_all)
@@ -73,6 +96,20 @@ static int get_value(const char* key_, const char* regex_)
 int main(int argc, const char **argv)
 {
        setup_git_directory();
+
+       while (1 < argc) {
+               if (!strcmp(argv[1], "--int"))
+                       type = T_INT;
+               else if (!strcmp(argv[1], "--bool"))
+                       type = T_BOOL;
+               else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
+                       return git_config(show_all_config);
+               else
+                       break;
+               argc--;
+               argv++;
+       }
+
        switch (argc) {
        case 2:
                return get_value(argv[1], NULL);