config: make empty js= omit script tag
[cgit.git] / scan-tree.c
blob6a2f65a86b2cda7826df470836f9757ba86abf98
1 /* scan-tree.c
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
9 #include "cgit.h"
10 #include "scan-tree.h"
11 #include "configfile.h"
12 #include "html.h"
13 #include <config.h>
15 /* return 1 if path contains a objects/ directory and a HEAD file */
16 static int is_git_dir(const char *path)
18 struct stat st;
19 struct strbuf pathbuf = STRBUF_INIT;
20 int result = 0;
22 strbuf_addf(&pathbuf, "%s/objects", path);
23 if (stat(pathbuf.buf, &st)) {
24 if (errno != ENOENT)
25 fprintf(stderr, "Error checking path %s: %s (%d)\n",
26 path, strerror(errno), errno);
27 goto out;
29 if (!S_ISDIR(st.st_mode))
30 goto out;
32 strbuf_reset(&pathbuf);
33 strbuf_addf(&pathbuf, "%s/HEAD", path);
34 if (stat(pathbuf.buf, &st)) {
35 if (errno != ENOENT)
36 fprintf(stderr, "Error checking path %s: %s (%d)\n",
37 path, strerror(errno), errno);
38 goto out;
40 if (!S_ISREG(st.st_mode))
41 goto out;
43 result = 1;
44 out:
45 strbuf_release(&pathbuf);
46 return result;
49 static struct cgit_repo *repo;
50 static repo_config_fn config_fn;
52 static void scan_tree_repo_config(const char *name, const char *value)
54 config_fn(repo, name, value);
57 static int gitconfig_config(const char *key, const char *value, void *cb)
59 const char *name;
61 if (!strcmp(key, "gitweb.owner"))
62 config_fn(repo, "owner", value);
63 else if (!strcmp(key, "gitweb.description"))
64 config_fn(repo, "desc", value);
65 else if (!strcmp(key, "gitweb.category"))
66 config_fn(repo, "section", value);
67 else if (!strcmp(key, "gitweb.homepage"))
68 config_fn(repo, "homepage", value);
69 else if (skip_prefix(key, "cgit.", &name))
70 config_fn(repo, name, value);
72 return 0;
75 static char *xstrrchr(char *s, char *from, int c)
77 while (from >= s && *from != c)
78 from--;
79 return from < s ? NULL : from;
82 static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
84 struct stat st;
85 struct passwd *pwd;
86 size_t pathlen;
87 struct strbuf rel = STRBUF_INIT;
88 char *p, *slash;
89 int n;
90 size_t size;
92 if (stat(path->buf, &st)) {
93 fprintf(stderr, "Error accessing %s: %s (%d)\n",
94 path->buf, strerror(errno), errno);
95 return;
98 strbuf_addch(path, '/');
99 pathlen = path->len;
101 if (ctx.cfg.strict_export) {
102 strbuf_addstr(path, ctx.cfg.strict_export);
103 if(stat(path->buf, &st))
104 return;
105 strbuf_setlen(path, pathlen);
108 strbuf_addstr(path, "noweb");
109 if (!stat(path->buf, &st))
110 return;
111 strbuf_setlen(path, pathlen);
113 if (!starts_with(path->buf, base))
114 strbuf_addbuf(&rel, path);
115 else
116 strbuf_addstr(&rel, path->buf + strlen(base) + 1);
118 if (!strcmp(rel.buf + rel.len - 5, "/.git"))
119 strbuf_setlen(&rel, rel.len - 5);
120 else if (rel.len && rel.buf[rel.len - 1] == '/')
121 strbuf_setlen(&rel, rel.len - 1);
123 repo = cgit_add_repo(rel.buf);
124 config_fn = fn;
125 if (ctx.cfg.enable_git_config) {
126 strbuf_addstr(path, "config");
127 git_config_from_file(gitconfig_config, path->buf, NULL);
128 strbuf_setlen(path, pathlen);
131 if (ctx.cfg.remove_suffix) {
132 size_t urllen;
133 strip_suffix(repo->url, ".git", &urllen);
134 strip_suffix_mem(repo->url, &urllen, "/");
135 repo->url[urllen] = '\0';
137 repo->path = xstrdup(path->buf);
138 while (!repo->owner) {
139 if ((pwd = getpwuid(st.st_uid)) == NULL) {
140 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
141 path->buf, strerror(errno), errno);
142 break;
144 if (pwd->pw_gecos)
145 if ((p = strchr(pwd->pw_gecos, ',')))
146 *p = '\0';
147 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
150 if (repo->desc == cgit_default_repo_desc || !repo->desc) {
151 strbuf_addstr(path, "description");
152 if (!stat(path->buf, &st))
153 readfile(path->buf, &repo->desc, &size);
154 strbuf_setlen(path, pathlen);
157 if (ctx.cfg.section_from_path) {
158 n = ctx.cfg.section_from_path;
159 if (n > 0) {
160 slash = rel.buf - 1;
161 while (slash && n && (slash = strchr(slash + 1, '/')))
162 n--;
163 } else {
164 slash = rel.buf + rel.len;
165 while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/')))
166 n++;
168 if (slash && !n) {
169 *slash = '\0';
170 repo->section = xstrdup(rel.buf);
171 *slash = '/';
172 if (starts_with(repo->name, repo->section)) {
173 repo->name += strlen(repo->section);
174 if (*repo->name == '/')
175 repo->name++;
180 strbuf_addstr(path, "cgitrc");
181 if (!stat(path->buf, &st))
182 parse_configfile(path->buf, &scan_tree_repo_config);
184 strbuf_release(&rel);
187 static void scan_path(const char *base, const char *path, repo_config_fn fn)
189 DIR *dir = opendir(path);
190 struct dirent *ent;
191 struct strbuf pathbuf = STRBUF_INIT;
192 size_t pathlen = strlen(path);
193 struct stat st;
195 if (!dir) {
196 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
197 path, strerror(errno), errno);
198 return;
201 strbuf_add(&pathbuf, path, strlen(path));
202 if (is_git_dir(pathbuf.buf)) {
203 add_repo(base, &pathbuf, fn);
204 goto end;
206 strbuf_addstr(&pathbuf, "/.git");
207 if (is_git_dir(pathbuf.buf)) {
208 add_repo(base, &pathbuf, fn);
209 goto end;
212 * Add one because we don't want to lose the trailing '/' when we
213 * reset the length of pathbuf in the loop below.
215 pathlen++;
216 while ((ent = readdir(dir)) != NULL) {
217 if (ent->d_name[0] == '.') {
218 if (ent->d_name[1] == '\0')
219 continue;
220 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
221 continue;
222 if (!ctx.cfg.scan_hidden_path)
223 continue;
225 strbuf_setlen(&pathbuf, pathlen);
226 strbuf_addstr(&pathbuf, ent->d_name);
227 if (stat(pathbuf.buf, &st)) {
228 fprintf(stderr, "Error checking path %s: %s (%d)\n",
229 pathbuf.buf, strerror(errno), errno);
230 continue;
232 if (S_ISDIR(st.st_mode))
233 scan_path(base, pathbuf.buf, fn);
235 end:
236 strbuf_release(&pathbuf);
237 closedir(dir);
240 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
242 struct strbuf line = STRBUF_INIT;
243 FILE *projects;
244 int err;
246 projects = fopen(projectsfile, "r");
247 if (!projects) {
248 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
249 projectsfile, strerror(errno), errno);
250 return;
252 while (strbuf_getline(&line, projects) != EOF) {
253 if (!line.len)
254 continue;
255 strbuf_insert(&line, 0, "/", 1);
256 strbuf_insert(&line, 0, path, strlen(path));
257 scan_path(path, line.buf, fn);
259 if ((err = ferror(projects))) {
260 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
261 projectsfile, strerror(err), err);
263 fclose(projects);
264 strbuf_release(&line);
267 void scan_tree(const char *path, repo_config_fn fn)
269 scan_path(path, path, fn);