cgit

commit 521e10c884055c800078e6dada97ccf6c5193aad

Author: Jason A. Donenfeld <Jason@zx2c4.com>

scan-tree: Unify gitweb.* and cgit.* settings into one config option.

After some back and forth with Jamie and René, it looks like the git
config semantics are going to be like this:

- gitweb.category maps to the cgit repo config key "section"
- gitweb.description maps to the cgit repo config key "desc"
- gitweb.owner maps to the cgit repo config key "owner"
- cgit.* maps to all cgit repo config keys

This option can be enabled with "enable-git-config=1", and replaces
all previous "enable-gitweb-*" config keys.

The order of operations is as follows:

- git config settings are applied in the order that they exist in
  the git config file
- if the owner is not set from git config, get the owner using the
  usual getpwuid call
- if the description is not set from git config, look inside the
  static $path/description file
- if section-from-path=1, override whatever previous settings were
  inside of git config using the section-from-path logic
- parse $path/cgitrc for local repo.* settings, that override all
  previous settings

 cgit.c | 10 +++-------
 cgit.h | 5 ++---
 cgitrc.5.txt | 28 ++++++++++------------------
 scan-tree.c | 48 +++++++++++++++++++-----------------------------
 shared.c | 3 ++-


diff --git a/cgit.c b/cgit.c
index a8dda23a38836b788ddd5a82ca5af62972cbbe48..d699cb6c22ddb2ba86459332deebbc443b8304bd 100644
--- a/cgit.c
+++ b/cgit.c
@@ -163,10 +163,6 @@ 	else if (!strcmp(name, "snapshots"))
 		ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
 	else if (!strcmp(name, "enable-filter-overrides"))
 		ctx.cfg.enable_filter_overrides = atoi(value);
-	else if (!strcmp(name, "enable-gitweb-desc"))
-		ctx.cfg.enable_gitweb_desc = atoi(value);
-	else if (!strcmp(name, "enable-gitweb-owner"))
-		ctx.cfg.enable_gitweb_owner = atoi(value);
 	else if (!strcmp(name, "enable-http-clone"))
 		ctx.cfg.enable_http_clone = atoi(value);
 	else if (!strcmp(name, "enable-index-links"))
@@ -183,6 +179,8 @@ 	else if (!strcmp(name, "enable-subject-links"))
 		ctx.cfg.enable_subject_links = atoi(value);
 	else if (!strcmp(name, "enable-tree-linenumbers"))
 		ctx.cfg.enable_tree_linenumbers = atoi(value);
+	else if (!strcmp(name, "enable-git-config"))
+		ctx.cfg.enable_git_config = atoi(value);
 	else if (!strcmp(name, "max-stats"))
 		ctx.cfg.max_stats = cgit_find_stats_period(value, NULL);
 	else if (!strcmp(name, "cache-size"))
@@ -343,11 +341,9 @@ 	ctx->cfg.case_sensitive_sort = 1;
 	ctx->cfg.css = "/cgit.css";
 	ctx->cfg.logo = "/cgit.png";
 	ctx->cfg.local_time = 0;
-	ctx->cfg.enable_gitweb_desc = 1;
-	ctx->cfg.enable_gitweb_owner = 1;
-	ctx->cfg.enable_gitweb_section = 1;
 	ctx->cfg.enable_http_clone = 1;
 	ctx->cfg.enable_tree_linenumbers = 1;
+	ctx->cfg.enable_git_config = 0;
 	ctx->cfg.max_repo_count = 50;
 	ctx->cfg.max_commit_count = 50;
 	ctx->cfg.max_lock_attempts = 5;




diff --git a/cgit.h b/cgit.h
index 8846e889e5acab101b70dc87155b8f5c69a28825..27420589bbba3863f0585696f83be06bf7c9dbae 100644
--- a/cgit.h
+++ b/cgit.h
@@ -200,9 +200,6 @@ 	int cache_static_ttl;
 	int case_sensitive_sort;
 	int embedded;
 	int enable_filter_overrides;
-	int enable_gitweb_owner;
-	int enable_gitweb_desc;
-	int enable_gitweb_section;
 	int enable_http_clone;
 	int enable_index_links;
 	int enable_commit_graph;
@@ -211,6 +208,7 @@ 	int enable_log_linecount;
 	int enable_remote_branches;
 	int enable_subject_links;
 	int enable_tree_linenumbers;
+	int enable_git_config;
 	int local_time;
 	int max_atom_items;
 	int max_repo_count;
@@ -285,6 +283,7 @@ extern struct cgit_repolist cgit_repolist;
 extern struct cgit_context ctx;
 extern const struct cgit_snapshot_format cgit_snapshot_formats[];
 
+extern char *cgit_default_repo_desc;
 extern struct cgit_repo *cgit_add_repo(const char *url);
 extern struct cgit_repo *cgit_get_repoinfo(const char *url);
 extern void cgit_repo_config_cb(const char *name, const char *value);




diff --git a/cgitrc.5.txt b/cgitrc.5.txt
index 7e1a93a26dfb6d87f3054f07585ffd0e8b57c3fd..278de90cfb5c6aba55d409b2d635abe653725384 100644
--- a/cgitrc.5.txt
+++ b/cgitrc.5.txt
@@ -110,24 +110,6 @@ enable-filter-overrides::
 	Flag which, when set to "1", allows all filter settings to be
 	overridden in repository-specific cgitrc files. Default value: none.
 
-enable-gitweb-desc::
-	If set to "1" and scan-path is enabled, we first check each repository
-	for the git config value "gitweb.description" to determine the owner.
-	Otherwise, the description is read from a file titled "description"
-	inside of the repository directory.
-	Default value: "1". See also: scan-path.
-
-enable-gitweb-owner::
-	If set to "1" and scan-path is enabled, we first check each repository
-	for the git config value "gitweb.owner" to determine the owner.
-	Default value: "1". See also: scan-path.
-
-enable-gitweb-section::
-	If set to "1" and scan-path is enabled, we first check each repository
-	for the git config value "gitweb.category" to determine the repository's
-	section. This value is overridden if section-from-path is enabled.
-	Default value: "1". See also: scan-path section-from-path.
-
 enable-http-clone::
 	If set to "1", cgit will act as an dumb HTTP endpoint for git clones.
 	If you use an alternate way of serving git repositories, you may wish
@@ -162,6 +144,15 @@
 enable-tree-linenumbers::
 	Flag which, when set to "1", will make cgit generate linenumber links
 	for plaintext blobs printed in the tree view. Default value: "1".
+
+enable-git-config::
+	Flag which, when set to "1", will allow cgit to use git config to set
+	any repo specific settings. This option is used in conjunction with
+	"scan-path" to augment _repo._ settings. The keys gitweb.owner,
+	gitweb.category, and gitweb.description will map to the cgit keys
+	repo.owner, repo.section, and repo.desc, respectivly. All git config
+	keys that begin with "cgit." will be mapped to the corresponding "repo."
+	key in cgit. Default value: "0". See also: scan-path section-from-path.
 
 favicon::
 	Url used as link to a shortcut icon for cgit. If specified, it is
@@ -393,6 +384,7 @@ 	'/cgit/tree/README' as opposed to '?r=cgit&p=tree&path=README'. Default
 	value: none.
 	NOTE: cgit has recently learned how to use PATH_INFO to achieve the
 	same kind of virtual urls, so this option will probably be deprecated.
+
 
 REPOSITORY SETTINGS
 -------------------




diff --git a/scan-tree.c b/scan-tree.c
index 6d1941e2f38a58c60160520d760ceb2431b1542b..6ce80365c5443af1eb954dc336381f0b6afce98f 100644
--- a/scan-tree.c
+++ b/scan-tree.c
@@ -47,27 +47,25 @@ }
 
 struct cgit_repo *repo;
 repo_config_fn config_fn;
-char *owner;
-char *desc;
-char *section;
 
 static void repo_config(const char *name, const char *value)
 {
 	config_fn(repo, name, value);
 }
 
-static int gitweb_config(const char *key, const char *value, void *cb)
+static int gitconfig_config(const char *key, const char *value, void *cb)
 {
-	if (ctx.cfg.enable_gitweb_owner && !strcmp(key, "gitweb.owner"))
-		owner = xstrdup(value);
-	else if (ctx.cfg.enable_gitweb_desc && !strcmp(key, "gitweb.description"))
-		desc = xstrdup(value);
-	else if (ctx.cfg.enable_gitweb_section && !strcmp(key, "gitweb.category"))
-		section = xstrdup(value);
+	if (!strcmp(key, "gitweb.owner"))
+		config_fn(repo, "owner", value);
+	else if (!strcmp(key, "gitweb.description"))
+		config_fn(repo, "desc", value);
+	else if (!strcmp(key, "gitweb.category"))
+		config_fn(repo, "section", value);
+	else if (!prefixcmp(key, "cgit."))
+		config_fn(repo, key + 5, value);
+
 	return 0;
 }
-
-
 
 static char *xstrrchr(char *s, char *from, int c)
 {
@@ -96,11 +94,6 @@
 	if (!stat(fmt("%s/noweb", path), &st))
 		return;
 
-	owner = NULL;
-	desc = NULL;
-	section = NULL;
-	git_config_from_file(gitweb_config, fmt("%s/config", path), NULL);
-	
 	if (base == path)
 		rel = xstrdup(fmt("%s", path));
 	else
@@ -110,12 +103,15 @@ 	if (!strcmp(rel + strlen(rel) - 5, "/.git"))
 		rel[strlen(rel) - 5] = '\0';
 
 	repo = cgit_add_repo(rel);
+	config_fn = fn;
+	if (ctx.cfg.enable_git_config)
+		git_config_from_file(gitconfig_config, fmt("%s/config", path), NULL);
+	
 	if (ctx.cfg.remove_suffix)
 		if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
 			*p = '\0';
-	repo->name = repo->url;
 	repo->path = xstrdup(path);
-	while (!owner) {
+	while (!repo->owner) {
 		if ((pwd = getpwuid(st.st_uid)) == NULL) {
 			fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
 				path, strerror(errno), errno);
@@ -124,13 +120,10 @@ 		}
 		if (pwd->pw_gecos)
 			if ((p = strchr(pwd->pw_gecos, ',')))
 				*p = '\0';
-		owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
+		repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
 	}
-	repo->owner = owner;
 
-	if (desc)
-		repo->desc = desc;
-	else {
+	if (repo->desc == cgit_default_repo_desc || !repo->desc) {
 		p = fmt("%s/description", path);
 		if (!stat(p, &st))
 			readfile(p, &repo->desc, &size);
@@ -141,8 +134,6 @@ 		p = fmt("%s/README.html", path);
 		if (!stat(p, &st))
 			repo->readme = "README.html";
 	}
-	if (section)
-		repo->section = section;
 	if (ctx.cfg.section_from_path) {
 		n  = ctx.cfg.section_from_path;
 		if (n > 0) {
@@ -167,10 +158,9 @@ 		}
 	}
 
 	p = fmt("%s/cgitrc", path);
-	if (!stat(p, &st)) {
-		config_fn = fn;
+	if (!stat(p, &st))
 		parse_configfile(xstrdup(p), &repo_config);
-	}
+
 
 	free(rel);
 }




diff --git a/shared.c b/shared.c
index 0a0e22e7255b0db8b9446c496fa85b58d46e24aa..3e20937a0ce7dbc568742a5d9cef8502d7dde406 100644
--- a/shared.c
+++ b/shared.c
@@ -33,6 +33,7 @@ 	    	die("%s: %s",msg, strerror(errno));
 	return result;
 }
 
+char *cgit_default_repo_desc = "[no description]";
 struct cgit_repo *cgit_add_repo(const char *url)
 {
 	struct cgit_repo *ret;
@@ -52,7 +53,7 @@ 	memset(ret, 0, sizeof(struct cgit_repo));
 	ret->url = trim_end(url, '/');
 	ret->name = ret->url;
 	ret->path = NULL;
-	ret->desc = "[no description]";
+	ret->desc = cgit_default_repo_desc;
 	ret->owner = NULL;
 	ret->section = ctx.cfg.section;
 	ret->snapshots = ctx.cfg.snapshots;