aboutsummaryrefslogtreecommitdiff
path: root/scan-tree.c
diff refs
from: back
to: back
| flip
diff options
context:
space:
mode:
Diffstat (limited to 'scan-tree.c')
-rw-r--r--scan-tree.c92
1 files changed, 46 insertions, 46 deletions
diff --git a/scan-tree.c b/scan-tree.c
index c120efe..beb584b 100644
--- a/scan-tree.c
+++ b/scan-tree.c
@@ -1,6 +1,7 @@
/* scan-tree.c
- *
- * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
+ *
+ * Copyright (C) 2008-2009 Lars Hjemli
+ * Copyright (C) 2010, 2012 Jason A. Donenfeld <Jason@zx2c4.com>
*
* Licensed under GNU General Public License v2
* (see COPYING for full license text)
@@ -10,7 +11,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)
@@ -46,28 +46,24 @@ out:
return result;
}
-static struct cgit_repo *repo;
+struct cgit_repo *repo;
+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);
- 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, "section", value);
+ else if (!prefixcmp(key, "cgit."))
+ config_fn(repo, key + 5, value);
return 0;
}
@@ -79,7 +75,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;
@@ -110,30 +106,26 @@ static void add_repo(const char *base, struct strbuf *path)
return;
strbuf_setlen(path, pathlen);
- if (!starts_with(path->buf, base))
+ if (strncmp(base, path->buf, strlen(base)))
strbuf_addbuf(&rel, path);
else
strbuf_addstr(&rel, path->buf + strlen(base) + 1);
if (!strcmp(rel.buf + rel.len - 5, "/.git"))
strbuf_setlen(&rel, rel.len - 5);
- else if (rel.len && rel.buf[rel.len - 1] == '/')
- 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);
strbuf_setlen(path, pathlen);
}
- if (ctx.cfg.remove_suffix) {
- size_t urllen;
- strip_suffix(repo->url, ".git", &urllen);
- strip_suffix_mem(repo->url, &urllen, "/");
- repo->url[urllen] = '\0';
- }
- repo->path = strdup_first_line(path->buf);
+ if (ctx.cfg.remove_suffix)
+ if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
+ *p = '\0';
+ 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,32 +135,38 @@ 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);
}
+ if (!repo->readme) {
+ strbuf_addstr(path, "README.html");
+ if (!stat(path->buf, &st))
+ repo->readme = "README.html";
+ strbuf_setlen(path, pathlen);
+ }
if (ctx.cfg.section_from_path) {
- n = ctx.cfg.section_from_path;
+ n = ctx.cfg.section_from_path;
if (n > 0) {
- slash = rel.buf - 1;
- while (slash && n && (slash = strchr(slash + 1, '/')))
+ slash = rel.buf;
+ while (slash && n && (slash = strchr(slash, '/')))
n--;
} else {
slash = rel.buf + rel.len;
- while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/')))
+ while (slash && n && (slash = xstrrchr(rel.buf, slash, '/')))
n++;
}
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)) {
+ if (!prefixcmp(repo->name, repo->section)) {
repo->name += strlen(repo->section);
if (*repo->name == '/')
repo->name++;
@@ -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(xstrdup(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,16 @@ 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)
+#define lastc(s) s[strlen(s) - 1]
+
+void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
{
struct strbuf line = STRBUF_INIT;
FILE *projects;
@@ -248,12 +248,12 @@ void scan_projects(const char *path, const char *projectsfile)
projectsfile, strerror(errno), errno);
return;
}
- while (strbuf_getline(&line, projects) != EOF) {
+ while (strbuf_getline(&line, projects, '\n') != EOF) {
if (!line.len)
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 +263,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);
}