Diffstat (limited to 'scan-tree.c')
| -rw-r--r-- | scan-tree.c | 52 |
1 files changed, 25 insertions, 27 deletions
diff --git a/scan-tree.c b/scan-tree.c index c120efe..1cb4e5d 100644 --- a/scan-tree.c +++ b/scan-tree.c @@ -10,7 +10,6 @@ #include "scan-tree.h" #include "configfile.h" #include "html.h" -#include <config.h> /* return 1 if path contains a objects/ directory and a HEAD file */ static int is_git_dir(const char *path) @@ -47,27 +46,25 @@ out: } static struct cgit_repo *repo; +static repo_config_fn config_fn; -static void scan_tree_repo_config(const char *name, const char *value) +static void repo_config(const char *name, const char *value) { - cgit_repo_config(repo, name, value); + config_fn(repo, name, value); } -static int gitconfig_config(const char *key, const char *value, - const __attribute__((unused)) struct config_context *ctx, void *cb) +static int gitconfig_config(const char *key, const char *value, void *cb) { - const char *name; - if (!strcmp(key, "gitweb.owner")) - cgit_repo_config(repo, "owner", value); + config_fn(repo, "owner", value); else if (!strcmp(key, "gitweb.description")) - cgit_repo_config(repo, "desc", value); + config_fn(repo, "desc", value); else if (!strcmp(key, "gitweb.category")) - cgit_repo_config(repo, "section", value); + config_fn(repo, "section", value); else if (!strcmp(key, "gitweb.homepage")) - cgit_repo_config(repo, "homepage", value); - else if (skip_prefix(key, "cgit.", &name)) - cgit_repo_config(repo, name, value); + config_fn(repo, "homepage", value); + else if (starts_with(key, "cgit.")) + config_fn(repo, key + 5, value); return 0; } @@ -79,7 +76,7 @@ static char *xstrrchr(char *s, char *from, int c) return from < s ? NULL : from; } -static void add_repo(const char *base, struct strbuf *path) +static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn) { struct stat st; struct passwd *pwd; @@ -121,6 +118,7 @@ static void add_repo(const char *base, struct strbuf *path) strbuf_setlen(&rel, rel.len - 1); repo = cgit_add_repo(rel.buf); + config_fn = fn; if (ctx.cfg.enable_git_config) { strbuf_addstr(path, "config"); git_config_from_file(gitconfig_config, path->buf, NULL); @@ -133,7 +131,7 @@ static void add_repo(const char *base, struct strbuf *path) strip_suffix_mem(repo->url, &urllen, "/"); repo->url[urllen] = '\0'; } - repo->path = strdup_first_line(path->buf); + repo->path = xstrdup(path->buf); while (!repo->owner) { if ((pwd = getpwuid(st.st_uid)) == NULL) { fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n", @@ -143,13 +141,13 @@ static void add_repo(const char *base, struct strbuf *path) if (pwd->pw_gecos) if ((p = strchr(pwd->pw_gecos, ','))) *p = '\0'; - repo->owner = strdup_first_line(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name); + repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name); } if (repo->desc == cgit_default_repo_desc || !repo->desc) { strbuf_addstr(path, "description"); if (!stat(path->buf, &st)) - read_first_line(path->buf, &repo->desc, &size); + readfile(path->buf, &repo->desc, &size); strbuf_setlen(path, pathlen); } @@ -166,7 +164,7 @@ static void add_repo(const char *base, struct strbuf *path) } if (slash && !n) { *slash = '\0'; - repo->section = strdup_first_line(rel.buf); + repo->section = xstrdup(rel.buf); *slash = '/'; if (starts_with(repo->name, repo->section)) { repo->name += strlen(repo->section); @@ -178,12 +176,12 @@ static void add_repo(const char *base, struct strbuf *path) strbuf_addstr(path, "cgitrc"); if (!stat(path->buf, &st)) - parse_configfile(path->buf, &scan_tree_repo_config); + parse_configfile(path->buf, &repo_config); strbuf_release(&rel); } -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 = opendir(path); struct dirent *ent; @@ -199,12 +197,12 @@ static void scan_path(const char *base, const char *path) strbuf_add(&pathbuf, path, strlen(path)); if (is_git_dir(pathbuf.buf)) { - add_repo(base, &pathbuf); + add_repo(base, &pathbuf, fn); goto end; } strbuf_addstr(&pathbuf, "/.git"); if (is_git_dir(pathbuf.buf)) { - add_repo(base, &pathbuf); + add_repo(base, &pathbuf, fn); goto end; } /* @@ -229,14 +227,14 @@ static void scan_path(const char *base, const char *path) continue; } if (S_ISDIR(st.st_mode)) - scan_path(base, pathbuf.buf); + scan_path(base, pathbuf.buf, fn); } end: strbuf_release(&pathbuf); closedir(dir); } -void scan_projects(const char *path, const char *projectsfile) +void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn) { struct strbuf line = STRBUF_INIT; FILE *projects; @@ -253,7 +251,7 @@ void scan_projects(const char *path, const char *projectsfile) continue; strbuf_insert(&line, 0, "/", 1); strbuf_insert(&line, 0, path, strlen(path)); - scan_path(path, line.buf); + scan_path(path, line.buf, fn); } if ((err = ferror(projects))) { fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n", @@ -263,7 +261,7 @@ void scan_projects(const char *path, const char *projectsfile) strbuf_release(&line); } -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); } |