ui-shared: add homepage to tabs
[cgit.git] / scan-tree.c
blob2e87999b11b8f10fdbeab0c9b2b20e22b7b46184
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"
14 /* return 1 if path contains a objects/ directory and a HEAD file */
15 static int is_git_dir(const char *path)
17 struct stat st;
18 struct strbuf pathbuf = STRBUF_INIT;
19 int result = 0;
21 strbuf_addf(&pathbuf, "%s/objects", path);
22 if (stat(pathbuf.buf, &st)) {
23 if (errno != ENOENT)
24 fprintf(stderr, "Error checking path %s: %s (%d)\n",
25 path, strerror(errno), errno);
26 goto out;
28 if (!S_ISDIR(st.st_mode))
29 goto out;
31 strbuf_reset(&pathbuf);
32 strbuf_addf(&pathbuf, "%s/HEAD", path);
33 if (stat(pathbuf.buf, &st)) {
34 if (errno != ENOENT)
35 fprintf(stderr, "Error checking path %s: %s (%d)\n",
36 path, strerror(errno), errno);
37 goto out;
39 if (!S_ISREG(st.st_mode))
40 goto out;
42 result = 1;
43 out:
44 strbuf_release(&pathbuf);
45 return result;
48 static struct cgit_repo *repo;
49 static repo_config_fn config_fn;
51 static void repo_config(const char *name, const char *value)
53 config_fn(repo, name, value);
56 static int gitconfig_config(const char *key, const char *value, void *cb)
58 if (!strcmp(key, "gitweb.owner"))
59 config_fn(repo, "owner", value);
60 else if (!strcmp(key, "gitweb.description"))
61 config_fn(repo, "desc", value);
62 else if (!strcmp(key, "gitweb.category"))
63 config_fn(repo, "section", value);
64 else if (!strcmp(key, "gitweb.homepage"))
65 config_fn(repo, "homepage", value);
66 else if (starts_with(key, "cgit."))
67 config_fn(repo, key + 5, value);
69 return 0;
72 static char *xstrrchr(char *s, char *from, int c)
74 while (from >= s && *from != c)
75 from--;
76 return from < s ? NULL : from;
79 static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
81 struct stat st;
82 struct passwd *pwd;
83 size_t pathlen;
84 struct strbuf rel = STRBUF_INIT;
85 char *p, *slash;
86 int n;
87 size_t size;
89 if (stat(path->buf, &st)) {
90 fprintf(stderr, "Error accessing %s: %s (%d)\n",
91 path->buf, strerror(errno), errno);
92 return;
95 strbuf_addch(path, '/');
96 pathlen = path->len;
98 if (ctx.cfg.strict_export) {
99 strbuf_addstr(path, ctx.cfg.strict_export);
100 if(stat(path->buf, &st))
101 return;
102 strbuf_setlen(path, pathlen);
105 strbuf_addstr(path, "noweb");
106 if (!stat(path->buf, &st))
107 return;
108 strbuf_setlen(path, pathlen);
110 if (!starts_with(path->buf, base))
111 strbuf_addbuf(&rel, path);
112 else
113 strbuf_addstr(&rel, path->buf + strlen(base) + 1);
115 if (!strcmp(rel.buf + rel.len - 5, "/.git"))
116 strbuf_setlen(&rel, rel.len - 5);
117 else if (rel.len && rel.buf[rel.len - 1] == '/')
118 strbuf_setlen(&rel, rel.len - 1);
120 repo = cgit_add_repo(rel.buf);
121 config_fn = fn;
122 if (ctx.cfg.enable_git_config) {
123 strbuf_addstr(path, "config");
124 git_config_from_file(gitconfig_config, path->buf, NULL);
125 strbuf_setlen(path, pathlen);
128 if (ctx.cfg.remove_suffix) {
129 size_t urllen;
130 strip_suffix(repo->url, ".git", &urllen);
131 strip_suffix_mem(repo->url, &urllen, "/");
132 repo->url[urllen] = '\0';
134 repo->path = xstrdup(path->buf);
135 while (!repo->owner) {
136 if ((pwd = getpwuid(st.st_uid)) == NULL) {
137 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
138 path->buf, strerror(errno), errno);
139 break;
141 if (pwd->pw_gecos)
142 if ((p = strchr(pwd->pw_gecos, ',')))
143 *p = '\0';
144 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
147 if (repo->desc == cgit_default_repo_desc || !repo->desc) {
148 strbuf_addstr(path, "description");
149 if (!stat(path->buf, &st))
150 readfile(path->buf, &repo->desc, &size);
151 strbuf_setlen(path, pathlen);
154 if (ctx.cfg.section_from_path) {
155 n = ctx.cfg.section_from_path;
156 if (n > 0) {
157 slash = rel.buf - 1;
158 while (slash && n && (slash = strchr(slash + 1, '/')))
159 n--;
160 } else {
161 slash = rel.buf + rel.len;
162 while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/')))
163 n++;
165 if (slash && !n) {
166 *slash = '\0';
167 repo->section = xstrdup(rel.buf);
168 *slash = '/';
169 if (starts_with(repo->name, repo->section)) {
170 repo->name += strlen(repo->section);
171 if (*repo->name == '/')
172 repo->name++;
177 strbuf_addstr(path, "cgitrc");
178 if (!stat(path->buf, &st))
179 parse_configfile(path->buf, &repo_config);
181 strbuf_release(&rel);
184 static void scan_path(const char *base, const char *path, repo_config_fn fn)
186 DIR *dir = opendir(path);
187 struct dirent *ent;
188 struct strbuf pathbuf = STRBUF_INIT;
189 size_t pathlen = strlen(path);
190 struct stat st;
192 if (!dir) {
193 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
194 path, strerror(errno), errno);
195 return;
198 strbuf_add(&pathbuf, path, strlen(path));
199 if (is_git_dir(pathbuf.buf)) {
200 add_repo(base, &pathbuf, fn);
201 goto end;
203 strbuf_addstr(&pathbuf, "/.git");
204 if (is_git_dir(pathbuf.buf)) {
205 add_repo(base, &pathbuf, fn);
206 goto end;
209 * Add one because we don't want to lose the trailing '/' when we
210 * reset the length of pathbuf in the loop below.
212 pathlen++;
213 while ((ent = readdir(dir)) != NULL) {
214 if (ent->d_name[0] == '.') {
215 if (ent->d_name[1] == '\0')
216 continue;
217 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
218 continue;
219 if (!ctx.cfg.scan_hidden_path)
220 continue;
222 strbuf_setlen(&pathbuf, pathlen);
223 strbuf_addstr(&pathbuf, ent->d_name);
224 if (stat(pathbuf.buf, &st)) {
225 fprintf(stderr, "Error checking path %s: %s (%d)\n",
226 pathbuf.buf, strerror(errno), errno);
227 continue;
229 if (S_ISDIR(st.st_mode))
230 scan_path(base, pathbuf.buf, fn);
232 end:
233 strbuf_release(&pathbuf);
234 closedir(dir);
237 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
239 struct strbuf line = STRBUF_INIT;
240 FILE *projects;
241 int err;
243 projects = fopen(projectsfile, "r");
244 if (!projects) {
245 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
246 projectsfile, strerror(errno), errno);
247 return;
249 while (strbuf_getline(&line, projects, '\n') != EOF) {
250 if (!line.len)
251 continue;
252 strbuf_insert(&line, 0, "/", 1);
253 strbuf_insert(&line, 0, path, strlen(path));
254 scan_path(path, line.buf, fn);
256 if ((err = ferror(projects))) {
257 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
258 projectsfile, strerror(err), err);
260 fclose(projects);
261 strbuf_release(&line);
264 void scan_tree(const char *path, repo_config_fn fn)
266 scan_path(path, path, fn);