cgit

commit 74061ed5f03e72796450aa3b8ca1cf6ced5d59e2

Author: Lars Hjemli <hjemli@gmail.com>

Add support for repo-local cgitrc file

When recursively scanning a directory tree looking for git repositories,
cgit will now parse cgitrc files found within such repositories.

The repo-specific config files can include any repo-specific options
except 'repo.url' and 'repo.path'. Also, in such config files the 'repo.'
prefix can not be used, i.e. the valid options then becomes:
* name
* clone-url
* desc
* ower
* defbranch
* snapshots
* enable-log-filecount
* enable-log-linecount
* max-stats
* module-link
* section
* about-filter
* commit-filter
* source-filter
* readme

Signed-off-by: Lars Hjemli <hjemli@gmail.com>

 cgit.c | 8 ++++----
 cgit.h | 3 +++
 cgitrc.5.txt | 9 +++++++++
 scan-tree.c | 30 ++++++++++++++++++++++--------
 scan-tree.h | 2 +-


diff --git a/cgit.c b/cgit.c
index 90ae124d2713591500045d6334c5e2185bec4973..e281aa9e224aca176db47ae4ddd4cdc7d9422b64 100644
--- a/cgit.c
+++ b/cgit.c
@@ -168,7 +168,7 @@ 	else if (!strcmp(name, "scan-path"))
 		if (!ctx.cfg.nocache && ctx.cfg.cache_size)
 			process_cached_repolist(value);
 		else
-			scan_tree(value);
+			scan_tree(value, repo_config);
 	else if (!strcmp(name, "source-filter"))
 		ctx.cfg.source_filter = new_filter(value, 1);
 	else if (!strcmp(name, "summary-log"))
@@ -476,7 +476,7 @@ 				locked_rc, strerror(errno), errno);
 		return errno;
 	}
 	idx = cgit_repolist.count;
-	scan_tree(path);
+	scan_tree(path, repo_config);
 	print_repolist(f, &cgit_repolist, idx);
 	if (rename(locked_rc, cached_rc))
 		fprintf(stderr, "[cgit] Error renaming %s to %s: %s (%d)\n",
@@ -500,7 +500,7 @@ 		 * if we fail to generate a cached repolist, we need to
 		 * invoke scan_tree manually.
 		 */
 		if (generate_cached_repolist(path, cached_rc))
-			scan_tree(path);
+			scan_tree(path, repo_config);
 		return;
 	}
 
@@ -559,7 +559,7 @@ 		}
 		if (!strncmp(argv[i], "--scan-tree=", 12) ||
 		    !strncmp(argv[i], "--scan-path=", 12)) {
 			scan++;
-			scan_tree(argv[i] + 12);
+			scan_tree(argv[i] + 12, repo_config);
 		}
 	}
 	if (scan) {




diff --git a/cgit.h b/cgit.h
index fc7c7d59358ea89a5d3b3afe6a8309d7c045eab5..3359be94dac71a6ea64f3844a3a6b296ae96b058 100644
--- a/cgit.h
+++ b/cgit.h
@@ -79,6 +79,9 @@ 	struct cgit_filter *commit_filter;
 	struct cgit_filter *source_filter;
 };
 
+typedef void (*repo_config_fn)(struct cgit_repo *repo, const char *name,
+	      const char *value);
+
 struct cgit_repolist {
 	int length;
 	int count;




diff --git a/cgitrc.5.txt b/cgitrc.5.txt
index e99c9f7b1f9765341419c17b621b98ebb5f8ca7b..df494aa5fbb66a57a5cecfe7a547a45e0ca53c21 100644
--- a/cgitrc.5.txt
+++ b/cgitrc.5.txt
@@ -326,6 +326,15 @@ 	The relative url used to access the repository. This must be the first
 	setting specified for each repo. Default value: none.
 
 
+REPOSITORY-SPECIFIC CGITRC FILE
+-------------------------------
+When the option 'scan-path' is used to auto-discover git repositories, cgit
+will try to parse the file 'cgitrc' within any found repository. Such a repo-
+specific config file may contain any of the repo-specific options described
+above, except 'repo.url' and 'repo.path'. Also, in a repo-specific config
+file, the 'repo.' prefix is dropped from the config option names.
+
+
 EXAMPLE CGITRC FILE
 -------------------
 




diff --git a/scan-tree.c b/scan-tree.c
index 67f4550ef8e0eec247527073c4fe7f670ba5f733..dbca7971105ae17732267822abe7049e98f2f523 100644
--- a/scan-tree.c
+++ b/scan-tree.c
@@ -1,4 +1,5 @@
 #include "cgit.h"
+#include "configfile.h"
 #include "html.h"
 
 #define MAX_PATH 4096
@@ -35,9 +36,16 @@
 	return 1;
 }
 
-static void add_repo(const char *base, const char *path)
+struct cgit_repo *repo;
+repo_config_fn config_fn;
+
+static void repo_config(const char *name, const char *value)
+{
+	config_fn(repo, name, value);
+}
+
+static void add_repo(const char *base, const char *path, repo_config_fn fn)
 {
-	struct cgit_repo *repo;
 	struct stat st;
 	struct passwd *pwd;
 	char *p;
@@ -76,9 +84,15 @@
 	p = fmt("%s/README.html", path);
 	if (!stat(p, &st))
 		repo->readme = "README.html";
+
+	p = fmt("%s/cgitrc", path);
+	if (!stat(p, &st)) {
+		config_fn = fn;
+		parse_configfile(xstrdup(p), &repo_config);
+	}
 }
 
-static void scan_path(const char *base, const char *path)
+static void scan_path(const char *base, const char *path, repo_config_fn fn)
 {
 	DIR *dir;
 	struct dirent *ent;
@@ -86,11 +100,11 @@ 	char *buf;
 	struct stat st;
 
 	if (is_git_dir(path)) {
-		add_repo(base, path);
+		add_repo(base, path, fn);
 		return;
 	}
 	if (is_git_dir(fmt("%s/.git", path))) {
-		add_repo(base, fmt("%s/.git", path));
+		add_repo(base, fmt("%s/.git", path), fn);
 		return;
 	}
 	dir = opendir(path);
@@ -120,13 +134,13 @@ 			free(buf);
 			continue;
 		}
 		if (S_ISDIR(st.st_mode))
-			scan_path(base, buf);
+			scan_path(base, buf, fn);
 		free(buf);
 	}
 	closedir(dir);
 }
 
-void scan_tree(const char *path)
+void scan_tree(const char *path, repo_config_fn fn)
 {
-	scan_path(path, path);
+	scan_path(path, path, fn);
 }




diff --git a/scan-tree.h b/scan-tree.h
index b103b16470df9b74394a1177747b86b73e87b168..11539f4c8116946a5d037fea62756b96e2446462 100644
--- a/scan-tree.h
+++ b/scan-tree.h
@@ -1,3 +1,3 @@
 
 
-extern void scan_tree(const char *path);
+extern void scan_tree(const char *path, repo_config_fn fn);