3 * Copyright (C) 2008-2009 Lars Hjemli
4 * Copyright (C) 2010 Jason A. Donenfeld <Jason@zx2c4.com>
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
11 #include "configfile.h"
16 /* return 1 if path contains a objects/ directory and a HEAD file */
17 static int is_git_dir(const char *path)
20 static char buf[MAX_PATH];
22 if (snprintf(buf, MAX_PATH, "%s/objects", path) >= MAX_PATH) {
23 fprintf(stderr, "Insanely long path: %s\n", path);
28 fprintf(stderr, "Error checking path %s: %s (%d)\n",
29 path, strerror(errno), errno);
32 if (!S_ISDIR(st.st_mode))
35 sprintf(buf, "%s/HEAD", path);
38 fprintf(stderr, "Error checking path %s: %s (%d)\n",
39 path, strerror(errno), errno);
42 if (!S_ISREG(st.st_mode))
48 struct cgit_repo *repo;
49 repo_config_fn config_fn;
53 static void repo_config(const char *name, const char *value)
55 config_fn(repo, name, value);
58 static int gitweb_config(const char *key, const char *value, void *cb)
60 if (ctx.cfg.enable_gitweb_owner && !strcmp(key, "gitweb.owner"))
61 owner = xstrdup(value);
62 else if (ctx.cfg.enable_gitweb_desc && !strcmp(key, "gitweb.description"))
63 desc = xstrdup(value);
69 static char *xstrrchr(char *s, char *from, int c)
71 while (from >= s && *from != c)
73 return from < s ? NULL : from;
76 static void add_repo(const char *base, const char *path, repo_config_fn fn)
80 char *rel, *p, *slash;
84 if (stat(path, &st)) {
85 fprintf(stderr, "Error accessing %s: %s (%d)\n",
86 path, strerror(errno), errno);
90 if (ctx.cfg.strict_export && stat(fmt("%s/%s", path, ctx.cfg.strict_export), &st))
93 if (!stat(fmt("%s/noweb", path), &st))
98 git_config_from_file(gitweb_config, fmt("%s/config", path), NULL);
101 rel = xstrdup(fmt("%s", path));
103 rel = xstrdup(fmt("%s", path + strlen(base) + 1));
105 if (!strcmp(rel + strlen(rel) - 5, "/.git"))
106 rel[strlen(rel) - 5] = '\0';
108 repo = cgit_add_repo(rel);
109 if (ctx.cfg.remove_suffix)
110 if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
112 repo->name = repo->url;
113 repo->path = xstrdup(path);
115 if ((pwd = getpwuid(st.st_uid)) == NULL) {
116 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
117 path, strerror(errno), errno);
121 if ((p = strchr(pwd->pw_gecos, ',')))
123 owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
130 p = fmt("%s/description", path);
132 readfile(p, &repo->desc, &size);
136 p = fmt("%s/README.html", path);
138 repo->readme = "README.html";
140 if (ctx.cfg.section_from_path) {
141 n = ctx.cfg.section_from_path;
144 while (slash && n && (slash = strchr(slash, '/')))
147 slash = rel + strlen(rel);
148 while (slash && n && (slash = xstrrchr(rel, slash, '/')))
153 repo->section = xstrdup(rel);
155 if (!prefixcmp(repo->name, repo->section)) {
156 repo->name += strlen(repo->section);
157 if (*repo->name == '/')
163 p = fmt("%s/cgitrc", path);
166 parse_configfile(xstrdup(p), &repo_config);
172 static void scan_path(const char *base, const char *path, repo_config_fn fn)
174 DIR *dir = opendir(path);
180 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
181 path, strerror(errno), errno);
184 if (is_git_dir(path)) {
185 add_repo(base, path, fn);
188 if (is_git_dir(fmt("%s/.git", path))) {
189 add_repo(base, fmt("%s/.git", path), fn);
192 while((ent = readdir(dir)) != NULL) {
193 if (ent->d_name[0] == '.') {
194 if (ent->d_name[1] == '\0')
196 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
198 if (!ctx.cfg.scan_hidden_path)
201 buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
203 fprintf(stderr, "Alloc error on %s: %s (%d)\n",
204 path, strerror(errno), errno);
207 sprintf(buf, "%s/%s", path, ent->d_name);
208 if (stat(buf, &st)) {
209 fprintf(stderr, "Error checking path %s: %s (%d)\n",
210 buf, strerror(errno), errno);
214 if (S_ISDIR(st.st_mode))
215 scan_path(base, buf, fn);
222 #define lastc(s) s[strlen(s) - 1]
224 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
226 char line[MAX_PATH * 2], *z;
230 projects = fopen(projectsfile, "r");
232 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
233 projectsfile, strerror(errno), errno);
236 while (fgets(line, sizeof(line), projects) != NULL) {
237 for (z = &lastc(line);
238 strlen(line) && strchr("\n\r", *z);
242 scan_path(path, fmt("%s/%s", path, line), fn);
244 if ((err = ferror(projects))) {
245 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
246 projectsfile, strerror(err), err);
251 void scan_tree(const char *path, repo_config_fn fn)
253 scan_path(path, path, fn);