ui-shared: extract date formatting to a function
[cgit.git] / scan-tree.c
blob8e3cf52b348f2df6436246103e7c2280926c2c99
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 (starts_with(key, "cgit."))
65 config_fn(repo, key + 5, value);
67 return 0;
70 static char *xstrrchr(char *s, char *from, int c)
72 while (from >= s && *from != c)
73 from--;
74 return from < s ? NULL : from;
77 static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
79 struct stat st;
80 struct passwd *pwd;
81 size_t pathlen;
82 struct strbuf rel = STRBUF_INIT;
83 char *p, *slash;
84 int n;
85 size_t size;
87 if (stat(path->buf, &st)) {
88 fprintf(stderr, "Error accessing %s: %s (%d)\n",
89 path->buf, strerror(errno), errno);
90 return;
93 strbuf_addch(path, '/');
94 pathlen = path->len;
96 if (ctx.cfg.strict_export) {
97 strbuf_addstr(path, ctx.cfg.strict_export);
98 if(stat(path->buf, &st))
99 return;
100 strbuf_setlen(path, pathlen);
103 strbuf_addstr(path, "noweb");
104 if (!stat(path->buf, &st))
105 return;
106 strbuf_setlen(path, pathlen);
108 if (!starts_with(path->buf, base))
109 strbuf_addbuf(&rel, path);
110 else
111 strbuf_addstr(&rel, path->buf + strlen(base) + 1);
113 if (!strcmp(rel.buf + rel.len - 5, "/.git"))
114 strbuf_setlen(&rel, rel.len - 5);
115 else if (rel.len && rel.buf[rel.len - 1] == '/')
116 strbuf_setlen(&rel, rel.len - 1);
118 repo = cgit_add_repo(rel.buf);
119 config_fn = fn;
120 if (ctx.cfg.enable_git_config) {
121 strbuf_addstr(path, "config");
122 git_config_from_file(gitconfig_config, path->buf, NULL);
123 strbuf_setlen(path, pathlen);
126 if (ctx.cfg.remove_suffix) {
127 size_t urllen;
128 strip_suffix(repo->url, ".git", &urllen);
129 strip_suffix_mem(repo->url, &urllen, "/");
130 repo->url[urllen] = '\0';
132 repo->path = xstrdup(path->buf);
133 while (!repo->owner) {
134 if ((pwd = getpwuid(st.st_uid)) == NULL) {
135 fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
136 path->buf, strerror(errno), errno);
137 break;
139 if (pwd->pw_gecos)
140 if ((p = strchr(pwd->pw_gecos, ',')))
141 *p = '\0';
142 repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name);
145 if (repo->desc == cgit_default_repo_desc || !repo->desc) {
146 strbuf_addstr(path, "description");
147 if (!stat(path->buf, &st))
148 readfile(path->buf, &repo->desc, &size);
149 strbuf_setlen(path, pathlen);
152 if (ctx.cfg.section_from_path) {
153 n = ctx.cfg.section_from_path;
154 if (n > 0) {
155 slash = rel.buf - 1;
156 while (slash && n && (slash = strchr(slash + 1, '/')))
157 n--;
158 } else {
159 slash = rel.buf + rel.len;
160 while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/')))
161 n++;
163 if (slash && !n) {
164 *slash = '\0';
165 repo->section = xstrdup(rel.buf);
166 *slash = '/';
167 if (starts_with(repo->name, repo->section)) {
168 repo->name += strlen(repo->section);
169 if (*repo->name == '/')
170 repo->name++;
175 strbuf_addstr(path, "cgitrc");
176 if (!stat(path->buf, &st))
177 parse_configfile(xstrdup(path->buf), &repo_config);
179 strbuf_release(&rel);
182 static void scan_path(const char *base, const char *path, repo_config_fn fn)
184 DIR *dir = opendir(path);
185 struct dirent *ent;
186 struct strbuf pathbuf = STRBUF_INIT;
187 size_t pathlen = strlen(path);
188 struct stat st;
190 if (!dir) {
191 fprintf(stderr, "Error opening directory %s: %s (%d)\n",
192 path, strerror(errno), errno);
193 return;
196 strbuf_add(&pathbuf, path, strlen(path));
197 if (is_git_dir(pathbuf.buf)) {
198 add_repo(base, &pathbuf, fn);
199 goto end;
201 strbuf_addstr(&pathbuf, "/.git");
202 if (is_git_dir(pathbuf.buf)) {
203 add_repo(base, &pathbuf, fn);
204 goto end;
207 * Add one because we don't want to lose the trailing '/' when we
208 * reset the length of pathbuf in the loop below.
210 pathlen++;
211 while ((ent = readdir(dir)) != NULL) {
212 if (ent->d_name[0] == '.') {
213 if (ent->d_name[1] == '\0')
214 continue;
215 if (ent->d_name[1] == '.' && ent->d_name[2] == '\0')
216 continue;
217 if (!ctx.cfg.scan_hidden_path)
218 continue;
220 strbuf_setlen(&pathbuf, pathlen);
221 strbuf_addstr(&pathbuf, ent->d_name);
222 if (stat(pathbuf.buf, &st)) {
223 fprintf(stderr, "Error checking path %s: %s (%d)\n",
224 pathbuf.buf, strerror(errno), errno);
225 continue;
227 if (S_ISDIR(st.st_mode))
228 scan_path(base, pathbuf.buf, fn);
230 end:
231 strbuf_release(&pathbuf);
232 closedir(dir);
235 void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
237 struct strbuf line = STRBUF_INIT;
238 FILE *projects;
239 int err;
241 projects = fopen(projectsfile, "r");
242 if (!projects) {
243 fprintf(stderr, "Error opening projectsfile %s: %s (%d)\n",
244 projectsfile, strerror(errno), errno);
245 return;
247 while (strbuf_getline(&line, projects, '\n') != EOF) {
248 if (!line.len)
249 continue;
250 strbuf_insert(&line, 0, "/", 1);
251 strbuf_insert(&line, 0, path, strlen(path));
252 scan_path(path, line.buf, fn);
254 if ((err = ferror(projects))) {
255 fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
256 projectsfile, strerror(err), err);
258 fclose(projects);
259 strbuf_release(&line);
262 void scan_tree(const char *path, repo_config_fn fn)
264 scan_path(path, path, fn);