get_ref_dir(): return early if directory cannot be read
[gitweb.git] / config.c
index 531d4d43e7de4780eb03c6a708fc942dc2492c8e..68d32940f3f8f1512fa36702beb98c2457ca5d46 100644 (file)
--- a/config.c
+++ b/config.c
@@ -26,7 +26,68 @@ static config_file *cf;
 
 static int zlib_compression_seen;
 
-const char *config_exclusive_filename = NULL;
+#define MAX_INCLUDE_DEPTH 10
+static const char include_depth_advice[] =
+"exceeded maximum include depth (%d) while including\n"
+"      %s\n"
+"from\n"
+"      %s\n"
+"Do you have circular includes?";
+static int handle_path_include(const char *path, struct config_include_data *inc)
+{
+       int ret = 0;
+       struct strbuf buf = STRBUF_INIT;
+
+       /*
+        * Use an absolute path as-is, but interpret relative paths
+        * based on the including config file.
+        */
+       if (!is_absolute_path(path)) {
+               char *slash;
+
+               if (!cf || !cf->name)
+                       return error("relative config includes must come from files");
+
+               slash = find_last_dir_sep(cf->name);
+               if (slash)
+                       strbuf_add(&buf, cf->name, slash - cf->name + 1);
+               strbuf_addstr(&buf, path);
+               path = buf.buf;
+       }
+
+       if (!access(path, R_OK)) {
+               if (++inc->depth > MAX_INCLUDE_DEPTH)
+                       die(include_depth_advice, MAX_INCLUDE_DEPTH, path,
+                           cf && cf->name ? cf->name : "the command line");
+               ret = git_config_from_file(git_config_include, path, inc);
+               inc->depth--;
+       }
+       strbuf_release(&buf);
+       return ret;
+}
+
+int git_config_include(const char *var, const char *value, void *data)
+{
+       struct config_include_data *inc = data;
+       const char *type;
+       int ret;
+
+       /*
+        * Pass along all values, including "include" directives; this makes it
+        * possible to query information on the includes themselves.
+        */
+       ret = inc->fn(var, value, inc->data);
+       if (ret < 0)
+               return ret;
+
+       type = skip_prefix(var, "include.");
+       if (!type)
+               return ret;
+
+       if (!strcmp(type, "path"))
+               ret = handle_path_include(value, inc);
+       return ret;
+}
 
 static void lowercase(char *p)
 {
@@ -135,8 +196,10 @@ static char *parse_value(void)
        for (;;) {
                int c = get_next_char();
                if (c == '\n') {
-                       if (quote)
+                       if (quote) {
+                               cf->linenr--;
                                return NULL;
+                       }
                        return cf->value.buf;
                }
                if (comment)
@@ -226,7 +289,7 @@ static int get_extended_base_var(char *name, int baselen, int c)
 {
        do {
                if (c == '\n')
-                       return -1;
+                       goto error_incomplete_line;
                c = get_next_char();
        } while (isspace(c));
 
@@ -238,13 +301,13 @@ static int get_extended_base_var(char *name, int baselen, int c)
        for (;;) {
                int c = get_next_char();
                if (c == '\n')
-                       return -1;
+                       goto error_incomplete_line;
                if (c == '"')
                        break;
                if (c == '\\') {
                        c = get_next_char();
                        if (c == '\n')
-                               return -1;
+                               goto error_incomplete_line;
                }
                name[baselen++] = c;
                if (baselen > MAXNAME / 2)
@@ -255,6 +318,9 @@ static int get_extended_base_var(char *name, int baselen, int c)
        if (get_next_char() != ']')
                return -1;
        return baselen;
+error_incomplete_line:
+       cf->linenr--;
+       return -1;
 }
 
 static int get_base_var(char *name)
@@ -915,10 +981,18 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
 }
 
 int git_config_with_options(config_fn_t fn, void *data,
-                           const char *filename)
+                           const char *filename, int respect_includes)
 {
        char *repo_config = NULL;
        int ret;
+       struct config_include_data inc = CONFIG_INCLUDE_INIT;
+
+       if (respect_includes) {
+               inc.fn = fn;
+               inc.data = data;
+               fn = git_config_include;
+               data = &inc;
+       }
 
        /*
         * If we have a specific filename, use it. Otherwise, follow the
@@ -936,7 +1010,7 @@ int git_config_with_options(config_fn_t fn, void *data,
 
 int git_config(config_fn_t fn, void *data)
 {
-       return git_config_with_options(fn, data, config_exclusive_filename);
+       return git_config_with_options(fn, data, NULL, 1);
 }
 
 /*
@@ -1435,8 +1509,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
 int git_config_set_multivar(const char *key, const char *value,
                        const char *value_regex, int multi_replace)
 {
-       return git_config_set_multivar_in_file(config_exclusive_filename,
-                                              key, value, value_regex,
+       return git_config_set_multivar_in_file(NULL, key, value, value_regex,
                                               multi_replace);
 }
 
@@ -1562,8 +1635,7 @@ int git_config_rename_section_in_file(const char *config_filename,
 
 int git_config_rename_section(const char *old_name, const char *new_name)
 {
-       return git_config_rename_section_in_file(config_exclusive_filename,
-                                                old_name, new_name);
+       return git_config_rename_section_in_file(NULL, old_name, new_name);
 }
 
 /*